1 /*
2 * Copyright (c) 2021-2025 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_message_open_channel.h"
17
18 #include <securec.h>
19 #include <stdatomic.h>
20
21 #include "bus_center_manager.h"
22 #include "g_enhance_trans_func_pack.h"
23 #include "softbus_access_token_adapter.h"
24 #include "softbus_adapter_crypto.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_adapter_socket.h"
27 #include "softbus_error_code.h"
28 #include "softbus_json_utils.h"
29 #include "softbus_utils.h"
30 #include "trans_channel_common.h"
31 #include "trans_log.h"
32 #include "trans_uk_manager.h"
33
34 #define FL_IDENTITY "FL_IDENTITY"
35 #define BASE64KEY 45 // Base64 encrypt SessionKey length
36 #define INVALID_USER_ID (-1)
37
PackError(int32_t errCode,const char * errDesc)38 char *PackError(int32_t errCode, const char *errDesc)
39 {
40 if (errDesc == NULL) {
41 TRANS_LOGW(TRANS_CTRL, "invalid param");
42 return NULL;
43 }
44 cJSON *json = cJSON_CreateObject();
45 if (json == NULL) {
46 TRANS_LOGE(TRANS_CTRL, "Cannot create cJSON object");
47 return NULL;
48 }
49 if (!AddNumberToJsonObject(json, CODE, CODE_OPEN_CHANNEL) ||
50 !AddNumberToJsonObject(json, ERR_CODE, errCode) ||
51 !AddStringToJsonObject(json, ERR_DESC, errDesc)) {
52 cJSON_Delete(json);
53 TRANS_LOGE(TRANS_CTRL, "add to cJSON object failed");
54 return NULL;
55 }
56 char *data = cJSON_PrintUnformatted(json);
57 if (data == NULL) {
58 TRANS_LOGE(TRANS_CTRL, "cJSON_PrintUnformatted failed");
59 }
60 cJSON_Delete(json);
61 return data;
62 }
63
PackFirstData(const AppInfo * appInfo,cJSON * json)64 static int32_t PackFirstData(const AppInfo *appInfo, cJSON *json)
65 {
66 TRANS_LOGD(TRANS_CTRL, "begin to pack first data");
67 uint8_t *encodeFastData = (uint8_t *)SoftBusCalloc(BASE64_FAST_DATA_LEN);
68 if (encodeFastData == NULL) {
69 TRANS_LOGE(TRANS_CTRL, "malloc encode fast data failed.");
70 return SOFTBUS_MALLOC_ERR;
71 }
72
73 uint32_t outLen;
74 char *buf = TransTdcPackFastData(appInfo, &outLen);
75 if (buf == NULL) {
76 TRANS_LOGE(TRANS_CTRL, "failed to pack bytes.");
77 SoftBusFree(encodeFastData);
78 return SOFTBUS_ENCRYPT_ERR;
79 }
80 if (outLen != appInfo->fastTransDataSize + FAST_TDC_EXT_DATA_SIZE) {
81 TRANS_LOGE(TRANS_CTRL, "pack bytes len error, outlen=%{public}d", outLen);
82 SoftBusFree(buf);
83 SoftBusFree(encodeFastData);
84 return SOFTBUS_ENCRYPT_ERR;
85 }
86 size_t fastDataSize = 0;
87 int32_t ret = SoftBusBase64Encode(encodeFastData, BASE64_FAST_DATA_LEN, &fastDataSize,
88 (const unsigned char *)buf, outLen);
89 if (ret != SOFTBUS_OK) {
90 TRANS_LOGE(TRANS_CTRL, "base64 encode failed.");
91 SoftBusFree(encodeFastData);
92 SoftBusFree(buf);
93 return SOFTBUS_DECRYPT_ERR;
94 }
95 if (!AddStringToJsonObject(json, FIRST_DATA, (char *)encodeFastData)) {
96 TRANS_LOGE(TRANS_CTRL, "add first data failed.");
97 SoftBusFree(encodeFastData);
98 SoftBusFree(buf);
99 return SOFTBUS_PARSE_JSON_ERR;
100 }
101 SoftBusFree(encodeFastData);
102 SoftBusFree(buf);
103 return SOFTBUS_OK;
104 }
105
JsonObjectPackRequestEx(const AppInfo * appInfo,cJSON * json,unsigned char * encodeSessionKey)106 static int32_t JsonObjectPackRequestEx(const AppInfo *appInfo, cJSON *json, unsigned char *encodeSessionKey)
107 {
108 if (!AddNumberToJsonObject(json, CODE, CODE_OPEN_CHANNEL) ||
109 !AddNumberToJsonObject(json, API_VERSION, appInfo->myData.apiVersion) ||
110 !AddStringToJsonObject(json, BUS_NAME, appInfo->peerData.sessionName) ||
111 !AddStringToJsonObject(json, GROUP_ID, appInfo->groupId) ||
112 !AddNumberToJsonObject(json, UID, appInfo->myData.uid) ||
113 !AddNumberToJsonObject(json, PID, appInfo->myData.pid) ||
114 !AddStringToJsonObject(json, SESSION_KEY, (char *)encodeSessionKey) ||
115 !AddNumberToJsonObject(json, MTU_SIZE, (int32_t)appInfo->myData.dataConfig)) {
116 return SOFTBUS_PARSE_JSON_ERR;
117 }
118
119 if (!AddNumberToJsonObject(json, TRANS_CAPABILITY, (int32_t)appInfo->channelCapability)) {
120 return SOFTBUS_PARSE_JSON_ERR;
121 }
122
123 char *authState = (char *)appInfo->myData.authState;
124 if (appInfo->myData.apiVersion != API_V1 &&
125 (!AddStringToJsonObject(json, PKG_NAME, appInfo->myData.pkgName) ||
126 !AddStringToJsonObject(json, CLIENT_BUS_NAME, appInfo->myData.sessionName) ||
127 !AddStringToJsonObject(json, AUTH_STATE, authState) ||
128 !AddNumberToJsonObject(json, MSG_ROUTE_TYPE, appInfo->routeType))) {
129 return SOFTBUS_PARSE_JSON_ERR;
130 }
131 (void)AddStringToJsonObject(json, DEVICE_ID, appInfo->myData.deviceId);
132 (void)AddNumberToJsonObject(json, BUSINESS_TYPE, appInfo->businessType);
133 (void)AddNumberToJsonObject(json, AUTO_CLOSE_TIME, appInfo->autoCloseTime);
134 (void)AddNumberToJsonObject(json, TRANS_FLAGS, TRANS_FLAG_HAS_CHANNEL_AUTH);
135 (void)AddNumberToJsonObject(json, MY_HANDLE_ID, appInfo->myHandleId);
136 (void)AddNumberToJsonObject(json, PEER_HANDLE_ID, appInfo->peerHandleId);
137 (void)AddNumber64ToJsonObject(json, JSON_KEY_CALLING_TOKEN_ID, (int64_t)appInfo->callingTokenId);
138 (void)AddStringToJsonObject(json, ACCOUNT_ID, appInfo->myData.accountId);
139 (void)AddNumberToJsonObject(json, USER_ID, appInfo->myData.userId);
140 (void)AddNumber64ToJsonObject(json, SOURCE_ACL_TOKEN_ID, (int64_t)appInfo->myData.tokenId);
141 (void)AddStringToJsonObject(json, SOURCE_ACL_EXTRA_INFO, appInfo->extraAccessInfo);
142 return SOFTBUS_OK;
143 }
144
PackRequest(const AppInfo * appInfo,int64_t requestId)145 char *PackRequest(const AppInfo *appInfo, int64_t requestId)
146 {
147 if (appInfo == NULL) {
148 TRANS_LOGW(TRANS_CTRL, "invalid param.");
149 return NULL;
150 }
151
152 cJSON *json = cJSON_CreateObject();
153 if (json == NULL) {
154 TRANS_LOGE(TRANS_CTRL, "Cannot create cJSON object");
155 return NULL;
156 }
157 if (!AddNumber16ToJsonObject(json, FIRST_DATA_SIZE, appInfo->fastTransDataSize)) {
158 cJSON_Delete(json);
159 return NULL;
160 }
161 if (appInfo->fastTransDataSize > 0 && PackFirstData(appInfo, json) != SOFTBUS_OK) {
162 TRANS_LOGE(TRANS_CTRL, "pack first data failed");
163 cJSON_Delete(json);
164 return NULL;
165 }
166
167 unsigned char encodeSessionKey[BASE64KEY] = {0};
168 size_t keyLen = 0;
169 int32_t ret = SoftBusBase64Encode(encodeSessionKey, BASE64KEY,
170 &keyLen, (unsigned char *)appInfo->sessionKey, SESSION_KEY_LENGTH);
171 if (ret != SOFTBUS_OK) {
172 cJSON_Delete(json);
173 return NULL;
174 }
175 ret = JsonObjectPackRequestEx(appInfo, json, encodeSessionKey);
176 (void)memset_s(encodeSessionKey, sizeof(encodeSessionKey), 0, sizeof(encodeSessionKey));
177 if (ret != SOFTBUS_OK) {
178 cJSON_Delete(json);
179 return NULL;
180 }
181 if (appInfo->fdProtocol == LNN_PROTOCOL_HTP) {
182 (void)AddNumber64ToJsonObject(json, FL_IDENTITY, requestId);
183 }
184 char *data = cJSON_PrintUnformatted(json);
185 if (data == NULL) {
186 TRANS_LOGW(TRANS_CTRL, "cJSON_PrintUnformatted failed");
187 }
188 cJSON_Delete(json);
189 return data;
190 }
191
UnpackFirstData(AppInfo * appInfo,const cJSON * json)192 static int32_t UnpackFirstData(AppInfo *appInfo, const cJSON *json)
193 {
194 (void)LnnGetNetworkIdByUuid(appInfo->peerData.deviceId, appInfo->peerNetWorkId, NETWORK_ID_BUF_LEN);
195 int32_t osType = 0;
196 (void)GetOsTypeByNetworkId(appInfo->peerNetWorkId, &osType);
197 if (osType == OH_OS_TYPE) {
198 TRANS_LOGW(TRANS_CTRL, "no need get fastData osType=%{public}d", osType);
199 return SOFTBUS_OK;
200 }
201 if (!GetJsonObjectNumber16Item(json, FIRST_DATA_SIZE, &(appInfo->fastTransDataSize))) {
202 appInfo->fastTransDataSize = 0;
203 }
204 TRANS_LOGD(TRANS_CTRL, "fastDataSize=%{public}d", appInfo->fastTransDataSize);
205 if (appInfo->fastTransDataSize > 0 && appInfo->fastTransDataSize <= MAX_FAST_DATA_LEN) {
206 uint8_t *encodeFastData = (uint8_t *)SoftBusCalloc(BASE64_FAST_DATA_LEN);
207 if (encodeFastData == NULL) {
208 TRANS_LOGE(TRANS_CTRL, "malloc encode fast data failed.");
209 return SOFTBUS_MALLOC_ERR;
210 }
211 size_t fastDataSize = 0;
212 if (!GetJsonObjectStringItem(json, FIRST_DATA, (char *)encodeFastData, BASE64_FAST_DATA_LEN)) {
213 TRANS_LOGE(TRANS_CTRL, "Failed to get fast data");
214 SoftBusFree(encodeFastData);
215 return SOFTBUS_PARSE_JSON_ERR;
216 }
217 appInfo->fastTransData = (uint8_t *)SoftBusCalloc(appInfo->fastTransDataSize + FAST_TDC_EXT_DATA_SIZE);
218 if (appInfo->fastTransData == NULL) {
219 TRANS_LOGE(TRANS_CTRL, "malloc fast data failed.");
220 SoftBusFree(encodeFastData);
221 return SOFTBUS_MALLOC_ERR;
222 }
223 int32_t ret = SoftBusBase64Decode((unsigned char *)appInfo->fastTransData, appInfo->fastTransDataSize +
224 FAST_TDC_EXT_DATA_SIZE, &fastDataSize, encodeFastData, strlen((char *)encodeFastData));
225 if (ret != SOFTBUS_OK) {
226 TRANS_LOGE(TRANS_CTRL, "mbedtls decode failed.");
227 SoftBusFree((void *)appInfo->fastTransData);
228 appInfo->fastTransData = NULL;
229 SoftBusFree(encodeFastData);
230 return SOFTBUS_DECRYPT_ERR;
231 }
232 SoftBusFree(encodeFastData);
233 }
234 return SOFTBUS_OK;
235 }
236
ParseMessageToAppInfo(const cJSON * msg,AppInfo * appInfo)237 static int32_t ParseMessageToAppInfo(const cJSON *msg, AppInfo *appInfo)
238 {
239 char sessionKey[BASE64KEY] = { 0 };
240 (void)GetJsonObjectStringItem(msg, SESSION_KEY, sessionKey, sizeof(sessionKey));
241 if (!GetJsonObjectStringItem(msg, BUS_NAME, (appInfo->myData.sessionName), SESSION_NAME_SIZE_MAX) ||
242 !GetJsonObjectStringItem(msg, GROUP_ID, (appInfo->groupId), GROUP_ID_SIZE_MAX)) {
243 TRANS_LOGE(TRANS_CTRL, "Failed to get BUS_NAME");
244 return SOFTBUS_PARSE_JSON_ERR;
245 }
246 if (!GetJsonObjectNumberItem(msg, MTU_SIZE, (int32_t *)&(appInfo->peerData.dataConfig))) {
247 TRANS_LOGW(TRANS_CTRL, "peer dataconfig is null.");
248 }
249 appInfo->peerData.uid = -1;
250 appInfo->peerData.pid = -1;
251 (void)GetJsonObjectNumberItem(msg, UID, &appInfo->peerData.uid);
252 (void)GetJsonObjectNumberItem(msg, PID, &appInfo->peerData.pid);
253 (void)GetJsonObjectStringItem(msg, ACCOUNT_ID, (appInfo->peerData.accountId), ACCOUNT_UID_LEN_MAX);
254 if (!GetJsonObjectNumberItem(msg, USER_ID, &appInfo->peerData.userId)) {
255 appInfo->peerData.userId = INVALID_USER_ID;
256 }
257 (void)GetJsonObjectNumber64Item(msg, SOURCE_ACL_TOKEN_ID, (int64_t *)&appInfo->peerData.tokenId);
258 (void)GetJsonObjectStringItem(msg, SOURCE_ACL_EXTRA_INFO, appInfo->extraAccessInfo, EXTRA_ACCESS_INFO_LEN_MAX);
259 appInfo->myHandleId = -1;
260 appInfo->peerHandleId = -1;
261 if (!GetJsonObjectInt32Item(msg, MY_HANDLE_ID, &(appInfo->peerHandleId)) ||
262 !GetJsonObjectInt32Item(msg, PEER_HANDLE_ID, &(appInfo->myHandleId))) {
263 appInfo->myHandleId = -1;
264 appInfo->peerHandleId = -1;
265 }
266
267 size_t len = 0;
268 if (strlen(sessionKey) != 0) {
269 int32_t ret = SoftBusBase64Decode((unsigned char *)appInfo->sessionKey, SESSION_KEY_LENGTH, &len,
270 (unsigned char *)sessionKey, strlen(sessionKey));
271 (void)memset_s(sessionKey, sizeof(sessionKey), 0, sizeof(sessionKey));
272 if (len != SESSION_KEY_LENGTH) {
273 TRANS_LOGE(TRANS_CTRL, "Failed to decode sessionKey ret=%{public}d, len=%{public}zu", ret, len);
274 return SOFTBUS_PARSE_JSON_ERR;
275 }
276 }
277 return SOFTBUS_OK;
278 }
279
UnpackRequest(const cJSON * msg,AppInfo * appInfo)280 int32_t UnpackRequest(const cJSON *msg, AppInfo *appInfo)
281 {
282 if (msg == NULL || appInfo == NULL) {
283 TRANS_LOGW(TRANS_CTRL, "invalid param");
284 return SOFTBUS_INVALID_PARAM;
285 }
286 (void)GetJsonObjectStringItem(msg, DEVICE_ID, appInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX);
287 int32_t ret = UnpackFirstData(appInfo, msg);
288 if (ret != SOFTBUS_OK) {
289 TRANS_LOGE(TRANS_CTRL, "unpack first data failed");
290 return ret;
291 }
292
293 int32_t apiVersion = API_V1;
294 (void)GetJsonObjectNumberItem(msg, API_VERSION, &apiVersion);
295 appInfo->peerData.apiVersion = (ApiVersion)apiVersion;
296 if (ParseMessageToAppInfo(msg, appInfo) != SOFTBUS_OK) {
297 TRANS_LOGE(TRANS_CTRL, "fill appInfo failed.");
298 SoftBusFree((void *)appInfo->fastTransData);
299 return SOFTBUS_PARSE_JSON_ERR;
300 }
301 if (apiVersion == API_V1) {
302 return SOFTBUS_OK;
303 }
304
305 if (!GetJsonObjectStringItem(msg, PKG_NAME, (appInfo->peerData.pkgName), PKG_NAME_SIZE_MAX) ||
306 !GetJsonObjectStringItem(msg, CLIENT_BUS_NAME, (appInfo->peerData.sessionName), SESSION_NAME_SIZE_MAX) ||
307 !GetJsonObjectStringItem(msg, AUTH_STATE, (appInfo->peerData.authState), AUTH_STATE_SIZE_MAX)) {
308 TRANS_LOGE(TRANS_CTRL, "Failed to get pkgName");
309 SoftBusFree((void *)appInfo->fastTransData);
310 return SOFTBUS_PARSE_JSON_ERR;
311 }
312 int32_t routeType = WIFI_STA;
313 if (GetJsonObjectNumberItem(msg, MSG_ROUTE_TYPE, &routeType) != SOFTBUS_OK) {
314 TRANS_LOGW(TRANS_CTRL, "Failed to get route type");
315 }
316 appInfo->routeType = (RouteType)routeType;
317 if (!GetJsonObjectNumberItem(msg, BUSINESS_TYPE, (int32_t *)&appInfo->businessType)) {
318 appInfo->businessType = BUSINESS_TYPE_NOT_CARE;
319 }
320 int32_t transFlag = TRANS_FLAG_HAS_CHANNEL_AUTH;
321 (void)GetJsonObjectNumberItem(msg, AUTO_CLOSE_TIME, (int32_t *)&appInfo->autoCloseTime);
322 (void)GetJsonObjectNumberItem(msg, TRANS_FLAGS, &transFlag);
323 if (!GetJsonObjectNumber64Item(msg, JSON_KEY_CALLING_TOKEN_ID, (int64_t *)&appInfo->callingTokenId)) {
324 appInfo->callingTokenId = TOKENID_NOT_SET;
325 }
326 uint32_t remoteCapability = 0;
327 (void)GetJsonObjectNumberItem(msg, TRANS_CAPABILITY, (int32_t *)&remoteCapability);
328 appInfo->channelCapability = remoteCapability & TRANS_CHANNEL_CAPABILITY;
329 return SOFTBUS_OK;
330 }
331
AddItemsToJsonObject(const AppInfo * appInfo,cJSON * json)332 static int32_t AddItemsToJsonObject(const AppInfo *appInfo, cJSON *json)
333 {
334 TRANS_CHECK_AND_RETURN_RET_LOGE(AddNumberToJsonObject(json, CODE, CODE_OPEN_CHANNEL),
335 SOFTBUS_CREATE_JSON_ERR, TRANS_CTRL, "Failed to add channels");
336 TRANS_CHECK_AND_RETURN_RET_LOGE(AddNumberToJsonObject(json, API_VERSION, appInfo->myData.apiVersion),
337 SOFTBUS_CREATE_JSON_ERR, TRANS_CTRL, "Failed to add apiVersion");
338 TRANS_CHECK_AND_RETURN_RET_LOGE(AddStringToJsonObject(json, DEVICE_ID, appInfo->myData.deviceId),
339 SOFTBUS_CREATE_JSON_ERR, TRANS_CTRL, "Failed to add deviceId");
340 TRANS_CHECK_AND_RETURN_RET_LOGE(AddNumberToJsonObject(json, UID, appInfo->myData.uid),
341 SOFTBUS_CREATE_JSON_ERR, TRANS_CTRL, "Failed to add uid");
342 TRANS_CHECK_AND_RETURN_RET_LOGE(AddNumberToJsonObject(json, PID, appInfo->myData.pid),
343 SOFTBUS_CREATE_JSON_ERR, TRANS_CTRL, "Failed to add pid");
344 TRANS_CHECK_AND_RETURN_RET_LOGE(AddNumberToJsonObject(json, TRANS_CAPABILITY, appInfo->channelCapability),
345 SOFTBUS_CREATE_JSON_ERR, TRANS_CTRL, "Failed to add channelCapability");
346 (void)AddStringToJsonObject(json, SINK_ACL_ACCOUNT_ID, appInfo->myData.accountId);
347 (void)AddNumberToJsonObject(json, USER_ID, appInfo->myData.userId);
348 (void)AddNumber64ToJsonObject(json, SINK_ACL_TOKEN_ID, (int64_t)appInfo->myData.tokenId);
349 return EncryptAndAddSinkSessionKey(json, appInfo);
350 }
351
PackReply(const AppInfo * appInfo)352 char *PackReply(const AppInfo *appInfo)
353 {
354 if (appInfo == NULL) {
355 TRANS_LOGE(TRANS_CTRL, "invalid param");
356 return NULL;
357 }
358 cJSON *json = cJSON_CreateObject();
359 if (json == NULL) {
360 return NULL;
361 }
362 char *data = NULL;
363 if (AddItemsToJsonObject(appInfo, json) != SOFTBUS_OK) {
364 goto EXIT_FAIL;
365 }
366 if (appInfo->peerData.dataConfig != 0) {
367 if (!AddNumberToJsonObject(json, MTU_SIZE, appInfo->myData.dataConfig)) {
368 goto EXIT_FAIL;
369 }
370 }
371 if (!AddNumber16ToJsonObject(json, FIRST_DATA_SIZE, appInfo->fastTransDataSize)) {
372 TRANS_LOGE(TRANS_CTRL, "Failed to add trans data size");
373 goto EXIT_FAIL;
374 }
375 if (appInfo->myData.apiVersion != API_V1) {
376 char *authState = (char *)appInfo->myData.authState;
377 if (!AddStringToJsonObject(json, PKG_NAME, appInfo->myData.pkgName) ||
378 !AddStringToJsonObject(json, AUTH_STATE, authState)) {
379 TRANS_LOGE(TRANS_CTRL, "Failed to add pkgName or authState");
380 goto EXIT_FAIL;
381 }
382 }
383 if (!AddNumberToJsonObject(json, MY_HANDLE_ID, appInfo->myHandleId) ||
384 !AddNumberToJsonObject(json, PEER_HANDLE_ID, appInfo->peerHandleId)) {
385 TRANS_LOGE(TRANS_CTRL, "Failed to add items");
386 goto EXIT_FAIL;
387 }
388 data = cJSON_PrintUnformatted(json);
389 if (data == NULL) {
390 TRANS_LOGW(TRANS_CTRL, "cJSON_PrintUnformatted failed");
391 }
392 cJSON_Delete(json);
393 return data;
394
395 EXIT_FAIL:
396 cJSON_Delete(json);
397 return NULL;
398 }
399
UnpackReply(const cJSON * msg,AppInfo * appInfo,uint16_t * fastDataSize)400 int32_t UnpackReply(const cJSON *msg, AppInfo *appInfo, uint16_t *fastDataSize)
401 {
402 if (msg == NULL || appInfo == NULL) {
403 TRANS_LOGW(TRANS_CTRL, "invalid param");
404 return SOFTBUS_INVALID_PARAM;
405 }
406
407 char uuid[DEVICE_ID_SIZE_MAX] = { 0 };
408 if (!GetJsonObjectStringItem(msg, DEVICE_ID, uuid, DEVICE_ID_SIZE_MAX)) {
409 TRANS_LOGE(TRANS_CTRL, "Failed to get uuid");
410 return SOFTBUS_PARSE_JSON_ERR;
411 }
412 if (strcmp(uuid, appInfo->peerData.deviceId) != 0) {
413 TRANS_LOGE(TRANS_CTRL, "Invalid uuid");
414 return SOFTBUS_TRANS_INVALID_UUID;
415 }
416 if (!GetJsonObjectNumber16Item(msg, FIRST_DATA_SIZE, fastDataSize)) {
417 TRANS_LOGW(TRANS_CTRL, "Failed to get fast data size");
418 }
419
420 int32_t apiVersion = API_V1;
421 (void)GetJsonObjectNumberItem(msg, API_VERSION, &apiVersion);
422 appInfo->peerData.apiVersion = (ApiVersion)apiVersion;
423 appInfo->peerData.uid = -1;
424 appInfo->peerData.pid = -1;
425 (void)GetJsonObjectNumberItem(msg, UID, &appInfo->peerData.uid);
426 (void)GetJsonObjectNumberItem(msg, PID, &appInfo->peerData.pid);
427 (void)GetJsonObjectStringItem(msg, SINK_ACL_ACCOUNT_ID, (appInfo->peerData.accountId), ACCOUNT_UID_LEN_MAX);
428 if (!GetJsonObjectNumberItem(msg, USER_ID, &appInfo->peerData.userId)) {
429 appInfo->peerData.userId = INVALID_USER_ID;
430 }
431 (void)GetJsonObjectNumber64Item(msg, SINK_ACL_TOKEN_ID, (int64_t *)&appInfo->peerData.tokenId);
432 if (!GetJsonObjectInt32Item(msg, MY_HANDLE_ID, &(appInfo->peerHandleId)) ||
433 !GetJsonObjectInt32Item(msg, PEER_HANDLE_ID, &(appInfo->myHandleId))) {
434 appInfo->myHandleId = -1;
435 appInfo->peerHandleId = -1;
436 }
437 if (!GetJsonObjectNumberItem(msg, MTU_SIZE, (int32_t *)&(appInfo->peerData.dataConfig))) {
438 TRANS_LOGW(TRANS_CTRL, "peer dataconfig is null.");
439 }
440 if (apiVersion != API_V1) {
441 if (!GetJsonObjectStringItem(msg, PKG_NAME, (appInfo->peerData.pkgName), PKG_NAME_SIZE_MAX) ||
442 !GetJsonObjectStringItem(msg, AUTH_STATE, (appInfo->peerData.authState), AUTH_STATE_SIZE_MAX)) {
443 TRANS_LOGE(TRANS_CTRL, "Failed to get pkgName or authState");
444 return SOFTBUS_PARSE_JSON_ERR;
445 }
446 }
447 if (!GetJsonObjectNumberItem(msg, TRANS_CAPABILITY, (int32_t *)&(appInfo->channelCapability))) {
448 appInfo->channelCapability = 0;
449 }
450 return DecryptAndAddSinkSessionKey(msg, appInfo);
451 }
452
UnpackReplyErrCode(const cJSON * msg,int32_t * errCode)453 int32_t UnpackReplyErrCode(const cJSON *msg, int32_t *errCode)
454 {
455 if ((msg == NULL) || (errCode == NULL)) {
456 TRANS_LOGW(TRANS_CTRL, "invalid param");
457 return SOFTBUS_INVALID_PARAM;
458 }
459
460 if (!GetJsonObjectInt32Item(msg, ERR_CODE, errCode)) {
461 return SOFTBUS_PARSE_JSON_ERR;
462 }
463
464 return SOFTBUS_OK;
465 }
466
TransTdcEncrypt(const char * sessionKey,const char * in,uint32_t inLen,char * out,uint32_t * outLen)467 static int32_t TransTdcEncrypt(const char *sessionKey, const char *in, uint32_t inLen, char *out, uint32_t *outLen)
468 {
469 AesGcmCipherKey cipherKey = {0};
470 cipherKey.keyLen = SESSION_KEY_LENGTH;
471 if (memcpy_s(cipherKey.key, SESSION_KEY_LENGTH, sessionKey, SESSION_KEY_LENGTH) != EOK) {
472 TRANS_LOGE(TRANS_CTRL, "memcpy key error.");
473 return SOFTBUS_MEM_ERR;
474 }
475 int32_t ret = SoftBusEncryptData(&cipherKey, (unsigned char *)in, inLen, (unsigned char *)out, outLen);
476 (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
477 if (ret != SOFTBUS_OK) {
478 TRANS_LOGE(TRANS_CTRL, "SoftBusEncryptData fail. ret=%{public}d", ret);
479 return SOFTBUS_ENCRYPT_ERR;
480 }
481 return SOFTBUS_OK;
482 }
483
PackTcpFastDataPacketHead(TcpFastDataPacketHead * data)484 static void PackTcpFastDataPacketHead(TcpFastDataPacketHead *data)
485 {
486 data->magicNumber = SoftBusHtoLl(data->magicNumber);
487 data->seq = (int32_t)SoftBusHtoLl((uint32_t)data->seq);
488 data->flags = SoftBusHtoLl(data->flags);
489 data->dataLen = SoftBusHtoLl(data->dataLen);
490 }
491
TransTdcPackFastData(const AppInfo * appInfo,uint32_t * outLen)492 char *TransTdcPackFastData(const AppInfo *appInfo, uint32_t *outLen)
493 {
494 #define MAGIC_NUMBER 0xBABEFACE
495 #define TDC_PKT_HEAD_SEQ_START 1024
496 if ((appInfo == NULL) || (outLen == NULL) || (appInfo->fastTransData == NULL)) {
497 TRANS_LOGE(TRANS_CTRL, "invalid param");
498 return NULL;
499 }
500 uint32_t dataLen = appInfo->fastTransDataSize + OVERHEAD_LEN;
501 char *buf = (char *)SoftBusCalloc(dataLen + FAST_DATA_HEAD_SIZE);
502 if (buf == NULL) {
503 TRANS_LOGE(TRANS_CTRL, "malloc failed.");
504 return NULL;
505 }
506 static _Atomic int32_t tdcPktHeadSeq = TDC_PKT_HEAD_SEQ_START;
507 TcpFastDataPacketHead pktHead = {
508 .magicNumber = MAGIC_NUMBER,
509 .seq = atomic_fetch_add_explicit(&tdcPktHeadSeq, 1, memory_order_relaxed),
510 .flags = (appInfo->businessType == BUSINESS_TYPE_BYTE) ? FLAG_BYTES : FLAG_MESSAGE,
511 .dataLen = dataLen,
512 };
513 PackTcpFastDataPacketHead(&pktHead);
514 if (memcpy_s(buf, FAST_DATA_HEAD_SIZE, &pktHead, sizeof(TcpFastDataPacketHead)) != EOK) {
515 SoftBusFree(buf);
516 TRANS_LOGE(TRANS_CTRL, "memcpy_s error");
517 return NULL;
518 }
519 if (TransTdcEncrypt(appInfo->sessionKey, (const char *)appInfo->fastTransData,
520 appInfo->fastTransDataSize, buf + FAST_DATA_HEAD_SIZE, &dataLen) != SOFTBUS_OK) {
521 TRANS_LOGE(TRANS_CTRL, "encrypt error");
522 SoftBusFree(buf);
523 return NULL;
524 }
525 *outLen = dataLen + FAST_DATA_HEAD_SIZE;
526 return buf;
527 }