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 #include "lnn_node_info.h"
22 #include "softbus_common.h"
23 #include "softbus_conn_interface.h"
24 #include "softbus_def.h"
25 
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31 
32 #define AUTH_INVALID_ID (-1)
33 
34 #define AUTH_IDENTICAL_ACCOUNT_GROUP 1
35 #define AUTH_PEER_TO_PEER_GROUP 256
36 #define CUST_UDID_LEN 16
37 
38 typedef enum {
39     /* nearby type v1 */
40     SOFTBUS_OLD_V1 = 1,
41     /* nearby type v2 */
42     SOFTBUS_OLD_V2 = 2,
43     /* softbus type v1 */
44     SOFTBUS_NEW_V1 = 100,
45     /* softbus type v2 */
46     SOFTBUS_NEW_V2 = 101,
47 } SoftBusVersion;
48 
49 typedef enum {
50     AUTH_LINK_TYPE_WIFI = 1,
51     AUTH_LINK_TYPE_BR,
52     AUTH_LINK_TYPE_BLE,
53     AUTH_LINK_TYPE_P2P,
54     AUTH_LINK_TYPE_ENHANCED_P2P,
55     AUTH_LINK_TYPE_RAW_ENHANCED_P2P,
56     AUTH_LINK_TYPE_NORMALIZED,
57     AUTH_LINK_TYPE_MAX,
58 } AuthLinkType;
59 
60 typedef struct {
61     uint32_t linkTypeNum;
62     AuthLinkType linkType[AUTH_LINK_TYPE_MAX];
63 } AuthLinkTypeList;
64 
65 typedef enum {
66     AUTH_MODULE_LNN,
67     AUTH_MODULE_TRANS,
68     AUTH_MODULE_BUTT,
69 } AuthVerifyModule;
70 
71 typedef struct {
72     AuthLinkType type;
73     union {
74         struct {
75             char brMac[BT_MAC_LEN];
76             uint32_t connectionId;
77         } brInfo;
78         struct {
79             BleProtocolType protocol;
80             char bleMac[BT_MAC_LEN];
81             uint8_t deviceIdHash[UDID_HASH_LEN];
82             int32_t psm;
83         } bleInfo;
84         struct {
85             char ip[IP_LEN];
86             uint8_t deviceIdHash[UDID_HASH_LEN];
87             int32_t port;
88             int64_t authId; /* for open p2p auth conn */
89             ListenerModule moduleId; /* for open enhance p2p auth conn */
90             char udid[UDID_BUF_LEN];
91         } ipInfo;
92     } info;
93     char peerUid[MAX_ACCOUNT_HASH_LEN];
94 } AuthConnInfo;
95 
96 typedef enum {
97     ONLINE_HICHAIN = 0,
98     ONLINE_METANODE,
99     ONLINE_MIX,
100     AUTH_TYPE_BUTT,
101 } AuthType;
102 
103 typedef struct {
104     void (*onDeviceVerifyPass)(AuthHandle authHandle, const NodeInfo *info);
105     void (*onDeviceNotTrusted)(const char *peerUdid);
106     void (*onDeviceDisconnect)(AuthHandle authHandle);
107 } AuthVerifyListener;
108 int32_t RegAuthVerifyListener(const AuthVerifyListener *listener);
109 void UnregAuthVerifyListener(void);
110 
111 typedef struct {
112     void (*onVerifyPassed)(uint32_t requestId, AuthHandle authHandle, const NodeInfo *info);
113     void (*onVerifyFailed)(uint32_t requestId, int32_t reason);
114 } AuthVerifyCallback;
115 
116 typedef struct {
117     void (*onConnOpened)(uint32_t requestId, AuthHandle authHandle);
118     void (*onConnOpenFailed)(uint32_t requestId, int32_t reason);
119 } AuthConnCallback;
120 
121 typedef struct {
122     const uint8_t *key;
123     uint32_t keyLen;
124 } AuthKeyInfo;
125 
126 uint32_t AuthGenRequestId(void);
127 int32_t AuthStartVerify(const AuthConnInfo *connInfo, uint32_t requestId, const AuthVerifyCallback *verifyCallback,
128     AuthVerifyModule module, bool isFastAuth);
129 int32_t AuthStartConnVerify(const AuthConnInfo *connInfo, uint32_t requestId, const AuthConnCallback *connCallback,
130     AuthVerifyModule module, bool isFastAuth);
131 void AuthHandleLeaveLNN(AuthHandle authHandle);
132 int32_t AuthFlushDevice(const char *uuid);
133 int32_t AuthSendKeepaliveOption(const char *uuid, ModeCycle cycle);
134 
135 int32_t AuthMetaStartVerify(uint32_t connectionId, const AuthKeyInfo *authKeyInfo, uint32_t requestId,
136     int32_t callingPid, const AuthVerifyCallback *callBack);
137 void AuthMetaReleaseVerify(int64_t authId);
138 void AuthServerDeathCallback(const char *pkgName, int32_t pid);
139 
140 typedef struct {
141     void (*onGroupCreated)(const char *groupId, int32_t groupType);
142     void (*onGroupDeleted)(const char *groupId, int32_t groupType);
143     void (*onDeviceBound)(const char *udid, const char *groupInfo);
144 } GroupChangeListener;
145 
146 typedef enum {
147     TRUSTED_RELATION_IGNORE = 0,
148     TRUSTED_RELATION_NO,
149     TRUSTED_RELATION_YES,
150 } TrustedReturnType;
151 
152 int32_t RegGroupChangeListener(const GroupChangeListener *listener);
153 void UnregGroupChangeListener(void);
154 
155 TrustedReturnType AuthHasTrustedRelation(void);
156 bool AuthIsPotentialTrusted(const DeviceInfo *device);
157 bool IsAuthHasTrustedRelation(void);
158 bool IsSameAccountDevice(const DeviceInfo *device);
159 bool AuthHasSameAccountGroup(void);
160 
161 int32_t AuthStartListening(AuthLinkType type, const char *ip, int32_t port);
162 void AuthStopListening(AuthLinkType type);
163 
164 int32_t AuthStartListeningForWifiDirect(AuthLinkType type, const char *ip, int32_t port, ListenerModule *moduleId);
165 void AuthStopListeningForWifiDirect(AuthLinkType type, ListenerModule moduleId);
166 
167 typedef struct {
168     int32_t module;
169     int32_t flag;
170     int64_t seq;
171     uint32_t len;
172     const uint8_t *data;
173 } AuthTransData;
174 
175 typedef struct {
176     void (*onDataReceived)(AuthHandle authHandle, const AuthTransData *data);
177     void (*onDisconnected)(AuthHandle authHandle);
178     void (*onException)(AuthHandle authHandle, int32_t error);
179 } AuthTransListener;
180 int32_t RegAuthTransListener(int32_t module, const AuthTransListener *listener);
181 void UnregAuthTransListener(int32_t module);
182 
183 int32_t AuthOpenConn(const AuthConnInfo *info, uint32_t requestId, const AuthConnCallback *callback, bool isMeta);
184 int32_t AuthPostTransData(AuthHandle authHandle, const AuthTransData *dataInfo);
185 void AuthCloseConn(AuthHandle authHandle);
186 int32_t AuthGetPreferConnInfo(const char *uuid, AuthConnInfo *connInfo, bool isMeta);
187 int32_t AuthGetConnInfoByType(const char *uuid, AuthLinkType type, AuthConnInfo *connInfo, bool isMeta);
188 int32_t AuthGetP2pConnInfo(const char *uuid, AuthConnInfo *connInfo, bool isMeta);
189 int32_t AuthGetHmlConnInfo(const char *uuid, AuthConnInfo *connInfo, bool isMeta);
190 int32_t AuthGetLatestAuthSeqList(const char *udid, int64_t *seqList, uint32_t num);
191 int32_t AuthGetLatestAuthSeqListByType(const char *udid, int64_t *seqList, uint64_t *authVerifyTime,
192     DiscoveryType type);
193 /* for ProxyChannel & P2P TcpDirectchannel */
194 void AuthGetLatestIdByUuid(const char *uuid, AuthLinkType type, bool isMeta, AuthHandle *authHandle);
195 int32_t AuthGetAuthHandleByIndex(const AuthConnInfo *connInfo, bool isServer, int32_t index, AuthHandle *authHandle);
196 int64_t AuthGetIdByConnInfo(const AuthConnInfo *connInfo, bool isServer, bool isMeta);
197 int64_t AuthGetIdByUuid(const char *uuid, AuthLinkType type, bool isServer, bool isMeta);
198 
199 uint32_t AuthGetEncryptSize(int64_t authId, uint32_t inLen);
200 uint32_t AuthGetDecryptSize(uint32_t inLen);
201 int32_t AuthEncrypt(AuthHandle *authHandle, const uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen);
202 int32_t AuthDecrypt(AuthHandle *authHandle, const uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen);
203 int32_t AuthSetP2pMac(int64_t authId, const char *p2pMac);
204 
205 int32_t AuthGetConnInfo(AuthHandle authHandle, AuthConnInfo *connInfo);
206 int32_t AuthGetServerSide(int64_t authId, bool *isServer);
207 int32_t AuthGetDeviceUuid(int64_t authId, char *uuid, uint16_t size);
208 int32_t AuthGetVersion(int64_t authId, SoftBusVersion *version);
209 int32_t AuthGetMetaType(int64_t authId, bool *isMetaAuth);
210 uint32_t AuthGetGroupType(const char *udid, const char *uuid);
211 bool IsSupportFeatureByCapaBit(uint32_t feature, AuthCapability capaBit);
212 
213 int32_t AuthAllocConn(const char *networkId, uint32_t authRequestId, AuthConnCallback *callback);
214 void AuthFreeConn(const AuthHandle *authHandle);
215 
216 int32_t AuthCheckSessionKeyValidByConnInfo(const char *networkId, const AuthConnInfo *connInfo);
217 int32_t AuthCheckSessionKeyValidByAuthHandle(const AuthHandle *authHandle);
218 int32_t AuthInit(void);
219 void AuthDeinit(void);
220 int32_t AuthRestoreAuthManager(const char *udidHash,
221     const AuthConnInfo *connInfo, uint32_t requestId, NodeInfo *nodeInfo, int64_t *authId);
222 int32_t AuthCheckMetaExist(const AuthConnInfo *connInfo, bool *isExist);
223 
224 #ifdef __cplusplus
225 #if __cplusplus
226 }
227 #endif
228 #endif
229 #endif /* AUTH_INTERFACE_H */
230