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 
93 #else /* _CUT_XXX_ */
94 
95 #include "parsedata.h"
96 DEFINE_EMPTY_STRUCT_FUNC(auth_start_response)
97 
98 #endif /* _CUT_XXX_ */
99