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 return CompareSaSdkVersion(saSdkVersionVec, hmpVersionVec);
73 }
74
GetPackInfoVer(const JsonNode & root,const std::string & key,std::string & version)75 bool GetPackInfoVer(const JsonNode &root, const std::string &key, std::string &version)
76 {
77 const JsonNode &package = root["package"];
78 std::optional<std::string> tmpVersion = package[key].As<std::string>();
79 if (!tmpVersion.has_value()) {
80 LOG(ERROR) << "count get version val";
81 return false;
82 }
83 version = tmpVersion.value();
84 LOG(INFO) << key << " " << version;
85 return true;
86 }
87 }
88
CheckPackInfoVer(const std::string & pkgPackInfoPath)89 bool CheckPackInfoVer(const std::string &pkgPackInfoPath)
90 {
91 std::string packInfo = GetContentFromZip(pkgPackInfoPath, PACK_INFO_NAME);
92 JsonNode root(packInfo);
93 std::string type;
94 if (!GetHmpType(root, type)) {
95 return false;
96 }
97 LOG(INFO) << pkgPackInfoPath << "; type = " << type;
98 std::string apiVersion;
99 if (type == HMP_APP_TYPE && GetPackInfoVer(root, HMP_API_VERSION, apiVersion)) {
100 return CheckApiVersion(apiVersion);
101 }
102 std::string saSdkVersion;
103 if ((type == HMP_SA_TYPE || type == HMP_SA_TYPE_OLD) &&
104 GetPackInfoVer(root, HMP_SA_SDK_VERSION, saSdkVersion)) {
105 return CheckSaSdkVersion(saSdkVersion);
106 }
107 if (type == HMP_MIX_TYPE &&
108 GetPackInfoVer(root, HMP_SA_SDK_VERSION, saSdkVersion) &&
109 GetPackInfoVer(root, HMP_API_VERSION, apiVersion)) {
110 return CheckApiVersion(apiVersion) && CheckSaSdkVersion(saSdkVersion);
111 }
112 return false;
113 }
114
CleanErrDir(const std::string & path)115 void CleanErrDir(const std::string &path)
116 {
117 if (path.find(UPDATE_ACTIVE_DIR) != std::string::npos ||
118 path.find(UPDATE_BACKUP_DIR) != std::string::npos) {
119 LOG(INFO) << "delete err dir :"<< path;
120 ForceRemoveDirectory(path.substr(0, path.rfind("/")));
121 }
122 }
123 }
124 }