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 #include "options.h"
17 
18 namespace OHOS {
19 namespace SignatureTools {
20 
21 /* Initializes the static member constant. */
22 const std::string Options::KEY_ALIAS = "keyAlias";
23 const std::string Options::KEY_RIGHTS = "keyPwd";
24 const std::string Options::KEY_ALG = "keyAlg";
25 const std::string Options::KEY_SIZE = "keySize";
26 const std::string Options::KEY_STORE_FILE = "keystoreFile";
27 const std::string Options::KEY_STORE_RIGHTS = "keystorePwd";
28 const std::string Options::ISSUER_KEY_ALIAS = "issuerKeyAlias";
29 const std::string Options::ISSUER_KEY_RIGHTS = "issuerKeyPwd";
30 const std::string Options::ISSUER_KEY_STORE_FILE = "issuerKeystoreFile";
31 const std::string Options::ISSUER_KEY_STORE_RIGHTS = "issuerKeystorePwd";
32 const std::string Options::ISSUER = "issuer";
33 const std::string Options::SUBJECT = "subject";
34 const std::string Options::VALIDITY = "validity";
35 const std::string Options::SIGN_ALG = "signAlg";
36 const std::string Options::BASIC_CONSTRAINTS_PATH_LEN = "basicConstraintsPathLen";
37 const std::string Options::OUT_FILE = "outFile";
38 const std::string Options::OUT_FORM = "outForm";
39 const std::string Options::SUB_CA_CERT_FILE = "subCaCertFile";
40 const std::string Options::CA_CERT_FILE = "rootCaCertFile";
41 const std::string Options::IN_FILE = "inFile";
42 const std::string Options::PROFILE_CERT_FILE = "profileCertFile";
43 const std::string Options::APP_CERT_FILE = "appCertFile";
44 const std::string Options::KEY_USAGE = "keyUsage";
45 const std::string Options::EXT_KEY_USAGE = "extKeyUsage";
46 const std::string Options::KEY_USAGE_CRITICAL = "keyUsageCritical";
47 const std::string Options::EXT_KEY_USAGE_CRITICAL = "extKeyUsageCritical";
48 const std::string Options::BASIC_CONSTRAINTS_CA = "basicConstraintsCa";
49 const std::string Options::BASIC_CONSTRAINTS_CRITICAL = "basicConstraintsCritical";
50 const std::string Options::BASIC_CONSTRAINTS = "basicConstraints";
51 const std::string Options::OUT_FORM_SCOPE = "cert,certChain";
52 const std::string Options::MODE = "mode";
53 const std::string Options::INFORM = "inForm";
54 const std::string Options::OUT_CERT_CHAIN = "outCertChain";
55 const std::string Options::OUT_PROFILE = "outProfile";
56 const std::string Options::PROOF_FILE = "outproof";
57 const std::string Options::PROFILE_FILE = "profileFile";
58 const std::string Options::PROFILE_SIGNED = "profileSigned";
59 
GetChars(const std::string& key)60 char* Options::GetChars(const std::string& key)
61 {
62     if (this->count(key) == 0) {
63         return nullptr;
64     }
65 
66     auto value = (*this)[key];
67     char** charsPtr = std::get_if<char*>(&value);
68     if (charsPtr == nullptr) {
69         SIGNATURE_TOOLS_LOGI("GetChars key = %s  value = nullptr !", key.c_str());
70         return nullptr;
71     }
72     return *charsPtr;
73 }
74 
GetString(const std::string& key)75 std::string Options::GetString(const std::string& key)
76 {
77     if (this->count(key) == 0) {
78         return "";
79     }
80 
81     auto value = (*this)[key];
82     std::string* stringPtr = std::get_if<std::string>(&value);
83     if (stringPtr == nullptr) {
84         SIGNATURE_TOOLS_LOGI("GetString  key = %s value = """, key.c_str());
85         return "";
86     }
87     return *stringPtr;
88 }
89 
GetString(const std::string& key, const std::string& checkStr)90 std::string Options::GetString(const std::string& key, const std::string& checkStr)
91 {
92     if (this->count(key) == 0) {
93         return "";
94     }
95 
96     auto value = (*this)[key];
97     std::string* stringPtr = std::get_if<std::string>(&value);
98     if (stringPtr == nullptr || *stringPtr == "") {
99         SIGNATURE_TOOLS_LOGI("GetString  key = %s value = %s ", key.c_str(), checkStr.c_str());
100         return checkStr;
101     }
102     return *stringPtr;
103 }
104 
GetInt(const std::string& key)105 int Options::GetInt(const std::string& key)
106 {
107     if (this->count(key) == 0) {
108         return 0;
109     }
110 
111     auto value = (*this)[key];
112     int* stringPtr = std::get_if<int>(&value);
113     if (stringPtr == nullptr) {
114         SIGNATURE_TOOLS_LOGI("GetInt  key = %s value = 0 ", key.c_str());
115         return 0;
116     }
117     return *stringPtr;
118 }
119 
Equals(const std::string& argf, const std::string& args)120 bool Options::Equals(const std::string& argf, const std::string& args)
121 {
122     std::string ksFile = GetString(argf);
123     std::string iksFile = GetString(args);
124     if (ksFile == iksFile) {
125         return true;
126     }
127     return  false;
128 }
129 
Required(const std::initializer_list<std::string>& keys)130 bool Options::Required(const std::initializer_list<std::string>& keys)
131 {
132     for (auto& key : keys) {
133         if (!this->IsEmpty(key) && !(this->find(key) != this->end())) {
134             PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "Params '-" + key + "' is required");
135             return false;
136         }
137     }
138     return true;
139 }
140 
IsEmpty(const std::string& cs)141 bool Options::IsEmpty(const std::string& cs)
142 {
143     if (cs.empty()) {
144         return true;
145     }
146     return false;
147 }
148 
GetBool(const std::string& key)149 bool Options::GetBool(const std::string& key)
150 {
151     auto value = (*this)[key];
152     bool* stringPtr = std::get_if<bool>(&value);
153     return *stringPtr;
154 }
155 } // namespace SignatureTools
156 } // namespace OHOS