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_STS_) || defined(_CUT_STS_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 "add_auth_info.h"
25 #include "key_agreement_version.h"
26
parse_auth_ack_request(const char * payload,enum json_object_data_type data_type)27 void *parse_auth_ack_request(const char *payload, enum json_object_data_type data_type)
28 {
29 struct sts_end_request_data *auth_ack_request =
30 (struct sts_end_request_data *)MALLOC(sizeof(struct sts_end_request_data));
31 if (auth_ack_request == NULL) {
32 return NULL;
33 }
34 (void)memset_s(auth_ack_request, sizeof(*auth_ack_request), 0, sizeof(*auth_ack_request));
35 json_handle obj = parse_payload(payload, data_type);
36 if (obj == NULL) {
37 LOGE("Parse Auth ACK Request parse payload failed");
38 goto error;
39 }
40 /* authData */
41 int32_t result = byte_convert(obj, FIELD_AUTH_DATA, auth_ack_request->auth_data.auth_data,
42 (uint32_t *)&auth_ack_request->auth_data.length, HC_AUTH_DATA_BUFF_LEN);
43 if (result != HC_OK) {
44 LOGE("Parse Auth ACK Request Data failed, field is null in addId");
45 goto error;
46 }
47 free_payload(obj, data_type);
48 return (void *)auth_ack_request;
49 error:
50 free_payload(obj, data_type);
51 FREE(auth_ack_request);
52 return NULL;
53 }
54
free_auth_ack_request(void * obj)55 void free_auth_ack_request(void *obj)
56 {
57 if (obj != NULL) {
58 FREE(obj);
59 }
60 }
61
make_auth_ack_request(void * data)62 char *make_auth_ack_request(void *data)
63 {
64 struct sts_end_request_data *auth_ack_request = data;
65 /* authdata */
66 uint8_t *tmp_data_hex = raw_byte_to_hex_string(auth_ack_request->auth_data.auth_data,
67 auth_ack_request->auth_data.length);
68 if (tmp_data_hex == NULL) {
69 return NULL;
70 }
71 char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
72 if (ret_str == NULL) {
73 FREE(tmp_data_hex);
74 return NULL;
75 }
76 (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
77 if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1, "{\"%s\":%d,\"%s\":%d,\"%s\":{\"%s\":\"%s\"}}",
78 FIELD_AUTH_FORM, AUTH_FORM, FIELD_MESSAGE, AUTH_ACK_REQUEST, FIELD_PAYLOAD,
79 FIELD_AUTH_DATA, tmp_data_hex) < 0) {
80 LOGE("String generate failed");
81 FREE(ret_str);
82 ret_str = NULL;
83 }
84 FREE(tmp_data_hex);
85 return ret_str;
86 }
87
88 #else /* _CUT_XXX_ */
89
90 #include "parsedata.h"
91 DEFINE_EMPTY_STRUCT_FUNC(auth_ack_request)
92
93 #endif /* _CUT_XXX_ */
94