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_key.h" 22 #include "tls.h" 23 24 namespace OHOS { 25 namespace NetStack { 26 namespace { 27 using namespace testing::ext; 28 constexpr int FILE_READ_KEY_LEN = 4096; 29 static char g_keyFile[] = 30 "-----BEGIN RSA PRIVATE KEY-----\r\n" 31 "MIIEowIBAAKCAQEAqVzrf6PkLu0uhp5yl2HPNm0vLyI1KLqgsdz5s+JvVdbPXNxD\r\n" 32 "g6fmdwa64tJXZPKx7i1KwNs/Jx3xv1N6rqB0au+Ku0Zdq7zbMCqej63SbFW1XWvQ\r\n" 33 "6RJ76GcitgrFMTlQN4AzfX0xLFaUJHRuDS4QC5UE9CmV3kD09BNgItu/hxPAHSwg\r\n" 34 "q6myc1uufYCwCUIV3bzxd65M343zubTlwOSmsCSqQIl8C1Gd6NWT69tL4fq2hHc/\r\n" 35 "09VAlcLvugztwM6NHwDCmRFEDz3RdRahAvCEde8OkY/Aor6UucYWzCJofLeyKVQg\r\n" 36 "6J3CTsT/zUE6pdKTvuhQbpRCtWKWSa7qDv1WywIDAQABAoIBAFGpbCPvcmbuFjDy\r\n" 37 "1W4Iy1EC9G1VoSwyUKlyUzRZSjWpjfLIggVJP+bEZ/hWU61pGEIvtIupK5pA5f/K\r\n" 38 "0KzC0V9+gPYrx563QTjIVAwTVBLIgNq60dCQCQ7WK/Z62voRGIyqVCl94+ftFyE8\r\n" 39 "wpO4UiRDhk/0fT7dMz882G32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w\r\n" 54 "hi15LA5+hEdbgvj87hmMiCOc75e0oz2Rd12ZoRlBVfbncH9ngfqBNQElM7Bueqoc\r\n" 55 "JOpKV+gw0gQtiu4beIdFnYsdZoZwrTjC4rW7OI0WYoLJabMFFh3I\r\n" 56 "-----END RSA PRIVATE KEY-----\r\n"; 57 } // namespace 58 59 class TlsKeyTest : public testing::Test { 60 public: SetUpTestCase()61 static void SetUpTestCase() {} 62 TearDownTestCase()63 static void TearDownTestCase() {} 64 SetUp()65 virtual void SetUp() {} 66 TearDown()67 virtual void TearDown() {} 68 }; 69 70 HWTEST_F(TlsKeyTest, AlgorithmTest, TestSize.Level2) 71 { 72 SecureData structureData(g_keyFile); 73 std::string keyPassStr = ""; 74 SecureData keyPass(keyPassStr); 75 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 76 KeyAlgorithm algorithm = tlsKey.Algorithm(); 77 EXPECT_EQ(algorithm, ALGORITHM_RSA); 78 } 79 80 HWTEST_F(TlsKeyTest, CopyConstruction, TestSize.Level2) 81 { 82 SecureData structureData(g_keyFile); 83 std::string keyPassStr = ""; 84 SecureData keyPass(keyPassStr); 85 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 86 TLSKey tlsKeyCopy = TLSKey(tlsKey); 87 KeyAlgorithm algorithm = tlsKeyCopy.Algorithm(); 88 EXPECT_EQ(algorithm, ALGORITHM_RSA); 89 } 90 91 HWTEST_F(TlsKeyTest, AssignmentConstruction, TestSize.Level2) 92 { 93 SecureData structureData(g_keyFile); 94 std::string keyPassStr = ""; 95 SecureData keyPass(keyPassStr); 96 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 97 TLSKey key = tlsKey; 98 KeyAlgorithm algorithm = key.Algorithm(); 99 EXPECT_EQ(algorithm, ALGORITHM_RSA); 100 } 101 102 HWTEST_F(TlsKeyTest, HandleTest, TestSize.Level2) 103 { 104 SecureData structureData(g_keyFile); 105 std::string keyPassStr = ""; 106 SecureData keyPass(keyPassStr); 107 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 108 Handle handle = tlsKey.handle(); 109 EXPECT_NE(handle, nullptr); 110 tlsKey.Clear(true); 111 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 112 Handle handleDsa = tlsKeyDsa.handle(); 113 EXPECT_EQ(handleDsa, nullptr); 114 tlsKeyDsa.Clear(true); 115 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 116 Handle handleEc = tlsKeyEc.handle(); 117 EXPECT_EQ(handleEc, nullptr); 118 tlsKeyEc.Clear(true); 119 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 120 Handle handleDh = tlsKeyDh.handle(); 121 EXPECT_EQ(handleDh, nullptr); 122 tlsKeyDh.Clear(true); 123 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 124 Handle handleOpaque = tlsKeyOpaque.handle(); 125 EXPECT_EQ(handleOpaque, nullptr); 126 tlsKeyOpaque.Clear(true); 127 TLSKey keyOpaque = tlsKeyOpaque; 128 EXPECT_NE(handle, nullptr); 129 } 130 131 HWTEST_F(TlsKeyTest, GetKeyPassTest, TestSize.Level2) 132 { 133 SecureData structureData(g_keyFile); 134 std::string keyPassStr = ""; 135 SecureData keyPass(keyPassStr); 136 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 137 SecureData getKeyPass = tlsKey.GetKeyPass(); 138 EXPECT_EQ(getKeyPass.Length(), keyPass.Length()); 139 } 140 141 HWTEST_F(TlsKeyTest, GetKeyDataTest, TestSize.Level2) 142 { 143 SecureData structureData(g_keyFile); 144 std::string keyPassStr = ""; 145 SecureData keyPass(keyPassStr); 146 TLSKey tlsKey = TLSKey(structureData, ALGORITHM_RSA, keyPass); 147 SecureData getKeyData= tlsKey.GetKeyData(); 148 EXPECT_EQ(getKeyData.Length(), structureData.Length()); 149 } 150 151 HWTEST_F(TlsKeyTest, AlgorithmTest2, TestSize.Level2) 152 { 153 SecureData structureData(g_keyFile); 154 std::string keyPassStr = ""; 155 SecureData keyPass(keyPassStr); 156 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 157 TLSKey keyDsa = tlsKeyDsa; 158 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 159 TLSKey keyEc = tlsKeyEc; 160 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 161 TLSKey keyDh = tlsKeyDh; 162 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 163 TLSKey keyOpaque = tlsKeyOpaque; 164 SecureData getKeyData= tlsKeyDsa.GetKeyData(); 165 EXPECT_EQ(getKeyData.Length(), structureData.Length()); 166 } 167 168 HWTEST_F(TlsKeyTest, SwitchAlgorithmTest, TestSize.Level2) 169 { 170 SecureData structureData(g_keyFile); 171 std::string keyPassStr = ""; 172 173 KeyType typePublic = KeyType::PUBLIC_KEY; 174 SecureData keyPass(keyPassStr); 175 KeyType typePrivate = KeyType::PRIVATE_KEY; 176 char privateKey[FILE_READ_KEY_LEN] = {0}; 177 const char *privateKeyData = static_cast<const char *>(privateKey); 178 BIO *bio = BIO_new_mem_buf(privateKeyData, -1); 179 TLSKey tlsKeyRsa = TLSKey(structureData, ALGORITHM_RSA, keyPass); 180 tlsKeyRsa.SwitchAlgorithm(typePrivate, ALGORITHM_RSA, bio); 181 tlsKeyRsa.SwitchAlgorithm(typePublic, ALGORITHM_RSA, bio); 182 TLSKey tlsKeyDsa = TLSKey(structureData, ALGORITHM_DSA, keyPass); 183 tlsKeyDsa.SwitchAlgorithm(typePrivate, ALGORITHM_DSA, bio); 184 tlsKeyDsa.SwitchAlgorithm(typePublic, ALGORITHM_DSA, bio); 185 TLSKey tlsKeyEc = TLSKey(structureData, ALGORITHM_EC, keyPass); 186 tlsKeyEc.SwitchAlgorithm(typePrivate, ALGORITHM_EC, bio); 187 tlsKeyEc.SwitchAlgorithm(typePublic, ALGORITHM_EC, bio); 188 TLSKey tlsKeyDh = TLSKey(structureData, ALGORITHM_DH, keyPass); 189 tlsKeyDh.SwitchAlgorithm(typePrivate, ALGORITHM_DH, bio); 190 tlsKeyDh.SwitchAlgorithm(typePublic, ALGORITHM_DH, bio); 191 TLSKey tlsKeyOpaque = TLSKey(structureData, OPAQUE, keyPass); 192 tlsKeyOpaque.SwitchAlgorithm(typePrivate, OPAQUE, bio); 193 tlsKeyOpaque.SwitchAlgorithm(typePublic, OPAQUE, bio); 194 SecureData getKeyData= tlsKeyDsa.GetKeyData(); 195 EXPECT_EQ(getKeyData.Length(), structureData.Length()); 196 } 197 } // namespace NetStack 198 } // namespace OHOS