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