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 <stdint.h> 20 #include <stdbool.h> 21 22 #include "auth_interface.h" 23 #include "lnn_device_info_recovery.h" 24 #include "softbus_common.h" 25 #include "softbus_conn_interface.h" 26 #include "softbus_errcode.h" 27 #include "softbus_log.h" 28 #include "softbus_utils.h" 29 30 #ifdef __cplusplus 31 #if __cplusplus 32 extern "C" { 33 #endif 34 #endif 35 36 typedef enum { 37 /* data type for device authentication */ 38 DATA_TYPE_AUTH = 0xFFFF0001, 39 /* data type for synchronizing peer device information */ 40 DATA_TYPE_DEVICE_INFO = 0xFFFF0002, 41 /* data type for synchronizing peer device id */ 42 DATA_TYPE_DEVICE_ID = 0xFFFF0003, 43 /* data type for connection */ 44 DATA_TYPE_CONNECTION = 0xFFFF0004, 45 /* data type for closing ack */ 46 DATA_TYPE_CLOSE_ACK = 0xFFFF0005, 47 /* data type for meta negotiation */ 48 DATA_TYPE_META_NEGOTIATION = 0xFFFF0006, 49 } AuthDataType; 50 51 #define CLIENT_SIDE_FLAG 0 52 #define SERVER_SIDE_FLAG 1 53 54 #define CHECK_NULL_PTR_RETURN_VOID(item) \ 55 if ((item) == NULL) { \ 56 return; \ 57 } 58 59 #define CHECK_NULL_PTR_RETURN_VALUE(item, value) \ 60 if ((item) == NULL) { \ 61 return value; \ 62 } 63 64 #define CHECK_EXPRESSION_RETURN_VOID(expression) \ 65 if (expression) { \ 66 return; \ 67 } 68 69 #define CHECK_EXPRESSION_RETURN_VALUE(expression, value) \ 70 if (expression) { \ 71 return value; \ 72 } 73 74 #define SEQ_INTERVAL 2 75 #define BYTES_BIT_NUM 8 76 #define INT32_BIT_NUM 32 77 #define INT32_MASK 0xFFFFFFFF 78 #define MASK_UINT64_L32 0x00000000FFFFFFFF 79 #define MASK_UINT64_H32 0xFFFFFFFF00000000 80 81 #define TO_INT32(value) ((int32_t)(((uint32_t)(value)) & INT32_MASK)) 82 #define TO_UINT32(value) ((uint32_t)(((uint32_t)(value)) & INT32_MASK)) 83 84 typedef struct { 85 uint32_t dataType; 86 int32_t module; 87 int64_t seq; 88 int32_t flag; 89 uint32_t len; 90 } AuthDataHead; 91 92 typedef struct { 93 void (*OnDataReceived)(int64_t authId, const AuthDataHead *head, const uint8_t *data, uint32_t len); 94 void (*OnDisconnected)(int64_t authId); 95 } AuthTransCallback; 96 97 /* Auth handler */ 98 typedef enum { 99 EVENT_CONNECT_CMD, 100 EVENT_CONNECT_RESULT, 101 EVENT_CONNECT_TIMEOUT, 102 EVENT_UPDATE_SESSION_KEY, 103 EVENT_AUTH_META_TIMEOUT, 104 EVENT_AUTH_DISCONNECT, 105 EVENT_BLE_DISCONNECT_DELAY, 106 } EventType; 107 typedef void(*EventHandler)(const void *obj); 108 int32_t PostAuthEvent(EventType event, EventHandler handler, 109 const void *obj, uint32_t size, uint64_t delayMs); 110 typedef int(*RemoveCompareFunc)(const void *obj, void *param); 111 int32_t RemoveAuthEvent(EventType event, RemoveCompareFunc func, void *param); 112 113 /* Auth Lock */ 114 bool RequireAuthLock(void); 115 void ReleaseAuthLock(void); 116 117 /* auth config */ 118 bool GetConfigSupportAsServer(void); 119 120 /* Common Functions */ 121 uint8_t *DupMemBuffer(const uint8_t *buf, uint32_t size); 122 int64_t GenSeq(bool isServer); 123 uint64_t GetCurrentTimeMs(void); 124 const char *GetAuthSideStr(bool isServer); 125 bool CompareConnInfo(const AuthConnInfo *info1, const AuthConnInfo *info2, bool cmpShortHash); 126 int32_t ConvertToConnectOption(const AuthConnInfo *connInfo, ConnectOption *option); 127 int32_t ConvertToAuthConnInfo(const ConnectionInfo *info, AuthConnInfo *connInfo); 128 int32_t GetPeerUdidByNetworkId(const char *networkId, char *udidHash); 129 130 int32_t AuthCommonInit(void); 131 void AuthCommonDeinit(void); 132 133 #ifdef __cplusplus 134 #if __cplusplus 135 } 136 #endif 137 #endif 138 #endif /* AUTH_COMMON_H */ 139