1 /*
2 * Copyright (c) 2024 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 "module_update_verify.h"
17 #include "cert_verify.h"
18 #include "directory_ex.h"
19 #include "hash_data_verifier.h"
20 #include "log/log.h"
21 #include "json_node.h"
22 #include "parameters.h"
23 #include "scope_guard.h"
24 #include "utils.h"
25 #include "module_constants.h"
26 #include "module_file.h"
27 #include "module_utils.h"
28
29 namespace OHOS {
30 namespace SysInstaller {
31 using namespace Hpackage;
32 using namespace Updater;
33
34 namespace {
GetHmpType(const JsonNode &root, std::string &type)35 bool GetHmpType(const JsonNode &root, std::string &type)
36 {
37 const JsonNode &typeJson = root["type"];
38 std::optional<std::string> hmpType = typeJson.As<std::string>();
39 if (!hmpType.has_value()) {
40 LOG(ERROR) << "HmpInfo: Failed to get type val";
41 return false;
42 }
43 type = hmpType.value();
44 return true;
45 }
46
CheckApiVersion(const std::string &apiVersion)47 bool CheckApiVersion(const std::string &apiVersion)
48 {
49 int sysApiVersion = GetDeviceApiVersion();
50 int hmpApiVersion = Utils::String2Int<int>(apiVersion, Utils::N_DEC);
51 if (hmpApiVersion <= sysApiVersion) {
52 return true;
53 }
54 LOG(ERROR) << "sysApiVersion: " << sysApiVersion << "; hmpApiVersion: " << hmpApiVersion;
55 return false;
56 }
57
CheckSaSdkVersion(const std::string &saSdkVersion)58 bool CheckSaSdkVersion(const std::string &saSdkVersion)
59 {
60 //sasdk_M.S.F.B
61 std::vector<std::string> saSdkVersionVec {};
62 std::string sysSaSdkVersion = GetDeviceSaSdkVersion();
63 if (!ParseVersion(sysSaSdkVersion, "_", saSdkVersionVec)) {
64 LOG(ERROR) << "ParseVersion sysSaSdkVersion failed: " << sysSaSdkVersion;
65 return false;
66 }
67 std::vector<std::string> hmpVersionVec {};
68 if (!ParseVersion(saSdkVersion, "_", hmpVersionVec)) {
69 LOG(ERROR) << "ParseVersion hmpSaSdkVersion failed: " << saSdkVersion;
70 return false;
71 }
72 if (!CompareSaSdkVersion(saSdkVersionVec, hmpVersionVec)) {
73 LOG(ERROR) << "saSdkVersion compare fail, sys:" << sysSaSdkVersion << "; hmp:" << saSdkVersion;
74 return false;
75 }
76 return true;
77 }
78
GetPackInfoVer(const JsonNode &root, const std::string &key, std::string &version)79 bool GetPackInfoVer(const JsonNode &root, const std::string &key, std::string &version)
80 {
81 const JsonNode &package = root["package"];
82 std::optional<std::string> tmpVersion = package[key].As<std::string>();
83 if (!tmpVersion.has_value()) {
84 LOG(ERROR) << "count get version val";
85 return false;
86 }
87 version = tmpVersion.value();
88 LOG(INFO) << key << " " << version;
89 return true;
90 }
91 }
92
CheckPackInfoVer(const std::string &pkgPackInfoPath)93 bool CheckPackInfoVer(const std::string &pkgPackInfoPath)
94 {
95 std::string packInfo = GetContentFromZip(pkgPackInfoPath, PACK_INFO_NAME);
96 JsonNode root(packInfo);
97 std::string type;
98 if (!GetHmpType(root, type)) {
99 return false;
100 }
101 LOG(INFO) << pkgPackInfoPath << "; type = " << type;
102 std::string apiVersion;
103 if (type == HMP_APP_TYPE && GetPackInfoVer(root, HMP_API_VERSION, apiVersion)) {
104 return CheckApiVersion(apiVersion);
105 }
106 std::string saSdkVersion;
107 if ((type == HMP_SA_TYPE || type == HMP_SA_TYPE_OLD) &&
108 GetPackInfoVer(root, HMP_SA_SDK_VERSION, saSdkVersion)) {
109 return CheckSaSdkVersion(saSdkVersion);
110 }
111 if (type == HMP_MIX_TYPE &&
112 GetPackInfoVer(root, HMP_SA_SDK_VERSION, saSdkVersion) &&
113 GetPackInfoVer(root, HMP_API_VERSION, apiVersion)) {
114 return CheckApiVersion(apiVersion) && CheckSaSdkVersion(saSdkVersion);
115 }
116 return false;
117 }
118
CleanErrDir(const std::string &path)119 void CleanErrDir(const std::string &path)
120 {
121 if (path.find(UPDATE_ACTIVE_DIR) != std::string::npos ||
122 path.find(UPDATE_BACKUP_DIR) != std::string::npos) {
123 LOG(INFO) << "delete err dir :"<< path;
124 ForceRemoveDirectory(path.substr(0, path.rfind("/")));
125 }
126 }
127 }
128 }