diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index fb711dd5b55ebd2efaab72643e54888359e80c44..72ee5cbc9d6f67af95a3b6f71011c593c0eef1a3 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 226692d10b6b9c8d3964d80b3a640af595fc6302..cc2ad5bff8cd671d2e10f56ad8a83da2b576bc73 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 0000000000000000000000000000000000000000..df388d4c7238c09834208e2ac50a94bef4ecdc56 --- /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 0000000000000000000000000000000000000000..1709efcd1177822785b24a5bf3109c78858f0df7 --- /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 0000000000000000000000000000000000000000..a490ff0058e45d58cebd624977e4340da385f228 --- /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 00274b1c45a843ac225fdcf6e0abf29ab2d7d2d3..0794ca5bdeddc60bbaee201dcd3a82aea1e74641 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 f92ceebf565ff179dfe66a43c90942fbad6a705b..e47a2578e3c5f289f5b87bd29444d2d9f9153874 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 3cd7e598f0665694404d0824447a7dc7773867dd..cfe1a1b4ba535e3d8c9f3558b6f4aea7d4df9fa5 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 5726050ab495b7da84bbb1d3ed5ab43cac4f5489..54b2a88c02223d9990ee7e07cd83340b7d64eea6 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 f2eb0278be363298a3b3399094c0c6fa6f4e60e5..1e5ec4c82c54e344a8013c215261416f94143e38 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -31,6 +31,8 @@ #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" @@ -522,5 +524,149 @@ 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); + + 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; + } + 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 5cc2d45bfdd3b0ee4d069d6dd18a3d29ee4d94a0..72457640b91e1f80c9b4da813dad810d0b90e8b3 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 @@ -29,6 +29,8 @@ #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 +514,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 5081c63b51b5265b4be30b2ff514631acb3e6194..590fb9199dfa2763adb0a0c7de8ff19805e542f8 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,17 @@ 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); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 9497bdb52fa45e4fc5f818d92811bf1546f37338..e8a3b67e8107cf31ec37554a348c5db748d1179a 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 0000000000000000000000000000000000000000..0cab7bad968b5fce886928a2188007abf003405b --- /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 IDmGroupResCallback, + 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 OnGroupResult(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 bf84287b76af25febc632a3f18758986f7086d79..522f837736422575eac3adaa1e8fb80b3369fc12 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("") { } }; @@ -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,47 @@ 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 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 UnRegisterHiChainGroupCallback(); + + /** + * @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 hiChainResCallback_; + static int32_t networkStyle_; }; } // 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 a5c573e2d2bb80b7e9731dd1bc14b72cfb17b4e3..cd4b9d8891cedd07be4127cd6e750de06736286e 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 IDmGroupResCallback { +public: + virtual void OnGroupResult(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 9dccd133bde4f3391f90338e874dec18c916e453..02a85970eeb36fe49f881c972cfeef4716f92877 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 0000000000000000000000000000000000000000..b6b7a3b7869322f48e29015dd284714fe82ce57c --- /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::OnGroupResult(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_->RegisterHiChainGroupCallback(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_->UnRegisterHiChainGroupCallback(); +} + +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 7e5b908d2d73bce9502a7c7ee954b209854078af..9be5f55c868635298c78a97ddb7af5a9e77b6a31 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::hiChainResCallback_ = nullptr; +int32_t HiChainConnector::networkStyle_ = PIN_CODE_NETWORK; +bool g_createGroupFlag = false; +bool g_deleteGroupFlag = false; +bool g_groupIsRedundance = false; 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; } + 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 (networkStyle_ == CREDENTIAL_NETWORK) { + if (hiChainResCallback_ != nullptr) { + int32_t importAction = 0; + hiChainResCallback_->OnGroupResult(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 (networkStyle_ == CREDENTIAL_NETWORK && hiChainResCallback_ != nullptr) { + if (!g_groupIsRedundance) { + int32_t deleteAction = 1; + hiChainResCallback_->OnGroupResult(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 (networkStyle_ == CREDENTIAL_NETWORK) { + if (hiChainResCallback_ != nullptr) { + int32_t importAction = 0; + hiChainResCallback_->OnGroupResult(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 (networkStyle_ == CREDENTIAL_NETWORK && hiChainResCallback_ != nullptr) { + if (!g_groupIsRedundance) { + int32_t deleteAction = 1; + hiChainResCallback_->OnGroupResult(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) +{ + 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); + 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::RegisterHiChainGroupCallback(const std::shared_ptr &callback) +{ + hiChainResCallback_ = callback; + return DM_OK; +} + +int32_t HiChainConnector::UnRegisterHiChainGroupCallback() +{ + hiChainResCallback_ = 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 ddacec442a9ea631bb6a090af594aaba8ea5a197..35d447f420449edae0423525aea10b100bb445c6 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 ebe2949acb882c958d118c9b10755e5af9a7abb6..5a999425781aae4e92cfd1a14900f0419a9028fc 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 4dc49f7d31b35faf791f3bc4f5d3506e9e529142..ba5e9493b85ef6f943e92a9ffaa401018037575d 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -84,6 +84,17 @@ public: 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); + 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 eae8b1451b294ec71374cc55d12aa2b712c2a586..ad20aac63a5a1e13e55ced65fc65593be5e30046 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 4c8af0469d6e7837416baf1197eb98bc3f6b0eab..df0c9353bc3d03e3494c765c35a258f841f9e879 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 3c6aa2166b74a9f7592066f725b6cbcdbd148217..84fab4d24dde319c056b2deb270c5744b5aa145b 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 8a9a802fddc37a03a346079b0fb4d4b19617cfc1..fc30c42c736521f09f8e23abc3ebbca902cc140c 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 c3d7be1f566be3824099438e0c245c3d4ef1603c..32d6f516d4f00d5b9c310a71b3dadd29a362bc80 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 41366190b90b85ab0b4228345bd0e8443f5fc0ed..06827dad614928c0c4fc220e6c53e5e7360947a3 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 534386e573cea1c386ae5bd584f970c31445f136..5ca6d5c5bb4ab249845cf333bb6ab65f2c980206 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 25a63b2b379c110675c9b38d2d6144c8ac21ca34..77c3269e2d94a73a754a4efb95f6f7f52725a66a 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 4e12dc526c7a23bb36f2433a10e22b983bfca8f3..1695b2304bdb55d54f833bc682bd38b9a611200c 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 9ee96f08e7ceeddab2d9df49784508f26c0edd6b..33355854c5d9c6df4dfe65184c51e59cb476f931 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; }