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_REMOVE_) || defined(_CUT_REMOVE_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 "remove_auth_info.h"
25 #include "key_agreement_version.h"
26 
parse_rmv_auth_info_data(const char * payload,enum json_object_data_type data_type)27 void *parse_rmv_auth_info_data(const char *payload, enum json_object_data_type data_type)
28 {
29     struct remove_auth_data *rmv_auth_data = (struct remove_auth_data *)MALLOC(sizeof(struct remove_auth_data));
30     if (rmv_auth_data == NULL) {
31         return NULL;
32     }
33     (void)memset_s(rmv_auth_data, sizeof(*rmv_auth_data), 0, sizeof(*rmv_auth_data));
34     json_handle obj = parse_payload(payload, data_type);
35     if (obj == NULL) {
36         LOGE("Parse Rmv AuthInfo Data parse payload failed");
37         goto error;
38     }
39     /* user_type */
40     int32_t type = get_json_int(obj, FIELD_RMV_TYPE);
41     if (type == -1) {
42         LOGE("Parse Rmv AuthInfo Data failed, field is null in rmvType");
43         goto error;
44     }
45     rmv_auth_data->user_type = type;
46     /* rmvId */
47     int32_t result = byte_convert(obj, FIELD_RMV_ID, rmv_auth_data->auth_id.auth_id,
48                                   (uint32_t *)&rmv_auth_data->auth_id.length, HC_AUTH_ID_BUFF_LEN);
49     if (result != HC_OK) {
50         LOGE("Parse Rmv AuthInfo Data failed, field is null in rmvId");
51         goto error;
52     }
53     free_payload(obj, data_type);
54     return (void *)rmv_auth_data;
55 error:
56     free_payload(obj, data_type);
57     FREE(rmv_auth_data);
58     return NULL;
59 }
60 
free_rmv_auth_info_data(void * obj)61 void free_rmv_auth_info_data(void *obj)
62 {
63     if (obj != NULL) {
64         FREE(obj);
65     }
66 }
67 
make_rmv_auth_info_data(void * data)68 char *make_rmv_auth_info_data(void *data)
69 {
70     struct remove_auth_data *rmv_auth_data = data;
71     /* rmvId */
72     uint8_t *tmp_rmv_id_data_hex = raw_byte_to_hex_string(rmv_auth_data->auth_id.auth_id,
73                                                           rmv_auth_data->auth_id.length);
74     if (tmp_rmv_id_data_hex == NULL) {
75         return NULL;
76     }
77     char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
78     if (ret_str == NULL) {
79         FREE(tmp_rmv_id_data_hex);
80         return NULL;
81     }
82     (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
83     if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1, "{\"%s\":%d,\"%s\":\"%s\"}", FIELD_RMV_TYPE,
84         rmv_auth_data->user_type, FIELD_RMV_ID, tmp_rmv_id_data_hex) < 0) {
85         LOGE("String generate failed");
86         FREE(ret_str);
87         ret_str = NULL;
88     }
89     FREE(tmp_rmv_id_data_hex);
90     return ret_str;
91 }
92 
93 #else /* _CUT_XXX_ */
94 
95 #include "parsedata.h"
96 DEFINE_EMPTY_STRUCT_FUNC(rmv_auth_info_data)
97 
98 #endif /* _CUT_XXX_ */
99