1 /*
2  * Copyright (C) 2021 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 #ifndef HAP_PKCS7_CONTEXT_H
16 #define HAP_PKCS7_CONTEXT_H
17 
18 #include <vector>
19 #include <string>
20 
21 #include "openssl/pkcs7.h"
22 #include "openssl/x509.h"
23 
24 #include "common/hap_byte_buffer.h"
25 #include "init/matching_result.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace Verify {
30 using CertChain = std::vector<X509*>;
31 using Pkcs7CertChains = std::vector<CertChain>;
32 
33 struct Pkcs7Context {
34     bool needWriteCrl;
35     int32_t digestAlgorithm = 0;
36     MatchingResult matchResult;
37     std::string certIssuer;
38     PKCS7* p7;
39     Pkcs7CertChains certChains;
40     HapByteBuffer content;
41     std::string rootCa;
42 
Pkcs7ContextPkcs7Context43     Pkcs7Context()
44         : needWriteCrl(false), digestAlgorithm(0), matchResult(), certIssuer(),
45           p7(nullptr), certChains(), content()
46     {
47     }
48 
~Pkcs7ContextPkcs7Context49     ~Pkcs7Context()
50     {
51         if (p7 != nullptr) {
52             PKCS7_free(p7);
53             p7 = nullptr;
54         }
55         for (auto certChain : certChains) {
56             for (auto cert : certChain) {
57                 X509_free(cert);
58             }
59         }
60         certChains.clear();
61     }
62 };
63 } // namespace Verify
64 } // namespace Security
65 } // namespace OHOS
66 #endif // HAP_PKCS7_CONTEXT_H
67