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