1 /*
2 * Copyright (c) 2022-2023 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 "auth_session_message.h"
17
18 #include <math.h>
19 #include <securec.h>
20
21 #include "auth_common.h"
22 #include "auth_request.h"
23 #include "auth_connection.h"
24 #include "auth_interface.h"
25 #include "auth_manager.h"
26 #include "bus_center_manager.h"
27 #include "lnn_cipherkey_manager.h"
28 #include "lnn_common_utils.h"
29 #include "lnn_extdata_config.h"
30 #include "lnn_local_net_ledger.h"
31 #include "lnn_feature_capability.h"
32 #include "lnn_network_manager.h"
33 #include "lnn_node_info.h"
34 #include "softbus_adapter_mem.h"
35 #include "softbus_adapter_socket.h"
36 #include "softbus_def.h"
37 #include "softbus_common.h"
38 #include "softbus_json_utils.h"
39 #include "lnn_compress.h"
40 #include "softbus_adapter_json.h"
41 #include "auth_device_common_key.h"
42 #include "auth_hichain_adapter.h"
43
44 /* DeviceId */
45 #define CMD_TAG "TECmd"
46 #define CMD_GET_AUTH_INFO "getAuthInfo"
47 #define CMD_RET_AUTH_INFO "retAuthInfo"
48 #define DATA_TAG "TEData"
49 #define DEVICE_ID_TAG "TEDeviceId"
50 #define DATA_BUF_SIZE_TAG "DataBufSize"
51 #define SOFTBUS_VERSION_TAG "softbusVersion"
52 #define SUPPORT_INFO_COMPRESS "supportInfoCompress"
53 #define EXCHANGE_ID_TYPE "exchangeIdType"
54 #define CMD_TAG_LEN 30
55 #define PACKET_SIZE (64 * 1024)
56
57 /* DeviceInfo-WiFi */
58 #define CODE_VERIFY_IP 1
59 #define BUS_MAX_VERSION "BUS_MAX_VERSION"
60 #define BUS_MIN_VERSION "BUS_MIN_VERSION"
61 #define AUTH_PORT "AUTH_PORT"
62 #define SESSION_PORT "SESSION_PORT"
63 #define PROXY_PORT "PROXY_PORT"
64 #define DEV_IP "DEV_IP"
65 #define BLE_OFFLINE_CODE "OFFLINE_CODE"
66 #define BUS_V1 1
67 #define BUS_V2 2
68
69 /* DeviceInfo-BT */
70 #define CODE_VERIFY_BT 5
71 #define DISCOVERY_TYPE "DISCOVERY_TYPE"
72 #define UUID "UUID"
73 #define DEVICE_VERSION_TYPE "DEVICE_VERSION_TYPE"
74 #define BR_MAC_ADDR "BR_MAC_ADDR"
75 #define CONNECT_INFO "CONNECT_INFO"
76
77 /* DeviceInfo-common */
78 #define CODE "CODE"
79 #define DEVICE_NAME "DEVICE_NAME"
80 #define DEVICE_TYPE "DEVICE_TYPE"
81 #define DEVICE_UDID "DEVICE_UDID"
82 #define DEVICE_UUID "DEVICE_UUID"
83 #define NETWORK_ID "NETWORK_ID"
84 #define NODE_ADDR "NODE_ADDR"
85 #define VERSION_TYPE "VERSION_TYPE"
86 #define BT_MAC "BT_MAC"
87 #define CONN_CAP "CONN_CAP"
88 #define SW_VERSION "SW_VERSION"
89 #define MASTER_UDID "MASTER_UDID"
90 #define MASTER_WEIGHT "MASTER_WEIGHT"
91 #define BLE_P2P "BLE_P2P"
92 #define STA_FREQUENCY "STA_FREQUENCY"
93 #define P2P_MAC_ADDR "P2P_MAC_ADDR"
94 #define P2P_ROLE "P2P_ROLE"
95 #define TRANSPORT_PROTOCOL "TRANSPORT_PROTOCOL"
96 #define DATA_CHANGE_FLAG "NODE_DATA_CHANGE_FLAG"
97 #define IS_CHARGING "IS_CHARGING"
98 #define BATTERY_LEAVEL "BATTERY_LEAVEL"
99 #define PKG_VERSION "PKG_VERSION"
100 #define WIFI_VERSION "WIFI_VERSION"
101 #define BLE_VERSION "BLE_VERSION"
102 #define HML_MAC "HML_MAC"
103 #define WIFI_CFG "WIFI_CFG"
104 #define CHAN_LIST_5G "CHAN_LIST_5G"
105 #define REMAIN_POWER "REMAIN_POWER"
106 #define IS_CHARGING "IS_CHARGING"
107 #define IS_SCREENON "IS_SCREENON"
108 #define IP_MAC "IP_MAC"
109 #define NODE_WEIGHT "NODE_WEIGHT"
110 #define ACCOUNT_ID "ACCOUNT_ID"
111 #define DISTRIBUTED_SWITCH "DISTRIBUTED_SWITCH"
112 #define TRANS_FLAGS "TRANS_FLAGS"
113 #define BLE_TIMESTAMP "BLE_TIMESTAMP"
114 #define WIFI_BUFF_SIZE "WIFI_BUFF_SIZE"
115 #define BR_BUFF_SIZE "BR_BUFF_SIZE"
116 #define FEATURE "FEATURE"
117 #define META_NODE_INFO_OF_EAR "MetaNodeInfoOfEar"
118 #define NEW_CONN_CAP "NEW_CONN_CAP"
119 #define EXTDATA "EXTDATA"
120 #define STATE_VERSION "STATE_VERSION"
121 #define BD_KEY "BD_KEY"
122 #define IV "IV"
123 #define UNIFIED_DISPLAY_DEVICE_NAME "UNIFIED_DISPLAY_DEVICE_NAME"
124 #define UNIFIED_DEFAULT_DEVICE_NAME "UNIFIED_DEFAULT_DEVICE_NAME"
125 #define UNIFIED_DEVICE_NAME "UNIFIED_DEVICE_NAME"
126
127 #define FLAG_COMPRESS_DEVICE_INFO 1
128 #define FLAG_UNCOMPRESS_DEVICE_INFO 0
129 #define FLAG_RELAY_DEVICE_INFO 1
130
131 #define HAS_CTRL_CHANNEL (0x1L)
132 #define HAS_CHANNEL_AUTH (0x2L)
133 #define HAS_P2P_AUTH_V2 (0x04L)
134 #define HAS_SUPPRESS_STRATEGY (0x08L)
135 #define HAS_WAIT_TCP_TX_DONE (0x10L)
136 #define LOCAL_FLAGS (HAS_CTRL_CHANNEL | HAS_P2P_AUTH_V2 | HAS_SUPPRESS_STRATEGY | HAS_WAIT_TCP_TX_DONE)
137 #define DEVICE_ID_STR_LEN 64 // for bt v1
138 #define DEFAULT_BATTERY_LEVEL 100
139 #define DEFAULT_NODE_WEIGHT 100
140 #define BASE64_OFFLINE_CODE_LEN ((OFFLINE_CODE_BYTE_SIZE / 3 + 1) * 4 + 1)
141 #define DEFAULT_WIFI_BUFF_SIZE 32768 // 32k
142 #define DEFAULT_BR_BUFF_SIZE 4096 // 4k
143 #define DEFAULT_BLE_TIMESTAMP (roundl(pow(2, 63)) - 1)
144 #define BT_DISC_TYPE_MAX_LEN 7 // br, ble,...
145 #define BT_MAC_LEN 18
146 #define DEFAULT_BT_DISC_TYPE_STR "NO"
147 #define PARSE_UNCOMPRESS_STRING_BUFF_LEN 6 // "true" or "false"
148 #define TRUE_STRING_TAG "true"
149 #define FALSE_STRING_TAG "false"
150
151 /* fast_auth */
152 #define ACCOUNT_HASH "accountHash"
153 #define COMMON_KEY_HASH "keyHash"
154 #define FAST_AUTH "fastauth"
155 #define SOFTBUS_FAST_AUTH "support_fast_auth"
156
157 /* VerifyDevice */
158 #define CODE_VERIFY_DEVICE 2
159 #define DEVICE_ID "DEVICE_ID"
160 #define ENCRYPTED_FAST_AUTH_MAX_LEN 512
161 #define UDID_SHORT_HASH_HEX_STR 16
162 #define UDID_SHORT_HASH_LEN_TEMP 8
163
OptString(const JsonObj * json,const char * const key,char * target,uint32_t targetLen,const char * defaultValue)164 static void OptString(const JsonObj *json, const char * const key,
165 char *target, uint32_t targetLen, const char *defaultValue)
166 {
167 if (JSON_GetStringFromOject(json, key, target, targetLen)) {
168 return;
169 }
170 if (strcpy_s(target, targetLen, defaultValue) != EOK) {
171 ALOGI("set default fail");
172 return;
173 }
174 ALOGI("(%s) prase fail, use default", key);
175 }
176
OptInt(const JsonObj * json,const char * const key,int * target,int defaultValue)177 static void OptInt(const JsonObj *json, const char * const key, int *target, int defaultValue)
178 {
179 if (JSON_GetInt32FromOject(json, key, target)) {
180 return;
181 }
182 ALOGI("(%s) prase fail, use default", key);
183 *target = defaultValue;
184 }
185
OptInt64(const JsonObj * json,const char * const key,int64_t * target,int64_t defaultValue)186 static void OptInt64(const JsonObj *json, const char * const key,
187 int64_t *target, int64_t defaultValue)
188 {
189 if (JSON_GetInt64FromOject(json, key, target)) {
190 return;
191 }
192 ALOGI("(%s) prase fail, use default", key);
193 *target = defaultValue;
194 }
195
OptBool(const JsonObj * json,const char * const key,bool * target,bool defaultValue)196 static void OptBool(const JsonObj *json, const char * const key, bool *target, bool defaultValue)
197 {
198 if (JSON_GetBoolFromOject(json, key, target)) {
199 return;
200 }
201 ALOGI("(%s) prase fail, use default", key);
202 *target = defaultValue;
203 }
204
PackFastAuthValue(JsonObj * obj,AuthDeviceKeyInfo * deviceCommKey)205 static int32_t PackFastAuthValue(JsonObj *obj, AuthDeviceKeyInfo *deviceCommKey)
206 {
207 uint32_t dataLen = 0;
208 uint8_t *data = NULL;
209 AesGcmInputParam aesParam = {0};
210 aesParam.data = (uint8_t *)SOFTBUS_FAST_AUTH;
211 aesParam.dataLen = strlen(SOFTBUS_FAST_AUTH);
212 aesParam.key = deviceCommKey->deviceKey;
213 aesParam.keyLen = deviceCommKey->keyLen;
214 int32_t ret = LnnEncryptAesGcm(&aesParam, (int32_t)deviceCommKey->keyIndex, &data, &dataLen);
215 if (ret != SOFTBUS_OK) {
216 ALOGE("SoftBusEncryptDataWithSeq fail(=%d).", ret);
217 return SOFTBUS_ERR;
218 }
219 if (data == NULL || dataLen == 0) {
220 ALOGE("encrypt data invalid");
221 return SOFTBUS_INVALID_PARAM;
222 }
223 char encryptFastAuth[ENCRYPTED_FAST_AUTH_MAX_LEN] = {0};
224 if (ConvertBytesToUpperCaseHexString(encryptFastAuth, ENCRYPTED_FAST_AUTH_MAX_LEN - 1,
225 data, dataLen) != SOFTBUS_OK) {
226 SoftBusFree(data);
227 return SOFTBUS_ERR;
228 }
229 ALOGD("pack fastAuthTag:%s", encryptFastAuth);
230 JSON_AddStringToObject(obj, FAST_AUTH, encryptFastAuth);
231 SoftBusFree(data);
232 return SOFTBUS_OK;
233 }
234
GetUdidOrShortHash(const AuthSessionInfo * info,char * udidBuf,uint32_t bufLen)235 static bool GetUdidOrShortHash(const AuthSessionInfo *info, char *udidBuf, uint32_t bufLen)
236 {
237 if (strlen(info->udid) != 0) {
238 ALOGI("use info->udid build fastAuthInfo");
239 uint8_t hash[SHA_256_HASH_LEN] = {0};
240 int ret = SoftBusGenerateStrHash((uint8_t *)info->udid, strlen(info->udid), hash);
241 if (ret != SOFTBUS_OK) {
242 ALOGE("generate udidHash fail");
243 return false;
244 }
245 if (ConvertBytesToUpperCaseHexString(udidBuf, bufLen, hash, UDID_SHORT_HASH_LEN_TEMP) != SOFTBUS_OK) {
246 ALOGE("convert bytes to string fail");
247 return false;
248 }
249 return true;
250 }
251 if (info->connInfo.type == AUTH_LINK_TYPE_BLE) {
252 ALOGI("use bleInfo deviceIdHash build fastAuthInfo");
253 AuthRequest request = {0};
254 if (GetAuthRequestNoLock(info->requestId, &request) != SOFTBUS_OK) {
255 LLOGE("GetAuthRequest fail");
256 return false;
257 }
258 return (memcpy_s(udidBuf, bufLen, request.connInfo.info.bleInfo.deviceIdHash,
259 UDID_SHORT_HASH_HEX_STR) == EOK);
260 }
261 return false;
262 }
263
PackFastAuth(JsonObj * obj,AuthSessionInfo * info,const NodeInfo * localNodeInfo)264 static void PackFastAuth(JsonObj *obj, AuthSessionInfo *info, const NodeInfo *localNodeInfo)
265 {
266 ALOGD("pack fastAuth, isServer:%d", info->isServer);
267 bool isNeedPack;
268 if (!info->isServer) {
269 isNeedPack = true;
270 } else if (info->isSupportFastAuth) {
271 isNeedPack = true;
272 } else {
273 ALOGI("unsupport fastAuth");
274 isNeedPack = false;
275 }
276 if (!isNeedPack) {
277 return;
278 }
279 char udidHashHexStr[SHA_256_HEX_HASH_LEN] = {0};
280 if (!GetUdidOrShortHash(info, udidHashHexStr, SHA_256_HEX_HASH_LEN)) {
281 ALOGE("get udid fail, bypass fastAuth");
282 info->isSupportFastAuth = false;
283 return;
284 }
285 ALOGD("udidHashHexStr:%s", udidHashHexStr);
286 if (!IsPotentialTrustedDevice(ID_TYPE_DEVID, (const char *)udidHashHexStr, false)) {
287 ALOGI("not potential trusted realtion, bypass fastAuthProc");
288 info->isSupportFastAuth = false;
289 return;
290 }
291 AuthDeviceKeyInfo deviceCommKey = {0};
292 if (AuthFindDeviceKey(udidHashHexStr, info->connInfo.type, &deviceCommKey) != SOFTBUS_OK) {
293 ALOGW("can't find common key, unsupport fastAuth");
294 info->isSupportFastAuth = false;
295 return;
296 }
297 if (PackFastAuthValue(obj, &deviceCommKey) != SOFTBUS_OK) {
298 info->isSupportFastAuth = false;
299 return;
300 }
301 (void)memset_s(&deviceCommKey, sizeof(AuthDeviceKeyInfo), 0, sizeof(AuthDeviceKeyInfo));
302 }
303
ParseFastAuthValue(AuthSessionInfo * info,const char * encryptedFastAuth,AuthDeviceKeyInfo * deviceKey)304 static void ParseFastAuthValue(AuthSessionInfo *info, const char *encryptedFastAuth,
305 AuthDeviceKeyInfo *deviceKey)
306 {
307 uint8_t fastAuthBytes[ENCRYPTED_FAST_AUTH_MAX_LEN] = {0};
308 if (ConvertHexStringToBytes(fastAuthBytes, ENCRYPTED_FAST_AUTH_MAX_LEN,
309 encryptedFastAuth, strlen(encryptedFastAuth)) != SOFTBUS_OK) {
310 ALOGE("fastAuth data String to bytes fail");
311 return;
312 }
313 uint32_t bytesLen = strlen(encryptedFastAuth) >> 1;
314 uint32_t dataLen = 0;
315 uint8_t *data = NULL;
316 AesGcmInputParam aesParam = {0};
317 aesParam.data = fastAuthBytes;
318 aesParam.dataLen = bytesLen;
319 aesParam.key = deviceKey->deviceKey;
320 aesParam.keyLen = deviceKey->keyLen;
321 int32_t ret = LnnDecryptAesGcm(&aesParam, &data, &dataLen);
322 if (ret != SOFTBUS_OK) {
323 ALOGE("LnnDecryptAesGcm fail(=%d), fastAuth not support", ret);
324 return;
325 }
326 if (data == NULL || dataLen == 0) {
327 ALOGE("decrypt data invalid, fastAuth not support");
328 return;
329 }
330 if (strncmp((char *)data, SOFTBUS_FAST_AUTH, strlen(SOFTBUS_FAST_AUTH)) != 0) {
331 ALOGE("fast auth info error");
332 SoftBusFree(data);
333 return;
334 }
335 ALOGD("parse fastAuth succ");
336 SoftBusFree(data);
337 info->isSupportFastAuth = true;
338 }
339
UnpackFastAuth(JsonObj * obj,AuthSessionInfo * info)340 static void UnpackFastAuth(JsonObj *obj, AuthSessionInfo *info)
341 {
342 info->isSupportFastAuth = false;
343 char encryptedFastAuth[ENCRYPTED_FAST_AUTH_MAX_LEN] = {0};
344 if (!JSON_GetStringFromOject(obj, FAST_AUTH, encryptedFastAuth, ENCRYPTED_FAST_AUTH_MAX_LEN)) {
345 ALOGI("old version or not support fastAuth");
346 return;
347 }
348 ALOGE("unpack fastAuthTag:%s", encryptedFastAuth);
349 uint8_t udidHash[SHA_256_HASH_LEN] = {0};
350 int ret = SoftBusGenerateStrHash((uint8_t *)info->udid, strlen(info->udid), udidHash);
351 if (ret != SOFTBUS_OK) {
352 ALOGE("generate udidHash fail");
353 return;
354 }
355 char udidShortHash[UDID_SHORT_HASH_HEX_STR + 1] = {0};
356 if (ConvertBytesToUpperCaseHexString(udidShortHash, UDID_SHORT_HASH_HEX_STR + 1,
357 udidHash, UDID_SHORT_HASH_LEN_TEMP) != SOFTBUS_OK) {
358 ALOGE("udid hash bytes to hexString fail");
359 return;
360 }
361 if (!IsPotentialTrustedDevice(ID_TYPE_DEVID, (const char *)udidShortHash, false)) {
362 ALOGI("not potential trusted realtion, fastAuth not support");
363 return;
364 }
365 AuthDeviceKeyInfo deviceKey = {0};
366 /* find comm key use udid or udidShortHash */
367 if (AuthFindDeviceKey(udidShortHash, info->connInfo.type, &deviceKey) != SOFTBUS_OK) {
368 ALOGW("can't find common key, fastAuth not support");
369 return;
370 }
371 ParseFastAuthValue(info, encryptedFastAuth, &deviceKey);
372 (void)memset_s(&deviceKey, sizeof(deviceKey), 0, sizeof(deviceKey));
373 }
374
PackCompressInfo(JsonObj * obj,const NodeInfo * info)375 static void PackCompressInfo(JsonObj *obj, const NodeInfo *info)
376 {
377 if (info != NULL) {
378 if (IsFeatureSupport(info->feature, BIT_INFO_COMPRESS)) {
379 JSON_AddStringToObject(obj, SUPPORT_INFO_COMPRESS, TRUE_STRING_TAG);
380 } else {
381 JSON_AddStringToObject(obj, SUPPORT_INFO_COMPRESS, FALSE_STRING_TAG);
382 }
383 }
384 }
385
PackDeviceIdJson(const AuthSessionInfo * info)386 static char *PackDeviceIdJson(const AuthSessionInfo *info)
387 {
388 ALOGI("PackDeviceId: connType = %d.", info->connInfo.type);
389 JsonObj *obj = JSON_CreateObject();
390 if (obj == NULL) {
391 return NULL;
392 }
393 char uuid[UUID_BUF_LEN] = {0};
394 char udid[UDID_BUF_LEN] = {0};
395 char networkId[NETWORK_ID_BUF_LEN] = {0};
396 if (LnnGetLocalStrInfo(STRING_KEY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK ||
397 LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK ||
398 LnnGetLocalStrInfo(STRING_KEY_NETWORKID, networkId, sizeof(networkId)) != SOFTBUS_OK) {
399 ALOGE("get uuid/udid/networkId fail.");
400 JSON_Delete(obj);
401 return NULL;
402 }
403 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && !info->isServer) {
404 if (!JSON_AddStringToObject(obj, CMD_TAG, CMD_GET_AUTH_INFO)) {
405 ALOGE("add CMD_GET fail.");
406 JSON_Delete(obj);
407 return NULL;
408 }
409 } else {
410 if (!JSON_AddStringToObject(obj, CMD_TAG, CMD_RET_AUTH_INFO)) {
411 ALOGE("add CMD_RET fail.");
412 JSON_Delete(obj);
413 return NULL;
414 }
415 }
416 if (info->idType == EXCHANGE_NETWORKID) {
417 if (!JSON_AddStringToObject(obj, DEVICE_ID_TAG, networkId)) {
418 ALOGE("add msg body fail.");
419 JSON_Delete(obj);
420 return NULL;
421 }
422 ALOGI("exchangeIdType=[%d], networkid=[%s]", info->idType, AnonymizesNetworkID(networkId));
423 } else {
424 if (!JSON_AddStringToObject(obj, DEVICE_ID_TAG, udid)) {
425 ALOGE("add msg body fail.");
426 JSON_Delete(obj);
427 return NULL;
428 }
429 ALOGI("exchangeIdType=[%d], udid=[%s]", info->idType, AnonymizesUDID(udid));
430 }
431 if (!JSON_AddStringToObject(obj, DATA_TAG, uuid) ||
432 !JSON_AddInt32ToObject(obj, DATA_BUF_SIZE_TAG, PACKET_SIZE) ||
433 !JSON_AddInt32ToObject(obj, SOFTBUS_VERSION_TAG, info->version) ||
434 !JSON_AddInt32ToObject(obj, EXCHANGE_ID_TYPE, info->idType)) {
435 ALOGE("add msg body fail.");
436 JSON_Delete(obj);
437 return NULL;
438 }
439 const NodeInfo *nodeInfo = LnnGetLocalNodeInfo();
440 PackCompressInfo(obj, nodeInfo);
441 PackFastAuth(obj, (AuthSessionInfo *)info, nodeInfo);
442 char *msg = JSON_PrintUnformatted(obj);
443 JSON_Delete(obj);
444 return msg;
445 }
446
UnPackBtDeviceIdV1(AuthSessionInfo * info,const uint8_t * data,uint32_t len)447 static int32_t UnPackBtDeviceIdV1(AuthSessionInfo *info, const uint8_t *data, uint32_t len)
448 {
449 if (!info->isServer) {
450 ALOGE("invalid bt deviceId msg");
451 return SOFTBUS_ERR;
452 }
453 if (memcpy_s(info->udid, UDID_BUF_LEN, data, len) != EOK) { // data:StandardCharsets.UTF_8
454 ALOGE("memcpy fail");
455 return SOFTBUS_ERR;
456 }
457 return SOFTBUS_OK;
458 }
459
SetCompressFlag(const char * compressCapa,bool * sessionSupportFlag)460 static void SetCompressFlag(const char *compressCapa, bool *sessionSupportFlag)
461 {
462 const NodeInfo *node = LnnGetLocalNodeInfo();
463 if (node == NULL) {
464 return;
465 }
466 bool isRemoteSupportCompress = false;
467 if (strncmp(compressCapa, TRUE_STRING_TAG, strlen(compressCapa)) == 0) {
468 isRemoteSupportCompress = true;
469 } else {
470 isRemoteSupportCompress = false;
471 }
472 if (IsFeatureSupport(node->feature, BIT_INFO_COMPRESS) && isRemoteSupportCompress) {
473 *sessionSupportFlag = true;
474 ALOGI("local-remote all support deviceinfo compress");
475 } else {
476 *sessionSupportFlag = false;
477 }
478 }
479
SetExchangeIdTypeAndValve(JsonObj * obj,AuthSessionInfo * info)480 static int32_t SetExchangeIdTypeAndValve(JsonObj *obj, AuthSessionInfo *info)
481 {
482 int32_t idType = -1;
483 char peerUdid[UDID_BUF_LEN] = {0};
484 if (obj == NULL || info == NULL) {
485 ALOGE("param invalid");
486 return SOFTBUS_ERR;
487 }
488 if (!JSON_GetInt32FromOject(obj, EXCHANGE_ID_TYPE, &idType)) {
489 ALOGI("parse idType failed, ignore");
490 info->idType = EXCHANHE_UDID;
491 return SOFTBUS_OK;
492 }
493 ALOGI("old idType=[%d] exchangeIdType=[%d] deviceId=[%s]", info->idType, idType, AnonymizesUDID(info->udid));
494 if (idType == EXCHANHE_UDID) {
495 info->idType = EXCHANHE_UDID;
496 return SOFTBUS_OK;
497 }
498 if (info->isServer) {
499 if (idType == EXCHANGE_NETWORKID) {
500 if (GetPeerUdidByNetworkId(info->udid, peerUdid) != SOFTBUS_OK) {
501 info->idType = EXCHANGE_FAIL;
502 } else {
503 if (memcpy_s(info->udid, UDID_BUF_LEN, peerUdid, UDID_BUF_LEN) != EOK) {
504 ALOGE("copy peer udid fail");
505 info->idType = EXCHANGE_FAIL;
506 return SOFTBUS_ERR;
507 }
508 info->idType = EXCHANGE_NETWORKID;
509 }
510 }
511 return SOFTBUS_OK;
512 }
513 if (info->idType == EXCHANGE_NETWORKID) {
514 if (idType == EXCHANGE_FAIL) {
515 info->idType = EXCHANGE_FAIL;
516 }
517 if (idType == EXCHANGE_NETWORKID) {
518 if (GetPeerUdidByNetworkId(info->udid, peerUdid) != SOFTBUS_OK) {
519 ALOGE("get peer udid fail, peer networkId=[%s]", AnonymizesUDID(info->udid));
520 info->idType = EXCHANGE_FAIL;
521 } else {
522 if (memcpy_s(info->udid, UDID_BUF_LEN, peerUdid, UDID_BUF_LEN) != EOK) {
523 ALOGE("copy peer udid fail");
524 info->idType = EXCHANGE_FAIL;
525 return SOFTBUS_ERR;
526 }
527 ALOGE("get peer udid success, peer udid=[%s]", AnonymizesUDID(info->udid));
528 info->idType = EXCHANGE_NETWORKID;
529 }
530 }
531 }
532 return SOFTBUS_OK;
533 }
534
UnpackDeviceIdJson(const char * msg,uint32_t len,AuthSessionInfo * info)535 static int32_t UnpackDeviceIdJson(const char *msg, uint32_t len, AuthSessionInfo *info)
536 {
537 JsonObj *obj = JSON_Parse(msg, len);
538 if (obj == NULL) {
539 ALOGE("json parse fail.");
540 return SOFTBUS_ERR;
541 }
542 char cmd[CMD_TAG_LEN] = {0};
543 if (!JSON_GetStringFromOject(obj, CMD_TAG, cmd, CMD_TAG_LEN)) {
544 ALOGE("CMD_TAG not found");
545 JSON_Delete(obj);
546 return SOFTBUS_ERR;
547 }
548 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && info->isServer) {
549 if (strncmp(cmd, CMD_GET_AUTH_INFO, strlen(CMD_GET_AUTH_INFO)) != 0) {
550 ALOGE("CMD_GET not match.");
551 JSON_Delete(obj);
552 return SOFTBUS_ERR;
553 }
554 } else {
555 if (strncmp(cmd, CMD_RET_AUTH_INFO, strlen(CMD_RET_AUTH_INFO)) != 0) {
556 ALOGE("CMD_RET not match.");
557 JSON_Delete(obj);
558 return SOFTBUS_ERR;
559 }
560 }
561 if (!JSON_GetStringFromOject(obj, DATA_TAG, info->uuid, UUID_BUF_LEN)) {
562 ALOGE("uuid not found");
563 JSON_Delete(obj);
564 return SOFTBUS_ERR;
565 }
566 int32_t maxBuffSize;
567 OptString(obj, DEVICE_ID_TAG, info->udid, UDID_BUF_LEN, "");
568 OptInt(obj, DATA_BUF_SIZE_TAG, &maxBuffSize, PACKET_SIZE);
569 if (strlen(info->udid) != 0) {
570 info->version = SOFTBUS_OLD_V2;
571 } else {
572 info->version = SOFTBUS_OLD_V1;
573 if (strcpy_s(info->udid, UDID_BUF_LEN, info->uuid) != EOK) {
574 ALOGE("strcpy udid fail, ignore");
575 }
576 }
577 if (!JSON_GetInt32FromOject(obj, SOFTBUS_VERSION_TAG, (int32_t *)&info->version)) {
578 // info->version = SOFTBUS_OLD_V2;
579 }
580 if (SetExchangeIdTypeAndValve(obj, info) != SOFTBUS_OK) {
581 ALOGE("set exchange id type or valve fail.");
582 JSON_Delete(obj);
583 return SOFTBUS_ERR;
584 }
585 if (info->connInfo.type != AUTH_LINK_TYPE_WIFI) {
586 char compressParse[PARSE_UNCOMPRESS_STRING_BUFF_LEN] = {0};
587 OptString(obj, SUPPORT_INFO_COMPRESS, compressParse,
588 PARSE_UNCOMPRESS_STRING_BUFF_LEN, FALSE_STRING_TAG);
589 SetCompressFlag(compressParse, &info->isSupportCompress);
590 }
591 UnpackFastAuth(obj, info);
592 JSON_Delete(obj);
593 return SOFTBUS_OK;
594 }
595
PackCommonDevInfo(JsonObj * json,const NodeInfo * info,bool isMetaAuth)596 static int32_t PackCommonDevInfo(JsonObj *json, const NodeInfo *info, bool isMetaAuth)
597 {
598 (void)JSON_AddStringToObject(json, UNIFIED_DEVICE_NAME, info->deviceInfo.unifiedName);
599 (void)JSON_AddStringToObject(json, UNIFIED_DISPLAY_DEVICE_NAME, info->deviceInfo.displayName);
600 if (!JSON_AddStringToObject(json, NETWORK_ID, info->networkId) ||
601 !JSON_AddStringToObject(json, DEVICE_NAME, LnnGetDeviceName(&info->deviceInfo)) ||
602 !JSON_AddStringToObject(json, DEVICE_TYPE, LnnConvertIdToDeviceType(info->deviceInfo.deviceTypeId)) ||
603 !JSON_AddStringToObject(json, DEVICE_UDID, LnnGetDeviceUdid(info))) {
604 ALOGE("JSON_AddStringToObject fail.");
605 return SOFTBUS_ERR;
606 }
607 if (isMetaAuth && !JSON_AddStringToObject(json, DEVICE_UUID, info->uuid)) {
608 ALOGE("JSON_AddStringToObject fail.");
609 return SOFTBUS_ERR;
610 }
611 return SOFTBUS_OK;
612 }
613
PackCommonFastAuth(JsonObj * json,const NodeInfo * info)614 static void PackCommonFastAuth(JsonObj *json, const NodeInfo *info)
615 {
616 (void)JSON_AddInt32ToObject(json, STATE_VERSION, info->stateVersion);
617 char extData[EXTDATA_LEN] = {0};
618 int32_t ret = GetExtData(extData, EXTDATA_LEN);
619 if (ret != SOFTBUS_OK) {
620 ALOGE("GetExtData fail.");
621 } else {
622 ALOGI("GetExtData : %s", extData);
623 (void)JSON_AddStringToObject(json, EXTDATA, extData);
624 }
625 }
626
PackCommP2pInfo(JsonObj * json,const NodeInfo * info)627 static void PackCommP2pInfo(JsonObj *json, const NodeInfo *info)
628 {
629 (void)JSON_AddInt32ToObject(json, P2P_ROLE, LnnGetP2pRole(info));
630 (void)JSON_AddStringToObject(json, P2P_MAC_ADDR, LnnGetP2pMac(info));
631 (void)JSON_AddStringToObject(json, HML_MAC, info->wifiDirectAddr);
632
633 (void)JSON_AddStringToObject(json, WIFI_CFG, info->p2pInfo.wifiCfg);
634 (void)JSON_AddStringToObject(json, CHAN_LIST_5G, info->p2pInfo.chanList5g);
635 (void)JSON_AddInt32ToObject(json, STA_FREQUENCY, LnnGetStaFrequency(info));
636 }
637
PackCommon(JsonObj * json,const NodeInfo * info,SoftBusVersion version,bool isMetaAuth)638 static int32_t PackCommon(JsonObj *json, const NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
639 {
640 if (version >= SOFTBUS_NEW_V1) {
641 if (!JSON_AddStringToObject(json, MASTER_UDID, info->masterUdid) ||
642 !JSON_AddInt32ToObject(json, MASTER_WEIGHT, info->masterWeight)) {
643 ALOGE("add master node info fail.");
644 return SOFTBUS_ERR;
645 }
646 if (!JSON_AddStringToObject(json, NODE_ADDR, info->nodeAddress)) {
647 ALOGE("pack node address Fail.");
648 return SOFTBUS_ERR;
649 }
650 }
651 if (!JSON_AddStringToObject(json, SW_VERSION, info->softBusVersion)) {
652 ALOGE("add version info fail.");
653 return SOFTBUS_ERR;
654 }
655 if (PackCommonDevInfo(json, info, isMetaAuth) != SOFTBUS_OK) {
656 return SOFTBUS_ERR;
657 }
658 if (!JSON_AddStringToObject(json, VERSION_TYPE, info->versionType) ||
659 !JSON_AddInt32ToObject(json, CONN_CAP, info->netCapacity) ||
660 !JSON_AddInt16ToObject(json, DATA_CHANGE_FLAG, info->dataChangeFlag) ||
661 !JSON_AddBoolToObject(json, IS_CHARGING, info->batteryInfo.isCharging) ||
662 !JSON_AddBoolToObject(json, BLE_P2P, info->isBleP2p) ||
663 !JSON_AddInt64ToObject(json, TRANSPORT_PROTOCOL, (int64_t)LnnGetSupportedProtocols(info))) {
664 ALOGE("JSON_AddStringToObject fail.");
665 return SOFTBUS_ERR;
666 }
667 char btMacUpper[BT_MAC_LEN] = {0};
668 if (StringToUpperCase(LnnGetBtMac(info), btMacUpper, BT_MAC_LEN) != SOFTBUS_OK) {
669 LLOGE("btMac to upperCase fail.");
670 if (memcpy_s(btMacUpper, BT_MAC_LEN, LnnGetBtMac(info), BT_MAC_LEN) != EOK) {
671 LLOGE("btMac cpy fail.");
672 return SOFTBUS_ERR;
673 }
674 }
675 if (!JSON_AddStringToObject(json, PKG_VERSION, info->pkgVersion) ||
676 !JSON_AddInt64ToObject(json, WIFI_VERSION, info->wifiVersion) ||
677 !JSON_AddInt64ToObject(json, BLE_VERSION, info->bleVersion) ||
678 !JSON_AddStringToObject(json, BT_MAC, btMacUpper) ||
679 !JSON_AddInt32ToObject(json, REMAIN_POWER, info->batteryInfo.batteryLevel) ||
680 !JSON_AddBoolToObject(json, IS_CHARGING, info->batteryInfo.isCharging) ||
681 !JSON_AddBoolToObject(json, IS_SCREENON, info->isScreenOn) ||
682 !JSON_AddInt32ToObject(json, NODE_WEIGHT, info->masterWeight) ||
683 !JSON_AddInt64ToObject(json, ACCOUNT_ID, info->accountId) ||
684 !JSON_AddBoolToObject(json, DISTRIBUTED_SWITCH, true) ||
685 !JSON_AddInt64ToObject(json, BLE_TIMESTAMP, info->bleStartTimestamp) ||
686 !JSON_AddInt32ToObject(json, WIFI_BUFF_SIZE, info->wifiBuffSize) ||
687 !JSON_AddInt32ToObject(json, BR_BUFF_SIZE, info->brBuffSize) ||
688 !JSON_AddInt64ToObject(json, FEATURE, info->feature) ||
689 !JSON_AddInt64ToObject(json, NEW_CONN_CAP, info->netCapacity)) {
690 ALOGE("JSON_AddStringToObject fail.");
691 return SOFTBUS_ERR;
692 }
693 PackCommonFastAuth(json, info);
694 if (!PackCipherKeySyncMsg(json)) {
695 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "PackCipherKeySyncMsg fail.");
696 }
697 PackCommP2pInfo(json, info);
698 return SOFTBUS_OK;
699 }
700
UnpackCommon(const JsonObj * json,NodeInfo * info,SoftBusVersion version,bool isMetaAuth)701 static void UnpackCommon(const JsonObj *json, NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
702 {
703 if (version >= SOFTBUS_NEW_V1) {
704 if (!JSON_GetStringFromOject(json, MASTER_UDID, info->masterUdid, UDID_BUF_LEN) ||
705 !JSON_GetInt32FromOject(json, MASTER_WEIGHT, &info->masterWeight)) {
706 ALOGE("get master node info fail");
707 }
708 ALOGE("get master weight: %d", info->masterWeight);
709 if (!JSON_GetStringFromOject(json, NODE_ADDR, info->nodeAddress, sizeof(info->nodeAddress))) {
710 ALOGW("no node address packed. set to address %s", NODE_ADDR_LOOPBACK);
711 (void)strcpy_s(info->nodeAddress, sizeof(info->nodeAddress), NODE_ADDR_LOOPBACK);
712 }
713 }
714 (void)JSON_GetStringFromOject(json, SW_VERSION, info->softBusVersion, VERSION_MAX_LEN);
715 OptString(json, PKG_VERSION, info->pkgVersion, VERSION_MAX_LEN, "");
716 OptString(json, UNIFIED_DEVICE_NAME, info->deviceInfo.unifiedName, DEVICE_NAME_BUF_LEN, "");
717 OptString(json, UNIFIED_DEFAULT_DEVICE_NAME, info->deviceInfo.unifiedDefaultName, DEVICE_NAME_BUF_LEN, "");
718 OptString(json, UNIFIED_DISPLAY_DEVICE_NAME, info->deviceInfo.displayName, DEVICE_NAME_BUF_LEN, "");
719 OptInt64(json, WIFI_VERSION, &info->wifiVersion, 0);
720 OptInt64(json, BLE_VERSION, &info->bleVersion, 0);
721 OptString(json, BT_MAC, info->connectInfo.macAddr, MAC_LEN, "");
722 char deviceType[DEVICE_TYPE_BUF_LEN] = {0};
723 (void)JSON_GetStringFromOject(json, DEVICE_NAME, info->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN);
724 if (JSON_GetStringFromOject(json, DEVICE_TYPE, deviceType, DEVICE_TYPE_BUF_LEN)) {
725 (void)LnnConvertDeviceTypeToId(deviceType, &(info->deviceInfo.deviceTypeId));
726 }
727 (void)JSON_GetStringFromOject(json, DEVICE_UDID, info->deviceInfo.deviceUdid, UDID_BUF_LEN);
728 if (isMetaAuth) {
729 (void)JSON_GetStringFromOject(json, DEVICE_UUID, info->uuid, UDID_BUF_LEN);
730 }
731 (void)JSON_GetStringFromOject(json, NETWORK_ID, info->networkId, NETWORK_ID_BUF_LEN);
732 (void)JSON_GetStringFromOject(json, VERSION_TYPE, info->versionType, VERSION_MAX_LEN);
733 (void)JSON_GetInt32FromOject(json, CONN_CAP, (int32_t *)&info->netCapacity);
734
735 info->isBleP2p = false;
736 (void)JSON_GetBoolFromOject(json, BLE_P2P, &info->isBleP2p);
737 (void)JSON_GetInt16FromOject(json, DATA_CHANGE_FLAG, (int16_t *)&info->dataChangeFlag);
738 (void)JSON_GetBoolFromOject(json, IS_CHARGING, &info->batteryInfo.isCharging);
739 (void)JSON_GetInt32FromOject(json, REMAIN_POWER, &info->batteryInfo.batteryLevel);
740 OptBool(json, IS_SCREENON, &info->isScreenOn, false);
741 OptInt64(json, ACCOUNT_ID, &info->accountId, 0);
742 OptInt(json, NODE_WEIGHT, &info->masterWeight, DEFAULT_NODE_WEIGHT);
743
744 //IS_SUPPORT_TCP_HEARTBEAT
745 OptInt64(json, NEW_CONN_CAP, (int64_t *)&info->netCapacity, -1);
746 if (info->netCapacity == (uint32_t)-1) {
747 (void)JSON_GetInt64FromOject(json, CONN_CAP, (int64_t *)&info->netCapacity);
748 }
749 OptInt(json, WIFI_BUFF_SIZE, &info->wifiBuffSize, DEFAULT_WIFI_BUFF_SIZE);
750 OptInt(json, BR_BUFF_SIZE, &info->wifiBuffSize, DEFAULT_BR_BUFF_SIZE);
751 OptInt64(json, FEATURE, (int64_t *)&info->feature, 0);
752 //MetaNodeInfoOfEar
753 OptString(json, EXTDATA, info->extData, EXTDATA_LEN, "");
754 if (version == SOFTBUS_OLD_V1) {
755 if (strcpy_s(info->networkId, NETWORK_ID_BUF_LEN, info->uuid) != EOK) {
756 ALOGE("v1 version strcpy networkid fail");
757 }
758 }
759 ProcessCipherKeySyncInfo(json, info->deviceInfo.deviceUdid);
760
761 // unpack p2p info
762 OptInt(json, P2P_ROLE, &info->p2pInfo.p2pRole, -1);
763 OptString(json, WIFI_CFG, info->p2pInfo.wifiCfg, WIFI_CFG_INFO_MAX_LEN, "");
764 OptString(json, CHAN_LIST_5G, info->p2pInfo.chanList5g, CHANNEL_LIST_STR_LEN, "");
765 OptInt(json, STA_FREQUENCY, &info->p2pInfo.staFrequency, -1);
766 OptString(json, P2P_MAC_ADDR, info->p2pInfo.p2pMac, MAC_LEN, "");
767 OptString(json, HML_MAC, info->wifiDirectAddr, MAC_LEN, "");
768 }
769
GetBtDiscTypeString(const NodeInfo * info,char * buf,uint32_t len)770 static int32_t GetBtDiscTypeString(const NodeInfo *info, char *buf, uint32_t len)
771 {
772 uint32_t i = 0;
773 if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BLE)) {
774 CHECK_EXPRESSION_RETURN_VALUE((i >= len), SOFTBUS_ERR);
775 buf[i++] = DISCOVERY_TYPE_BLE + '0';
776 }
777 if (LnnHasDiscoveryType(info, DISCOVERY_TYPE_BR)) {
778 if (i != 0) {
779 CHECK_EXPRESSION_RETURN_VALUE((i >= len), SOFTBUS_ERR);
780 buf[i++] = ',';
781 }
782 CHECK_EXPRESSION_RETURN_VALUE((i >= len), SOFTBUS_ERR);
783 buf[i++] = DISCOVERY_TYPE_BR + '0';
784 }
785 return SOFTBUS_OK;
786 }
787
AddDiscoveryType(JsonObj * json,const char * remoteUuid)788 static void AddDiscoveryType(JsonObj *json, const char *remoteUuid)
789 {
790 if (remoteUuid == NULL) {
791 return;
792 }
793 char networkId[NETWORK_ID_BUF_LEN] = {0};
794 if (LnnGetNetworkIdByUuid(remoteUuid, networkId, sizeof(networkId)) != SOFTBUS_OK) {
795 ALOGI("networkId not found by uuid, maybe first online!");
796 return;
797 }
798 uint32_t discoveryType = 0;
799 if (LnnGetRemoteNumInfo(networkId, NUM_KEY_DISCOVERY_TYPE, (int32_t *)&discoveryType) != SOFTBUS_OK) {
800 ALOGE("get discoveryType fail!");
801 return;
802 }
803 NodeInfo nodeInfo; // only for discType calc
804 (void)memset_s(&nodeInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo));
805 nodeInfo.discoveryType = discoveryType;
806 char discTypeStr[BT_DISC_TYPE_MAX_LEN] = {0};
807 if (GetBtDiscTypeString(&nodeInfo, discTypeStr, BT_DISC_TYPE_MAX_LEN) != SOFTBUS_OK) {
808 ALOGE("disc Type calc fail");
809 return;
810 }
811 ALOGD("pack discType is:%s", discTypeStr);
812 JSON_AddStringToObject(json, DISCOVERY_TYPE, discTypeStr);
813 }
814
PackBt(JsonObj * json,const NodeInfo * info,SoftBusVersion version,bool isMetaAuth,const char * remoteUuid)815 static int32_t PackBt(JsonObj *json, const NodeInfo *info, SoftBusVersion version,
816 bool isMetaAuth, const char *remoteUuid)
817 {
818 if (!JSON_AddInt32ToObject(json, CODE, CODE_VERIFY_BT)) {
819 ALOGE("add bt info fail");
820 return SOFTBUS_ERR;
821 }
822 AddDiscoveryType(json, remoteUuid);
823 if (PackCommon(json, info, version, isMetaAuth) != SOFTBUS_OK) {
824 ALOGE("PackCommon fail");
825 return SOFTBUS_ERR;
826 }
827 return SOFTBUS_OK;
828 }
829
SetDiscType(uint32_t * discType,const char * discStr)830 static int32_t SetDiscType(uint32_t *discType, const char *discStr)
831 {
832 if (strcmp(discStr, DEFAULT_BT_DISC_TYPE_STR) == 0) {
833 ALOGE("disc type can't parse");
834 return SOFTBUS_ERR;
835 }
836 return SOFTBUS_OK;
837 }
838
UnpackBt(const JsonObj * json,NodeInfo * info,SoftBusVersion version,bool isMetaAuth)839 static int32_t UnpackBt(const JsonObj *json, NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
840 {
841 char discTypeStr[BT_DISC_TYPE_MAX_LEN] = {0};
842 if (!JSON_GetInt64FromOject(json, TRANSPORT_PROTOCOL, (int64_t *)&info->supportedProtocols)) {
843 info->supportedProtocols = LNN_PROTOCOL_BR | LNN_PROTOCOL_BLE;
844 }
845 OptString(json, DISCOVERY_TYPE, discTypeStr, BT_DISC_TYPE_MAX_LEN, DEFAULT_BT_DISC_TYPE_STR);
846 (void)SetDiscType(&info->discoveryType, discTypeStr);
847 OptInt64(json, BLE_TIMESTAMP, &info->bleStartTimestamp, DEFAULT_BLE_TIMESTAMP);
848 OptInt(json, STATE_VERSION, &info->stateVersion, 0);
849 UnpackCommon(json, info, version, isMetaAuth);
850 return SOFTBUS_OK;
851 }
852
PackWiFi(JsonObj * json,const NodeInfo * info,SoftBusVersion version,bool isMetaAuth)853 static int32_t PackWiFi(JsonObj *json, const NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
854 {
855 ALOGD("devIp %d", strlen(info->connectInfo.deviceIp));
856 if (!JSON_AddInt32ToObject(json, CODE, CODE_VERIFY_IP) ||
857 !JSON_AddInt32ToObject(json, BUS_MAX_VERSION, BUS_V2) ||
858 !JSON_AddInt32ToObject(json, BUS_MIN_VERSION, BUS_V1) ||
859 !JSON_AddInt32ToObject(json, AUTH_PORT, LnnGetAuthPort(info)) ||
860 !JSON_AddInt32ToObject(json, SESSION_PORT, LnnGetSessionPort(info)) ||
861 !JSON_AddInt32ToObject(json, PROXY_PORT, LnnGetProxyPort(info)) ||
862 !JSON_AddStringToObject(json, DEV_IP, info->connectInfo.deviceIp)) {
863 ALOGE("add wifi info fail.");
864 return SOFTBUS_ERR;
865 }
866 char offlineCode[BASE64_OFFLINE_CODE_LEN] = {0};
867 size_t len = 0;
868 ALOGE("offlineCode %d, %d", strlen(offlineCode), sizeof(info->offlineCode));
869 int32_t ret = SoftBusBase64Encode((unsigned char*)offlineCode, BASE64_OFFLINE_CODE_LEN, &len,
870 (unsigned char*)info->offlineCode, sizeof(info->offlineCode));
871 if (ret != 0) {
872 ALOGE("mbedtls base64 encode failed.");
873 return SOFTBUS_ERR;
874 }
875 (void)JSON_AddStringToObject(json, BLE_OFFLINE_CODE, offlineCode);
876 if (PackCommon(json, info, version, isMetaAuth) != SOFTBUS_OK) {
877 ALOGE("PackCommon fail");
878 return SOFTBUS_ERR;
879 }
880 return SOFTBUS_OK;
881 }
882
CheckBusVersion(const JsonObj * json)883 static int32_t CheckBusVersion(const JsonObj *json)
884 {
885 int32_t maxVersion;
886 int32_t minVersion;
887 OptInt(json, BUS_MAX_VERSION, &maxVersion, -1);
888 OptInt(json, BUS_MIN_VERSION, &minVersion, -1);
889 if (maxVersion > BUS_V2) {
890 maxVersion = BUS_V2;
891 }
892 if (minVersion < BUS_V1) {
893 minVersion = BUS_V1;
894 }
895 if (maxVersion < 0 || maxVersion < minVersion) {
896 ALOGE("no common version");
897 return SOFTBUS_ERR;
898 }
899 return maxVersion;
900 }
901
UnpackWiFi(const JsonObj * json,NodeInfo * info,SoftBusVersion version,bool isMetaAuth)902 static int32_t UnpackWiFi(const JsonObj *json, NodeInfo *info, SoftBusVersion version, bool isMetaAuth)
903 {
904 if (CheckBusVersion(json) < 0) {
905 return SOFTBUS_ERR;
906 }
907 (void)JSON_GetInt32FromOject(json, AUTH_PORT, &info->connectInfo.authPort);
908 (void)JSON_GetInt32FromOject(json, SESSION_PORT, &info->connectInfo.sessionPort);
909 (void)JSON_GetInt32FromOject(json, PROXY_PORT, &info->connectInfo.proxyPort);
910 if (!JSON_GetInt64FromOject(json, TRANSPORT_PROTOCOL, (int64_t *)&info->supportedProtocols)) {
911 info->supportedProtocols = LNN_PROTOCOL_IP;
912 }
913 char offlineCode[BASE64_OFFLINE_CODE_LEN] = {0};
914 OptString(json, DEV_IP, info->connectInfo.deviceIp, MAX_ADDR_LEN, ""); // check ip available
915 OptString(json, BLE_OFFLINE_CODE, offlineCode, BASE64_OFFLINE_CODE_LEN, "");
916 size_t len;
917 if (SoftBusBase64Decode(info->offlineCode, OFFLINE_CODE_BYTE_SIZE,
918 &len, (const unsigned char *)offlineCode, strlen(offlineCode)) != 0) {
919 ALOGE("base64Decode fail");
920 }
921 if (len != OFFLINE_CODE_BYTE_SIZE) {
922 ALOGE("base64Decode data err");
923 }
924 UnpackCommon(json, info, version, isMetaAuth);
925 return SOFTBUS_OK;
926 }
927
PackDeviceInfoBtV1(JsonObj * json,const NodeInfo * info,bool isMetaAuth)928 static int32_t PackDeviceInfoBtV1(JsonObj *json, const NodeInfo *info, bool isMetaAuth)
929 {
930 ALOGI("pack deviceInfo bt-v1");
931 if (!JSON_AddStringToObject(json, DEVICE_NAME, LnnGetDeviceName(&info->deviceInfo)) ||
932 !JSON_AddStringToObject(json, DEVICE_TYPE, LnnConvertIdToDeviceType(info->deviceInfo.deviceTypeId)) ||
933 !JSON_AddStringToObject(json, DEVICE_VERSION_TYPE, info->versionType) ||
934 !JSON_AddStringToObject(json, BR_MAC_ADDR, LnnGetBtMac(info)) ||
935 !JSON_AddStringToObject(json, P2P_MAC_ADDR, LnnGetP2pMac(info)) ||
936 !JSON_AddStringToObject(json, UUID, info->uuid) ||
937 !JSON_AddStringToObject(json, SW_VERSION, info->softBusVersion) ||
938 !JSON_AddStringToObject(json, DEVICE_UDID, info->deviceInfo.deviceUdid) ||
939 !JSON_AddInt64ToObject(json, WIFI_VERSION, info->wifiVersion) ||
940 !JSON_AddInt64ToObject(json, BLE_VERSION, info->bleVersion) ||
941 !JSON_AddInt64ToObject(json, CONN_CAP, info->netCapacity) ||
942 !JSON_AddInt64ToObject(json, NEW_CONN_CAP, info->netCapacity) ||
943 !JSON_AddStringToObject(json, BT_MAC, info->connectInfo.macAddr) ||
944 !JSON_AddStringToObject(json, HML_MAC, info->wifiDirectAddr) ||
945 !JSON_AddInt32ToObject(json, REMAIN_POWER, info->batteryInfo.batteryLevel) ||
946 !JSON_AddBoolToObject(json, IS_CHARGING, info->batteryInfo.isCharging) ||
947 !JSON_AddBoolToObject(json, IS_SCREENON, info->isScreenOn) ||
948 !JSON_AddInt32ToObject(json, P2P_ROLE, info->p2pInfo.p2pRole) ||
949 !JSON_AddInt64ToObject(json, ACCOUNT_ID, info->accountId) ||
950 !JSON_AddInt32ToObject(json, NODE_WEIGHT, info->masterWeight)) {
951 ALOGE("add wifi info fail");
952 return SOFTBUS_ERR;
953 }
954 return SOFTBUS_OK;
955 }
956
UnpackDeviceInfoBtV1(const JsonObj * json,NodeInfo * info)957 static int32_t UnpackDeviceInfoBtV1(const JsonObj *json, NodeInfo *info)
958 {
959 char deviceType[DEVICE_TYPE_BUF_LEN] = {0};
960 if (!JSON_GetStringFromOject(json, DEVICE_NAME, info->deviceInfo.deviceName, DEVICE_NAME_BUF_LEN) ||
961 !JSON_GetStringFromOject(json, DEVICE_TYPE, deviceType, DEVICE_TYPE_BUF_LEN) ||
962 !JSON_GetStringFromOject(json, DEVICE_UDID, info->deviceInfo.deviceUdid, UDID_BUF_LEN) ||
963 !JSON_GetStringFromOject(json, UUID, info->uuid, UUID_BUF_LEN) ||
964 !JSON_GetStringFromOject(json, BR_MAC_ADDR, info->connectInfo.macAddr, MAC_LEN)) {
965 ALOGE("prase devinfo fail, invalid msg");
966 return SOFTBUS_ERR;
967 }
968 (void)LnnConvertDeviceTypeToId(deviceType, &(info->deviceInfo.deviceTypeId));
969 OptString(json, HML_MAC, info->wifiDirectAddr, MAC_LEN, "");
970 OptString(json, P2P_MAC_ADDR, info->p2pInfo.p2pMac, MAC_LEN, "");
971 OptString(json, SW_VERSION, info->softBusVersion, VERSION_MAX_LEN, "");
972 OptString(json, DEVICE_VERSION_TYPE, info->versionType, VERSION_MAX_LEN, "");
973 OptInt64(json, WIFI_VERSION, &info->wifiVersion, 0);
974 OptInt64(json, BLE_VERSION, &info->bleVersion, 0);
975 OptInt(json, REMAIN_POWER, &info->batteryInfo.batteryLevel, DEFAULT_BATTERY_LEVEL);
976 OptBool(json, IS_CHARGING, &info->batteryInfo.isCharging, false);
977 OptBool(json, IS_SCREENON, &info->isScreenOn, false);
978 OptInt(json, P2P_ROLE, &info->p2pInfo.p2pRole, 0);
979 OptInt64(json, ACCOUNT_ID, &info->accountId, 0);
980 OptInt(json, NODE_WEIGHT, &info->masterWeight, DEFAULT_NODE_WEIGHT);
981 OptInt64(json, FEATURE, (int64_t *)&info->feature, 0);
982 OptInt64(json, NEW_CONN_CAP, (int64_t *)&info->netCapacity, -1);
983 if (info->netCapacity == (uint32_t)-1) {
984 OptInt64(json, CONN_CAP, (int64_t *)&info->netCapacity, 0);
985 }
986 if (strcpy_s(info->networkId, NETWORK_ID_BUF_LEN, info->uuid) != EOK) {
987 ALOGE("strcpy networkId fail");
988 }
989 return SOFTBUS_OK;
990 }
991
PackDeviceInfoMessage(int32_t linkType,SoftBusVersion version,bool isMetaAuth,const char * remoteUuid)992 char *PackDeviceInfoMessage(int32_t linkType, SoftBusVersion version, bool isMetaAuth, const char *remoteUuid)
993 {
994 ALOGI("PackDeviceInfo: connType = %d.", linkType);
995 const NodeInfo *info = LnnGetLocalNodeInfo();
996 if (info == NULL) {
997 ALOGE("local info is null.");
998 return NULL;
999 }
1000 JsonObj *json = JSON_CreateObject();
1001 if (json == NULL) {
1002 ALOGE("create cjson fail.");
1003 return NULL;
1004 }
1005 int32_t ret;
1006 if (linkType == AUTH_LINK_TYPE_WIFI) {
1007 ret = PackWiFi(json, info, version, isMetaAuth);
1008 } else if (version == SOFTBUS_OLD_V1) {
1009 ret = PackDeviceInfoBtV1(json, info, isMetaAuth);
1010 } else {
1011 ret = PackBt(json, info, version, isMetaAuth, remoteUuid);
1012 }
1013 if (ret != SOFTBUS_OK) {
1014 JSON_Delete(json);
1015 return NULL;
1016 }
1017
1018 char *msg = JSON_PrintUnformatted(json);
1019 if (msg == NULL) {
1020 ALOGE("JSON_PrintUnformatted fail.");
1021 }
1022 JSON_Delete(json);
1023 return msg;
1024 }
1025
UnpackDeviceInfoMessage(const DevInfoData * devInfo,NodeInfo * nodeInfo,bool isMetaAuth)1026 int32_t UnpackDeviceInfoMessage(const DevInfoData *devInfo, NodeInfo *nodeInfo, bool isMetaAuth)
1027 {
1028 CHECK_NULL_PTR_RETURN_VALUE(devInfo, SOFTBUS_INVALID_PARAM);
1029 CHECK_NULL_PTR_RETURN_VALUE(nodeInfo, SOFTBUS_INVALID_PARAM);
1030 ALOGI("UnpackDeviceInfo: connType = %d.", devInfo->linkType);
1031 JsonObj *json = JSON_Parse(devInfo->msg, devInfo->len);
1032 if (json == NULL) {
1033 ALOGE("parse cjson fail.");
1034 return SOFTBUS_ERR;
1035 }
1036 int32_t ret;
1037 if (devInfo->linkType == AUTH_LINK_TYPE_WIFI) {
1038 ret = UnpackWiFi(json, nodeInfo, devInfo->version, isMetaAuth);
1039 } else if (devInfo->version == SOFTBUS_OLD_V1) {
1040 ret = UnpackDeviceInfoBtV1(json, nodeInfo);
1041 } else {
1042 ret = UnpackBt(json, nodeInfo, devInfo->version, isMetaAuth);
1043 }
1044 JSON_Delete(json);
1045 return ret;
1046 }
1047
PostDeviceIdData(int64_t authSeq,const AuthSessionInfo * info,uint8_t * data,uint32_t len)1048 static int32_t PostDeviceIdData(int64_t authSeq, const AuthSessionInfo *info, uint8_t *data, uint32_t len)
1049 {
1050 AuthDataHead head = {
1051 .dataType = DATA_TYPE_DEVICE_ID,
1052 .module = MODULE_TRUST_ENGINE,
1053 .seq = authSeq,
1054 .flag = info->isServer ? SERVER_SIDE_FLAG : CLIENT_SIDE_FLAG,
1055 .len = len,
1056 };
1057 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
1058 ALOGE("post device id fail");
1059 return SOFTBUS_ERR;
1060 }
1061 return SOFTBUS_OK;
1062 }
1063
PostBtV1DevId(int64_t authSeq,const AuthSessionInfo * info)1064 static int32_t PostBtV1DevId(int64_t authSeq, const AuthSessionInfo *info)
1065 {
1066 if (!info->isServer) {
1067 ALOGE("client don't send Bt-v1 devId");
1068 return SOFTBUS_ERR;
1069 }
1070 char uuid[UUID_BUF_LEN] = {0};
1071 if (LnnGetLocalStrInfo(STRING_KEY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK) {
1072 ALOGE("get uuid fail");
1073 return SOFTBUS_ERR;
1074 }
1075 return PostDeviceIdData(authSeq, info, (uint8_t *)uuid, strlen(uuid));
1076 }
1077
PostWifiV1DevId(int64_t authSeq,const AuthSessionInfo * info)1078 static int32_t PostWifiV1DevId(int64_t authSeq, const AuthSessionInfo *info)
1079 {
1080 if (!info->isServer) {
1081 ALOGE("client don't send wifi-v1 devId");
1082 return SOFTBUS_ERR;
1083 }
1084 char *msg = PackDeviceIdJson(info);
1085 if (msg == NULL) {
1086 ALOGE("pack devId fail");
1087 return SOFTBUS_ERR;
1088 }
1089 if (PostDeviceIdData(authSeq, info, (uint8_t *)msg, strlen(msg) + 1) != SOFTBUS_OK) {
1090 JSON_Free(msg);
1091 return SOFTBUS_ERR;
1092 }
1093 JSON_Free(msg);
1094 return SOFTBUS_OK;
1095 }
1096
PostDeviceIdV1(int64_t authSeq,const AuthSessionInfo * info)1097 static int32_t PostDeviceIdV1(int64_t authSeq, const AuthSessionInfo *info)
1098 {
1099 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI) {
1100 return PostWifiV1DevId(authSeq, info);
1101 } else {
1102 ALOGI("process v1 bt deviceIdSync");
1103 return PostBtV1DevId(authSeq, info);
1104 }
1105 }
1106
PostDeviceIdNew(int64_t authSeq,const AuthSessionInfo * info)1107 static int32_t PostDeviceIdNew(int64_t authSeq, const AuthSessionInfo *info)
1108 {
1109 char *msg = PackDeviceIdJson(info);
1110 if (msg == NULL) {
1111 ALOGE("pack devId fail");
1112 return SOFTBUS_ERR;
1113 }
1114 if (PostDeviceIdData(authSeq, info, (uint8_t *)msg, strlen(msg) + 1) != SOFTBUS_OK) {
1115 JSON_Free(msg);
1116 return SOFTBUS_ERR;
1117 }
1118 JSON_Free(msg);
1119 return SOFTBUS_OK;
1120 }
1121
PostDeviceIdMessage(int64_t authSeq,const AuthSessionInfo * info)1122 int32_t PostDeviceIdMessage(int64_t authSeq, const AuthSessionInfo *info)
1123 {
1124 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1125 if (info->version == SOFTBUS_OLD_V1) {
1126 return PostDeviceIdV1(authSeq, info);
1127 } else {
1128 return PostDeviceIdNew(authSeq, info);
1129 }
1130 }
1131
ProcessDeviceIdMessage(AuthSessionInfo * info,const uint8_t * data,uint32_t len)1132 int32_t ProcessDeviceIdMessage(AuthSessionInfo *info, const uint8_t *data, uint32_t len)
1133 {
1134 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1135 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
1136 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) &&
1137 (len == DEVICE_ID_STR_LEN) &&
1138 (info->isServer)) {
1139 info->version = SOFTBUS_OLD_V1;
1140 return UnPackBtDeviceIdV1(info, data, len);
1141 }
1142 return UnpackDeviceIdJson((const char *)data, len, info);
1143 }
1144
GetSessionKeyList(int64_t authSeq,const AuthSessionInfo * info,SessionKeyList * list)1145 static void GetSessionKeyList(int64_t authSeq, const AuthSessionInfo *info, SessionKeyList *list)
1146 {
1147 ListInit(list);
1148 SessionKey sessionKey;
1149 if (AuthManagerGetSessionKey(authSeq, info, &sessionKey) != SOFTBUS_OK) {
1150 ALOGE("get session key fail.");
1151 return;
1152 }
1153 if (AddSessionKey(list, TO_INT32(authSeq), &sessionKey) != SOFTBUS_OK) {
1154 ALOGE("add session key fail.");
1155 return;
1156 }
1157 }
1158
PostDeviceInfoMessage(int64_t authSeq,const AuthSessionInfo * info)1159 int32_t PostDeviceInfoMessage(int64_t authSeq, const AuthSessionInfo *info)
1160 {
1161 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1162 char *msg = PackDeviceInfoMessage(info->connInfo.type, info->version, false, info->uuid);
1163 if (msg == NULL) {
1164 ALOGE("pack device info fail.");
1165 return SOFTBUS_ERR;
1166 }
1167 int32_t compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
1168 uint8_t *compressData = NULL;
1169 uint32_t compressLen = 0;
1170 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
1171 ALOGI("before compress, datalen:%d", strlen(msg) + 1);
1172 if (DataCompress((uint8_t *)msg, strlen(msg) + 1, &compressData, &compressLen) != SOFTBUS_OK) {
1173 compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
1174 } else {
1175 compressFlag = FLAG_COMPRESS_DEVICE_INFO;
1176 ALOGI("deviceInfo compress finish");
1177 }
1178 ALOGI("after compress, datalen:%d", compressLen);
1179 }
1180 uint8_t *inputData = NULL;
1181 uint32_t inputLen;
1182 uint8_t *data = NULL;
1183 uint32_t dataLen = 0;
1184 if ((compressData != NULL) && (compressLen != 0)) {
1185 inputData = compressData;
1186 inputLen = compressLen;
1187 } else {
1188 inputData = (uint8_t *)msg;
1189 inputLen = strlen(msg) + 1;
1190 }
1191 SessionKeyList sessionKeyList;
1192 GetSessionKeyList(authSeq, info, &sessionKeyList);
1193 if (EncryptInner(&sessionKeyList, inputData, inputLen, &data, &dataLen) != SOFTBUS_OK) {
1194 ALOGE("encrypt device info fail.");
1195 JSON_Free(msg);
1196 SoftBusFree(compressData);
1197 return SOFTBUS_ENCRYPT_ERR;
1198 }
1199 JSON_Free(msg);
1200 SoftBusFree(compressData);
1201 if (info->connInfo.type == AUTH_LINK_TYPE_WIFI && info->isServer) {
1202 compressFlag = FLAG_RELAY_DEVICE_INFO;
1203 authSeq = 0;
1204 }
1205 AuthDataHead head = {
1206 .dataType = DATA_TYPE_DEVICE_INFO,
1207 .module = MODULE_AUTH_CONNECTION,
1208 .seq = authSeq,
1209 .flag = compressFlag,
1210 .len = dataLen,
1211 };
1212 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
1213 ALOGE("post device info fail.");
1214 SoftBusFree(data);
1215 return SOFTBUS_ERR;
1216 }
1217 SoftBusFree(data);
1218 return SOFTBUS_OK;
1219 }
1220
ProcessDeviceInfoMessage(int64_t authSeq,AuthSessionInfo * info,const uint8_t * data,uint32_t len)1221 int32_t ProcessDeviceInfoMessage(int64_t authSeq, AuthSessionInfo *info, const uint8_t *data, uint32_t len)
1222 {
1223 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1224 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
1225 uint8_t *msg = NULL;
1226 uint32_t msgSize = 0;
1227 SessionKeyList sessionKeyList;
1228 GetSessionKeyList(authSeq, info, &sessionKeyList);
1229 if (DecryptInner(&sessionKeyList, data, len, &msg, &msgSize) != SOFTBUS_OK) {
1230 ALOGE("decrypt device info fail");
1231 return SOFTBUS_DECRYPT_ERR;
1232 }
1233 uint8_t *decompressData = NULL;
1234 uint32_t decompressLen = 0;
1235 if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
1236 ALOGI("before decompress, msgSize:%d", msgSize);
1237 if (DataDecompress((uint8_t *)msg, msgSize, &decompressData, &decompressLen) != SOFTBUS_OK) {
1238 ALOGE("data decompress fail");
1239 SoftBusFree(msg);
1240 return SOFTBUS_ERR;
1241 } else {
1242 ALOGI("deviceInfo deCompress finish, decompress:%d", decompressLen);
1243 }
1244 ALOGI("after decompress, datalen:%d", decompressLen);
1245 }
1246 DevInfoData devInfo = {NULL, 0, info->connInfo.type, info->version};
1247 if ((decompressData != NULL) && (decompressLen != 0)) {
1248 devInfo.msg = (const char *)decompressData;
1249 devInfo.len = decompressLen;
1250 } else {
1251 devInfo.msg = (const char *)msg;
1252 devInfo.len = msgSize;
1253 }
1254 if (UnpackDeviceInfoMessage(&devInfo, &info->nodeInfo, false) != SOFTBUS_OK) {
1255 ALOGE("unpack device info fail");
1256 SoftBusFree(msg);
1257 SoftBusFree(decompressData);
1258 return SOFTBUS_ERR;
1259 }
1260 SoftBusFree(msg);
1261 SoftBusFree(decompressData);
1262 return SOFTBUS_OK;
1263 }
1264
PostCloseAckMessage(int64_t authSeq,const AuthSessionInfo * info)1265 int32_t PostCloseAckMessage(int64_t authSeq, const AuthSessionInfo *info)
1266 {
1267 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1268 const char *msg = "";
1269 AuthDataHead head = {
1270 .dataType = DATA_TYPE_CLOSE_ACK,
1271 .module = 0,
1272 .seq = authSeq,
1273 .flag = 0,
1274 .len = strlen(msg) + 1,
1275 };
1276 if (PostAuthData(info->connId, !info->isServer, &head, (uint8_t *)msg) != SOFTBUS_OK) {
1277 ALOGE("post close ack fail.");
1278 return SOFTBUS_ERR;
1279 }
1280 return SOFTBUS_OK;
1281 }
1282
PostHichainAuthMessage(int64_t authSeq,const AuthSessionInfo * info,const uint8_t * data,uint32_t len)1283 int32_t PostHichainAuthMessage(int64_t authSeq, const AuthSessionInfo *info, const uint8_t *data, uint32_t len)
1284 {
1285 CHECK_NULL_PTR_RETURN_VALUE(info, SOFTBUS_INVALID_PARAM);
1286 CHECK_NULL_PTR_RETURN_VALUE(data, SOFTBUS_INVALID_PARAM);
1287 AuthDataHead head = {
1288 .dataType = DATA_TYPE_AUTH,
1289 .module = MODULE_AUTH_SDK,
1290 .seq = authSeq,
1291 .flag = 0,
1292 .len = len,
1293 };
1294 if (PostAuthData(info->connId, !info->isServer, &head, data) != SOFTBUS_OK) {
1295 ALOGE("post hichain data fail.");
1296 return SOFTBUS_ERR;
1297 }
1298 return SOFTBUS_OK;
1299 }
1300
PackVerifyDeviceMessage(const char * uuid)1301 static char *PackVerifyDeviceMessage(const char *uuid)
1302 {
1303 JsonObj *obj = JSON_CreateObject();
1304 if (obj == NULL) {
1305 ALOGE("create json fail.");
1306 return NULL;
1307 }
1308 if (!JSON_AddInt32ToObject(obj, CODE, CODE_VERIFY_DEVICE) ||
1309 !JSON_AddStringToObject(obj, DEVICE_ID, uuid)) {
1310 ALOGE("add uuid fail.");
1311 JSON_Delete(obj);
1312 return NULL;
1313 }
1314 char *msg = JSON_PrintUnformatted(obj);
1315 JSON_Delete(obj);
1316 return msg;
1317 }
1318
IsFlushDevicePacket(const AuthConnInfo * connInfo,const AuthDataHead * head,const uint8_t * data,bool isServer)1319 bool IsFlushDevicePacket(const AuthConnInfo *connInfo, const AuthDataHead *head, const uint8_t *data, bool isServer)
1320 {
1321 if (connInfo->type != AUTH_LINK_TYPE_WIFI) {
1322 return false;
1323 }
1324 int64_t authId = AuthDeviceGetIdByConnInfo(connInfo, isServer);
1325 if (authId == AUTH_INVALID_ID) {
1326 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "is flush device packet not find authId");
1327 return false;
1328 }
1329 uint32_t decDataLen = AuthGetDecryptSize(head->len);
1330 uint8_t *decData = (uint8_t *)SoftBusCalloc(decDataLen);
1331 if (decData == NULL) {
1332 return false;
1333 }
1334 if (AuthDeviceDecrypt(authId, data, head->len, decData, &decDataLen) != SOFTBUS_OK) {
1335 SoftBusFree(decData);
1336 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "parse device info decrypt fail");
1337 return false;
1338 }
1339 JsonObj *json = JSON_Parse((char *)decData, decDataLen);
1340 if (json == NULL) {
1341 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "parse json fail.");
1342 SoftBusFree(decData);
1343 return false;
1344 }
1345 bool result = false;
1346 int32_t verifyDevice = 0;
1347 if (!JSON_GetInt32FromOject(json, CODE, &verifyDevice)) {
1348 SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "parse device info fail");
1349 }
1350 if (verifyDevice == CODE_VERIFY_DEVICE) {
1351 result = true;
1352 }
1353 JSON_Delete(json);
1354 SoftBusFree(decData);
1355 return result;
1356 }
1357
PostVerifyDeviceMessage(const AuthManager * auth,int32_t flagRelay)1358 int32_t PostVerifyDeviceMessage(const AuthManager *auth, int32_t flagRelay)
1359 {
1360 CHECK_NULL_PTR_RETURN_VALUE(auth, SOFTBUS_INVALID_PARAM);
1361 char *msg = PackVerifyDeviceMessage(auth->uuid);
1362 if (msg == NULL) {
1363 ALOGE("pack verify device msg fail.");
1364 return SOFTBUS_ERR;
1365 }
1366
1367 uint8_t *data = NULL;
1368 uint32_t dataLen = 0;
1369 if (EncryptInner(&auth->sessionKeyList, (uint8_t *)msg, strlen(msg) + 1, &data, &dataLen) != SOFTBUS_OK) {
1370 ALOGE("encrypt device info fail.");
1371 JSON_Free(msg);
1372 return SOFTBUS_ENCRYPT_ERR;
1373 }
1374 JSON_Free(msg);
1375
1376 AuthDataHead head = {
1377 .dataType = DATA_TYPE_DEVICE_INFO,
1378 .module = MODULE_AUTH_CONNECTION,
1379 .seq = auth->authId,
1380 .flag = flagRelay,
1381 .len = dataLen,
1382 };
1383 if (PostAuthData(auth->connId, !auth->isServer, &head, data) != SOFTBUS_OK) {
1384 ALOGE("post verify device msg fail.");
1385 SoftBusFree(data);
1386 return SOFTBUS_ERR;
1387 }
1388 SoftBusFree(data);
1389 return SOFTBUS_OK;
1390 }
1391