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 
79     int32_t parametersSize;
80     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, parametersSize);
81     CONTAINER_SECURITY_VERIFY(parcel, parametersSize, &parameters);
82     for (int32_t i = 0; i < parametersSize; ++i) {
83         std::string key = Str16ToStr8(parcel.ReadString16());
84         std::string value = Str16ToStr8(parcel.ReadString16());
85         parameters.emplace(key, value);
86     }
87     return true;
88 }
89 
Unmarshalling(Parcel & parcel)90 InstallParam *InstallParam::Unmarshalling(Parcel &parcel)
91 {
92     InstallParam *info = new (std::nothrow) InstallParam();
93     if (info && !info->ReadFromParcel(parcel)) {
94         APP_LOGW("read from parcel failed");
95         delete info;
96         info = nullptr;
97     }
98     return info;
99 }
100 
Marshalling(Parcel & parcel) const101 bool InstallParam::Marshalling(Parcel &parcel) const
102 {
103     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(installFlag));
104     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(installLocation));
105     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
106     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepData);
107 
108     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(hashParams.size()));
109     for (const auto &hashParam : hashParams) {
110         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hashParam.first));
111         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hashParam.second));
112     }
113     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, crowdtestDeadline);
114 
115     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(sharedBundleDirPaths.size()));
116     for (const auto& sharedBundleDirPath : sharedBundleDirPaths) {
117         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(sharedBundleDirPath));
118     }
119 
120     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(specifiedDistributionType));
121     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(additionalInfo));
122     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(verifyCodeParams.size()));
123     for (const auto &verifyCodeParam : verifyCodeParams) {
124         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(verifyCodeParam.first));
125         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(verifyCodeParam.second));
126     }
127     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isSelfUpdate);
128 
129     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pgoParams.size()));
130     for (const auto &pgoParam : pgoParams) {
131         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pgoParam.first));
132         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(pgoParam.second));
133     }
134 
135     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(parameters.size()));
136     for (const auto &parameter : parameters) {
137         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(parameter.first));
138         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(parameter.second));
139     }
140     return true;
141 }
142 
ReadFromParcel(Parcel & parcel)143 bool UninstallParam::ReadFromParcel(Parcel &parcel)
144 {
145     bundleName = Str16ToStr8(parcel.ReadString16());
146     moduleName = Str16ToStr8(parcel.ReadString16());
147     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, versionCode);
148     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
149     return true;
150 }
151 
Unmarshalling(Parcel & parcel)152 UninstallParam* UninstallParam::Unmarshalling(Parcel &parcel)
153 {
154     UninstallParam *info = new (std::nothrow) UninstallParam();
155     if (info && !info->ReadFromParcel(parcel)) {
156         APP_LOGW("read from parcel failed");
157         delete info;
158         info = nullptr;
159     }
160     return info;
161 }
162 
Marshalling(Parcel & parcel) const163 bool UninstallParam::Marshalling(Parcel &parcel) const
164 {
165     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
166     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
167     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, versionCode);
168     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, userId);
169     return true;
170 }
171 
CheckPermission() const172 bool InstallParam::CheckPermission() const
173 {
174     const int32_t FOUNDATION_UID = 5523;
175     if (IPCSkeleton::GetCallingUid() != FOUNDATION_UID) {
176         APP_LOGE("set installParam failed");
177         return false;
178     }
179     return true;
180 }
181 }  // namespace AppExecFwk
182 }  // namespace OHOS
183