1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #include <iostream> 18 #include <string> 19 20 #define private public 21 #include "tls.h" 22 #include "tls_key.h" 23 24 namespace OHOS { 25 namespace NetStack { 26 namespace TlsSocket { 27 namespace { 28 using namespace testing::ext; 29 constexpr int FILE_READ_KEY_LEN = 4096; 30 static char g_keyFile[] = 31 "-----BEGIN RSA PRIVATE KEY-----\r\n" 32 "MIIEowIBAAKCAQEAqVzrf6PkLu0uhp5yl2HPNm0vLyI1KLqgsdz5s+JvVdbPXNxD\r\n" 33 "g6fmdwa64tJXZPKx7i1KwNs/Jx3xv1N6rqB0au+Ku0Zdq7zbMCqej63SbFW1XWvQ\r\n" 34 "6RJ76GcitgrFMTlQN4AzfX0xLFaUJHRuDS4QC5UE9CmV3kD09BNgItu/hxPAHSwg\r\n" 35 "q6myc1uufYCwCUIV3bzxd65M343zubTlwOSmsCSqQIl8C1Gd6NWT69tL4fq2hHc/\r\n" 36 "09VAlcLvugztwM6NHwDCmRFEDz3RdRahAvCEde8OkY/Aor6UucYWzCJofLeyKVQg\r\n" 37 "6J3CTsT/zUE6pdKTvuhQbpRCtWKWSa7qDv1WywIDAQABAoIBAFGpbCPvcmbuFjDy\r\n" 38 "1W4Iy1EC9G1VoSwyUKlyUzRZSjWpjfLIggVJP+bEZ/hWU61pGEIvtIupK5pA5f/K\r\n" 39 "0KzC0V9+gPYrx563QTjIVAwTVBLIgNq60dCQCQ7WK/Z62voRGIyqVCl94+ftFyE8\r\n" 40 "wpO4UiRD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+CM/Vsx9cvBZw\r\n" 55 "hi15LA5+hEdbgvj87hmMiCOc75e0oz2Rd12ZoRlBVfbncH9ngfqBNQElM7Bueqoc\r\n" 56 "JOpKV+gw0gQtiu4beIdFnYsdZoZwrTjC4rW7OI0WYoLJabMFFh3I\r\n" 57 "-----END RSA PRIVATE KEY-----\r\n"; 58 } // namespace 59 60 class TlsKeyTest : public testing::Test { 61 public: SetUpTestCase()62 static void SetUpTestCase() {} 63 TearDownTestCase()64 static void TearDownTestCase() {} 65 SetUp()66 virtual void SetUp() {} 67 TearDown()68 virtual void TearDown() {} 69 }; 70 71 HWTEST_F(TlsKeyTest, AlgorithmTest, TestSize.Level2) 72 { 73 SecureData structureData(g_keyFile); 74 std::string keyPassStr = ""; 75 SecureData keyPass(keyPassStr); 76 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 77 KeyAlgorithm algorithm = tlsKey.Algorithm(); 78 EXPECT_EQ(algorithm, ALGORITHM_RSA); 79 } 80 81 HWTEST_F(TlsKeyTest, DecodePemTest, TestSize.Level2) 82 { 83 std::string fileName = ""; 84 std::string keyPassStr = ""; 85 SecureData keyPass(keyPassStr); 86 TLSKey tlsKey1 = TLSKey(fileName, ALGORITHM_RSA, keyPass); 87 TLSKey tlsKey2 = TLSKey(fileName, ALGORITHM_RSA, keyPass, EncodingFormat::DER); 88 std::string fileName2 = "/system/lib"; 89 TLSKey tlsKey3 = TLSKey(fileName2, ALGORITHM_RSA, keyPass); 90 TLSKey tlsKey4 = TLSKey(fileName2, ALGORITHM_RSA, keyPass, EncodingFormat::DER); 91 SecureData structureData(g_keyFile); 92 structureData.length_ = sizeof(g_keyFile); 93 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 94 EXPECT_EQ(tlsKey.keyIsNull_, false); 95 } 96 97 HWTEST_F(TlsKeyTest, CopyConstruction, TestSize.Level2) 98 { 99 SecureData structureData(g_keyFile); 100 std::string keyPassStr = ""; 101 SecureData keyPass(keyPassStr); 102 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 103 TLSKey tlsKeyCopy = TLSKey(tlsKey); 104 KeyAlgorithm algorithm = tlsKeyCopy.Algorithm(); 105 EXPECT_EQ(algorithm, ALGORITHM_RSA); 106 } 107 108 HWTEST_F(TlsKeyTest, AssignmentConstruction, TestSize.Level2) 109 { 110 SecureData structureData(g_keyFile); 111 std::string keyPassStr = ""; 112 SecureData keyPass(keyPassStr); 113 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 114 TLSKey key = tlsKey; 115 KeyAlgorithm algorithm = key.Algorithm(); 116 EXPECT_EQ(algorithm, ALGORITHM_RSA); 117 } 118 119 HWTEST_F(TlsKeyTest, HandleTest, TestSize.Level2) 120 { 121 SecureData structureData(g_keyFile); 122 std::string keyPassStr = ""; 123 SecureData keyPass(keyPassStr); 124 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 125 Handle handle = tlsKey.handle(); 126 EXPECT_NE(handle, nullptr); 127 tlsKey.Clear(true); 128 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 129 Handle handleDsa = tlsKeyDsa.handle(); 130 EXPECT_EQ(handleDsa, nullptr); 131 tlsKeyDsa.Clear(true); 132 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 133 Handle handleEc = tlsKeyEc.handle(); 134 EXPECT_EQ(handleEc, nullptr); 135 tlsKeyEc.Clear(true); 136 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 137 Handle handleDh = tlsKeyDh.handle(); 138 EXPECT_EQ(handleDh, nullptr); 139 tlsKeyDh.Clear(true); 140 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 141 Handle handleOpaque = tlsKeyOpaque.handle(); 142 EXPECT_EQ(handleOpaque, nullptr); 143 tlsKeyOpaque.Clear(true); 144 TLSKey keyOpaque = tlsKeyOpaque; 145 EXPECT_NE(handle, nullptr); 146 } 147 148 HWTEST_F(TlsKeyTest, GetKeyPassTest, TestSize.Level2) 149 { 150 SecureData structureData(g_keyFile); 151 std::string keyPassStr = ""; 152 SecureData keyPass(keyPassStr); 153 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 154 SecureData getKeyPass = tlsKey.GetKeyPass(); 155 EXPECT_EQ(getKeyPass.Length(), keyPass.Length()); 156 } 157 158 HWTEST_F(TlsKeyTest, GetKeyDataTest, TestSize.Level2) 159 { 160 SecureData structureData(g_keyFile); 161 std::string keyPassStr = ""; 162 SecureData keyPass(keyPassStr); 163 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 164 SecureData getKeyData = tlsKey.GetKeyData(); 165 EXPECT_EQ(getKeyData.Length(), structureData.Length()); 166 } 167 168 HWTEST_F(TlsKeyTest, AlgorithmTest2, TestSize.Level2) 169 { 170 SecureData structureData(g_keyFile); 171 std::string keyPassStr = ""; 172 SecureData keyPass(keyPassStr); 173 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 174 TLSKey keyDsa = tlsKeyDsa; 175 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 176 TLSKey keyEc = tlsKeyEc; 177 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 178 TLSKey keyDh = tlsKeyDh; 179 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 180 TLSKey keyOpaque = tlsKeyOpaque; 181 SecureData getKeyData = tlsKeyDsa.GetKeyData(); 182 EXPECT_EQ(getKeyData.Length(), structureData.Length()); 183 } 184 185 HWTEST_F(TlsKeyTest, SwitchAlgorithmTest, TestSize.Level2) 186 { 187 SecureData structureData(g_keyFile); 188 std::string keyPassStr = ""; 189 190 KeyType typePublic = KeyType::PUBLIC_KEY; 191 SecureData keyPass(keyPassStr); 192 KeyType typePrivate = KeyType::PRIVATE_KEY; 193 char privateKey[FILE_READ_KEY_LEN] = {0}; 194 const char *privateKeyData = static_cast<const char *>(privateKey); 195 BIO *bio = BIO_new_mem_buf(privateKeyData, -1); 196 TLSKey tlsKeyRsa = TLSKey(structureData, ALGORITHM_RSA, keyPass); 197 tlsKeyRsa.SwitchAlgorithm(typePrivate, ALGORITHM_RSA, bio); 198 tlsKeyRsa.SwitchAlgorithm(typePublic, ALGORITHM_RSA, bio); 199 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 200 tlsKeyDsa.SwitchAlgorithm(typePrivate, ALGORITHM_DSA, bio); 201 tlsKeyDsa.SwitchAlgorithm(typePublic, ALGORITHM_DSA, bio); 202 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 203 tlsKeyEc.SwitchAlgorithm(typePrivate, ALGORITHM_EC, bio); 204 tlsKeyEc.SwitchAlgorithm(typePublic, ALGORITHM_EC, bio); 205 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 206 tlsKeyDh.SwitchAlgorithm(typePrivate, ALGORITHM_DH, bio); 207 tlsKeyDh.SwitchAlgorithm(typePublic, ALGORITHM_DH, bio); 208 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 209 tlsKeyOpaque.SwitchAlgorithm(typePrivate, OPAQUE, bio); 210 tlsKeyOpaque.SwitchAlgorithm(typePublic, OPAQUE, bio); 211 SecureData getKeyData = tlsKeyDsa.GetKeyData(); 212 EXPECT_EQ(getKeyData.Length(), structureData.Length()); 213 } 214 215 HWTEST_F(TlsKeyTest, ClearTest, TestSize.Level2) 216 { 217 SecureData structureData(g_keyFile); 218 std::string keyPassStr = ""; 219 SecureData keyPass(keyPassStr); 220 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 221 tlsKeyDsa.dsa_ = DSA_new(); 222 tlsKeyDsa.Clear(true); 223 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 224 tlsKeyDh.dh_ = DH_new(); 225 tlsKeyDh.Clear(true); 226 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 227 tlsKeyEc.ec_ = EC_KEY_new(); 228 tlsKeyEc.Clear(true); 229 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 230 tlsKeyOpaque.genericKey_ = EVP_PKEY_new(); 231 tlsKeyOpaque.Clear(true); 232 EXPECT_EQ(tlsKeyOpaque.keyIsNull_, true); 233 } 234 235 HWTEST_F(TlsKeyTest, DecodeDataTest, TestSize.Level2) 236 { 237 SecureData data; 238 std::string keyPassStr = ""; 239 SecureData keyPass(keyPassStr); 240 TLSKey tlsKey = TLSKey(data, ALGORITHM_DSA, keyPass); 241 tlsKey.DecodeData(data, ALGORITHM_DSA, keyPass); 242 SecureData getKeyData = tlsKey.GetKeyData(); 243 EXPECT_EQ(getKeyData.Length(), data.Length()); 244 } 245 } // namespace TlsSocket 246 } // namespace NetStack 247 } // namespace OHOS