1 /*
2 * Copyright (C) 2021-2025 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 "ipc_adapt.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "ipc_callback_proxy.h"
21 #include "ipc_callback_stub.h"
22 #include "ipc_dev_auth_proxy.h"
23 #include "ipc_dev_auth_stub.h"
24 #include "ipc_sdk_defines.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "securec.h"
28 #include "system_ability_definition.h"
29 #include "parameter.h"
30 #include "sa_load_on_demand.h"
31 #include "string_util.h"
32
33 using namespace std;
34 using namespace OHOS;
35 namespace {
36 static const int32_t BUFF_MAX_SZ = 128;
37 static const int32_t IPC_CALL_BACK_MAX_NODES = 64;
38 static const int32_t IPC_CALL_BACK_STUB_NODES = 4;
39 static const uint32_t DEV_AUTH_MAX_THREAD_NUM = 2;
40 }
41
42 static sptr<StubDevAuthCb> g_sdkCbStub[IPC_CALL_BACK_STUB_NODES] = { nullptr, nullptr, nullptr, nullptr };
43
44 typedef struct {
45 uintptr_t cbHook;
46 const IpcDataInfo *cbDataCache;
47 int32_t cacheNum;
48 MessageParcel &reply;
49 } CallbackParams;
50
51 typedef void (*CallbackStub)(CallbackParams params);
52 typedef struct {
53 union {
54 DeviceAuthCallback devAuth;
55 DataChangeListener listener;
56 CredChangeListener credListener;
57 } cbCtx;
58 int64_t requestId;
59 char appId[BUFF_MAX_SZ];
60 int32_t cbType;
61 int32_t delOnFni;
62 int32_t methodId;
63 int32_t proxyId;
64 int32_t nodeIdx;
65 } IpcCallBackNode;
66
67 static struct {
68 IpcCallBackNode *ctx;
69 int32_t nodeCnt;
70 } g_ipcCallBackList = {nullptr, 0};
71 static std::mutex g_cbListLock;
72
SetIpcCallBackNodeDefault(IpcCallBackNode & node)73 static void SetIpcCallBackNodeDefault(IpcCallBackNode &node)
74 {
75 (void)memset_s(&node, sizeof(IpcCallBackNode), 0, sizeof(IpcCallBackNode));
76 node.proxyId = -1;
77 node.nodeIdx = -1;
78 return;
79 }
80
InitIpcCallBackList(void)81 int32_t InitIpcCallBackList(void)
82 {
83 int32_t i;
84
85 LOGI("initializing ...");
86 if (g_ipcCallBackList.ctx != nullptr) {
87 LOGI("has initialized");
88 return HC_SUCCESS;
89 }
90
91 g_ipcCallBackList.ctx = new(std::nothrow) IpcCallBackNode[IPC_CALL_BACK_MAX_NODES];
92 if (g_ipcCallBackList.ctx == nullptr) {
93 LOGE("initialized failed");
94 return HC_ERROR;
95 }
96 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
97 SetIpcCallBackNodeDefault(g_ipcCallBackList.ctx[i]);
98 }
99 g_ipcCallBackList.nodeCnt = 0;
100 LOGI("initialized successful");
101 return HC_SUCCESS;
102 }
103
ResetIpcCallBackNode(IpcCallBackNode & node)104 static void ResetIpcCallBackNode(IpcCallBackNode &node)
105 {
106 char errStr[] = "invalid";
107 char *appId = errStr;
108 if ((node.appId[0] != 0) && (node.appId[sizeof(node.appId) - 1] == 0)) {
109 appId = node.appId;
110 }
111 LOGI("appid is %" LOG_PUB "s ", appId);
112 ServiceDevAuth::ResetRemoteObject(node.proxyId);
113 SetIpcCallBackNodeDefault(node);
114 return;
115 }
116
DeInitIpcCallBackList(void)117 void DeInitIpcCallBackList(void)
118 {
119 int32_t i;
120
121 std::lock_guard<std::mutex> autoLock(g_cbListLock);
122 if (g_ipcCallBackList.ctx == nullptr) {
123 return;
124 }
125 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
126 ResetIpcCallBackNode(g_ipcCallBackList.ctx[i]);
127 }
128 delete[] g_ipcCallBackList.ctx;
129 g_ipcCallBackList.ctx = nullptr;
130 return;
131 }
132
ResetIpcCallBackNodeByNodeId(int32_t nodeIdx)133 void ResetIpcCallBackNodeByNodeId(int32_t nodeIdx)
134 {
135 LOGI("starting..., index %" LOG_PUB "d", nodeIdx);
136 if ((nodeIdx < 0) || (nodeIdx >= IPC_CALL_BACK_MAX_NODES)) {
137 LOGW("Invalid node index: %" LOG_PUB "d", nodeIdx);
138 return;
139 }
140 std::lock_guard<std::mutex> autoLock(g_cbListLock);
141 if (g_ipcCallBackList.ctx == nullptr) {
142 LOGW("Callback node list is null!");
143 return;
144 }
145 if (g_ipcCallBackList.ctx[nodeIdx].proxyId < 0) {
146 LOGW("Invalid node proxy id: %" LOG_PUB "d", g_ipcCallBackList.ctx[nodeIdx].proxyId);
147 return;
148 }
149 ResetIpcCallBackNode(g_ipcCallBackList.ctx[nodeIdx]);
150 g_ipcCallBackList.nodeCnt--;
151 LOGI("done, index %" LOG_PUB "d", nodeIdx);
152 return;
153 }
154
GetIpcCallBackByAppId(const char * appId,int32_t type)155 static IpcCallBackNode *GetIpcCallBackByAppId(const char *appId, int32_t type)
156 {
157 int32_t i;
158
159 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
160 if (g_ipcCallBackList.ctx[i].appId[0] == 0) {
161 continue;
162 }
163 if (IsStrEqual(g_ipcCallBackList.ctx[i].appId, appId) && (g_ipcCallBackList.ctx[i].cbType == type)) {
164 return &g_ipcCallBackList.ctx[i];
165 }
166 }
167 return nullptr;
168 }
169
GetFreeIpcCallBackNode(void)170 static IpcCallBackNode *GetFreeIpcCallBackNode(void)
171 {
172 int32_t i;
173
174 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
175 if ((g_ipcCallBackList.ctx[i].appId[0] == 0) && (g_ipcCallBackList.ctx[i].cbType == 0)) {
176 g_ipcCallBackList.ctx[i].nodeIdx = i;
177 return &g_ipcCallBackList.ctx[i];
178 }
179 }
180 return nullptr;
181 }
182
SetCbDeathRecipient(int32_t type,int32_t objIdx,int32_t cbDataIdx)183 static void SetCbDeathRecipient(int32_t type, int32_t objIdx, int32_t cbDataIdx)
184 {
185 if ((type == CB_TYPE_DEV_AUTH) || (type == CB_TYPE_LISTENER)) {
186 ServiceDevAuth::AddCbDeathRecipient(objIdx, cbDataIdx);
187 }
188 return;
189 }
190
AddIpcCbObjByAppId(const char * appId,int32_t objIdx,int32_t type)191 void AddIpcCbObjByAppId(const char *appId, int32_t objIdx, int32_t type)
192 {
193 IpcCallBackNode *node = nullptr;
194
195 std::lock_guard<std::mutex> autoLock(g_cbListLock);
196 if (g_ipcCallBackList.ctx == nullptr) {
197 LOGE("CallbackList is not init.");
198 return;
199 }
200
201 if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
202 LOGE("CallbackList is full.");
203 return;
204 }
205
206 node = GetIpcCallBackByAppId(appId, type);
207 if (node != nullptr) {
208 node->proxyId = objIdx;
209 SetCbDeathRecipient(type, objIdx, node->nodeIdx);
210 LOGI("ipc object add success, appid: %" LOG_PUB "s, proxyId %" LOG_PUB "d", appId, node->proxyId);
211 }
212 return;
213 }
214
AddIpcCallBackByAppId(const char * appId,const uint8_t * cbPtr,int32_t cbSz,int32_t type)215 int32_t AddIpcCallBackByAppId(const char *appId, const uint8_t *cbPtr, int32_t cbSz, int32_t type)
216 {
217 IpcCallBackNode *node = nullptr;
218 errno_t eno;
219
220 std::lock_guard<std::mutex> autoLock(g_cbListLock);
221 if (g_ipcCallBackList.ctx == nullptr) {
222 LOGE("list not inited");
223 return HC_ERROR;
224 }
225
226 if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
227 LOGE("list is full");
228 return HC_ERROR;
229 }
230
231 node = GetIpcCallBackByAppId(appId, type);
232 if (node != nullptr) {
233 eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
234 if (eno != EOK) {
235 LOGE("Callback context memory copy failed");
236 return HC_ERROR;
237 }
238 if (node->proxyId >= 0) {
239 ServiceDevAuth::ResetRemoteObject(node->proxyId);
240 node->proxyId = -1;
241 }
242 LOGI("Callback add success, appid: %" LOG_PUB "s", appId);
243 return HC_SUCCESS;
244 }
245
246 LOGI("new callback to add, appid: %" LOG_PUB "s", appId);
247 node = GetFreeIpcCallBackNode();
248 if (node == nullptr) {
249 LOGE("get free node failed");
250 return HC_ERROR;
251 }
252 node->cbType = type;
253 eno = memcpy_s(&(node->appId), sizeof(node->appId), appId, HcStrlen(appId) + 1);
254 if (eno != EOK) {
255 ResetIpcCallBackNode(*node);
256 LOGE("appid memory copy failed");
257 return HC_ERROR;
258 }
259 eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
260 if (eno != EOK) {
261 ResetIpcCallBackNode(*node);
262 LOGE("callback context memory copy failed");
263 return HC_ERROR;
264 }
265 node->proxyId = -1;
266 g_ipcCallBackList.nodeCnt++;
267 LOGI("callback add success, appid: %" LOG_PUB "s, type %" LOG_PUB "d", node->appId, node->cbType);
268 return HC_SUCCESS;
269 }
270
DelIpcCallBackByAppId(const char * appId,int32_t type)271 void DelIpcCallBackByAppId(const char *appId, int32_t type)
272 {
273 IpcCallBackNode *node = nullptr;
274
275 std::lock_guard<std::mutex> autoLock(g_cbListLock);
276 if ((g_ipcCallBackList.nodeCnt <= 0) || (g_ipcCallBackList.ctx == nullptr)) {
277 return;
278 }
279
280 node = GetIpcCallBackByAppId(appId, type);
281 if (node != nullptr) {
282 ResetIpcCallBackNode(*node);
283 g_ipcCallBackList.nodeCnt--;
284 }
285 return;
286 }
287
GetIpcCallBackByReqId(int64_t reqId,int32_t type)288 static IpcCallBackNode *GetIpcCallBackByReqId(int64_t reqId, int32_t type)
289 {
290 int32_t i;
291
292 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
293 if ((reqId == g_ipcCallBackList.ctx[i].requestId) &&
294 (g_ipcCallBackList.ctx[i].cbType == type)) {
295 return &g_ipcCallBackList.ctx[i];
296 }
297 }
298 return nullptr;
299 }
300
AddReqIdByAppId(const char * appId,int64_t reqId)301 int32_t AddReqIdByAppId(const char *appId, int64_t reqId)
302 {
303 IpcCallBackNode *node = nullptr;
304
305 std::lock_guard<std::mutex> autoLock(g_cbListLock);
306 if (g_ipcCallBackList.ctx == nullptr) {
307 LOGE("ipc callback list not inited");
308 return HC_ERROR;
309 }
310
311 node = GetIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
312 if (node == nullptr) {
313 LOGE("ipc callback node not found, appid: %" LOG_PUB "s", appId);
314 return HC_ERROR;
315 }
316 node->requestId = reqId;
317 node->delOnFni = 0;
318 LOGI("success, appid: %" LOG_PUB "s, requestId: %" LOG_PUB "lld", appId, static_cast<long long>(reqId));
319 return HC_SUCCESS;
320 }
321
AddIpcCbObjByReqId(int64_t reqId,int32_t objIdx,int32_t type)322 void AddIpcCbObjByReqId(int64_t reqId, int32_t objIdx, int32_t type)
323 {
324 IpcCallBackNode *node = nullptr;
325
326 std::lock_guard<std::mutex> autoLock(g_cbListLock);
327 if (g_ipcCallBackList.ctx == nullptr) {
328 LOGE("CallbackList not inited");
329 return;
330 }
331
332 if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
333 LOGE("list is full");
334 return;
335 }
336
337 node = GetIpcCallBackByReqId(reqId, type);
338 if (node != nullptr) {
339 node->proxyId = objIdx;
340 LOGI("ipc object add success, request id %" LOG_PUB "lld, type %" LOG_PUB "d, proxy id %" LOG_PUB "d",
341 static_cast<long long>(reqId), type, node->proxyId);
342 }
343 return;
344 }
345
AddIpcCallBackByReqId(int64_t reqId,const uint8_t * cbPtr,int32_t cbSz,int32_t type)346 int32_t AddIpcCallBackByReqId(int64_t reqId, const uint8_t *cbPtr, int32_t cbSz, int32_t type)
347 {
348 IpcCallBackNode *node = nullptr;
349 errno_t eno;
350
351 std::lock_guard<std::mutex> autoLock(g_cbListLock);
352 if (g_ipcCallBackList.ctx == nullptr) {
353 LOGE("list is full");
354 return HC_ERROR;
355 }
356
357 if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
358 LOGE("list is full");
359 return HC_ERROR;
360 }
361
362 node = GetIpcCallBackByReqId(reqId, type);
363 if (node != nullptr) {
364 eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
365 if (eno != EOK) {
366 LOGE("callback context memory copy failed");
367 return HC_ERROR;
368 }
369 if (node->proxyId >= 0) {
370 ServiceDevAuth::ResetRemoteObject(node->proxyId);
371 node->proxyId = -1;
372 }
373 LOGI("callback replaced success, request id %" LOG_PUB "lld, type %" LOG_PUB "d",
374 static_cast<long long>(reqId), type);
375 return HC_SUCCESS;
376 }
377
378 LOGI("new callback to add, request id %" LOG_PUB "lld, type %" LOG_PUB "d",
379 static_cast<long long>(reqId), type);
380 node = GetFreeIpcCallBackNode();
381 if (node == nullptr) {
382 LOGE("get free node failed");
383 return HC_ERROR;
384 }
385 node->cbType = type;
386 node->requestId = reqId;
387 eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
388 if (eno != EOK) {
389 ResetIpcCallBackNode(*node);
390 LOGE("callback context memory copy failed");
391 return HC_ERROR;
392 }
393 node->delOnFni = 1;
394 node->proxyId = -1;
395 g_ipcCallBackList.nodeCnt++;
396 LOGI("callback added success, request id %" LOG_PUB "lld, type %" LOG_PUB "d",
397 static_cast<long long>(reqId), type);
398 return HC_SUCCESS;
399 }
400
DelCallBackByReqId(int64_t reqId,int32_t type)401 static void DelCallBackByReqId(int64_t reqId, int32_t type)
402 {
403 IpcCallBackNode *node = nullptr;
404
405 if ((g_ipcCallBackList.nodeCnt <= 0) || (g_ipcCallBackList.ctx == nullptr)) {
406 return;
407 }
408
409 node = GetIpcCallBackByReqId(reqId, type);
410 if ((node != nullptr) && (node->delOnFni == 1)) {
411 ResetIpcCallBackNode(*node);
412 g_ipcCallBackList.nodeCnt--;
413 }
414 return;
415 }
416
DelIpcCallBackByReqId(int64_t reqId,int32_t type,bool withLock)417 void DelIpcCallBackByReqId(int64_t reqId, int32_t type, bool withLock)
418 {
419 if (withLock) {
420 std::lock_guard<std::mutex> autoLock(g_cbListLock);
421 DelCallBackByReqId(reqId, type);
422 return;
423 }
424 DelCallBackByReqId(reqId, type);
425 return;
426 }
427
OnTransmitStub(CallbackParams params)428 __attribute__((no_sanitize("cfi"))) static void OnTransmitStub(CallbackParams params)
429 {
430 int64_t requestId = 0;
431 int32_t inOutLen = 0;
432 uint8_t *data = nullptr;
433 uint32_t dataLen = 0u;
434 bool bRet = false;
435 bool (*onTransmitHook)(int64_t, uint8_t *, uint32_t) = nullptr;
436
437 onTransmitHook = reinterpret_cast<bool (*)(int64_t, uint8_t *, uint32_t)>(params.cbHook);
438 inOutLen = sizeof(requestId);
439 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
440 reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
441 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum,
442 PARAM_TYPE_COMM_DATA, reinterpret_cast<uint8_t *>(&data), reinterpret_cast<int32_t *>(&dataLen));
443 bRet = onTransmitHook(requestId, data, dataLen);
444 (bRet == true) ? params.reply.WriteInt32(HC_SUCCESS) : params.reply.WriteInt32(HC_ERROR);
445 return;
446 }
447
OnSessKeyStub(CallbackParams params)448 __attribute__((no_sanitize("cfi"))) static void OnSessKeyStub(CallbackParams params)
449 {
450 int64_t requestId = 0;
451 int32_t inOutLen = 0;
452 uint8_t *keyData = nullptr;
453 uint32_t dataLen = 0u;
454 void (*onSessKeyHook)(int64_t, uint8_t *, uint32_t) = nullptr;
455
456 (void)params.reply;
457 onSessKeyHook = reinterpret_cast<void (*)(int64_t, uint8_t *, uint32_t)>(params.cbHook);
458 inOutLen = sizeof(requestId);
459 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
460 reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
461 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_SESS_KEY,
462 reinterpret_cast<uint8_t *>(&keyData), reinterpret_cast<int32_t *>(&dataLen));
463 onSessKeyHook(requestId, keyData, dataLen);
464 return;
465 }
466
OnFinishStub(CallbackParams params)467 __attribute__((no_sanitize("cfi"))) static void OnFinishStub(CallbackParams params)
468 {
469 int64_t requestId = 0;
470 int32_t opCode = 0;
471 int32_t inOutLen = 0;
472 char *data = nullptr;
473 void (*onFinishHook)(int64_t, int32_t, char *) = nullptr;
474
475 (void)params.reply;
476 onFinishHook = reinterpret_cast<void (*)(int64_t, int32_t, char *)>(params.cbHook);
477 inOutLen = sizeof(requestId);
478 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
479 reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
480 inOutLen = sizeof(opCode);
481 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_OPCODE,
482 reinterpret_cast<uint8_t *>(&opCode), &inOutLen);
483 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_COMM_DATA,
484 reinterpret_cast<uint8_t *>(&data), nullptr);
485 onFinishHook(requestId, opCode, data);
486 return;
487 }
488
OnErrorStub(CallbackParams params)489 __attribute__((no_sanitize("cfi"))) static void OnErrorStub(CallbackParams params)
490 {
491 int64_t requestId = 0;
492 int32_t opCode = 0;
493 int32_t errCode = 0;
494 int32_t inOutLen = 0;
495 char *errInfo = nullptr;
496 void (*onErrorHook)(int64_t, int32_t, int32_t, char *) = nullptr;
497
498 onErrorHook = reinterpret_cast<void (*)(int64_t, int32_t, int32_t, char *)>(params.cbHook);
499 inOutLen = sizeof(requestId);
500 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
501 reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
502 inOutLen = sizeof(opCode);
503 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_OPCODE,
504 reinterpret_cast<uint8_t *>(&opCode), &inOutLen);
505 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_ERRCODE,
506 reinterpret_cast<uint8_t *>(&errCode), &inOutLen);
507 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_ERR_INFO,
508 reinterpret_cast<uint8_t *>(&errInfo), nullptr);
509 onErrorHook(requestId, opCode, errCode, errInfo);
510 return;
511 }
512
OnRequestStub(CallbackParams params)513 __attribute__((no_sanitize("cfi"))) static void OnRequestStub(CallbackParams params)
514 {
515 int64_t requestId = 0;
516 int32_t opCode = 0;
517 int32_t inOutLen = 0;
518 char *reqParams = nullptr;
519 char *reqResult = nullptr;
520 char *(*onReqHook)(int64_t, int32_t, char *) = nullptr;
521
522 onReqHook = reinterpret_cast<char *(*)(int64_t, int32_t, char *)>(params.cbHook);
523 inOutLen = sizeof(requestId);
524 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQID,
525 reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
526 inOutLen = sizeof(opCode);
527 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_OPCODE,
528 reinterpret_cast<uint8_t *>(&opCode), &inOutLen);
529 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_REQ_INFO,
530 reinterpret_cast<uint8_t *>(&reqParams), nullptr);
531 reqResult = onReqHook(requestId, opCode, reqParams);
532 if (reqResult == nullptr) {
533 params.reply.WriteInt32(HC_ERROR);
534 return;
535 }
536 params.reply.WriteInt32(HC_SUCCESS);
537 params.reply.WriteCString(const_cast<const char *>(reqResult));
538 HcFree(reqResult);
539 reqResult = nullptr;
540 return;
541 }
542
OnGroupCreatedStub(CallbackParams params)543 __attribute__((no_sanitize("cfi"))) static void OnGroupCreatedStub(CallbackParams params)
544 {
545 const char *groupInfo = nullptr;
546 void (*onGroupCreatedHook)(const char *) = nullptr;
547
548 onGroupCreatedHook = reinterpret_cast<void (*)(const char *)>(params.cbHook);
549 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_GROUP_INFO,
550 reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
551 onGroupCreatedHook(groupInfo);
552 return;
553 }
554
OnGroupDeletedStub(CallbackParams params)555 __attribute__((no_sanitize("cfi"))) static void OnGroupDeletedStub(CallbackParams params)
556 {
557 const char *groupInfo = nullptr;
558 void (*onDelGroupHook)(const char *) = nullptr;
559
560 onDelGroupHook = reinterpret_cast<void (*)(const char *)>(params.cbHook);
561 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_GROUP_INFO,
562 reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
563 onDelGroupHook(groupInfo);
564 return;
565 }
566
OnDevBoundStub(CallbackParams params)567 __attribute__((no_sanitize("cfi"))) static void OnDevBoundStub(CallbackParams params)
568 {
569 const char *groupInfo = nullptr;
570 const char *udid = nullptr;
571 void (*onDevBoundHook)(const char *, const char *) = nullptr;
572
573 onDevBoundHook = reinterpret_cast<void (*)(const char *, const char *)>(params.cbHook);
574 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID,
575 reinterpret_cast<uint8_t *>(&udid), nullptr);
576 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_GROUP_INFO,
577 reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
578 onDevBoundHook(udid, groupInfo);
579 return;
580 }
581
OnDevUnboundStub(CallbackParams params)582 __attribute__((no_sanitize("cfi"))) static void OnDevUnboundStub(CallbackParams params)
583 {
584 const char *groupInfo = nullptr;
585 const char *udid = nullptr;
586 void (*onDevUnBoundHook)(const char *, const char *) = nullptr;
587
588 onDevUnBoundHook = reinterpret_cast<void (*)(const char *, const char *)>(params.cbHook);
589 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID,
590 reinterpret_cast<uint8_t *>(&udid), nullptr);
591 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_GROUP_INFO,
592 reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
593 onDevUnBoundHook(udid, groupInfo);
594 return;
595 }
596
OnDevUnTrustStub(CallbackParams params)597 __attribute__((no_sanitize("cfi"))) static void OnDevUnTrustStub(CallbackParams params)
598 {
599 const char *udid = nullptr;
600 void (*onDevUnTrustHook)(const char *) = nullptr;
601
602 onDevUnTrustHook = reinterpret_cast<void (*)(const char *)>(params.cbHook);
603 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID,
604 reinterpret_cast<uint8_t *>(&udid), nullptr);
605 onDevUnTrustHook(udid);
606 return;
607 }
608
OnDelLastGroupStub(CallbackParams params)609 __attribute__((no_sanitize("cfi"))) static void OnDelLastGroupStub(CallbackParams params)
610 {
611 const char *udid = nullptr;
612 int32_t groupType = 0;
613 int32_t inOutLen = 0;
614 void (*onDelLastGroupHook)(const char *, int32_t) = nullptr;
615
616 onDelLastGroupHook = reinterpret_cast<void (*)(const char *, int32_t)>(params.cbHook);
617 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_UDID,
618 reinterpret_cast<uint8_t *>(&udid), nullptr);
619 inOutLen = sizeof(groupType);
620 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_GROUP_TYPE,
621 reinterpret_cast<uint8_t *>(&groupType), &inOutLen);
622 onDelLastGroupHook(udid, groupType);
623 return;
624 }
625
OnTrustDevNumChangedStub(CallbackParams params)626 __attribute__((no_sanitize("cfi"))) static void OnTrustDevNumChangedStub(CallbackParams params)
627 {
628 int32_t devNum = 0;
629 int32_t inOutLen = 0;
630 void (*onTrustDevNumChangedHook)(int32_t) = nullptr;
631
632 onTrustDevNumChangedHook = reinterpret_cast<void (*)(int32_t)>(params.cbHook);
633 inOutLen = sizeof(devNum);
634 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_DATA_NUM,
635 reinterpret_cast<uint8_t *>(&devNum), &inOutLen);
636 onTrustDevNumChangedHook(devNum);
637 return;
638 }
639
OnCredAddStub(CallbackParams params)640 __attribute__((no_sanitize("cfi"))) static void OnCredAddStub(CallbackParams params)
641 {
642 char *credId = nullptr;
643 char *credInfo = nullptr;
644 void (*onCredAddHook)(char *, char *) = nullptr;
645 onCredAddHook = reinterpret_cast<void (*)(char *, char *)>(params.cbHook);
646
647 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_CRED_ID,
648 reinterpret_cast<uint8_t *>(&credId), nullptr);
649 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_CRED_INFO,
650 reinterpret_cast<uint8_t *>(&credInfo), nullptr);
651 onCredAddHook(credId, credInfo);
652 return;
653 }
654
OnCredDeleteStub(CallbackParams params)655 __attribute__((no_sanitize("cfi"))) static void OnCredDeleteStub(CallbackParams params)
656 {
657 char *credId = nullptr;
658 char *credInfo = nullptr;
659 void (*onCredDeleteHook)(char *, char *) = nullptr;
660 onCredDeleteHook = reinterpret_cast<void (*)(char *, char *)>(params.cbHook);
661
662 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_CRED_ID,
663 reinterpret_cast<uint8_t *>(&credId), nullptr);
664 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_CRED_INFO,
665 reinterpret_cast<uint8_t *>(&credInfo), nullptr);
666 onCredDeleteHook(credId, credInfo);
667 return;
668 }
669
OnCredUpdateStub(CallbackParams params)670 __attribute__((no_sanitize("cfi"))) static void OnCredUpdateStub(CallbackParams params)
671 {
672 char *credId = nullptr;
673 char *credInfo = nullptr;
674 void (*onCredUpdateHook)(char *, char *) = nullptr;
675 onCredUpdateHook = reinterpret_cast<void (*)(char *, char *)>(params.cbHook);
676
677 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_CRED_ID,
678 reinterpret_cast<uint8_t *>(&credId), nullptr);
679 (void)GetIpcRequestParamByType(params.cbDataCache, params.cacheNum, PARAM_TYPE_CRED_INFO,
680 reinterpret_cast<uint8_t *>(&credInfo), nullptr);
681 onCredUpdateHook(credId, credInfo);
682 return;
683 }
684
ProcCbHook(int32_t callbackId,uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,uintptr_t replyCtx)685 void ProcCbHook(int32_t callbackId, uintptr_t cbHook,
686 const IpcDataInfo *cbDataCache, int32_t cacheNum, uintptr_t replyCtx)
687 {
688 CallbackStub stubTable[] = {
689 OnTransmitStub, OnSessKeyStub, OnFinishStub, OnErrorStub,
690 OnRequestStub, OnGroupCreatedStub, OnGroupDeletedStub, OnDevBoundStub,
691 OnDevUnboundStub, OnDevUnTrustStub, OnDelLastGroupStub, OnTrustDevNumChangedStub,
692 OnCredAddStub, OnCredDeleteStub, OnCredUpdateStub,
693 };
694 MessageParcel *reply = reinterpret_cast<MessageParcel *>(replyCtx);
695 if ((callbackId < CB_ID_ON_TRANS) || (callbackId > CB_ID_ON_CRED_UPDATE)) {
696 LOGE("Invalid call back id");
697 return;
698 }
699 if (cbHook == 0x0) {
700 LOGE("Invalid call back hook");
701 return;
702 }
703 CallbackParams params = { cbHook, cbDataCache, cacheNum, *reply };
704 stubTable[callbackId - 1](params);
705 return;
706 }
707
EncodeCallData(MessageParcel & dataParcel,int32_t type,const uint8_t * param,int32_t paramSz)708 static uint32_t EncodeCallData(MessageParcel &dataParcel, int32_t type, const uint8_t *param, int32_t paramSz)
709 {
710 const uint8_t *paramTmp = nullptr;
711 int32_t zeroVal = 0;
712
713 paramTmp = param;
714 if ((param == nullptr) || (paramSz == 0)) {
715 paramTmp = reinterpret_cast<const uint8_t *>(&zeroVal);
716 paramSz = sizeof(zeroVal);
717 }
718 if (dataParcel.WriteInt32(type) && dataParcel.WriteInt32(paramSz) &&
719 dataParcel.WriteBuffer(reinterpret_cast<const void *>(paramTmp), static_cast<size_t>(paramSz))) {
720 return static_cast<uint32_t>(HC_SUCCESS);
721 }
722 return static_cast<uint32_t>(HC_ERROR);
723 }
724
725 /* group or cred auth callback adapter */
GaCbOnTransmitWithType(int64_t requestId,const uint8_t * data,uint32_t dataLen,int32_t type)726 static bool GaCbOnTransmitWithType(int64_t requestId, const uint8_t *data, uint32_t dataLen, int32_t type)
727 {
728 int32_t ret = -1;
729 uint32_t uRet;
730 MessageParcel dataParcel;
731 MessageParcel reply;
732 IpcCallBackNode *node = nullptr;
733
734 LOGI("starting ... request id: %" LOG_PUB "lld, type %" LOG_PUB "d", static_cast<long long>(requestId), type);
735 std::lock_guard<std::mutex> autoLock(g_cbListLock);
736 node = GetIpcCallBackByReqId(requestId, type);
737 if (node == nullptr) {
738 LOGE("onTransmit hook is null, request id %" LOG_PUB "lld", static_cast<long long>(requestId));
739 return false;
740 }
741 uRet = EncodeCallData(dataParcel, PARAM_TYPE_REQID,
742 reinterpret_cast<const uint8_t *>(&requestId), sizeof(requestId));
743 uRet |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA, data, dataLen);
744 if (uRet != HC_SUCCESS) {
745 LOGE("build trans data failed");
746 return false;
747 }
748 ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_TRANS, true,
749 reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onTransmit), dataParcel, reply);
750 LOGI("process done, request id: %" LOG_PUB "lld", static_cast<long long>(requestId));
751 if (reply.ReadInt32(ret) && (ret == HC_SUCCESS)) {
752 return true;
753 }
754 return false;
755 }
756
IpcGaCbOnTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)757 static bool IpcGaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
758 {
759 return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_DEV_AUTH);
760 }
761
TmpIpcGaCbOnTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)762 static bool TmpIpcGaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
763 {
764 return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_TMP_DEV_AUTH);
765 }
766
IpcCaCbOnTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)767 static bool IpcCaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
768 {
769 return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_CRED_DEV_AUTH);
770 }
771
GaCbOnSessionKeyRetWithType(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen,int32_t type)772 static void GaCbOnSessionKeyRetWithType(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen, int32_t type)
773 {
774 uint32_t ret;
775 MessageParcel dataParcel;
776 MessageParcel reply;
777 IpcCallBackNode *node = nullptr;
778
779 LOGI("starting ... request id: %" LOG_PUB "lld, type %" LOG_PUB "d", static_cast<long long>(requestId), type);
780 std::lock_guard<std::mutex> autoLock(g_cbListLock);
781 node = GetIpcCallBackByReqId(requestId, type);
782 if (node == nullptr) {
783 LOGE("onSessionKeyReturned hook is null, request id %" LOG_PUB "lld", static_cast<long long>(requestId));
784 return;
785 }
786
787 ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
788 ret |= EncodeCallData(dataParcel, PARAM_TYPE_SESS_KEY, sessKey, sessKeyLen);
789 if (ret != HC_SUCCESS) {
790 LOGE("build trans data failed");
791 return;
792 }
793 ServiceDevAuth::ActCallback(node->proxyId, CB_ID_SESS_KEY_DONE, false,
794 reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onSessionKeyReturned), dataParcel, reply);
795 LOGI("process done, request id: %" LOG_PUB "lld", static_cast<long long>(requestId));
796 return;
797 }
798
IpcGaCbOnSessionKeyReturned(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen)799 static void IpcGaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
800 {
801 GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_DEV_AUTH);
802 return;
803 }
804
TmpIpcGaCbOnSessionKeyReturned(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen)805 static void TmpIpcGaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
806 {
807 GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_TMP_DEV_AUTH);
808 return;
809 }
810
IpcCaCbOnSessionKeyReturned(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen)811 static void IpcCaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
812 {
813 GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_CRED_DEV_AUTH);
814 return;
815 }
816
GaCbOnFinishWithType(int64_t requestId,int32_t operationCode,const char * returnData,int32_t type)817 static void GaCbOnFinishWithType(int64_t requestId, int32_t operationCode, const char *returnData, int32_t type)
818 {
819 uint32_t ret;
820 MessageParcel dataParcel;
821 MessageParcel reply;
822 IpcCallBackNode *node = nullptr;
823
824 LOGI("starting ... request id: %" LOG_PUB "lld, type %" LOG_PUB "d", static_cast<long long>(requestId), type);
825 std::lock_guard<std::mutex> autoLock(g_cbListLock);
826 node = GetIpcCallBackByReqId(requestId, type);
827 if (node == nullptr) {
828 LOGE("onFinish hook is null, request id %" LOG_PUB "lld", static_cast<long long>(requestId));
829 return;
830 }
831 ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
832 ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE,
833 reinterpret_cast<uint8_t *>(&operationCode), sizeof(operationCode));
834 if (returnData != nullptr) {
835 ret |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA,
836 reinterpret_cast<const uint8_t *>(returnData), HcStrlen(returnData) + 1);
837 }
838 if (ret != HC_SUCCESS) {
839 LOGE("build trans data failed");
840 return;
841 }
842 ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_FINISH, false,
843 reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onFinish), dataParcel, reply);
844 /* delete request id */
845 DelIpcCallBackByReqId(requestId, type, false);
846 LOGI("process done, request id: %" LOG_PUB "lld", static_cast<long long>(requestId));
847 return;
848 }
849
IpcGaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)850 static void IpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
851 {
852 GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_DEV_AUTH);
853 return;
854 }
855
TmpIpcGaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)856 static void TmpIpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
857 {
858 GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_TMP_DEV_AUTH);
859 return;
860 }
861
IpcCaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)862 static void IpcCaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
863 {
864 GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_CRED_DEV_AUTH);
865 return;
866 }
867
GaCbOnErrorWithType(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn,int32_t type)868 static void GaCbOnErrorWithType(int64_t requestId, int32_t operationCode,
869 int32_t errorCode, const char *errorReturn, int32_t type)
870 {
871 uint32_t ret;
872 MessageParcel dataParcel;
873 MessageParcel reply;
874 IpcCallBackNode *node = nullptr;
875
876 LOGI("starting ... request id: %" LOG_PUB "lld, type %" LOG_PUB "d", static_cast<long long>(requestId), type);
877 std::lock_guard<std::mutex> autoLock(g_cbListLock);
878 node = GetIpcCallBackByReqId(requestId, type);
879 if (node == nullptr) {
880 LOGE("onError hook is null, request id %" LOG_PUB "lld", static_cast<long long>(requestId));
881 return;
882 }
883 ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
884 ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE,
885 reinterpret_cast<uint8_t *>(&operationCode), sizeof(operationCode));
886 ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERRCODE, reinterpret_cast<uint8_t *>(&errorCode), sizeof(errorCode));
887 if (errorReturn != nullptr) {
888 ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERR_INFO,
889 reinterpret_cast<const uint8_t *>(errorReturn), HcStrlen(errorReturn) + 1);
890 }
891 if (ret != HC_SUCCESS) {
892 LOGE("build trans data failed");
893 return;
894 }
895 ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_ERROR, false,
896 reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onError), dataParcel, reply);
897 /* delete request id */
898 DelIpcCallBackByReqId(requestId, type, false);
899 LOGI("process done, request id: %" LOG_PUB "lld", static_cast<long long>(requestId));
900 return;
901 }
902
IpcGaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)903 static void IpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
904 {
905 GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_DEV_AUTH);
906 return;
907 }
908
TmpIpcGaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)909 static void TmpIpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
910 {
911 GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_TMP_DEV_AUTH);
912 return;
913 }
914
IpcCaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)915 static void IpcCaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
916 {
917 GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_CRED_DEV_AUTH);
918 return;
919 }
920
GaCbOnRequestWithType(int64_t requestId,int32_t operationCode,const char * reqParams,int32_t type)921 static char *GaCbOnRequestWithType(int64_t requestId, int32_t operationCode, const char *reqParams, int32_t type)
922 {
923 int32_t ret = -1;
924 uint32_t uRet;
925 MessageParcel dataParcel;
926 MessageParcel reply;
927 const char *dPtr = nullptr;
928 IpcCallBackNode *node = nullptr;
929
930 LOGI("starting ... request id: %" LOG_PUB "lld, type %" LOG_PUB "d", static_cast<long long>(requestId), type);
931 std::lock_guard<std::mutex> autoLock(g_cbListLock);
932 node = GetIpcCallBackByReqId(requestId, type);
933 if (node == nullptr) {
934 LOGE("onRequest hook is null, request id %" LOG_PUB "lld", static_cast<long long>(requestId));
935 return nullptr;
936 }
937
938 uRet = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
939 uRet |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE,
940 reinterpret_cast<uint8_t *>(&operationCode), sizeof(operationCode));
941 if (reqParams != nullptr) {
942 uRet |= EncodeCallData(dataParcel, PARAM_TYPE_REQ_INFO,
943 reinterpret_cast<const uint8_t *>(reqParams), HcStrlen(reqParams) + 1);
944 }
945 if (uRet != HC_SUCCESS) {
946 LOGE("build trans data failed");
947 return nullptr;
948 }
949
950 ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_REQUEST, true,
951 reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onRequest), dataParcel, reply);
952 if (reply.ReadInt32(ret) && (ret == HC_SUCCESS)) {
953 if (reply.GetReadableBytes() == 0) {
954 LOGE("onRequest has no data, but success");
955 return nullptr;
956 }
957 dPtr = reply.ReadCString();
958 LOGI("process done, request id: %" LOG_PUB "lld, %" LOG_PUB "s string", static_cast<long long>(requestId),
959 (dPtr != nullptr) ? "valid" : "invalid");
960 return (dPtr != nullptr) ? strdup(dPtr) : nullptr;
961 }
962 return nullptr;
963 }
964
CanFindCbByReqId(int64_t requestId,int32_t type)965 static bool CanFindCbByReqId(int64_t requestId, int32_t type)
966 {
967 std::lock_guard<std::mutex> autoLock(g_cbListLock);
968 IpcCallBackNode *node = GetIpcCallBackByReqId(requestId, type);
969 return (node != nullptr) ? true : false;
970 }
971
IpcGaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)972 static char *IpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
973 {
974 if (!CanFindCbByReqId(requestId, CB_TYPE_DEV_AUTH)) {
975 CJson *reqParamsJson = CreateJsonFromString(reqParams);
976 if (reqParamsJson == nullptr) {
977 LOGE("Create json from string occur error!");
978 return nullptr;
979 }
980 const char *callerAppId = GetStringFromJson(reqParamsJson, FIELD_APP_ID);
981 if (callerAppId == nullptr) {
982 LOGE("failed to get appId from json object!");
983 FreeJson(reqParamsJson);
984 return nullptr;
985 }
986 int32_t ret = AddReqIdByAppId(callerAppId, requestId);
987 FreeJson(reqParamsJson);
988 if (ret != HC_SUCCESS) {
989 return nullptr;
990 }
991 }
992 return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_DEV_AUTH);
993 }
994
TmpIpcGaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)995 static char *TmpIpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
996 {
997 return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_TMP_DEV_AUTH);
998 }
999
IpcCaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)1000 static char *IpcCaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
1001 {
1002 if (!CanFindCbByReqId(requestId, CB_TYPE_CRED_DEV_AUTH)) {
1003 CJson *reqParamsJson = CreateJsonFromString(reqParams);
1004 if (reqParamsJson == nullptr) {
1005 LOGE("Failed to create json from string!");
1006 return nullptr;
1007 }
1008 const char *callerAppId = GetStringFromJson(reqParamsJson, FIELD_APP_ID);
1009 if (callerAppId == nullptr) {
1010 LOGE("Failed to get appId from reqParams json!");
1011 FreeJson(reqParamsJson);
1012 return nullptr;
1013 }
1014 int32_t ret = AddReqIdByAppId(callerAppId, requestId);
1015 FreeJson(reqParamsJson);
1016 if (ret != HC_SUCCESS) {
1017 return nullptr;
1018 }
1019 }
1020 return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_CRED_DEV_AUTH);
1021 }
1022
1023 namespace {
IpcOnGroupCreated(const char * groupInfo)1024 void IpcOnGroupCreated(const char *groupInfo)
1025 {
1026 int32_t i;
1027 uint32_t ret;
1028 MessageParcel reply;
1029 MessageParcel dataParcel;
1030 DataChangeListener *listener = nullptr;
1031
1032 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1033 if (g_ipcCallBackList.ctx == nullptr) {
1034 LOGE("IpcCallBackList is not initialized");
1035 return;
1036 }
1037
1038 if (groupInfo == nullptr) {
1039 LOGE("IpcOnGroupCreated, params error");
1040 return;
1041 }
1042
1043 ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
1044 reinterpret_cast<const uint8_t *>(groupInfo), HcStrlen(groupInfo) + 1);
1045 if (ret != HC_SUCCESS) {
1046 LOGE("Error occurs, IpcOnGroupCreated build trans data failed");
1047 return;
1048 }
1049
1050 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1051 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1052 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1053 if (listener->onGroupCreated == nullptr) {
1054 continue;
1055 }
1056 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_CREATED,
1057 false, reinterpret_cast<uintptr_t>(listener->onGroupCreated), dataParcel, reply);
1058 }
1059 }
1060 return;
1061 }
1062
IpcOnGroupDeleted(const char * groupInfo)1063 void IpcOnGroupDeleted(const char *groupInfo)
1064 {
1065 int32_t i;
1066 uint32_t ret;
1067 MessageParcel dataParcel;
1068 MessageParcel reply;
1069 DataChangeListener *listener = nullptr;
1070
1071 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1072 if (g_ipcCallBackList.ctx == nullptr) {
1073 LOGE("IpcCallBackList un-initialized");
1074 return;
1075 }
1076
1077 if (groupInfo == nullptr) {
1078 LOGE("IpcOnGroupDeleted, params error");
1079 return;
1080 }
1081
1082 ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
1083 reinterpret_cast<const uint8_t *>(groupInfo), HcStrlen(groupInfo) + 1);
1084 if (ret != HC_SUCCESS) {
1085 LOGE("IpcOnGroupDeleted, build trans data failed");
1086 return;
1087 }
1088
1089 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1090 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1091 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1092 if (listener->onGroupDeleted == nullptr) {
1093 continue;
1094 }
1095 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_DELETED,
1096 false, reinterpret_cast<uintptr_t>(listener->onGroupDeleted), dataParcel, reply);
1097 }
1098 }
1099 return;
1100 }
1101
IpcOnDeviceBound(const char * peerUdid,const char * groupInfo)1102 void IpcOnDeviceBound(const char *peerUdid, const char *groupInfo)
1103 {
1104 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1105 if (g_ipcCallBackList.ctx == nullptr) {
1106 LOGE("CallBackList un-initialized.");
1107 return;
1108 }
1109 if ((peerUdid == nullptr) || (groupInfo == nullptr)) {
1110 LOGE("Error occurs, param is nullptr.");
1111 return;
1112 }
1113
1114 MessageParcel dataParcel;
1115 uint32_t ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1116 reinterpret_cast<const uint8_t *>(peerUdid), HcStrlen(peerUdid) + 1);
1117 ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
1118 reinterpret_cast<const uint8_t *>(groupInfo), HcStrlen(groupInfo) + 1);
1119 if (ret != HC_SUCCESS) {
1120 LOGE("build transmit data failed.");
1121 return;
1122 }
1123
1124 MessageParcel reply;
1125 DataChangeListener *listener = nullptr;
1126 int32_t i;
1127 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1128 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1129 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1130 if (listener->onDeviceBound == nullptr) {
1131 continue;
1132 }
1133 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_BOUND,
1134 false, reinterpret_cast<uintptr_t>(listener->onDeviceBound), dataParcel, reply);
1135 }
1136 }
1137 return;
1138 }
1139
IpcOnDeviceUnBound(const char * peerUdid,const char * groupInfo)1140 void IpcOnDeviceUnBound(const char *peerUdid, const char *groupInfo)
1141 {
1142 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1143 if (g_ipcCallBackList.ctx == nullptr) {
1144 LOGE("CallBackList ctx is nullptr.");
1145 return;
1146 }
1147 if ((peerUdid == nullptr) || (groupInfo == nullptr)) {
1148 LOGE("peerUdid is nullptr or groupInfo is nullptr.");
1149 return;
1150 }
1151
1152 uint32_t ret;
1153 MessageParcel dataParcel;
1154 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1155 reinterpret_cast<const uint8_t *>(peerUdid), HcStrlen(peerUdid) + 1);
1156 ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
1157 reinterpret_cast<const uint8_t *>(groupInfo), HcStrlen(groupInfo) + 1);
1158 if (ret != HC_SUCCESS) {
1159 LOGE("build trans data failed.");
1160 return;
1161 }
1162
1163 int32_t i;
1164 MessageParcel reply;
1165 DataChangeListener *listener = nullptr;
1166 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1167 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1168 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1169 if (listener->onDeviceUnBound == nullptr) {
1170 continue;
1171 }
1172 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNBOUND,
1173 false, reinterpret_cast<uintptr_t>(listener->onDeviceUnBound), dataParcel, reply);
1174 }
1175 }
1176 return;
1177 }
1178
IpcOnDeviceNotTrusted(const char * peerUdid)1179 void IpcOnDeviceNotTrusted(const char *peerUdid)
1180 {
1181 int32_t i;
1182 uint32_t ret;
1183 MessageParcel dataParcel;
1184 MessageParcel reply;
1185 DataChangeListener *listener = nullptr;
1186
1187 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1188 if (g_ipcCallBackList.ctx == nullptr) {
1189 LOGE("Error occurs, callBackList ctx is nullptr.");
1190 return;
1191 }
1192
1193 if (peerUdid == nullptr) {
1194 LOGE("Error occurs, peerUdid is nullptr.");
1195 return;
1196 }
1197
1198 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1199 reinterpret_cast<const uint8_t *>(peerUdid), HcStrlen(peerUdid) + 1);
1200 if (ret != HC_SUCCESS) {
1201 LOGE("Error occurs, encode trans data failed.");
1202 return;
1203 }
1204
1205 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1206 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1207 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1208 if (listener->onDeviceNotTrusted == nullptr) {
1209 continue;
1210 }
1211 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNTRUSTED,
1212 false, reinterpret_cast<uintptr_t>(listener->onDeviceNotTrusted), dataParcel, reply);
1213 }
1214 }
1215 return;
1216 }
1217
IpcOnLastGroupDeleted(const char * peerUdid,int32_t groupType)1218 void IpcOnLastGroupDeleted(const char *peerUdid, int32_t groupType)
1219 {
1220 int32_t i;
1221 uint32_t ret;
1222 MessageParcel dataParcel;
1223 MessageParcel reply;
1224 DataChangeListener *listener = nullptr;
1225
1226 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1227 if (g_ipcCallBackList.ctx == nullptr) {
1228 LOGE("Error occurs, callBackList ctx is uninitialized.");
1229 return;
1230 }
1231
1232 if (peerUdid == nullptr) {
1233 LOGE("Error occurs, param is nullptr.");
1234 return;
1235 }
1236
1237 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1238 reinterpret_cast<const uint8_t *>(peerUdid), HcStrlen(peerUdid) + 1);
1239 ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_TYPE,
1240 reinterpret_cast<const uint8_t *>(&groupType), sizeof(groupType));
1241 if (ret != HC_SUCCESS) {
1242 LOGE("Encode call data failed.");
1243 return;
1244 }
1245
1246 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1247 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1248 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1249 if (listener->onLastGroupDeleted == nullptr) {
1250 continue;
1251 }
1252 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_LAST_GROUP_DELETED,
1253 false, reinterpret_cast<uintptr_t>(listener->onLastGroupDeleted), dataParcel, reply);
1254 }
1255 }
1256 return;
1257 }
1258
IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)1259 void IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)
1260 {
1261 int32_t i;
1262 uint32_t ret;
1263 MessageParcel reply;
1264
1265 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1266 if (g_ipcCallBackList.ctx == nullptr) {
1267 LOGE("IpcCallBackList un-initialized");
1268 return;
1269 }
1270
1271 MessageParcel dataParcel;
1272 ret = EncodeCallData(dataParcel, PARAM_TYPE_DATA_NUM,
1273 reinterpret_cast<const uint8_t *>(&curTrustedDeviceNum), sizeof(curTrustedDeviceNum));
1274 if (ret != HC_SUCCESS) {
1275 LOGE("IpcOnTrustedDeviceNumChanged, build trans data failed");
1276 return;
1277 }
1278
1279 DataChangeListener *listener = nullptr;
1280 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1281 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1282 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1283 if (listener->onTrustedDeviceNumChanged == nullptr) {
1284 continue;
1285 }
1286 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_TRUST_DEV_NUM_CHANGED,
1287 false, reinterpret_cast<uintptr_t>(listener->onTrustedDeviceNumChanged), dataParcel, reply);
1288 }
1289 }
1290 return;
1291 }
1292
IpcOnCredAdd(const char * credId,const char * credInfo)1293 void IpcOnCredAdd(const char *credId, const char *credInfo)
1294 {
1295 int32_t i;
1296 uint32_t ret;
1297 MessageParcel reply;
1298 CredChangeListener *listener = nullptr;
1299
1300 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1301 if (g_ipcCallBackList.ctx == nullptr) {
1302 LOGE("IpcOnCredAdd failed, callBackList is un-initialized.");
1303 return;
1304 }
1305
1306 if (credId == nullptr) {
1307 LOGE("IpcOnCredAdd failed, params error.");
1308 return;
1309 }
1310 MessageParcel dataParcel;
1311 ret = EncodeCallData(dataParcel, PARAM_TYPE_CRED_ID,
1312 reinterpret_cast<const uint8_t *>(credId), HcStrlen(credId) + 1);
1313 ret |= EncodeCallData(dataParcel, PARAM_TYPE_CRED_INFO,
1314 reinterpret_cast<const uint8_t *>(credInfo), HcStrlen(credInfo) + 1);
1315 if (ret != HC_SUCCESS) {
1316 LOGE("IpcOnCredAdd, build trans data failed");
1317 return;
1318 }
1319
1320 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1321 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_CRED_LISTENER) {
1322 listener = &(g_ipcCallBackList.ctx[i].cbCtx.credListener);
1323 if (listener->onCredAdd == nullptr) {
1324 continue;
1325 }
1326 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_CRED_ADD,
1327 false, reinterpret_cast<uintptr_t>(listener->onCredAdd), dataParcel, reply);
1328 }
1329 }
1330 return;
1331 }
1332
IpcOnCredDelete(const char * credId,const char * credInfo)1333 void IpcOnCredDelete(const char *credId, const char *credInfo)
1334 {
1335 int32_t i;
1336 uint32_t ret;
1337 MessageParcel dataParcel;
1338 MessageParcel reply;
1339 CredChangeListener *listener = nullptr;
1340
1341 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1342 if (g_ipcCallBackList.ctx == nullptr) {
1343 LOGE("IpcOnCredDelete failed, CallBackList un-initialized");
1344 return;
1345 }
1346
1347 if (credId == nullptr) {
1348 LOGE("IpcOnCredDelete failed, credId is nullptr.");
1349 return;
1350 }
1351
1352 ret = EncodeCallData(dataParcel, PARAM_TYPE_CRED_ID,
1353 reinterpret_cast<const uint8_t *>(credId), HcStrlen(credId) + 1);
1354 ret |= EncodeCallData(dataParcel, PARAM_TYPE_CRED_INFO,
1355 reinterpret_cast<const uint8_t *>(credInfo), HcStrlen(credInfo) + 1);
1356 if (ret != HC_SUCCESS) {
1357 LOGE("IpcOnCredDelete build trans data failed");
1358 return;
1359 }
1360
1361 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1362 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_CRED_LISTENER) {
1363 listener = &(g_ipcCallBackList.ctx[i].cbCtx.credListener);
1364 if (listener->onCredDelete == nullptr) {
1365 continue;
1366 }
1367 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_CRED_DELETE,
1368 false, reinterpret_cast<uintptr_t>(listener->onCredDelete), dataParcel, reply);
1369 }
1370 }
1371 return;
1372 }
1373
IpcOnCredUpdate(const char * credId,const char * credInfo)1374 void IpcOnCredUpdate(const char *credId, const char *credInfo)
1375 {
1376 int32_t i;
1377 uint32_t ret;
1378 MessageParcel dataParcel;
1379 MessageParcel reply;
1380 CredChangeListener *listener = nullptr;
1381
1382 std::lock_guard<std::mutex> autoLock(g_cbListLock);
1383 if (g_ipcCallBackList.ctx == nullptr) {
1384 LOGE("IpcOnCredUpdate failed, IpcCallBackList un-initialized");
1385 return;
1386 }
1387
1388 if (credId == nullptr) {
1389 LOGE("IpcOnCredUpdate failed, params error");
1390 return;
1391 }
1392
1393 ret = EncodeCallData(dataParcel, PARAM_TYPE_CRED_ID,
1394 reinterpret_cast<const uint8_t *>(credId), HcStrlen(credId) + 1);
1395 ret |= EncodeCallData(dataParcel, PARAM_TYPE_CRED_INFO,
1396 reinterpret_cast<const uint8_t *>(credInfo), HcStrlen(credInfo) + 1);
1397 if (ret != HC_SUCCESS) {
1398 LOGE("IpcOnCredUpdate build trans data failed");
1399 return;
1400 }
1401
1402 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1403 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_CRED_LISTENER) {
1404 listener = &(g_ipcCallBackList.ctx[i].cbCtx.credListener);
1405 if (listener->onCredUpdate == nullptr) {
1406 continue;
1407 }
1408 ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_CRED_UPDATE,
1409 false, reinterpret_cast<uintptr_t>(listener->onCredUpdate), dataParcel, reply);
1410 }
1411 }
1412 return;
1413 }
1414 };
1415
InitDeviceAuthCbCtx(DeviceAuthCallback * ctx,int32_t type)1416 void InitDeviceAuthCbCtx(DeviceAuthCallback *ctx, int32_t type)
1417 {
1418 if (ctx == nullptr) {
1419 return;
1420 }
1421 if (type == CB_TYPE_DEV_AUTH) {
1422 ctx->onTransmit = IpcGaCbOnTransmit;
1423 ctx->onSessionKeyReturned = IpcGaCbOnSessionKeyReturned;
1424 ctx->onFinish = IpcGaCbOnFinish;
1425 ctx->onError = IpcGaCbOnError;
1426 ctx->onRequest = IpcGaCbOnRequest;
1427 }
1428 if (type == CB_TYPE_TMP_DEV_AUTH) {
1429 ctx->onTransmit = TmpIpcGaCbOnTransmit;
1430 ctx->onSessionKeyReturned = TmpIpcGaCbOnSessionKeyReturned;
1431 ctx->onFinish = TmpIpcGaCbOnFinish;
1432 ctx->onError = TmpIpcGaCbOnError;
1433 ctx->onRequest = TmpIpcGaCbOnRequest;
1434 }
1435 if (type == CB_TYPE_CRED_DEV_AUTH) {
1436 ctx->onTransmit = IpcCaCbOnTransmit;
1437 ctx->onSessionKeyReturned = IpcCaCbOnSessionKeyReturned;
1438 ctx->onFinish = IpcCaCbOnFinish;
1439 ctx->onError = IpcCaCbOnError;
1440 ctx->onRequest = IpcCaCbOnRequest;
1441 }
1442 return;
1443 }
1444
InitDevAuthListenerCbCtx(DataChangeListener * ctx)1445 void InitDevAuthListenerCbCtx(DataChangeListener *ctx)
1446 {
1447 if (ctx == nullptr) {
1448 return;
1449 }
1450 ctx->onGroupCreated = IpcOnGroupCreated;
1451 ctx->onGroupDeleted = IpcOnGroupDeleted;
1452 ctx->onDeviceBound = IpcOnDeviceBound;
1453 ctx->onDeviceUnBound = IpcOnDeviceUnBound;
1454 ctx->onDeviceNotTrusted = IpcOnDeviceNotTrusted;
1455 ctx->onLastGroupDeleted = IpcOnLastGroupDeleted;
1456 ctx->onTrustedDeviceNumChanged = IpcOnTrustedDeviceNumChanged;
1457 return;
1458 }
1459
InitDevAuthCredListenerCbCtx(CredChangeListener * ctx)1460 void InitDevAuthCredListenerCbCtx(CredChangeListener *ctx)
1461 {
1462 if (ctx == nullptr) {
1463 return;
1464 }
1465 ctx->onCredAdd = IpcOnCredAdd;
1466 ctx->onCredDelete = IpcOnCredDelete;
1467 ctx->onCredUpdate = IpcOnCredUpdate;
1468 return;
1469 }
1470
1471 /* ipc client process adapter */
CreateCallCtx(uintptr_t * callCtx)1472 int32_t CreateCallCtx(uintptr_t *callCtx)
1473 {
1474 if (callCtx == nullptr) {
1475 return HC_ERR_INVALID_PARAMS;
1476 }
1477
1478 ProxyDevAuthData *dataCache = new(std::nothrow) ProxyDevAuthData();
1479 if (dataCache == nullptr) {
1480 LOGE("call context alloc failed");
1481 return HC_ERR_ALLOC_MEMORY;
1482 }
1483 *callCtx = reinterpret_cast<uintptr_t>(dataCache);
1484 return HC_SUCCESS;
1485 }
1486
DestroyCallCtx(uintptr_t * callCtx)1487 void DestroyCallCtx(uintptr_t *callCtx)
1488 {
1489 ProxyDevAuthData *dataCache = nullptr;
1490 if ((callCtx != nullptr) && (*callCtx != 0)) {
1491 dataCache = reinterpret_cast<ProxyDevAuthData *>(*callCtx);
1492 delete dataCache;
1493 *callCtx = 0;
1494 }
1495 return;
1496 }
1497
SetCbCtxToDataCtx(uintptr_t callCtx,int32_t cbIdx)1498 void SetCbCtxToDataCtx(uintptr_t callCtx, int32_t cbIdx)
1499 {
1500 ProxyDevAuthData *dataCache = nullptr;
1501 sptr<IRemoteObject> remote = g_sdkCbStub[cbIdx];
1502 dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1503 dataCache->SetCallbackStub(remote);
1504 return;
1505 }
1506
SetCallRequestParamInfo(uintptr_t callCtx,int32_t type,const uint8_t * param,int32_t paramSz)1507 int32_t SetCallRequestParamInfo(uintptr_t callCtx, int32_t type, const uint8_t *param, int32_t paramSz)
1508 {
1509 ProxyDevAuthData *dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1510
1511 return dataCache->EncodeCallRequest(type, param, paramSz);
1512 }
1513
DoBinderCall(uintptr_t callCtx,int32_t methodId,bool withSync)1514 int32_t DoBinderCall(uintptr_t callCtx, int32_t methodId, bool withSync)
1515 {
1516 if (LoadDeviceAuthSaIfNotLoad() != HC_SUCCESS) {
1517 LOGW("sa not load.");
1518 return HC_ERR_IPC_SA_NOT_LOAD;
1519 }
1520 int32_t ret;
1521 ProxyDevAuthData *dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1522
1523 ret = dataCache->FinalCallRequest(methodId);
1524 if (ret != HC_SUCCESS) {
1525 return ret;
1526 }
1527 return dataCache->ActCall(withSync);
1528 }
1529
1530 /* ipc service process adapter */
SetIpcCallMap(uintptr_t ipcInstance,IpcServiceCall method,int32_t methodId)1531 uint32_t SetIpcCallMap(uintptr_t ipcInstance, IpcServiceCall method, int32_t methodId)
1532 {
1533 if ((method == nullptr) || (methodId <= 0)) {
1534 return static_cast<uint32_t>(HC_ERR_INVALID_PARAMS);
1535 }
1536
1537 ServiceDevAuth *service = reinterpret_cast<ServiceDevAuth *>(ipcInstance);
1538 return static_cast<uint32_t>(service->SetCallMap(method, methodId));
1539 }
1540
CreateServiceInstance(uintptr_t * ipcInstance)1541 int32_t CreateServiceInstance(uintptr_t *ipcInstance)
1542 {
1543 ServiceDevAuth *service = nullptr;
1544 service = new(std::nothrow) ServiceDevAuth();
1545 if (service == nullptr) {
1546 return HC_ERR_ALLOC_MEMORY;
1547 }
1548 *ipcInstance = reinterpret_cast<uintptr_t>(service);
1549 return HC_SUCCESS;
1550 }
1551
DestroyServiceInstance(uintptr_t ipcInstance)1552 void DestroyServiceInstance(uintptr_t ipcInstance)
1553 {
1554 ServiceDevAuth *service = reinterpret_cast<ServiceDevAuth *>(ipcInstance);
1555 if (service == nullptr) {
1556 return;
1557 }
1558 delete service;
1559 }
1560
AddDevAuthServiceToManager(uintptr_t serviceInstance)1561 int32_t AddDevAuthServiceToManager(uintptr_t serviceInstance)
1562 {
1563 // Wait samgr ready for up to 1 second to ensure adding service to samgr.
1564 WaitParameter("bootevent.samgr.ready", "true", 1);
1565
1566 IPCSkeleton::SetMaxWorkThreadNum(DEV_AUTH_MAX_THREAD_NUM);
1567
1568 sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1569 if (sysMgr == nullptr) {
1570 LOGE("Failed to get system ability manager!");
1571 return HC_ERR_IPC_GET_SERVICE;
1572 }
1573 ServiceDevAuth *servicePtr = reinterpret_cast<ServiceDevAuth *>(serviceInstance);
1574 int32_t ret = sysMgr->AddSystemAbility(DEVICE_AUTH_SERVICE_ID, servicePtr);
1575 if (ret != ERR_OK) {
1576 LOGE("add service failed");
1577 return HC_ERROR;
1578 }
1579 LOGI("AddSystemAbility to SA manager success");
1580 return HC_SUCCESS;
1581 }
1582
IpcEncodeCallReply(uintptr_t replayCache,int32_t type,const uint8_t * result,int32_t resultSz)1583 int32_t IpcEncodeCallReply(uintptr_t replayCache, int32_t type, const uint8_t *result, int32_t resultSz)
1584 {
1585 int32_t errCnt = 0;
1586 MessageParcel *replyParcel = nullptr;
1587 unsigned long valZero = 0uL;
1588
1589 replyParcel = reinterpret_cast<MessageParcel *>(replayCache);
1590 errCnt += replyParcel->WriteInt32(type) ? 0 : 1;
1591 errCnt += replyParcel->WriteInt32(resultSz) ? 0 : 1;
1592 if ((result != nullptr) && (resultSz > 0)) {
1593 errCnt += replyParcel->WriteBuffer(
1594 reinterpret_cast<const void *>(result), static_cast<size_t>(resultSz)) ? 0 : 1;
1595 } else {
1596 errCnt += replyParcel->WriteBuffer(
1597 reinterpret_cast<const void *>(&valZero), sizeof(unsigned long)) ? 0 : 1;
1598 }
1599 if (errCnt != 0) {
1600 LOGE("encode call reply fail.");
1601 return HC_ERROR;
1602 }
1603 return HC_SUCCESS;
1604 }
1605
DecodeIpcData(uintptr_t data,int32_t * type,uint8_t ** val,int32_t * valSz)1606 int32_t DecodeIpcData(uintptr_t data, int32_t *type, uint8_t **val, int32_t *valSz)
1607 {
1608 MessageParcel *dataPtr = nullptr;
1609
1610 dataPtr = reinterpret_cast<MessageParcel *>(data);
1611 if (dataPtr->GetReadableBytes() == 0) {
1612 return HC_SUCCESS;
1613 }
1614 if (dataPtr->GetReadableBytes() < sizeof(int32_t)) {
1615 LOGE("Insufficient data available in IPC container. [Data]: type");
1616 return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
1617 }
1618 *type = dataPtr->ReadInt32();
1619 if (dataPtr->GetReadableBytes() < sizeof(int32_t)) {
1620 LOGE("Insufficient data available in IPC container. [Data]: valSz");
1621 return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
1622 }
1623 *valSz = dataPtr->ReadInt32();
1624 if (*valSz > static_cast<int32_t>(dataPtr->GetReadableBytes())) {
1625 LOGE("Insufficient data available in IPC container. [Data]: val");
1626 return HC_ERR_IPC_BAD_VAL_LENGTH;
1627 }
1628 *val = const_cast<uint8_t *>(dataPtr->ReadUnpadBuffer(*valSz));
1629 return HC_SUCCESS;
1630 }
1631
DecodeCallReply(uintptr_t callCtx,IpcDataInfo * replyCache,int32_t cacheNum)1632 void DecodeCallReply(uintptr_t callCtx, IpcDataInfo *replyCache, int32_t cacheNum)
1633 {
1634 int32_t dataLen = 0;
1635 int32_t i;
1636 int32_t ret;
1637
1638 ProxyDevAuthData *dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1639 MessageParcel *tmpParcel = dataCache->GetReplyParcel();
1640 if (tmpParcel->GetReadableBytes() < sizeof(int32_t)) {
1641 LOGE("Insufficient data available in IPC container. [Data]: dataLen");
1642 return;
1643 }
1644 dataLen = tmpParcel->ReadInt32();
1645 if ((dataLen <= 0) || (dataLen != static_cast<int32_t>(tmpParcel->GetReadableBytes()))) {
1646 LOGE("decode failed, data length %" LOG_PUB "d", dataLen);
1647 return;
1648 }
1649
1650 for (i = 0; i < cacheNum; i++) {
1651 ret = DecodeIpcData(reinterpret_cast<uintptr_t>(tmpParcel),
1652 &(replyCache[i].type), &(replyCache[i].val), &(replyCache[i].valSz));
1653 if (ret != HC_SUCCESS) {
1654 return;
1655 }
1656 }
1657 return;
1658 }
1659
IsTypeForSettingPtr(int32_t type)1660 static bool IsTypeForSettingPtr(int32_t type)
1661 {
1662 int32_t typeList[] = {
1663 PARAM_TYPE_APPID, PARAM_TYPE_DEV_AUTH_CB, PARAM_TYPE_LISTENER, PARAM_TYPE_CREATE_PARAMS,
1664 PARAM_TYPE_GROUPID, PARAM_TYPE_UDID, PARAM_TYPE_ADD_PARAMS, PARAM_TYPE_DEL_PARAMS,
1665 PARAM_TYPE_QUERY_PARAMS, PARAM_TYPE_COMM_DATA, PARAM_TYPE_SESS_KEY,
1666 PARAM_TYPE_REQ_INFO, PARAM_TYPE_GROUP_INFO, PARAM_TYPE_AUTH_PARAMS, PARAM_TYPE_REQ_JSON,
1667 PARAM_TYPE_PSEUDONYM_ID, PARAM_TYPE_INDEX_KEY, PARAM_TYPE_ERR_INFO, PARAM_TYPE_REQUEST_PARAMS,
1668 PARAM_TYPE_CRED_ID, PARAM_TYPE_PK_WITH_SIG, PARAM_TYPE_SERVICE_ID, PARAM_TYPE_RANDOM, PARAM_TYPE_CRED_INFO,
1669 };
1670 int32_t i;
1671 int32_t n = sizeof(typeList) / sizeof(typeList[0]);
1672 for (i = 0; i < n; i++) {
1673 if (typeList[i] == type) {
1674 return true;
1675 }
1676 }
1677 return false;
1678 }
1679
IsTypeForCpyData(int32_t type)1680 static bool IsTypeForCpyData(int32_t type)
1681 {
1682 int32_t typeList[] = {
1683 PARAM_TYPE_REQID, PARAM_TYPE_GROUP_TYPE, PARAM_TYPE_OPCODE, PARAM_TYPE_ERRCODE, PARAM_TYPE_OS_ACCOUNT_ID
1684 };
1685 int32_t i;
1686 int32_t n = sizeof(typeList) / sizeof(typeList[0]);
1687 for (i = 0; i < n; i++) {
1688 if (typeList[i] == type) {
1689 return true;
1690 }
1691 }
1692 return false;
1693 }
1694
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)1695 void DevAuthDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
1696 {
1697 LOGI("remote is not actively, to reset local resource");
1698 ResetIpcCallBackNodeByNodeId(callbackIdx);
1699 }
1700
GetIpcRequestParamByType(const IpcDataInfo * ipcParams,int32_t paramNum,int32_t type,uint8_t * paramCache,int32_t * cacheLen)1701 int32_t GetIpcRequestParamByType(const IpcDataInfo *ipcParams, int32_t paramNum,
1702 int32_t type, uint8_t *paramCache, int32_t *cacheLen)
1703 {
1704 int32_t i;
1705 int32_t ret = HC_ERR_IPC_BAD_MSG_TYPE;
1706 errno_t eno;
1707
1708 for (i = 0; i < paramNum; i++) {
1709 if (ipcParams[i].type != type) {
1710 continue;
1711 }
1712 ret = HC_SUCCESS;
1713 if (IsTypeForSettingPtr(type)) {
1714 *(reinterpret_cast<uint8_t **>(paramCache)) = ipcParams[i].val;
1715 if (cacheLen != nullptr) {
1716 *cacheLen = ipcParams[i].valSz;
1717 }
1718 break;
1719 }
1720 if (IsTypeForCpyData(type)) {
1721 if ((ipcParams[i].val == nullptr) || (ipcParams[i].valSz <= 0) || (cacheLen == nullptr)) {
1722 ret = HC_ERR_INVALID_PARAMS;
1723 break;
1724 }
1725 eno = memcpy_s(paramCache, *cacheLen, ipcParams[i].val, ipcParams[i].valSz);
1726 if (eno != EOK) {
1727 ret = HC_ERR_MEMORY_COPY;
1728 }
1729 *cacheLen = ipcParams[i].valSz;
1730 break;
1731 }
1732 if ((type == PARAM_TYPE_CB_OBJECT) && (cacheLen != nullptr) &&
1733 (static_cast<uint32_t>(*cacheLen) >= sizeof(ipcParams[i].idx))) {
1734 *(reinterpret_cast<int32_t *>(paramCache)) = ipcParams[i].idx;
1735 }
1736 break;
1737 }
1738 return ret;
1739 }
1740
IsCallbackMethod(int32_t methodId)1741 bool IsCallbackMethod(int32_t methodId)
1742 {
1743 if ((methodId == IPC_CALL_ID_REG_CB) || (methodId == IPC_CALL_ID_REG_LISTENER) ||
1744 (methodId == IPC_CALL_ID_DA_AUTH_DEVICE) || (methodId == IPC_CALL_ID_DA_PROC_DATA) ||
1745 (methodId == IPC_CALL_ID_GA_PROC_DATA) || (methodId == IPC_CALL_ID_AUTH_DEVICE) ||
1746 (methodId == IPC_CALL_ID_CM_REG_LISTENER) || (methodId == IPC_CALL_ID_CA_AUTH_CREDENTIAL) ||
1747 (methodId == IPC_CALL_ID_CA_PROCESS_CRED_DATA) || (methodId == IPC_CALL_ID_LA_START_LIGHT_ACCOUNT_AUTH)||
1748 (methodId == IPC_CALL_ID_LA_PROCESS_LIGHT_ACCOUNT_AUTH)) {
1749 return true;
1750 }
1751 return false;
1752 }
1753
UnInitProxyAdapt(void)1754 void UnInitProxyAdapt(void)
1755 {
1756 g_sdkCbStub[IPC_CALL_BACK_STUB_AUTH_ID] = nullptr;
1757 g_sdkCbStub[IPC_CALL_BACK_STUB_BIND_ID] = nullptr;
1758 g_sdkCbStub[IPC_CALL_BACK_STUB_DIRECT_AUTH_ID] = nullptr;
1759 g_sdkCbStub[IPC_CALL_BACK_STUB_LIGHT_AUTH_ID] = nullptr;
1760 return;
1761 }
1762
InitProxyAdapt(void)1763 int32_t InitProxyAdapt(void)
1764 {
1765 g_sdkCbStub[IPC_CALL_BACK_STUB_AUTH_ID] = new(std::nothrow) StubDevAuthCb;
1766 g_sdkCbStub[IPC_CALL_BACK_STUB_BIND_ID] = new(std::nothrow) StubDevAuthCb;
1767 g_sdkCbStub[IPC_CALL_BACK_STUB_DIRECT_AUTH_ID] = new(std::nothrow) StubDevAuthCb;
1768 g_sdkCbStub[IPC_CALL_BACK_STUB_LIGHT_AUTH_ID] = new(std::nothrow) StubDevAuthCb;
1769 if (!g_sdkCbStub[IPC_CALL_BACK_STUB_AUTH_ID] || !g_sdkCbStub[IPC_CALL_BACK_STUB_BIND_ID] ||
1770 !g_sdkCbStub[IPC_CALL_BACK_STUB_DIRECT_AUTH_ID] || !g_sdkCbStub[IPC_CALL_BACK_STUB_LIGHT_AUTH_ID]) {
1771 LOGE("alloc callback stub object failed");
1772 UnInitProxyAdapt();
1773 return HC_ERR_ALLOC_MEMORY;
1774 }
1775 return HC_SUCCESS;
1776 }
1777