• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tls_configuration.h"
17 
18 #include <openssl/x509.h>
19 
20 #include "secure_data.h"
21 #include "tls.h"
22 #include "tls_key.h"
23 
24 namespace OHOS {
25 namespace NetStack {
26 namespace TlsSocket {
TLSConfiguration(const TLSConfiguration & other)27 TLSConfiguration::TLSConfiguration(const TLSConfiguration &other)
28 {
29     privateKey_ = other.privateKey_;
30     localCertificate_ = other.localCertificate_;
31     caCertificate_ = other.caCertificate_;
32     minProtocol_ = other.minProtocol_;
33     maxProtocol_ = other.maxProtocol_;
34     cipherSuite_ = other.cipherSuite_;
35     tlsVerifyMode_ = other.tlsVerifyMode_;
36 }
37 
PrivateKey() const38 const TLSKey &TLSConfiguration::PrivateKey() const
39 {
40     return privateKey_;
41 }
42 
operator =(const TLSConfiguration & other)43 TLSConfiguration &TLSConfiguration::operator=(const TLSConfiguration &other)
44 {
45     privateKey_ = other.privateKey_;
46     localCertificate_ = other.localCertificate_;
47     caCertificate_ = other.caCertificate_;
48     minProtocol_ = other.minProtocol_;
49     maxProtocol_ = other.maxProtocol_;
50     cipherSuite_ = other.cipherSuite_;
51     caCertificateChain_ = other.caCertificateChain_;
52     signatureAlgorithms_ = other.signatureAlgorithms_;
53     privateKey_ = other.privateKey_;
54     tlsVerifyMode_ = other.tlsVerifyMode_;
55     return *this;
56 }
57 
SetLocalCertificate(const TLSCertificate & certificate)58 void TLSConfiguration::SetLocalCertificate(const TLSCertificate &certificate)
59 {
60     localCertificate_ = certificate;
61 }
62 
SetCaCertificate(const TLSCertificate & certificate)63 void TLSConfiguration::SetCaCertificate(const TLSCertificate &certificate)
64 {
65     caCertificate_ = certificate;
66 }
67 
SetPrivateKey(const TLSKey & key)68 void TLSConfiguration::SetPrivateKey(const TLSKey &key)
69 {
70     privateKey_ = key;
71 }
72 
SetPrivateKey(const SecureData & key,const SecureData & keyPass)73 void TLSConfiguration::SetPrivateKey(const SecureData &key, const SecureData &keyPass)
74 {
75     TLSKey pkey(key, ALGORITHM_RSA, keyPass);
76     privateKey_ = pkey;
77 }
78 
SetLocalCertificate(const std::string & certificate)79 void TLSConfiguration::SetLocalCertificate(const std::string &certificate)
80 {
81     TLSCertificate local(certificate, LOCAL_CERT);
82     localCertificate_ = local;
83 }
84 
SetCaCertificate(const std::vector<std::string> & certificate)85 void TLSConfiguration::SetCaCertificate(const std::vector<std::string> &certificate)
86 {
87     caCertificateChain_ = certificate;
88 }
89 
SetProtocol(const std::vector<std::string> & Protocol)90 void TLSConfiguration::SetProtocol(const std::vector<std::string> &Protocol)
91 {
92     bool isTls1_3 = false;
93     bool isTls1_2 = false;
94     for (const auto &p : Protocol) {
95         if (p == PROTOCOL_TLS_V13) {
96             maxProtocol_ = TLS_V1_3;
97             isTls1_3 = true;
98         }
99         if (p == PROTOCOL_TLS_V12) {
100             minProtocol_ = TLS_V1_2;
101             isTls1_2 = true;
102         }
103     }
104     if (!isTls1_3) {
105         maxProtocol_ = TLS_V1_2;
106     }
107     if (!isTls1_2) {
108         minProtocol_ = TLS_V1_3;
109     }
110     protocol_ = maxProtocol_;
111 }
112 
GetMinProtocol() const113 TLSProtocol TLSConfiguration::GetMinProtocol() const
114 {
115     return minProtocol_;
116 }
117 
GetMaxProtocol() const118 TLSProtocol TLSConfiguration::GetMaxProtocol() const
119 {
120     return maxProtocol_;
121 }
122 
GetProtocol() const123 TLSProtocol TLSConfiguration::GetProtocol() const
124 {
125     return protocol_;
126 }
127 
GetCipherSuite() const128 std::string TLSConfiguration::GetCipherSuite() const
129 {
130     return cipherSuite_;
131 }
132 
GetCipherSuiteVec() const133 std::vector<CipherSuite> TLSConfiguration::GetCipherSuiteVec() const
134 {
135     return cipherSuiteVec_;
136 }
137 
GetCertificate() const138 const X509CertRawData &TLSConfiguration::GetCertificate() const
139 {
140     return localCertificate_.GetLocalCertRawData();
141 }
142 
SetCipherSuite(const std::string & cipherSuite)143 void TLSConfiguration::SetCipherSuite(const std::string &cipherSuite)
144 {
145     cipherSuite_ = cipherSuite;
146 }
147 
SetSignatureAlgorithms(const std::string & signatureAlgorithms)148 void TLSConfiguration::SetSignatureAlgorithms(const std::string &signatureAlgorithms)
149 {
150     signatureAlgorithms_ = signatureAlgorithms;
151 }
152 
GetSignatureAlgorithms() const153 const std::string &TLSConfiguration::GetSignatureAlgorithms() const
154 {
155     return signatureAlgorithms_;
156 }
157 
SetUseRemoteCipherPrefer(bool useRemoteCipherPrefer)158 void TLSConfiguration::SetUseRemoteCipherPrefer(bool useRemoteCipherPrefer)
159 {
160     useRemoteCipherPrefer_ = useRemoteCipherPrefer;
161 }
162 
GetUseRemoteCipherPrefer() const163 bool TLSConfiguration::GetUseRemoteCipherPrefer() const
164 {
165     return useRemoteCipherPrefer_;
166 }
167 
GetCaCertificate() const168 std::vector<std::string> TLSConfiguration::GetCaCertificate() const
169 {
170     return caCertificateChain_;
171 }
172 
GetLocalCertificate() const173 TLSCertificate TLSConfiguration::GetLocalCertificate() const
174 {
175     return localCertificate_;
176 }
177 
GetPrivateKey() const178 TLSKey TLSConfiguration::GetPrivateKey() const
179 {
180     return privateKey_;
181 }
SetVerifyMode(VerifyMode verifyMode)182 void TLSConfiguration::SetVerifyMode(VerifyMode verifyMode)
183 {
184     tlsVerifyMode_ = verifyMode;
185 }
GetVerifyMode() const186 VerifyMode TLSConfiguration::GetVerifyMode() const
187 {
188     return tlsVerifyMode_;
189 }
190 } // namespace TlsSocket
191 } // namespace NetStack
192 } // namespace OHOS
193