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