• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     free_payload(obj, data_type);
62     return (void *)add_auth_info_response;
63 error:
64     free_payload(obj, data_type);
65     free_add_auth_info_response(add_auth_info_response);
66     return NULL;
67 }
68 
free_add_auth_info_response(void * obj)69 void free_add_auth_info_response(void *obj)
70 {
71     if (obj != NULL) {
72         add_response_data *data = obj;
73         if (data->cipher.val != NULL) {
74             FREE(data->cipher.val);
75         }
76         FREE(data);
77     }
78 }
79 
make_add_auth_info_response(void * data)80 char *make_add_auth_info_response(void *data)
81 {
82     add_response_data *add_auth_info_response = data;
83     /* authdata */
84     uint8_t *tmp_data_hex = raw_byte_to_hex_string(add_auth_info_response->cipher.val,
85                                                    add_auth_info_response->cipher.length);
86     if (tmp_data_hex == NULL) {
87         return NULL;
88     }
89     char *ret_str = (char *)MALLOC(RET_STR_LENGTH);
90     if (ret_str == NULL) {
91         FREE(tmp_data_hex);
92         return NULL;
93     }
94     (void)memset_s(ret_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
95     if (snprintf_s(ret_str, RET_STR_LENGTH, RET_STR_LENGTH - 1, "{\"%s\":%d,\"%s\":{\"%s\":\"%s\"}}", FIELD_MESSAGE,
96         ADD_AUTHINFO_RESPONSE, FIELD_PAYLOAD, FIELD_ADD_RETURN, tmp_data_hex) < 0) {
97         LOGE("String generate failed");
98         FREE(ret_str);
99         ret_str = NULL;
100     }
101     FREE(tmp_data_hex);
102     return ret_str;
103 }
104 
105 #else /* _CUT_XXX_ */
106 
107 #include "parsedata.h"
108 DEFINE_EMPTY_STRUCT_FUNC(add_auth_info_response)
109 
110 #endif /* _CUT_XXX_ */
111