• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pake_v1_server_task.h"
17 #include "device_auth_defines.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "pake_v1_protocol_task_common.h"
21 #include "pake_v1_server_protocol_task.h"
22 #include "pake_task_common.h"
23 #include "standard_server_bind_exchange_task.h"
24 
GetPakeV1ServerTaskType(const struct SubTaskBaseT * task)25 static int GetPakeV1ServerTaskType(const struct SubTaskBaseT *task)
26 {
27     PakeV1ServerTask *realTask = (PakeV1ServerTask *)task;
28     if (realTask->curTask == NULL) {
29         LOGE("CurTask is null.");
30         return TASK_TYPE_NONE;
31     }
32     return realTask->curTask->getCurTaskType();
33 }
34 
DestroyPakeV1ServerTask(struct SubTaskBaseT * task)35 static void DestroyPakeV1ServerTask(struct SubTaskBaseT *task)
36 {
37     PakeV1ServerTask *innerTask = (PakeV1ServerTask *)task;
38     if (innerTask == NULL) {
39         return;
40     }
41 
42     DestroyDasPakeV1Params(&(innerTask->params));
43     if (innerTask->curTask != NULL) {
44         innerTask->curTask->destroyTask(innerTask->curTask);
45     }
46     HcFree(innerTask);
47 }
48 
CreateAndProcessNextBindTask(PakeV1ServerTask * realTask,const CJson * in,CJson * out,int * status)49 static int CreateAndProcessNextBindTask(PakeV1ServerTask *realTask, const CJson *in, CJson *out, int *status)
50 {
51     realTask->curTask->destroyTask(realTask->curTask);
52     realTask->curTask = CreateStandardBindExchangeServerTask();
53     if (realTask->curTask == NULL) {
54         LOGE("CreateStandardBindExchangeServerTask failed.");
55         return HC_ERROR;
56     }
57     int res = realTask->curTask->process(realTask->curTask, &(realTask->params), in, out, status);
58     if (res != HC_SUCCESS) {
59         LOGE("Process StandardBindExchangeServerTask failed.");
60     }
61     return res;
62 }
63 
CreateNextTask(PakeV1ServerTask * realTask,const CJson * in,CJson * out,int * status)64 static int CreateNextTask(PakeV1ServerTask *realTask, const CJson *in, CJson *out, int *status)
65 {
66     int res = HC_SUCCESS;
67     switch (realTask->params.opCode) {
68         case OP_BIND:
69             if (realTask->curTask->getCurTaskType() == TASK_TYPE_BIND_STANDARD_EXCHANGE) {
70                 break;
71             }
72             *status = CONTINUE;
73             res = CreateAndProcessNextBindTask(realTask, in, out, status);
74             break;
75         case AUTH_KEY_AGREEMENT:
76         case AUTHENTICATE:
77             break;
78         default:
79             LOGE("Unsupported opCode: %" LOG_PUB "d.", realTask->params.opCode);
80             res = HC_ERR_NOT_SUPPORT;
81     }
82     if (res != HC_SUCCESS) {
83         LOGE("Create and process next task failed, res: %" LOG_PUB "d, opcode: %" LOG_PUB "d.",
84             res, realTask->params.opCode);
85         return res;
86     }
87     if (*status != FINISH) {
88         return res;
89     }
90     res = SendResultToSelf(&realTask->params, out);
91     if (res != HC_SUCCESS) {
92         LOGE("send result to out failed, res: %" LOG_PUB "d.", res);
93         return res;
94     }
95     LOGI("End server task successfully, opcode: %" LOG_PUB "d.", realTask->params.opCode);
96     return res;
97 }
98 
Process(struct SubTaskBaseT * task,const CJson * in,CJson * out,int * status)99 static int Process(struct SubTaskBaseT *task, const CJson *in, CJson *out, int *status)
100 {
101     PakeV1ServerTask *realTask = (PakeV1ServerTask *)task;
102     if (realTask->curTask == NULL) {
103         LOGE("CurTask is null.");
104         return HC_ERR_NULL_PTR;
105     }
106 
107     realTask->params.baseParams.supportedPakeAlg = GetSupportedPakeAlg(&(realTask->taskBase.curVersion), PAKE_V1);
108     realTask->params.isPskSupported = IsSupportedPsk(&(realTask->taskBase.curVersion));
109     int res = realTask->curTask->process(realTask->curTask, &(realTask->params), in, out, status);
110     if (res != HC_SUCCESS) {
111         LOGE("CurTask processes failed, res: %" LOG_PUB "x.", res);
112         return res;
113     }
114     if (*status != FINISH) {
115         return res;
116     }
117     return CreateNextTask(realTask, in, out, status);
118 }
119 
CreatePakeV1ServerTask(const CJson * in)120 SubTaskBase *CreatePakeV1ServerTask(const CJson *in)
121 {
122     PakeV1ServerTask *task = (PakeV1ServerTask *)HcMalloc(sizeof(PakeV1ServerTask), 0);
123     if (task == NULL) {
124         LOGE("Malloc for PakeV1ServerTask failed.");
125         return NULL;
126     }
127 
128     task->taskBase.getTaskType = GetPakeV1ServerTaskType;
129     task->taskBase.destroyTask = DestroyPakeV1ServerTask;
130     task->taskBase.process = Process;
131 
132     int res = InitDasPakeV1Params(&(task->params), in);
133     if (res != HC_SUCCESS) {
134         LOGE("Init das pake params failed, res: %" LOG_PUB "d.", res);
135         DestroyPakeV1ServerTask((struct SubTaskBaseT *)task);
136         return NULL;
137     }
138     task->curTask = CreatePakeV1ProtocolServerTask();
139     if (task->curTask == NULL) {
140         LOGE("Create pake protocol server task failed.");
141         DestroyPakeV1ServerTask((struct SubTaskBaseT *)task);
142         return NULL;
143     }
144     return (SubTaskBase *)task;
145 }
146