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 
free_auth_start_request(void * obj)27 void free_auth_start_request(void *obj)
28 {
29     if (obj != NULL) {
30         FREE(obj);
31     }
32 }
33 
make_request_json_str(struct sts_start_request_data * auth_start_request,struct sts_start_request_data_hex tmp_hex)34 static char *make_request_json_str(struct sts_start_request_data *auth_start_request,
35                                    struct sts_start_request_data_hex tmp_hex)
36 {
37     char *tmp_str = (char *)MALLOC(RET_STR_LENGTH);
38     if (tmp_str == NULL) {
39         return NULL;
40     }
41     (void)memset_s(tmp_str, RET_STR_LENGTH, 0, RET_STR_LENGTH);
42     if (snprintf_s(tmp_str, RET_STR_LENGTH, RET_STR_LENGTH - 1,
43         "{\"%s\":%d,\"%s\":%d,\"%s\":{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":%d,"
44         "\"%s\":{\"%s\":\"%u.%u.%u\",\"%s\":\"%u.%u.%u\"},\"%s\":\"%s\","
45         "\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%d\",\"%s\":\"%u\"}}",
46         FIELD_AUTH_FORM, AUTH_FORM, FIELD_MESSAGE, AUTH_START_REQUEST, FIELD_PAYLOAD,
47         FIELD_CHALLENGE, tmp_hex.tmp_cha_data_hex, FIELD_EPK, tmp_hex.tmp_epk_data_hex,
48         FIELD_OPERATION_CODE, auth_start_request->operation_code,
49         FIELD_VERSION, FIELD_CURRENT_VERSION, auth_start_request->peer_version.first,
50         auth_start_request->peer_version.second,
51         auth_start_request->peer_version.third, FIELD_MIN_VERSION,
52         auth_start_request->peer_support_version.first,
53         auth_start_request->peer_support_version.second,
54         auth_start_request->peer_support_version.third,
55         FIELD_PKG_NAME, auth_start_request->package_name.name,
56         FIELD_SERVICE_TYPE, tmp_hex.tmp_type_data_hex,
57         FIELD_PEER_AUTH_ID, tmp_hex.tmp_auth_id_data_hex,
58         FIELD_PEER_USER_TYPE, auth_start_request->peer_user_type,
59         FIELD_KEY_LENGTH, auth_start_request->key_length) < 0) {
60         LOGE("String generate failed");
61         FREE(tmp_str);
62         tmp_str = NULL;
63     }
64     return tmp_str;
65 }
66 
make_auth_start_request(void * data)67 char *make_auth_start_request(void *data)
68 {
69     struct sts_start_request_data *auth_start_request = data;
70     struct sts_start_request_data_hex tmp_hex = {0, 0, 0, 0};
71     /* challenge */
72     tmp_hex.tmp_cha_data_hex = raw_byte_to_hex_string(auth_start_request->challenge.challenge,
73                                                       auth_start_request->challenge.length);
74     if (tmp_hex.tmp_cha_data_hex == NULL) {
75         return NULL;
76     }
77     /* epk */
78     tmp_hex.tmp_epk_data_hex = raw_byte_to_hex_string(auth_start_request->epk.stpk, auth_start_request->epk.length);
79     if (tmp_hex.tmp_epk_data_hex == NULL) {
80         FREE(tmp_hex.tmp_cha_data_hex);
81         return NULL;
82     }
83     /* service_type */
84     tmp_hex.tmp_type_data_hex = raw_byte_to_hex_string(auth_start_request->service_type.type,
85                                                        auth_start_request->service_type.length);
86     if (tmp_hex.tmp_type_data_hex == NULL) {
87         FREE(tmp_hex.tmp_epk_data_hex);
88         FREE(tmp_hex.tmp_cha_data_hex);
89         return NULL;
90     }
91     /* peerAuthId */
92     tmp_hex.tmp_auth_id_data_hex = raw_byte_to_hex_string(auth_start_request->self_auth_id.auth_id,
93                                                           auth_start_request->self_auth_id.length);
94     if (tmp_hex.tmp_auth_id_data_hex == NULL) {
95         FREE(tmp_hex.tmp_epk_data_hex);
96         FREE(tmp_hex.tmp_type_data_hex);
97         FREE(tmp_hex.tmp_cha_data_hex);
98         return NULL;
99     }
100     char *ret_str = make_request_json_str(auth_start_request, tmp_hex);
101     FREE(tmp_hex.tmp_epk_data_hex);
102     FREE(tmp_hex.tmp_cha_data_hex);
103     FREE(tmp_hex.tmp_type_data_hex);
104     FREE(tmp_hex.tmp_auth_id_data_hex);
105     return ret_str;
106 }
107 
108 #else /* _CUT_XXX_ */
109 
110 #include "parsedata.h"
111 DEFINE_EMPTY_STRUCT_FUNC(auth_start_request)
112 
113 #endif /* _CUT_XXX_ */
114 
115