• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "das_task_main.h"
17 #include "alg_loader.h"
18 #include "base_sub_task.h"
19 #include "das_lite_token_manager.h"
20 #include "das_standard_token_manager.h"
21 #include "das_task_common.h"
22 #include "hc_log.h"
23 #include "iso_task_main.h"
24 #include "pake_v2_task_main.h"
25 #include "pake_protocol_dl_common.h"
26 #include "pake_protocol_ec_common.h"
27 #include "pake_v1_task_main.h"
28 #include "protocol_common.h"
29 #include "string_util.h"
30 
31 typedef struct DasProtocolEntityT {
32     ProtocolType type;
33     uint32_t algInProtocol;
34     const TokenManager *tokenManagerInstance;
35     SubTaskBase *(*createSubTask)(const CJson *);
36 } DasProtocolEntity;
37 
38 IMPLEMENT_HC_VECTOR(SubTaskVec, void *, 1)
39 DECLARE_HC_VECTOR(DasProtocolEntityVec, void *);
40 IMPLEMENT_HC_VECTOR(DasProtocolEntityVec, void *, 1)
41 
42 DasProtocolEntityVec g_protocolEntityVec;
43 ProtocolType g_taskTypeToProtocolType[] = {
44     ISO, // TASK_TYPE_ISO_PROTOCOL = 0,
45     PAKE_V1, // TASK_TYPE_PAKE_V1_PROTOCOL = 1,
46     PAKE_V2 // TASK_TYPE_PAKE_V2_PROTOCOL = 2,
47 };
48 
GetMinVersion(VersionStruct * version)49 static void GetMinVersion(VersionStruct *version)
50 {
51     version->first = 1;
52     version->second = 0;
53     version->third = 0;
54 }
55 
GetMaxVersion(VersionStruct * version)56 static void GetMaxVersion(VersionStruct *version)
57 {
58     version->first = MAJOR_VERSION_NO;
59     version->second = 0;
60     version->third = 0;
61 
62     uint32_t index;
63     void **ptr = NULL;
64     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
65         DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
66         version->third = (version->third) | temp->algInProtocol;
67     }
68 }
69 
InitVersionInfo(VersionInfo * versionInfo)70 static void InitVersionInfo(VersionInfo *versionInfo)
71 {
72     GetMinVersion(&(versionInfo->minVersion));
73     GetMaxVersion(&(versionInfo->curVersion));
74     versionInfo->versionStatus = INITIAL;
75 }
76 
AddVersionToOut(const VersionInfo * versionInfo,CJson * out)77 static int AddVersionToOut(const VersionInfo *versionInfo, CJson *out)
78 {
79     CJson *payload = GetObjFromJson(out, FIELD_PAYLOAD);
80     if (payload == NULL) {
81         LOGD("Not find payload.");
82         return HC_SUCCESS;
83     }
84     return AddVersionToJson(payload, &(versionInfo->minVersion), &(versionInfo->curVersion));
85 }
86 
CombineJson(CJson * desObj,const CJson * srcObj)87 static int CombineJson(CJson *desObj, const CJson *srcObj)
88 {
89     CHECK_PTR_RETURN_ERROR_CODE(desObj, "desObj");
90     CHECK_PTR_RETURN_ERROR_CODE(srcObj, "srcObj");
91     int res;
92     int len = GetItemNum(srcObj);
93     for (int i = 0; i < len; i++) {
94         CJson *item = GetItemFromArray(srcObj, i);
95         const char *key = GetItemKey(item);
96         if (key == NULL) {
97             LOGE("key is null.");
98             return HC_ERR_NULL_PTR;
99         }
100         CJson *payload = GetObjFromJson(desObj, FIELD_PAYLOAD);
101         if (IsStrEqual(key, FIELD_PAYLOAD) && payload != NULL) {
102             res = CombineJson(payload, item);
103             if (res != HC_SUCCESS) {
104                 LOGE("Combine payload failed, res: %" LOG_PUB "x.", res);
105                 return res;
106             }
107         } else {
108             if (AddObjToJson(desObj, key, item) != HC_SUCCESS) {
109                 LOGE("AddObjToJson failed.");
110                 return HC_ERR_JSON_ADD;
111             }
112         }
113     }
114     return HC_SUCCESS;
115 }
116 
DestroyTaskT(Task * task)117 static void DestroyTaskT(Task *task)
118 {
119     if (task == NULL) {
120         return;
121     }
122     uint32_t index;
123     void **ptr = NULL;
124     FOR_EACH_HC_VECTOR(task->vec, index, ptr) {
125         ((SubTaskBase *)(*ptr))->destroyTask((SubTaskBase *)(*ptr));
126     }
127     DESTROY_HC_VECTOR(SubTaskVec, &(task->vec));
128     HcFree(task);
129 }
130 
ProcessMultiTask(Task * task,const CJson * in,CJson * out,int32_t * status)131 static int ProcessMultiTask(Task *task, const CJson *in, CJson *out, int32_t *status)
132 {
133     int res = HC_SUCCESS;
134     uint32_t index;
135     void **ptr = NULL;
136     CJson *tmpOut = NULL;
137     CJson *combinedSendToPeer = CreateJson();
138     if (combinedSendToPeer == NULL) {
139         LOGE("Create combinedSendToPeer failed.");
140         return HC_ERR_JSON_CREATE;
141     }
142     FOR_EACH_HC_VECTOR(task->vec, index, ptr) {
143         tmpOut = CreateJson();
144         if (tmpOut == NULL) {
145             LOGE("Create tmpOut failed.");
146             res = HC_ERR_JSON_CREATE;
147             goto ERR;
148         }
149         res = ((SubTaskBase *)(*ptr))->process((*ptr), in, tmpOut, status);
150         if (res != HC_SUCCESS) {
151             LOGE("Process subTask failed, index: %" LOG_PUB "u, res: %" LOG_PUB "x.", index, res);
152             goto ERR;
153         }
154 
155         CJson *tmpSendToPeer = GetObjFromJson(tmpOut, FIELD_SEND_TO_PEER);
156         res = CombineJson(combinedSendToPeer, tmpSendToPeer);
157         if (res != HC_SUCCESS) {
158             LOGE("CombineJson failed, res: %" LOG_PUB "x.", res);
159             goto ERR;
160         }
161         FreeJson(tmpOut);
162         tmpOut = NULL;
163     }
164     if (AddObjToJson(out, FIELD_SEND_TO_PEER, combinedSendToPeer) != HC_SUCCESS) {
165         LOGE("Add combinedSendToPeer to json object failed.");
166         res = HC_ERR_JSON_ADD;
167         goto ERR;
168     }
169 ERR:
170     FreeJson(combinedSendToPeer);
171     FreeJson(tmpOut);
172     return res;
173 }
174 
NegotiateAndProcessTask(Task * task,const CJson * in,CJson * out,int32_t * status)175 static int NegotiateAndProcessTask(Task *task, const CJson *in, CJson *out, int32_t *status)
176 {
177     VersionStruct curVersionPeer = { 0, 0, 0 };
178     VersionStruct minVersionPeer = { 0, 0, 0 };
179     int res = GetVersionFromJson(in, &minVersionPeer, &curVersionPeer);
180     if (res != HC_SUCCESS) {
181         LOGE("Get peer version info failed, res: %" LOG_PUB "x.", res);
182         return res;
183     }
184     res = NegotiateVersion(&minVersionPeer, &curVersionPeer, &(task->versionInfo.curVersion));
185     if (res != HC_SUCCESS) {
186         LOGE("NegotiateVersion failed, res: %" LOG_PUB "x.", res);
187         return res;
188     }
189     if (!IsVersionEqual(&(task->versionInfo.curVersion), &curVersionPeer)) {
190         LOGE("Negotiated version is not matched with peer.");
191         return HC_ERR_UNSUPPORTED_VERSION;
192     }
193     ProtocolType protocolType = GetPrototolType(&(task->versionInfo.curVersion), task->versionInfo.opCode);
194     LOGI("Client select protocolType: %" LOG_PUB "d", protocolType);
195 
196     SubTaskBase *subTask = NULL;
197     uint32_t index = 0;
198     void **ptr = task->vec.getp(&(task->vec), 0);
199     while (index < task->vec.size(&(task->vec)) && ptr != NULL) {
200         SubTaskBase *temp = (SubTaskBase *)(*ptr);
201         if (g_taskTypeToProtocolType[temp->getTaskType(temp)] == protocolType) {
202             subTask = temp;
203             index++;
204         } else {
205             temp->destroyTask(temp);
206             task->vec.eraseElement(&(task->vec), ptr, index);
207         }
208         ptr = task->vec.getp(&(task->vec), index);
209     }
210     if (subTask == NULL) {
211         LOGE("Can't find matched subTask.");
212         return HC_ERR_NOT_SUPPORT;
213     }
214     subTask->curVersion = task->versionInfo.curVersion;
215     res = subTask->process(subTask, in, out, status);
216     if (res != HC_SUCCESS) {
217         LOGE("Process subTask failed, res: %" LOG_PUB "x.", res);
218     }
219     return res;
220 }
221 
IsPeerErrMessage(const CJson * in,int32_t * res)222 static bool IsPeerErrMessage(const CJson *in, int32_t *res)
223 {
224     int32_t message = 0;
225     if (GetIntFromJson(in, FIELD_MESSAGE, &message) != HC_SUCCESS) {
226         LOGD("There is no message code.");
227         return false;
228     }
229     if (message != ERR_MESSAGE) {
230         return false;
231     }
232 
233     if (GetIntFromJson(in, FIELD_ERROR_CODE, res) != HC_SUCCESS) {
234         LOGE("Get peer error code failed.");
235     }
236     return true;
237 }
238 
ProcessTaskT(Task * task,const CJson * in,CJson * out,int32_t * status)239 static int ProcessTaskT(Task *task, const CJson *in, CJson *out, int32_t *status)
240 {
241     int32_t res;
242     if (IsPeerErrMessage(in, &res)) {
243         LOGE("Receive error message from peer, errCode: %" LOG_PUB "x.", res);
244         DasSendErrMsgToSelf(out, res | PEER_ERROR_MASK);
245         return HC_ERR_PEER_ERROR;
246     }
247     if (task->vec.size(&(task->vec)) == 0) {
248         LOGE("Task hasn't subTask.");
249         res = HC_ERR_TASK_IS_NULL;
250         goto ERR;
251     }
252 
253     if (task->versionInfo.versionStatus == INITIAL) {
254         res = ProcessMultiTask(task, in, out, status);
255         if (res != HC_SUCCESS) {
256             LOGE("ProcessMultiTask failed, res: %" LOG_PUB "x.", res);
257             goto ERR;
258         }
259         task->versionInfo.versionStatus = VERSION_CONFIRM;
260     } else if (task->versionInfo.versionStatus == VERSION_CONFIRM) {
261         res = NegotiateAndProcessTask(task, in, out, status);
262         if (res != HC_SUCCESS) {
263             LOGE("NegotiateAndProcessTask failed, res: %" LOG_PUB "x.", res);
264             goto ERR;
265         }
266         task->versionInfo.versionStatus = VERSION_DECIDED;
267     } else {
268         SubTaskBase *subTask = HC_VECTOR_GET(&(task->vec), 0);
269         res = subTask->process(subTask, in, out, status);
270         if (res != HC_SUCCESS) {
271             LOGE("Process subTask failed, res: %" LOG_PUB "x.", res);
272             goto ERR;
273         }
274     }
275 
276     res = AddVersionToOut(&(task->versionInfo), out);
277     if (res != HC_SUCCESS) {
278         LOGE("AddVersionToOut failed, res: %" LOG_PUB "x.", res);
279         goto ERR;
280     }
281     return res;
282 ERR:
283     if (task->versionInfo.versionStatus == INITIAL) {
284         DasSendErrMsgToSelf(out, res);
285     } else {
286         DasSendErrorToOut(out, res);
287     }
288     return res;
289 }
290 
291 #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
ShouldSkipIso(Task * task,const CJson * in)292 static bool ShouldSkipIso(Task *task, const CJson *in)
293 {
294     if (task->versionInfo.opCode != OP_BIND) {
295         return false;
296     }
297     int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
298     (void)GetIntFromJson(in, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
299     return protocolExpandVal != LITE_PROTOCOL_STANDARD_MODE && protocolExpandVal != LITE_PROTOCOL_COMPATIBILITY_MODE;
300 }
301 #endif
302 
CreateMultiSubTask(Task * task,const CJson * in)303 static int CreateMultiSubTask(Task *task, const CJson *in)
304 {
305     InitVersionInfo(&(task->versionInfo));
306     uint32_t index;
307     void **ptr = NULL;
308     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
309         DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
310     #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
311         if (temp->type == ISO && ShouldSkipIso(task, in)) {
312             LOGI("Skip iso protocol!");
313             continue;
314         }
315     #endif
316         SubTaskBase *subTask = temp->createSubTask(in);
317         if (subTask == NULL) {
318             LOGE("Create subTask failed, protocolType: %" LOG_PUB "d.", temp->type);
319             return HC_ERR_ALLOC_MEMORY;
320         }
321         subTask->curVersion = task->versionInfo.curVersion;
322         if (task->vec.pushBackT(&(task->vec), (void *)subTask) == NULL) {
323             LOGE("Failed to push subTask!");
324             subTask->destroyTask(subTask);
325             return HC_ERR_ALLOC_MEMORY;
326         }
327     }
328     return HC_SUCCESS;
329 }
330 
CreateSingleSubTask(Task * task,const CJson * in)331 static int CreateSingleSubTask(Task *task, const CJson *in)
332 {
333     VersionStruct curVersionPeer = { 0, 0, 0 };
334     VersionStruct minVersionPeer = { 0, 0, 0 };
335     int res = GetVersionFromJson(in, &minVersionPeer, &curVersionPeer);
336     if (res != HC_SUCCESS) {
337         LOGE("Get peer version info failed, res: %" LOG_PUB "x.", res);
338         return res;
339     }
340     InitVersionInfo(&(task->versionInfo));
341     res = NegotiateVersion(&minVersionPeer, &curVersionPeer, &(task->versionInfo.curVersion));
342     if (res != HC_SUCCESS) {
343         LOGE("NegotiateVersion failed, res: %" LOG_PUB "x.", res);
344         return res;
345     }
346     task->versionInfo.versionStatus = VERSION_DECIDED;
347 
348     ProtocolType protocolType = GetPrototolType(&(task->versionInfo.curVersion), task->versionInfo.opCode);
349     LOGI("Server select protocolType: %" LOG_PUB "d", protocolType);
350 
351     uint32_t index;
352     void **ptr = NULL;
353     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
354         if (((DasProtocolEntity *)(*ptr))->type == protocolType) {
355         #ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
356             if (protocolType == ISO && ShouldSkipIso(task, in)) {
357                 LOGE("Skip iso protocol!");
358                 return HC_ERR_NOT_SUPPORT;
359             }
360         #endif
361             DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
362             SubTaskBase *subTask = temp->createSubTask(in);
363             if (subTask == NULL) {
364                 LOGE("Create subTask failed.");
365                 return HC_ERR_ALLOC_MEMORY;
366             }
367             subTask->curVersion = task->versionInfo.curVersion;
368             if (task->vec.pushBackT(&(task->vec), (void *)subTask) == NULL) {
369                 LOGE("Failed to push subTask!");
370                 subTask->destroyTask(subTask);
371                 return HC_ERR_ALLOC_MEMORY;
372             }
373             return HC_SUCCESS;
374         }
375     }
376 
377     LOGE("Can't find protocolType.");
378     return HC_ERR_NOT_SUPPORT;
379 }
380 
CreateTaskT(int32_t * taskId,const CJson * in,CJson * out)381 Task *CreateTaskT(int32_t *taskId, const CJson *in, CJson *out)
382 {
383     int res;
384     Task *task = NULL;
385     bool isClient = true;
386     if (GetBoolFromJson(in, FIELD_IS_CLIENT, &isClient) != HC_SUCCESS) {
387         LOGE("Get isClient failed.");
388         res = HC_ERR_JSON_GET;
389         goto ERR;
390     }
391     task = (Task *)HcMalloc(sizeof(Task), 0);
392     if (task == NULL) {
393         LOGE("Malloc for das task failed.");
394         res = HC_ERR_ALLOC_MEMORY;
395         goto ERR;
396     }
397     task->vec = CREATE_HC_VECTOR(SubTaskVec);
398     task->destroyTask = DestroyTaskT;
399     task->processTask = ProcessTaskT;
400 
401     Uint8Buff taskIdBuf = { (uint8_t *)taskId, sizeof(int) };
402     res = GetLoaderInstance()->generateRandom(&taskIdBuf);
403     if (res != 0) {
404         LOGE("Generate taskId failed.");
405         goto ERR;
406     }
407     task->taskId = *taskId;
408     if (GetIntFromJson(in, FIELD_OPERATION_CODE, &(task->versionInfo.opCode)) != HC_SUCCESS) {
409         LOGE("Get opcode failed.");
410         res = HC_ERR_JSON_GET;
411         goto ERR;
412     }
413     if (isClient) {
414         res = CreateMultiSubTask(task, in);
415     } else {
416         res = CreateSingleSubTask(task, in);
417     }
418     if (res != HC_SUCCESS) {
419         LOGE("Create sub task failed, res: %" LOG_PUB "x.", res);
420         goto ERR;
421     }
422     return task;
423 ERR:
424     if (isClient) {
425         DasSendErrMsgToSelf(out, res);
426     } else {
427         DasSendErrorToOut(out, res);
428     }
429     DestroyTaskT(task);
430     return NULL;
431 }
432 
RegisterLocalIdentityInTask(const TokenManagerParams * params)433 int32_t RegisterLocalIdentityInTask(const TokenManagerParams *params)
434 {
435     int32_t res = HC_SUCCESS;
436     uint32_t index;
437     void **ptr = NULL;
438     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
439         DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
440         if ((temp->tokenManagerInstance == NULL) || (temp->tokenManagerInstance->registerLocalIdentity == NULL)) {
441             LOGD("Protocol type: %" LOG_PUB "d, unsupported method!", temp->type);
442             continue;
443         }
444         res = temp->tokenManagerInstance->registerLocalIdentity(params);
445         if (res != HC_SUCCESS) {
446             LOGE("Protocol type: %" LOG_PUB "d, registerLocalIdentity failed, res: %" LOG_PUB "d!", temp->type, res);
447             return HC_ERR_GENERATE_KEY_FAILED;
448         }
449     }
450     return res;
451 }
452 
UnregisterLocalIdentityInTask(const TokenManagerParams * params)453 int32_t UnregisterLocalIdentityInTask(const TokenManagerParams *params)
454 {
455     int32_t res = HC_SUCCESS;
456     uint32_t index;
457     void **ptr = NULL;
458     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
459         DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
460         if ((temp->tokenManagerInstance == NULL) || (temp->tokenManagerInstance->unregisterLocalIdentity == NULL)) {
461             LOGD("Protocol type: %" LOG_PUB "d, unsupported method!", temp->type);
462             continue;
463         }
464         res = temp->tokenManagerInstance->unregisterLocalIdentity(params);
465         if (res != HC_SUCCESS) {
466             LOGE("Protocol type: %" LOG_PUB "d, unregisterLocalIdentity failed, res: %" LOG_PUB "d!", temp->type, res);
467             return res;
468         }
469     }
470     return res;
471 }
472 
DeletePeerAuthInfoInTask(const TokenManagerParams * params)473 int32_t DeletePeerAuthInfoInTask(const TokenManagerParams *params)
474 {
475     int32_t res = HC_SUCCESS;
476     uint32_t index;
477     void **ptr = NULL;
478     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
479         DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
480         if ((temp->tokenManagerInstance == NULL) || (temp->tokenManagerInstance->deletePeerAuthInfo == NULL)) {
481             LOGD("Protocol type: %" LOG_PUB "d, unsupported method!", temp->type);
482             continue;
483         }
484         res = temp->tokenManagerInstance->deletePeerAuthInfo(params);
485         if (res != HC_SUCCESS) {
486             LOGE("Protocol type: %" LOG_PUB "d, deletePeerAuthInfo failed, res: %" LOG_PUB "d!", temp->type, res);
487             return res;
488         }
489     }
490     return res;
491 }
492 
GetPublicKeyInTask(const TokenManagerParams * params,Uint8Buff * returnPk)493 int32_t GetPublicKeyInTask(const TokenManagerParams *params, Uint8Buff *returnPk)
494 {
495     uint32_t index;
496     void **ptr = NULL;
497     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
498         DasProtocolEntity *temp = (DasProtocolEntity *)(*ptr);
499         if ((temp->tokenManagerInstance == NULL) || (temp->tokenManagerInstance->getPublicKey == NULL)) {
500             LOGD("Protocol type: %" LOG_PUB "d, unsupported method!", temp->type);
501             continue;
502         }
503         return temp->tokenManagerInstance->getPublicKey(params, returnPk);
504     }
505     LOGE("Failed to find valid protocol!");
506     return HC_ERR_NOT_SUPPORT;
507 }
508 
GetPakeAlgInProtocol(int offset)509 static uint32_t GetPakeAlgInProtocol(int offset)
510 {
511     uint32_t algInProtocol = PSK_SPEKE;
512 #ifdef P2P_PAKE_DL_TYPE
513     algInProtocol |= (GetPakeDlAlg() << offset);
514 #endif
515 #ifdef P2P_PAKE_EC_TYPE
516     algInProtocol |= (GetPakeEcAlg() << offset);
517 #endif
518     (void)offset;
519     return algInProtocol;
520 }
521 
PushISOEntity(void)522 static int32_t PushISOEntity(void)
523 {
524     DasProtocolEntity *protocol = (DasProtocolEntity *)HcMalloc(sizeof(DasProtocolEntity), 0);
525     if (protocol == NULL) {
526         LOGE("Malloc for Iso dasProtocolEntity failed.");
527         return HC_ERR_ALLOC_MEMORY;
528     }
529     protocol->type = ISO;
530     protocol->algInProtocol = ISO_ALG;
531     protocol->createSubTask = CreateIsoSubTask;
532     protocol->tokenManagerInstance = GetLiteTokenManagerInstance();
533     if (g_protocolEntityVec.pushBackT(&g_protocolEntityVec, (void *)protocol) == NULL) {
534         LOGE("Failed to push protocol!");
535         HcFree(protocol);
536         return HC_ERR_ALLOC_MEMORY;
537     }
538     return HC_SUCCESS;
539 }
540 
PushPakeV1Entity(void)541 static int32_t PushPakeV1Entity(void)
542 {
543     DasProtocolEntity *protocol = (DasProtocolEntity *)HcMalloc(sizeof(DasProtocolEntity), 0);
544     if (protocol == NULL) {
545         LOGE("Malloc for pake v1 dasProtocolEntity failed.");
546         return HC_ERR_ALLOC_MEMORY;
547     }
548     protocol->type = PAKE_V1;
549     protocol->algInProtocol = GetPakeAlgInProtocol(ALG_OFFSET_FOR_PAKE_V1);
550     protocol->createSubTask = CreatePakeV1SubTask;
551     protocol->tokenManagerInstance = GetStandardTokenManagerInstance();
552     if (g_protocolEntityVec.pushBackT(&g_protocolEntityVec, (void *)protocol) == NULL) {
553         LOGE("Failed to push protocol!");
554         HcFree(protocol);
555         return HC_ERR_ALLOC_MEMORY;
556     }
557     return HC_SUCCESS;
558 }
559 
PushPakeV2Entity(void)560 static int32_t PushPakeV2Entity(void)
561 {
562     DasProtocolEntity *protocol = (DasProtocolEntity *)HcMalloc(sizeof(DasProtocolEntity), 0);
563     if (protocol == NULL) {
564         LOGE("Malloc for pake v2 dasProtocolEntity failed.");
565         return HC_ERR_ALLOC_MEMORY;
566     }
567     protocol->type = PAKE_V2;
568     protocol->algInProtocol = GetPakeAlgInProtocol(ALG_OFFSET_FOR_PAKE_V2);
569     protocol->createSubTask = CreatePakeV2SubTask;
570     protocol->tokenManagerInstance = GetStandardTokenManagerInstance();
571     if (g_protocolEntityVec.pushBackT(&g_protocolEntityVec, (void *)protocol) == NULL) {
572         LOGE("Failed to push protocol!");
573         HcFree(protocol);
574         return HC_ERR_ALLOC_MEMORY;
575     }
576     return HC_SUCCESS;
577 }
578 
InitDasProtocolEntities(void)579 int32_t InitDasProtocolEntities(void)
580 {
581     g_protocolEntityVec = CREATE_HC_VECTOR(DasProtocolEntityVec);
582     int32_t res = HC_ERROR;
583     if (IsIsoSupported()) {
584         res = PushISOEntity();
585         if (res != HC_SUCCESS) {
586             LOGE("Failed to push ISO protocol!");
587             DESTROY_HC_VECTOR(DasProtocolEntityVec, &g_protocolEntityVec);
588             return res;
589         }
590     }
591 
592     if (IsSupportPakeV1()) {
593         res = PushPakeV1Entity();
594         if (res != HC_SUCCESS) {
595             LOGE("Failed to push PAKEV1 protocol!");
596             DESTROY_HC_VECTOR(DasProtocolEntityVec, &g_protocolEntityVec);
597             return res;
598         }
599     }
600 
601     if (IsSupportPakeV2()) {
602         res = PushPakeV2Entity();
603         if (res != HC_SUCCESS) {
604             LOGE("Failed to push PAKEV2 protocol!");
605             DESTROY_HC_VECTOR(DasProtocolEntityVec, &g_protocolEntityVec);
606             return res;
607         }
608     }
609     return HC_SUCCESS;
610 }
611 
DestroyDasProtocolEntities(void)612 void DestroyDasProtocolEntities(void)
613 {
614     uint32_t index;
615     void **ptr = NULL;
616     FOR_EACH_HC_VECTOR(g_protocolEntityVec, index, ptr) {
617         HcFree((DasProtocolEntity *)(*ptr));
618         *ptr = NULL;
619     }
620     DESTROY_HC_VECTOR(DasProtocolEntityVec, &g_protocolEntityVec);
621 }