1 /*
2 * Copyright (c) 2021-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 "install_param.h"
17
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20
21 #include "app_log_wrapper.h"
22 #include "parcel_macro.h"
23 #include "ipc_skeleton.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)27 bool InstallParam::ReadFromParcel(Parcel &parcel)
28 {
29 int32_t flagData;
30 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, flagData);
31 installFlag = static_cast<InstallFlag>(flagData);
32
33 int32_t locationData;
34 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, locationData);
35 installLocation = static_cast<InstallLocation>(locationData);
36
37 userId = parcel.ReadInt32();
38 isKeepData = parcel.ReadBool();
39
40 int32_t hashParamSize;
41 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, hashParamSize);
42 CONTAINER_SECURITY_VERIFY(parcel, hashParamSize, &hashParams);
43 for (int32_t i = 0; i < hashParamSize; ++i) {
44 std::string moduleName = Str16ToStr8(parcel.ReadString16());
45 std::string hashValue = Str16ToStr8(parcel.ReadString16());
46 hashParams.emplace(moduleName, hashValue);
47 }
48 crowdtestDeadline = parcel.ReadInt64();
49
50 int32_t sharedBundleDirPathsSize;
51 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, sharedBundleDirPathsSize);
52 CONTAINER_SECURITY_VERIFY(parcel, sharedBundleDirPathsSize, &sharedBundleDirPaths);
53 for (int32_t i = 0; i < sharedBundleDirPathsSize; ++i) {
54 std::string sharedBundleDirPath = Str16ToStr8(parcel.ReadString16());
55 sharedBundleDirPaths.emplace_back(sharedBundleDirPath);
56 }
57 specifiedDistributionType = Str16ToStr8(parcel.ReadString16());
58 additionalInfo = Str16ToStr8(parcel.ReadString16());
59 isDataPreloadHap = parcel.ReadBool();
60 appIdentifier = Str16ToStr8(parcel.ReadString16());
61
62 int32_t verifyCodeParamSize;
63 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, verifyCodeParamSize);
64 CONTAINER_SECURITY_VERIFY(parcel, verifyCodeParamSize, &verifyCodeParams);
65 for (int32_t i = 0; i < verifyCodeParamSize; ++i) {
66 std::string moduleName = Str16ToStr8(parcel.ReadString16());
67 std::string signatureFilePath = Str16ToStr8(parcel.ReadString16());
68 verifyCodeParams.emplace(moduleName, signatureFilePath);
69 }
70 isSelfUpdate = parcel.ReadBool();
71
72 int32_t pgoParamsSize;
73 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pgoParamsSize);
74 CONTAINER_SECURITY_VERIFY(parcel, pgoParamsSize, &pgoParams);
75 for (int32_t i = 0; i < pgoParamsSize; ++i) {
76 std::string moduleName = Str16ToStr8(parcel.ReadString16());
77 std::string pgoPath = Str16ToStr8(parcel.ReadString16());
78 pgoParams.emplace(moduleName, pgoPath);
79 }
80
81 uint32_t parametersSize;
82 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, parametersSize);
83 CONTAINER_SECURITY_VERIFY(parcel, parametersSize, ¶meters);
84 for (uint32_t i = 0; i < parametersSize; ++i) {
85 std::string key = Str16ToStr8(parcel.ReadString16());
86 std::string value = Str16ToStr8(parcel.ReadString16());
87 parameters.emplace(key, value);
88 }
89 isPatch = parcel.ReadBool();
90 return true;
91 }
92
Unmarshalling(Parcel & parcel)93 InstallParam *InstallParam::Unmarshalling(Parcel &parcel)
94 {
95 InstallParam *info = new (std::nothrow) InstallParam();
96 if (info && !info->ReadFromParcel(parcel)) {
97 APP_LOGW("read from parcel failed");
98 delete info;
99 info = nullptr;
100 }
101 return info;
102 }
103
Marshalling(Parcel & parcel) const104 bool InstallParam::Marshalling(Parcel &parcel) const
105 {
106 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(installFlag));
107 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(installLocation));
108 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
109 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepData);
110
111 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(hashParams.size()));
112 for (const auto &hashParam : hashParams) {
113 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hashParam.first));
114 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hashParam.second));
115 }
116 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, crowdtestDeadline);
117
118 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(sharedBundleDirPaths.size()));
119 for (const auto& sharedBundleDirPath : sharedBundleDirPaths) {
120 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(sharedBundleDirPath));
121 }
122
123 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(specifiedDistributionType));
124 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(additionalInfo));
125 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isDataPreloadHap);
126 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appIdentifier));
127 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(verifyCodeParams.size()));
128 for (const auto &verifyCodeParam : verifyCodeParams) {
129 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(verifyCodeParam.first));
130 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(verifyCodeParam.second));
131 }
132 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isSelfUpdate);
133
134 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pgoParams.size()));
135 for (const auto &pgoParam : pgoParams) {
136 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pgoParam.first));
137 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pgoParam.second));
138 }
139
140 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, static_cast<uint32_t>(parameters.size()));
141 for (const auto ¶meter : parameters) {
142 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(parameter.first));
143 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(parameter.second));
144 }
145 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isPatch);
146 return true;
147 }
148
ReadFromParcel(Parcel & parcel)149 bool UninstallParam::ReadFromParcel(Parcel &parcel)
150 {
151 bundleName = Str16ToStr8(parcel.ReadString16());
152 moduleName = Str16ToStr8(parcel.ReadString16());
153 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, versionCode);
154 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
155 return true;
156 }
157
Unmarshalling(Parcel & parcel)158 UninstallParam* UninstallParam::Unmarshalling(Parcel &parcel)
159 {
160 UninstallParam *info = new (std::nothrow) UninstallParam();
161 if (info && !info->ReadFromParcel(parcel)) {
162 APP_LOGW("read from parcel failed");
163 delete info;
164 info = nullptr;
165 }
166 return info;
167 }
168
Marshalling(Parcel & parcel) const169 bool UninstallParam::Marshalling(Parcel &parcel) const
170 {
171 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
172 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
173 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, versionCode);
174 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
175 return true;
176 }
177
CheckPermission() const178 bool InstallParam::CheckPermission() const
179 {
180 const int32_t FOUNDATION_UID = 5523;
181 if (IPCSkeleton::GetCallingUid() != FOUNDATION_UID) {
182 APP_LOGE("set installParam failed");
183 return false;
184 }
185 return true;
186 }
187 } // namespace AppExecFwk
188 } // namespace OHOS
189