diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 0a2645f57234a62fd297432c44b7c54f216c1083..8b13571e87c6d18ef1311a97dcd6658a8de38080 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -56,6 +56,8 @@ DM_EXPORT extern const char* FIELD_CREDENTIAL_EXISTS; DM_EXPORT extern const char* DM_TYPE_MINE; DM_EXPORT extern const char* DM_TYPE_OH; DM_EXPORT extern const char* TAG_SESSION_HEARTBEAT; +DM_EXPORT extern const char* TAG_BUNDLE_NAME; +DM_EXPORT extern const char* TAG_TOKENID; //The following constant are provided only for HiLink. DM_EXPORT extern const char *EXT_PART; @@ -158,6 +160,7 @@ DM_EXPORT extern const char* PARAM_KEY_HML_ACTIONID; DM_EXPORT extern const char* CONN_SESSION_TYPE_HML; DM_EXPORT extern const char* CONN_SESSION_TYPE_BLE; DM_EXPORT extern const char* UN_BIND_PARAM_UDID_KEY; +DM_EXPORT extern const char* PARAM_KEY_IS_PROXY_UNBIND; // screen state enum ScreenState { @@ -182,7 +185,7 @@ extern const char* DM_VERSION_5_0_3; extern const char* DM_VERSION_5_0_4; extern const char* DM_VERSION_5_0_5; extern const char* DM_VERSION_5_1_0; -extern const char* DM_VERSION_5_1_1; +extern const char* DM_CURRENT_VERSION; extern const char* DM_ACL_AGING_VERSION; extern const char* DM_VERSION_5_0_OLD_MAX; // Estimated highest version number of the old version } // namespace DistributedHardware diff --git a/common/src/dm_constants.cpp b/common/src/dm_constants.cpp index 9be2b9fb7155033e0478433413a1cc37f8615d6c..630f8a0308ba424c1c515513a904b0d8a45afbd5 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -47,6 +47,8 @@ const char* FIELD_CREDENTIAL_EXISTS = "isCredentialExists"; const char* DM_TYPE_MINE = "MINE"; const char* DM_TYPE_OH = "OH"; const char* TAG_SESSION_HEARTBEAT = "session_heartbeat"; +const char* TAG_BUNDLE_NAME = "bundleName"; +const char* TAG_TOKENID = "tokenId"; //The following constant are provided only for HiLink. const char *EXT_PART = "ext_part"; @@ -148,6 +150,7 @@ const char* PARAM_KEY_HML_ACTIONID = "hmlActionId"; const char* CONN_SESSION_TYPE_HML = "HML"; const char* CONN_SESSION_TYPE_BLE = "BLE"; const char* UN_BIND_PARAM_UDID_KEY = "udidKey"; +const char* PARAM_KEY_IS_PROXY_UNBIND = "isProxyUnBind"; // errCode map const std::map MAP_ERROR_CODE = { @@ -170,7 +173,7 @@ const char* DM_VERSION_5_0_3 = "5.0.3"; const char* DM_VERSION_5_0_4 = "5.0.4"; const char* DM_VERSION_5_0_5 = "5.0.5"; const char* DM_VERSION_5_1_0 = "5.1.0"; -const char* DM_VERSION_5_1_1 = "5.1.1"; +const char* DM_CURRENT_VERSION = DM_VERSION_5_1_0; const char* DM_ACL_AGING_VERSION = DM_VERSION_5_1_0; const char* DM_VERSION_5_0_OLD_MAX = "5.0.99"; // Estimated highest version number of the old version } // namespace DistributedHardware diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 1aa8990e17a5031f78fa60426d4f400ebc7c6ec6..cf60484c09e911f0c7fc8c9742bbc235c726010e 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -186,6 +186,10 @@ public: DM_EXPORT std::vector GetProcessInfoFromAclByUserId(const std::string &localDeviceId, const std::string &targetDeviceId, int32_t userId); + DM_EXPORT DistributedDeviceProfile::AccessControlProfile GetAccessControlProfileByaccessControlId( + int64_t accessControlId) + DM_EXPORT std::vector> GetAgentToProxyVecFromAclByUserId( + const std::string &localDeviceId, const std::string &targetDeviceId, int32_t userId); DM_EXPORT bool CheckSrcDevIdInAclForDevBind(const std::string &pkgName, const std::string &deviceId); DM_EXPORT bool CheckSinkDevIdInAclForDevBind(const std::string &pkgName, @@ -330,6 +334,7 @@ private: const std::vector &profilesFilter, const int32_t &userId); int32_t GetAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, const std::string &trustDev, const std::string &reqDev); + bool CheckAuthFormProxyTokenId(const std::string extraStr); int32_t CheckAuthForm(DmAuthForm form, DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); bool SingleUserProcess(const DistributedDeviceProfile::AccessControlProfile &profile, const DmAccessCaller &caller, @@ -384,7 +389,6 @@ private: std::string GetDeviceAuthVersionInfo(std::string localUdid, std::string remoteUdid, std::vector profiles); - void ParseExtra(const std::string &extra, uint64_t &peerTokenId, std::string &peerBundleName); bool CacheLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, const std::string &localUdid, DmAclIdParam &dmAclIdParam); void CheckLastLnnAcl(const std::string &localDeviceId, int32_t userId, const std::string &remoteDeviceId, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index aebb454592cb4e7a111c4f40760fd0e374aa785a..9047403e0c60c95a43ff1ed09df4ba9453e01abf 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -24,6 +24,9 @@ #include "multiple_user_connector.h" #include "distributed_device_profile_client.h" #include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "dm_jsonstr_handle.h" +#include "app_manager.h" using namespace OHOS::DistributedDeviceProfile; @@ -37,8 +40,6 @@ 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 int32_t USLEEP_TIME_US_120000 = 120000; // 120ms -constexpr uint32_t RETRY_TIMES = 3; const uint32_t DM_INVALIED_TYPE = 2048; const uint32_t SERVICE = 2; @@ -427,21 +428,6 @@ void DeviceProfileConnector::DeleteCacheAcl(std::vector delAclIdVec, } } -void DeviceProfileConnector::ParseExtra(const std::string &extra, uint64_t &peerTokenId, std::string &peerBundleName) -{ - JsonObject extraInfoJson(extra); - if (extraInfoJson.IsDiscarded()) { - LOGE("ParseExtra extraInfoJson error"); - return; - } - if (!extraInfoJson[TAG_PEER_BUNDLE_NAME].IsString() || !extraInfoJson[TAG_PEER_TOKENID].IsNumberInteger()) { - LOGE("ParseExtra TAG_PEER_BUNDLE_NAME or TAG_PEER_TOKENID error"); - return; - } - peerTokenId = extraInfoJson[TAG_PEER_TOKENID].Get(); - peerBundleName = extraInfoJson[TAG_PEER_BUNDLE_NAME].Get(); -} - bool DeviceProfileConnector::FindTargetAcl(const DistributedDeviceProfile::AccessControlProfile &acl, const std::string &localUdid, const uint32_t localTokenId, const std::string &remoteUdid, const uint32_t peerTokenId, @@ -575,7 +561,7 @@ void DeviceProfileConnector::FilterNeedDeleteACLInfos( { uint64_t peerTokenId = 0; std::string peerBundleName = ""; - ParseExtra(extra, peerTokenId, peerBundleName); + JsonStrHandle::GetInstance().GetPeerAppInfoParseExtra(extra, peerTokenId, peerBundleName); for (auto &item : profiles) { if (item.GetTrustDeviceId() != remoteUdid) { continue; @@ -798,6 +784,18 @@ int32_t DeviceProfileConnector::GetDeviceAclParam(DmDiscoveryInfo discoveryInfo, return DM_OK; } +bool DeviceProfileConnector::CheckAuthFormProxyTokenId(const std::string extraStr) +{ + std::vector proxyTokenIdVec = JsonStrHandle::GetInstance().GetProxyTokenIdByExtra(extraStr); + int64_t callingTokenId = static_cast(IPCSkeleton::GetCallingTokenID()); + for (auto &proxyTokenId : proxyTokenIdVec) { + if (callingTokenId == proxyTokenId) { + return true; + } + } + return false; +} + int32_t DeviceProfileConnector::CheckAuthForm(DmAuthForm form, AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo) { @@ -809,11 +807,13 @@ int32_t DeviceProfileConnector::CheckAuthForm(DmAuthForm form, AccessControlProf return form; } if (profiles.GetBindLevel() == APP || profiles.GetBindLevel() == SERVICE) { - if (discoveryInfo.pkgname == profiles.GetAccesser().GetAccesserBundleName() && + if ((discoveryInfo.pkgname == profiles.GetAccesser().GetAccesserBundleName() || + CheckAuthFormProxyTokenId(profiles.GetAccesser().GetAccesserExtraData())) && discoveryInfo.localDeviceId == profiles.GetAccesser().GetAccesserDeviceId()) { return form; } - if (discoveryInfo.pkgname == profiles.GetAccessee().GetAccesseeBundleName() && + if ((discoveryInfo.pkgname == profiles.GetAccessee().GetAccesseeBundleName() || + CheckAuthFormProxyTokenId(profiles.GetAccessee().GetAccesseeExtraData())) && discoveryInfo.localDeviceId == profiles.GetAccessee().GetAccesseeDeviceId()) { return form; } @@ -927,7 +927,7 @@ DM_EXPORT uint64_t DeviceProfileConnector::GetTokenIdByNameAndDeviceId(std::stri { uint64_t peerTokenId = 0; std::string pkgName = ""; - ParseExtra(extra, peerTokenId, pkgName); + JsonStrHandle::GetInstance().GetPeerAppInfoParseExtra(extra, peerTokenId, pkgName); std::vector profiles = GetAccessControlProfile(); for (auto &item : profiles) { if (item.GetAccesser().GetAccesserBundleName() == pkgName && @@ -1088,22 +1088,79 @@ std::vector DeviceProfileConnector::GetP std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId(); OHOS::DistributedHardware::ProcessInfo processInfo; + std::string extraStr; if (accesserUdid == localDeviceId) { processInfo.pkgName = item.GetAccesser().GetAccesserBundleName(); processInfo.userId = item.GetAccesser().GetAccesserUserId(); processInfoVec.push_back(processInfo); - continue; - } - if (accesseeUdid == localDeviceId) { + extraStr = item.GetAccesser().GetAccesserExtraData(); + } else if (accesseeUdid == localDeviceId) { processInfo.pkgName = item.GetAccessee().GetAccesseeBundleName(); processInfo.userId = item.GetAccessee().GetAccesseeUserId(); processInfoVec.push_back(processInfo); + extraStr = item.GetAccessee().GetAccesseeExtraData(); + } else { continue; } + std::vector proxyTokenIdVec = JsonStrHandle::GetInstance().GetProxyTokenIdByExtra(extraStr); + for (auto &proxyTokenId : proxyTokenIdVec) { + std::string proxyBundleName; + if (AppManager::GetInstance().GetBundleNameByTokenId(proxyTokenId, proxyBundleName) != DM_OK) { + continue; + } + processInfo.pkgName = proxyBundleName; + processInfoVec.push_back(processInfo); + } } return processInfoVec; } +DM_EXPORT AccessControlProfile DeviceProfileConnector::GetAccessControlProfileByaccessControlId( + int64_t accessControlId) +{ + AccessControlProfile profile; + profile.SetAccessControlId(0); + std::vector profiles = GetAccessControlProfile(); + for (auto &item : profiles) { + if(item.GetAccessControlId() == accessControlId) { + return item; + } + } + return profile; +} + +DM_EXPORT std::vector> DeviceProfileConnector::GetAgentToProxyVecFromAclByUserId( + const std::string &localDeviceId, const std::string &targetDeviceId, int32_t userId) +{ + std::vector filterProfiles = GetAclProfileByUserId(localDeviceId, + userId, targetDeviceId); + LOGI("filterProfiles size is %{public}zu", filterProfiles.size()); + std::vector> agentToProxyVec; + for (auto &item : filterProfiles) { + if (IsLnnAcl(item) || item.GetTrustDeviceId() != targetDeviceId) { + continue; + } + std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); + std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId(); + int64_t agentTokenId; + std::string extraStr; + if (accesserUdid == localDeviceId) { + agentTokenId = item.GetAccesser().GetAccesserTokenId(); + extraStr = item.GetAccesser().GetAccesserExtraData(); + } else if (accesseeUdid == localDeviceId) { + agentTokenId = item.GetAccessee().GetAccesseeTokenId(); + extraStr = item.GetAccessee().GetAccesseeExtraData(); + } else { + continue; + } + std::vector proxyTokenIdVec = JsonStrHandle::GetInstance().GetProxyTokenIdByExtra(extraStr); + for (auto &proxyTokenId : proxyTokenIdVec) { + agentToProxyVec.push_back(std::pair(agentTokenId, proxyTokenId)); + } + } + return agentToProxyVec; +} + int32_t DeviceProfileConnector::PutAccessControlList(DmAclInfo aclInfo, DmAccesser dmAccesser, DmAccessee dmAccessee) { LOGI("Start."); @@ -1351,7 +1408,7 @@ void DeviceProfileConnector::DeleteAppBindLevel(DmOfflineParam &offlineParam, co int32_t deleteNums = 0; uint64_t peerTokenId = 0; std::string peerBundleName; - ParseExtra(extra, peerTokenId, peerBundleName); + JsonStrHandle::GetInstance().GetPeerAppInfoParseExtra(extra, peerTokenId, peerBundleName); for (auto &item : profiles) { if (item.GetTrustDeviceId() != remoteUdid || item.GetBindType() == DM_IDENTICAL_ACCOUNT || item.GetBindLevel() != APP) { @@ -2198,34 +2255,18 @@ DmOfflineParam DeviceProfileConnector::HandleServiceUnBindEvent(int32_t remoteUs DM_EXPORT std::vector DeviceProfileConnector::GetAllAccessControlProfile() { std::vector profiles; - int32_t ret; - uint32_t retryTimes = 0; - do { - ret = DistributedDeviceProfileClient::GetInstance().GetAllAccessControlProfile(profiles); - if (ret != DM_OK) { - ++retryTimes; - LOGE("DP failed."); - usleep(USLEEP_TIME_US_120000); - } - } while (ret != DM_OK && retryTimes < RETRY_TIMES); - + if (DistributedDeviceProfileClient::GetInstance().GetAllAccessControlProfile(profiles) != DM_OK) { + LOGE("DP failed."); + } return profiles; } DM_EXPORT std::vector DeviceProfileConnector::GetAllAclIncludeLnnAcl() { std::vector profiles; - int32_t ret; - uint32_t retryTimes = 0; - do { - ret = DistributedDeviceProfileClient::GetInstance().GetAllAclIncludeLnnAcl(profiles); - if (ret != DM_OK) { - ++retryTimes; - LOGE("DP failed."); - usleep(USLEEP_TIME_US_120000); - } - } while (ret != DM_OK && retryTimes < RETRY_TIMES); - + if (DistributedDeviceProfileClient::GetInstance().GetAllAclIncludeLnnAcl(profiles) != DM_OK) { + LOGE("DP failed."); + } return profiles; } diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h index b93d073e433fb89e043b45a94d39dd81b9cbc3ab..cb4bb85bc260c8e3638704898fe98dfe3c505d4a 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext.h @@ -106,6 +106,7 @@ public: virtual void OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) = 0; virtual void OnSessionClosed(const int32_t sessionId) = 0; virtual void OnBytesReceived(const int32_t sessionId, const std::string message) = 0; + virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; }; using CreateDMServiceImplExtFuncPtr = IDMServiceImplExt *(*)(void); diff --git a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h index 0b957825216fc0e4fea898150b6d52d067605322..b1685ebeca2c6b5c34d65fec3faae73050fe0fde 100644 --- a/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h +++ b/interfaces/inner_kits/native_cpp/include/i_dm_service_impl_ext_resident.h @@ -110,6 +110,7 @@ public: std::vector &dmDeviceProfileInfos) = 0; virtual int32_t RestoreLocalDeviceName() = 0; virtual void ClearCacheWhenLogout(int32_t userId, const std::string &oldAccountId) = 0; + virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; virtual void HandleScreenLockEvent(bool isLock) = 0; }; diff --git a/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h b/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h index 5fc5b0a45a0896d79721182e33e476085fce95d5..e85db72922df6703f92e12774e5625729423385e 100644 --- a/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h +++ b/interfaces/inner_kits/native_cpp/include/idevice_manager_service_listener.h @@ -178,6 +178,7 @@ public: * @return std::string the name without privacy info */ virtual std::string GetLocalDisplayDeviceNameForPrivacy() = 0; + virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 509cb730bc8386fb3cb33fe971c6527a78053cfa..a4d1ec014cb452c6d7cf7d6052962b40d8c7a123 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -593,7 +593,6 @@ private: bool IsSinkMsgValid(); bool IsSourceMsgValid(); void ProcessReqPublicKey(); - int32_t GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId); void RegisterCleanNotifyCallback(CleanNotifyCallback cleanNotifyCallback); void GetBindCallerInfo(); @@ -626,6 +625,7 @@ private: std::string dmVersion_ = ""; bool isAuthDevice_ = false; bool isAuthenticateDevice_ = false; + bool isNeedJoinLnn_ = true; int32_t authForm_ = DmAuthForm::ACROSS_ACCOUNT; std::string remoteVersion_ = ""; std::atomic authType_ = AUTH_TYPE_UNKNOW; diff --git a/services/implementation/include/authentication_v2/auth_manager.h b/services/implementation/include/authentication_v2/auth_manager.h index 2767c79b47b40e0c818e8cf156e7244e29793f68..b40e117d5372249717c2261659bd33937814f08e 100644 --- a/services/implementation/include/authentication_v2/auth_manager.h +++ b/services/implementation/include/authentication_v2/auth_manager.h @@ -110,7 +110,6 @@ public: void SetAuthContext(std::shared_ptr context); std::shared_ptr GetAuthContext(); static bool IsHmlSessionType(const std::string &sessionType); - int32_t GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId); void GetBindTargetParams(std::string &pkgName, PeerTargetId &targetId, std::map &bindParam); void GetAuthCodeAndPkgName(std::string &pkgName, std::string &authCode); diff --git a/services/implementation/include/authentication_v2/dm_auth_context.h b/services/implementation/include/authentication_v2/dm_auth_context.h index faf47baa267731adacb7dbbfd59fdeaf9bfebff6..c007f4ae5cd81da2bd556b707612e9c9411f0d7c 100644 --- a/services/implementation/include/authentication_v2/dm_auth_context.h +++ b/services/implementation/include/authentication_v2/dm_auth_context.h @@ -219,6 +219,7 @@ struct DmAuthContext { DmAccess accesser; DmAccess accessee; std::multimap proxy; // Multimap where the key is the accessor and the value is the accesssee + bool isNeedJoinLnn{true}; std::shared_ptr authStateMachine; std::shared_ptr authUiStateMgr; diff --git a/services/implementation/include/dependency/softbus/softbus_session.h b/services/implementation/include/dependency/softbus/softbus_session.h index b3dfc2840c51d70cb62f9858279d96ad30b329d8..aaa548f361113f91bb2f8223f50c04a06673634d 100644 --- a/services/implementation/include/dependency/softbus/softbus_session.h +++ b/services/implementation/include/dependency/softbus/softbus_session.h @@ -59,13 +59,6 @@ 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 isEnable160m); - /** * @tc.name: SoftbusSession::CloseAuthSession * @tc.desc: Close AuthSession of the Softbus Session diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 7e22d16a890f33f2410849e04ada8542e191a20a..c7516b1a8e3060518a0a27b0972f138cc629b11c 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -259,6 +259,7 @@ private: int32_t DeleteAclForProcV2(const std::string &localUdid, uint32_t localTokenId, const std::string &remoteUdid, int32_t bindLevel, const std::string &extra, int32_t userId); int32_t DeleteSkCredAndAcl(const std::vector &acls); + void DeleteCredential(DmAclIdParam &acl); void DeleteAclByTokenId(const int32_t accessTokenId, std::vector &profiles, std::map &delProfileMap, diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 4bef5ffdc0e753cefb79e083f35561c1f6b27782..996f57e8a438a5da419cf49fa68ea9d620e7c694 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -64,6 +64,7 @@ const int32_t ALREADY_BIND = 1; const int32_t STRTOLL_BASE_10 = 10; const int32_t MAX_PUT_SESSIONKEY_TIMEOUT = 100; //ms const int32_t SESSION_CLOSE_TIMEOUT = 2; +const char* IS_NEED_JOIN_LNN = "IsNeedJoinLnn"; // clone task timeout map const std::map TASK_TIME_OUT_MAP = { @@ -867,10 +868,11 @@ int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId) int32_t sessionId = 0; if (IsHmlSessionType()) { CHECK_NULL_RETURN(authRequestContext_, ERR_DM_FAILED); + CHECK_NULL_RETURN(listener_, ERR_DM_FAILED); LOGI("hmlActionId %{public}d, hmlReleaseTime %{public}d, hmlEnable160M %{public}d", authRequestContext_->hmlActionId, authRequestContext_->closeSessionDelaySeconds, authRequestContext_->hmlEnable160M); - sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSessionWithPara(deviceId, + sessionId = listener_->OpenAuthSessionWithPara(deviceId, authRequestContext_->hmlActionId, authRequestContext_->hmlEnable160M); } else { sessionId = softbusConnector_->GetSoftbusSession()->OpenAuthSession(deviceId); @@ -1495,6 +1497,7 @@ void DmAuthManager::AuthenticateFinish() isAddingMember_ = false; isAuthenticateDevice_ = false; isAuthDevice_ = false; + isNeedJoinLnn_ = true; if (DeviceProfileConnector::GetInstance().GetTrustNumber(remoteDeviceId_) >= 1 && CompareVersion(remoteVersion_, std::string(DM_VERSION_4_1_5_1)) && softbusConnector_->CheckIsOnline(remoteDeviceId_) && authResponseContext_->isFinish) { @@ -2040,6 +2043,10 @@ int32_t DmAuthManager::BindTarget(const std::string &pkgName, const PeerTargetId if (!DmRadarHelper::GetInstance().ReportDiscoverUserRes(info)) { LOGE("ReportDiscoverUserRes failed"); } + if (bindParam.find(IS_NEED_JOIN_LNN) != bindParam.end()) { + std::string isNeedJoinLnnStr = bindParam.at(IS_NEED_JOIN_LNN); + isNeedJoinLnn_ = std::atoi(isNeedJoinLnnStr.c_str()); + } if (pkgName.empty()) { LOGE("DmAuthManager::BindTarget failed, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; @@ -3256,20 +3263,10 @@ void DmAuthManager::JoinLnn(const std::string &deviceId, bool isForceJoin) authResponseContext_->remoteSessionKeyId); return; } - softbusConnector_->JoinLnn(deviceId, isForceJoin); -} - -int32_t DmAuthManager::GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId) -{ - int32_t ret = AppManager::GetInstance().GetNativeTokenIdByName(bundleName, tokenId); - if (ret == DM_OK) { - return DM_OK; - } - ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, 0, tokenId); - if (ret != DM_OK) { - LOGE("get tokenId by bundleName failed %{public}s", GetAnonyString(bundleName).c_str()); + if (isNeedJoinLnn_) { + LOGI("isNeedJoinLnn %{public}d", isNeedJoinLnn_); + softbusConnector_->JoinLnn(deviceId, isForceJoin); } - return ret; } void DmAuthManager::OnSoftbusJoinLNNResult(const int32_t sessionId, const char *networkId, int32_t result) diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index cf5a594b28f2402dcf4b1f6264e2e2cd5fc7b0b9..1a1097f55733b0c1676f67264b7607a2016b7978 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -24,8 +24,6 @@ #include "multiple_user_connector.h" #include "auth_manager.h" -#include "dm_auth_cert.h" -#include "dm_auth_attest_common.h" #include "dm_constants.h" #include "dm_crypto.h" #include "dm_random.h" @@ -47,6 +45,7 @@ constexpr int32_t MIN_PIN_CODE = 100000; constexpr int32_t MAX_PIN_CODE = 999999; constexpr int32_t DM_ULTRASONIC_FORWARD = 0; constexpr int32_t DM_ULTRASONIC_REVERSE = 1; +const char* IS_NEED_JOIN_LNN = "IsNeedJoinLnn"; int32_t GetCloseSessionDelaySeconds(std::string &delaySecondsStr) { @@ -132,8 +131,8 @@ AuthManager::AuthManager(std::shared_ptr softbusConnector, context_->authenticationMap[AUTH_TYPE_PIN_ULTRASONIC] = nullptr; context_->authenticationMap[AUTH_TYPE_NFC] = nullptr; context_->authenticationMap[AUTH_TYPE_CRE] = nullptr; - context_->accesser.dmVersion = DM_VERSION_5_1_1; - context_->accessee.dmVersion = DM_VERSION_5_1_1; + context_->accesser.dmVersion = DM_CURRENT_VERSION; + context_->accessee.dmVersion = DM_CURRENT_VERSION; context_->timer = std::make_shared(); context_->authMessageProcessor = std::make_shared(); } @@ -480,19 +479,6 @@ void AuthManager::ParseUltrasonicSide(const JsonObject &jsonObject) } } -int32_t AuthManager::GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId) -{ - int32_t ret = AppManager::GetInstance().GetNativeTokenIdByName(bundleName, tokenId); - if (ret == DM_OK) { - return DM_OK; - } - ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, 0, tokenId); - if (ret != DM_OK) { - LOGE("get tokenId by bundleName failed %{public}s", GetAnonyString(bundleName).c_str()); - } - return ret; -} - bool CheckBindLevel(const JsonItemObject &jsonObj, const std::string &key, int32_t &bindLevel) { if (IsJsonValIntegerString(jsonObj, TAG_BIND_LEVEL)) { @@ -621,29 +607,6 @@ int32_t AuthManager::AuthenticateDevice(const std::string &pkgName, int32_t auth return DM_OK; } -std::string GenerateCertificate(std::shared_ptr context_) -{ -#ifdef DEVICE_MANAGER_COMMON_FLAG - if (context_ == nullptr) { - LOGE("context_ is nullptr!"); - return ""; - } - context_->accesser.isCommonFlag = true; - LOGI("Blue device do not generate cert!"); - return ""; -#else - DmCertChain dmCertChain; - int32_t certRet = AuthCert::GetInstance().GenerateCertificate(dmCertChain); - if (certRet != DM_OK) { - LOGE("generate cert fail, certRet = %{public}d", certRet); - return ""; - } - std::string cert = AuthAttestCommon::GetInstance().SerializeDmCertChain(&dmCertChain); - AuthAttestCommon::GetInstance().FreeDmCertChain(dmCertChain); - return cert; -#endif -} - int32_t AuthManager::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map &bindParam, int sessionId, uint64_t logicalSessionId) { @@ -661,6 +624,10 @@ int32_t AuthManager::BindTarget(const std::string &pkgName, const PeerTargetId & if (!DmRadarHelper::GetInstance().ReportDiscoverUserRes(info)) { LOGE("ReportDiscoverUserRes failed"); } + if (bindParam.find(IS_NEED_JOIN_LNN) != bindParam.end()) { + std::string isNeedJoinLnnStr = bindParam.at(IS_NEED_JOIN_LNN); + context_->isNeedJoinLnn = std::atoi(isNeedJoinLnnStr.c_str()); + } if (pkgName.empty()) { LOGE("AuthManager::BindTarget failed, pkgName is empty."); return ERR_DM_INPUT_PARA_INVALID; @@ -677,15 +644,12 @@ int32_t AuthManager::BindTarget(const std::string &pkgName, const PeerTargetId & } if (!targetId.deviceId.empty()) { ret = AuthenticateDevice(pkgName, authType, targetId.deviceId, ParseExtraFromMap(bindParam)); - if (ret != DM_OK) { - return ret; - } + if (ret != DM_OK) { return ret; } } else { LOGE("AuthManager::BindTarget failed, targetId is error."); return ERR_DM_INPUT_PARA_INVALID; } - context_->accesser.cert = GenerateCertificate(context_); context_->sessionId = sessionId; context_->logicalSessionId = logicalSessionId; context_->requestId = static_cast(logicalSessionId); diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_acl.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_acl.cpp index 87b5b33fa662955aa5d8e569f6adda4dcf65af8e..950e09b05326b0e591cf19a63e24f51363b0570c 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_acl.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_acl.cpp @@ -77,7 +77,7 @@ int32_t AuthSrcDataSyncState::Action(std::shared_ptr context) } bool isNeedJoinLnn = context->softbusConnector->CheckIsNeedJoinLnn(peerDeviceId, context->accessee.addr); // Trigger networking - if (!context->accesser.isOnline || isNeedJoinLnn) { + if ((!context->accesser.isOnline || isNeedJoinLnn) && context->isNeedJoinLnn) { if (context->connSessionType == CONN_SESSION_TYPE_HML) { context->softbusConnector->JoinLnnByHml(context->sessionId, context->accesser.transmitSessionKeyId, context->accessee.transmitSessionKeyId); @@ -121,6 +121,7 @@ int32_t AuthSinkFinishState::Action(std::shared_ptr context) ret = FreezeProcess::GetInstance().UpdateFreezeRecord(); LOGI("UpdateFreezeData ret: %{public}d", ret); } + context->isNeedJoinLnn = true; SinkFinish(context); LOGI("AuthSinkFinishState::Action ok"); if (context->cleanNotifyCallback != nullptr) { @@ -144,6 +145,7 @@ int32_t AuthSrcFinishState::Action(std::shared_ptr context) } else { context->state = static_cast(GetStateType()); } + context->isNeedJoinLnn = true; SourceFinish(context); LOGI("AuthSrcFinishState::Action ok"); std::shared_ptr tempContext = context; diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp index ae747211ef6bbc7c37ca14f5967b6a146f20f1dc..bf1474fd68d1bcd2adeb7203f8031abd5ba479ba 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp @@ -181,47 +181,6 @@ int32_t AuthSinkNegotiateStateMachine::ProcRespNegotiate5_1_0(std::shared_ptr context) -{ -#ifdef DEVICE_MANAGER_COMMON_FLAG - (void)context; - LOGI("Blue device do not verify cert!"); - return DM_OK; -#else - // Compatible with 5.1.0 and earlier - if (!CompareVersion(context->accesser.dmVersion, DM_VERSION_5_1_0)) { - LOGI("cert verify is not supported"); - return DM_OK; - } - // Compatible common device - if (CompareVersion(context->accesser.dmVersion, DM_VERSION_5_1_0) - && context->accesser.isCommonFlag == true) { - LOGI("src is common device."); - if (DeviceProfileConnector::GetInstance() - .CheckIsSameAccountByUdidHash(context->accesser.deviceIdHash) == DM_OK) { - LOGE("src is common device, but the udidHash is identical in acl!"); - return ERR_DM_VERIFY_CERT_FAILED; - } - return DM_OK; - } - DmCertChain dmCertChain{nullptr, 0}; - if (!AuthAttestCommon::GetInstance() - .DeserializeDmCertChain(context->accesser.cert, &dmCertChain)) { - LOGE("cert deserialize fail!"); - return ERR_DM_DESERIAL_CERT_FAILED; - } - int32_t certRet = AuthCert::GetInstance() - .VerifyCertificate(dmCertChain, context->accesser.deviceIdHash.c_str()); - // free dmCertChain memory - AuthAttestCommon::GetInstance().FreeDmCertChain(dmCertChain); - if (certRet != DM_OK) { - LOGE("validate cert fail, certRet = %{public}d", certRet); - return ERR_DM_VERIFY_CERT_FAILED; - } - return DM_OK; -#endif -} - int32_t AuthSinkNegotiateStateMachine::Action(std::shared_ptr context) { LOGI("AuthSinkNegotiateStateMachine::Action sessionid %{public}d", context->sessionId); @@ -246,14 +205,7 @@ int32_t AuthSinkNegotiateStateMachine::Action(std::shared_ptr con context->reason = ERR_DM_VERSION_INCOMPATIBLE; return ERR_DM_VERSION_INCOMPATIBLE; } - // verify cert - int32_t ret = VerifyCertificate(context); - if (ret != DM_OK) { - LOGE("AuthSinkNegotiateStateMachine::Action cert verify fail!"); - context->reason = ret; - return ret; - } - ret = ProcRespNegotiate5_1_0(context); + int32_t ret = ProcRespNegotiate5_1_0(context); if (ret != DM_OK) { LOGE("AuthSinkNegotiateStateMachine::Action proc response negotiate failed"); context->reason = ret; diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index 043302bf8374c79955eafed7cfcc7b5bd48afc6d..2dbaeb495b4b81d5fa625a148452703e5f8fd79d 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -646,8 +646,6 @@ int32_t DmAuthMessageProcessor::CreateNegotiateMessage(std::shared_ptraccesser.tokenIdHash; jsonObject[TAG_BUNDLE_NAME_V2] = context->accesser.bundleName; jsonObject[TAG_EXTRA_INFO] = context->accesser.extraInfo; - jsonObject[TAG_IS_COMMON_FLAG] = context->accesser.isCommonFlag; - jsonObject[TAG_DM_CERT_CHAIN] = context->accesser.cert; jsonObject[TAG_PEER_BUNDLE_NAME_V2] = context->accessee.bundleName; jsonObject[TAG_ULTRASONIC_SIDE] = static_cast(context->ultrasonicInfo); @@ -1016,7 +1014,6 @@ int32_t DmAuthMessageProcessor::ParseNegotiateMessage( context->pkgLabel = jsonObject[TAG_HOST_PKGLABEL].Get(); } ParseUltrasonicSide(jsonObject, context); - ParseCert(jsonObject, context); context->authStateMachine->TransitionTo(std::make_shared()); return DM_OK; } diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index 056eb40442acffaca75ff0bc67d5ff22945fa10f..1563f8b861969b505a82fda35348ba07a38436ca 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -23,9 +23,6 @@ #include "json_object.h" #include "softbus_connector.h" #include "softbus_error_code.h" -#ifndef DEVICE_MANAGER_COMMON_FLAG -#include "session_ex.h" -#endif namespace OHOS { namespace DistributedHardware { @@ -75,29 +72,6 @@ int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId) return sessionId; } -int32_t SoftbusSession::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) -{ -#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 = static_cast(actionId); - para.enable160M = isEnable160m; - 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/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 1e504dae001fdba0ada6441fdc01e8d9a44bc4d0..a638be9eac793c731b4ae2686042ebc8e588ab4f 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -65,6 +65,8 @@ constexpr const char* DM_TAG_LOGICAL_SESSION_ID = "logicalSessionId"; constexpr const char* DM_TAG_PEER_DISPLAY_ID = "peerDisplayId"; constexpr const char* DM_TAG_ACCESSEE_USER_ID = "accesseeUserId"; constexpr const char* DM_TAG_EXTRA_INFO = "extraInfo"; +constexpr const char* FILED_CRED_ID = "credId"; +constexpr const char* FILED_AUTHORIZED_APP_LIST = "authorizedAppList"; constexpr const char* CHANGE_PINTYPE = "1"; constexpr const char* BIND_CALLER_USERID = "bindCallerUserId"; // currently, we just support one bind session in one device at same time @@ -694,9 +696,10 @@ void DeviceManagerServiceImpl::HandleOffline(DmDeviceState devState, DmDeviceInf softbusConnector_->SetProcessInfo(processInfo); } else if (static_cast(item.second) == SERVICE || static_cast(item.second) == APP) { LOGI("The offline device is PEER_TO_PEER_TYPE bind type, %{public}" PRIu32, item.second); - std::vector processInfoVec = - DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId, - item.first); + auto processInfoVec = DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId( + requestDeviceId, trustDeviceId, item.first); + std::set processInfoSet(processInfoVec.begin(), processInfoVec.end()); + processInfoVec.assign(processInfoSet.begin(), processInfoSet.end()); softbusConnector_->SetProcessInfoVec(processInfoVec); } deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo); @@ -743,12 +746,16 @@ void DeviceManagerServiceImpl::SetOnlineProcessInfo(const uint32_t &bindType, Pr std::vector processInfoVec = DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId, MultipleUserConnector::GetFirstForegroundUserId()); + std::set processInfoSet(processInfoVec.begin(), processInfoVec.end()); + processInfoVec.assign(processInfoSet.begin(), processInfoSet.end()); softbusConnector_->SetProcessInfoVec(processInfoVec); devInfo.authForm = DmAuthForm::PEER_TO_PEER; } else if (bindType == APP_ACROSS_ACCOUNT_TYPE || bindType == SERVICE_ACROSS_ACCOUNT_TYPE) { std::vector processInfoVec = DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId, MultipleUserConnector::GetFirstForegroundUserId()); + std::set processInfoSet(processInfoVec.begin(), processInfoVec.end()); + processInfoVec.assign(processInfoSet.begin(), processInfoSet.end()); softbusConnector_->SetProcessInfoVec(processInfoVec); devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT; } else if (bindType == SHARE_TYPE) { @@ -1147,7 +1154,7 @@ void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, return; } if (msgType == MSG_TYPE_REQ_ACL_NEGOTIATE || msgType == MSG_TYPE_RESP_ACL_NEGOTIATE) { - curSession->version_ = DM_VERSION_5_1_1; + curSession->version_ = DM_CURRENT_VERSION; } } else { /** @@ -1523,8 +1530,8 @@ int DeviceManagerServiceImpl::OpenAuthSession(const std::string& deviceId, return ret; } LOGI("hmlActionId %{public}d, hmlEnable160M %{public}d", hmlActionId, hmlEnable160M); - return softbusConnector_->GetSoftbusSession()->OpenAuthSessionWithPara(deviceId, - hmlActionId, hmlEnable160M); + CHECK_NULL_RETURN(listener_, ERR_DM_FAILED); + return listener_->OpenAuthSessionWithPara(deviceId, hmlActionId, hmlEnable160M); } else { return softbusConnector_->GetSoftbusSession()->OpenAuthSession(deviceId); } @@ -2744,6 +2751,48 @@ int32_t DeviceManagerServiceImpl::DeleteAcl(const std::string &pkgName, const st return ERR_DM_FAILED; } +void DeviceManagerServiceImpl::DeleteCredential(DmAclIdParam &acl) +{ + CHECK_NULL_VOID(hiChainAuthConnector_); + JsonObject credJson; + int32_t ret = hiChainAuthConnector_->QueryCredInfoByCredId(acl.userId, acl.credId, credJson); + if (ret != DM_OK || !credJson.Contains(FILED_CRED_ID) || !credJson[FILED_CRED_ID].IsString()) { + LOGE("DeleteCredential err, ret:%{public}d", ret); + return; + } + if (!credJson.Contains(FILED_AUTHORIZED_APP_LIST)) { + ret = hiChainAuthConnector_->DeleteCredential(userId, credJson[FILED_CRED_ID].Get()); + if (ret != DM_OK) { + LOGE("DeletecredId err, ret:%{public}d", ret); + } + return; + } + DistributedDeviceProfile::AccessControlProfile profile = + DeviceProfileConnector::GetInstance().GetAccessControlProfileByaccessControlId(acl.accessControlId); + if (profile.GetAccessControlId() != acl.accessControlId) { + LOGE("DeleteCredential, no found profile"); + return; + } + std::vector appList; + credJson[FILED_AUTHORIZED_APP_LIST].Get(appList); + auto erIt = std::find(appList.begin(), appList.end(), std::to_string(profile.GetAccesser().GetAccesserTokenId())); + if (erIt != appList.end()) { + appList.erase(erIt); + } + auto eeIt = std::find(appList.begin(), appList.end(), std::to_string(profile.GetAccessee().GetAccesseeTokenId())); + if (eeIt != appList.end()) { + appList.erase(eeIt); + } + if (appList.size() == 0) { + ret = hiChainAuthConnector_->DeleteCredential(acl.userId, credJson[FILED_CRED_ID].Get()); + if (ret != DM_OK) { + LOGE("DeletecredId err, ret:%{public}d", ret); + } + return; + } + hiChainAuthConnector_->UpdateCredential(credJson[FILED_CRED_ID].Get(), acl.userId, appList); +} + int32_t DeviceManagerServiceImpl::DeleteSkCredAndAcl(const std::vector &acls) { LOGI("start."); @@ -2753,16 +2802,13 @@ int32_t DeviceManagerServiceImpl::DeleteSkCredAndAcl(const std::vectorDeleteCredential(item.userId, item.credId); - if (ret != DM_OK) { - LOGE("DeletecredId err, userId:%{public}d, credId:%{public}s, ret:%{public}d", item.userId, - item.credId.c_str(), ret); + LOGE("DeleteSessionKey err, ret:%{public}d", ret); } + DeleteCredential(item); DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.accessControlId); } return ret; @@ -2798,9 +2844,10 @@ int32_t DeviceManagerServiceImpl::DeleteAclV2(const std::string &pkgName, const { LOGI("pkgName %{public}s, localUdid %{public}s, remoteUdid %{public}s, bindLevel %{public}d.", pkgName.c_str(), GetAnonyString(localUdid).c_str(), GetAnonyString(remoteUdid).c_str(), bindLevel); - uint32_t tokenId = 0; - MultipleUserConnector::GetTokenId(tokenId); + int64_t tokenId = 0; int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::string bundleName = pkgName; + AppManager::GetInstance().GetTokenIdByBundleName(userId, bundleName, tokenId); bool isNewVersion = IsAuthNewVersion(bindLevel, localUdid, remoteUdid, tokenId, userId); if (!isNewVersion) { return DeleteAcl(pkgName, localUdid, remoteUdid, bindLevel, extra); diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index da3023d9a6460877d2eb3494b63c97d17115f651..012a88a2d7ad93121f4c47fd367575fc9a6fecef 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -96,6 +96,9 @@ public: int32_t BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &bindParam); + int32_t UnBindDeviceParseExtra(const std::string &pkgName, const std::string &udidHash, + const std::string &extra); + int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash); int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash, const std::string &extra); @@ -205,6 +208,8 @@ public: void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid); void HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid, const std::vector &acceptEventUdids); + std::set> GetProxyInfosByParseExtra(const std::string &pkgName, + const std::string &extra, std::vector> &agentToProxyVec); #endif int32_t SetDnPolicy(const std::string &pkgName, std::map &policy); void ClearDiscoveryCache(const ProcessInfo &processInfo); @@ -253,6 +258,7 @@ public: int32_t GetDeviceNetworkIdList(const std::string &pkgName, const NetworkIdQueryFilter &queryFilter, std::vector &networkIds); void ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid, int32_t userId); + int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m); int32_t UnRegisterPinHolderCallback(const std::string &pkgName); void ProcessReceiveRspAppUninstall(const std::string &remoteUdid); void ProcessReceiveRspAppUnbind(const std::string &remoteUdid); diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index f4780273b910de94de0c6e8287ac6ccac128ce1f..fbf03d22ab075989afc2b3f3cefeca6d724a6801 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -91,6 +91,7 @@ public: const std::string &deviceName, int32_t code) override; std::string GetLocalDisplayDeviceNameForPrivacy() override; + int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) override; private: void ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 2bcd88c8f183ad3f2e4e0c70e53f9f34fe237441..535f5c6b77f9b089295b4e472326fad9ae672476 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -33,6 +33,7 @@ #include "dm_comm_tool.h" #include "dm_random.h" #include "dm_transport_msg.h" +#include "dm_jsonstr_handle.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "kv_adapter_manager.h" @@ -836,6 +837,108 @@ int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std return DM_OK; } +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +std::set> DeviceManagerService::GetProxyInfosByParseExtra( + const std::string &pkgName, const std::string &extra, + std::vector> &agentToProxyVec) +{ + std::set> proxyInfos; + JsonObject jsonObject(extra); + if (jsonObject.IsDiscarded()) { + proxyInfos.insert(std::pair(pkgName, extra)); + return proxyInfos; + } + if (IsString(jsonObject, PARAM_KEY_IS_PROXY_UNBIND) && + sonObject[PARAM_KEY_IS_PROXY_UNBIND].Get() == DM_VAL_TRUE) { + if (!IsString(jsonObject, PARAM_KEY_SUBJECT_PROXYED_SUBJECTS)) { + return proxyInfos; + } + } else { + if (!jsonObject.Contains(PARAM_KEY_SUBJECT_PROXYED_SUBJECTS)) { + proxyInfos.insert(std::pair(pkgName, extra)); + } + return proxyInfos; + } + if (!AppManager::GetInstance().IsSystemSA()) { + LOGE("no proxy permission"); + return proxyInfos; + } + std::string subjectProxyAppsStr = jsonObject[PARAM_KEY_SUBJECT_PROXYED_SUBJECTS].Get(); + JsonObject allProxyObj; + if (!allProxyObj.Parse(subjectProxyAppsStr) || allProxyObj.Items().size() != 1) { + return proxyInfos; + } + int64_t proxyTokenId = static_cast(IPCSkeleton::GetCallingTokenID()); + for (const auto &object : allProxyObj.Items()) { + if (!object.Contains(TAG_BUNDLE_NAME) || !IsString(object, TAG_BUNDLE_NAME)) { + continue; + } + if (!object.Contains(TAG_TOKENID) || !IsInt64(object, TAG_TOKENID)) { + continue; + } + std::string bundleName = object[TAG_BUNDLE_NAME].Get(); + int64_t agentTokenId = object[TAG_TOKENID].Get(); + for (uint32_t i = 0; i < agentToProxyVec.size(); i++) { + if (agentTokenId == agentToProxyVec[i].first && proxyTokenId == agentToProxyVec[i].second) { + proxyInfos.insert(std::pair(bundleName, object.Dump())); + break; + } + } + } + return proxyInfos; +} +#endif + +int32_t DeviceManagerService::UnBindDeviceParseExtra(const std::string &pkgName, const std::string &udidHash, + const std::string &extra) +{ + int32_t result = ValidateUnBindDeviceParams(pkgName, udidHash, extra); + if (result != DM_OK) { + return result; + } + std::string realDeviceId = udidHash; +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + std::string udidHashTemp = ""; + if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) { + realDeviceId = udidHashTemp; + } + CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL); + std::string udid = ""; + if (softbusListener_->GetUdidFromDp(realDeviceId, udid) != DM_OK) { + LOGE("Get udid by udidhash failed."); + return ERR_DM_FAILED; + } + char localUdid[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localUdid, DEVICE_UUID_LENGTH); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + std::vector> agentToProxyVec = + DeviceProfileConnector::GetInstance().GetAgentToProxyVecFromAclByUserId( + std::string(localUdid), udid, userId); + std::set> proxyInfoSet = GetProxyInfosByParseExtra( + pkgName, extra, agentToProxyVec); + if (proxyInfoSet.size() != 1) { + LOGE("UnBind size error."); + return ERR_DM_FAILED; + } + auto proxyInfo = proxyInfoSet.begin(); + uint64_t peerTokenId = 0; + std::string peerBundleName = ""; + JsonStrHandle::GetInstance().GetPeerAppInfoParseExtra(proxyInfo->second, peerTokenId, peerBundleName); + if (peerBundleName == "") { + result = UnBindDevice(proxyInfo->first, udidHash); + } else { + result = UnBindDevice(proxyInfo->first, udidHash, proxyInfo->second); + } +#else + if (extra == "") { + result = UnBindDevice(pkgName, udidHash); + } else { + result = UnBindDevice(pkgName, udidHash, extra); + } +#endif + return result; +} + int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash, const std::string &extra) { @@ -4169,6 +4272,15 @@ void DeviceManagerService::ProcessSyncAccountLogout(const std::string &accountId dmServiceImpl_->HandleAccountLogoutEvent(userId, accountId, peerUdid); } +int32_t DeviceManagerService::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) +{ + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + return dmServiceImplExtResident_->OpenAuthSessionWithPara(deviceId, actionId, isEnable160m); +} + int32_t DeviceManagerService::UnRegisterPinHolderCallback(const std::string &pkgName) { if (!PermissionManager::GetInstance().CheckPermission()) { diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index 2fa0fe73b2c1ee95a64ca5aff5cbc1f68b47402c..d1d0a84b8377d0256468ec5d844b975814030128 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -45,6 +45,7 @@ #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "datetime_ex.h" #include "device_name_manager.h" +#include "device_manager_service.h" #include "kv_adapter_manager.h" #include "multiple_user_connector.h" #endif @@ -1018,5 +1019,15 @@ std::string DeviceManagerServiceListener::GetLocalDisplayDeviceNameForPrivacy() return ""; #endif } + +int32_t DeviceManagerServiceListener::OpenAuthSessionWithPara(const std::string &deviceId, + int32_t actionId, bool isEnable160m) +{ +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + return DeviceManagerService::GetInstance().OpenAuthSessionWithPara(deviceId, actionId, isEnable160m); +#else + return DM_OK; +#endif +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index 58517d1567c15030abaa37ca975a1aa5333b1c8b..9a70e70779b6cec36ce7779fdd7b5a8e3a145876 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -873,11 +873,7 @@ ON_IPC_CMD(UNBIND_DEVICE, MessageParcel &data, MessageParcel &reply) std::string deviceId = data.ReadString(); std::string extra = data.ReadString(); int32_t result = 0; - if (extra == "") { - result = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId); - } else { - result = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId, extra); - } + result = DeviceManagerService::GetInstance().UnBindDeviceParseExtra(pkgName, deviceId, extra); if (!reply.WriteInt32(result)) { LOGE("write result failed"); return ERR_DM_IPC_WRITE_FAILED; diff --git a/test/commonunittest/UTTest_dm_auth_manager_first.cpp b/test/commonunittest/UTTest_dm_auth_manager_first.cpp index 887ebd6e8b4832399c8836e8b01fa47798aa06cb..e820998354face4bd70916343a14975c8115c57a 100644 --- a/test/commonunittest/UTTest_dm_auth_manager_first.cpp +++ b/test/commonunittest/UTTest_dm_auth_manager_first.cpp @@ -1490,34 +1490,6 @@ HWTEST_F(DmAuthManagerTest, StopAuthenticateDevice_001, testing::ext::TestSize.L ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthManagerTest, GetBindLevel_001, testing::ext::TestSize.Level1) -{ - int32_t bindLevel = INVALIED_TYPE; - std::string udid; - authManager_->HandleDeviceNotTrust(udid); - udid = "988989"; - authManager_->HandleDeviceNotTrust(udid); - int32_t sessionId = 32166; - authManager_->ProcIncompatible(sessionId); - - authManager_->authResponseContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE; - authManager_->authResponseContext_->importAuthCode = "importAuthCode"; - authManager_->importAuthCode_ = "importAuthCode"; - authManager_->ProcessAuthRequest(sessionId); - - authManager_->authResponseContext_->authType == AUTH_TYPE_NFC; - authManager_->authResponseContext_->isOnline = false; - authManager_->authResponseContext_->reply = 0; - authManager_->authResponseContext_->isIdenticalAccount = false; - authManager_->authResponseContext_->isAuthCodeReady = true; - authManager_->ProcessAuthRequest(sessionId); - - authManager_->authResponseContext_->reply = ERR_DM_UNSUPPORTED_AUTH_TYPE; - authManager_->authResponseContext_->authType = AUTH_TYPE_IMPORT_AUTH_CODE; - authManager_->authResponseContext_->isAuthCodeReady == false; - authManager_->ProcessAuthRequest(sessionId); -} - HWTEST_F(DmAuthManagerTest, IsAuthFinish_001, testing::ext::TestSize.Level1) { authManager_->authResponseContext_->reply = ERR_DM_UNSUPPORTED_AUTH_TYPE; @@ -1982,46 +1954,6 @@ HWTEST_F(DmAuthManagerTest, GetCloseSessionDelaySeconds_001, testing::ext::TestS ASSERT_EQ(ret, DM_OK); } -HWTEST_F(DmAuthManagerTest, GetTokenIdByBundleName_001, testing::ext::TestSize.Level1) -{ - int32_t userId = 1; - std::string bundleName = "b********Info"; - int64_t tokenId = 0; - EXPECT_CALL(*appManagerMock_, GetNativeTokenIdByName(_, _)).WillOnce(Return(DM_OK)); - int32_t ret = authManager_->GetTokenIdByBundleName(userId, bundleName, tokenId); - ASSERT_EQ(ret, DM_OK); - - EXPECT_CALL(*appManagerMock_, GetNativeTokenIdByName(_, _)).WillOnce(Return(ERR_DM_FAILED)); - EXPECT_CALL(*appManagerMock_, GetHapTokenIdByName(_, _, _, _)).WillOnce(Return(ERR_DM_FAILED)); - ret = authManager_->GetTokenIdByBundleName(userId, bundleName, tokenId); - ASSERT_EQ(ret, ERR_DM_FAILED); - - EXPECT_CALL(*appManagerMock_, GetNativeTokenIdByName(_, _)).WillOnce(Return(DM_OK)); - EXPECT_CALL(*appManagerMock_, GetHapTokenIdByName(_, _, _, _)).WillOnce(Return(DM_OK)); - ret = authManager_->GetTokenIdByBundleName(userId, bundleName, tokenId); - ASSERT_EQ(ret, DM_OK); - - std::string deviceId = "de*******8"; - authManager_->authResponseContext_ = std::make_shared(); - authManager_->authRequestContext_ = std::make_shared(); - authManager_->authRequestContext_->connSessionType = CONN_SESSION_TYPE_HML; - authManager_->JoinLnn(deviceId, false); - - int32_t errorCode = 0; - std::shared_ptr listener = std::make_shared(); - authManager_->authUiStateMgr_ = std::make_shared(listener); - authManager_->authResponseContext_->authType = AUTH_TYPE_IMPORT_AUTH_CODE; - authManager_->UpdateInputPincodeDialog(errorCode); - - authManager_->authResponseContext_->authType = AUTH_TYPE_NFC; - errorCode = ERR_DM_HICHAIN_PROOFMISMATCH; - authManager_->pincodeDialogEverShown_ = false; - authManager_->authRequestContext_->hostPkgName = "hostPkgName"; - authManager_->importAuthCode_ = "14785"; - authManager_->importPkgName_ = "hostPkgName"; - authManager_->UpdateInputPincodeDialog(errorCode); -} - HWTEST_F(DmAuthManagerTest, CheckNeedShowAuthInfoDialog_001, testing::ext::TestSize.Level1) { authManager_->authResponseContext_ = std::make_shared(); diff --git a/test/servicesfuzztest/authmanager_fuzzer/auth_manager_fuzzer.cpp b/test/servicesfuzztest/authmanager_fuzzer/auth_manager_fuzzer.cpp index f5026935e9549c36f04c656b654dc0a4f983793b..fae170d20d49895f6fe47976e6891ac86066cf40 100644 --- a/test/servicesfuzztest/authmanager_fuzzer/auth_manager_fuzzer.cpp +++ b/test/servicesfuzztest/authmanager_fuzzer/auth_manager_fuzzer.cpp @@ -170,14 +170,6 @@ void ParseJsonObjectFuzzTest(FuzzedDataProvider &fdp) authManager->ParseJsonObject(jsonObject); } -void GetTokenIdByBundleNameFuzzTest(FuzzedDataProvider &fdp) -{ - int32_t userId = fdp.ConsumeIntegral(); - std::string bundleName = fdp.ConsumeRandomLengthString(); - int64_t tokenId = fdp.ConsumeIntegral(); - authManager->GetTokenIdByBundleName(userId, bundleName, tokenId); -} - void GetBindLevelFuzzTest(FuzzedDataProvider &fdp) { int32_t bindLevel = fdp.ConsumeIntegral(); @@ -380,7 +372,6 @@ void AuthManagerFuzzTest(const uint8_t* data, size_t size) AuthDeviceFinishFuzzTest(fdp); GetAuthCodeAndPkgNameFuzzTest(fdp); GetPinCodeFuzzTest(fdp); - GetTokenIdByBundleNameFuzzTest(fdp); AuthenticateDeviceFuzzTest(fdp); BindTargetFuzzTest(fdp); ParseUltrasonicSideFuzzTest(fdp); diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp index c4e7f4be8efac95e6c34a929d20409295f022666..28dbe38324c9ab2aa75bd2647b888b0e90980102 100644 --- a/test/unittest/UTTest_device_manager_service.cpp +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -2493,6 +2493,15 @@ HWTEST_F(DeviceManagerServiceTest, GetEncryptedUuidByNetworkId_004, testing::ext EXPECT_EQ(ret, DM_OK); } +HWTEST_F(DeviceManagerServiceTest, OpenAuthSessionWithPara_001, testing::ext::TestSize.Level1) +{ + const std::string deviceId = "OpenAuthSessionWithPara"; + int32_t actionId = 0; + bool isEnable160m = false; + int32_t ret = DeviceManagerService::GetInstance().OpenAuthSessionWithPara(deviceId, actionId, isEnable160m); + EXPECT_NE(ret, DM_OK); +} + #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) HWTEST_F(DeviceManagerServiceTest, ConvertUdidHashToAnoyDeviceId_001, testing::ext::TestSize.Level1) { diff --git a/test/unittest/UTTest_device_manager_service_listener.cpp b/test/unittest/UTTest_device_manager_service_listener.cpp index a2bc5c562297f1d8b6140fe7238809eebec6ce09..4f654d2d4e797eecea27ab8cf69ac1a32ebb2353 100644 --- a/test/unittest/UTTest_device_manager_service_listener.cpp +++ b/test/unittest/UTTest_device_manager_service_listener.cpp @@ -1084,6 +1084,16 @@ HWTEST_F(DeviceManagerServiceListenerTest, SetDeviceInfo_001, testing::ext::Test listener_->SetDeviceInfo(pReq, processInfo, state, deviceInfo, deviceBasicInfo); EXPECT_EQ(listener_->alreadyOnlinePkgName_.empty(), false); } + +HWTEST_F(DeviceManagerServiceListenerTest, OpenAuthSessionWithPara_001, testing::ext::TestSize.Level1) +{ + const std::string deviceId = "OpenAuthSessionWithPara"; + int32_t actionId = 0; + bool isEnable160m = false; + std::shared_ptr listener_ = std::make_shared(); + int32_t ret = listener_->OpenAuthSessionWithPara(deviceId, actionId, isEnable160m); + EXPECT_NE(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index 178290401a8a6030224bd61365c34aadf68c6a80..8aedde22cea2b7c08fd4f364d8365bfd07429de0 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -1818,11 +1818,6 @@ HWTEST_F(DeviceManagerServiceTest, SendShareTypeUnBindBroadCast_001, testing::ex EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr); } -HWTEST_F(DeviceManagerServiceTest, HandleCredentialDeleted_001, testing::ext::TestSize.Level1) -{ - DeviceManagerService::GetInstance().HandleCredentialDeleted("credId", "credInfo"); -} - HWTEST_F(DeviceManagerServiceTest, HandleCredentialDeleted_002, testing::ext::TestSize.Level1) { EXPECT_CALL(*deviceManagerServiceImplMock_, HandleCredentialDeleted(_, _, _, _)).Times(0); @@ -1830,11 +1825,6 @@ HWTEST_F(DeviceManagerServiceTest, HandleCredentialDeleted_002, testing::ext::Te DeviceManagerService::GetInstance().HandleCredentialDeleted(nullptr, "credInfo"); } -HWTEST_F(DeviceManagerServiceTest, HandleCredentialDeleted_003, testing::ext::TestSize.Level1) -{ - DeviceManagerService::GetInstance().HandleCredentialDeleted("credId", nullptr); -} - HWTEST_F(DeviceManagerServiceTest, HandleCredentialDeleted_004, testing::ext::TestSize.Level1) { EXPECT_CALL(*multipleUserConnectorMock_, GetCurrentAccountUserID()).Times(0); diff --git a/test/unittest/UTTest_dm_pin_holder.h b/test/unittest/UTTest_dm_pin_holder.h index 3b3746babd5dc367fa12bf250f6855f76424ce32..58c9e4ef6409ccfa8623031ea085b3e763428f38 100644 --- a/test/unittest/UTTest_dm_pin_holder.h +++ b/test/unittest/UTTest_dm_pin_holder.h @@ -216,14 +216,14 @@ public: (void)processInfo; } - virtual void OnDevStateCallbackAdd(const ProcessInfo &processInfo, + void OnDevStateCallbackAdd(const ProcessInfo &processInfo, const std::vector &deviceList) override { (void)processInfo; (void)deviceList; } - virtual void OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo, + void OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo, const std::vector &deviceProfileInfos, int32_t code) override { (void)processInfo; @@ -231,7 +231,7 @@ public: (void)code; } - virtual void OnGetDeviceIconInfoResult(const ProcessInfo &processInfo, + void OnGetDeviceIconInfoResult(const ProcessInfo &processInfo, const DmDeviceIconInfo &dmDeviceIconInfo, int32_t code) override { (void)processInfo; @@ -239,7 +239,7 @@ public: (void)code; } - virtual void OnSetLocalDeviceNameResult(const ProcessInfo &processInfo, + void OnSetLocalDeviceNameResult(const ProcessInfo &processInfo, const std::string &deviceName, int32_t code) override { (void)processInfo; @@ -247,7 +247,7 @@ public: (void)code; } - virtual void OnSetRemoteDeviceNameResult(const ProcessInfo &processInfo, const std::string &deviceId, + void OnSetRemoteDeviceNameResult(const ProcessInfo &processInfo, const std::string &deviceId, const std::string &deviceName, int32_t code) override { (void)processInfo; @@ -260,6 +260,14 @@ public: { return ""; } + + virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) + { + (void)deviceId; + (void)actionId; + (void)isEnable160m; + return 0; + } }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/softbus_session_mock.cpp b/test/unittest/mock/softbus_session_mock.cpp index c85d7045d13a77be7fea3621d4830ec6912454da..331e0e2cbf58b2a546b64340caadf437006e2967 100644 --- a/test/unittest/mock/softbus_session_mock.cpp +++ b/test/unittest/mock/softbus_session_mock.cpp @@ -30,11 +30,6 @@ int32_t SoftbusSession::SendData(int32_t sessionId, std::string &message) return DmSoftbusSession::dmSoftbusSession->SendData(sessionId, message); } -int32_t SoftbusSession::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) -{ - return DmSoftbusSession::dmSoftbusSession->OpenAuthSessionWithPara(deviceId, actionId, isEnable160m); -} - int32_t SoftbusSession::OpenAuthSession(const std::string &deviceId) { return DmSoftbusSession::dmSoftbusSession->OpenAuthSession(deviceId); diff --git a/test/unittest/mock/softbus_session_mock.h b/test/unittest/mock/softbus_session_mock.h index 9f386d18388b8c01da00ab872a8e5fad15852dd3..bdb1ad0f52d1db87ee431265bebe491599a1e0f6 100644 --- a/test/unittest/mock/softbus_session_mock.h +++ b/test/unittest/mock/softbus_session_mock.h @@ -28,7 +28,6 @@ public: public: virtual int32_t GetPeerDeviceId(int32_t sessionId, std::string &peerDevId) = 0; virtual int32_t SendData(int32_t sessionId, std::string &message) = 0; - virtual int32_t OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m) = 0; virtual int32_t OpenAuthSession(const std::string &deviceId) = 0; public: @@ -39,7 +38,6 @@ class SoftbusSessionMock : public DmSoftbusSession { public: MOCK_METHOD(int32_t, GetPeerDeviceId, (int32_t, std::string &)); MOCK_METHOD(int32_t, SendData, (int32_t, std::string &)); - MOCK_METHOD(int32_t, OpenAuthSessionWithPara, (const std::string &, int32_t, bool)); MOCK_METHOD(int32_t, OpenAuthSession, (const std::string &)); }; } diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 7f4e0fe83efd156c1d13e2cca2b4c7fa15c707b5..c4d1ba8fe5106a23dc36a67e65a175e117e5a6a1 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -24,9 +24,10 @@ if (defined(ohos_lite)) { shared_library("devicemanagerutils") { include_dirs = [ "include", + "include/appInfo/lite", "include/crypto", "include/fwkload/lite", - "include/appInfo/lite", + "include/jsonstr_handle", "include/timer/lite", "${common_path}/include", "${common_path}/include/dfx", @@ -51,6 +52,7 @@ if (defined(ohos_lite)) { "src/crypto/dm_crypto.cpp", "src/dm_random.cpp", "src/fwkload/lite/dm_distributed_hardware_load.cpp", + "src/jsonstr_handle/dm_jsonstr_handle.cpp", "src/timer/lite/dm_timer.cpp", ] @@ -75,10 +77,11 @@ if (defined(ohos_lite)) { config("devicemanagerutils_config") { include_dirs = [ "include", + "include/appInfo/standard", "include/crypto", - "include/kvadapter", "include/fwkload/standard", - "include/appInfo/standard", + "include/jsonstr_handle", + "include/kvadapter", "include/timer", "${innerkits_path}/native_cpp/include", "${common_path}/include", @@ -128,6 +131,7 @@ if (defined(ohos_lite)) { "src/appInfo/standard/app_manager.cpp", "src/crypto/dm_crypto.cpp", "src/dm_random.cpp", + "src/jsonstr_handle/dm_jsonstr_handle.cpp", "src/kvadapter/dm_kv_info.cpp", "src/kvadapter/kv_adapter.cpp", "src/kvadapter/kv_adapter_manager.cpp", diff --git a/utils/include/appInfo/standard/app_manager.h b/utils/include/appInfo/standard/app_manager.h index 117fef077c0f44fcfd935151c114af5792e929eb..09247de53890f03e878bdf71574c4e947431bbbc 100644 --- a/utils/include/appInfo/standard/app_manager.h +++ b/utils/include/appInfo/standard/app_manager.h @@ -44,6 +44,8 @@ public: int32_t instIndex, int64_t &tokenId); DM_EXPORT int32_t GetCallerProcessName(std::string &processName); DM_EXPORT int32_t GetBundleNameForSelf(std::string &bundleName); + DM_EXPORT int32_t GetBundleNameByTokenId(int64_t tokenId, std::string &bundleName); + DM_EXPORT int32_t GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId); private: bool GetBundleManagerProxy(sptr &bundleManager); std::mutex appIdMapLock_; diff --git a/utils/include/jsonstr_handle/dm_jsonstr_handle.h b/utils/include/jsonstr_handle/dm_jsonstr_handle.h new file mode 100644 index 0000000000000000000000000000000000000000..9a89f31ab9932ea74b8a4f23ebe2c68c72e402ba --- /dev/null +++ b/utils/include/jsonstr_handle/dm_jsonstr_handle.h @@ -0,0 +1,36 @@ +/* + * 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_JSONSTR_HANDLE_H +#define OHOS_DM_JSONSTR_HANDLE_H + +#include "dm_single_instance.h" +#include +#include + + +namespace OHOS { +namespace DistributedHardware { +class JsonStrHandle { + DM_DECLARE_SINGLE_INSTANCE(JsonStrHandle); + +public: + DM_EXPORT void GetPeerAppInfoParseExtra(const std::string &extra, + uint64_t &peerTokenId, std::string &peerBundleName); + DM_EXPORT std::vector GetProxyTokenIdByExtra(const std::string &extraInfo); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_APP_MANAGER_H diff --git a/utils/src/appInfo/standard/app_manager.cpp b/utils/src/appInfo/standard/app_manager.cpp index ad0d9552246ea107ff099a9825278ddd3733a5bd..b424debff95f3c32fe6abda80173b0e36fcda0c1 100644 --- a/utils/src/appInfo/standard/app_manager.cpp +++ b/utils/src/appInfo/standard/app_manager.cpp @@ -191,6 +191,35 @@ DM_EXPORT int32_t AppManager::GetCallerName(bool isSystemSA, std::string &caller return DM_OK; } +DM_EXPORT int32_t AppManager::GetBundleNameByTokenId(int64_t tokenId, std::string &bundleName) +{ + if (tokenId < 0) { + LOGE("GetBundleNameByTokenId error."); + return ERR_DM_FAILED; + } + AccessTokenID tokenIdTemp = static_cast(tokenId); + ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenIdTemp); + if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) { + HapTokenInfo tokenInfo; + if (AccessTokenKit::GetHapTokenInfo(tokenIdTemp, tokenInfo) != EOK) { + LOGE("GetHapTokenInfo failed."); + return ERR_DM_FAILED; + } + bundleName = std::move(tokenInfo.bundleName); + } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) { + NativeTokenInfo tokenInfo; + if (AccessTokenKit::GetNativeTokenInfo(tokenIdTemp, tokenInfo) != EOK) { + LOGE("GetNativeTokenInfo failed."); + return ERR_DM_FAILED; + } + bundleName = std::move(tokenInfo.processName); + } else { + LOGE("failed, unsupported process."); + return ERR_DM_FAILED; + } + return DM_OK; +} + DM_EXPORT int32_t AppManager::GetNativeTokenIdByName(std::string &processName, int64_t &tokenId) { @@ -251,6 +280,19 @@ DM_EXPORT int32_t AppManager::GetCallerProcessName(std::string &processName) return DM_OK; } +int32_t AppManager::GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId) +{ + int32_t ret = GetNativeTokenIdByName(bundleName, tokenId); + if (ret == DM_OK) { + return DM_OK; + } + ret = GetHapTokenIdByName(userId, bundleName, 0, tokenId); + if (ret != DM_OK) { + LOGE("get tokenId by bundleName failed %{public}s", GetAnonyString(bundleName).c_str()); + } + return ret; +} + int32_t AppManager::GetBundleNameForSelf(std::string &bundleName) { sptr bundleManager = nullptr; diff --git a/utils/src/jsonstr_handle/dm_jsonstr_handle.cpp b/utils/src/jsonstr_handle/dm_jsonstr_handle.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7451a44e22c020a9a8c344957d004be20940b4c9 --- /dev/null +++ b/utils/src/jsonstr_handle/dm_jsonstr_handle.cpp @@ -0,0 +1,67 @@ +/* + * 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. + */ + +#include "dm_jsonstr_handle.h" + +#include "json_object.h" +#include "dm_anonymous.h" +#include "dm_error_type.h" +#include "dm_log.h" + +const char* TAG_PROXY = "proxy"; +const char* TAG_PEER_BUNDLE_NAME = "peerBundleName"; +const char* TAG_PEER_TOKENID = "peerTokenId"; + +namespace OHOS { +namespace DistributedHardware { +DM_IMPLEMENT_SINGLE_INSTANCE(JsonStrHandle); + +DM_EXPORT void JsonStrHandle::GetPeerAppInfoParseExtra(const std::string &extra, + uint64_t &peerTokenId, std::string &peerBundleName) +{ + JsonObject extraInfoJson(extra); + if (extraInfoJson.IsDiscarded()) { + LOGE("ParseExtra extraInfoJson error"); + return; + } + if (!extraInfoJson[TAG_PEER_BUNDLE_NAME].IsString() || !extraInfoJson[TAG_PEER_TOKENID].IsNumberInteger()) { + LOGE("ParseExtra TAG_PEER_BUNDLE_NAME or TAG_PEER_TOKENID error"); + return; + } + peerTokenId = extraInfoJson[TAG_PEER_TOKENID].Get(); + peerBundleName = extraInfoJson[TAG_PEER_BUNDLE_NAME].Get(); +} + +DM_EXPORT std::vector JsonStrHandle::GetProxyTokenIdByExtra(const std::string &extraInfo) +{ + std::vector tokenIdVec; + JsonObject extraInfoJson(extraInfo); + if (extraInfoJson.IsDiscarded() || !IsString(extraInfoJson, TAG_PROXY)) { + LOGE("GetProxyTokenIdByExtra extraInfoJson error"); + return tokenIdVec; + } + std::string proxyListStr = extraInfoJson[TAG_PROXY].Get(); + JsonObject proxyList; + if (!proxyList.Parse(proxyListStr)) { + LOGE("GetProxyTokenIdByExtra proxyListStr Parse error"); + return tokenIdVec; + } + for (auto &item : proxyList.Items()) { + tokenIdVec.push_back(item.Get()); + } + return tokenIdVec; +} +} // namespace DistributedHardware +} // namespace OHOS