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_request(const char * payload,enum json_object_data_type data_type)27 void *parse_exchange_request(const char *payload, enum json_object_data_type data_type)
28 {
29 exchange_request_data *exchange_request = (exchange_request_data *)MALLOC(sizeof(exchange_request_data));
30 if (exchange_request == NULL) {
31 return NULL;
32 }
33 (void)memset_s(exchange_request, sizeof(*exchange_request), 0, sizeof(*exchange_request));
34 json_handle obj = parse_payload(payload, data_type);
35 if (obj == NULL) {
36 LOGE("Parse Exchange Request parse payload failed");
37 goto error;
38 }
39 /* authData */
40 const char *ex_auth_info = get_json_string(obj, FIELD_EX_AUTH_INFO);
41 if (ex_auth_info == NULL) {
42 LOGE("Parse Exchange Request failed, field is null in exAuthInfo");
43 goto error;
44 }
45 int32_t len_ex_auth_info = strlen(ex_auth_info);
46 if ((len_ex_auth_info / BYTE_TO_HEX_OPER_LENGTH) > HC_AUTH_REQUEST_LEN) {
47 LOGE("Parse Exchange Request failed, field length is not match in exAuthInfo");
48 goto error;
49 }
50 exchange_request->cipher.length = len_ex_auth_info / BYTE_TO_HEX_OPER_LENGTH;
51 exchange_request->cipher.size = exchange_request->cipher.length;
52 exchange_request->cipher.val = (uint8_t *)MALLOC(exchange_request->cipher.size);
53 if (exchange_request->cipher.val == NULL) {
54 LOGE("malloc exchangeRequest cipher failed");
55 goto error;
56 }
57 (void)memset_s(exchange_request->cipher.val, exchange_request->cipher.size, 0, exchange_request->cipher.size);
58 if (hex_string_to_byte(ex_auth_info, len_ex_auth_info, exchange_request->cipher.val) != HC_OK) {
59 goto error;
60 }
61 free_payload(obj, data_type);
62 return (void *)exchange_request;
63 error:
64 free_payload(obj, data_type);
65 free_exchange_request(exchange_request);
66 return NULL;
67 }
68
free_exchange_request(void * obj)69 void free_exchange_request(void *obj)
70 {
71 if (obj != NULL) {
72 exchange_request_data *data = obj;
73 if (data->cipher.val != NULL) {
74 FREE(data->cipher.val);
75 }
76 FREE(data);
77 }
78 }
make_exchange_request(void * data)79 char *make_exchange_request(void *data)
80 {
81 exchange_request_data *exchange_request = data;
82 /* authinfo */
83 uint8_t *tmp_data_hex = raw_byte_to_hex_string(exchange_request->cipher.val, exchange_request->cipher.length);
84 if (tmp_data_hex == NULL) {
85 return NULL;
86 }
87 char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
88 if (ret_str == NULL) {
89 FREE(tmp_data_hex);
90 return NULL;
91 }
92 (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
93 if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1, "{\"%s\":%d,\"%s\":{\"%s\":\"%s\"}}", FIELD_MESSAGE,
94 EXCHANGE_REQUEST, FIELD_PAYLOAD, FIELD_EX_AUTH_INFO, tmp_data_hex) < 0) {
95 LOGE("String generate failed");
96 FREE(ret_str);
97 ret_str = NULL;
98 }
99 FREE(tmp_data_hex);
100 return ret_str;
101 }
102
103 #else /* _CUT_XXX_ */
104
105 #include "parsedata.h"
106 DEFINE_EMPTY_STRUCT_FUNC(exchange_request)
107
108 #endif /* _CUT_XXX_ */
109