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
18namespace OHOS {
19namespace SignatureTools {
20
21/* Initializes the static member constant. */
22const std::string Options::KEY_ALIAS = "keyAlias";
23const std::string Options::KEY_RIGHTS = "keyPwd";
24const std::string Options::KEY_ALG = "keyAlg";
25const std::string Options::KEY_SIZE = "keySize";
26const std::string Options::KEY_STORE_FILE = "keystoreFile";
27const std::string Options::KEY_STORE_RIGHTS = "keystorePwd";
28const std::string Options::ISSUER_KEY_ALIAS = "issuerKeyAlias";
29const std::string Options::ISSUER_KEY_RIGHTS = "issuerKeyPwd";
30const std::string Options::ISSUER_KEY_STORE_FILE = "issuerKeystoreFile";
31const std::string Options::ISSUER_KEY_STORE_RIGHTS = "issuerKeystorePwd";
32const std::string Options::ISSUER = "issuer";
33const std::string Options::SUBJECT = "subject";
34const std::string Options::VALIDITY = "validity";
35const std::string Options::SIGN_ALG = "signAlg";
36const std::string Options::BASIC_CONSTRAINTS_PATH_LEN = "basicConstraintsPathLen";
37const std::string Options::OUT_FILE = "outFile";
38const std::string Options::OUT_FORM = "outForm";
39const std::string Options::SUB_CA_CERT_FILE = "subCaCertFile";
40const std::string Options::CA_CERT_FILE = "rootCaCertFile";
41const std::string Options::IN_FILE = "inFile";
42const std::string Options::PROFILE_CERT_FILE = "profileCertFile";
43const std::string Options::APP_CERT_FILE = "appCertFile";
44const std::string Options::KEY_USAGE = "keyUsage";
45const std::string Options::EXT_KEY_USAGE = "extKeyUsage";
46const std::string Options::KEY_USAGE_CRITICAL = "keyUsageCritical";
47const std::string Options::EXT_KEY_USAGE_CRITICAL = "extKeyUsageCritical";
48const std::string Options::BASIC_CONSTRAINTS_CA = "basicConstraintsCa";
49const std::string Options::BASIC_CONSTRAINTS_CRITICAL = "basicConstraintsCritical";
50const std::string Options::BASIC_CONSTRAINTS = "basicConstraints";
51const std::string Options::OUT_FORM_SCOPE = "cert,certChain";
52const std::string Options::MODE = "mode";
53const std::string Options::INFORM = "inForm";
54const std::string Options::OUT_CERT_CHAIN = "outCertChain";
55const std::string Options::OUT_PROFILE = "outProfile";
56const std::string Options::PROOF_FILE = "outproof";
57const std::string Options::PROFILE_FILE = "profileFile";
58const std::string Options::PROFILE_SIGNED = "profileSigned";
59
60char* 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
75std::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
90std::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
105int 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
120bool 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
130bool 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
141bool Options::IsEmpty(const std::string& cs)
142{
143    if (cs.empty()) {
144        return true;
145    }
146    return false;
147}
148
149bool 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