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_response(const char * payload,enum json_object_data_type data_type)27 void *parse_rmv_auth_info_response(const char *payload, enum json_object_data_type data_type)
28 {
29     remove_response_data *rmv_auth_info_response = (remove_response_data *)MALLOC(sizeof(remove_response_data));
30     if (rmv_auth_info_response == NULL) {
31         return NULL;
32     }
33     (void)memset_s(rmv_auth_info_response, sizeof(*rmv_auth_info_response), 0, sizeof(*rmv_auth_info_response));
34     json_handle obj = parse_payload(payload, data_type);
35     if (obj == NULL) {
36         LOGE("Parse Rmv AuthInfo Response parse payload failed");
37         goto error;
38     }
39     /* authData */
40     const char *rmv_return = get_json_string(obj, FIELD_RMV_RETURN);
41     if (rmv_return == NULL) {
42         LOGE("Parse Rmv AuthInfo Response failed, field is null in rmvReturn");
43         goto error;
44     }
45     int32_t len_rmv_return = strlen(rmv_return);
46     if ((len_rmv_return / BYTE_TO_HEX_OPER_LENGTH) > HC_AUTH_DATA_BUFF_LEN) {
47         LOGE("Parse Rmv AuthInfo Response failed, field length is not match in rmvReturn");
48         goto error;
49     }
50     rmv_auth_info_response->cipher.length = len_rmv_return / BYTE_TO_HEX_OPER_LENGTH;
51     rmv_auth_info_response->cipher.size = rmv_auth_info_response->cipher.length;
52     rmv_auth_info_response->cipher.val = (uint8_t *)MALLOC(rmv_auth_info_response->cipher.size);
53     if (rmv_auth_info_response->cipher.val == NULL) {
54         LOGE("malloc rmvAuthInfoResponse cipher failed");
55         goto error;
56     }
57     (void)memset_s(rmv_auth_info_response->cipher.val, rmv_auth_info_response->cipher.size,
58                    0, rmv_auth_info_response->cipher.size);
59     if (hex_string_to_byte(rmv_return, len_rmv_return, rmv_auth_info_response->cipher.val) != HC_OK) {
60         goto error;
61     }
62     free_payload(obj, data_type);
63     return (void *)rmv_auth_info_response;
64 error:
65     free_payload(obj, data_type);
66     free_rmv_auth_info_response(rmv_auth_info_response);
67     return NULL;
68 }
69 
free_rmv_auth_info_response(void * obj)70 void free_rmv_auth_info_response(void *obj)
71 {
72     if (obj != NULL) {
73         remove_response_data *data = obj;
74         if (data->cipher.val != NULL) {
75             FREE(data->cipher.val);
76         }
77         FREE(data);
78     }
79 }
80 
make_rmv_auth_info_response(void * data)81 char *make_rmv_auth_info_response(void *data)
82 {
83     remove_response_data *rmv_auth_info_response_data = data;
84     /* authinfo */
85     uint8_t *tmp_data_hex = raw_byte_to_hex_string(rmv_auth_info_response_data->cipher.val,
86                                                    rmv_auth_info_response_data->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         REMOVE_AUTHINFO_RESPONSE, FIELD_PAYLOAD, FIELD_RMV_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(rmv_auth_info_response)
110 
111 #endif /* _CUT_XXX_ */
112