diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index d0ac1fc8a6e66e01ad9a10bc5e993af459429296..82e1ad3fc49552865ba5d6b64c158e7b1eae3133 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -136,6 +136,16 @@ typedef struct DmAuthRequestContext { int32_t closeSessionDelaySeconds = 0; } DmAuthRequestContext; +typedef struct DMRecheckData { + std::string edition; + std::string localDeviceId; + int32_t localUserId; + std::string localAccountId; + int64_t tokenId; + std::string bundleName; + int32_t localBindLevel; +} DMRecheckData; + typedef struct DmAuthResponseContext { int32_t authType; std::string deviceId; @@ -191,6 +201,7 @@ typedef struct DmAuthResponseContext { bool isFinish = false; std::string edition; int32_t localBindLevel; + DMRecheckData recheckData; } DmAuthResponseContext; class AuthMessageProcessor; @@ -517,6 +528,7 @@ public: int32_t DeleteGroup(const std::string &pkgName, int32_t userId, const std::string &deviceId); int32_t StopAuthenticateDevice(const std::string &pkgName); void RequestReCheckMsg(); + void SendResponseReCheckMsg(); void ResponseReCheckMsg(); void RequestReCheckMsgDone(); private: @@ -592,6 +604,10 @@ private: std::mutex srcReqMsgLock_; bool isNeedProcCachedSrcReqMsg_ = false; std::string srcReqMsg_ = ""; + std::mutex recheckMutex_; + bool recheckMsgReceived_ = false; + std::string savedRecheckMsg_; + std::recursive_mutex savedRecheckMsgMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index b67246a0b4afb00059c8509d13377ccd84db51e2..5b6d7bf44f3f5affec7df09c53f96eedff6ea000 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -730,6 +730,14 @@ void AuthMessageProcessor::ParseReqReCheckMessage(nlohmann::json &json) if (IsInt32(jsonObject, TAG_BIND_LEVEL)) { authResponseContext_->localBindLevel = jsonObject[TAG_BIND_LEVEL].get(); } + + authResponseContext_->recheckData.edition = authResponseContext_->edition; + authResponseContext_->recheckData.localDeviceId = authResponseContext_->localDeviceId; + authResponseContext_->recheckData.localUserId = authResponseContext_->localUserId; + authResponseContext_->recheckData.localAccountId = authResponseContext_->localAccountId; + authResponseContext_->recheckData.tokenId = authResponseContext_->tokenId; + authResponseContext_->recheckData.bundleName = authResponseContext_->bundleName; + authResponseContext_->recheckData.localBindLevel = authResponseContext_->localBindLevel; } int32_t AuthMessageProcessor::SaveSessionKey(const uint8_t *sessionKey, const uint32_t keyLen) diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 9a9271708f5b758841655766f3099ac0b2b41e4d..06f4a1e4a6e4bab4ca08e3d9b4036c4c4e12a8a3 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -299,6 +299,7 @@ int32_t DmAuthManager::GetCloseSessionDelaySeconds(std::string &delaySecondsStr) void DmAuthManager::InitAuthState(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra) { + LOGI("[temp003] InitAuthState"); if (authenticationMap_.find(authType) != authenticationMap_.end()) { authPtr_ = authenticationMap_[authType]; } @@ -344,6 +345,9 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au LOGE("CheckAuthParamVaildExtra failed, param is invaild."); return ret; } + recheckMsgReceived_ = false; + savedRecheckMsg_.clear(); + isAuthenticateDevice_ = true; if (authType == AUTH_TYPE_CRE) { LOGI("DmAuthManager::AuthenticateDevice for credential type, joinLNN directly."); @@ -572,8 +576,13 @@ void DmAuthManager::ProcessSourceMsg() } break; case MSG_TYPE_RESP_RECHECK_MSG: - if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_RECHECK_MSG) { - authRequestState_->TransitionTo(std::make_shared()); + { + std::lock_guard guard(recheckMutex_); + if (authRequestState_->GetStateType() == AuthState::AUTH_REQUEST_RECHECK_MSG) { + authRequestState_->TransitionTo(std::make_shared()); + } else { + LOGI("[temp003] not in state, skip MSG_TYPE_RESP_RECHECK_MSG"); + } } break; default: @@ -652,6 +661,16 @@ void DmAuthManager::OnDataReceived(const int32_t sessionId, const std::string me if ((authRequestState_ != nullptr) && (authResponseState_ == nullptr)) { // source device auth process + if (authResponseContext_->msgType == MSG_TYPE_RESP_RECHECK_MSG) { + std::lock_guard guard(savedRecheckMsgMutex_); + recheckMsgReceived_ = true; + if (authResponseContext_->edition.empty()) { + LOGE("[temp003] parse failed, save MSG_TYPE_RESP_RECHECK_MSG"); + savedRecheckMsg_ = message; + return; + } + } + ProcessSourceMsg(); } else if ((authResponseState_ != nullptr) && (authRequestState_ == nullptr)) { // sink device auth process @@ -2085,7 +2104,9 @@ bool DmAuthManager::AuthDeviceTransmit(int64_t requestId, const uint8_t *data, u void DmAuthManager::SrcAuthDeviceFinish() { + LOGI("[temp003] SrcAuthDeviceFinish"); CHECK_NULL_VOID(authRequestState_); + std::lock_guard guard(recheckMutex_); authRequestState_->TransitionTo(std::make_shared()); if (authResponseContext_->confirmOperation != USER_OPERATION_TYPE_ALLOW_AUTH && authResponseContext_->confirmOperation != USER_OPERATION_TYPE_ALLOW_AUTH_ALWAYS) { @@ -2103,6 +2124,16 @@ void DmAuthManager::SrcAuthDeviceFinish() ConverToFinish(); return; } + + savedRecheckMsgMutex_.lock(); + if (!savedRecheckMsg_.empty()) { + LOGI("[temp003] process savedRecheckMsg_"); + authMessageProcessor_->SetResponseContext(authResponseContext_); + authMessageProcessor_->ParseMessage(savedRecheckMsg_); + savedRecheckMsg_.clear(); + } + savedRecheckMsgMutex_.unlock(); + if (authResponseContext_->isOnline && !authResponseContext_->haveCredential) { authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_CANCEL_PIN_CODE_INPUT); if (!authResponseContext_->isIdenticalAccount && !authResponseContext_->hostPkgName.empty()) { @@ -2155,19 +2186,22 @@ void DmAuthManager::SinkAuthDeviceFinish() srcReqMsg_ = ""; isNeedProcCachedSrcReqMsg_ = false; } - if (!isNeedProcCachedSrcReqMsg || srcReqMsg.empty()) { - LOGI("please wait client request."); - return; - } - authMessageProcessor_->SetResponseContext(authResponseContext_); - if (authMessageProcessor_->ParseMessage(srcReqMsg) != DM_OK) { - LOGE("ParseMessage failed."); - return; - } - if (!CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { - authResponseState_->TransitionTo(std::make_shared()); + if (isNeedProcCachedSrcReqMsg && !srcReqMsg.empty()) { + authMessageProcessor_->SetResponseContext(authResponseContext_); + if (authMessageProcessor_->ParseMessage(srcReqMsg) != DM_OK) { + LOGE("ParseMessage failed."); + return; + } + if (!CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { + authResponseState_->TransitionTo(std::make_shared()); + } else { + authResponseState_->TransitionTo(std::make_shared()); + } } else { - authResponseState_->TransitionTo(std::make_shared()); + if (CompareVersion(remoteVersion_, std::string(DM_VERSION_5_0_2))) { + LOGE("[temp003] SinkAuthDeviceFinish SendResponseReCheckMsg"); + SendResponseReCheckMsg(); + } } } @@ -2837,23 +2871,15 @@ void DmAuthManager::RequestReCheckMsg() authMessageProcessor_->SetResponseContext(authResponseContext_); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_RECHECK_MSG); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); + + if (recheckMsgReceived_) { + LOGI("[temp003] recheckMsgReceived_ enter AuthRequestReCheckMsgDone"); + authRequestState_->TransitionTo(std::make_shared()); + } } -void DmAuthManager::ResponseReCheckMsg() +void DmAuthManager::SendResponseReCheckMsg() { - LOGI("remoteVersion %{public}s, authResponseContext_->edition %{public}s.", - remoteVersion_.c_str(), authResponseContext_->edition.c_str()); - if (!IsSinkMsgValid()) { - LOGE("peer deviceId not trust."); - authResponseContext_->isFinish = false; - isFinishOfLocal_ = false; - authMessageProcessor_->SetEncryptFlag(false); - int32_t sessionId = authResponseContext_->sessionId; - authResponseState_->TransitionTo(std::make_shared()); - CHECK_NULL_VOID(softbusConnector_->GetSoftbusSession()); - softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); - return; - } char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); authResponseContext_->edition = DM_VERSION_5_0_3; @@ -2869,6 +2895,25 @@ void DmAuthManager::ResponseReCheckMsg() authMessageProcessor_->SetEncryptFlag(true); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_RECHECK_MSG); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); +} + +void DmAuthManager::ResponseReCheckMsg() +{ + LOGI("remoteVersion %{public}s, authResponseContext_->edition %{public}s.", + remoteVersion_.c_str(), authResponseContext_->edition.c_str()); + if (!IsSinkMsgValid()) { + LOGE("peer deviceId not trust."); + authResponseContext_->isFinish = false; + isFinishOfLocal_ = false; + authMessageProcessor_->SetEncryptFlag(false); + int32_t sessionId = authResponseContext_->sessionId; + authResponseState_->TransitionTo(std::make_shared()); + CHECK_NULL_VOID(softbusConnector_->GetSoftbusSession()); + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; + } + SendResponseReCheckMsg(); + PutAccessControlList(); } @@ -2884,17 +2929,18 @@ void DmAuthManager::RequestReCheckMsgDone() authRequestState_->TransitionTo(std::make_shared()); return; } + PutAccessControlList(); authRequestState_->TransitionTo(std::make_shared()); } bool DmAuthManager::IsSinkMsgValid() { - if (authResponseContext_->edition != remoteVersion_ || - authResponseContext_->localDeviceId != remoteDeviceId_ || - authResponseContext_->localUserId != authResponseContext_->remoteUserId || - authResponseContext_->bundleName != authResponseContext_->hostPkgName || - authResponseContext_->localBindLevel != authResponseContext_->bindLevel) { + if (authResponseContext_->recheckData.edition != remoteVersion_ || + authResponseContext_->recheckData.localDeviceId != remoteDeviceId_ || + authResponseContext_->recheckData.localUserId != authResponseContext_->remoteUserId || + authResponseContext_->recheckData.bundleName != authResponseContext_->hostPkgName || + authResponseContext_->recheckData.localBindLevel != authResponseContext_->bindLevel) { return false; } return true; @@ -2902,11 +2948,11 @@ bool DmAuthManager::IsSinkMsgValid() bool DmAuthManager::IsSourceMsgValid() { - if (authResponseContext_->edition != remoteVersion_ || - authResponseContext_->localDeviceId != remoteDeviceId_ || - authResponseContext_->localUserId != authRequestContext_->remoteUserId || - authResponseContext_->bundleName != authResponseContext_->peerBundleName || - authResponseContext_->localBindLevel != authResponseContext_->bindLevel) { + if (authResponseContext_->recheckData.edition != remoteVersion_ || + authResponseContext_->recheckData.localDeviceId != remoteDeviceId_ || + authResponseContext_->recheckData.localUserId != authRequestContext_->remoteUserId || + authResponseContext_->recheckData.bundleName != authResponseContext_->peerBundleName || + authResponseContext_->recheckData.localBindLevel != authResponseContext_->bindLevel) { return false; } authResponseContext_->localAccountId = authRequestContext_->localAccountId;