• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "sign_tool_service_impl.h"
17 #include "pkcs7_data.h"
18 #include "profile_sign_tool.h"
19 #include "nlohmann/json.hpp"
20 #include "profile_info.h"
21 #include "profile_verify.h"
22 #include "signature_tools_errno.h"
23 #include "self_sign_sign_provider.h"
24 #include "local_sign_provider.h"
25 #include "signature_tools_log.h"
26 #include "param_constants.h"
27 #include "constant.h"
28 #include "remote_sign_provider.h"
29 #include "verify_elf.h"
30 
31 namespace OHOS {
32 namespace SignatureTools {
Sign(Options * options)33 bool SignToolServiceImpl::Sign(Options* options)
34 {
35     std::string inFile = options->GetString(Options::IN_FILE);
36     if (!FileUtils::isElfFile(inFile)) {
37         SIGNATURE_TOOLS_LOGE("inFile is not a elf file");
38         return false;
39     }
40     std::string mode = options->GetString(Options::MODE);
41     std::string selfSign = options->GetString(Options::SELF_SIGN);
42     std::shared_ptr<SignProvider> signProvider;
43     if (ParamConstants::SELF_SIGN_TYPE_1 == selfSign) {
44         signProvider = std::make_shared<SelfSignSignProvider>();
45     } else if (LOCAL_SIGN == mode) {
46         signProvider = std::make_shared<LocalSignProvider>();
47     } else if (REMOTE_SIGN == mode) {
48         signProvider = std::make_shared<RemoteSignProvider>();
49     } else {
50         SIGNATURE_TOOLS_LOGE("Resign mode. But not implemented yet");
51         return false;
52     }
53     return signProvider->SignElf(options);
54 }
55 
GetProvisionContent(const std::string & input,std::string & ret)56 int SignToolServiceImpl::GetProvisionContent(const std::string& input, std::string& ret)
57 {
58     std::string bytes;
59     if (FileUtils::ReadFile(input, bytes) < 0) {
60         SIGNATURE_TOOLS_LOGE("provision read faild!");
61         return IO_ERROR;
62     }
63     nlohmann::json obj = nlohmann::json::parse(bytes);
64     if (obj.is_discarded() || (!obj.is_structured())) {
65         PrintErrorNumberMsg("PARSE ERROR", PARSE_ERROR, "Parsing provision failed!");
66         return PARSE_ERROR;
67     }
68     ret = obj.dump();
69     ProfileInfo provision;
70     AppProvisionVerifyResult result = ParseProvision(ret, provision);
71     if (result != PROVISION_OK) {
72         SIGNATURE_TOOLS_LOGE("invalid provision");
73         return INVALIDPARAM_ERROR;
74     }
75     return 0;
76 }
77 
Verify(Options * option)78 bool SignToolServiceImpl::Verify(Options* option)
79 {
80     VerifyElf verifyElf;
81     if (!verifyElf.Verify(option)) {
82         return false;
83     }
84     return true;
85 }
86 } // namespace SignatureTools
87 } // namespace OHOS
88