From ff6b839344bb99acffb25a202b706ac4703c9609 Mon Sep 17 00:00:00 2001 From: gaobo Date: Wed, 28 Sep 2022 03:42:37 -0700 Subject: [PATCH 1/2] app cert napi layer code Signed-off-by: gaobo --- interfaces/kits/napi/BUILD.gn | 4 + interfaces/kits/napi/include/cm_napi_common.h | 1 + .../napi/include/cm_napi_get_app_cert_info.h | 3 + .../cm_napi_get_app_cert_info_common.h | 52 ++++ .../napi/include/cm_napi_get_app_cert_list.h | 3 + .../cm_napi_get_app_cert_list_common.h | 50 ++++ .../napi/include/cm_napi_install_app_cert.h | 3 + .../include/cm_napi_install_app_cert_common.h | 50 ++++ .../napi/include/cm_napi_uninstall_app_cert.h | 3 + .../cm_napi_uninstall_app_cert_common.h | 47 ++++ interfaces/kits/napi/src/cm_napi.cpp | 5 + .../napi/src/cm_napi_get_app_cert_info.cpp | 184 +------------- .../src/cm_napi_get_app_cert_info_common.cpp | 200 +++++++++++++++ .../napi/src/cm_napi_get_app_cert_list.cpp | 171 +------------ .../src/cm_napi_get_app_cert_list_common.cpp | 189 ++++++++++++++ .../napi/src/cm_napi_install_app_cert.cpp | 205 +-------------- .../src/cm_napi_install_app_cert_common.cpp | 236 ++++++++++++++++++ .../src/cm_napi_uninstall_all_app_cert.cpp | 3 +- .../napi/src/cm_napi_uninstall_app_cert.cpp | 149 +---------- .../src/cm_napi_uninstall_app_cert_common.cpp | 167 +++++++++++++ .../core/src/cert_manager_file_operator.c | 5 + .../os_dependency/idl/cm_ipc/cm_ipc_service.c | 2 +- .../os_dependency/sa/cm_event_observer.cpp | 10 +- .../main/os_dependency/sa/cm_sa.cpp | 6 + .../main/os_dependency/sa/cm_sa.h | 2 +- 25 files changed, 1056 insertions(+), 694 deletions(-) create mode 100755 interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.h create mode 100755 interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h create mode 100755 interfaces/kits/napi/include/cm_napi_install_app_cert_common.h create mode 100755 interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h create mode 100755 interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp create mode 100755 interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp create mode 100755 interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp create mode 100755 interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index d5fbf8e..be1fdf4 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -37,6 +37,10 @@ ohos_shared_library("certmanager") { "src/cm_napi_uninstall_all_app_cert.cpp", "src/cm_napi_get_app_cert_list.cpp", "src/cm_napi_get_app_cert_info.cpp", + "src/cm_napi_get_app_cert_list_common.cpp", + "src/cm_napi_get_app_cert_info_common.cpp", + "src/cm_napi_install_app_cert_common.cpp", + "src/cm_napi_uninstall_app_cert_common.cpp", ] external_deps = [ diff --git a/interfaces/kits/napi/include/cm_napi_common.h b/interfaces/kits/napi/include/cm_napi_common.h index 9f4dc25..f20fb3e 100644 --- a/interfaces/kits/napi/include/cm_napi_common.h +++ b/interfaces/kits/napi/include/cm_napi_common.h @@ -71,6 +71,7 @@ static const std::string CM_HANDLE_PROPERTY_HANDLE = "handle"; static const int32_t CERT_MANAGER_SYS_CAP = 17500000; static const int32_t RESULT_NUMBER = 2; static const uint32_t APPLICATION_CERTIFICATE_STORE = 0; +static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3; static const uint32_t SYSTEM_CERTIFICATE_STORE = 1; static const std::string PARAM_TYPE_ERROR_NUMBER = "401"; diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h index e576d12..18990d4 100644 --- a/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiGetAppCertInfo(napi_env env, napi_callback_info info); + +napi_value CMNapiGetPrivateAppCertInfo(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_GET_CREDENTIAL_INFO_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.h new file mode 100755 index 0000000..a4c7651 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.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 CM_NAPI_GET_APP_CERT_INFO_COMMON_H +#define CM_NAPI_GET_APP_CERT_INFO_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +struct GetAppCertInfoAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + + struct CmBlob *keyUri = nullptr; + uint32_t store = 0; + struct Credential *credential = nullptr; +}; +using GetAppCertInfoAsyncContext = GetAppCertInfoAsyncContextT *; + +namespace CMNapi { + +GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext(); + +void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context); + +napi_value GetAppCertInfoParseParams( + napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context); + +napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context); + +napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext context); + +napi_value CMNapiGetAppCertInfoCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_GET_APP_CERT_INFO_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h index b5b3b75..a8d3169 100644 --- a/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiGetAppCertList(napi_env env, napi_callback_info info); + +napi_value CMNapiGetPrivateAppCertList(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_GET_APP_CERTIFICATE_LIST_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h new file mode 100755 index 0000000..a7a1c00 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h @@ -0,0 +1,50 @@ +/* + * 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 CM_NAPI_GET_APP_CERTIFICATE_LIST_COMMON_H +#define CM_NAPI_GET_APP_CERTIFICATE_LIST_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "napi/native_node_api.h" + +struct GetAppCertListAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + uint32_t store = 0; + struct CredentialList *credentialList = nullptr; +}; +using GetAppCertListAsyncContext = GetAppCertListAsyncContextT *; + +namespace CMNapi { +GetAppCertListAsyncContext CreateGetAppCertListAsyncContext(); + +void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context); + +napi_value GetAppCertListParseParams( + napi_env env, napi_callback_info info, GetAppCertListAsyncContext context); + +napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context); + +napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext context); + +napi_value CMNapiGetAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_GET_APP_CERTIFICATE_LIST_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_install_app_cert.h b/interfaces/kits/napi/include/cm_napi_install_app_cert.h index ff7f864..deea9f3 100644 --- a/interfaces/kits/napi/include/cm_napi_install_app_cert.h +++ b/interfaces/kits/napi/include/cm_napi_install_app_cert.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiInstallAppCert(napi_env env, napi_callback_info info); + +napi_value CMNapiInstallPrivateAppCert(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_INSTALL_APP_CERT_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_install_app_cert_common.h b/interfaces/kits/napi/include/cm_napi_install_app_cert_common.h new file mode 100755 index 0000000..dbc3bcf --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_install_app_cert_common.h @@ -0,0 +1,50 @@ +/* + * 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 CM_NAPI_INSTALL_APP_CERT_COMMON_H +#define CM_NAPI_INSTALL_APP_CERT_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +struct InstallAppCertAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + struct CmBlob *keystore = nullptr; + struct CmBlob *keystorePwd = nullptr; + struct CmBlob *keyAlias = nullptr; + struct CmBlob *keyUri = nullptr; + uint32_t store = 0; +}; +using InstallAppCertAsyncContext = InstallAppCertAsyncContextT *; + +namespace CMNapi { +InstallAppCertAsyncContext CreateInstallAppCertAsyncContext(); + +void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context); + +napi_value InstallAppCertParseParams( + napi_env env, napi_callback_info info, InstallAppCertAsyncContext context); + +napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext context); + +napi_value CMNapiInstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_INSTALL_APP_CERT_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h index 50a3be7..0b61757 100644 --- a/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h +++ b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiUninstallAppCert(napi_env env, napi_callback_info info); + +napi_value CMNapiUninstallPrivateAppCert(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_UNINSTALL_APP_CERT_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h new file mode 100755 index 0000000..469e401 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h @@ -0,0 +1,47 @@ +/* + * 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 CM_NAPI_UNINSTALL_APP_CERT_COMMON_H +#define CM_NAPI_UNINSTALL_APP_CERT_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +struct UninstallAppCertAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + struct CmBlob *keyUri = nullptr; + uint32_t store = 0; +}; +using UninstallAppCertAsyncContext = UninstallAppCertAsyncContextT *; + +namespace CMNapi { +UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext(); + +void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context); + +napi_value UninstallAppCertParseParams( + napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context); + +napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext context); + +napi_value CMNapiUninstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_UNINSTALL_APP_CERT_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/src/cm_napi.cpp b/interfaces/kits/napi/src/cm_napi.cpp index a63ef25..ef8fd48 100644 --- a/interfaces/kits/napi/src/cm_napi.cpp +++ b/interfaces/kits/napi/src/cm_napi.cpp @@ -89,6 +89,11 @@ extern "C" { DECLARE_NAPI_FUNCTION("uninstallAppCertificate", CMNapiUninstallAppCert), DECLARE_NAPI_FUNCTION("getAppCertificateList", CMNapiGetAppCertList), DECLARE_NAPI_FUNCTION("getAppCertificate", CMNapiGetAppCertInfo), + + DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert), + DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert), + DECLARE_NAPI_FUNCTION("getPrivateCertificateList", CMNapiGetPrivateAppCertList), + DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp index a3eceaa..a479b44 100644 --- a/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp @@ -14,192 +14,20 @@ */ #include "cm_napi_get_app_cert_info.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_get_app_cert_info_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS = 1; -constexpr int CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS = 2; -} // namespace - -struct GetAppCertInfoAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - - struct CmBlob *keyUri = nullptr; - uint32_t store = 0; - struct Credential *credential = nullptr; -}; -using GetAppCertInfoAsyncContext = GetAppCertInfoAsyncContextT *; - -static GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext() -{ - GetAppCertInfoAsyncContext context = - static_cast(CmMalloc(sizeof(GetAppCertInfoAsyncContextT))); - if (context != nullptr) { - (void)memset_s(context, sizeof(GetAppCertInfoAsyncContextT), 0, sizeof(GetAppCertInfoAsyncContextT)); - } - return context; -} - -static void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->keyUri != nullptr) { - FreeCmBlob(context->keyUri); - } - - if (context->credential != nullptr) { - FreeCredential(context->credential); - } - - CmFree(context); - context = nullptr; -} - -static napi_value GetAppCertInfoParseParams( - napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context) -{ - size_t argc = CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS; - napi_value argv[CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc < CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("CertInfo Missing parameter"); - return nullptr; - } - - size_t index = 0; - napi_value result = ParseString(env, argv[index], context->keyUri); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get key uri"); - return nullptr; - } - - index++; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; /* 0 is store type, indicate application certificate */ - return GetInt32(env, 0); -} - -static napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context) -{ - napi_value result = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - napi_value certInfo = GenerateAppCertInfo(env, context->credential); - if (certInfo != nullptr) { - napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL.c_str(), certInfo); - } else { - NAPI_CALL(env, napi_get_undefined(env, &result)); - } - return result; -} - -static void InitAppCert(struct Credential *credential) +napi_value CMNapiGetAppCertInfo(napi_env env, napi_callback_info info) { - credential->credData.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE_CHAIN); - if (credential->credData.data == NULL) { - CM_LOG_E("malloc file buffer failed"); - return; - } - (void)memset_s(credential->credData.data, MAX_LEN_CERTIFICATE_CHAIN, 0, MAX_LEN_CERTIFICATE_CHAIN); - credential->credData.size = MAX_LEN_CERTIFICATE_CHAIN; + return CMNapiGetAppCertInfoCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext context) +napi_value CMNapiGetPrivateAppCertInfo(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertInfoAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - GetAppCertInfoAsyncContext context = static_cast(data); - - context->credential = (struct Credential *)CmMalloc(sizeof(struct Credential)); - if (context->credential != nullptr) { - (void)memset_s(context->credential, sizeof(struct Credential), 0, sizeof(struct Credential)); - InitAppCert(context->credential); - } - context->result = CmGetAppCert(context->keyUri, context->store, context->credential); - }, - [](napi_env env, napi_status status, void *data) { - GetAppCertInfoAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - result[1] = GetAppCertInfoWriteResult(env, context); - } else { - const char *errorMessage = "get app cert info error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteGetAppCertInfoAsyncContext(env, context); - }, - (void *)context, - &context->asyncWork)); - - napi_status status = napi_queue_async_work(env, context->asyncWork); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteGetAppCertInfoAsyncContext(env, context); - CM_LOG_E("could not queue async work"); - return nullptr; - } - - return promise; + return CMNapiGetAppCertInfoCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiGetAppCertInfo(napi_env env, napi_callback_info info) -{ - GetAppCertInfoAsyncContext context = CreateGetAppCertInfoAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = GetAppCertInfoParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteGetAppCertInfoAsyncContext(env, context); - return nullptr; - } - result = GetAppCertInfoAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteGetAppCertInfoAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp new file mode 100755 index 0000000..4a5a8ce --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp @@ -0,0 +1,200 @@ +/* + * 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 "cm_napi_get_app_cert_info.h" +#include "cm_napi_get_app_cert_info_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS = 1; +constexpr int CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS = 2; +} // namespace + +GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext() +{ + GetAppCertInfoAsyncContext context = + static_cast(CmMalloc(sizeof(GetAppCertInfoAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(GetAppCertInfoAsyncContextT), 0, sizeof(GetAppCertInfoAsyncContextT)); + } + return context; +} + +void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->keyUri != nullptr) { + FreeCmBlob(context->keyUri); + } + + if (context->credential != nullptr) { + FreeCredential(context->credential); + } + + CmFree(context); + context = nullptr; +} + +napi_value GetAppCertInfoParseParams( + napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context) +{ + size_t argc = CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS; + napi_value argv[CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("CertInfo Missing parameter"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseString(env, argv[index], context->keyUri); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get key uri"); + return nullptr; + } + + index++; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("GetAppCertInfo get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + napi_value certInfo = GenerateAppCertInfo(env, context->credential); + if (certInfo != nullptr) { + napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL.c_str(), certInfo); + } else { + NAPI_CALL(env, napi_get_undefined(env, &result)); + } + return result; +} + +static void InitAppCert(struct Credential *credential) +{ + credential->credData.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE_CHAIN); + if (credential->credData.data == NULL) { + CM_LOG_E("malloc file buffer failed"); + return; + } + (void)memset_s(credential->credData.data, MAX_LEN_CERTIFICATE_CHAIN, 0, MAX_LEN_CERTIFICATE_CHAIN); + credential->credData.size = MAX_LEN_CERTIFICATE_CHAIN; +} + +napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertInfoAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + GetAppCertInfoAsyncContext context = static_cast(data); + + context->credential = (struct Credential *)CmMalloc(sizeof(struct Credential)); + if (context->credential != nullptr) { + (void)memset_s(context->credential, sizeof(struct Credential), 0, sizeof(struct Credential)); + InitAppCert(context->credential); + } + context->result = CmGetAppCert(context->keyUri, context->store, context->credential); + }, + [](napi_env env, napi_status status, void *data) { + GetAppCertInfoAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + result[1] = GetAppCertInfoWriteResult(env, context); + } else { + const char *errorMessage = "get app cert info error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteGetAppCertInfoAsyncContext(env, context); + }, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteGetAppCertInfoAsyncContext(env, context); + CM_LOG_E("could not queue async work"); + return nullptr; + } + + return promise; +} + +napi_value CMNapiGetAppCertInfoCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + GetAppCertInfoAsyncContext context = CreateGetAppCertInfoAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = GetAppCertInfoParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteGetAppCertInfoAsyncContext(env, context); + return nullptr; + } + result = GetAppCertInfoAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteGetAppCertInfoAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp index 71ca5ca..c95c60d 100644 --- a/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp @@ -14,179 +14,20 @@ */ #include "cm_napi_get_app_cert_list.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_get_app_cert_list_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS = 0; -constexpr int CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS = 1; -} // namespace - -struct GetAppCertListAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - uint32_t store = 0; - struct CredentialList *credentialList = nullptr; -}; -using GetAppCertListAsyncContext = GetAppCertListAsyncContextT *; - -static GetAppCertListAsyncContext CreateGetAppCertListAsyncContext() -{ - GetAppCertListAsyncContext context = - static_cast(CmMalloc(sizeof(GetAppCertListAsyncContextT))); - if (context != nullptr) { - (void)memset_s(context, sizeof(GetAppCertListAsyncContextT), 0, sizeof(GetAppCertListAsyncContextT)); - } - return context; -} - -static void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->credentialList != nullptr) { - FreeCredentialList(context->credentialList); - } - - CmFree(context); - context = nullptr; -} - -static napi_value GetAppCertListParseParams( - napi_env env, napi_callback_info info, GetAppCertListAsyncContext context) -{ - size_t argc = CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS; - napi_value argv[CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc < CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("Missing parameter"); - return nullptr; - } - - size_t index = 0; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; /* 0 is store type, indicate application certificate */ - return GetInt32(env, 0); -} - -static napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context) -{ - napi_value result = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - napi_value credentail = GenerateCredentialAbstractArray(env, - context->credentialList->credentialAbstract, context->credentialList->credentialCount); - if (credentail != nullptr) { - napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL_LIST.c_str(), credentail); - } else { - NAPI_CALL(env, napi_get_undefined(env, &result)); - } - return result; -} - -static void InitAppCertList(struct CredentialList *credentialList) +napi_value CMNapiGetAppCertList(napi_env env, napi_callback_info info) { - uint32_t buffSize = (MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract)); - credentialList->credentialAbstract = (struct CredentialAbstract *)CmMalloc(buffSize); - if (credentialList->credentialAbstract == NULL) { - CM_LOG_E("malloc file buffer failed"); - return; - } - (void)memset_s(credentialList->credentialAbstract, buffSize, 0, buffSize); - credentialList->credentialCount = MAX_COUNT_CERTIFICATE; + return CMNapiGetAppCertListCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext context) +napi_value CMNapiGetPrivateAppCertList(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - GetAppCertListAsyncContext context = static_cast(data); - - context->credentialList = (struct CredentialList *)CmMalloc(sizeof(struct CredentialList)); - if (context->credentialList != nullptr) { - InitAppCertList(context->credentialList); - } - context->result = CmGetAppCertList(context->store, context->credentialList); - }, - [](napi_env env, napi_status status, void *data) { - GetAppCertListAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - result[1] = GetAppCertListWriteResult(env, context); - } else { - const char *errorMessage = "get app cert list info error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteGetAppCertListAsyncContext(env, context); - CM_LOG_I("get app cert list end"); - }, - (void *)context, - &context->asyncWork)); - - napi_status napiStatus = napi_queue_async_work(env, context->asyncWork); - if (napiStatus != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteGetAppCertListAsyncContext(env, context); - CM_LOG_E("get app cert list could not queue async work"); - return nullptr; - } - return promise; + return CMNapiGetAppCertListCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiGetAppCertList(napi_env env, napi_callback_info info) -{ - GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = GetAppCertListParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteGetAppCertListAsyncContext(env, context); - return nullptr; - } - result = GetAppCertListAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteGetAppCertListAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp new file mode 100755 index 0000000..e94cf0e --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp @@ -0,0 +1,189 @@ +/* + * 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 "cm_napi_get_app_cert_list.h" +#include "cm_napi_get_app_cert_list_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS = 0; +constexpr int CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS = 1; +} // namespace + +GetAppCertListAsyncContext CreateGetAppCertListAsyncContext() +{ + GetAppCertListAsyncContext context = + static_cast(CmMalloc(sizeof(GetAppCertListAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(GetAppCertListAsyncContextT), 0, sizeof(GetAppCertListAsyncContextT)); + } + return context; +} + +void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->credentialList != nullptr) { + FreeCredentialList(context->credentialList); + } + + CmFree(context); + context = nullptr; +} + +napi_value GetAppCertListParseParams( + napi_env env, napi_callback_info info, GetAppCertListAsyncContext context) +{ + size_t argc = CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS; + napi_value argv[CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("Missing parameter"); + return nullptr; + } + + size_t index = 0; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("GetAppCertLis get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + napi_value credentail = GenerateCredentialAbstractArray(env, + context->credentialList->credentialAbstract, context->credentialList->credentialCount); + if (credentail != nullptr) { + napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL_LIST.c_str(), credentail); + } else { + NAPI_CALL(env, napi_get_undefined(env, &result)); + } + return result; +} + +void InitAppCertList(struct CredentialList *credentialList) +{ + uint32_t buffSize = (MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract)); + credentialList->credentialAbstract = (struct CredentialAbstract *)CmMalloc(buffSize); + if (credentialList->credentialAbstract == NULL) { + CM_LOG_E("malloc file buffer failed"); + return; + } + (void)memset_s(credentialList->credentialAbstract, buffSize, 0, buffSize); + credentialList->credentialCount = MAX_COUNT_CERTIFICATE; +} + +napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + GetAppCertListAsyncContext context = static_cast(data); + + context->credentialList = (struct CredentialList *)CmMalloc(sizeof(struct CredentialList)); + if (context->credentialList != nullptr) { + InitAppCertList(context->credentialList); + } + context->result = CmGetAppCertList(context->store, context->credentialList); + }, + [](napi_env env, napi_status status, void *data) { + GetAppCertListAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + result[1] = GetAppCertListWriteResult(env, context); + } else { + const char *errorMessage = "get app cert list info error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteGetAppCertListAsyncContext(env, context); + CM_LOG_I("get app cert list end"); + }, + (void *)context, + &context->asyncWork)); + + napi_status napiStatus = napi_queue_async_work(env, context->asyncWork); + if (napiStatus != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteGetAppCertListAsyncContext(env, context); + CM_LOG_E("get app cert list could not queue async work"); + return nullptr; + } + return promise; +} + +napi_value CMNapiGetAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = GetAppCertListParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteGetAppCertListAsyncContext(env, context); + return nullptr; + } + result = GetAppCertListAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteGetAppCertListAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp b/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp index 5158837..a3a2ae9 100644 --- a/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp +++ b/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp @@ -14,213 +14,20 @@ */ #include "cm_napi_install_app_cert.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_install_app_cert_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_INSTALL_APP_CERT_MIN_ARGS = 3; -constexpr int CM_NAPI_INSTALL_APP_CERT_MAX_ARGS = 4; -} // namespace - -struct InstallAppCertAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - struct CmBlob *keystore = nullptr; - struct CmBlob *keystorePwd = nullptr; - struct CmBlob *keyAlias = nullptr; - struct CmBlob *keyUri = nullptr; - uint32_t store = 0; -}; -using InstallAppCertAsyncContext = InstallAppCertAsyncContextT *; - -static InstallAppCertAsyncContext CreateInstallAppCertAsyncContext() -{ - InstallAppCertAsyncContext context = - static_cast(CmMalloc(sizeof(InstallAppCertAsyncContextT))); - if (context != nullptr) { - (void)memset_s(context, sizeof(InstallAppCertAsyncContextT), 0, sizeof(InstallAppCertAsyncContextT)); - } - return context; -} - -static void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->keystore != nullptr) { - FreeCmBlob(context->keystore); - } - - if (context->keystorePwd != nullptr) { - FreeCmBlob(context->keystorePwd); - } - - if (context->keyAlias != nullptr) { - FreeCmBlob(context->keyAlias); - } - - if (context->keyUri != nullptr) { - FreeCmBlob(context->keyUri); - } - - CmFree(context); - context = nullptr; -} - -static napi_value InstallAppCertParseParams( - napi_env env, napi_callback_info info, InstallAppCertAsyncContext context) -{ - size_t argc = CM_NAPI_INSTALL_APP_CERT_MAX_ARGS; - napi_value argv[CM_NAPI_INSTALL_APP_CERT_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - - if (argc < CM_NAPI_INSTALL_APP_CERT_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("Missing parameter"); - return nullptr; - } - - size_t index = 0; - context->keystore = (CmBlob *)CmMalloc(sizeof(CmBlob)); - if (context->keystore == nullptr) { - CM_LOG_E("could not alloc memory"); - return nullptr; - } - (void)memset_s(context->keystore, sizeof(CmBlob), 0, sizeof(CmBlob)); - - napi_value result = GetUint8Array(env, argv[index], *context->keystore); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get keystore"); - return nullptr; - } - - index++; - result = ParseString(env, argv[index], context->keystorePwd); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get keystore Pwd"); - return nullptr; - } - index++; - result = ParseString(env, argv[index], context->keyAlias); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get uri"); - return nullptr; - } - - index++; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; - return GetInt32(env, 0); -} - -static void InitKeyUri(struct CmBlob *&keyUri) +napi_value CMNapiInstallAppCert(napi_env env, napi_callback_info info) { - keyUri = (uint8_t *)CmMalloc(sizeof(struct CmBlob)); - if (keyUri == NULL) { - CM_LOG_E("malloc keyUri buffer failed"); - return; - } - - keyUri->data = (uint8_t *)CmMalloc(MAX_LEN_URI); - if (keyUri->data == NULL) { - CM_LOG_E("malloc keyUri->data buffer failed"); - return; - } - - (void)memset_s(keyUri->data, MAX_LEN_URI, 0, MAX_LEN_URI); - keyUri->size = MAX_LEN_URI; + return CMNapiInstallAppCertCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext context) +napi_value CMNapiInstallPrivateAppCert(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "InstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - InstallAppCertAsyncContext context = static_cast(data); - InitKeyUri(context->keyUri); - context->result = CmInstallAppCert(context->keystore, - context->keystorePwd, context->keyAlias, context->store, context->keyUri); - }, - [](napi_env env, napi_status status, void *data) { - InstallAppCertAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1])); - } else { - const char *errorMessage = "install app cert error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteInstallAppCertAsyncContext(env, context); - }, - (void *)context, - &context->asyncWork)); - - napi_status status = napi_queue_async_work(env, context->asyncWork); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteInstallAppCertAsyncContext(env, context); - CM_LOG_E("could not queue async work"); - return nullptr; - } - return promise; + return CMNapiInstallAppCertCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiInstallAppCert(napi_env env, napi_callback_info info) -{ - InstallAppCertAsyncContext context = CreateInstallAppCertAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = InstallAppCertParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteInstallAppCertAsyncContext(env, context); - return nullptr; - } - result = InstallAppCertAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteInstallAppCertAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp b/interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp new file mode 100755 index 0000000..46d5f20 --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp @@ -0,0 +1,236 @@ +/* + * 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 "cm_napi_install_app_cert.h" +#include "cm_napi_install_app_cert_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_INSTALL_APP_CERT_MIN_ARGS = 3; +constexpr int CM_NAPI_INSTALL_APP_CERT_MAX_ARGS = 4; +} // namespace + +InstallAppCertAsyncContext CreateInstallAppCertAsyncContext() +{ + InstallAppCertAsyncContext context = + static_cast(CmMalloc(sizeof(InstallAppCertAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(InstallAppCertAsyncContextT), 0, sizeof(InstallAppCertAsyncContextT)); + } + return context; +} + +void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->keystore != nullptr) { + FreeCmBlob(context->keystore); + } + + if (context->keystorePwd != nullptr) { + FreeCmBlob(context->keystorePwd); + } + + if (context->keyAlias != nullptr) { + FreeCmBlob(context->keyAlias); + } + + if (context->keyUri != nullptr) { + FreeCmBlob(context->keyUri); + } + + CmFree(context); + context = nullptr; +} + +napi_value InstallAppCertParseParams( + napi_env env, napi_callback_info info, InstallAppCertAsyncContext context) +{ + size_t argc = CM_NAPI_INSTALL_APP_CERT_MAX_ARGS; + napi_value argv[CM_NAPI_INSTALL_APP_CERT_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_INSTALL_APP_CERT_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("Missing parameter"); + return nullptr; + } + + size_t index = 0; + context->keystore = (CmBlob *)CmMalloc(sizeof(CmBlob)); + if (context->keystore == nullptr) { + CM_LOG_E("could not alloc memory"); + return nullptr; + } + (void)memset_s(context->keystore, sizeof(CmBlob), 0, sizeof(CmBlob)); + + napi_value result = GetUint8Array(env, argv[index], *context->keystore); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get keystore"); + return nullptr; + } + + index++; + result = ParseString(env, argv[index], context->keystorePwd); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get keystore Pwd"); + return nullptr; + } + + index++; + result = ParseString(env, argv[index], context->keyAlias); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get uri"); + return nullptr; + } + + index++; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E(" InstallAppCert get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +static void InitKeyUri(struct CmBlob *&keyUri) +{ + keyUri = (struct CmBlob *)CmMalloc(sizeof(struct CmBlob)); + if (keyUri == NULL) { + CM_LOG_E("malloc keyUri buffer failed"); + return; + } + + keyUri->data = (uint8_t *)CmMalloc(MAX_LEN_URI); + if (keyUri->data == NULL) { + CM_LOG_E("malloc keyUri->data buffer failed"); + return; + } + + (void)memset_s(keyUri->data, MAX_LEN_URI, 0, MAX_LEN_URI); + keyUri->size = MAX_LEN_URI; +} + +static napi_value InstallAppCertWriteResult(napi_env env, InstallAppCertAsyncContext context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + + napi_value keyUri = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, (char *)context->keyUri->data, + context->keyUri->size, &keyUri)); + if (keyUri != nullptr) { + napi_set_named_property(env, result, CM_CERT_PROPERTY_URI.c_str(), keyUri); + } else { + NAPI_CALL(env, napi_get_undefined(env, &result)); + } + return result; +} + +napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "InstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + InstallAppCertAsyncContext context = static_cast(data); + InitKeyUri(context->keyUri); + context->result = CmInstallAppCert(context->keystore, + context->keystorePwd, context->keyAlias, context->store, context->keyUri); + }, + [](napi_env env, napi_status status, void *data) { + InstallAppCertAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + result[1] = InstallAppCertWriteResult(env, context); + } else { + const char *errorMessage = "install app cert error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteInstallAppCertAsyncContext(env, context); + }, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteInstallAppCertAsyncContext(env, context); + CM_LOG_E("could not queue async work"); + return nullptr; + } + return promise; +} + +napi_value CMNapiInstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + InstallAppCertAsyncContext context = CreateInstallAppCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = InstallAppCertParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteInstallAppCertAsyncContext(env, context); + return nullptr; + } + result = InstallAppCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteInstallAppCertAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp b/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp index f5ef134..6bf6619 100644 --- a/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp +++ b/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp @@ -97,8 +97,7 @@ static napi_value UninstallAllAppCertAsyncWork(napi_env env, UninstallAllAppCert resourceName, [](napi_env env, void *data) { UninstallAllAppCertAsyncContext context = static_cast(data); - /* fix CmUninstallAllAppCert */ - context->result = 0; + context->result = CmUninstallAllAppCert(); }, [](napi_env env, napi_status status, void *data) { UninstallAllAppCertAsyncContext context = static_cast(data); diff --git a/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp index 083c849..6fc3052 100644 --- a/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp +++ b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp @@ -14,157 +14,20 @@ */ #include "cm_napi_uninstall_app_cert.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_uninstall_app_cert_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS = 1; -constexpr int CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS = 2; -} // namespace - -struct UninstallAppCertAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - struct CmBlob *keyUri = nullptr; - uint32_t store = 0; -}; -using UninstallAppCertAsyncContext = UninstallAppCertAsyncContextT *; - -static UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext() -{ - UninstallAppCertAsyncContext context = - static_cast(CmMalloc(sizeof(UninstallAppCertAsyncContextT))); - if (context != nullptr) { - (void)memset_s( - context, sizeof(UninstallAppCertAsyncContextT), 0, sizeof(UninstallAppCertAsyncContextT)); - } - return context; -} - -static void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->keyUri != nullptr) { - FreeCmBlob(context->keyUri); - } - - CmFree(context); - context = nullptr; -} -static napi_value UninstallAppCertParseParams( - napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context) +napi_value CMNapiUninstallAppCert(napi_env env, napi_callback_info info) { - size_t argc = CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS; - napi_value argv[CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - - if (argc < CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("Missing parameter"); - return nullptr; - } - - size_t index = 0; - napi_value result = ParseString(env, argv[index], context->keyUri); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get cert uri"); - return nullptr; - } - - index++; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; /* 0 is store type, indicate app certificate */ - return GetInt32(env, 0); + return CMNapiUninstallAppCertCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext context) +napi_value CMNapiUninstallPrivateAppCert(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - UninstallAppCertAsyncContext context = static_cast(data); - context->result = CmUninstallAppCert(context->keyUri, context->store); - }, - [](napi_env env, napi_status status, void *data) { - UninstallAppCertAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1])); - } else { - const char *errorMessage = "uninstall app cert error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteUninstallAppCertAsyncContext(env, context); - }, - (void *)context, - &context->asyncWork)); - - napi_status status = napi_queue_async_work(env, context->asyncWork); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteUninstallAppCertAsyncContext(env, context); - CM_LOG_E("could not queue async work"); - return nullptr; - } - - return promise; + return CMNapiUninstallAppCertCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiUninstallAppCert(napi_env env, napi_callback_info info) -{ - UninstallAppCertAsyncContext context = CreateUninstallAppCertAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = UninstallAppCertParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteUninstallAppCertAsyncContext(env, context); - return nullptr; - } - result = UninstallAppCertAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteUninstallAppCertAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp new file mode 100755 index 0000000..b8a3eef --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp @@ -0,0 +1,167 @@ +/* + * 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 "cm_napi_uninstall_app_cert.h" +#include "cm_napi_uninstall_app_cert_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS = 1; +constexpr int CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS = 2; +} // namespace + +UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext() +{ + UninstallAppCertAsyncContext context = + static_cast(CmMalloc(sizeof(UninstallAppCertAsyncContextT))); + if (context != nullptr) { + (void)memset_s( + context, sizeof(UninstallAppCertAsyncContextT), 0, sizeof(UninstallAppCertAsyncContextT)); + } + return context; +} + +void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->keyUri != nullptr) { + FreeCmBlob(context->keyUri); + } + + CmFree(context); + context = nullptr; +} + +napi_value UninstallAppCertParseParams( + napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context) +{ + size_t argc = CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS; + napi_value argv[CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("Missing parameter"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseString(env, argv[index], context->keyUri); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get cert uri"); + return nullptr; + } + + index++; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("UninstallAppCert get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + UninstallAppCertAsyncContext context = static_cast(data); + context->result = CmUninstallAppCert(context->keyUri, context->store); + }, + [](napi_env env, napi_status status, void *data) { + UninstallAppCertAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1])); + } else { + const char *errorMessage = "uninstall app cert error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteUninstallAppCertAsyncContext(env, context); + }, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteUninstallAppCertAsyncContext(env, context); + CM_LOG_E("could not queue async work"); + return nullptr; + } + + return promise; +} + +napi_value CMNapiUninstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + UninstallAppCertAsyncContext context = CreateUninstallAppCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = UninstallAppCertParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteUninstallAppCertAsyncContext(env, context); + return nullptr; + } + result = UninstallAppCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteUninstallAppCertAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c index 73d8f93..caab0fe 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c @@ -331,6 +331,11 @@ static int32_t CmUidLayerGetFileNames(const char *filePath, struct CmBlob *fileN return CMR_ERROR_BUFFER_TOO_SMALL; } uint32_t filePathLen = strlen(filePath); + if (filePathLen >= CM_MAX_FILE_NAME_LEN) { + CM_LOG_E("CmUidLayerGetFileNames filePathLen:%u", filePathLen); + return CMR_ERROR_BUFFER_TOO_SMALL; + } + fileNames[count].data = (uint8_t *)CMMalloc(filePathLen + 1); if (fileNames[count].data == NULL) { return CMR_ERROR_MALLOC_FAIL; diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c index 7efe325..e93d83b 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c @@ -544,7 +544,7 @@ static int32_t CmInstallAppCertCheck(const struct CmBlob *appCert, const struct return CMR_ERROR_INVALID_ARGUMENT; } - if (appCert->size == 0 || appCert->size == 0 ||appCert->size == 0 || + if (appCert->size == 0 || appCertPwd->size == 0 || certAlias->size == 0 || appCert->size > MAX_LEN_APP_CERT || appCertPwd->size > MAX_LEN_APP_CERT_PASSWD || certAlias->size > MAX_LEN_CERT_ALIAS) { CM_LOG_E("CmInstallAppCertCheck paramSet check fail, appCert:%u, appCertPwd:%u, certAlias:%u", diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp index 4993349..be71ff6 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp @@ -36,7 +36,7 @@ SystemEventSubscriber::SystemEventSubscriber(const OHOS::EventFwk::CommonEventSu void SystemEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) { - int uid; + int userId; struct CmContext context; context.userId = INVALID_VALUE; auto want = data.GetWant(); @@ -44,13 +44,13 @@ void SystemEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { context.uid = (uint32_t)want.GetIntParam(AppExecFwk::Constants::UID, -1); - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(context.uid, uid); - context.userId = (uint32_t)uid; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(context.uid, userId); + context.userId = (uint32_t)userId; CM_LOG_I("CmService package removed: uid is %u userId is %u", context.uid, context.userId); CmDeleteProcessInfo(&context); } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - int userId = data.GetCode(); - CM_LOG_I("CmService user removed: userId is %d", userId); + context.userId = data.GetCode(); + CM_LOG_I("CmService user removed: userId is %d", context.userId); CmDeleteProcessInfo(&context); } } diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp index 71a5e3a..6c5c6f3 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp @@ -174,6 +174,12 @@ int CertManagerService::OnRemoteRequest(uint32_t code, MessageParcel &data, // this is the temporary version which comments the descriptor check std::u16string descriptor = CertManagerService::GetDescriptor(); std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + CM_LOG_E("descriptor is diff"); + return HW_SYSTEM_ERROR; + } + + CM_LOG_I("OnRemoteRequest code:%u", code); uint32_t outSize = (uint32_t)data.ReadUint32(); struct CmBlob srcData = { 0, nullptr }; diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h index 1aefab2..1c0ee55 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h @@ -38,7 +38,7 @@ constexpr int SA_ID_KEYSTORE_SERVICE = 3512; class ICertManagerService : public IRemoteBroker { public: - DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.CertManager.service"); + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.cm.service"); }; class CertManagerService : public SystemAbility, public IRemoteStub { -- Gitee From 7c56254172ea8596c671b56cb8487264a6307ab7 Mon Sep 17 00:00:00 2001 From: gaobo Date: Wed, 28 Sep 2022 03:42:37 -0700 Subject: [PATCH 2/2] app cert napi layer code Signed-off-by: gaobo --- interfaces/kits/napi/BUILD.gn | 4 + interfaces/kits/napi/include/cm_napi_common.h | 1 + .../napi/include/cm_napi_get_app_cert_info.h | 3 + .../cm_napi_get_app_cert_info_common.h | 52 ++++ .../napi/include/cm_napi_get_app_cert_list.h | 3 + .../cm_napi_get_app_cert_list_common.h | 50 ++++ .../napi/include/cm_napi_install_app_cert.h | 3 + .../include/cm_napi_install_app_cert_common.h | 50 ++++ .../napi/include/cm_napi_uninstall_app_cert.h | 3 + .../cm_napi_uninstall_app_cert_common.h | 47 ++++ interfaces/kits/napi/src/cm_napi.cpp | 5 + .../napi/src/cm_napi_get_app_cert_info.cpp | 184 +------------- .../src/cm_napi_get_app_cert_info_common.cpp | 200 +++++++++++++++ .../napi/src/cm_napi_get_app_cert_list.cpp | 171 +------------ .../src/cm_napi_get_app_cert_list_common.cpp | 189 ++++++++++++++ .../napi/src/cm_napi_install_app_cert.cpp | 205 +-------------- .../src/cm_napi_install_app_cert_common.cpp | 236 ++++++++++++++++++ .../src/cm_napi_uninstall_all_app_cert.cpp | 3 +- .../napi/src/cm_napi_uninstall_app_cert.cpp | 149 +---------- .../src/cm_napi_uninstall_app_cert_common.cpp | 167 +++++++++++++ .../core/src/cert_manager_file_operator.c | 5 + .../os_dependency/idl/cm_ipc/cm_ipc_service.c | 2 +- .../os_dependency/sa/cm_event_observer.cpp | 10 +- .../main/os_dependency/sa/cm_sa.cpp | 6 + .../main/os_dependency/sa/cm_sa.h | 2 +- 25 files changed, 1056 insertions(+), 694 deletions(-) create mode 100755 interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.h create mode 100755 interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h create mode 100755 interfaces/kits/napi/include/cm_napi_install_app_cert_common.h create mode 100755 interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h create mode 100755 interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp create mode 100755 interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp create mode 100755 interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp create mode 100755 interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index d5fbf8e..be1fdf4 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -37,6 +37,10 @@ ohos_shared_library("certmanager") { "src/cm_napi_uninstall_all_app_cert.cpp", "src/cm_napi_get_app_cert_list.cpp", "src/cm_napi_get_app_cert_info.cpp", + "src/cm_napi_get_app_cert_list_common.cpp", + "src/cm_napi_get_app_cert_info_common.cpp", + "src/cm_napi_install_app_cert_common.cpp", + "src/cm_napi_uninstall_app_cert_common.cpp", ] external_deps = [ diff --git a/interfaces/kits/napi/include/cm_napi_common.h b/interfaces/kits/napi/include/cm_napi_common.h index 9f4dc25..f20fb3e 100644 --- a/interfaces/kits/napi/include/cm_napi_common.h +++ b/interfaces/kits/napi/include/cm_napi_common.h @@ -71,6 +71,7 @@ static const std::string CM_HANDLE_PROPERTY_HANDLE = "handle"; static const int32_t CERT_MANAGER_SYS_CAP = 17500000; static const int32_t RESULT_NUMBER = 2; static const uint32_t APPLICATION_CERTIFICATE_STORE = 0; +static const uint32_t APPLICATION_PRIVATE_CERTIFICATE_STORE = 3; static const uint32_t SYSTEM_CERTIFICATE_STORE = 1; static const std::string PARAM_TYPE_ERROR_NUMBER = "401"; diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h index e576d12..18990d4 100644 --- a/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_info.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiGetAppCertInfo(napi_env env, napi_callback_info info); + +napi_value CMNapiGetPrivateAppCertInfo(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_GET_CREDENTIAL_INFO_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.h new file mode 100755 index 0000000..a4c7651 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_info_common.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 CM_NAPI_GET_APP_CERT_INFO_COMMON_H +#define CM_NAPI_GET_APP_CERT_INFO_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +struct GetAppCertInfoAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + + struct CmBlob *keyUri = nullptr; + uint32_t store = 0; + struct Credential *credential = nullptr; +}; +using GetAppCertInfoAsyncContext = GetAppCertInfoAsyncContextT *; + +namespace CMNapi { + +GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext(); + +void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context); + +napi_value GetAppCertInfoParseParams( + napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context); + +napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context); + +napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext context); + +napi_value CMNapiGetAppCertInfoCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_GET_APP_CERT_INFO_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h index b5b3b75..a8d3169 100644 --- a/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_list.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiGetAppCertList(napi_env env, napi_callback_info info); + +napi_value CMNapiGetPrivateAppCertList(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_GET_APP_CERTIFICATE_LIST_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h b/interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h new file mode 100755 index 0000000..a7a1c00 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_get_app_cert_list_common.h @@ -0,0 +1,50 @@ +/* + * 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 CM_NAPI_GET_APP_CERTIFICATE_LIST_COMMON_H +#define CM_NAPI_GET_APP_CERTIFICATE_LIST_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "napi/native_node_api.h" + +struct GetAppCertListAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + uint32_t store = 0; + struct CredentialList *credentialList = nullptr; +}; +using GetAppCertListAsyncContext = GetAppCertListAsyncContextT *; + +namespace CMNapi { +GetAppCertListAsyncContext CreateGetAppCertListAsyncContext(); + +void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context); + +napi_value GetAppCertListParseParams( + napi_env env, napi_callback_info info, GetAppCertListAsyncContext context); + +napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context); + +napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext context); + +napi_value CMNapiGetAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_GET_APP_CERTIFICATE_LIST_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_install_app_cert.h b/interfaces/kits/napi/include/cm_napi_install_app_cert.h index ff7f864..deea9f3 100644 --- a/interfaces/kits/napi/include/cm_napi_install_app_cert.h +++ b/interfaces/kits/napi/include/cm_napi_install_app_cert.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiInstallAppCert(napi_env env, napi_callback_info info); + +napi_value CMNapiInstallPrivateAppCert(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_INSTALL_APP_CERT_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_install_app_cert_common.h b/interfaces/kits/napi/include/cm_napi_install_app_cert_common.h new file mode 100755 index 0000000..dbc3bcf --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_install_app_cert_common.h @@ -0,0 +1,50 @@ +/* + * 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 CM_NAPI_INSTALL_APP_CERT_COMMON_H +#define CM_NAPI_INSTALL_APP_CERT_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +struct InstallAppCertAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + struct CmBlob *keystore = nullptr; + struct CmBlob *keystorePwd = nullptr; + struct CmBlob *keyAlias = nullptr; + struct CmBlob *keyUri = nullptr; + uint32_t store = 0; +}; +using InstallAppCertAsyncContext = InstallAppCertAsyncContextT *; + +namespace CMNapi { +InstallAppCertAsyncContext CreateInstallAppCertAsyncContext(); + +void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context); + +napi_value InstallAppCertParseParams( + napi_env env, napi_callback_info info, InstallAppCertAsyncContext context); + +napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext context); + +napi_value CMNapiInstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_INSTALL_APP_CERT_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h index 50a3be7..0b61757 100644 --- a/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h +++ b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert.h @@ -21,6 +21,9 @@ namespace CMNapi { napi_value CMNapiUninstallAppCert(napi_env env, napi_callback_info info); + +napi_value CMNapiUninstallPrivateAppCert(napi_env env, napi_callback_info info); + } // namespace CertManagerNapi #endif // CM_NAPI_UNINSTALL_APP_CERT_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h new file mode 100755 index 0000000..469e401 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_uninstall_app_cert_common.h @@ -0,0 +1,47 @@ +/* + * 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 CM_NAPI_UNINSTALL_APP_CERT_COMMON_H +#define CM_NAPI_UNINSTALL_APP_CERT_COMMON_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +struct UninstallAppCertAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t result = 0; + struct CmBlob *keyUri = nullptr; + uint32_t store = 0; +}; +using UninstallAppCertAsyncContext = UninstallAppCertAsyncContextT *; + +namespace CMNapi { +UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext(); + +void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context); + +napi_value UninstallAppCertParseParams( + napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context); + +napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext context); + +napi_value CMNapiUninstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store); + +} // namespace CertManagerNapi + +#endif // CM_NAPI_UNINSTALL_APP_CERT_COMMON_H \ No newline at end of file diff --git a/interfaces/kits/napi/src/cm_napi.cpp b/interfaces/kits/napi/src/cm_napi.cpp index a63ef25..ef8fd48 100644 --- a/interfaces/kits/napi/src/cm_napi.cpp +++ b/interfaces/kits/napi/src/cm_napi.cpp @@ -89,6 +89,11 @@ extern "C" { DECLARE_NAPI_FUNCTION("uninstallAppCertificate", CMNapiUninstallAppCert), DECLARE_NAPI_FUNCTION("getAppCertificateList", CMNapiGetAppCertList), DECLARE_NAPI_FUNCTION("getAppCertificate", CMNapiGetAppCertInfo), + + DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert), + DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert), + DECLARE_NAPI_FUNCTION("getPrivateCertificateList", CMNapiGetPrivateAppCertList), + DECLARE_NAPI_FUNCTION("getPrivateCertificate", CMNapiGetPrivateAppCertInfo), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp index a3eceaa..a479b44 100644 --- a/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_info.cpp @@ -14,192 +14,20 @@ */ #include "cm_napi_get_app_cert_info.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_get_app_cert_info_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS = 1; -constexpr int CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS = 2; -} // namespace - -struct GetAppCertInfoAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - - struct CmBlob *keyUri = nullptr; - uint32_t store = 0; - struct Credential *credential = nullptr; -}; -using GetAppCertInfoAsyncContext = GetAppCertInfoAsyncContextT *; - -static GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext() -{ - GetAppCertInfoAsyncContext context = - static_cast(CmMalloc(sizeof(GetAppCertInfoAsyncContextT))); - if (context != nullptr) { - (void)memset_s(context, sizeof(GetAppCertInfoAsyncContextT), 0, sizeof(GetAppCertInfoAsyncContextT)); - } - return context; -} - -static void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->keyUri != nullptr) { - FreeCmBlob(context->keyUri); - } - - if (context->credential != nullptr) { - FreeCredential(context->credential); - } - - CmFree(context); - context = nullptr; -} - -static napi_value GetAppCertInfoParseParams( - napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context) -{ - size_t argc = CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS; - napi_value argv[CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc < CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("CertInfo Missing parameter"); - return nullptr; - } - - size_t index = 0; - napi_value result = ParseString(env, argv[index], context->keyUri); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get key uri"); - return nullptr; - } - - index++; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; /* 0 is store type, indicate application certificate */ - return GetInt32(env, 0); -} - -static napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context) -{ - napi_value result = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - napi_value certInfo = GenerateAppCertInfo(env, context->credential); - if (certInfo != nullptr) { - napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL.c_str(), certInfo); - } else { - NAPI_CALL(env, napi_get_undefined(env, &result)); - } - return result; -} - -static void InitAppCert(struct Credential *credential) +napi_value CMNapiGetAppCertInfo(napi_env env, napi_callback_info info) { - credential->credData.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE_CHAIN); - if (credential->credData.data == NULL) { - CM_LOG_E("malloc file buffer failed"); - return; - } - (void)memset_s(credential->credData.data, MAX_LEN_CERTIFICATE_CHAIN, 0, MAX_LEN_CERTIFICATE_CHAIN); - credential->credData.size = MAX_LEN_CERTIFICATE_CHAIN; + return CMNapiGetAppCertInfoCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext context) +napi_value CMNapiGetPrivateAppCertInfo(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertInfoAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - GetAppCertInfoAsyncContext context = static_cast(data); - - context->credential = (struct Credential *)CmMalloc(sizeof(struct Credential)); - if (context->credential != nullptr) { - (void)memset_s(context->credential, sizeof(struct Credential), 0, sizeof(struct Credential)); - InitAppCert(context->credential); - } - context->result = CmGetAppCert(context->keyUri, context->store, context->credential); - }, - [](napi_env env, napi_status status, void *data) { - GetAppCertInfoAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - result[1] = GetAppCertInfoWriteResult(env, context); - } else { - const char *errorMessage = "get app cert info error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteGetAppCertInfoAsyncContext(env, context); - }, - (void *)context, - &context->asyncWork)); - - napi_status status = napi_queue_async_work(env, context->asyncWork); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteGetAppCertInfoAsyncContext(env, context); - CM_LOG_E("could not queue async work"); - return nullptr; - } - - return promise; + return CMNapiGetAppCertInfoCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiGetAppCertInfo(napi_env env, napi_callback_info info) -{ - GetAppCertInfoAsyncContext context = CreateGetAppCertInfoAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = GetAppCertInfoParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteGetAppCertInfoAsyncContext(env, context); - return nullptr; - } - result = GetAppCertInfoAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteGetAppCertInfoAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp new file mode 100755 index 0000000..4a5a8ce --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_info_common.cpp @@ -0,0 +1,200 @@ +/* + * 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 "cm_napi_get_app_cert_info.h" +#include "cm_napi_get_app_cert_info_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS = 1; +constexpr int CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS = 2; +} // namespace + +GetAppCertInfoAsyncContext CreateGetAppCertInfoAsyncContext() +{ + GetAppCertInfoAsyncContext context = + static_cast(CmMalloc(sizeof(GetAppCertInfoAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(GetAppCertInfoAsyncContextT), 0, sizeof(GetAppCertInfoAsyncContextT)); + } + return context; +} + +void DeleteGetAppCertInfoAsyncContext(napi_env env, GetAppCertInfoAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->keyUri != nullptr) { + FreeCmBlob(context->keyUri); + } + + if (context->credential != nullptr) { + FreeCredential(context->credential); + } + + CmFree(context); + context = nullptr; +} + +napi_value GetAppCertInfoParseParams( + napi_env env, napi_callback_info info, GetAppCertInfoAsyncContext context) +{ + size_t argc = CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS; + napi_value argv[CM_NAPI_GET_APP_CERT_INFO_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_GET_APP_CERT_INFO_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("CertInfo Missing parameter"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseString(env, argv[index], context->keyUri); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get key uri"); + return nullptr; + } + + index++; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("GetAppCertInfo get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +napi_value GetAppCertInfoWriteResult(napi_env env, GetAppCertInfoAsyncContext context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + napi_value certInfo = GenerateAppCertInfo(env, context->credential); + if (certInfo != nullptr) { + napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL.c_str(), certInfo); + } else { + NAPI_CALL(env, napi_get_undefined(env, &result)); + } + return result; +} + +static void InitAppCert(struct Credential *credential) +{ + credential->credData.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE_CHAIN); + if (credential->credData.data == NULL) { + CM_LOG_E("malloc file buffer failed"); + return; + } + (void)memset_s(credential->credData.data, MAX_LEN_CERTIFICATE_CHAIN, 0, MAX_LEN_CERTIFICATE_CHAIN); + credential->credData.size = MAX_LEN_CERTIFICATE_CHAIN; +} + +napi_value GetAppCertInfoAsyncWork(napi_env env, GetAppCertInfoAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertInfoAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + GetAppCertInfoAsyncContext context = static_cast(data); + + context->credential = (struct Credential *)CmMalloc(sizeof(struct Credential)); + if (context->credential != nullptr) { + (void)memset_s(context->credential, sizeof(struct Credential), 0, sizeof(struct Credential)); + InitAppCert(context->credential); + } + context->result = CmGetAppCert(context->keyUri, context->store, context->credential); + }, + [](napi_env env, napi_status status, void *data) { + GetAppCertInfoAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + result[1] = GetAppCertInfoWriteResult(env, context); + } else { + const char *errorMessage = "get app cert info error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteGetAppCertInfoAsyncContext(env, context); + }, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteGetAppCertInfoAsyncContext(env, context); + CM_LOG_E("could not queue async work"); + return nullptr; + } + + return promise; +} + +napi_value CMNapiGetAppCertInfoCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + GetAppCertInfoAsyncContext context = CreateGetAppCertInfoAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = GetAppCertInfoParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteGetAppCertInfoAsyncContext(env, context); + return nullptr; + } + result = GetAppCertInfoAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteGetAppCertInfoAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp index 71ca5ca..c95c60d 100644 --- a/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_list.cpp @@ -14,179 +14,20 @@ */ #include "cm_napi_get_app_cert_list.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_get_app_cert_list_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS = 0; -constexpr int CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS = 1; -} // namespace - -struct GetAppCertListAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - uint32_t store = 0; - struct CredentialList *credentialList = nullptr; -}; -using GetAppCertListAsyncContext = GetAppCertListAsyncContextT *; - -static GetAppCertListAsyncContext CreateGetAppCertListAsyncContext() -{ - GetAppCertListAsyncContext context = - static_cast(CmMalloc(sizeof(GetAppCertListAsyncContextT))); - if (context != nullptr) { - (void)memset_s(context, sizeof(GetAppCertListAsyncContextT), 0, sizeof(GetAppCertListAsyncContextT)); - } - return context; -} - -static void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->credentialList != nullptr) { - FreeCredentialList(context->credentialList); - } - - CmFree(context); - context = nullptr; -} - -static napi_value GetAppCertListParseParams( - napi_env env, napi_callback_info info, GetAppCertListAsyncContext context) -{ - size_t argc = CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS; - napi_value argv[CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc < CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("Missing parameter"); - return nullptr; - } - - size_t index = 0; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; /* 0 is store type, indicate application certificate */ - return GetInt32(env, 0); -} - -static napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context) -{ - napi_value result = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - napi_value credentail = GenerateCredentialAbstractArray(env, - context->credentialList->credentialAbstract, context->credentialList->credentialCount); - if (credentail != nullptr) { - napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL_LIST.c_str(), credentail); - } else { - NAPI_CALL(env, napi_get_undefined(env, &result)); - } - return result; -} - -static void InitAppCertList(struct CredentialList *credentialList) +napi_value CMNapiGetAppCertList(napi_env env, napi_callback_info info) { - uint32_t buffSize = (MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract)); - credentialList->credentialAbstract = (struct CredentialAbstract *)CmMalloc(buffSize); - if (credentialList->credentialAbstract == NULL) { - CM_LOG_E("malloc file buffer failed"); - return; - } - (void)memset_s(credentialList->credentialAbstract, buffSize, 0, buffSize); - credentialList->credentialCount = MAX_COUNT_CERTIFICATE; + return CMNapiGetAppCertListCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext context) +napi_value CMNapiGetPrivateAppCertList(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - GetAppCertListAsyncContext context = static_cast(data); - - context->credentialList = (struct CredentialList *)CmMalloc(sizeof(struct CredentialList)); - if (context->credentialList != nullptr) { - InitAppCertList(context->credentialList); - } - context->result = CmGetAppCertList(context->store, context->credentialList); - }, - [](napi_env env, napi_status status, void *data) { - GetAppCertListAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - result[1] = GetAppCertListWriteResult(env, context); - } else { - const char *errorMessage = "get app cert list info error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteGetAppCertListAsyncContext(env, context); - CM_LOG_I("get app cert list end"); - }, - (void *)context, - &context->asyncWork)); - - napi_status napiStatus = napi_queue_async_work(env, context->asyncWork); - if (napiStatus != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteGetAppCertListAsyncContext(env, context); - CM_LOG_E("get app cert list could not queue async work"); - return nullptr; - } - return promise; + return CMNapiGetAppCertListCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiGetAppCertList(napi_env env, napi_callback_info info) -{ - GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = GetAppCertListParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteGetAppCertListAsyncContext(env, context); - return nullptr; - } - result = GetAppCertListAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteGetAppCertListAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp b/interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp new file mode 100755 index 0000000..e94cf0e --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_get_app_cert_list_common.cpp @@ -0,0 +1,189 @@ +/* + * 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 "cm_napi_get_app_cert_list.h" +#include "cm_napi_get_app_cert_list_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS = 0; +constexpr int CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS = 1; +} // namespace + +GetAppCertListAsyncContext CreateGetAppCertListAsyncContext() +{ + GetAppCertListAsyncContext context = + static_cast(CmMalloc(sizeof(GetAppCertListAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(GetAppCertListAsyncContextT), 0, sizeof(GetAppCertListAsyncContextT)); + } + return context; +} + +void DeleteGetAppCertListAsyncContext(napi_env env, GetAppCertListAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->credentialList != nullptr) { + FreeCredentialList(context->credentialList); + } + + CmFree(context); + context = nullptr; +} + +napi_value GetAppCertListParseParams( + napi_env env, napi_callback_info info, GetAppCertListAsyncContext context) +{ + size_t argc = CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS; + napi_value argv[CM_NAPI_GET_APP_CERT_LIST_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_GET_APP_CERT_LIST_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("Missing parameter"); + return nullptr; + } + + size_t index = 0; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("GetAppCertLis get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +napi_value GetAppCertListWriteResult(napi_env env, GetAppCertListAsyncContext context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + napi_value credentail = GenerateCredentialAbstractArray(env, + context->credentialList->credentialAbstract, context->credentialList->credentialCount); + if (credentail != nullptr) { + napi_set_named_property(env, result, CM_RESULT_PRPPERTY_CREDENTIAL_LIST.c_str(), credentail); + } else { + NAPI_CALL(env, napi_get_undefined(env, &result)); + } + return result; +} + +void InitAppCertList(struct CredentialList *credentialList) +{ + uint32_t buffSize = (MAX_COUNT_CERTIFICATE * sizeof(struct CredentialAbstract)); + credentialList->credentialAbstract = (struct CredentialAbstract *)CmMalloc(buffSize); + if (credentialList->credentialAbstract == NULL) { + CM_LOG_E("malloc file buffer failed"); + return; + } + (void)memset_s(credentialList->credentialAbstract, buffSize, 0, buffSize); + credentialList->credentialCount = MAX_COUNT_CERTIFICATE; +} + +napi_value GetAppCertListAsyncWork(napi_env env, GetAppCertListAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "GetAppCertListAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + GetAppCertListAsyncContext context = static_cast(data); + + context->credentialList = (struct CredentialList *)CmMalloc(sizeof(struct CredentialList)); + if (context->credentialList != nullptr) { + InitAppCertList(context->credentialList); + } + context->result = CmGetAppCertList(context->store, context->credentialList); + }, + [](napi_env env, napi_status status, void *data) { + GetAppCertListAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + result[1] = GetAppCertListWriteResult(env, context); + } else { + const char *errorMessage = "get app cert list info error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteGetAppCertListAsyncContext(env, context); + CM_LOG_I("get app cert list end"); + }, + (void *)context, + &context->asyncWork)); + + napi_status napiStatus = napi_queue_async_work(env, context->asyncWork); + if (napiStatus != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteGetAppCertListAsyncContext(env, context); + CM_LOG_E("get app cert list could not queue async work"); + return nullptr; + } + return promise; +} + +napi_value CMNapiGetAppCertListCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + GetAppCertListAsyncContext context = CreateGetAppCertListAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = GetAppCertListParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteGetAppCertListAsyncContext(env, context); + return nullptr; + } + result = GetAppCertListAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteGetAppCertListAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp b/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp index 5158837..a3a2ae9 100644 --- a/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp +++ b/interfaces/kits/napi/src/cm_napi_install_app_cert.cpp @@ -14,213 +14,20 @@ */ #include "cm_napi_install_app_cert.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_install_app_cert_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_INSTALL_APP_CERT_MIN_ARGS = 3; -constexpr int CM_NAPI_INSTALL_APP_CERT_MAX_ARGS = 4; -} // namespace - -struct InstallAppCertAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - struct CmBlob *keystore = nullptr; - struct CmBlob *keystorePwd = nullptr; - struct CmBlob *keyAlias = nullptr; - struct CmBlob *keyUri = nullptr; - uint32_t store = 0; -}; -using InstallAppCertAsyncContext = InstallAppCertAsyncContextT *; - -static InstallAppCertAsyncContext CreateInstallAppCertAsyncContext() -{ - InstallAppCertAsyncContext context = - static_cast(CmMalloc(sizeof(InstallAppCertAsyncContextT))); - if (context != nullptr) { - (void)memset_s(context, sizeof(InstallAppCertAsyncContextT), 0, sizeof(InstallAppCertAsyncContextT)); - } - return context; -} - -static void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->keystore != nullptr) { - FreeCmBlob(context->keystore); - } - - if (context->keystorePwd != nullptr) { - FreeCmBlob(context->keystorePwd); - } - - if (context->keyAlias != nullptr) { - FreeCmBlob(context->keyAlias); - } - - if (context->keyUri != nullptr) { - FreeCmBlob(context->keyUri); - } - - CmFree(context); - context = nullptr; -} - -static napi_value InstallAppCertParseParams( - napi_env env, napi_callback_info info, InstallAppCertAsyncContext context) -{ - size_t argc = CM_NAPI_INSTALL_APP_CERT_MAX_ARGS; - napi_value argv[CM_NAPI_INSTALL_APP_CERT_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - - if (argc < CM_NAPI_INSTALL_APP_CERT_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("Missing parameter"); - return nullptr; - } - - size_t index = 0; - context->keystore = (CmBlob *)CmMalloc(sizeof(CmBlob)); - if (context->keystore == nullptr) { - CM_LOG_E("could not alloc memory"); - return nullptr; - } - (void)memset_s(context->keystore, sizeof(CmBlob), 0, sizeof(CmBlob)); - - napi_value result = GetUint8Array(env, argv[index], *context->keystore); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get keystore"); - return nullptr; - } - - index++; - result = ParseString(env, argv[index], context->keystorePwd); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get keystore Pwd"); - return nullptr; - } - index++; - result = ParseString(env, argv[index], context->keyAlias); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get uri"); - return nullptr; - } - - index++; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; - return GetInt32(env, 0); -} - -static void InitKeyUri(struct CmBlob *&keyUri) +napi_value CMNapiInstallAppCert(napi_env env, napi_callback_info info) { - keyUri = (uint8_t *)CmMalloc(sizeof(struct CmBlob)); - if (keyUri == NULL) { - CM_LOG_E("malloc keyUri buffer failed"); - return; - } - - keyUri->data = (uint8_t *)CmMalloc(MAX_LEN_URI); - if (keyUri->data == NULL) { - CM_LOG_E("malloc keyUri->data buffer failed"); - return; - } - - (void)memset_s(keyUri->data, MAX_LEN_URI, 0, MAX_LEN_URI); - keyUri->size = MAX_LEN_URI; + return CMNapiInstallAppCertCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext context) +napi_value CMNapiInstallPrivateAppCert(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "InstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - InstallAppCertAsyncContext context = static_cast(data); - InitKeyUri(context->keyUri); - context->result = CmInstallAppCert(context->keystore, - context->keystorePwd, context->keyAlias, context->store, context->keyUri); - }, - [](napi_env env, napi_status status, void *data) { - InstallAppCertAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1])); - } else { - const char *errorMessage = "install app cert error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteInstallAppCertAsyncContext(env, context); - }, - (void *)context, - &context->asyncWork)); - - napi_status status = napi_queue_async_work(env, context->asyncWork); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteInstallAppCertAsyncContext(env, context); - CM_LOG_E("could not queue async work"); - return nullptr; - } - return promise; + return CMNapiInstallAppCertCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiInstallAppCert(napi_env env, napi_callback_info info) -{ - InstallAppCertAsyncContext context = CreateInstallAppCertAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = InstallAppCertParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteInstallAppCertAsyncContext(env, context); - return nullptr; - } - result = InstallAppCertAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteInstallAppCertAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp b/interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp new file mode 100755 index 0000000..46d5f20 --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_install_app_cert_common.cpp @@ -0,0 +1,236 @@ +/* + * 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 "cm_napi_install_app_cert.h" +#include "cm_napi_install_app_cert_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_INSTALL_APP_CERT_MIN_ARGS = 3; +constexpr int CM_NAPI_INSTALL_APP_CERT_MAX_ARGS = 4; +} // namespace + +InstallAppCertAsyncContext CreateInstallAppCertAsyncContext() +{ + InstallAppCertAsyncContext context = + static_cast(CmMalloc(sizeof(InstallAppCertAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(InstallAppCertAsyncContextT), 0, sizeof(InstallAppCertAsyncContextT)); + } + return context; +} + +void DeleteInstallAppCertAsyncContext(napi_env env, InstallAppCertAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->keystore != nullptr) { + FreeCmBlob(context->keystore); + } + + if (context->keystorePwd != nullptr) { + FreeCmBlob(context->keystorePwd); + } + + if (context->keyAlias != nullptr) { + FreeCmBlob(context->keyAlias); + } + + if (context->keyUri != nullptr) { + FreeCmBlob(context->keyUri); + } + + CmFree(context); + context = nullptr; +} + +napi_value InstallAppCertParseParams( + napi_env env, napi_callback_info info, InstallAppCertAsyncContext context) +{ + size_t argc = CM_NAPI_INSTALL_APP_CERT_MAX_ARGS; + napi_value argv[CM_NAPI_INSTALL_APP_CERT_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_INSTALL_APP_CERT_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("Missing parameter"); + return nullptr; + } + + size_t index = 0; + context->keystore = (CmBlob *)CmMalloc(sizeof(CmBlob)); + if (context->keystore == nullptr) { + CM_LOG_E("could not alloc memory"); + return nullptr; + } + (void)memset_s(context->keystore, sizeof(CmBlob), 0, sizeof(CmBlob)); + + napi_value result = GetUint8Array(env, argv[index], *context->keystore); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get keystore"); + return nullptr; + } + + index++; + result = ParseString(env, argv[index], context->keystorePwd); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get keystore Pwd"); + return nullptr; + } + + index++; + result = ParseString(env, argv[index], context->keyAlias); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get uri"); + return nullptr; + } + + index++; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E(" InstallAppCert get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +static void InitKeyUri(struct CmBlob *&keyUri) +{ + keyUri = (struct CmBlob *)CmMalloc(sizeof(struct CmBlob)); + if (keyUri == NULL) { + CM_LOG_E("malloc keyUri buffer failed"); + return; + } + + keyUri->data = (uint8_t *)CmMalloc(MAX_LEN_URI); + if (keyUri->data == NULL) { + CM_LOG_E("malloc keyUri->data buffer failed"); + return; + } + + (void)memset_s(keyUri->data, MAX_LEN_URI, 0, MAX_LEN_URI); + keyUri->size = MAX_LEN_URI; +} + +static napi_value InstallAppCertWriteResult(napi_env env, InstallAppCertAsyncContext context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + + napi_value keyUri = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, (char *)context->keyUri->data, + context->keyUri->size, &keyUri)); + if (keyUri != nullptr) { + napi_set_named_property(env, result, CM_CERT_PROPERTY_URI.c_str(), keyUri); + } else { + NAPI_CALL(env, napi_get_undefined(env, &result)); + } + return result; +} + +napi_value InstallAppCertAsyncWork(napi_env env, InstallAppCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "InstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + InstallAppCertAsyncContext context = static_cast(data); + InitKeyUri(context->keyUri); + context->result = CmInstallAppCert(context->keystore, + context->keystorePwd, context->keyAlias, context->store, context->keyUri); + }, + [](napi_env env, napi_status status, void *data) { + InstallAppCertAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + result[1] = InstallAppCertWriteResult(env, context); + } else { + const char *errorMessage = "install app cert error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteInstallAppCertAsyncContext(env, context); + }, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteInstallAppCertAsyncContext(env, context); + CM_LOG_E("could not queue async work"); + return nullptr; + } + return promise; +} + +napi_value CMNapiInstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + InstallAppCertAsyncContext context = CreateInstallAppCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = InstallAppCertParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteInstallAppCertAsyncContext(env, context); + return nullptr; + } + result = InstallAppCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteInstallAppCertAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp b/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp index f5ef134..6bf6619 100644 --- a/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp +++ b/interfaces/kits/napi/src/cm_napi_uninstall_all_app_cert.cpp @@ -97,8 +97,7 @@ static napi_value UninstallAllAppCertAsyncWork(napi_env env, UninstallAllAppCert resourceName, [](napi_env env, void *data) { UninstallAllAppCertAsyncContext context = static_cast(data); - /* fix CmUninstallAllAppCert */ - context->result = 0; + context->result = CmUninstallAllAppCert(); }, [](napi_env env, napi_status status, void *data) { UninstallAllAppCertAsyncContext context = static_cast(data); diff --git a/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp index 083c849..6fc3052 100644 --- a/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp +++ b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert.cpp @@ -14,157 +14,20 @@ */ #include "cm_napi_uninstall_app_cert.h" - -#include "securec.h" - -#include "cert_manager_api.h" -#include "cm_log.h" -#include "cm_mem.h" -#include "cm_type.h" +#include "cm_napi_uninstall_app_cert_common.h" #include "cm_napi_common.h" +#include "cm_log.h" namespace CMNapi { -namespace { -constexpr int CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS = 1; -constexpr int CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS = 2; -} // namespace - -struct UninstallAppCertAsyncContextT { - napi_async_work asyncWork = nullptr; - napi_deferred deferred = nullptr; - napi_ref callback = nullptr; - - int32_t result = 0; - struct CmBlob *keyUri = nullptr; - uint32_t store = 0; -}; -using UninstallAppCertAsyncContext = UninstallAppCertAsyncContextT *; - -static UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext() -{ - UninstallAppCertAsyncContext context = - static_cast(CmMalloc(sizeof(UninstallAppCertAsyncContextT))); - if (context != nullptr) { - (void)memset_s( - context, sizeof(UninstallAppCertAsyncContextT), 0, sizeof(UninstallAppCertAsyncContextT)); - } - return context; -} - -static void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context) -{ - if (context == nullptr) { - return; - } - - DeleteNapiContext(env, context->asyncWork, context->callback); - - if (context->keyUri != nullptr) { - FreeCmBlob(context->keyUri); - } - - CmFree(context); - context = nullptr; -} -static napi_value UninstallAppCertParseParams( - napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context) +napi_value CMNapiUninstallAppCert(napi_env env, napi_callback_info info) { - size_t argc = CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS; - napi_value argv[CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS] = {0}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - - if (argc < CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); - CM_LOG_E("Missing parameter"); - return nullptr; - } - - size_t index = 0; - napi_value result = ParseString(env, argv[index], context->keyUri); - if (result == nullptr) { - napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get cert uri"); - return nullptr; - } - - index++; - if (index < argc) { - context->callback = GetCallback(env, argv[index]); - } - - context->store = APPLICATION_CERTIFICATE_STORE; /* 0 is store type, indicate app certificate */ - return GetInt32(env, 0); + return CMNapiUninstallAppCertCommon(env, info, APPLICATION_CERTIFICATE_STORE); } -static napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext context) +napi_value CMNapiUninstallPrivateAppCert(napi_env env, napi_callback_info info) { - napi_value promise = nullptr; - GenerateNapiPromise(env, context->callback, &context->deferred, &promise); - - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); - - NAPI_CALL(env, napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - UninstallAppCertAsyncContext context = static_cast(data); - context->result = CmUninstallAppCert(context->keyUri, context->store); - }, - [](napi_env env, napi_status status, void *data) { - UninstallAppCertAsyncContext context = static_cast(data); - napi_value result[RESULT_NUMBER] = {0}; - if (context->result == CM_SUCCESS) { - NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); - NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1])); - } else { - const char *errorMessage = "uninstall app cert error"; - result[0] = GenerateBusinessError(env, context->result, errorMessage); - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); - } - if (context->deferred != nullptr) { - GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); - } else { - GenerateCallback(env, context->callback, result, sizeof(result)); - } - DeleteUninstallAppCertAsyncContext(env, context); - }, - (void *)context, - &context->asyncWork)); - - napi_status status = napi_queue_async_work(env, context->asyncWork); - if (status != napi_ok) { - GET_AND_THROW_LAST_ERROR((env)); - DeleteUninstallAppCertAsyncContext(env, context); - CM_LOG_E("could not queue async work"); - return nullptr; - } - - return promise; + return CMNapiUninstallAppCertCommon(env, info, APPLICATION_PRIVATE_CERTIFICATE_STORE); } -napi_value CMNapiUninstallAppCert(napi_env env, napi_callback_info info) -{ - UninstallAppCertAsyncContext context = CreateUninstallAppCertAsyncContext(); - if (context == nullptr) { - CM_LOG_E("could not create context"); - return nullptr; - } - - napi_value result = UninstallAppCertParseParams(env, info, context); - if (result == nullptr) { - CM_LOG_E("could not parse params"); - DeleteUninstallAppCertAsyncContext(env, context); - return nullptr; - } - result = UninstallAppCertAsyncWork(env, context); - if (result == nullptr) { - CM_LOG_E("could not start async work"); - DeleteUninstallAppCertAsyncContext(env, context); - return nullptr; - } - return result; -} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp new file mode 100755 index 0000000..b8a3eef --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_uninstall_app_cert_common.cpp @@ -0,0 +1,167 @@ +/* + * 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 "cm_napi_uninstall_app_cert.h" +#include "cm_napi_uninstall_app_cert_common.h" + +#include "securec.h" + +#include "cert_manager_api.h" +#include "cm_log.h" +#include "cm_mem.h" +#include "cm_type.h" +#include "cm_napi_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS = 1; +constexpr int CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS = 2; +} // namespace + +UninstallAppCertAsyncContext CreateUninstallAppCertAsyncContext() +{ + UninstallAppCertAsyncContext context = + static_cast(CmMalloc(sizeof(UninstallAppCertAsyncContextT))); + if (context != nullptr) { + (void)memset_s( + context, sizeof(UninstallAppCertAsyncContextT), 0, sizeof(UninstallAppCertAsyncContextT)); + } + return context; +} + +void DeleteUninstallAppCertAsyncContext(napi_env env, UninstallAppCertAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + + if (context->keyUri != nullptr) { + FreeCmBlob(context->keyUri); + } + + CmFree(context); + context = nullptr; +} + +napi_value UninstallAppCertParseParams( + napi_env env, napi_callback_info info, UninstallAppCertAsyncContext context) +{ + size_t argc = CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS; + napi_value argv[CM_NAPI_UNINSTALL_APP_CERT_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if (argc < CM_NAPI_UNINSTALL_APP_CERT_MIN_ARGS) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Missing parameter"); + CM_LOG_E("Missing parameter"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseString(env, argv[index], context->keyUri); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("could not get cert uri"); + return nullptr; + } + + index++; + if (index < argc) { + context->callback = GetCallback(env, argv[index]); + if (context->callback == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("UninstallAppCert get callback function faild"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +napi_value UninstallAppCertAsyncWork(napi_env env, UninstallAppCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + UninstallAppCertAsyncContext context = static_cast(data); + context->result = CmUninstallAppCert(context->keyUri, context->store); + }, + [](napi_env env, napi_status status, void *data) { + UninstallAppCertAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->result == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0])); + NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1])); + } else { + const char *errorMessage = "uninstall app cert error"; + result[0] = GenerateBusinessError(env, context->result, errorMessage); + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1])); + } + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->result, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + DeleteUninstallAppCertAsyncContext(env, context); + }, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + GET_AND_THROW_LAST_ERROR((env)); + DeleteUninstallAppCertAsyncContext(env, context); + CM_LOG_E("could not queue async work"); + return nullptr; + } + + return promise; +} + +napi_value CMNapiUninstallAppCertCommon(napi_env env, napi_callback_info info, uint32_t store) +{ + UninstallAppCertAsyncContext context = CreateUninstallAppCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("could not create context"); + return nullptr; + } + + context->store = store; + + napi_value result = UninstallAppCertParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse params"); + DeleteUninstallAppCertAsyncContext(env, context); + return nullptr; + } + result = UninstallAppCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("could not start async work"); + DeleteUninstallAppCertAsyncContext(env, context); + return nullptr; + } + return result; +} + +} // namespace CertManagerNapi diff --git a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c index 73d8f93..caab0fe 100644 --- a/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c +++ b/services/cert_manager_standard/cert_manager_engine/main/core/src/cert_manager_file_operator.c @@ -331,6 +331,11 @@ static int32_t CmUidLayerGetFileNames(const char *filePath, struct CmBlob *fileN return CMR_ERROR_BUFFER_TOO_SMALL; } uint32_t filePathLen = strlen(filePath); + if (filePathLen >= CM_MAX_FILE_NAME_LEN) { + CM_LOG_E("CmUidLayerGetFileNames filePathLen:%u", filePathLen); + return CMR_ERROR_BUFFER_TOO_SMALL; + } + fileNames[count].data = (uint8_t *)CMMalloc(filePathLen + 1); if (fileNames[count].data == NULL) { return CMR_ERROR_MALLOC_FAIL; diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c index 7efe325..e93d83b 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/idl/cm_ipc/cm_ipc_service.c @@ -544,7 +544,7 @@ static int32_t CmInstallAppCertCheck(const struct CmBlob *appCert, const struct return CMR_ERROR_INVALID_ARGUMENT; } - if (appCert->size == 0 || appCert->size == 0 ||appCert->size == 0 || + if (appCert->size == 0 || appCertPwd->size == 0 || certAlias->size == 0 || appCert->size > MAX_LEN_APP_CERT || appCertPwd->size > MAX_LEN_APP_CERT_PASSWD || certAlias->size > MAX_LEN_CERT_ALIAS) { CM_LOG_E("CmInstallAppCertCheck paramSet check fail, appCert:%u, appCertPwd:%u, certAlias:%u", diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp index 4993349..be71ff6 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_event_observer.cpp @@ -36,7 +36,7 @@ SystemEventSubscriber::SystemEventSubscriber(const OHOS::EventFwk::CommonEventSu void SystemEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) { - int uid; + int userId; struct CmContext context; context.userId = INVALID_VALUE; auto want = data.GetWant(); @@ -44,13 +44,13 @@ void SystemEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { context.uid = (uint32_t)want.GetIntParam(AppExecFwk::Constants::UID, -1); - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(context.uid, uid); - context.userId = (uint32_t)uid; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(context.uid, userId); + context.userId = (uint32_t)userId; CM_LOG_I("CmService package removed: uid is %u userId is %u", context.uid, context.userId); CmDeleteProcessInfo(&context); } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - int userId = data.GetCode(); - CM_LOG_I("CmService user removed: userId is %d", userId); + context.userId = data.GetCode(); + CM_LOG_I("CmService user removed: userId is %d", context.userId); CmDeleteProcessInfo(&context); } } diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp index 71a5e3a..6c5c6f3 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.cpp @@ -174,6 +174,12 @@ int CertManagerService::OnRemoteRequest(uint32_t code, MessageParcel &data, // this is the temporary version which comments the descriptor check std::u16string descriptor = CertManagerService::GetDescriptor(); std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + CM_LOG_E("descriptor is diff"); + return HW_SYSTEM_ERROR; + } + + CM_LOG_I("OnRemoteRequest code:%u", code); uint32_t outSize = (uint32_t)data.ReadUint32(); struct CmBlob srcData = { 0, nullptr }; diff --git a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h index 1aefab2..1c0ee55 100644 --- a/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h +++ b/services/cert_manager_standard/cert_manager_service/main/os_dependency/sa/cm_sa.h @@ -38,7 +38,7 @@ constexpr int SA_ID_KEYSTORE_SERVICE = 3512; class ICertManagerService : public IRemoteBroker { public: - DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.CertManager.service"); + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.security.cm.service"); }; class CertManagerService : public SystemAbility, public IRemoteStub { -- Gitee