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_SESSION_H
17 #define AUTH_SESSION_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #include "auth_common.h"
23 #include "auth_interface.h"
24 #include "auth_session_key.h"
25 #include "auth_device_common_key.h"
26 #include "common_list.h"
27 #include "lnn_node_info.h"
28 #include "lnn_p2p_info.h"
29 #include "lnn_state_machine.h"
30 #include "softbus_hisysevt_bus_center.h"
31 
32 #ifdef __cplusplus
33 #if __cplusplus
34 extern "C" {
35 #endif
36 #endif
37 
38 #define AUTH_FSM_NAME_LEN 32
39 
40 typedef enum {
41     STATE_SYNC_NEGOTIATION,
42     STATE_SYNC_DEVICE_ID,
43     STATE_DEVICE_AUTH,
44     STATE_SYNC_DEVICE_INFO,
45     STATE_NUM_MAX
46 } AuthFsmStateIndex;
47 
48 typedef enum {
49     EXCHANGE_UDID = 0,
50     EXCHANGE_NETWORKID,
51     EXCHANGE_FAIL,
52     EXCHANGE_TYPE_MAX
53 } ExchangeDataType;
54 
55 typedef enum {
56     NORMALIZED_NOT_SUPPORT,
57     NORMALIZED_KEY_ERROR,
58     NORMALIZED_SUPPORT,
59 } NormalizedType;
60 
61 typedef enum {
62     AUTH_STATE_WAIT = 1,
63     AUTH_STATE_START,
64     AUTH_STATE_UNKNOW,
65     AUTH_STATE_ACK,
66     AUTH_STATE_COMPATIBLE,
67 } AuthStartState;
68 
69 typedef struct {
70     uint32_t requestId;
71     bool isServer;
72     bool isConnectServer;
73     uint64_t connId;
74     AuthConnInfo connInfo;
75     uint8_t *deviceInfoData;
76     uint32_t deviceInfoDataLen;
77     NodeInfo nodeInfo;
78     bool isNodeInfoReceived;
79     bool isCloseAckReceived;
80     bool isAuthFinished;
81     char udid[UDID_BUF_LEN];
82     char uuid[UUID_BUF_LEN];
83     char udidHash[SHA_256_HEX_HASH_LEN];
84     SoftBusVersion version;
85     bool isSupportCompress;
86     bool isSupportFastAuth;
87     bool isNeedFastAuth;
88     int64_t oldIndex;
89     int32_t idType;
90     bool isNeedPackCert;
91     uint64_t sessionKeyRandomNum;
92     AuthVerifyModule module;
93     NormalizedType normalizedType;
94     SessionKey *normalizedKey;
95     int64_t normalizedIndex;
96     bool isOldKey;
97     bool isSavedSessionKey;
98     AuthStartState localState;
99     AuthStartState peerState;
100 } AuthSessionInfo;
101 
102 typedef struct {
103     ListNode node;
104     uint32_t id;
105     int64_t authSeq;
106     char fsmName[AUTH_FSM_NAME_LEN];
107     FsmStateMachine fsm;
108     AuthSessionInfo info;
109     AuthFsmStateIndex curState;
110     AuthStatisticData statisticData;
111     bool isDead;
112 } AuthFsm;
113 
114 typedef struct {
115     int64_t authSeq;
116     uint32_t requestId;
117     uint64_t connId;
118     bool isServer;
119     bool isFastAuth;
120 } AuthParam;
121 
122 int32_t AuthSessionStartAuth(const AuthParam *authParam, const AuthConnInfo *connInfo);
123 int32_t AuthSessionProcessDevIdData(int64_t authSeq, const uint8_t *data, uint32_t len);
124 int32_t AuthSessionPostAuthData(int64_t authSeq, const uint8_t *data, uint32_t len);
125 int32_t AuthSessionProcessAuthData(int64_t authSeq, const uint8_t *data, uint32_t len);
126 int32_t AuthSessionGetUdid(int64_t authSeq, char *udid, uint32_t size);
127 int32_t AuthSessionSaveSessionKey(int64_t authSeq, const uint8_t *key, uint32_t len);
128 int32_t AuthSessionHandleAuthFinish(int64_t authSeq);
129 int32_t AuthSessionHandleAuthError(int64_t authSeq, int32_t reason);
130 int32_t AuthSessionProcessDevInfoData(int64_t authSeq, const uint8_t *data, uint32_t len);
131 int32_t AuthSessionProcessCloseAck(int64_t authSeq, const uint8_t *data, uint32_t len);
132 int32_t AuthSessionProcessDevInfoDataByConnId(uint64_t connId, bool isServer, const uint8_t *data, uint32_t len);
133 int32_t AuthSessionProcessCloseAckByConnId(uint64_t connId, bool isServer, const uint8_t *data, uint32_t len);
134 int32_t AuthSessionProcessCancelAuthByConnId(uint64_t connId, bool isConnectServer, const uint8_t *data, uint32_t len);
135 int32_t AuthSessionHandleDeviceNotTrusted(const char *udid);
136 int32_t AuthSessionHandleDeviceDisconnected(uint64_t connId);
137 AuthFsm *GetAuthFsmByAuthSeq(int64_t authSeq);
138 AuthFsm *GetAuthFsmByConnId(uint64_t connId, bool isServer, bool isConnectSide);
139 int32_t AuthNotifyRequestVerify(int64_t authSeq);
140 void AuthSessionFsmExit(void);
141 
142 #ifdef __cplusplus
143 #if __cplusplus
144 }
145 #endif
146 #endif
147 #endif /* AUTH_SESSION_H */
148