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