• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "bundle_verify_mgr.h"
17 
18 #include <map>
19 
20 #include "app_log_wrapper.h"
21 #include "bms_extension_data_mgr.h"
22 #include "bundle_constants.h"
23 #include "interfaces/hap_verify.h"
24 #include "ipc_skeleton.h"
25 
26 using namespace OHOS::Security::Verify;
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::map<int32_t, ErrCode> HAP_VERIFY_ERR_MAP = {
32     {HapVerifyResultCode::VERIFY_SUCCESS, ERR_OK},
33     {HapVerifyResultCode::FILE_PATH_INVALID, ERR_APPEXECFWK_INSTALL_FAILED_INVALID_SIGNATURE_FILE_PATH},
34     {HapVerifyResultCode::OPEN_FILE_ERROR, ERR_APPEXECFWK_INSTALL_FAILED_BAD_BUNDLE_SIGNATURE_FILE},
35     {HapVerifyResultCode::SIGNATURE_NOT_FOUND, ERR_APPEXECFWK_INSTALL_FAILED_NO_BUNDLE_SIGNATURE},
36     {HapVerifyResultCode::VERIFY_APP_PKCS7_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_VERIFY_APP_PKCS7_FAIL},
37     {HapVerifyResultCode::PROFILE_PARSE_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_PROFILE_PARSE_FAIL},
38     {HapVerifyResultCode::APP_SOURCE_NOT_TRUSTED, ERR_APPEXECFWK_INSTALL_FAILED_APP_SOURCE_NOT_TRUESTED},
39     {HapVerifyResultCode::GET_DIGEST_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BAD_DIGEST},
40     {HapVerifyResultCode::VERIFY_INTEGRITY_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BUNDLE_INTEGRITY_VERIFICATION_FAILURE},
41     {HapVerifyResultCode::FILE_SIZE_TOO_LARGE, ERR_APPEXECFWK_INSTALL_FAILED_FILE_SIZE_TOO_LARGE},
42     {HapVerifyResultCode::GET_PUBLICKEY_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BAD_PUBLICKEY},
43     {HapVerifyResultCode::GET_SIGNATURE_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BAD_BUNDLE_SIGNATURE},
44     {HapVerifyResultCode::NO_PROFILE_BLOCK_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_NO_PROFILE_BLOCK_FAIL},
45     {HapVerifyResultCode::VERIFY_SIGNATURE_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_BUNDLE_SIGNATURE_VERIFICATION_FAILURE},
46     {HapVerifyResultCode::VERIFY_SOURCE_INIT_FAIL, ERR_APPEXECFWK_INSTALL_FAILED_VERIFY_SOURCE_INIT_FAIL}
47 };
48 } // namespace
49 
HapVerify(const std::string & filePath,HapVerifyResult & hapVerifyResult)50 ErrCode BundleVerifyMgr::HapVerify(const std::string &filePath, HapVerifyResult &hapVerifyResult)
51 {
52     BmsExtensionDataMgr bmsExtensionDataMgr;
53     ErrCode res = bmsExtensionDataMgr.HapVerify(filePath, hapVerifyResult);
54     if (res == ERR_BUNDLEMANAGER_INSTALL_FAILED_SIGNATURE_EXTENSION_NOT_EXISTED) {
55         auto ret = Security::Verify::HapVerify(filePath, hapVerifyResult);
56         APP_LOGI("HapVerify result %{public}d", ret);
57         if (HAP_VERIFY_ERR_MAP.find(ret) == HAP_VERIFY_ERR_MAP.end()) {
58             return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
59         }
60         return HAP_VERIFY_ERR_MAP.at(ret);
61     }
62     return res;
63 }
64 
65 bool BundleVerifyMgr::isDebug_ = false;
66 
EnableDebug()67 void BundleVerifyMgr::EnableDebug()
68 {
69     if (isDebug_) {
70         APP_LOGD("verify mode is already debug mode");
71         return;
72     }
73     if (!Security::Verify::EnableDebugMode()) {
74         APP_LOGE("start debug mode failed");
75         return;
76     }
77     isDebug_ = true;
78 }
79 
DisableDebug()80 void BundleVerifyMgr::DisableDebug()
81 {
82     if (!isDebug_) {
83         APP_LOGD("verify mode is already signature mode");
84         return;
85     }
86     Security::Verify::DisableDebugMode();
87     isDebug_ = false;
88 }
89 
ParseHapProfile(const std::string & filePath,HapVerifyResult & hapVerifyResult)90 ErrCode BundleVerifyMgr::ParseHapProfile(const std::string &filePath, HapVerifyResult &hapVerifyResult)
91 {
92     auto ret = Security::Verify::ParseHapProfile(filePath, hapVerifyResult);
93     APP_LOGI("ParseHapProfile result %{public}d", ret);
94     if (HAP_VERIFY_ERR_MAP.find(ret) == HAP_VERIFY_ERR_MAP.end()) {
95         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
96     }
97     return HAP_VERIFY_ERR_MAP.at(ret);
98 }
99 }  // namespace AppExecFwk
100 }  // namespace OHOS