From 2da5fc0f6b70c9716b249057a205d737259cf889 Mon Sep 17 00:00:00 2001 From: haixiangw Date: Fri, 20 Dec 2024 04:32:19 -0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7CA=E8=AF=81?= =?UTF-8?q?=E4=B9=A6=E5=88=97=E8=A1=A8=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A?= =?UTF-8?q?CA=E8=AF=81=E4=B9=A6=E7=9A=84=E4=BD=8D=E7=BD=AE=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: haixiangw --- interfaces/kits/napi/src/cm_napi_common.cpp | 4 +- .../napi/src/cm_napi_get_system_cert_list.cpp | 52 ++++++++++++++++--- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/napi/src/cm_napi_common.cpp b/interfaces/kits/napi/src/cm_napi_common.cpp index 1634b50..fc9437f 100644 --- a/interfaces/kits/napi/src/cm_napi_common.cpp +++ b/interfaces/kits/napi/src/cm_napi_common.cpp @@ -315,7 +315,7 @@ static napi_value GenerateAarrayBuffer(napi_env env, uint8_t *data, uint32_t siz napi_value GenerateCertAbstractArray(napi_env env, const struct CertAbstract *certAbstract, const uint32_t certCount) { - if (certCount == 0 || certAbstract == nullptr) { + if (certAbstract == nullptr) { return nullptr; } napi_value array = nullptr; @@ -349,7 +349,7 @@ napi_value GenerateCertAbstractArray(napi_env env, const struct CertAbstract *ce napi_value GenerateCredentialAbstractArray(napi_env env, const struct CredentialAbstract *credentialAbstract, const uint32_t credentialCount) { - if (credentialCount == 0 || credentialAbstract == nullptr) { + if (credentialAbstract == nullptr) { return nullptr; } napi_value array = nullptr; 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 8ac38a1..a6d7eb1 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 @@ -36,6 +36,7 @@ struct GetCertListAsyncContextT { int32_t result = 0; uint32_t store = 0; + enum CmCertScope scope = CM_ALL_USER; struct CertList *certificateList = nullptr; }; using GetCertListAsyncContext = GetCertListAsyncContextT *; @@ -67,6 +68,24 @@ static void DeleteGetCertListAsyncContext(napi_env env, GetCertListAsyncContext context = nullptr; } +static int32_t GetAndCheckScope(napi_env env, napi_value arg, enum CmCertScope &certScope) +{ + uint32_t scope = 0; + napi_value result = ParseUint32(env, arg, scope); + if (result == nullptr) { + CM_LOG_E("Failed to get scope value"); + return CM_FAILURE; + } + + if (!IsValidCertScope(scope)) { + CM_LOG_E("scope[%u] is invalid", scope); + return CM_FAILURE; + } + + certScope = static_cast(scope); + return CM_SUCCESS; +} + static napi_value GetCertListParseParams( napi_env env, napi_callback_info info, GetCertListAsyncContext context, uint32_t store) { @@ -74,19 +93,30 @@ static napi_value GetCertListParseParams( napi_env env, napi_callback_info in napi_value argv[CM_NAPI_GET_CERT_LIST_MAX_ARGS] = { nullptr }; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + /* get system ca list */ + if (store == CM_SYSTEM_TRUSTED_STORE) { + if (argc != CM_NAPI_GET_CERT_LIST_MIN_ARGS) { /* no args when get system ca list */ + ThrowError(env, PARAM_ERROR, "arguments count invalid when getting system trusted certificate list"); + CM_LOG_E("arguments count is not expected when getting system trusted certificate list"); + return nullptr; + } + context->store = store; + return GetInt32(env, 0); + } + + /* get user ca list */ if ((argc != CM_NAPI_GET_CERT_LIST_MIN_ARGS) && (argc != CM_NAPI_GET_CERT_LIST_MAX_ARGS)) { - ThrowError(env, PARAM_ERROR, "arguments count invalid when getting trusted certificate list"); + ThrowError(env, PARAM_ERROR, "arguments count invalid when getting user trusted certificate list"); CM_LOG_E("arguments count is not expected when getting trusted certificate list"); return nullptr; } - size_t index = 0; - if (index < argc) { - int32_t ret = GetCallback(env, argv[index], context->callback); + if (argc == CM_NAPI_GET_CERT_LIST_MAX_ARGS) { + int32_t ret = GetAndCheckScope(env, argv[0], context->scope); if (ret != CM_SUCCESS) { - ThrowError(env, PARAM_ERROR, "Get callback type failed."); - CM_LOG_E("get callback function failed when get certlist function"); + ThrowError(env, PARAM_ERROR, "Failed to get scope"); + CM_LOG_E("Failed to get scope when get certlist function"); return nullptr; } } @@ -122,7 +152,7 @@ static void GetCertListExecute(napi_env env, void *data) context->certificateList->certAbstract = nullptr; context->certificateList->certsCount = 0; - uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract); + uint32_t buffSize = MAX_COUNT_CERTIFICATE_ALL * sizeof(struct CertAbstract); context->certificateList->certAbstract = static_cast(CmMalloc(buffSize)); if (context->certificateList->certAbstract == nullptr) { CM_LOG_E("malloc certificateList certAbstract fail"); @@ -130,10 +160,16 @@ static void GetCertListExecute(napi_env env, void *data) return; } (void)memset_s(context->certificateList->certAbstract, buffSize, 0, buffSize); - context->certificateList->certsCount = MAX_COUNT_CERTIFICATE; + context->certificateList->certsCount = MAX_COUNT_CERTIFICATE_ALL; if (context->store == CM_SYSTEM_TRUSTED_STORE) { context->result = CmGetCertList(context->store, context->certificateList); + return; + } + + if (context->scope == CM_CURRENT_USER || context->scope == CM_GLOBAL_USER) { + struct UserCAProperty prop = { INIT_INVALID_VALUE, context->scope }; + context->result = CmGetUserCACertList(&prop, context->certificateList); } else { context->result = CmGetUserCertList(context->store, context->certificateList); } -- Gitee