From ce907e388755df6c640ae60a259f11b1a9651b18 Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Wed, 2 Mar 2022 09:52:24 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5deviceInfo=E4=B8=AD?= =?UTF-8?q?=E7=9A=84Id=E6=98=AFnetworkId=E8=BF=98=E6=98=AFUDID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../native_cpp/include/dm_device_info.h | 1 + .../kits/js/src/native_devicemanager_js.cpp | 2 + .../dependency/softbus/softbus_connector.cpp | 31 +++++++- utils/BUILD.gn | 3 + utils/include/dm_hash.h | 33 ++++++++ utils/src/dm_hash.cpp | 77 +++++++++++++++++++ 6 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 utils/include/dm_hash.h create mode 100644 utils/src/dm_hash.cpp diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index b17b597b1..bf37c5ab5 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -49,6 +49,7 @@ typedef struct DmDeviceInfo { char deviceId[DM_MAX_DEVICE_ID_LEN]; char deviceName[DM_MAX_DEVICE_NAME_LEN]; uint16_t deviceTypeId; + std::string networkId; } DmDeviceInfo; typedef struct DmAuthParam { diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index bb96c37a7..2679ae001 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -398,6 +398,7 @@ void DeviceManagerNapi::OnDeviceStateChange(DmNapiDevStateChangeAction action, napi_value device = nullptr; napi_create_object(env_, &device); SetValueUtf8String(env_, "deviceId", deviceInfo.deviceId, device); + SetValueUtf8String(env_, "networkId", deviceInfo.networkId, device); SetValueUtf8String(env_, "deviceName", deviceInfo.deviceName, device); SetValueInt32(env_, "deviceType", (int)deviceInfo.deviceTypeId, device); @@ -415,6 +416,7 @@ void DeviceManagerNapi::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo & napi_value device = nullptr; napi_create_object(env_, &device); SetValueUtf8String(env_, "deviceId", deviceInfo.deviceId, device); + SetValueUtf8String(env_, "networkId", deviceInfo.networkId, device); SetValueUtf8String(env_, "deviceName", deviceInfo.deviceName, device); SetValueInt32(env_, "deviceType", (int)deviceInfo.deviceTypeId, device); diff --git a/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp b/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp index 4ece01e32..579d9f0eb 100644 --- a/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp +++ b/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp @@ -25,11 +25,12 @@ #include "nlohmann/json.hpp" #include "parameter.h" #include "system_ability_definition.h" +#include "dm_hash.h" namespace OHOS { namespace DistributedHardware { SoftbusConnector::PulishStatus SoftbusConnector::publishStatus = SoftbusConnector::STATUS_UNKNOWN; -std::map> SoftbusConnector::discoveryDeviceInfoMap_ = {}; +std::map> SoftbusConnector::discoveryDeviceInfoMap_ = {};//比对 std::map> SoftbusConnector::stateCallbackMap_ = {}; std::map> SoftbusConnector::discoveryCallbackMap_ = {}; @@ -399,15 +400,36 @@ ConnectionAddr *SoftbusConnector::GetConnectAddr(const std::string &deviceId, st int32_t SoftbusConnector::CovertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeBasicInfo, DmDeviceInfo &dmDeviceInfo) { (void)memset_s(&dmDeviceInfo, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo)); - if (memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId), nodeBasicInfo.networkId, - std::min(sizeof(dmDeviceInfo.deviceId), sizeof(nodeBasicInfo.networkId))) != DM_OK) { - LOGE("copy data failed"); + + uint8_t mUdid[UDID_BUF_LEN] = {0}; + + int32_t ret = + GetNodeKeyInfo(DM_PKG_NAME.c_str(), nodeBasicInfo.networkId, NodeDeviceInfoKey::NODE_KEY_UDID, mUdid, UDID_BUF_LEN); + + if (ret != DM_OK) { + LOGE("GetUdidByNetworkId GetNodeKeyInfo failed"); + return DM_FAILED; } + + + uint8_t outudiddData[32]; + if(GetUdidHash(mUdid,sizeof(mUdid),outudiddData) != DM_OK) { + LOGE("GetUdidHash is failed"); + return DM_FAILED; + } + if (memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), nodeBasicInfo.deviceName, std::min(sizeof(dmDeviceInfo.deviceName), sizeof(nodeBasicInfo.deviceName))) != DM_OK) { LOGE("copy data failed"); } + + if (memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId),outudiddData, + std::min(sizeof(dmDeviceInfo.deviceId), sizeof(outudiddData))) != DM_OK) { + LOGE("copy data failed"); + } + dmDeviceInfo.deviceTypeId = nodeBasicInfo.deviceTypeId; + dmDeviceInfo.networkId = nodeBasicInfo.networkId; return DM_OK; } @@ -423,6 +445,7 @@ void SoftbusConnector::CovertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, LOGE("copy data failed"); } dmDeviceInfo.deviceTypeId = deviceInfo.devType; + dmDeviceInfo.networkId = {0}; } void SoftbusConnector::OnPublishSuccess(int32_t publishId) diff --git a/utils/BUILD.gn b/utils/BUILD.gn index f1c17e3ee..96688fc80 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -31,6 +31,8 @@ if (defined(ohos_lite)) { "${common_path}/include/ipc", "${common_path}/include/ipc/model", "include/permission/standard", + "//third_party/mbedtls/include/mbedtls", + "//base/account/os_account/interfaces/innerkits/osaccount/native/include", ] } @@ -40,6 +42,7 @@ if (defined(ohos_lite)) { "src/dm_log.cpp", "src/dm_random.cpp", "src/ipc/standard/ipc_cmd_register.cpp", + "src/dm_hash.cpp", ] if (support_jsapi) { sources += [ "src/permission/standard/permission_manager.cpp" ] diff --git a/utils/include/dm_hash.h b/utils/include/dm_hash.h new file mode 100644 index 000000000..738913a9d --- /dev/null +++ b/utils/include/dm_hash.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DM_HASH_H +#define OHOS_DM_HASH_H + +#include +#include "base64.h" +#include "ctr_drbg.h" +#include "entropy.h" +#include "gcm.h" +#include "md.h" +#include "platform.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t GetUdidHash(uint8_t *udid, int32_t udiddataLen, uint8_t outudiddData[32]); +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_DM_HASH_H diff --git a/utils/src/dm_hash.cpp b/utils/src/dm_hash.cpp new file mode 100644 index 000000000..b3687687c --- /dev/null +++ b/utils/src/dm_hash.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dm_log.h" +#include "dm_constants.h" +#include "dm_hash.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t GetUdidHash(uint8_t *udid, int32_t udiddataLen, uint8_t outudiddData[32]) +{ + LOGI("11111111111111111 GetUdidHash"); + if (udid == nullptr || *udid < 0) { + LOGE("udid is nullptr or dataLen: %d", udiddataLen); + return DM_POINT_NULL; + } + mbedtls_md_context_t ctx; + const mbedtls_md_info_t *info; + + mbedtls_md_init(&ctx); + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); + + if (info == nullptr) { + LOGE("info is nullptr"); + return DM_POINT_NULL; + } + + int32_t ret = DM_OK; + do { + ret = mbedtls_md_setup(&ctx, info, 0); + if (ret != 0) { + LOGE("mbedtls_md_setup is fail"); + ret = DM_FAILED; + break; + } + + ret = mbedtls_md_starts(&ctx); + if (ret != 0) { + LOGE("mbedtls_md_starts is fail"); + ret = DM_FAILED; + break; + } + + ret = mbedtls_md_update(&ctx, udid, udiddataLen); + if (ret != 0) { + LOGE("mbedtls_md_update is fail"); + ret = DM_FAILED; + break; + } + + ret = mbedtls_md_finish(&ctx, outudiddData); + if (ret != 0) { + LOGE("mbedtls_md_finish is fail"); + ret = DM_FAILED; + break; + } + ret = DM_OK; + } while (0); + + mbedtls_md_free(&ctx); + return ret; +} + +} // namespace DistributedHardware +} // namespace OHOS -- Gitee From fe8d04fd0ea4187c090e2b0ec15c23271bdd6c1c Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Wed, 2 Mar 2022 15:01:57 +0800 Subject: [PATCH 2/4] 1111 --- BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index 7e5dfc76c..737c05477 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -20,4 +20,4 @@ if (defined(ohos_lite)) { features = [] } } -} +} -- Gitee From 6d5c571c94211a78dac1d322a11c40489adb99e0 Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Wed, 2 Mar 2022 15:03:16 +0800 Subject: [PATCH 3/4] 2112 --- BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index 737c05477..7e5dfc76c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -20,4 +20,4 @@ if (defined(ohos_lite)) { features = [] } } -} +} -- Gitee From ab3d0bfa1e83fab6c0b97a2360a6895a1aa7a59f Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Wed, 2 Mar 2022 15:36:39 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5deviceInfo=E4=B8=AD?= =?UTF-8?q?=E7=9A=84Id=E6=98=AFnetworkId=E8=BF=98=E6=98=AFUDID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../native_cpp/include/dm_device_info.h | 2 +- .../dependency/softbus/softbus_connector.cpp | 17 ++++++++++------- utils/BUILD.gn | 1 - utils/src/dm_hash.cpp | 11 +++++------ 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index bf37c5ab5..662f5d35f 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -49,7 +49,7 @@ typedef struct DmDeviceInfo { char deviceId[DM_MAX_DEVICE_ID_LEN]; char deviceName[DM_MAX_DEVICE_NAME_LEN]; uint16_t deviceTypeId; - std::string networkId; + char networkId[DM_MAX_DEVICE_ID_LEN]; } DmDeviceInfo; typedef struct DmAuthParam { diff --git a/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp b/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp index 579d9f0eb..7356fce11 100644 --- a/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp +++ b/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp @@ -30,7 +30,7 @@ namespace OHOS { namespace DistributedHardware { SoftbusConnector::PulishStatus SoftbusConnector::publishStatus = SoftbusConnector::STATUS_UNKNOWN; -std::map> SoftbusConnector::discoveryDeviceInfoMap_ = {};//比对 +std::map> SoftbusConnector::discoveryDeviceInfoMap_ = {}; std::map> SoftbusConnector::stateCallbackMap_ = {}; std::map> SoftbusConnector::discoveryCallbackMap_ = {}; @@ -412,8 +412,8 @@ int32_t SoftbusConnector::CovertNodeBasicInfoToDmDevice(const NodeBasicInfo &nod } - uint8_t outudiddData[32]; - if(GetUdidHash(mUdid,sizeof(mUdid),outudiddData) != DM_OK) { + uint8_t outUdidData[32]; + if(GetUdidHash(mUdid,sizeof(mUdid),outUdidData) != DM_OK) { LOGE("GetUdidHash is failed"); return DM_FAILED; } @@ -423,13 +423,16 @@ int32_t SoftbusConnector::CovertNodeBasicInfoToDmDevice(const NodeBasicInfo &nod LOGE("copy data failed"); } - if (memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId),outudiddData, - std::min(sizeof(dmDeviceInfo.deviceId), sizeof(outudiddData))) != DM_OK) { + if (memcpy_s(dmDeviceInfo.deviceId, sizeof(dmDeviceInfo.deviceId),outUdidData, + std::min(sizeof(dmDeviceInfo.deviceId), sizeof(outUdidData))) != DM_OK) { LOGE("copy data failed"); } dmDeviceInfo.deviceTypeId = nodeBasicInfo.deviceTypeId; - dmDeviceInfo.networkId = nodeBasicInfo.networkId; + if (memcpy_s(dmDeviceInfo.networkId, sizeof(dmDeviceInfo.networkId),nodeBasicInfo.networkId, + std::min(sizeof(dmDeviceInfo.networkId), sizeof(nodeBasicInfo.networkId))) != DM_OK) { + LOGE("copy data failed"); + } return DM_OK; } @@ -445,7 +448,7 @@ void SoftbusConnector::CovertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, LOGE("copy data failed"); } dmDeviceInfo.deviceTypeId = deviceInfo.devType; - dmDeviceInfo.networkId = {0}; + // dmDeviceInfo.networkId = ""; } void SoftbusConnector::OnPublishSuccess(int32_t publishId) diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 96688fc80..44a6f02e7 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -32,7 +32,6 @@ if (defined(ohos_lite)) { "${common_path}/include/ipc/model", "include/permission/standard", "//third_party/mbedtls/include/mbedtls", - "//base/account/os_account/interfaces/innerkits/osaccount/native/include", ] } diff --git a/utils/src/dm_hash.cpp b/utils/src/dm_hash.cpp index b3687687c..6a26031c9 100644 --- a/utils/src/dm_hash.cpp +++ b/utils/src/dm_hash.cpp @@ -19,11 +19,11 @@ namespace OHOS { namespace DistributedHardware { -int32_t GetUdidHash(uint8_t *udid, int32_t udiddataLen, uint8_t outudiddData[32]) +int32_t GetUdidHash(uint8_t *udid, int32_t udidDataLen, uint8_t outudidData[32]) { - LOGI("11111111111111111 GetUdidHash"); + LOGI("GetUdidHash"); if (udid == nullptr || *udid < 0) { - LOGE("udid is nullptr or dataLen: %d", udiddataLen); + LOGE("udid is nullptr or dataLen: %d", udidDataLen); return DM_POINT_NULL; } mbedtls_md_context_t ctx; @@ -53,14 +53,14 @@ int32_t GetUdidHash(uint8_t *udid, int32_t udiddataLen, uint8_t outudiddData[32] break; } - ret = mbedtls_md_update(&ctx, udid, udiddataLen); + ret = mbedtls_md_update(&ctx, udid, udidDataLen); if (ret != 0) { LOGE("mbedtls_md_update is fail"); ret = DM_FAILED; break; } - ret = mbedtls_md_finish(&ctx, outudiddData); + ret = mbedtls_md_finish(&ctx, outudidData); if (ret != 0) { LOGE("mbedtls_md_finish is fail"); ret = DM_FAILED; @@ -72,6 +72,5 @@ int32_t GetUdidHash(uint8_t *udid, int32_t udiddataLen, uint8_t outudiddData[32] mbedtls_md_free(&ctx); return ret; } - } // namespace DistributedHardware } // namespace OHOS -- Gitee