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 b17b597b186a7fcf4c7105f14860de42683ebb63..662f5d35f4db1a8e67d522190878884612c34275 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 bb96c37a7044c5a55a282bc3077f4f9ed3ee30f6..2679ae001eef0974ae18f54a32a7315447e7f28b 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 4ece01e321d9abed10799d25318b18acd538fada..7356fce118e7ed2865db3bcbe9c0c9624c24669a 100644 --- a/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp +++ b/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp @@ -25,6 +25,7 @@ #include "nlohmann/json.hpp" #include "parameter.h" #include "system_ability_definition.h" +#include "dm_hash.h" namespace OHOS { namespace DistributedHardware { @@ -399,15 +400,39 @@ 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 outUdidData[32]; + if(GetUdidHash(mUdid,sizeof(mUdid),outUdidData) != 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),outUdidData, + std::min(sizeof(dmDeviceInfo.deviceId), sizeof(outUdidData))) != DM_OK) { + LOGE("copy data failed"); + } + dmDeviceInfo.deviceTypeId = nodeBasicInfo.deviceTypeId; + 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; } @@ -423,6 +448,7 @@ void SoftbusConnector::CovertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, LOGE("copy data failed"); } dmDeviceInfo.deviceTypeId = deviceInfo.devType; + // dmDeviceInfo.networkId = ""; } void SoftbusConnector::OnPublishSuccess(int32_t publishId) diff --git a/utils/BUILD.gn b/utils/BUILD.gn index f1c17e3ee2f1143d05d16b027856d0a19f8340e0..44a6f02e7d375843aeba885e1470ddc50ad87ee9 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 0000000000000000000000000000000000000000..738913a9da6de69da63043f2b03961f81f79a120 --- /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 0000000000000000000000000000000000000000..6a26031c978fe3f42dacfc7c9acb594a1186be11 --- /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