• 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 {
TLSConfiguration(const TLSConfiguration & other)26 TLSConfiguration::TLSConfiguration(const TLSConfiguration &other)
27 {
28     privateKey_ = other.privateKey_;
29     localCertificate_ = other.localCertificate_;
30     caCertificate_ = other.caCertificate_;
31     minProtocol_ = other.minProtocol_;
32     maxProtocol_ = other.maxProtocol_;
33     cipherSuite_ = other.cipherSuite_;
34 }
35 
PrivateKey() const36 const TLSKey &TLSConfiguration::PrivateKey() const
37 {
38     return privateKey_;
39 }
40 
operator =(const TLSConfiguration & other)41 TLSConfiguration &TLSConfiguration::operator=(const TLSConfiguration &other)
42 {
43     privateKey_ = other.privateKey_;
44     localCertificate_ = other.localCertificate_;
45     caCertificate_ = other.caCertificate_;
46     minProtocol_ = other.minProtocol_;
47     maxProtocol_ = other.maxProtocol_;
48     cipherSuite_ = other.cipherSuite_;
49     caCertificateChain_ = other.caCertificateChain_;
50     signatureAlgorithms_ = other.signatureAlgorithms_;
51     privateKey_ = other.privateKey_;
52     return *this;
53 }
54 
SetLocalCertificate(const TLSCertificate & certificate)55 void TLSConfiguration::SetLocalCertificate(const TLSCertificate &certificate)
56 {
57     localCertificate_ = certificate;
58 }
59 
SetCaCertificate(const TLSCertificate & certificate)60 void TLSConfiguration::SetCaCertificate(const TLSCertificate &certificate)
61 {
62     caCertificate_ = certificate;
63 }
64 
SetPrivateKey(const TLSKey & key)65 void TLSConfiguration::SetPrivateKey(const TLSKey &key)
66 {
67     privateKey_ = key;
68 }
69 
SetPrivateKey(const SecureData & key,const SecureData & keyPass)70 void TLSConfiguration::SetPrivateKey(const SecureData &key, const SecureData &keyPass)
71 {
72     TLSKey pkey(key, ALGORITHM_RSA, keyPass);
73     privateKey_ = pkey;
74 }
75 
SetLocalCertificate(const std::string & certificate)76 void TLSConfiguration::SetLocalCertificate(const std::string &certificate)
77 {
78     TLSCertificate local(certificate, LOCAL_CERT);
79     localCertificate_ = local;
80 }
81 
SetCaCertificate(const std::vector<std::string> & certificate)82 void TLSConfiguration::SetCaCertificate(const std::vector<std::string> &certificate)
83 {
84     caCertificateChain_ = certificate;
85 }
86 
SetProtocol(const std::vector<std::string> & Protocol)87 void TLSConfiguration::SetProtocol(const std::vector<std::string> &Protocol)
88 {
89     bool isTls1_3 = false;
90     bool isTls1_2 = false;
91     for (const auto &p : Protocol) {
92         if (p == PROTOCOL_TLS_V13) {
93             maxProtocol_ = TLS_V1_3;
94             isTls1_3 = true;
95         }
96         if (p == PROTOCOL_TLS_V12) {
97             minProtocol_ = TLS_V1_2;
98             isTls1_2 = true;
99         }
100     }
101     if (!isTls1_3) {
102         maxProtocol_ = TLS_V1_2;
103     }
104     if (!isTls1_2) {
105         minProtocol_ = TLS_V1_3;
106     }
107     protocol_ = maxProtocol_;
108 }
109 
GetMinProtocol() const110 TLSProtocol TLSConfiguration::GetMinProtocol() const
111 {
112     return minProtocol_;
113 }
114 
GetMaxProtocol() const115 TLSProtocol TLSConfiguration::GetMaxProtocol() const
116 {
117     return maxProtocol_;
118 }
119 
GetProtocol() const120 TLSProtocol TLSConfiguration::GetProtocol() const
121 {
122     return protocol_;
123 }
124 
GetCipherSuite() const125 std::string TLSConfiguration::GetCipherSuite() const
126 {
127     return cipherSuite_;
128 }
129 
GetCipherSuiteVec() const130 std::vector<CipherSuite> TLSConfiguration::GetCipherSuiteVec() const
131 {
132     return cipherSuiteVec_;
133 }
134 
GetCertificate() const135 const X509CertRawData &TLSConfiguration::GetCertificate() const
136 {
137     return localCertificate_.GetLocalCertRawData();
138 }
139 
SetCipherSuite(const std::string & cipherSuite)140 void TLSConfiguration::SetCipherSuite(const std::string &cipherSuite)
141 {
142     cipherSuite_ = cipherSuite;
143 }
144 
SetSignatureAlgorithms(const std::string & signatureAlgorithms)145 void TLSConfiguration::SetSignatureAlgorithms(const std::string &signatureAlgorithms)
146 {
147     signatureAlgorithms_ = signatureAlgorithms;
148 }
149 
GetSignatureAlgorithms() const150 const std::string &TLSConfiguration::GetSignatureAlgorithms() const
151 {
152     return signatureAlgorithms_;
153 }
154 
SetUseRemoteCipherPrefer(bool useRemoteCipherPrefer)155 void TLSConfiguration::SetUseRemoteCipherPrefer(bool useRemoteCipherPrefer)
156 {
157     useRemoteCipherPrefer_ = useRemoteCipherPrefer;
158 }
159 
GetUseRemoteCipherPrefer() const160 bool TLSConfiguration::GetUseRemoteCipherPrefer() const
161 {
162     return useRemoteCipherPrefer_;
163 }
164 
GetCaCertificate() const165 std::vector<std::string> TLSConfiguration::GetCaCertificate() const
166 {
167     return caCertificateChain_;
168 }
169 
GetLocalCertificate() const170 TLSCertificate TLSConfiguration::GetLocalCertificate() const
171 {
172     return localCertificate_;
173 }
174 
GetPrivateKey() const175 TLSKey TLSConfiguration::GetPrivateKey() const
176 {
177     return privateKey_;
178 }
179 } // namespace NetStack
180 } // namespace OHOS
181