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 #include "tls_configuration.h" 21 #include "tls.h" 22 23 namespace OHOS { 24 namespace NetStack { 25 namespace TlsSocket { 26 namespace { 27 using namespace testing::ext; 28 static char g_clientFile[] = 29 "-----BEGIN CERTIFICATE-----\r\n" 30 "MIIDezCCAmMCFD6h5R4QvySV9q9mC6s31qQFLX14MA0GCSqGSIb3DQEBCwUAMHgx\r\n" 31 "CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJHRDELMAkGA1UEBwwCU1oxDDAKBgNVBAoM\r\n" 32 "A0NPTTEMMAoGA1UECwwDTlNQMQswCQYDVQQDDAJDQTEmMCQGCSqGSIb3DQEJARYX\r\n" 33 "emhhbmd6aGV3ZWkwMTAzQDE2My5jb20wHhcNMjIwNDI0MDIwMjU3WhcNMjMwNDI0\r\n" 34 "MDIwMjU3WjB8MQswCQYDVQQGEwJDTjELMAkGA1UECAwCR0QxCzAJBgNVBAcMAlNa\r\n" 35 "MQwwCgYDVQQKDANDT00xDDAKBgNVBAsMA05TUDEPMA0GA1UEAwwGQ0xJRU5UMSYw\r\n" 36 "JAYJKoZIhvcNAQkBFhd6aGFuZ3poZXdlaTAxMDNAMTYzLmNvbTCCASIwDQYJKoZI\r\n" 37 "hvcNAQEBBQADggEPADCCAQoCggEBAKlc63+j5C7tLoaecpdhzzZtLy8iNSi6oLHc\r\n" 38 "+bPib1XWz1zcQ4On5ncGuuLSV2Tyse4tSsDbPycd8b9Teq6gdGrvirtGXau82zAq\r\n" 39 "no+t0mxVtV1r0OkSe+hnIrYKxTE5UDeAM319MSxWlCR0bg0uEAuVBPQpld5A9PQT\r\n" 40 "YCLbv4cTwB0sIKupsnNbrn2AsAlCFd288XeuTN+N87m05cDkprAkqkCJfAtRnejV\r\n" 41 "k+vbS+H6toR3P9PVQJXC77oM7cDOjR8AwpkRRA890XUWoQLwhHXvDpGPwKK+lLnG\r\n" 42 "FswiaHy3silUIOidwk7E/81BOqXSk77oUG6UQrVilkmu6g79VssCAwEAATANBgkq\r\n" 43 "hkiG9w0BAQsFAAOCAQEAOeqp+hFVRs4YB3UjU/3bvAUFQLS97gapCp2lk6jS88jt\r\n" 44 "uNeyvwulOAtZEbcoIIvzzNxvBDOVibTJ6gZU9P9g0WyRu2RTgy+UggNwH8u8KZzM\r\n" 45 "DT8sxuoYvRcEWbOhlNQgACa7AlQSLQifo8nvEMS2i9o8WHoHu42MRDYOHYVIwWXH\r\n" 46 "h6mZzfo+zrPyv3NFlwlWqaNiTGgnGCXzlVK3p5YYqLbNVYpy0U5FBxQ7fITsqcbK\r\n" 47 "PusAAEZzPxm8Epo647M28gNkdEEM/7bqhSTJO+jfkojgyQt2ghlw+NGCmG4dJGZb\r\n" 48 "yA7Z3PBj8aqEwmRUF8SAR1bxWBGk2IYRwgStuwvusg==\r\n" 49 "-----END CERTIFICATE-----\r\n"; 50 51 static char g_caCrtFile[] = 52 "Certificate:\r\n" 53 " Data:\r\n" 54 " Version: 3 (0x2)\r\n" 55 " Serial Number: 1 (0x1)\r\n" 56 " Signature Algorithm: sha256WithRSAEncryption\r\n" 57 " Issuer: C=CN, ST=beijing, O=ahaha Inc, OU=Root CA, CN=ahaha CA\r\n" 58 " Validity\r\n" 59 " Not Before: Aug 23 07:33:55 2022 GMT\r\n" 60 " Not After : Aug 23 07:33:55 2023 GMT\r\n" 61 " Subject: C=CN, ST=beijing, O=ahaha Inc, OU=Root CA, CN=ahaha CA\r\n" 62 " Subject Public Key Info:\r\n" 63 " Public Key Algorithm: rsaEncryption\r\n" 64 " RSA Public-Key: (2048 bit)\r\n" 65 " Modulus:\r\n" 66 " 00:9d:df:68:f7:7b:78:0b:21:f3:6f:24:60:ef:ce:\r\n" 67 " 02:90:24:df:c4:d3:f3:e4:26:6c:c7:12:bf:28:cd:\r\n" 68 " 38:2d:3f:ab:76:11:64:ce:6b:f6:07:fd:35:1e:b9:\r\n" 69 " ec:22:72:03:4d:eb:d2:94:49:2d:82:44:6c:72:59:\r\n" 70 " 14:ab:e7:0c:72:32:3e:ad:fa:9d:52:da:24:8d:e9:\r\n" 71 " a4:10:d7:dd:34:66:df:7e:e0:0e:66:53:8b:ee:91:\r\n" 72 " 07:9a:ce:2a:85:25:09:77:3d:5f:75:1c:a1:b3:ab:\r\n" 73 " 86:3b:21:28:f8:43:aa:f0:0b:7d:4d:f9:df:85:33:\r\n" 74 " 4a:3b:ff:e4:03:59:25:62:a1:e9:da:92:63:02:93:\r\n" 75 " bd:f9:df:6e:c6:57:a7:d2:e6:7b:37:14:a9:ba:69:\r\n" 76 " 71:0c:c5:4f:66:fe:67:66:5c:8d:d7:04:4d:d8:f3:\r\n" 77 " 0b:c0:0b:7d:49:eb:68:94:28:f6:31:0f:0d:2a:03:\r\n" 78 " 70:a7:97:f9:38:90:36:d4:4b:39:4b:53:a5:2c:32:\r\n" 79 " 72:f2:41:86:32:13:3c:40:2d:3f:e8:63:d3:8c:8a:\r\n" 80 " 83:79:d3:20:f6:bc:cd:97:3e:94:91:4e:3c:74:8d:\r\n" 81 " 9a:fa:29:de:c4:a5:f7:e1:e2:06:55:e6:6c:41:0f:\r\n" 82 " 60:3b:90:de:3a:84:ef:3a:77:79:27:00:23:55:66:\r\n" 83 " ca:81\r\n" 84 " Exponent: 65537 (0x10001)\r\n" 85 " X509v3 extensions:\r\n" 86 " X509v3 Basic Constraints:\r\n" 87 " CA:TRUE\r\n" 88 " Signature Algorithm: sha256WithRSAEncryption\r\n" 89 " 61:3e:39:71:7f:b1:50:dd:71:97:cd:dc:a9:4b:72:96:0a:12:\r\n" 90 " c1:18:fd:35:b5:e0:97:1b:76:58:22:8d:cd:75:51:0f:ba:04:\r\n" 91 " 00:94:6a:46:d5:3a:c5:ac:ea:7d:9c:ec:6f:19:b6:f1:2b:06:\r\n" 92 " e9:bb:cb:49:24:34:0b:55:bd:02:19:24:19:85:bb:e4:a4:80:\r\n" 93 " f4:d6:90:82:7e:81:5c:9b:89:d4:15:ed:3a:b7:a2:37:59:40:\r\n" 94 " db:b4:18:25:90:2e:ae:82:f9:a8:0c:9d:bd:c7:8c:54:85:ed:\r\n" 95 " 07:d1:70:1d:ee:a1:92:bd:12:97:83:4d:9e:9e:b7:01:b5:56:\r\n" 96 " a5:1f:31:6e:a1:48:68:a4:4f:1c:fa:b0:38:27:47:12:eb:55:\r\n" 97 " a3:45:f7:e3:18:ba:d7:85:3c:1f:2c:1e:5e:38:75:5e:80:8a:\r\n" 98 " fd:1c:84:4f:9b:ef:85:b7:79:89:d7:43:eb:d4:fb:c5:51:5b:\r\n" 99 " 84:6f:0e:06:32:54:13:e4:a7:e2:20:2d:b8:fa:2d:09:f8:8f:\r\n" 100 " dd:01:19:39:cc:23:c0:d1:39:19:9a:f7:7c:53:63:bf:ea:be:\r\n" 101 " 04:9b:af:3e:6e:1e:77:c8:b9:0b:78:e9:0e:62:a7:51:db:1e:\r\n" 102 " c0:63:4d:4d:14:ff:ca:44:7f:15:e4:fa:98:1e:3d:58:c2:b6:\r\n" 103 " 5a:64:68:d0\r\n" 104 "-----BEGIN CERTIFICATE-----\r\n" 105 "MIIDazCCAlOgAwIBAgIBATANBgkqhkiG9w0BAQsFADBwMQswCQYDVQQGEwJDTjEQ\r\n" 106 "MA4GA1UECAwHYmVpamluZzEdMBsGA1UECgwUR2xvYmFsIEdvb2dsZSBDQSBJbmMx\r\n" 107 "EDAOBgNVBAsMB1Jvb3QgQ0ExHjAcBgNVBAMMFUdsb2JhbCBHb29nbGUgUm9vdCBD\r\n" 108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YqdR2x7AY01NFP/K\r\n" 123 "RH8V5PqYHj1YwrZaZGjQ\r\n" 124 "-----END CERTIFICATE-----\r\n"; 125 126 static char g_priKeyFile[] = 127 "-----BEGIN RSA PRIVATE KEY-----" 128 "MIIEowIBAAKCAQEAqVzrf6PkLu0uhp5yl2HPNm0vLyI1KLqgsdz5s+JvVdbPXNxD" 129 "g6fmdwa64tJXZPKx7i1KwNs/Jx3xv1N6rqB0au+Ku0Zdq7zbMCqej63SbFW1XWvQ" 130 "6RJ76GcitgrFMTlQN4AzfX0xLFaUJHRuDS4QC5UE9CmV3kD09BNgItu/hxPAHSwg" 131 "q6myc1uufYCwCUIV3bzxd65M343zubTlwOSmsCSqQIl8C1Gd6NWT69tL4fq2hHc/" 132 "09VAlcLvugztwM6NHwDCmRFEDz3RdRahAvCEde8OkY/Aor6UucYWzCJofLeyKVQg" 133 "6J3CTsT/zUE6pdKTvuhQbpRCtWKWSa7qDv1WywIDAQABAoIBAFGpbCPvcmbuFjDy" 134 "1W4Iy1EC9G1VoSwyUKlyUzRZSjWpjfLIggVJP+bEZ/hWU61pGEIvtIupK5pA5f/K" 135 "0KzC0V9+gPYrx563QTjIVAwTVBLIgNq60dCQCQ7WK/Z62voRGIyqVCl94+ftFyE8" 136 "wpO4UiRDhk/0fT7dMz882G32ZzNJmY9eHu+yOaRctJW2gRBROHpQfDGBCz7w8s2j" 137 "ulIcnvwGOrvVllsL+vgY95M0LOq0W8ObbUSlawTnNTSRxFL68Hz5EaVJ19EYvEcC" 138 "eWnpEqIfF8OhQ+mYbdrAutXCkqJLz3rdu5P2Lbk5Ht5ETfr7rtUzvb4+ExIcxVOs" 139 "eys8EgECgYEA29tTxJOy2Cb4DKB9KwTErD1sFt9Ed+Z/A3RGmnM+/h75DHccqS8n" 140 "g9DpvHVMcMWYFVYGlEHC1F+bupM9CgxqQcVhGk/ysJ5kXF6lSTnOQxORnku3HXnV" 141 "4QzgKtLfHbukW1Y2RZM3aCz+Hg+bJrpacWyWZ4tRWNYsO58JRaubZjsCgYEAxTSP" 142 "yUBleQejl5qO76PGUUs2W8+GPr492NJGb63mEiM1zTYLVN0uuDJ2JixzHb6o1NXZ" 143 "6i00pSksT3+s0eiBTRnF6BJ0y/8J07ZnfQQXRAP8ypiZtd3jdOnUxEHfBw2QaIdP" 144 "tVdUc2mpIhosAYT9sWpHYvlUqTCdeLwhkYfgeLECgYBoajjVcmQM3i0OKiZoCOKy" 145 "/pTYI/8rho+p/04MylEPdXxIXEWDYD6/DrgDZh4ArQc2kt2bCcRTAnk+WfEyVYUd" 146 "aXVdfry+/uqhJ94N8eMw3hlZeZIk8JkQQgIwtGd8goJjUoWB85Hr6vphIn5IHVcY" 147 "6T5hPLxMmaL2SeioawDpwwKBgQCFXjDH6Hc3zQTEKND2HIqou/b9THH7yOlG056z" 148 "NKZeKdXe/OfY8uT/yZDB7FnGCgVgO2huyTfLYvcGpNAZ/eZEYGPJuYGn3MmmlruS" 149 "fsvFQfUahu2dY3zKusEcIXhV6sR5DNnJSFBi5VhvKcgNFwYDkF7K/thUu/4jgwgo" 150 "xf33YQKBgDQffkP1jWqT/pzlVLFtF85/3eCC/uedBfxXknVMrWE+CM/Vsx9cvBZw" 151 "hi15LA5+hEdbgvj87hmMiCOc75e0oz2Rd12ZoRlBVfbncH9ngfqBNQElM7Bueqoc" 152 "JOpKV+gw0gQtiu4beIdFnYsdZoZwrTjC4rW7OI0WYoLJabMFFh3I" 153 "-----END RSA PRIVATE KEY-----"; 154 } // namespace 155 156 class TlsConfigurationTest : public testing::Test { 157 public: SetUpTestCase()158 static void SetUpTestCase() {} 159 TearDownTestCase()160 static void TearDownTestCase() {} 161 SetUp()162 virtual void SetUp() {} 163 TearDown()164 virtual void TearDown() {} 165 }; 166 167 HWTEST_F(TlsConfigurationTest, AssignmentConstruction, TestSize.Level2) 168 { 169 TLSConfiguration tlsConfiguration; 170 TLSConfiguration configuration = tlsConfiguration; 171 configuration.SetLocalCertificate(g_clientFile); 172 TLSCertificate tlsCertificate = configuration.GetLocalCertificate(); 173 EXPECT_NE(tlsCertificate.handle(), nullptr); 174 X509CertRawData x509CertRawData = configuration.GetCertificate(); 175 EXPECT_NE(x509CertRawData.data.Length(), 0); 176 } 177 178 HWTEST_F(TlsConfigurationTest, CopyConstruction, TestSize.Level2) 179 { 180 TLSConfiguration tlsConfiguration; 181 tlsConfiguration.SetLocalCertificate(g_clientFile); 182 TLSConfiguration configuration = TLSConfiguration(tlsConfiguration); 183 TLSCertificate tlsCertificate = configuration.GetLocalCertificate(); 184 EXPECT_NE(tlsCertificate.handle(), nullptr); 185 } 186 187 HWTEST_F(TlsConfigurationTest, SetAndGetCa, TestSize.Level2) 188 { 189 TLSConfiguration tlsConfiguration; 190 tlsConfiguration.SetLocalCertificate(g_clientFile); 191 std::vector<std::string> certificate; 192 certificate.push_back(g_caCrtFile); 193 tlsConfiguration.SetCaCertificate(certificate); 194 std::vector<std::string> getCaCertificate; 195 getCaCertificate = tlsConfiguration.GetCaCertificate(); 196 EXPECT_NE(getCaCertificate.size(), 0); 197 } 198 199 HWTEST_F(TlsConfigurationTest, SetPrivateKey, TestSize.Level2) 200 { 201 TLSConfiguration tlsConfiguration; 202 tlsConfiguration.SetLocalCertificate(g_clientFile); 203 SecureData structureData(g_priKeyFile); 204 std::string keyPassStr = ""; 205 SecureData keyPass(keyPassStr); 206 tlsConfiguration.SetPrivateKey(structureData, keyPass); 207 TLSKey tlsKey = tlsConfiguration.GetPrivateKey(); 208 SecureData tlsKeyData = tlsKey.GetKeyData(); 209 EXPECT_EQ(tlsKeyData.Length(), strlen(g_priKeyFile)); 210 } 211 212 HWTEST_F(TlsConfigurationTest, SetProtocol, TestSize.Level2) 213 { 214 TLSConfiguration tlsConfiguration; 215 std::vector<std::string> protocol; 216 std::string protocolVer = "TLSv1.3"; 217 protocol.push_back(protocolVer); 218 tlsConfiguration.SetProtocol(protocol); 219 TLSProtocol tlsProtocol = tlsConfiguration.GetProtocol(); 220 EXPECT_EQ(tlsProtocol, TLS_V1_3); 221 TLSProtocol minProtocol = tlsConfiguration.GetMinProtocol(); 222 EXPECT_EQ(minProtocol, TLS_V1_3); 223 TLSProtocol maxProtocol = tlsConfiguration.GetMaxProtocol(); 224 EXPECT_EQ(maxProtocol, TLS_V1_3); 225 226 protocol.clear(); 227 protocolVer = "TLSv1.2"; 228 protocol.push_back(protocolVer); 229 tlsConfiguration.SetProtocol(protocol); 230 tlsProtocol = tlsConfiguration.GetProtocol(); 231 EXPECT_EQ(tlsProtocol, TLS_V1_2); 232 minProtocol = tlsConfiguration.GetMinProtocol(); 233 EXPECT_EQ(minProtocol, TLS_V1_2); 234 maxProtocol = tlsConfiguration.GetMaxProtocol(); 235 EXPECT_EQ(maxProtocol, TLS_V1_2); 236 } 237 238 HWTEST_F(TlsConfigurationTest, UseRemoteCipherPrefer, TestSize.Level2) 239 { 240 TLSConfiguration tlsConfiguration; 241 tlsConfiguration.SetUseRemoteCipherPrefer(true); 242 bool isUsePemoteCipherPrefer = tlsConfiguration.GetUseRemoteCipherPrefer(); 243 EXPECT_TRUE(isUsePemoteCipherPrefer); 244 } 245 246 HWTEST_F(TlsConfigurationTest, CipherSuite, TestSize.Level2) 247 { 248 TLSConfiguration tlsConfiguration; 249 std::string cipherSuite = "AES256-SHA256"; 250 tlsConfiguration.SetCipherSuite(cipherSuite); 251 std::string getCipherSuite; 252 getCipherSuite = tlsConfiguration.GetCipherSuite(); 253 std::cout << "getCipherSuite:" << getCipherSuite << std::endl; 254 int idx = getCipherSuite.find(cipherSuite); 255 EXPECT_NE(idx, std::string::npos); 256 } 257 258 HWTEST_F(TlsConfigurationTest, SignatureAlgorithms, TestSize.Level2) 259 { 260 TLSConfiguration tlsConfiguration; 261 std::string signatureAlgorithms = "rsa_pss_rsae_sha256:ECDSA+SHA256"; 262 tlsConfiguration.SetSignatureAlgorithms(signatureAlgorithms); 263 std::string getSignatureAlgorithms; 264 getSignatureAlgorithms = tlsConfiguration.GetSignatureAlgorithms(); 265 std::cout << "getSignatureAlgorithms:" << getSignatureAlgorithms << std::endl; 266 std::string subStr = "ECDSA+SHA256"; 267 int idx = getSignatureAlgorithms.find(subStr); 268 EXPECT_NE(idx, std::string::npos); 269 } 270 } // namespace TlsSocket 271 } // namespace NetStack 272 } // namespace OHOS 273