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