• 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 #ifndef COMMUNICATION_NETSTACK_TLS_CERTIFICATE_H
17 #define COMMUNICATION_NETSTACK_TLS_CERTIFICATE_H
18 
19 #include <list>
20 #include <memory>
21 #include <string>
22 
23 #include <openssl/x509.h>
24 
25 #include "tls.h"
26 
27 namespace OHOS {
28 namespace NetStack {
29 class TLSCertificate {
30 public:
31     TLSCertificate() = default;
32     TLSCertificate(const std::string &data, EncodingFormat format = PEM, CertType certType = CA_CERT);
33     TLSCertificate(const std::string &data, CertType certType);
34     ~TLSCertificate() = default;
35 
36     TLSCertificate(const TLSCertificate &other);
37     TLSCertificate &operator=(const TLSCertificate &other);
38 
39     bool CertificateFromData(const std::string &data, CertType certType);
40     bool CertificateFromPem(const std::string &data, CertType certType);
41     bool CertificateFromDer(const std::string &data, CertType certType);
42     bool CaCertToString(X509 *x509);
43     bool LocalCertToString(X509 *x509);
44     std::string GetLocalCertString() const;
45     std::string GetSignatureAlgorithm() const;
46     const X509CertRawData &GetLocalCertRawData() const;
47 
48     Handle handle() const;
49 
50 private:
51     bool SetSerialNumber(X509 *x509);
52     bool SetX509Version(X509 *x509);
53     bool SetNotValidTime(X509 *x509);
54     bool SetSignatureAlgorithm(X509 *x509);
55     bool AnalysisCertificate(CertType certType, X509 *x509);
56     bool SetLocalCertRawData(X509 *x509);
57 
58 private:
59     X509 *x509_ = nullptr;
60     std::string version_;
61     std::string serialNumber_;
62     std::string signatureAlgorithm_;
63     std::string localCertString_;
64     std::string caCertString_;
65     X509CertRawData rawData_;
66 };
67 } // namespace NetStack
68 } // namespace OHOS
69 #endif // COMMUNICATION_NETSTACK_TLS_CERTIFICATE_H
70