From b281880ea13b51ef53ba9a5a1074694d486c2a83 Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Thu, 3 Mar 2022 19:33:34 +0800 Subject: [PATCH 1/3] =?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 Signed-off-by: wangyb0625 --- .../native_cpp/include/dm_device_info.h | 1 + .../kits/js/src/native_devicemanager_js.cpp | 2 + .../dependency/softbus/softbus_connector.cpp | 12 +++ utils/BUILD.gn | 2 + utils/include/dm_hash.h | 32 ++++++++ utils/src/dm_hash.cpp | 76 +++++++++++++++++++ 6 files changed, 125 insertions(+) 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..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,6 +49,7 @@ typedef struct DmDeviceInfo { char deviceId[DM_MAX_DEVICE_ID_LEN]; char deviceName[DM_MAX_DEVICE_NAME_LEN]; uint16_t deviceTypeId; + char networkId[DM_MAX_DEVICE_ID_LEN]; } 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 644386adf..a590b43b8 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 f3278fe8e..230c3d2ff 100644 --- a/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp +++ b/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp @@ -429,6 +429,12 @@ int32_t SoftbusConnector::CovertNodeBasicInfoToDmDevice(const NodeBasicInfo &nod std::min(sizeof(dmDeviceInfo.deviceId), sizeof(nodeBasicInfo.networkId))) != DM_OK) { LOGE("copy data failed"); } + + if (memcpy_s(dmDeviceInfo.networkId, sizeof(dmDeviceInfo.networkId), nodeBasicInfo.networkId, + std::min(sizeof(dmDeviceInfo.networkId), sizeof(nodeBasicInfo.networkId))) != DM_OK) { + LOGE("copy data 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"); @@ -444,6 +450,12 @@ void SoftbusConnector::CovertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, std::min(sizeof(dmDeviceInfo.deviceId), sizeof(deviceInfo.devId))) != DM_OK) { LOGE("copy data failed"); } + + if (memcpy_s(dmDeviceInfo.networkId, sizeof(dmDeviceInfo.networkId), 0, + sizeof(dmDeviceInfo.networkId)) != DM_OK) { + LOGE("copy data failed"); + } + if (memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), deviceInfo.devName, std::min(sizeof(dmDeviceInfo.deviceName), sizeof(deviceInfo.devName))) != DM_OK) { LOGE("copy data failed"); diff --git a/utils/BUILD.gn b/utils/BUILD.gn index f1c17e3ee..44a6f02e7 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -31,6 +31,7 @@ if (defined(ohos_lite)) { "${common_path}/include/ipc", "${common_path}/include/ipc/model", "include/permission/standard", + "//third_party/mbedtls/include/mbedtls", ] } @@ -40,6 +41,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..851e5bfe9 --- /dev/null +++ b/utils/include/dm_hash.h @@ -0,0 +1,32 @@ +/* + * 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 \ No newline at end of file diff --git a/utils/src/dm_hash.cpp b/utils/src/dm_hash.cpp new file mode 100644 index 000000000..488f0b794 --- /dev/null +++ b/utils/src/dm_hash.cpp @@ -0,0 +1,76 @@ +/* + * 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 outudidData[32]) +{ + LOGI("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, outudidData); + 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 \ No newline at end of file -- Gitee From e00a81a3a314528228668c570f63e4f814ca83eb Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Thu, 3 Mar 2022 19:49:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyb0625 --- utils/src/dm_hash.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utils/src/dm_hash.cpp b/utils/src/dm_hash.cpp index 488f0b794..82eee581e 100644 --- a/utils/src/dm_hash.cpp +++ b/utils/src/dm_hash.cpp @@ -31,12 +31,10 @@ int32_t GetUdidHash(uint8_t *udid, int32_t udidDataLen, uint8_t outudidData[32]) mbedtls_md_init(&ctx); info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); - if (info == nullptr) { LOGE("info is nullptr"); - return DM_POINT_NULL; + return DM_POINT_NULL; } - int32_t ret = DM_OK; do { ret = mbedtls_md_setup(&ctx, info, 0); -- Gitee From 5668d391ea3e298c7973e676a879695707192089 Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Fri, 4 Mar 2022 16:52:15 +0800 Subject: [PATCH 3/3] =?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 Signed-off-by: wangyb0625 --- utils/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 44a6f02e7..97f5c361e 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -38,10 +38,10 @@ if (defined(ohos_lite)) { ohos_shared_library("devicemanagerutils") { sources = [ "src/dm_anonymous.cpp", + "src/dm_hash.cpp", "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" ] -- Gitee