diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index f0f697595f7739e221c02caec1df47afb1cb17e0..7b8a016e757896a81c1126db1f836ffaf18dff00 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -33,12 +33,14 @@ 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; EXPORT extern const uint32_t DM_IDENTICAL_ACCOUNT; EXPORT extern const uint32_t DM_POINT_TO_POINT; +EXPORT extern const uint32_t DM_SHARE_TYPE; EXPORT extern const uint32_t DM_ACROSS_ACCOUNT; EXPORT extern const uint32_t DM_INVALIED_BINDTYPE; EXPORT extern const uint32_t DEVICE; @@ -240,6 +242,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 bb48d6c15f711683375e9077a87f44e742f8a272..e75532c4ee70d1f440247f3e46db7a56bbf9ad2e 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -28,12 +28,14 @@ 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 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 DM_IDENTICAL_ACCOUNT = 1; const uint32_t DM_POINT_TO_POINT = 256; +const uint32_t DM_SHARE_TYPE = 2; const uint32_t DM_ACROSS_ACCOUNT = 1282; const uint32_t DM_INVALIED_BINDTYPE = 2048; const uint32_t DEVICE = 1; @@ -104,6 +106,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(); @@ -117,6 +126,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; } @@ -128,6 +141,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; @@ -136,6 +157,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); @@ -211,6 +246,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_TYPE) { + return CheckAuthForm(DmAuthForm::ACROSS_ACCOUNT, profiles, discoveryInfo); + } return DmAuthForm::INVALID_TYPE; } @@ -244,6 +282,9 @@ int32_t DeviceProfileConnector::GetAuthForm(DistributedDeviceProfile::AccessCont case DM_IDENTICAL_ACCOUNT: priority = IDENTICAL_ACCOUNT_TYPE; break; + case DM_SHARE_TYPE: + priority = SHARE_TYPE; + break; case DM_POINT_TO_POINT: if (profiles.GetBindLevel() == DEVICE) { priority = DEVICE_PEER_TO_PEER_TYPE; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 423e5be0f97b24201ee09d1fd99478b97be22ffb..971debc1fbd6e21506663a33d4be929ef64cf22e 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -136,12 +136,20 @@ 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); 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 6cd5df070fa5e1050378aa840148670db89480a6..3dd319da47f45e33db170ae6004eb420d0208873 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -158,6 +158,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); private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 12ad2f7fd1c914bfadfc2d17b64aa1b3aacac9c7..3a79cbedf243c7a8eda1d64c8ea05623219e60fc 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -37,6 +37,7 @@ namespace OHOS { namespace DistributedHardware { // One year 365 * 24 * 60 * 60 constexpr int32_t MAX_ALWAYS_ALLOW_SECONDS = 31536000; +constexpr int32_t ACL_CREDID_LENGTH = 6; DeviceManagerServiceImpl::DeviceManagerServiceImpl() { @@ -180,11 +181,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; } @@ -194,8 +196,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) == DEVICE) { - LOGI("The offline device is device bind type."); + } else if (item.second == DEVICE && 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 (item.second == DEVICE && bindType != SHARE_TYPE) { devInfo.authForm = DmAuthForm::PEER_TO_PEER; processInfo.userId = item.first; softbusConnector_->SetProcessInfo(processInfo); @@ -232,6 +238,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); @@ -253,9 +265,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_TYPE && 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) @@ -1098,6 +1137,77 @@ void DeviceManagerServiceImpl::CheckDeleteCredential(const std::string &remoteUd } } +void DeviceManagerServiceImpl::HandleCredentialDeleted(const char *credId, const char *credInfo, + const std::string &localUdid, std::string &remoteUdid) +{ + LOGI("DeviceManagerServiceImpl::HandleCredentialDeleted Start, credId = %{public}s, credInfo = %{public}s.", + credId, credInfo); + 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(); + } + LOGI("DeviceManagerServiceImpl::HandleCredentialDeleted remoteUdid = %{public}s, userId = %{public}d.", + remoteUdid.c_str(), userId); + for (const auto &item : profiles) { + if (item.GetBindType() != DM_SHARE_TYPE) { + 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) +{ + LOGI("DeviceManagerServiceImpl::HandleShareUnbindBroadCast Start, credId = %{public}s, userId = %{public}d.", + credId.c_str(), userId); + std::vector profiles = + DeviceProfileConnector::GetInstance().GetAccessControlProfile(); + for (const auto &item : profiles) { + if (item.GetBindType() != DM_SHARE_TYPE) { + 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]; + } + LOGI("HandleShareUnbindBroadCast accesserCredId = %{public}s, accesseeCredId = %{public}s.", + accesserCredId.c_str(), accesseeCredId.c_str()); + 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 516b29bdae29d001874dd3f6398db0dced44974e..44177d2dcc9df42270a5c0277542b01868446b6a 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; @@ -553,6 +563,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 8b40da361de4cc8b9c948e410a4e99f8a483bb4d..5811032c5bd50765fe3e866116a85d41925abcbb 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); @@ -283,6 +284,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(); @@ -303,6 +306,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 7d2e349069b9cd300cc5c2419109c5377b114cc3..596fa69351a9ff3fb88353c7a0f165c889e0d0a5 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -259,6 +259,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; }; diff --git a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h index d0787d446100bd24d710f928f2ad4c4cd86a33fb..0409dd7f654a02f2f9fb0c61ae2138d3a9346b5d 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; @@ -71,6 +73,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); @@ -79,6 +82,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 aa87475de926b8826e682a596f4ba2100eebe5aa..1d46e0272fcae0de056411d8094f71c411637adf 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -73,6 +73,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_TYPE = 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; @@ -132,6 +133,7 @@ void DeviceManagerService::InitHichainListener() hichainListener_ = std::make_shared(); } hichainListener_->RegisterDataChangeCb(); + hichainListener_->RegisterCredentialCb(); } #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -1867,6 +1869,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"); @@ -2496,6 +2513,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()) { @@ -2537,6 +2580,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; @@ -2544,6 +2590,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 52870f4babf35fd04f1621193407c1f3b088ce13..5798ba5b3a5fcf0ede74d369c54b56aefe7939bb 100644 --- a/services/service/src/hichain/hichain_listener.cpp +++ b/services/service/src/hichain/hichain_listener.cpp @@ -32,6 +32,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()) { @@ -81,10 +85,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."); } @@ -109,6 +118,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"); @@ -140,6 +164,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 962391c13c068ba88046af53acd81126816f3722..ea368fa2bcea406dc699ca522d177bbbc9c42d62 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"; @@ -127,6 +129,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; @@ -134,6 +140,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); @@ -161,6 +180,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; @@ -168,6 +190,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; @@ -187,6 +239,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: case RelationShipChangeType::APP_UNINSTALL: // current NOT support @@ -220,7 +275,8 @@ bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type) (type == (uint32_t)RelationShipChangeType::APP_UNBIND) || (type == (uint32_t)RelationShipChangeType::SYNC_USERID) || (type == (uint32_t)RelationShipChangeType::DEL_USER) || - (type == (uint32_t)RelationShipChangeType::STOP_USER); + (type == (uint32_t)RelationShipChangeType::STOP_USER) || + (type == (uint32_t)RelationShipChangeType::SHARE_UNBIND); } void RelationShipChangeMsg::ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const diff --git a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp index 9a82df9538f3a0d4e9a1ef223ebacbcaad812817..ee730a0277115ae3a2fb6578095d8eb6d94059da 100644 --- a/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp +++ b/test/commonunittest/UTTest_dm_deviceprofile_connector.cpp @@ -1985,7 +1985,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 b8b947ac7f70ca2d01631a19a65efa5d2e1b75a1..a57d3c98433f1d554615ed69fabd46218c9db0fb 100644 --- a/test/unittest/UTTest_device_manager_service_impl.cpp +++ b/test/unittest/UTTest_device_manager_service_impl.cpp @@ -1753,6 +1753,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