diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index a7c10452a0913ed9b51b6bcbcc8ab5478554cdac..f78489019b7e28783da72238b46bdf30c4792cea 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -34,6 +34,7 @@ enum AllowAuthType { EXPORT extern const uint32_t INVALIED_TYPE; EXPORT extern const uint32_t APP_PEER_TO_PEER_TYPE; EXPORT extern const uint32_t APP_ACROSS_ACCOUNT_TYPE; +EXPORT extern const uint32_t SHARE_TYPE; EXPORT extern const uint32_t DEVICE_PEER_TO_PEER_TYPE; EXPORT extern const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE; EXPORT extern const uint32_t IDENTICAL_ACCOUNT_TYPE; @@ -308,6 +309,10 @@ private: uint32_t index, std::string targetDeviceId); bool CheckAppLevelAccess(const DistributedDeviceProfile::AccessControlProfile &profile, const DmAccessCaller &caller, const DmAccessCallee &callee); + bool CheckSinkShareType(DistributedDeviceProfile::AccessControlProfile profile, + const int32_t &userId, const std::string &deviceId, const std::string &trustDeviceId, const int32_t &bindType); + std::unordered_map GetAuthFormMap(const std::string &pkgName, const std::string &deviceId, + std::vector profilesFilter, const int32_t &userId); int32_t GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, const std::string &trustDev, const std::string &reqDev); int32_t CheckAuthForm(DmAuthForm form, DistributedDeviceProfile::AccessControlProfile profiles, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index 5fecc26d072fc710ab6dbfc5a1005772d2467c1b..7f48e8758dbedeceb68daa9b6c4f5a0792f42c58 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -30,11 +30,12 @@ using namespace OHOS::DistributedDeviceProfile; const uint32_t INVALIED_TYPE = 0; const uint32_t APP_PEER_TO_PEER_TYPE = 1; const uint32_t APP_ACROSS_ACCOUNT_TYPE = 2; -const uint32_t DEVICE_PEER_TO_PEER_TYPE = 3; -const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 4; -const uint32_t IDENTICAL_ACCOUNT_TYPE = 5; -const uint32_t SERVICE_PEER_TO_PEER_TYPE = 6; -const uint32_t SERVICE_ACROSS_ACCOUNT_TYPE = 7; +const uint32_t SHARE_TYPE = 3; +const uint32_t DEVICE_PEER_TO_PEER_TYPE = 4; +const uint32_t DEVICE_ACROSS_ACCOUNT_TYPE = 5; +const uint32_t IDENTICAL_ACCOUNT_TYPE = 6; +const uint32_t SERVICE_PEER_TO_PEER_TYPE = 7; +const uint32_t SERVICE_ACROSS_ACCOUNT_TYPE = 8; const uint32_t DM_INVALIED_TYPE = 2048; const uint32_t SERVICE = 2; @@ -692,6 +693,13 @@ EXPORT std::unordered_map DeviceProfileConnector::GetAp profilesFilter.push_back(item); } } + return GetAuthFormMap(pkgName, deviceId, profilesFilter, userId); +} + +std::unordered_map DeviceProfileConnector::GetAuthFormMap(const std::string &pkgName, + const std::string &deviceId, std::vector profilesFilter, + const int32_t &userId) +{ std::unordered_map deviceIdMap; for (auto &item : profilesFilter) { std::string trustDeviceId = item.GetTrustDeviceId(); @@ -705,6 +713,10 @@ EXPORT std::unordered_map DeviceProfileConnector::GetAp continue; } if (deviceIdMap.find(trustDeviceId) == deviceIdMap.end()) { + if (CheckSinkShareType(item, userId, deviceId, trustDeviceId, bindType)) { + LOGI("GetAuthFormMap CheckSinkShareType true."); + continue; + } deviceIdMap[trustDeviceId] = static_cast(bindType); continue; } @@ -716,6 +728,14 @@ EXPORT std::unordered_map DeviceProfileConnector::GetAp deviceIdMap[trustDeviceId] = DmAuthForm::IDENTICAL_ACCOUNT; continue; } + if (bindType == DmAuthForm::ACROSS_ACCOUNT) { + if (CheckSinkShareType(item, userId, deviceId, trustDeviceId, bindType)) { + LOGI("GetAuthFormMap CheckSinkShareType true."); + continue; + } + deviceIdMap[trustDeviceId] = DmAuthForm::ACROSS_ACCOUNT; + continue; + } if (bindType == DmAuthForm::PEER_TO_PEER && authForm == DmAuthForm::ACROSS_ACCOUNT) { deviceIdMap[trustDeviceId] = DmAuthForm::PEER_TO_PEER; continue; @@ -724,6 +744,20 @@ EXPORT std::unordered_map DeviceProfileConnector::GetAp return deviceIdMap; } +bool DeviceProfileConnector::CheckSinkShareType(DistributedDeviceProfile::AccessControlProfile profile, + const int32_t &userId, const std::string &deviceId, const std::string &trustDeviceId, const int32_t &bindType) +{ + if ((profile.GetAccessee().GetAccesseeUserId() == userId || + profile.GetAccessee().GetAccesseeUserId() == 0 || + profile.GetAccessee().GetAccesseeUserId() == -1) && + profile.GetAccessee().GetAccesseeDeviceId() == deviceId && + profile.GetAccesser().GetAccesserDeviceId() == trustDeviceId && + bindType == DmAuthForm::ACROSS_ACCOUNT) { + return true; + } + return false; +} + int32_t DeviceProfileConnector::GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, bool &isOnline, int32_t &authForm) { std::vector profiles = GetAccessControlProfileByUserId(discoveryInfo.userId); @@ -803,6 +837,9 @@ int32_t DeviceProfileConnector::HandleDmAuthForm(AccessControlProfile profiles, if (profiles.GetBindType() == DM_ACROSS_ACCOUNT) { return CheckAuthForm(DmAuthForm::ACROSS_ACCOUNT, profiles, discoveryInfo); } + if (profiles.GetBindType() == DM_SHARE) { + return CheckAuthForm(DmAuthForm::ACROSS_ACCOUNT, profiles, discoveryInfo); + } return DmAuthForm::INVALID_TYPE; } @@ -837,6 +874,9 @@ int32_t DeviceProfileConnector::GetAuthForm(DistributedDeviceProfile::AccessCont case DM_IDENTICAL_ACCOUNT: priority = IDENTICAL_ACCOUNT_TYPE; break; + case DM_SHARE: + priority = SHARE_TYPE; + break; case DM_POINT_TO_POINT: if (profiles.GetBindLevel() == USER) { priority = DEVICE_PEER_TO_PEER_TYPE; diff --git a/services/implementation/include/authentication_v2/dm_auth_context.h b/services/implementation/include/authentication_v2/dm_auth_context.h index e54240926f9a0da5ad65f3728af5f3da321b9dea..3c17556824a4e79b7787537eff1535e684e426d3 100644 --- a/services/implementation/include/authentication_v2/dm_auth_context.h +++ b/services/implementation/include/authentication_v2/dm_auth_context.h @@ -86,7 +86,7 @@ enum DmRole { enum { DM_UNKNOWN_TYPE = 0, DM_SAME_ACCOUNT_TYPE = 1, - DM_SHARE_TYPE = 2, + DM_SHARE = 2, DM_POINT_TO_POINT_TYPE = 256, DM_SAME_GROUP_TYPE = 3, }; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 6ca296fb02b5acc55caae26f15525d4cd54206d1..c146782b015ddc4c671f7fdf37717488323076e7 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -166,6 +166,9 @@ public: int32_t RegisterAuthenticationType(int32_t authenticationType); void DeleteAlwaysAllowTimeOut(); void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId); + void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, + std::string &remoteUdid); + void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, const std::string &localUdid); int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); int32_t DeleteAcl(const std::string &sessionName, const std::string &localUdid, const std::string &remoteUdid, int32_t bindLevel, const std::string &extra); @@ -181,8 +184,13 @@ public: private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); + void SetOnlineProcessInfo(uint32_t bindType, ProcessInfo &processInfo, DmDeviceInfo &devInfo, + const std::string &requestDeviceId, const std::string &trustDeviceId, DmDeviceState devState); + void HandleDeletedAclOffline(const std::string &trustDeviceId, + const std::string &requestDeviceId, DmDeviceInfo &devInfo, ProcessInfo &processInfo, DmDeviceState &devState); void HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo); void HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo); + bool CheckSharePeerSrc(const std::string &peerUdid, const std::string &localUdid); std::map GetDeviceIdAndBindLevel(int32_t userId); std::multimap GetDeviceIdAndUserId(int32_t userId, const std::string &accountId); void HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash, diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 30d1a279b009d7fc019b4a0243fd8158462298eb..628bd26097f6bc7ff89442989ff6babd67705163 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -162,6 +162,9 @@ public: int32_t RegisterAuthenticationType(int32_t authenticationType); void DeleteAlwaysAllowTimeOut(); void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId); + void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, + std::string &remoteUdid); + void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, const std::string &localUdid); int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId); void HandleServiceUnBindEvent(int32_t userId, const std::string &remoteUdid, int32_t remoteTokenId); diff --git a/services/implementation/src/authentication_v2/dm_auth_state.cpp b/services/implementation/src/authentication_v2/dm_auth_state.cpp index 9457036b88b398647646b96a3d26645334f3a2d1..08144632baeaa102a64bf9e675687c54e3a4f436 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state.cpp @@ -276,7 +276,7 @@ int32_t DmAuthState::GetAclBindType(std::shared_ptr context, std: return DM_POINT_TO_POINT_TYPE; } if (credType == DM_AUTH_CREDENTIAL_ACCOUNT_ACROSS) { - return DM_SHARE_TYPE; + return DM_SHARE; } return DM_UNKNOWN_TYPE; } diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index e946f7fc414e17e3fbb23dbe46b5c4f91ec2688b..55869e415d5e02382b5fc86c030aa2561f12a18c 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -45,6 +45,7 @@ namespace { // One year 365 * 24 * 60 * 60 constexpr int32_t MAX_ALWAYS_ALLOW_SECONDS = 31536000; +constexpr int32_t ACL_CREDID_LENGTH = 6; constexpr int32_t MIN_PIN_CODE = 100000; constexpr int32_t MAX_PIN_CODE = 999999; // New protocol field definition. To avoid dependency on the new protocol header file, @@ -586,11 +587,12 @@ void DeviceManagerServiceImpl::HandleOffline(DmDeviceState devState, DmDeviceInf char localUdid[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localUdid, DEVICE_UUID_LENGTH); std::string requestDeviceId = std::string(localUdid); - std::map userIdAndBindLevel = - DeviceProfileConnector::GetInstance().GetUserIdAndBindLevel(requestDeviceId, trustDeviceId); ProcessInfo processInfo; processInfo.pkgName = std::string(DM_PKG_NAME); processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId(); + uint32_t bindType = DeviceProfileConnector::GetInstance().CheckBindType(trustDeviceId, requestDeviceId); + std::map userIdAndBindLevel = + DeviceProfileConnector::GetInstance().GetUserIdAndBindLevel(requestDeviceId, trustDeviceId); if (userIdAndBindLevel.empty() || userIdAndBindLevel.find(processInfo.userId) == userIdAndBindLevel.end()) { userIdAndBindLevel[processInfo.userId] = INVALIED_TYPE; } @@ -600,7 +602,12 @@ void DeviceManagerServiceImpl::HandleOffline(DmDeviceState devState, DmDeviceInf devInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT; processInfo.userId = item.first; softbusConnector_->SetProcessInfo(processInfo); - } else if (static_cast(item.second) == USER) { + } else if (static_cast(item.second) == USER && bindType == SHARE_TYPE) { + LOGI("The offline device is device bind level and share bind type."); + devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT; + processInfo.userId = item.first; + softbusConnector_->SetProcessInfo(processInfo); + } else if (static_cast(item.second) == USER && bindType != SHARE_TYPE) { LOGI("The offline device is device bind type."); devInfo.authForm = DmAuthForm::PEER_TO_PEER; processInfo.userId = item.first; @@ -638,6 +645,12 @@ void DeviceManagerServiceImpl::HandleOnline(DmDeviceState devState, DmDeviceInfo ProcessInfo processInfo; processInfo.pkgName = std::string(DM_PKG_NAME); processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId(); + SetOnlineProcessInfo(bindType, processInfo, devInfo, requestDeviceId, trustDeviceId, devState); +} + +void DeviceManagerServiceImpl::SetOnlineProcessInfo(uint32_t bindType, ProcessInfo &processInfo, DmDeviceInfo &devInfo, + const std::string &requestDeviceId, const std::string &trustDeviceId, DmDeviceState devState) +{ if (bindType == IDENTICAL_ACCOUNT_TYPE) { devInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT; softbusConnector_->SetProcessInfo(processInfo); @@ -659,9 +672,36 @@ void DeviceManagerServiceImpl::HandleOnline(DmDeviceState devState, DmDeviceInfo MultipleUserConnector::GetFirstForegroundUserId()); softbusConnector_->SetProcessInfoVec(processInfoVec); devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT; + } else if (bindType == SHARE_TYPE) { + if (CheckSharePeerSrc(trustDeviceId, requestDeviceId)) { + LOGI("ProcessDeviceStateChange authForm is share, peer is src."); + return; + } + devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT; + softbusConnector_->SetProcessInfo(processInfo); } - LOGI("DeviceManagerServiceImpl::HandleOnline success devInfo auform %{public}d.", devInfo.authForm); + LOGI("DeviceManagerServiceImpl::HandleOnline success devInfo authForm is %{public}d.", devInfo.authForm); deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo); + return; +} + +bool DeviceManagerServiceImpl::CheckSharePeerSrc(const std::string &peerUdid, const std::string &localUdid) +{ + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAccessControlProfile(); + for (auto &aclItem : profiles) { + if (aclItem.GetBindType() == DM_SHARE && aclItem.GetTrustDeviceId() == peerUdid) { + if (aclItem.GetAccesser().GetAccesserDeviceId() == peerUdid && + aclItem.GetAccessee().GetAccesseeDeviceId() == localUdid) { + return true; + } + if (aclItem.GetAccesser().GetAccesserDeviceId() == localUdid && + aclItem.GetAccessee().GetAccesseeDeviceId() == peerUdid) { + return false; + } + } + } + return false; } void DeviceManagerServiceImpl::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo) @@ -2242,6 +2282,69 @@ void DeviceManagerServiceImpl::CheckDeleteCredential(const std::string &remoteUd } } +void DeviceManagerServiceImpl::HandleCredentialDeleted(const char *credId, const char *credInfo, + const std::string &localUdid, std::string &remoteUdid) +{ + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAccessControlProfile(); + JsonObject jsonObject; + jsonObject.Parse(std::string(credInfo)); + if (jsonObject.IsDiscarded()) { + LOGE("credInfo prase error."); + return; + } + std::string deviceIdTag = "deviceId"; + std::string userIdTag = "userId"; + int32_t userId = 0; + if (IsString(jsonObject, deviceIdTag)) { + remoteUdid = jsonObject[deviceIdTag].Get(); + } + if (IsInt32(jsonObject, userIdTag)) { + userId = jsonObject[userIdTag].Get(); + } + for (const auto &item : profiles) { + if (item.GetBindType() != DM_SHARE) { + continue; + } + if ((item.GetAccesser().GetAccesserCredentialId() == atoi(credId) && + item.GetAccesser().GetAccesserDeviceId() == localUdid && + item.GetAccessee().GetAccesseeUserId() == userId && + item.GetAccessee().GetAccesseeDeviceId() == remoteUdid) || + (item.GetAccessee().GetAccesseeCredentialId() == atoi(credId) && + item.GetAccessee().GetAccesseeDeviceId() == localUdid && + item.GetAccesser().GetAccesserUserId() == userId && + item.GetAccesser().GetAccesserDeviceId() == remoteUdid)) { + DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId()); + } + } +} + +void DeviceManagerServiceImpl::HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, + const std::string &localUdid) +{ + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAccessControlProfile(); + for (const auto &item : profiles) { + if (item.GetBindType() != DM_SHARE) { + continue; + } + std::string accesserCredId = ""; + std::string accesseeCredId = ""; + for (int32_t i = 0; i < ACL_CREDID_LENGTH; i++) { + accesserCredId[i] = std::to_string(item.GetAccesser().GetAccesserCredentialId())[i]; + accesseeCredId[i] = std::to_string(item.GetAccessee().GetAccesseeCredentialId())[i]; + } + if (accesserCredId == credId && item.GetAccessee().GetAccesseeDeviceId() == localUdid && + item.GetAccesser().GetAccesserUserId() == userId) { + DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId()); + } + if (accesseeCredId == credId && item.GetAccesser().GetAccesserDeviceId() == localUdid && + item.GetAccessee().GetAccesseeUserId() == userId) { + DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId()); + } + } +} + int32_t DeviceManagerServiceImpl::CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId) { diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 26f39c7aa56bb3931344c9292c2a50b427433613..57120d8694d66402a3bec09ab1f92be6204a1b74 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -330,6 +330,16 @@ int32_t DeviceManagerServiceImpl::ImportAuthCode(const std::string &pkgName, con return DM_OK; } +void DeviceManagerServiceImpl::HandleCredentialDeleted(const char *credId, const char *credInfo, + const std::string &localUdid, std::string &remoteUdid) +{ + (void)credId; + (void)credInfo; + (void)localUdid; + (void)remoteUdid; + return; +} + int32_t DeviceManagerServiceImpl::ExportAuthCode(std::string &authCode) { (void)authCode; @@ -571,6 +581,15 @@ void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector return; } +void DeviceManagerServiceImpl::HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, + const std::string &localUdid) +{ + (void)credId; + (void)userId; + (void)localUdid; + return; +} + void DeviceManagerServiceImpl::HandleRemoteUserRemoved(int32_t preUserId, const std::string &remoteUdid) { (void)preUserId; diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 43d47980d0d50505f62f35aede6576fc5a0d6be5..026a653701932a9a3340acdc61c3a0ed54e6840f 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -192,6 +192,7 @@ public: bool CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee); void HandleDeviceNotTrust(const std::string &msg); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + void HandleCredentialDeleted(const char *credId, const char *credInfo); void HandleDeviceTrustedChange(const std::string &msg); void HandleUserIdCheckSumChange(const std::string &msg); void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid); @@ -288,6 +289,8 @@ private: #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) void SubscribeAccountCommonEvent(); + void SendShareTypeUnBindBroadCast(const char *credId, const int32_t localUserId, + const std::vector &peerUdids); EXPORT void AccountCommonEventCallback(const std::string commonEventType, int32_t currentUserId, int32_t beforeUserId); void SubscribeScreenLockEvent(); @@ -308,6 +311,7 @@ private: void HandleUserSwitched(int32_t curUserId, int32_t preUserId); void HandleUserIdsBroadCast(const std::vector &remoteUserIdInfos, const std::string &remoteUdid, bool isNeedResponse); + void HandleShareUnbindBroadCast(const int32_t userId, const std::string &credId); void NotifyRemoteLocalUserSwitch(int32_t curUserId, int32_t preUserId, const std::vector &peerUdids, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds); void NotifyRemoteLocalUserSwitchByWifi(int32_t curUserId, int32_t preUserId, diff --git a/services/service/include/hichain/hichain_listener.h b/services/service/include/hichain/hichain_listener.h index 07f40880374ea6eedc08237991f7229c4900bedf..cc43346a26077da6088c62513d889081af95b9f4 100644 --- a/services/service/include/hichain/hichain_listener.h +++ b/services/service/include/hichain/hichain_listener.h @@ -58,6 +58,7 @@ public: HichainListener(); ~HichainListener(); void RegisterDataChangeCb(); + void RegisterCredentialCb(); void DeleteAllGroup(const std::string &localUdid, const std::vector &backgroundUserIds); int32_t GetRelatedGroups(int32_t userId, const std::string &deviceId, std::vector &groupList); @@ -69,9 +70,11 @@ public: int64_t GenRequestId(); static void OnHichainDeviceUnBound(const char *peerUdid, const char *groupInfo); + static void OnCredentialDeleted(const char *credId, const char *credInfo); private: const DeviceGroupManager *deviceGroupManager_ = nullptr; + const CredManager *credManager_ = nullptr; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index fa2ae7ca36fc4ecce096394dc23bc97cc6f99911..e4598a8867ce0cc81335a6eb010fafbe7708f714 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -263,6 +263,10 @@ public: virtual int32_t RegisterAuthenticationType(int32_t authenticationType) = 0; virtual void DeleteAlwaysAllowTimeOut() = 0; virtual void CheckDeleteCredential(const std::string &remoteUdid, int32_t remoteUserId) = 0; + virtual void HandleCredentialDeleted(const char *credId, const char *credInfo, const std::string &localUdid, + std::string &remoteUdid) = 0; + virtual void HandleShareUnbindBroadCast(const std::string &credId, const int32_t &userId, + const std::string &localUdid) = 0; virtual int32_t CheckDeviceInfoPermission(const std::string &localUdid, const std::string &peerDeviceId) = 0; virtual void HandleServiceUnBindEvent(int32_t userId, const std::string &remoteUdid, int32_t remoteTokenId) = 0; diff --git a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h index cc6409ce119d6d4ea1f2cdf1bcb96129cdc9ccff..ebaef0e7292805ce6087a6fe834836d2d228b471 100644 --- a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h +++ b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h @@ -30,7 +30,8 @@ enum class RelationShipChangeType : uint32_t { APP_UNINSTALL = 5, SYNC_USERID = 6, STOP_USER = 7, - TYPE_MAX = 8 + SHARE_UNBIND = 8, + TYPE_MAX = 9 }; struct UserIdInfo { @@ -58,6 +59,7 @@ struct RelationShipChangeMsg { bool syncUserIdFlag; // The foreground and background user id infos std::vector userIdInfos; + std::string credId; explicit RelationShipChangeMsg(); bool ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) const; @@ -72,6 +74,7 @@ struct RelationShipChangeMsg { bool ToSyncFrontOrBackUserIdPayLoad(uint8_t *&msg, uint32_t &len) const; void ToDelUserPayLoad(uint8_t *&msg, uint32_t &len) const; void ToStopUserPayLoad(uint8_t *&msg, uint32_t &len) const; + void ToShareUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; cJSON *ToPayLoadJson() const; bool FromAccountLogoutPayLoad(const cJSON *payloadJson); @@ -81,6 +84,7 @@ struct RelationShipChangeMsg { bool FromSyncFrontOrBackUserIdPayLoad(const cJSON *payloadJson); bool FromDelUserPayLoad(const cJSON *payloadJson); bool FromStopUserPayLoad(const cJSON *payloadJson); + bool FromShareUnbindPayLoad(const cJSON *payloadJson); std::string ToJson() const; bool FromJson(const std::string &msgJson); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 3cc7595f8fd2492f75396e3281f4b384b03c3ff2..b699f1866498347ab48a31a9d903d4b35711e1ab 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -74,6 +74,7 @@ namespace { constexpr const char *NETWORKID = "NETWORK_ID"; constexpr uint32_t INVALIED_BIND_LEVEL = 0; constexpr uint32_t DM_IDENTICAL_ACCOUNT = 1; + constexpr uint32_t DM_SHARE = 2; const std::string USERID_CHECKSUM_NETWORKID_KEY = "networkId"; const std::string USERID_CHECKSUM_DISCOVER_TYPE_KEY = "discoverType"; constexpr uint32_t USERID_CHECKSUM_DISCOVERY_TYPE_WIFI_MASK = 0b0010; @@ -133,6 +134,7 @@ void DeviceManagerService::InitHichainListener() hichainListener_ = std::make_shared(); } hichainListener_->RegisterDataChangeCb(); + hichainListener_->RegisterCredentialCb(); } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -1872,6 +1874,21 @@ void DeviceManagerService::SubscribeAccountCommonEvent() return; } +void DeviceManagerService::SendShareTypeUnBindBroadCast(const char *credId, const int32_t localUserId, + const std::vector &peerUdids) +{ + LOGI("SendShareTypeUnBindBroadCast Start."); + RelationShipChangeMsg msg; + msg.type = RelationShipChangeType::SHARE_UNBIND; + msg.userId = static_cast(localUserId); + msg.credId = credId; + msg.peerUdids = peerUdids; + std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg); + LOGI("SendShareTypeUnBindBroadCast broadCastMsg = %{public}s.", broadCastMsg.c_str()); + CHECK_NULL_VOID(softbusListener_); + softbusListener_->SendAclChangedBroadcast(broadCastMsg); +} + void DeviceManagerService::SubscribeScreenLockEvent() { LOGI("Start"); @@ -2501,6 +2518,32 @@ void DeviceManagerService::SendServiceUnBindBroadCast(const std::vectorSendAclChangedBroadcast(broadCastMsg); } +void DeviceManagerService::HandleCredentialDeleted(const char *credId, const char *credInfo) +{ + LOGI("HandleCredentialDeleted start."); + if (credId == nullptr || credInfo == nullptr) { + LOGE("HandleCredentialDeleted credId or credInfo is nullptr."); + return; + } + char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localUdidTemp); + if (!IsDMServiceImplReady()) { + LOGE("HandleCredentialDeleted failed, instance not init or init failed."); + return; + } + std::string remoteUdid = ""; + dmServiceImpl_->HandleCredentialDeleted(credId, credInfo, localUdid, remoteUdid); + if (remoteUdid.empty()) { + LOGE("HandleCredentialDeleted failed, remoteUdid is empty."); + return; + } + std::vector peerUdids; + peerUdids.emplace_back(remoteUdid); + SendShareTypeUnBindBroadCast(credId, MultipleUserConnector::GetCurrentAccountUserID(), peerUdids); + return; +} + void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg) { if (msg.empty()) { @@ -2546,6 +2589,9 @@ void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg) case RelationShipChangeType::STOP_USER: HandleUserStopBroadCast(relationShipMsg.userId, relationShipMsg.peerUdid); break; + case RelationShipChangeType::SHARE_UNBIND: + HandleShareUnbindBroadCast(relationShipMsg.userId, relationShipMsg.credId); + break; default: LOGI("Dm have not this event type."); break; @@ -2553,6 +2599,22 @@ void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg) return; } +void DeviceManagerService::HandleShareUnbindBroadCast(const int32_t userId, const std::string &credId) +{ + LOGI("HandleShareUnbindBroadCast start."); + if (credId == "") { + LOGE("HandleShareUnbindBroadCast credId is null."); + return; + } + char localUdidTemp[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH); + std::string localUdid = std::string(localUdidTemp); + if (IsDMServiceImplReady()) { + dmServiceImpl_->HandleShareUnbindBroadCast(credId, userId, localUdid); + } + return; +} + int32_t DeviceManagerService::ParseCheckSumMsg(const std::string &msg, std::string &networkId, uint32_t &discoveryType, bool &isChange) { diff --git a/services/service/src/hichain/hichain_listener.cpp b/services/service/src/hichain/hichain_listener.cpp index 8aa199c8ecca5aec051415c5cd07d00b9d5da4e2..4ee33e95fbbc66f1ad09420aa683d5650b024f57 100644 --- a/services/service/src/hichain/hichain_listener.cpp +++ b/services/service/src/hichain/hichain_listener.cpp @@ -33,6 +33,10 @@ static DataChangeListener dataChangeListener_ = { .onDeviceUnBound = HichainListener::OnHichainDeviceUnBound, }; +static CredChangeListener credChangeListener_ = { + .onCredDelete = HichainListener::OnCredentialDeleted, +}; + void FromJson(const JsonItemObject &jsonObject, GroupInformation &groupInfo) { if (jsonObject.Contains(FIELD_GROUP_TYPE) && jsonObject.At(FIELD_GROUP_TYPE).IsNumberInteger()) { @@ -82,10 +86,15 @@ HichainListener::HichainListener() LOGI("HichainListener constructor start."); InitDeviceAuthService(); deviceGroupManager_ = GetGmInstance(); + credManager_ = GetCredMgrInstance(); if (deviceGroupManager_ == nullptr) { LOGE("[HICHAIN]failed to init group manager."); return; } + if (credManager_ == nullptr) { + LOGE("[HICHAIN]failed to init cred manager."); + return; + } LOGI("HichainListener::constructor success."); } @@ -110,6 +119,21 @@ void HichainListener::RegisterDataChangeCb() LOGI("RegisterDataChangeCb success!"); } +void HichainListener::RegisterCredentialCb() +{ + LOGI("HichainListener::RegisterCredentialCb start"); + if (credManager_ == nullptr) { + LOGE("credManager_ is null!"); + return; + } + int32_t ret = credManager_->registerChangeListener(DM_PKG_NAME, &credChangeListener_); + if (ret != DM_OK) { + LOGE("[HICHAIN]registerChangeListener failed with ret: %{public}d.", ret); + return; + } + LOGI("RegisterCredentialCb success!"); +} + void HichainListener::OnHichainDeviceUnBound(const char *peerUdid, const char *groupInfo) { LOGI("HichainListener::onDeviceUnBound start"); @@ -141,6 +165,21 @@ void HichainListener::OnHichainDeviceUnBound(const char *peerUdid, const char *g } } +void HichainListener::OnCredentialDeleted(const char *credId, const char *credInfo) +{ + LOGI("HichainListener::OnCredentialDeleted start"); + if (credId == nullptr || credInfo == nullptr) { + LOGE("credId or credInfo is null!"); + return; + } + if (strlen(credId) > MAX_DATA_LEN || strlen(credInfo) > MAX_DATA_LEN) { + LOGE("credId or credInfo is invalid"); + return; + } + DeviceManagerService::GetInstance().HandleCredentialDeleted(credId, credInfo); + return; +} + void HichainListener::DeleteAllGroup(const std::string &localUdid, const std::vector &backgroundUserIds) { LOGI("OnStart HichainListener::DeleteAllGroup"); diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index 823dd2a5b6e89d551284531973025c998e99b5d3..58ba6c6322bd95256ae33a2024035cf8ff749245 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -46,6 +46,7 @@ namespace { */ const int32_t DEL_USER_PAYLOAD_LEN = 2; const int32_t STOP_USER_PAYLOAD_LEN = 2; + const int32_t SHARE_UNBIND_PAYLOAD_LEN = 8; /** * @brief the userid payload cost 2 bytes. * @@ -58,6 +59,7 @@ namespace { const int32_t USERID_BYTES = 2; const int32_t BITS_PER_BYTE = 8; const int32_t INVALIED_PAYLOAD_SIZE = 12; + const int32_t CREDID_PAYLOAD_LEN = 8; const char * const MSG_TYPE = "TYPE"; const char * const MSG_VALUE = "VALUE"; @@ -131,6 +133,10 @@ bool RelationShipChangeMsg::ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) con ToStopUserPayLoad(msg, len); ret = true; break; + case RelationShipChangeType::SHARE_UNBIND: + ToShareUnbindPayLoad(msg, len); + ret = true; + break; default: LOGE("RelationShipChange type invalid"); break; @@ -138,6 +144,19 @@ bool RelationShipChangeMsg::ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) con return ret; } +void RelationShipChangeMsg::ToShareUnbindPayLoad(uint8_t *&msg, uint32_t &len) const +{ + msg = new uint8_t[SHARE_UNBIND_PAYLOAD_LEN](); + for (int i = 0; i < USERID_PAYLOAD_LEN; i++) { + msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF; + } + + for (int i = USERID_PAYLOAD_LEN; i < CREDID_PAYLOAD_LEN; i++) { + msg[i] = credId[i - USERID_PAYLOAD_LEN]; + } + len = SHARE_UNBIND_PAYLOAD_LEN; +} + bool RelationShipChangeMsg::FromBroadcastPayLoad(const cJSON *payloadJson, RelationShipChangeType type) { LOGI("FromBroadcastPayLoad type %{public}d.", type); @@ -168,6 +187,9 @@ bool RelationShipChangeMsg::FromBroadcastPayLoad(const cJSON *payloadJson, Relat case RelationShipChangeType::STOP_USER: ret = FromStopUserPayLoad(payloadJson); break; + case RelationShipChangeType::SHARE_UNBIND: + ret = FromShareUnbindPayLoad(payloadJson); + break; default: LOGE("RelationShipChange type invalid"); break; @@ -175,6 +197,36 @@ bool RelationShipChangeMsg::FromBroadcastPayLoad(const cJSON *payloadJson, Relat return ret; } +bool RelationShipChangeMsg::FromShareUnbindPayLoad(const cJSON *payloadJson) +{ + if (payloadJson == NULL) { + LOGE("Share unbind payloadJson is null."); + return false; + } + int32_t arraySize = cJSON_GetArraySize(payloadJson); + if (arraySize < SHARE_UNBIND_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) { + LOGE("Payload invalied,the size is %{public}d.", arraySize); + return false; + } + userId = 0; + for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) { + cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i); + CHECK_NULL_RETURN(payloadItem, false); + if (cJSON_IsNumber(payloadItem)) { + userId |= (static_cast(payloadItem->valueint)) << (i * BITS_PER_BYTE); + } + } + credId = ""; + for (uint32_t j = USERID_PAYLOAD_LEN; j < CREDID_PAYLOAD_LEN; j++) { + cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); + CHECK_NULL_RETURN(payloadItem, false); + if (cJSON_IsNumber(payloadItem)) { + credId += static_cast(payloadItem->valueint); + } + } + return true; +} + bool RelationShipChangeMsg::IsValid() const { bool ret = false; @@ -194,6 +246,9 @@ bool RelationShipChangeMsg::IsValid() const case RelationShipChangeType::STOP_USER: ret = (userId != UINT32_MAX); break; + case RelationShipChangeType::SHARE_UNBIND: + ret = (userId != UINT32_MAX); + break; case RelationShipChangeType::SERVICE_UNBIND: ret = (userId != UINT32_MAX); break; @@ -231,6 +286,7 @@ bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type) (type == (uint32_t)RelationShipChangeType::SYNC_USERID) || (type == (uint32_t)RelationShipChangeType::DEL_USER) || (type == (uint32_t)RelationShipChangeType::STOP_USER) || + (type == (uint32_t)RelationShipChangeType::SHARE_UNBIND) || (type == (uint32_t)RelationShipChangeType::SERVICE_UNBIND); } diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp index 1cf70e56a91a393917d9942dc9f090b44b0348e5..0e691ddc1281b98e6a45ee856f9e35e821c99ac7 100644 --- a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp @@ -1997,7 +1997,7 @@ HWTEST_F(DeviceProfileConnectorTest, CheckBindType_004, testing::ext::TestSize.L EXPECT_CALL(*multipleUserConnectorMock_, GetFirstForegroundUserId()).WillOnce(Return(1234)); ret = DeviceProfileConnector::GetInstance().CheckBindType(peerUdid, localUdid); - EXPECT_EQ(ret, 5); + EXPECT_EQ(ret, 6); } HWTEST_F(DeviceProfileConnectorTest, GetTokenIdByNameAndDeviceId_001, testing::ext::TestSize.Level1) diff --git a/test/unittest/UTTest_device_manager_service_impl.cpp b/test/unittest/UTTest_device_manager_service_impl.cpp index f08ba99a9c1c9dea9e509424aefd84b09cd4fb92..5a4cea3c90e154c56e9c6a3f21f5d480c777032f 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -1737,6 +1737,10 @@ HWTEST_F(DeviceManagerServiceImplTest, HandleOnline_003, testing::ext::TestSize. EXPECT_CALL(*deviceProfileConnectorMock_, CheckBindType(_, _)).WillOnce(Return(DEVICE_ACROSS_ACCOUNT_TYPE)); deviceManagerServiceImpl_->HandleOnline(devState, devInfo); + EXPECT_CALL(*softbusConnectorMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); + EXPECT_CALL(*deviceProfileConnectorMock_, CheckBindType(_, _)).WillOnce(Return(SHARE_TYPE)); + deviceManagerServiceImpl_->HandleOnline(devState, devInfo); + std::vector profiles; EXPECT_CALL(*softbusConnectorMock_, GetUdidByNetworkId(_, _)).WillOnce(Return(DM_OK)); EXPECT_CALL(*deviceProfileConnectorMock_, CheckBindType(_, _)).WillOnce(Return(APP_PEER_TO_PEER_TYPE)); diff --git a/test/unittest/mock/device_auth.h b/test/unittest/mock/device_auth.h index 6e520f6989bd1c79deec4dad598e5749bedf53dd..1a37168fc5af279461ba0ebd4132cb1646ce11dc 100644 --- a/test/unittest/mock/device_auth.h +++ b/test/unittest/mock/device_auth.h @@ -138,6 +138,12 @@ using DataChangeListener = struct DataChangeListener { void (*onTrustedDeviceNumChanged)(int curTrustedDeviceNum); }; +using CredChangeListener = struct CredChangeListener { + void (*onCredAdd)(const char *credId, const char *credInfo); + void (*onCredDelete)(const char *credId, const char *credInfo); + void (*onCredUpdate)(const char *credId, const char *credInfo); +}; + using DeviceAuthCallback = struct DeviceAuthCallback { bool (*onTransmit)(int64_t requestId, const uint8_t *data, uint32_t dataLen); void (*onSessionKeyReturned)(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen); @@ -146,6 +152,22 @@ using DeviceAuthCallback = struct DeviceAuthCallback { char *(*onRequest)(int64_t requestId, int operationCode, const char *reqParams); }; +using CredManager = struct CredManager { + int32_t (*addCredential)(int32_t osAccountId, const char *requestParams, char **returnData); + int32_t (*exportCredential)(int32_t osAccountId, const char *credId, char **returnData); + int32_t (*queryCredentialByParams)(int32_t osAccountId, const char *requestParams, char **returnData); + int32_t (*queryCredInfoByCredId)(int32_t osAccountId, const char *credId, char **returnData); + int32_t (*deleteCredential)(int32_t osAccountId, const char *credId); + int32_t (*updateCredInfo)(int32_t osAccountId, const char *credId, const char *requestParams); + int32_t (*agreeCredential)(int32_t osAccountId, const char *selfCredId, const char *requestParams, + char **returnData); + int32_t (*registerChangeListener)(const char *appId, CredChangeListener *listener); + int32_t (*unregisterChangeListener)(const char *appId); + int32_t (*deleteCredByParams)(int32_t osAccountId, const char *requestParams, char **returnData); + int32_t (*batchUpdateCredentials)(int32_t osAccountId, const char *requestParams, char **returnData); + void (*destroyInfo)(char **returnData); +}; + using GroupAuthManager = struct GroupAuthManager { int32_t (*processData)(int64_t authReqId, const uint8_t *data, uint32_t dataLen, const DeviceAuthCallback *gaCallback); @@ -214,6 +236,7 @@ DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService(void); DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService(void); DEVICE_AUTH_API_PUBLIC const GroupAuthManager *GetGaInstance(void); DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance(void); +DEVICE_AUTH_API_PUBLIC const CredManager *GetCredMgrInstance(void); #ifdef __cplusplus } #endif