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 "params_trust_list.h"
17 #include "constant.h"
18 #include "params.h"
19 #include "string_utils.h"
20 #include "help.h"
21 
22 namespace OHOS {
23 namespace SignatureTools {
24 const std::string options = "[options]:";
25 
26 const std::vector<std::string> commands = {
27     GENERATE_KEYPAIR + options,
28     GENERATE_CSR + options,
29     GENERATE_CERT + options,
30     GENERATE_CA + options,
31     GENERATE_APP_CERT + options,
32     GENERATE_PROFILE_CERT + options,
33     SIGN_PROFILE + options,
34     VERIFY_PROFILE + options,
35     SIGN_APP + options,
36     VERIFY_APP + options
37 };
38 
GetInstance()39 ParamsTrustList ParamsTrustList::GetInstance()
40 {
41     static ParamsTrustList instance;
42     return instance;
43 }
44 
PutTrustMap(const std::string& cmdStandBy, const std::string& param)45 void ParamsTrustList::PutTrustMap(const std::string& cmdStandBy, const std::string& param)
46 {
47     if (param.at(0) == '-') {
48         size_t pos = param.find(':');
49         std::string subParam = param.substr(0, pos);
50         subParam = StringUtils::Trim(subParam);
51         bool isExists = false;
52         if (trustMap.find(cmdStandBy) != trustMap.end()) {
53             isExists = true;
54         }
55         std::vector<std::string> trustList = isExists ? trustMap[cmdStandBy] : std::vector<std::string>();
56         trustList.push_back(subParam);
57         trustMap[cmdStandBy] = trustList;
58     }
59 }
60 
ReadHelpParam(std::istringstream& fd)61 void ParamsTrustList::ReadHelpParam(std::istringstream& fd)
62 {
63     std::string str;
64     std::string cmdStandBy;
65     while (std::getline(fd, str)) {
66         bool isExists = false;
67         std::string params = StringUtils::Trim(str);
68         if (params.empty()) {
69             continue;
70         }
71         for (const auto& it : commands) {
72             if (it == params) {
73                 cmdStandBy = params;
74                 isExists = true;
75                 break;
76             }
77         }
78         if (!isExists) {
79             PutTrustMap(cmdStandBy, params);
80         }
81     }
82 }
83 
GenerateTrustList()84 void ParamsTrustList::GenerateTrustList()
85 {
86     std::istringstream iss(HELP_TXT);
87     ReadHelpParam(iss);
88 }
89 
GetTrustList(const std::string& command)90 std::vector<std::string> ParamsTrustList::GetTrustList(const std::string& command)
91 {
92     std::string keyParam = command + options;
93     GenerateTrustList();
94     if (trustMap.find(keyParam) != trustMap.end()) {
95         return trustMap[keyParam];
96     } else {
97         PrintErrorNumberMsg("COMMAND_ERROR", COMMAND_ERROR, "'" + command + "is not trust command");
98         trustMap[keyParam].clear();
99         return trustMap[keyParam];
100     }
101 }
102 } // namespace SignatureTools
103 } // namespace OHOS