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)200 int32_t SecCompProxy::SendReportClickEventRequest(MessageParcel& data)
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 return res;
230 }
231
ReportSecurityComponentClickEvent(int32_t scId,const std::string & componentInfo,const SecCompClickEvent & clickInfo,sptr<IRemoteObject> callerToken,sptr<IRemoteObject> dialogCallback)232 int32_t SecCompProxy::ReportSecurityComponentClickEvent(int32_t scId,
233 const std::string& componentInfo, const SecCompClickEvent& clickInfo,
234 sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback)
235 {
236 std::lock_guard<std::mutex> lock(useIPCMutex_);
237 MessageParcel rawData;
238 MessageParcel data;
239 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
240 SC_LOG_ERROR(LABEL, "Report write descriptor failed.");
241 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
242 }
243
244 if (!rawData.WriteInt32(scId)) {
245 SC_LOG_ERROR(LABEL, "Report write scId failed.");
246 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
247 }
248
249 if (!rawData.WriteString(componentInfo)) {
250 SC_LOG_ERROR(LABEL, "Report write componentInfo failed.");
251 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
252 }
253
254 sptr<SecCompClickEventParcel> parcel = new (std::nothrow) SecCompClickEventParcel();
255 if (parcel == nullptr) {
256 SC_LOG_ERROR(LABEL, "Report new click event parcel failed.");
257 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
258 }
259 parcel->clickInfoParams_ = clickInfo;
260 if (!rawData.WriteParcelable(parcel)) {
261 SC_LOG_ERROR(LABEL, "Report write clickInfo failed.");
262 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
263 }
264
265 if ((callerToken != nullptr) && !data.WriteRemoteObject(callerToken)) {
266 SC_LOG_ERROR(LABEL, "Report write caller token failed.");
267 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
268 }
269
270 if ((dialogCallback != nullptr) && !data.WriteRemoteObject(dialogCallback)) {
271 SC_LOG_ERROR(LABEL, "Report write caller token failed.");
272 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
273 }
274
275 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
276 SC_LOG_ERROR(LABEL, "Report serialize session info failed.");
277 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
278 }
279
280 return SendReportClickEventRequest(data);
281 }
282
VerifySavePermission(AccessToken::AccessTokenID tokenId)283 bool SecCompProxy::VerifySavePermission(AccessToken::AccessTokenID tokenId)
284 {
285 std::lock_guard<std::mutex> lock(useIPCMutex_);
286 MessageParcel data;
287 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
288 SC_LOG_ERROR(LABEL, "Verify write descriptor failed.");
289 return false;
290 }
291 if (!data.WriteUint32(tokenId)) {
292 SC_LOG_ERROR(LABEL, "Verify write tokenId failed.");
293 return false;
294 }
295
296 MessageParcel reply;
297 MessageOption option(MessageOption::TF_SYNC);
298 sptr<IRemoteObject> remote = Remote();
299 if (remote == nullptr) {
300 SC_LOG_ERROR(LABEL, "Verify remote service is null.");
301 return false;
302 }
303 int32_t requestResult = remote->SendRequest(
304 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::VERIFY_TEMP_SAVE_PERMISSION),
305 data, reply, option);
306 if (requestResult != SC_OK) {
307 SC_LOG_ERROR(LABEL, "Verify request failed, result: %{public}d.", requestResult);
308 return false;
309 }
310 bool res;
311 if (!reply.ReadBool(res)) {
312 SC_LOG_ERROR(LABEL, "Verify read res failed.");
313 return false;
314 }
315 return res;
316 }
317
GetEnhanceRemoteObject()318 sptr<IRemoteObject> SecCompProxy::GetEnhanceRemoteObject()
319 {
320 std::lock_guard<std::mutex> lock(useIPCMutex_);
321 MessageParcel rawData;
322 MessageParcel data;
323 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
324 SC_LOG_ERROR(LABEL, "Get enhance write descriptor failed.");
325 return nullptr;
326 }
327
328 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
329 SC_LOG_ERROR(LABEL, "Get enhance serialize session info failed.");
330 return nullptr;
331 }
332
333 MessageParcel reply;
334 MessageParcel deserializedReply;
335 MessageOption option(MessageOption::TF_SYNC);
336 sptr<IRemoteObject> remote = Remote();
337 if (remote == nullptr) {
338 SC_LOG_ERROR(LABEL, "Get enhance remote service is null.");
339 return nullptr;
340 }
341 int32_t requestResult = remote->SendRequest(
342 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT),
343 data, reply, option);
344
345 sptr<IRemoteObject> callback;
346 if (requestResult == SC_OK) {
347 callback = reply.ReadRemoteObject();
348 if (callback == nullptr) {
349 SC_LOG_ERROR(LABEL, "Get enhance read callback failed.");
350 }
351 } else {
352 SC_LOG_ERROR(LABEL, "Get enhance request failed, result: %{public}d.", requestResult);
353 }
354
355 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
356 SC_LOG_ERROR(LABEL, "Get enhance deserialize session info failed.");
357 }
358
359 return callback;
360 }
361
PreRegisterSecCompProcess()362 int32_t SecCompProxy::PreRegisterSecCompProcess()
363 {
364 std::lock_guard<std::mutex> lock(useIPCMutex_);
365 MessageParcel rawData;
366 MessageParcel data;
367 if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) {
368 SC_LOG_ERROR(LABEL, "PreRegister write descriptor failed.");
369 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
370 }
371
372 if (!SecCompEnhanceAdapter::EnhanceClientSerialize(rawData, data)) {
373 SC_LOG_ERROR(LABEL, "PreRegister serialize session info failed.");
374 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
375 }
376
377 MessageParcel reply;
378 MessageParcel deserializedReply;
379 MessageOption option(MessageOption::TF_SYNC);
380 sptr<IRemoteObject> remote = Remote();
381 if (remote == nullptr) {
382 SC_LOG_ERROR(LABEL, "PreRegister remote service is null.");
383 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
384 }
385 int32_t requestResult = remote->SendRequest(
386 static_cast<uint32_t>(SecurityComponentServiceInterfaceCode::PRE_REGISTER_PROCESS),
387 data, reply, option);
388
389 if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(reply, deserializedReply)) {
390 SC_LOG_ERROR(LABEL, "PreRegister deserialize session info failed.");
391 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
392 }
393
394 if (requestResult != SC_OK) {
395 SC_LOG_ERROR(LABEL, "PreRegister request failed, result: %{public}d.", requestResult);
396 return requestResult;
397 }
398
399 int32_t res;
400 if (!deserializedReply.ReadInt32(res)) {
401 SC_LOG_ERROR(LABEL, "PreRegister read res failed.");
402 return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
403 }
404 return res;
405 }
406 } // namespace SecurityComponent
407 } // namespace Security
408 } // namespace OHOS
409