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