From ed7867500f56302c7fdd7ebc03ee66cd4ab81b90 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 25 Nov 2023 19:29:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=BA=A7=E7=BB=91?= =?UTF-8?q?=E5=AE=9Adp=E6=9F=A5=E8=AF=A2=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- services/implementation/BUILD.gn | 1 + .../authentication/auth_message_processor.h | 4 + .../authentication/auth_request_state.h | 30 ++ .../authentication/auth_response_state.h | 19 ++ .../include/authentication/dm_auth_manager.h | 46 +++ .../hichain/hichain_auth_connector.h | 69 ++++ .../hichain/hichain_connector_callback.h | 10 + .../dependency/softbus/softbus_connector.h | 5 + .../dependency/softbus/softbus_session.h | 1 + .../include/dependency/timer/dm_timer.h | 3 + .../authentication/auth_message_processor.cpp | 11 + .../src/authentication/auth_request_state.cpp | 83 +++++ .../authentication/auth_response_state.cpp | 48 +++ .../src/authentication/dm_auth_manager.cpp | 224 ++++++++++++- .../hichain/hichain_auth_connector.cpp | 305 ++++++++++++++++++ .../dependency/softbus/softbus_connector.cpp | 44 +++ .../dependency/softbus/softbus_session.cpp | 6 + 17 files changed, 908 insertions(+), 1 deletion(-) create mode 100644 services/implementation/include/dependency/hichain/hichain_auth_connector.h create mode 100644 services/implementation/src/dependency/hichain/hichain_auth_connector.cpp diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index d90b5058d..aaa0675eb 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -186,6 +186,7 @@ if (defined(ohos_lite)) { "src/credential/dm_credential_manager.cpp", "src/dependency/commonevent/dm_common_event_manager.cpp", "src/dependency/deviceprofile/deviceprofile_connector.cpp", + "src/dependency/hichain/hichain_auth_connector.cpp", "src/dependency/hichain/hichain_connector.cpp", "src/dependency/multipleuser/multiple_user_connector.cpp", "src/dependency/softbus/softbus_connector.cpp", diff --git a/services/implementation/include/authentication/auth_message_processor.h b/services/implementation/include/authentication/auth_message_processor.h index 94e2e1d78..a2ef59016 100644 --- a/services/implementation/include/authentication/auth_message_processor.h +++ b/services/implementation/include/authentication/auth_message_processor.h @@ -51,6 +51,9 @@ constexpr const char* QR_CODE_KEY = "qrCode"; constexpr const char* TAG_AUTH_TOKEN = "authToken"; constexpr const char* NFC_CODE_KEY = "nfcCode"; constexpr const char* OLD_VERSION_ACCOUNT = "oldVersionAccount"; +constexpr const char* TAG_DATA = "data"; +constexpr const char* TAG_DATA_LEN = "dataLen"; + class DmAuthManager; struct DmAuthRequestContext; @@ -67,6 +70,7 @@ public: void SetResponseContext(std::shared_ptr authResponseContext); std::shared_ptr GetResponseContext(); std::shared_ptr GetRequestContext(); + std::string CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen); private: std::string CreateRequestAuthMessage(nlohmann::json &json); diff --git a/services/implementation/include/authentication/auth_request_state.h b/services/implementation/include/authentication/auth_request_state.h index 647ce7db1..21327c4ac 100644 --- a/services/implementation/include/authentication/auth_request_state.h +++ b/services/implementation/include/authentication/auth_request_state.h @@ -181,6 +181,36 @@ public: */ int32_t Enter() override; }; + +class AuthRequestCredential : public AuthRequestState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + +class AuthRequestCredentialDone : public AuthRequestState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + +class AuthRequestDeleteInit : public AuthRequestState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + +class AuthRequestSyncDeleteAcl : public AuthRequestState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + +class AuthRequestSyncDeleteAclNone : public AuthRequestState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_AUTH_REQUEST_STATE_H diff --git a/services/implementation/include/authentication/auth_response_state.h b/services/implementation/include/authentication/auth_response_state.h index c5982464d..d4a933e3a 100644 --- a/services/implementation/include/authentication/auth_response_state.h +++ b/services/implementation/include/authentication/auth_response_state.h @@ -106,6 +106,25 @@ public: int32_t GetStateType() override; int32_t Enter() override; }; + +class AuthResponseCredential : public AuthResponseState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + +class AuthResponseSyncDeleteAcl : public AuthResponseState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + +class AuthResponseSyncDeleteAclNone : public AuthResponseState { +public: + int32_t GetStateType() override; + int32_t Enter() override; +}; + } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_AUTH_RESPONSE_STATE_H diff --git a/services/implementation/include/authentication/dm_auth_manager.h b/services/implementation/include/authentication/dm_auth_manager.h index 6f37a1aba..c04301ce5 100644 --- a/services/implementation/include/authentication/dm_auth_manager.h +++ b/services/implementation/include/authentication/dm_auth_manager.h @@ -29,6 +29,7 @@ #include "dm_constants.h" #include "dm_device_info.h" #include "dm_timer.h" +#include "hichain_auth_connector.h" #include "hichain_connector.h" #include "softbus_connector.h" #include "softbus_session.h" @@ -42,13 +43,21 @@ typedef enum AuthState { AUTH_REQUEST_REPLY, AUTH_REQUEST_JOIN, AUTH_REQUEST_NETWORK, + AUTH_REQUEST_CREDENTIAL, + AUTH_REQUEST_CREDENTIAL_DONE, AUTH_REQUEST_FINISH, + AUTH_REQUEST_DELETE_INIT, + AUTH_REQUEST_SYNCDELETE, + AUTH_REQUEST_SYNCDELETE_DONE, AUTH_RESPONSE_INIT = 20, AUTH_RESPONSE_NEGOTIATE, AUTH_RESPONSE_CONFIRM, AUTH_RESPONSE_GROUP, AUTH_RESPONSE_SHOW, + AUTH_RESPONSE_CREDENTIAL, AUTH_RESPONSE_FINISH, + AUTH_RESPONSE_SYNCDELETE, + AUTH_RESPONSE_SYNCDELETE_DONE, } AuthState; enum DmMsgType : int32_t { @@ -64,6 +73,13 @@ enum DmMsgType : int32_t { MSG_TYPE_CHANNEL_CLOSED = 300, MSG_TYPE_SYNC_GROUP = 400, MSG_TYPE_AUTH_BY_PIN = 500, + + MSG_TYPE_REQ_PUBLICKEY, + MSG_TYPE_RESP_PUBLICKEY, + MSG_TYPE_REQ_SYNC_DELETE, + MSG_TYPE_REQ_SYNC_DELETE_DONE, + MSG_TYPE_REQ_AUTH_DEVICE_NEGOTIATE, + MSG_TYPE_RESP_AUTH_DEVICE_NEGOTIATE, }; enum DmAuthType : int32_t { @@ -133,12 +149,17 @@ typedef struct DmAuthResponseContext { int32_t state; std::vector syncGroupList; std::string accountGroupIdHash; + std::string publicKey; + bool isOnline; + int32_t bindLevel; + bool haveCredential; } DmAuthResponseContext; class AuthMessageProcessor; class DmAuthManager final : public ISoftbusSessionCallback, public IHiChainConnectorCallback, + public IDmDeviceAuthCallback, public std::enable_shared_from_this { public: DmAuthManager(std::shared_ptr softbusConnector, @@ -450,6 +471,25 @@ private: int32_t ParseConnectAddr(const PeerTargetId &targetId, std::string &deviceId); int32_t ParseAuthType(const std::map &bindParam, int32_t &authType); int32_t ParseExtra(const std::map &bindParam, std::string &extra); + +public: + void RequestCredential(); + void GenerateCredential(std::string &publicKey); + void RequestCredentialDone(); + void RequestSyncDeleteAcl(); + void ResponseCredential(); + void ResponseSyncDeleteAcl(); + bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen); + void AuthDeviceFinish(int64_t requestId); + void AuthDeviceError(int64_t requestId, int32_t errorCode); + void GetRemoteDeviceId(std::string &deviceId); + int32_t EstablishUnbindChannel(const std::string &deviceIdHash); + void SyncDeleteAclDone(); + void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen); + +private: + int32_t ImportCredential(std::string &deviceId, std::string &publicKey); + private: std::shared_ptr softbusConnector_; std::shared_ptr hiChainConnector_; @@ -473,6 +513,12 @@ private: std::string importPkgName_ = ""; std::string importAuthCode_ = ""; PeerTargetId peerTargetId_; + bool unBindFlag_ = false; +private: + std::shared_ptr hiChainAuthConnector_; + const uint8_t *sessionKey_ = nullptr; + uint32_t sessionKeyLen_ = 0; + std::string remoteDeviceId_ = ""; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h new file mode 100644 index 000000000..84d660f8b --- /dev/null +++ b/services/implementation/include/dependency/hichain/hichain_auth_connector.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2023 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_HICHAIN_AUTH_CONNECTOR_H +#define OHOS_HICHAIN_AUTH_CONNECTOR_H + +#include "device_auth.h" +#include "device_auth_defines.h" +#include "dm_constants.h" +#include "hichain_connector_callback.h" +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace DistributedHardware { + +enum { + /** invalid operationCode for initialize */ + CRED_OP_INVALID = -1, + /** operationCode for ProcessCredential to query credential */ + CRED_OP_QUERY, + /** operationCode for ProcessCredential to create credential */ + CRED_OP_CREATE, + /** operationCode for ProcessCredential to import credential */ + CRED_OP_IMPORT, + /** operationCode for ProcessCredential to delete credential */ + CRED_OP_DELETE, +}; + +class HiChainAuthConnector { +public: + static bool onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen); + static void onFinish(int64_t requestId, int operationCode, const char *returnData); + static void onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn); + static char *onRequest(int64_t requestId, int operationCode, const char *reqParams); + static void onSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen); + +public: + HiChainAuthConnector(); + ~HiChainAuthConnector(); + int32_t AuthDevice(int32_t pinCode, int32_t osAccountId, std::string udid, int64_t requestId); + int32_t ProcessAuthData(int64_t requestId, std::string authData, int32_t osAccountId); + int32_t GenerateCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey); + bool QueryCredential(std::string &localUdid, int32_t osAccountId); + int32_t ImportCredential(int32_t osAccountId, std::string deviceId, std::string publicKey); + int32_t DeleteCredential(std::string &deviceId); + int32_t RegisterHiChainAuthCallback(std::shared_ptr callback); + int32_t StartAuthDevice(int64_t requestId, const char* authParams, const DeviceAuthCallback* callbak); + int32_t ProcessAuthDevice(int64_t requestId, const char* authParams, const DeviceAuthCallback* callbak); + int32_t ProcessCredential(int32_t operationCode, const char* requestParams, char** returnData); + +private: + DeviceAuthCallback deviceAuthCallback_; + static std::shared_ptr dmDeviceAuthCallback_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_HICHAIN_AUTH_CONNECTOR_H diff --git a/services/implementation/include/dependency/hichain/hichain_connector_callback.h b/services/implementation/include/dependency/hichain/hichain_connector_callback.h index cd4b9d889..7918c3274 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector_callback.h +++ b/services/implementation/include/dependency/hichain/hichain_connector_callback.h @@ -30,6 +30,16 @@ class IDmGroupResCallback { public: virtual void OnGroupResult(int64_t requestId, int32_t action, const std::string &resultInfo) = 0; }; + +class IDmDeviceAuthCallback { +public: + virtual bool AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) = 0; + virtual void AuthDeviceFinish(int64_t requestId) = 0; + virtual void AuthDeviceError(int64_t requestId, int32_t errorCode) = 0; + virtual void AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) = 0; + virtual int32_t GetPinCode() = 0; + virtual void GetRemoteDeviceId(std::string &deviceId) = 0; +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_HICHAIN_CONNECTOR_CALLBACK_H diff --git a/services/implementation/include/dependency/softbus/softbus_connector.h b/services/implementation/include/dependency/softbus/softbus_connector.h index 642635f94..2ae485182 100644 --- a/services/implementation/include/dependency/softbus/softbus_connector.h +++ b/services/implementation/include/dependency/softbus/softbus_connector.h @@ -139,6 +139,9 @@ public: std::string GetLocalDeviceName(); int32_t GetLocalDeviceTypeId(); int32_t AddMemberToDiscoverMap(const std::string &deviceId, std::shared_ptr deviceInfo); + std::string GetNetworkIdByUdidHash(const std::string &deviceIdHash); + void HandleDeviceOnline(std::string &deviceId); + void SetPkgName(std::string pkgName); private: static void ConvertDeviceInfoToDmDevice(const DeviceInfo &deviceInfo, DmDeviceInfo &dmDeviceInfo); @@ -162,10 +165,12 @@ private: static std::map> publishCallbackMap_; static std::queue discoveryDeviceIdQueue_; static std::unordered_map deviceUdidMap_; + static std::vector pkgNameVec_; static std::mutex discoveryCallbackMutex_; static std::mutex discoveryDeviceInfoMutex_; static std::mutex stateCallbackMutex_; static std::mutex deviceUdidLocks_; + static std::mutex pkgNameVecMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/softbus/softbus_session.h b/services/implementation/include/dependency/softbus/softbus_session.h index e90b44dd6..dc41f9fd3 100644 --- a/services/implementation/include/dependency/softbus/softbus_session.h +++ b/services/implementation/include/dependency/softbus/softbus_session.h @@ -83,6 +83,7 @@ public: * @tc.type: FUNC */ int32_t GetPeerDeviceId(int32_t sessionId, std::string &peerDevId); + int32_t OpenUnbindSession(const std::string &netWorkId); private: static std::shared_ptr sessionCallback_; diff --git a/services/implementation/include/dependency/timer/dm_timer.h b/services/implementation/include/dependency/timer/dm_timer.h index 955fd3cfb..743ae33d8 100644 --- a/services/implementation/include/dependency/timer/dm_timer.h +++ b/services/implementation/include/dependency/timer/dm_timer.h @@ -35,6 +35,9 @@ constexpr const char* ADD_TIMEOUT_TASK = "deviceManagerTimer:add"; constexpr const char* WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate"; constexpr const char* WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest"; constexpr const char* STATE_TIMER_PREFIX = "deviceManagerTimer:stateTimer_"; +constexpr const char* AUTH_DEVICE_TIMEOUT_TASK = "deviceManagerTimer:authDevice_"; +constexpr const char* SYNC_DELETE_TIMEOUT_TASK = "deviceManagerTimer:syncDelete_"; + constexpr int32_t DELAY_TICK_MILLSECONDS = 1000; typedef std::chrono::steady_clock::time_point timerPoint; diff --git a/services/implementation/src/authentication/auth_message_processor.cpp b/services/implementation/src/authentication/auth_message_processor.cpp index d676a690f..c108c64d6 100644 --- a/services/implementation/src/authentication/auth_message_processor.cpp +++ b/services/implementation/src/authentication/auth_message_processor.cpp @@ -399,5 +399,16 @@ std::shared_ptr AuthMessageProcessor::GetRequestContext() { return authRequestContext_; } + +std::string AuthMessageProcessor::CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen) +{ + LOGI("CreateDeviceAuthMessage start, msgType %d.", msgType); + nlohmann::json jsonObj; + jsonObj[TAG_MSG_TYPE] = msgType; + std::string authDataStr = std::string(reinterpret_cast(data), dataLen); + jsonObj[TAG_DATA] = authDataStr; + jsonObj[TAG_DATA_LEN] = dataLen; + return jsonObj.dump(); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/authentication/auth_request_state.cpp b/services/implementation/src/authentication/auth_request_state.cpp index 94ea2544a..d77539960 100644 --- a/services/implementation/src/authentication/auth_request_state.cpp +++ b/services/implementation/src/authentication/auth_request_state.cpp @@ -170,5 +170,88 @@ int32_t AuthRequestFinishState::Enter() stateAuthManager->AuthenticateFinish(); return DM_OK; } + +//pkgbind +int32_t AuthRequestCredential::GetStateType() +{ + return AuthState::AUTH_REQUEST_CREDENTIAL; +} + +int32_t AuthRequestCredential::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return ERR_DM_FAILED; + } + stateAuthManager->RequestCredential(); + return DM_OK; +} + +int32_t AuthRequestCredentialDone::GetStateType() +{ + return AuthState::AUTH_REQUEST_CREDENTIAL_DONE; +} + +int32_t AuthRequestCredentialDone::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return ERR_DM_FAILED; + } + stateAuthManager->RequestCredentialDone(); + return DM_OK; +} + +int32_t AuthRequestDeleteInit::GetStateType() +{ + return AuthState::AUTH_REQUEST_DELETE_INIT; +} + +int32_t AuthRequestDeleteInit::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return ERR_DM_FAILED; + } + stateAuthManager->EstablishUnbindChannel(context_->deviceId); + return DM_OK; +} + +int32_t AuthRequestSyncDeleteAcl::GetStateType() +{ + return AuthState::AUTH_REQUEST_SYNCDELETE; +} + +int32_t AuthRequestSyncDeleteAcl::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return ERR_DM_FAILED; + } + stateAuthManager->RequestSyncDeleteAcl(); + return DM_OK; +} + +int32_t AuthRequestSyncDeleteAclNone::GetStateType() +{ + return AuthState::AUTH_REQUEST_SYNCDELETE_DONE; +} + +int32_t AuthRequestSyncDeleteAclNone::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return ERR_DM_FAILED; + } + stateAuthManager->SyncDeleteAclDone(); + return DM_OK; +} + + } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/authentication/auth_response_state.cpp b/services/implementation/src/authentication/auth_response_state.cpp index c1196a8ae..58d3fd4c3 100644 --- a/services/implementation/src/authentication/auth_response_state.cpp +++ b/services/implementation/src/authentication/auth_response_state.cpp @@ -151,5 +151,53 @@ int32_t AuthResponseFinishState::Enter() stateAuthManager->AuthenticateFinish(); return DM_OK; } + +int32_t AuthResponseCredential::GetStateType() +{ + return AuthState::AUTH_RESPONSE_CREDENTIAL; +} + +int32_t AuthResponseCredential::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthResponseFinishState authManager_ is null"); + return ERR_DM_FAILED; + } + stateAuthManager->ResponseCredential(); + return DM_OK; +} + +int32_t AuthResponseSyncDeleteAcl::GetStateType() +{ + return AuthState::AUTH_RESPONSE_SYNCDELETE; +} + +int32_t AuthResponseSyncDeleteAcl::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthResponseFinishState authManager_ is null"); + return ERR_DM_FAILED; + } + stateAuthManager->ResponseSyncDeleteAcl(); + return DM_OK; +} + +int32_t AuthResponseSyncDeleteAclNone::GetStateType() +{ + return AuthState::AUTH_RESPONSE_SYNCDELETE_DONE; +} + +int32_t AuthResponseSyncDeleteAclNone::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthResponseFinishState authManager_ is null"); + return ERR_DM_FAILED; + } + stateAuthManager->SyncDeleteAclDone(); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index 046cbdd9f..b4f7b0868 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -51,7 +51,7 @@ const int32_t DM_AUTH_TYPE_MAX = 5; const int32_t DM_AUTH_TYPE_MIN = 1; const int32_t AUTH_SESSION_SIDE_SERVER = 0; const int32_t USLEEP_TIME_MS = 500000; // 500ms - +const int32_t DM_APP = 3; constexpr const char* APP_OPERATION_KEY = "appOperation"; constexpr const char* TARGET_PKG_NAME_KEY = "targetPkgName"; constexpr const char* CUSTOM_DESCRIPTION_KEY = "customDescription"; @@ -1321,5 +1321,227 @@ bool DmAuthManager::IsAuthTypeSupported(const int32_t &authType) } return true; } + +void DmAuthManager::RequestCredential() +{ + LOGI("DmAuthManager::RequestCredential start."); + std::string publicKey = ""; + GenerateCredential(publicKey); + authResponseContext_->publicKey = publicKey; + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_PUBLICKEY); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); +} + +void DmAuthManager::GenerateCredential(std::string &publicKey) +{ + LOGI("DmAuthManager::GenerateCredential start."); + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string localUdid = localDeviceId; + int32_t osAccountId = MultipleUserConnector::GetCurrentAccountUserID(); + hiChainAuthConnector_->GenerateCredential(localUdid, osAccountId, publicKey); +} + +void DmAuthManager::RequestCredentialDone() +{ + LOGI("DmAuthManager ExchangeCredentailDone start"); + if (authResponseContext_ == nullptr) { + LOGE("failed to JoinNeWork because authResponseContext_ is nullptr"); + return; + } + if (ImportCredential(remoteDeviceId_, authResponseContext_->publicKey) != DM_OK) { + LOGE("ResponseCredential import credential failed."); + } + timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); + softbusConnector_->JoinLnn(authRequestContext_->deviceId); + authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; + authRequestContext_->reason = DM_OK; + authRequestState_->TransitionTo(std::make_shared()); +} + +int32_t DmAuthManager::ImportCredential(std::string &deviceId, std::string &publicKey) +{ + LOGI("DmAuthManager::ImportCredential"); + int32_t osAccountId = MultipleUserConnector::GetCurrentAccountUserID(); + return hiChainAuthConnector_->ImportCredential(osAccountId, deviceId, publicKey); +} + +int32_t DmAuthManager::EstablishUnbindChannel(const std::string &deviceIdHash) +{ + LOGI("DmAuthManager::EstablishUnbindChannel"); + std::string netWorkId = softbusConnector_->GetNetworkIdByUdidHash(deviceIdHash); + int32_t sessionId = softbusConnector_->GetSoftbusSession()->OpenUnbindSession(netWorkId); + if (sessionId < 0) { + LOGE("OpenAuthSession failed, stop the syncdeleteacl."); + authResponseContext_ = std::make_shared(); + authResponseContext_->state = AuthState::AUTH_REQUEST_SYNCDELETE; + authRequestContext_->reason = ERR_DM_AUTH_OPEN_SESSION_FAILED; + if (authRequestState_ != nullptr) { + authRequestState_->TransitionTo(std::make_shared()); + } + } + return DM_OK; +} + +void DmAuthManager::RequestSyncDeleteAcl() +{ + LOGI("RequestSyncDeleteAcl start."); + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_SYNC_DELETE); + softbusConnector_->GetSoftbusSession()->SendData(authRequestContext_->sessionId, message); +} + +void DmAuthManager::SyncDeleteAclDone() +{ + LOGI("SyncDeleteAclDone start."); + unBindFlag_ = false; + if (authRequestState_ != nullptr) { + if (authResponseContext_->reply == DM_OK || isFinishOfLocal_) { + authMessageProcessor_->SetResponseContext(authResponseContext_); + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_SYNC_DELETE_DONE); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); + } + listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId, + authRequestContext_->token, authResponseContext_->state, authRequestContext_->reason); + usleep(USLEEP_TIME_MS); // 500ms + softbusConnector_->GetSoftbusSession()->CloseAuthSession(authRequestContext_->sessionId); + timer_->DeleteAll(); + isFinishOfLocal_ = true; + authRequestContext_ = nullptr; + authResponseContext_ = nullptr; + authRequestState_ = nullptr; + authMessageProcessor_ = nullptr; + } else if (authResponseState_ != nullptr) { + if (isFinishOfLocal_) { + authMessageProcessor_->SetResponseContext(authResponseContext_); + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_SYNC_DELETE_DONE); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); + } + timer_->DeleteAll(); + isFinishOfLocal_ = true; + authResponseContext_ = nullptr; + authResponseState_ = nullptr; + authMessageProcessor_ = nullptr; + } +} + +void DmAuthManager::ResponseCredential() +{ + LOGI("DmAuthManager::ResponseCredential start."); + std::string publicKey = ""; + GenerateCredential(publicKey); + if (ImportCredential(remoteDeviceId_, authResponseContext_->publicKey) != DM_OK) { + LOGE("ResponseCredential import credential failed."); + } + authResponseContext_->publicKey = publicKey; + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_RESP_PUBLICKEY); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); +} + +void DmAuthManager::ResponseSyncDeleteAcl() +{ + LOGI("ResponseSyncDeleteAcl start."); + timer_->DeleteTimer(std::string(SYNC_DELETE_TIMEOUT_TASK)); + authResponseContext_->reply = DM_OK; + authMessageProcessor_->SetResponseContext(authResponseContext_); + std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_SYNC_DELETE_DONE); + softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); +} + +bool DmAuthManager::AuthDeviceTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) +{ + LOGI("DmAuthManager::onTransmit start."); + if (requestId != authResponseContext_->requestId) { + LOGE("DmAuthManager::onTransmit requestId %d is error.", requestId); + return false; + } + std::string message = ""; + if (authRequestState_ != nullptr && authResponseState_ == nullptr) { + LOGI("SoftbusSession send msgType %d.", MSG_TYPE_REQ_AUTH_DEVICE_NEGOTIATE); + message = authMessageProcessor_->CreateDeviceAuthMessage(MSG_TYPE_REQ_AUTH_DEVICE_NEGOTIATE, data, dataLen); + } else if (authRequestState_ == nullptr && authResponseState_ != nullptr) { + LOGI("SoftbusSession send msgType %d.", MSG_TYPE_RESP_AUTH_DEVICE_NEGOTIATE); + message = authMessageProcessor_->CreateDeviceAuthMessage(MSG_TYPE_RESP_AUTH_DEVICE_NEGOTIATE, data, dataLen); + } + if (softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message) != DM_OK) { + LOGE("SoftbusSession send data failed."); + return false; + } + return true; +} + +void DmAuthManager::AuthDeviceFinish(int64_t requestId) +{ + LOGI("DmAuthManager::AuthDeviceFinish start."); + if (requestId != authResponseContext_->requestId) { + LOGE("DmAuthManager::onTransmit requestId %lld is error.", requestId); + return; + } + timer_->DeleteTimer(std::string(AUTH_DEVICE_TIMEOUT_TASK)); + if (authRequestState_ != nullptr && authResponseState_ == nullptr) { + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_CANCEL_PIN_CODE_INPUT); + if (authResponseContext_->isOnline) { + LOGI("The device is online."); + if (authResponseContext_->bindLevel == DM_APP && !authResponseContext_->isIdenticalAccount) { + softbusConnector_->SetPkgName(authResponseContext_->hostPkgName); + } + softbusConnector_->HandleDeviceOnline(authRequestContext_->deviceId); + } + if (authResponseContext_->haveCredential) { + softbusConnector_->JoinLnn(authRequestContext_->deviceId); + timer_->DeleteTimer(std::string(AUTHENTICATE_TIMEOUT_TASK)); + authRequestState_->TransitionTo(std::make_shared()); + } else if (!authResponseContext_->haveCredential) { + authRequestState_->TransitionTo(std::make_shared()); + } + } else if (authRequestState_ == nullptr && authResponseState_ != nullptr) { + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_CANCEL_PIN_CODE_SHOW); + if (authResponseContext_->isOnline) { + LOGI("The device is online."); + if (authResponseContext_->bindLevel == DM_APP && !authResponseContext_->isIdenticalAccount) { + softbusConnector_->SetPkgName(authResponseContext_->hostPkgName); + } + softbusConnector_->HandleDeviceOnline(authResponseContext_->deviceId); + } + } +} + +void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) +{ + LOGI("AuthDeviceError start."); + if (authRequestState_ != nullptr && authResponseState_ == nullptr) { + authTimes_++; + timer_->DeleteTimer(std::string(AUTH_DEVICE_TIMEOUT_TASK)); + if (errorCode != DM_OK || requestId != authResponseContext_->requestId) { + if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { + authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; + authRequestContext_->reason = ERR_DM_INPUT_PARA_INVALID; + authRequestState_->TransitionTo(std::make_shared()); + } else { + timer_->StartTimer(std::string(INPUT_TIMEOUT_TASK), INPUT_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); + } + } + } +} + +void DmAuthManager::AuthDeviceSessionKey(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) +{ + LOGI("DmAuthManager::AuthDeviceSessionKey start."); + if (requestId != authResponseContext_->requestId) { + LOGE("DmAuthManager::onTransmit requestId %lld is error.", requestId); + return; + } + sessionKey_ = sessionKey; + sessionKeyLen_ = sessionKeyLen; +} + +void DmAuthManager::GetRemoteDeviceId(std::string &deviceId) +{ + LOGI("GetRemoteDeviceId start."); + deviceId = authResponseContext_->localDeviceId; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp new file mode 100644 index 000000000..06780ba7b --- /dev/null +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -0,0 +1,305 @@ +/* + * Copyright (c) 2023 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 "hichain_auth_connector.h" + +#include "dm_log.h" +#include "dm_anonymous.h" +#include "hichain_connector_callback.h" +#include "parameter.h" + +namespace OHOS { +namespace DistributedHardware { + +const int32_t P2P_BIND = 0 +std::shared_ptr HiChainAuthConnector::dmDeviceAuthCallback_ = nullptr; + +int32_t HiChainAuthConnector::StartAuthDevice(int64_t requestId, + const char* authParams, const DeviceAuthCallback* callbak) +{ + LOGI("StartAuthDevice mock."); + (void)requestId; + (void)authParams; + (void)callbak; + return DM_OK; +} +int32_t HiChainAuthConnector::ProcessAuthDevice(int64_t requestId, + const char* authParams, const DeviceAuthCallback* callbak) +{ + LOGI("ProcessAuthDevice mock."); + (void)requestId; + (void)authParams; + (void)callbak; + return DM_OK; +} +int32_t ProcessCredential(int32_t operationCode, const char* requestParams, char** returnData) +{ + LOGI("ProcessCredential mock."); + (void)operationCode; + (void)requestParams; + (void)returnData; + return DM_OK; +} + +HiChainAuthConnector::HiChainAuthConnector() +{ + int32_t ret = InitDeviceAuthService(); + if (ret != HC_SUCCESS) { + LOGE("hichain InitDeviceAuthService failed, err %d.", ret); + } + deviceAuthCallback_ = {.onTransmit = HiChainAuthConnector::onTransmit, + .onSessionKeyReturned = HiChainAuthConnector::onSessionKeyReturned, + .onFinish = HiChainAuthConnector::onFinish, + .onError = HiChainAuthConnector::onError, + .onRequest = HiChainAuthConnector::onRequest}; + LOGI("hichain GetGaInstance success."); +} + +HiChainAuthConnector::~HiChainAuthConnector() +{ + DestroyDeviceAuthService(); +} + +int32_t HiChainAuthConnector::RegisterHiChainAuthCallback(std::shared_ptr callback) +{ + dmDeviceAuthCallback_ = callback; + return DM_OK; +} + +int32_t HiChainAuthConnector::AuthDevice(int32_t pinCode, int32_t osAccountId, std::string udid, int64_t requestId) +{ + LOGI("HiChainAuthConnector::AuthDevice start."); + nlohmann::json authParamJson; + authParamJson["osAccountId"] = osAccountId; + authParamJson["pinCode"] = std::to_string(pinCode); + authParamJson["acquireType"] = P2P_BIND; + char *authParam = strdup(authParamJson.dump().c_str()); + LOGI("StartAuthDevice authParam %s ,requestId %d.", authParam, requestId); + int32_t ret = StartAuthDevice(requestId, authParam, &deviceAuthCallback_); + if (ret != HC_SUCCESS) { + LOGE("Hichain authDevice failed, ret is %d.", ret); + return ERR_DM_FAILED; + } + return DM_OK; +} + +int32_t HiChainAuthConnector::ProcessAuthData(int64_t requestId, std::string authData, int32_t osAccountId) +{ + LOGI("HiChainAuthConnector::ProcessAuthData start."); + nlohmann::json jsonObject = nlohmann::json::parse(authData, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("DecodeRequestAuth jsonStr error"); + return ERR_DM_FAILED; + } + nlohmann::json jsonAuthParam; + jsonAuthParam["osAccountId"] = osAccountId; + jsonAuthParam["data"] = jsonObject; + int32_t ret = ProcessAuthDevice(requestId, authData.c_str(), &deviceAuthCallback_); + if (ret != HC_SUCCESS) { + LOGE("Hichain processData failed ret %d.", ret); + return ERR_DM_FAILED; + } + return DM_OK; +} + +bool HiChainAuthConnector::onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) +{ + LOGI("AuthDevice onTransmit, requestId %d.", requestId); + if (dmDeviceAuthCallback_ == nullptr) { + LOGE("HiChainAuthConnector::onTransmit dmDeviceAuthCallback_ is nullptr."); + return false; + } + return dmDeviceAuthCallback_->AuthDeviceTransmit(requestId, data, dataLen); +} + +char *HiChainAuthConnector::onRequest(int64_t requestId, int operationCode, const char *reqParams) +{ + LOGI("HiChainAuthConnector::onRequest start."); + (void)requestId; + (void)reqParams; + if (dmDeviceAuthCallback_ == nullptr) { + LOGE("HiChainAuthConnector::onRequest dmDeviceAuthCallback_ is nullptr."); + return nullptr; + } + nlohmann::json jsonObj; + int32_t pinCode = dmDeviceAuthCallback_->GetPinCode(); + if (pinCode == ERR_DM_AUTH_NOT_START) { + jsonObj[FIELD_CONFIRMATION] = RequestResponse::REQUEST_REJECTED; + } else { + jsonObj[FIELD_CONFIRMATION] = RequestResponse::REQUEST_ACCEPTED; + } + std::string deviceId = ""; + dmDeviceAuthCallback_->GetRemoteDeviceId(deviceId); + jsonObj[FIELD_PIN_CODE] = std::to_string(pinCode); + jsonObj[FIELD_PEER_CONN_DEVICE_ID] = deviceId; + std::string jsonStr = jsonObj.dump(); + char *buffer = strdup(jsonStr.c_str()); + return buffer; +} + +void HiChainAuthConnector::onFinish(int64_t requestId, int operationCode, const char *returnData) +{ + LOGI("HiChainAuthConnector::onFinish reqId:%lld, operation:%d, returnData %s.", + requestId, operationCode, returnData); + if (dmDeviceAuthCallback_ == nullptr) { + LOGE("HiChainAuthConnector::onFinish dmDeviceAuthCallback_ is nullptr."); + return; + } + dmDeviceAuthCallback_->AuthDeviceFinish(requestId); +} + +void HiChainAuthConnector::onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn) +{ + LOGI("HichainAuthenCallBack::onError reqId:%lld, operation:%d, errorCode:%d.", requestId, operationCode, errorCode); + (void)operationCode; + (void)errorReturn; + if (dmDeviceAuthCallback_ == nullptr) { + LOGE("HiChainAuthConnector::onError dmDeviceAuthCallback_ is nullptr."); + return; + } + dmDeviceAuthCallback_->AuthDeviceError(requestId, ERR_DM_FAILED); +} + +void HiChainAuthConnector::onSessionKeyReturned(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen) +{ + LOGI("HiChainAuthConnector::onSessionKeyReturned start."); + if (dmDeviceAuthCallback_ == nullptr) { + LOGE("HiChainAuthConnector::onSessionKeyReturned dmDeviceAuthCallback_ is nullptr."); + return; + } + dmDeviceAuthCallback_->AuthDeviceSessionKey(requestId, sessionKey, sessionKeyLen); +} + +int32_t HiChainAuthConnector::GenerateCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey) +{ + LOGI("HiChainAuthConnector::GenerateCredential start."); + nlohmann::json jsonObj; + jsonObj["osAccountId"] = osAccountId; + jsonObj["deviceId"] = localUdid; + jsonObj["acquireType"] = P2P_BIND; + jsonObj["flag"] = 1; + char *requestParam = strdup(jsonObj.dump().c_str()); + char *returnData = nullptr; + if (ProcessCredential(CRED_OP_CREATE, requestParam, &returnData) != HC_SUCCESS) { + LOGE("Hichain generate credential failed."); + return ERR_DM_FAILED; + } + std::string returnDataStr = static_cast(returnData); + nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("Decode generate return data jsonStr error."); + return ERR_DM_FAILED; + } + if (!IsInt32(jsonObject, "result") || !IsString(jsonObject, "publicKey") || + jsonObject["result"].get() != HC_SUCCESS) { + LOGE("Hichain generate public key jsonObject invalied."); + return ERR_DM_FAILED; + } + if (jsonObject["result"].get() != 0) { + LOGE("Hichain generate public key failed"); + return ERR_DM_FAILED; + } + publicKey = jsonObject["publicKey"].get(); + return DM_OK; +} + +bool HiChainAuthConnector::QueryCredential(std::string &localUdid, int32_t osAccountId) +{ + LOGI("HiChainAuthConnector::QueryCredential start."); + nlohmann::json jsonObj; + jsonObj["osAccountId"] = osAccountId; + jsonObj["deviceId"] = localUdid; + jsonObj["acquireType"] = P2P_BIND; + jsonObj["flag"] = 1; + char *requestParam = strdup(jsonObj.dump().c_str()); + char *returnData = nullptr; + if (ProcessCredential(CRED_OP_QUERY, requestParam, &returnData) != HC_SUCCESS) { + LOGE("Hichain query credential failed."); + return false; + } + std::string returnDataStr = static_cast(returnData); + nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("Decode query return data jsonStr error."); + return false; + } + if (!IsInt32(jsonObject, "result") || jsonObject["result"].get() == -1) { + LOGE("Hichain generate public key failed."); + return false; + } + if (!IsString(jsonObject, "publicKey") || jsonObject["result"].get() == 1) { + LOGI("Credential not exist."); + return false; + } + return true; +} + +int32_t HiChainAuthConnector::ImportCredential(int32_t osAccountId, std::string deviceId, std::string publicKey) +{ + LOGI("HiChainAuthConnector::ImportCredential"); + nlohmann::json jsonObj; + jsonObj["osAccountId"] = osAccountId; + jsonObj["deviceId"] = deviceId; + jsonObj["acquireType"] = P2P_BIND; + jsonObj["publicKey"] = publicKey; + char *requestParam = strdup(jsonObj.dump().c_str()); + char *returnData = nullptr; + if (ProcessCredential(CRED_OP_IMPORT, requestParam, &returnData) != HC_SUCCESS) { + LOGE("Hichain query credential failed."); + return ERR_DM_FAILED; + } + std::string returnDataStr = static_cast(returnData); + nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("Decode import return data jsonStr error."); + return ERR_DM_FAILED; + } + if (!IsInt32(jsonObject, "result")) { + LOGI("Hichain import public key jsonObject invalied.", jsonObject["result"].get()); + return ERR_DM_FAILED; + } + int32_t result = jsonObject["result"].get(); + if (result != 0) { + LOGE("Hichain import public key result is %d.", result); + return ERR_DM_FAILED; + } + return DM_OK; +} + +int32_t HiChainAuthConnector::DeleteCredential(std::string &deviceId) +{ + LOGI("DeleteCredential start."); + nlohmann::json jsonObj; + jsonObj["deviceId"] = deviceId; + jsonObj["acquireType"] = P2P_BIND; + char *requestParam = strdup(jsonObj.dump().c_str()); + char *returnData = nullptr; + if (ProcessCredential(CRED_OP_DELETE, requestParam, &returnData) != HC_SUCCESS) { + LOGE("Hichain query credential failed."); + return false; + } + std::string returnDataStr = static_cast(returnData); + nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("Decode import return data jsonStr error."); + return false; + } + if (!IsInt32(jsonObject, "result")) { + LOGI("Hichain delete credential result json key is invalied."); + return ERR_DM_FAILED; + } + return jsonObject["result"].get(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/implementation/src/dependency/softbus/softbus_connector.cpp b/services/implementation/src/dependency/softbus/softbus_connector.cpp index 7e525ea17..00d0eda21 100644 --- a/services/implementation/src/dependency/softbus/softbus_connector.cpp +++ b/services/implementation/src/dependency/softbus/softbus_connector.cpp @@ -47,10 +47,12 @@ std::map> SoftbusConnect std::map> SoftbusConnector::publishCallbackMap_ = {}; std::queue SoftbusConnector::discoveryDeviceIdQueue_ = {}; std::unordered_map SoftbusConnector::deviceUdidMap_ = {}; +std::vector SoftbusConnector::pkgNameVec_ = {}; std::mutex SoftbusConnector::discoveryCallbackMutex_; std::mutex SoftbusConnector::discoveryDeviceInfoMutex_; std::mutex SoftbusConnector::stateCallbackMutex_; std::mutex SoftbusConnector::deviceUdidLocks_; +std::mutex SoftbusConnector::pkgNameVecMutex_; IPublishCb SoftbusConnector::softbusPublishCallback_ = { .OnPublishResult = SoftbusConnector::OnSoftbusPublishResult, @@ -635,5 +637,47 @@ int32_t SoftbusConnector::AddMemberToDiscoverMap(const std::string &deviceId, st return DM_OK; } + +std::string SoftbusConnector::GetNetworkIdByUdidHash(const std::string &deviceIdHash) +{ + LOGI("Check the device is online."); + int32_t deviceCount = 0; + NodeBasicInfo *nodeInfo = nullptr; + if (GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &deviceCount) != DM_OK) { + LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed."); + return ""; + } + for (int32_t i = 0; i < deviceCount; ++i) { + NodeBasicInfo *nodeBasicInfo = nodeInfo + i; + uint8_t mUdid[UDID_BUF_LEN] = {0}; + if (GetNodeKeyInfo(DM_PKG_NAME, reinterpret_cast(nodeBasicInfo->networkId), + NodeDeviceInfoKey::NODE_KEY_UDID, mUdid, sizeof(mUdid)) != DM_OK) { + LOGE("[SOFTBUS]GetNodeKeyInfo failed."); + } + std::string udid = reinterpret_cast(mUdid); + char mUdidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + if (DmSoftbusAdapterCrypto::GetUdidHash(udid, (uint8_t *)mUdidHash) != DM_OK) { + LOGE("get mUdidHash by udid: %s failed.", GetAnonyString(udid).c_str()); + } + std::string udidHash = static_cast(mUdidHash); + if (udidHash == deviceIdHash) { + return static_cast(nodeBasicInfo->networkId); + } + } + return ""; +} + +void SoftbusConnector::SetPkgName(std::string pkgName) +{ + LOGI("Set online device report pkgName"); + std::lock_guard lock(pkgNameVecMutex_); + pkgNameVec_ .push_back(pkgName); +} + +void SoftbusConnector::HandleDeviceOnline(std::string &deviceId) +{ + LOGI("SoftbusConnector::HandleDeviceOnline"); + return; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index cf75a5739..dca85b73f 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -186,5 +186,11 @@ void SoftbusSession::decrypt(char *cipherText, unsigned int cipherTextLen, char realCipherTextLen); plainText[realCipherTextLen - TAG_LEN] = '\0'; } + +int32_t SoftbusSession::OpenUnbindSession(const std::string &netWorkId) +{ + LOGI("SoftbusSession::OpenUnbindSession"); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee From 292fb4bd7ea87ce73e1b86f6fd20c67c42bbeeec Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 25 Nov 2023 20:23:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=BA=A7=E7=BB=91?= =?UTF-8?q?=E5=AE=9Adp=E6=9F=A5=E8=AF=A2=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../hichain/hichain_auth_connector.h | 2 +- .../src/authentication/dm_auth_manager.cpp | 32 ++++++++++--------- .../hichain/hichain_auth_connector.cpp | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/services/implementation/include/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h index 84d660f8b..1e4f5d554 100644 --- a/services/implementation/include/dependency/hichain/hichain_auth_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_auth_connector.h @@ -49,7 +49,7 @@ public: public: HiChainAuthConnector(); ~HiChainAuthConnector(); - int32_t AuthDevice(int32_t pinCode, int32_t osAccountId, std::string udid, int64_t requestId); + int32_t AuthDevice(int32_t pinCode, int32_t osAccountId, std::string udid, int64_t requestId); int32_t ProcessAuthData(int64_t requestId, std::string authData, int32_t osAccountId); int32_t GenerateCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey); bool QueryCredential(std::string &localUdid, int32_t osAccountId); diff --git a/services/implementation/src/authentication/dm_auth_manager.cpp b/services/implementation/src/authentication/dm_auth_manager.cpp index b4f7b0868..03848fd66 100644 --- a/services/implementation/src/authentication/dm_auth_manager.cpp +++ b/services/implementation/src/authentication/dm_auth_manager.cpp @@ -1508,21 +1508,23 @@ void DmAuthManager::AuthDeviceFinish(int64_t requestId) void DmAuthManager::AuthDeviceError(int64_t requestId, int32_t errorCode) { LOGI("AuthDeviceError start."); - if (authRequestState_ != nullptr && authResponseState_ == nullptr) { - authTimes_++; - timer_->DeleteTimer(std::string(AUTH_DEVICE_TIMEOUT_TASK)); - if (errorCode != DM_OK || requestId != authResponseContext_->requestId) { - if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { - authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; - authRequestContext_->reason = ERR_DM_INPUT_PARA_INVALID; - authRequestState_->TransitionTo(std::make_shared()); - } else { - timer_->StartTimer(std::string(INPUT_TIMEOUT_TASK), INPUT_TIMEOUT, - [this] (std::string name) { - DmAuthManager::HandleAuthenticateTimeout(name); - }); - authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); - } + if (authRequestState_ == nullptr || authResponseState_ != nullptr) { + LOGD("AuthDeviceError sink return."); + return; + } + authTimes_++; + timer_->DeleteTimer(std::string(AUTH_DEVICE_TIMEOUT_TASK)); + if (errorCode != DM_OK || requestId != authResponseContext_->requestId) { + if (authRequestState_ != nullptr && authTimes_ >= MAX_AUTH_TIMES) { + authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; + authRequestContext_->reason = ERR_DM_INPUT_PARA_INVALID; + authRequestState_->TransitionTo(std::make_shared()); + } else { + timer_->StartTimer(std::string(INPUT_TIMEOUT_TASK), INPUT_TIMEOUT, + [this] (std::string name) { + DmAuthManager::HandleAuthenticateTimeout(name); + }); + authUiStateMgr_->UpdateUiState(DmUiStateMsg::MSG_PIN_CODE_ERROR); } } } diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 06780ba7b..333fe9159 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -22,7 +22,7 @@ namespace OHOS { namespace DistributedHardware { -const int32_t P2P_BIND = 0 +const int32_t P2P_BIND = 0; std::shared_ptr HiChainAuthConnector::dmDeviceAuthCallback_ = nullptr; int32_t HiChainAuthConnector::StartAuthDevice(int64_t requestId, -- Gitee From 2bd24f487ac29402c986403388dcde2246403215 Mon Sep 17 00:00:00 2001 From: yangwei_814916 Date: Sat, 25 Nov 2023 21:43:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=BA=A7=E7=BB=91?= =?UTF-8?q?=E5=AE=9Adp=E6=9F=A5=E8=AF=A2=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangwei_814916 --- .../src/dependency/hichain/hichain_auth_connector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 333fe9159..0b6bede47 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -43,7 +43,7 @@ int32_t HiChainAuthConnector::ProcessAuthDevice(int64_t requestId, (void)callbak; return DM_OK; } -int32_t ProcessCredential(int32_t operationCode, const char* requestParams, char** returnData) +int32_t HiChainAuthConnector::ProcessCredential(int32_t operationCode, const char* requestParams, char** returnData) { LOGI("ProcessCredential mock."); (void)operationCode; -- Gitee