1 /*
2 * Copyright (c) 2024-2024 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 #include <iostream>
16 #include <openssl/x509_vfy.h>
17
18 #include "signer_factory.h"
19 #include "localization_adapter.h"
20 #include "signer_config.h"
21
22 namespace OHOS {
23 namespace SignatureTools {
SignerConfig()24 SignerConfig::SignerConfig() : options(nullptr),
25 certificates(nullptr),
26 x509CRLs(nullptr),
27 signer(nullptr),
28 compatibleVersion(0)
29 {
30 }
31
~SignerConfig()32 SignerConfig::~SignerConfig()
33 {
34 if (certificates) {
35 sk_X509_pop_free(certificates, X509_free);
36 }
37 certificates = NULL;
38
39 if (x509CRLs) {
40 sk_X509_CRL_pop_free(x509CRLs, X509_CRL_free);
41 }
42 x509CRLs = NULL;
43 }
44
GetOptions() const45 Options* SignerConfig::GetOptions() const
46 {
47 return options;
48 }
49
SetOptions(Options * optionsParam)50 void SignerConfig::SetOptions(Options* optionsParam)
51 {
52 options = optionsParam;
53 }
54
STACK_OF(X509)55 STACK_OF(X509)* SignerConfig::GetCertificates() const
56 {
57 if (IsInputCertChainNotEmpty() || signer == nullptr) {
58 return certificates;
59 }
60 return signer->GetCertificates();
61 }
62
SetCertificates(STACK_OF (X509)* certificatesParam)63 void SignerConfig::SetCertificates(STACK_OF(X509)* certificatesParam)
64 {
65 certificates = certificatesParam;
66 }
67
STACK_OF(X509_CRL)68 STACK_OF(X509_CRL)* SignerConfig::GetX509CRLs() const
69 {
70 if (IsInputCertChainNotEmpty() || IsInputCrlNotEmpty() || signer == nullptr) {
71 return x509CRLs;
72 }
73 return signer->GetCrls();
74 }
75
SetX509CRLs(STACK_OF (X509_CRL)* crls)76 void SignerConfig::SetX509CRLs(STACK_OF(X509_CRL)* crls)
77 {
78 x509CRLs = crls;
79 }
80
GetSignatureAlgorithms() const81 std::vector<SignatureAlgorithmHelper> SignerConfig::GetSignatureAlgorithms() const
82 {
83 return signatureAlgorithms;
84 }
85
SetSignatureAlgorithms(const std::vector<SignatureAlgorithmHelper> & signatureAlgorithmsParam)86 void SignerConfig::SetSignatureAlgorithms(const std::vector<SignatureAlgorithmHelper>& signatureAlgorithmsParam)
87 {
88 signatureAlgorithms = signatureAlgorithmsParam;
89 }
90
GetSignParamMap() const91 const std::map<std::string, std::string>& SignerConfig::GetSignParamMap() const
92 {
93 return signParamMap;
94 }
95
FillParameters(const std::map<std::string,std::string> & params)96 void SignerConfig::FillParameters(const std::map<std::string, std::string>& params)
97 {
98 signParamMap = params;
99 }
100
GetSigner()101 std::shared_ptr<Signer> SignerConfig::GetSigner()
102 {
103 std::lock_guard<std::mutex> lockGuard(signerMtx);
104 if (signer == nullptr) {
105 SignerFactory factory;
106 LocalizationAdapter adapter(options);
107 signer = factory.GetSigner(adapter);
108 }
109 return signer;
110 }
111
GetCompatibleVersion() const112 int SignerConfig::GetCompatibleVersion() const
113 {
114 return compatibleVersion;
115 }
116
SetCompatibleVersion(int compatibleVersionParam)117 void SignerConfig::SetCompatibleVersion(int compatibleVersionParam)
118 {
119 compatibleVersion = compatibleVersionParam;
120 }
121
IsInputCertChainNotEmpty() const122 bool SignerConfig::IsInputCertChainNotEmpty() const
123 {
124 return certificates != nullptr;
125 }
126
IsInputCrlNotEmpty() const127 bool SignerConfig::IsInputCrlNotEmpty() const
128 {
129 return x509CRLs != nullptr;
130 }
131 } // namespace SignatureTools
132 } // namespace OHOS