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