• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "mk_agree_task.h"
17 
18 #include "alg_loader.h"
19 #include "common_defs.h"
20 #include "device_auth_defines.h"
21 #include "hc_dev_info.h"
22 #include "hc_log.h"
23 #include "hc_types.h"
24 #include "key_manager.h"
25 #include "uint8buff_utils.h"
26 
27 #define PAKE_X25519_KEY_PAIR_LEN 32
28 
GeneratePakeRequestPayload(PakeMkAgreeTask * pakeTask,CJson * payload)29 static int32_t GeneratePakeRequestPayload(PakeMkAgreeTask *pakeTask, CJson *payload)
30 {
31     Uint8Buff saltBuff = { pakeTask->clientSalt, SALT_LEN };
32     int32_t res = pakeTask->taskBase.loader->generateRandom(&saltBuff);
33     if (res != HC_SUCCESS) {
34         LOGE("Failed to generate client salt!");
35         return res;
36     }
37     Uint8Buff devicePk = { NULL, 0 };
38     if (InitUint8Buff(&devicePk, PAKE_X25519_KEY_PAIR_LEN) != HC_SUCCESS) {
39         LOGE("Failed to init devicePk!");
40         return HC_ERR_ALLOC_MEMORY;
41     }
42     res = GetDevicePubKey(&devicePk);
43     if (res != HC_SUCCESS) {
44         LOGE("Failed to get device pk!");
45         FreeUint8Buff(&devicePk);
46         return res;
47     }
48     if (AddByteToJson(payload, FIELD_SALT, pakeTask->clientSalt, SALT_LEN) != HC_SUCCESS) {
49         LOGE("Failed to add salt to payload!");
50         ClearFreeUint8Buff(&devicePk);
51         return HC_ERR_JSON_ADD;
52     }
53     if (AddByteToJson(payload, FIELD_PUBLIC_KEY, devicePk.val, devicePk.length) != HC_SUCCESS) {
54         LOGE("Failed to add devicePk to payload!");
55         ClearFreeUint8Buff(&devicePk);
56         return HC_ERR_JSON_ADD;
57     }
58     ClearFreeUint8Buff(&devicePk);
59     return HC_SUCCESS;
60 }
61 
GeneratePakeRequestPayloadData(PakeMkAgreeTask * pakeTask,Uint8Buff * payloadData)62 static int32_t GeneratePakeRequestPayloadData(PakeMkAgreeTask *pakeTask, Uint8Buff *payloadData)
63 {
64     CJson *payload = CreateJson();
65     if (payload == NULL) {
66         LOGE("Failed to create payload!");
67         return HC_ERR_JSON_CREATE;
68     }
69     int32_t res = GeneratePakeRequestPayload(pakeTask, payload);
70     if (res != HC_SUCCESS) {
71         LOGE("Failed to generate pake request payload!");
72         FreeJson(payload);
73         return res;
74     }
75     char *payloadStr = PackJsonToString(payload);
76     FreeJson(payload);
77     if (payloadStr == NULL) {
78         LOGE("Failed to pack payload json to string!");
79         return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
80     }
81     uint32_t payloadLen = HcStrlen(payloadStr);
82     if (InitUint8Buff(payloadData, payloadLen + 1) != HC_SUCCESS) {
83         LOGE("Failed to init payload data!");
84         FreeJsonString(payloadStr);
85         return HC_ERR_ALLOC_MEMORY;
86     }
87     if (memcpy_s(payloadData->val, payloadData->length, payloadStr, payloadLen) != EOK) {
88         LOGE("Failed to copy payload!");
89         FreeJsonString(payloadStr);
90         FreeUint8Buff(payloadData);
91         return HC_ERR_MEMORY_COPY;
92     }
93     FreeJsonString(payloadStr);
94     return HC_SUCCESS;
95 }
96 
GeneratePakeResponsePayload(PakeMkAgreeTask * pakeTask,const Uint8Buff * kcfData,CJson * payload)97 static int32_t GeneratePakeResponsePayload(PakeMkAgreeTask *pakeTask, const Uint8Buff *kcfData, CJson *payload)
98 {
99     Uint8Buff devicePk = { NULL, 0 };
100     if (InitUint8Buff(&devicePk, PAKE_X25519_KEY_PAIR_LEN) != HC_SUCCESS) {
101         LOGE("Failed to init devicePk!");
102         return HC_ERR_ALLOC_MEMORY;
103     }
104     int32_t res = GetDevicePubKey(&devicePk);
105     if (res != HC_SUCCESS) {
106         LOGE("Failed to get device public key!");
107         FreeUint8Buff(&devicePk);
108         return res;
109     }
110     if (AddByteToJson(payload, FIELD_SALT, pakeTask->serverSalt, SALT_LEN) != HC_SUCCESS) {
111         LOGE("Failed to add server salt!");
112         ClearFreeUint8Buff(&devicePk);
113         return HC_ERR_JSON_ADD;
114     }
115     if (AddByteToJson(payload, FIELD_PUBLIC_KEY, devicePk.val, devicePk.length) != HC_SUCCESS) {
116         LOGE("Failed to add device public key!");
117         ClearFreeUint8Buff(&devicePk);
118         return HC_ERR_JSON_ADD;
119     }
120     ClearFreeUint8Buff(&devicePk);
121     if (AddByteToJson(payload, FIELD_KCF_DATA, kcfData->val, kcfData->length) != HC_SUCCESS) {
122         LOGE("Failed to add kcf data!");
123         return HC_ERR_JSON_ADD;
124     }
125     return HC_SUCCESS;
126 }
127 
CombineSalt(const Uint8Buff * firstSalt,const Uint8Buff * secondSalt,Uint8Buff * returnSalt)128 static int32_t CombineSalt(const Uint8Buff *firstSalt, const Uint8Buff *secondSalt, Uint8Buff *returnSalt)
129 {
130     int32_t res = InitUint8Buff(returnSalt, firstSalt->length + secondSalt->length);
131     if (res != HC_SUCCESS) {
132         LOGE("Failed to init return salt!");
133         return res;
134     }
135     if (memcpy_s(returnSalt->val, returnSalt->length, firstSalt->val, firstSalt->length) != EOK) {
136         LOGE("Failed to memcpy the first salt!");
137         FreeUint8Buff(returnSalt);
138         return HC_ERR_MEMORY_COPY;
139     }
140     if (memcpy_s(returnSalt->val + firstSalt->length, returnSalt->length - firstSalt->length,
141         secondSalt->val, secondSalt->length) != EOK) {
142         LOGE("Failed to memcpy server salt!");
143         FreeUint8Buff(returnSalt);
144         return HC_ERR_MEMORY_COPY;
145     }
146     return HC_SUCCESS;
147 }
148 
ComputeKcfData(PakeMkAgreeTask * pakeTask,const Uint8Buff * firstSalt,const Uint8Buff * secondSalt,Uint8Buff * returnKcfData)149 static int32_t ComputeKcfData(PakeMkAgreeTask *pakeTask, const Uint8Buff *firstSalt, const Uint8Buff *secondSalt,
150     Uint8Buff *returnKcfData)
151 {
152     Uint8Buff saltBuff = { NULL, 0 };
153     int32_t res = CombineSalt(firstSalt, secondSalt, &saltBuff);
154     if (res != HC_SUCCESS) {
155         LOGE("Failed to combine salt!");
156         return res;
157     }
158     Uint8Buff tmpKcfData = { NULL, 0 };
159     PseudonymKeyInfo info = { pakeTask->taskBase.peerInfo, pakeTask->taskBase.pdidIndex };
160     res = GenerateAndSavePseudonymId(pakeTask->taskBase.osAccountId, pakeTask->taskBase.peerUdid,
161         &info, &saltBuff, &tmpKcfData);
162     FreeUint8Buff(&saltBuff);
163     if (res != HC_SUCCESS) {
164         LOGE("Failed to generate and save pdid!");
165         return res;
166     }
167     returnKcfData->val = tmpKcfData.val;
168     returnKcfData->length = tmpKcfData.length;
169     return HC_SUCCESS;
170 }
171 
GeneratePakeResponsePayloadData(PakeMkAgreeTask * pakeTask,Uint8Buff * payloadData)172 static int32_t GeneratePakeResponsePayloadData(PakeMkAgreeTask *pakeTask, Uint8Buff *payloadData)
173 {
174     Uint8Buff kcfData = { NULL, 0 };
175     if (InitUint8Buff(&kcfData, HMAC_LEN) != HC_SUCCESS) {
176         LOGE("Failed to init kcf data!");
177         return HC_ERR_ALLOC_MEMORY;
178     }
179     Uint8Buff clientSaltBuff = { pakeTask->clientSalt, SALT_LEN };
180     Uint8Buff serverSaltBuff = { pakeTask->serverSalt, SALT_LEN };
181     int32_t res = ComputeKcfData(pakeTask, &serverSaltBuff, &clientSaltBuff, &kcfData);
182     if (res != HC_SUCCESS) {
183         LOGE("Failed to compute kcf data!");
184         FreeUint8Buff(&kcfData);
185         return res;
186     }
187     CJson *payload = CreateJson();
188     if (payload == NULL) {
189         LOGE("Failed to create payload!");
190         ClearFreeUint8Buff(&kcfData);
191         return HC_ERR_JSON_CREATE;
192     }
193     res = GeneratePakeResponsePayload(pakeTask, &kcfData, payload);
194     ClearFreeUint8Buff(&kcfData);
195     if (res != HC_SUCCESS) {
196         LOGE("Failed to generate pake response payload!");
197         FreeJson(payload);
198         return res;
199     }
200     char *payloadStr = PackJsonToString(payload);
201     FreeJson(payload);
202     if (payloadStr == NULL) {
203         LOGE("Failed to pack payload json to string!");
204         return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
205     }
206     uint32_t payloadLen = HcStrlen(payloadStr);
207     if (InitUint8Buff(payloadData, payloadLen + 1) != HC_SUCCESS) {
208         LOGE("Failed to init payload data!");
209         FreeJsonString(payloadStr);
210         return HC_ERR_ALLOC_MEMORY;
211     }
212     if (memcpy_s(payloadData->val, payloadData->length, payloadStr, payloadLen) != EOK) {
213         LOGE("Failed to copy payload!");
214         FreeJsonString(payloadStr);
215         FreeUint8Buff(payloadData);
216         return HC_ERR_MEMORY_COPY;
217     }
218     FreeJsonString(payloadStr);
219     return HC_SUCCESS;
220 }
221 
AddSelfUdidToSendData(CJson * sendData)222 static int32_t AddSelfUdidToSendData(CJson *sendData)
223 {
224     char udid[INPUT_UDID_LEN] = { 0 };
225     int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
226     if (res != HC_SUCCESS) {
227         LOGE("Failed to get local udid! res: %d", res);
228         return res;
229     }
230     return AddStringToJson(sendData, FIELD_PEER_UDID, udid);
231 }
232 
GeneratePakeRequestData(PakeMkAgreeTask * pakeTask,CJson * out)233 static int32_t GeneratePakeRequestData(PakeMkAgreeTask *pakeTask, CJson *out)
234 {
235     Uint8Buff payloadData = { NULL, 0 };
236     int32_t res = GeneratePakeRequestPayloadData(pakeTask, &payloadData);
237     if (res != HC_SUCCESS) {
238         LOGE("Failed to generate pake request payload data!");
239         return res;
240     }
241     CJson *sendData = CreateJson();
242     if (sendData == NULL) {
243         LOGE("Failed to create send data json!");
244         ClearFreeUint8Buff(&payloadData);
245         return HC_ERR_JSON_CREATE;
246     }
247     if (AddByteToJson(sendData, FIELD_PAYLOAD, payloadData.val, payloadData.length) != HC_SUCCESS) {
248         LOGE("Failed to add payload data to json!");
249         ClearFreeUint8Buff(&payloadData);
250         FreeJson(sendData);
251         return HC_ERR_JSON_ADD;
252     }
253     ClearFreeUint8Buff(&payloadData);
254     res = AddSelfUdidToSendData(sendData);
255     if (res != HC_SUCCESS) {
256         LOGE("Failed to add self udid to json!");
257         FreeJson(sendData);
258         return res;
259     }
260     if (AddIntToJson(sendData, MK_AGREE_MESSAGE_CODE, START_MK_AGREE_REQUEST) != HC_SUCCESS) {
261         LOGE("Failed to add message code to json!");
262         FreeJson(sendData);
263         return HC_ERR_JSON_ADD;
264     }
265     if (AddObjToJson(out, FIELD_SEND_TO_PEER, sendData) != HC_SUCCESS) {
266         LOGE("Failed to add send data!");
267         FreeJson(sendData);
268         return HC_ERR_JSON_ADD;
269     }
270     FreeJson(sendData);
271     return HC_SUCCESS;
272 }
273 
StartPakeMkAgreeRequest(MkAgreeTaskBase * task,CJson * out)274 static int32_t StartPakeMkAgreeRequest(MkAgreeTaskBase *task, CJson *out)
275 {
276     LOGI("start pake mk agree request!");
277     PakeMkAgreeTask *pakeTask = (PakeMkAgreeTask *)task;
278     int32_t res = GeneratePakeRequestData(pakeTask, out);
279     if (res != HC_SUCCESS) {
280         LOGE("Failed to generate pake request data!");
281         return res;
282     }
283     task->taskStatus = STATUS_WAIT_MK_RESPONSE;
284     LOGI("start pake mk agree request successfully!");
285     return HC_SUCCESS;
286 }
287 
GenerateMkByPeer(PakeMkAgreeTask * pakeTask,const CJson * payload)288 static int32_t GenerateMkByPeer(PakeMkAgreeTask *pakeTask, const CJson *payload)
289 {
290     const char *peerDevPkStr = GetStringFromJson(payload, FIELD_PUBLIC_KEY);
291     if (peerDevPkStr == NULL) {
292         LOGE("Failed to get peer device pk string!");
293         return HC_ERR_JSON_GET;
294     }
295     uint32_t peerDevPkLen = HcStrlen(peerDevPkStr) / BYTE_TO_HEX_OPER_LENGTH;
296     uint8_t *peerDevPkVal = (uint8_t *)HcMalloc(peerDevPkLen, 0);
297     if (peerDevPkVal == NULL) {
298         LOGE("Failed to alloc memory for peer device pk!");
299         return HC_ERR_ALLOC_MEMORY;
300     }
301     int32_t res = HexStringToByte(peerDevPkStr, peerDevPkVal, peerDevPkLen);
302     if (res != HC_SUCCESS) {
303         LOGE("Failed to convert peer device pk from string to byte!");
304         HcFree(peerDevPkVal);
305         return res;
306     }
307     Uint8Buff peerPkBuff = { peerDevPkVal, peerDevPkLen };
308     res = GenerateMk(pakeTask->taskBase.peerUdid, &peerPkBuff);
309     HcFree(peerDevPkVal);
310     if (res != HC_SUCCESS) {
311         LOGE("Failed to generate mk!");
312         return res;
313     }
314     Uint8Buff clientSaltBuff = { pakeTask->clientSalt, SALT_LEN };
315     Uint8Buff serverSaltBuff = { pakeTask->serverSalt, SALT_LEN };
316     Uint8Buff saltBuff = { NULL, 0 };
317     res = CombineSalt(&clientSaltBuff, &serverSaltBuff, &saltBuff);
318     if (res != HC_SUCCESS) {
319         LOGE("Failed to combine salt!");
320         return res;
321     }
322     res = GeneratePseudonymPsk(pakeTask->taskBase.peerUdid, &saltBuff);
323     FreeUint8Buff(&saltBuff);
324     if (res != HC_SUCCESS) {
325         LOGE("Failed to generate and save tmp mk!");
326     }
327     return res;
328 }
329 
GeneratePakeResponseData(PakeMkAgreeTask * pakeTask,CJson * out)330 static int32_t GeneratePakeResponseData(PakeMkAgreeTask *pakeTask, CJson *out)
331 {
332     Uint8Buff payloadData = { NULL, 0 };
333     int32_t res = GeneratePakeResponsePayloadData(pakeTask, &payloadData);
334     if (res != HC_SUCCESS) {
335         LOGE("Failed to generate pake response payload data!");
336         return res;
337     }
338     CJson *sendData = CreateJson();
339     if (sendData == NULL) {
340         LOGE("Failed to create send data json!");
341         ClearFreeUint8Buff(&payloadData);
342         return HC_ERR_JSON_CREATE;
343     }
344     if (AddByteToJson(sendData, FIELD_PAYLOAD, payloadData.val, payloadData.length) != HC_SUCCESS) {
345         LOGE("Failed to add payload data to json!");
346         ClearFreeUint8Buff(&payloadData);
347         FreeJson(sendData);
348         return HC_ERR_JSON_ADD;
349     }
350     ClearFreeUint8Buff(&payloadData);
351     res = AddSelfUdidToSendData(sendData);
352     if (res != HC_SUCCESS) {
353         LOGE("Failed to add self udid to json!");
354         FreeJson(sendData);
355         return res;
356     }
357     if (AddIntToJson(sendData, MK_AGREE_MESSAGE_CODE, SEND_MK_AGREE_RESPONSE) != HC_SUCCESS) {
358         LOGE("Failed to add message code to json!");
359         FreeJson(sendData);
360         return HC_ERR_JSON_ADD;
361     }
362     if (AddObjToJson(out, FIELD_SEND_TO_PEER, sendData) != HC_SUCCESS) {
363         LOGE("Failed to add send data!");
364         FreeJson(sendData);
365         return HC_ERR_JSON_ADD;
366     }
367     FreeJson(sendData);
368     return HC_SUCCESS;
369 }
370 
ParsePakeData(const CJson * in,Uint8Buff * payload)371 static int32_t ParsePakeData(const CJson *in, Uint8Buff *payload)
372 {
373     const char *payloadStr = GetStringFromJson(in, FIELD_PAYLOAD);
374     if (payloadStr == NULL) {
375         LOGE("Failed to get payloadStr!");
376         return HC_ERR_JSON_GET;
377     }
378     uint32_t payloadLen = HcStrlen(payloadStr) / BYTE_TO_HEX_OPER_LENGTH;
379     if (InitUint8Buff(payload, payloadLen) != HC_SUCCESS) {
380         LOGE("Failed to init payload!");
381         return HC_ERR_ALLOC_MEMORY;
382     }
383     int32_t res = HexStringToByte(payloadStr, payload->val, payload->length);
384     if (res != HC_SUCCESS) {
385         LOGE("Failed to convert payload data from hex string to byte!");
386         FreeUint8Buff(payload);
387     }
388     return res;
389 }
390 
ProcessPakeMkAgreeRequest(MkAgreeTaskBase * task,const CJson * in,CJson * out)391 static int32_t ProcessPakeMkAgreeRequest(MkAgreeTaskBase *task, const CJson *in, CJson *out)
392 {
393     LOGI("process pake mk agree request!");
394     PakeMkAgreeTask *pakeTask = (PakeMkAgreeTask *)task;
395     if (task->taskStatus != STATUS_WAIT_MK_REQUEST) {
396         LOGE("Task status not match!");
397         return HC_ERR_INVALID_PARAMS;
398     }
399     Uint8Buff payloadBuff = { NULL, 0 };
400     int32_t res = ParsePakeData(in, &payloadBuff);
401     if (res != HC_SUCCESS) {
402         LOGE("Failed to parse pake request data!");
403         return res;
404     }
405     CJson *payload = CreateJsonFromString((const char *)payloadBuff.val);
406     ClearFreeUint8Buff(&payloadBuff);
407     if (payload == NULL) {
408         LOGE("Failed to create payload json!");
409         return HC_ERR_JSON_CREATE;
410     }
411     if (GetByteFromJson(payload, FIELD_SALT, pakeTask->clientSalt, SALT_LEN) != HC_SUCCESS) {
412         LOGE("Failed to get client salt from payload!");
413         FreeJson(payload);
414         return HC_ERR_JSON_GET;
415     }
416     Uint8Buff serverSaltBuff = { pakeTask->serverSalt, SALT_LEN };
417     res = pakeTask->taskBase.loader->generateRandom(&serverSaltBuff);
418     if (res != HC_SUCCESS) {
419         FreeJson(payload);
420         LOGE("Failed to generate server salt!");
421         return res;
422     }
423     res = GenerateMkByPeer(pakeTask, payload);
424     FreeJson(payload);
425     if (res != HC_SUCCESS) {
426         LOGE("Failed to generate mk by peer!");
427         return res;
428     }
429     res = GeneratePakeResponseData(pakeTask, out);
430     if (res != HC_SUCCESS) {
431         LOGE("Failed to generate pake response data!");
432         return res;
433     }
434     task->taskStatus = STATUS_FINISH;
435     LOGI("process pake mk agree request successfully!");
436     return HC_SUCCESS;
437 }
438 
VerifyPeerKcf(PakeMkAgreeTask * pakeTask,const CJson * payload)439 static int32_t VerifyPeerKcf(PakeMkAgreeTask *pakeTask, const CJson *payload)
440 {
441     const char *kcfDataStr = GetStringFromJson(payload, FIELD_KCF_DATA);
442     if (kcfDataStr == NULL) {
443         LOGE("Failed to get kcf data!");
444         return HC_ERR_JSON_GET;
445     }
446     uint32_t kcfDataLen = HcStrlen(kcfDataStr) / BYTE_TO_HEX_OPER_LENGTH;
447     uint8_t *kcfDataVal = (uint8_t *)HcMalloc(kcfDataLen, 0);
448     if (kcfDataVal == NULL) {
449         LOGE("Failed to alloc memory for kcf data!");
450         return HC_ERR_ALLOC_MEMORY;
451     }
452     int32_t res = HexStringToByte(kcfDataStr, kcfDataVal, kcfDataLen);
453     if (res != HC_SUCCESS) {
454         LOGE("Failed to convert kcf data from hex string to byte!");
455         HcFree(kcfDataVal);
456         return res;
457     }
458     Uint8Buff recoverKcfBuff = { NULL, 0 };
459     Uint8Buff clientSaltBuff = { pakeTask->clientSalt, SALT_LEN };
460     Uint8Buff serverSaltBuff = { pakeTask->serverSalt, SALT_LEN };
461     res = ComputeKcfData(pakeTask, &serverSaltBuff, &clientSaltBuff, &recoverKcfBuff);
462     if (res != HC_SUCCESS) {
463         LOGE("Failed to comput recover kcf data!");
464         HcFree(kcfDataVal);
465         return res;
466     }
467     if ((recoverKcfBuff.length != kcfDataLen) || memcmp(recoverKcfBuff.val, kcfDataVal, kcfDataLen) != 0) {
468         LOGE("Kcf data not equal!");
469         HcFree(kcfDataVal);
470         ClearFreeUint8Buff(&recoverKcfBuff);
471         return HC_ERR_MEMORY_COMPARE;
472     }
473     HcFree(kcfDataVal);
474     ClearFreeUint8Buff(&recoverKcfBuff);
475     return HC_SUCCESS;
476 }
477 
ProcessPakeMkAgreeResponse(MkAgreeTaskBase * task,const CJson * in)478 static int32_t ProcessPakeMkAgreeResponse(MkAgreeTaskBase *task, const CJson *in)
479 {
480     LOGI("process pake mk agree response!");
481     PakeMkAgreeTask *pakeTask = (PakeMkAgreeTask *)task;
482     if (task->taskStatus != STATUS_WAIT_MK_RESPONSE) {
483         LOGE("Task status not match!");
484         return HC_ERR_INVALID_PARAMS;
485     }
486     Uint8Buff payloadBuff = { NULL, 0 };
487     int32_t res = ParsePakeData(in, &payloadBuff);
488     if (res != HC_SUCCESS) {
489         LOGE("Failed to parse pake response data!");
490         return res;
491     }
492     CJson *payload = CreateJsonFromString((const char *)payloadBuff.val);
493     ClearFreeUint8Buff(&payloadBuff);
494     if (payload == NULL) {
495         LOGE("Failed to create payload json!");
496         return HC_ERR_JSON_CREATE;
497     }
498     if (GetByteFromJson(payload, FIELD_SALT, pakeTask->serverSalt, SALT_LEN) != HC_SUCCESS) {
499         LOGE("Failed to get server salt!");
500         FreeJson(payload);
501         return HC_ERR_JSON_GET;
502     }
503     res = GenerateMkByPeer(pakeTask, payload);
504     if (res != HC_SUCCESS) {
505         LOGE("Failed to generate mk by peer!");
506         FreeJson(payload);
507         return res;
508     }
509     res = VerifyPeerKcf(pakeTask, payload);
510     FreeJson(payload);
511     if (res != HC_SUCCESS) {
512         LOGE("Failed to verify kcf data!");
513         return res;
514     }
515     task->taskStatus = STATUS_FINISH;
516     LOGI("process pake mk agree response successfully!");
517     return HC_SUCCESS;
518 }
519 
GetDuplicateUdid(const CJson * params,char ** returnUdid)520 static int32_t GetDuplicateUdid(const CJson *params, char **returnUdid)
521 {
522     const char *udid = GetStringFromJson(params, FIELD_PEER_UDID);
523     if (udid == NULL) {
524         LOGE("Failed to get udid from json!");
525         return HC_ERR_JSON_GET;
526     }
527     uint32_t udidLen = HcStrlen(udid);
528     *returnUdid = (char *)HcMalloc(udidLen + 1, 0);
529     if (*returnUdid == NULL) {
530         LOGE("Failed to allocate return udid memory!");
531         return HC_ERR_ALLOC_MEMORY;
532     }
533     if (memcpy_s(*returnUdid, udidLen + 1, udid, udidLen) != EOK) {
534         LOGE("Failed to copy udid!");
535         HcFree(*returnUdid);
536         *returnUdid = NULL;
537         return HC_ERR_MEMORY_COPY;
538     }
539     return HC_SUCCESS;
540 }
541 
ProcessPakeMkAgreeTask(MkAgreeTaskBase * task,const CJson * in,CJson * out)542 static int32_t ProcessPakeMkAgreeTask(MkAgreeTaskBase *task, const CJson *in, CJson *out)
543 {
544     if (task == NULL || out == NULL) {
545         LOGE("Invalid input params!");
546         return HC_ERR_INVALID_PARAMS;
547     }
548     if (task->taskStatus == STATUS_INIT) {
549         return StartPakeMkAgreeRequest(task, out);
550     }
551     if (in == NULL) {
552         LOGE("in param is null!");
553         return HC_ERR_INVALID_PARAMS;
554     }
555     int32_t msgCode;
556     if (GetIntFromJson(in, MK_AGREE_MESSAGE_CODE, &msgCode) != HC_SUCCESS) {
557         LOGE("Failed to get message code!");
558         return HC_ERR_JSON_GET;
559     }
560     int32_t res = GetDuplicateUdid(in, &task->peerUdid);
561     if (res != HC_SUCCESS) {
562         LOGE("Failed to get peer udid!");
563         return res;
564     }
565     switch (msgCode) {
566         case START_MK_AGREE_REQUEST:
567             res = ProcessPakeMkAgreeRequest(task, in, out);
568             break;
569         case SEND_MK_AGREE_RESPONSE:
570             res = ProcessPakeMkAgreeResponse(task, in);
571             break;
572         default:
573             LOGE("Invalid message code!");
574             res = HC_ERR_CASE;
575             break;
576     }
577     if (res != HC_SUCCESS) {
578         LOGE("Failed to process mk agree task!");
579     }
580     return res;
581 }
582 
DestroyPakeMkAgreeTask(MkAgreeTaskBase * task)583 static void DestroyPakeMkAgreeTask(MkAgreeTaskBase *task)
584 {
585     if (task == NULL) {
586         return;
587     }
588     HcFree(task->peerInfo);
589     task->peerInfo = NULL;
590     HcFree(task->pdidIndex);
591     task->pdidIndex = NULL;
592     HcFree(task->peerUdid);
593     task->peerUdid = NULL;
594     HcFree(task);
595 }
596 
InitTaskParams(MkAgreeTaskBase * task,const CJson * in)597 static int32_t InitTaskParams(MkAgreeTaskBase *task, const CJson *in)
598 {
599     if (GetIntFromJson(in, FIELD_OS_ACCOUNT_ID, &task->osAccountId) != HC_SUCCESS) {
600         LOGE("Failed to get osAccountId!");
601         return HC_ERR_JSON_GET;
602     }
603     const char *peerInfo = GetStringFromJson(in, FIELD_REAL_INFO);
604     if (peerInfo == NULL) {
605         LOGE("Failed to get peerInfo!");
606         return HC_ERR_JSON_GET;
607     }
608     if (DeepCopyString(peerInfo, &task->peerInfo) != HC_SUCCESS) {
609         LOGE("Failed to copy peerInfo!");
610         return HC_ERR_ALLOC_MEMORY;
611     }
612     const char *pdidIndex = GetStringFromJson(in, FIELD_INDEX_KEY);
613     if (pdidIndex == NULL) {
614         LOGE("Failed to get pdidIndex!");
615         return HC_ERR_JSON_GET;
616     }
617     if (DeepCopyString(pdidIndex, &task->pdidIndex) != HC_SUCCESS) {
618         LOGE("Failed to copy pdidIndex!");
619         return HC_ERR_ALLOC_MEMORY;
620     }
621     task->loader = GetLoaderInstance();
622     return HC_SUCCESS;
623 }
624 
CreatePakeMkAgreeTask(bool isClient,const CJson * in,MkAgreeTaskBase ** returnTask)625 static int32_t CreatePakeMkAgreeTask(bool isClient, const CJson *in, MkAgreeTaskBase **returnTask)
626 {
627     PakeMkAgreeTask *task = (PakeMkAgreeTask *)HcMalloc(sizeof(PakeMkAgreeTask), 0);
628     if (task == NULL) {
629         LOGE("Failed to alloc pake task memory!");
630         return HC_ERR_ALLOC_MEMORY;
631     }
632     if (isClient) {
633         task->taskBase.taskStatus = STATUS_INIT;
634     } else {
635         task->taskBase.taskStatus = STATUS_WAIT_MK_REQUEST;
636     }
637     task->taskBase.taskType = TASK_TYPE_PAKE;
638     task->taskBase.process = ProcessPakeMkAgreeTask;
639     task->taskBase.destroy = DestroyPakeMkAgreeTask;
640 
641     int32_t res = InitTaskParams((MkAgreeTaskBase *)task, in);
642     if (res != HC_SUCCESS) {
643         DestroyPakeMkAgreeTask((MkAgreeTaskBase *)task);
644         return res;
645     }
646 
647     *returnTask = (MkAgreeTaskBase *)task;
648     return HC_SUCCESS;
649 }
650 
CreateMkAgreeTask(int protocolType,const CJson * in,MkAgreeTaskBase ** returnTask)651 int32_t CreateMkAgreeTask(int protocolType, const CJson *in, MkAgreeTaskBase **returnTask)
652 {
653     if (in == NULL || returnTask == NULL)  {
654         LOGE("Invalid input params!");
655         return HC_ERR_INVALID_PARAMS;
656     }
657     bool isClient = false;
658     if (GetBoolFromJson(in, FIELD_IS_CLIENT, &isClient) != HC_SUCCESS) {
659         LOGE("Failed to get isClient!");
660         return HC_ERR_JSON_GET;
661     }
662     int32_t res;
663     switch (protocolType) {
664         case TASK_TYPE_ISO:
665             LOGE("ISO is not supported!");
666             res = HC_ERR_NOT_SUPPORT;
667             break;
668         case TASK_TYPE_PAKE:
669             res = CreatePakeMkAgreeTask(isClient, in, returnTask);
670             break;
671         default:
672             LOGE("Invalid protocol type");
673             res = HC_ERR_INVALID_PARAMS;
674             break;
675     }
676     return res;
677 }