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 #define SHORT_HASH_LEN 8
87 
88 #define SOFTBUS_SUB_SYSTEM 203
89 #define SOFTBUS_AUTH_MODULE 3
90 #define SOFTBUS_HICHAIN_MAX (-((SOFTBUS_SUB_SYSTEM << 21) | (SOFTBUS_AUTH_MODULE << 16) | 0x0001))
91 #define SOFTBUS_HICHAIN_MIN (-((SOFTBUS_SUB_SYSTEM << 21) | (SOFTBUS_AUTH_MODULE << 16) | 0x10FF))
92 
93 #define TO_INT32(value) ((int32_t)(((uint32_t)(value)) & INT32_MASK))
94 #define TO_UINT32(value) ((uint32_t)(((uint32_t)(value)) & INT32_MASK))
95 
96 typedef struct {
97     uint32_t dataType;
98     int32_t module;
99     int64_t seq;
100     int32_t flag;
101     uint32_t len;
102 } AuthDataHead;
103 
104 typedef struct {
105     int32_t magic;
106     int32_t module;
107     int64_t seq;
108     int32_t flag;
109     uint32_t len;
110 } SocketPktHead;
111 
112 typedef struct {
113     void (*onDataReceived)(AuthHandle authHandle, const AuthDataHead *head, const uint8_t *data, uint32_t len);
114     void (*onDisconnected)(AuthHandle authHandle);
115     void (*onException)(AuthHandle authHandle, int32_t error);
116 } AuthTransCallback;
117 
118 /* Auth handler */
119 typedef enum {
120     EVENT_CONNECT_CMD,
121     EVENT_CONNECT_RESULT,
122     EVENT_CONNECT_TIMEOUT,
123     EVENT_UPDATE_SESSION_KEY,
124     EVENT_AUTH_META_TIMEOUT,
125     EVENT_AUTH_DISCONNECT,
126     EVENT_BLE_DISCONNECT_DELAY,
127     EVENT_AUTH_META_SYNC_PTK_TIMEOUT,
128 } EventType;
129 typedef void(*EventHandler)(const void *obj);
130 int32_t PostAuthEvent(EventType event, EventHandler handler,
131     const void *obj, uint32_t size, uint64_t delayMs);
132 typedef int(*RemoveCompareFunc)(const void *obj, void *param);
133 int32_t RemoveAuthEvent(EventType event, RemoveCompareFunc func, void *param);
134 
135 /* Auth Lock */
136 bool RequireAuthLock(void);
137 void ReleaseAuthLock(void);
138 
139 /* auth config */
140 bool GetConfigSupportAsServer(void);
141 
142 /* auth capacity */
143 uint32_t GetAuthCapacity(void);
144 
145 /* Common Functions */
146 uint8_t *DupMemBuffer(const uint8_t *buf, uint32_t size);
147 int64_t GenSeq(bool isServer);
148 uint64_t GetCurrentTimeMs(void);
149 const char *GetAuthSideStr(bool isServer);
150 bool CompareConnInfo(const AuthConnInfo *info1, const AuthConnInfo *info2, bool cmpShortHash);
151 int32_t ConvertToConnectOption(const AuthConnInfo *connInfo, ConnectOption *option);
152 int32_t ConvertToAuthConnInfo(const ConnectionInfo *info, AuthConnInfo *connInfo);
153 int32_t GetPeerUdidByNetworkId(const char *networkId, char *udid, uint32_t len);
154 int32_t GetIsExchangeUdidByNetworkId(const char *networkId, bool *isExchangeUdid);
155 DiscoveryType ConvertToDiscoveryType(AuthLinkType type);
156 AuthLinkType ConvertToAuthLinkType(DiscoveryType type);
157 bool CheckAuthConnInfoType(const AuthConnInfo *connInfo);
158 void PrintAuthConnInfo(const AuthConnInfo *connInfo);
159 
160 int32_t AuthCommonInit(void);
161 void AuthCommonDeinit(void);
162 
163 #ifdef __cplusplus
164 #if __cplusplus
165 }
166 #endif
167 #endif
168 #endif /* AUTH_COMMON_H */
169