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