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 <openssl/ssl.h>
21 
22 #define private public
23 #include "tls_context.h"
24 #include "tls.h"
25 #include "TlsTest.h"
26 
27 namespace OHOS {
28 namespace NetStack {
29 namespace TlsSocket {
30 namespace {
31 using namespace testing::ext;
32 constexpr const char *PROTOCOL13 = "TLSv1.3";
33 constexpr const char *PROTOCOL12 = "TLSv1.2";
34 constexpr const char *PROTOCOL11 = "TLSv1.1";
35 constexpr const char *CIPHER_SUITE = "AES256-SHA256";
36 constexpr const char *SIGNATURE_ALGORITHMS = "rsa_pss_rsae_sha256:ECDSA+SHA256";
37 } // namespace
38 
39 class TlsContextTest : public testing::Test {
40 public:
SetUpTestCase()41     static void SetUpTestCase() {}
42 
TearDownTestCase()43     static void TearDownTestCase() {}
44 
SetUp()45     virtual void SetUp() {}
46 
TearDown()47     virtual void TearDown() {}
48 };
49 
50 HWTEST_F(TlsContextTest, ContextTest1, TestSize.Level2)
51 {
52     TLSConfiguration configuration;
53     configuration.SetCipherSuite(CIPHER_SUITE);
54     configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
55     std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
56 
57     EXPECT_NE(tlsContext, nullptr);
58     tlsContext->CloseCtx();
59 }
60 
61 HWTEST_F(TlsContextTest, ContextTest2, TestSize.Level2)
62 {
63     std::vector<std::string> protocol;
64     protocol.push_back(PROTOCOL13);
65     protocol.push_back(PROTOCOL12);
66     protocol.push_back(PROTOCOL11);
67     TLSConfiguration configuration;
68     std::vector<std::string> caVec = {CA_CRT_FILE};
69     configuration.SetCaCertificate(caVec);
70     configuration.SetProtocol(protocol);
71     configuration.SetCipherSuite(CIPHER_SUITE);
72     configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
73     configuration.SetLocalCertificate(CLIENT_FILE);
74     std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
75     EXPECT_NE(tlsContext, nullptr);
76     TLSContext::SetMinAndMaxProtocol(tlsContext.get());
77     bool isInitTlsContext = TLSContext::InitTlsContext(tlsContext.get(), configuration);
78     EXPECT_TRUE(isInitTlsContext);
79     bool isSetCipherList = TLSContext::SetCipherList(tlsContext.get(), configuration);
80     EXPECT_TRUE(isSetCipherList);
81     bool isSetSignatureAlgorithms = TLSContext::SetSignatureAlgorithms(tlsContext.get(), configuration);
82     EXPECT_TRUE(isSetSignatureAlgorithms);
83     TLSContext::GetCiphers(tlsContext.get());
84     TLSContext::UseRemoteCipher(tlsContext.get());
85     bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration);
86     EXPECT_TRUE(setCaAndVerify);
87     bool setLocalCert = TLSContext::SetLocalCertificate(tlsContext.get(), configuration);
88     EXPECT_TRUE(setLocalCert);
89     bool setKeyAndCheck = TLSContext::SetKeyAndCheck(tlsContext.get(), configuration);
90     EXPECT_FALSE(setKeyAndCheck);
91     TLSContext::SetVerify(tlsContext.get());
92     SSL *ssl = tlsContext->CreateSsl();
93     EXPECT_NE(ssl, nullptr);
94     SSL_free(ssl);
95     ssl = nullptr;
96     tlsContext->CloseCtx();
97 }
98 
99 HWTEST_F(TlsContextTest, ContextTest3, TestSize.Level2)
100 {
101     TLSConfiguration configuration;
102     std::vector<std::string> caVec = {};
103     configuration.SetCaCertificate(caVec);
104     std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
105     EXPECT_NE(tlsContext, nullptr);
106     bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration);
107     tlsContext->CloseCtx();
108     EXPECT_TRUE(setCaAndVerify);
109 }
110 
111 HWTEST_F(TlsContextTest, InitTlsContext3, TestSize.Level2)
112 {
113     TLSConfiguration configuration;
114     std::string cipherSuite = "";
115     configuration.SetCipherSuite(cipherSuite);
116     std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
117 
118     EXPECT_NE(tlsContext, nullptr);
119     tlsContext->CloseCtx();
120 }
121 
122 HWTEST_F(TlsContextTest, InitTlsContext4, TestSize.Level2)
123 {
124     TLSConfiguration configuration;
125     std::string signatureAlgorithms = "";
126     configuration.SetCipherSuite(CIPHER_SUITE);
127     configuration.SetSignatureAlgorithms(signatureAlgorithms);
128     std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
129 
130     EXPECT_NE(tlsContext, nullptr);
131     tlsContext->CloseCtx();
132 }
133 
134 HWTEST_F(TlsContextTest, ContextNullTest, TestSize.Level2)
135 {
136     std::vector<std::string> protocol;
137     protocol.push_back(PROTOCOL13);
138     protocol.push_back(PROTOCOL12);
139     protocol.push_back(PROTOCOL11);
140     TLSConfiguration configuration;
141     std::vector<std::string> caVec = {CA_CRT_FILE};
142     configuration.SetCaCertificate(caVec);
143     configuration.SetProtocol(protocol);
144     configuration.SetCipherSuite(CIPHER_SUITE);
145     configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
146     configuration.SetLocalCertificate(CLIENT_FILE);
147     std::unique_ptr<TLSContext> tlsContext = nullptr;
148     EXPECT_EQ(tlsContext, nullptr);
149     TLSContext::SetMinAndMaxProtocol(tlsContext.get());
150     bool isInitTlsContext = TLSContext::InitTlsContext(tlsContext.get(), configuration);
151     EXPECT_FALSE(isInitTlsContext);
152     bool isSetCipherList = TLSContext::SetCipherList(tlsContext.get(), configuration);
153     EXPECT_FALSE(isSetCipherList);
154     bool isSetSignatureAlgorithms = TLSContext::SetSignatureAlgorithms(tlsContext.get(), configuration);
155     EXPECT_FALSE(isSetSignatureAlgorithms);
156     TLSContext::GetCiphers(tlsContext.get());
157     TLSContext::UseRemoteCipher(tlsContext.get());
158     bool setCaAndVerify = TLSContext::SetCaAndVerify(tlsContext.get(), configuration);
159     EXPECT_FALSE(setCaAndVerify);
160     bool setLocalCert = TLSContext::SetLocalCertificate(tlsContext.get(), configuration);
161     EXPECT_FALSE(setLocalCert);
162     bool setKeyAndCheck = TLSContext::SetKeyAndCheck(tlsContext.get(), configuration);
163     EXPECT_FALSE(setKeyAndCheck);
164     TLSContext::SetVerify(tlsContext.get());
165 }
166 
167 HWTEST_F(TlsContextTest, ContextFailTest1, TestSize.Level2)
168 {
169     std::vector<std::string> protocol;
170     protocol.push_back("1.3");
171     protocol.push_back("1.2");
172     TLSConfiguration configuration;
173     std::vector<std::string> caVec = {CA_CRT_FILE};
174     configuration.SetCaCertificate(caVec);
175     configuration.SetProtocol(protocol);
176     configuration.SetCipherSuite(CIPHER_SUITE);
177     configuration.SetSignatureAlgorithms(SIGNATURE_ALGORITHMS);
178     configuration.SetLocalCertificate("certificate");
179     SecureData key("key");
180     SecureData keyPass("123456");
181     configuration.SetPrivateKey(key, keyPass);
182     std::unique_ptr<TLSContext> tlsContext = TLSContext::CreateConfiguration(configuration);
183     EXPECT_NE(tlsContext, nullptr);
184 }
185 } // namespace TlsSocket
186 } // namespace NetStack
187 } // namespace OHOS