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 
16 #ifndef SIGNATRUETOOLS_LOCALIIZATION_ADAPTER_H
17 #define SIGNATRUETOOLS_LOCALIIZATION_ADAPTER_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "openssl/ssl.h"
23 #include "openssl/pem.h"
24 #include "openssl/err.h"
25 #include "options.h"
26 #include "key_store_helper.h"
27 #include "cert_dn_utils.h"
28 #include "signature_tools_log.h"
29 #include "digest_common.h"
30 
31 namespace OHOS {
32 namespace SignatureTools {
33 class LocalizationAdapter {
34 public:
35     LocalizationAdapter() = default;
36     LocalizationAdapter(Options* options);
37     ~LocalizationAdapter() = default;
38 
39     int IsAliasExist(const std::string& alias);
40     int GetKeyPair(bool autoCreate, EVP_PKEY** keyPair);
41     int IssuerKeyStoreFile(EVP_PKEY** keyPair, bool autoCreate);
42     int KeyStoreFile(EVP_PKEY** keyPair, bool autoCreate);
43 
44     void ResetPwd();
45     void SetIssuerKeyStoreFile(bool issuerKeyStoreFile);
46     void AppAndProfileAssetsRealse(std::initializer_list<EVP_PKEY*> keys,
47                                    std::initializer_list<X509_REQ*> reqs,
48                                    std::initializer_list<X509*> certs);
49 
50     bool IsOutFormChain();
51     bool IsRemoteSigner();
52 
53     const std::string GetSignAlg() const;
54     const std::string GetOutFile();
55     const std::string GetInFile();
56 
57     Options* GetOptions();
58     EVP_PKEY* GetAliasKey(bool autoCreate);
59     EVP_PKEY* GetIssuerKeyByAlias();
60     X509* GetSubCaCertFile();
61     X509* GetCaCertFile();
62     STACK_OF(X509*) GetSignCertChain();
63     std::vector<X509*> GetCertsFromFile(std::string& certPath, const std::string& logTitle);
64 
65 private:
66     void ResetChars(char* chars);
67 
68 public:
69     Options* options;
70     std::unique_ptr<KeyStoreHelper> keyStoreHelper;
71 
72 private:
73     static constexpr int MIN_CERT_CHAIN_SIZE = 2;
74     static constexpr int MAX_CERT_CHAIN_SIZE = 3;
75     bool isIssuerKeyStoreFile;
76 };
77 } // namespace SignatureTools
78 } // namespace OHOS
79 #endif // SIGNATRUETOOLS_LOCALIIZATION_ADAPTER_H
80