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_) || defined(_CUT_EXCHANGE_) || defined(_CUT_EXCHANGE_SERVER_))
17 
18 #include "securec.h"
19 #include "log.h"
20 #include "mem_stat.h"
21 #include "jsonutil.h"
22 #include "commonutil.h"
23 #include "parsedata.h"
24 #include "exchange_auth_info.h"
25 #include "key_agreement_version.h"
26 
parse_exchange_data(const char * payload,enum json_object_data_type data_type)27 void *parse_exchange_data(const char *payload, enum json_object_data_type data_type)
28 {
29     struct exchange_auth_data *exchange_auth_data =
30         (struct exchange_auth_data *)MALLOC(sizeof(struct exchange_auth_data));
31     if (exchange_auth_data == NULL) {
32         return NULL;
33     }
34     (void)memset_s(exchange_auth_data, sizeof(*exchange_auth_data), 0, sizeof(*exchange_auth_data));
35     json_handle obj = parse_payload(payload, data_type);
36     if (obj == NULL) {
37         LOGE("Parse Exchange Data parse payload failed");
38         goto error;
39     }
40     /* authId */
41     int32_t result = byte_convert(obj, FIELD_AUTH_ID, exchange_auth_data->auth_id.auth_id,
42                                   (uint32_t *)&exchange_auth_data->auth_id.length, HC_AUTH_ID_BUFF_LEN);
43     if (result != HC_OK) {
44         LOGE("Parse Exchange Data failed, field is null in authId");
45         goto error;
46     }
47     /* authPk */
48     result = byte_convert(obj, FIELD_AUTH_PK, exchange_auth_data->ltpk.ltpk,
49                           (uint32_t *)&exchange_auth_data->ltpk.length, HC_LT_PRIVATE_KEY_LEN);
50     if (result != HC_OK) {
51         LOGE("Parse Exchange Data failed, field is null in authPk");
52         goto error;
53     }
54     free_payload(obj, data_type);
55     return (void *)exchange_auth_data;
56 error:
57     free_payload(obj, data_type);
58     FREE(exchange_auth_data);
59     return NULL;
60 }
61 
free_exchange_data(void * obj)62 void free_exchange_data(void *obj)
63 {
64     if (obj != NULL) {
65         FREE(obj);
66     }
67 }
68 
make_exchange_data(void * data)69 char *make_exchange_data(void *data)
70 {
71     struct exchange_auth_data *exchange_auth_data = data;
72     /* authId */
73     uint8_t  *tmp_auth_id_data_hex = raw_byte_to_hex_string(exchange_auth_data->auth_id.auth_id,
74                                                             exchange_auth_data->auth_id.length);
75     if (tmp_auth_id_data_hex == NULL) {
76         return NULL;
77     }
78     /* authPk */
79     uint8_t  *tmp_auth_pk_data_hex = raw_byte_to_hex_string(exchange_auth_data->ltpk.ltpk,
80                                                             exchange_auth_data->ltpk.length);
81     if (tmp_auth_pk_data_hex == NULL) {
82         FREE(tmp_auth_id_data_hex);
83         return NULL;
84     }
85     char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
86     if (ret_str == NULL) {
87         FREE(tmp_auth_id_data_hex);
88         FREE(tmp_auth_pk_data_hex);
89         return NULL;
90     }
91     (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
92     if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1, "{\"%s\":\"%s\", \"%s\":\"%s\"}", FIELD_AUTH_ID,
93         tmp_auth_id_data_hex, FIELD_AUTH_PK, tmp_auth_pk_data_hex) < 0) {
94         LOGE("String generate failed");
95         FREE(ret_str);
96         ret_str = NULL;
97     }
98     FREE(tmp_auth_id_data_hex);
99     FREE(tmp_auth_pk_data_hex);
100     return ret_str;
101 }
102 
103 #else /* _CUT_XXX_ */
104 
105 #include "parsedata.h"
106 DEFINE_EMPTY_STRUCT_FUNC(exchange_data)
107 
108 #endif /* _CUT_XXX_ */
109