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 
27 
free_exchange_request(void * obj)28 void free_exchange_request(void *obj)
29 {
30     if (obj != NULL) {
31         exchange_request_data *data = obj;
32         if (data->cipher.val != NULL) {
33             FREE(data->cipher.val);
34         }
35         FREE(data);
36     }
37 }
make_exchange_request(void * data)38 char *make_exchange_request(void *data)
39 {
40     exchange_request_data *exchange_request = data;
41     /* authinfo */
42     uint8_t *tmp_data_hex = raw_byte_to_hex_string(exchange_request->cipher.val, exchange_request->cipher.length);
43     if (tmp_data_hex == NULL) {
44         return NULL;
45     }
46     char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
47     if (ret_str == NULL) {
48         FREE(tmp_data_hex);
49         return NULL;
50     }
51     (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
52     if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1, "{\"%s\":%d,\"%s\":{\"%s\":\"%s\"}}", FIELD_MESSAGE,
53         EXCHANGE_REQUEST, FIELD_PAYLOAD, FIELD_EX_AUTH_INFO, tmp_data_hex) < 0) {
54         LOGE("String generate failed");
55         FREE(ret_str);
56         ret_str = NULL;
57     }
58     FREE(tmp_data_hex);
59     return ret_str;
60 }
61 
62 #else /* _CUT_XXX_ */
63 
64 #include "parsedata.h"
65 DEFINE_EMPTY_STRUCT_FUNC(exchange_request)
66 
67 #endif /* _CUT_XXX_ */
68