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