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