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_MANAGER_H
17 #define AUTH_MANAGER_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #include "auth_common.h"
23 #include "auth_device.h"
24 #include "auth_interface.h"
25 #include "auth_lane.h"
26 #include "auth_normalize_request.h"
27 #include "auth_session_fsm.h"
28 #include "auth_session_key.h"
29 #include "common_list.h"
30 #include "lnn_lane_interface.h"
31 
32 #ifdef __cplusplus
33 #if __cplusplus
34 extern "C" {
35 #endif
36 #endif
37 
38 typedef struct {
39     int64_t authId;
40     bool isServer;
41     /* connInfo */
42     uint64_t connId[AUTH_LINK_TYPE_MAX];
43     AuthConnInfo connInfo[AUTH_LINK_TYPE_MAX];
44     uint64_t lastActiveTime;
45     /* sessionKeyInfo */
46     int64_t lastAuthSeq[AUTH_LINK_TYPE_MAX];
47     uint64_t lastVerifyTime;
48     SessionKeyList sessionKeyList;
49     /* deviceInfo */
50     char p2pMac[MAC_LEN];
51     char udid[UDID_BUF_LEN];
52     char uuid[UUID_BUF_LEN];
53     SoftBusVersion version;
54     /* authState */
55     bool hasAuthPassed[AUTH_LINK_TYPE_MAX];
56     ListNode node;
57 } AuthManager;
58 
59 typedef struct {
60     int32_t messageType;
61     ModeCycle cycle;
62 } DeviceMessageParse;
63 
64 int32_t AuthManagerSetSessionKey(int64_t authSeq, AuthSessionInfo *info, const SessionKey *sessionKey,
65     bool isConnect, bool isOldKey);
66 int32_t AuthManagerGetSessionKey(int64_t authSeq, const AuthSessionInfo *info, SessionKey *sessionKey);
67 
68 void AuthManagerSetAuthPassed(int64_t authSeq, const AuthSessionInfo *info);
69 void AuthManagerSetAuthFailed(int64_t authSeq, const AuthSessionInfo *info, int32_t reason);
70 void AuthManagerSetAuthFinished(int64_t authSeq, const AuthSessionInfo *info);
71 bool IsNeedAuthLimit(const char *udidHash);
72 void ClearAuthLimitMap(void);
73 
74 void AuthNotifyAuthPassed(int64_t authSeq, const AuthSessionInfo *info);
75 int32_t DelAuthReqInfoByAuthHandle(const AuthHandle *authHandle);
76 void AuthFreeLane(const AuthHandle *authHandle);
77 int32_t GetAuthLinkTypeList(const char *networkId, AuthLinkTypeList *linkTypeList);
78 int32_t GetAuthConn(const char *uuid, LaneLinkType laneType, AuthConnInfo *connInfo);
79 int32_t AuthAllocLane(const char *networkId, uint32_t authRequestId, AuthConnCallback *callback);
80 
81 /* Note: must call DelAuthManager to free. */
82 AuthManager *GetAuthManagerByAuthId(int64_t authId);
83 AuthManager *GetAuthManagerByConnInfo(const AuthConnInfo *connInfo, bool isServer);
84 void RemoveAuthSessionKeyByIndex(int64_t authId, int32_t index, AuthLinkType type);
85 void DelAuthManager(AuthManager *auth, int32_t type);
86 void DelDupAuthManager(AuthManager *auth);
87 void RemoveAuthManagerByAuthId(AuthHandle authHandle);
88 int32_t AuthDeviceGetPreferConnInfo(const char *uuid, AuthConnInfo *connInfo);
89 int32_t AuthDeviceGetConnInfoByType(const char *uuid, AuthLinkType type, AuthConnInfo *connInfo);
90 int32_t AuthDeviceGetP2pConnInfo(const char *uuid, AuthConnInfo *connInfo);
91 int32_t AuthDeviceGetHmlConnInfo(const char *uuid, AuthConnInfo *connInfo);
92 /*check whether AUTH device is exist or not*/
93 bool AuthDeviceCheckConnInfo(const char* uuid, AuthLinkType type, bool checkConnection);
94 
95 /* for ProxyChannel & P2P TcpDirectchannel */
96 void AuthDeviceGetLatestIdByUuid(const char *uuid, AuthLinkType type, AuthHandle *authHandle);
97 int64_t AuthDeviceGetIdByConnInfo(const AuthConnInfo *connInfo, bool isServer);
98 int64_t AuthDeviceGetIdByUuid(const char *uuid, AuthLinkType type, bool isServer);
99 int32_t AuthDeviceGetAuthHandleByIndex(const char *udid, bool isServer, int32_t index, AuthHandle *authHandle);
100 AuthManager *NewAuthManager(int64_t authSeq, const AuthSessionInfo *info);
101 int32_t AuthDeviceSetP2pMac(int64_t authId, const char *p2pMac);
102 
103 int32_t AuthDeviceInit(const AuthTransCallback *callback);
104 int32_t RegTrustListenerOnHichainSaStart(void);
105 int32_t GetHmlOrP2pAuthHandle(AuthHandle **authHandle, int32_t *num);
106 void AuthDeviceDeinit(void);
107 int64_t GetActiveAuthIdByConnInfo(const AuthConnInfo *connInfo, bool judgeTimeOut);
108 int64_t GetLatestIdByConnInfo(const AuthConnInfo *connInfo);
109 int32_t GetAuthConnInfoByUuid(const char *uuid, AuthLinkType type, AuthConnInfo *connInfo);
110 int32_t TryGetBrConnInfo(const char *uuid, AuthConnInfo *connInfo);
111 void RemoveNotPassedAuthManagerByUdid(const char *udid);
112 AuthManager *GetDeviceAuthManager(int64_t authSeq, const AuthSessionInfo *info, bool *isNewCreated,
113     int64_t lastAuthSeq);
114 
115 #ifdef __cplusplus
116 #if __cplusplus
117 }
118 #endif
119 #endif
120 #endif /* AUTH_MANAGER_H */
121