1 /*
2 * Copyright (c) 2023 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 #include "sec_comp_proxy.h"
16
17 #include "sec_comp_click_event_parcel.h"
18 #include "sec_comp_enhance_adapter.h"
19 #include "sec_comp_err.h"
20 #include "sec_comp_log.h"
21 #include <mutex>
22
23 namespace OHOS {
24 namespace Security {
25 namespace SecurityComponent {
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompProxy"};
28 }
29
SecCompProxy(const sptr<IRemoteObject> & impl)30 SecCompProxy::SecCompProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<ISecCompService>(impl)
31 {}
32
~SecCompProxy()33 SecCompProxy::~SecCompProxy()
34 {}
35
RegisterSecurityComponent(SecCompType type,const std::string & componentInfo,int32_t & scId)36 int32_t SecCompProxy::RegisterSecurityComponent(SecCompType type,
37 const std::string& componentInfo, int32_t& scId)
38 {
39 std::lock_guard<std::mutex> lock(useIPCMutex_);
40 MessageParcel rawData;
41 MessageParcel data;
42 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
43 SC_LOG_ERROR(LABEL, "Register write descriptor failed.");
44 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
45 }
46
47 if (!rawData.WriteUint32(type)) {
48 SC_LOG_ERROR(LABEL, "Register write type failed.");
49 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
50 }
51
52 if (!rawData.WriteString(componentInfo)) {
53 SC_LOG_ERROR(LABEL, "Register write componentInfo failed.");
54 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
55 }
56
57 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
58 SC_LOG_ERROR(LABEL, "Register serialize session info failed.");
59 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
60 }
61
62 MessageParcel reply;
63 MessageParcel deserializedReply;
64 MessageOption option(MessageOption::TF_SYNC);
65 sptr<IRemoteObject> remote = Remote();
66 if (remote == nullptr) {
67 SC_LOG_ERROR(LABEL, "Register remote service is null.");
68 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
69 }
70 int32_t requestResult = remote->SendRequest(
71 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::REGISTER_SECURITY_COMPONENT),
72 data, reply, option);
73
74 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
75 SC_LOG_ERROR(LABEL, "Register deserialize session info failed.");
76 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
77 }
78
79 if (requestResult != SC_OK) {
80 SC_LOG_ERROR(LABEL, "Register request failed, result: %{public}d.", requestResult);
81 return requestResult;
82 }
83
84 int32_t res;
85 if (!deserializedReply.ReadInt32(res)) {
86 SC_LOG_ERROR(LABEL, "Register read res failed.");
87 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
88 }
89
90 if (!deserializedReply.ReadInt32(scId)) {
91 SC_LOG_ERROR(LABEL, "Register read scId failed.");
92 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
93 }
94 return res;
95 }
96
UpdateSecurityComponent(int32_t scId,const std::string & componentInfo)97 int32_t SecCompProxy::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo)
98 {
99 std::lock_guard<std::mutex> lock(useIPCMutex_);
100 MessageParcel rawData;
101 MessageParcel data;
102 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
103 SC_LOG_ERROR(LABEL, "Update write descriptor failed.");
104 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
105 }
106
107 if (!rawData.WriteInt32(scId)) {
108 SC_LOG_ERROR(LABEL, "Update write scId failed.");
109 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
110 }
111 if (!rawData.WriteString(componentInfo)) {
112 SC_LOG_ERROR(LABEL, "Update write componentInfo failed.");
113 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
114 }
115
116 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
117 SC_LOG_ERROR(LABEL, "Update serialize session info failed.");
118 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
119 }
120
121 MessageParcel reply;
122 MessageParcel deserializedReply;
123 MessageOption option(MessageOption::TF_SYNC);
124 sptr<IRemoteObject> remote = Remote();
125 if (remote == nullptr) {
126 SC_LOG_ERROR(LABEL, "Update remote update service is null.");
127 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
128 }
129 int32_t requestResult = remote->SendRequest(
130 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::UPDATE_SECURITY_COMPONENT), data, reply, option);
131
132 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
133 SC_LOG_ERROR(LABEL, "Update deserialize session info failed.");
134 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
135 }
136
137 if (requestResult != SC_OK) {
138 SC_LOG_ERROR(LABEL, "Update request failed, result: %{public}d.", requestResult);
139 return requestResult;
140 }
141
142 int32_t res;
143 if (!deserializedReply.ReadInt32(res)) {
144 SC_LOG_ERROR(LABEL, "Update read res failed.");
145 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
146 }
147 return res;
148 }
149
UnregisterSecurityComponent(int32_t scId)150 int32_t SecCompProxy::UnregisterSecurityComponent(int32_t scId)
151 {
152 std::lock_guard<std::mutex> lock(useIPCMutex_);
153 MessageParcel rawData;
154 MessageParcel data;
155 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
156 SC_LOG_ERROR(LABEL, "Unregister write descriptor failed.");
157 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
158 }
159
160 if (!rawData.WriteInt32(scId)) {
161 SC_LOG_ERROR(LABEL, "Unregister write scId failed.");
162 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
163 }
164
165 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
166 SC_LOG_ERROR(LABEL, "Unregister serialize session info failed.");
167 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
168 }
169
170 MessageParcel reply;
171 MessageParcel deserializedReply;
172 MessageOption option(MessageOption::TF_SYNC);
173 sptr<IRemoteObject> remote = Remote();
174 if (remote == nullptr) {
175 SC_LOG_ERROR(LABEL, "Unregister remote service is null.");
176 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
177 }
178 int32_t requestResult = remote->SendRequest(
179 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::UNREGISTER_SECURITY_COMPONENT),
180 data, reply, option);
181
182 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
183 SC_LOG_ERROR(LABEL, "Unregister deserialize session info failed.");
184 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
185 }
186
187 if (requestResult != SC_OK) {
188 SC_LOG_ERROR(LABEL, "Unregister request failed, result: %{public}d.", requestResult);
189 return requestResult;
190 }
191
192 int32_t res;
193 if (!deserializedReply.ReadInt32(res)) {
194 SC_LOG_ERROR(LABEL, "Unregister read res failed.");
195 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
196 }
197 return res;
198 }
199
SendReportClickEventRequest(MessageParcel & data,std::string & message)200 int32_t SecCompProxy::SendReportClickEventRequest(MessageParcel& data, std::string& message)
201 {
202 MessageParcel reply;
203 MessageParcel deserializedReply;
204 MessageOption option(MessageOption::TF_SYNC);
205 sptr<IRemoteObject> remote = Remote();
206 if (remote == nullptr) {
207 SC_LOG_ERROR(LABEL, "Report remote service is null.");
208 return SC_SERVICE_ERROR_IPC_REQUEST_FAIL;
209 }
210 int32_t requestResult = remote->SendRequest(
211 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::REPORT_SECURITY_COMPONENT_CLICK_EVENT),
212 data, reply, option);
213
214 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
215 SC_LOG_ERROR(LABEL, "Report deserialize session info failed.");
216 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
217 }
218
219 if (requestResult != SC_OK) {
220 SC_LOG_ERROR(LABEL, "Report request failed, result: %{public}d.", requestResult);
221 return requestResult;
222 }
223
224 int32_t res;
225 if (!deserializedReply.ReadInt32(res)) {
226 SC_LOG_ERROR(LABEL, "Report read res failed.");
227 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
228 }
229
230 if (!deserializedReply.ReadString(message)) {
231 SC_LOG_ERROR(LABEL, "Report read error message failed.");
232 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
233 }
234 return res;
235 }
236
ReportSecurityComponentClickEvent(SecCompInfo & secCompInfo,sptr<IRemoteObject> callerToken,sptr<IRemoteObject> dialogCallback,std::string & message)237 int32_t SecCompProxy::ReportSecurityComponentClickEvent(SecCompInfo& secCompInfo,
238 sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback, std::string& message)
239 {
240 std::lock_guard<std::mutex> lock(useIPCMutex_);
241 MessageParcel rawData;
242 MessageParcel data;
243 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
244 SC_LOG_ERROR(LABEL, "Report write descriptor failed.");
245 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
246 }
247
248 if (!rawData.WriteInt32(secCompInfo.scId)) {
249 SC_LOG_ERROR(LABEL, "Report write scId failed.");
250 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
251 }
252
253 if (!rawData.WriteString(secCompInfo.componentInfo)) {
254 SC_LOG_ERROR(LABEL, "Report write componentInfo failed.");
255 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
256 }
257
258 sptr<SecCompClickEventParcel> parcel = new (std::nothrow) SecCompClickEventParcel();
259 if (parcel == nullptr) {
260 SC_LOG_ERROR(LABEL, "Report new click event parcel failed.");
261 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
262 }
263 parcel->clickInfoParams_ = secCompInfo.clickInfo;
264 if (!rawData.WriteParcelable(parcel)) {
265 SC_LOG_ERROR(LABEL, "Report write clickInfo failed.");
266 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
267 }
268
269 if ((callerToken != nullptr) && !data.WriteRemoteObject(callerToken)) {
270 SC_LOG_ERROR(LABEL, "Report write caller token failed.");
271 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
272 }
273
274 if ((dialogCallback != nullptr) && !data.WriteRemoteObject(dialogCallback)) {
275 SC_LOG_ERROR(LABEL, "Report write caller token failed.");
276 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
277 }
278
279 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
280 SC_LOG_ERROR(LABEL, "Report serialize session info failed.");
281 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
282 }
283
284 return SendReportClickEventRequest(data, message);
285 }
286
VerifySavePermission(AccessToken::AccessTokenID tokenId)287 bool SecCompProxy::VerifySavePermission(AccessToken::AccessTokenID tokenId)
288 {
289 std::lock_guard<std::mutex> lock(useIPCMutex_);
290 MessageParcel data;
291 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
292 SC_LOG_ERROR(LABEL, "Verify write descriptor failed.");
293 return false;
294 }
295 if (!data.WriteUint32(tokenId)) {
296 SC_LOG_ERROR(LABEL, "Verify write tokenId failed.");
297 return false;
298 }
299
300 MessageParcel reply;
301 MessageOption option(MessageOption::TF_SYNC);
302 sptr<IRemoteObject> remote = Remote();
303 if (remote == nullptr) {
304 SC_LOG_ERROR(LABEL, "Verify remote service is null.");
305 return false;
306 }
307 int32_t requestResult = remote->SendRequest(
308 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION),
309 data, reply, option);
310 if (requestResult != SC_OK) {
311 SC_LOG_ERROR(LABEL, "Verify request failed, result: %{public}d.", requestResult);
312 return false;
313 }
314 bool res;
315 if (!reply.ReadBool(res)) {
316 SC_LOG_ERROR(LABEL, "Verify read res failed.");
317 return false;
318 }
319 return res;
320 }
321
GetEnhanceRemoteObject()322 sptr<IRemoteObject> SecCompProxy::GetEnhanceRemoteObject()
323 {
324 std::lock_guard<std::mutex> lock(useIPCMutex_);
325 MessageParcel rawData;
326 MessageParcel data;
327 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
328 SC_LOG_ERROR(LABEL, "Get enhance write descriptor failed.");
329 return nullptr;
330 }
331
332 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
333 SC_LOG_ERROR(LABEL, "Get enhance serialize session info failed.");
334 return nullptr;
335 }
336
337 MessageParcel reply;
338 MessageParcel deserializedReply;
339 MessageOption option(MessageOption::TF_SYNC);
340 sptr<IRemoteObject> remote = Remote();
341 if (remote == nullptr) {
342 SC_LOG_ERROR(LABEL, "Get enhance remote service is null.");
343 return nullptr;
344 }
345 int32_t requestResult = remote->SendRequest(
346 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT),
347 data, reply, option);
348
349 sptr<IRemoteObject> callback;
350 if (requestResult == SC_OK) {
351 callback = reply.ReadRemoteObject();
352 if (callback == nullptr) {
353 SC_LOG_ERROR(LABEL, "Get enhance read callback failed.");
354 }
355 } else {
356 SC_LOG_ERROR(LABEL, "Get enhance request failed, result: %{public}d.", requestResult);
357 }
358
359 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
360 SC_LOG_ERROR(LABEL, "Get enhance deserialize session info failed.");
361 }
362
363 return callback;
364 }
365
PreRegisterSecCompProcess()366 int32_t SecCompProxy::PreRegisterSecCompProcess()
367 {
368 std::lock_guard<std::mutex> lock(useIPCMutex_);
369 MessageParcel rawData;
370 MessageParcel data;
371 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
372 SC_LOG_ERROR(LABEL, "PreRegister write descriptor failed.");
373 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
374 }
375
376 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
377 SC_LOG_ERROR(LABEL, "PreRegister serialize session info failed.");
378 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
379 }
380
381 MessageParcel reply;
382 MessageParcel deserializedReply;
383 MessageOption option(MessageOption::TF_SYNC);
384 sptr<IRemoteObject> remote = Remote();
385 if (remote == nullptr) {
386 SC_LOG_ERROR(LABEL, "PreRegister remote service is null.");
387 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
388 }
389 int32_t requestResult = remote->SendRequest(
390 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::PRE_REGISTER_PROCESS),
391 data, reply, option);
392
393 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
394 SC_LOG_ERROR(LABEL, "PreRegister deserialize session info failed.");
395 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
396 }
397
398 if (requestResult != SC_OK) {
399 SC_LOG_ERROR(LABEL, "PreRegister request failed, result: %{public}d.", requestResult);
400 return requestResult;
401 }
402
403 int32_t res;
404 if (!deserializedReply.ReadInt32(res)) {
405 SC_LOG_ERROR(LABEL, "PreRegister read res failed.");
406 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
407 }
408 return res;
409 }
410 } // namespace SecurityComponent
411 } // namespace Security
412 } // namespace OHOS
413