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