1 /* 2 * Copyright (c) 2022 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_COMMON_H 17 #define AUTH_COMMON_H 18 19 #include "auth_interface.h" 20 #include "lnn_device_info_recovery.h" 21 #include "softbus_utils.h" 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 29 typedef enum { 30 /* data type for device authentication */ 31 DATA_TYPE_AUTH = 0xFFFF0001, 32 /* data type for synchronizing peer device information */ 33 DATA_TYPE_DEVICE_INFO = 0xFFFF0002, 34 /* data type for synchronizing peer device id */ 35 DATA_TYPE_DEVICE_ID = 0xFFFF0003, 36 /* data type for connection */ 37 DATA_TYPE_CONNECTION = 0xFFFF0004, 38 /* data type for closing ack */ 39 DATA_TYPE_CLOSE_ACK = 0xFFFF0005, 40 /* data type for meta negotiation */ 41 DATA_TYPE_META_NEGOTIATION = 0xFFFF0006, 42 /* data type for decrypt fail */ 43 DATA_TYPE_DECRYPT_FAIL = 0xFFFF0007, 44 /* data type for info ack */ 45 DATA_TYPE_META_DEVICE_INFO_ACK = 0xFFFF0008, 46 /* data type for cancel auth */ 47 DATA_TYPE_CANCEL_AUTH = 0xFFFF0009, 48 /* data type for testing reuse dm sessionkey*/ 49 DATA_TYPE_TEST_AUTH = 0xFFFF0010, 50 } AuthDataType; 51 52 #define CLIENT_SIDE_FLAG 0 53 #define SERVER_SIDE_FLAG 1 54 55 #define CHECK_NULL_PTR_RETURN_VOID(item) \ 56 if ((item) == NULL) { \ 57 return; \ 58 } 59 60 #define CHECK_NULL_PTR_RETURN_VALUE(item, value) \ 61 if ((item) == NULL) { \ 62 return value; \ 63 } 64 65 #define CHECK_EXPRESSION_RETURN_VOID(expression) \ 66 if (expression) { \ 67 return; \ 68 } 69 70 #define CHECK_EXPRESSION_RETURN_VALUE(expression, value) \ 71 if (expression) { \ 72 return value; \ 73 } 74 75 #define SEQ_INTERVAL 2 76 #define BYTES_BIT_NUM 8 77 #define INT32_BIT_NUM 32 78 #define INT32_MASK 0xFFFFFFFF 79 #define MASK_UINT64_L32 0x00000000FFFFFFFF 80 #define MASK_UINT64_H32 0xFFFFFFFF00000000 81 #define AUTH_REQUEST_TIMTOUR 30000 82 /* ble network advdata take 8 bytes of UDID hash */ 83 #define SHORT_HASH_LEN 8 84 85 #define SOFTBUS_SUB_SYSTEM 203 86 #define SOFTBUS_AUTH_MODULE 3 87 #define SOFTBUS_HICHAIN_MAX (-((SOFTBUS_SUB_SYSTEM << 21) | (SOFTBUS_AUTH_MODULE << 16) | 0x0001)) 88 #define SOFTBUS_HICHAIN_MIN (-((SOFTBUS_SUB_SYSTEM << 21) | (SOFTBUS_AUTH_MODULE << 16) | 0x10FF)) 89 90 #define TO_INT32(value) ((int32_t)(((uint32_t)(value)) & INT32_MASK)) 91 #define TO_UINT32(value) ((uint32_t)(((uint32_t)(value)) & INT32_MASK)) 92 93 typedef struct { 94 uint32_t dataType; 95 int32_t module; 96 int64_t seq; 97 int32_t flag; 98 uint32_t len; 99 } AuthDataHead; 100 101 typedef struct { 102 int32_t magic; 103 int32_t module; 104 int64_t seq; 105 int32_t flag; 106 uint32_t len; 107 } SocketPktHead; 108 109 typedef struct { 110 void (*onDataReceived)(AuthHandle authHandle, const AuthDataHead *head, const uint8_t *data, uint32_t len); 111 void (*onDisconnected)(AuthHandle authHandle); 112 void (*onException)(AuthHandle authHandle, int32_t error); 113 } AuthTransCallback; 114 115 /* Auth handler */ 116 typedef enum { 117 EVENT_CONNECT_CMD, 118 EVENT_CONNECT_RESULT, 119 EVENT_CONNECT_TIMEOUT, 120 EVENT_UPDATE_SESSION_KEY, 121 EVENT_AUTH_META_TIMEOUT, 122 EVENT_AUTH_DISCONNECT, 123 EVENT_BLE_DISCONNECT_DELAY, 124 EVENT_AUTH_META_SYNC_PTK_TIMEOUT, 125 } EventType; 126 typedef void(*EventHandler)(const void *obj); 127 int32_t PostAuthEvent(EventType event, EventHandler handler, 128 const void *obj, uint32_t size, uint64_t delayMs); 129 typedef int(*RemoveCompareFunc)(const void *obj, void *param); 130 int32_t RemoveAuthEvent(EventType event, RemoveCompareFunc func, void *param); 131 132 /* Auth Lock */ 133 bool RequireAuthLock(void); 134 void ReleaseAuthLock(void); 135 136 /* auth config */ 137 bool GetConfigSupportAsServer(void); 138 139 /* auth capacity */ 140 uint32_t GetAuthCapacity(void); 141 142 /* Common Functions */ 143 uint8_t *DupMemBuffer(const uint8_t *buf, uint32_t size); 144 int64_t GenSeq(bool isServer); 145 uint64_t GetCurrentTimeMs(void); 146 const char *GetAuthSideStr(bool isServer); 147 bool CompareConnInfo(const AuthConnInfo *info1, const AuthConnInfo *info2, bool cmpShortHash); 148 int32_t ConvertToConnectOption(const AuthConnInfo *connInfo, ConnectOption *option); 149 int32_t ConvertToAuthConnInfo(const ConnectionInfo *info, AuthConnInfo *connInfo); 150 int32_t GetPeerUdidByNetworkId(const char *networkId, char *udid, uint32_t len); 151 int32_t GetIsExchangeUdidByNetworkId(const char *networkId, bool *isExchangeUdid); 152 DiscoveryType ConvertToDiscoveryType(AuthLinkType type); 153 AuthLinkType ConvertToAuthLinkType(DiscoveryType type); 154 bool CheckAuthConnInfoType(const AuthConnInfo *connInfo); 155 void PrintAuthConnInfo(const AuthConnInfo *connInfo); 156 157 int32_t AuthCommonInit(void); 158 void AuthCommonDeinit(void); 159 160 #ifdef __cplusplus 161 #if __cplusplus 162 } 163 #endif 164 #endif 165 #endif /* AUTH_COMMON_H */ 166