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 #ifndef __STS_SERVER_H__
17 #define __STS_SERVER_H__
18 
19 #include "hichain.h"
20 #include "base.h"
21 
22 enum sts_message_type {
23     STS_START_MSG = 1,
24     STS_END_MSG,
25 };
26 
27 #include "key_agreement_server.h"
28 
29 #if (defined(_SUPPORT_SEC_CLONE_) || defined(_SUPPORT_SEC_CLONE_SERVER_))
30 #define HC_AUTH_DATA_BUFF_LEN 8192
31 #else
32 #define HC_AUTH_DATA_BUFF_LEN 256
33 #endif
34 
35 #define STS_SESSION_KEY_LENGTH 16
36 #define PEER_USER_TYPE_EMPTY (-1)
37 #define PEER_USER_TYPE_ACCESSORY 99
38 
39 #define HICHAIN_RETURN_KEY "hichain_return_key"
40 #define HICHAIN_AUTH_INFO  "hichain_auth_info"
41 
42 struct auth_data {
43     uint32_t length;
44     uint8_t auth_data[HC_AUTH_DATA_BUFF_LEN];
45 };
46 
47 struct auth_return {
48     uint32_t length;
49     uint8_t auth_return[HC_AUTH_DATA_BUFF_LEN];
50 };
51 
52 struct sts_session_key {
53     uint32_t length;
54     uint8_t key[STS_SESSION_KEY_LENGTH];
55 };
56 
57 struct sts_end_response_data {
58     struct auth_return auth_return;
59 };
60 
61 struct sts_start_response_data {
62     struct key_agreement_version self_version;
63     struct key_agreement_version self_support_version;
64     struct challenge challenge;
65     struct hc_salt salt;
66     struct stpk epk;
67     struct auth_data auth_data;
68     struct hc_auth_id self_auth_id;
69     enum hc_user_type peer_user_type;
70 };
71 
72 #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_))
73 struct sts_server {
74     struct key_agreement_server server_info;
75     const struct session_identity *identity;
76     struct stpk self_public_key;
77     struct stsk self_private_key;
78     struct stpk peer_public_key;
79     struct sts_session_key session_key;
80     struct hc_session_key service_key;
81     uint32_t key_length;
82     struct challenge peer_challenge;
83     struct challenge my_challenge;
84     struct hc_auth_id peer_id;
85     struct hc_auth_id self_id;
86     enum hc_user_type peer_user_type;
87     struct hc_salt salt;
88     hc_handle hichain_handle;
89     uint32_t start_request;
90     uint32_t ack_request;
91     struct sts_start_response_data start_response_data;
92     struct sts_end_response_data end_response_data;
93 };
94 #else
95 struct sts_server {
96     char rsv;
97 };
98 #endif
99 
100 struct sts_start_request_data {
101     struct key_agreement_version peer_version;
102     struct key_agreement_version peer_support_version;
103     int32_t operation_code;
104     struct challenge challenge;
105     struct stpk epk;
106     struct hc_package_name package_name;
107     struct hc_service_type service_type;
108     struct hc_auth_id self_auth_id;
109     enum hc_user_type peer_user_type;
110     uint32_t key_length;
111 };
112 
113 struct sts_start_request_data_hex {
114     uint8_t *tmp_cha_data_hex;
115     uint8_t *tmp_epk_data_hex;
116     uint8_t *tmp_type_data_hex;
117     uint8_t *tmp_auth_id_data_hex;
118 };
119 
120 struct sts_start_response_data_hex {
121     uint8_t *tmp_auth_data_hex;
122     uint8_t *tmp_cha_data_hex;
123     uint8_t *tmp_salt_data_hex;
124     uint8_t *tmp_epk_data_hex;
125     uint8_t *tmp_auth_id_data_hex;
126 };
127 
128 struct sts_end_request_data {
129     struct auth_data auth_data;
130 };
131 
132 #if !(defined(_CUT_STS_) || defined(_CUT_STS_SERVER_))
sts_server_sn(struct sts_server * server)133 static inline uint32_t sts_server_sn(struct sts_server *server)
134 {
135     return server->server_info.protocol_base_info.sn;
136 }
137 
138 #endif /* _CUT_XXX_ */
139 
140 void destroy_sts_server(struct sts_server *handle);
141 
142 #endif /* __STS_SERVER_H__ */
143