diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index 2a091b3cc3c25a49916af2198f17673cd46f43fc..52380c42c5ceff703e0f66ad874a9a4e79d326e6 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -38,13 +38,12 @@ ohos_shared_library("certmanager") { "src/cm_napi_set_cert_status.cpp", "src/cm_napi_sign_verify.cpp", "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", "src/cm_napi_uninstall_app_cert.cpp", + "src/cm_napi_user_trusted_cert.cpp", ] external_deps = [ diff --git a/interfaces/kits/napi/include/cm_napi_get_system_cert_info.h b/interfaces/kits/napi/include/cm_napi_get_system_cert_info.h index 722c085f564a6d7e9862f7f07356c25bd699faaf..7615c5ed5788eb4be3bd7b496373a4675d28b3d3 100644 --- a/interfaces/kits/napi/include/cm_napi_get_system_cert_info.h +++ b/interfaces/kits/napi/include/cm_napi_get_system_cert_info.h @@ -21,6 +21,8 @@ namespace CMNapi { napi_value CMNapiGetSystemCertInfo(napi_env env, napi_callback_info info); + +napi_value CMNapiGetUserTrustedCertInfo(napi_env env, napi_callback_info info); } // namespace CertManagerNapi #endif // CM_NAPI_GET_CERTIFICATE_INFO_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_get_system_cert_list.h b/interfaces/kits/napi/include/cm_napi_get_system_cert_list.h index d4206452517a819998b1b8544202d24033295452..6acbd97eafc26e7fd62c0f5e85d37768d9988b37 100644 --- a/interfaces/kits/napi/include/cm_napi_get_system_cert_list.h +++ b/interfaces/kits/napi/include/cm_napi_get_system_cert_list.h @@ -21,6 +21,8 @@ namespace CMNapi { napi_value CMNapiGetSystemCertList(napi_env env, napi_callback_info info); + +napi_value CMNapiGetUserTrustedCertList(napi_env env, napi_callback_info info); } // namespace CertManagerNapi #endif // CM_NAPI_GET_CERTIFICATE_LIST_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/cm_napi_user_trusted_cert.h b/interfaces/kits/napi/include/cm_napi_user_trusted_cert.h new file mode 100755 index 0000000000000000000000000000000000000000..27a889c70b79102870f208a5f56c0bd5b6f1f145 --- /dev/null +++ b/interfaces/kits/napi/include/cm_napi_user_trusted_cert.h @@ -0,0 +1,29 @@ +/* + * 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_USER_TRUSTED_CERT_H +#define CM_NAPI_USER_TRUSTED_CERT_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace CMNapi { + napi_value CMNapiInstallUserTrustedCert(napi_env env, napi_callback_info info); + napi_value CMNapiUninstallAllUserTrustedCert(napi_env env, napi_callback_info info); + napi_value CMNapiUninstallUserTrustedCert(napi_env env, napi_callback_info info); +} // namespace CMNapi + +#endif /* CM_NAPI_USER_TRUSTED_CERT_H */ + diff --git a/interfaces/kits/napi/src/cm_napi.cpp b/interfaces/kits/napi/src/cm_napi.cpp index 388809dddea2a6ab97215f82588b805c54354143..b4dda928ffc81cdf301069b8331144d03de9ccc9 100644 --- a/interfaces/kits/napi/src/cm_napi.cpp +++ b/interfaces/kits/napi/src/cm_napi.cpp @@ -28,6 +28,7 @@ #include "cm_napi_get_app_cert_info.h" #include "cm_napi_grant.h" #include "cm_napi_sign_verify.h" +#include "cm_napi_user_trusted_cert.h" namespace CMNapi { inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value) @@ -102,6 +103,12 @@ extern "C" { DECLARE_NAPI_FUNCTION("uninstallAppCertificate", CMNapiUninstallAppCert), DECLARE_NAPI_FUNCTION("getAppCertificateList", CMNapiGetAppCertList), DECLARE_NAPI_FUNCTION("getAppCertificate", CMNapiGetAppCertInfo), + + DECLARE_NAPI_FUNCTION("installUserTrustedCertificate", CMNapiInstallUserTrustedCert), + DECLARE_NAPI_FUNCTION("uninstallAllUserTrustedCertificate", CMNapiUninstallAllUserTrustedCert), + DECLARE_NAPI_FUNCTION("uninstallUserTrustedCertificate", CMNapiUninstallUserTrustedCert), + DECLARE_NAPI_FUNCTION("getUserTrustedCertificateList", CMNapiGetUserTrustedCertList), + DECLARE_NAPI_FUNCTION("getUserTrustedCertificate", CMNapiGetUserTrustedCertInfo), DECLARE_NAPI_FUNCTION("installPrivateCertificate", CMNapiInstallPrivateAppCert), DECLARE_NAPI_FUNCTION("uninstallPrivateCertificate", CMNapiUninstallPrivateAppCert), DECLARE_NAPI_FUNCTION("getPrivateCertificateList", CMNapiGetPrivateAppCertList), diff --git a/interfaces/kits/napi/src/cm_napi_get_system_cert_info.cpp b/interfaces/kits/napi/src/cm_napi_get_system_cert_info.cpp index 012408f620d1397cb3ebe39a4fd9261a09cd99df..e19765b926704dda6b293077a8a22bdc713cf835 100644 --- a/interfaces/kits/napi/src/cm_napi_get_system_cert_info.cpp +++ b/interfaces/kits/napi/src/cm_napi_get_system_cert_info.cpp @@ -27,6 +27,8 @@ namespace CMNapi { namespace { constexpr int CM_NAPI_GET_SYSTEM_CERT_INFO_MIN_ARGS = 2; constexpr int CM_NAPI_GET_SYSTEM_CERT_INFO_MAX_ARGS = 3; +constexpr int CM_NAPI_GET_USER_CERT_INFO_MIN_ARGS = 1; +constexpr int CM_NAPI_GET_USER_CERT_INFO_MAX_ARGS = 2; } // namespace struct GetCertInfoAsyncContextT { @@ -114,6 +116,41 @@ static napi_value GetSystemCertInfoParseParams( return GetInt32(env, 0); } +static napi_value GetUserCertInfoParseParams( + napi_env env, napi_callback_info info, GetCertInfoAsyncContext context) +{ + size_t argc = CM_NAPI_GET_USER_CERT_INFO_MAX_ARGS; + napi_value argv[CM_NAPI_GET_USER_CERT_INFO_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if ((argc != CM_NAPI_GET_USER_CERT_INFO_MAX_ARGS) && (argc != CM_NAPI_GET_USER_CERT_INFO_MIN_ARGS)) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "get user trust info arguments count invalid"); + CM_LOG_E("get user trust info arguments count invalid"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseString(env, argv[index], context->certUri); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("get user trust info get cert uri failed"); + 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("get user trust info get callback function failed"); + return nullptr; + } + } + + context->store = CM_USER_TRUSTED_STORE; + return GetInt32(env, 0); +} + static napi_value GetCertInfoWriteResult(napi_env env, GetCertInfoAsyncContext context) { napi_value result = nullptr; @@ -127,6 +164,47 @@ static napi_value GetCertInfoWriteResult(napi_env env, GetCertInfoAsyncContext c return result; } +static void GetCertInfoExecute(napi_env env, void *data) +{ + GetCertInfoAsyncContext context = static_cast(data); + + context->certificate = (struct CertInfo *)CmMalloc(sizeof(struct CertInfo)); + if (context->certificate != nullptr) { + (void)memset_s(context->certificate, sizeof(struct CertInfo), 0, sizeof(struct CertInfo)); + } + + if (context->store == CM_SYSTEM_TRUSTED_STORE) { + context->result = CmGetCertInfo(context->cmContext, context->certUri, context->store, + context->certificate); + } else { + context->certificate->certInfo.data = (uint8_t *)CmMalloc(MAX_LEN_CERTIFICATE); + context->certificate->certInfo.size = MAX_LEN_CERTIFICATE; + context->result = CmGetUserCertInfo(context->certUri, context->store, context->certificate); + } +} + +static void GetCertInfoComplete(napi_env env, napi_status status, void *data) +{ + GetCertInfoAsyncContext 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] = GetCertInfoWriteResult(env, context); + } else { + const char *errorMessage = "get system 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)); + } + DeleteGetCertInfoAsyncContext(env, context); + CM_LOG_I("get system cert info end"); +} + + static napi_value GetCertInfoAsyncWork(napi_env env, GetCertInfoAsyncContext context) { napi_value promise = nullptr; @@ -139,34 +217,8 @@ static napi_value GetCertInfoAsyncWork(napi_env env, GetCertInfoAsyncContext con env, nullptr, resourceName, - [](napi_env env, void *data) { - GetCertInfoAsyncContext context = static_cast(data); - - context->certificate = (struct CertInfo *)CmMalloc(sizeof(struct CertInfo)); - if (context->certificate != nullptr) { - (void)memset_s(context->certificate, sizeof(struct CertInfo), 0, sizeof(struct CertInfo)); - } - context->result = CmGetCertInfo(context->cmContext, context->certUri, context->store, context->certificate); - }, - [](napi_env env, napi_status status, void *data) { - GetCertInfoAsyncContext 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] = GetCertInfoWriteResult(env, context); - } else { - const char *errorMessage = "get system 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)); - } - DeleteGetCertInfoAsyncContext(env, context); - CM_LOG_I("get system cert info end"); - }, + GetCertInfoExecute, + GetCertInfoComplete, (void *)context, &context->asyncWork)); napi_status status = napi_queue_async_work(env, context->asyncWork); @@ -201,4 +253,27 @@ napi_value CMNapiGetSystemCertInfo(napi_env env, napi_callback_info info) } return result; } + +napi_value CMNapiGetUserTrustedCertInfo(napi_env env, napi_callback_info info) +{ + GetCertInfoAsyncContext context = CreateGetCertInfoAsyncContext(); + if (context == nullptr) { + CM_LOG_E("create cert info context failed"); + return nullptr; + } + + napi_value result = GetUserCertInfoParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("parse get cert info params failed"); + DeleteGetCertInfoAsyncContext(env, context); + return nullptr; + } + result = GetCertInfoAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("get cert info params async work failed"); + DeleteGetCertInfoAsyncContext(env, context); + return nullptr; + } + return result; +} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_get_system_cert_list.cpp b/interfaces/kits/napi/src/cm_napi_get_system_cert_list.cpp index acc0349b71e1cd0047c9bd3fe95fc6a5ef89eb5d..0a4f17f1eaf68d0ba355ba366d1451e3c443a29b 100644 --- a/interfaces/kits/napi/src/cm_napi_get_system_cert_list.cpp +++ b/interfaces/kits/napi/src/cm_napi_get_system_cert_list.cpp @@ -27,6 +27,8 @@ namespace CMNapi { namespace { constexpr int CM_NAPI_GET_SYSTEM_CERT_LIST_MIN_ARGS = 1; constexpr int CM_NAPI_GET_SYSTEM_CERT_LIST_MAX_ARGS = 2; +constexpr int CM_NAPI_GET_USER_CERT_LIST_MIN_ARGS = 0; +constexpr int CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS = 1; } // namespace struct GetCertListAsyncContextT { @@ -101,6 +103,34 @@ static napi_value GetCertListParseParams( return GetInt32(env, 0); } +static napi_value GetUserTrustedCertListParseParams( + napi_env env, napi_callback_info info, GetCertListAsyncContext context) +{ + size_t argc = CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS; + napi_value argv[CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if ((argc != CM_NAPI_GET_USER_CERT_LIST_MIN_ARGS) && + (argc != CM_NAPI_GET_USER_CERT_LIST_MAX_ARGS)) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "get user trust list arguments count invalid"); + CM_LOG_E("get user trust list arguments count is not expected"); + 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("get user trust list get callback function failed"); + return nullptr; + } + } + + context->store = CM_USER_TRUSTED_STORE; + return GetInt32(env, 0); +} + static napi_value GetCertListWriteResult(napi_env env, GetCertListAsyncContext context) { napi_value result = nullptr; @@ -115,6 +145,48 @@ static napi_value GetCertListWriteResult(napi_env env, GetCertListAsyncContext c return result; } +static void GetCertListExecute(napi_env env, void *data) +{ + GetCertListAsyncContext context = static_cast(data); + + context->certificateList = (struct CertList *)CmMalloc(sizeof(struct CertList)); + if (context->certificateList != nullptr) { + context->certificateList->certAbstract = nullptr; + context->certificateList->certsCount = 0; + } + if (context->store == CM_SYSTEM_TRUSTED_STORE) { + context->result = CmGetCertList(context->cmContext, context->store, context->certificateList); + } else { + uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract); + context->certificateList->certAbstract = (struct CertAbstract *)CmMalloc(buffSize); + if (context->certificateList->certAbstract != nullptr) { + (void)memset_s(context->certificateList->certAbstract, buffSize, 0, buffSize); + context->certificateList->certsCount = MAX_COUNT_CERTIFICATE; + } + context->result = CmGetUserCertList(context->store, context->certificateList); + } +} + +static void GetCertListComplete(napi_env env, napi_status status, void *data) +{ + GetCertListAsyncContext 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] = GetCertListWriteResult(env, context); + } else { + const char *errorMessage = "get system cert list 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)); + } + DeleteGetCertListAsyncContext(env, context); +} + static napi_value GetCertListAsyncWork(napi_env env, GetCertListAsyncContext context) { napi_value promise = nullptr; @@ -127,34 +199,8 @@ static napi_value GetCertListAsyncWork(napi_env env, GetCertListAsyncContext con env, nullptr, resourceName, - [](napi_env env, void *data) { - GetCertListAsyncContext context = static_cast(data); - - context->certificateList = (struct CertList *)CmMalloc(sizeof(struct CertList)); - if (context->certificateList != nullptr) { - context->certificateList->certAbstract = nullptr; - context->certificateList->certsCount = 0; - } - context->result = CmGetCertList(context->cmContext, context->store, context->certificateList); - }, - [](napi_env env, napi_status status, void *data) { - GetCertListAsyncContext 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] = GetCertListWriteResult(env, context); - } else { - const char *errorMessage = "get system cert list 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)); - } - DeleteGetCertListAsyncContext(env, context); - }, + GetCertListExecute, + GetCertListComplete, (void *)context, &context->asyncWork)); @@ -189,4 +235,28 @@ napi_value CMNapiGetSystemCertList(napi_env env, napi_callback_info info) } return result; } + +napi_value CMNapiGetUserTrustedCertList(napi_env env, napi_callback_info info) +{ + GetCertListAsyncContext context = CreateGetCertListAsyncContext(); + if (context == nullptr) { + CM_LOG_E("create context failed"); + return nullptr; + } + + napi_value result = GetUserTrustedCertListParseParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("could not parse user trusted cert list params"); + DeleteGetCertListAsyncContext(env, context); + return nullptr; + } + + result = GetCertListAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("get user trusted cert list async work failed"); + DeleteGetCertListAsyncContext(env, context); + return nullptr; + } + return result; +} } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/src/cm_napi_set_cert_status.cpp b/interfaces/kits/napi/src/cm_napi_set_cert_status.cpp index 19cab48d4022d86f59a1311d864e2c538ea6d232..3fd512e103a7647bd00c8b84fa270cab690894dd 100644 --- a/interfaces/kits/napi/src/cm_napi_set_cert_status.cpp +++ b/interfaces/kits/napi/src/cm_napi_set_cert_status.cpp @@ -87,26 +87,26 @@ static napi_value SetCertStatusParseParams( } size_t index = 0; - napi_value result = ParseString(env, argv[index], context->certUri); + napi_value result = ParseCmContext(env, argv[index], context->cmContext); if (result == nullptr) { napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("CertStatus could not get cert uri"); + CM_LOG_E("could not get cert manager context"); return nullptr; } index++; - result = ParseUint32(env, argv[index], context->store); + result = ParseString(env, argv[index], context->certUri); if (result == nullptr) { napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get store"); + CM_LOG_E("CertStatus could not get cert uri"); return nullptr; } index++; - result = ParseCmContext(env, argv[index], context->cmContext); + result = ParseUint32(env, argv[index], context->store); if (result == nullptr) { napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); - CM_LOG_E("could not get cert manager context"); + CM_LOG_E("could not get store"); return nullptr; } @@ -125,6 +125,39 @@ static napi_value SetCertStatusParseParams( return GetInt32(env, 0); } +static void SetCertStatusExecute(napi_env env, void *data) +{ + SetCertStatusAsyncContext context = static_cast(data); + if (context->store == CM_SYSTEM_TRUSTED_STORE) { + context->result = CmSetCertStatus(context->cmContext, context->certUri, context->store, + context->status); + } else if (context->store == CM_USER_TRUSTED_STORE) { + context->result = CmSetUserCertStatus(context->certUri, context->store, context->status); + } else { + context->result = CMR_ERROR_INVALID_ARGUMENT; + } +} + +static void SetCertStatusComplete(napi_env env, napi_status status, void *data) +{ + SetCertStatusAsyncContext 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 = "set cert status 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)); + } + DeleteSetCertStatusAsyncContext(env, context); +} + static napi_value SetCertStatusAsyncWork(napi_env env, SetCertStatusAsyncContext context) { napi_value promise = nullptr; @@ -137,29 +170,8 @@ static napi_value SetCertStatusAsyncWork(napi_env env, SetCertStatusAsyncContext env, nullptr, resourceName, - [](napi_env env, void *data) { - SetCertStatusAsyncContext context = static_cast(data); - - context->result = CmSetCertStatus(context->cmContext, context->certUri, context->store, context->status); - }, - [](napi_env env, napi_status status, void *data) { - SetCertStatusAsyncContext 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 = "set cert status 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)); - } - DeleteSetCertStatusAsyncContext(env, context); - }, + SetCertStatusExecute, + SetCertStatusComplete, (void *)context, &context->asyncWork)); diff --git a/interfaces/kits/napi/src/cm_napi_user_trusted_cert.cpp b/interfaces/kits/napi/src/cm_napi_user_trusted_cert.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2cec6a7614c40c47118900f221b37cf9d6af411d --- /dev/null +++ b/interfaces/kits/napi/src/cm_napi_user_trusted_cert.cpp @@ -0,0 +1,465 @@ +/* + * 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_user_trusted_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_common.h" + +namespace CMNapi { +namespace { +constexpr int CM_NAPI_USER_INSTALL_ARGS_CNT = 2; +constexpr int CM_NAPI_USER_UNINSTALL_ARGS_CNT = 2; +constexpr int CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT = 1; +constexpr int CM_NAPI_CALLBACK_ARG_CNT = 1; +constexpr uint32_t OUT_AUTH_URI_SIZE = 1000; +} // namespace + +struct UserCertAsyncContextT { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + + int32_t errCode = 0; + + struct CmBlob *userCert = nullptr; + struct CmBlob *certAlias = nullptr; + struct CmBlob *certUri = nullptr; +}; +using UserCertAsyncContext = UserCertAsyncContextT *; + +static UserCertAsyncContext InitUserCertAsyncContext(void) +{ + UserCertAsyncContext context = static_cast(CmMalloc(sizeof(UserCertAsyncContextT))); + if (context != nullptr) { + (void)memset_s(context, sizeof(UserCertAsyncContextT), 0, sizeof(UserCertAsyncContextT)); + } + return context; +} + +static void FreeUserCertAsyncContext(napi_env env, UserCertAsyncContext &context) +{ + if (context == nullptr) { + return; + } + + DeleteNapiContext(env, context->asyncWork, context->callback); + FreeCmBlob(context->userCert); + FreeCmBlob(context->certAlias); + FreeCmBlob(context->certUri); + CM_FREE_PTR(context); +} + +static int32_t GetUserCertData(napi_env env, napi_value object, UserCertAsyncContext context) +{ + context->userCert = static_cast(CmMalloc(sizeof(CmBlob))); + if (context->userCert == nullptr) { + CM_LOG_E("could not alloc userCert blob memory"); + return CMR_ERROR_MALLOC_FAIL; + } + (void)memset_s(context->userCert, sizeof(CmBlob), 0, sizeof(CmBlob)); + + napi_value result = GetUint8Array(env, object, *(context->userCert)); + if (result == nullptr) { + CM_LOG_E("could not get userCert data"); + return CMR_ERROR_INVALID_OPERATION; + } + + return CM_SUCCESS; +} + +static int32_t GetCertAliasData(napi_env env, napi_value object, UserCertAsyncContext context) +{ + napi_value result = ParseString(env, object, context->certAlias); + if (result == nullptr) { + CM_LOG_E("could not get certAlias data"); + return CMR_ERROR_INVALID_OPERATION; + } + + return CM_SUCCESS; +} + +static napi_value ParseCertInfo(napi_env env, napi_value object, UserCertAsyncContext context) +{ + napi_valuetype type = napi_undefined; + NAPI_CALL(env, napi_typeof(env, object, &type)); + if (type != napi_object) { + CM_LOG_E("type of param CertBlob is not object"); + return nullptr; + } + + napi_value userCertValue = nullptr; + napi_status status = napi_get_named_property(env, object, "inData", &userCertValue); + if (status != napi_ok || userCertValue == nullptr) { + CM_LOG_E("get inData failed"); + return nullptr; + } + + napi_value certAliasValue = nullptr; + status = napi_get_named_property(env, object, "alias", &certAliasValue); + if (status != napi_ok || certAliasValue == nullptr) { + CM_LOG_E("get cert alias failed"); + return nullptr; + } + + int32_t ret = GetUserCertData(env, userCertValue, context); + if (ret != CM_SUCCESS) { + return nullptr; + } + + ret = GetCertAliasData(env, certAliasValue, context); + if (ret != CM_SUCCESS) { + return nullptr; + } + + return GetInt32(env, 0); +} + +static napi_value ParseInstallUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context) +{ + size_t argc = CM_NAPI_USER_INSTALL_ARGS_CNT; + napi_value argv[CM_NAPI_USER_INSTALL_ARGS_CNT] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if ((argc != CM_NAPI_USER_INSTALL_ARGS_CNT) && + (argc != (CM_NAPI_USER_INSTALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "install user cert arguments count invalid"); + CM_LOG_E("install user cert arguments count is not expected"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseCertInfo(env, argv[index], context); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("install user cert get CertBlob failed"); + 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("install user cert get callback function failed"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +static napi_value ParseUninstallUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context) +{ + size_t argc = CM_NAPI_USER_UNINSTALL_ARGS_CNT; + napi_value argv[CM_NAPI_USER_UNINSTALL_ARGS_CNT] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if ((argc != CM_NAPI_USER_UNINSTALL_ARGS_CNT) && + (argc != (CM_NAPI_USER_UNINSTALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "uninstall user cert arguments count invalid"); + CM_LOG_E("uninstall user cert arguments count is not expected"); + return nullptr; + } + + size_t index = 0; + napi_value result = ParseString(env, argv[index], context->certUri); + if (result == nullptr) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("uninstall user cert get CertBlob failed"); + 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("uninstall user cert get callback function failed"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +static napi_value ParseUninstallAllUserCertParams(napi_env env, napi_callback_info info, UserCertAsyncContext context) +{ + size_t argc = CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT; + napi_value argv[CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT] = {0}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + if ((argc != CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT) && + (argc != (CM_NAPI_USER_UNINSTALL_ALL_ARGS_CNT - CM_NAPI_CALLBACK_ARG_CNT))) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "uninstall all user cert arguments count invalid"); + CM_LOG_E("uninstall all user cert arguments count is not expected"); + 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("uninstall all user cert get callback function failed"); + return nullptr; + } + } + + return GetInt32(env, 0); +} + +static void InstallUserCertExecute(napi_env env, void *data) +{ + UserCertAsyncContext context = static_cast(data); + context->certUri = static_cast(CmMalloc(sizeof(CmBlob))); + if (context->certUri == nullptr) { + CM_LOG_E("malloc certUri failed"); + context->errCode = CMR_ERROR_MALLOC_FAIL; + return; + } + (void)memset_s(context->certUri, sizeof(CmBlob), 0, sizeof(CmBlob)); + + context->certUri->data = static_cast(CmMalloc(OUT_AUTH_URI_SIZE)); + if (context->certUri->data == nullptr) { + CM_LOG_E("malloc certUri.data failed"); + context->errCode = CMR_ERROR_MALLOC_FAIL; + return; + } + (void)memset_s(context->certUri->data, OUT_AUTH_URI_SIZE, 0, OUT_AUTH_URI_SIZE); + context->certUri->size = OUT_AUTH_URI_SIZE; + + context->errCode = CmInstallUserTrustedCert(context->userCert, context->certAlias, context->certUri); +} + +static napi_value ConvertResultCertUri(napi_env env, const CmBlob *certUri) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + + napi_value certUriNapi = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, (const char *)certUri->data, NAPI_AUTO_LENGTH, &certUriNapi)); + NAPI_CALL(env, napi_set_named_property(env, result, "uri", certUriNapi)); + + return result; +} + +static void InstallUserCertComplete(napi_env env, napi_status status, void *data) +{ + UserCertAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->errCode == CM_SUCCESS) { + napi_create_uint32(env, 0, &result[0]); + result[1] = ConvertResultCertUri(env, context->certUri); + } else { + const char *errMsg = "install user cert failed"; + result[0] = GenerateBusinessError(env, context->errCode, errMsg); + napi_get_undefined(env, &result[1]); + } + + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->errCode, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + FreeUserCertAsyncContext(env, context); +} + +static napi_value InstallUserCertAsyncWork(napi_env env, UserCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "installUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, nullptr, resourceName, + InstallUserCertExecute, + InstallUserCertComplete, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("queue install user cert async work failed"); + return nullptr; + } + return promise; +} + +static void UninstallUserCertExecute(napi_env env, void *data) +{ + UserCertAsyncContext context = static_cast(data); + context->errCode = CmUninstallUserTrustedCert(context->certUri); +} + +static void UninstallComplete(napi_env env, napi_status status, void *data) +{ + UserCertAsyncContext context = static_cast(data); + napi_value result[RESULT_NUMBER] = {0}; + if (context->errCode == CM_SUCCESS) { + napi_create_uint32(env, 0, &result[0]); + napi_get_boolean(env, true, &result[1]); + } else { + const char *errMsg = "uninstall failed"; + result[0] = GenerateBusinessError(env, context->errCode, errMsg); + napi_get_undefined(env, &result[1]); + } + + if (context->deferred != nullptr) { + GeneratePromise(env, context->deferred, context->errCode, result, sizeof(result)); + } else { + GenerateCallback(env, context->callback, result, sizeof(result)); + } + FreeUserCertAsyncContext(env, context); +} + +static napi_value UninstallUserCertAsyncWork(napi_env env, UserCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "uninstallUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, nullptr, resourceName, + UninstallUserCertExecute, + UninstallComplete, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("queue uninstall user cert async work failed"); + return nullptr; + } + return promise; +} + +static void UninstallAllUserCertExecute(napi_env env, void *data) +{ + UserCertAsyncContext context = static_cast(data); + context->errCode = CmUninstallAllUserTrustedCert(); +} + +static napi_value UninstallAllUserCertAsyncWork(napi_env env, UserCertAsyncContext context) +{ + napi_value promise = nullptr; + GenerateNapiPromise(env, context->callback, &context->deferred, &promise); + + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, "uninstallAllUserCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName)); + + NAPI_CALL(env, napi_create_async_work( + env, nullptr, resourceName, + UninstallAllUserCertExecute, + UninstallComplete, + (void *)context, + &context->asyncWork)); + + napi_status status = napi_queue_async_work(env, context->asyncWork); + if (status != napi_ok) { + napi_throw_error(env, PARAM_TYPE_ERROR_NUMBER.c_str(), "Type error"); + CM_LOG_E("queue uninstall all user cert async work failed"); + return nullptr; + } + return promise; +} + +napi_value CMNapiInstallUserTrustedCert(napi_env env, napi_callback_info info) +{ + UserCertAsyncContext context = InitUserCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("init install user cert context failed"); + return nullptr; + } + + napi_value result = ParseInstallUserCertParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("parse install user cert params failed"); + FreeUserCertAsyncContext(env, context); + return nullptr; + } + + result = InstallUserCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("start install user cert async work failed"); + FreeUserCertAsyncContext(env, context); + return nullptr; + } + + return result; +} + +napi_value CMNapiUninstallUserTrustedCert(napi_env env, napi_callback_info info) +{ + UserCertAsyncContext context = InitUserCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("init uninstall user cert context failed"); + return nullptr; + } + + napi_value result = ParseUninstallUserCertParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("parse uninstall user cert params failed"); + FreeUserCertAsyncContext(env, context); + return nullptr; + } + + result = UninstallUserCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("start uninstall user cert async work failed"); + FreeUserCertAsyncContext(env, context); + return nullptr; + } + + return result; +} + +napi_value CMNapiUninstallAllUserTrustedCert(napi_env env, napi_callback_info info) +{ + UserCertAsyncContext context = InitUserCertAsyncContext(); + if (context == nullptr) { + CM_LOG_E("init uninstall all user cert context failed"); + return nullptr; + } + + napi_value result = ParseUninstallAllUserCertParams(env, info, context); + if (result == nullptr) { + CM_LOG_E("parse uninstall all user cert params failed"); + FreeUserCertAsyncContext(env, context); + return nullptr; + } + + result = UninstallAllUserCertAsyncWork(env, context); + if (result == nullptr) { + CM_LOG_E("start uninstall all user cert async work failed"); + FreeUserCertAsyncContext(env, context); + return nullptr; + } + + return result; +} +} // namespace CMNapi + diff --git a/test/unittest/src/cm_user_cert_test.cpp b/test/unittest/src/cm_user_cert_test.cpp index 509cdeaf71ffb69d164c818c1b52758011d21dfe..0193e21d897e48babe5af7e0df36a1c149b6a6b4 100755 --- a/test/unittest/src/cm_user_cert_test.cpp +++ b/test/unittest/src/cm_user_cert_test.cpp @@ -187,7 +187,6 @@ static const uint8_t g_certData02[] = { /* 985c1f52.0 */ 0x48, 0xc8, 0xd8, 0xb0, 0x99, 0xc5, 0x15, 0x84, 0x09, 0xf6, 0xd6, 0x49, 0x49, 0xc0, 0x65, 0xb8, 0xe6, 0x1a, 0x71, 0x6e, 0xa0, 0xa8, 0xf1, 0x82, 0xe8, 0x45, 0x3e, 0x6c, 0xd6, 0x02, 0xd7, 0x0a, 0x67, 0x83, 0x05, 0x5a, 0xc9, 0xa4, 0x10 - }; static const uint8_t g_certData03[] = { /* 1df5a75f.0 */ @@ -259,7 +258,6 @@ static const uint8_t g_certData03[] = { /* 1df5a75f.0 */ 0x51, 0xd4, 0x50, 0xd4, 0x59, 0x28, 0xbd, 0x83, 0xf2, 0xcc, 0x28, 0x7b, 0x53, 0x86, 0x6d, 0xd8, 0x26, 0x88, 0x70, 0xd7, 0xea, 0x91, 0xcd, 0x3e, 0xb9, 0xca, 0xc0, 0x90, 0x6e, 0x5a, 0xc6, 0x5e, 0x74, 0x65, 0xd7, 0x5c, 0xfe, 0xa3, 0xe2 - }; static const uint8_t g_certData04[] = { /* invalid data */