• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "identity_service_ipc_sdk.h"
17 
18 #include "common_defs.h"
19 #include "device_auth_defines.h"
20 #include "device_auth.h"
21 #include "hc_log.h"
22 #include "hc_mutex.h"
23 #include "ipc_sdk_defines.h"
24 
25 #include "ipc_adapt.h"
26 #include "securec.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define IPC_DATA_CACHES_1 1
33 #define IPC_DATA_CACHES_3 3
34 #define IPC_DATA_CACHES_4 4
35 #define REPLAY_CACHE_NUM(caches) (sizeof(caches) / sizeof(IpcDataInfo))
36 #define IPC_APPID_LEN 128
37 
38 #define IS_COMM_DATA_VALID(dPtr, dLen) (((dPtr) != NULL) && ((dLen) > 0) && ((dLen) <= 4096))
39 
40 static const int32_t IPC_RESULT_NUM_1 = 1;
41 
42 typedef struct {
43     uintptr_t inst;
44     char appId[IPC_APPID_LEN];
45 } IpcProxyCbInfo;
46 
47 static IpcProxyCbInfo g_ipcCredListenerCbList = { 0 };
48 static HcMutex g_ipcMutex;
49 
IsStrInvalid(const char * str)50 static bool IsStrInvalid(const char *str)
51 {
52     return (str == NULL || str[0] == 0);
53 }
54 
DelIpcCliCallbackCtx(const char * appId,IpcProxyCbInfo * cbCache)55 static void DelIpcCliCallbackCtx(const char *appId, IpcProxyCbInfo *cbCache)
56 {
57     int32_t ret;
58 
59     if (cbCache->appId[0] == 0) {
60         return;
61     }
62     (void)LockHcMutex(&g_ipcMutex);
63     ret = memcmp(appId, cbCache->appId, HcStrlen(cbCache->appId) + 1);
64     if (ret == 0) {
65         cbCache->appId[0] = 0;
66     }
67     UnlockHcMutex(&g_ipcMutex);
68     return;
69 }
70 
AddIpcCliCallbackCtx(const char * appId,uintptr_t cbInst,IpcProxyCbInfo * cbCache)71 static void AddIpcCliCallbackCtx(const char *appId, uintptr_t cbInst, IpcProxyCbInfo *cbCache)
72 {
73     errno_t eno;
74 
75     (void)LockHcMutex(&g_ipcMutex);
76     eno = memcpy_s(cbCache->appId, IPC_APPID_LEN, appId, HcStrlen(appId) + 1);
77     if (eno != EOK) {
78         UnlockHcMutex(&g_ipcMutex);
79         LOGE("memory copy failed");
80         return;
81     }
82     cbCache->inst = cbInst;
83     UnlockHcMutex(&g_ipcMutex);
84 }
85 
GetIpcReplyByType(const IpcDataInfo * ipcData,int32_t dataNum,int32_t type,uint8_t * outCache,int32_t * cacheLen)86 static void GetIpcReplyByType(const IpcDataInfo *ipcData,
87     int32_t dataNum, int32_t type, uint8_t *outCache, int32_t *cacheLen)
88 {
89     int32_t i;
90     errno_t eno;
91 
92     for (i = 0; i < dataNum; i++) {
93         if (ipcData[i].type != type) {
94             continue;
95         }
96         switch (type) {
97             case PARAM_TYPE_REG_INFO:
98             case PARAM_TYPE_DEVICE_INFO:
99             case PARAM_TYPE_GROUP_INFO:
100             case PARAM_TYPE_CRED_ID:
101             case PARAM_TYPE_CRED_INFO:
102             case PARAM_TYPE_CRED_INFO_LIST:
103             case PARAM_TYPE_CRED_VAL:
104             case PARAM_TYPE_RETURN_DATA:
105                 *(uint8_t **)outCache = ipcData[i].val;
106                 break;
107             case PARAM_TYPE_IPC_RESULT:
108             case PARAM_TYPE_IPC_RESULT_NUM:
109             case PARAM_TYPE_COMM_DATA:
110             case PARAM_TYPE_DATA_NUM:
111                 eno = memcpy_s(outCache, *cacheLen, ipcData[i].val, ipcData[i].valSz);
112                 if (eno != EOK) {
113                     break;
114                 }
115                 *cacheLen = ipcData[i].valSz;
116                 break;
117             default:
118                 LOGE("un-expectation type case");
119                 break;
120         }
121     }
122     return;
123 }
124 
EncodeAddCredentialParams(uintptr_t callCtx,int32_t osAccountId,const char * requestParams)125 static int32_t EncodeAddCredentialParams(uintptr_t callCtx, int32_t osAccountId, const char *requestParams)
126 {
127     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
128         sizeof(osAccountId));
129     if (ret != HC_SUCCESS) {
130         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
131         return HC_ERR_IPC_BUILD_PARAM;
132     }
133     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQUEST_PARAMS, (const uint8_t *)requestParams,
134         HcStrlen(requestParams) + 1);
135     if (ret != HC_SUCCESS) {
136         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQUEST_PARAMS);
137         return HC_ERR_IPC_BUILD_PARAM;
138     }
139     return HC_SUCCESS;
140 }
141 
AddCredentialIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData)142 static int32_t AddCredentialIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData)
143 {
144     int32_t inOutLen;
145     int32_t ret;
146 
147     inOutLen = sizeof(int32_t);
148     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
149     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
150         return HC_ERR_IPC_OUT_DATA_NUM;
151     }
152     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_ID, (uint8_t *)returnData, NULL);
153     if (*returnData == NULL) {
154         return HC_ERR_IPC_OUT_DATA;
155     }
156     *returnData = strdup(*returnData);
157     if (*returnData == NULL) {
158         return HC_ERR_ALLOC_MEMORY;
159     }
160     return HC_SUCCESS;
161 }
162 
AgreeCredentialIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData)163 static int32_t AgreeCredentialIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData)
164 {
165     int32_t inOutLen;
166     int32_t ret;
167 
168     inOutLen = sizeof(int32_t);
169     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
170     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
171         return HC_ERR_IPC_OUT_DATA_NUM;
172     }
173     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_ID, (uint8_t *)returnData, NULL);
174     if (*returnData == NULL) {
175         return HC_ERR_IPC_OUT_DATA;
176     }
177     *returnData = strdup(*returnData);
178     if (*returnData == NULL) {
179         return HC_ERR_ALLOC_MEMORY;
180     }
181     return HC_SUCCESS;
182 }
183 
IpcCmAddCredential(int32_t osAccountId,const char * requestParams,char ** returnData)184 static int32_t IpcCmAddCredential(int32_t osAccountId, const char *requestParams, char **returnData)
185 {
186     uintptr_t callCtx = 0x0;
187     int32_t ret;
188     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
189 
190     LOGI("starting ...");
191     if (IsStrInvalid(requestParams) || returnData == NULL) {
192         LOGE("invalid params");
193         return HC_ERR_INVALID_PARAMS;
194     }
195     ret = CreateCallCtx(&callCtx, NULL);
196     if (ret != HC_SUCCESS) {
197         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
198         return HC_ERR_IPC_INIT;
199     }
200     ret = EncodeAddCredentialParams(callCtx, osAccountId, requestParams);
201     if (ret != HC_SUCCESS) {
202         DestroyCallCtx(&callCtx, NULL);
203         return ret;
204     }
205     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_ADD_CREDENTIAL, true);
206     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
207         LOGE("ipc call failed");
208         DestroyCallCtx(&callCtx, NULL);
209         return HC_ERR_IPC_PROC_FAILED;
210     }
211     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
212     ret = HC_ERR_IPC_UNKNOW_REPLY;
213     int32_t inOutLen = sizeof(int32_t);
214     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
215     if (ret != HC_SUCCESS) {
216         DestroyCallCtx(&callCtx, NULL);
217         return ret;
218     }
219     ret = AddCredentialIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnData);
220     DestroyCallCtx(&callCtx, NULL);
221     LOGI("process done, ret %" LOG_PUB "d", ret);
222     return ret;
223 }
224 
IpcCmRegChangeListener(const char * appId,CredChangeListener * listener)225 static int32_t IpcCmRegChangeListener(const char *appId, CredChangeListener *listener)
226 {
227     uintptr_t callCtx = 0x0;
228     int32_t ret;
229 
230     LOGI("starting ...");
231     if (IsStrInvalid(appId) || listener == NULL) {
232         LOGE("invalid params");
233         return HC_ERR_INVALID_PARAMS;
234     }
235     ret = CreateCallCtx(&callCtx, NULL);
236     if (ret != HC_SUCCESS) {
237         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
238         return HC_ERR_IPC_INIT;
239     }
240     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId,
241         HcStrlen(appId) + 1);
242     if (ret != HC_SUCCESS) {
243         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
244         DestroyCallCtx(&callCtx, NULL);
245         return HC_ERR_IPC_BUILD_PARAM;
246     }
247     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_LISTERNER, (const uint8_t *)listener,
248         sizeof(*listener));
249     if (ret != HC_SUCCESS) {
250         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_LISTERNER);
251         DestroyCallCtx(&callCtx, NULL);
252         return HC_ERR_IPC_BUILD_PARAM;
253     }
254     // why do this ?
255     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_BIND_ID);
256     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_REG_LISTENER, true);
257     if (ret == HC_SUCCESS) {
258         AddIpcCliCallbackCtx(appId, 0, &g_ipcCredListenerCbList);
259     }
260     DestroyCallCtx(&callCtx, NULL);
261     LOGI("process done, ret %" LOG_PUB "d", ret);
262     return (ret == HC_SUCCESS) ? HC_SUCCESS : HC_ERR_IPC_PROC_FAILED;
263 }
264 
IpcCmUnRegChangeListener(const char * appId)265 static int32_t IpcCmUnRegChangeListener(const char *appId)
266 {
267     uintptr_t callCtx = 0x0;
268     int32_t ret;
269 
270     LOGI("starting ...");
271     if (IsStrInvalid(appId)) {
272         LOGE("invalid params");
273         return HC_ERR_INVALID_PARAMS;
274     }
275     ret = CreateCallCtx(&callCtx, NULL);
276     if (ret != HC_SUCCESS) {
277         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
278         return HC_ERR_IPC_INIT;
279     }
280     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_APPID, (const uint8_t *)appId,
281         HcStrlen(appId) + 1);
282     if (ret != HC_SUCCESS) {
283         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_APPID);
284         DestroyCallCtx(&callCtx, NULL);
285         return HC_ERR_IPC_BUILD_PARAM;
286     }
287     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_UNREG_LISTENER, true);
288     if (ret == HC_SUCCESS) {
289         DelIpcCliCallbackCtx(appId, &g_ipcCredListenerCbList);
290     }
291     DestroyCallCtx(&callCtx, NULL);
292     LOGI("process done, ret %" LOG_PUB "d", ret);
293     return HC_SUCCESS;
294 }
295 
EncodeExportCredentialParams(uintptr_t callCtx,int32_t osAccountId,const char * credId)296 static int32_t EncodeExportCredentialParams(uintptr_t callCtx, int32_t osAccountId, const char *credId)
297 {
298     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
299         sizeof(osAccountId));
300     if (ret != HC_SUCCESS) {
301         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
302         return HC_ERR_IPC_BUILD_PARAM;
303     }
304     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CRED_ID, (const uint8_t *)credId,
305         HcStrlen(credId) + 1);
306     if (ret != HC_SUCCESS) {
307         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_CRED_ID);
308         return HC_ERR_IPC_BUILD_PARAM;
309     }
310     return HC_SUCCESS;
311 }
312 
ExportCredentialIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData)313 static int32_t ExportCredentialIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData)
314 {
315     int32_t inOutLen;
316     int32_t ret;
317 
318     inOutLen = sizeof(int32_t);
319     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
320     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
321         return HC_ERR_IPC_OUT_DATA_NUM;
322     }
323     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_VAL, (uint8_t *)returnData, NULL);
324     if (*returnData == NULL) {
325         return HC_ERR_IPC_OUT_DATA;
326     }
327     *returnData = strdup(*returnData);
328     if (*returnData == NULL) {
329         return HC_ERR_ALLOC_MEMORY;
330     }
331     return HC_SUCCESS;
332 }
333 
IpcCmExportCredential(int32_t osAccountId,const char * credId,char ** returnData)334 static int32_t IpcCmExportCredential(int32_t osAccountId, const char *credId, char **returnData)
335 {
336     uintptr_t callCtx = 0x0;
337     int32_t ret;
338     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
339 
340     LOGI("starting ...");
341     if (IsStrInvalid(credId) || returnData == NULL) {
342         LOGE("invalid params");
343         return HC_ERR_INVALID_PARAMS;
344     }
345     ret = CreateCallCtx(&callCtx, NULL);
346     if (ret != HC_SUCCESS) {
347         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
348         return HC_ERR_IPC_INIT;
349     }
350     ret = EncodeExportCredentialParams(callCtx, osAccountId, credId);
351     if (ret != HC_SUCCESS) {
352         DestroyCallCtx(&callCtx, NULL);
353         return ret;
354     }
355     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_EXPORT_CREDENTIAL, true);
356     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
357         LOGE("ipc call failed");
358         DestroyCallCtx(&callCtx, NULL);
359         return HC_ERR_IPC_PROC_FAILED;
360     }
361     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
362     ret = HC_ERR_IPC_UNKNOW_REPLY;
363     int32_t inOutLen = sizeof(int32_t);
364     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
365     if (ret != HC_SUCCESS) {
366         DestroyCallCtx(&callCtx, NULL);
367         return ret;
368     }
369     ret = ExportCredentialIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnData);
370     DestroyCallCtx(&callCtx, NULL);
371     LOGI("process done, ret %" LOG_PUB "d", ret);
372     return ret;
373 }
374 
SearchCredsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnCredList)375 static int32_t SearchCredsIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnCredList)
376 {
377     int32_t inOutLen;
378     int32_t ret;
379 
380     inOutLen = sizeof(int32_t);
381     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
382     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
383         return HC_ERR_IPC_OUT_DATA_NUM;
384     }
385     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_INFO_LIST, (uint8_t *)returnCredList, NULL);
386     if (*returnCredList == NULL) {
387         return HC_ERR_IPC_OUT_DATA;
388     }
389     *returnCredList = strdup(*returnCredList);
390     if (*returnCredList == NULL) {
391         return HC_ERR_ALLOC_MEMORY;
392     }
393     return HC_SUCCESS;
394 }
395 
IpcCmQueryCredByParams(int32_t osAccountId,const char * requestParams,char ** returnCredList)396 static int32_t IpcCmQueryCredByParams(int32_t osAccountId, const char *requestParams, char **returnCredList)
397 {
398     uintptr_t callCtx = 0x0;
399     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
400 
401     LOGI("starting ...");
402     if (IsStrInvalid(requestParams) || returnCredList == NULL) {
403         LOGE("invalid params");
404         return HC_ERR_INVALID_PARAMS;
405     }
406     int32_t ret = CreateCallCtx(&callCtx, NULL);
407     if (ret != HC_SUCCESS) {
408         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
409         return HC_ERR_IPC_INIT;
410     }
411     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
412         sizeof(osAccountId));
413     if (ret != HC_SUCCESS) {
414         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
415         DestroyCallCtx(&callCtx, NULL);
416         return HC_ERR_IPC_BUILD_PARAM;
417     }
418     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_QUERY_PARAMS, (const uint8_t *)requestParams,
419         HcStrlen(requestParams) + 1);
420     if (ret != HC_SUCCESS) {
421         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_QUERY_PARAMS);
422         DestroyCallCtx(&callCtx, NULL);
423         return HC_ERR_IPC_BUILD_PARAM;
424     }
425 
426     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_QUERY_CREDENTIAL_BY_PARAMS, true);
427     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
428         LOGE("ipc call failed");
429         DestroyCallCtx(&callCtx, NULL);
430         return HC_ERR_IPC_PROC_FAILED;
431     }
432     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
433     ret = HC_ERR_IPC_UNKNOW_REPLY;
434     int32_t inOutLen = sizeof(int32_t);
435     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
436     if (ret != HC_SUCCESS) {
437         DestroyCallCtx(&callCtx, NULL);
438         return ret;
439     }
440     ret = SearchCredsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnCredList);
441     DestroyCallCtx(&callCtx, NULL);
442     LOGI("process done, ret %" LOG_PUB "d", ret);
443     return ret;
444 }
445 
SearchCredInfoIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnCredInfo)446 static int32_t SearchCredInfoIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnCredInfo)
447 {
448     int32_t inOutLen;
449     int32_t ret;
450 
451     inOutLen = sizeof(int32_t);
452     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
453     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
454         return HC_ERR_IPC_OUT_DATA_NUM;
455     }
456     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_INFO, (uint8_t *)returnCredInfo, NULL);
457     if (*returnCredInfo == NULL) {
458         return HC_ERR_IPC_OUT_DATA;
459     }
460     *returnCredInfo = strdup(*returnCredInfo);
461     if (*returnCredInfo == NULL) {
462         return HC_ERR_ALLOC_MEMORY;
463     }
464     return HC_SUCCESS;
465 }
466 
IpcCmQueryCredInfoByCredId(int32_t osAccountId,const char * credId,char ** returnCredInfo)467 static int32_t IpcCmQueryCredInfoByCredId(int32_t osAccountId, const char *credId, char **returnCredInfo)
468 {
469     uintptr_t callCtx = 0x0;
470     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
471 
472     LOGI("starting ...");
473     if (IsStrInvalid(credId) || returnCredInfo == NULL) {
474         LOGE("invalid params");
475         return HC_ERR_INVALID_PARAMS;
476     }
477     int32_t ret = CreateCallCtx(&callCtx, NULL);
478     if (ret != HC_SUCCESS) {
479         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
480         return HC_ERR_IPC_INIT;
481     }
482     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
483         sizeof(osAccountId));
484     if (ret != HC_SUCCESS) {
485         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
486         DestroyCallCtx(&callCtx, NULL);
487         return HC_ERR_IPC_BUILD_PARAM;
488     }
489     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CRED_ID, (const uint8_t *)credId,
490         HcStrlen(credId) + 1);
491     if (ret != HC_SUCCESS) {
492         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_QUERY_PARAMS);
493         DestroyCallCtx(&callCtx, NULL);
494         return HC_ERR_IPC_BUILD_PARAM;
495     }
496 
497     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_QUERY_CREDENTIAL_BY_CRED_ID, true);
498     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
499         LOGE("ipc call failed");
500         DestroyCallCtx(&callCtx, NULL);
501         return HC_ERR_IPC_PROC_FAILED;
502     }
503     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
504     ret = HC_ERR_IPC_UNKNOW_REPLY;
505     int32_t inOutLen = sizeof(int32_t);
506     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
507     if (ret != HC_SUCCESS) {
508         DestroyCallCtx(&callCtx, NULL);
509         return ret;
510     }
511     ret = SearchCredInfoIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnCredInfo);
512     LOGI("process done, ret %" LOG_PUB "d", ret);
513     DestroyCallCtx(&callCtx, NULL);
514     return ret;
515 }
516 
EncodeDeleteCredentialParams(uintptr_t callCtx,int32_t osAccountId,const char * credId)517 static int32_t EncodeDeleteCredentialParams(uintptr_t callCtx, int32_t osAccountId, const char *credId)
518 {
519     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
520         sizeof(osAccountId));
521     if (ret != HC_SUCCESS) {
522         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
523         return HC_ERR_IPC_BUILD_PARAM;
524     }
525     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CRED_ID, (const uint8_t *)credId, HcStrlen(credId) + 1);
526     if (ret != HC_SUCCESS) {
527         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_CRED_ID);
528         return HC_ERR_IPC_BUILD_PARAM;
529     }
530     return HC_SUCCESS;
531 }
532 
IpcCmDeleteCredential(int32_t osAccountId,const char * credId)533 static int32_t IpcCmDeleteCredential(int32_t osAccountId, const char *credId)
534 {
535     uintptr_t callCtx = 0x0;
536     int32_t ret;
537     IpcDataInfo replyCache = { 0 };
538 
539     LOGI("starting ...");
540     if (IsStrInvalid(credId)) {
541         LOGE("invalid params");
542         return HC_ERR_INVALID_PARAMS;
543     }
544     ret = CreateCallCtx(&callCtx, NULL);
545     if (ret != HC_SUCCESS) {
546         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
547         return HC_ERR_IPC_INIT;
548     }
549     ret = EncodeDeleteCredentialParams(callCtx, osAccountId, credId);
550     if (ret != HC_SUCCESS) {
551         DestroyCallCtx(&callCtx, NULL);
552         return ret;
553     }
554     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_DEL_CREDENTIAL, true);
555     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
556         LOGE("ipc call failed");
557         DestroyCallCtx(&callCtx, NULL);
558         return HC_ERR_IPC_PROC_FAILED;
559     }
560     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
561     ret = HC_ERR_IPC_UNKNOW_REPLY;
562     int32_t inOutLen = sizeof(int32_t);
563     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
564     DestroyCallCtx(&callCtx, NULL);
565     LOGI("process done, ret %" LOG_PUB "d", ret);
566     return ret;
567 }
568 
EncodeUpdateParams(uintptr_t callCtx,int32_t osAccountId,const char * credId,const char * requestParams)569 static int32_t EncodeUpdateParams(uintptr_t callCtx, int32_t osAccountId, const char *credId, const char *requestParams)
570 {
571     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
572         sizeof(osAccountId));
573     if (ret != HC_SUCCESS) {
574         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
575         return HC_ERR_IPC_BUILD_PARAM;
576     }
577     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CRED_ID, (const uint8_t *)credId, HcStrlen(credId) + 1);
578     if (ret != HC_SUCCESS) {
579         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_CRED_ID);
580         return HC_ERR_IPC_BUILD_PARAM;
581     }
582     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQUEST_PARAMS, (const uint8_t *)requestParams,
583         HcStrlen(requestParams) + 1);
584     if (ret != HC_SUCCESS) {
585         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQUEST_PARAMS);
586         return HC_ERR_IPC_BUILD_PARAM;
587     }
588     return HC_SUCCESS;
589 }
590 
IpcCmUpdateCredInfo(int32_t osAccountId,const char * credId,const char * requestParams)591 static int32_t IpcCmUpdateCredInfo(int32_t osAccountId, const char *credId, const char *requestParams)
592 {
593     uintptr_t callCtx = 0x0;
594     int32_t ret;
595     IpcDataInfo replyCache = { 0 };
596 
597     LOGI("starting ...");
598     if (IsStrInvalid(credId) || IsStrInvalid(requestParams)) {
599         LOGE("invalid params");
600         return HC_ERR_INVALID_PARAMS;
601     }
602     ret = CreateCallCtx(&callCtx, NULL);
603     if (ret != HC_SUCCESS) {
604         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
605         return HC_ERR_IPC_INIT;
606     }
607     ret = EncodeUpdateParams(callCtx, osAccountId, credId, requestParams);
608     if (ret != HC_SUCCESS) {
609         DestroyCallCtx(&callCtx, NULL);
610         return ret;
611     }
612     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_UPDATE_CRED_INFO, true);
613     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
614         LOGE("ipc call failed");
615         DestroyCallCtx(&callCtx, NULL);
616         return HC_ERR_IPC_PROC_FAILED;
617     }
618     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
619     ret = HC_ERR_IPC_UNKNOW_REPLY;
620     int32_t inOutLen = sizeof(int32_t);
621     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
622     DestroyCallCtx(&callCtx, NULL);
623     LOGI("process done, ret %" LOG_PUB "d", ret);
624     return ret;
625 }
626 
EncodeAgreeCredentialParams(uintptr_t callCtx,int32_t osAccountId,const char * selfCredId,const char * requestParams)627 static int32_t EncodeAgreeCredentialParams(uintptr_t callCtx, int32_t osAccountId, const char *selfCredId,
628     const char *requestParams)
629 {
630     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
631         sizeof(osAccountId));
632     if (ret != HC_SUCCESS) {
633         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
634         return HC_ERR_IPC_BUILD_PARAM;
635     }
636     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_CRED_ID, (const uint8_t *)selfCredId, HcStrlen(selfCredId) + 1);
637     if (ret != HC_SUCCESS) {
638         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_CRED_ID);
639         return HC_ERR_IPC_BUILD_PARAM;
640     }
641     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQUEST_PARAMS, (const uint8_t *)requestParams,
642         HcStrlen(requestParams) + 1);
643     if (ret != HC_SUCCESS) {
644         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQUEST_PARAMS);
645         return HC_ERR_IPC_BUILD_PARAM;
646     }
647     return HC_SUCCESS;
648 }
649 
IpcCmAgreeCredential(int32_t osAccountId,const char * selfCredId,const char * requestParams,char ** returnData)650 static int32_t IpcCmAgreeCredential(int32_t osAccountId, const char *selfCredId, const char *requestParams,
651     char **returnData)
652 {
653     uintptr_t callCtx = 0x0;
654     int32_t ret;
655     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
656 
657     LOGI("starting ...");
658     if (IsStrInvalid(selfCredId) || IsStrInvalid(requestParams) || returnData == NULL) {
659         LOGE("invalid params");
660         return HC_ERR_INVALID_PARAMS;
661     }
662     ret = CreateCallCtx(&callCtx, NULL);
663     if (ret != HC_SUCCESS) {
664         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
665         return HC_ERR_IPC_INIT;
666     }
667     ret = EncodeAgreeCredentialParams(callCtx, osAccountId, selfCredId, requestParams);
668     if (ret != HC_SUCCESS) {
669         DestroyCallCtx(&callCtx, NULL);
670         return ret;
671     }
672     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_AGREE_CREDENTIAL, true);
673     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
674         LOGE("ipc call failed");
675         DestroyCallCtx(&callCtx, NULL);
676         return HC_ERR_IPC_PROC_FAILED;
677     }
678     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
679     ret = HC_ERR_IPC_UNKNOW_REPLY;
680     int32_t inOutLen = sizeof(int32_t);
681     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
682     if (ret != HC_SUCCESS) {
683         DestroyCallCtx(&callCtx, NULL);
684         return ret;
685     }
686     ret = AgreeCredentialIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnData);
687     DestroyCallCtx(&callCtx, NULL);
688     LOGI("process done, ret %" LOG_PUB "d", ret);
689     return ret;
690 }
691 
EncodeDelCredByParams(uintptr_t callCtx,int32_t osAccountId,const char * requestParams)692 static int32_t EncodeDelCredByParams(uintptr_t callCtx, int32_t osAccountId, const char *requestParams)
693 {
694     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
695         sizeof(osAccountId));
696     if (ret != HC_SUCCESS) {
697         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
698         return HC_ERR_IPC_BUILD_PARAM;
699     }
700     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQUEST_PARAMS, (const uint8_t *)requestParams,
701         HcStrlen(requestParams) + 1);
702     if (ret != HC_SUCCESS) {
703         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQUEST_PARAMS);
704         return HC_ERR_IPC_BUILD_PARAM;
705     }
706     return HC_SUCCESS;
707 }
708 
DelCredByParamsIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData)709 static int32_t DelCredByParamsIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData)
710 {
711     int32_t inOutLen;
712     int32_t ret;
713 
714     inOutLen = sizeof(int32_t);
715     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
716     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
717         return HC_ERR_IPC_OUT_DATA_NUM;
718     }
719     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_INFO_LIST, (uint8_t *)returnData, NULL);
720     if (*returnData == NULL) {
721         return HC_ERR_IPC_OUT_DATA;
722     }
723     *returnData = strdup(*returnData);
724     if (*returnData == NULL) {
725         return HC_ERR_ALLOC_MEMORY;
726     }
727     return HC_SUCCESS;
728 }
729 
IpcCmDelCredByParams(int32_t osAccountId,const char * requestParams,char ** returnData)730 static int32_t IpcCmDelCredByParams(int32_t osAccountId, const char *requestParams, char **returnData)
731 {
732     uintptr_t callCtx = 0x0;
733     int32_t ret;
734     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
735 
736     LOGI("starting ...");
737     if (IsStrInvalid(requestParams) || returnData == NULL) {
738         LOGE("invalid params");
739         return HC_ERR_INVALID_PARAMS;
740     }
741     ret = CreateCallCtx(&callCtx, NULL);
742     if (ret != HC_SUCCESS) {
743         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
744         return HC_ERR_IPC_INIT;
745     }
746     ret = EncodeDelCredByParams(callCtx, osAccountId, requestParams);
747     if (ret != HC_SUCCESS) {
748         DestroyCallCtx(&callCtx, NULL);
749         return ret;
750     }
751     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_DEL_CRED_BY_PARAMS, true);
752     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
753         LOGE("ipc call failed");
754         DestroyCallCtx(&callCtx, NULL);
755         return HC_ERR_IPC_PROC_FAILED;
756     }
757     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
758     ret = HC_ERR_IPC_UNKNOW_REPLY;
759     int32_t inOutLen = sizeof(int32_t);
760     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
761     if (ret != HC_SUCCESS) {
762         DestroyCallCtx(&callCtx, NULL);
763         return ret;
764     }
765     ret = DelCredByParamsIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnData);
766     DestroyCallCtx(&callCtx, NULL);
767     LOGI("process done, ret %" LOG_PUB "d", ret);
768     return ret;
769 }
770 
EncodeBatchUpdateParam(uintptr_t callCtx,int32_t osAccountId,const char * requestParams)771 static int32_t EncodeBatchUpdateParam(uintptr_t callCtx, int32_t osAccountId, const char *requestParams)
772 {
773     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
774         sizeof(osAccountId));
775     if (ret != HC_SUCCESS) {
776         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
777         return HC_ERR_IPC_BUILD_PARAM;
778     }
779     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQUEST_PARAMS, (const uint8_t *)requestParams,
780         HcStrlen(requestParams) + 1);
781     if (ret != HC_SUCCESS) {
782         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_REQUEST_PARAMS);
783         return HC_ERR_IPC_BUILD_PARAM;
784     }
785     return HC_SUCCESS;
786 }
787 
BatchUpdateCredIpcResult(const IpcDataInfo * replies,int32_t cacheNum,char ** returnData)788 static int32_t BatchUpdateCredIpcResult(const IpcDataInfo *replies, int32_t cacheNum, char **returnData)
789 {
790     int32_t inOutLen;
791     int32_t ret;
792 
793     inOutLen = sizeof(int32_t);
794     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_IPC_RESULT_NUM, (uint8_t *)&ret, &inOutLen);
795     if ((ret < IPC_RESULT_NUM_1) || (inOutLen != sizeof(int32_t))) {
796         return HC_ERR_IPC_OUT_DATA_NUM;
797     }
798     GetIpcReplyByType(replies, cacheNum, PARAM_TYPE_CRED_INFO_LIST, (uint8_t *)returnData, NULL);
799     if (*returnData == NULL) {
800         return HC_ERR_IPC_OUT_DATA;
801     }
802     *returnData = strdup(*returnData);
803     if (*returnData == NULL) {
804         return HC_ERR_ALLOC_MEMORY;
805     }
806     return HC_SUCCESS;
807 }
808 
IpcCmBatchUpdateCredentials(int32_t osAccountId,const char * requestParams,char ** returnData)809 static int32_t IpcCmBatchUpdateCredentials(int32_t osAccountId, const char *requestParams, char **returnData)
810 {
811     uintptr_t callCtx = 0x0;
812     int32_t ret;
813     IpcDataInfo replyCache[IPC_DATA_CACHES_3] = { { 0 } };
814 
815     LOGI("starting ...");
816     if (IsStrInvalid(requestParams) || returnData == NULL) {
817         LOGE("invalid params");
818         return HC_ERR_INVALID_PARAMS;
819     }
820     ret = CreateCallCtx(&callCtx, NULL);
821     if (ret != HC_SUCCESS) {
822         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
823         return HC_ERR_IPC_INIT;
824     }
825     ret = EncodeBatchUpdateParam(callCtx, osAccountId, requestParams);
826     if (ret != HC_SUCCESS) {
827         DestroyCallCtx(&callCtx, NULL);
828         return ret;
829     }
830     ret = DoBinderCall(callCtx, IPC_CALL_ID_CM_BATCH_UPDATE_CREDENTIALS, true);
831     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
832         LOGE("ipc call failed");
833         DestroyCallCtx(&callCtx, NULL);
834         return HC_ERR_IPC_PROC_FAILED;
835     }
836     DecodeCallReply(callCtx, replyCache, REPLAY_CACHE_NUM(replyCache));
837     ret = HC_ERR_IPC_UNKNOW_REPLY;
838     int32_t inOutLen = sizeof(int32_t);
839     GetIpcReplyByType(replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
840     if (ret != HC_SUCCESS) {
841         DestroyCallCtx(&callCtx, NULL);
842         return ret;
843     }
844     ret = BatchUpdateCredIpcResult(replyCache, REPLAY_CACHE_NUM(replyCache), returnData);
845     DestroyCallCtx(&callCtx, NULL);
846     LOGI("process done, ret %" LOG_PUB "d", ret);
847     return ret;
848 }
849 
IsJsonString(const char * str)850 static bool IsJsonString(const char *str)
851 {
852     CJson *json = CreateJsonFromString(str);
853     if (json == NULL) {
854         return false;
855     }
856     FreeJson(json);
857     return true;
858 }
859 
IpcCmDestroyInfo(char ** returnData)860 static void IpcCmDestroyInfo(char **returnData)
861 {
862     if (returnData == NULL || *returnData == NULL) {
863         return;
864     }
865     if (IsJsonString(*returnData)) {
866         FreeJsonString(*returnData);
867     } else {
868         HcFree(*returnData);
869     }
870     *returnData = NULL;
871 }
872 
InitIpcCmMethods(CredManager * cmMethodObj)873 static void InitIpcCmMethods(CredManager *cmMethodObj)
874 {
875     cmMethodObj->addCredential = IpcCmAddCredential;
876     cmMethodObj->exportCredential = IpcCmExportCredential;
877     cmMethodObj->registerChangeListener = IpcCmRegChangeListener;
878     cmMethodObj->unregisterChangeListener = IpcCmUnRegChangeListener;
879     cmMethodObj->queryCredentialByParams = IpcCmQueryCredByParams;
880     cmMethodObj->queryCredInfoByCredId = IpcCmQueryCredInfoByCredId;
881     cmMethodObj->deleteCredential = IpcCmDeleteCredential;
882     cmMethodObj->updateCredInfo = IpcCmUpdateCredInfo;
883     cmMethodObj->agreeCredential = IpcCmAgreeCredential;
884     cmMethodObj->deleteCredByParams = IpcCmDelCredByParams;
885     cmMethodObj->batchUpdateCredentials = IpcCmBatchUpdateCredentials;
886     cmMethodObj->destroyInfo = IpcCmDestroyInfo;
887     return;
888 }
889 
890 
EncodeAuthDeviceParams(uintptr_t callCtx,int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * callback)891 static int32_t EncodeAuthDeviceParams(uintptr_t callCtx, int32_t osAccountId, int64_t authReqId,
892     const char *authParams, const DeviceAuthCallback *callback)
893 {
894     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_OS_ACCOUNT_ID, (const uint8_t *)&osAccountId,
895         sizeof(osAccountId));
896     if (ret != HC_SUCCESS) {
897         LOGE("set request param failed, ret %" LOG_PUB "d, param id %" LOG_PUB "d", ret, PARAM_TYPE_OS_ACCOUNT_ID);
898         return HC_ERR_IPC_BUILD_PARAM;
899     }
900     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
901     if (ret != HC_SUCCESS) {
902         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
903         return HC_ERR_IPC_BUILD_PARAM;
904     }
905     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_AUTH_PARAMS, (const uint8_t *)authParams,
906         HcStrlen(authParams) + 1);
907     if (ret != HC_SUCCESS) {
908         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_AUTH_PARAMS);
909         return HC_ERR_IPC_BUILD_PARAM;
910     }
911     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
912     if (ret != HC_SUCCESS) {
913         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
914         return HC_ERR_IPC_BUILD_PARAM;
915     }
916     SetCbCtxToDataCtx(callCtx, IPC_CALL_BACK_STUB_AUTH_ID);
917     return HC_SUCCESS;
918 }
919 
IpcCmAuthCredential(int32_t osAccountId,int64_t authReqId,const char * authParams,const DeviceAuthCallback * caCallback)920 static int32_t IpcCmAuthCredential(int32_t osAccountId, int64_t authReqId, const char *authParams,
921     const DeviceAuthCallback *caCallback)
922 {
923     uintptr_t callCtx = 0x0;
924     int32_t ret;
925     int32_t inOutLen;
926     IpcDataInfo replyCache = { 0 };
927 
928     LOGI("starting ...");
929     if (IsStrInvalid(authParams) || (caCallback == NULL)) {
930         LOGE("invalid params");
931         return HC_ERR_INVALID_PARAMS;
932     }
933     ret = CreateCallCtx(&callCtx, NULL);
934     if (ret != HC_SUCCESS) {
935         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
936         return HC_ERR_IPC_INIT;
937     }
938     ret = EncodeAuthDeviceParams(callCtx, osAccountId, authReqId, authParams, caCallback);
939     if (ret != HC_SUCCESS) {
940         DestroyCallCtx(&callCtx, NULL);
941         return ret;
942     }
943     ret = DoBinderCall(callCtx, IPC_CALL_ID_CA_AUTH_DEVICE, true);
944     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
945         LOGE("ipc call failed");
946         DestroyCallCtx(&callCtx, NULL);
947         return HC_ERR_IPC_PROC_FAILED;
948     }
949     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
950     ret = HC_ERR_IPC_UNKNOW_REPLY;
951     inOutLen = sizeof(int32_t);
952     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
953     LOGI("process done, ret %" LOG_PUB "d", ret);
954     DestroyCallCtx(&callCtx, NULL);
955     return ret;
956 }
957 
EncodeProcessDataParams(uintptr_t callCtx,int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * callback)958 static int32_t EncodeProcessDataParams(uintptr_t callCtx, int64_t authReqId,
959     const uint8_t *data, uint32_t dataLen, const DeviceAuthCallback *callback)
960 {
961     int32_t ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_REQID, (const uint8_t *)(&authReqId), sizeof(authReqId));
962     if (ret != HC_SUCCESS) {
963         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_REQID);
964         return HC_ERR_IPC_BUILD_PARAM;
965     }
966     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_COMM_DATA, (const uint8_t *)data, dataLen);
967     if (ret != HC_SUCCESS) {
968         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_COMM_DATA);
969         return HC_ERR_IPC_BUILD_PARAM;
970     }
971     ret = SetCallRequestParamInfo(callCtx, PARAM_TYPE_DEV_AUTH_CB, (const uint8_t *)callback, sizeof(*callback));
972     if (ret != HC_SUCCESS) {
973         LOGE("set request param failed, ret %" LOG_PUB "d, type %" LOG_PUB "d", ret, PARAM_TYPE_DEV_AUTH_CB);
974         return HC_ERR_IPC_BUILD_PARAM;
975     }
976     SetCbCtxToDataCtx(callCtx, 0x0);
977     return HC_SUCCESS;
978 }
979 
IpcCmProcessCredData(int64_t authReqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * callback)980 static int32_t IpcCmProcessCredData(int64_t authReqId, const uint8_t *data, uint32_t dataLen,
981     const DeviceAuthCallback *callback)
982 {
983     uintptr_t callCtx = 0x0;
984     int32_t ret;
985     int32_t inOutLen;
986     IpcDataInfo replyCache = { 0 };
987 
988     LOGI("starting ...");
989     if (!IS_COMM_DATA_VALID(data, dataLen) || (callback == NULL)) {
990         LOGE("invalid params");
991         return HC_ERR_INVALID_PARAMS;
992     }
993     ret = CreateCallCtx(&callCtx, NULL);
994     if (ret != HC_SUCCESS) {
995         LOGE("CreateCallCtx failed, ret %" LOG_PUB "d", ret);
996         return HC_ERR_IPC_INIT;
997     }
998     ret = EncodeProcessDataParams(callCtx, authReqId, data, dataLen, callback);
999     if (ret != HC_SUCCESS) {
1000         DestroyCallCtx(&callCtx, NULL);
1001         return ret;
1002     }
1003     ret = DoBinderCall(callCtx, IPC_CALL_ID_CA_PROCESS_CRED_DATA, true);
1004     if (ret == HC_ERR_IPC_INTERNAL_FAILED) {
1005         LOGE("ipc call failed");
1006         DestroyCallCtx(&callCtx, NULL);
1007         return HC_ERR_IPC_PROC_FAILED;
1008     }
1009     DecodeCallReply(callCtx, &replyCache, REPLAY_CACHE_NUM(replyCache));
1010     ret = HC_ERR_IPC_UNKNOW_REPLY;
1011     inOutLen = sizeof(int32_t);
1012     GetIpcReplyByType(&replyCache, REPLAY_CACHE_NUM(replyCache), PARAM_TYPE_IPC_RESULT, (uint8_t *)&ret, &inOutLen);
1013     LOGI("process done, ret %" LOG_PUB "d", ret);
1014     DestroyCallCtx(&callCtx, NULL);
1015     return ret;
1016 }
1017 
InitIpcCaMethods(CredAuthManager * caMethodObj)1018 static void InitIpcCaMethods(CredAuthManager *caMethodObj)
1019 {
1020     caMethodObj->authCredential = IpcCmAuthCredential;
1021     caMethodObj->processCredData = IpcCmProcessCredData;
1022     return;
1023 }
1024 
InitISIpc(void)1025 void InitISIpc(void)
1026 {
1027     InitHcMutex(&g_ipcMutex, false);
1028 }
1029 
DeInitISIpc(void)1030 void DeInitISIpc(void)
1031 {
1032     DestroyHcMutex(&g_ipcMutex);
1033 }
1034 
GetCredMgrInstance(void)1035 DEVICE_AUTH_API_PUBLIC const CredManager *GetCredMgrInstance(void)
1036 {
1037     static CredManager cmInstCtx;
1038     static CredManager *cmInstPtr = NULL;
1039 
1040     if (cmInstPtr == NULL) {
1041         InitIpcCmMethods(&cmInstCtx);
1042         cmInstPtr = &cmInstCtx;
1043     }
1044     return (const CredManager *)(cmInstPtr);
1045 }
1046 
GetCredAuthInstance(void)1047 DEVICE_AUTH_API_PUBLIC const CredAuthManager *GetCredAuthInstance(void)
1048 {
1049     static CredAuthManager caInstCtx;
1050     static CredAuthManager *caInstPtr = NULL;
1051 
1052     if (caInstPtr == NULL) {
1053         InitIpcCaMethods(&caInstCtx);
1054         caInstPtr = &caInstCtx;
1055     }
1056     return (const CredAuthManager *)(caInstPtr);
1057 }
1058 
1059 #ifdef __cplusplus
1060 }
1061 #endif
1062