1 /*
2 * Copyright (c) 2022 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 "pkg_verify.h"
17
18 #include "log/log.h"
19 #include "package/cert_verify.h"
20 #include "package/pkg_manager.h"
21 #include "scope_guard.h"
22 #include "utils.h"
23
24 namespace OHOS {
25 namespace SysInstaller {
26 using namespace Updater;
27 using namespace Hpackage;
28
29 constexpr size_t PKG_VERIFY_PERCENT = 4;
Init()30 void PkgVerify::Init()
31 {
32 CertVerify::GetInstance().RegisterCertHelper(std::make_unique<SingleCertHelper>());
33 }
34
Verify(const std::vector<std::string> & pkgPath)35 int PkgVerify::Verify(const std::vector<std::string> &pkgPath)
36 {
37 if (statusManager_ == nullptr) {
38 LOG(ERROR) << "statusManager_ nullptr";
39 return -1;
40 }
41
42 if (!verifyInit_) {
43 Init();
44 verifyInit_ = true;
45 }
46 std::vector<std::string> pkgList {};
47 for (auto &pkg : pkgPath) {
48 if (pkg != "") {
49 pkgList.push_back(pkg);
50 }
51 }
52 size_t pkgNum = pkgList.size();
53 if (pkgNum == 0) {
54 LOG(INFO) << "there is no package";
55 return 0;
56 }
57 size_t count = 0;
58 statusManager_->SetUpdatePercent(1); // 1 : 1%
59 for (const auto &file : pkgList) {
60 count++;
61 std::string realpath {};
62 if (!Utils::PathToRealPath(file, realpath)) {
63 LOG(ERROR) << "get real path failed: " << file;
64 return -1;
65 }
66 int ret = VerifyPackage(realpath.c_str(), Utils::GetCertName().c_str(), "", nullptr, 0);
67 if (ret != 0) {
68 LOG(ERROR) << "VerifyPackage failed: " << file << ", " << ret;
69 return ret;
70 }
71 int percent = static_cast<int>(
72 PKG_VERIFY_PERCENT * static_cast<double>(count) / static_cast<double>(pkgNum));
73 statusManager_->SetUpdatePercent(percent + 1); // 1 : 1%
74 LOG(INFO) << "VerifyPackage success: " << file;
75 }
76 statusManager_->SetUpdatePercent(5); // 5 : %5
77 LOG(INFO) << "UpdatePreCheck successful";
78 return 0;
79 }
80
PerformAction()81 void PkgVerify::PerformAction()
82 {
83 InstallerErrCode errCode = SYS_UPDATE_SUCCESS;
84 std::string errStr = "";
85 int ret = 0;
86 Detail::ScopeGuard guard([&] {
87 LOG(INFO) << "PerformAction ret:" << ret;
88 if (ret != 0) {
89 errCode = SYS_SIGN_VERIFY_FAIL;
90 errStr = std::to_string(ret);
91 }
92 if (actionCallBack_ != nullptr) {
93 actionCallBack_(errCode, errStr);
94 }
95 });
96
97 ret = Verify(pkgPath_);
98 }
99 } // namespace SysInstaller
100 } // namespace OHOS
101