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 "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
60 int32_t verifyCodeParamSize;
61 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, verifyCodeParamSize);
62 CONTAINER_SECURITY_VERIFY(parcel, verifyCodeParamSize, &verifyCodeParams);
63 for (int32_t i = 0; i < verifyCodeParamSize; ++i) {
64 std::string moduleName = Str16ToStr8(parcel.ReadString16());
65 std::string signatureFilePath = Str16ToStr8(parcel.ReadString16());
66 verifyCodeParams.emplace(moduleName, signatureFilePath);
67 }
68 isSelfUpdate = parcel.ReadBool();
69
70 int32_t pgoParamsSize;
71 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pgoParamsSize);
72 CONTAINER_SECURITY_VERIFY(parcel, pgoParamsSize, &pgoParams);
73 for (int32_t i = 0; i < pgoParamsSize; ++i) {
74 std::string moduleName = Str16ToStr8(parcel.ReadString16());
75 std::string pgoPath = Str16ToStr8(parcel.ReadString16());
76 pgoParams.emplace(moduleName, pgoPath);
77 }
78 return true;
79 }
80
Unmarshalling(Parcel & parcel)81 InstallParam *InstallParam::Unmarshalling(Parcel &parcel)
82 {
83 InstallParam *info = new (std::nothrow) InstallParam();
84 if (info && !info->ReadFromParcel(parcel)) {
85 APP_LOGW("read from parcel failed");
86 delete info;
87 info = nullptr;
88 }
89 return info;
90 }
91
Marshalling(Parcel & parcel) const92 bool InstallParam::Marshalling(Parcel &parcel) const
93 {
94 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(installFlag));
95 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(installLocation));
96 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
97 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepData);
98
99 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(hashParams.size()));
100 for (const auto &hashParam : hashParams) {
101 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hashParam.first));
102 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hashParam.second));
103 }
104 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, crowdtestDeadline);
105
106 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(sharedBundleDirPaths.size()));
107 for (const auto& sharedBundleDirPath : sharedBundleDirPaths) {
108 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(sharedBundleDirPath));
109 }
110
111 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(specifiedDistributionType));
112 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(additionalInfo));
113 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(verifyCodeParams.size()));
114 for (const auto &verifyCodeParam : verifyCodeParams) {
115 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(verifyCodeParam.first));
116 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(verifyCodeParam.second));
117 }
118 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isSelfUpdate);
119
120 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pgoParams.size()));
121 for (const auto &pgoParam : pgoParams) {
122 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pgoParam.first));
123 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pgoParam.second));
124 }
125 return true;
126 }
127
ReadFromParcel(Parcel & parcel)128 bool UninstallParam::ReadFromParcel(Parcel &parcel)
129 {
130 bundleName = Str16ToStr8(parcel.ReadString16());
131 moduleName = Str16ToStr8(parcel.ReadString16());
132 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, versionCode);
133 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
134 return true;
135 }
136
Unmarshalling(Parcel & parcel)137 UninstallParam* UninstallParam::Unmarshalling(Parcel &parcel)
138 {
139 UninstallParam *info = new (std::nothrow) UninstallParam();
140 if (info && !info->ReadFromParcel(parcel)) {
141 APP_LOGW("read from parcel failed");
142 delete info;
143 info = nullptr;
144 }
145 return info;
146 }
147
Marshalling(Parcel & parcel) const148 bool UninstallParam::Marshalling(Parcel &parcel) const
149 {
150 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
151 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
152 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, versionCode);
153 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
154 return true;
155 }
156
CheckPermission() const157 bool InstallParam::CheckPermission() const
158 {
159 const int32_t FOUNDATION_UID = 5523;
160 if (IPCSkeleton::GetCallingUid() != FOUNDATION_UID) {
161 APP_LOGE("set installParam failed");
162 return false;
163 }
164 return true;
165 }
166 } // namespace AppExecFwk
167 } // namespace OHOS
168