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
Init()29 void PkgVerify::Init()
30 {
31 CertVerify::GetInstance().RegisterCertHelper(std::make_unique<SingleCertHelper>());
32 }
33
Verify(const std::string & pkgPath)34 int PkgVerify::Verify(const std::string &pkgPath)
35 {
36 if (statusManager_ == nullptr) {
37 LOG(ERROR) << "statusManager_ nullptr";
38 return -1;
39 }
40
41 if (!verifyInit_) {
42 Init();
43 verifyInit_ = true;
44 }
45
46 std::string realPath {};
47 if (!Utils::PathToRealPath(pkgPath, realPath)) {
48 LOG(ERROR) << "get real path failed";
49 return -1;
50 }
51
52 statusManager_->SetUpdatePercent(1); // 1 : 1%
53 int ret = VerifyPackage(realPath.c_str(), Utils::GetCertName().c_str(), "", nullptr, 0);
54 if (ret != 0) {
55 LOG(ERROR) << "VerifyPackage failed:" << ret;
56 return ret;
57 }
58
59 statusManager_->SetUpdatePercent(5); // 5 : %5
60 LOG(INFO) << "UpdatePreCheck successful";
61 return 0;
62 }
63
PerformAction()64 void PkgVerify::PerformAction()
65 {
66 InstallerErrCode errCode = SYS_UPDATE_SUCCESS;
67 std::string errStr = "";
68 int ret = 0;
69 Detail::ScopeGuard guard([&] {
70 LOG(INFO) << "PerformAction ret:" << ret;
71 if (ret != 0) {
72 errCode = SYS_SIGN_VERIFY_FAIL;
73 errStr = std::to_string(ret);
74 }
75 if (actionCallBack_ != nullptr) {
76 actionCallBack_(errCode, errStr);
77 }
78 });
79
80 ret = Verify(pkgPath_);
81 }
82 } // namespace SysInstaller
83 } // namespace OHOS
84