1 /*
2  * Copyright (c) 2021-2024 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 SOFTBUS_PROXYCHANNEL_MESSAGE_H
17 #define SOFTBUS_PROXYCHANNEL_MESSAGE_H
18 #include "stdint.h"
19 #include "common_list.h"
20 #include "softbus_app_info.h"
21 #include "softbus_conn_interface.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 typedef enum {
30     PROXYCHANNEL_MSG_TYPE_NORMAL,
31     PROXYCHANNEL_MSG_TYPE_HANDSHAKE,
32     PROXYCHANNEL_MSG_TYPE_HANDSHAKE_ACK,
33     PROXYCHANNEL_MSG_TYPE_RESET,
34     PROXYCHANNEL_MSG_TYPE_KEEPALIVE,
35     PROXYCHANNEL_MSG_TYPE_KEEPALIVE_ACK,
36     PROXYCHANNEL_MSG_TYPE_HANDSHAKE_AUTH,
37 
38     PROXYCHANNEL_MSG_TYPE_MAX
39 } MsgType;
40 
41 #define JSON_KEY_TYPE "TYPE"
42 #define JSON_KEY_IDENTITY "IDENTITY"
43 #define JSON_KEY_DEVICE_ID "DEVICE_ID"
44 #define JSON_KEY_MTU_SIZE "MTU_SIZE"
45 #define JSON_KEY_DST_BUS_NAME "DST_BUS_NAME"
46 #define JSON_KEY_SRC_BUS_NAME "SRC_BUS_NAME"
47 #define JSON_KEY_HAS_PRIORITY "HAS_PRIORITY"
48 #define JSON_KEY_UID "UID"
49 #define JSON_KEY_PID "PID"
50 #define JSON_KEY_GROUP_ID "GROUP_ID"
51 #define JSON_KEY_PKG_NAME "PKG_NAME"
52 #define JSON_KEY_SESSION_KEY "SESSION_KEY"
53 #define JSON_KEY_REQUEST_ID "REQUEST_ID"
54 #define JSON_KEY_ENCRYPT "ENCRYPT"
55 #define JSON_KEY_ALGORITHM "ALGORITHM"
56 #define JSON_KEY_CRC "CRC"
57 #define JSON_KEY_BUSINESS_TYPE "BUSINESS_TYPE"
58 #define JSON_KEY_TRANS_FLAGS "TRANS_FLAGS"
59 #define JSON_KEY_MIGRATE_OPTION "MIGRATE_OPTION"
60 #define JSON_KEY_MY_HANDLE_ID "MY_HANDLE_ID"
61 #define JSON_KEY_PEER_HANDLE_ID "PEER_HANDLE_ID"
62 #define JSON_KEY_AUTH_SEQ "AUTH_SEQ"
63 #define JSON_KEY_ROUTE_TYPE "ROUTE_TYPE"
64 #define JSON_KEY_FIRST_DATA "FIRST_DATA"
65 #define JSON_KEY_FIRST_DATA_SIZE "FIRST_DATA_SIZE"
66 #define JSON_KEY_CALLING_TOKEN_ID "CALLING_TOKEN_ID"
67 
68 typedef struct {
69     uint8_t type; // MsgType
70     uint8_t cipher;
71     int16_t myId;
72     int16_t peerId;
73     int16_t reserved;
74 } ProxyMessageHead;
75 
76 typedef struct {
77     ProxyMessageHead msgHead;
78     int32_t dateLen;
79     char *data;
80     uint32_t connId;
81     AuthHandle authHandle; /* for cipher */
82     int32_t keyIndex;
83 } ProxyMessage;
84 
85 #define VERSION 1
86 #define PROXY_CHANNEL_HEAD_LEN 8
87 #define VERSION_SHIFT 4
88 #define FOUR_BIT_MASK 0xF
89 #define ENCRYPTED 0x1
90 #define AUTH_SERVER_SIDE 0x2
91 #define USE_BLE_CIPHER 0x4
92 #define BAD_CIPHER 0x8
93 #define CS_MODE 0x10
94 #define AUTH_SINGLE_CIPHER 0x28 // To be compatible with LegacyOs, use 0x28 which & BAD_CIPHER also BAD_CIPHER
95 #define PROXY_BYTES_LENGTH_MAX (4 * 1024 * 1024)
96 #define PROXY_MESSAGE_LENGTH_MAX 1024
97 
98 #define IDENTITY_LEN 32
99 typedef enum {
100     PROXY_CHANNEL_STATUS_PYH_CONNECTED,
101     PROXY_CHANNEL_STATUS_PYH_CONNECTING,
102     PROXY_CHANNEL_STATUS_HANDSHAKEING,
103     PROXY_CHANNEL_STATUS_KEEPLIVEING,
104     PROXY_CHANNEL_STATUS_TIMEOUT,
105     PROXY_CHANNEL_STATUS_HANDSHAKE_TIMEOUT,
106     PROXY_CHANNEL_STATUS_CONNECTING_TIMEOUT,
107     PROXY_CHANNEL_STATUS_COMPLETED
108 } ProxyChannelStatus;
109 
110 #define BASE64KEY 45 // encrypt SessionKey len
111 typedef struct {
112     char sessionKeyBase64[BASE64KEY];
113     size_t len;
114 } SessionKeyBase64;
115 
116 typedef struct {
117     ListNode node;
118     int32_t channelId;
119     int32_t reqId;
120     int8_t isServer;
121     int8_t status;
122     uint16_t timeout;
123     int16_t myId;
124     int16_t peerId;
125     uint32_t connId;
126     ConnectType type;
127     BleProtocolType bleProtocolType;
128     int32_t seq;
129     char identity[IDENTITY_LEN + 1];
130     AppInfo appInfo;
131     AuthHandle authHandle; /* for cipher */
132     bool deviceTypeIsWinpc;
133 } ProxyChannelInfo;
134 
135 typedef struct {
136     uint8_t *inData;
137     uint32_t inLen;
138     uint8_t *outData;
139     uint32_t outLen;
140 } ProxyDataInfo;
141 
142 typedef struct  {
143     int32_t magicNumber;
144     int32_t seq;
145     int32_t flags;
146     int32_t dataLen;
147 } PacketFastHead;
148 
149 typedef struct {
150     int32_t priority;
151     int32_t sliceNum;
152     int32_t sliceSeq;
153     int32_t reserved;
154 } SliceFastHead;
155 
156 typedef struct {
157     int seq;
158     int packetFlag;
159     int shouldAck;
160 } SessionHead;
161 
162 int32_t TransProxyUnPackHandshakeErrMsg(const char *msg, int32_t *errCode, int32_t len);
163 int32_t TransProxyUnPackRestErrMsg(const char *msg, int32_t *errCode, int32_t len);
164 int32_t TransProxyUnpackHandshakeAckMsg(const char *msg, ProxyChannelInfo *chanInfo,
165     int32_t len, uint16_t *fastDataSize);
166 char* TransProxyPackHandshakeAckMsg(ProxyChannelInfo *chan);
167 char* TransProxyPackHandshakeErrMsg(int32_t errCode);
168 int32_t TransProxyParseMessage(char *data, int32_t len, ProxyMessage *msg, AuthHandle *auth);
169 int32_t TransProxyPackMessage(ProxyMessageHead *msg, AuthHandle authHandle, ProxyDataInfo *dataInfo);
170 char* TransProxyPackHandshakeMsg(ProxyChannelInfo *info);
171 int32_t TransProxyUnpackHandshakeMsg(const char *msg, ProxyChannelInfo *chan, int32_t len);
172 char* TransProxyPackIdentity(const char *identity);
173 int32_t TransProxyUnpackIdentity(const char *msg, char *identity, uint32_t identitySize, int32_t len);
174 char *TransProxyPackFastData(const AppInfo *appInfo, uint32_t *outLen);
175 int32_t PackPlaintextMessage(ProxyMessageHead *msg, ProxyDataInfo *dataInfo);
176 int32_t GetBrMacFromConnInfo(uint32_t connId, char *peerBrMac, uint32_t len);
177 
178 #ifdef __cplusplus
179 #if __cplusplus
180 }
181 #endif /* __cplusplus */
182 #endif /* __cplusplus */
183 
184 #endif
185