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 37 typedef enum { 38 /* nearby type v1 */ 39 SOFTBUS_OLD_V1 = 1, 40 /* nearby type v2 */ 41 SOFTBUS_OLD_V2 = 2, 42 /* softbus type v1 */ 43 SOFTBUS_NEW_V1 = 100, 44 /* softbus type v2 */ 45 SOFTBUS_NEW_V2 = 101, 46 } SoftBusVersion; 47 48 typedef enum { 49 AUTH_LINK_TYPE_WIFI = 1, 50 AUTH_LINK_TYPE_BR, 51 AUTH_LINK_TYPE_BLE, 52 AUTH_LINK_TYPE_P2P, 53 AUTH_LINK_TYPE_ENHANCED_P2P, 54 AUTH_LINK_TYPE_MAX, 55 } AuthLinkType; 56 57 typedef struct { 58 AuthLinkType type; 59 union { 60 struct { 61 char brMac[BT_MAC_LEN]; 62 } brInfo; 63 struct { 64 BleProtocolType protocol; 65 char bleMac[BT_MAC_LEN]; 66 uint8_t deviceIdHash[UDID_HASH_LEN]; 67 int32_t psm; 68 } bleInfo; 69 struct { 70 char ip[IP_LEN]; 71 int32_t port; 72 int64_t authId; /* for open p2p auth conn */ 73 ListenerModule moduleId; /* for open enhance p2p auth conn */ 74 char udid[UDID_BUF_LEN]; 75 } ipInfo; 76 } info; 77 char peerUid[MAX_ACCOUNT_HASH_LEN]; 78 } AuthConnInfo; 79 80 typedef enum { 81 ONLINE_HICHAIN = 0, 82 ONLINE_METANODE, 83 ONLINE_MIX, 84 85 AUTH_TYPE_BUTT, 86 } AuthType; 87 88 typedef struct { 89 void (*onDeviceVerifyPass)(int64_t authId, const NodeInfo *info); 90 void (*onDeviceNotTrusted)(const char *peerUdid); 91 void (*onDeviceDisconnect)(int64_t authId); 92 } AuthVerifyListener; 93 int32_t RegAuthVerifyListener(const AuthVerifyListener *listener); 94 void UnregAuthVerifyListener(void); 95 96 typedef struct { 97 void (*onVerifyPassed)(uint32_t requestId, int64_t authId, const NodeInfo *info); 98 void (*onVerifyFailed)(uint32_t requestId, int32_t reason); 99 } AuthVerifyCallback; 100 101 uint32_t AuthGenRequestId(void); 102 int32_t AuthStartVerify(const AuthConnInfo *connInfo, uint32_t requestId, 103 const AuthVerifyCallback *callback, bool isFastAuth); 104 void AuthHandleLeaveLNN(int64_t authId); 105 int32_t AuthFlushDevice(const char *uuid); 106 107 int32_t AuthMetaStartVerify(uint32_t connectionId, const uint8_t *key, uint32_t keyLen, 108 uint32_t requestId, int32_t callingPid, const AuthVerifyCallback *callBack); 109 void AuthMetaReleaseVerify(int64_t authId); 110 void AuthServerDeathCallback(const char *pkgName, int32_t pid); 111 112 typedef struct { 113 void (*onGroupCreated)(const char *groupId, int32_t groupType); 114 void (*onGroupDeleted)(const char *groupId); 115 void (*onDeviceBound)(const char *udid, const char *groupInfo); 116 } GroupChangeListener; 117 118 typedef enum { 119 TRUSTED_RELATION_IGNORE = 0, 120 TRUSTED_RELATION_NO, 121 TRUSTED_RELATION_YES, 122 } TrustedReturnType; 123 124 int32_t RegGroupChangeListener(const GroupChangeListener *listener); 125 void UnregGroupChangeListener(void); 126 127 TrustedReturnType AuthHasTrustedRelation(void); 128 bool AuthIsPotentialTrusted(const DeviceInfo *device); 129 bool IsAuthHasTrustedRelation(void); 130 131 int32_t AuthStartListening(AuthLinkType type, const char *ip, int32_t port); 132 void AuthStopListening(AuthLinkType type); 133 134 int32_t AuthStartListeningForWifiDirect(AuthLinkType type, const char *ip, int32_t port, ListenerModule *moduleId); 135 void AuthStopListeningForWifiDirect(AuthLinkType type, ListenerModule moduleId); 136 137 typedef struct { 138 int32_t module; 139 int32_t flag; 140 int64_t seq; 141 uint32_t len; 142 const uint8_t *data; 143 } AuthTransData; 144 145 typedef struct { 146 void (*onDataReceived)(int64_t authId, const AuthTransData *data); 147 void (*onDisconnected)(int64_t authId); 148 } AuthTransListener; 149 int32_t RegAuthTransListener(int32_t module, const AuthTransListener *listener); 150 void UnregAuthTransListener(int32_t module); 151 152 typedef struct { 153 void (*onConnOpened)(uint32_t requestId, int64_t authId); 154 void (*onConnOpenFailed)(uint32_t requestId, int32_t reason); 155 } AuthConnCallback; 156 int32_t AuthOpenConn(const AuthConnInfo *info, uint32_t requestId, const AuthConnCallback *callback, bool isMeta); 157 int32_t AuthPostTransData(int64_t authId, const AuthTransData *dataInfo); 158 void AuthCloseConn(int64_t authId); 159 int32_t AuthGetPreferConnInfo(const char *uuid, AuthConnInfo *connInfo, bool isMeta); 160 int32_t AuthGetP2pConnInfo(const char *uuid, AuthConnInfo *connInfo, bool isMeta); 161 void AuthDeleteStoredAuthKey(const char *udid, int32_t discoveryType); 162 int32_t AuthGetLatestAuthSeqList(const char *udid, int64_t *seqList, uint32_t num); 163 void AuthDeleteStoredAuthKey(const char *udid, int32_t discoveryType); 164 165 /* for ProxyChannel & P2P TcpDirectchannel */ 166 int64_t AuthGetLatestIdByUuid(const char *uuid, AuthLinkType type, bool isMeta); 167 int64_t AuthGetIdByConnInfo(const AuthConnInfo *connInfo, bool isServer, bool isMeta); 168 int64_t AuthGetIdByUuid(const char *uuid, AuthLinkType type, bool isServer, bool isMeta); 169 170 uint32_t AuthGetEncryptSize(uint32_t inLen); 171 uint32_t AuthGetDecryptSize(uint32_t inLen); 172 int32_t AuthEncrypt(int64_t authId, const uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen); 173 int32_t AuthDecrypt(int64_t authId, const uint8_t *inData, uint32_t inLen, uint8_t *outData, uint32_t *outLen); 174 175 int32_t AuthSetP2pMac(int64_t authId, const char *p2pMac); 176 177 int32_t AuthGetConnInfo(int64_t authId, AuthConnInfo *connInfo); 178 int32_t AuthGetServerSide(int64_t authId, bool *isServer); 179 int32_t AuthGetDeviceUuid(int64_t authId, char *uuid, uint16_t size); 180 int32_t AuthGetVersion(int64_t authId, SoftBusVersion *version); 181 int32_t AuthGetMetaType(int64_t authId, bool *isMetaAuth); 182 int32_t AuthGetGroupType(const char *udid, const char *uuid); 183 184 int32_t AuthInit(void); 185 void AuthDeinit(void); 186 int32_t AuthRestoreAuthManager(const char *udidHash, 187 const AuthConnInfo *connInfo, uint32_t requestId, NodeInfo *nodeInfo, int64_t *authId); 188 189 #ifdef __cplusplus 190 #if __cplusplus 191 } 192 #endif 193 #endif 194 #endif /* AUTH_INTERFACE_H */