1 /*
2 * Copyright (c) 2021 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 #include "softbus_proxychannel_message.h"
17
18 #include <securec.h>
19
20 #include "auth_interface.h"
21 #include "bus_center_manager.h"
22 #include "softbus_message_open_channel.h"
23 #include "softbus_adapter_crypto.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_errcode.h"
26 #include "softbus_json_utils.h"
27 #include "softbus_log.h"
28 #include "softbus_proxychannel_manager.h"
29 #include "softbus_proxychannel_transceiver.h"
30 #include "softbus_utils.h"
31 #include "softbus_datahead_transform.h"
32 #include "softbus_adapter_socket.h"
33
TransProxyParseMessageHead(char * data,int32_t len,ProxyMessage * msg)34 static int32_t TransProxyParseMessageHead(char *data, int32_t len, ProxyMessage *msg)
35 {
36 char *ptr = data;
37 uint8_t firstByte = *ptr;
38 ptr += sizeof(int8_t);
39 int8_t version = (firstByte >> VERSION_SHIFT) & FOUR_BIT_MASK;
40 msg->msgHead.type = firstByte & FOUR_BIT_MASK;
41 if (version != VERSION || msg->msgHead.type >= PROXYCHANNEL_MSG_TYPE_MAX) {
42 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "parseMessage: unsupported message, version(%d), type(%d)",
43 version, msg->msgHead.type);
44 return SOFTBUS_ERR;
45 }
46
47 msg->msgHead.cipher = *ptr;
48 ptr += sizeof(int8_t);
49 msg->msgHead.peerId = *(int16_t *)ptr;
50 ptr += sizeof(uint16_t);
51 msg->msgHead.myId = *(int16_t *)ptr;
52 msg->data = data + sizeof(ProxyMessageHead);
53 msg->dateLen = len - sizeof(ProxyMessageHead);
54 UnpackProxyMessageHead(&msg->msgHead);
55
56 return SOFTBUS_OK;
57 }
58
TransProxyPackMessageHead(ProxyMessageHead * msgHead,uint8_t * buf,uint32_t size)59 static void TransProxyPackMessageHead(ProxyMessageHead *msgHead, uint8_t *buf, uint32_t size)
60 {
61 if (size < PROXY_CHANNEL_HEAD_LEN) {
62 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "proxy head not enough");
63 return;
64 }
65 uint32_t offset = 0;
66 *buf = msgHead->type;
67 offset += sizeof(uint8_t);
68 *(buf + offset) = msgHead->cipher;
69 offset += sizeof(uint8_t);
70 *(uint16_t *)(buf + offset) = SoftBusHtoLs((uint16_t)msgHead->myId);
71 offset += sizeof(uint16_t);
72 *(uint16_t *)(buf + offset) = SoftBusHtoLs((uint16_t)msgHead->peerId);
73 offset += sizeof(uint16_t);
74 *(uint16_t *)(buf + offset) = SoftBusHtoLs((uint16_t)msgHead->reserved);
75 }
76
GetRemoteUdidByBtMac(const char * peerMac,char * udid,int32_t len)77 static int32_t GetRemoteUdidByBtMac(const char *peerMac, char *udid, int32_t len)
78 {
79 char networkId[NETWORK_ID_BUF_LEN] = {0};
80 if (LnnGetNetworkIdByBtMac(peerMac, networkId, sizeof(networkId)) != SOFTBUS_OK) {
81 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "LnnGetNetworkIdByBtMac fail");
82 return SOFTBUS_NOT_FIND;
83 }
84 if (LnnGetRemoteStrInfo(networkId, STRING_KEY_DEV_UDID, udid, len) != SOFTBUS_OK) {
85 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "LnnGetRemoteStrInfo UDID fail");
86 return SOFTBUS_ERR;
87 }
88 return SOFTBUS_OK;
89 }
90
TransProxyGetAuthConnInfo(uint32_t connId,AuthConnInfo * connInfo)91 static int32_t TransProxyGetAuthConnInfo(uint32_t connId, AuthConnInfo *connInfo)
92 {
93 ConnectionInfo info = {0};
94 if (ConnGetConnectionInfo(connId, &info) != SOFTBUS_OK) {
95 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ConnGetConnectionInfo fail, connId=%u", connId);
96 return SOFTBUS_ERR;
97 }
98 switch (info.type) {
99 case CONNECT_TCP:
100 connInfo->type = AUTH_LINK_TYPE_WIFI;
101 if (strcpy_s(connInfo->info.ipInfo.ip, IP_LEN, info.socketInfo.addr) != EOK) {
102 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "copy ip fail.");
103 return SOFTBUS_MEM_ERR;
104 }
105 break;
106 case CONNECT_BR:
107 connInfo->type = AUTH_LINK_TYPE_BR;
108 if (strcpy_s(connInfo->info.brInfo.brMac, BT_MAC_LEN, info.brInfo.brMac) != EOK) {
109 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "copy brMac fail.");
110 return SOFTBUS_MEM_ERR;
111 }
112 break;
113 default:
114 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unexpected connType: %d.", info.type);
115 return SOFTBUS_ERR;
116 }
117 return SOFTBUS_OK;
118 }
119
GetAuthIdByHandshakeMsg(uint32_t connId,uint8_t cipher)120 static int64_t GetAuthIdByHandshakeMsg(uint32_t connId, uint8_t cipher)
121 {
122 AuthConnInfo connInfo;
123 if (TransProxyGetAuthConnInfo(connId, &connInfo) != SOFTBUS_OK) {
124 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get connInfo fail connId[%d]", connId);
125 return AUTH_INVALID_ID;
126 }
127 bool isBle = ((cipher & USE_BLE_CIPHER) != 0);
128 if (isBle && connInfo.type == AUTH_LINK_TYPE_BR) {
129 char udid[UDID_BUF_LEN] = {0};
130 if (GetRemoteUdidByBtMac(connInfo.info.brInfo.brMac, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
131 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get udid by btmac fail");
132 return AUTH_INVALID_ID;
133 }
134 if (SoftBusGenerateStrHash((unsigned char *)udid, strlen(udid),
135 connInfo.info.bleInfo.deviceIdHash) != SOFTBUS_OK) {
136 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "generate udid hash fail");
137 return AUTH_INVALID_ID;
138 }
139 connInfo.type = AUTH_LINK_TYPE_BLE;
140 }
141 bool isAuthServer = !((cipher & AUTH_SERVER_SIDE) != 0);
142 return AuthGetIdByConnInfo(&connInfo, isAuthServer, false);
143 }
144
TransProxyParseMessage(char * data,int32_t len,ProxyMessage * msg)145 int32_t TransProxyParseMessage(char *data, int32_t len, ProxyMessage *msg)
146 {
147 if (len <= PROXY_CHANNEL_HEAD_LEN) {
148 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "parseMessage: invalid message length(%d)", len);
149 return SOFTBUS_ERR;
150 }
151 if (TransProxyParseMessageHead(data, len, msg) != SOFTBUS_OK) {
152 return SOFTBUS_ERR;
153 }
154
155 bool isEncrypted = ((msg->msgHead.cipher & ENCRYPTED) != 0);
156 if (isEncrypted) {
157 if (msg->msgHead.type == PROXYCHANNEL_MSG_TYPE_HANDSHAKE) {
158 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO,
159 "prxoy recv handshake cipher: 0x%02x", msg->msgHead.cipher);
160 msg->authId = GetAuthIdByHandshakeMsg(msg->connId, msg->msgHead.cipher);
161 } else {
162 msg->authId = TransProxyGetAuthId(msg->msgHead.myId);
163 }
164 if (msg->authId == AUTH_INVALID_ID) {
165 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
166 "get authId for decrypt fail, connId[%d], myId[%d], type[%d]",
167 msg->connId, msg->msgHead.myId, msg->msgHead.type);
168 return SOFTBUS_ERR;
169 }
170 uint32_t decDataLen = AuthGetDecryptSize((uint32_t)msg->dateLen);
171 uint8_t *decData = (uint8_t *)SoftBusCalloc(decDataLen);
172 if (decData == NULL) {
173 return SOFTBUS_ERR;
174 }
175 if (AuthDecrypt(msg->authId, (uint8_t *)msg->data, (uint32_t)msg->dateLen,
176 decData, &decDataLen) != SOFTBUS_OK) {
177 SoftBusFree(decData);
178 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "parse msg decrypt fail");
179 return SOFTBUS_ERR;
180 }
181 msg->data = (char *)decData;
182 msg->dateLen = (int32_t)decDataLen;
183 }
184 return SOFTBUS_OK;
185 }
186
PackPlaintextMessage(ProxyMessageHead * msg,ProxyDataInfo * dataInfo)187 static int32_t PackPlaintextMessage(ProxyMessageHead *msg, ProxyDataInfo *dataInfo)
188 {
189 uint32_t connHeadLen = ConnGetHeadSize();
190 uint32_t size = PROXY_CHANNEL_HEAD_LEN + connHeadLen + dataInfo->inLen;
191 uint8_t *buf = (uint8_t *)SoftBusCalloc(size);
192 if (buf == NULL) {
193 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "malloc proxy buf fail, myId[%d]", msg->myId);
194 return SOFTBUS_MALLOC_ERR;
195 }
196 TransProxyPackMessageHead(msg, buf + connHeadLen, PROXY_CHANNEL_HEAD_LEN);
197 if (memcpy_s(buf + connHeadLen + PROXY_CHANNEL_HEAD_LEN, size - connHeadLen - PROXY_CHANNEL_HEAD_LEN,
198 dataInfo->inData, dataInfo->inLen) != EOK) {
199 SoftBusFree(buf);
200 return SOFTBUS_MEM_ERR;
201 }
202 dataInfo->outData = buf;
203 dataInfo->outLen = size;
204 return SOFTBUS_OK;
205 }
206
PackEncryptedMessage(ProxyMessageHead * msg,int64_t authId,ProxyDataInfo * dataInfo)207 static int32_t PackEncryptedMessage(ProxyMessageHead *msg, int64_t authId, ProxyDataInfo *dataInfo)
208 {
209 if (authId == AUTH_INVALID_ID) {
210 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid authId, myId[%d]", msg->myId);
211 return SOFTBUS_INVALID_PARAM;
212 }
213 uint32_t size = ConnGetHeadSize() + PROXY_CHANNEL_HEAD_LEN + AuthGetEncryptSize(dataInfo->inLen);
214 uint8_t *buf = (uint8_t *)SoftBusCalloc(size);
215 if (buf == NULL) {
216 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "malloc enc buf fail, myId[%d]", msg->myId);
217 return SOFTBUS_MALLOC_ERR;
218 }
219 TransProxyPackMessageHead(msg, buf + ConnGetHeadSize(), PROXY_CHANNEL_HEAD_LEN);
220 uint8_t *encData = buf + ConnGetHeadSize() + PROXY_CHANNEL_HEAD_LEN;
221 uint32_t encDataLen = size - ConnGetHeadSize() - PROXY_CHANNEL_HEAD_LEN;
222 if (AuthEncrypt(authId, dataInfo->inData, dataInfo->inLen, encData, &encDataLen) != SOFTBUS_OK) {
223 SoftBusFree(buf);
224 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "pack msg encrypt fail, myId[%d]", msg->myId);
225 return SOFTBUS_ENCRYPT_ERR;
226 }
227 dataInfo->outData = buf;
228 dataInfo->outLen = size;
229 return SOFTBUS_OK;
230 }
231
TransProxyPackMessage(ProxyMessageHead * msg,int64_t authId,ProxyDataInfo * dataInfo)232 int32_t TransProxyPackMessage(ProxyMessageHead *msg, int64_t authId, ProxyDataInfo *dataInfo)
233 {
234 if (msg == NULL || dataInfo == NULL || dataInfo->inData == NULL || dataInfo->inData == 0) {
235 return SOFTBUS_INVALID_PARAM;
236 }
237 if (msg->type != PROXYCHANNEL_MSG_TYPE_NORMAL) {
238 AnonyPacketPrintout(SOFTBUS_LOG_TRAN,
239 "TransProxyPackMessage, payload: ", (const char *)dataInfo->inData, dataInfo->inLen);
240 }
241
242 int32_t ret;
243 if ((msg->cipher & ENCRYPTED) == 0) {
244 ret = PackPlaintextMessage(msg, dataInfo);
245 } else {
246 ret = PackEncryptedMessage(msg, authId, dataInfo);
247 }
248 if (ret != SOFTBUS_OK) {
249 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "pack proxy msg fail, myId[%d]", msg->myId);
250 return SOFTBUS_ERR;
251 }
252 return SOFTBUS_OK;
253 }
254
PackHandshakeMsgForNormal(SessionKeyBase64 * sessionBase64,AppInfo * appInfo,cJSON * root)255 static int32_t PackHandshakeMsgForNormal(SessionKeyBase64 *sessionBase64, AppInfo *appInfo, cJSON *root)
256 {
257 int32_t ret = SoftBusBase64Encode((unsigned char *)sessionBase64->sessionKeyBase64,
258 sizeof(sessionBase64->sessionKeyBase64), &(sessionBase64->len),
259 (unsigned char *)appInfo->sessionKey, sizeof(appInfo->sessionKey));
260 if (ret != 0) {
261 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "mbedtls_base64_encode FAIL %d", ret);
262 return ret;
263 }
264 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "mbedtls_base64_encode len %d", sessionBase64->len);
265 if (!AddNumberToJsonObject(root, JSON_KEY_UID, appInfo->myData.uid) ||
266 !AddNumberToJsonObject(root, JSON_KEY_PID, appInfo->myData.pid) ||
267 !AddStringToJsonObject(root, JSON_KEY_GROUP_ID, appInfo->groupId) ||
268 !AddStringToJsonObject(root, JSON_KEY_PKG_NAME, appInfo->myData.pkgName) ||
269 !AddStringToJsonObject(root, JSON_KEY_SESSION_KEY, sessionBase64->sessionKeyBase64)) {
270 return SOFTBUS_ERR;
271 }
272 if (!AddNumberToJsonObject(root, JSON_KEY_ENCRYPT, appInfo->encrypt) ||
273 !AddNumberToJsonObject(root, JSON_KEY_ALGORITHM, appInfo->algorithm) ||
274 !AddNumberToJsonObject(root, JSON_KEY_CRC, appInfo->crc)) {
275 return SOFTBUS_ERR;
276 }
277 (void)AddNumberToJsonObject(root, JSON_KEY_BUSINESS_TYPE, appInfo->businessType);
278 return SOFTBUS_OK;
279 }
280
TransProxyPackHandshakeErrMsg(int32_t errCode)281 char *TransProxyPackHandshakeErrMsg(int32_t errCode)
282 {
283 cJSON *root = NULL;
284 char *buf = NULL;
285
286 root = cJSON_CreateObject();
287 if (root == NULL) {
288 return NULL;
289 }
290
291 if (!AddNumberToJsonObject(root, ERR_CODE, errCode)) {
292 cJSON_Delete(root);
293 return NULL;
294 }
295
296 buf = cJSON_PrintUnformatted(root);
297 cJSON_Delete(root);
298 return buf;
299 }
300
TransProxyPackHandshakeMsg(ProxyChannelInfo * info)301 char *TransProxyPackHandshakeMsg(ProxyChannelInfo *info)
302 {
303 cJSON *root = NULL;
304 SessionKeyBase64 sessionBase64;
305 char *buf = NULL;
306 AppInfo *appInfo = &(info->appInfo);
307 int32_t ret;
308
309 root = cJSON_CreateObject();
310 if (root == NULL) {
311 return NULL;
312 }
313 (void)memset_s(&sessionBase64, sizeof(SessionKeyBase64), 0, sizeof(SessionKeyBase64));
314 if (!AddNumberToJsonObject(root, JSON_KEY_TYPE, appInfo->appType) ||
315 !AddStringToJsonObject(root, JSON_KEY_IDENTITY, info->identity) ||
316 !AddStringToJsonObject(root, JSON_KEY_DEVICE_ID, appInfo->myData.deviceId) ||
317 !AddStringToJsonObject(root, JSON_KEY_SRC_BUS_NAME, appInfo->myData.sessionName) ||
318 !AddStringToJsonObject(root, JSON_KEY_DST_BUS_NAME, appInfo->peerData.sessionName)) {
319 goto EXIT;
320 }
321 (void)cJSON_AddTrueToObject(root, JSON_KEY_HAS_PRIORITY);
322
323 if (appInfo->appType == APP_TYPE_NORMAL) {
324 ret = PackHandshakeMsgForNormal(&sessionBase64, appInfo, root);
325 if (ret != SOFTBUS_OK) {
326 goto EXIT;
327 }
328 } else if (appInfo->appType == APP_TYPE_AUTH) {
329 if (strlen(appInfo->reqId) == 0 && GenerateRandomStr(appInfo->reqId, REQ_ID_SIZE_MAX) != SOFTBUS_OK) {
330 goto EXIT;
331 }
332 if (!AddStringToJsonObject(root, JSON_KEY_REQUEST_ID, appInfo->reqId)) {
333 goto EXIT;
334 }
335 if (!AddStringToJsonObject(root, JSON_KEY_PKG_NAME, appInfo->myData.pkgName)) {
336 goto EXIT;
337 }
338 } else {
339 ret = SoftBusBase64Encode((uint8_t *)sessionBase64.sessionKeyBase64, sizeof(sessionBase64.sessionKeyBase64),
340 &(sessionBase64.len), (uint8_t *)appInfo->sessionKey, sizeof(appInfo->sessionKey));
341 if (ret != 0) {
342 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "mbedtls_base64_encode FAIL %d", ret);
343 goto EXIT;
344 }
345 if (!AddStringToJsonObject(root, JSON_KEY_SESSION_KEY, sessionBase64.sessionKeyBase64)) {
346 goto EXIT;
347 }
348 }
349
350 buf = cJSON_PrintUnformatted(root);
351 EXIT:
352 cJSON_Delete(root);
353 return buf;
354 }
355
TransProxyPackHandshakeAckMsg(ProxyChannelInfo * chan)356 char *TransProxyPackHandshakeAckMsg(ProxyChannelInfo *chan)
357 {
358 cJSON *root = NULL;
359 char *buf = NULL;
360 AppInfo *appInfo = &(chan->appInfo);
361 if (appInfo == NULL || appInfo->appType == APP_TYPE_NOT_CARE) {
362 return NULL;
363 }
364
365 root = cJSON_CreateObject();
366 if (root == NULL) {
367 return NULL;
368 }
369
370 if (!AddStringToJsonObject(root, JSON_KEY_IDENTITY, chan->identity) ||
371 !AddStringToJsonObject(root, JSON_KEY_DEVICE_ID, appInfo->myData.deviceId)) {
372 cJSON_Delete(root);
373 return NULL;
374 }
375 (void)cJSON_AddTrueToObject(root, JSON_KEY_HAS_PRIORITY);
376 if (appInfo->appType == APP_TYPE_NORMAL) {
377 if (!AddNumberToJsonObject(root, JSON_KEY_UID, appInfo->myData.uid) ||
378 !AddNumberToJsonObject(root, JSON_KEY_PID, appInfo->myData.pid) ||
379 !AddStringToJsonObject(root, JSON_KEY_PKG_NAME, appInfo->myData.pkgName) ||
380 !AddNumberToJsonObject(root, JSON_KEY_ENCRYPT, appInfo->encrypt) ||
381 !AddNumberToJsonObject(root, JSON_KEY_ALGORITHM, appInfo->algorithm) ||
382 !AddNumberToJsonObject(root, JSON_KEY_CRC, appInfo->crc)) {
383 cJSON_Delete(root);
384 return NULL;
385 }
386 } else if (appInfo->appType == APP_TYPE_AUTH) {
387 if (!AddStringToJsonObject(root, JSON_KEY_PKG_NAME, appInfo->myData.pkgName)) {
388 cJSON_Delete(root);
389 return NULL;
390 }
391 }
392
393 buf = cJSON_PrintUnformatted(root);
394 cJSON_Delete(root);
395 return buf;
396 }
397
TransProxyUnPackHandshakeErrMsg(const char * msg,int * errCode,int32_t len)398 int32_t TransProxyUnPackHandshakeErrMsg(const char *msg, int *errCode, int32_t len)
399 {
400 cJSON *root = cJSON_ParseWithLength(msg, len);
401 if ((root == NULL) || (errCode == NULL)) {
402 return SOFTBUS_ERR;
403 }
404
405 if (!GetJsonObjectInt32Item(root, ERR_CODE, errCode)) {
406 cJSON_Delete(root);
407 return SOFTBUS_ERR;
408 }
409
410 cJSON_Delete(root);
411 return SOFTBUS_OK;
412 }
413
414
TransProxyUnpackHandshakeAckMsg(const char * msg,ProxyChannelInfo * chanInfo,int32_t len)415 int32_t TransProxyUnpackHandshakeAckMsg(const char *msg, ProxyChannelInfo *chanInfo, int32_t len)
416 {
417 cJSON *root = 0;
418 AppInfo *appInfo = &(chanInfo->appInfo);
419 if (appInfo == NULL) {
420 return SOFTBUS_ERR;
421 }
422 root = cJSON_ParseWithLength(msg, len);
423 if (root == NULL) {
424 return SOFTBUS_ERR;
425 }
426
427 if (!GetJsonObjectStringItem(root, JSON_KEY_IDENTITY, chanInfo->identity, sizeof(chanInfo->identity)) ||
428 !GetJsonObjectStringItem(root, JSON_KEY_DEVICE_ID, appInfo->peerData.deviceId,
429 sizeof(appInfo->peerData.deviceId))) {
430 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "fail to get json item");
431 cJSON_Delete(root);
432 return SOFTBUS_ERR;
433 }
434 appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT;
435 appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256;
436 appInfo->crc = APP_INFO_FILE_FEATURES_NO_SUPPORT;
437 int32_t appType = TransProxyGetAppInfoType(chanInfo->myId, chanInfo->identity);
438 if (appType == SOFTBUS_ERR) {
439 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "fail to get app type");
440 cJSON_Delete(root);
441 return SOFTBUS_ERR;
442 }
443 appInfo->appType = (AppType)appType;
444 if (appInfo->appType == APP_TYPE_NORMAL) {
445 if (!GetJsonObjectNumberItem(root, JSON_KEY_UID, &appInfo->peerData.uid) ||
446 !GetJsonObjectNumberItem(root, JSON_KEY_PID, &appInfo->peerData.pid) ||
447 !GetJsonObjectNumberItem(root, JSON_KEY_ENCRYPT, &appInfo->encrypt) ||
448 !GetJsonObjectNumberItem(root, JSON_KEY_ALGORITHM, &appInfo->algorithm) ||
449 !GetJsonObjectNumberItem(root, JSON_KEY_CRC, &appInfo->crc)) {
450 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "unpack handshake ack old version");
451 appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT;
452 appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256;
453 appInfo->crc = APP_INFO_FILE_FEATURES_NO_SUPPORT;
454 }
455 }
456
457 if (!GetJsonObjectStringItem(root, JSON_KEY_PKG_NAME, appInfo->peerData.pkgName,
458 sizeof(appInfo->peerData.pkgName))) {
459 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "no item to get pkg name");
460 }
461 cJSON_Delete(root);
462 return SOFTBUS_OK;
463 }
464
UnpackHandshakeMsgForNormal(cJSON * root,AppInfo * appInfo,char * sessionKey,int32_t sessionKeyLen)465 static int32_t UnpackHandshakeMsgForNormal(cJSON *root, AppInfo *appInfo, char *sessionKey, int32_t sessionKeyLen)
466 {
467 if (!GetJsonObjectNumberItem(root, JSON_KEY_UID, &(appInfo->peerData.uid)) ||
468 !GetJsonObjectNumberItem(root, JSON_KEY_PID, &(appInfo->peerData.pid)) ||
469 !GetJsonObjectStringItem(root, JSON_KEY_PKG_NAME, appInfo->peerData.pkgName,
470 sizeof(appInfo->peerData.pkgName)) ||
471 !GetJsonObjectStringItem(root, JSON_KEY_SESSION_KEY, sessionKey, sessionKeyLen)) {
472 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Failed to get handshake msg APP_TYPE_NORMAL");
473 return SOFTBUS_ERR;
474 }
475 if (!GetJsonObjectNumberItem(root, JSON_KEY_ENCRYPT, &appInfo->encrypt) ||
476 !GetJsonObjectNumberItem(root, JSON_KEY_ALGORITHM, &appInfo->algorithm) ||
477 !GetJsonObjectNumberItem(root, JSON_KEY_CRC, &appInfo->crc)) {
478 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "unpack handshake old version");
479 appInfo->encrypt = APP_INFO_FILE_FEATURES_SUPPORT;
480 appInfo->algorithm = APP_INFO_ALGORITHM_AES_GCM_256;
481 appInfo->crc = APP_INFO_FILE_FEATURES_NO_SUPPORT;
482 }
483 if (!GetJsonObjectNumberItem(root, JSON_KEY_BUSINESS_TYPE, (int*)&appInfo->businessType)) {
484 appInfo->businessType = BUSINESS_TYPE_NOT_CARE;
485 }
486
487 size_t len = 0;
488 int32_t ret = SoftBusBase64Decode((uint8_t *)appInfo->sessionKey, sizeof(appInfo->sessionKey),
489 &len, (uint8_t *)sessionKey, strlen(sessionKey));
490 if (len != sizeof(appInfo->sessionKey) || ret != 0) {
491 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "decode session fail %d ", ret);
492 return SOFTBUS_ERR;
493 }
494 return SOFTBUS_OK;
495 }
496
TransProxyUnpackAuthHandshakeMsg(cJSON * root,AppInfo * appInfo)497 static int32_t TransProxyUnpackAuthHandshakeMsg(cJSON *root, AppInfo *appInfo)
498 {
499 if (!GetJsonObjectStringItem(root, JSON_KEY_REQUEST_ID, appInfo->reqId, REQ_ID_SIZE_MAX)) {
500 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Failed to get handshake msg REQUEST_ID");
501 return SOFTBUS_ERR;
502 }
503 if (!GetJsonObjectStringItem(root, JSON_KEY_PKG_NAME,
504 appInfo->peerData.pkgName, sizeof(appInfo->peerData.pkgName))) {
505 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Failed to get handshake msg pkgName");
506 return SOFTBUS_ERR;
507 }
508 return SOFTBUS_OK;
509 }
510
TransProxyUnpackHandshakeMsg(const char * msg,ProxyChannelInfo * chan,int32_t len)511 int32_t TransProxyUnpackHandshakeMsg(const char *msg, ProxyChannelInfo *chan, int32_t len)
512 {
513 cJSON *root = cJSON_ParseWithLength(msg, len);
514 if (root == NULL) {
515 return SOFTBUS_ERR;
516 }
517 char sessionKey[BASE64KEY] = {0};
518 AppInfo *appInfo = &(chan->appInfo);
519 int32_t appType = 0;
520
521 if (!GetJsonObjectNumberItem(root, JSON_KEY_TYPE, &(appType)) ||
522 !GetJsonObjectStringItem(root, JSON_KEY_IDENTITY, chan->identity, sizeof(chan->identity)) ||
523 !GetJsonObjectStringItem(root, JSON_KEY_DEVICE_ID, appInfo->peerData.deviceId,
524 sizeof(appInfo->peerData.deviceId)) ||
525 !GetJsonObjectStringItem(root, JSON_KEY_SRC_BUS_NAME, appInfo->peerData.sessionName,
526 sizeof(appInfo->peerData.sessionName)) ||
527 !GetJsonObjectStringItem(root, JSON_KEY_DST_BUS_NAME, appInfo->myData.sessionName,
528 sizeof(appInfo->myData.sessionName))) {
529 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Failed to get handshake msg");
530 cJSON_Delete(root);
531 return SOFTBUS_ERR;
532 }
533 appInfo->appType = (AppType)appType;
534
535 if (appInfo->appType == APP_TYPE_NORMAL) {
536 int32_t ret = UnpackHandshakeMsgForNormal(root, appInfo, sessionKey, BASE64KEY);
537 if (ret != SOFTBUS_OK) {
538 cJSON_Delete(root);
539 return ret;
540 }
541 } else if (appInfo->appType == APP_TYPE_AUTH) {
542 if (TransProxyUnpackAuthHandshakeMsg(root, appInfo) != SOFTBUS_OK) {
543 cJSON_Delete(root);
544 return SOFTBUS_ERR;
545 }
546 } else {
547 if (!GetJsonObjectStringItem(root, JSON_KEY_SESSION_KEY, sessionKey, sizeof(sessionKey))) {
548 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Failed to get handshake msg");
549 cJSON_Delete(root);
550 return SOFTBUS_ERR;
551 }
552 size_t len = 0;
553 int32_t ret = SoftBusBase64Decode((uint8_t *)appInfo->sessionKey, sizeof(appInfo->sessionKey),
554 &len, (uint8_t *)sessionKey, strlen(sessionKey));
555 if (len != sizeof(appInfo->sessionKey) || ret != 0) {
556 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "decode session fail %d ", ret);
557 cJSON_Delete(root);
558 return SOFTBUS_ERR;
559 }
560 }
561 cJSON_Delete(root);
562 return SOFTBUS_OK;
563 }
564
TransProxyPackIdentity(const char * identity)565 char *TransProxyPackIdentity(const char *identity)
566 {
567 cJSON *root = NULL;
568 char *buf = NULL;
569
570 if (identity == NULL) {
571 return NULL;
572 }
573
574 root = cJSON_CreateObject();
575 if (root == NULL) {
576 return NULL;
577 }
578
579 if (!AddStringToJsonObject(root, JSON_KEY_IDENTITY, identity)) {
580 cJSON_Delete(root);
581 return NULL;
582 }
583
584 buf = cJSON_PrintUnformatted(root);
585 cJSON_Delete(root);
586 return buf;
587 }
588
TransProxyUnpackIdentity(const char * msg,char * identity,uint32_t identitySize,int32_t len)589 int32_t TransProxyUnpackIdentity(const char *msg, char *identity, uint32_t identitySize, int32_t len)
590 {
591 cJSON *root = NULL;
592
593 root = cJSON_ParseWithLength(msg, len);
594 if (root == NULL) {
595 return SOFTBUS_ERR;
596 }
597
598 if (!GetJsonObjectStringItem(root, JSON_KEY_IDENTITY, identity, identitySize)) {
599 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "fail to get json item");
600 cJSON_Delete(root);
601 return SOFTBUS_ERR;
602 }
603
604 cJSON_Delete(root);
605 return SOFTBUS_OK;
606 }
607