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_data(const char * payload,enum json_object_data_type data_type)27 void *parse_add_auth_info_data(const char *payload, enum json_object_data_type data_type)
28 {
29 struct add_auth_data *add_auth_data = (struct add_auth_data *)MALLOC(sizeof(struct add_auth_data));
30 if (add_auth_data == NULL) {
31 return NULL;
32 }
33 (void)memset_s(add_auth_data, sizeof(*add_auth_data), 0, sizeof(*add_auth_data));
34 json_pobject obj = parse_payload(payload, data_type);
35 if (obj == NULL) {
36 LOGE("Parse Add AuthInfo Data parse payload failed");
37 goto error;
38 }
39 /* user_type */
40 int32_t type = get_json_int(obj, FIELD_ADD_TYPE);
41 if (type == -1) {
42 LOGE("Parse Add AuthInfo Data failed, field in addType");
43 goto error;
44 }
45 add_auth_data->user_type = type;
46 /* addId */
47 int32_t result = byte_convert(obj, FIELD_ADD_ID, add_auth_data->auth_id.auth_id,
48 (uint32_t *)&add_auth_data->auth_id.length, HC_AUTH_ID_BUFF_LEN);
49 if (result != HC_OK) {
50 LOGE("Parse Add AuthInfo Data failed, field is wrong in addId");
51 goto error;
52 }
53 /* addKey */
54 result = byte_convert(obj, FIELD_ADD_KEY, add_auth_data->ltpk.ltpk,
55 &add_auth_data->ltpk.length, HC_LT_PUBLIC_KEY_LEN);
56 if (result != HC_OK) {
57 LOGE("Parse Add AuthInfo Data failed, field is wrong in addKey");
58 goto error;
59 }
60 /* Permission */
61 int32_t permission = get_json_bool(obj, FIELD_PERMISSION);
62 if (permission == -1) {
63 LOGE("Parse Add AuthInfo Data failed, field is null in Permission");
64 goto error;
65 }
66 add_auth_data->permission = permission;
67 free_payload(obj, data_type);
68 return (void *)add_auth_data;
69 error:
70 free_payload(obj, data_type);
71 FREE(add_auth_data);
72 return NULL;
73 }
74
free_add_auth_info_data(void * obj)75 void free_add_auth_info_data(void *obj)
76 {
77 if (obj != NULL) {
78 FREE(obj);
79 }
80 }
81
make_add_auth_info_data(void * data)82 char *make_add_auth_info_data(void *data)
83 {
84 struct add_auth_data *add_auth_data = data;
85 /* addId */
86 uint8_t *tmp_add_id_data_hex = raw_byte_to_hex_string(add_auth_data->auth_id.auth_id,
87 add_auth_data->auth_id.length);
88 if (tmp_add_id_data_hex == NULL) {
89 return NULL;
90 }
91 /* addKey */
92 uint8_t *tmp_add_key_data_hex = raw_byte_to_hex_string(add_auth_data->ltpk.ltpk, add_auth_data->ltpk.length);
93 if (tmp_add_key_data_hex == NULL) {
94 FREE(tmp_add_id_data_hex);
95 return NULL;
96 }
97 char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
98 if (ret_str == NULL) {
99 FREE(tmp_add_id_data_hex);
100 FREE(tmp_add_key_data_hex);
101 return NULL;
102 }
103 (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
104 if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1,
105 "{\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":%s}", FIELD_ADD_TYPE, add_auth_data->user_type,
106 FIELD_ADD_ID, tmp_add_id_data_hex, FIELD_ADD_KEY, tmp_add_key_data_hex, FIELD_PERMISSION,
107 add_auth_data->permission ? "true" : "false") < 0) {
108 LOGE("String generate failed");
109 FREE(ret_str);
110 ret_str = NULL;
111 }
112 FREE(tmp_add_id_data_hex);
113 FREE(tmp_add_key_data_hex);
114 return ret_str;
115 }
116
117 #else /* _CUT_XXX_ */
118
119 #include "parsedata.h"
120 DEFINE_EMPTY_STRUCT_FUNC(add_auth_info_data)
121
122 #endif /* _CUT_XXX_ */
123