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