1 /*
2  * Copyright (c) 2020 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 #if !(defined(_CUT_PAKE_) || defined(_CUT_PAKE_SERVER_))
17 
18 #include <log.h>
19 #include "securec.h"
20 #include "mem_stat.h"
21 #include "jsonutil.h"
22 #include "commonutil.h"
23 #include "pake_server.h"
24 #include "parsedata.h"
25 #include "key_agreement_version.h"
26 
free_pake_client_confirm(void * obj)27 void free_pake_client_confirm(void *obj)
28 {
29     if (obj != NULL) {
30         FREE(obj);
31     }
32 }
33 
make_pake_client_confirm(void * data)34 char *make_pake_client_confirm(void *data)
35 {
36     struct pake_end_request_data *pake_client_confirm = data;
37     /* kcfData */
38     uint8_t *tmp_kcf_data_hex = raw_byte_to_hex_string(pake_client_confirm->kcf_data.hmac,
39                                                        pake_client_confirm->kcf_data.length);
40     if (tmp_kcf_data_hex == NULL) {
41         return NULL;
42     }
43     /* challenge */
44     uint8_t *tmp_cha_data_hex = raw_byte_to_hex_string(pake_client_confirm->challenge.challenge,
45                                                        pake_client_confirm->challenge.length);
46     if (tmp_cha_data_hex == NULL) {
47         FREE(tmp_kcf_data_hex);
48         return NULL;
49     }
50     /* epk */
51     uint8_t *tmp_epk_data_hex = raw_byte_to_hex_string(pake_client_confirm->epk.epk,
52                                                        pake_client_confirm->epk.length);
53     if (tmp_epk_data_hex == NULL) {
54         FREE(tmp_kcf_data_hex);
55         FREE(tmp_cha_data_hex);
56         return NULL;
57     }
58     char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
59     if (ret_str == NULL) {
60         FREE(tmp_kcf_data_hex);
61         FREE(tmp_cha_data_hex);
62         FREE(tmp_epk_data_hex);
63         return NULL;
64     }
65     (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
66     if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1,
67         "{\"%s\":%d,\"%s\":{\"%s\":\"%s\", \"%s\":\"%s\", \"%s\":\"%s\"}}", FIELD_MESSAGE,
68         PAKE_CLIENT_CONFIRM, FIELD_PAYLOAD, FIELD_KCF_DATA, tmp_kcf_data_hex, FIELD_CHALLENGE,
69         tmp_cha_data_hex, FIELD_EPK, (char *)tmp_epk_data_hex) < 0) {
70         LOGE("String generate failed");
71         FREE(ret_str);
72         ret_str = NULL;
73     }
74     FREE(tmp_kcf_data_hex);
75     FREE(tmp_cha_data_hex);
76     FREE(tmp_epk_data_hex);
77     return ret_str;
78 }
79 
80 #else /* _CUT_XXX_ */
81 
82 #include "parsedata.h"
83 DEFINE_EMPTY_STRUCT_FUNC(pake_client_confirm)
84 
85 #endif /* _CUT_XXX_ */
86