• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 AUTH_INTERFACE_H
17 #define AUTH_INTERFACE_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 
22 #include "softbus_conn_interface.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define MAX_DEVICE_KEY_LEN 64
29 #define AUTH_ERROR_CODE (-1)
30 #define AUTH_INVALID_ID (-1)
31 
32 typedef enum {
33     /* nearby type v1 */
34     SOFT_BUS_OLD_V1 = 1,
35 
36     /* nearby type v2 */
37     SOFT_BUS_OLD_V2 = 2,
38 
39     /* softbus type v1 */
40     SOFT_BUS_NEW_V1 = 100,
41 } SoftBusVersion;
42 
43 typedef enum {
44     /* data type for device authentication */
45     DATA_TYPE_AUTH = 0xFFFF0001,
46 
47     /* data type for synchronizing peer device information */
48     DATA_TYPE_SYNC = 0xFFFF0002,
49 
50     /* data type for synchronizing peer device id */
51     DATA_TYPE_DEVICE_ID = 0xFFFF0003,
52 
53     /* data type for connection */
54     DATA_TYPE_CONNECTION = 0xFFFF0004,
55 
56     /* data type for closing ack */
57     DATA_TYPE_CLOSE_ACK = 0xFFFF0005,
58 } AuthDataType;
59 
60 typedef enum {
61     /* reserved */
62     NONE = 0,
63 
64     /* trust Engine, use plain text */
65     TRUST_ENGINE = 1,
66 
67     /* hiChain, use plain text */
68     HICHAIN = 2,
69 
70     /* authentication SDK, use plain text */
71     AUTH_SDK = 3,
72 
73     /* hichain sync data, use plain text */
74     HICHAIN_SYNC = 4,
75 } AuthDataModule;
76 
77 typedef enum {
78     CLIENT_SIDE_FLAG = 0,
79     SERVER_SIDE_FLAG = 1,
80     AUTH_SIDE_ANY,
81 } AuthSideFlag;
82 
83 typedef enum {
84     LNN = 0,
85     BUSCENTER_MONITOR,
86     VERIFY_P2P_DEVICE,
87     VERIFY_MODULE_NUM
88 } AuthVerifyModule;
89 
90 typedef enum {
91     TRANS_UDP_DATA = 0,
92     TRANS_AUTH_CHANNEL,
93     TRANS_TIME_SYNC_CHANNEL,
94     TRANS_P2P_MODULE,
95     TRANS_P2P_LISTEN,
96     TRANS_MODULE_NUM
97 } AuthTransModule;
98 
99 typedef struct {
100     uint8_t *buf;
101     uint32_t bufLen;
102     uint32_t outLen;
103 } OutBuf;
104 
105 typedef struct {
106     AuthDataType dataType;
107     int32_t module;
108     int64_t authId;
109     int32_t flag;
110     int64_t seq;
111 } AuthDataHead;
112 
113 typedef struct {
114     int32_t module;
115     int32_t flags;
116     int64_t seq;
117     char *data;
118     uint32_t len;
119 } AuthTransDataInfo;
120 
121 typedef struct {
122     void (*onKeyGenerated)(int64_t authId, ConnectOption *option, SoftBusVersion peerVersion);
123     void (*onDeviceVerifyFail)(int64_t authId, int32_t reason);
124     void (*onRecvSyncDeviceInfo)(int64_t authId, AuthSideFlag side, const char *peerUuid, uint8_t *data, uint32_t len);
125     void (*onDeviceVerifyPass)(int64_t authId);
126     void (*onDeviceNotTrusted)(const char *peerUdid);
127     void (*onDisconnect)(int64_t authId);
128     void (*onGroupCreated)(const char *groupId);
129     void (*onGroupDeleted)(const char *groupId);
130 } VerifyCallback;
131 
132 typedef struct {
133     void (*onTransUdpDataRecv)(int64_t authId, const ConnectOption *option, const AuthTransDataInfo *info);
134     void (*onAuthChannelClose)(int64_t authId);
135 } AuthTransCallback;
136 
137 uint32_t AuthGetEncryptHeadLen(void);
138 int32_t AuthEncrypt(const ConnectOption *option, AuthSideFlag *side, uint8_t *data, uint32_t len, OutBuf *outBuf);
139 int32_t AuthDecrypt(const ConnectOption *option, AuthSideFlag side, uint8_t *data, uint32_t len, OutBuf *outbuf);
140 int32_t AuthEncryptBySeq(int32_t seq, AuthSideFlag *side, uint8_t *data, uint32_t len, OutBuf *outBuf);
141 
142 int32_t OpenAuthServer(void);
143 void CloseAuthServer(void);
144 int32_t AuthRegCallback(AuthVerifyModule moduleId, VerifyCallback *cb);
145 int32_t AuthTransDataRegCallback(AuthTransModule moduleId, AuthTransCallback *cb);
146 void AuthTransDataUnRegCallback(AuthTransModule moduleId);
147 
148 int64_t AuthVerifyDevice(AuthVerifyModule moduleId, const ConnectionAddr *addr);
149 
150 int64_t AuthOpenChannel(const ConnectOption *option);
151 int32_t AuthPostData(const AuthDataHead *head, const uint8_t *data, uint32_t len);
152 int32_t AuthCloseChannel(int64_t authId);
153 int32_t AuthHandleLeaveLNN(int64_t authId);
154 
155 int32_t AuthGetUuidByOption(const ConnectOption *option, char *buf, uint32_t bufLen);
156 int32_t AuthGetIdByOption(const ConnectOption *option, int64_t *authId);
157 int32_t AuthGetServerSideByOption(const ConnectOption *option, bool *isServerSide);
158 
159 int32_t AuthInit(void);
160 void AuthDeinit(void);
161 
162 typedef enum {
163     AUTH_LINK_TYPE_WIFI = 0,
164     AUTH_LINK_TYPE_BR,
165     AUTH_LINK_TYPE_BLE,
166     AUTH_LINK_TYPE_P2P,
167     AUTH_LINK_TYPE_MAX
168 } AuthLinkType;
169 
170 typedef struct {
171     AuthLinkType type;
172     union {
173         struct {
174             char brMac[BT_MAC_LEN];
175         } brInfo;
176         struct {
177             char bleMac[BT_MAC_LEN];
178         } bleInfo;
179         struct {
180             char ip[IP_LEN];
181             uint16_t port;
182         } ipInfo;
183     } info;
184     char peerUid[MAX_ACCOUNT_HASH_LEN];
185 } AuthConnInfo;
186 
187 /**
188  * @brief Defines auth connection callbacks.
189  */
190 typedef struct {
191     /**
192      * @brief Called when an auth connection is opened successfully.
193      *
194      * @param requestId indicates the open request.
195      * @param authId id of auth connection.
196      */
197     void (*onConnOpened)(uint32_t requestId, int64_t authId);
198     /**
199      * @brief Called when an auth connection is opened failed.
200      *
201      * @param requestId indicates the open request.
202      * @param reason error code.
203      */
204     void (*onConnOpenFailed)(uint32_t requestId, int32_t reason);
205 } AuthConnCallback;
206 
207 typedef struct {
208     AuthLinkType type; /* WIFI_WLAN and WIFI_P2P are supported. */
209     union {
210         struct {
211             char ip[IP_STR_MAX_LEN];
212             uint16_t port;
213         } ipInfo;
214     } info;
215 } AuthListennerInfo;
216 
217 /**
218  * @brief Start auth server listener, which identified by ip and port.
219  *
220  * @param info listener info {@link AuthListennerInfo}.
221  * @return return SOFTBUS_OK if start successfully, otherwise return an error code.
222  */
223 int32_t AuthStartListening(const AuthListennerInfo *info);
224 
225 /**
226  * @brief Stop auth server listener.
227  *
228  * @param info listener info {@link AuthListennerInfo}, only {@link type} is used.
229  * @return return SOFTBUS_OK if stop successfully, otherwise return an error code.
230  */
231 int32_t AuthStopListening(const AuthListennerInfo *info);
232 
233 /**
234  * @brief Initiate an auth connection open request, which is an asynchronous process.
235  *
236  * For WIFI_WLAN, just return the active auth connection.
237  * For BR/BLE/WIFI_P2P, firstly establish a connection, and then do verify process.
238  *
239  * @param info auth connection info to special remote device {@link AuthConnInfo}.
240  * @param requestId unique request id, which generated by {@link AuthGenRequestId}.
241  * @param callback callback {@link AuthConnCallback} to receive open result, which cannot be empty.
242  * @return return SOFTBUS_OK if request successfully, otherwise return an error code.
243  */
244 int32_t AuthOpenConn(const AuthConnInfo *info, uint32_t requestId, const AuthConnCallback *callback);
245 
246 /**
247  * @brief Close an auth connection.
248  *
249  * @param authId id of auth connection.
250  */
251 void AuthCloseConn(int64_t authId);
252 
253 /**
254  * @brief Generate an unique request id.
255  *
256  * @return return request id.
257  */
258 uint32_t AuthGenRequestId(void);
259 
260 /**
261  * @brief Get auth connection info, which identified by auth id.
262  *
263  * @param authId id of auth connection.
264  * @param info auth connection info {@link AuthConnInfo}.
265  * @return return SOFTBUS_OK if get successfully, otherwise return an error code.
266  */
267 int32_t AuthGetConnInfo(int64_t authId, AuthConnInfo *info);
268 
269 /**
270  * @brief Get peer device uuid from auth connection, which identified by auth id.
271  *
272  * @param authId id of auth connection.
273  * @param buf buffer to cache uuid.
274  * @param size size of buffer {@link UUID_BUF_LEN}.
275  * @return return SOFTBUS_OK if get successfully, otherwise return an error code.
276  */
277 int32_t AuthGetDeviceUuid(int64_t authId, char *buf, uint32_t size);
278 
279 /**
280  * @brief Get a preferred auth connection info for p2p lane, priority order: WiFi > BR > BLE.
281  *
282  * @param udid peer device uuid.
283  * @param connInfo auth connection info {@link AuthConnInfo}.
284  * @return return SOFTBUS_OK if get successfully, otherwise return an error code.
285  */
286 int32_t AuthGetPreferConnInfo(const char *uuid, AuthConnInfo *connInfo);
287 
288 /**
289  * @brief Set p2p mac address info.
290  *
291  * @param authId id of auth connection.
292  * @param mac p2p mac address string.
293  * @return return SOFTBUS_OK if set successfully, otherwise return an error code.
294  */
295 int32_t AuthSetP2pMac(int64_t authId, const char *mac);
296 
297 /**
298  * @brief Get ConnectOption info by p2p mac.
299  *
300  * @param authId id of auth connection.
301  * @param mac p2p mac address string.
302  * @param option connect option info {@link ConnectOption}.
303  * @return return SOFTBUS_OK if get successfully, otherwise return an error code.
304  */
305 int32_t AuthGetConnectOptionByP2pMac(const char *mac, AuthLinkType type, ConnectOption *option);
306 
307 /**
308  * @brief Get ConnectOption info by uuid and ConnectType.
309  *
310  * @param uuid device uuid string.
311  * @param type connect type.
312  * @param option connect option info {@link ConnectOption}.
313  * @return return SOFTBUS_OK if get successfully, otherwise return an error code.
314  */
315 int32_t AuthGetActiveConnectOption(const char *uuid, ConnectType type, ConnectOption *option);
316 
317 /**
318  * @brief Get ble ConnectOption info by uuid.
319  *
320  * @param uuid device uuid string.
321  * @param isServerSide client or server.
322  * @param option connect option info {@link ConnectOption}.
323  * @return return SOFTBUS_OK if get successfully, otherwise return an error code.
324  */
325 int32_t AuthGetActiveBleConnectOption(const char *uuid, bool isServerSide, ConnectOption *option);
326 
327 #ifdef __cplusplus
328 }
329 #endif
330 #endif
331