From 1bde767090123f569a3af7ae7516adceb5e79d71 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Tue, 28 Jan 2025 08:39:32 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=B8=80=E7=A2=B0=E6=8A=95=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzhihui7 Change-Id: I67878704b36bcd4980e3052e67c29b3f2bace816 --- .../device_manager_ipc_interface_code.h | 6 + common/include/dm_constants.h | 11 +- .../include/ipc/model/ipc_common_param_req.h | 10 + .../include/ipc/model/ipc_gen_serviceid_rsp.h | 43 +++ .../model/ipc_get_serviceinfo_bycaller_rsp.h | 45 +++ .../ipc/model/ipc_get_serviceinfo_byid_rsp.h | 42 +++ .../ipc/model/ipc_register_serviceinfo_req.h | 42 +++ common/include/ipc/standard/ipc_model_codec.h | 4 + common/src/ipc/standard/ipc_model_codec.cpp | 84 ++++++ .../include/deviceprofile_connector.h | 9 + .../src/deviceprofile_connector.cpp | 63 ++++ .../native_cpp/include/device_manager.h | 6 + .../native_cpp/include/device_manager_impl.h | 6 + .../native_cpp/include/dm_device_info.h | 35 +++ .../native_cpp/src/device_manager_impl.cpp | 131 +++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 176 +++++++++++ interfaces/kits/js4.0/src/dm_native_util.cpp | 15 + .../js4.0/src/native_devicemanager_js.cpp | 58 ++++ sa_profile/device_manager.cfg | 1 + .../authentication/auth_message_processor.h | 1 + .../include/authentication/dm_auth_manager.h | 18 +- .../dependency/softbus/softbus_connector.h | 7 + .../dependency/softbus/softbus_session.h | 7 + .../authentication/auth_message_processor.cpp | 13 + .../src/authentication/dm_auth_manager.cpp | 232 ++++++++++++++- .../hichain/hichain_auth_connector.cpp | 6 +- .../dependency/softbus/softbus_connector.cpp | 12 + .../dependency/softbus/softbus_session.cpp | 27 ++ services/service/BUILD.gn | 1 + .../service/include/device_manager_service.h | 15 + .../service/src/device_manager_service.cpp | 204 +++++++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 90 ++++++ test/unittest/BUILD.gn | 1 + test/unittest/UTTest_device_manager_impl.cpp | 104 +++++++ test/unittest/UTTest_device_manager_impl.h | 1 + .../unittest/UTTest_ipc_cmd_parser_client.cpp | 276 ++++++++++++++++++ 36 files changed, 1790 insertions(+), 12 deletions(-) create mode 100644 common/include/ipc/model/ipc_gen_serviceid_rsp.h create mode 100644 common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h create mode 100644 common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h create mode 100644 common/include/ipc/model/ipc_register_serviceinfo_req.h diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index ec42dc7a8..998170ddd 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -108,6 +108,12 @@ enum DMIpcCmdInterfaceCode { SET_REMOTE_DEVICE_NAME_RESULT, SET_LOCAL_DEVICE_NAME, SET_LOCAL_DEVICE_NAME_RESULT, + GEN_SERVICEID, + REG_SERVICE_INFO, + UNREG_SERVICE_INFO, + UPDATE_SERVICE_INFO, + GET_SERVICEINFO_BYID, + GET_SERVICEINFOS_CALLER, // Add ipc msg here IPC_MSG_BUTT }; diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 400647c70..c91b78b8a 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -50,6 +50,7 @@ enum { ERR_DM_BIND_USER_CANCEL_PIN_CODE_DISPLAY = -20056, ERR_DM_SYNC_DELETE_DEVICE_REPEATED = -20058, ERR_DM_VERSION_INCOMPATIBLE = -20059, + ERR_DM_SERVICEINFO_NOTVALID = -20060, ERR_DM_FAILED = 96929744, ERR_DM_NOT_INIT = 96929746, @@ -129,7 +130,8 @@ enum { ERR_DM_HILINKSVC_ICON_URL_EMPTY = 96929828, ERR_DM_HILINKSVC_DISCONNECT = 96929829, ERR_DM_WISE_NEED_LOGIN = 96929830, - ERR_DM_NAME_EMPTY = 96929831 + ERR_DM_NAME_EMPTY = 96929831, + ERR_DM_HICHAIN_PROOFMISMATCH = 96929832 }; constexpr const char* TAG_GROUP_ID = "groupId"; @@ -262,6 +264,13 @@ constexpr const char* PROCESS_NAME = "PROCESS_NAME"; constexpr const char* PARAM_CLOSE_SESSION_DELAY_SECONDS = "DM_CLOSE_SESSION_DELAY_SECONDS"; constexpr const char* DM_AUTHENTICATION_TYPE = "DM_AUTHENTICATION_TYPE"; +constexpr const char* PARAM_KEY_CONN_SESSIONTYPE = "connSessionType"; +constexpr const char* PARAM_KEY_HML_RELEASETIME = "hmlReleaseTime"; +constexpr const char* PARAM_KEY_HML_ENABLE_160M = "hmlEnable160M"; +constexpr const char* PARAM_KEY_HML_ACTIONID = "hmlActionId"; + +const std::string CONN_SESSION_TYPE_HML = "HML"; +const std::string CONN_SESSION_TYPE_BLE = "BLE"; // screen state constexpr int32_t DM_SCREEN_UNKNOWN = -1; constexpr int32_t DM_SCREEN_ON = 0; diff --git a/common/include/ipc/model/ipc_common_param_req.h b/common/include/ipc/model/ipc_common_param_req.h index 688e70670..135a23040 100644 --- a/common/include/ipc/model/ipc_common_param_req.h +++ b/common/include/ipc/model/ipc_common_param_req.h @@ -64,9 +64,19 @@ public: secondParam_ = secondParam; } + int64_t GetInt64Param() const + { + return int64Param_; + } + + void SetInt64Param(int64_t param) + { + int64Param_ = param; + } private: std::string firstParam_; std::string secondParam_; + int64_t int64Param_ = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_gen_serviceid_rsp.h b/common/include/ipc/model/ipc_gen_serviceid_rsp.h new file mode 100644 index 000000000..74840316c --- /dev/null +++ b/common/include/ipc/model/ipc_gen_serviceid_rsp.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 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_IPC_GEN_SERVICEID_RSP_H +#define OHOS_DM_IPC_GEN_SERVICEID_RSP_H + +#include "ipc_rsp.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGenServiceIdRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGenServiceIdRsp); + +public: + int64_t GetServiceId() const + { + return serviceId_; + } + + void SetServiceId(const int64_t &serviceId) + { + serviceId_ = serviceId; + } + +private: + int64_t serviceId_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GEN_SERVICEID_RSP_H diff --git a/common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h b/common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h new file mode 100644 index 000000000..c0090a8a1 --- /dev/null +++ b/common/include/ipc/model/ipc_get_serviceinfo_bycaller_rsp.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2025 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_IPC_GET_SERVICEINFO_BYCALLER_RSP_H +#define OHOS_DM_IPC_GET_SERVICEINFO_BYCALLER_RSP_H + +#include + +#include "dm_device_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetServiceInfoByCallerRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetServiceInfoByCallerRsp); + +public: + const std::vector& GetServiceInfos() const + { + return serviceInfos_; + } + + void SetServiceInfos(const std::vector &infos) + { + serviceInfos_ = infos; + } + +private: + std::vector serviceInfos_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_SERVICEINFO_BYCALLER_RSP_H diff --git a/common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h b/common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h new file mode 100644 index 000000000..cd3cd1396 --- /dev/null +++ b/common/include/ipc/model/ipc_get_serviceinfo_byid_rsp.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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_IPC_GET_SERVICEINFO_BYID_RSP_H +#define OHOS_DM_IPC_GET_SERVICEINFO_BYID_RSP_H + +#include "ipc_req.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetServiceInfoByIdRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetServiceInfoByIdRsp); + +public: + const DMServiceInfo &GetServiceInfo() const + { + return serviceInfo_; + } + + void SetServiceInfo(const DMServiceInfo &info) + { + serviceInfo_ = info; + } +private: + DMServiceInfo serviceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_SERVICEINFO_BYID_RSP_H diff --git a/common/include/ipc/model/ipc_register_serviceinfo_req.h b/common/include/ipc/model/ipc_register_serviceinfo_req.h new file mode 100644 index 000000000..02c68202a --- /dev/null +++ b/common/include/ipc/model/ipc_register_serviceinfo_req.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 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_IPC_REGISTER_SERVICE_INFO_REQ_H +#define OHOS_DM_IPC_REGISTER_SERVICE_INFO_REQ_H + +#include "ipc_req.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcRegServiceInfoReq : public IpcReq { + DECLARE_IPC_MODEL(IpcRegServiceInfoReq); + +public: + const DMServiceInfo &GetServiceInfo() const + { + return serviceInfo_; + } + + void SetServiceInfo(const DMServiceInfo &info) + { + serviceInfo_ = info; + } +private: + DMServiceInfo serviceInfo_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_REGISTER_SERVICE_INFO_REQ_H diff --git a/common/include/ipc/standard/ipc_model_codec.h b/common/include/ipc/standard/ipc_model_codec.h index 76e75969e..e608b4701 100644 --- a/common/include/ipc/standard/ipc_model_codec.h +++ b/common/include/ipc/standard/ipc_model_codec.h @@ -50,6 +50,10 @@ public: static bool EncodeDmDeviceIconInfoFilterOptions(const DmDeviceIconInfoFilterOptions &filterOptions, MessageParcel &parcel); static void DecodeDmDeviceInfo(MessageParcel &parcel, DmDeviceInfo &devInfo); + static bool EncodeServiceInfo(const DMServiceInfo &serviceInfo, MessageParcel &parcel); + static bool EncodeServiceInfos(const std::vector &serviceInfos, MessageParcel &parcel); + static bool DecodeServiceInfo(MessageParcel &parcel, DMServiceInfo &serviceInfo); + static bool DecodeServiceInfos(MessageParcel &parcel, std::vector &serviceInfos); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp index 0deff2a12..db7018754 100644 --- a/common/src/ipc/standard/ipc_model_codec.cpp +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -24,6 +24,15 @@ namespace { const std::string UK_SEPARATOR = "#"; } +#define READ_HELPER_RET(parcel, type, out, failRet) \ + do { \ + bool ret = (parcel).Read##type((out)); \ + if (!ret) { \ + LOGE("read value failed!"); \ + return failRet; \ + } \ + } while (0) + void IpcModelCodec::DecodeDmDeviceBasicInfo(MessageParcel &parcel, DmDeviceBasicInfo &devInfo) { std::string deviceIdStr = parcel.ReadString(); @@ -357,5 +366,80 @@ void IpcModelCodec::DecodeDmDeviceInfo(MessageParcel &parcel, DmDeviceInfo &devI devInfo.authForm = static_cast(parcel.ReadInt32()); devInfo.extraData = parcel.ReadString(); } + +bool IpcModelCodec::EncodeServiceInfo(const DMServiceInfo &serviceInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteInt64(serviceInfo.serviceId)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceType)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceName)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceDisplayName)); + bRet = (bRet && parcel.WriteString(serviceInfo.customData)); + bRet = (bRet && parcel.WriteString(serviceInfo.bundleName)); + bRet = (bRet && parcel.WriteString(serviceInfo.moduleName)); + bRet = (bRet && parcel.WriteString(serviceInfo.abilityName)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.authBoxType)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.authType)); + bRet = (bRet && parcel.WriteInt32(serviceInfo.pinExchangeType)); + bRet = (bRet && parcel.WriteString(serviceInfo.pinCode)); + bRet = (bRet && parcel.WriteString(serviceInfo.description)); + bRet = (bRet && parcel.WriteString(serviceInfo.serviceDiscoveryScope)); + return bRet; +} + +bool IpcModelCodec::EncodeServiceInfos(const std::vector &serviceInfos, MessageParcel &parcel) +{ + uint32_t num = serviceInfos.size(); + if (!parcel.WriteUint32(num)){ + LOGE("[rm003] WriteInt32 num failed"); + return false; + } + bool bRet = true; + for (uint32_t k = 0; k < num; k++) { + const DMServiceInfo& serviceInfo = serviceInfos[k]; + bRet = EncodeServiceInfo(serviceInfo, parcel); + if (!bRet) { + LOGE("[rm003] encode serviceinfos failed"); + break; + } + } + return bRet; +} + +bool IpcModelCodec::DecodeServiceInfo(MessageParcel &parcel, DMServiceInfo &serviceInfo) +{ + READ_HELPER_RET(parcel, Int64, serviceInfo.serviceId, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceType, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceName, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceDisplayName, false); + READ_HELPER_RET(parcel, String, serviceInfo.customData, false); + READ_HELPER_RET(parcel, String, serviceInfo.bundleName, false); + READ_HELPER_RET(parcel, String, serviceInfo.moduleName, false); + READ_HELPER_RET(parcel, String, serviceInfo.abilityName, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.authBoxType, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.authType, false); + READ_HELPER_RET(parcel, Int32, serviceInfo.pinExchangeType, false); + READ_HELPER_RET(parcel, String, serviceInfo.pinCode, false); + READ_HELPER_RET(parcel, String, serviceInfo.description, false); + READ_HELPER_RET(parcel, String, serviceInfo.serviceDiscoveryScope, false); + return true; +} + +bool IpcModelCodec::DecodeServiceInfos(MessageParcel &parcel, std::vector &serviceInfos) +{ + uint32_t num = 0; + READ_HELPER_RET(parcel, Uint32, num, false); + bool bRet = true; + for (uint32_t k = 0; k < num; k++) { + DMServiceInfo serviceInfo; + bRet = DecodeServiceInfo(parcel, serviceInfo); + if (!bRet) { + LOGE("[rm003] DecodeServiceInfo failed"); + break; + } + serviceInfos.emplace_back(serviceInfo); + } + return bRet; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index d0bb054ce..8c087802b 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -21,6 +21,8 @@ #include "dm_device_info.h" #include "dm_single_instance.h" #include "i_dp_inited_callback.h" +#include "service_info_profile.h" +#include "service_info_unique_key.h" #include "trusted_device_info.h" constexpr uint32_t ALLOW_AUTH_ONCE = 1; @@ -167,6 +169,13 @@ public: int32_t SubscribeDeviceProfileInited(sptr dpInitedCallback); int32_t UnSubscribeDeviceProfileInited(); int32_t PutAllTrustedDevices(const std::vector &deviceInfos); + int32_t PutServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile); + int32_t DeleteServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoUniqueKey &key); + int32_t UpdateServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile); + int32_t GetServiceInfoProfileByUniqueKey(const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile); + int32_t GetServiceInfoProfileListByTokenId(const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &serviceInfoProfiles); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 6d57f99bc..4292ba751 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1631,6 +1631,69 @@ int32_t DeviceProfileConnector::PutAllTrustedDevices( return DM_OK; } +int32_t DeviceProfileConnector::PutServiceInfoProfile( + const DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile) +{ + LOGI("[rm003] DeviceProfileConnector::PutServiceInfoProfile"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::DeleteServiceInfoProfile(const DistributedDeviceProfile::ServiceInfoUniqueKey &key) +{ + LOGI("[rm003] DeviceProfileConnector::DeleteServiceInfoProfile"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().DeleteServiceInfoProfile(key); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::UpdateServiceInfoProfile( + const DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile) +{ + LOGI("[rm003] DeviceProfileConnector::UpdateServiceInfoProfile"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().UpdateServiceInfoProfile(serviceInfoProfile); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileByUniqueKey( + const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + DistributedDeviceProfile::ServiceInfoProfile &serviceInfoProfile) +{ + LOGI("[rm003] DeviceProfileConnector::GetServiceInfoProfileByUniqueKey"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileByUniqueKey( + key, serviceInfoProfile); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t DeviceProfileConnector::GetServiceInfoProfileListByTokenId( + const DistributedDeviceProfile::ServiceInfoUniqueKey &key, + std::vector &serviceInfoProfiles) +{ + LOGI("[rm003] DeviceProfileConnector::GetServiceInfoProfileListByTokenId"); + int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId( + key, serviceInfoProfiles); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 1723de345..f92921bb9 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -653,6 +653,12 @@ public: const std::vector &deviceProfileInfoList) = 0; virtual int32_t GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, std::string &displayName) = 0; + virtual int32_t GenerateServiceId(int64_t &serviceId) = 0; + virtual int32_t RegisterServiceInfo(const DMServiceInfo &info) = 0; + virtual int32_t UnRegisterServiceInfo(int64_t serviceId) = 0; + virtual int32_t UpdateServiceInfo(const DMServiceInfo &info) = 0; + virtual int32_t GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) = 0; + virtual int32_t GetCallerServiceInfos(std::vector &serviceInfos) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index ee2c5ca87..bedd10178 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -420,6 +420,12 @@ public: const std::vector &deviceProfileInfoList) override; virtual int32_t GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, std::string &displayName) override; + virtual int32_t GenerateServiceId(int64_t &serviceId) override; + virtual int32_t RegisterServiceInfo(const DMServiceInfo &info) override; + virtual int32_t UnRegisterServiceInfo(int64_t serviceId) override; + virtual int32_t UpdateServiceInfo(const DMServiceInfo &info) override; + virtual int32_t GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) override; + virtual int32_t GetCallerServiceInfos(std::vector &serviceInfos) override; private: DeviceManagerImpl() = default; 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 00d087fcb..595cb885d 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -413,6 +413,41 @@ typedef struct DmNotifyKey { notifyUserId == other.notifyUserId && udid < other.udid); } } DmNotifyKey; + +typedef enum : int32_t { + DMServiceInfoAuthType_TrustOneTime = 0, + DMServiceInfoAuthType_TrustAlways = 6, + DMServiceInfoAuthType_Cancel = 1 +} DMServiceInfoAuthType; + +typedef enum : int32_t { + DMServiceInfoPinExchangeType_PinBox = 1, + DMServiceInfoPinExchangeType_UltraSound = 2, + DMServiceInfoPinExchangeType_FromDp = 3 +} DMServiceInfoExchangeType; + +typedef enum : int32_t { + DMServiceInfoAuthBoxType_3State = 1, + DMServiceInfoAuthBoxType_SkipConfirm = 2, + DMServiceInfoAuthBoxType_2In1 = 3 +} DMServiceInfoAuthBoxType; + +typedef struct DMServiceInfo { + int64_t serviceId = 0; + std::string serviceType; + std::string serviceName; + std::string serviceDisplayName; + std::string customData; + std::string bundleName; + std::string moduleName; + std::string abilityName; + int32_t authBoxType = 0; + int32_t authType = 0; + int32_t pinExchangeType = 0; + std::string pinCode; + std::string description; + std::string serviceDiscoveryScope; +} DMServiceInfo; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index fffca73c3..3f59cfdc5 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -36,6 +36,7 @@ #include "ipc_destroy_pin_holder_req.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_gen_serviceid_rsp.h" #include "ipc_get_anony_local_udid_rsp.h" #include "ipc_get_device_icon_info_req.h" #include "ipc_get_device_info_rsp.h" @@ -48,6 +49,8 @@ #include "ipc_get_local_device_info_rsp.h" #include "ipc_get_local_display_device_name_req.h" #include "ipc_get_local_display_device_name_rsp.h" +#include "ipc_get_serviceinfo_bycaller_rsp.h" +#include "ipc_get_serviceinfo_byid_rsp.h" #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" #include "ipc_import_auth_code_req.h" @@ -55,6 +58,7 @@ #include "ipc_permission_req.h" #include "ipc_publish_req.h" #include "ipc_put_device_profile_info_list_req.h" +#include "ipc_register_serviceinfo_req.h" #include "ipc_req.h" #include "ipc_rsp.h" #include "ipc_set_credential_req.h" @@ -2693,5 +2697,132 @@ int32_t DeviceManagerImpl::GetLocalDisplayDeviceName(const std::string &pkgName, LOGI("Completed"); return DM_OK; } + +int32_t DeviceManagerImpl::GenerateServiceId(int64_t &serviceId) +{ + LOGI("Start"); + LOGI("[rm003] GenerateServiceId"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + int32_t ret = ipcClientProxy_->SendRequest(GEN_SERVICEID, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + serviceId = rsp->GetServiceId(); + LOGI("Completed, serviceId %{public}" PRId64, serviceId); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterServiceInfo(const DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] RegisterServiceInfo"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceInfo(info); + int32_t ret = ipcClientProxy_->SendRequest(REG_SERVICE_INFO, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterServiceInfo(int64_t serviceId) +{ + LOGI("Start"); + LOGI("[rm003] UnRegisterServiceInfo"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetInt64Param(serviceId); + int32_t ret = ipcClientProxy_->SendRequest(UNREG_SERVICE_INFO, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::UpdateServiceInfo(const DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] UpdateServiceInfo"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetServiceInfo(info); + int32_t ret = ipcClientProxy_->SendRequest(UPDATE_SERVICE_INFO, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) +{ + LOGI("Start"); + LOGI("[rm003] GetServiceInfoByServiceId, %{public}" PRId64, serviceId); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetInt64Param(serviceId); + int32_t ret = ipcClientProxy_->SendRequest(GET_SERVICEINFO_BYID, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + info = rsp->GetServiceInfo(); + LOGI("Completed"); + return DM_OK; +} + +int32_t DeviceManagerImpl::GetCallerServiceInfos(std::vector &serviceInfos) +{ + LOGI("Start"); + LOGI("[rm003] GetCallerServiceInfos"); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + int32_t ret = ipcClientProxy_->SendRequest(GET_SERVICEINFOS_CALLER, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Failed with ret %{public}d", ret); + return ret; + } + serviceInfos = rsp->GetServiceInfos(); + LOGI("Completed, serviceInfos size %{public}zu", serviceInfos.size()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index c4744e673..dc2c1c426 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -32,6 +32,7 @@ #include "ipc_def.h" #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_gen_serviceid_rsp.h" #include "ipc_get_anony_local_udid_rsp.h" #include "ipc_get_device_icon_info_req.h" #include "ipc_get_device_info_rsp.h" @@ -44,6 +45,8 @@ #include "ipc_get_local_device_info_rsp.h" #include "ipc_get_local_display_device_name_req.h" #include "ipc_get_local_display_device_name_rsp.h" +#include "ipc_get_serviceinfo_byid_rsp.h" +#include "ipc_get_serviceinfo_bycaller_rsp.h" #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" #include "ipc_import_auth_code_req.h" @@ -51,6 +54,7 @@ #include "ipc_notify_event_req.h" #include "ipc_put_device_profile_info_list_req.h" #include "ipc_register_listener_req.h" +#include "ipc_register_serviceinfo_req.h" #include "ipc_req.h" #include "ipc_rsp.h" #include "ipc_set_credential_req.h" @@ -2174,5 +2178,177 @@ ON_IPC_READ_RESPONSE(GET_LOCAL_DISPLAY_DEVICE_NAME, MessageParcel &reply, std::s pRsp->SetDisplayName(reply.ReadString()); return DM_OK; } + +ON_IPC_SET_REQUEST(GEN_SERVICEID, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(GEN_SERVICEID"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GEN_SERVICEID, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(GEN_SERVICEID"); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + if (pRsp == nullptr) { + LOGE("pRsp is null"); + return ERR_DM_FAILED; + } + pRsp->SetErrCode(reply.ReadInt32()); + if (pRsp->GetErrCode() == DM_OK) { + pRsp->SetServiceId(reply.ReadInt64()); + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(REG_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + LOGI("[rm003] ON_IPC_SET_REQUEST(REG_SERVICE_INFO"); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + const DMServiceInfo& info = pReq->GetServiceInfo(); + if (!IpcModelCodec::EncodeServiceInfo(info, data)) { + LOGE("EncodeServiceInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(REG_SERVICE_INFO"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREG_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(UNREG_SERVICE_INFO"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetInt64Param(); + if (!data.WriteInt64(serviceId)) { + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + const DMServiceInfo& info = pReq->GetServiceInfo(); + if (!IpcModelCodec::EncodeServiceInfo(info, data)) { + LOGE("EncodeServiceInfo failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + int64_t serviceId = pReq->GetInt64Param(); + if (!data.WriteInt64(serviceId)) { + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + if (pBaseRsp->GetErrCode() == DM_OK) { + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + DMServiceInfo info; + bool ret = IpcModelCodec::DecodeServiceInfo(reply, info); + if (!ret) { + LOGE("[rm003] DecodeServiceInfo failed"); + pBaseRsp->SetErrCode(ERR_DM_IPC_READ_FAILED); + } + pRsp->SetServiceInfo(info); + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(GET_SERVICEINFOS_CALLER, std::shared_ptr pBaseReq, MessageParcel &data) +{ + LOGI("[rm003] ON_IPC_SET_REQUEST(GET_SERVICEINFOS_CALLER"); + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return ERR_DM_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_SERVICEINFOS_CALLER, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + LOGI("[rm003] ON_IPC_READ_RESPONSE(GET_SERVICEINFOS_CALLER"); + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + if (pBaseRsp->GetErrCode() == DM_OK) { + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + std::vector infos; + bool ret = IpcModelCodec::DecodeServiceInfos(reply, infos); + if (!ret) { + LOGE("[rm003] DecodeServiceInfo failed"); + pBaseRsp->SetErrCode(ERR_DM_IPC_READ_FAILED); + } + pRsp->SetServiceInfos(infos); + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js4.0/src/dm_native_util.cpp b/interfaces/kits/js4.0/src/dm_native_util.cpp index 8b08c668a..788ff0ced 100644 --- a/interfaces/kits/js4.0/src/dm_native_util.cpp +++ b/interfaces/kits/js4.0/src/dm_native_util.cpp @@ -42,6 +42,7 @@ const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_ONE = -1; const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_TWO = -2; const int32_t DM_NAPI_DESCRIPTION_BUF_LENGTH = 16384; const int32_t DM_NAPI_BUF_LENGTH = 256; +const int32_t DM_NAPI_CONN_SESSIONTYPE_LENGTH = 16; const int32_t MAX_OBJECT_LEN = 4096; void JsObjectToString(const napi_env &env, const napi_value &object, const std::string &fieldStr, @@ -350,6 +351,15 @@ void JsToBindParam(const napi_env &env, const napi_value &object, std::string &b int32_t bindLevel = 0; JsObjectToInt(env, object, "bindLevel", bindLevel); + char connSessionType[DM_NAPI_CONN_SESSIONTYPE_LENGTH] = ""; + JsObjectToString(env, object, "connSessionType", connSessionType, sizeof(connSessionType)); + int32_t hmlReleaseTime = 0; + JsObjectToInt(env, object, "hmlReleaseTime", hmlReleaseTime); + int32_t hmlActionId = 0; + JsObjectToInt(env, object, "hmlActionId", hmlActionId); + bool hmlEnable160M = false; + JsObjectToBool(env, object, "hmlEnable160M", hmlEnable160M); + nlohmann::json jsonObj; jsonObj[AUTH_TYPE] = bindType; jsonObj[APP_OPERATION] = std::string(appOperation); @@ -365,6 +375,11 @@ void JsToBindParam(const napi_env &env, const napi_value &object, std::string &b jsonObj[PARAM_KEY_WIFI_IP] = std::string(wifiIP); jsonObj[PARAM_KEY_WIFI_PORT] = wifiPort; jsonObj[BIND_LEVEL] = bindLevel; + + jsonObj[PARAM_KEY_CONN_SESSIONTYPE] = std::string(connSessionType); + jsonObj[PARAM_KEY_HML_RELEASETIME] = hmlReleaseTime; + jsonObj[PARAM_KEY_HML_ENABLE_160M] = hmlEnable160M; + jsonObj[PARAM_KEY_HML_ACTIONID] = hmlActionId; bindParam = SafetyDump(jsonObj); } diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index 5ac09825f..f48aabd63 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp @@ -1658,6 +1658,59 @@ napi_value DeviceManagerNapi::UnPublishDeviceDiscoverySync(napi_env env, napi_ca return result; } +bool xProcessRegisterServiceInfo(std::string bindParam) +{ + nlohmann::json bindParamObj = nlohmann::json::parse(bindParam, nullptr, false); + if (bindParamObj.is_discarded()) { + return false; + } + if (IsInt32(bindParamObj, AUTH_TYPE)) { + int32_t op = bindParamObj[AUTH_TYPE].get(); + if (op == 11) { + LOGI("[temp003] xProcessRegisterServiceInfo"); + std::vector infos; + DeviceManager::GetInstance().GetCallerServiceInfos(infos); + for (const auto &item : infos) { + DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); + } + infos.clear(); + + std::string pincode; + if (IsString(bindParamObj, PARAM_KEY_PIN_CODE)) { + pincode = bindParamObj[PARAM_KEY_PIN_CODE].get(); + LOGI("[temp003] pincode = %{public}s", pincode.c_str()); + } + if (pincode.empty()) { + pincode = "100000"; + LOGI("[temp003] set pincode to 100000"); + } + DMServiceInfo serviceInfo; + serviceInfo.serviceType = "profile.serviceType"; + serviceInfo.serviceName = "profile.serviceName"; + serviceInfo.serviceDisplayName = "profile.serviceDisplayName"; + serviceInfo.customData = "profile.customData"; + serviceInfo.bundleName = "profile.bundleName"; + serviceInfo.moduleName = "profile.moduleName"; + serviceInfo.abilityName = "profile.abilityName"; + serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; + serviceInfo.authType = DMServiceInfoAuthType_TrustOneTime; + serviceInfo.pinExchangeType = DMServiceInfoPinExchangeType_FromDp; + serviceInfo.pinCode = pincode; + serviceInfo.description = "profile.description"; + serviceInfo.serviceDiscoveryScope = "profile.serviceDiscoveryScope"; + int32_t err = DeviceManager::GetInstance().GenerateServiceId(serviceInfo.serviceId); + if (err == DM_OK) { + DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo); + } else { + LOGI("[temp003] DeviceManager::GenerateServiceId failed"); + } + LOGI("[temp003] DeviceManager::GetInstance().RegisterServiceInfo complete"); + return true; + } + } + return false; +} + void DeviceManagerNapi::BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, const std::string &deviceId, napi_env env, napi_value &object) { @@ -1666,6 +1719,11 @@ void DeviceManagerNapi::BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, bool isMetaType = false; JsToBindParam(env, object, bindParam, authAsyncCallbackInfo_.authType, isMetaType); + bool processRet = xProcessRegisterServiceInfo(bindParam); + if (processRet) { + return; + } + LOGI("[rm003] BindDevOrTarget %{public}s", bindParam.c_str()); if (isMetaType) { std::shared_ptr bindTargetCallback = nullptr; { diff --git a/sa_profile/device_manager.cfg b/sa_profile/device_manager.cfg index 4a495502f..de736fd0a 100644 --- a/sa_profile/device_manager.cfg +++ b/sa_profile/device_manager.cfg @@ -30,6 +30,7 @@ "ohos.permission.ACCESS_BLUETOOTH", "ohos.permission.MANAGE_BLUETOOTH", "ohos.permission.MANAGE_SETTINGS", + "ohos.permission.MANAGE_SOFTBUS_NETWORK", "ohos.permission.START_SYSTEM_DIALOG", "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS", "ohos.permission.GET_BUNDLE_RESOURCES", diff --git a/services/implementation/include/authentication/auth_message_processor.h b/services/implementation/include/authentication/auth_message_processor.h index d21279a34..b97818a49 100644 --- a/services/implementation/include/authentication/auth_message_processor.h +++ b/services/implementation/include/authentication/auth_message_processor.h @@ -43,6 +43,7 @@ constexpr const char* TAG_INDEX = "INDEX"; constexpr const char* TAG_SLICE_NUM = "SLICE"; constexpr const char* TAG_IS_AUTH_CODE_READY = "IS_AUTH_CODE_READY"; constexpr const char* TAG_IS_SHOW_DIALOG = "IS_SHOW_DIALOG"; +constexpr const char* TAG_PINCODE_SETTED = "TAG_PINCODE_SETTED"; constexpr const char* TAG_TOKEN = "TOKEN"; constexpr const char* TAG_CRYPTO_NAME = "CRYPTONAME"; constexpr const char* TAG_CRYPTO_VERSION = "CRYPTOVERSION"; diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 760000cb4..406e9142b 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -33,6 +33,7 @@ #include "dm_timer.h" #include "hichain_auth_connector.h" #include "hichain_connector.h" +#include "service_info_unique_key.h" #include "softbus_connector.h" #include "softbus_session.h" @@ -134,6 +135,11 @@ typedef struct DmAuthRequestContext { std::string addr; std::string hostPkgLabel; int32_t closeSessionDelaySeconds = 0; + int32_t presetPinCode = 0; + std::string connSessionType; + int32_t hmlReleaseTime = 0; + bool hmlEnable160M = false; + int32_t hmlActionId = 0; } DmAuthRequestContext; typedef struct DmAuthResponseContext { @@ -191,6 +197,7 @@ typedef struct DmAuthResponseContext { bool isFinish = false; std::string edition; int32_t localBindLevel; + bool isPincodeSetted = false; } DmAuthResponseContext; class AuthMessageProcessor; @@ -403,7 +410,7 @@ public: * @tc.desc: Show AuthInfo Dialog of the DeviceManager Authenticate Manager * @tc.type: FUNC */ - void ShowAuthInfoDialog(); + void ShowAuthInfoDialog(bool authDeviceError = false); /** * @tc.name: DmAuthManager::ShowStartAuthDialog @@ -472,8 +479,14 @@ public: void HandleSessionHeartbeat(std::string name); int32_t RegisterAuthenticationType(int32_t authenticationType); + static bool IsPinCodeValid(std::string strpin); + static bool IsPinCodeValid(int32_t numpin); private: + bool IsHmlSessionType(); + bool CanUsePinFromDp(); + void InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); + bool IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceInfoProfile &profile); int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra); int32_t CheckAuthParamVaildExtra(const std::string &extra); @@ -552,6 +565,7 @@ private: int32_t GetBindLevel(int32_t bindLevel); std::string GetBundleName(nlohmann::json &jsonObject); int32_t GetBinderInfo(); + int64_t GetBinderTokenId(); void SetProcessInfo(); int32_t GetCloseSessionDelaySeconds(std::string &delaySecondsStr); void ConverToFinish(); @@ -597,6 +611,8 @@ private: bool isNeedProcCachedSrcReqMsg_ = false; std::string srcReqMsg_ = ""; int32_t authenticationType_ = USER_OPERATION_TYPE_ALLOW_AUTH; + DistributedDeviceProfile::ServiceInfoProfile serviceInfoProfile_; + bool pincodeDialogEverShown_ = false; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/softbus/softbus_connector.h b/services/implementation/include/dependency/softbus/softbus_connector.h index 2536eeacc..30d44b6b1 100644 --- a/services/implementation/include/dependency/softbus/softbus_connector.h +++ b/services/implementation/include/dependency/softbus/softbus_connector.h @@ -77,6 +77,13 @@ public: * @tc.type: FUNC */ static void JoinLnn(const std::string &deviceId); + + /** + * @tc.name: SoftbusConnector::JoinLnnByHml + * @tc.desc: join lnn by session id + * @tc.type: FUNC + */ + static void JoinLnnByHml(const int32_t sessionId); public: SoftbusConnector(); ~SoftbusConnector(); diff --git a/services/implementation/include/dependency/softbus/softbus_session.h b/services/implementation/include/dependency/softbus/softbus_session.h index 99387bb76..2e7be8f57 100644 --- a/services/implementation/include/dependency/softbus/softbus_session.h +++ b/services/implementation/include/dependency/softbus/softbus_session.h @@ -60,6 +60,13 @@ public: */ int32_t OpenAuthSession(const std::string &deviceId); + /** + * @tc.name: SoftbusSession::OpenAuthSessionWithPara + * @tc.desc: Open HML AuthSession of the Softbus Session + * @tc.type: FUNC + */ + int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool enable160m); + /** * @tc.name: SoftbusSession::CloseAuthSession * @tc.desc: Close AuthSession of the Softbus Session diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index b67246a0b..76175a452 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -71,6 +71,13 @@ void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj) } else { jsonObj[TAG_IS_SHOW_DIALOG] = true; } + if (DmAuthManager::IsPinCodeValid(authRequestContext_->presetPinCode)) { + LOGI("[rm003-nopincode] TAG_PINCODE_SETTED"); + jsonObj[TAG_PINCODE_SETTED] = true; + } else { + LOGI("[rm003-nopincode] TAG_PINCODE_SETTED not set"); + jsonObj[TAG_PINCODE_SETTED] = false; + } jsonObj[TAG_APP_OPERATION] = authRequestContext_->appOperation; jsonObj[TAG_CUSTOM_DESCRIPTION] = authRequestContext_->customDesc; jsonObj[TAG_APP_NAME] = authRequestContext_->appName; @@ -457,6 +464,12 @@ int32_t AuthMessageProcessor::ParseAuthRequestMessage(nlohmann::json &json) } else { authResponseContext_->isShowDialog = true; } + if (IsBool(json, TAG_PINCODE_SETTED)) { + authResponseContext_->isPincodeSetted = json[TAG_PINCODE_SETTED].get(); + } else { + authResponseContext_->isPincodeSetted = false; + } + LOGI("[rm003-nopincode] isPincodeSetted %{public}d", authResponseContext_->isPincodeSetted); if (IsInt32(json, TAG_BIND_TYPE_SIZE)) { int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].get(); if (bindTypeSize > MAX_BINDTYPE_SIZE) { diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 1d284dab3..1dc09a323 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -63,6 +63,7 @@ const int32_t CLONE_ADD_TIMEOUT = 10; const int32_t CLONE_WAIT_NEGOTIATE_TIMEOUT = 10; const int32_t CLONE_WAIT_REQUEST_TIMEOUT = 10; const int32_t CLONE_SESSION_HEARTBEAT_TIMEOUT = 20; +const int32_t HML_SESSION_TIMEOUT = 10; const int32_t CANCEL_PIN_CODE_DISPLAY = 1; const int32_t DEVICE_ID_HALF = 2; const int32_t MAX_AUTH_TIMES = 3; @@ -127,6 +128,12 @@ DmAuthManager::~DmAuthManager() LOGI("DmAuthManager destructor"); } +bool DmAuthManager::IsHmlSessionType() +{ + CHECK_NULL_RETURN(authRequestContext_, false); + return authRequestContext_->connSessionType == CONN_SESSION_TYPE_HML; +} + int32_t DmAuthManager::CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { @@ -179,6 +186,25 @@ int32_t DmAuthManager::CheckAuthParamVaild(const std::string &pkgName, int32_t a int32_t DmAuthManager::CheckAuthParamVaildExtra(const std::string &extra) { nlohmann::json jsonObject = nlohmann::json::parse(extra, nullptr, false); + if (jsonObject.is_discarded()) { + return DM_OK; + } + std::string connSessionType; + if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { + connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); + } + if (connSessionType == CONN_SESSION_TYPE_HML) { + if (!IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { + LOGE("[rm003] CONN_SESSION_TYPE_HML, input actionId is !Int32"); + return ERR_DM_INPUT_PARA_INVALID; + } + int32_t actionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); + if (actionId <= 0) { + LOGE("[rm003] CONN_SESSION_TYPE_HML, input actionId is <=0"); + return ERR_DM_INPUT_PARA_INVALID; + } + } + if (jsonObject.is_discarded() || jsonObject.find(TAG_BIND_LEVEL) == jsonObject.end() || !IsInt32(jsonObject, TAG_BIND_LEVEL)) { return DM_OK; @@ -281,6 +307,39 @@ void DmAuthManager::ParseJsonObject(nlohmann::json jsonObject) } else { authRequestContext_->peerBundleName = authRequestContext_->hostPkgName; } + if (IsString(jsonObject, PARAM_KEY_PIN_CODE)) { + std::string strpin = jsonObject[PARAM_KEY_PIN_CODE].get(); + if (DmAuthManager::IsPinCodeValid(strpin)) { + authRequestContext_->presetPinCode = std::stoi(strpin); + LOGI("[rm003-nopincode] input pincode %{public}s", strpin.c_str()); + } + } + if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { + authRequestContext_->connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); + LOGI("[rm003-nopincode] input connSessionType %{public}s", authRequestContext_->connSessionType.c_str()); + } + if (IsInt32(jsonObject, PARAM_KEY_HML_RELEASETIME)) { + authRequestContext_->hmlReleaseTime = jsonObject[PARAM_KEY_HML_RELEASETIME].get(); + LOGI("[rm003-nopincode] input hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); + } + if (IsBool(jsonObject, PARAM_KEY_HML_ENABLE_160M)) { + authRequestContext_->hmlEnable160M = jsonObject[PARAM_KEY_HML_ENABLE_160M].get(); + LOGI("[rm003-nopincode] input hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); + } + if (IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { + authRequestContext_->hmlActionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); + if (authRequestContext_->hmlActionId <= 0) { + authRequestContext_->hmlActionId = 0; + } + LOGI("[rm003-nopincode] input hmlActionId %{public}d", authRequestContext_->hmlActionId); + } + if (IsHmlSessionType()) { + if (authRequestContext_->hmlReleaseTime > 0) { + authRequestContext_->closeSessionDelaySeconds = authRequestContext_->hmlReleaseTime; + } else { + authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; + } + } } authRequestContext_->bundleName = GetBundleName(jsonObject); } @@ -800,7 +859,18 @@ void DmAuthManager::HandleAuthenticateTimeout(std::string name) int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId) { - int32_t sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSession(deviceId); + int32_t sessionId = 0; + if (IsHmlSessionType()) { + CHECK_NULL_RETURN(authRequestContext_, ERR_DM_FAILED); + LOGI("[rm003] EstablishAuthChannel, try OpenAuthSessionWithPara"); + LOGI("[rm003] hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); + LOGI("[rm003] hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); + LOGI("[rm003] hmlActionId %{public}d", authRequestContext_->hmlActionId); + sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSessionWithPara(deviceId, + authRequestContext_->hmlActionId, authRequestContext_->hmlEnable160M); + } else { + sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSession(deviceId); + } struct RadarInfo info = { .funcName = "EstablishAuthChannel", .stageRes = (sessionId > 0) ? @@ -1091,7 +1161,11 @@ bool DmAuthManager::IsAuthFinish() if ((authResponseContext_->isIdenticalAccount && !authResponseContext_->authed) || (authResponseContext_->authed && !authResponseContext_->isOnline)) { - softbusConnector_->JoinLnn(authRequestContext_->addr); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authRequestContext_->addr); + } authRequestContext_->reason = DM_OK; authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authResponseContext_->reply = DM_OK; @@ -1140,7 +1214,10 @@ int32_t DmAuthManager::ConfirmProcessExt(const int32_t &action) authMessageProcessor_->SetResponseContext(authResponseContext_); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH && authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_CONFIRM) { - if (!authResponseContext_->isShowDialog) { + if (CanUsePinFromDp() && authResponseContext_->isPincodeSetted) { + authResponseContext_->code = std::stoi(serviceInfoProfile_.GetPinCode()); + LOGI("[rm003-nopincode] import from dp, pin %{public}d", authResponseContext_->code); + } else if (!authResponseContext_->isShowDialog) { GetAuthCode(authResponseContext_->hostPkgName, authResponseContext_->code); } else { authResponseContext_->code = GeneratePincode(); @@ -1376,6 +1453,7 @@ void DmAuthManager::AuthenticateFinish() srcReqMsg_ = ""; isNeedProcCachedSrcReqMsg_ = false; } + pincodeDialogEverShown_ = false; authType_ = AUTH_TYPE_UNKNOW; std::lock_guard autoLock(g_authFinishLock); if (authResponseContext_ == nullptr || authUiStateMgr_ == nullptr) { @@ -1440,6 +1518,63 @@ int32_t DmAuthManager::GeneratePincode() return GenRandInt(MIN_PIN_CODE, MAX_PIN_CODE); } +bool DmAuthManager::IsPinCodeValid(std::string strpin) +{ + if (strpin.empty()) { + return false; + } + int pinnum = std::stoi(strpin); + return IsPinCodeValid(pinnum); +} + +bool DmAuthManager::IsPinCodeValid(int32_t numpin) +{ + if (numpin < MIN_PIN_CODE || numpin > MAX_PIN_CODE) { + return false; + } + return true; +} + +bool DmAuthManager::CanUsePinFromDp() +{ + return (serviceInfoProfile_.GetPinExchangeType() == DMServiceInfoPinExchangeType_FromDp && + DmAuthManager::IsPinCodeValid(serviceInfoProfile_.GetPinCode())); +} + +void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + key.SetDeviceId(std::string(localDeviceId)); + int64_t tokenId = GetBinderTokenId(); + key.SetTokenId(std::to_string(tokenId)); + int32_t userId = MultipleUserConnector::GetFirstForegroundUserId(); + key.SetUserId(userId); + LOGI("[rm003] localDeviceId %{public}s, userId %{public}d, tokenId %{public}" PRId64, localDeviceId, userId, tokenId); +} + +bool DmAuthManager::IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceInfoProfile &profile) +{ + if (profile.GetAuthType() != DMServiceInfoAuthType_TrustOneTime && + profile.GetAuthType() != DMServiceInfoAuthType_TrustAlways && + profile.GetAuthType() != DMServiceInfoAuthType_Cancel) { + LOGE("[rm003] AuthType not valid, %{public}d", profile.GetAuthType()); + return false; + } + if (profile.GetAuthBoxType() != DMServiceInfoAuthBoxType_3State && + profile.GetAuthBoxType() != DMServiceInfoAuthBoxType_SkipConfirm) { + LOGE("[rm003] AuthBoxType not valid, %{public}d", profile.GetAuthBoxType()); + return false; + } + if (profile.GetPinExchangeType() != DMServiceInfoPinExchangeType_PinBox && + profile.GetPinExchangeType() != DMServiceInfoPinExchangeType_FromDp && + profile.GetPinExchangeType() != DMServiceInfoPinExchangeType_UltraSound) { + LOGE("[rm003] PinExchangeType not valid, %{public}d", profile.GetPinExchangeType()); + return false; + } + return true; +} + std::string DmAuthManager::GenerateGroupName() { if (authResponseContext_ == nullptr) { @@ -1519,6 +1654,11 @@ void DmAuthManager::ShowConfigDialog() LOGE("failed to ShowConfigDialog because authResponseContext_ is nullptr"); return; } + if (serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { + LOGI("[rm003-nopincode] no need confirm dialog"); + StartAuthProcess(serviceInfoProfile_.GetAuthType()); + return; + } if (!authResponseContext_->isShowDialog) { LOGI("start auth process"); StartAuthProcess(authenticationType_); @@ -1551,13 +1691,20 @@ void DmAuthManager::ShowConfigDialog() LOGI("ShowConfigDialog end"); } -void DmAuthManager::ShowAuthInfoDialog() +void DmAuthManager::ShowAuthInfoDialog(bool authDeviceError) { if (authResponseContext_ == nullptr) { LOGE("failed to ShowAuthInfoDialog because authResponseContext_ is nullptr"); return; } LOGI("DmAuthManager::ShowAuthInfoDialog start"); + if (!authDeviceError && CanUsePinFromDp() && authResponseContext_->isPincodeSetted) { + LOGI("[rm003-nopincode] pin import from dp, not show dialog"); + return; + } + if (pincodeDialogEverShown_) { + return; + } if (!authResponseContext_->isShowDialog) { LOGI("not show dialog."); return; @@ -1572,6 +1719,7 @@ void DmAuthManager::ShowAuthInfoDialog() nlohmann::json jsonObj; jsonObj[PIN_CODE_KEY] = authResponseContext_->code; std::string authParam = SafetyDump(jsonObj); + pincodeDialogEverShown_ = true; DmDialogManager::GetInstance().ShowPinDialog(std::to_string(authResponseContext_->code)); } @@ -1606,7 +1754,14 @@ void DmAuthManager::ShowStartAuthDialog() return; } LOGI("DmAuthManager::ShowStartAuthDialog start"); - DmDialogManager::GetInstance().ShowInputDialog(authResponseContext_->targetDeviceName); + LOGI("[rm003] presetPinCode %{public}d", authRequestContext_->presetPinCode); + if (authRequestContext_->presetPinCode > 0) { + LOGI("[rm003] already has pin code"); + ProcessPincode(authRequestContext_->presetPinCode); + } else { + pincodeDialogEverShown_ = true; + DmDialogManager::GetInstance().ShowInputDialog(authResponseContext_->targetDeviceName); + } } int32_t DmAuthManager::ProcessPincode(int32_t pinCode) @@ -2024,7 +2179,11 @@ void DmAuthManager::RequestCredentialDone() if (timer_ != nullptr) { timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); } - softbusConnector_->JoinLnn(authRequestContext_->addr); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authRequestContext_->addr); + } authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authResponseContext_->reply = DM_OK; @@ -2118,7 +2277,11 @@ void DmAuthManager::SrcAuthDeviceFinish() return; } if (!authResponseContext_->isOnline && authResponseContext_->haveCredential) { - softbusConnector_->JoinLnn(authRequestContext_->addr); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authRequestContext_->addr); + } timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); ConverToFinish(); return; @@ -2201,8 +2364,13 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) LOGI("AuthDeviceError start."); isAuthDevice_ = false; if (authRequestState_ == nullptr || authResponseState_ != nullptr) { - authTimes_++; LOGI("AuthDeviceError sink authTimes %{public}d.", authTimes_); + if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && !pincodeDialogEverShown_ && CanUsePinFromDp()) { + LOGI("[rm003-nopincode] AuthDeviceError, ShowAuthInfoDialog"); + ShowAuthInfoDialog(true); + return; + } + authTimes_++; if (authTimes_ >= MAX_AUTH_TIMES) { authResponseContext_->isFinish = false; authResponseContext_->reply = ERR_DM_AUTH_CODE_INCORRECT; @@ -2241,7 +2409,13 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) DmAuthManager::HandleAuthenticateTimeout(name); }); } - authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); + authRequestContext_->presetPinCode = 0; + if (!pincodeDialogEverShown_ && errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { + LOGI("[rm003] auth error, ShowStartAuthDialog"); + ShowStartAuthDialog(); + } else { + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); + } } } } @@ -2353,6 +2527,20 @@ void DmAuthManager::ProcRespNegotiateExt(const int32_t &sessionId) } std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_NEGOTIATE); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); + + DistributedDeviceProfile::ServiceInfoUniqueKey key; + InitServiceInfoUniqueKey(key); + std::vector profiles; + int32_t result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); + if (result == DM_OK && profiles.size() > 0) { + if (IsServiceInfoProfileValid(profiles[0])) { + serviceInfoProfile_ = profiles[0]; + LOGI("[rm003] authBoxType %{public}d", serviceInfoProfile_.GetAuthBoxType()); + LOGI("[rm003] authType %{public}d", serviceInfoProfile_.GetAuthType()); + LOGI("[rm003] pinExchangeType %{public}d", serviceInfoProfile_.GetPinExchangeType()); + LOGI("[rm003] pinCode %{public}s", serviceInfoProfile_.GetPinCode().c_str()); + } + } } void DmAuthManager::ProcRespNegotiate(const int32_t &sessionId) @@ -2787,6 +2975,32 @@ int32_t DmAuthManager::GetBinderInfo() return ret; } +int64_t DmAuthManager::GetBinderTokenId() +{ + LOGI("[rm003-nopincode] GetBinderTokenId bundleName is %{public}s", authResponseContext_->bundleName.c_str()); + CHECK_NULL_RETURN(authResponseContext_, 0); + if (authResponseContext_->bundleName.empty()) { + LOGI("[rm003-nopincode] bundleName is empty"); + return authResponseContext_->remoteTokenId; + + } + int32_t localUserId = MultipleUserConnector::GetFirstForegroundUserId(); + std::string localAccountId = MultipleUserConnector::GetOhosAccountIdByUserId(localUserId); + int32_t ret = AppManager::GetInstance(). + GetNativeTokenIdByName(authResponseContext_->bundleName, authResponseContext_->tokenId); + if (ret == DM_OK) { + LOGI("[rm003-nopincode] bundleName is sa"); + return authResponseContext_->tokenId; + } + ret = AppManager::GetInstance().GetHapTokenIdByName(localUserId, + authResponseContext_->peerBundleName, 0, authResponseContext_->tokenId); + if (ret != DM_OK) { + LOGI("[rm003-nopincode] get tokenId by bundleName failed"); + return authResponseContext_->remoteTokenId; + } + return authResponseContext_->tokenId; +} + void DmAuthManager::SetProcessInfo() { CHECK_NULL_VOID(authResponseContext_); diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index a723bb322..eeb4da1d9 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -145,7 +145,11 @@ void HiChainAuthConnector::onError(int64_t requestId, int operationCode, int err LOGE("HiChainAuthConnector::onError dmDeviceAuthCallback_ is nullptr."); return; } - dmDeviceAuthCallback_->AuthDeviceError(requestId, ERR_DM_FAILED); + int32_t dmErrorCode = ERR_DM_FAILED; + if (errorCode == PROOF_MISMATCH) { + dmErrorCode = ERR_DM_HICHAIN_PROOFMISMATCH; + } + dmDeviceAuthCallback_->AuthDeviceError(requestId, dmErrorCode); } void HiChainAuthConnector::onSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index cf4eeb5d3..a65aab3bc 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -96,6 +96,18 @@ void SoftbusConnector::JoinLnn(const std::string &deviceId) return; } +void SoftbusConnector::JoinLnnByHml(const int32_t sessionId) +{ + LOGI("start, JoinLnnByHml sessionId: %{public}d.", sessionId); + ConnectionAddr addrInfo; + addrInfo.type = CONNECTION_ADDR_SESSION; + addrInfo.info.session.sessionId = sessionId; + int32_t ret = ::JoinLNN(DM_PKG_NAME, &addrInfo, OnSoftbusJoinLNNResult); + if (ret != DM_OK) { + LOGE("[SOFTBUS]JoinLNN failed, ret: %{public}d.", ret); + } +} + int32_t SoftbusConnector::GetUdidByNetworkId(const char *networkId, std::string &udid) { LOGI("start, networkId: %{public}s.", GetAnonyString(std::string(networkId)).c_str()); diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index 9e579ba80..4c17ce3ae 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -22,6 +22,10 @@ #include "dm_log.h" #include "nlohmann/json.hpp" #include "softbus_connector.h" +#include "softbus_error_code.h" +#ifndef DEVICE_MANAGER_COMMON_FLAG +#include "session_ex.h" +#endif namespace OHOS { namespace DistributedHardware { @@ -110,6 +114,29 @@ int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId) return sessionId; } +int32_t SoftbusSession::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool enable160m) +{ +#ifdef DEVICE_MANAGER_COMMON_FLAG + LOGE("[SOFTBUS] OpenAuthSessionWithPara no implement"); + return SOFTBUS_NOT_IMPLEMENT; +#else + DmTraceStart(std::string(DM_HITRACE_AUTH_TO_OPPEN_SESSION)); + LinkPara para; + para.type = PARA_ACTION; + para.action.actionId = actionId; + para.enable160M = enable160m; + para.accountInfo = false; + int32_t sessionId = ::OpenAuthSessionWithPara(DM_SESSION_NAME, ¶); + if (sessionId < 0) { + LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId); + return sessionId; + } + DmTraceEnd(); + LOGI("OpenAuthSessionWithPara success. sessionId: %{public}d.", sessionId); + return sessionId; +#endif +} + int32_t SoftbusSession::CloseAuthSession(int32_t sessionId) { LOGI("CloseAuthSession."); diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 212fd0968..0913a70c1 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -144,6 +144,7 @@ if (defined(ohos_lite)) { "${devicemanager_path}/commondependency/include", "${devicemanager_path}/radar/include", "${innerkits_path}/native_cpp/include", + "${servicesimpl_path}/include/dependency/deviceprofile", "${softbuscache_parh}/include", "${utils_path}/include/appInfo/standard", "${utils_path}/include/crypto", diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 85b19822d..50a9dcc6c 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -44,6 +44,7 @@ #include "dm_publish_common_event.h" #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI #endif +#include "service_info_profile.h" namespace OHOS { namespace DistributedHardware { @@ -184,6 +185,20 @@ public: int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map &unbindParam); + bool IsServiceInfoValid(const DMServiceInfo &serviceInfo); + int64_t GenerateSerivceId(); + bool InitServiceInfoProfile(const DMServiceInfo &serviceInfo, + DistributedDeviceProfile::ServiceInfoProfile &profile); + void InitServiceInfo(const DistributedDeviceProfile::ServiceInfoProfile &profile, DMServiceInfo &serviceInfo); + void InitServiceInfos(const std::vector &profiles, + std::vector &serviceInfos); + bool InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); + int32_t RegisterServiceInfo(DMServiceInfo &serviceInfo); + int32_t UnRegisterServiceInfo(int64_t serviceId); + int32_t UpdateServiceInfo(DMServiceInfo &serviceInfo); + int32_t GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo); + int32_t GetCallerServiceInfos(std::vector &serviceInfos); + int32_t DpAclAdd(const std::string &udid); int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 2917002c2..00a636260 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -20,11 +20,13 @@ #include "app_manager.h" #include "nlohmann/json.hpp" +#include "deviceprofile_connector.h" #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_crypto.h" #include "dm_hidumper.h" #include "dm_log.h" +#include "dm_random.h" #include "dm_softbus_cache.h" #include "dm_radar_helper.h" #include "parameter.h" @@ -35,10 +37,12 @@ #include "device_name_manager.h" #include "dm_comm_tool.h" #include "dm_transport_msg.h" +#include "ipc_skeleton.h" #include "iservice_registry.h" #include "kv_adapter_manager.h" #include "multiple_user_connector.h" #include "relationship_sync_mgr.h" +#include "openssl/sha.h" #if defined(SUPPORT_POWER_MANAGER) #include "power_mgr_client.h" #endif // SUPPORT_POWER_MANAGER @@ -1444,6 +1448,206 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee return dmServiceImplExtResident_->UnbindTargetExt(pkgName, targetId, unbindParam); } +bool DeviceManagerService::IsServiceInfoValid(const DMServiceInfo &serviceInfo) +{ + //todo + return true; +} + +int64_t DeviceManagerService::GenerateSerivceId() +{ + DmDeviceInfo devInfo; + int32_t ret = GetLocalDeviceInfo(devInfo); + if (ret != DM_OK) { + LOGE("[rm003] GetLocalDeviceInfo failed"); + return 0; + } + std::string udid = devInfo.deviceId; + if (udid.empty()) { + LOGE("[rm003] udid empty"); + return 0; + } + int64_t tokenId = IPCSkeleton::GetCallingTokenID(); + const uint32_t dataLength = sizeof(int64_t) + sizeof(int64_t) + udid.length(); + std::unique_ptr data = std::make_unique(dataLength); + int64_t randomSeed = GenRandLongLong(0, std::numeric_limits::max()); + unsigned char* dataPtr = data.get(); + *((int64_t*)dataPtr) = randomSeed; + dataPtr += sizeof(int64_t); + *((int64_t*)dataPtr) = tokenId; + dataPtr += sizeof(int64_t); + if (memcpy_s(dataPtr, udid.length(), udid.c_str(), udid.length()) != EOK) { + LOGE("[rm003] memcpy udid failed"); + return 0; + } + + unsigned char out[SHA256_DIGEST_LENGTH] = {0}; + SHA256_CTX ctx; + SHA256_Init(&ctx); + SHA256_Update(&ctx, data.get(), dataLength); + SHA256_Final(out, &ctx); + + int64_t serviceId = 0; + if (memcpy_s(&serviceId, sizeof(int64_t), out, sizeof(int64_t)) != EOK) { + serviceId = 0; + LOGE("[rm003] memcpy serviceId failed"); + } + serviceId = std::abs(serviceId); + LOGI("[rm003] GenerateSeivceId %{public}" PRId64, serviceId); + return serviceId; +} + +bool DeviceManagerService::InitServiceInfoProfile(const DMServiceInfo &serviceInfo, + DistributedDeviceProfile::ServiceInfoProfile &profile) +{ + DmDeviceInfo devInfo; + int32_t ret = GetLocalDeviceInfo(devInfo); + if (ret != DM_OK) { + LOGE("[rm003] GetLocalDeviceInfo failed"); + return false; + } + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + profile.SetDeviceId(std::string(localDeviceId)); + profile.SetNetworkId(devInfo.networkId); + profile.SetUserId(MultipleUserConnector::GetFirstForegroundUserId()); + profile.SetTokenId(std::to_string(IPCSkeleton::GetCallingTokenID())); + profile.SetServiceId(serviceInfo.serviceId); + profile.SetServiceType(serviceInfo.serviceType); + profile.SetServiceName(serviceInfo.serviceName); + profile.SetServiceDisplayName(serviceInfo.serviceDisplayName); + profile.SetCustomData(serviceInfo.customData); + profile.SetCustomDataLen(serviceInfo.customData.size()); + profile.SetBundleName(serviceInfo.bundleName); + profile.SetModuleName(serviceInfo.moduleName); + profile.SetAbilityName(serviceInfo.abilityName); + profile.SetAuthBoxType(serviceInfo.authBoxType); + profile.SetAuthType(serviceInfo.authType); + profile.SetPinExchangeType(serviceInfo.pinExchangeType); + profile.SetPinCode(serviceInfo.pinCode); + profile.SetDescription(serviceInfo.description); + profile.SetServiceDicoveryScope(serviceInfo.serviceDiscoveryScope); + return true; +} + +void DeviceManagerService::InitServiceInfo(const DistributedDeviceProfile::ServiceInfoProfile &profile, + DMServiceInfo &serviceInfo) +{ + serviceInfo.serviceId = profile.GetServiceId(); + serviceInfo.serviceType = profile.GetServiceType(); + serviceInfo.serviceName = profile.GetServiceName(); + serviceInfo.serviceDisplayName = profile.GetServiceDisplayName(); + serviceInfo.customData = profile.GetCustomData(); + serviceInfo.bundleName = profile.GetBundleName(); + serviceInfo.moduleName = profile.GetModuleName(); + serviceInfo.abilityName = profile.GetAbilityName(); + serviceInfo.authBoxType = profile.GetAuthBoxType(); + serviceInfo.authType = profile.GetAuthType(); + serviceInfo.pinExchangeType = profile.GetPinExchangeType(); + serviceInfo.pinCode = profile.GetPinCode(); + serviceInfo.description = profile.GetDescription(); + serviceInfo.serviceDiscoveryScope = profile.GetServiceDicoveryScope(); +} + +void DeviceManagerService::InitServiceInfos(const std::vector &profiles, + std::vector &serviceInfos) +{ + for (const auto &profileItem : profiles) { + DMServiceInfo infoItem; + InitServiceInfo(profileItem, infoItem); + serviceInfos.emplace_back(infoItem); + } +} + +bool DeviceManagerService::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key) +{ + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + key.SetDeviceId(std::string(localDeviceId)); + key.SetUserId(MultipleUserConnector::GetFirstForegroundUserId()); + key.SetTokenId(std::to_string(IPCSkeleton::GetCallingTokenID())); + return true; +} + +int32_t DeviceManagerService::RegisterServiceInfo(DMServiceInfo &serviceInfo) +{ + //todo permission verifier + if (!IsServiceInfoValid(serviceInfo)) { + LOGE("[rm003] IsServiceInfoValid failed"); + return ERR_DM_SERVICEINFO_NOTVALID; + } + LOGI("[rm003] DeviceManagerService::RegisterServiceInfo"); + DistributedDeviceProfile::ServiceInfoProfile profile; + bool success = InitServiceInfoProfile(serviceInfo, profile); + if (!success) { + LOGE("[rm003] InitServiceInfoProfile failed"); + return ERR_DM_FAILED; + } + int32_t result = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(profile); + return result; +} + +int32_t DeviceManagerService::UnRegisterServiceInfo(int64_t serviceId) +{ + LOGI("[rm003] DeviceManagerService::UnRegisterServiceInfo"); + DistributedDeviceProfile::ServiceInfoUniqueKey key; + bool result = InitServiceInfoUniqueKey(key); + if (!result) { + LOGE("[rm003] InitServiceInfoUniqueKey failed"); + return ERR_DM_FAILED; + } + key.SetServiceId(serviceId); + return DeviceProfileConnector::GetInstance().DeleteServiceInfoProfile(key); +} + +int32_t DeviceManagerService::UpdateServiceInfo(DMServiceInfo &serviceInfo) +{ + LOGI("[rm003] DeviceManagerService::UpdateServiceInfo"); + DistributedDeviceProfile::ServiceInfoProfile profile; + bool success = InitServiceInfoProfile(serviceInfo, profile); + if (!success) { + LOGE("[rm003] InitServiceInfoProfile failed"); + return ERR_DM_FAILED; + } + int32_t result = DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(profile); + return result; +} + +int32_t DeviceManagerService::GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo) +{ + LOGI("[rm003] DeviceManagerService::GetServiceInfoById, %{public}" PRId64, serviceId); + DistributedDeviceProfile::ServiceInfoUniqueKey key; + bool result = InitServiceInfoUniqueKey(key); + if (!result) { + LOGE("[rm003] InitServiceInfoUniqueKey failed"); + return ERR_DM_FAILED; + } + key.SetServiceId(serviceId); + DistributedDeviceProfile::ServiceInfoProfile profile; + result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey(key, profile); + if (result == DM_OK) { + InitServiceInfo(profile, serviceInfo); + } + return result; +} + +int32_t DeviceManagerService::GetCallerServiceInfos(std::vector &serviceInfos) +{ + LOGI("[rm003] DeviceManagerService::GetCallerServiceInfos"); + DistributedDeviceProfile::ServiceInfoUniqueKey key; + bool result = InitServiceInfoUniqueKey(key); + if (!result) { + LOGE("[rm003] InitServiceInfoUniqueKey failed"); + return ERR_DM_FAILED; + } + std::vector profiles; + result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); + if (result == DM_OK) { + InitServiceInfos(profiles, serviceInfos); + } + return result; +} + int32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName) { if (!PermissionManager::GetInstance().CheckPermission()) { diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index c804c05d2..c4014508d 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -1824,5 +1824,95 @@ ON_IPC_CMD(GET_LOCAL_DISPLAY_DEVICE_NAME, MessageParcel &data, MessageParcel &re } return DM_OK; } + +ON_IPC_CMD(GEN_SERVICEID, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(GEN_SERVICEID"); + int64_t serviceId = DeviceManagerService::GetInstance().GenerateSerivceId(); + int32_t result = DM_OK; + if (serviceId == 0) { + result = ERR_DM_FAILED; + } + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!reply.WriteInt64(serviceId)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(REG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(REG_SERVICE_INFO"); + DMServiceInfo serviceInfo; + IpcModelCodec::DecodeServiceInfo(data, serviceInfo); + int32_t result = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(UNREG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(UNREG_SERVICE_INFO"); + int64_t serviceId = data.ReadInt64(); + int32_t result = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceId); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(UPDATE_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(UPDATE_SERVICE_INFO"); + DMServiceInfo serviceInfo; + IpcModelCodec::DecodeServiceInfo(data, serviceInfo); + int32_t result = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(GET_SERVICEINFO_BYID, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(GET_SERVICEINFO_BYID"); + int64_t serviceId = data.ReadInt64(); + DMServiceInfo serviceInfo; + int32_t result = DeviceManagerService::GetInstance().GetServiceInfoById(serviceId, serviceInfo); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (result == DM_OK && !IpcModelCodec::EncodeServiceInfo(serviceInfo, reply)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_CMD(GET_SERVICEINFOS_CALLER, MessageParcel &data, MessageParcel &reply) +{ + LOGI("[rm003] ON_IPC_CMD(GET_SERVICEINFOS_CALLER"); + std::vector serviceInfos; + int32_t result = DeviceManagerService::GetInstance().GetCallerServiceInfos(serviceInfos); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (result == DM_OK && !IpcModelCodec::EncodeServiceInfos(serviceInfos, reply)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index c3d6c9758..2f50033dd 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -146,6 +146,7 @@ ohos_unittest("UTTest_ipc_cmd_parser_client") { include_dirs = [ "${devicemanager_path}/commondependency/include" ] sources = [ + "${common_path}/src/ipc/standard/ipc_model_codec.cpp", "${innerkits_path}/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp", "UTTest_ipc_cmd_parser_client.cpp", ] diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index 3ccd06d71..91e106511 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -80,6 +80,22 @@ void DeviceManagerImplTest::TearDownTestCase() deviceManagerNotifyMock_ = nullptr; } +void DeviceManagerImplTest::InitServiceInfo(DMServiceInfo &serviceInfo) +{ + serviceInfo.serviceType = "profile.serviceType"; + serviceInfo.serviceName = "profile.serviceName"; + serviceInfo.serviceDisplayName = "profile.serviceDisplayName"; + serviceInfo.customData = "profile.customData"; + serviceInfo.bundleName = "profile.bundleName"; + serviceInfo.moduleName = "profile.moduleName"; + serviceInfo.abilityName = "profile.abilityName"; + serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; + serviceInfo.authType = DMServiceInfoAuthType_TrustOneTime; + serviceInfo.pinExchangeType = DMServiceInfoPinExchangeType_FromDp; + serviceInfo.pinCode = "123456"; + serviceInfo.description = "profile.description"; + serviceInfo.serviceDiscoveryScope = "profile.serviceDiscoveryScope"; +} namespace { /** * @tc.name: InitDeviceManager_001 @@ -2554,6 +2570,94 @@ HWTEST_F(DeviceManagerImplTest, GetDeviceSecurityLevel_001, testing::ext::TestSi ret = DeviceManager::GetInstance().GetDeviceSecurityLevel(pkgName, netWorkId, securityLevel); ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } + +HWTEST_F(DeviceManagerImplTest, REG_SERVICEINFO_101, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.test"; + std::shared_ptr callback = std::make_shared(); + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); + ASSERT_EQ(ret, DM_OK); + + DMServiceInfo serviceInfoOld; + InitServiceInfo(serviceInfoOld); + + int64_t serviceId = 0; + ret = DeviceManager::GetInstance().GenerateServiceId(serviceId); + ASSERT_EQ(ret, DM_OK); + DMServiceInfo serviceInfo; + InitServiceInfo(serviceInfo); + serviceInfo.serviceId = serviceId; + ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo); + ASSERT_EQ(ret, DM_OK); + DMServiceInfo retInfo; + LOGI("[rm003] serviceId %{public}" PRId64, serviceId); + ret = DeviceManager::GetInstance().GetServiceInfoByServiceId(serviceId, retInfo); + ASSERT_EQ(ret, DM_OK); + ASSERT_TRUE(serviceId == retInfo.serviceId); + ASSERT_TRUE(serviceInfoOld.serviceType == retInfo.serviceType); + ASSERT_TRUE(serviceInfoOld.abilityName == retInfo.abilityName); + ASSERT_TRUE(serviceInfoOld.serviceDiscoveryScope == retInfo.serviceDiscoveryScope); + + retInfo.abilityName = "kkk"; + ret = DeviceManager::GetInstance().UpdateServiceInfo(retInfo); + ASSERT_EQ(ret, DM_OK); + + ret = DeviceManager::GetInstance().GetServiceInfoByServiceId(serviceId, retInfo); + ASSERT_EQ(ret, DM_OK); + ASSERT_TRUE(retInfo.abilityName == "kkk"); + + std::vector infos; + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + ASSERT_EQ(ret, DM_OK); + for (const auto &item : infos) { + DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); + } + infos.clear(); + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + ASSERT_NE(ret, DM_OK); + ASSERT_EQ(infos.size(), 0); + DeviceManager::GetInstance().UnInitDeviceManager(packName); +} + +HWTEST_F(DeviceManagerImplTest, REG_SERVICEINFO_102, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.test"; + std::shared_ptr callback = std::make_shared(); + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); + ASSERT_EQ(ret, DM_OK); + + std::vector infos; + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + for (const auto &item : infos) { + DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); + } + infos.clear(); + + DMServiceInfo serviceInfo1; + InitServiceInfo(serviceInfo1); + ret = DeviceManager::GetInstance().GenerateServiceId(serviceInfo1.serviceId); + ASSERT_EQ(ret, DM_OK); + ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo1); + ASSERT_EQ(ret, DM_OK); + + DMServiceInfo serviceInfo2; + InitServiceInfo(serviceInfo2); + ret = DeviceManager::GetInstance().GenerateServiceId(serviceInfo2.serviceId); + ASSERT_EQ(ret, DM_OK); + ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo2); + ASSERT_EQ(ret, DM_OK); + + ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); + ASSERT_EQ(ret, DM_OK); + ASSERT_TRUE(infos.size() >= 2); + for (const auto &item : infos) { + LOGI("[rm003] UnRegisterServiceInfo, serviceId %{public}" PRId64, item.serviceId); + DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); + } + infos.clear(); + DeviceManager::GetInstance().UnInitDeviceManager(packName); +} + } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_impl.h b/test/unittest/UTTest_device_manager_impl.h index 92f1c661f..82271328e 100644 --- a/test/unittest/UTTest_device_manager_impl.h +++ b/test/unittest/UTTest_device_manager_impl.h @@ -39,6 +39,7 @@ public: static inline std::shared_ptr deviceManagerNotifyMock_ = std::make_shared(); + void InitServiceInfo(DMServiceInfo &serviceInfo); }; class DeviceDiscoveryCallbackTest : public DiscoveryCallback { diff --git a/test/unittest/UTTest_ipc_cmd_parser_client.cpp b/test/unittest/UTTest_ipc_cmd_parser_client.cpp index 75d7cc0bc..6b52ab63a 100644 --- a/test/unittest/UTTest_ipc_cmd_parser_client.cpp +++ b/test/unittest/UTTest_ipc_cmd_parser_client.cpp @@ -26,16 +26,21 @@ #include "ipc_create_pin_holder_req.h" #include "ipc_register_listener_req.h" #include "ipc_generate_encrypted_uuid_req.h" +#include "ipc_gen_serviceid_rsp.h" #include "ipc_get_device_screen_status_req.h" #include "ipc_get_device_screen_status_rsp.h" #include "ipc_get_info_by_network_req.h" #include "ipc_get_info_by_network_rsp.h" +#include "ipc_get_serviceinfo_byid_rsp.h" +#include "ipc_get_serviceinfo_bycaller_rsp.h" #include "ipc_get_trustdevice_req.h" +#include "ipc_model_codec.h" #include "ipc_publish_req.h" #include "ipc_unpublish_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_unbind_device_req.h" #include "ipc_authenticate_device_req.h" +#include "ipc_register_serviceinfo_req.h" #include "ipc_set_credential_req.h" #include "ipc_set_credential_rsp.h" #include "ipc_notify_event_req.h" @@ -46,6 +51,8 @@ #include "dm_constants.h" #include "nlohmann/json.hpp" +#define NUM_200 200 + namespace OHOS { namespace DistributedHardware { void IpcCmdParserClientTest::SetUp() @@ -328,6 +335,165 @@ HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_034, testing::ext::TestSize.Le ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); } +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_080, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GEN_SERVICEID; + ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_081, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = REG_SERVICE_INFO; + ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_082, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = UNREG_SERVICE_INFO; + ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_083, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = UPDATE_SERVICE_INFO; + ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_084, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFO_BYID; + ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_085, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFOS_CALLER; + ASSERT_EQ(TestReadResponseRspNull(cmdCode), ERR_DM_FAILED); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_086, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GEN_SERVICEID; + MessageParcel reply; + std::shared_ptr rsp = std::make_shared(); + int32_t retCode = 0; + reply.WriteInt32(retCode); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + ReadResponseFunc ptr = GetResponseFunc(cmdCode); + if (ptr) { + ret = ptr(reply, rsp); + } + ASSERT_EQ(ret, DM_OK); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_087, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = REG_SERVICE_INFO; + ASSERT_EQ(TestReadResponseRspNotNull(cmdCode), DM_OK); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_088, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = UNREG_SERVICE_INFO; + ASSERT_EQ(TestReadResponseRspNotNull(cmdCode), DM_OK); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_089, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = UPDATE_SERVICE_INFO; + ASSERT_EQ(TestReadResponseRspNotNull(cmdCode), DM_OK); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_090, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFO_BYID; + MessageParcel reply; + std::shared_ptr rsp = std::make_shared(); + int32_t retCode = 0; + reply.WriteInt32(retCode); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + ReadResponseFunc ptr = GetResponseFunc(cmdCode); + if (ptr) { + ret = ptr(reply, rsp); + } + ASSERT_EQ(ret, DM_OK); + ASSERT_EQ(rsp->GetErrCode(), ERR_DM_IPC_READ_FAILED); + + MessageParcel reply2; + reply2.WriteInt32(DM_OK); + DMServiceInfo info; + info.abilityName = "debug"; + ASSERT_TRUE(IpcModelCodec::EncodeServiceInfo(info, reply2)); + if (ptr) { + ret = ptr(reply2, rsp); + } + ASSERT_EQ(ret, DM_OK); + ASSERT_EQ(rsp->GetErrCode(), DM_OK); + ASSERT_TRUE(rsp->GetServiceInfo().abilityName == "debug"); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_091, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFOS_CALLER; + MessageParcel reply; + std::shared_ptr rsp = std::make_shared(); + int32_t retCode = 0; + reply.WriteInt32(retCode); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + ReadResponseFunc ptr = GetResponseFunc(cmdCode); + if (ptr) { + ret = ptr(reply, rsp); + } + ASSERT_EQ(ret, DM_OK); + ASSERT_EQ(rsp->GetErrCode(), ERR_DM_IPC_READ_FAILED); + + MessageParcel reply2; + reply2.WriteInt32(DM_OK); + std::vector infos; + DMServiceInfo info1; + info1.abilityName = "debug0"; + DMServiceInfo info2; + info2.abilityName = "debug1"; + infos.emplace_back(info1); + infos.emplace_back(info2); + ASSERT_TRUE(IpcModelCodec::EncodeServiceInfos(infos, reply2)); + if (ptr) { + ret = ptr(reply2, rsp); + } + ASSERT_EQ(ret, DM_OK); + ASSERT_EQ(rsp->GetErrCode(), DM_OK); + std::vector infos2 = rsp->GetServiceInfos(); + ASSERT_EQ(infos2.size(), infos.size()); + ASSERT_TRUE(infos2[0].abilityName == "debug0"); + ASSERT_TRUE(infos2[1].abilityName == "debug1"); +} + +HWTEST_F(IpcCmdParserClientTest, ReadResponseFunc_092, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFOS_CALLER; + ReadResponseFunc ptr = GetResponseFunc(cmdCode); + MessageParcel reply; + std::shared_ptr rsp = std::make_shared(); + reply.WriteInt32(DM_OK); + std::vector infos; + for (int k = 0; k < NUM_200; k++) { + DMServiceInfo info; + info.abilityName = std::string("debug") + std::to_string(k); + infos.emplace_back(info); + } + ASSERT_TRUE(IpcModelCodec::EncodeServiceInfos(infos, reply)); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + if (ptr) { + ret = ptr(reply, rsp); + } + ASSERT_EQ(ret, DM_OK); + ASSERT_EQ(rsp->GetErrCode(), DM_OK); + std::vector infos2 = rsp->GetServiceInfos(); + ASSERT_EQ(infos2.size(), infos.size()); + ASSERT_TRUE(infos2[0].abilityName == "debug0"); + ASSERT_TRUE(infos2[1].abilityName == "debug1"); +} + HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_002, testing::ext::TestSize.Level0) { int32_t cmdCode = GET_NETWORKTYPE_BY_NETWORK; @@ -570,6 +736,96 @@ HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_015, testing::ext::TestSize.L ASSERT_EQ(DM_OK, ret); } +HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_030, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GEN_SERVICEID; + MessageParcel data; + std::shared_ptr req = std::make_shared(); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + SetIpcRequestFunc ptr = GetIpcRequestFunc(cmdCode); + if (ptr) { + ret = ptr(req, data); + } + ASSERT_EQ(DM_OK, ret); +} + +HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_031, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = REG_SERVICE_INFO; + DMServiceInfo serviceInfo; + MessageParcel data; + std::shared_ptr req = std::make_shared(); + req->SetServiceInfo(serviceInfo); + + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + SetIpcRequestFunc ptr = GetIpcRequestFunc(cmdCode); + if (ptr) { + ret = ptr(req, data); + } + ASSERT_EQ(DM_OK, ret); +} + +HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_032, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = UNREG_SERVICE_INFO; + DMServiceInfo serviceInfo; + MessageParcel data; + std::shared_ptr req = std::make_shared(); + req->SetInt64Param(100); + + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + SetIpcRequestFunc ptr = GetIpcRequestFunc(cmdCode); + if (ptr) { + ret = ptr(req, data); + } + ASSERT_EQ(DM_OK, ret); +} + +HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_033, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = UPDATE_SERVICE_INFO; + DMServiceInfo serviceInfo; + MessageParcel data; + std::shared_ptr req = std::make_shared(); + req->SetServiceInfo(serviceInfo); + + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + SetIpcRequestFunc ptr = GetIpcRequestFunc(cmdCode); + if (ptr) { + ret = ptr(req, data); + } + ASSERT_EQ(DM_OK, ret); +} + +HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_034, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFO_BYID; + DMServiceInfo serviceInfo; + MessageParcel data; + std::shared_ptr req = std::make_shared(); + req->SetInt64Param(100); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + SetIpcRequestFunc ptr = GetIpcRequestFunc(cmdCode); + if (ptr) { + ret = ptr(req, data); + } + ASSERT_EQ(DM_OK, ret); +} + +HWTEST_F(IpcCmdParserClientTest, SetIpcRequestFunc_035, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = GET_SERVICEINFOS_CALLER; + DMServiceInfo serviceInfo; + MessageParcel data; + std::shared_ptr req = std::make_shared(); + int ret = ERR_DM_UNSUPPORTED_IPC_COMMAND; + SetIpcRequestFunc ptr = GetIpcRequestFunc(cmdCode); + if (ptr) { + ret = ptr(req, data); + } + ASSERT_EQ(DM_OK, ret); +} + HWTEST_F(IpcCmdParserClientTest, OnIpcCmdFunc_001, testing::ext::TestSize.Level0) { int32_t cmdCode = SERVER_DEVICE_DISCOVERY; @@ -1158,6 +1414,16 @@ HWTEST_F(IpcCmdParserClientTest, TEST_IPC_REQUEST_NULL_002, testing::ext::TestSi EXPECT_EQ(TestIpcRequestNull(SYNC_CALLBACK), ERR_DM_FAILED); } +HWTEST_F(IpcCmdParserClientTest, TEST_IPC_REQUEST_NULL_003, testing::ext::TestSize.Level2) +{ + EXPECT_EQ(TestIpcRequestNull(GEN_SERVICEID), ERR_DM_FAILED); + EXPECT_EQ(TestIpcRequestNull(REG_SERVICE_INFO), ERR_DM_FAILED); + EXPECT_EQ(TestIpcRequestNull(UNREG_SERVICE_INFO), ERR_DM_FAILED); + EXPECT_EQ(TestIpcRequestNull(UPDATE_SERVICE_INFO), ERR_DM_FAILED); + EXPECT_EQ(TestIpcRequestNull(GET_SERVICEINFO_BYID), ERR_DM_FAILED); + EXPECT_EQ(TestIpcRequestNull(GET_SERVICEINFOS_CALLER), ERR_DM_FAILED); +} + HWTEST_F(IpcCmdParserClientTest, TEST_READ_RESPONSE_NULL_001, testing::ext::TestSize.Level2) { EXPECT_EQ(TestReadResponseRspNull(UNREGISTER_DEVICE_MANAGER_LISTENER), ERR_DM_FAILED); @@ -1201,6 +1467,16 @@ HWTEST_F(IpcCmdParserClientTest, TEST_READ_RESPONSE_NULL_001, testing::ext::Test EXPECT_EQ(TestReadResponseRspNull(REGISTER_DEV_STATE_CALLBACK), ERR_DM_FAILED); EXPECT_EQ(TestReadResponseRspNull(SYNC_CALLBACK), ERR_DM_FAILED); } + +HWTEST_F(IpcCmdParserClientTest, TEST_READ_RESPONSE_NULL_002, testing::ext::TestSize.Level2) +{ + EXPECT_EQ(TestReadResponseRspNull(GEN_SERVICEID), ERR_DM_FAILED); + EXPECT_EQ(TestReadResponseRspNull(REG_SERVICE_INFO), ERR_DM_FAILED); + EXPECT_EQ(TestReadResponseRspNull(UNREG_SERVICE_INFO), ERR_DM_FAILED); + EXPECT_EQ(TestReadResponseRspNull(UPDATE_SERVICE_INFO), ERR_DM_FAILED); + EXPECT_EQ(TestReadResponseRspNull(GET_SERVICEINFO_BYID), ERR_DM_FAILED); + EXPECT_EQ(TestReadResponseRspNull(GET_SERVICEINFOS_CALLER), ERR_DM_FAILED); +} } // namespace } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file -- Gitee From 7539d4b85ed73612c58c236d6ac9a09a23313a15 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Mon, 10 Feb 2025 15:42:44 +0800 Subject: [PATCH 2/7] patch1 Change-Id: I390d8a4d56b91c55f762e5714e0d88e5f9ca4ff7 --- .../src/authentication/dm_auth_manager.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 1dc09a323..871b8b4c0 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -2742,7 +2742,11 @@ int32_t DmAuthManager::CheckTrustState() } if (authResponseContext_->isIdenticalAccount) { if (IsIdenticalAccount()) { - softbusConnector_->JoinLnn(authResponseContext_->deviceId); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authResponseContext_->deviceId); + } authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authResponseContext_->reply = DM_OK; @@ -2755,7 +2759,11 @@ int32_t DmAuthManager::CheckTrustState() authResponseContext_->localDeviceId)) { CompatiblePutAcl(); } - softbusConnector_->JoinLnn(authResponseContext_->deviceId); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + } else { + softbusConnector_->JoinLnn(authResponseContext_->deviceId); + } authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestState_->TransitionTo(std::make_shared()); return ALREADY_BIND; -- Gitee From afa55c9647dfe61c3e39b3e88b0d0f80737f29c6 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Tue, 11 Feb 2025 14:06:16 +0800 Subject: [PATCH 3/7] patch2 Change-Id: I0f2b1eadd85a588c45dd75d94c45f9fdcd0bfa9e Signed-off-by: zhuzhihui7 --- common/include/dm_constants.h | 1 - common/src/ipc/standard/ipc_model_codec.cpp | 8 +- .../src/deviceprofile_connector.cpp | 5 - .../native_cpp/src/device_manager_impl.cpp | 8 +- .../src/ipc/standard/ipc_cmd_parser.cpp | 16 +- .../js4.0/src/native_devicemanager_js.cpp | 17 +- .../authentication/auth_message_processor.h | 1 + .../include/authentication/dm_auth_manager.h | 8 +- .../authentication/auth_message_processor.cpp | 13 +- .../src/authentication/dm_auth_manager.cpp | 157 ++++++++++-------- .../hichain/hichain_auth_connector.cpp | 2 +- .../service/include/device_manager_service.h | 1 - .../service/src/device_manager_service.cpp | 42 ++--- .../src/ipc/standard/ipc_cmd_parser.cpp | 6 - test/unittest/UTTest_device_manager_impl.cpp | 104 ------------ test/unittest/UTTest_device_manager_impl.h | 1 - 16 files changed, 145 insertions(+), 245 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index c91b78b8a..62fe6cc29 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -50,7 +50,6 @@ enum { ERR_DM_BIND_USER_CANCEL_PIN_CODE_DISPLAY = -20056, ERR_DM_SYNC_DELETE_DEVICE_REPEATED = -20058, ERR_DM_VERSION_INCOMPATIBLE = -20059, - ERR_DM_SERVICEINFO_NOTVALID = -20060, ERR_DM_FAILED = 96929744, ERR_DM_NOT_INIT = 96929746, diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp index db7018754..36cf4c74b 100644 --- a/common/src/ipc/standard/ipc_model_codec.cpp +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -389,9 +389,9 @@ bool IpcModelCodec::EncodeServiceInfo(const DMServiceInfo &serviceInfo, MessageP bool IpcModelCodec::EncodeServiceInfos(const std::vector &serviceInfos, MessageParcel &parcel) { - uint32_t num = serviceInfos.size(); + uint32_t num = static_cast(serviceInfos.size()); if (!parcel.WriteUint32(num)){ - LOGE("[rm003] WriteInt32 num failed"); + LOGE("WriteUint32 num failed"); return false; } bool bRet = true; @@ -399,7 +399,7 @@ bool IpcModelCodec::EncodeServiceInfos(const std::vector &service const DMServiceInfo& serviceInfo = serviceInfos[k]; bRet = EncodeServiceInfo(serviceInfo, parcel); if (!bRet) { - LOGE("[rm003] encode serviceinfos failed"); + LOGE("EncodeServiceInfo failed"); break; } } @@ -434,7 +434,7 @@ bool IpcModelCodec::DecodeServiceInfos(MessageParcel &parcel, std::vector &serviceInfoProfiles) { - LOGI("[rm003] DeviceProfileConnector::GetServiceInfoProfileListByTokenId"); int32_t ret = DistributedDeviceProfileClient::GetInstance().GetServiceInfoProfileListByTokenId( key, serviceInfoProfiles); if (ret != DM_OK) { diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 2cfbcfb72..d38e33efc 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -2740,7 +2740,6 @@ int32_t DeviceManagerImpl::GetLocalDisplayDeviceName(const std::string &pkgName, int32_t DeviceManagerImpl::GenerateServiceId(int64_t &serviceId) { LOGI("Start"); - LOGI("[rm003] GenerateServiceId"); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); int32_t ret = ipcClientProxy_->SendRequest(GEN_SERVICEID, req, rsp); @@ -2761,7 +2760,6 @@ int32_t DeviceManagerImpl::GenerateServiceId(int64_t &serviceId) int32_t DeviceManagerImpl::RegisterServiceInfo(const DMServiceInfo &info) { LOGI("Start"); - LOGI("[rm003] RegisterServiceInfo"); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetServiceInfo(info); @@ -2782,7 +2780,6 @@ int32_t DeviceManagerImpl::RegisterServiceInfo(const DMServiceInfo &info) int32_t DeviceManagerImpl::UnRegisterServiceInfo(int64_t serviceId) { LOGI("Start"); - LOGI("[rm003] UnRegisterServiceInfo"); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetInt64Param(serviceId); @@ -2803,7 +2800,6 @@ int32_t DeviceManagerImpl::UnRegisterServiceInfo(int64_t serviceId) int32_t DeviceManagerImpl::UpdateServiceInfo(const DMServiceInfo &info) { LOGI("Start"); - LOGI("[rm003] UpdateServiceInfo"); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetServiceInfo(info); @@ -2823,8 +2819,7 @@ int32_t DeviceManagerImpl::UpdateServiceInfo(const DMServiceInfo &info) int32_t DeviceManagerImpl::GetServiceInfoByServiceId(int64_t serviceId, DMServiceInfo &info) { - LOGI("Start"); - LOGI("[rm003] GetServiceInfoByServiceId, %{public}" PRId64, serviceId); + LOGI("Start GetServiceInfoByServiceId, %{public}" PRId64, serviceId); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); req->SetInt64Param(serviceId); @@ -2846,7 +2841,6 @@ int32_t DeviceManagerImpl::GetServiceInfoByServiceId(int64_t serviceId, DMServic int32_t DeviceManagerImpl::GetCallerServiceInfos(std::vector &serviceInfos) { LOGI("Start"); - LOGI("[rm003] GetCallerServiceInfos"); std::shared_ptr req = std::make_shared(); std::shared_ptr rsp = std::make_shared(); int32_t ret = ipcClientProxy_->SendRequest(GET_SERVICEINFOS_CALLER, req, rsp); diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index 06126d3e3..54ba52960 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -1877,7 +1877,6 @@ ON_IPC_READ_RESPONSE(GET_LOCAL_DISPLAY_DEVICE_NAME, MessageParcel &reply, std::s ON_IPC_SET_REQUEST(GEN_SERVICEID, std::shared_ptr pBaseReq, MessageParcel &data) { - LOGI("[rm003] ON_IPC_SET_REQUEST(GEN_SERVICEID"); if (pBaseReq == nullptr) { LOGE("pBaseReq is null"); return ERR_DM_FAILED; @@ -1887,7 +1886,6 @@ ON_IPC_SET_REQUEST(GEN_SERVICEID, std::shared_ptr pBaseReq, MessageParce ON_IPC_READ_RESPONSE(GEN_SERVICEID, MessageParcel &reply, std::shared_ptr pBaseRsp) { - LOGI("[rm003] ON_IPC_READ_RESPONSE(GEN_SERVICEID"); std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); if (pRsp == nullptr) { LOGE("pRsp is null"); @@ -1906,7 +1904,6 @@ ON_IPC_SET_REQUEST(REG_SERVICE_INFO, std::shared_ptr pBaseReq, MessagePa LOGE("pBaseReq is null"); return ERR_DM_FAILED; } - LOGI("[rm003] ON_IPC_SET_REQUEST(REG_SERVICE_INFO"); std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); const DMServiceInfo& info = pReq->GetServiceInfo(); if (!IpcModelCodec::EncodeServiceInfo(info, data)) { @@ -1918,7 +1915,6 @@ ON_IPC_SET_REQUEST(REG_SERVICE_INFO, std::shared_ptr pBaseReq, MessagePa ON_IPC_READ_RESPONSE(REG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) { - LOGI("[rm003] ON_IPC_READ_RESPONSE(REG_SERVICE_INFO"); if (pBaseRsp == nullptr) { LOGE("pBaseRsp is null"); return ERR_DM_FAILED; @@ -1929,7 +1925,6 @@ ON_IPC_READ_RESPONSE(REG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseReq, MessageParcel &data) { - LOGI("[rm003] ON_IPC_SET_REQUEST(UNREG_SERVICE_INFO"); if (pBaseReq == nullptr) { LOGE("pBaseReq is null"); return ERR_DM_FAILED; @@ -1944,7 +1939,6 @@ ON_IPC_SET_REQUEST(UNREG_SERVICE_INFO, std::shared_ptr pBaseReq, Message ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) { - LOGI("[rm003] ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO"); if (pBaseRsp == nullptr) { LOGE("pBaseRsp is null"); return ERR_DM_FAILED; @@ -1955,7 +1949,6 @@ ON_IPC_READ_RESPONSE(UNREG_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseReq, MessageParcel &data) { - LOGI("[rm003] ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO"); if (pBaseReq == nullptr) { LOGE("pBaseReq is null"); return ERR_DM_FAILED; @@ -1971,7 +1964,6 @@ ON_IPC_SET_REQUEST(UPDATE_SERVICE_INFO, std::shared_ptr pBaseReq, Messag ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO, MessageParcel &reply, std::shared_ptr pBaseRsp) { - LOGI("[rm003] ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO"); if (pBaseRsp == nullptr) { LOGE("pBaseRsp is null"); return ERR_DM_FAILED; @@ -1982,7 +1974,6 @@ ON_IPC_READ_RESPONSE(UPDATE_SERVICE_INFO, MessageParcel &reply, std::shared_ptr< ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID, std::shared_ptr pBaseReq, MessageParcel &data) { - LOGI("[rm003] ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID"); if (pBaseReq == nullptr) { LOGE("pBaseReq is null"); return ERR_DM_FAILED; @@ -1997,7 +1988,6 @@ ON_IPC_SET_REQUEST(GET_SERVICEINFO_BYID, std::shared_ptr pBaseReq, Messa ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID, MessageParcel &reply, std::shared_ptr pBaseRsp) { - LOGI("[rm003] ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID"); if (pBaseRsp == nullptr) { LOGE("pBaseRsp is null"); return ERR_DM_FAILED; @@ -2008,7 +1998,7 @@ ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID, MessageParcel &reply, std::shared_ptr DMServiceInfo info; bool ret = IpcModelCodec::DecodeServiceInfo(reply, info); if (!ret) { - LOGE("[rm003] DecodeServiceInfo failed"); + LOGE("DecodeServiceInfo failed"); pBaseRsp->SetErrCode(ERR_DM_IPC_READ_FAILED); } pRsp->SetServiceInfo(info); @@ -2018,7 +2008,6 @@ ON_IPC_READ_RESPONSE(GET_SERVICEINFO_BYID, MessageParcel &reply, std::shared_ptr ON_IPC_SET_REQUEST(GET_SERVICEINFOS_CALLER, std::shared_ptr pBaseReq, MessageParcel &data) { - LOGI("[rm003] ON_IPC_SET_REQUEST(GET_SERVICEINFOS_CALLER"); if (pBaseReq == nullptr) { LOGE("pBaseReq is null"); return ERR_DM_FAILED; @@ -2028,7 +2017,6 @@ ON_IPC_SET_REQUEST(GET_SERVICEINFOS_CALLER, std::shared_ptr pBaseReq, Me ON_IPC_READ_RESPONSE(GET_SERVICEINFOS_CALLER, MessageParcel &reply, std::shared_ptr pBaseRsp) { - LOGI("[rm003] ON_IPC_READ_RESPONSE(GET_SERVICEINFOS_CALLER"); if (pBaseRsp == nullptr) { LOGE("pBaseRsp is null"); return ERR_DM_FAILED; @@ -2039,7 +2027,7 @@ ON_IPC_READ_RESPONSE(GET_SERVICEINFOS_CALLER, MessageParcel &reply, std::shared_ std::vector infos; bool ret = IpcModelCodec::DecodeServiceInfos(reply, infos); if (!ret) { - LOGE("[rm003] DecodeServiceInfo failed"); + LOGE("DecodeServiceInfo failed"); pBaseRsp->SetErrCode(ERR_DM_IPC_READ_FAILED); } pRsp->SetServiceInfos(infos); diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index f48aabd63..fc989b70c 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp @@ -1711,6 +1711,20 @@ bool xProcessRegisterServiceInfo(std::string bindParam) return false; } +void xProcessPincode(std::string bindParam, std::string pkgName) +{ + nlohmann::json bindParamObj = nlohmann::json::parse(bindParam, nullptr, false); + if (bindParamObj.is_discarded()) { + return; + } + std::string pincode; + if (IsString(bindParamObj, PARAM_KEY_PIN_CODE)) { + pincode = bindParamObj[PARAM_KEY_PIN_CODE].get(); + LOGI("[temp003] xProcessPincode = %{public}s", pincode.c_str()); + DeviceManager::GetInstance().ImportAuthCode(pkgName, pincode); + } +} + void DeviceManagerNapi::BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, const std::string &deviceId, napi_env env, napi_value &object) { @@ -1723,7 +1737,8 @@ void DeviceManagerNapi::BindDevOrTarget(DeviceManagerNapi *deviceManagerWrapper, if (processRet) { return; } - LOGI("[rm003] BindDevOrTarget %{public}s", bindParam.c_str()); + xProcessPincode(bindParam, deviceManagerWrapper->bundleName_); + if (isMetaType) { std::shared_ptr bindTargetCallback = nullptr; { diff --git a/services/implementation/include/authentication/auth_message_processor.h b/services/implementation/include/authentication/auth_message_processor.h index b97818a49..bfa61a4b0 100644 --- a/services/implementation/include/authentication/auth_message_processor.h +++ b/services/implementation/include/authentication/auth_message_processor.h @@ -119,6 +119,7 @@ private: void GetJsonObj(nlohmann::json &jsonObj); void CreateReqReCheckMessage(nlohmann::json &jsonObj); void ParseReqReCheckMessage(nlohmann::json &json); + bool IsImportedPincodeValid(); private: std::weak_ptr authMgr_; diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 406e9142b..5bafbbeb2 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -137,9 +137,9 @@ typedef struct DmAuthRequestContext { int32_t closeSessionDelaySeconds = 0; int32_t presetPinCode = 0; std::string connSessionType; + int32_t hmlActionId = 0; int32_t hmlReleaseTime = 0; bool hmlEnable160M = false; - int32_t hmlActionId = 0; } DmAuthRequestContext; typedef struct DmAuthResponseContext { @@ -481,12 +481,16 @@ public: int32_t RegisterAuthenticationType(int32_t authenticationType); static bool IsPinCodeValid(std::string strpin); static bool IsPinCodeValid(int32_t numpin); + bool IsImportedAuthCodeValid(); private: bool IsHmlSessionType(); bool CanUsePinFromDp(); void InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); bool IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceInfoProfile &profile); + int64_t GetBinderTokenId(); + bool CheckNeedShowAuthInfoDialog(); + void JoinLnn(const std::string &deviceId); int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra); int32_t CheckAuthParamVaildExtra(const std::string &extra); @@ -540,6 +544,7 @@ private: void GetAuthParam(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra); void ParseJsonObject(nlohmann::json jsonObject); + void ParseHmlInfoInJsonObject(nlohmann::json jsonObject); int32_t DeleteAcl(const std::string &pkgName, const std::string &localUdid, const std::string &remoteUdid, int32_t bindLevel, const std::string &extra); void ProcessAuthRequestExt(const int32_t &sessionId); @@ -565,7 +570,6 @@ private: int32_t GetBindLevel(int32_t bindLevel); std::string GetBundleName(nlohmann::json &jsonObject); int32_t GetBinderInfo(); - int64_t GetBinderTokenId(); void SetProcessInfo(); int32_t GetCloseSessionDelaySeconds(std::string &delaySecondsStr); void ConverToFinish(); diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index 76175a452..ebe09ab75 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -71,8 +71,8 @@ void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj) } else { jsonObj[TAG_IS_SHOW_DIALOG] = true; } - if (DmAuthManager::IsPinCodeValid(authRequestContext_->presetPinCode)) { - LOGI("[rm003-nopincode] TAG_PINCODE_SETTED"); + if (authRequestContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && IsImportedPincodeValid()) { + LOGI("[rm003-nopincode] TAG_PINCODE_SETTED set"); jsonObj[TAG_PINCODE_SETTED] = true; } else { LOGI("[rm003-nopincode] TAG_PINCODE_SETTED not set"); @@ -89,6 +89,15 @@ void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj) } } +bool AuthMessageProcessor::IsImportedPincodeValid() +{ + auto sptrAuthMgr = authMgr_.lock(); + if (sptrAuthMgr == nullptr) { + return false; + } + return sptrAuthMgr->IsImportedAuthCodeValid(); +} + std::vector AuthMessageProcessor::CreateAuthRequestMessage() { LOGI("AuthMessageProcessor::CreateAuthRequestMessage start."); diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 871b8b4c0..78abee454 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -307,41 +307,44 @@ void DmAuthManager::ParseJsonObject(nlohmann::json jsonObject) } else { authRequestContext_->peerBundleName = authRequestContext_->hostPkgName; } - if (IsString(jsonObject, PARAM_KEY_PIN_CODE)) { - std::string strpin = jsonObject[PARAM_KEY_PIN_CODE].get(); - if (DmAuthManager::IsPinCodeValid(strpin)) { - authRequestContext_->presetPinCode = std::stoi(strpin); - LOGI("[rm003-nopincode] input pincode %{public}s", strpin.c_str()); - } - } - if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { - authRequestContext_->connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); - LOGI("[rm003-nopincode] input connSessionType %{public}s", authRequestContext_->connSessionType.c_str()); - } - if (IsInt32(jsonObject, PARAM_KEY_HML_RELEASETIME)) { - authRequestContext_->hmlReleaseTime = jsonObject[PARAM_KEY_HML_RELEASETIME].get(); - LOGI("[rm003-nopincode] input hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); - } - if (IsBool(jsonObject, PARAM_KEY_HML_ENABLE_160M)) { - authRequestContext_->hmlEnable160M = jsonObject[PARAM_KEY_HML_ENABLE_160M].get(); - LOGI("[rm003-nopincode] input hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); - } - if (IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { - authRequestContext_->hmlActionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); - if (authRequestContext_->hmlActionId <= 0) { - authRequestContext_->hmlActionId = 0; - } - LOGI("[rm003-nopincode] input hmlActionId %{public}d", authRequestContext_->hmlActionId); + ParseHmlInfoInJsonObject(jsonObject); + } + authRequestContext_->bundleName = GetBundleName(jsonObject); +} + +void DmAuthManager::ParseHmlInfoInJsonObject(nlohmann::json jsonObject) +{ + if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { + authRequestContext_->connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); + LOGI("[rm003-nopincode] input connSessionType %{public}s", authRequestContext_->connSessionType.c_str()); + } + if (!IsHmlSessionType()) { + LOGI("[rm003-nopincode] not HML session type"); + return; + } + if (IsInt32(jsonObject, PARAM_KEY_HML_RELEASETIME)) { + authRequestContext_->hmlReleaseTime = jsonObject[PARAM_KEY_HML_RELEASETIME].get(); + if (authRequestContext_->hmlReleaseTime <= 0) { + authRequestContext_->hmlReleaseTime = 0; } - if (IsHmlSessionType()) { - if (authRequestContext_->hmlReleaseTime > 0) { - authRequestContext_->closeSessionDelaySeconds = authRequestContext_->hmlReleaseTime; - } else { - authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; - } + LOGI("[rm003-nopincode] input hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); + } + if (IsBool(jsonObject, PARAM_KEY_HML_ENABLE_160M)) { + authRequestContext_->hmlEnable160M = jsonObject[PARAM_KEY_HML_ENABLE_160M].get(); + LOGI("[rm003-nopincode] input hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); + } + if (IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { + authRequestContext_->hmlActionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); + if (authRequestContext_->hmlActionId <= 0) { + authRequestContext_->hmlActionId = 0; } + LOGI("[rm003-nopincode] input hmlActionId %{public}d", authRequestContext_->hmlActionId); + } + if (authRequestContext_->hmlReleaseTime > 0) { + authRequestContext_->closeSessionDelaySeconds = authRequestContext_->hmlReleaseTime; + } else { + authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; } - authRequestContext_->bundleName = GetBundleName(jsonObject); } int32_t DmAuthManager::GetCloseSessionDelaySeconds(std::string &delaySecondsStr) @@ -1161,11 +1164,7 @@ bool DmAuthManager::IsAuthFinish() if ((authResponseContext_->isIdenticalAccount && !authResponseContext_->authed) || (authResponseContext_->authed && !authResponseContext_->isOnline)) { - if (IsHmlSessionType()) { - softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); - } else { - softbusConnector_->JoinLnn(authRequestContext_->addr); - } + JoinLnn(authRequestContext_->addr); authRequestContext_->reason = DM_OK; authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authResponseContext_->reply = DM_OK; @@ -1754,10 +1753,11 @@ void DmAuthManager::ShowStartAuthDialog() return; } LOGI("DmAuthManager::ShowStartAuthDialog start"); - LOGI("[rm003] presetPinCode %{public}d", authRequestContext_->presetPinCode); - if (authRequestContext_->presetPinCode > 0) { - LOGI("[rm003] already has pin code"); - ProcessPincode(authRequestContext_->presetPinCode); + int32_t pincode = 0; + if (GetAuthCode(authResponseContext_->hostPkgName, pincode) == DM_OK && + IsPinCodeValid(pincode)) { + LOGI("[rm003] already has pin code, %{public}d", pincode); + ProcessPincode(pincode); } else { pincodeDialogEverShown_ = true; DmDialogManager::GetInstance().ShowInputDialog(authResponseContext_->targetDeviceName); @@ -2100,10 +2100,10 @@ bool DmAuthManager::IsAuthCodeReady(const std::string &pkgName) int32_t DmAuthManager::DeleteAuthCode() { - if (authResponseContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) { + //if (authResponseContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) { importAuthCode_ = ""; importPkgName_ = ""; - } + //} return DM_OK; } @@ -2121,6 +2121,16 @@ int32_t DmAuthManager::GetAuthCode(const std::string &pkgName, int32_t &pinCode) return DM_OK; } +bool DmAuthManager::IsImportedAuthCodeValid() +{ + int32_t pinCode = 0; + if (GetAuthCode(authRequestContext_->hostPkgName, pinCode) == DM_OK && + DmAuthManager::IsPinCodeValid(pinCode)) { + return true; + } + return false; +} + bool DmAuthManager::IsAuthTypeSupported(const int32_t &authType) { if (authenticationMap_.find(authType) == authenticationMap_.end()) { @@ -2179,11 +2189,7 @@ void DmAuthManager::RequestCredentialDone() if (timer_ != nullptr) { timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); } - if (IsHmlSessionType()) { - softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); - } else { - softbusConnector_->JoinLnn(authRequestContext_->addr); - } + JoinLnn(authRequestContext_->addr); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authResponseContext_->reply = DM_OK; @@ -2277,11 +2283,7 @@ void DmAuthManager::SrcAuthDeviceFinish() return; } if (!authResponseContext_->isOnline && authResponseContext_->haveCredential) { - if (IsHmlSessionType()) { - softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); - } else { - softbusConnector_->JoinLnn(authRequestContext_->addr); - } + JoinLnn(authRequestContext_->addr); timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); ConverToFinish(); return; @@ -2364,10 +2366,7 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) LOGI("AuthDeviceError start."); isAuthDevice_ = false; if (authRequestState_ == nullptr || authResponseState_ != nullptr) { - LOGI("AuthDeviceError sink authTimes %{public}d.", authTimes_); - if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && !pincodeDialogEverShown_ && CanUsePinFromDp()) { - LOGI("[rm003-nopincode] AuthDeviceError, ShowAuthInfoDialog"); - ShowAuthInfoDialog(true); + if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && CheckNeedShowAuthInfoDialog()) { return; } authTimes_++; @@ -2409,9 +2408,10 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) DmAuthManager::HandleAuthenticateTimeout(name); }); } - authRequestContext_->presetPinCode = 0; - if (!pincodeDialogEverShown_ && errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { + if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && !pincodeDialogEverShown_ && + errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { LOGI("[rm003] auth error, ShowStartAuthDialog"); + DeleteAuthCode(); ShowStartAuthDialog(); } else { authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); @@ -2742,11 +2742,7 @@ int32_t DmAuthManager::CheckTrustState() } if (authResponseContext_->isIdenticalAccount) { if (IsIdenticalAccount()) { - if (IsHmlSessionType()) { - softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); - } else { - softbusConnector_->JoinLnn(authResponseContext_->deviceId); - } + JoinLnn(authResponseContext_->deviceId); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authResponseContext_->reply = DM_OK; @@ -2759,11 +2755,7 @@ int32_t DmAuthManager::CheckTrustState() authResponseContext_->localDeviceId)) { CompatiblePutAcl(); } - if (IsHmlSessionType()) { - softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); - } else { - softbusConnector_->JoinLnn(authResponseContext_->deviceId); - } + JoinLnn(authResponseContext_->deviceId); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestState_->TransitionTo(std::make_shared()); return ALREADY_BIND; @@ -3105,8 +3097,8 @@ void DmAuthManager::RequestReCheckMsgDone() authRequestState_->TransitionTo(std::make_shared()); return; } - PutAccessControlList(); authRequestState_->TransitionTo(std::make_shared()); + PutAccessControlList(); } bool DmAuthManager::IsSinkMsgValid() @@ -3157,5 +3149,30 @@ void DmAuthManager::ProcessReqPublicKey() isNeedProcCachedSrcReqMsg_ = true; } } + +bool DmAuthManager::CheckNeedShowAuthInfoDialog() +{ + CHECK_NULL_RETURN(authResponseContext_, false); + if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && + !pincodeDialogEverShown_ && CanUsePinFromDp()) { + authResponseContext_->code = GeneratePincode(); + LOGI("[rm003-nopincode] AuthDeviceError, GeneratePincode, ShowAuthInfoDialog"); + ShowAuthInfoDialog(true); + return true; + } + return false; +} + +void DmAuthManager::JoinLnn(const std::string &deviceId) +{ + CHECK_NULL_VOID(authRequestContext_); + CHECK_NULL_VOID(softbusConnector_); + if (IsHmlSessionType()) { + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + return; + } + softbusConnector_->JoinLnn(authRequestContext_->addr); +} + } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index eeb4da1d9..2c22d3baf 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -148,7 +148,7 @@ void HiChainAuthConnector::onError(int64_t requestId, int operationCode, int err int32_t dmErrorCode = ERR_DM_FAILED; if (errorCode == PROOF_MISMATCH) { dmErrorCode = ERR_DM_HICHAIN_PROOFMISMATCH; - } + } dmDeviceAuthCallback_->AuthDeviceError(requestId, dmErrorCode); } diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 3bc1d0bd6..78b92684d 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -185,7 +185,6 @@ public: int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map &unbindParam); - bool IsServiceInfoValid(const DMServiceInfo &serviceInfo); int64_t GenerateSerivceId(); bool InitServiceInfoProfile(const DMServiceInfo &serviceInfo, DistributedDeviceProfile::ServiceInfoProfile &profile); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 7f01daa6a..67bf84c65 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1470,23 +1470,17 @@ int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const Pee return dmServiceImplExtResident_->UnbindTargetExt(pkgName, targetId, unbindParam); } -bool DeviceManagerService::IsServiceInfoValid(const DMServiceInfo &serviceInfo) -{ - //todo - return true; -} - int64_t DeviceManagerService::GenerateSerivceId() { DmDeviceInfo devInfo; int32_t ret = GetLocalDeviceInfo(devInfo); if (ret != DM_OK) { - LOGE("[rm003] GetLocalDeviceInfo failed"); + LOGE("GetLocalDeviceInfo failed"); return 0; } std::string udid = devInfo.deviceId; if (udid.empty()) { - LOGE("[rm003] udid empty"); + LOGE("udid empty"); return 0; } int64_t tokenId = IPCSkeleton::GetCallingTokenID(); @@ -1499,7 +1493,7 @@ int64_t DeviceManagerService::GenerateSerivceId() *((int64_t*)dataPtr) = tokenId; dataPtr += sizeof(int64_t); if (memcpy_s(dataPtr, udid.length(), udid.c_str(), udid.length()) != EOK) { - LOGE("[rm003] memcpy udid failed"); + LOGE("memcpy udid failed"); return 0; } @@ -1512,10 +1506,10 @@ int64_t DeviceManagerService::GenerateSerivceId() int64_t serviceId = 0; if (memcpy_s(&serviceId, sizeof(int64_t), out, sizeof(int64_t)) != EOK) { serviceId = 0; - LOGE("[rm003] memcpy serviceId failed"); + LOGE("memcpy serviceId failed"); } serviceId = std::abs(serviceId); - LOGI("[rm003] GenerateSeivceId %{public}" PRId64, serviceId); + LOGI("GenerateSeivceId %{public}" PRId64, serviceId); return serviceId; } @@ -1525,7 +1519,7 @@ bool DeviceManagerService::InitServiceInfoProfile(const DMServiceInfo &serviceIn DmDeviceInfo devInfo; int32_t ret = GetLocalDeviceInfo(devInfo); if (ret != DM_OK) { - LOGE("[rm003] GetLocalDeviceInfo failed"); + LOGE("GetLocalDeviceInfo failed"); return false; } char localDeviceId[DEVICE_UUID_LENGTH] = {0}; @@ -1594,15 +1588,11 @@ bool DeviceManagerService::InitServiceInfoUniqueKey(DistributedDeviceProfile::Se int32_t DeviceManagerService::RegisterServiceInfo(DMServiceInfo &serviceInfo) { //todo permission verifier - if (!IsServiceInfoValid(serviceInfo)) { - LOGE("[rm003] IsServiceInfoValid failed"); - return ERR_DM_SERVICEINFO_NOTVALID; - } - LOGI("[rm003] DeviceManagerService::RegisterServiceInfo"); + LOGI("start, serviceId %{public}" PRId64, serviceInfo.serviceId); DistributedDeviceProfile::ServiceInfoProfile profile; bool success = InitServiceInfoProfile(serviceInfo, profile); if (!success) { - LOGE("[rm003] InitServiceInfoProfile failed"); + LOGE("InitServiceInfoProfile failed"); return ERR_DM_FAILED; } int32_t result = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(profile); @@ -1611,11 +1601,11 @@ int32_t DeviceManagerService::RegisterServiceInfo(DMServiceInfo &serviceInfo) int32_t DeviceManagerService::UnRegisterServiceInfo(int64_t serviceId) { - LOGI("[rm003] DeviceManagerService::UnRegisterServiceInfo"); + LOGI("start, serviceId %{public}" PRId64, serviceId); DistributedDeviceProfile::ServiceInfoUniqueKey key; bool result = InitServiceInfoUniqueKey(key); if (!result) { - LOGE("[rm003] InitServiceInfoUniqueKey failed"); + LOGE("InitServiceInfoUniqueKey failed"); return ERR_DM_FAILED; } key.SetServiceId(serviceId); @@ -1624,11 +1614,11 @@ int32_t DeviceManagerService::UnRegisterServiceInfo(int64_t serviceId) int32_t DeviceManagerService::UpdateServiceInfo(DMServiceInfo &serviceInfo) { - LOGI("[rm003] DeviceManagerService::UpdateServiceInfo"); + LOGI("start, serviceId %{public}" PRId64, serviceInfo.serviceId); DistributedDeviceProfile::ServiceInfoProfile profile; bool success = InitServiceInfoProfile(serviceInfo, profile); if (!success) { - LOGE("[rm003] InitServiceInfoProfile failed"); + LOGE("InitServiceInfoProfile failed"); return ERR_DM_FAILED; } int32_t result = DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(profile); @@ -1637,11 +1627,11 @@ int32_t DeviceManagerService::UpdateServiceInfo(DMServiceInfo &serviceInfo) int32_t DeviceManagerService::GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo) { - LOGI("[rm003] DeviceManagerService::GetServiceInfoById, %{public}" PRId64, serviceId); + LOGI("start, serviceId %{public}" PRId64, serviceId); DistributedDeviceProfile::ServiceInfoUniqueKey key; bool result = InitServiceInfoUniqueKey(key); if (!result) { - LOGE("[rm003] InitServiceInfoUniqueKey failed"); + LOGE("InitServiceInfoUniqueKey failed"); return ERR_DM_FAILED; } key.SetServiceId(serviceId); @@ -1655,11 +1645,11 @@ int32_t DeviceManagerService::GetServiceInfoById(int64_t serviceId, DMServiceInf int32_t DeviceManagerService::GetCallerServiceInfos(std::vector &serviceInfos) { - LOGI("[rm003] DeviceManagerService::GetCallerServiceInfos"); + LOGI("start"); DistributedDeviceProfile::ServiceInfoUniqueKey key; bool result = InitServiceInfoUniqueKey(key); if (!result) { - LOGE("[rm003] InitServiceInfoUniqueKey failed"); + LOGE("InitServiceInfoUniqueKey failed"); return ERR_DM_FAILED; } std::vector profiles; diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index 0716cb924..135660bc4 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -1852,7 +1852,6 @@ ON_IPC_CMD(GET_LOCAL_DISPLAY_DEVICE_NAME, MessageParcel &data, MessageParcel &re ON_IPC_CMD(GEN_SERVICEID, MessageParcel &data, MessageParcel &reply) { - LOGI("[rm003] ON_IPC_CMD(GEN_SERVICEID"); int64_t serviceId = DeviceManagerService::GetInstance().GenerateSerivceId(); int32_t result = DM_OK; if (serviceId == 0) { @@ -1871,7 +1870,6 @@ ON_IPC_CMD(GEN_SERVICEID, MessageParcel &data, MessageParcel &reply) ON_IPC_CMD(REG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) { - LOGI("[rm003] ON_IPC_CMD(REG_SERVICE_INFO"); DMServiceInfo serviceInfo; IpcModelCodec::DecodeServiceInfo(data, serviceInfo); int32_t result = DeviceManagerService::GetInstance().RegisterServiceInfo(serviceInfo); @@ -1884,7 +1882,6 @@ ON_IPC_CMD(REG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) ON_IPC_CMD(UNREG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) { - LOGI("[rm003] ON_IPC_CMD(UNREG_SERVICE_INFO"); int64_t serviceId = data.ReadInt64(); int32_t result = DeviceManagerService::GetInstance().UnRegisterServiceInfo(serviceId); if (!reply.WriteInt32(result)) { @@ -1896,7 +1893,6 @@ ON_IPC_CMD(UNREG_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) ON_IPC_CMD(UPDATE_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) { - LOGI("[rm003] ON_IPC_CMD(UPDATE_SERVICE_INFO"); DMServiceInfo serviceInfo; IpcModelCodec::DecodeServiceInfo(data, serviceInfo); int32_t result = DeviceManagerService::GetInstance().UpdateServiceInfo(serviceInfo); @@ -1909,7 +1905,6 @@ ON_IPC_CMD(UPDATE_SERVICE_INFO, MessageParcel &data, MessageParcel &reply) ON_IPC_CMD(GET_SERVICEINFO_BYID, MessageParcel &data, MessageParcel &reply) { - LOGI("[rm003] ON_IPC_CMD(GET_SERVICEINFO_BYID"); int64_t serviceId = data.ReadInt64(); DMServiceInfo serviceInfo; int32_t result = DeviceManagerService::GetInstance().GetServiceInfoById(serviceId, serviceInfo); @@ -1926,7 +1921,6 @@ ON_IPC_CMD(GET_SERVICEINFO_BYID, MessageParcel &data, MessageParcel &reply) ON_IPC_CMD(GET_SERVICEINFOS_CALLER, MessageParcel &data, MessageParcel &reply) { - LOGI("[rm003] ON_IPC_CMD(GET_SERVICEINFOS_CALLER"); std::vector serviceInfos; int32_t result = DeviceManagerService::GetInstance().GetCallerServiceInfos(serviceInfos); if (!reply.WriteInt32(result)) { diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index 91e106511..3ccd06d71 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -80,22 +80,6 @@ void DeviceManagerImplTest::TearDownTestCase() deviceManagerNotifyMock_ = nullptr; } -void DeviceManagerImplTest::InitServiceInfo(DMServiceInfo &serviceInfo) -{ - serviceInfo.serviceType = "profile.serviceType"; - serviceInfo.serviceName = "profile.serviceName"; - serviceInfo.serviceDisplayName = "profile.serviceDisplayName"; - serviceInfo.customData = "profile.customData"; - serviceInfo.bundleName = "profile.bundleName"; - serviceInfo.moduleName = "profile.moduleName"; - serviceInfo.abilityName = "profile.abilityName"; - serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; - serviceInfo.authType = DMServiceInfoAuthType_TrustOneTime; - serviceInfo.pinExchangeType = DMServiceInfoPinExchangeType_FromDp; - serviceInfo.pinCode = "123456"; - serviceInfo.description = "profile.description"; - serviceInfo.serviceDiscoveryScope = "profile.serviceDiscoveryScope"; -} namespace { /** * @tc.name: InitDeviceManager_001 @@ -2570,94 +2554,6 @@ HWTEST_F(DeviceManagerImplTest, GetDeviceSecurityLevel_001, testing::ext::TestSi ret = DeviceManager::GetInstance().GetDeviceSecurityLevel(pkgName, netWorkId, securityLevel); ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } - -HWTEST_F(DeviceManagerImplTest, REG_SERVICEINFO_101, testing::ext::TestSize.Level0) -{ - std::string packName = "com.ohos.test"; - std::shared_ptr callback = std::make_shared(); - int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); - ASSERT_EQ(ret, DM_OK); - - DMServiceInfo serviceInfoOld; - InitServiceInfo(serviceInfoOld); - - int64_t serviceId = 0; - ret = DeviceManager::GetInstance().GenerateServiceId(serviceId); - ASSERT_EQ(ret, DM_OK); - DMServiceInfo serviceInfo; - InitServiceInfo(serviceInfo); - serviceInfo.serviceId = serviceId; - ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo); - ASSERT_EQ(ret, DM_OK); - DMServiceInfo retInfo; - LOGI("[rm003] serviceId %{public}" PRId64, serviceId); - ret = DeviceManager::GetInstance().GetServiceInfoByServiceId(serviceId, retInfo); - ASSERT_EQ(ret, DM_OK); - ASSERT_TRUE(serviceId == retInfo.serviceId); - ASSERT_TRUE(serviceInfoOld.serviceType == retInfo.serviceType); - ASSERT_TRUE(serviceInfoOld.abilityName == retInfo.abilityName); - ASSERT_TRUE(serviceInfoOld.serviceDiscoveryScope == retInfo.serviceDiscoveryScope); - - retInfo.abilityName = "kkk"; - ret = DeviceManager::GetInstance().UpdateServiceInfo(retInfo); - ASSERT_EQ(ret, DM_OK); - - ret = DeviceManager::GetInstance().GetServiceInfoByServiceId(serviceId, retInfo); - ASSERT_EQ(ret, DM_OK); - ASSERT_TRUE(retInfo.abilityName == "kkk"); - - std::vector infos; - ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); - ASSERT_EQ(ret, DM_OK); - for (const auto &item : infos) { - DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); - } - infos.clear(); - ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); - ASSERT_NE(ret, DM_OK); - ASSERT_EQ(infos.size(), 0); - DeviceManager::GetInstance().UnInitDeviceManager(packName); -} - -HWTEST_F(DeviceManagerImplTest, REG_SERVICEINFO_102, testing::ext::TestSize.Level0) -{ - std::string packName = "com.ohos.test"; - std::shared_ptr callback = std::make_shared(); - int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); - ASSERT_EQ(ret, DM_OK); - - std::vector infos; - ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); - for (const auto &item : infos) { - DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); - } - infos.clear(); - - DMServiceInfo serviceInfo1; - InitServiceInfo(serviceInfo1); - ret = DeviceManager::GetInstance().GenerateServiceId(serviceInfo1.serviceId); - ASSERT_EQ(ret, DM_OK); - ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo1); - ASSERT_EQ(ret, DM_OK); - - DMServiceInfo serviceInfo2; - InitServiceInfo(serviceInfo2); - ret = DeviceManager::GetInstance().GenerateServiceId(serviceInfo2.serviceId); - ASSERT_EQ(ret, DM_OK); - ret = DeviceManager::GetInstance().RegisterServiceInfo(serviceInfo2); - ASSERT_EQ(ret, DM_OK); - - ret = DeviceManager::GetInstance().GetCallerServiceInfos(infos); - ASSERT_EQ(ret, DM_OK); - ASSERT_TRUE(infos.size() >= 2); - for (const auto &item : infos) { - LOGI("[rm003] UnRegisterServiceInfo, serviceId %{public}" PRId64, item.serviceId); - DeviceManager::GetInstance().UnRegisterServiceInfo(item.serviceId); - } - infos.clear(); - DeviceManager::GetInstance().UnInitDeviceManager(packName); -} - } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_impl.h b/test/unittest/UTTest_device_manager_impl.h index 82271328e..92f1c661f 100644 --- a/test/unittest/UTTest_device_manager_impl.h +++ b/test/unittest/UTTest_device_manager_impl.h @@ -39,7 +39,6 @@ public: static inline std::shared_ptr deviceManagerNotifyMock_ = std::make_shared(); - void InitServiceInfo(DMServiceInfo &serviceInfo); }; class DeviceDiscoveryCallbackTest : public DiscoveryCallback { -- Gitee From 2c118230b0548d955212a63a17b1135028f5d143 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Thu, 13 Feb 2025 08:56:41 +0800 Subject: [PATCH 4/7] patch3 Change-Id: I4110e91223d7d4b31599f22df86f7b832466268c --- services/implementation/BUILD.gn | 1 + .../authentication/auth_message_processor.h | 4 +- .../include/authentication/dm_auth_manager.h | 5 +- .../authentication/auth_message_processor.cpp | 28 +++--- .../src/authentication/dm_auth_manager.cpp | 99 ++++++++++++------- .../service/src/device_manager_service.cpp | 11 +-- utils/include/crypto/dm_crypto.h | 2 + 7 files changed, 89 insertions(+), 61 deletions(-) diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index e841ebcdc..3b442952c 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -230,6 +230,7 @@ if (defined(ohos_lite)) { "init:libbegetutil", "ipc:ipc_core", "mbedtls:mbedtls_shared", + "openssl:libcrypto_shared", "os_account:libaccountkits", "resource_management:resmgr_napi_core", "samgr:samgr_proxy", diff --git a/services/implementation/include/authentication/auth_message_processor.h b/services/implementation/include/authentication/auth_message_processor.h index bfa61a4b0..03e4b9523 100644 --- a/services/implementation/include/authentication/auth_message_processor.h +++ b/services/implementation/include/authentication/auth_message_processor.h @@ -43,7 +43,7 @@ constexpr const char* TAG_INDEX = "INDEX"; constexpr const char* TAG_SLICE_NUM = "SLICE"; constexpr const char* TAG_IS_AUTH_CODE_READY = "IS_AUTH_CODE_READY"; constexpr const char* TAG_IS_SHOW_DIALOG = "IS_SHOW_DIALOG"; -constexpr const char* TAG_PINCODE_SETTED = "TAG_PINCODE_SETTED"; +constexpr const char* TAG_SRC_PINCODE_IMPORTED = "TAG_SRC_PINCODE_IMPORTED"; constexpr const char* TAG_TOKEN = "TOKEN"; constexpr const char* TAG_CRYPTO_NAME = "CRYPTONAME"; constexpr const char* TAG_CRYPTO_VERSION = "CRYPTOVERSION"; @@ -119,7 +119,7 @@ private: void GetJsonObj(nlohmann::json &jsonObj); void CreateReqReCheckMessage(nlohmann::json &jsonObj); void ParseReqReCheckMessage(nlohmann::json &json); - bool IsImportedPincodeValid(); + bool IsPincodeImported(); private: std::weak_ptr authMgr_; diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 5bafbbeb2..1d16dcae0 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -135,7 +135,6 @@ typedef struct DmAuthRequestContext { std::string addr; std::string hostPkgLabel; int32_t closeSessionDelaySeconds = 0; - int32_t presetPinCode = 0; std::string connSessionType; int32_t hmlActionId = 0; int32_t hmlReleaseTime = 0; @@ -197,7 +196,7 @@ typedef struct DmAuthResponseContext { bool isFinish = false; std::string edition; int32_t localBindLevel; - bool isPincodeSetted = false; + bool isSrcPincodeImported = false; } DmAuthResponseContext; class AuthMessageProcessor; @@ -489,7 +488,9 @@ private: void InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); bool IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceInfoProfile &profile); int64_t GetBinderTokenId(); + void GetServiceInfoProfile(); bool CheckNeedShowAuthInfoDialog(); + void UpdateInputPincodeDialog(int32_t errorCode); void JoinLnn(const std::string &deviceId); int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra); diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index ebe09ab75..568cd947a 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -71,13 +71,6 @@ void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj) } else { jsonObj[TAG_IS_SHOW_DIALOG] = true; } - if (authRequestContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && IsImportedPincodeValid()) { - LOGI("[rm003-nopincode] TAG_PINCODE_SETTED set"); - jsonObj[TAG_PINCODE_SETTED] = true; - } else { - LOGI("[rm003-nopincode] TAG_PINCODE_SETTED not set"); - jsonObj[TAG_PINCODE_SETTED] = false; - } jsonObj[TAG_APP_OPERATION] = authRequestContext_->appOperation; jsonObj[TAG_CUSTOM_DESCRIPTION] = authRequestContext_->customDesc; jsonObj[TAG_APP_NAME] = authRequestContext_->appName; @@ -89,7 +82,7 @@ void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj) } } -bool AuthMessageProcessor::IsImportedPincodeValid() +bool AuthMessageProcessor::IsPincodeImported() { auto sptrAuthMgr = authMgr_.lock(); if (sptrAuthMgr == nullptr) { @@ -233,6 +226,13 @@ void AuthMessageProcessor::CreateNegotiateMessage(nlohmann::json &json) json[TAG_HAVE_CREDENTIAL] = authResponseContext_->haveCredential; json[TAG_HOST_PKGLABEL] = authResponseContext_->hostPkgLabel; json[TAG_EDITION] = authResponseContext_->edition; + if (authRequestContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && IsPincodeImported()) { + LOGI("[rm003-nopincode] TAG_SRC_PINCODE_IMPORTED set"); + json[TAG_SRC_PINCODE_IMPORTED] = true; + } else { + LOGI("[rm003-nopincode] TAG_SRC_PINCODE_IMPORTED not set"); + json[TAG_SRC_PINCODE_IMPORTED] = false; + } } void AuthMessageProcessor::CreateRespNegotiateMessage(nlohmann::json &json) @@ -473,12 +473,6 @@ int32_t AuthMessageProcessor::ParseAuthRequestMessage(nlohmann::json &json) } else { authResponseContext_->isShowDialog = true; } - if (IsBool(json, TAG_PINCODE_SETTED)) { - authResponseContext_->isPincodeSetted = json[TAG_PINCODE_SETTED].get(); - } else { - authResponseContext_->isPincodeSetted = false; - } - LOGI("[rm003-nopincode] isPincodeSetted %{public}d", authResponseContext_->isPincodeSetted); if (IsInt32(json, TAG_BIND_TYPE_SIZE)) { int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].get(); if (bindTypeSize > MAX_BINDTYPE_SIZE) { @@ -622,6 +616,12 @@ void AuthMessageProcessor::ParseNegotiateMessage(const nlohmann::json &json) authResponseContext_->peerBundleName = authResponseContext_->hostPkgName; } ParsePkgNegotiateMessage(json); + if (IsBool(json, TAG_SRC_PINCODE_IMPORTED)) { + authResponseContext_->isSrcPincodeImported = json[TAG_SRC_PINCODE_IMPORTED].get(); + } else { + authResponseContext_->isSrcPincodeImported = false; + } + LOGI("[rm003-nopincode] isSrcPincodeImported %{public}d", authResponseContext_->isSrcPincodeImported); } void AuthMessageProcessor::ParseRespNegotiateMessage(const nlohmann::json &json) diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 78abee454..c82b357c6 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -41,6 +41,7 @@ #include "dm_random.h" #include "multiple_user_connector.h" #include "nlohmann/json.hpp" +#include "openssl/sha.h" #include "parameter.h" #include "show_confirm.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -1213,7 +1214,7 @@ int32_t DmAuthManager::ConfirmProcessExt(const int32_t &action) authMessageProcessor_->SetResponseContext(authResponseContext_); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH && authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_CONFIRM) { - if (CanUsePinFromDp() && authResponseContext_->isPincodeSetted) { + if (CanUsePinFromDp()) { authResponseContext_->code = std::stoi(serviceInfoProfile_.GetPinCode()); LOGI("[rm003-nopincode] import from dp, pin %{public}d", authResponseContext_->code); } else if (!authResponseContext_->isShowDialog) { @@ -1453,6 +1454,7 @@ void DmAuthManager::AuthenticateFinish() isNeedProcCachedSrcReqMsg_ = false; } pincodeDialogEverShown_ = false; + serviceInfoProfile_ = {}; authType_ = AUTH_TYPE_UNKNOW; std::lock_guard autoLock(g_authFinishLock); if (authResponseContext_ == nullptr || authUiStateMgr_ == nullptr) { @@ -1522,6 +1524,11 @@ bool DmAuthManager::IsPinCodeValid(std::string strpin) if (strpin.empty()) { return false; } + for (size_t i = 0; i < strpin.length(); i++) { + if (!isdigit(strpin[i])) { + return false; + } + } int pinnum = std::stoi(strpin); return IsPinCodeValid(pinnum); } @@ -1536,8 +1543,10 @@ bool DmAuthManager::IsPinCodeValid(int32_t numpin) bool DmAuthManager::CanUsePinFromDp() { - return (serviceInfoProfile_.GetPinExchangeType() == DMServiceInfoPinExchangeType_FromDp && - DmAuthManager::IsPinCodeValid(serviceInfoProfile_.GetPinCode())); + CHECK_NULL_RETURN(authResponseContext_, false); + return (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && + authResponseContext_->isSrcPincodeImported && + serviceInfoProfile_.GetPinExchangeType() == DMServiceInfoPinExchangeType_FromDp); } void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key) @@ -1571,6 +1580,10 @@ bool DmAuthManager::IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceI LOGE("[rm003] PinExchangeType not valid, %{public}d", profile.GetPinExchangeType()); return false; } + if (!IsPinCodeValid(profile.GetPinCode())) { + LOGE("[rm003] pincode not valid"); + return false; + } return true; } @@ -1653,7 +1666,7 @@ void DmAuthManager::ShowConfigDialog() LOGE("failed to ShowConfigDialog because authResponseContext_ is nullptr"); return; } - if (serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { + if (CanUsePinFromDp() && serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { LOGI("[rm003-nopincode] no need confirm dialog"); StartAuthProcess(serviceInfoProfile_.GetAuthType()); return; @@ -1697,7 +1710,7 @@ void DmAuthManager::ShowAuthInfoDialog(bool authDeviceError) return; } LOGI("DmAuthManager::ShowAuthInfoDialog start"); - if (!authDeviceError && CanUsePinFromDp() && authResponseContext_->isPincodeSetted) { + if (!authDeviceError && CanUsePinFromDp()) { LOGI("[rm003-nopincode] pin import from dp, not show dialog"); return; } @@ -1754,8 +1767,7 @@ void DmAuthManager::ShowStartAuthDialog() } LOGI("DmAuthManager::ShowStartAuthDialog start"); int32_t pincode = 0; - if (GetAuthCode(authResponseContext_->hostPkgName, pincode) == DM_OK && - IsPinCodeValid(pincode)) { + if (GetAuthCode(authResponseContext_->hostPkgName, pincode) == DM_OK) { LOGI("[rm003] already has pin code, %{public}d", pincode); ProcessPincode(pincode); } else { @@ -2101,8 +2113,8 @@ bool DmAuthManager::IsAuthCodeReady(const std::string &pkgName) int32_t DmAuthManager::DeleteAuthCode() { //if (authResponseContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) { - importAuthCode_ = ""; - importPkgName_ = ""; + importAuthCode_ = ""; + importPkgName_ = ""; //} return DM_OK; } @@ -2124,8 +2136,7 @@ int32_t DmAuthManager::GetAuthCode(const std::string &pkgName, int32_t &pinCode) bool DmAuthManager::IsImportedAuthCodeValid() { int32_t pinCode = 0; - if (GetAuthCode(authRequestContext_->hostPkgName, pinCode) == DM_OK && - DmAuthManager::IsPinCodeValid(pinCode)) { + if (GetAuthCode(authRequestContext_->hostPkgName, pinCode) == DM_OK) { return true; } return false; @@ -2394,7 +2405,6 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) if (timer_ != nullptr) { timer_->DeleteTimer(std::string(AUTH_DEVICE_TIMEOUT_TASK)); } - if (errorCode != DM_OK || requestId != authResponseContext_->requestId) { if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; @@ -2408,14 +2418,7 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) DmAuthManager::HandleAuthenticateTimeout(name); }); } - if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && !pincodeDialogEverShown_ && - errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { - LOGI("[rm003] auth error, ShowStartAuthDialog"); - DeleteAuthCode(); - ShowStartAuthDialog(); - } else { - authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); - } + UpdateInputPincodeDialog(errorCode); } } } @@ -2432,6 +2435,8 @@ void DmAuthManager::AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessi if (ret != DM_OK) { LOGE("Save session key err, ret: %{public}d", ret); } + unsigned char hash[SHA256_DIGEST_LENGTH] = ""; + DmGenerateStrHash(sessionKey, sessionKeyLen, hash, SHA256_DIGEST_LENGTH, 0); } void DmAuthManager::GetRemoteDeviceId(std::string &deviceId) @@ -2528,18 +2533,8 @@ void DmAuthManager::ProcRespNegotiateExt(const int32_t &sessionId) std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_NEGOTIATE); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); - DistributedDeviceProfile::ServiceInfoUniqueKey key; - InitServiceInfoUniqueKey(key); - std::vector profiles; - int32_t result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); - if (result == DM_OK && profiles.size() > 0) { - if (IsServiceInfoProfileValid(profiles[0])) { - serviceInfoProfile_ = profiles[0]; - LOGI("[rm003] authBoxType %{public}d", serviceInfoProfile_.GetAuthBoxType()); - LOGI("[rm003] authType %{public}d", serviceInfoProfile_.GetAuthType()); - LOGI("[rm003] pinExchangeType %{public}d", serviceInfoProfile_.GetPinExchangeType()); - LOGI("[rm003] pinCode %{public}s", serviceInfoProfile_.GetPinCode().c_str()); - } + if (authResponseContext_->isSrcPincodeImported) { + GetServiceInfoProfile(); } } @@ -3150,11 +3145,34 @@ void DmAuthManager::ProcessReqPublicKey() } } +void DmAuthManager::GetServiceInfoProfile() +{ + DistributedDeviceProfile::ServiceInfoUniqueKey key; + InitServiceInfoUniqueKey(key); + std::vector profiles; + int32_t result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); + if (result != DM_OK) { + return; + } + for (auto &item : profiles) { + if (IsServiceInfoProfileValid(item)) { + serviceInfoProfile_ = item; + LOGI("[rm003] authBoxType %{public}d", serviceInfoProfile_.GetAuthBoxType()); + LOGI("[rm003] authType %{public}d", serviceInfoProfile_.GetAuthType()); + LOGI("[rm003] pinExchangeType %{public}d", serviceInfoProfile_.GetPinExchangeType()); + LOGI("[rm003] pinCode %{public}s", serviceInfoProfile_.GetPinCode().c_str()); + auto updateProfile = serviceInfoProfile_; + updateProfile.SetPinCode("######"); + DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(updateProfile); + break; + } + } +} + bool DmAuthManager::CheckNeedShowAuthInfoDialog() { CHECK_NULL_RETURN(authResponseContext_, false); - if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && - !pincodeDialogEverShown_ && CanUsePinFromDp()) { + if (!pincodeDialogEverShown_ && CanUsePinFromDp()) { authResponseContext_->code = GeneratePincode(); LOGI("[rm003-nopincode] AuthDeviceError, GeneratePincode, ShowAuthInfoDialog"); ShowAuthInfoDialog(true); @@ -3163,6 +3181,18 @@ bool DmAuthManager::CheckNeedShowAuthInfoDialog() return false; } +void DmAuthManager::UpdateInputPincodeDialog(int32_t errorCode) +{ + if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && !pincodeDialogEverShown_ && + IsImportedAuthCodeValid() && errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { + LOGI("[rm003] auth error, ShowStartAuthDialog"); + DeleteAuthCode(); + ShowStartAuthDialog(); + } else { + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); + } +} + void DmAuthManager::JoinLnn(const std::string &deviceId) { CHECK_NULL_VOID(authRequestContext_); @@ -3173,6 +3203,5 @@ void DmAuthManager::JoinLnn(const std::string &deviceId) } softbusConnector_->JoinLnn(authRequestContext_->addr); } - } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 67bf84c65..3e26018fa 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1496,15 +1496,10 @@ int64_t DeviceManagerService::GenerateSerivceId() LOGE("memcpy udid failed"); return 0; } - - unsigned char out[SHA256_DIGEST_LENGTH] = {0}; - SHA256_CTX ctx; - SHA256_Init(&ctx); - SHA256_Update(&ctx, data.get(), dataLength); - SHA256_Final(out, &ctx); - + unsigned char hash[SHA256_DIGEST_LENGTH] = {0}; + DmGenerateStrHash(data.get(), dataLength, hash, SHA256_DIGEST_LENGTH, 0); int64_t serviceId = 0; - if (memcpy_s(&serviceId, sizeof(int64_t), out, sizeof(int64_t)) != EOK) { + if (memcpy_s(&serviceId, sizeof(int64_t), hash, sizeof(int64_t)) != EOK) { serviceId = 0; LOGE("memcpy serviceId failed"); } diff --git a/utils/include/crypto/dm_crypto.h b/utils/include/crypto/dm_crypto.h index c30eb32c7..249185195 100644 --- a/utils/include/crypto/dm_crypto.h +++ b/utils/include/crypto/dm_crypto.h @@ -22,6 +22,8 @@ namespace OHOS { namespace DistributedHardware { +void DmGenerateStrHash(const void *data, size_t dataSize, unsigned char *outBuf, uint32_t outBufLen, + uint32_t startIndex); class Crypto { public: static std::string Sha256(const std::string &text, bool isUpper = false); -- Gitee From ebd1f7c6618b2b8e0d12b1654d15339adfc456ce Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Tue, 18 Feb 2025 09:09:43 +0800 Subject: [PATCH 5/7] patch4 Change-Id: Ib8ff377e5a5092542aac15e7b2eb406f552183e8 --- common/include/dm_constants.h | 1 - common/src/ipc/standard/ipc_model_codec.cpp | 2 +- .../include/deviceprofile_connector.h | 1 + .../src/deviceprofile_connector.cpp | 13 ++ .../src/ipc/standard/ipc_cmd_parser.cpp | 3 +- interfaces/kits/js4.0/src/dm_native_util.cpp | 3 - .../js4.0/src/native_devicemanager_js.cpp | 7 +- permission/dm_permission.json | 2 +- .../authentication/auth_message_processor.h | 1 + .../include/authentication/dm_auth_manager.h | 11 +- .../dependency/softbus/softbus_connector.h | 2 +- .../authentication/auth_message_processor.cpp | 11 +- .../src/authentication/dm_auth_manager.cpp | 151 +++++++++--------- .../dependency/softbus/softbus_connector.cpp | 10 +- .../service/include/device_manager_service.h | 25 ++- test/unittest/UTTest_device_manager_impl.cpp | 13 ++ 16 files changed, 147 insertions(+), 109 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 9f378618f..c759db158 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -264,7 +264,6 @@ constexpr const char* PARAM_CLOSE_SESSION_DELAY_SECONDS = "DM_CLOSE_SESSION_DELA constexpr const char* DM_AUTHENTICATION_TYPE = "DM_AUTHENTICATION_TYPE"; constexpr const char* PARAM_KEY_CONN_SESSIONTYPE = "connSessionType"; -constexpr const char* PARAM_KEY_HML_RELEASETIME = "hmlReleaseTime"; constexpr const char* PARAM_KEY_HML_ENABLE_160M = "hmlEnable160M"; constexpr const char* PARAM_KEY_HML_ACTIONID = "hmlActionId"; diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp index 36cf4c74b..41e5e75f4 100644 --- a/common/src/ipc/standard/ipc_model_codec.cpp +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -390,7 +390,7 @@ bool IpcModelCodec::EncodeServiceInfo(const DMServiceInfo &serviceInfo, MessageP bool IpcModelCodec::EncodeServiceInfos(const std::vector &serviceInfos, MessageParcel &parcel) { uint32_t num = static_cast(serviceInfos.size()); - if (!parcel.WriteUint32(num)){ + if (!parcel.WriteUint32(num)) { LOGE("WriteUint32 num failed"); return false; } diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index c09549d5c..b75485463 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -179,6 +179,7 @@ public: std::vector &serviceInfoProfiles); int32_t GetServiceInfoProfileListByBundleName(const DistributedDeviceProfile::ServiceInfoUniqueKey& key, std::vector& serviceInfoProfiles); + int32_t PutSessionKey(const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 4c6bb4bf1..84c8eeec2 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1804,6 +1804,19 @@ int32_t DeviceProfileConnector::GetServiceInfoProfileListByBundleName( return DM_OK; } +int32_t DeviceProfileConnector::PutSessionKey( + const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId) +{ + uint32_t userId = static_cast(MultipleUserConnector::GetCurrentAccountUserID()); + std::vector sessionKeyArray(sessionKey, sessionKey + length); + int32_t ret = DistributedDeviceProfileClient::GetInstance().PutSessionKey(userId, sessionKeyArray, sessionKeyId); + if (ret != DM_OK) { + LOGE("failed: %{public}d", ret); + return ret; + } + return DM_OK; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index 54ba52960..30dc52de3 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -2023,7 +2023,8 @@ ON_IPC_READ_RESPONSE(GET_SERVICEINFOS_CALLER, MessageParcel &reply, std::shared_ } pBaseRsp->SetErrCode(reply.ReadInt32()); if (pBaseRsp->GetErrCode() == DM_OK) { - std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + std::shared_ptr pRsp = + std::static_pointer_cast(pBaseRsp); std::vector infos; bool ret = IpcModelCodec::DecodeServiceInfos(reply, infos); if (!ret) { diff --git a/interfaces/kits/js4.0/src/dm_native_util.cpp b/interfaces/kits/js4.0/src/dm_native_util.cpp index 788ff0ced..97480abac 100644 --- a/interfaces/kits/js4.0/src/dm_native_util.cpp +++ b/interfaces/kits/js4.0/src/dm_native_util.cpp @@ -353,8 +353,6 @@ void JsToBindParam(const napi_env &env, const napi_value &object, std::string &b char connSessionType[DM_NAPI_CONN_SESSIONTYPE_LENGTH] = ""; JsObjectToString(env, object, "connSessionType", connSessionType, sizeof(connSessionType)); - int32_t hmlReleaseTime = 0; - JsObjectToInt(env, object, "hmlReleaseTime", hmlReleaseTime); int32_t hmlActionId = 0; JsObjectToInt(env, object, "hmlActionId", hmlActionId); bool hmlEnable160M = false; @@ -377,7 +375,6 @@ void JsToBindParam(const napi_env &env, const napi_value &object, std::string &b jsonObj[BIND_LEVEL] = bindLevel; jsonObj[PARAM_KEY_CONN_SESSIONTYPE] = std::string(connSessionType); - jsonObj[PARAM_KEY_HML_RELEASETIME] = hmlReleaseTime; jsonObj[PARAM_KEY_HML_ENABLE_160M] = hmlEnable160M; jsonObj[PARAM_KEY_HML_ACTIONID] = hmlActionId; bindParam = SafetyDump(jsonObj); diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index b2725e76c..07b431637 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp @@ -1685,12 +1685,17 @@ bool xProcessRegisterServiceInfo(std::string bindParam) pincode = "100000"; LOGI("[temp003] set pincode to 100000"); } + std::string bundleName = "com.huawei.msdp.hmringdiscriminator"; + if (IsString(bindParamObj, PARAM_KEY_PEER_BUNDLE_NAME)) { + bundleName = bindParamObj[PARAM_KEY_PEER_BUNDLE_NAME].get(); + LOGI("[temp003] bundleName = %{public}s", bundleName.c_str()); + } DMServiceInfo serviceInfo; serviceInfo.serviceType = "profile.serviceType"; serviceInfo.serviceName = "profile.serviceName"; serviceInfo.serviceDisplayName = "profile.serviceDisplayName"; serviceInfo.customData = "profile.customData"; - serviceInfo.bundleName = "profile.bundleName"; + serviceInfo.bundleName = bundleName; serviceInfo.moduleName = "profile.moduleName"; serviceInfo.abilityName = "profile.abilityName"; serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; diff --git a/permission/dm_permission.json b/permission/dm_permission.json index eeeab591a..5ca165167 100644 --- a/permission/dm_permission.json +++ b/permission/dm_permission.json @@ -1,5 +1,5 @@ { - "ImportAuthCode": ["com.huawei.msdp.hmringgenerator", "com.huawei.msdp.hmringdiscriminator", "CollaborationFwk", "wear_link_service", "watch_system_service"], + "ImportAuthCode": ["com.huawei.msdp.hmringgenerator", "com.huawei.msdp.hmringdiscriminator", "CollaborationFwk", "wear_link_service", "watch_system_service", "com.huawei.hmos.huaweicast"], "ExportAuthCode": ["com.huawei.msdp.hmringgenerator", "com.huawei.msdp.hmringdiscriminator", "CollaborationFwk", "wear_link_service", "watch_system_service"], "RegisterPinHolderCallback": ["CollaborationFwk"], "CreatePinHolder": ["CollaborationFwk"], diff --git a/services/implementation/include/authentication/auth_message_processor.h b/services/implementation/include/authentication/auth_message_processor.h index 03e4b9523..37a977568 100644 --- a/services/implementation/include/authentication/auth_message_processor.h +++ b/services/implementation/include/authentication/auth_message_processor.h @@ -78,6 +78,7 @@ constexpr const char* TAG_EDITION = "edition"; constexpr const char* TAG_BUNDLE_NAME = "bundleName"; constexpr const char* TAG_CRYPTIC_MSG = "encryptMsg"; constexpr const char* TAG_PEER_BUNDLE_NAME = "PEER_BUNDLE_NAME"; +constexpr const char* TAG_SESSIONKEY_ID = "sessionKeyId"; class DmAuthManager; struct DmAuthRequestContext; diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 1d16dcae0..23c19878d 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -137,7 +137,6 @@ typedef struct DmAuthRequestContext { int32_t closeSessionDelaySeconds = 0; std::string connSessionType; int32_t hmlActionId = 0; - int32_t hmlReleaseTime = 0; bool hmlEnable160M = false; } DmAuthRequestContext; @@ -197,6 +196,8 @@ typedef struct DmAuthResponseContext { std::string edition; int32_t localBindLevel; bool isSrcPincodeImported = false; + int32_t localSessionKeyId = 0; + int32_t remoteSessionKeyId = 0; } DmAuthResponseContext; class AuthMessageProcessor; @@ -484,12 +485,14 @@ public: private: bool IsHmlSessionType(); - bool CanUsePinFromDp(); + bool CanUsePincodeFromDp(); void InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); + bool IsServiceInfoAuthTypeValid(int32_t authType); + bool IsServiceInfoAuthBoxTypeValid(int32_t authBoxType); + bool IsServiceInfoPinExchangeTypeValid(int32_t pinExchangeType); bool IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceInfoProfile &profile); - int64_t GetBinderTokenId(); void GetServiceInfoProfile(); - bool CheckNeedShowAuthInfoDialog(); + bool CheckNeedShowAuthInfoDialog(int32_t errorCode); void UpdateInputPincodeDialog(int32_t errorCode); void JoinLnn(const std::string &deviceId); int32_t CheckAuthParamVaild(const std::string &pkgName, int32_t authType, const std::string &deviceId, diff --git a/services/implementation/include/dependency/softbus/softbus_connector.h b/services/implementation/include/dependency/softbus/softbus_connector.h index 30d44b6b1..1ad028299 100644 --- a/services/implementation/include/dependency/softbus/softbus_connector.h +++ b/services/implementation/include/dependency/softbus/softbus_connector.h @@ -83,7 +83,7 @@ public: * @tc.desc: join lnn by session id * @tc.type: FUNC */ - static void JoinLnnByHml(const int32_t sessionId); + static void JoinLnnByHml(const int32_t sessionId, int32_t sessionKeyId, int32_t remoteSessionKeyId); public: SoftbusConnector(); ~SoftbusConnector(); diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index 568cd947a..3361b8221 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -176,6 +176,7 @@ void AuthMessageProcessor::CreatePublicKeyMessageExt(nlohmann::json &json) return; } else { nlohmann::json jsonTemp; + jsonTemp[TAG_SESSIONKEY_ID] = authResponseContext_->localSessionKeyId; jsonTemp[TAG_PUBLICKEY] = authResponseContext_->publicKey; std::string strTemp = SafetyDump(jsonTemp); std::string encryptStr = ""; @@ -227,10 +228,10 @@ void AuthMessageProcessor::CreateNegotiateMessage(nlohmann::json &json) json[TAG_HOST_PKGLABEL] = authResponseContext_->hostPkgLabel; json[TAG_EDITION] = authResponseContext_->edition; if (authRequestContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && IsPincodeImported()) { - LOGI("[rm003-nopincode] TAG_SRC_PINCODE_IMPORTED set"); + LOGI("[rm003] TAG_SRC_PINCODE_IMPORTED set"); json[TAG_SRC_PINCODE_IMPORTED] = true; } else { - LOGI("[rm003-nopincode] TAG_SRC_PINCODE_IMPORTED not set"); + LOGI("[rm003] TAG_SRC_PINCODE_IMPORTED not set"); json[TAG_SRC_PINCODE_IMPORTED] = false; } } @@ -384,6 +385,10 @@ void AuthMessageProcessor::ParsePublicKeyMessageExt(nlohmann::json &json) if (IsString(jsonObject, TAG_PUBLICKEY)) { authResponseContext_->publicKey = jsonObject[TAG_PUBLICKEY].get(); } + if (IsInt32(jsonObject, TAG_SESSIONKEY_ID)) { + authResponseContext_->remoteSessionKeyId = jsonObject[TAG_SESSIONKEY_ID].get(); + LOGI("[rm003] remoteSessionKeyId %{public}d", authResponseContext_->remoteSessionKeyId); + } return; } } @@ -621,7 +626,7 @@ void AuthMessageProcessor::ParseNegotiateMessage(const nlohmann::json &json) } else { authResponseContext_->isSrcPincodeImported = false; } - LOGI("[rm003-nopincode] isSrcPincodeImported %{public}d", authResponseContext_->isSrcPincodeImported); + LOGI("[rm003] isSrcPincodeImported %{public}d", authResponseContext_->isSrcPincodeImported); } void AuthMessageProcessor::ParseRespNegotiateMessage(const nlohmann::json &json) diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 93c7f1f9e..b65365eca 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -317,34 +317,26 @@ void DmAuthManager::ParseHmlInfoInJsonObject(nlohmann::json jsonObject) { if (IsString(jsonObject, PARAM_KEY_CONN_SESSIONTYPE)) { authRequestContext_->connSessionType = jsonObject[PARAM_KEY_CONN_SESSIONTYPE].get(); - LOGI("[rm003-nopincode] input connSessionType %{public}s", authRequestContext_->connSessionType.c_str()); + LOGI("[rm003] connSessionType %{public}s", authRequestContext_->connSessionType.c_str()); } if (!IsHmlSessionType()) { - LOGI("[rm003-nopincode] not HML session type"); return; } - if (IsInt32(jsonObject, PARAM_KEY_HML_RELEASETIME)) { - authRequestContext_->hmlReleaseTime = jsonObject[PARAM_KEY_HML_RELEASETIME].get(); - if (authRequestContext_->hmlReleaseTime <= 0) { - authRequestContext_->hmlReleaseTime = 0; - } - LOGI("[rm003-nopincode] input hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); + if (authRequestContext_->closeSessionDelaySeconds < 0 || + authRequestContext_->closeSessionDelaySeconds > HML_SESSION_TIMEOUT) { + authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; } + LOGI("[rm003] hmlReleaseTime %{public}d", authRequestContext_->closeSessionDelaySeconds); if (IsBool(jsonObject, PARAM_KEY_HML_ENABLE_160M)) { authRequestContext_->hmlEnable160M = jsonObject[PARAM_KEY_HML_ENABLE_160M].get(); - LOGI("[rm003-nopincode] input hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); + LOGI("[rm003] hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); } if (IsInt32(jsonObject, PARAM_KEY_HML_ACTIONID)) { authRequestContext_->hmlActionId = jsonObject[PARAM_KEY_HML_ACTIONID].get(); if (authRequestContext_->hmlActionId <= 0) { authRequestContext_->hmlActionId = 0; } - LOGI("[rm003-nopincode] input hmlActionId %{public}d", authRequestContext_->hmlActionId); - } - if (authRequestContext_->hmlReleaseTime > 0) { - authRequestContext_->closeSessionDelaySeconds = authRequestContext_->hmlReleaseTime; - } else { - authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; + LOGI("[rm003] hmlActionId %{public}d", authRequestContext_->hmlActionId); } } @@ -866,10 +858,9 @@ int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId) int32_t sessionId = 0; if (IsHmlSessionType()) { CHECK_NULL_RETURN(authRequestContext_, ERR_DM_FAILED); - LOGI("[rm003] EstablishAuthChannel, try OpenAuthSessionWithPara"); - LOGI("[rm003] hmlReleaseTime %{public}d", authRequestContext_->hmlReleaseTime); - LOGI("[rm003] hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); - LOGI("[rm003] hmlActionId %{public}d", authRequestContext_->hmlActionId); + LOGI("[rm003] hmlActionId %{public}d, hmlReleaseTime %{public}d, hmlEnable160M %{public}d", + authRequestContext_->hmlActionId, authRequestContext_->closeSessionDelaySeconds, + authRequestContext_->hmlEnable160M); sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSessionWithPara(deviceId, authRequestContext_->hmlActionId, authRequestContext_->hmlEnable160M); } else { @@ -1214,9 +1205,9 @@ int32_t DmAuthManager::ConfirmProcessExt(const int32_t &action) authMessageProcessor_->SetResponseContext(authResponseContext_); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH && authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_CONFIRM) { - if (CanUsePinFromDp()) { + if (CanUsePincodeFromDp()) { authResponseContext_->code = std::stoi(serviceInfoProfile_.GetPinCode()); - LOGI("[rm003-nopincode] import from dp, pin %{public}d", authResponseContext_->code); + LOGI("[rm003] import pincode from dp, pin %{public}d", authResponseContext_->code); } else if (!authResponseContext_->isShowDialog) { GetAuthCode(authResponseContext_->hostPkgName, authResponseContext_->code); } else { @@ -1453,7 +1444,7 @@ void DmAuthManager::AuthenticateFinish() srcReqMsg_ = ""; isNeedProcCachedSrcReqMsg_ = false; } - pincodeDialogEverShown_ = false; + pincodeDialogEverShown_ = false; serviceInfoProfile_ = {}; authType_ = AUTH_TYPE_UNKNOW; std::lock_guard autoLock(g_authFinishLock); @@ -1529,7 +1520,7 @@ bool DmAuthManager::IsPinCodeValid(std::string strpin) return false; } } - int pinnum = std::stoi(strpin); + int32_t pinnum = std::stoi(strpin); return IsPinCodeValid(pinnum); } @@ -1541,7 +1532,7 @@ bool DmAuthManager::IsPinCodeValid(int32_t numpin) return true; } -bool DmAuthManager::CanUsePinFromDp() +bool DmAuthManager::CanUsePincodeFromDp() { CHECK_NULL_RETURN(authResponseContext_, false); return (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && @@ -1554,8 +1545,6 @@ void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceIn char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); key.SetDeviceId(std::string(localDeviceId)); - //int64_t tokenId = GetBinderTokenId(); - //key.SetTokenId(std::to_string(tokenId)); key.SetBundleName(authResponseContext_->hostPkgName); int32_t userId = MultipleUserConnector::GetFirstForegroundUserId(); key.SetUserId(userId); @@ -1563,22 +1552,46 @@ void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceIn localDeviceId, userId, authResponseContext_->hostPkgName.c_str()); } +bool DmAuthManager::IsServiceInfoAuthTypeValid(int32_t authType) +{ + if (authType != DMServiceInfoAuthType_TrustOneTime && + authType != DMServiceInfoAuthType_TrustAlways && + authType != DMServiceInfoAuthType_Cancel) { + return false; + } + return true; +} + +bool DmAuthManager::IsServiceInfoAuthBoxTypeValid(int32_t authBoxType) +{ + if (authBoxType != DMServiceInfoAuthBoxType_3State && + authBoxType != DMServiceInfoAuthBoxType_SkipConfirm) { + return false; + } + return true; +} + +bool DmAuthManager::IsServiceInfoPinExchangeTypeValid(int32_t pinExchangeType) +{ + if (pinExchangeType != DMServiceInfoPinExchangeType_PinBox && + pinExchangeType != DMServiceInfoPinExchangeType_FromDp && + pinExchangeType != DMServiceInfoPinExchangeType_UltraSound) { + return false; + } + return true; +} + bool DmAuthManager::IsServiceInfoProfileValid(DistributedDeviceProfile::ServiceInfoProfile &profile) { - if (profile.GetAuthType() != DMServiceInfoAuthType_TrustOneTime && - profile.GetAuthType() != DMServiceInfoAuthType_TrustAlways && - profile.GetAuthType() != DMServiceInfoAuthType_Cancel) { + if (!IsServiceInfoAuthTypeValid(profile.GetAuthType())) { LOGE("[rm003] AuthType not valid, %{public}d", profile.GetAuthType()); return false; } - if (profile.GetAuthBoxType() != DMServiceInfoAuthBoxType_3State && - profile.GetAuthBoxType() != DMServiceInfoAuthBoxType_SkipConfirm) { + if (!IsServiceInfoAuthBoxTypeValid(profile.GetAuthBoxType())) { LOGE("[rm003] AuthBoxType not valid, %{public}d", profile.GetAuthBoxType()); return false; } - if (profile.GetPinExchangeType() != DMServiceInfoPinExchangeType_PinBox && - profile.GetPinExchangeType() != DMServiceInfoPinExchangeType_FromDp && - profile.GetPinExchangeType() != DMServiceInfoPinExchangeType_UltraSound) { + if (!IsServiceInfoPinExchangeTypeValid(profile.GetPinExchangeType())) { LOGE("[rm003] PinExchangeType not valid, %{public}d", profile.GetPinExchangeType()); return false; } @@ -1668,8 +1681,8 @@ void DmAuthManager::ShowConfigDialog() LOGE("failed to ShowConfigDialog because authResponseContext_ is nullptr"); return; } - if (CanUsePinFromDp() && serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { - LOGI("[rm003-nopincode] no need confirm dialog"); + if (CanUsePincodeFromDp() && serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { + LOGI("[rm003] no need confirm dialog"); StartAuthProcess(serviceInfoProfile_.GetAuthType()); return; } @@ -1712,8 +1725,8 @@ void DmAuthManager::ShowAuthInfoDialog(bool authDeviceError) return; } LOGI("DmAuthManager::ShowAuthInfoDialog start"); - if (!authDeviceError && CanUsePinFromDp()) { - LOGI("[rm003-nopincode] pin import from dp, not show dialog"); + if (!authDeviceError && CanUsePincodeFromDp()) { + LOGI("[rm003] pin import from dp, not show dialog"); return; } if (pincodeDialogEverShown_) { @@ -2114,10 +2127,8 @@ bool DmAuthManager::IsAuthCodeReady(const std::string &pkgName) int32_t DmAuthManager::DeleteAuthCode() { - //if (authResponseContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) { importAuthCode_ = ""; importPkgName_ = ""; - //} return DM_OK; } @@ -2379,7 +2390,7 @@ void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) LOGI("AuthDeviceError start."); isAuthDevice_ = false; if (authRequestState_ == nullptr || authResponseState_ != nullptr) { - if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && CheckNeedShowAuthInfoDialog()) { + if (CheckNeedShowAuthInfoDialog(errorCode)) { return; } authTimes_++; @@ -2436,11 +2447,17 @@ void DmAuthManager::AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessi int32_t ret = authMessageProcessor_->SaveSessionKey(sessionKey, sessionKeyLen); if (ret != DM_OK) { LOGE("Save session key err, ret: %{public}d", ret); + return; } - unsigned char hash[SHA256_DIGEST_LENGTH] = ""; + authResponseContext_->localSessionKeyId = 0; + unsigned char hash[SHA256_DIGEST_LENGTH] = { 0 }; DmGenerateStrHash(sessionKey, sessionKeyLen, hash, SHA256_DIGEST_LENGTH, 0); - ret = PutSessionKey(sessionKeyIndex) - + int32_t sessionKeyId = 0; + ret = DeviceProfileConnector::GetInstance().PutSessionKey(hash, SHA256_DIGEST_LENGTH, sessionKeyId); + if (ret == DM_OK && sessionKeyId > 0) { + authResponseContext_->localSessionKeyId = sessionKeyId; + LOGI("[rm003] localSessionKeyId = %{public}d", sessionKeyId); + } } void DmAuthManager::GetRemoteDeviceId(std::string &deviceId) @@ -2974,32 +2991,6 @@ int32_t DmAuthManager::GetBinderInfo() return ret; } -int64_t DmAuthManager::GetBinderTokenId() -{ - LOGI("[rm003-nopincode] GetBinderTokenId bundleName is %{public}s", authResponseContext_->bundleName.c_str()); - CHECK_NULL_RETURN(authResponseContext_, 0); - if (authResponseContext_->bundleName.empty()) { - LOGI("[rm003-nopincode] bundleName is empty"); - return authResponseContext_->remoteTokenId; - - } - int32_t localUserId = MultipleUserConnector::GetFirstForegroundUserId(); - std::string localAccountId = MultipleUserConnector::GetOhosAccountIdByUserId(localUserId); - int32_t ret = AppManager::GetInstance(). - GetNativeTokenIdByName(authResponseContext_->bundleName, authResponseContext_->tokenId); - if (ret == DM_OK) { - LOGI("[rm003-nopincode] bundleName is sa"); - return authResponseContext_->tokenId; - } - ret = AppManager::GetInstance().GetHapTokenIdByName(localUserId, - authResponseContext_->peerBundleName, 0, authResponseContext_->tokenId); - if (ret != DM_OK) { - LOGI("[rm003-nopincode] get tokenId by bundleName failed"); - return authResponseContext_->remoteTokenId; - } - return authResponseContext_->tokenId; -} - void DmAuthManager::SetProcessInfo() { CHECK_NULL_VOID(authResponseContext_); @@ -3161,9 +3152,9 @@ void DmAuthManager::GetServiceInfoProfile() for (auto &item : profiles) { if (IsServiceInfoProfileValid(item)) { serviceInfoProfile_ = item; - LOGI("[rm003] authBoxType %{public}d", serviceInfoProfile_.GetAuthBoxType()); - LOGI("[rm003] authType %{public}d", serviceInfoProfile_.GetAuthType()); - LOGI("[rm003] pinExchangeType %{public}d", serviceInfoProfile_.GetPinExchangeType()); + LOGI("[rm003] authBoxType %{public}d, authType %{public}d, pinExchangeType %{public}d", + serviceInfoProfile_.GetAuthBoxType(), serviceInfoProfile_.GetAuthType(), + serviceInfoProfile_.GetPinExchangeType()); LOGI("[rm003] pinCode %{public}s", serviceInfoProfile_.GetPinCode().c_str()); auto updateProfile = serviceInfoProfile_; updateProfile.SetPinCode("######"); @@ -3173,12 +3164,12 @@ void DmAuthManager::GetServiceInfoProfile() } } -bool DmAuthManager::CheckNeedShowAuthInfoDialog() +bool DmAuthManager::CheckNeedShowAuthInfoDialog(int32_t errorCode) { CHECK_NULL_RETURN(authResponseContext_, false); - if (!pincodeDialogEverShown_ && CanUsePinFromDp()) { + if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && !pincodeDialogEverShown_ && CanUsePincodeFromDp()) { authResponseContext_->code = GeneratePincode(); - LOGI("[rm003-nopincode] AuthDeviceError, GeneratePincode, ShowAuthInfoDialog"); + LOGI("[rm003] AuthDeviceError, GeneratePincode, ShowAuthInfoDialog"); ShowAuthInfoDialog(true); return true; } @@ -3189,7 +3180,7 @@ void DmAuthManager::UpdateInputPincodeDialog(int32_t errorCode) { if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && !pincodeDialogEverShown_ && IsImportedAuthCodeValid() && errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { - LOGI("[rm003] auth error, ShowStartAuthDialog"); + LOGI("[rm003] AuthDeviceError, ShowStartAuthDialog"); DeleteAuthCode(); ShowStartAuthDialog(); } else { @@ -3200,12 +3191,14 @@ void DmAuthManager::UpdateInputPincodeDialog(int32_t errorCode) void DmAuthManager::JoinLnn(const std::string &deviceId) { CHECK_NULL_VOID(authRequestContext_); + CHECK_NULL_VOID(authResponseContext_); CHECK_NULL_VOID(softbusConnector_); if (IsHmlSessionType()) { - softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId); + softbusConnector_->JoinLnnByHml(authRequestContext_->sessionId, authResponseContext_->localSessionKeyId, + authResponseContext_->remoteSessionKeyId); return; } - softbusConnector_->JoinLnn(authRequestContext_->addr); + softbusConnector_->JoinLnn(deviceId); } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index a65aab3bc..7f15d33c0 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -96,12 +96,20 @@ void SoftbusConnector::JoinLnn(const std::string &deviceId) return; } -void SoftbusConnector::JoinLnnByHml(const int32_t sessionId) +void SoftbusConnector::JoinLnnByHml(const int32_t sessionId, int32_t sessionKeyId, int32_t remoteSessionKeyId) { LOGI("start, JoinLnnByHml sessionId: %{public}d.", sessionId); ConnectionAddr addrInfo; addrInfo.type = CONNECTION_ADDR_SESSION; addrInfo.info.session.sessionId = sessionId; + if (sessionKeyId > 0 && remoteSessionKeyId > 0) { + addrInfo.info.session.localDeviceKeyId = sessionKeyId; + addrInfo.info.session.remoteDeviceKeyId = remoteSessionKeyId; + LOGI("sessionKeyId valid"); + } else { + addrInfo.info.session.localDeviceKeyId = 0; + addrInfo.info.session.remoteDeviceKeyId = 0; + } int32_t ret = ::JoinLNN(DM_PKG_NAME, &addrInfo, OnSoftbusJoinLNNResult); if (ret != DM_OK) { LOGE("[SOFTBUS]JoinLNN failed, ret: %{public}d.", ret); diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 78b92684d..0c1900720 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -185,19 +185,6 @@ public: int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map &unbindParam); - int64_t GenerateSerivceId(); - bool InitServiceInfoProfile(const DMServiceInfo &serviceInfo, - DistributedDeviceProfile::ServiceInfoProfile &profile); - void InitServiceInfo(const DistributedDeviceProfile::ServiceInfoProfile &profile, DMServiceInfo &serviceInfo); - void InitServiceInfos(const std::vector &profiles, - std::vector &serviceInfos); - bool InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); - int32_t RegisterServiceInfo(DMServiceInfo &serviceInfo); - int32_t UnRegisterServiceInfo(int64_t serviceId); - int32_t UpdateServiceInfo(DMServiceInfo &serviceInfo); - int32_t GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo); - int32_t GetCallerServiceInfos(std::vector &serviceInfos); - int32_t DpAclAdd(const std::string &udid); int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType); @@ -237,6 +224,12 @@ public: std::vector &deviceProfileInfoList); int32_t GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, std::string &displayName); std::vector GetDeviceNamePrefixs(); + int64_t GenerateSerivceId(); + int32_t RegisterServiceInfo(DMServiceInfo &serviceInfo); + int32_t UnRegisterServiceInfo(int64_t serviceId); + int32_t UpdateServiceInfo(DMServiceInfo &serviceInfo); + int32_t GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo); + int32_t GetCallerServiceInfos(std::vector &serviceInfos); private: bool IsDMServiceImplReady(); @@ -280,6 +273,12 @@ private: std::vector backgroundUserIds); void ProcessCheckSumByBT(std::string networkId, std::vector foregroundUserIds, std::vector backgroundUserIds); + bool InitServiceInfoProfile(const DMServiceInfo &serviceInfo, + DistributedDeviceProfile::ServiceInfoProfile &profile); + void InitServiceInfo(const DistributedDeviceProfile::ServiceInfoProfile &profile, DMServiceInfo &serviceInfo); + void InitServiceInfos(const std::vector &profiles, + std::vector &serviceInfos); + bool InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void SubscribeAccountCommonEvent(); diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index c0e5b6039..0e3b76fe6 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -2554,6 +2554,19 @@ HWTEST_F(DeviceManagerImplTest, GetDeviceSecurityLevel_001, testing::ext::TestSi ret = DeviceManager::GetInstance().GetDeviceSecurityLevel(pkgName, netWorkId, securityLevel); ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } + +HWTEST_F(DeviceManagerImplTest, GenerateServiceId_100, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.test"; + std::shared_ptr callback = std::make_shared(); + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); + ASSERT_EQ(ret, DM_OK); + + int64_t serviceId = 0; + ret = DeviceManager::GetInstance().GenerateServiceId(serviceId); + ASSERT_EQ(ret, DM_OK); +} + } // namespace } // namespace DistributedHardware } // namespace OHOS -- Gitee From 2c138e52be51b730feb819752114d87cd7a59f7d Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Fri, 21 Feb 2025 09:58:22 +0800 Subject: [PATCH 6/7] patch5 Change-Id: I41ea60273961b936825ced070ad16aa8d4465c48 --- commondependency/src/deviceprofile_connector.cpp | 4 ++++ test/unittest/UTTest_device_manager_impl.cpp | 13 ------------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 84c8eeec2..972885b49 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -1807,6 +1807,10 @@ int32_t DeviceProfileConnector::GetServiceInfoProfileListByBundleName( int32_t DeviceProfileConnector::PutSessionKey( const uint8_t* sessionKey, uint32_t length, int32_t& sessionKeyId) { + if (sessionKey == nullptr) { + LOGE("sessionKey nullptr"); + return ERR_DM_FAILED; + } uint32_t userId = static_cast(MultipleUserConnector::GetCurrentAccountUserID()); std::vector sessionKeyArray(sessionKey, sessionKey + length); int32_t ret = DistributedDeviceProfileClient::GetInstance().PutSessionKey(userId, sessionKeyArray, sessionKeyId); diff --git a/test/unittest/UTTest_device_manager_impl.cpp b/test/unittest/UTTest_device_manager_impl.cpp index 0e3b76fe6..c0e5b6039 100644 --- a/test/unittest/UTTest_device_manager_impl.cpp +++ b/test/unittest/UTTest_device_manager_impl.cpp @@ -2554,19 +2554,6 @@ HWTEST_F(DeviceManagerImplTest, GetDeviceSecurityLevel_001, testing::ext::TestSi ret = DeviceManager::GetInstance().GetDeviceSecurityLevel(pkgName, netWorkId, securityLevel); ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID); } - -HWTEST_F(DeviceManagerImplTest, GenerateServiceId_100, testing::ext::TestSize.Level0) -{ - std::string packName = "com.ohos.test"; - std::shared_ptr callback = std::make_shared(); - int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, callback); - ASSERT_EQ(ret, DM_OK); - - int64_t serviceId = 0; - ret = DeviceManager::GetInstance().GenerateServiceId(serviceId); - ASSERT_EQ(ret, DM_OK); -} - } // namespace } // namespace DistributedHardware } // namespace OHOS -- Gitee From 637957d855fd25b0fa1ca8f0982d8735b7c39dbd Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Sat, 22 Feb 2025 09:47:13 +0800 Subject: [PATCH 7/7] patch6 Change-Id: Icb8a2d1538f74d838fe65de7973c6ddf71a933ee --- common/src/ipc/standard/ipc_model_codec.cpp | 2 +- commondependency/BUILD.gn | 1 + .../src/deviceprofile_connector.cpp | 5 +++ .../native_cpp/include/dm_device_info.h | 34 +++++++++--------- .../js4.0/src/native_devicemanager_js.cpp | 6 ++-- .../src/authentication/dm_auth_manager.cpp | 36 +++++++++---------- .../service/src/device_manager_service.cpp | 24 ++++++------- utils/include/crypto/dm_crypto.h | 4 +-- utils/src/crypto/dm_crypto.cpp | 2 +- 9 files changed, 60 insertions(+), 54 deletions(-) diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp index 41e5e75f4..a411577b3 100644 --- a/common/src/ipc/standard/ipc_model_codec.cpp +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -396,7 +396,7 @@ bool IpcModelCodec::EncodeServiceInfos(const std::vector &service } bool bRet = true; for (uint32_t k = 0; k < num; k++) { - const DMServiceInfo& serviceInfo = serviceInfos[k]; + DMServiceInfo serviceInfo = serviceInfos[k]; bRet = EncodeServiceInfo(serviceInfo, parcel); if (!bRet) { LOGE("EncodeServiceInfo failed"); diff --git a/commondependency/BUILD.gn b/commondependency/BUILD.gn index 2426a6ecf..be745450c 100644 --- a/commondependency/BUILD.gn +++ b/commondependency/BUILD.gn @@ -40,6 +40,7 @@ ohos_shared_library("devicemanagerdependency") { "${common_path}/include", "${innerkits_path}/native_cpp/include", "${utils_path}/include/crypto", + "${servicesimpl_path}/include/cryptomgr", ] sources = [ diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index a9a480fc4..b49c2cc64 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -14,6 +14,7 @@ */ #include "deviceprofile_connector.h" +#include "crypto_mgr.h" #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_crypto.h" @@ -1771,6 +1772,10 @@ int32_t DeviceProfileConnector::PutSessionKey( LOGE("sessionKey nullptr"); return ERR_DM_FAILED; } + if (length > MAX_SESSION_KEY_LENGTH) { + LOGE("SessionKey too long, len: %{public}d", length); + return ERR_DM_FAILED; + } uint32_t userId = static_cast(MultipleUserConnector::GetCurrentAccountUserID()); std::vector sessionKeyArray(sessionKey, sessionKey + length); int32_t ret = DistributedDeviceProfileClient::GetInstance().PutSessionKey(userId, sessionKeyArray, sessionKeyId); 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 595cb885d..1b3e67d6c 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -414,23 +414,23 @@ typedef struct DmNotifyKey { } } DmNotifyKey; -typedef enum : int32_t { - DMServiceInfoAuthType_TrustOneTime = 0, - DMServiceInfoAuthType_TrustAlways = 6, - DMServiceInfoAuthType_Cancel = 1 -} DMServiceInfoAuthType; - -typedef enum : int32_t { - DMServiceInfoPinExchangeType_PinBox = 1, - DMServiceInfoPinExchangeType_UltraSound = 2, - DMServiceInfoPinExchangeType_FromDp = 3 -} DMServiceInfoExchangeType; - -typedef enum : int32_t { - DMServiceInfoAuthBoxType_3State = 1, - DMServiceInfoAuthBoxType_SkipConfirm = 2, - DMServiceInfoAuthBoxType_2In1 = 3 -} DMServiceInfoAuthBoxType; +enum class DMServiceInfoAuthType : int32_t { + TRUST_ONETIME = 0, + TRUST_ALWAYS = 6, + CANCEL = 1 +}; + +enum class DMServiceInfoPinExchangeType : int32_t { + PINBOX = 1, + ULTRASOUND = 2, + FROMDP = 3 +}; + +enum class DMServiceInfoAuthBoxType : int32_t { + STATE3 = 1, + SKIP_CONFIRM = 2, + TWO_IN1 = 3 +}; typedef struct DMServiceInfo { int64_t serviceId = 0; diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index 07b431637..d4b2a5281 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp @@ -1698,9 +1698,9 @@ bool xProcessRegisterServiceInfo(std::string bindParam) serviceInfo.bundleName = bundleName; serviceInfo.moduleName = "profile.moduleName"; serviceInfo.abilityName = "profile.abilityName"; - serviceInfo.authBoxType = DMServiceInfoAuthBoxType_SkipConfirm; - serviceInfo.authType = DMServiceInfoAuthType_TrustOneTime; - serviceInfo.pinExchangeType = DMServiceInfoPinExchangeType_FromDp; + serviceInfo.authBoxType = (int32_t)DMServiceInfoAuthBoxType::SKIP_CONFIRM; + serviceInfo.authType = (int32_t)DMServiceInfoAuthType::TRUST_ONETIME; + serviceInfo.pinExchangeType = (int32_t)DMServiceInfoPinExchangeType::FROMDP; serviceInfo.pinCode = pincode; serviceInfo.description = "profile.description"; serviceInfo.serviceDiscoveryScope = "profile.serviceDiscoveryScope"; diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index e312066a0..ab57f44e5 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -322,11 +322,7 @@ void DmAuthManager::ParseHmlInfoInJsonObject(nlohmann::json jsonObject) if (!IsHmlSessionType()) { return; } - if (authRequestContext_->closeSessionDelaySeconds < 0 || - authRequestContext_->closeSessionDelaySeconds > HML_SESSION_TIMEOUT) { - authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; - } - LOGI("[rm003] hmlReleaseTime %{public}d", authRequestContext_->closeSessionDelaySeconds); + authRequestContext_->closeSessionDelaySeconds = HML_SESSION_TIMEOUT; if (IsBool(jsonObject, PARAM_KEY_HML_ENABLE_160M)) { authRequestContext_->hmlEnable160M = jsonObject[PARAM_KEY_HML_ENABLE_160M].get(); LOGI("[rm003] hmlEnable160M %{public}d", authRequestContext_->hmlEnable160M); @@ -1206,7 +1202,7 @@ int32_t DmAuthManager::ConfirmProcessExt(const int32_t &action) if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH && authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_CONFIRM) { if (CanUsePincodeFromDp()) { - authResponseContext_->code = std::stoi(serviceInfoProfile_.GetPinCode()); + authResponseContext_->code = std::atoi(serviceInfoProfile_.GetPinCode().c_str()); LOGI("[rm003] import pincode from dp, pin %{public}d", authResponseContext_->code); } else if (!authResponseContext_->isShowDialog) { GetAuthCode(authResponseContext_->hostPkgName, authResponseContext_->code); @@ -1537,7 +1533,8 @@ bool DmAuthManager::CanUsePincodeFromDp() CHECK_NULL_RETURN(authResponseContext_, false); return (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && authResponseContext_->isSrcPincodeImported && - serviceInfoProfile_.GetPinExchangeType() == DMServiceInfoPinExchangeType_FromDp); + IsPinCodeValid(serviceInfoProfile_.GetPinCode()) && + serviceInfoProfile_.GetPinExchangeType() == (int32_t)DMServiceInfoPinExchangeType::FROMDP); } void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceInfoUniqueKey &key) @@ -1554,9 +1551,9 @@ void DmAuthManager::InitServiceInfoUniqueKey(DistributedDeviceProfile::ServiceIn bool DmAuthManager::IsServiceInfoAuthTypeValid(int32_t authType) { - if (authType != DMServiceInfoAuthType_TrustOneTime && - authType != DMServiceInfoAuthType_TrustAlways && - authType != DMServiceInfoAuthType_Cancel) { + if (authType != (int32_t)DMServiceInfoAuthType::TRUST_ONETIME && + authType != (int32_t)DMServiceInfoAuthType::TRUST_ALWAYS && + authType != (int32_t)DMServiceInfoAuthType::CANCEL) { return false; } return true; @@ -1564,8 +1561,8 @@ bool DmAuthManager::IsServiceInfoAuthTypeValid(int32_t authType) bool DmAuthManager::IsServiceInfoAuthBoxTypeValid(int32_t authBoxType) { - if (authBoxType != DMServiceInfoAuthBoxType_3State && - authBoxType != DMServiceInfoAuthBoxType_SkipConfirm) { + if (authBoxType != (int32_t)DMServiceInfoAuthBoxType::STATE3 && + authBoxType != (int32_t)DMServiceInfoAuthBoxType::SKIP_CONFIRM) { return false; } return true; @@ -1573,9 +1570,9 @@ bool DmAuthManager::IsServiceInfoAuthBoxTypeValid(int32_t authBoxType) bool DmAuthManager::IsServiceInfoPinExchangeTypeValid(int32_t pinExchangeType) { - if (pinExchangeType != DMServiceInfoPinExchangeType_PinBox && - pinExchangeType != DMServiceInfoPinExchangeType_FromDp && - pinExchangeType != DMServiceInfoPinExchangeType_UltraSound) { + if (pinExchangeType != (int32_t)DMServiceInfoPinExchangeType::PINBOX && + pinExchangeType != (int32_t)DMServiceInfoPinExchangeType::FROMDP && + pinExchangeType != (int32_t)DMServiceInfoPinExchangeType::ULTRASOUND) { return false; } return true; @@ -1681,7 +1678,8 @@ void DmAuthManager::ShowConfigDialog() LOGE("failed to ShowConfigDialog because authResponseContext_ is nullptr"); return; } - if (CanUsePincodeFromDp() && serviceInfoProfile_.GetAuthBoxType() == DMServiceInfoAuthBoxType_SkipConfirm) { + if (CanUsePincodeFromDp() && + serviceInfoProfile_.GetAuthBoxType() == (int32_t)DMServiceInfoAuthBoxType::SKIP_CONFIRM) { LOGI("[rm003] no need confirm dialog"); StartAuthProcess(serviceInfoProfile_.GetAuthType()); return; @@ -2451,7 +2449,7 @@ void DmAuthManager::AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessi } authResponseContext_->localSessionKeyId = 0; unsigned char hash[SHA256_DIGEST_LENGTH] = { 0 }; - DmGenerateStrHash(sessionKey, sessionKeyLen, hash, SHA256_DIGEST_LENGTH, 0); + Crypto::DmGenerateStrHash(sessionKey, sessionKeyLen, hash, SHA256_DIGEST_LENGTH, 0); int32_t sessionKeyId = 0; ret = DeviceProfileConnector::GetInstance().PutSessionKey(hash, SHA256_DIGEST_LENGTH, sessionKeyId); if (ret == DM_OK && sessionKeyId > 0) { @@ -3169,7 +3167,8 @@ bool DmAuthManager::CheckNeedShowAuthInfoDialog(int32_t errorCode) CHECK_NULL_RETURN(authResponseContext_, false); if (errorCode == ERR_DM_HICHAIN_PROOFMISMATCH && !pincodeDialogEverShown_ && CanUsePincodeFromDp()) { authResponseContext_->code = GeneratePincode(); - LOGI("[rm003] AuthDeviceError, GeneratePincode, ShowAuthInfoDialog"); + LOGI("AuthDeviceError, GeneratePincode, ShowAuthInfoDialog"); + authTimes_ = 0; ShowAuthInfoDialog(true); return true; } @@ -3181,6 +3180,7 @@ void DmAuthManager::UpdateInputPincodeDialog(int32_t errorCode) if (authResponseContext_->authType != AUTH_TYPE_IMPORT_AUTH_CODE && !pincodeDialogEverShown_ && IsImportedAuthCodeValid() && errorCode == ERR_DM_HICHAIN_PROOFMISMATCH) { LOGI("[rm003] AuthDeviceError, ShowStartAuthDialog"); + authTimes_ = 0; DeleteAuthCode(); ShowStartAuthDialog(); } else { diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 4193120c6..e4d997138 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -1515,7 +1515,10 @@ int64_t DeviceManagerService::GenerateSerivceId() return 0; } unsigned char hash[SHA256_DIGEST_LENGTH] = {0}; - DmGenerateStrHash(data.get(), dataLength, hash, SHA256_DIGEST_LENGTH, 0); + Crypto::DmGenerateStrHash(data.get(), dataLength, hash, SHA256_DIGEST_LENGTH, 0); + if (memset_s(data.get(), dataLength, 0, dataLength) != EOK) { + LOGW("memset_s failed."); + } int64_t serviceId = 0; if (memcpy_s(&serviceId, sizeof(int64_t), hash, sizeof(int64_t)) != EOK) { serviceId = 0; @@ -1600,7 +1603,6 @@ bool DeviceManagerService::InitServiceInfoUniqueKey(DistributedDeviceProfile::Se int32_t DeviceManagerService::RegisterServiceInfo(const DMServiceInfo &serviceInfo) { - //todo permission verifier LOGI("start, serviceId %{public}" PRId64, serviceInfo.serviceId); DistributedDeviceProfile::ServiceInfoProfile profile; bool success = InitServiceInfoProfile(serviceInfo, profile); @@ -1608,8 +1610,7 @@ int32_t DeviceManagerService::RegisterServiceInfo(const DMServiceInfo &serviceIn LOGE("InitServiceInfoProfile failed"); return ERR_DM_FAILED; } - int32_t result = DeviceProfileConnector::GetInstance().PutServiceInfoProfile(profile); - return result; + return DeviceProfileConnector::GetInstance().PutServiceInfoProfile(profile); } int32_t DeviceManagerService::UnRegisterServiceInfo(int64_t serviceId) @@ -1634,8 +1635,7 @@ int32_t DeviceManagerService::UpdateServiceInfo(const DMServiceInfo &serviceInfo LOGE("InitServiceInfoProfile failed"); return ERR_DM_FAILED; } - int32_t result = DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(profile); - return result; + return DeviceProfileConnector::GetInstance().UpdateServiceInfoProfile(profile); } int32_t DeviceManagerService::GetServiceInfoById(int64_t serviceId, DMServiceInfo &serviceInfo) @@ -1649,11 +1649,11 @@ int32_t DeviceManagerService::GetServiceInfoById(int64_t serviceId, DMServiceInf } key.SetServiceId(serviceId); DistributedDeviceProfile::ServiceInfoProfile profile; - result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey(key, profile); - if (result == DM_OK) { + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileByUniqueKey(key, profile); + if (ret == DM_OK) { InitServiceInfo(profile, serviceInfo); } - return result; + return ret; } int32_t DeviceManagerService::GetCallerServiceInfos(std::vector &serviceInfos) @@ -1666,11 +1666,11 @@ int32_t DeviceManagerService::GetCallerServiceInfos(std::vector & return ERR_DM_FAILED; } std::vector profiles; - result = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); - if (result == DM_OK) { + int32_t ret = DeviceProfileConnector::GetInstance().GetServiceInfoProfileListByTokenId(key, profiles); + if (ret == DM_OK) { InitServiceInfos(profiles, serviceInfos); } - return result; + return ret; } int32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName) diff --git a/utils/include/crypto/dm_crypto.h b/utils/include/crypto/dm_crypto.h index 249185195..6f766087a 100644 --- a/utils/include/crypto/dm_crypto.h +++ b/utils/include/crypto/dm_crypto.h @@ -22,10 +22,10 @@ namespace OHOS { namespace DistributedHardware { -void DmGenerateStrHash(const void *data, size_t dataSize, unsigned char *outBuf, uint32_t outBufLen, - uint32_t startIndex); class Crypto { public: + static void DmGenerateStrHash(const void *data, size_t dataSize, unsigned char *outBuf, uint32_t outBufLen, + uint32_t startIndex); static std::string Sha256(const std::string &text, bool isUpper = false); static std::string Sha256(const void *data, size_t size, bool isUpper = false); static int32_t ConvertHexStringToBytes(unsigned char *outBuf, uint32_t outBufLen, diff --git a/utils/src/crypto/dm_crypto.cpp b/utils/src/crypto/dm_crypto.cpp index 473fd4ec2..6bb651eb7 100644 --- a/utils/src/crypto/dm_crypto.cpp +++ b/utils/src/crypto/dm_crypto.cpp @@ -50,7 +50,7 @@ uint32_t HexifyLen(uint32_t len) return len * HEX_TO_UINT8 + 1; } -void DmGenerateStrHash(const void *data, size_t dataSize, unsigned char *outBuf, uint32_t outBufLen, +void Crypto::DmGenerateStrHash(const void *data, size_t dataSize, unsigned char *outBuf, uint32_t outBufLen, uint32_t startIndex) { if (data == nullptr || outBuf == nullptr || startIndex > outBufLen) { -- Gitee