1 /*
2 * Copyright (c) 2025-2025 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 const std::string Options::MODULE_FILE = "moduleFile";
60 const std::string Options::SELF_SIGN = "selfSign";
61
GetChars(const std::string & key)62 char* Options::GetChars(const std::string& key)
63 {
64 if (this->count(key) == 0) {
65 return nullptr;
66 }
67
68 auto value = (*this)[key];
69 char** charsPtr = std::get_if<char*>(&value);
70 if (charsPtr == nullptr) {
71 SIGNATURE_TOOLS_LOGI("GetChars key = %s value = nullptr !", key.c_str());
72 return nullptr;
73 }
74 return *charsPtr;
75 }
76
GetString(const std::string & key)77 std::string Options::GetString(const std::string& key)
78 {
79 if (this->count(key) == 0) {
80 return "";
81 }
82
83 auto value = (*this)[key];
84 std::string* stringPtr = std::get_if<std::string>(&value);
85 if (stringPtr == nullptr) {
86 SIGNATURE_TOOLS_LOGI("GetString key = %s value = """, key.c_str());
87 return "";
88 }
89 return *stringPtr;
90 }
91
GetString(const std::string & key,const std::string & checkStr)92 std::string Options::GetString(const std::string& key, const std::string& checkStr)
93 {
94 if (this->count(key) == 0) {
95 return "";
96 }
97
98 auto value = (*this)[key];
99 std::string* stringPtr = std::get_if<std::string>(&value);
100 if (stringPtr == nullptr || *stringPtr == "") {
101 SIGNATURE_TOOLS_LOGI("GetString key = %s value = %s ", key.c_str(), checkStr.c_str());
102 return checkStr;
103 }
104 return *stringPtr;
105 }
106
GetInt(const std::string & key)107 int Options::GetInt(const std::string& key)
108 {
109 if (this->count(key) == 0) {
110 return 0;
111 }
112
113 auto value = (*this)[key];
114 int* stringPtr = std::get_if<int>(&value);
115 if (stringPtr == nullptr) {
116 SIGNATURE_TOOLS_LOGI("GetInt key = %s value = 0 ", key.c_str());
117 return 0;
118 }
119 return *stringPtr;
120 }
121
Equals(const std::string & argf,const std::string & args)122 bool Options::Equals(const std::string& argf, const std::string& args)
123 {
124 std::string ksFile = GetString(argf);
125 std::string iksFile = GetString(args);
126 if (ksFile == iksFile) {
127 return true;
128 }
129 return false;
130 }
131
Required(const std::initializer_list<std::string> & keys)132 bool Options::Required(const std::initializer_list<std::string>& keys)
133 {
134 for (auto& key : keys) {
135 if (!this->IsEmpty(key) && !(this->find(key) != this->end())) {
136 PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "Params '-" + key + "' is required");
137 return false;
138 }
139 }
140 return true;
141 }
142
IsEmpty(const std::string & cs)143 bool Options::IsEmpty(const std::string& cs)
144 {
145 if (cs.empty()) {
146 return true;
147 }
148 return false;
149 }
150
GetBool(const std::string & key)151 bool Options::GetBool(const std::string& key)
152 {
153 auto value = (*this)[key];
154 bool* stringPtr = std::get_if<bool>(&value);
155 return *stringPtr;
156 }
157 } // namespace SignatureTools
158 } // namespace OHOS