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 "pake_v1_client_task.h"
17 #include "device_auth_defines.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "pake_v1_client_protocol_task.h"
21 #include "pake_v1_protocol_task_common.h"
22 #include "pake_task_common.h"
23 #include "standard_client_bind_exchange_task.h"
24 #include "standard_client_unbind_exchange_task.h"
25
GetPakeV1ClientTaskType(const struct SubTaskBaseT * task)26 static int GetPakeV1ClientTaskType(const struct SubTaskBaseT *task)
27 {
28 PakeV1ClientTask *realTask = (PakeV1ClientTask *)task;
29 if (realTask->curTask == NULL) {
30 LOGE("CurTask is null.");
31 return TASK_TYPE_NONE;
32 }
33 return realTask->curTask->getCurTaskType();
34 }
35
DestroyPakeV1ClientTask(struct SubTaskBaseT * task)36 static void DestroyPakeV1ClientTask(struct SubTaskBaseT *task)
37 {
38 PakeV1ClientTask *innerTask = (PakeV1ClientTask *)task;
39 if (innerTask == NULL) {
40 return;
41 }
42
43 DestroyDasPakeV1Params(&(innerTask->params));
44 if (innerTask->curTask != NULL) {
45 innerTask->curTask->destroyTask(innerTask->curTask);
46 }
47 HcFree(innerTask);
48 }
49
CreateAndProcessNextBindTask(PakeV1ClientTask * realTask,const CJson * in,CJson * out,int * status)50 static int CreateAndProcessNextBindTask(PakeV1ClientTask *realTask, const CJson *in, CJson *out, int *status)
51 {
52 realTask->curTask->destroyTask(realTask->curTask);
53 realTask->curTask = CreateStandardBindExchangeClientTask();
54 if (realTask->curTask == NULL) {
55 LOGE("CreateStandardBindExchangeClientTask failed.");
56 return HC_ERROR;
57 }
58 int res = realTask->curTask->process(realTask->curTask, &(realTask->params), in, out, status);
59 if (res != HC_SUCCESS) {
60 LOGE("Process StandardBindExchangeClientTask failed.");
61 }
62 return res;
63 }
64
CreateAndProcessNextUnbindTask(PakeV1ClientTask * realTask,const CJson * in,CJson * out,int * status)65 static int CreateAndProcessNextUnbindTask(PakeV1ClientTask *realTask, const CJson *in, CJson *out, int *status)
66 {
67 realTask->curTask->destroyTask(realTask->curTask);
68 realTask->curTask = CreateStandardUnbindExchangeClientTask();
69 if (realTask->curTask == NULL) {
70 LOGE("CreateStandardUnbindExchangeClientTask failed.");
71 return HC_ERROR;
72 }
73 int res = realTask->curTask->process(realTask->curTask, &(realTask->params), in, out, status);
74 if (res != HC_SUCCESS) {
75 LOGE("Process StandardUnbindExchangeClientTask failed.");
76 }
77 return res;
78 }
79
CreateNextTask(PakeV1ClientTask * realTask,const CJson * in,CJson * out,int * status)80 static int CreateNextTask(PakeV1ClientTask *realTask, const CJson *in, CJson *out, int *status)
81 {
82 int res = HC_SUCCESS;
83 switch (realTask->params.opCode) {
84 case OP_BIND:
85 if (realTask->curTask->getCurTaskType() == TASK_TYPE_BIND_STANDARD_EXCHANGE) {
86 break;
87 }
88 *status = CONTINUE;
89 res = CreateAndProcessNextBindTask(realTask, in, out, status);
90 break;
91 case OP_UNBIND:
92 if (realTask->curTask->getCurTaskType() == TASK_TYPE_UNBIND_STANDARD_EXCHANGE) {
93 break;
94 }
95 *status = CONTINUE;
96 res = CreateAndProcessNextUnbindTask(realTask, in, out, status);
97 break;
98 case AUTH_KEY_AGREEMENT:
99 case AUTHENTICATE:
100 break;
101 default:
102 LOGE("Unsupported opCode: %d.", realTask->params.opCode);
103 res = HC_ERR_NOT_SUPPORT;
104 }
105 if (res != HC_SUCCESS) {
106 LOGE("Create and process next task failed, opcode: %d, res: %d.", realTask->params.opCode, res);
107 return res;
108 }
109 if (*status != FINISH) {
110 return res;
111 }
112 res = SendResultToSelf(&realTask->params, out);
113 if (res != HC_SUCCESS) {
114 LOGE("SendResultToSelf failed, res: %d", res);
115 return res;
116 }
117 LOGI("End client task successfully, opcode: %d.", realTask->params.opCode);
118 return res;
119 }
120
Process(struct SubTaskBaseT * task,const CJson * in,CJson * out,int * status)121 static int Process(struct SubTaskBaseT *task, const CJson *in, CJson *out, int *status)
122 {
123 PakeV1ClientTask *realTask = (PakeV1ClientTask *)task;
124 if (realTask->curTask == NULL) {
125 LOGE("CurTask is null.");
126 return HC_ERR_NULL_PTR;
127 }
128
129 realTask->params.baseParams.supportedPakeAlg = GetSupportedPakeAlg(&(realTask->taskBase.curVersion), PAKE_V1);
130 realTask->params.isPskSupported = IsSupportedPsk(&(realTask->taskBase.curVersion));
131 int res = realTask->curTask->process(realTask->curTask, &(realTask->params), in, out, status);
132 if (res != HC_SUCCESS) {
133 LOGE("CurTask processes failed, res: %x.", res);
134 return res;
135 }
136 if (*status != FINISH) {
137 return res;
138 }
139 return CreateNextTask(realTask, in, out, status);
140 }
141
CreatePakeV1ClientTask(const CJson * in)142 SubTaskBase *CreatePakeV1ClientTask(const CJson *in)
143 {
144 PakeV1ClientTask *task = (PakeV1ClientTask *)HcMalloc(sizeof(PakeV1ClientTask), 0);
145 if (task == NULL) {
146 LOGE("Malloc for PakeV1ClientTask failed.");
147 return NULL;
148 }
149
150 task->taskBase.getTaskType = GetPakeV1ClientTaskType;
151 task->taskBase.destroyTask = DestroyPakeV1ClientTask;
152 task->taskBase.process = Process;
153
154 int res = InitDasPakeV1Params(&(task->params), in);
155 if (res != HC_SUCCESS) {
156 LOGE("Init das pake params failed, res: %d.", res);
157 DestroyPakeV1ClientTask((struct SubTaskBaseT *)task);
158 return NULL;
159 }
160 task->curTask = CreatePakeV1ProtocolClientTask();
161 if (task->curTask == NULL) {
162 LOGE("Create pake protocol client task failed.");
163 DestroyPakeV1ClientTask((struct SubTaskBaseT *)task);
164 return NULL;
165 }
166 return (SubTaskBase *)task;
167 }
168