From 12715df0c07ef0dd10ea0d0809fc82d947abc768 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Fri, 1 Jul 2022 12:39:55 +0800 Subject: [PATCH 01/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- common/include/dm_constants.h | 23 +- common/include/ipc/ipc_def.h | 6 + .../ipc/model/ipc_notify_credential_req.h | 74 +++++ .../ipc/model/ipc_set_credential_req.h | 52 ++++ .../ipc/model/ipc_set_credential_rsp.h | 55 ++++ .../native_cpp/include/device_manager.h | 32 +++ .../include/device_manager_callback.h | 8 + .../native_cpp/include/device_manager_impl.h | 32 +++ .../include/notify/device_manager_notify.h | 4 + .../native_cpp/src/device_manager_impl.cpp | 146 ++++++++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 125 ++++++++ .../src/notify/device_manager_notify.cpp | 26 ++ services/implementation/BUILD.gn | 3 + .../credential/dm_credential_manager.h | 109 +++++++ .../dependency/hichain/hichain_connector.h | 46 ++- .../hichain/hichain_connector_callback.h | 5 + .../include/device_manager_service_impl.h | 12 + .../src/credential/dm_credential_manager.cpp | 271 ++++++++++++++++++ .../dependency/hichain/hichain_connector.cpp | 232 ++++++++++++++- .../src/device_manager_service_impl.cpp | 75 +++++ services/service/BUILD.gn | 7 +- .../service/include/device_manager_service.h | 13 +- .../include/device_manager_service_listener.h | 1 + .../include/idevice_manager_service_impl.h | 31 ++ .../idevice_manager_service_listener.h | 7 + .../service/src/device_manager_service.cpp | 45 +++ .../src/device_manager_service_listener.cpp | 14 + .../src/ipc/standard/ipc_cmd_parser.cpp | 108 +++++++ test/unittest/mock/device_auth.h | 2 +- utils/include/dm_hash.h | 1 + utils/src/dm_hash.cpp | 32 +++ utils/src/ipc/standard/ipc_cmd_register.cpp | 2 +- 32 files changed, 1584 insertions(+), 15 deletions(-) create mode 100644 common/include/ipc/model/ipc_notify_credential_req.h create mode 100644 common/include/ipc/model/ipc_set_credential_req.h create mode 100644 common/include/ipc/model/ipc_set_credential_rsp.h create mode 100644 services/implementation/include/credential/dm_credential_manager.h create mode 100644 services/implementation/src/credential/dm_credential_manager.cpp diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index fb711dd5b..72ee5cbc9 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -79,6 +79,7 @@ enum { ERR_DM_AUTH_MESSAGE_INCOMPLETE = -20025, ERR_DM_CREATE_GROUP_FAILED = -20026, ERR_DM_IPC_READ_FAILED = -20027, + ERR_DM_ENCRYPT_FAILED = -20028, }; const std::string TARGET_PKG_NAME_KEY = "targetPkgName"; @@ -147,7 +148,10 @@ const int32_t AUTH_TYPE_TOUCH = 3; const int32_t DEFAULT_PIN_CODE = 0; const int32_t DEFAULT_PIN_TOKEN = 0; const int32_t DEFAULT_PIN_CODE_LENGTH = 6; - +const int32_t LOCAL_CREDENTIAL_DEAL_TYPE = 1; +const int32_t REMOTE_CREDENTIAL_DEAL_TYPE = 2; +const int32_t NONSYMMETRY_CREDENTIAL_TYPE = 2; +const int32_t SYMMETRY_CREDENTIAL_TYPE = 1; // Softbus const int32_t SOFTBUS_CHECK_INTERVAL = 100000; // 100ms const uint32_t SOFTBUS_SUBSCRIBE_ID_PREFIX_LEN = 16; @@ -206,6 +210,23 @@ const int32_t MAX_EVENT_NUMBER = 10; const int32_t EXPAND_TWICE = 2; const int32_t SEC_TO_MM = 1000; const int32_t MAX_EVENTS = 5; + +// credential +const std::string FIELD_CREDENTIAL = "credential"; +const std::string FIELD_CREDENTIAL_VERSION = "version"; +const std::string FIELD_DEVICE_PK = "devicePk"; +const std::string FIELD_SERVER_PK = "serverPk"; +const std::string FIELD_PKINFO_SIGNATURE = "pkInfoSignature"; +const std::string FIELD_PKINFO = "pkInfo"; +const std::string FIELD_PROCESS_TYPE = "processType"; +const std::string FIELD_AUTH_TYPE = "authType"; +const std::string FIELD_CREDENTIAL_DATA = "credentialData"; +const std::string FIELD_CREDENTIAL_ID = "credentialId"; +const int32_t SAME_ACCOUNT_TYPE = 1; +const int32_t CROSS_ACCOUNT_TYPE = 2; +const int32_t PIN_CODE_NETWORK = 0; +const int32_t CREDENTIAL_NETWORK = 1; +const int32_t DELAY_TIME_MS = 10000; // 10ms } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H diff --git a/common/include/ipc/ipc_def.h b/common/include/ipc/ipc_def.h index 226692d10..cc2ad5bff 100644 --- a/common/include/ipc/ipc_def.h +++ b/common/include/ipc/ipc_def.h @@ -54,8 +54,14 @@ enum IpcCmdID { SERVER_GET_DMFA_INFO, SERVER_USER_AUTH_OPERATION, SERVER_DEVICE_FA_NOTIFY, + SERVER_CREDENTIAL_RESULT, REGISTER_DEV_STATE_CALLBACK, UNREGISTER_DEV_STATE_CALLBACK, + REQUEST_CREDENTIAL, + IMPORT_CREDENTIAL, + DELETE_CREDENTIAL, + REGISTER_CREDENTIAL_CALLBACK, + UNREGISTER_CREDENTIAL_CALLBACK, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_notify_credential_req.h b/common/include/ipc/model/ipc_notify_credential_req.h new file mode 100644 index 000000000..df388d4c7 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_credential_req.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022 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_IPC_NOTIFY_CREDENTIAL_REQ_H +#define OHOS_DM_IPC_NOTIFY_CREDENTIAL_REQ_H + +#include "dm_device_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyCredentialReq : public IpcReq { + DECLARE_IPC_MODEL(IpcNotifyCredentialReq); + +public: + /** + * @tc.name: IpcNotifyCredentialReq::SetCredentialAction + * @tc.desc: Set CredentialAction of the Ipc Notify Credential Action Request + * @tc.type: FUNC + */ + void SetCredentialAction(const int32_t action) + { + credentialAction_ = action; + } + + /** + * @tc.name: IpcNotifyCredentialReq::SetCredentialResult + * @tc.desc: Set CredentialAction of the Ipc Notify Credential Result Request + * @tc.type: FUNC + */ + void SetCredentialResult(const std::string &resultInfo) + { + credentialResult_ = resultInfo; + } + + /** + * @tc.name: IpcNotifyCredentialReq::GetCredentialAction + * @tc.desc: Get CredentialAction of the Ipc Notify Credential Action Request + * @tc.type: FUNC + */ + int32_t GetCredentialAction() + { + return credentialAction_; + } + + /** + * @tc.name: IpcNotifyCredentialReq::GetCredentialResult + * @tc.desc: Get CredentialAction of the Ipc Notify Credential Result Request + * @tc.type: FUNC + */ + const std::string &GetCredentialResult() + { + return credentialResult_; + } + +private: + int32_t credentialAction_; + std::string credentialResult_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_NOTIFY_CREDENTIAL_REQ_H diff --git a/common/include/ipc/model/ipc_set_credential_req.h b/common/include/ipc/model/ipc_set_credential_req.h new file mode 100644 index 000000000..1709efcd1 --- /dev/null +++ b/common/include/ipc/model/ipc_set_credential_req.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 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_IPC_SET_CREDENTIAL_REQ_H +#define OHOS_DM_IPC_SET_CREDENTIAL_REQ_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcSetCredentialReq : public IpcReq { + DECLARE_IPC_MODEL(IpcSetCredentialReq); + +public: + /** + * @tc.name: IpcSetCredentialReq::GetCredentialParam + * @tc.desc: Ipc Set Credential Request Get CredentialParam + * @tc.type: FUNC + */ + const std::string &GetCredentialParam() const + { + return credentialParam_; + } + + /** + * @tc.name: IpcSetCredentialReq::SetCredentialParam + * @tc.desc: Ipc Set Credential Request Set CredentialParam + * @tc.type: FUNC + */ + void SetCredentialParam(const std::string &credentialParam) + { + credentialParam_ = credentialParam; + } + +private: + std::string credentialParam_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_SET_CREDENTIAL_REQ_H diff --git a/common/include/ipc/model/ipc_set_credential_rsp.h b/common/include/ipc/model/ipc_set_credential_rsp.h new file mode 100644 index 000000000..a490ff005 --- /dev/null +++ b/common/include/ipc/model/ipc_set_credential_rsp.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 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_IPC_SET_CREDENTIAL_RSP_H +#define OHOS_DM_IPC_SET_CREDENTIAL_RSP_H + +#include + +#include "dm_device_info.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcSetCredentialRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcSetCredentialRsp); + +public: + /** + * @tc.name: IpcSetCredentialRsp::GetCredentialResult + * @tc.desc: Ipc Set Credential Response Get CredentialResult + * @tc.type: FUNC + */ + const std::string &GetCredentialResult() const + { + return credentialResult_; + } + + /** + * @tc.name: IpcSetCredentialRsp::SetCredentialResult + * @tc.desc: Ipc Set Credential Response Set CredentialResult + * @tc.type: FUNC + */ + void SetCredentialResult(const std::string &credentialResult) + { + credentialResult_ = credentialResult; + } + +private: + std::string credentialResult_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_SET_CREDENTIAL_RSP_H diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 00274b1c4..0794ca5bd 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -151,6 +151,38 @@ public: * @tc.type: FUNC */ virtual int32_t UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra) = 0; + /** + * @tc.name: DeviceManagerImpl::RequestCredential + * @tc.desc: RequestCredential + * @tc.type: FUNC + */ + virtual int32_t RequestCredential(const std::string &pkgName, const std::string &reqJsonStr, + std::string &returnJsonStr) = 0; + /** + * @tc.name: DeviceManagerImpl::ImportCredential + * @tc.desc: ImportCredential + * @tc.type: FUNC + */ + virtual int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo) = 0; + /** + * @tc.name: DeviceManagerImpl::DeleteCredential + * @tc.desc: DeleteCredential + * @tc.type: FUNC + */ + virtual int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) = 0; + /** + * @tc.name: DeviceManagerImpl::RegisterCredentialCallback + * @tc.desc: RegisterCredentialCallback + * @tc.type: FUNC + */ + virtual int32_t RegisterCredentialCallback(const std::string &pkgName, + std::shared_ptr callback) = 0; + /** + * @tc.name: DeviceManagerImpl::UnRegisterCredentialCallback + * @tc.desc: UnRegisterCredentialCallback + * @tc.type: FUNC + */ + virtual int32_t UnRegisterCredentialCallback(const std::string &pkgName) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h index f92ceebf5..e47a2578e 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h @@ -75,6 +75,14 @@ public: } virtual void OnCall(const std::string ¶mJson) = 0; }; + +class CredentialCallback { +public: + virtual ~CredentialCallback() + { + } + virtual void OnCredentialResult(int32_t &action, const std::string &credentialResult) = 0; +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CALLBACK_H diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 3cd7e598f..cfe1a1b4b 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -154,6 +154,38 @@ public: * @tc.type: FUNC */ virtual int32_t UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra) override; + /** + * @tc.name: DeviceManagerImpl::RequestCredential + * @tc.desc: RequestCredential + * @tc.type: FUNC + */ + virtual int32_t RequestCredential(const std::string &pkgName, const std::string &reqJsonStr, + std::string &returnJsonStr) override; + /** + * @tc.name: DeviceManagerImpl::ImportCredential + * @tc.desc: ImportCredential + * @tc.type: FUNC + */ + virtual int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo) override; + /** + * @tc.name: DeviceManagerImpl::DeleteCredential + * @tc.desc: DeleteCredential + * @tc.type: FUNC + */ + virtual int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) override; + /** + * @tc.name: DeviceManagerImpl::RegisterCredentialCallback + * @tc.desc: RegisterCredentialCallback + * @tc.type: FUNC + */ + virtual int32_t RegisterCredentialCallback(const std::string &pkgName, + std::shared_ptr callback) override; + /** + * @tc.name: DeviceManagerImpl::UnRegisterCredentialCallback + * @tc.desc: UnRegisterCredentialCallback + * @tc.type: FUNC + */ + virtual int32_t UnRegisterCredentialCallback(const std::string &pkgName) override; private: DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 5726050ab..54b2a88c0 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -49,6 +49,8 @@ public: void UnRegisterVerifyAuthenticationCallback(const std::string &pkgName); void RegisterDeviceManagerFaCallback(const std::string &pkgName, std::shared_ptr callback); void UnRegisterDeviceManagerFaCallback(const std::string &pkgName); + void RegisterCredentialCallback(const std::string &pkgName, std::shared_ptr callback); + void UnRegisterCredentialCallback(const std::string &pkgName); public: void OnRemoteDied(); @@ -63,6 +65,7 @@ public: uint32_t status, uint32_t reason); void OnVerifyAuthResult(const std::string &pkgName, const std::string &deviceId, int32_t resultCode, int32_t flag); void OnFaCall(std::string &pkgName, std::string ¶mJson); + void OnCredentialResult(const std::string &pkgName, int32_t &action, const std::string &credentialResult); private: #if !defined(__LITEOS_M__) @@ -74,6 +77,7 @@ private: std::map> verifyAuthCallback_; std::map> dmInitCallback_; std::map> dmFaCallback_; + std::map> credentialCallback_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index f2eb0278b..b5bb6ebf7 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -31,12 +31,15 @@ #include "ipc_get_trustdevice_rsp.h" #include "ipc_req.h" #include "ipc_rsp.h" +#include "ipc_set_credential_req.h" +#include "ipc_set_credential_rsp.h" #include "ipc_set_useroperation_req.h" #include "ipc_start_discovery_req.h" #include "ipc_stop_discovery_req.h" #include "ipc_unauthenticate_device_req.h" #include "ipc_verify_authenticate_req.h" #include "ipc_register_dev_state_callback_req.h" +#include "ipc_register_credential_callback_req.h" #include "securec.h" namespace OHOS { @@ -522,5 +525,148 @@ int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName } return DM_OK; } + +int32_t DeviceManagerImpl::RequestCredential(const std::string &pkgName, const std::string &reqJsonStr, + std::string &returnJsonStr) +{ + if (pkgName.empty()) { + LOGE("request credential failed, pkgName is empty."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (reqJsonStr.empty()) { + LOGE("request credential reqJsonStr is empty."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + + LOGI("start to RequestCredential."); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetCredentialParam(reqJsonStr); + if (ipcClientProxy_->SendRequest(REQUEST_CREDENTIAL, req, rsp) != DM_OK) { + LOGE("failed to send request while request credential."); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("failed to get return errcode while request credential."); + return ret; + } + returnJsonStr = rsp->GetCredentialResult(); + LOGI("request device credential completed."); + return DM_OK; +} + +int32_t DeviceManagerImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo) +{ + if (pkgName.empty()) { + LOGE("import credential failed, pkgName is empty."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialInfo.empty()) { + LOGE("the imported credential credentialInfo is empty."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + + LOGI("start to ImportCredential."); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetCredentialParam(credentialInfo); + if (ipcClientProxy_->SendRequest(IMPORT_CREDENTIAL, req, rsp) != DM_OK) { + LOGE("failed to request while import credential."); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("failed to get return errcode while import credential."); + return ret; + } + LOGI("import credential to device completed."); + return DM_OK; +} + +int32_t DeviceManagerImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) +{ + if (pkgName.empty()) { + LOGE("delete credential failed, pkgName is empty."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (deleteInfo.empty()) { + LOGE("the deleted credential deleteInfo is empty."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + + LOGI("start to DeleteCredential."); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetCredentialParam(deleteInfo); + if (ipcClientProxy_->SendRequest(DELETE_CREDENTIAL, req, rsp) != DM_OK) { + LOGE("failed to request while delete credential."); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("failed to get return errcode while import credential."); + return ret; + } + LOGI("delete credential from device completed."); + return DM_OK; +} + +int32_t DeviceManagerImpl::RegisterCredentialCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + if (pkgName.empty() || callback == nullptr) { + LOGE("RegisterCredentialCallback error: Invalid para"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + + LOGI("DeviceManager::RegisterCredentialCallback start, pkgName: %s", pkgName.c_str()); + DeviceManagerNotify::GetInstance().RegisterCredentialCallback(pkgName, callback); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + int32_t ret = ipcClientProxy_->SendRequest(REGISTER_CREDENTIAL_CALLBACK, req, rsp); + if (ret != DM_OK) { + LOGE("RegisterCredentialCallback error: Send Request failed ret: %d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("RegisterCredentialCallback error: Failed with ret %d", ret); + return ret; + } + LOGI("RegisterCredentialCallback completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} + +int32_t DeviceManagerImpl::UnRegisterCredentialCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("UnRegisterCredentialCallback error: Invalid para"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + + LOGI("DeviceManager::UnRegisterCredentialCallback start, pkgName: %s", pkgName.c_str()); + DeviceManagerNotify::GetInstance().UnRegisterCredentialCallback(pkgName); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + + if (ipcClientProxy_->SendRequest(UNREGISTER_CREDENTIAL_CALLBACK, req, rsp) != DM_OK) { + LOGE("UnRegisterCredentialCallback error: Send Request failed ret: %d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + int32_t ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("UnRegisterCredentialCallback Failed with ret %d", ret); + return ret; + } + LOGI("UnRegisterCredentialCallback completed, pkgName: %s", pkgName.c_str()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index 5cc2d45bf..4c7ae4c31 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -26,9 +26,12 @@ #include "ipc_get_local_device_info_rsp.h" #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" +#include "ipc_register_credential_callback_req.h" #include "ipc_register_listener_req.h" #include "ipc_req.h" #include "ipc_rsp.h" +#include "ipc_set_credential_req.h" +#include "ipc_set_credential_rsp.h" #include "ipc_set_useroperation_req.h" #include "ipc_start_discovery_req.h" #include "ipc_stop_discovery_req.h" @@ -512,5 +515,127 @@ ON_IPC_CMD(SERVER_DEVICE_FA_NOTIFY, MessageParcel &data, MessageParcel &reply) } return DM_OK; } + +ON_IPC_SET_REQUEST(REQUEST_CREDENTIAL, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string requestJsonStr = pReq->GetCredentialParam(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkg failed."); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(requestJsonStr)) { + LOGE("write requestJsonStr failed."); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REQUEST_CREDENTIAL, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + if (pRsp->GetErrCode() == DM_OK) { + std::string returnJsonStr = reply.ReadString(); + pRsp->SetCredentialResult(returnJsonStr); + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(IMPORT_CREDENTIAL, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string credentialInfo = pReq->GetCredentialParam(); + if (!data.WriteString(pkgName)) { + LOGE("write pkg failed."); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(credentialInfo)) { + LOGE("write credentialInfo failed."); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(IMPORT_CREDENTIAL, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(DELETE_CREDENTIAL, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + std::string deleteInfo = pReq->GetCredentialParam(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkg failed."); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(deleteInfo)) { + LOGE("write deleteInfo failed."); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(DELETE_CREDENTIAL, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(REGISTER_CREDENTIAL_CALLBACK, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(REGISTER_CREDENTIAL_CALLBACK, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_SET_REQUEST(UNREGISTER_CREDENTIAL_CALLBACK, std::shared_ptr pBaseReq, MessageParcel &data) +{ + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(UNREGISTER_CREDENTIAL_CALLBACK, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_CMD(SERVER_CREDENTIAL_RESULT, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + int32_t action = data.ReadInt32(); + std::string credentialResult = data.ReadString(); + + DeviceManagerNotify::GetInstance().OnCredentialResult(pkgName, action, credentialResult); + reply.WriteInt32(DM_OK); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 5081c63b5..1a44d2f1a 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -130,6 +130,19 @@ void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &p dmFaCallback_.erase(pkgName); } +void DeviceManagerNotify::RegisterCredentialCallback(const std::string &pkgName, + std::shared_ptr callback) +{ + std::lock_guard autoLock(lock_); + credentialCallback_[pkgName] = callback; +} + +void DeviceManagerNotify::UnRegisterCredentialCallback(const std::string &pkgName) +{ + std::lock_guard autoLock(lock_); + credentialCallback_.erase(pkgName); +} + void DeviceManagerNotify::OnRemoteDied() { LOGW("DeviceManager : OnRemoteDied"); @@ -285,5 +298,18 @@ void DeviceManagerNotify::OnFaCall(std::string &pkgName, std::string ¶mJson) } dmFaCallback_[pkgName]->OnCall(paramJson); } + +void DeviceManagerNotify::OnCredentialResult(const std::string &pkgName, int32_t &action, + const std::string &credentialResult) +{ + LOGI("DeviceManagerNotify::OnCredentialResult pkgName:%s, action:%d", pkgName.c_str(), action); + std::lock_guard autoLock(lock_); + if (credentialCallback_.count(pkgName) == 0) { + LOGE("DeviceManager OnCredentialResult: no register credentialCallback_ for this package"); + return; + } + credentialCallback_[pkgName]->OnCredentialResult(action, credentialResult); + credentialCallback_.erase(pkgName); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 9497bdb52..e8a3b67e8 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -31,6 +31,7 @@ if (defined(ohos_lite)) { "${servicesimpl_path}/include/authentication", "${servicesimpl_path}/include/authentication/showconfirm/lite", "${servicesimpl_path}/include/ability", + "${servicesimpl_path}/include/credential", "${servicesimpl_path}/include/devicestate", "${servicesimpl_path}/include/discovery", "${servicesimpl_path}/include/dependency/commonevent", @@ -146,6 +147,7 @@ if (defined(ohos_lite)) { "include/authentication", "include/authentication/showconfirm/standard", "include/ability", + "include/credential", "include/devicestate", "include/discovery", "include/dependency/commonevent", @@ -182,6 +184,7 @@ if (defined(ohos_lite)) { "src/authentication/dm_auth_manager.cpp", "src/authentication/showconfirm/standard/show_confirm.cpp", "src/config/dm_config_manager.cpp", + "src/credential/dm_credential_manager.cpp", "src/dependency/commonevent/dm_common_event_manager.cpp", "src/dependency/hichain/hichain_connector.cpp", "src/dependency/multipleuser/multiple_user_connector.cpp", diff --git a/services/implementation/include/credential/dm_credential_manager.h b/services/implementation/include/credential/dm_credential_manager.h new file mode 100644 index 000000000..c37e431a2 --- /dev/null +++ b/services/implementation/include/credential/dm_credential_manager.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2022 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_CREDENTIAL_MANAGER_H +#define OHOS_DM_CREDENTIAL_MANAGER_H + +#include "device_auth.h" +#include "idevice_manager_service_listener.h" +#include "dm_timer.h" +#include "hichain_connector.h" +namespace OHOS { +namespace DistributedHardware { +typedef struct { + int32_t credentialType; + std::string credentialId; + std::string serverPk; + std::string pkInfoSignature; + std::string pkInfo; + std::string authCode; + std::string peerDeviceId; +} CredentialData; +class DmCredentialManager final : public IDmCredentialCallback, + public std::enable_shared_from_this { +public: + DmCredentialManager(std::shared_ptr hiChainConnector, + std::shared_ptr listener); + ~DmCredentialManager(); + + /** + * @tc.name: HiChainConnector::RegisterCredentialCallback + * @tc.desc: Register Credential Callback Info of the DmCredential Manager + * @tc.type: FUNC + */ + void RegisterCredentialCallback(const std::string &pkgName); + + /** + * @tc.name: HiChainConnector::UnRegisterCredentialCallback + * @tc.desc: UnRegister Credential Callback Info of the DmCredential Manager + * @tc.type: FUNC + */ + void UnRegisterCredentialCallback(const std::string &pkgName); + + /** + * @tc.name: HiChainConnector::RequestCredential + * @tc.desc: Request Credential Info of the DmCredential Manager + * @tc.type: FUNC + */ + int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); + + /** + * @tc.name: HiChainConnector::ImportCredential + * @tc.desc: Import Credential Info of the DmCredential Manager + * @tc.type: FUNC + */ + int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo); + + /** + * @tc.name: HiChainConnector::ImportLocalCredential + * @tc.desc: Import Nonsymmetry Credential Info of the DmCredential Manager + * @tc.type: FUNC + */ + int32_t ImportLocalCredential(const std::string &credentialInfo); + + /** + * @tc.name: HiChainConnector::ImportRemoteCredential + * @tc.desc: Import Symmetry Credential Info of the DmCredential Manager + * @tc.type: FUNC + */ + int32_t ImportRemoteCredential(const std::string &credentialInfo); + + /** + * @tc.name: HiChainConnector::DeleteCredential + * @tc.desc: Delete Credential Info of the DmCredential Manager + * @tc.type: FUNC + */ + int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo); + + /** + * @tc.name: HiChainConnector::OnCredentialResult + * @tc.desc: Credential Result of the DmCredential Manager + * @tc.type: FUNC + */ + void OnCredentialResult(int64_t requestId, int32_t action, const std::string &resultInfo); +private: + std::shared_ptr hiChainConnector_; + std::shared_ptr listener_; + std::vector credentialVec_; + int64_t requestId_; + std::mutex locks_; + std::string pkgName_; +private: + int32_t GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData, + nlohmann::json &jsonOutObj); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_CREDENTIAL_MANAGER_H \ No newline at end of file diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index bf84287b7..83483930b 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -35,8 +35,9 @@ struct GroupInfo { std::string groupOwner; int32_t groupType; int32_t groupVisibility; + std::string userId; - GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0) + GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId(0) { } }; @@ -75,6 +76,14 @@ public: */ int32_t CreateGroup(int64_t requestId, const std::string &groupName); + /** + * @tc.name: HiChainConnector::CreateGroup + * @tc.desc: Create Group of the HiChain Connector + * @tc.type: FUNC + */ + int32_t CreateGroup(int64_t requestId, int32_t authType, const std::string &userId, + nlohmann::json &jsonOutObj); + /** * @tc.name: HiChainConnector::AddMember * @tc.desc: Add Member of the HiChain Connector @@ -103,6 +112,13 @@ public: */ int32_t DeleteGroup(const int32_t userId, std::string &groupId); + /** + * @tc.name: HiChainConnector::DeleteGroup + * @tc.desc: DeleteGroup of the HiChain Connector + * @tc.type: FUNC + */ + int32_t DeleteGroup(int64_t requestId_, const std::string &userId, const int32_t authType); + /** * @tc.name: HiChainConnector::IsDevicesInGroup * @tc.desc: IsDevicesInGroup of the HiChain Connector @@ -137,18 +153,46 @@ public: * @tc.type: FUNC */ int32_t DeleteTimeOutGroup(const char* deviceId); + + /** + * @tc.name: HiChainConnector::RegisterHiChainCallback + * @tc.desc: Register HiChain Callback of the HiChain Connector + * @tc.type: FUNC + */ + int32_t RegisterhiChainCredentialCallback(const std::shared_ptr &callback); + + /** + * @tc.name: HiChainConnector::UnRegisterHiChainCallback + * @tc.desc: Un Register HiChain Callback of the HiChain Connector + * @tc.type: FUNC + */ + int32_t UnRegisterhiChainCredentialCallback(); + + /** + * @tc.name: HiChainConnector::getRegisterInfo + * @tc.desc: Get RegisterInfo Info of the HiChain Connector + * @tc.type: FUNC + */ + int32_t getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr); + private: int64_t GenRequestId(); int32_t SyncGroups(std::string deviceId, std::vector &remoteGroupIdList); int32_t GetSyncGroupList(std::vector &groupList, std::vector &syncGroupList); std::string GetConnectPara(std::string deviceId, std::string reqDeviceId); bool IsGroupCreated(std::string groupName, GroupInfo &groupInfo); + bool IsRedundanceGroup(const std::string &userId, int32_t authType, std::vector &groupList); + void DealRedundanceGroup(const std::string &userId, int32_t authType); + void DeleteRedundanceGroup(std::string &userId); bool IsGroupInfoInvalid(GroupInfo &group); + int32_t GetStrFieldByType(const std::string &reqJsonStr, const std::string &outField, int32_t type); + int32_t GetNumsFieldByType(const std::string &reqJsonStr, int32_t &outField, int32_t type); private: const DeviceGroupManager *deviceGroupManager_ = nullptr; DeviceAuthCallback deviceAuthCallback_; static std::shared_ptr hiChainConnectorCallback_; + static std::shared_ptr hiChainCredentialCallback_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/hichain/hichain_connector_callback.h b/services/implementation/include/dependency/hichain/hichain_connector_callback.h index a5c573e2d..1ffb2b7dc 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector_callback.h +++ b/services/implementation/include/dependency/hichain/hichain_connector_callback.h @@ -25,6 +25,11 @@ public: virtual std::string GetConnectAddr(std::string deviceId) = 0; virtual int32_t GetPinCode() = 0; }; + +class IDmCredentialCallback { +public: + virtual void OnCredentialResult(int64_t requestId, int32_t action, const std::string &resultInfo) = 0; +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_HICHAIN_CONNECTOR_CALLBACK_H diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 9dccd133b..02a85970e 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -21,6 +21,7 @@ #include "dm_ability_manager.h" #include "dm_auth_manager.h" +#include "dm_credential_manager.h" #include "dm_device_info.h" #include "dm_device_state_manager.h" #include "dm_discovery_manager.h" @@ -69,6 +70,16 @@ public: void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); + int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); + + int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo); + + int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo); + + int32_t RegisterCredentialCallback(const std::string &pkgName); + + int32_t UnRegisterCredentialCallback(const std::string &pkgName); + private: std::shared_ptr authMgr_; std::shared_ptr deviceStateMgr_; @@ -76,6 +87,7 @@ private: std::shared_ptr softbusConnector_; std::shared_ptr abilityMgr_; std::shared_ptr hiChainConnector_; + std::shared_ptr credentialMgr_; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/implementation/src/credential/dm_credential_manager.cpp b/services/implementation/src/credential/dm_credential_manager.cpp new file mode 100644 index 000000000..3f9f30128 --- /dev/null +++ b/services/implementation/src/credential/dm_credential_manager.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2022 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_credential_manager.h" + +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_hash.h" +#include "dm_log.h" +#include "dm_random.h" +#include "parameter.h" + +namespace OHOS { +namespace DistributedHardware { +void from_json(const nlohmann::json &jsonObject, CredentialData &credentialData) +{ + if (!jsonObject.contains(FIELD_CREDENTIAL_TYPE) || !jsonObject.contains(FIELD_CREDENTIAL_ID) || + !jsonObject.contains(FIELD_SERVER_PK) || !jsonObject.contains(FIELD_PKINFO_SIGNATURE) || + !jsonObject.contains(FIELD_PKINFO) || !jsonObject.contains(FIELD_AUTH_CODE) || + !jsonObject.contains(FIELD_PEER_DEVICE_ID)) { + LOGE("CredentialData json key Not complete"); + return; + } + jsonObject[FIELD_CREDENTIAL_TYPE].get_to(credentialData.credentialType); + jsonObject[FIELD_CREDENTIAL_ID].get_to(credentialData.credentialId); + jsonObject[FIELD_SERVER_PK].get_to(credentialData.serverPk); + jsonObject[FIELD_PKINFO_SIGNATURE].get_to(credentialData.pkInfoSignature); + jsonObject[FIELD_PKINFO].get_to(credentialData.pkInfo); + jsonObject[FIELD_AUTH_CODE].get_to(credentialData.authCode); + jsonObject[FIELD_PEER_DEVICE_ID].get_to(credentialData.peerDeviceId); +} + +DmCredentialManager::DmCredentialManager(std::shared_ptr hiChainConnector, + std::shared_ptr listener) + : hiChainConnector_(hiChainConnector), listener_(listener) +{ + LOGI("DmCredentialManager constructor"); +} + +DmCredentialManager::~DmCredentialManager() +{ + LOGI("DmCredentialManager destructor"); +} + +int32_t DmCredentialManager::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr) +{ + LOGI("start to request credential."); + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + nlohmann::json jsonObject = nlohmann::json::parse(reqJsonStr, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("reqJsonStr string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_USER_ID) || !jsonObject.contains(FIELD_CREDENTIAL_VERSION)) { + LOGE("user id or credential version string key not exist!"); + return ERR_DM_FAILED; + } + std::string userId = jsonObject[FIELD_USER_ID]; + std::string credentialVersion = jsonObject[FIELD_CREDENTIAL_VERSION]; + nlohmann::json jsonObj; + jsonObj[FIELD_CREDENTIAL_VERSION] = credentialVersion; + jsonObj[FIELD_USER_ID] = userId; + jsonObj[FIELD_DEVICE_ID] = localDeviceId; + std::string tmpStr = jsonObj.dump(); + return hiChainConnector_->getRegisterInfo(tmpStr.c_str(), returnJsonStr); +} + +int32_t DmCredentialManager::ImportCredential(const std::string &pkgName, const std::string &credentialInfo) +{ + std::lock_guard autoLock(locks_); + std::vector::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName); + if (iter == credentialVec_.end()) { + LOGE("credentialInfo not found by pkgName %s", GetAnonyString(pkgName).c_str()); + return ERR_DM_FAILED; + } + pkgName_ = pkgName; + nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("credentialInfo string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_PROCESS_TYPE)) { + LOGE("credential type string key not exist!"); + return ERR_DM_FAILED; + } + int32_t processType = jsonObject[FIELD_PROCESS_TYPE]; + if (processType == LOCAL_CREDENTIAL_DEAL_TYPE) { + return ImportLocalCredential(credentialInfo); + } else if (processType == REMOTE_CREDENTIAL_DEAL_TYPE) { + return DM_OK; + } else { + LOGE("credential type error!"); + } + return ERR_DM_FAILED; +} + +int32_t DmCredentialManager::ImportLocalCredential(const std::string &credentialInfo) +{ + LOGI("ImportLocalCredential start"); + nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("credentialInfo string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_AUTH_TYPE) || !jsonObject.contains(FIELD_USER_ID) + || !jsonObject.contains(FIELD_CREDENTIAL_DATA)) { + LOGE("auth type or user id string key not exist!"); + return ERR_DM_FAILED; + } + int32_t authType = jsonObject[FIELD_AUTH_TYPE]; + if (authType == SAME_ACCOUNT_TYPE) { + authType = IDENTICAL_ACCOUNT_GROUP; + } + if (authType == CROSS_ACCOUNT_TYPE) { + authType = ACROSS_ACCOUNT_AUTHORIZE_GROUP; + } + std::string userId = jsonObject[FIELD_USER_ID]; + requestId_ = GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID); + nlohmann::json creArray = jsonObject[FIELD_CREDENTIAL_DATA.c_str()]; + if (!creArray.is_array()) { + LOGI("ImportLocalCredential credentialData is not a array object!"); + return ERR_DM_FAILED; + } + std::vector vecCredentialData = + jsonObject[FIELD_CREDENTIAL_DATA.c_str()].get>(); + if (vecCredentialData.size() != 1) { + LOGI("ImportLocalCredential credentialData err"); + return ERR_DM_FAILED; + } + LOGI("ImportLocalCredential get credentialData success!"); + nlohmann::json jsonOutObj; + if (GetCredentialData(credentialInfo, vecCredentialData[0], jsonOutObj) != DM_OK) { + LOGE("failed to get credentialData field from input credential."); + return ERR_DM_FAILED; + } + if (hiChainConnector_->CreateGroup(requestId_, authType, userId, jsonOutObj) != DM_OK) { + LOGE("failed to create hichain group function."); + return ERR_DM_FAILED; + } + return DM_OK; +} + +int32_t DmCredentialManager::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) +{ + std::lock_guard autoLock(locks_); + std::vector::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName); + if (iter == credentialVec_.end()) { + LOGE("credentialInfo not found by pkgName %s", GetAnonyString(pkgName).c_str()); + return ERR_DM_FAILED; + } + pkgName_ = pkgName; + nlohmann::json jsonObject = nlohmann::json::parse(deleteInfo, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("deleteInfo string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_PROCESS_TYPE) || !jsonObject.contains(FIELD_AUTH_TYPE) + || !jsonObject.contains(FIELD_USER_ID)) { + LOGE("process type string key not exist!"); + return ERR_DM_FAILED; + } + int32_t processType = jsonObject[FIELD_PROCESS_TYPE]; + int32_t authType = jsonObject[FIELD_AUTH_TYPE]; + if (authType == SAME_ACCOUNT_TYPE) { + authType = IDENTICAL_ACCOUNT_GROUP; + } + if (authType == CROSS_ACCOUNT_TYPE) { + authType = ACROSS_ACCOUNT_AUTHORIZE_GROUP; + } + std::string userId = jsonObject[FIELD_USER_ID]; + requestId_ = GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID); + if (processType == LOCAL_CREDENTIAL_DEAL_TYPE) { + return hiChainConnector_->DeleteGroup(requestId_, userId, authType); + } else if (processType == REMOTE_CREDENTIAL_DEAL_TYPE) { + return DM_OK; + } else { + LOGE("credential type error!"); + } + return DM_OK; +} + +void DmCredentialManager::OnCredentialResult(int64_t requestId, int32_t action, + const std::string &resultInfo) +{ + LOGI("DmCredentialManager::OnImportResult"); + if (requestId_ != requestId) { + return; + } + listener_->OnCredentialResult(pkgName_, action, resultInfo); +} + +void DmCredentialManager::RegisterCredentialCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("DmCredentialManager::RegisterCredentialCallback input param is empty"); + return; + } + LOGI("DmCredentialManager::RegisterCredentialCallback pkgName=%s", + GetAnonyString(pkgName).c_str()); + credentialVec_.push_back(pkgName); + hiChainConnector_->RegisterhiChainCredentialCallback(std::shared_ptr(shared_from_this())); +} + +void DmCredentialManager::UnRegisterCredentialCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("DmCredentialManager::UnRegisterCredentialStateCallback input param is empty"); + return; + } + LOGI("DmCredentialManager::UnRegisterCredentialStateCallback pkgName=%s", + GetAnonyString(pkgName).c_str()); + std::vector::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName); + if (iter != credentialVec_.end()) { + credentialVec_.erase(iter); + } + hiChainConnector_->UnRegisterhiChainCredentialCallback(); +} + +int32_t DmCredentialManager::GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData, + nlohmann::json &jsonOutObj) +{ + nlohmann::json jsonCreObj; + jsonCreObj[FIELD_CREDENTIAL_TYPE] = inputCreData.credentialType; + int32_t credentialType = inputCreData.credentialType; + if (credentialType == NONSYMMETRY_CREDENTIAL_TYPE) { + nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false); + if (jsonObject.is_discarded()) { + LOGE("credentialInfo string not a json type."); + return ERR_DM_FAILED; + } + if (!jsonObject.contains(FIELD_USER_ID) || !jsonObject.contains(FIELD_CREDENTIAL_VERSION) + || !jsonObject.contains(FIELD_DEVICE_ID) || !jsonObject.contains(FIELD_DEVICE_PK)) { + LOGE("auth type or user id string key not exist!"); + return ERR_DM_FAILED; + } + std::string userId = jsonObject[FIELD_USER_ID]; + std::string deviceId = jsonObject[FIELD_DEVICE_ID]; + std::string verSion = jsonObject[FIELD_CREDENTIAL_VERSION]; + std::string devicePk = jsonObject[FIELD_DEVICE_PK]; + nlohmann::json jsonPkInfo; + jsonPkInfo[FIELD_USER_ID] = userId; + jsonPkInfo[FIELD_DEVICE_ID] = deviceId; + jsonPkInfo[FIELD_CREDENTIAL_VERSION] = verSion; + jsonPkInfo[FIELD_DEVICE_PK] = devicePk; + jsonCreObj[FIELD_PKINFO] = jsonPkInfo; + jsonCreObj[FIELD_SERVER_PK] = inputCreData.serverPk; + jsonCreObj[FIELD_PKINFO_SIGNATURE] = inputCreData.pkInfoSignature; + } else if (credentialType == SYMMETRY_CREDENTIAL_TYPE) { + jsonCreObj[FIELD_AUTH_CODE] = inputCreData.authCode; + } else { + LOGE("invalid credentialType field!"); + return ERR_DM_FAILED; + } + jsonOutObj = jsonCreObj; + return DM_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index 7e5b908d2..9efe475c0 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -54,9 +54,18 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) if (jsonObject.find(FIELD_GROUP_VISIBILITY) != jsonObject.end()) { groupInfo.groupVisibility = jsonObject.at(FIELD_GROUP_VISIBILITY).get(); } + + if (jsonObject.find(FIELD_USER_ID) != jsonObject.end()) { + groupInfo.userId = jsonObject.at(FIELD_USER_ID).get(); + } } std::shared_ptr HiChainConnector::hiChainConnectorCallback_ = nullptr; +std::shared_ptr HiChainConnector::hiChainCredentialCallback_ = nullptr; +bool g_createGroupFlag = false; +bool g_deleteGroupFlag = false; +bool g_groupIsRedundance = false; +int32_t g_networkStyle = PIN_CODE_NETWORK; HiChainConnector::HiChainConnector() { @@ -99,6 +108,7 @@ int32_t HiChainConnector::CreateGroup(int64_t requestId, const std::string &grou LOGE("HiChainConnector::CreateGroup group manager is null, requestId %lld.", requestId); return ERR_DM_INPUT_PARAMETER_EMPTY; } + g_networkStyle = PIN_CODE_NETWORK; GroupInfo groupInfo; if (IsGroupCreated(groupName, groupInfo)) { DeleteGroup(groupInfo.groupId); @@ -141,6 +151,28 @@ bool HiChainConnector::IsGroupCreated(std::string groupName, GroupInfo &groupInf return false; } +bool HiChainConnector::IsRedundanceGroup(const std::string &userId, int32_t authType, std::vector &groupList) +{ + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_TYPE] = authType; + std::string queryParams = jsonObj.dump(); + + int32_t osAccountUserId = MultipleUserConnector::GetCurrentAccountUserID(); + if (osAccountUserId < 0) { + LOGE("get current process account user id failed"); + return ERR_DM_FAILED; + } + if (!GetGroupInfo(osAccountUserId, queryParams, groupList)) { + return false; + } + for (auto iter = groupList.begin(); iter != groupList.end(); iter++) { + if (iter->userId != userId) { + return true; + } + } + return false; +} + bool HiChainConnector::GetGroupInfo(const std::string &queryParams, std::vector &groupList) { char *groupVec = nullptr; @@ -270,22 +302,40 @@ void HiChainConnector::onFinish(int64_t requestId, int operationCode, const char if (operationCode == GroupOperationCode::GROUP_CREATE) { LOGI("Create group success"); SysEventWrite(DM_CREATE_GROUP_SUCCESS, DM_HISYEVENT_BEHAVIOR, DM_CREATE_GROUP_SUCCESS_MSG); - if (hiChainConnectorCallback_ != nullptr) { - hiChainConnectorCallback_->OnMemberJoin(requestId, DM_OK); - hiChainConnectorCallback_->OnGroupCreated(requestId, data); + if (g_networkStyle == CREDENTIAL_NETWORK) { + if (hiChainCredentialCallback_ != nullptr) { + int32_t importAction = 0; + hiChainCredentialCallback_->OnCredentialResult(requestId, importAction, data); + g_createGroupFlag = true; + } + } else { + if (hiChainConnectorCallback_ != nullptr) { + hiChainConnectorCallback_->OnMemberJoin(requestId, DM_OK); + hiChainConnectorCallback_->OnGroupCreated(requestId, data); + } } } if (operationCode == GroupOperationCode::MEMBER_DELETE) { LOGI("Delete Member from group success"); } if (operationCode == GroupOperationCode::GROUP_DISBAND) { + if (g_networkStyle == CREDENTIAL_NETWORK && hiChainCredentialCallback_ != nullptr) { + if (!g_groupIsRedundance) { + int32_t deleteAction = 1; + hiChainCredentialCallback_->OnCredentialResult(requestId, deleteAction, data); + } + g_deleteGroupFlag = true; + } LOGI("Disband group success"); } } void HiChainConnector::onError(int64_t requestId, int operationCode, int errorCode, const char *errorReturn) { - (void)errorReturn; + std::string data = ""; + if (errorReturn != nullptr) { + data = std::string(errorReturn); + } LOGI("HichainAuthenCallBack::onError reqId:%lld, operation:%d, errorCode:%d.", requestId, operationCode, errorCode); if (operationCode == GroupOperationCode::MEMBER_JOIN) { LOGE("Add Member To Group failed"); @@ -297,14 +347,29 @@ void HiChainConnector::onError(int64_t requestId, int operationCode, int errorCo if (operationCode == GroupOperationCode::GROUP_CREATE) { LOGE("Create group failed"); SysEventWrite(DM_CREATE_GROUP_FAILED, DM_HISYEVENT_BEHAVIOR, DM_CREATE_GROUP_FAILED_MSG); - if (hiChainConnectorCallback_ != nullptr) { - hiChainConnectorCallback_->OnGroupCreated(requestId, "{}"); + if (g_networkStyle == CREDENTIAL_NETWORK) { + if (hiChainCredentialCallback_ != nullptr) { + int32_t importAction = 0; + hiChainCredentialCallback_->OnCredentialResult(requestId, importAction, data); + g_createGroupFlag = true; + } + } else { + if (hiChainConnectorCallback_ != nullptr) { + hiChainConnectorCallback_->OnGroupCreated(requestId, "{}"); + } } } if (operationCode == GroupOperationCode::MEMBER_DELETE) { LOGE("Delete Member from group failed"); } if (operationCode == GroupOperationCode::GROUP_DISBAND) { + if (g_networkStyle == CREDENTIAL_NETWORK && hiChainCredentialCallback_ != nullptr) { + if (!g_groupIsRedundance) { + int32_t deleteAction = 1; + hiChainCredentialCallback_->OnCredentialResult(requestId, deleteAction, data); + } + g_deleteGroupFlag = true; + } LOGE("Disband group failed"); } } @@ -513,6 +578,56 @@ int32_t HiChainConnector::DeleteGroup(const int32_t userId, std::string &groupId return DM_OK; } +int32_t HiChainConnector::DeleteGroup(int64_t requestId_, const std::string &userId, const int32_t authType) +{ + g_networkStyle = CREDENTIAL_NETWORK; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_TYPE] = authType; + std::string queryParams = jsonObj.dump(); + std::vector groupList; + if (GetGroupInfo(queryParams, groupList)) { + LOGE("failed to get device join groups"); + return ERR_DM_FAILED; + } + LOGI("HiChainConnector::DeleteGroup groupList count=%d", groupList.size()); + bool userIsExist = false; + std::string groupId = ""; + for (auto iter = groupList.begin(); iter != groupList.end(); iter++) { + if (iter->userId == userId) { + userIsExist = true; + groupId = iter->groupId; + break; + } + } + if (!userIsExist) { + LOGE(" input userId is exist in groupList!"); + return ERR_DM_FAILED; + } + jsonObj[FIELD_GROUP_ID] = groupId; + std::string disbandParams = jsonObj.dump(); + g_deleteGroupFlag = false; + int32_t osAccountUserId = MultipleUserConnector::GetCurrentAccountUserID(); + if (osAccountUserId < 0) { + LOGE("get current process account user id failed"); + return ERR_DM_FAILED; + } + int32_t ret = deviceGroupManager_->deleteGroup(osAccountUserId, requestId_, DM_PKG_NAME.c_str(), + disbandParams.c_str()); + if (ret != 0) { + LOGE("HiChainConnector::DeleteGroup failed , ret: %d.", ret); + return ERR_DM_FAILED; + } + int32_t nTickTimes = 0; + while (!g_deleteGroupFlag) { + usleep(DELAY_TIME_MS); + if (++nTickTimes > SERVICE_INIT_TRY_MAX_NUM) { + LOGE("failed to delete group because timeout!"); + return ERR_DM_FAILED; + } + } + return DM_OK; +} + int32_t HiChainConnector::DeleteTimeOutGroup(const char* deviceId) { LOGI("HiChainConnector::DeleteTimeOutGroup start"); @@ -533,5 +648,110 @@ int32_t HiChainConnector::DeleteTimeOutGroup(const char* deviceId) } return ERR_DM_FAILED; } + +void HiChainConnector::DeleteRedundanceGroup(std::string &userId) +{ + int32_t nTickTimes = 0; + g_deleteGroupFlag = false; + DeleteGroup(userId); + while (!g_deleteGroupFlag) { + usleep(DELAY_TIME_MS); + if (++nTickTimes > SERVICE_INIT_TRY_MAX_NUM) { + LOGE("failed to delete group because timeout!"); + return; + } + } +} + +void HiChainConnector::DealRedundanceGroup(const std::string &userId, int32_t authType) +{ + g_groupIsRedundance = false; + std::vector groupList; + if (IsRedundanceGroup(userId, authType, groupList)) { + LOGI("HiChainConnector::CreateGroup IsRedundanceGroup"); + g_groupIsRedundance = true; + for (auto iter = groupList.begin(); iter != groupList.end(); iter++) { + if (iter->userId != userId) { + DeleteRedundanceGroup(iter->userId); + } + } + g_groupIsRedundance = false; + } +} + +int32_t HiChainConnector::CreateGroup(int64_t requestId, int32_t authType, const std::string &userId, + nlohmann::json &jsonOutObj) +{ + if (deviceGroupManager_ == nullptr) { + LOGE("HiChainConnector::CreateGroup group manager is null, requestId %lld.", requestId); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + DealRedundanceGroup(userId, authType); + g_networkStyle = CREDENTIAL_NETWORK; + LOGI("HiChainConnector::CreateGroup requestId %lld", requestId); + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + std::string sLocalDeviceId = localDeviceId; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_TYPE] = authType; + jsonObj[FIELD_USER_ID] = userId; + jsonObj[FIELD_CREDENTIAL] = jsonOutObj; + jsonObj[FIELD_DEVICE_ID] = sLocalDeviceId; + jsonObj[FIELD_USER_TYPE] = 0; + jsonObj[FIELD_GROUP_VISIBILITY] = GROUP_VISIBILITY_PUBLIC; + jsonObj[FIELD_EXPIRE_TIME] = FIELD_EXPIRE_TIME_VALUE; + g_createGroupFlag = false; + int32_t osAccountUserId = MultipleUserConnector::GetCurrentAccountUserID(); + if (osAccountUserId < 0) { + LOGE("get current process account user id failed"); + return ERR_DM_FAILED; + } + LOGI("[DM] createParams:%s", jsonObj.dump().c_str()); + int32_t ret = deviceGroupManager_->createGroup(osAccountUserId, requestId, DM_PKG_NAME.c_str(), + jsonObj.dump().c_str()); + if (ret != DM_OK) { + LOGE("Failed to start CreateGroup task, ret: %d, requestId %lld.", ret, requestId); + return ERR_DM_CREATE_GROUP_FAILED; + } + int32_t nTickTimes = 0; + while (!g_createGroupFlag) { + usleep(DELAY_TIME_MS); + if (++nTickTimes > SERVICE_INIT_TRY_MAX_NUM) { + LOGE("failed to create group because timeout!"); + return ERR_DM_FAILED; + } + } + return DM_OK; +} + +int32_t HiChainConnector::RegisterhiChainCredentialCallback(const std::shared_ptr &callback) +{ + hiChainCredentialCallback_ = callback; + return DM_OK; +} + +int32_t HiChainConnector::UnRegisterhiChainCredentialCallback() +{ + hiChainCredentialCallback_ = nullptr; + return DM_OK; +} + +int32_t HiChainConnector::getRegisterInfo(const std::string &queryParams, std::string &returnJsonStr) +{ + if (deviceGroupManager_ == nullptr) { + LOGE("HiChainConnector::deviceGroupManager_ is nullptr."); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + char *credentialInfo = nullptr; + if (deviceGroupManager_->getRegisterInfo(queryParams.c_str(), &credentialInfo) != DM_OK) { + LOGE("failed to request hichain registerinfo."); + return ERR_DM_FAILED; + } + + returnJsonStr = credentialInfo; + deviceGroupManager_->destroyInfo(&credentialInfo); + LOGI("request hichain device registerinfo successfully."); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index ddacec442..35d447f42 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -80,6 +80,13 @@ int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptrGetSoftbusSession()->RegisterSessionCallback(authMgr_); hiChainConnector_->RegisterHiChainCallback(authMgr_); } + if (credentialMgr_ == nullptr) { + credentialMgr_ = std::make_shared(hiChainConnector_, listener); + if (credentialMgr_ == nullptr) { + LOGE("Init failed, credentialMgr_ apply for failure"); + return ERR_DM_INIT_FAILED; + } + } int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); if (userId > 0) { @@ -266,6 +273,74 @@ void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, SoftbusSession::OnBytesReceived(sessionId, data, dataLen); } +int32_t DeviceManagerServiceImpl::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr) +{ + if (reqJsonStr.empty()) { + LOGE("reqJsonStr is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialMgr_!= nullptr) { + credentialMgr_->RequestCredential(reqJsonStr, returnJsonStr); + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo) +{ + if (pkgName.empty()) { + LOGE("ImportCredential failed, pkgName is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialInfo.empty()) { + LOGE("credentialInfo is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialMgr_!= nullptr) { + credentialMgr_->ImportCredential(pkgName, credentialInfo); + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) +{ + if (pkgName.empty()) { + LOGE("DeleteCredential failed, pkgName is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (deleteInfo.empty()) { + LOGE("deleteInfo is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialMgr_!= nullptr) { + credentialMgr_->DeleteCredential(pkgName, deleteInfo); + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::RegisterCredentialCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("RegisterCredentialCallback failed, pkgName is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialMgr_ != nullptr) { + credentialMgr_->RegisterCredentialCallback(pkgName); + } + return DM_OK; +} + +int32_t DeviceManagerServiceImpl::UnRegisterCredentialCallback(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("UnRegisterCredentialCallback failed, pkgName is empty"); + return ERR_DM_INPUT_PARAMETER_EMPTY; + } + if (credentialMgr_!= nullptr) { + credentialMgr_->UnRegisterCredentialCallback(pkgName); + } + return DM_OK; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index ebe2949ac..5a9994257 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -31,6 +31,7 @@ if (defined(ohos_lite)) { "${servicesimpl_path}/include/authentication", "${servicesimpl_path}/include/authentication/showconfirm/lite", "${servicesimpl_path}/include/ability", + "${servicesimpl_path}/include/credential", "${servicesimpl_path}/include/devicestate", "${servicesimpl_path}/include/discovery", "${servicesimpl_path}/include/dependency/commonevent", @@ -170,9 +171,7 @@ if (defined(ohos_lite)) { "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", ] - sources = [ - "src/ipc/lite/ipc_server_main.cpp", - ] + sources = [ "src/ipc/lite/ipc_server_main.cpp" ] ldflags = dm_ldflags defines = [ @@ -260,4 +259,4 @@ if (defined(ohos_lite)) { part_name = "device_manager" } -} \ No newline at end of file +} diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 4dc49f7d3..5cc33c832 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -82,8 +82,19 @@ public: void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); bool IsDMServiceImplReady(); + + int32_t DmHiDumper(const std::vector& args, std::string &result); + + int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); + + int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo); + + int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo); + + int32_t RegisterCredentialCallback(const std::string &pkgName); + + int32_t UnRegisterCredentialCallback(const std::string &pkgName); - int32_t DmHiDumper(const std::vector& args, std::string &result); private: bool isImplsoLoaded_ = false; std::shared_ptr softbusListener_; diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index eae8b1451..ad20aac63 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -49,6 +49,7 @@ public: void OnFaCall(std::string &pkgName, std::string ¶mJson); + void OnCredentialResult(const std::string &pkgName, int32_t action, const std::string &resultInfo); private: #if !defined(__LITEOS_M__) IpcServerListener ipcServerListener_; diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 4c8af0469..df0c9353b 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -142,6 +142,37 @@ public: * @tc.type: FUNC */ virtual void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); + + /** + * @tc.name: DeviceManagerService::RequestCredential + * @tc.desc: RequestCredential of the Device Manager Service + * @tc.type: FUNC + */ + virtual int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); + /** + * @tc.name: DeviceManagerService::ImportCredential + * @tc.desc: ImportCredential of the Device Manager Service + * @tc.type: FUNC + */ + virtual int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo); + /** + * @tc.name: DeviceManagerService::DeleteCredential + * @tc.desc: DeleteCredential of the Device Manager Service + * @tc.type: FUNC + */ + virtual int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo); + /** + * @tc.name: DeviceManagerService::RegisterCredentialCallback + * @tc.desc: RegisterCredentialCallback + * @tc.type: FUNC + */ + virtual int32_t RegisterCredentialCallback(const std::string &pkgName); + /** + * @tc.name: DeviceManagerService::UnRegisterCredentialCallback + * @tc.desc: UnRegisterCredentialCallback + * @tc.type: FUNC + */ + virtual int32_t UnRegisterCredentialCallback(const std::string &pkgName); }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/include/idevice_manager_service_listener.h b/services/service/include/idevice_manager_service_listener.h index 3c6aa2166..84fab4d24 100644 --- a/services/service/include/idevice_manager_service_listener.h +++ b/services/service/include/idevice_manager_service_listener.h @@ -77,6 +77,13 @@ public: * @tc.type: FUNC */ virtual void OnFaCall(std::string &pkgName, std::string ¶mJson); + + /** + * @tc.name: IDeviceManagerServiceListener::OnCredentialResult + * @tc.desc: Credential Result of the DeviceManager Service Listener + * @tc.type: FUNC + */ + virtual void OnCredentialResult(const std::string &pkgName, int32_t action, const std::string &resultInfo); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 8a9a802fd..fc30c42c7 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -237,6 +237,51 @@ void DeviceManagerService::OnBytesReceived(int sessionId, const void *data, unsi dmServiceImpl_->OnBytesReceived(sessionId, data, dataLen); } +int32_t DeviceManagerService::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr) +{ + if (!IsDMServiceImplReady()) { + LOGE("RequestCredential failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->RequestCredential(reqJsonStr, returnJsonStr); +} + +int32_t DeviceManagerService::ImportCredential(const std::string &pkgName, const std::string &credentialInfo) +{ + if (!IsDMServiceImplReady()) { + LOGE("ImportCredential failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->ImportCredential(pkgName, credentialInfo); +} + +int32_t DeviceManagerService::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo) +{ + if (!IsDMServiceImplReady()) { + LOGE("DeleteCredential failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->DeleteCredential(pkgName, deleteInfo); +} + +int32_t DeviceManagerService::RegisterCredentialCallback(const std::string &pkgName) +{ + if (!IsDMServiceImplReady()) { + LOGE("RegisterCredentialCallback failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->RegisterCredentialCallback(pkgName); +} + +int32_t DeviceManagerService::UnRegisterCredentialCallback(const std::string &pkgName) +{ + if (!IsDMServiceImplReady()) { + LOGE("UnRegisterCredentialCallback failed, instance not init or init failed."); + return ERR_DM_NOT_INIT; + } + return dmServiceImpl_->UnRegisterCredentialCallback(pkgName); +} + bool DeviceManagerService::IsDMServiceImplReady() { if (isImplsoLoaded_ && (dmServiceImpl_ != nullptr)) { diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index c3d7be1f5..32d6f516d 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -19,6 +19,7 @@ #include "dm_constants.h" #include "dm_log.h" #include "ipc_notify_auth_result_req.h" +#include "ipc_notify_credential_req.h" #include "ipc_notify_device_found_req.h" #include "ipc_notify_device_state_req.h" #include "ipc_notify_discover_result_req.h" @@ -118,5 +119,18 @@ void DeviceManagerServiceListener::OnFaCall(std::string &pkgName, std::string &p pReq->SetJsonParam(paramJson); ipcServerListener_.SendRequest(SERVER_DEVICE_FA_NOTIFY, pReq, pRsp); } + +void DeviceManagerServiceListener::OnCredentialResult(const std::string &pkgName, int32_t action, + const std::string &resultInfo) +{ + LOGI("call OnCredentialResult for %s, action %d", pkgName.c_str(), action); + std::shared_ptr pReq = std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + + pReq->SetPkgName(pkgName); + pReq->SetCredentialAction(action); + pReq->SetCredentialResult(resultInfo); + ipcServerListener_.SendRequest(SERVER_CREDENTIAL_RESULT, pReq, pRsp); +} } // 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 41366190b..06827dad6 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -24,6 +24,7 @@ #include "ipc_cmd_register.h" #include "ipc_def.h" #include "ipc_notify_auth_result_req.h" +#include "ipc_notify_credential_req.h" #include "ipc_notify_device_found_req.h" #include "ipc_notify_device_state_req.h" #include "ipc_notify_discover_result_req.h" @@ -506,5 +507,112 @@ ON_IPC_CMD(UNREGISTER_DEV_STATE_CALLBACK, MessageParcel &data, MessageParcel &re } return result; } + +ON_IPC_CMD(REQUEST_CREDENTIAL, MessageParcel &data, MessageParcel &reply) +{ + LOGE("start to request device credential."); + std::string packageName = data.ReadString(); + std::string reqJsonStr = data.ReadString(); + std::string returnJsonStr; + int32_t ret = DeviceManagerService::GetInstance().RequestCredential(reqJsonStr, returnJsonStr); + if (!reply.WriteInt32(ret)) { + LOGE("write ret failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (ret == DM_OK && !returnJsonStr.empty()) { + if (!reply.WriteString(returnJsonStr)) + { + LOGE("write returnJsonStr failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + } + LOGI("REQUEST_CREDENTIAL success."); + return DM_OK; +} + +ON_IPC_CMD(IMPORT_CREDENTIAL, MessageParcel &data, MessageParcel &reply) +{ + LOGE("start to import credential to device."); + std::string packageName = data.ReadString(); + std::string credentialInfo = data.ReadString(); + int32_t ret = DeviceManagerService::GetInstance().ImportCredential(packageName, credentialInfo); + if (!reply.WriteInt32(ret)) { + LOGE("write ret failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + LOGI("IMPORT_CREDENTIAL success."); + return DM_OK; +} + +ON_IPC_CMD(DELETE_CREDENTIAL, MessageParcel &data, MessageParcel &reply) +{ + LOGE("start to delete credential from device."); + std::string packageName = data.ReadString(); + std::string deleteInfo = data.ReadString(); + int32_t ret = DeviceManagerService::GetInstance().DeleteCredential(packageName, deleteInfo); + if (!reply.WriteInt32(ret)) { + LOGE("write ret failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + LOGI("DELETE_CREDENTIAL success."); + return DM_OK; +} + +ON_IPC_CMD(REGISTER_CREDENTIAL_CALLBACK, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + int result = DeviceManagerService::GetInstance().RegisterCredentialCallback(packageName); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return result; +} + +ON_IPC_CMD(UNREGISTER_CREDENTIAL_CALLBACK, MessageParcel &data, MessageParcel &reply) +{ + std::string packageName = data.ReadString(); + int result = DeviceManagerService::GetInstance().UnRegisterCredentialCallback(packageName); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return result; +} + +ON_IPC_SET_REQUEST(SERVER_CREDENTIAL_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + return ERR_DM_FAILED; + } + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + int32_t action = pReq->GetCredentialAction(); + std::string credentialResult = pReq->GetCredentialResult(); + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt32(action)) { + LOGE("write action failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteString(credentialResult)) { + LOGE("write credentialResult failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + + return DM_OK; +} + +ON_IPC_READ_RESPONSE(SERVER_CREDENTIAL_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/device_auth.h b/test/unittest/mock/device_auth.h index 534386e57..5ca6d5c5b 100644 --- a/test/unittest/mock/device_auth.h +++ b/test/unittest/mock/device_auth.h @@ -174,7 +174,7 @@ using DeviceGroupManager = struct _DeviceGroupManager { int32_t (*authKeyAgree)(int64_t requestId, const char *appId, const char *buildParams); int32_t (*processKeyAgreeData)(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen); int32_t (*processCredential)(int operationCode, const char *reqJsonStr, char **returnJsonStr); - int32_t (*getRegisterInfo)(char **returnRegisterInfo); + int32_t (*getRegisterInfo)(const char *reqJsonStr, char **returnRegisterInfo); int32_t (*getLocalConnectInfo)(char *returnInfo, int32_t bufLen); int32_t (*checkAccessToGroup)(int32_t osAccountId, const char *appId, const char *groupId); int32_t (*getPkInfoList)(int32_t osAccountId, const char *appId, const char *queryParams, char **returnInfoList, diff --git a/utils/include/dm_hash.h b/utils/include/dm_hash.h index 25a63b2b3..77c3269e2 100644 --- a/utils/include/dm_hash.h +++ b/utils/include/dm_hash.h @@ -27,6 +27,7 @@ namespace OHOS { namespace DistributedHardware { int32_t GetUdidHash(uint8_t *udid, int32_t udiddataLen, uint8_t outudiddData[32]); +int32_t GenerateStrHash(const unsigned char *strInput, uint32_t len, unsigned char *hash); } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_HASH_H \ No newline at end of file diff --git a/utils/src/dm_hash.cpp b/utils/src/dm_hash.cpp index 4e12dc526..1695b2304 100644 --- a/utils/src/dm_hash.cpp +++ b/utils/src/dm_hash.cpp @@ -70,5 +70,37 @@ int32_t GetUdidHash(uint8_t *udid, int32_t udidDataLen, uint8_t outudidData[32]) mbedtls_md_free(&ctx); return ret; } + +int32_t GenerateStrHash(const unsigned char *strInput, uint32_t len, unsigned char *hash) +{ + if (strInput == nullptr || hash == nullptr || len == 0) { + return ERR_DM_POINT_NULL; + } + + mbedtls_md_context_t ctx; + const mbedtls_md_info_t *info = nullptr; + mbedtls_md_init(&ctx); + + info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); + if (mbedtls_md_setup(&ctx, info, 0) != 0) { + mbedtls_md_free(&ctx); + return ERR_DM_FAILED; + } + if (mbedtls_md_starts(&ctx) != 0) { + mbedtls_md_free(&ctx); + return ERR_DM_FAILED; + } + if (mbedtls_md_update(&ctx, strInput, len) != 0) { + mbedtls_md_free(&ctx); + return ERR_DM_FAILED; + } + if (mbedtls_md_finish(&ctx, hash) != 0) { + mbedtls_md_free(&ctx); + return ERR_DM_FAILED; + } + + mbedtls_md_free(&ctx); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/utils/src/ipc/standard/ipc_cmd_register.cpp b/utils/src/ipc/standard/ipc_cmd_register.cpp index 9ee96f08e..33355854c 100644 --- a/utils/src/ipc/standard/ipc_cmd_register.cpp +++ b/utils/src/ipc/standard/ipc_cmd_register.cpp @@ -26,7 +26,7 @@ IMPLEMENT_SINGLE_INSTANCE(IpcCmdRegister); int32_t IpcCmdRegister::SetRequest(int32_t cmdCode, std::shared_ptr pBaseReq, MessageParcel &data) { int32_t ret = DM_OK; - if (cmdCode < 0 || cmdCode >= UNREGISTER_DEV_STATE_CALLBACK || pBaseReq == nullptr) { + if (cmdCode < 0 || cmdCode >= UNREGISTER_CREDENTIAL_CALLBACK || pBaseReq == nullptr) { LOGE("IpcCmdRegister::SetRequest cmdCode param invalid!"); return ERR_DM_INPUT_PARAMETER_EMPTY; } -- Gitee From 05a577be995d68aa917ae686926399f0e63777d0 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Fri, 1 Jul 2022 15:00:02 +0800 Subject: [PATCH 02/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp | 1 - .../inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index b5bb6ebf7..14d436aa2 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -39,7 +39,6 @@ #include "ipc_unauthenticate_device_req.h" #include "ipc_verify_authenticate_req.h" #include "ipc_register_dev_state_callback_req.h" -#include "ipc_register_credential_callback_req.h" #include "securec.h" namespace OHOS { diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index 4c7ae4c31..72457640b 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -26,7 +26,6 @@ #include "ipc_get_local_device_info_rsp.h" #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" -#include "ipc_register_credential_callback_req.h" #include "ipc_register_listener_req.h" #include "ipc_req.h" #include "ipc_rsp.h" -- Gitee From 52d3e1a5cf855c84026bc9be315739b7c698e683 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Fri, 1 Jul 2022 15:04:05 +0800 Subject: [PATCH 03/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- services/service/include/device_manager_service.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 5cc33c832..ba5e9493b 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -82,8 +82,8 @@ public: void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); bool IsDMServiceImplReady(); - - int32_t DmHiDumper(const std::vector& args, std::string &result); + + int32_t DmHiDumper(const std::vector& args, std::string &result); int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr); -- Gitee From 49766ac88f585d0c5c29eb80056424fe54b031ff Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Fri, 1 Jul 2022 15:34:48 +0800 Subject: [PATCH 04/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index 14d436aa2..1e5ec4c82 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -655,11 +655,12 @@ int32_t DeviceManagerImpl::UnRegisterCredentialCallback(const std::string &pkgNa std::shared_ptr rsp = std::make_shared(); req->SetPkgName(pkgName); - if (ipcClientProxy_->SendRequest(UNREGISTER_CREDENTIAL_CALLBACK, req, rsp) != DM_OK) { + int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_CREDENTIAL_CALLBACK, req, rsp); + if (ret != DM_OK) { LOGE("UnRegisterCredentialCallback error: Send Request failed ret: %d", ret); return ERR_DM_IPC_SEND_REQUEST_FAILED; } - int32_t ret = rsp->GetErrCode(); + ret = rsp->GetErrCode(); if (ret != DM_OK) { LOGE("UnRegisterCredentialCallback Failed with ret %d", ret); return ret; -- Gitee From 6f39f0fdef56d018ad40ad2f857c88d7929d2e63 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Fri, 1 Jul 2022 16:42:12 +0800 Subject: [PATCH 05/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../include/dependency/hichain/hichain_connector.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index 83483930b..565899820 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -37,7 +37,7 @@ struct GroupInfo { int32_t groupVisibility; std::string userId; - GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId(0) + GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0), userId("") { } }; -- Gitee From 1102247add31138fe266c18ec1c7f6d39c1f3ca6 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Fri, 1 Jul 2022 17:57:57 +0800 Subject: [PATCH 06/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../implementation/src/dependency/hichain/hichain_connector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index 9efe475c0..e4949c4f5 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -585,7 +585,7 @@ int32_t HiChainConnector::DeleteGroup(int64_t requestId_, const std::string &use jsonObj[FIELD_GROUP_TYPE] = authType; std::string queryParams = jsonObj.dump(); std::vector groupList; - if (GetGroupInfo(queryParams, groupList)) { + if (!GetGroupInfo(queryParams, groupList)) { LOGE("failed to get device join groups"); return ERR_DM_FAILED; } -- Gitee From 3044312e1a81706dd89448f5e4972b69fe9a5364 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Sat, 2 Jul 2022 14:27:15 +0800 Subject: [PATCH 07/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../inner_kits/native_cpp/src/notify/device_manager_notify.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 1a44d2f1a..590fb9199 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp @@ -309,7 +309,6 @@ void DeviceManagerNotify::OnCredentialResult(const std::string &pkgName, int32_t return; } credentialCallback_[pkgName]->OnCredentialResult(action, credentialResult); - credentialCallback_.erase(pkgName); } } // namespace DistributedHardware } // namespace OHOS -- Gitee From 9a5f19504d2156888121ecb61483fa653a38b736 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Mon, 4 Jul 2022 13:11:39 +0800 Subject: [PATCH 08/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../credential/dm_credential_manager.h | 4 +- .../dependency/hichain/hichain_connector.h | 7 ++-- .../hichain/hichain_connector_callback.h | 4 +- .../src/credential/dm_credential_manager.cpp | 8 ++-- .../dependency/hichain/hichain_connector.cpp | 37 +++++++++---------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/services/implementation/include/credential/dm_credential_manager.h b/services/implementation/include/credential/dm_credential_manager.h index c37e431a2..0cab7bad9 100644 --- a/services/implementation/include/credential/dm_credential_manager.h +++ b/services/implementation/include/credential/dm_credential_manager.h @@ -31,7 +31,7 @@ typedef struct { std::string authCode; std::string peerDeviceId; } CredentialData; -class DmCredentialManager final : public IDmCredentialCallback, +class DmCredentialManager final : public IDmGroupResCallback, public std::enable_shared_from_this { public: DmCredentialManager(std::shared_ptr hiChainConnector, @@ -92,7 +92,7 @@ public: * @tc.desc: Credential Result of the DmCredential Manager * @tc.type: FUNC */ - void OnCredentialResult(int64_t requestId, int32_t action, const std::string &resultInfo); + void OnGroupResult(int64_t requestId, int32_t action, const std::string &resultInfo); private: std::shared_ptr hiChainConnector_; std::shared_ptr listener_; diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index 565899820..50cedf05f 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -159,14 +159,14 @@ public: * @tc.desc: Register HiChain Callback of the HiChain Connector * @tc.type: FUNC */ - int32_t RegisterhiChainCredentialCallback(const std::shared_ptr &callback); + int32_t RegisterHiChainGroupCallback(const std::shared_ptr &callback); /** * @tc.name: HiChainConnector::UnRegisterHiChainCallback * @tc.desc: Un Register HiChain Callback of the HiChain Connector * @tc.type: FUNC */ - int32_t UnRegisterhiChainCredentialCallback(); + int32_t UnRegisterHiChainGroupCallback(); /** * @tc.name: HiChainConnector::getRegisterInfo @@ -192,7 +192,8 @@ private: const DeviceGroupManager *deviceGroupManager_ = nullptr; DeviceAuthCallback deviceAuthCallback_; static std::shared_ptr hiChainConnectorCallback_; - static std::shared_ptr hiChainCredentialCallback_; + static std::shared_ptr hiChainCredentialCallback_; + int32_t networkStyle_ = PIN_CODE_NETWORK; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/include/dependency/hichain/hichain_connector_callback.h b/services/implementation/include/dependency/hichain/hichain_connector_callback.h index 1ffb2b7dc..cd4b9d889 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector_callback.h +++ b/services/implementation/include/dependency/hichain/hichain_connector_callback.h @@ -26,9 +26,9 @@ public: virtual int32_t GetPinCode() = 0; }; -class IDmCredentialCallback { +class IDmGroupResCallback { public: - virtual void OnCredentialResult(int64_t requestId, int32_t action, const std::string &resultInfo) = 0; + virtual void OnGroupResult(int64_t requestId, int32_t action, const std::string &resultInfo) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/credential/dm_credential_manager.cpp b/services/implementation/src/credential/dm_credential_manager.cpp index 3f9f30128..b6b7a3b78 100644 --- a/services/implementation/src/credential/dm_credential_manager.cpp +++ b/services/implementation/src/credential/dm_credential_manager.cpp @@ -30,7 +30,7 @@ void from_json(const nlohmann::json &jsonObject, CredentialData &credentialData) !jsonObject.contains(FIELD_SERVER_PK) || !jsonObject.contains(FIELD_PKINFO_SIGNATURE) || !jsonObject.contains(FIELD_PKINFO) || !jsonObject.contains(FIELD_AUTH_CODE) || !jsonObject.contains(FIELD_PEER_DEVICE_ID)) { - LOGE("CredentialData json key Not complete"); + LOGE("CredentialData json key not complete"); return; } jsonObject[FIELD_CREDENTIAL_TYPE].get_to(credentialData.credentialType); @@ -192,7 +192,7 @@ int32_t DmCredentialManager::DeleteCredential(const std::string &pkgName, const return DM_OK; } -void DmCredentialManager::OnCredentialResult(int64_t requestId, int32_t action, +void DmCredentialManager::OnGroupResult(int64_t requestId, int32_t action, const std::string &resultInfo) { LOGI("DmCredentialManager::OnImportResult"); @@ -211,7 +211,7 @@ void DmCredentialManager::RegisterCredentialCallback(const std::string &pkgName) LOGI("DmCredentialManager::RegisterCredentialCallback pkgName=%s", GetAnonyString(pkgName).c_str()); credentialVec_.push_back(pkgName); - hiChainConnector_->RegisterhiChainCredentialCallback(std::shared_ptr(shared_from_this())); + hiChainConnector_->RegisterHiChainGroupCallback(std::shared_ptr(shared_from_this())); } void DmCredentialManager::UnRegisterCredentialCallback(const std::string &pkgName) @@ -226,7 +226,7 @@ void DmCredentialManager::UnRegisterCredentialCallback(const std::string &pkgNam if (iter != credentialVec_.end()) { credentialVec_.erase(iter); } - hiChainConnector_->UnRegisterhiChainCredentialCallback(); + hiChainConnector_->UnRegisterHiChainGroupCallback(); } int32_t DmCredentialManager::GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData, diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index e4949c4f5..d80e2ad6e 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -61,11 +61,10 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) } std::shared_ptr HiChainConnector::hiChainConnectorCallback_ = nullptr; -std::shared_ptr HiChainConnector::hiChainCredentialCallback_ = nullptr; +std::shared_ptr HiChainConnector::hiChainResCallback_ = nullptr; bool g_createGroupFlag = false; bool g_deleteGroupFlag = false; bool g_groupIsRedundance = false; -int32_t g_networkStyle = PIN_CODE_NETWORK; HiChainConnector::HiChainConnector() { @@ -108,7 +107,7 @@ int32_t HiChainConnector::CreateGroup(int64_t requestId, const std::string &grou LOGE("HiChainConnector::CreateGroup group manager is null, requestId %lld.", requestId); return ERR_DM_INPUT_PARAMETER_EMPTY; } - g_networkStyle = PIN_CODE_NETWORK; + networkStyle_ = PIN_CODE_NETWORK; GroupInfo groupInfo; if (IsGroupCreated(groupName, groupInfo)) { DeleteGroup(groupInfo.groupId); @@ -302,10 +301,10 @@ void HiChainConnector::onFinish(int64_t requestId, int operationCode, const char if (operationCode == GroupOperationCode::GROUP_CREATE) { LOGI("Create group success"); SysEventWrite(DM_CREATE_GROUP_SUCCESS, DM_HISYEVENT_BEHAVIOR, DM_CREATE_GROUP_SUCCESS_MSG); - if (g_networkStyle == CREDENTIAL_NETWORK) { - if (hiChainCredentialCallback_ != nullptr) { + if (networkStyle_ == CREDENTIAL_NETWORK) { + if (hiChainResCallback_ != nullptr) { int32_t importAction = 0; - hiChainCredentialCallback_->OnCredentialResult(requestId, importAction, data); + hiChainResCallback_->OnGroupResult(requestId, importAction, data); g_createGroupFlag = true; } } else { @@ -319,10 +318,10 @@ void HiChainConnector::onFinish(int64_t requestId, int operationCode, const char LOGI("Delete Member from group success"); } if (operationCode == GroupOperationCode::GROUP_DISBAND) { - if (g_networkStyle == CREDENTIAL_NETWORK && hiChainCredentialCallback_ != nullptr) { + if (networkStyle_ == CREDENTIAL_NETWORK && hiChainResCallback_ != nullptr) { if (!g_groupIsRedundance) { int32_t deleteAction = 1; - hiChainCredentialCallback_->OnCredentialResult(requestId, deleteAction, data); + hiChainResCallback_->OnGroupResult(requestId, deleteAction, data); } g_deleteGroupFlag = true; } @@ -347,10 +346,10 @@ void HiChainConnector::onError(int64_t requestId, int operationCode, int errorCo if (operationCode == GroupOperationCode::GROUP_CREATE) { LOGE("Create group failed"); SysEventWrite(DM_CREATE_GROUP_FAILED, DM_HISYEVENT_BEHAVIOR, DM_CREATE_GROUP_FAILED_MSG); - if (g_networkStyle == CREDENTIAL_NETWORK) { - if (hiChainCredentialCallback_ != nullptr) { + if (networkStyle_ == CREDENTIAL_NETWORK) { + if (hiChainResCallback_ != nullptr) { int32_t importAction = 0; - hiChainCredentialCallback_->OnCredentialResult(requestId, importAction, data); + hiChainResCallback_->OnGroupResult(requestId, importAction, data); g_createGroupFlag = true; } } else { @@ -363,10 +362,10 @@ void HiChainConnector::onError(int64_t requestId, int operationCode, int errorCo LOGE("Delete Member from group failed"); } if (operationCode == GroupOperationCode::GROUP_DISBAND) { - if (g_networkStyle == CREDENTIAL_NETWORK && hiChainCredentialCallback_ != nullptr) { + if (networkStyle_ == CREDENTIAL_NETWORK && hiChainResCallback_ != nullptr) { if (!g_groupIsRedundance) { int32_t deleteAction = 1; - hiChainCredentialCallback_->OnCredentialResult(requestId, deleteAction, data); + hiChainResCallback_->OnGroupResult(requestId, deleteAction, data); } g_deleteGroupFlag = true; } @@ -580,7 +579,7 @@ int32_t HiChainConnector::DeleteGroup(const int32_t userId, std::string &groupId int32_t HiChainConnector::DeleteGroup(int64_t requestId_, const std::string &userId, const int32_t authType) { - g_networkStyle = CREDENTIAL_NETWORK; + networkStyle_ = CREDENTIAL_NETWORK; nlohmann::json jsonObj; jsonObj[FIELD_GROUP_TYPE] = authType; std::string queryParams = jsonObj.dump(); @@ -687,7 +686,7 @@ int32_t HiChainConnector::CreateGroup(int64_t requestId, int32_t authType, const return ERR_DM_INPUT_PARAMETER_EMPTY; } DealRedundanceGroup(userId, authType); - g_networkStyle = CREDENTIAL_NETWORK; + networkStyle_ = CREDENTIAL_NETWORK; LOGI("HiChainConnector::CreateGroup requestId %lld", requestId); char localDeviceId[DEVICE_UUID_LENGTH] = {0}; GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); @@ -724,15 +723,15 @@ int32_t HiChainConnector::CreateGroup(int64_t requestId, int32_t authType, const return DM_OK; } -int32_t HiChainConnector::RegisterhiChainCredentialCallback(const std::shared_ptr &callback) +int32_t HiChainConnector::RegisterHiChainGroupCallback(const std::shared_ptr &callback) { - hiChainCredentialCallback_ = callback; + hiChainResCallback_ = callback; return DM_OK; } -int32_t HiChainConnector::UnRegisterhiChainCredentialCallback() +int32_t HiChainConnector::UnRegisterHiChainGroupCallback() { - hiChainCredentialCallback_ = nullptr; + hiChainResCallback_ = nullptr; return DM_OK; } -- Gitee From 21d6be5fd8645a0ede91a7211ba0534666647391 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Mon, 4 Jul 2022 14:10:25 +0800 Subject: [PATCH 09/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../include/dependency/hichain/hichain_connector.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index 50cedf05f..e15b081f1 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -192,8 +192,8 @@ private: const DeviceGroupManager *deviceGroupManager_ = nullptr; DeviceAuthCallback deviceAuthCallback_; static std::shared_ptr hiChainConnectorCallback_; - static std::shared_ptr hiChainCredentialCallback_; - int32_t networkStyle_ = PIN_CODE_NETWORK; + static std::shared_ptr hiChainResCallback_; + static int32_t networkStyle_ = PIN_CODE_NETWORK; }; } // namespace DistributedHardware } // namespace OHOS -- Gitee From 580f81b82f0197bc72581136be8e9485bdf553d3 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Mon, 4 Jul 2022 14:12:55 +0800 Subject: [PATCH 10/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../include/dependency/hichain/hichain_connector.h | 1 + 1 file changed, 1 insertion(+) diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index e15b081f1..439455df8 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -23,6 +23,7 @@ #include #include "device_auth.h" +#include "dm_constants.h" #include "hichain_connector_callback.h" #include "nlohmann/json.hpp" #include "single_instance.h" -- Gitee From cc2ddd4b6cf337bb16145739d570c826885546c5 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Mon, 4 Jul 2022 14:24:36 +0800 Subject: [PATCH 11/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../include/dependency/hichain/hichain_connector.h | 3 +-- .../src/dependency/hichain/hichain_connector.cpp | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/implementation/include/dependency/hichain/hichain_connector.h b/services/implementation/include/dependency/hichain/hichain_connector.h index 439455df8..522f83773 100644 --- a/services/implementation/include/dependency/hichain/hichain_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_connector.h @@ -23,7 +23,6 @@ #include #include "device_auth.h" -#include "dm_constants.h" #include "hichain_connector_callback.h" #include "nlohmann/json.hpp" #include "single_instance.h" @@ -194,7 +193,7 @@ private: DeviceAuthCallback deviceAuthCallback_; static std::shared_ptr hiChainConnectorCallback_; static std::shared_ptr hiChainResCallback_; - static int32_t networkStyle_ = PIN_CODE_NETWORK; + static int32_t networkStyle_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index d80e2ad6e..5179ef212 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -65,6 +65,7 @@ std::shared_ptr HiChainConnector::hiChainResCallback_ = nul bool g_createGroupFlag = false; bool g_deleteGroupFlag = false; bool g_groupIsRedundance = false; +networkStyle_ = PIN_CODE_NETWORK; HiChainConnector::HiChainConnector() { -- Gitee From 2dbbf6543d447720b514c99de0402f5007017be1 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Mon, 4 Jul 2022 14:36:33 +0800 Subject: [PATCH 12/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../implementation/src/dependency/hichain/hichain_connector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index 5179ef212..d71272abb 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -65,7 +65,7 @@ std::shared_ptr HiChainConnector::hiChainResCallback_ = nul bool g_createGroupFlag = false; bool g_deleteGroupFlag = false; bool g_groupIsRedundance = false; -networkStyle_ = PIN_CODE_NETWORK; +HiChainConnector::networkStyle_ = PIN_CODE_NETWORK; HiChainConnector::HiChainConnector() { -- Gitee From dbe33f3ca991ea1e0689a30636563dae2f7ff670 Mon Sep 17 00:00:00 2001 From: wuqi0105 Date: Mon, 4 Jul 2022 14:47:42 +0800 Subject: [PATCH 13/13] =?UTF-8?q?oh=E5=87=AD=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqi0105 --- .../implementation/src/dependency/hichain/hichain_connector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index d71272abb..9be5f55c8 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -62,10 +62,10 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) std::shared_ptr HiChainConnector::hiChainConnectorCallback_ = nullptr; std::shared_ptr HiChainConnector::hiChainResCallback_ = nullptr; +int32_t HiChainConnector::networkStyle_ = PIN_CODE_NETWORK; bool g_createGroupFlag = false; bool g_deleteGroupFlag = false; bool g_groupIsRedundance = false; -HiChainConnector::networkStyle_ = PIN_CODE_NETWORK; HiChainConnector::HiChainConnector() { -- Gitee