• 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_))
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 "key_agreement_version.h"
25 #include "add_auth_info.h"
26 
parse_auth_start_response(const char * payload,enum json_object_data_type data_type)27 void *parse_auth_start_response(const char *payload, enum json_object_data_type data_type)
28 {
29     struct sts_start_response_data *auth_start_response =
30         (struct sts_start_response_data *)MALLOC(sizeof(struct sts_start_response_data));
31     if (auth_start_response == NULL) {
32         return NULL;
33     }
34     (void)memset_s(auth_start_response, sizeof(*auth_start_response), 0, sizeof(*auth_start_response));
35     json_handle obj = parse_payload(payload, data_type);
36     if (obj == NULL) {
37         LOGE("Parse AuthStart Response parse payload failed");
38         goto error;
39     }
40     /* authData */
41     int32_t result = byte_convert(obj, FIELD_AUTH_DATA, auth_start_response->auth_data.auth_data,
42                                   &auth_start_response->auth_data.length, HC_AUTH_DATA_BUFF_LEN);
43     if (result != HC_OK) {
44         LOGE("Parse AuthStart Response failed, field is null in authData");
45         goto error;
46     }
47 
48     /* challenge */
49     result = byte_convert(obj, FIELD_CHALLENGE, auth_start_response->challenge.challenge,
50                           &auth_start_response->challenge.length, CHALLENGE_BUFF_LENGTH);
51     if (result != HC_OK) {
52         LOGE("Parse AuthStart Response failed, field is null in challenge");
53         goto error;
54     }
55 
56     /* salt */
57     result = byte_convert(obj, FIELD_SALT, auth_start_response->salt.salt,
58                           (uint32_t *)&auth_start_response->salt.length, HC_SALT_BUFF_LEN);
59     if (result != HC_OK) {
60         LOGE("Parse AuthStart Response failed, field is null in salt");
61         goto error;
62     }
63 
64     /* epk */
65     result = byte_convert(obj, FIELD_EPK, auth_start_response->epk.stpk,
66                           &auth_start_response->epk.length, HC_ST_PUBLIC_KEY_LEN);
67     if (result != HC_OK) {
68         LOGE("Parse AuthStart Response failed, field is null in epk");
69         goto error;
70     }
71     /* version */
72     json_pobject obj_ver = get_json_obj(obj, FIELD_VERSION);
73     bool ret = parse_version(obj_ver, &auth_start_response->self_version, &auth_start_response->self_support_version);
74     if (!ret) {
75         LOGE("Parse AuthStart Response failed, field is null in version");
76         goto error;
77     }
78     free_payload(obj, data_type);
79     return (void *)auth_start_response;
80 error:
81     free_payload(obj, data_type);
82     FREE(auth_start_response);
83     return NULL;
84 }
85 
free_auth_start_response(void * obj)86 void free_auth_start_response(void *obj)
87 {
88     if (obj != NULL) {
89         FREE(obj);
90     }
91 }
92 
make_response_json_str(struct sts_start_response_data * as_res,struct sts_start_response_data_hex tmp_hex)93 static char *make_response_json_str(struct sts_start_response_data *as_res,
94                                     struct sts_start_response_data_hex tmp_hex)
95 {
96     char *tmp_str = (char *)MALLOC(RET_STR_LENGTH);
97     if (tmp_str == NULL) {
98         return NULL;
99     }
100 
101     (void)memset_s(tmp_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
102     if (snprintf_s(tmp_str, RET_STR_LENGTH, RET_STR_LENGTH - 1,
103         "{\"%s\":%d,\"%s\":%d,\"%s\":{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\","
104         "\"%s\":{\"%s\":\"%u.%u.%u\",\"%s\":\"%u.%u.%u\"},\"%s\":\"%s\",\"%s\":\"%d\"}}",
105         FIELD_AUTH_FORM, AUTH_FORM, FIELD_MESSAGE, AUTH_START_RESPONSE, FIELD_PAYLOAD, FIELD_AUTH_DATA,
106         tmp_hex.tmp_auth_data_hex, FIELD_CHALLENGE, tmp_hex.tmp_cha_data_hex, FIELD_EPK,
107         tmp_hex.tmp_epk_data_hex, FIELD_SALT, tmp_hex.tmp_salt_data_hex,
108         FIELD_VERSION, FIELD_CURRENT_VERSION, as_res->self_version.first,
109         as_res->self_version.second, as_res->self_version.third, FIELD_MIN_VERSION,
110         as_res->self_support_version.first, as_res->self_support_version.second,
111         as_res->self_support_version.third, FIELD_PEER_AUTH_ID, tmp_hex.tmp_auth_id_data_hex,
112         FIELD_PEER_USER_TYPE, as_res->peer_user_type) < 0) {
113         LOGE("String generate failed");
114         FREE(tmp_str);
115         tmp_str = NULL;
116     }
117     return tmp_str;
118 }
119 
make_auth_start_response(void * data)120 char *make_auth_start_response(void *data)
121 {
122     struct sts_start_response_data *as_res = data;
123     struct sts_start_response_data_hex tmp_hex = {0, 0, 0, 0, 0};
124     /* authData */
125     tmp_hex.tmp_auth_data_hex = raw_byte_to_hex_string(as_res->auth_data.auth_data, as_res->auth_data.length);
126     if (tmp_hex.tmp_auth_data_hex == NULL) {
127         return NULL;
128     }
129     /* challenge */
130     tmp_hex.tmp_cha_data_hex = raw_byte_to_hex_string(as_res->challenge.challenge, as_res->challenge.length);
131     if (tmp_hex.tmp_cha_data_hex == NULL) {
132         FREE(tmp_hex.tmp_auth_data_hex);
133         return NULL;
134     }
135     /* salt */
136     tmp_hex.tmp_salt_data_hex = raw_byte_to_hex_string(as_res->salt.salt, as_res->salt.length);
137     if (tmp_hex.tmp_salt_data_hex == NULL) {
138         FREE(tmp_hex.tmp_auth_data_hex);
139         FREE(tmp_hex.tmp_cha_data_hex);
140         return NULL;
141     }
142     /* epk */
143     tmp_hex.tmp_epk_data_hex = raw_byte_to_hex_string(as_res->epk.stpk, as_res->epk.length);
144     if (tmp_hex.tmp_epk_data_hex == NULL) {
145         FREE(tmp_hex.tmp_auth_data_hex);
146         FREE(tmp_hex.tmp_cha_data_hex);
147         FREE(tmp_hex.tmp_salt_data_hex);
148         return NULL;
149     }
150     /* peerAuthId */
151     tmp_hex.tmp_auth_id_data_hex = raw_byte_to_hex_string(as_res->self_auth_id.auth_id, as_res->self_auth_id.length);
152     if (tmp_hex.tmp_auth_id_data_hex == NULL) {
153         FREE(tmp_hex.tmp_epk_data_hex);
154         FREE(tmp_hex.tmp_cha_data_hex);
155         FREE(tmp_hex.tmp_salt_data_hex);
156         FREE(tmp_hex.tmp_auth_data_hex);
157         return NULL;
158     }
159     char *ret_str = make_response_json_str(as_res, tmp_hex);
160 
161     FREE(tmp_hex.tmp_auth_data_hex);
162     FREE(tmp_hex.tmp_cha_data_hex);
163     FREE(tmp_hex.tmp_salt_data_hex);
164     FREE(tmp_hex.tmp_epk_data_hex);
165     FREE(tmp_hex.tmp_auth_id_data_hex);
166     return ret_str;
167 }
168 
169 #else /* _CUT_XXX_ */
170 
171 #include "parsedata.h"
172 DEFINE_EMPTY_STRUCT_FUNC(auth_start_response)
173 
174 #endif /* _CUT_XXX_ */
175