From 13b9d3553fff67ac0a6b04d8b6e3936d944dd1ac Mon Sep 17 00:00:00 2001 From: winnie-hu Date: Sat, 5 Nov 2022 21:57:07 +0800 Subject: [PATCH] change crypto_framework to crypto and cert Signed-off-by: winnie-hu --- BUILD.gn | 6 +- README_zh.md | 11 +- bundle.json | 2 +- cfi_blocklist.txt | 3 +- frameworks/BUILD.gn | 61 - frameworks/crypto_operation/mac.c | 6 +- frameworks/crypto_operation/md.c | 6 +- frameworks/js/napi/certificate/BUILD.gn | 64 + .../inc/napi_cert_chain_validator.h | 0 .../inc/napi_x509_certificate.h | 0 .../{ => certificate}/inc/napi_x509_crl.h | 0 .../inc/napi_x509_crl_entry.h | 0 .../src/napi_cert_chain_validator.cpp | 15 +- .../certificate/src/napi_certificate_init.cpp | 123 ++ .../src/napi_x509_certificate.cpp | 83 +- .../{ => certificate}/src/napi_x509_crl.cpp | 63 +- .../src/napi_x509_crl_entry.cpp | 15 +- frameworks/js/napi/crypto/BUILD.gn | 73 + .../{ => crypto}/inc/napi_asy_key_generator.h | 4 +- .../js/napi/{ => crypto}/inc/napi_cipher.h | 4 +- .../inc/napi_crypto_framework_defines.h | 21 +- .../js/napi/{ => crypto}/inc/napi_key.h | 6 +- .../{ => crypto}/inc/napi_key_agreement.h | 4 +- .../js/napi/{ => crypto}/inc/napi_key_pair.h | 4 +- .../js/napi/{ => crypto}/inc/napi_mac.h | 2 +- frameworks/js/napi/{ => crypto}/inc/napi_md.h | 2 +- .../js/napi/{ => crypto}/inc/napi_pri_key.h | 6 +- .../js/napi/{ => crypto}/inc/napi_pub_key.h | 6 +- .../js/napi/{ => crypto}/inc/napi_rand.h | 2 +- .../js/napi/{ => crypto}/inc/napi_sign.h | 4 +- .../js/napi/{ => crypto}/inc/napi_sym_key.h | 8 +- .../{ => crypto}/inc/napi_sym_key_generator.h | 6 +- .../js/napi/{ => crypto}/inc/napi_utils.h | 14 +- .../js/napi/{ => crypto}/inc/napi_verify.h | 2 +- .../src/napi_asy_key_generator.cpp | 46 +- .../js/napi/{ => crypto}/src/napi_cipher.cpp | 60 +- .../js/napi/{ => crypto}/src/napi_init.cpp | 56 +- .../js/napi/{ => crypto}/src/napi_key.cpp | 9 +- .../{ => crypto}/src/napi_key_agreement.cpp | 35 +- .../napi/{ => crypto}/src/napi_key_pair.cpp | 0 .../js/napi/{ => crypto}/src/napi_mac.cpp | 29 +- .../js/napi/{ => crypto}/src/napi_md.cpp | 23 +- .../js/napi/{ => crypto}/src/napi_pri_key.cpp | 7 +- .../js/napi/{ => crypto}/src/napi_pub_key.cpp | 5 +- .../js/napi/{ => crypto}/src/napi_rand.cpp | 21 +- .../js/napi/{ => crypto}/src/napi_sign.cpp | 71 +- .../js/napi/{ => crypto}/src/napi_sym_key.cpp | 3 +- .../src/napi_sym_key_generator.cpp | 44 +- .../js/napi/{ => crypto}/src/napi_utils.cpp | 83 +- .../js/napi/{ => crypto}/src/napi_verify.cpp | 71 +- frameworks/rand/rand.c | 6 +- interfaces/kits/js/@ohos.security.cert.d.ts | 731 ++++++++++ .../js/@ohos.security.cryptoFramework.d.ts | 1219 +++++++---------- 53 files changed, 1933 insertions(+), 1212 deletions(-) create mode 100644 frameworks/js/napi/certificate/BUILD.gn rename frameworks/js/napi/{ => certificate}/inc/napi_cert_chain_validator.h (100%) rename frameworks/js/napi/{ => certificate}/inc/napi_x509_certificate.h (100%) rename frameworks/js/napi/{ => certificate}/inc/napi_x509_crl.h (100%) rename frameworks/js/napi/{ => certificate}/inc/napi_x509_crl_entry.h (100%) rename frameworks/js/napi/{ => certificate}/src/napi_cert_chain_validator.cpp (95%) create mode 100644 frameworks/js/napi/certificate/src/napi_certificate_init.cpp rename frameworks/js/napi/{ => certificate}/src/napi_x509_certificate.cpp (94%) rename frameworks/js/napi/{ => certificate}/src/napi_x509_crl.cpp (96%) rename frameworks/js/napi/{ => certificate}/src/napi_x509_crl_entry.cpp (96%) create mode 100644 frameworks/js/napi/crypto/BUILD.gn rename frameworks/js/napi/{ => crypto}/inc/napi_asy_key_generator.h (94%) rename frameworks/js/napi/{ => crypto}/inc/napi_cipher.h (95%) rename frameworks/js/napi/{ => crypto}/inc/napi_crypto_framework_defines.h (78%) rename frameworks/js/napi/{ => crypto}/inc/napi_key.h (93%) rename frameworks/js/napi/{ => crypto}/inc/napi_key_agreement.h (94%) rename frameworks/js/napi/{ => crypto}/inc/napi_key_pair.h (94%) rename frameworks/js/napi/{ => crypto}/inc/napi_mac.h (97%) rename frameworks/js/napi/{ => crypto}/inc/napi_md.h (97%) rename frameworks/js/napi/{ => crypto}/inc/napi_pri_key.h (93%) rename frameworks/js/napi/{ => crypto}/inc/napi_pub_key.h (93%) rename frameworks/js/napi/{ => crypto}/inc/napi_rand.h (97%) rename frameworks/js/napi/{ => crypto}/inc/napi_sign.h (96%) rename frameworks/js/napi/{ => crypto}/inc/napi_sym_key.h (90%) rename frameworks/js/napi/{ => crypto}/inc/napi_sym_key_generator.h (91%) rename frameworks/js/napi/{ => crypto}/inc/napi_utils.h (91%) rename frameworks/js/napi/{ => crypto}/inc/napi_verify.h (97%) rename frameworks/js/napi/{ => crypto}/src/napi_asy_key_generator.cpp (93%) rename frameworks/js/napi/{ => crypto}/src/napi_cipher.cpp (92%) rename frameworks/js/napi/{ => crypto}/src/napi_init.cpp (65%) rename frameworks/js/napi/{ => crypto}/src/napi_key.cpp (96%) rename frameworks/js/napi/{ => crypto}/src/napi_key_agreement.cpp (90%) rename frameworks/js/napi/{ => crypto}/src/napi_key_pair.cpp (100%) rename frameworks/js/napi/{ => crypto}/src/napi_mac.cpp (95%) rename frameworks/js/napi/{ => crypto}/src/napi_md.cpp (95%) rename frameworks/js/napi/{ => crypto}/src/napi_pri_key.cpp (95%) rename frameworks/js/napi/{ => crypto}/src/napi_pub_key.cpp (96%) rename frameworks/js/napi/{ => crypto}/src/napi_rand.cpp (96%) rename frameworks/js/napi/{ => crypto}/src/napi_sign.cpp (92%) rename frameworks/js/napi/{ => crypto}/src/napi_sym_key.cpp (97%) rename frameworks/js/napi/{ => crypto}/src/napi_sym_key_generator.cpp (92%) rename frameworks/js/napi/{ => crypto}/src/napi_utils.cpp (91%) rename frameworks/js/napi/{ => crypto}/src/napi_verify.cpp (92%) create mode 100644 interfaces/kits/js/@ohos.security.cert.d.ts diff --git a/BUILD.gn b/BUILD.gn index 7168321..468f633 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -27,8 +27,10 @@ group("crypto_openssl_plugin_lib") { group("cryptoframework_napi") { if (os_level == "standard") { - deps = - [ "//base/security/crypto_framework/frameworks:cryptoframework_napi" ] + deps = [ + "//base/security/crypto_framework/frameworks/js/napi/certificate:cert_napi", + "//base/security/crypto_framework/frameworks/js/napi/crypto:cryptoframework_napi", + ] } } diff --git a/README_zh.md b/README_zh.md index 289dc57..6d68c57 100644 --- a/README_zh.md +++ b/README_zh.md @@ -34,17 +34,20 @@ base/security/crypto_framwork ``` ## 约束 -[加解密算法库框架-overview-约束与限制](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/cryptoFramework-overview.md#%E7%BA%A6%E6%9D%9F%E4%B8%8E%E9%99%90%E5%88%B6) - +- [加解密算法库框架-overview-约束与限制](../../../docs/blob/master/zh-cn/application-dev/security/cryptoFramework-overview.md#%E7%BA%A6%E6%9D%9F%E4%B8%8E%E9%99%90%E5%88%B6) +- [证书-overview-约束与限制](../../../docs/blob/f1b6842ccdb2209bc8927b1d8b9180a9df795ad9/zh-cn/application-dev/security/cert-overview.md) ## 说明 ### 接口说明 -[加解密算法库框架-API参考](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-cryptoFramework.md) +- [加解密算法库框架-API参考](../../../docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-cryptoFramework.md) +- [证书-API参考](../../../docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-cert.md) ### 使用说明 -[加解密算法库框架-开发指南](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/security/cryptoFramework-guidelines.md) +- [加解密算法库框架-开发指南](../../../docs/blob/master/zh-cn/application-dev/security/cryptoFramework-guidelines.md) + +- [证书-开发指南](../../../docs/blob/master/zh-cn/application-dev/security/cert-guidelines.md) ## 相关仓 diff --git a/bundle.json b/bundle.json index e92dc0b..3ff987c 100644 --- a/bundle.json +++ b/bundle.json @@ -14,7 +14,7 @@ "component": { "name": "crypto_framework", "subsystem": "security", - "syscap": [ "SystemCapability.Security.CryptoFramework" ], + "syscap": [ "SystemCapability.Security.CryptoFramework", "SystemCapability.Security.Cert" ], "features": [], "adapted_system_type": [ "standard" diff --git a/cfi_blocklist.txt b/cfi_blocklist.txt index 4fc5765..b7fb0f1 100644 --- a/cfi_blocklist.txt +++ b/cfi_blocklist.txt @@ -2,4 +2,5 @@ src:*/frameworks/certificate/* src:*/frameworks/crypto_operation/* src:*/frameworks/key/* src:*/frameworks/rand/* -src:*/frameworks/js/napi/src/* \ No newline at end of file +src:*/frameworks/js/napi/certificate/src/* +src:*/frameworks/js/napi/crypto/src/* \ No newline at end of file diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 590080c..357fb1b 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -63,64 +63,3 @@ ohos_shared_library("crypto_framework_lib") { "hiviewdfx_hilog_native:libhilog", ] } - -ohos_shared_library("cryptoframework_napi") { - subsystem_name = "security" - part_name = "crypto_framework" - relative_install_dir = "module/security" - include_dirs = [ - "//base/security/crypto_framework/interfaces/kits/js", - "//third_party/bounds_checking_function/include", - "//commonlibrary/c_utils/base/include", - "//base/security/crypto_framework/frameworks/js/napi/inc", - "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", - ] - include_dirs += framework_inc_path - - if (os_level == "standard") { - sanitize = { - cfi = true - debug = false - blocklist = "//base/security/crypto_framework/cfi_blocklist.txt" - } - } - - cflags = [ - "-DHILOG_ENABLE", - "-fPIC", - "-g3", - ] - - sources = [ - "js/napi/src/napi_asy_key_generator.cpp", - "js/napi/src/napi_cert_chain_validator.cpp", - "js/napi/src/napi_cipher.cpp", - "js/napi/src/napi_init.cpp", - "js/napi/src/napi_key.cpp", - "js/napi/src/napi_key_agreement.cpp", - "js/napi/src/napi_key_pair.cpp", - "js/napi/src/napi_mac.cpp", - "js/napi/src/napi_md.cpp", - "js/napi/src/napi_pri_key.cpp", - "js/napi/src/napi_pub_key.cpp", - "js/napi/src/napi_rand.cpp", - "js/napi/src/napi_sign.cpp", - "js/napi/src/napi_sym_key.cpp", - "js/napi/src/napi_sym_key_generator.cpp", - "js/napi/src/napi_utils.cpp", - "js/napi/src/napi_verify.cpp", - "js/napi/src/napi_x509_certificate.cpp", - "js/napi/src/napi_x509_crl.cpp", - "js/napi/src/napi_x509_crl_entry.cpp", - ] - - deps = [ - "//base/security/crypto_framework/frameworks:crypto_framework_lib", - "//third_party/bounds_checking_function:libsec_static", - ] - - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "napi:ace_napi", - ] -} diff --git a/frameworks/crypto_operation/mac.c b/frameworks/crypto_operation/mac.c index 1ec15e8..2630fb4 100644 --- a/frameworks/crypto_operation/mac.c +++ b/frameworks/crypto_operation/mac.c @@ -149,9 +149,9 @@ static void MacDestroy(HcfObjectBase *self) HcfFree(impl); } -HcfResult HcfMacCreate(const char *algoName, HcfMac **macApi) +HcfResult HcfMacCreate(const char *algoName, HcfMac **mac) { - if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (macApi == NULL)) { + if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (mac == NULL)) { LOGE("Invalid input params while creating mac!"); return HCF_INVALID_PARAMS; } @@ -185,6 +185,6 @@ HcfResult HcfMacCreate(const char *algoName, HcfMac **macApi) returnMacApi->base.getMacLength = GetMacLength; returnMacApi->base.getAlgoName = GetAlgoName; returnMacApi->spiObj = spiObj; - *macApi = (HcfMac *)returnMacApi; + *mac = (HcfMac *)returnMacApi; return HCF_SUCCESS; } \ No newline at end of file diff --git a/frameworks/crypto_operation/md.c b/frameworks/crypto_operation/md.c index de6c3d1..fe180ca 100644 --- a/frameworks/crypto_operation/md.c +++ b/frameworks/crypto_operation/md.c @@ -137,9 +137,9 @@ static void MdDestroy(HcfObjectBase *self) HcfFree(impl); } -HcfResult HcfMdCreate(const char *algoName, HcfMd **mdApi) +HcfResult HcfMdCreate(const char *algoName, HcfMd **md) { - if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (mdApi == NULL)) { + if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (md == NULL)) { LOGE("Invalid input params while creating md!"); return HCF_INVALID_PARAMS; } @@ -172,6 +172,6 @@ HcfResult HcfMdCreate(const char *algoName, HcfMd **mdApi) returnMdApi->base.getMdLength = GetMdLength; returnMdApi->base.getAlgoName = GetAlgoName; returnMdApi->spiObj = spiObj; - *mdApi = (HcfMd *)returnMdApi; + *md = (HcfMd *)returnMdApi; return HCF_SUCCESS; } \ No newline at end of file diff --git a/frameworks/js/napi/certificate/BUILD.gn b/frameworks/js/napi/certificate/BUILD.gn new file mode 100644 index 0000000..554b308 --- /dev/null +++ b/frameworks/js/napi/certificate/BUILD.gn @@ -0,0 +1,64 @@ +# 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. + +import("//base/security/crypto_framework/common/common.gni") +import("//base/security/crypto_framework/frameworks/frameworks.gni") +import("//build/ohos.gni") + +ohos_shared_library("cert_napi") { + subsystem_name = "security" + part_name = "crypto_framework" + relative_install_dir = "module/security" + include_dirs = [ + "//base/security/crypto_framework/interfaces/kits/js", + "//third_party/bounds_checking_function/include", + "//commonlibrary/c_utils/base/include", + "//base/security/crypto_framework/frameworks/js/napi/certificate/inc", + "//base/security/crypto_framework/frameworks/js/napi/crypto/inc", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + ] + include_dirs += framework_inc_path + + if (os_level == "standard") { + sanitize = { + cfi = true + debug = false + blocklist = "//base/security/crypto_framework/cfi_blocklist.txt" + } + } + + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-g3", + ] + + sources = [ + "src/napi_cert_chain_validator.cpp", + "src/napi_certificate_init.cpp", + "src/napi_x509_certificate.cpp", + "src/napi_x509_crl.cpp", + "src/napi_x509_crl_entry.cpp", + ] + + deps = [ + "//base/security/crypto_framework/frameworks:crypto_framework_lib", + "//base/security/crypto_framework/frameworks/js/napi/crypto:cryptoframework_napi", + "//third_party/bounds_checking_function:libsec_static", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] +} diff --git a/frameworks/js/napi/inc/napi_cert_chain_validator.h b/frameworks/js/napi/certificate/inc/napi_cert_chain_validator.h similarity index 100% rename from frameworks/js/napi/inc/napi_cert_chain_validator.h rename to frameworks/js/napi/certificate/inc/napi_cert_chain_validator.h diff --git a/frameworks/js/napi/inc/napi_x509_certificate.h b/frameworks/js/napi/certificate/inc/napi_x509_certificate.h similarity index 100% rename from frameworks/js/napi/inc/napi_x509_certificate.h rename to frameworks/js/napi/certificate/inc/napi_x509_certificate.h diff --git a/frameworks/js/napi/inc/napi_x509_crl.h b/frameworks/js/napi/certificate/inc/napi_x509_crl.h similarity index 100% rename from frameworks/js/napi/inc/napi_x509_crl.h rename to frameworks/js/napi/certificate/inc/napi_x509_crl.h diff --git a/frameworks/js/napi/inc/napi_x509_crl_entry.h b/frameworks/js/napi/certificate/inc/napi_x509_crl_entry.h similarity index 100% rename from frameworks/js/napi/inc/napi_x509_crl_entry.h rename to frameworks/js/napi/certificate/inc/napi_x509_crl_entry.h diff --git a/frameworks/js/napi/src/napi_cert_chain_validator.cpp b/frameworks/js/napi/certificate/src/napi_cert_chain_validator.cpp similarity index 95% rename from frameworks/js/napi/src/napi_cert_chain_validator.cpp rename to frameworks/js/napi/certificate/src/napi_cert_chain_validator.cpp index 7f5767f..8f9a13f 100644 --- a/frameworks/js/napi/src/napi_cert_chain_validator.cpp +++ b/frameworks/js/napi/certificate/src/napi_cert_chain_validator.cpp @@ -80,7 +80,7 @@ static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, true); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -98,7 +98,8 @@ static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result) if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, true)); } } @@ -135,7 +136,7 @@ napi_value NapiCertChainValidator::Validate(napi_env env, napi_callback_info inf napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } CfCtx *context = static_cast(HcfMalloc(sizeof(CfCtx), 0)); @@ -153,7 +154,7 @@ napi_value NapiCertChainValidator::Validate(napi_env env, napi_callback_info inf } napi_value promise = nullptr; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, argv[PARAM1], &context->callback)) { + if (!GetCallbackFromJSParams(env, argv[PARAM1], &context->callback, true)) { LOGE("get callback failed!"); FreeCryptoFwkCtx(env, context); return nullptr; @@ -207,20 +208,20 @@ napi_value NapiCertChainValidator::CreateCertChainValidator(napi_env env, napi_c napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != ARGS_SIZE_ONE) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", true)); LOGE("invalid params count!"); return nullptr; } std::string algorithm; - if (!GetStringFromJSParams(env, argv[PARAM0], algorithm)) { + if (!GetStringFromJSParams(env, argv[PARAM0], algorithm, true)) { LOGE("Failed to get algorithm."); return nullptr; } HcfCertChainValidator *certChainValidator = nullptr; HcfResult res = HcfCertChainValidatorCreate(algorithm.c_str(), &certChainValidator); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create cert chain validator failed")); + napi_throw(env, GenerateBusinessError(env, res, "create cert chain validator failed", true)); LOGE("Failed to create c cert chain validator."); return nullptr; } diff --git a/frameworks/js/napi/certificate/src/napi_certificate_init.cpp b/frameworks/js/napi/certificate/src/napi_certificate_init.cpp new file mode 100644 index 0000000..30b1745 --- /dev/null +++ b/frameworks/js/napi/certificate/src/napi_certificate_init.cpp @@ -0,0 +1,123 @@ +/* + * 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 "securec.h" +#include "log.h" + +#include "napi_x509_certificate.h" +#include "napi_cert_chain_validator.h" +#include "napi_pub_key.h" +#include "napi_utils.h" +#include "napi_x509_crl_entry.h" +#include "napi_x509_crl.h" +#include "napi_crypto_framework_defines.h" + +namespace OHOS { +namespace CryptoFramework { +static napi_value CreateEncodingFormat(napi_env env) +{ + napi_value encodingFormat = nullptr; + napi_create_object(env, &encodingFormat); + + AddUint32Property(env, encodingFormat, "FORMAT_DER", HCF_FORMAT_DER); + AddUint32Property(env, encodingFormat, "FORMAT_PEM", HCF_FORMAT_PEM); + + return encodingFormat; +} + +static void DefineEncodingFormatProperties(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY("EncodingFormat", CreateEncodingFormat(env)), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); +} + +static napi_value CreateCertResultCode(napi_env env) +{ + napi_value resultCode = nullptr; + napi_create_object(env, &resultCode); + + AddUint32Property(env, resultCode, "INVALID_PARAMS", JS_ERR_CERT_INVALID_PARAMS); + AddUint32Property(env, resultCode, "NOT_SUPPORT", JS_ERR_CERT_NOT_SUPPORT); + AddUint32Property(env, resultCode, "ERR_OUT_OF_MEMORY", JS_ERR_CERT_OUT_OF_MEMORY); + AddUint32Property(env, resultCode, "ERR_RUNTIME_ERROR", JS_ERR_RUNTIME_ERROR); + AddUint32Property(env, resultCode, "ERR_CRYPTO_OPERATION", JS_ERR_CERT_CRYPTO_OPERATION); + AddUint32Property(env, resultCode, "ERR_CERT_SIGNATURE_FAILURE", JS_ERR_CERT_SIGNATURE_FAILURE); + AddUint32Property(env, resultCode, "ERR_CERT_NOT_YET_VALID", JS_ERR_CERT_NOT_YET_VALID); + AddUint32Property(env, resultCode, "ERR_CERT_HAS_EXPIRED", JS_ERR_CERT_HAS_EXPIRED); + AddUint32Property(env, resultCode, "ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY", + JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY); + AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_CERTSIGN", JS_ERR_KEYUSAGE_NO_CERTSIGN); + AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE", JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE); + + return resultCode; +} + +static void DefineResultCodeProperties(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY("CertResult", CreateCertResultCode(env)), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); +} + +static void CreateCryptoModule(napi_env env) +{ + napi_value globalValue; + napi_get_global(env, &globalValue); + napi_value func; + napi_get_named_property(env, globalValue, "requireNapi", &func); + + napi_value cryptoInfo; + napi_create_string_utf8(env, "security.cryptoFramework", NAPI_AUTO_LENGTH, &cryptoInfo); + napi_value funcArgv[1] = { cryptoInfo }; + napi_value returnValue; + napi_call_function(env, globalValue, func, 1, funcArgv, &returnValue); +} + +/*********************************************** + * Module export and register + ***********************************************/ +static napi_value CertModuleExport(napi_env env, napi_value exports) +{ + LOGI("module init start."); + CreateCryptoModule(env); + DefineEncodingFormatProperties(env, exports); + DefineResultCodeProperties(env, exports); + + NapiCertChainValidator::DefineCertChainValidatorJSClass(env, exports); + NapiX509Certificate::DefineX509CertJSClass(env, exports); + NapiX509CrlEntry::DefineX509CrlEntryJSClass(env); + NapiX509Crl::DefineX509CrlJSClass(env, exports); + LOGI("module init end."); + return exports; +} + +extern "C" __attribute__((constructor)) void RegisterCertModule(void) +{ + napi_module cryptoFrameworkCertModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = CertModuleExport, + .nm_modname = "security.cert", + .nm_priv = ((void*)0), + .reserved = { 0 }, + }; + napi_module_register(&cryptoFrameworkCertModule); +} +} // namespace CryptoFramework +} // namespace OHOS diff --git a/frameworks/js/napi/src/napi_x509_certificate.cpp b/frameworks/js/napi/certificate/src/napi_x509_certificate.cpp similarity index 94% rename from frameworks/js/napi/src/napi_x509_certificate.cpp rename to frameworks/js/napi/certificate/src/napi_x509_certificate.cpp index 27c8c29..1c788f8 100644 --- a/frameworks/js/napi/src/napi_x509_certificate.cpp +++ b/frameworks/js/napi/certificate/src/napi_x509_certificate.cpp @@ -88,7 +88,7 @@ static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, true); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -106,7 +106,8 @@ static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result) if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, true)); } } @@ -124,7 +125,7 @@ static bool CreateCallbackAndPromise(napi_env env, CfCtx *context, size_t argc, { context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) { + if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, true)) { LOGE("get callback failed!"); return false; } @@ -153,7 +154,7 @@ static void VerifyComplete(napi_env env, napi_status status, void *data) FreeCryptoFwkCtx(env, context); } -static void checkValidityWithDateExecute(napi_env env, void *data) +static void CheckValidityWithDateExecute(napi_env env, void *data) { LOGI("start to check validity."); CfCtx *context = static_cast(data); @@ -165,7 +166,7 @@ static void checkValidityWithDateExecute(napi_env env, void *data) } } -static void checkValidityWithDateComplete(napi_env env, napi_status status, void *data) +static void CheckValidityWithDateComplete(napi_env env, napi_status status, void *data) { CfCtx *context = static_cast(data); ReturnResult(env, context, NapiGetNull(env)); @@ -243,7 +244,7 @@ napi_value NapiX509Certificate::Verify(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } @@ -257,7 +258,7 @@ napi_value NapiX509Certificate::Verify(napi_env env, napi_callback_info info) NapiPubKey *pubKey = nullptr; napi_unwrap(env, argv[PARAM0], (void**)&pubKey); if (pubKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "public key is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "public key is null", true)); LOGE("pubKey is null!"); FreeCryptoFwkCtx(env, context); return nullptr; @@ -290,7 +291,7 @@ napi_value NapiX509Certificate::GetEncoded(napi_env env, napi_callback_info info napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -327,7 +328,7 @@ napi_value NapiX509Certificate::GetPublicKey(napi_env env, napi_callback_info in napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -364,7 +365,7 @@ napi_value NapiX509Certificate::CheckValidityWithDate(napi_env env, napi_callbac napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } @@ -375,7 +376,7 @@ napi_value NapiX509Certificate::CheckValidityWithDate(napi_env env, napi_callbac } context->certClass = this; - if (!GetStringFromJSParams(env, argv[PARAM0], context->date)) { + if (!GetStringFromJSParams(env, argv[PARAM0], context->date, true)) { LOGE("get date param failed!"); FreeCryptoFwkCtx(env, context); return nullptr; @@ -388,8 +389,8 @@ napi_value NapiX509Certificate::CheckValidityWithDate(napi_env env, napi_callbac napi_create_async_work( env, nullptr, GetResourceName(env, "CheckValidityWithDate"), - checkValidityWithDateExecute, - checkValidityWithDateComplete, + CheckValidityWithDateExecute, + CheckValidityWithDateComplete, static_cast(context), &context->asyncWork); @@ -406,7 +407,7 @@ napi_value NapiX509Certificate::GetVersion(napi_env env, napi_callback_info info size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } HcfX509Certificate *cert = GetX509Cert(); @@ -422,7 +423,7 @@ napi_value NapiX509Certificate::GetSerialNumber(napi_env env, napi_callback_info size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } HcfX509Certificate *cert = GetX509Cert(); @@ -437,7 +438,7 @@ napi_value NapiX509Certificate::GetIssuerName(napi_env env, napi_callback_info i size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -449,7 +450,7 @@ napi_value NapiX509Certificate::GetIssuerName(napi_env env, napi_callback_info i HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getIssuerName(cert, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get issuer name failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get issuer name failed", true)); LOGE("getIssuerName failed!"); HcfFree(blob); blob = nullptr; @@ -467,7 +468,7 @@ napi_value NapiX509Certificate::GetSubjectName(napi_env env, napi_callback_info size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -479,7 +480,7 @@ napi_value NapiX509Certificate::GetSubjectName(napi_env env, napi_callback_info HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getSubjectName(cert, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get subject name failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get subject name failed", true)); LOGE("getSubjectName failed!"); HcfFree(blob); blob = nullptr; @@ -497,7 +498,7 @@ napi_value NapiX509Certificate::GetNotBeforeTime(napi_env env, napi_callback_inf size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -509,7 +510,7 @@ napi_value NapiX509Certificate::GetNotBeforeTime(napi_env env, napi_callback_inf HcfX509Certificate *cert = GetX509Cert(); HcfResult res = cert->getNotBeforeTime(cert, blob); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "get not before time failed")); + napi_throw(env, GenerateBusinessError(env, res, "get not before time failed", true)); LOGE("getNotBeforeTime failed!"); HcfFree(blob); blob = nullptr; @@ -528,7 +529,7 @@ napi_value NapiX509Certificate::GetNotAfterTime(napi_env env, napi_callback_info size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -540,7 +541,7 @@ napi_value NapiX509Certificate::GetNotAfterTime(napi_env env, napi_callback_info HcfX509Certificate *cert = GetX509Cert(); HcfResult res = cert->getNotAfterTime(cert, blob); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "get not after time failed")); + napi_throw(env, GenerateBusinessError(env, res, "get not after time failed", true)); LOGE("getNotAfterTime failed!"); HcfFree(blob); blob = nullptr; @@ -559,7 +560,7 @@ napi_value NapiX509Certificate::GetSignature(napi_env env, napi_callback_info in size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -571,7 +572,7 @@ napi_value NapiX509Certificate::GetSignature(napi_env env, napi_callback_info in HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getSignature(cert, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get signature failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get signature failed", true)); LOGE("getSignature failed!"); HcfFree(blob); blob = nullptr; @@ -589,7 +590,7 @@ napi_value NapiX509Certificate::GetSigAlgName(napi_env env, napi_callback_info i size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -601,7 +602,7 @@ napi_value NapiX509Certificate::GetSigAlgName(napi_env env, napi_callback_info i HcfX509Certificate *cert = GetX509Cert(); HcfResult res = cert->getSignatureAlgName(cert, blob); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "get signature alg name failed")); + napi_throw(env, GenerateBusinessError(env, res, "get signature alg name failed", true)); LOGE("getSignatureAlgName failed!"); HcfFree(blob); blob = nullptr; @@ -620,7 +621,7 @@ napi_value NapiX509Certificate::GetSigAlgOID(napi_env env, napi_callback_info in size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); @@ -631,7 +632,7 @@ napi_value NapiX509Certificate::GetSigAlgOID(napi_env env, napi_callback_info in HcfX509Certificate *cert = GetX509Cert(); HcfResult res = cert->getSignatureAlgOid(cert, blob); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "get signature alg oid failed")); + napi_throw(env, GenerateBusinessError(env, res, "get signature alg oid failed", true)); LOGE("getSignatureAlgOid failed!"); HcfFree(blob); blob = nullptr; @@ -650,7 +651,7 @@ napi_value NapiX509Certificate::GetSigAlgParams(napi_env env, napi_callback_info size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -662,7 +663,7 @@ napi_value NapiX509Certificate::GetSigAlgParams(napi_env env, napi_callback_info HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getSignatureAlgParams(cert, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get signature alg params failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get signature alg params failed", true)); LOGE("getSignatureAlgParams failed!"); HcfFree(blob); blob = nullptr; @@ -680,7 +681,7 @@ napi_value NapiX509Certificate::GetKeyUsage(napi_env env, napi_callback_info inf size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); @@ -691,7 +692,7 @@ napi_value NapiX509Certificate::GetKeyUsage(napi_env env, napi_callback_info inf HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getKeyUsage(cert, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get key usage failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get key usage failed", true)); LOGE("getKeyUsage failed!"); HcfFree(blob); blob = nullptr; @@ -708,7 +709,7 @@ napi_value NapiX509Certificate::GetExtendedKeyUsage(napi_env env, napi_callback_ { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -720,7 +721,7 @@ napi_value NapiX509Certificate::GetExtendedKeyUsage(napi_env env, napi_callback_ HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getExtKeyUsage(cert, array); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get ext key usage failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get ext key usage failed", true)); LOGE("call getExtKeyUsage failed!"); HcfFree(array); array = nullptr; @@ -739,7 +740,7 @@ napi_value NapiX509Certificate::GetBasicConstraints(napi_env env, napi_callback_ size_t argc = ARGS_SIZE_ZERO; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -754,7 +755,7 @@ napi_value NapiX509Certificate::GetSubjectAlternativeNames(napi_env env, napi_ca { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -766,7 +767,7 @@ napi_value NapiX509Certificate::GetSubjectAlternativeNames(napi_env env, napi_ca HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getSubjectAltNames(cert, array); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get subject alt names failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get subject alt names failed", true)); LOGE("call getSubjectAltNames failed!"); HcfFree(array); array = nullptr; @@ -783,7 +784,7 @@ napi_value NapiX509Certificate::GetIssuerAlternativeNames(napi_env env, napi_cal { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -795,7 +796,7 @@ napi_value NapiX509Certificate::GetIssuerAlternativeNames(napi_env env, napi_cal HcfX509Certificate *cert = GetX509Cert(); HcfResult ret = cert->getIssuerAltNames(cert, array); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get issuer alt names failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get issuer alt names failed", true)); LOGE("call getIssuerAltNames failed!"); HcfFree(array); array = nullptr; @@ -1093,7 +1094,7 @@ napi_value NapiX509Certificate::NapiCreateX509Cert(napi_env env, napi_callback_i napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } diff --git a/frameworks/js/napi/src/napi_x509_crl.cpp b/frameworks/js/napi/certificate/src/napi_x509_crl.cpp similarity index 96% rename from frameworks/js/napi/src/napi_x509_crl.cpp rename to frameworks/js/napi/certificate/src/napi_x509_crl.cpp index b4b8a48..656b6a9 100644 --- a/frameworks/js/napi/src/napi_x509_crl.cpp +++ b/frameworks/js/napi/certificate/src/napi_x509_crl.cpp @@ -95,7 +95,7 @@ static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, true); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -113,7 +113,8 @@ static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result) if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, true)); } } @@ -131,7 +132,7 @@ static bool CreateCallbackAndPromise(napi_env env, CfCtx *context, size_t argc, { context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) { + if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, true)) { LOGE("get callback failed!"); return false; } @@ -388,14 +389,14 @@ napi_value NapiX509Crl::IsRevoked(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } NapiX509Certificate *x509Cert = nullptr; napi_unwrap(env, argv[PARAM0], (void**)&x509Cert); if (x509Cert == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "x509 cert is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "x509 cert is null", true)); LOGE("x509Cert is null!"); return nullptr; } @@ -431,7 +432,7 @@ napi_value NapiX509Crl::GetType(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -448,7 +449,7 @@ napi_value NapiX509Crl::GetEncoded(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -485,14 +486,14 @@ napi_value NapiX509Crl::Verify(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } NapiPubKey *pubKey = nullptr; napi_unwrap(env, argv[PARAM0], (void**)&pubKey); if (pubKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "public key is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "public key is null", true)); LOGE("pubKey is null!"); return nullptr; } @@ -529,7 +530,7 @@ napi_value NapiX509Crl::GetVersion(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -544,7 +545,7 @@ napi_value NapiX509Crl::GetIssuerDN(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -556,7 +557,7 @@ napi_value NapiX509Crl::GetIssuerDN(napi_env env, napi_callback_info info) } HcfResult ret = x509Crl->getIssuerName(x509Crl, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get issuer name failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get issuer name failed", true)); LOGE("getIssuerDN failed!"); HcfFree(blob); blob = nullptr; @@ -573,7 +574,7 @@ napi_value NapiX509Crl::GetThisUpdate(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -585,7 +586,7 @@ napi_value NapiX509Crl::GetThisUpdate(napi_env env, napi_callback_info info) } HcfResult ret = x509Crl->getLastUpdate(x509Crl, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get last update failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get last update failed", true)); LOGE("getLastUpdate failed!"); HcfFree(blob); blob = nullptr; @@ -603,7 +604,7 @@ napi_value NapiX509Crl::GetNextUpdate(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -615,7 +616,7 @@ napi_value NapiX509Crl::GetNextUpdate(napi_env env, napi_callback_info info) } HcfResult ret = x509Crl->getNextUpdate(x509Crl, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get next update failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get next update failed", true)); LOGE("getNextUpdate failed!"); HcfFree(blob); blob = nullptr; @@ -635,7 +636,7 @@ napi_value NapiX509Crl::GetRevokedCertificate(napi_env env, napi_callback_info i napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } @@ -646,7 +647,7 @@ napi_value NapiX509Crl::GetRevokedCertificate(napi_env env, napi_callback_info i } context->crlClass = this; - if (!GetInt32FromJSParams(env, argv[PARAM0], context->serialNumber)) { + if (!GetInt32FromJSParams(env, argv[PARAM0], context->serialNumber, true)) { LOGE("get serialNumber failed!"); FreeCryptoFwkCtx(env, context); return nullptr; @@ -678,14 +679,14 @@ napi_value NapiX509Crl::GetRevokedCertificateWithCert(napi_env env, napi_callbac napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } NapiX509Certificate *x509Cert = nullptr; napi_unwrap(env, argv[PARAM0], (void**)&x509Cert); if (x509Cert == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "x509 cert is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "x509 cert is null", true)); LOGE("x509Cert is null!"); return nullptr; } @@ -724,7 +725,7 @@ napi_value NapiX509Crl::GetRevokedCertificates(napi_env env, napi_callback_info napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -761,7 +762,7 @@ napi_value NapiX509Crl::GetTBSCertList(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -796,7 +797,7 @@ napi_value NapiX509Crl::GetSignature(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -808,7 +809,7 @@ napi_value NapiX509Crl::GetSignature(napi_env env, napi_callback_info info) } HcfResult result = x509Crl->getSignature(x509Crl, blob); if (result != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, result, "get signature failed")); + napi_throw(env, GenerateBusinessError(env, result, "get signature failed", true)); LOGE("getSignature failed!"); HcfFree(blob); blob = nullptr; @@ -825,7 +826,7 @@ napi_value NapiX509Crl::GetSigAlgName(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -837,7 +838,7 @@ napi_value NapiX509Crl::GetSigAlgName(napi_env env, napi_callback_info info) } HcfResult ret = x509Crl->getSignatureAlgName(x509Crl, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get signature alg name failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get signature alg name failed", true)); LOGE("getSigAlgName failed!"); HcfFree(blob); blob = nullptr; @@ -855,7 +856,7 @@ napi_value NapiX509Crl::GetSigAlgOID(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -867,7 +868,7 @@ napi_value NapiX509Crl::GetSigAlgOID(napi_env env, napi_callback_info info) } HcfResult ret = x509Crl->getSignatureAlgOid(x509Crl, blob); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, ret, "get signature alg oid failed")); + napi_throw(env, GenerateBusinessError(env, ret, "get signature alg oid failed", true)); LOGE("getSigAlgOID failed!"); HcfFree(blob); blob = nullptr; @@ -885,7 +886,7 @@ napi_value NapiX509Crl::GetSigAlgParams(napi_env env, napi_callback_info info) { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -897,7 +898,7 @@ napi_value NapiX509Crl::GetSigAlgParams(napi_env env, napi_callback_info info) } HcfResult result = x509Crl->getSignatureAlgParams(x509Crl, blob); if (result != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, result, "get signature alg params failed")); + napi_throw(env, GenerateBusinessError(env, result, "get signature alg params failed", true)); LOGE("getSigAlgParams failed!"); HcfFree(blob); blob = nullptr; @@ -1158,7 +1159,7 @@ napi_value NapiX509Crl::NapiCreateX509Crl(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, true)) { return nullptr; } diff --git a/frameworks/js/napi/src/napi_x509_crl_entry.cpp b/frameworks/js/napi/certificate/src/napi_x509_crl_entry.cpp similarity index 96% rename from frameworks/js/napi/src/napi_x509_crl_entry.cpp rename to frameworks/js/napi/certificate/src/napi_x509_crl_entry.cpp index e7b0133..53c48ea 100644 --- a/frameworks/js/napi/src/napi_x509_crl_entry.cpp +++ b/frameworks/js/napi/certificate/src/napi_x509_crl_entry.cpp @@ -73,7 +73,7 @@ static void ReturnCallbackResult(napi_env env, CfCtx *context, napi_value result { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, true); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -91,7 +91,8 @@ static void ReturnPromiseResult(napi_env env, CfCtx *context, napi_value result) if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, true)); } } @@ -109,7 +110,7 @@ static bool CreateCallbackAndPromise(napi_env env, CfCtx *context, size_t argc, { context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) { + if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, true)) { LOGE("get callback failed!"); return false; } @@ -235,7 +236,7 @@ napi_value NapiX509CrlEntry::GetEncoded(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -270,7 +271,7 @@ napi_value NapiX509CrlEntry::GetSerialNumber(napi_env env, napi_callback_info in { size_t argc = ARGS_SIZE_ZERO; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, true)) { return nullptr; } @@ -287,7 +288,7 @@ napi_value NapiX509CrlEntry::GetCertificateIssuer(napi_env env, napi_callback_in napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } @@ -324,7 +325,7 @@ napi_value NapiX509CrlEntry::GetRevocationDate(napi_env env, napi_callback_info napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, true)) { return nullptr; } diff --git a/frameworks/js/napi/crypto/BUILD.gn b/frameworks/js/napi/crypto/BUILD.gn new file mode 100644 index 0000000..e24a501 --- /dev/null +++ b/frameworks/js/napi/crypto/BUILD.gn @@ -0,0 +1,73 @@ +# 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. + +import("//base/security/crypto_framework/common/common.gni") +import("//base/security/crypto_framework/frameworks/frameworks.gni") +import("//build/ohos.gni") + +ohos_shared_library("cryptoframework_napi") { + subsystem_name = "security" + part_name = "crypto_framework" + relative_install_dir = "module/security" + include_dirs = [ + "//base/security/crypto_framework/interfaces/kits/js", + "//third_party/bounds_checking_function/include", + "//commonlibrary/c_utils/base/include", + "//base/security/crypto_framework/frameworks/js/napi/crypto/inc", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + ] + include_dirs += framework_inc_path + + if (os_level == "standard") { + sanitize = { + cfi = true + debug = false + blocklist = "//base/security/crypto_framework/cfi_blocklist.txt" + } + } + + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-g3", + ] + + sources = [ + "src/napi_asy_key_generator.cpp", + "src/napi_cipher.cpp", + "src/napi_init.cpp", + "src/napi_key.cpp", + "src/napi_key_agreement.cpp", + "src/napi_key_pair.cpp", + "src/napi_mac.cpp", + "src/napi_md.cpp", + "src/napi_pri_key.cpp", + "src/napi_pub_key.cpp", + "src/napi_rand.cpp", + "src/napi_sign.cpp", + "src/napi_sym_key.cpp", + "src/napi_sym_key_generator.cpp", + "src/napi_utils.cpp", + "src/napi_verify.cpp", + ] + + deps = [ + "//base/security/crypto_framework/frameworks:crypto_framework_lib", + "//third_party/bounds_checking_function:libsec_static", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "napi:ace_napi", + ] +} diff --git a/frameworks/js/napi/inc/napi_asy_key_generator.h b/frameworks/js/napi/crypto/inc/napi_asy_key_generator.h similarity index 94% rename from frameworks/js/napi/inc/napi_asy_key_generator.h rename to frameworks/js/napi/crypto/inc/napi_asy_key_generator.h index 620707d..39923ae 100644 --- a/frameworks/js/napi/inc/napi_asy_key_generator.h +++ b/frameworks/js/napi/crypto/inc/napi_asy_key_generator.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_ASY_KEY_GENERATOR_H #define HCF_NAPI_ASY_KEY_GENERATOR_H -#include +#include #include "asy_key_generator.h" #include "log.h" #include "napi/native_api.h" @@ -26,7 +26,7 @@ namespace OHOS { namespace CryptoFramework { class NapiAsyKeyGenerator { public: - NapiAsyKeyGenerator(HcfAsyKeyGenerator *generator); + explicit NapiAsyKeyGenerator(HcfAsyKeyGenerator *generator); ~NapiAsyKeyGenerator(); HcfAsyKeyGenerator *GetAsyKeyGenerator(); diff --git a/frameworks/js/napi/inc/napi_cipher.h b/frameworks/js/napi/crypto/inc/napi_cipher.h similarity index 95% rename from frameworks/js/napi/inc/napi_cipher.h rename to frameworks/js/napi/crypto/inc/napi_cipher.h index 14e0773..6c36b67 100644 --- a/frameworks/js/napi/inc/napi_cipher.h +++ b/frameworks/js/napi/crypto/inc/napi_cipher.h @@ -27,7 +27,7 @@ namespace OHOS { namespace CryptoFramework { class NapiCipher { public: - NapiCipher(HcfCipher *cipher); + explicit NapiCipher(HcfCipher *cipher); ~NapiCipher(); static void DefineCipherJSClass(napi_env env, napi_value exports); @@ -38,7 +38,7 @@ public: static napi_value JsCipherUpdate(napi_env env, napi_callback_info info); static napi_value JsCipherDoFinal(napi_env env, napi_callback_info info); static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info); - HcfCipher *GetCipher(); + HcfCipher *GetCipher() const; static thread_local napi_ref classRef_; private: diff --git a/frameworks/js/napi/inc/napi_crypto_framework_defines.h b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h similarity index 78% rename from frameworks/js/napi/inc/napi_crypto_framework_defines.h rename to frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h index c60707c..e76f397 100644 --- a/frameworks/js/napi/inc/napi_crypto_framework_defines.h +++ b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h @@ -16,7 +16,7 @@ #ifndef NAPI_CRYPTO_FRAMEWORK_DEFINES_H #define NAPI_CRYPTO_FRAMEWORK_DEFINES_H -#include +#include namespace OHOS { namespace CryptoFramework { @@ -37,12 +37,19 @@ constexpr uint32_t JS_ERR_NOT_SUPPORT = 801; constexpr uint32_t JS_ERR_OUT_OF_MEMORY = 17620001; constexpr uint32_t JS_ERR_RUNTIME_ERROR = 17620002; constexpr uint32_t JS_ERR_CRYPTO_OPERATION = 17630001; -constexpr uint32_t JS_ERR_CERT_SIGNATURE_FAILURE = 17630002; -constexpr uint32_t JS_ERR_CERT_NOT_YET_VALID = 17630003; -constexpr uint32_t JS_ERR_CERT_HAS_EXPIRED = 17630004; -constexpr uint32_t JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 17630005; -constexpr uint32_t JS_ERR_KEYUSAGE_NO_CERTSIGN = 17630006; -constexpr uint32_t JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE = 17630007; + +constexpr uint32_t JS_ERR_CERT_INVALID_PARAMS = 401; +constexpr uint32_t JS_ERR_CERT_NOT_SUPPORT = 801; +constexpr uint32_t JS_ERR_CERT_OUT_OF_MEMORY = 19020001; +constexpr uint32_t JS_ERR_CERT_RUNTIME_ERROR = 19020002; +constexpr uint32_t JS_ERR_CERT_CRYPTO_OPERATION = 19030001; +constexpr uint32_t JS_ERR_CERT_SIGNATURE_FAILURE = 19030002; +constexpr uint32_t JS_ERR_CERT_NOT_YET_VALID = 19030003; +constexpr uint32_t JS_ERR_CERT_HAS_EXPIRED = 19030004; +constexpr uint32_t JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 19030005; +constexpr uint32_t JS_ERR_KEYUSAGE_NO_CERTSIGN = 19030006; +constexpr uint32_t JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE = 19030007; + const std::string CRYPTO_TAG_DATA = "data"; const std::string CRYPTO_TAG_ERR_CODE = "code"; const std::string CRYPTO_TAG_ERR_MSG = "message"; diff --git a/frameworks/js/napi/inc/napi_key.h b/frameworks/js/napi/crypto/inc/napi_key.h similarity index 93% rename from frameworks/js/napi/inc/napi_key.h rename to frameworks/js/napi/crypto/inc/napi_key.h index 683206e..95a45dc 100644 --- a/frameworks/js/napi/inc/napi_key.h +++ b/frameworks/js/napi/crypto/inc/napi_key.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_HCF_KEY_H #define HCF_NAPI_HCF_KEY_H -#include +#include #include "log.h" #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -26,9 +26,9 @@ namespace OHOS { namespace CryptoFramework { class NapiKey { public: - NapiKey(HcfKey *hcfKey); + explicit NapiKey(HcfKey *hcfKey); virtual ~NapiKey(); - HcfKey *GetHcfKey(); + HcfKey *GetHcfKey() const; static void DefineHcfKeyJSClass(napi_env env); static napi_value KeyConstructor(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/inc/napi_key_agreement.h b/frameworks/js/napi/crypto/inc/napi_key_agreement.h similarity index 94% rename from frameworks/js/napi/inc/napi_key_agreement.h rename to frameworks/js/napi/crypto/inc/napi_key_agreement.h index 94e2491..7d3529a 100644 --- a/frameworks/js/napi/inc/napi_key_agreement.h +++ b/frameworks/js/napi/crypto/inc/napi_key_agreement.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_KEY_AGREEMENT_H #define HCF_NAPI_KEY_AGREEMENT_H -#include +#include #include "log.h" #include "key_agreement.h" #include "napi/native_api.h" @@ -26,7 +26,7 @@ namespace OHOS { namespace CryptoFramework { class NapiKeyAgreement { public: - NapiKeyAgreement(HcfKeyAgreement *keyAgreement); + explicit NapiKeyAgreement(HcfKeyAgreement *keyAgreement); ~NapiKeyAgreement(); HcfKeyAgreement *GetKeyAgreement(); diff --git a/frameworks/js/napi/inc/napi_key_pair.h b/frameworks/js/napi/crypto/inc/napi_key_pair.h similarity index 94% rename from frameworks/js/napi/inc/napi_key_pair.h rename to frameworks/js/napi/crypto/inc/napi_key_pair.h index a08e764..e5db6c0 100644 --- a/frameworks/js/napi/inc/napi_key_pair.h +++ b/frameworks/js/napi/crypto/inc/napi_key_pair.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_KEY_PAIR_H #define HCF_NAPI_KEY_PAIR_H -#include +#include #include "log.h" #include "key_pair.h" #include "napi/native_api.h" @@ -26,7 +26,7 @@ namespace OHOS { namespace CryptoFramework { class NapiKeyPair { public: - NapiKeyPair(HcfKeyPair *keyPair); + explicit NapiKeyPair(HcfKeyPair *keyPair); ~NapiKeyPair(); napi_value ConvertToJsKeyPair(napi_env env); diff --git a/frameworks/js/napi/inc/napi_mac.h b/frameworks/js/napi/crypto/inc/napi_mac.h similarity index 97% rename from frameworks/js/napi/inc/napi_mac.h rename to frameworks/js/napi/crypto/inc/napi_mac.h index 0af1c42..674c805 100644 --- a/frameworks/js/napi/inc/napi_mac.h +++ b/frameworks/js/napi/crypto/inc/napi_mac.h @@ -28,7 +28,7 @@ namespace OHOS { namespace CryptoFramework { class NapiMac { public: - NapiMac(HcfMac *macObj); + explicit NapiMac(HcfMac *macObj); ~NapiMac(); static napi_ref classRef_; diff --git a/frameworks/js/napi/inc/napi_md.h b/frameworks/js/napi/crypto/inc/napi_md.h similarity index 97% rename from frameworks/js/napi/inc/napi_md.h rename to frameworks/js/napi/crypto/inc/napi_md.h index 158eec3..4467807 100644 --- a/frameworks/js/napi/inc/napi_md.h +++ b/frameworks/js/napi/crypto/inc/napi_md.h @@ -28,7 +28,7 @@ namespace OHOS { namespace CryptoFramework { class NapiMd { public: - NapiMd(HcfMd *mdObj); + explicit NapiMd(HcfMd *mdObj); ~NapiMd(); static napi_ref classRef_; diff --git a/frameworks/js/napi/inc/napi_pri_key.h b/frameworks/js/napi/crypto/inc/napi_pri_key.h similarity index 93% rename from frameworks/js/napi/inc/napi_pri_key.h rename to frameworks/js/napi/crypto/inc/napi_pri_key.h index 011fbfb..b87dd94 100644 --- a/frameworks/js/napi/inc/napi_pri_key.h +++ b/frameworks/js/napi/crypto/inc/napi_pri_key.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_PRI_KEY_H #define HCF_NAPI_PRI_KEY_H -#include +#include #include "log.h" #include "pri_key.h" #include "napi_key.h" @@ -27,8 +27,8 @@ namespace OHOS { namespace CryptoFramework { class NapiPriKey : public NapiKey { public: - NapiPriKey(HcfPriKey *priKey); - ~NapiPriKey(); + explicit NapiPriKey(HcfPriKey *priKey); + ~NapiPriKey() override; HcfPriKey *GetPriKey(); napi_value ConvertToJsPriKey(napi_env env); diff --git a/frameworks/js/napi/inc/napi_pub_key.h b/frameworks/js/napi/crypto/inc/napi_pub_key.h similarity index 93% rename from frameworks/js/napi/inc/napi_pub_key.h rename to frameworks/js/napi/crypto/inc/napi_pub_key.h index bcbca92..389aaca 100644 --- a/frameworks/js/napi/inc/napi_pub_key.h +++ b/frameworks/js/napi/crypto/inc/napi_pub_key.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_PUB_KEY_H #define HCF_NAPI_PUB_KEY_H -#include +#include #include "log.h" #include "pub_key.h" #include "napi_key.h" @@ -27,8 +27,8 @@ namespace OHOS { namespace CryptoFramework { class NapiPubKey : public NapiKey { public: - NapiPubKey(HcfPubKey *pubKey); - ~NapiPubKey(); + explicit NapiPubKey(HcfPubKey *pubKey); + ~NapiPubKey() override; HcfPubKey *GetPubKey(); napi_value ConvertToJsPubKey(napi_env env); diff --git a/frameworks/js/napi/inc/napi_rand.h b/frameworks/js/napi/crypto/inc/napi_rand.h similarity index 97% rename from frameworks/js/napi/inc/napi_rand.h rename to frameworks/js/napi/crypto/inc/napi_rand.h index a4b2ebf..dc854dc 100644 --- a/frameworks/js/napi/inc/napi_rand.h +++ b/frameworks/js/napi/crypto/inc/napi_rand.h @@ -28,7 +28,7 @@ namespace OHOS { namespace CryptoFramework { class NapiRand { public: - NapiRand(HcfRand *randObj); + explicit NapiRand(HcfRand *randObj); ~NapiRand(); static napi_ref classRef_; diff --git a/frameworks/js/napi/inc/napi_sign.h b/frameworks/js/napi/crypto/inc/napi_sign.h similarity index 96% rename from frameworks/js/napi/inc/napi_sign.h rename to frameworks/js/napi/crypto/inc/napi_sign.h index b1557d1..e4f7776 100644 --- a/frameworks/js/napi/inc/napi_sign.h +++ b/frameworks/js/napi/crypto/inc/napi_sign.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_SIGNATURE_H #define HCF_NAPI_SIGNATURE_H -#include +#include #include "log.h" #include "signature.h" #include "napi/native_api.h" @@ -26,7 +26,7 @@ namespace OHOS { namespace CryptoFramework { class NapiSign { public: - NapiSign(HcfSign *sign); + explicit NapiSign(HcfSign *sign); ~NapiSign(); HcfSign *GetSign(); diff --git a/frameworks/js/napi/inc/napi_sym_key.h b/frameworks/js/napi/crypto/inc/napi_sym_key.h similarity index 90% rename from frameworks/js/napi/inc/napi_sym_key.h rename to frameworks/js/napi/crypto/inc/napi_sym_key.h index 4a3c646..a0613b0 100644 --- a/frameworks/js/napi/inc/napi_sym_key.h +++ b/frameworks/js/napi/crypto/inc/napi_sym_key.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_SYM_KEY_H #define HCF_NAPI_SYM_KEY_H -#include +#include #include "log.h" #include "napi_key.h" #include "napi/native_api.h" @@ -27,9 +27,9 @@ namespace OHOS { namespace CryptoFramework { class NapiSymKey : public NapiKey { public: - NapiSymKey(HcfSymKey *symKey); - ~NapiSymKey(); - HcfSymKey *GetSymKey(); + explicit NapiSymKey(HcfSymKey *symKey); + ~NapiSymKey() override; + HcfSymKey *GetSymKey() const; static void DefineSymKeyJSClass(napi_env env); static napi_value CreateSymKey(napi_env env); diff --git a/frameworks/js/napi/inc/napi_sym_key_generator.h b/frameworks/js/napi/crypto/inc/napi_sym_key_generator.h similarity index 91% rename from frameworks/js/napi/inc/napi_sym_key_generator.h rename to frameworks/js/napi/crypto/inc/napi_sym_key_generator.h index 68401ee..92f6591 100644 --- a/frameworks/js/napi/inc/napi_sym_key_generator.h +++ b/frameworks/js/napi/crypto/inc/napi_sym_key_generator.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_SYM_KEY_GENERATOR_H #define HCF_NAPI_SYM_KEY_GENERATOR_H -#include +#include #include "log.h" #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -26,9 +26,9 @@ namespace OHOS { namespace CryptoFramework { class NapiSymKeyGenerator { public: - NapiSymKeyGenerator(HcfSymKeyGenerator *generator); + explicit NapiSymKeyGenerator(HcfSymKeyGenerator *generator); ~NapiSymKeyGenerator(); - HcfSymKeyGenerator *GetSymKeyGenerator(); + HcfSymKeyGenerator *GetSymKeyGenerator() const; static void DefineSymKeyGeneratorJSClass(napi_env env, napi_value exports); static napi_value CreateSymKeyGenerator(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h similarity index 91% rename from frameworks/js/napi/inc/napi_utils.h rename to frameworks/js/napi/crypto/inc/napi_utils.h index 493b4c7..46a4f07 100644 --- a/frameworks/js/napi/inc/napi_utils.h +++ b/frameworks/js/napi/crypto/inc/napi_utils.h @@ -16,7 +16,7 @@ #ifndef HCF_NAPI_UILTS_H #define HCF_NAPI_UILTS_H -#include +#include #include #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -48,19 +48,19 @@ HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg); bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec); napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob); -bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr); -bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt); -bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt); -bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb); +bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr, bool isCertFunc); +bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt, bool isCertFunc); +bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt, bool isCertFunc); +bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb, bool isCertFunc); bool GetEncodingBlobFromValue(napi_env env, napi_value object, HcfEncodingBlob **encodingBlob); bool GetCertChainFromValue(napi_env env, napi_value object, HcfCertChainData **certChainData); -bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync); +bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync, bool isCertFunc); napi_value GetResourceName(napi_env env, const char *name); napi_value GenerateArrayBuffer(napi_env env, uint8_t *data, uint32_t size); napi_value NapiGetNull(napi_env env); napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array); napi_value ConvertEncodingBlobToNapiValue(napi_env env, HcfEncodingBlob *encodingBlob); -napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg); +napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg, bool isCertFunc); } // namespace CryptoFramework } // namespace OHOS #endif diff --git a/frameworks/js/napi/inc/napi_verify.h b/frameworks/js/napi/crypto/inc/napi_verify.h similarity index 97% rename from frameworks/js/napi/inc/napi_verify.h rename to frameworks/js/napi/crypto/inc/napi_verify.h index ac0aec9..2d2aa07 100644 --- a/frameworks/js/napi/inc/napi_verify.h +++ b/frameworks/js/napi/crypto/inc/napi_verify.h @@ -26,7 +26,7 @@ namespace OHOS { namespace CryptoFramework { class NapiVerify { public: - NapiVerify(HcfVerify *verify); + explicit NapiVerify(HcfVerify *verify); ~NapiVerify(); HcfVerify *GetVerify(); diff --git a/frameworks/js/napi/src/napi_asy_key_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp similarity index 93% rename from frameworks/js/napi/src/napi_asy_key_generator.cpp rename to frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp index 4b06761..8cd9d2d 100644 --- a/frameworks/js/napi/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -114,16 +114,16 @@ static bool BuildGenKeyPairCtx(napi_env env, napi_callback_info info, GenKeyPair napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; NapiAsyKeyGenerator *napiGenerator; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator)); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok) { LOGE("failed to unwrap napi asyKeyGenerator obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } @@ -134,7 +134,7 @@ static bool BuildGenKeyPairCtx(napi_env env, napi_callback_info info, GenKeyPair napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -148,7 +148,8 @@ static bool GetPkAndSkBlobFromNapiValueIfInput(napi_env env, napi_value pkValue, pubKey = GetBlobFromNapiValue(env, pkValue); if (pubKey == nullptr) { LOGE("failed to get pubKey."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: must be of the DataBlob type.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "[PubKey]: must be of the DataBlob type.", false)); return false; } } @@ -159,7 +160,8 @@ static bool GetPkAndSkBlobFromNapiValueIfInput(napi_env env, napi_value pkValue, priKey = GetBlobFromNapiValue(env, skValue); if (priKey == nullptr) { LOGE("failed to get priKey."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: must be of the DataBlob type.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "[PriKey]: must be of the DataBlob type.", false)); return false; } } @@ -178,16 +180,16 @@ static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKey napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; NapiAsyKeyGenerator *napiGenerator; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator)); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok) { LOGE("failed to unwrap napi asyKeyGenerator obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } @@ -206,7 +208,7 @@ static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKey napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -214,7 +216,7 @@ static void ReturnGenKeyPairCallbackResult(napi_env env, GenKeyPairCtx *ctx, nap { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -233,7 +235,8 @@ static void ReturnGenKeyPairPromiseResult(napi_env env, GenKeyPairCtx *ctx, napi if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, + GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false)); } } @@ -241,7 +244,7 @@ static void ReturnConvertKeyCallbackResult(napi_env env, ConvertKeyCtx *ctx, nap { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -260,7 +263,8 @@ static void ReturnConvertKeyPromiseResult(napi_env env, ConvertKeyCtx *ctx, napi if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, + GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false)); } } @@ -418,10 +422,10 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPair(napi_env env, napi_callback_in { LOGI("enter ..."); GenKeyPairCtx *ctx = static_cast(HcfMalloc(sizeof(GenKeyPairCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc ctx fail.")); - return NULL; + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc ctx fail.", false)); + return nullptr; } if (!BuildGenKeyPairCtx(env, info, ctx)) { @@ -437,9 +441,9 @@ napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info in { LOGI("enter ..."); ConvertKeyCtx *ctx = static_cast(HcfMalloc(sizeof(ConvertKeyCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildConvertKeyCtx(env, info, ctx)) { @@ -481,12 +485,12 @@ napi_value NapiAsyKeyGenerator::CreateJsAsyKeyGenerator(napi_env env, napi_callb napi_new_instance(env, constructor, argc, argv, &instance); std::string algName; - if (!GetStringFromJSParams(env, argv[0], algName)) { + if (!GetStringFromJSParams(env, argv[0], algName, false)) { LOGE("failed to get algoName."); return NapiGetNull(env); } - HcfAsyKeyGenerator *generator = NULL; + HcfAsyKeyGenerator *generator = nullptr; int32_t res = HcfAsyKeyGeneratorCreate(algName.c_str(), &generator); if (res != HCF_SUCCESS) { LOGE("create c generator fail."); diff --git a/frameworks/js/napi/src/napi_cipher.cpp b/frameworks/js/napi/crypto/src/napi_cipher.cpp similarity index 92% rename from frameworks/js/napi/src/napi_cipher.cpp rename to frameworks/js/napi/crypto/src/napi_cipher.cpp index 78c2b56..a172e4b 100644 --- a/frameworks/js/napi/src/napi_cipher.cpp +++ b/frameworks/js/napi/crypto/src/napi_cipher.cpp @@ -41,8 +41,8 @@ struct CipherFwkCtxT { HcfCipher *cipher = nullptr; HcfKey *key = nullptr; HcfParamsSpec *paramsSpec = nullptr; - HcfBlob input = { 0 }; - HcfBlob output = { 0 }; + HcfBlob input = { .data = nullptr, .len = 0 }; + HcfBlob output = { .data = nullptr, .len = 0 }; enum HcfCryptoMode opMode = ENCRYPT_MODE; int32_t errCode = 0; @@ -134,13 +134,13 @@ bool BuildContextForInit(napi_env env, napi_callback_info info, CipherFwkCtx con napi_value argv[ARGS_SIZE_FOUR] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "init failed for wrong argument num.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "init failed for wrong argument num.", false)); LOGE("wrong argument num. require 3 or 4 arguments. [Argc]: %zu!", argc); return false; } context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok) { LOGE("failed to unwrap napi napiCipher obj!"); return false; @@ -149,16 +149,16 @@ bool BuildContextForInit(napi_env env, napi_callback_info info, CipherFwkCtx con // get opMode, type is uint32 size_t index = ARGS_SIZE_ZERO; - if (napi_get_value_uint32(env, argv[index++], (uint32_t *)&(context->opMode)) != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get opMode failed!")); + if (napi_get_value_uint32(env, argv[index++], reinterpret_cast(&(context->opMode))) != napi_ok) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get opMode failed!", false)); LOGE("get opMode failed!"); return false; } // get key, unwrap from JS - status = napi_unwrap(env, argv[index++], (void **)&napiKey); + status = napi_unwrap(env, argv[index++], reinterpret_cast(&napiKey)); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi napiSymKey obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi napiSymKey obj!", false)); LOGE("failed to unwrap napi napiSymKey obj!"); return false; } @@ -169,7 +169,7 @@ bool BuildContextForInit(napi_env env, napi_callback_info info, CipherFwkCtx con napi_typeof(env, argv[index], &valueType); if (valueType != napi_null) { if (!GetParamsSpecFromNapiValue(env, argv[index], context->opMode, &context->paramsSpec)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get valid params spec!")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get valid params spec!", false)); LOGE("GetParamsSpecFromNapiValue failed!"); return false; } @@ -180,7 +180,7 @@ bool BuildContextForInit(napi_env env, napi_callback_info info, CipherFwkCtx con napi_create_promise(env, &context->deferred, &context->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[index], &context->callback); + return GetCallbackFromJSParams(env, argv[index], &context->callback, false); } } @@ -193,13 +193,13 @@ bool BuildContextForUpdate(napi_env env, napi_callback_info info, CipherFwkCtx c napi_value argv[ARGS_SIZE_TWO] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "update failed for wrong argument num.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "update failed for wrong argument num.", false)); LOGE("wrong argument num. require 1 or 2 arguments. [Argc]: %zu!", argc); return false; } context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok) { LOGE("failed to unwrap napi napiCipher obj!"); return false; @@ -211,7 +211,7 @@ bool BuildContextForUpdate(napi_env env, napi_callback_info info, CipherFwkCtx c HcfBlob *input = nullptr; input = GetBlobFromNapiValue(env, argv[index++]); if (input == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "update failed for invalid input blob.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "update failed for invalid input blob.", false)); LOGE("GetBlobFromNapiValue failed!"); return false; } @@ -222,7 +222,7 @@ bool BuildContextForUpdate(napi_env env, napi_callback_info info, CipherFwkCtx c napi_create_promise(env, &context->deferred, &context->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[index], &context->callback); + return GetCallbackFromJSParams(env, argv[index], &context->callback, false); } } @@ -235,13 +235,14 @@ bool BuildContextForFinal(napi_env env, napi_callback_info info, CipherFwkCtx co napi_value argv[ARGS_SIZE_TWO] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "doFinal failed for invalid input blob.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "doFinal failed for invalid input blob.", false)); LOGE("wrong argument num. require 1 or 2 arguments. [Argc]: %zu!", argc); return false; } context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok) { LOGE("failed to unwrap napi napiCipher obj!"); return false; @@ -256,7 +257,8 @@ bool BuildContextForFinal(napi_env env, napi_callback_info info, CipherFwkCtx co HcfBlob *input = nullptr; input = GetBlobFromNapiValue(env, argv[index]); if (input == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "doFinal failed for invalid input blob.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "doFinal failed for invalid input blob.", false)); LOGE("GetBlobFromNapiValue failed!"); return false; } @@ -269,7 +271,7 @@ bool BuildContextForFinal(napi_env env, napi_callback_info info, CipherFwkCtx co napi_create_promise(env, &context->deferred, &context->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[index], &context->callback); + return GetCallbackFromJSParams(env, argv[index], &context->callback, false); } } @@ -277,7 +279,7 @@ static void ReturnCallbackResult(napi_env env, CipherFwkCtx context, napi_value { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -295,7 +297,8 @@ static void ReturnPromiseResult(napi_env env, CipherFwkCtx context, napi_value r if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, false)); } } @@ -312,7 +315,7 @@ void AsyncInitProcess(napi_env env, void *data) HcfResult res = cipher->init(cipher, context->opMode, key, params); if (res != HCF_SUCCESS) { - LOGE("init ret:%d", (int32_t)res); + LOGE("init ret:%d", res); context->errCode = res; context->errMsg = "init failed."; return; @@ -330,7 +333,7 @@ void AsyncUpdateProcess(napi_env env, void *data) HcfCipher *cipher = context->cipher; HcfResult res = cipher->update(cipher, &context->input, &context->output); if (res != HCF_SUCCESS) { - LOGE("Update ret:%d!", (int32_t)res); + LOGE("Update ret:%d!", res); context->errCode = res; context->errMsg = "update failed."; return; @@ -348,7 +351,7 @@ void AsyncDoFinalProcess(napi_env env, void *data) HcfResult res = cipher->doFinal(cipher, &context->input, &context->output); if (res != HCF_SUCCESS) { - LOGE("doFinal ret:%d!", (int32_t)res); + LOGE("doFinal ret:%d!", res); context->errCode = res; context->errMsg = "doFinal failed."; return; @@ -501,7 +504,7 @@ NapiCipher::~NapiCipher() HcfObjDestroy(this->cipher_); } -HcfCipher *NapiCipher::GetCipher() +HcfCipher *NapiCipher::GetCipher() const { return this->cipher_; } @@ -563,8 +566,7 @@ napi_value NapiCipher::JsGetAlgorithm(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - // get HcfSymKeyGenerator pointer - napi_status status = napi_unwrap(env, thisVar, (void **)&napiCipher); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok) { LOGE("failed to unwrap napiCipher obj!"); return nullptr; @@ -599,7 +601,7 @@ napi_value NapiCipher::CreateCipher(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); if (argc != expectedArgc) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid.", false)); LOGE("The input args num is invalid."); return nullptr; } @@ -612,7 +614,7 @@ napi_value NapiCipher::CreateCipher(napi_env env, napi_callback_info info) // parse input string std::string algoName; - if (!GetStringFromJSParams(env, argv[ARGS_SIZE_ZERO], algoName)) { + if (!GetStringFromJSParams(env, argv[ARGS_SIZE_ZERO], algoName, false)) { LOGE("GetStringFromJSParams failed!"); return nullptr; } @@ -621,7 +623,7 @@ napi_value NapiCipher::CreateCipher(napi_env env, napi_callback_info info) HcfCipher *cipher = nullptr; HcfResult res = HcfCipherCreate(algoName.c_str(), &cipher); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create C cipher fail!")); + napi_throw(env, GenerateBusinessError(env, res, "create C cipher fail!", false)); LOGE("create C cipher fail!"); return nullptr; } diff --git a/frameworks/js/napi/src/napi_init.cpp b/frameworks/js/napi/crypto/src/napi_init.cpp similarity index 65% rename from frameworks/js/napi/src/napi_init.cpp rename to frameworks/js/napi/crypto/src/napi_init.cpp index 449d548..96d20c2 100644 --- a/frameworks/js/napi/src/napi_init.cpp +++ b/frameworks/js/napi/crypto/src/napi_init.cpp @@ -15,13 +15,10 @@ #include "securec.h" #include "log.h" -#include "memory.h" -#include "napi_x509_certificate.h" #include "napi_asy_key_generator.h" #include "napi_sym_key_generator.h" #include "napi_cipher.h" -#include "napi_cert_chain_validator.h" #include "napi_key_pair.h" #include "napi_pri_key.h" #include "napi_pub_key.h" @@ -34,31 +31,10 @@ #include "napi_sym_key.h" #include "napi_key.h" #include "napi_utils.h" -#include "napi_x509_crl_entry.h" -#include "napi_x509_crl.h" #include "napi_crypto_framework_defines.h" namespace OHOS { namespace CryptoFramework { -static napi_value CreateEncodingFormat(napi_env env) -{ - napi_value encodingFormat = nullptr; - napi_create_object(env, &encodingFormat); - - AddUint32Property(env, encodingFormat, "FORMAT_DER", HCF_FORMAT_DER); - AddUint32Property(env, encodingFormat, "FORMAT_PEM", HCF_FORMAT_PEM); - - return encodingFormat; -} - -static void DefineEncodingFormatProperties(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("EncodingFormat", CreateEncodingFormat(env)), - }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); -} - static napi_value CreateCryptoMode(napi_env env) { napi_value cryptoMode = nullptr; @@ -87,13 +63,6 @@ static napi_value CreateResultCode(napi_env env) AddUint32Property(env, resultCode, "ERR_OUT_OF_MEMORY", JS_ERR_OUT_OF_MEMORY); AddUint32Property(env, resultCode, "ERR_RUNTIME_ERROR", JS_ERR_RUNTIME_ERROR); AddUint32Property(env, resultCode, "ERR_CRYPTO_OPERATION", JS_ERR_CRYPTO_OPERATION); - AddUint32Property(env, resultCode, "ERR_CERT_SIGNATURE_FAILURE", JS_ERR_CERT_SIGNATURE_FAILURE); - AddUint32Property(env, resultCode, "ERR_CERT_NOT_YET_VALID", JS_ERR_CERT_NOT_YET_VALID); - AddUint32Property(env, resultCode, "ERR_CERT_HAS_EXPIRED", JS_ERR_CERT_HAS_EXPIRED); - AddUint32Property(env, resultCode, "ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY", - JS_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY); - AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_CERTSIGN", JS_ERR_KEYUSAGE_NO_CERTSIGN); - AddUint32Property(env, resultCode, "ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE", JS_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE); return resultCode; } @@ -113,7 +82,6 @@ static napi_value ModuleExport(napi_env env, napi_value exports) { LOGI("module init start."); - DefineEncodingFormatProperties(env, exports); DefineCryptoModeProperties(env, exports); DefineResultCodeProperties(env, exports); NapiAsyKeyGenerator::DefineAsyKeyGeneratorJSClass(env, exports); @@ -124,8 +92,6 @@ static napi_value ModuleExport(napi_env env, napi_value exports) NapiSign::DefineSignJSClass(env, exports); NapiVerify::DefineVerifyJSClass(env, exports); NapiKeyAgreement::DefineKeyAgreementJSClass(env, exports); - // NapiCertFactory::DefineCertFactoryJSClass(env, exports); - NapiCertChainValidator::DefineCertChainValidatorJSClass(env, exports); NapiMac::DefineMacJSClass(env, exports); NapiMd::DefineMdJSClass(env, exports); NapiPubKey::DefinePubKeyJSClass(env); @@ -134,25 +100,21 @@ static napi_value ModuleExport(napi_env env, napi_value exports) NapiCipher::DefineCipherJSClass(env, exports); NapiSymKey::DefineSymKeyJSClass(env); NapiKey::DefineHcfKeyJSClass(env); - NapiX509Certificate::DefineX509CertJSClass(env, exports); - NapiX509CrlEntry::DefineX509CrlEntryJSClass(env); - NapiX509Crl::DefineX509CrlJSClass(env, exports); LOGI("module init end."); return exports; } -static napi_module cryptoFrameworkModule = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = ModuleExport, - .nm_modname = "security.cryptoFramework", - .nm_priv = ((void*)0), - .reserved = { 0 }, -}; - extern "C" __attribute__((constructor)) void RegisterModule(void) { + napi_module cryptoFrameworkModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ModuleExport, + .nm_modname = "security.cryptoFramework", + .nm_priv = ((void*)0), + .reserved = { 0 }, + }; napi_module_register(&cryptoFrameworkModule); } } // namespace CryptoFramework diff --git a/frameworks/js/napi/src/napi_key.cpp b/frameworks/js/napi/crypto/src/napi_key.cpp similarity index 96% rename from frameworks/js/napi/src/napi_key.cpp rename to frameworks/js/napi/crypto/src/napi_key.cpp index 8323ff1..ae80d9a 100644 --- a/frameworks/js/napi/src/napi_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_key.cpp @@ -17,7 +17,6 @@ #include "securec.h" #include "log.h" -#include "memory.h" #include "napi_utils.h" #include "napi_crypto_framework_defines.h" @@ -36,7 +35,7 @@ NapiKey::~NapiKey() this->hcfKey_ = nullptr; } -HcfKey *NapiKey::GetHcfKey() +HcfKey *NapiKey::GetHcfKey() const { return this->hcfKey_; } @@ -80,15 +79,15 @@ napi_value NapiKey::JsGetEncoded(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&napiKey))); HcfKey *key = napiKey->GetHcfKey(); - HcfBlob blob = {0}; + HcfBlob blob = { .data = nullptr, .len = 0 }; HcfResult res = key->getEncoded(key, &blob); if (res != 0) { - napi_throw(env, GenerateBusinessError(env, res, "getEncoded failed.")); + napi_throw(env, GenerateBusinessError(env, res, "getEncoded failed.", false)); LOGE("getEncoded failed!"); return nullptr; } napi_value instance = ConvertBlobToNapiValue(env, &blob); - HcfFree(blob.data); + HcfBlobDataFree(&blob); return instance; } diff --git a/frameworks/js/napi/src/napi_key_agreement.cpp b/frameworks/js/napi/crypto/src/napi_key_agreement.cpp similarity index 90% rename from frameworks/js/napi/src/napi_key_agreement.cpp rename to frameworks/js/napi/crypto/src/napi_key_agreement.cpp index c2371e5..cb92759 100644 --- a/frameworks/js/napi/src/napi_key_agreement.cpp +++ b/frameworks/js/napi/crypto/src/napi_key_agreement.cpp @@ -79,34 +79,34 @@ static bool BuildKeyAgreementJsCtx(napi_env env, napi_callback_info info, KeyAgr napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - NapiKeyAgreement *napiKeyAgreement = NULL; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiKeyAgreement)); + NapiKeyAgreement *napiKeyAgreement = nullptr; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiKeyAgreement)); if (status != napi_ok) { LOGE("failed to unwrap napi verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } size_t index = 0; - NapiPriKey *napiPriKey = NULL; - status = napi_unwrap(env, argv[index], (void **)(&napiPriKey)); + NapiPriKey *napiPriKey = nullptr; + status = napi_unwrap(env, argv[index], reinterpret_cast(&napiPriKey)); if (status != napi_ok) { LOGE("failed to unwrap priKey verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: param unwarp error.", false)); return false; } index++; - NapiPubKey *napiPubKey = NULL; - status = napi_unwrap(env, argv[index], (void **)(&napiPubKey)); + NapiPubKey *napiPubKey = nullptr; + status = napi_unwrap(env, argv[index], reinterpret_cast(&napiPubKey)); if (status != napi_ok) { LOGE("failed to unwrap napi pubKey obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: param unwarp error.", false)); return false; } @@ -118,7 +118,7 @@ static bool BuildKeyAgreementJsCtx(napi_env env, napi_callback_info info, KeyAgr napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -126,7 +126,7 @@ static void ReturnCallbackResult(napi_env env, KeyAgreementCtx *ctx, napi_value { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -145,7 +145,8 @@ static void ReturnPromiseResult(napi_env env, KeyAgreementCtx *ctx, napi_value r if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, + GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false)); } } @@ -226,9 +227,9 @@ napi_value NapiKeyAgreement::JsGenerateSecret(napi_env env, napi_callback_info i { LOGI("enter ..."); KeyAgreementCtx *ctx = static_cast(HcfMalloc(sizeof(KeyAgreementCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildKeyAgreementJsCtx(env, info, ctx)) { @@ -270,11 +271,11 @@ napi_value NapiKeyAgreement::CreateJsKeyAgreement(napi_env env, napi_callback_in napi_new_instance(env, constructor, argc, argv, &instance); std::string algName; - if (!GetStringFromJSParams(env, argv[0], algName)) { + if (!GetStringFromJSParams(env, argv[0], algName, false)) { return nullptr; } - HcfKeyAgreement *keyAgreement = NULL; + HcfKeyAgreement *keyAgreement = nullptr; int32_t res = HcfKeyAgreementCreate(algName.c_str(), &keyAgreement); if (res != HCF_SUCCESS) { LOGE("create c keyAgreement fail."); diff --git a/frameworks/js/napi/src/napi_key_pair.cpp b/frameworks/js/napi/crypto/src/napi_key_pair.cpp similarity index 100% rename from frameworks/js/napi/src/napi_key_pair.cpp rename to frameworks/js/napi/crypto/src/napi_key_pair.cpp diff --git a/frameworks/js/napi/src/napi_mac.cpp b/frameworks/js/napi/crypto/src/napi_mac.cpp similarity index 95% rename from frameworks/js/napi/src/napi_mac.cpp rename to frameworks/js/napi/crypto/src/napi_mac.cpp index 96fcf97..bf1ad5b 100644 --- a/frameworks/js/napi/src/napi_mac.cpp +++ b/frameworks/js/napi/crypto/src/napi_mac.cpp @@ -77,7 +77,7 @@ static void ReturnCallbackResult(napi_env env, MacCtx *context, napi_value resul { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -95,7 +95,8 @@ static void ReturnPromiseResult(napi_env env, MacCtx *context, napi_value result if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, false)); } } @@ -104,7 +105,7 @@ static bool CreateCallbackAndPromise(napi_env env, MacCtx *context, size_t argc, { context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) { + if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, false)) { LOGE("get callback failed!"); return false; } @@ -222,12 +223,12 @@ napi_value NapiMac::MacInit(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, false)) { return nullptr; } MacCtx *context = static_cast(HcfMalloc(sizeof(MacCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return nullptr; } @@ -235,7 +236,7 @@ napi_value NapiMac::MacInit(napi_env env, napi_callback_info info) NapiSymKey *symKey = nullptr; napi_unwrap(env, argv[PARAM0], (void**)&symKey); if (symKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "symKey is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "symKey is null", false)); LOGE("symKey is null!"); FreeCryptoFwkCtx(env, context); return nullptr; @@ -267,19 +268,19 @@ napi_value NapiMac::MacUpdate(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, false)) { return nullptr; } MacCtx *context = static_cast(HcfMalloc(sizeof(MacCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return nullptr; } context->macClass = this; context->inBlob = GetBlobFromNapiValue(env, argv[PARAM0]); if (context->inBlob == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null", false)); LOGE("inBlob is null!"); return nullptr; } @@ -308,12 +309,12 @@ napi_value NapiMac::MacDoFinal(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, false)) { return nullptr; } MacCtx *context = static_cast(HcfMalloc(sizeof(MacCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return nullptr; } @@ -343,7 +344,7 @@ napi_value NapiMac::GetMacLength(napi_env env, napi_callback_info info) size_t argc = expectedArgsCount; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, false)) { return nullptr; } HcfMac *macObj = GetMac(); @@ -423,14 +424,14 @@ napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info) return nullptr; } std::string algoName; - if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + if (!GetStringFromJSParams(env, argv[PARAM0], algoName, false)) { LOGE("Failed to get algorithm."); return nullptr; } HcfMac *macObj = nullptr; HcfResult res = HcfMacCreate(algoName.c_str(), &macObj); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.", false)); LOGE("create c macObj failed."); return nullptr; } diff --git a/frameworks/js/napi/src/napi_md.cpp b/frameworks/js/napi/crypto/src/napi_md.cpp similarity index 95% rename from frameworks/js/napi/src/napi_md.cpp rename to frameworks/js/napi/crypto/src/napi_md.cpp index bbe8f71..1fb9a34 100644 --- a/frameworks/js/napi/src/napi_md.cpp +++ b/frameworks/js/napi/crypto/src/napi_md.cpp @@ -75,7 +75,7 @@ static void ReturnCallbackResult(napi_env env, MdCtx *context, napi_value result { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; napi_value func = nullptr; @@ -92,7 +92,8 @@ static void ReturnPromiseResult(napi_env env, MdCtx *context, napi_value result) if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, false)); } } @@ -101,7 +102,7 @@ static bool CreateCallbackAndPromise(napi_env env, MdCtx *context, size_t argc, { context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) { + if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, false)) { LOGE("get callback failed!"); return false; } @@ -190,19 +191,19 @@ napi_value NapiMd::MdUpdate(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_TWO] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_TWO, false, false)) { return nullptr; } MdCtx *context = static_cast(HcfMalloc(sizeof(MdCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return nullptr; } context->mdClass = this; context->inBlob = GetBlobFromNapiValue(env, argv[PARAM0]); if (context->inBlob == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null", false)); LOGE("inBlob is null!"); return nullptr; } @@ -231,12 +232,12 @@ napi_value NapiMd::MdDoFinal(napi_env env, napi_callback_info info) napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ONE, false, false)) { return nullptr; } MdCtx *context = static_cast(HcfMalloc(sizeof(MdCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return nullptr; } @@ -265,7 +266,7 @@ napi_value NapiMd::GetMdLength(napi_env env, napi_callback_info info) size_t argc = expectedArgsCount; napi_value thisVar = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr); - if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true)) { + if (!CheckArgsCount(env, argc, ARGS_SIZE_ZERO, true, false)) { return nullptr; } HcfMd *mdObj = GetMd(); @@ -335,14 +336,14 @@ napi_value NapiMd::CreateMd(napi_env env, napi_callback_info info) return nullptr; } std::string algoName; - if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + if (!GetStringFromJSParams(env, argv[PARAM0], algoName, false)) { LOGE("Failed to get algorithm."); return nullptr; } HcfMd *mdObj = nullptr; HcfResult res = HcfMdCreate(algoName.c_str(), &mdObj); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.", false)); LOGE("create c mdObj failed."); return nullptr; } diff --git a/frameworks/js/napi/src/napi_pri_key.cpp b/frameworks/js/napi/crypto/src/napi_pri_key.cpp similarity index 95% rename from frameworks/js/napi/src/napi_pri_key.cpp rename to frameworks/js/napi/crypto/src/napi_pri_key.cpp index 6c406ec..9014e15 100644 --- a/frameworks/js/napi/src/napi_pri_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_pri_key.cpp @@ -16,7 +16,6 @@ #include "napi_pri_key.h" #include "log.h" -#include "memory.h" #include "napi_crypto_framework_defines.h" #include "napi_utils.h" #include "securec.h" @@ -74,7 +73,7 @@ napi_value NapiPriKey::JsGetEncoded(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); NapiPriKey *napiPriKey = nullptr; - napi_unwrap(env, thisVar, (void **)(&napiPriKey)); + napi_unwrap(env, thisVar, reinterpret_cast(&napiPriKey)); HcfPriKey *priKey = napiPriKey->GetPriKey(); @@ -86,7 +85,7 @@ napi_value NapiPriKey::JsGetEncoded(napi_env env, napi_callback_info info) } napi_value instance = ConvertBlobToNapiValue(env, &returnBlob); - HcfFree(returnBlob.data); + HcfBlobDataFree(&returnBlob); return instance; } @@ -95,7 +94,7 @@ napi_value NapiPriKey::JsClearMem(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); NapiPriKey *napiPriKey = nullptr; - napi_unwrap(env, thisVar, (void **)(&napiPriKey)); + napi_unwrap(env, thisVar, reinterpret_cast(&napiPriKey)); HcfPriKey *priKey = napiPriKey->GetPriKey(); diff --git a/frameworks/js/napi/src/napi_pub_key.cpp b/frameworks/js/napi/crypto/src/napi_pub_key.cpp similarity index 96% rename from frameworks/js/napi/src/napi_pub_key.cpp rename to frameworks/js/napi/crypto/src/napi_pub_key.cpp index 7acd807..ac5ce9a 100644 --- a/frameworks/js/napi/src/napi_pub_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_pub_key.cpp @@ -16,7 +16,6 @@ #include "napi_pub_key.h" #include "log.h" -#include "memory.h" #include "napi_crypto_framework_defines.h" #include "napi_utils.h" #include "securec.h" @@ -74,7 +73,7 @@ napi_value NapiPubKey::JsGetEncoded(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); NapiPubKey *napiPubKey = nullptr; - napi_unwrap(env, thisVar, (void **)(&napiPubKey)); + napi_unwrap(env, thisVar, reinterpret_cast(&napiPubKey)); HcfPubKey *pubKey = napiPubKey->GetPubKey(); HcfBlob returnBlob; @@ -85,7 +84,7 @@ napi_value NapiPubKey::JsGetEncoded(napi_env env, napi_callback_info info) } napi_value instance = ConvertBlobToNapiValue(env, &returnBlob); - HcfFree(returnBlob.data); + HcfBlobDataFree(&returnBlob); return instance; } diff --git a/frameworks/js/napi/src/napi_rand.cpp b/frameworks/js/napi/crypto/src/napi_rand.cpp similarity index 96% rename from frameworks/js/napi/src/napi_rand.cpp rename to frameworks/js/napi/crypto/src/napi_rand.cpp index 79ad203..05b32be 100644 --- a/frameworks/js/napi/src/napi_rand.cpp +++ b/frameworks/js/napi/crypto/src/napi_rand.cpp @@ -75,7 +75,7 @@ static void ReturnCallbackResult(napi_env env, RandCtx *context, napi_value resu { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -93,7 +93,8 @@ static void ReturnPromiseResult(napi_env env, RandCtx *context, napi_value resul if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, false)); } } @@ -102,7 +103,7 @@ static bool CreateCallbackAndPromise(napi_env env, RandCtx *context, size_t argc { context->asyncType = (argc == maxCount) ? ASYNC_TYPE_CALLBACK : ASYNC_TYPE_PROMISE; if (context->asyncType == ASYNC_TYPE_CALLBACK) { - if (!GetCallbackFromJSParams(env, callbackValue, &context->callback)) { + if (!GetCallbackFromJSParams(env, callbackValue, &context->callback, false)) { LOGE("get callback failed!"); return false; } @@ -197,18 +198,18 @@ napi_value NapiRand::GenerateRandom(napi_env env, napi_callback_info info) napi_value ret = NapiGetNull(env); napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgsCount) && (argc != expectedArgsCount - CALLBACK_SIZE)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", false)); LOGE("The arguments count is not expected!"); return ret; } RandCtx *context = static_cast(HcfMalloc(sizeof(RandCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return ret; } context->randClass = this; - if (!GetUint32FromJSParams(env, argv[PARAM0], context->numBytes)) { + if (!GetUint32FromJSParams(env, argv[PARAM0], context->numBytes, false)) { LOGE("get numBytes failed!"); FreeCryptoFwkCtx(env, context); return ret; @@ -240,20 +241,20 @@ napi_value NapiRand::SetSeed(napi_env env, napi_callback_info info) napi_value ret = NapiGetNull(env); napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgsCount) && (argc != expectedArgsCount - CALLBACK_SIZE)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", false)); LOGE("The arguments count is not expected!"); return ret; } RandCtx *context = static_cast(HcfMalloc(sizeof(RandCtx), 0)); if (context == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed", false)); LOGE("malloc context failed!"); return ret; } context->randClass = this; context->seedBlob = GetBlobFromNapiValue(env, argv[PARAM0]); if (context->seedBlob == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "seedBlob is null")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "seedBlob is null", false)); LOGE("seedBlob is null!"); return ret; } @@ -322,7 +323,7 @@ napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info) HcfRand *randObj = nullptr; HcfResult res = HcfRandCreate(&randObj); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.", false)); LOGE("create c randObj failed."); return nullptr; } diff --git a/frameworks/js/napi/src/napi_sign.cpp b/frameworks/js/napi/crypto/src/napi_sign.cpp similarity index 92% rename from frameworks/js/napi/src/napi_sign.cpp rename to frameworks/js/napi/crypto/src/napi_sign.cpp index c0497c1..4fe9f75 100644 --- a/frameworks/js/napi/src/napi_sign.cpp +++ b/frameworks/js/napi/crypto/src/napi_sign.cpp @@ -147,25 +147,25 @@ static bool BuildSignJsInitCtx(napi_env env, napi_callback_info info, SignInitCt napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - NapiSign *napiSign = NULL; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiSign)); + NapiSign *napiSign = nullptr; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiSign)); if (status != napi_ok) { LOGE("failed to unwrap napi sign obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } size_t index = 0; - NapiPriKey *napiPriKey = NULL; - status = napi_unwrap(env, argv[index], (void **)(&napiPriKey)); + NapiPriKey *napiPriKey = nullptr; + status = napi_unwrap(env, argv[index], reinterpret_cast(&napiPriKey)); if (status != napi_ok) { LOGE("failed to unwrap napi priKey obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PriKey]: param unwarp error.", false)); return false; } @@ -177,7 +177,7 @@ static bool BuildSignJsInitCtx(napi_env env, napi_callback_info info, SignInitCt napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -190,16 +190,16 @@ static bool BuildSignJsUpdateCtx(napi_env env, napi_callback_info info, SignUpda napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgc) && (argc != expectedArgc - 1)) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == PARAMS_NUM_TWO) ? ASYNC_CALLBACK : ASYNC_PROMISE; - NapiSign *napiSign = NULL; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiSign)); + NapiSign *napiSign = nullptr; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiSign)); if (status != napi_ok) { LOGE("failed to unwrap napi sign obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } @@ -207,7 +207,8 @@ static bool BuildSignJsUpdateCtx(napi_env env, napi_callback_info info, SignUpda HcfBlob *blob = GetBlobFromNapiValue(env, argv[index]); if (blob == nullptr) { LOGE("failed to get data."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "[Data]: must be of the DataBlob type.", false)); return false; } @@ -218,7 +219,7 @@ static bool BuildSignJsUpdateCtx(napi_env env, napi_callback_info info, SignUpda napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -231,16 +232,16 @@ static bool BuildSignJsDoFinalCtx(napi_env env, napi_callback_info info, SignDoF napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgc) && (argc != expectedArgc - 1)) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == PARAMS_NUM_TWO) ? ASYNC_CALLBACK : ASYNC_PROMISE; NapiSign *napiSign = nullptr; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiSign)); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiSign)); if (status != napi_ok) { LOGE("failed to unwrap napi sign obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } @@ -252,7 +253,8 @@ static bool BuildSignJsDoFinalCtx(napi_env env, napi_callback_info info, SignDoF data = GetBlobFromNapiValue(env, argv[index]); if (data == nullptr) { LOGE("failed to get data."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "[Data]: must be of the DataBlob type.", false)); return false; } } @@ -264,7 +266,7 @@ static bool BuildSignJsDoFinalCtx(napi_env env, napi_callback_info info, SignDoF napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -272,7 +274,7 @@ static void ReturnInitCallbackResult(napi_env env, SignInitCtx *ctx, napi_value { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_ONE] = { businessError }; @@ -291,7 +293,8 @@ static void ReturnInitPromiseResult(napi_env env, SignInitCtx *ctx, napi_value r if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, + COMMON_ERR_MSG.c_str(), false)); } } @@ -299,7 +302,7 @@ static void ReturnUpdateCallbackResult(napi_env env, SignUpdateCtx *ctx, napi_va { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_ONE] = { businessError }; @@ -318,7 +321,8 @@ static void ReturnUpdatePromiseResult(napi_env env, SignUpdateCtx *ctx, napi_val if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, + COMMON_ERR_MSG.c_str(), false)); } } @@ -326,7 +330,7 @@ static void ReturnDoFinalCallbackResult(napi_env env, SignDoFinalCtx *ctx, napi_ { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -345,7 +349,8 @@ static void ReturnDoFinalPromiseResult(napi_env env, SignDoFinalCtx *ctx, napi_v if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, + COMMON_ERR_MSG.c_str(), false)); } } @@ -529,9 +534,9 @@ napi_value NapiSign::JsInit(napi_env env, napi_callback_info info) { LOGI("enter ..."); SignInitCtx *ctx = static_cast(HcfMalloc(sizeof(SignInitCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildSignJsInitCtx(env, info, ctx)) { @@ -547,9 +552,9 @@ napi_value NapiSign::JsUpdate(napi_env env, napi_callback_info info) { LOGI("enter ..."); SignUpdateCtx *ctx = static_cast(HcfMalloc(sizeof(SignUpdateCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildSignJsUpdateCtx(env, info, ctx)) { @@ -565,9 +570,9 @@ napi_value NapiSign::JsSign(napi_env env, napi_callback_info info) { LOGI("enter ..."); SignDoFinalCtx *ctx = static_cast(HcfMalloc(sizeof(SignDoFinalCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildSignJsDoFinalCtx(env, info, ctx)) { @@ -609,11 +614,11 @@ napi_value NapiSign::CreateJsSign(napi_env env, napi_callback_info info) napi_new_instance(env, constructor, argc, argv, &instance); std::string algName; - if (!GetStringFromJSParams(env, argv[0], algName)) { + if (!GetStringFromJSParams(env, argv[0], algName, false)) { return nullptr; } - HcfSign *sign = NULL; + HcfSign *sign = nullptr; int32_t res = HcfSignCreate(algName.c_str(), &sign); if (res != HCF_SUCCESS) { LOGE("create c sign fail."); diff --git a/frameworks/js/napi/src/napi_sym_key.cpp b/frameworks/js/napi/crypto/src/napi_sym_key.cpp similarity index 97% rename from frameworks/js/napi/src/napi_sym_key.cpp rename to frameworks/js/napi/crypto/src/napi_sym_key.cpp index 842a9ba..3aa64e5 100644 --- a/frameworks/js/napi/src/napi_sym_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_sym_key.cpp @@ -17,7 +17,6 @@ #include "securec.h" #include "log.h" -#include "memory.h" #include "napi_utils.h" #include "napi_crypto_framework_defines.h" @@ -29,7 +28,7 @@ NapiSymKey::NapiSymKey(HcfSymKey *symKey) : NapiKey(reinterpret_cast(s NapiSymKey::~NapiSymKey() {} -HcfSymKey *NapiSymKey::GetSymKey() +HcfSymKey *NapiSymKey::GetSymKey() const { return reinterpret_cast(NapiKey::GetHcfKey()); } diff --git a/frameworks/js/napi/src/napi_sym_key_generator.cpp b/frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp similarity index 92% rename from frameworks/js/napi/src/napi_sym_key_generator.cpp rename to frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp index d6b61fa..1d2b862 100644 --- a/frameworks/js/napi/src/napi_sym_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp @@ -39,7 +39,7 @@ struct SymKeyGeneratorFwkCtxT { const char *errMsg = nullptr; HcfSymKeyGenerator *generator = nullptr; - HcfBlob keyMaterial = { 0 }; + HcfBlob keyMaterial = { .data = nullptr, .len = 0 }; }; using SymKeyGeneratorFwkCtx = SymKeyGeneratorFwkCtxT *; @@ -80,13 +80,14 @@ static bool BuildContextForGenerateKey(napi_env env, napi_callback_info info, Sy napi_value argv[ARGS_SIZE_ONE] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key failed for wrong argument num.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "generate key failed for wrong argument num.", false)); LOGE("wrong argument num. require 0 or 1 arguments. [Argc]: %zu!", argc); return false; } context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; NapiSymKeyGenerator *napiGenerator; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator)); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok) { LOGE("failed to unwrap NapiSymKeyGenerator obj!"); return false; @@ -101,7 +102,7 @@ static bool BuildContextForGenerateKey(napi_env env, napi_callback_info info, Sy napi_create_promise(env, &context->deferred, &context->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[ARGS_SIZE_ZERO], &context->callback); + return GetCallbackFromJSParams(env, argv[ARGS_SIZE_ZERO], &context->callback, false); } } @@ -113,14 +114,15 @@ static bool BuildContextForConvertKey(napi_env env, napi_callback_info info, Sym napi_value argv[ARGS_SIZE_TWO] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc && argc != expectedArgc - 1) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "convert key failed for wrong argument num.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "convert key failed for wrong argument num.", false)); LOGE("wrong argument num. require 1 or 2 arguments. [Argc]: %zu!", argc); return false; } context->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; NapiSymKeyGenerator *napiGenerator; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiGenerator)); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok) { LOGE("failed to unwrap NapiSymKeyGenerator obj!"); return false; @@ -135,7 +137,8 @@ static bool BuildContextForConvertKey(napi_env env, napi_callback_info info, Sym size_t index = 0; HcfBlob *blob = GetBlobFromNapiValue(env, argv[index++]); if (blob == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "convert key failed for invalid input blob.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "convert key failed for invalid input blob.", false)); LOGE("get keyMaterial failed!"); return false; } @@ -145,7 +148,7 @@ static bool BuildContextForConvertKey(napi_env env, napi_callback_info info, Sym napi_create_promise(env, &context->deferred, &context->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[index], &context->callback); + return GetCallbackFromJSParams(env, argv[index], &context->callback, false); } } @@ -154,7 +157,8 @@ static void ReturnPromiseResult(napi_env env, SymKeyGeneratorFwkCtx context, nap if (context->errCode == HCF_SUCCESS) { napi_resolve_deferred(env, context->deferred, result); } else { - napi_reject_deferred(env, context->deferred, GenerateBusinessError(env, context->errCode, context->errMsg)); + napi_reject_deferred(env, context->deferred, + GenerateBusinessError(env, context->errCode, context->errMsg, false)); } } @@ -162,7 +166,7 @@ static void ReturnCallbackResult(napi_env env, SymKeyGeneratorFwkCtx context, na { napi_value businessError = nullptr; if (context->errCode != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, context->errCode, context->errMsg); + businessError = GenerateBusinessError(env, context->errCode, context->errMsg, false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -180,7 +184,7 @@ static void AsyncGenKeyProcess(napi_env env, void *data) SymKeyGeneratorFwkCtx context = static_cast(data); HcfSymKeyGenerator *generator = context->generator; - HcfSymKey *key = NULL; + HcfSymKey *key = nullptr; HcfResult res = generator->generateSymKey(generator, &key); if (res != HCF_SUCCESS) { LOGE("generate sym key failed."); @@ -199,7 +203,7 @@ static void AsyncKeyReturn(napi_env env, napi_status status, void *data) SymKeyGeneratorFwkCtx context = static_cast(data); NapiSymKey *napiSymKey = new (std::nothrow) NapiSymKey(context->returnSymKey); if (napiSymKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi sym key failed.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi sym key failed.", false)); FreeSymKeyGeneratorFwkCtx(env, context); LOGE("new napi sym key failed."); return; @@ -232,7 +236,7 @@ static void AsyncConvertKeyProcess(napi_env env, void *data) SymKeyGeneratorFwkCtx context = static_cast(data); HcfSymKeyGenerator *generator = context->generator; - HcfSymKey *key = NULL; + HcfSymKey *key = nullptr; HcfResult res = generator->convertSymKey(generator, &context->keyMaterial, &key); if (res != HCF_SUCCESS) { LOGE("convertSymKey key failed!"); @@ -311,7 +315,7 @@ NapiSymKeyGenerator::~NapiSymKeyGenerator() HcfObjDestroy(this->generator_); } -HcfSymKeyGenerator *NapiSymKeyGenerator::GetSymKeyGenerator() +HcfSymKeyGenerator *NapiSymKeyGenerator::GetSymKeyGenerator() const { return this->generator_; } @@ -373,11 +377,11 @@ napi_value NapiSymKeyGenerator::CreateSymKeyGenerator(napi_env env, napi_callbac { size_t expectedArgc = ARGS_SIZE_ONE; size_t argc = ARGS_SIZE_ONE; - napi_value argv[ARGS_SIZE_ONE] = { 0 }; + napi_value argv[ARGS_SIZE_ONE] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (argc != expectedArgc) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid.", false)); LOGE("The input args num is invalid."); return nullptr; } @@ -388,15 +392,15 @@ napi_value NapiSymKeyGenerator::CreateSymKeyGenerator(napi_env env, napi_callbac NAPI_CALL(env, napi_new_instance(env, constructor, argc, argv, &instance)); std::string algoName; - if (!GetStringFromJSParams(env, argv[ARGS_SIZE_ZERO], algoName)) { + if (!GetStringFromJSParams(env, argv[ARGS_SIZE_ZERO], algoName, false)) { LOGE("failed to get algoName."); return nullptr; } - HcfSymKeyGenerator *generator = NULL; + HcfSymKeyGenerator *generator = nullptr; int32_t res = HcfSymKeyGeneratorCreate(algoName.c_str(), &generator); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create C generator fail.")); + napi_throw(env, GenerateBusinessError(env, res, "create C generator fail.", false)); LOGE("create C generator fail."); return nullptr; } @@ -428,7 +432,7 @@ napi_value NapiSymKeyGenerator::JsGetAlgorithm(napi_env env, napi_callback_info napi_value thisVar = nullptr; NapiSymKeyGenerator *napiSymKeyGenerator = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&napiSymKeyGenerator)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&napiSymKeyGenerator))); HcfSymKeyGenerator *generator = napiSymKeyGenerator->GetSymKeyGenerator(); const char *algo = generator->getAlgoName(generator); diff --git a/frameworks/js/napi/src/napi_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp similarity index 91% rename from frameworks/js/napi/src/napi_utils.cpp rename to frameworks/js/napi/crypto/src/napi_utils.cpp index e09802d..6799358 100644 --- a/frameworks/js/napi/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -103,14 +103,14 @@ static bool GetDataOfEncodingBlob(napi_env env, napi_value data, HcfEncodingBlob void *rawData = nullptr; napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length, - (void **)&rawData, &arrayBuffer, &offset); + reinterpret_cast(&rawData), &arrayBuffer, &offset); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed", true)); LOGE("failed to get array data!"); return false; } if (arrayType != napi_uint8_array) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array", true)); LOGE("array is not uint8 array!"); return false; } @@ -144,7 +144,7 @@ bool GetEncodingBlobFromValue(napi_env env, napi_value obj, HcfEncodingBlob **en napi_value data = nullptr; napi_status status = napi_get_named_property(env, obj, CRYPTO_TAG_DATA.c_str(), &data); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob data failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob data failed", true)); LOGE("failed to get encoding blob data!"); HcfFree(*encodingBlob); *encodingBlob = nullptr; @@ -158,7 +158,7 @@ bool GetEncodingBlobFromValue(napi_env env, napi_value obj, HcfEncodingBlob **en napi_value format = nullptr; status = napi_get_named_property(env, obj, CRYPTO_TAG_ENCODING_FORMAT.c_str(), &format); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob format failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get encoding blob format failed", true)); LOGE("failed to get encoding blob format!"); HcfFree((*encodingBlob)->data); (*encodingBlob)->data = nullptr; @@ -166,7 +166,7 @@ bool GetEncodingBlobFromValue(napi_env env, napi_value obj, HcfEncodingBlob **en *encodingBlob = nullptr; return false; } - napi_get_value_uint32(env, format, (uint32_t *)(&(*encodingBlob)->encodingFormat)); + napi_get_value_uint32(env, format, reinterpret_cast(&(*encodingBlob)->encodingFormat)); return true; } @@ -207,7 +207,8 @@ HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg) napi_value arrayBuffer = nullptr; napi_typedarray_type arrayType; // Warning: Do not release the rawData returned by this interface because the rawData is managed by VM. - status = napi_get_typedarray_info(env, data, &arrayType, &length, (void **)&rawData, &arrayBuffer, &offset); + status = napi_get_typedarray_info(env, data, &arrayType, &length, + reinterpret_cast(&rawData), &arrayBuffer, &offset); if ((status != napi_ok) || (length == 0) || (rawData == nullptr)) { LOGE("failed to get valid rawData."); return nullptr; @@ -218,7 +219,7 @@ HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg) } HcfBlob *newBlob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); - if (newBlob == NULL) { + if (newBlob == nullptr) { LOGE("Failed to allocate newBlob memory!"); return nullptr; } @@ -435,7 +436,7 @@ bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMo return false; } string algoName; - if (!GetStringFromJSParams(env, data, algoName)) { + if (!GetStringFromJSParams(env, data, algoName, false)) { LOGE("GetStringFromJSParams failed!"); return false; } @@ -496,14 +497,14 @@ static bool GetDataOfCertChain(napi_env env, napi_value data, HcfCertChainData * void *rawData = nullptr; napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length, - (void **)&rawData, &arrayBuffer, &offset); + reinterpret_cast(&rawData), &arrayBuffer, &offset); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get array data failed", true)); LOGE("failed to get array data!"); return false; } if (arrayType != napi_uint8_array) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "array type is not uint8 array", true)); LOGE("array is not uint8 array!"); return false; } @@ -537,7 +538,7 @@ bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **cert napi_value data = nullptr; napi_status status = napi_get_named_property(env, obj, CRYPTO_TAG_DATA.c_str(), &data); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain data failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain data failed", true)); LOGE("failed to get cert chain data!"); HcfFree(*certChainData); *certChainData = nullptr; @@ -552,7 +553,7 @@ bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **cert napi_value certCount = nullptr; status = napi_get_named_property(env, obj, CRYPTO_TAG_COUNT.c_str(), &certCount); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain count failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain count failed", true)); LOGE("failed to get cert count!"); HcfFree((*certChainData)->data); (*certChainData)->data = nullptr; @@ -560,12 +561,12 @@ bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **cert *certChainData = nullptr; return false; } - napi_get_value_uint32(env, certCount, (uint32_t *)(&(*certChainData)->count)); + napi_get_value_uint32(env, certCount, reinterpret_cast(&(*certChainData)->count)); napi_value format = nullptr; status = napi_get_named_property(env, obj, CRYPTO_TAG_ENCODING_FORMAT.c_str(), &format); if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain format failed")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get cert chain format failed", true)); LOGE("failed to get cert chain format!"); HcfFree((*certChainData)->data); (*certChainData)->data = nullptr; @@ -573,16 +574,16 @@ bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **cert *certChainData = nullptr; return false; } - napi_get_value_uint32(env, format, (uint32_t *)(&(*certChainData)->format)); + napi_get_value_uint32(env, format, reinterpret_cast(&(*certChainData)->format)); return true; } -bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr) +bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr, bool isCertFunc) { napi_valuetype valueType; napi_typeof(env, arg, &valueType); if (valueType != napi_string) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not string")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not string", isCertFunc)); LOGE("wrong argument type. expect string type. [Type]: %d", valueType); return false; } @@ -601,12 +602,12 @@ bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr) return true; } -bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt) +bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt, bool isCertFunc) { napi_valuetype valueType; napi_typeof(env, arg, &valueType); if (valueType != napi_number) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number", isCertFunc)); LOGE("wrong argument type. expect int type. [Type]: %d", valueType); return false; } @@ -618,12 +619,12 @@ bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt) return true; } -bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt) +bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt, bool isCertFunc) { napi_valuetype valueType; napi_typeof(env, arg, &valueType); if (valueType != napi_number) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not number", isCertFunc)); LOGE("wrong argument type. expect int type. [Type]: %d", valueType); return false; } @@ -635,12 +636,12 @@ bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt) return true; } -bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb) +bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb, bool isCertFunc) { napi_valuetype valueType = napi_undefined; napi_typeof(env, arg, &valueType); if (valueType != napi_function) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not function")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "param type is not function", isCertFunc)); LOGE("wrong argument type. expect callback type. [Type]: %d", valueType); return false; } @@ -662,6 +663,24 @@ static uint32_t GetJsErrValueByErrCode(int32_t errCode) return JS_ERR_RUNTIME_ERROR; case HCF_ERR_CRYPTO_OPERATION: return JS_ERR_CRYPTO_OPERATION; + default: + return JS_ERR_DEFAULT_ERR; + } +} + +static uint32_t GetCertErrValueByErrCode(int32_t errCode) +{ + switch (errCode) { + case HCF_INVALID_PARAMS: + return JS_ERR_CERT_INVALID_PARAMS; + case HCF_NOT_SUPPORT: + return JS_ERR_CERT_NOT_SUPPORT; + case HCF_ERR_MALLOC: + return JS_ERR_CERT_OUT_OF_MEMORY; + case HCF_ERR_COPY: + return JS_ERR_CERT_RUNTIME_ERROR; + case HCF_ERR_CRYPTO_OPERATION: + return JS_ERR_CERT_CRYPTO_OPERATION; case HCF_ERR_CERT_SIGNATURE_FAILURE: return JS_ERR_CERT_SIGNATURE_FAILURE; case HCF_ERR_CERT_NOT_YET_VALID: @@ -679,12 +698,16 @@ static uint32_t GetJsErrValueByErrCode(int32_t errCode) } } -napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg) +napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg, bool isCertFunc) { napi_value businessError = nullptr; napi_value code = nullptr; - napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code); + if (isCertFunc) { + napi_create_uint32(env, GetCertErrValueByErrCode(errCode), &code); + } else { + napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code); + } napi_value msg = nullptr; napi_create_string_utf8(env, errMsg, NAPI_AUTO_LENGTH, &msg); @@ -695,17 +718,17 @@ napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errM return businessError; } -bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync) +bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync, bool isCertFunc) { if (isSync) { if (argc != expectedCount) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", isCertFunc)); LOGE("invalid params count!"); return false; } } else { if ((argc != expectedCount) && (argc != (expectedCount - ARGS_SIZE_ONE))) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid params count", isCertFunc)); LOGE("invalid params count!"); return false; } diff --git a/frameworks/js/napi/src/napi_verify.cpp b/frameworks/js/napi/crypto/src/napi_verify.cpp similarity index 92% rename from frameworks/js/napi/src/napi_verify.cpp rename to frameworks/js/napi/crypto/src/napi_verify.cpp index 94dc577..05559fa 100644 --- a/frameworks/js/napi/src/napi_verify.cpp +++ b/frameworks/js/napi/crypto/src/napi_verify.cpp @@ -148,25 +148,25 @@ static bool BuildVerifyJsInitCtx(napi_env env, napi_callback_info info, VerifyIn napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgc) && (argc != expectedArgc - 1)) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - NapiVerify *napiVerify = NULL; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiVerify)); + NapiVerify *napiVerify = nullptr; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiVerify)); if (status != napi_ok) { LOGE("failed to unwrap napi verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } size_t index = 0; - NapiPubKey *napiPubKey = NULL; - status = napi_unwrap(env, argv[index], (void **)(&napiPubKey)); + NapiPubKey *napiPubKey = nullptr; + status = napi_unwrap(env, argv[index], reinterpret_cast(&napiPubKey)); if (status != napi_ok) { LOGE("failed to unwrap napi pubKey obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[PubKey]: param unwarp error.", false)); return false; } @@ -178,7 +178,7 @@ static bool BuildVerifyJsInitCtx(napi_env env, napi_callback_info info, VerifyIn napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -191,23 +191,23 @@ static bool BuildVerifyJsUpdateCtx(napi_env env, napi_callback_info info, Verify napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgc) && (argc != expectedArgc - 1)) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; - NapiVerify *napiVerify = NULL; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiVerify)); + NapiVerify *napiVerify = nullptr; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiVerify)); if (status != napi_ok) { LOGE("failed to unwrap napi verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } size_t index = 0; HcfBlob *blob = GetBlobFromNapiValue(env, argv[index]); if (blob == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type.", false)); return false; } @@ -218,7 +218,7 @@ static bool BuildVerifyJsUpdateCtx(napi_env env, napi_callback_info info, Verify napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -232,7 +232,8 @@ static bool GetDataBlobAndSignatureFromInput(napi_env env, napi_value dataValue, data = GetBlobFromNapiValue(env, dataValue); if (data == nullptr) { LOGE("failed to get data."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Data]: must be of the DataBlob type.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, + "[Data]: must be of the DataBlob type.", false)); return false; } } @@ -241,7 +242,7 @@ static bool GetDataBlobAndSignatureFromInput(napi_env env, napi_value dataValue, if (signatureData == nullptr) { LOGE("failed to get signature."); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, - "[SignatureData]: must be of the DataBlob type.")); + "[SignatureData]: must be of the DataBlob type.", false)); HcfBlobDataFree(data); HcfFree(data); return false; @@ -261,16 +262,16 @@ static bool BuildVerifyJsDoFinalCtx(napi_env env, napi_callback_info info, Verif napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if ((argc != expectedArgc) && (argc != expectedArgc - 1)) { LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "params num error.", false)); return false; } ctx->asyncType = (argc == expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE; NapiVerify *napiVerify = nullptr; - napi_status status = napi_unwrap(env, thisVar, (void **)(&napiVerify)); + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiVerify)); if (status != napi_ok) { LOGE("failed to unwrap napi verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.")); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "[Self]: param unwarp error.", false)); return false; } @@ -289,7 +290,7 @@ static bool BuildVerifyJsDoFinalCtx(napi_env env, napi_callback_info info, Verif napi_create_promise(env, &ctx->deferred, &ctx->promise); return true; } else { - return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback); + return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback, false); } } @@ -297,7 +298,7 @@ static void ReturnInitCallbackResult(napi_env env, VerifyInitCtx *ctx, napi_valu { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_ONE] = { businessError }; @@ -316,7 +317,8 @@ static void ReturnInitPromiseResult(napi_env env, VerifyInitCtx *ctx, napi_value if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, + GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false)); } } @@ -324,7 +326,7 @@ static void ReturnUpdateCallbackResult(napi_env env, VerifyUpdateCtx *ctx, napi_ { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_ONE] = { businessError }; @@ -343,7 +345,8 @@ static void ReturnUpdatePromiseResult(napi_env env, VerifyUpdateCtx *ctx, napi_v if (ctx->result == HCF_SUCCESS) { napi_resolve_deferred(env, ctx->deferred, result); } else { - napi_reject_deferred(env, ctx->deferred, GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str())); + napi_reject_deferred(env, ctx->deferred, + GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false)); } } @@ -351,7 +354,7 @@ static void ReturnDoFinalCallbackResult(napi_env env, VerifyDoFinalCtx *ctx, nap { napi_value businessError = nullptr; if (ctx->result != HCF_SUCCESS) { - businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str()); + businessError = GenerateBusinessError(env, ctx->result, COMMON_ERR_MSG.c_str(), false); } napi_value params[ARGS_SIZE_TWO] = { businessError, result }; @@ -371,7 +374,7 @@ static void ReturnDoFinalPromiseResult(napi_env env, VerifyDoFinalCtx *ctx, napi napi_resolve_deferred(env, ctx->deferred, result); } else { napi_reject_deferred(env, ctx->deferred, - GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, COMMON_ERR_MSG.c_str())); + GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, COMMON_ERR_MSG.c_str(), false)); } } @@ -556,9 +559,9 @@ napi_value NapiVerify::JsInit(napi_env env, napi_callback_info info) { LOGI("enter ..."); VerifyInitCtx *ctx = static_cast(HcfMalloc(sizeof(VerifyInitCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildVerifyJsInitCtx(env, info, ctx)) { @@ -574,9 +577,9 @@ napi_value NapiVerify::JsUpdate(napi_env env, napi_callback_info info) { LOGI("enter ..."); VerifyUpdateCtx *ctx = static_cast(HcfMalloc(sizeof(VerifyUpdateCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildVerifyJsUpdateCtx(env, info, ctx)) { @@ -592,9 +595,9 @@ napi_value NapiVerify::JsVerify(napi_env env, napi_callback_info info) { LOGI("enter ..."); VerifyDoFinalCtx *ctx = static_cast(HcfMalloc(sizeof(VerifyDoFinalCtx), 0)); - if (ctx == NULL) { + if (ctx == nullptr) { LOGE("create context fail."); - return NULL; + return nullptr; } if (!BuildVerifyJsDoFinalCtx(env, info, ctx)) { @@ -636,12 +639,12 @@ napi_value NapiVerify::CreateJsVerify(napi_env env, napi_callback_info info) napi_new_instance(env, constructor, argc, argv, &instance); std::string algName; - if (!GetStringFromJSParams(env, argv[0], algName)) { + if (!GetStringFromJSParams(env, argv[0], algName, false)) { LOGE("failed to get algoName."); return nullptr; } - HcfVerify *verify = NULL; + HcfVerify *verify = nullptr; int32_t res = HcfVerifyCreate(algName.c_str(), &verify); if (res != HCF_SUCCESS) { LOGE("create c verify fail."); diff --git a/frameworks/rand/rand.c b/frameworks/rand/rand.c index 4681bfa..aac2781 100644 --- a/frameworks/rand/rand.c +++ b/frameworks/rand/rand.c @@ -101,9 +101,9 @@ static void HcfRandDestroy(HcfObjectBase *self) HcfFree(impl); } -HcfResult HcfRandCreate(HcfRand **randApi) +HcfResult HcfRandCreate(HcfRand **random) { - if (randApi == NULL) { + if (random == NULL) { LOGE("Invalid input params while creating rand!"); return HCF_INVALID_PARAMS; } @@ -129,6 +129,6 @@ HcfResult HcfRandCreate(HcfRand **randApi) returnRandApi->base.generateRandom = GenerateRandom; returnRandApi->base.setSeed = SetSeed; returnRandApi->spiObj = spiObj; - *randApi = (HcfRand *)returnRandApi; + *random = (HcfRand *)returnRandApi; return HCF_SUCCESS; } \ No newline at end of file diff --git a/interfaces/kits/js/@ohos.security.cert.d.ts b/interfaces/kits/js/@ohos.security.cert.d.ts new file mode 100644 index 0000000..ffca88f --- /dev/null +++ b/interfaces/kits/js/@ohos.security.cert.d.ts @@ -0,0 +1,731 @@ +/* + * 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. + */ + +import {AsyncCallback, Callback} from './basic'; +import cryptoFramework from '@ohos.security.cryptoFramework' + +/** + * Provides a set of cert operation, shields the underlying differences, + * encapsulates the relevant algorithm library, and provides a unified functional interface upward. + * @namespace cert + * @syscap SystemCapability.Security.Cert + * @since 9 + */ +declare namespace cert { + /** + * Enum for result code + * @enum {number} + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + enum CertResult { + /** Indicates that input parameters is invalid. + * @since 9 + */ + INVALID_PARAMS = 401, + + /** Indicates that function or algorithm is not supported. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + NOT_SUPPORT = 801, + + /** Indicates the memory error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_OUT_OF_MEMORY = 19020001, + + /** Indicates that runtime error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_RUNTIME_ERROR = 19020002, + + /** Indicates the crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_CRYPTO_OPERATION = 19030001, + + /* Indicates that the certificate signature verification failed. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_CERT_SIGNATURE_FAILURE = 19030002, + + /* Indicates that the certificate has not taken effect. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_CERT_NOT_YET_VALID = 19030003, + + /* Indicates that the certificate has expired. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_CERT_HAS_EXPIRED = 19030004, + + /* Indicates that we failed to obtain the certificate issuer.. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 19030005, + + /* The key cannot be used for signing a certificate. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_KEYUSAGE_NO_CERTSIGN = 19030006, + + /* The key cannot be used for digital signature. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE = 19030007, + } + + /** + * Provides the data blob type. + * @typedef DataBlob + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface DataBlob { + data : Uint8Array; + } + + /** + * Provides the data array type. + * @typedef DataArray + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface DataArray { + data : Array; + } + + /** + * Enum for supported cert encoding format. + * @enum {number} + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + enum EncodingFormat { + /** + * The value of cert DER format. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + FORMAT_DER = 0, + + /** + * The value of cert PEM format. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + FORMAT_PEM = 1, + } + + /** + * Provides the cert encoding blob type. + * @typedef EncodingBlob + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface EncodingBlob { + /** + * The data input. + * @type { Uint8Array } + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + data : Uint8Array; + /** + * The data encoding format. + * @type { EncodingFormat } + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + encodingFormat : EncodingFormat; + } + + /** + * Provides the cert chain data type. + * @typedef CertChainData + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface CertChainData { + /** + * The data input. + * @type { Uint8Array } + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + data: Uint8Array; + /** + * The number of certs. + * @type { number } + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + count : number; + /** + * The data encoding format. + * @type { EncodingFormat } + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + encodingFormat: EncodingFormat; + } + + /** + * Provides the x509 cert type. + * @typedef X509Cert + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface X509Cert { + /** + * Verify the X509 cert. + * @param key Indicates the cert chain validator data. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + verify(key : cryptoFramework.PubKey, callback : AsyncCallback) : void; + verify(key : cryptoFramework.PubKey) : Promise; + + /** + * Get X509 cert encoded data. + * @return Returns X509 cert encoded data. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getEncoded(callback : AsyncCallback) : void; + getEncoded() : Promise; + + /** + * Get X509 cert public key. + * @return Returns X509 cert pubKey. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getPublicKey(callback : AsyncCallback) : void; + getPublicKey() : Promise; + + /** + * Check the X509 cert validity with date. + * @param date Indicates the cert date. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @throws { BusinessError } 19030003 - the certificate has not taken effect. + * @throws { BusinessError } 19030004 - the certificate has expired. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + checkValidityWithDate(date: string, callback : AsyncCallback) : void; + checkValidityWithDate(date: string) : Promise; + + /** + * Get X509 cert version. + * @return Returns X509 cert version. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getVersion() : number; + + /** + * Get X509 cert serial number. + * + * @return Returns X509 cert serial number. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSerialNumber() : number; + + /** + * Get X509 cert issuer name. + * @return Returns X509 cert issuer name. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getIssuerName() : DataBlob; + + /** + * Get X509 cert subject name. + * @return Returns X509 cert subject name. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSubjectName() : DataBlob; + + /** + * Get X509 cert not before time. + * @return Returns X509 cert not before time. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getNotBeforeTime() : string; + + /** + * Get X509 cert not after time. + * @return Returns X509 cert not after time. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getNotAfterTime() : string; + + /** + * Get X509 cert signature. + * @return Returns X509 cert signature. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignature() : DataBlob; + + /** + * Get X509 cert signature's algorithm name. + * @return Returns X509 cert signature's algorithm name. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignatureAlgName() : string; + + /** + * Get X509 cert signature's algorithm oid. + * @return Returns X509 cert signature's algorithm oid. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignatureAlgOid() : string; + + /** + * Get X509 cert signature's algorithm name. + * @return Returns X509 cert signature's algorithm name. + * @throws { BusinessError } 801 - this operation is not supported. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignatureAlgParams() : DataBlob; + + /** + * Get X509 cert key usage. + * @return Returns X509 cert key usage. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getKeyUsage() : DataBlob; + + /** + * Get X509 cert extended key usage. + * @return Returns X509 cert extended key usage. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getExtKeyUsage() : DataArray; + + /** + * Get X509 cert basic constraints path len. + * @return Returns X509 cert basic constraints path len. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getBasicConstraints() : number; + + /** + * Get X509 cert subject alternative name. + * @return Returns X509 cert subject alternative name. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSubjectAltNames() : DataArray; + + /** + * Get X509 cert issuer alternative name. + * @return Returns X509 cert issuer alternative name. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getIssuerAltNames() : DataArray; + } + + /** + * Provides the x509 cert func. + * @param inStream Indicates the input cert data. + * @return Returns X509 cert instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 801 - this operation is not supported. + * @throws { BusinessError } 19020001 - memory error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + function createX509Cert(inStream : EncodingBlob, callback : AsyncCallback) : void; + function createX509Cert(inStream : EncodingBlob) : Promise; + + /** + * Interface of X509CrlEntry. + * @typedef X509CrlEntry + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface X509CrlEntry { + /** + * Returns the ASN of this CRL entry 1 der coding form, i.e. internal sequence. + * @return Returns EncodingBlob of crl entry. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getEncoded(callback : AsyncCallback) : void; + getEncoded() : Promise; + + /** + * Get the serial number from this x509crl entry. + * @return Returns serial number of crl entry. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSerialNumber() : number; + + /** + * Get the issuer of the x509 certificate described by this entry. + * @return Returns DataBlob of issuer. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 801 - this operation is not supported. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getCertIssuer(callback : AsyncCallback) : void; + getCertIssuer() : Promise; + + /** + * Get the revocation date from x509crl entry. + * @return Returns string of revocation date. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getRevocationDate(callback : AsyncCallback) : void; + getRevocationDate() : Promise; + } + + /** + * Interface of X509Crl. + * @typedef X509Crl + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface X509Crl { + /** + * Check if the given certificate is on this CRL. + * @param cert Input cert data. + * @return Returns result of Check cert is revoked or not. + * @throws { BusinessError } 401 - invalid parameters. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + isRevoked(cert : X509Cert, callback : AsyncCallback) : void; + isRevoked(cert : X509Cert) : Promise; + + /** + * Returns the type of this CRL. + * @return Returns string of crl type. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getType() : string; + + /** + * Get the der coding format. + * @return Returns EncodingBlob of crl. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getEncoded(callback : AsyncCallback) : void; + getEncoded() : Promise; + + /** + * Use the public key to verify the signature of CRL. + * @param key Input public Key. + * @return Returns verify result. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + verify(key : cryptoFramework.PubKey, callback : AsyncCallback) : void; + verify(key : cryptoFramework.PubKey) : Promise; + + /** + * Get version number from CRL. + * @return Returns version of crl. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getVersion() : number; + + /** + * Get the issuer name from CRL. Issuer means the entity that signs and publishes the CRL. + * @return Returns issuer name of crl. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getIssuerName() : DataBlob; + + /** + * Get lastUpdate value from CRL. + * @return Returns last update of crl. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getLastUpdate() : string; + + /** + * Get nextUpdate value from CRL. + * @return Returns next update of crl. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getNextUpdate() : string; + + /** + * This method can be used to find CRL entries in indirect CRLs. + * @param serialNumber serial number of crl. + * @return Returns next update of crl. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getRevokedCert(serialNumber : number, callback : AsyncCallback) : void; + getRevokedCert(serialNumber : number) : Promise; + + /** + * This method can be used to find CRL entries in indirect cert. + * @param cert Cert of x509. + * @return Returns X509CrlEntry instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getRevokedCertWithCert(cert : X509Cert, callback : AsyncCallback) : void; + getRevokedCertWithCert(cert : X509Cert) : Promise; + + /** + * Get all entries in this CRL. + * @return Returns Array of X509CrlEntry instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getRevokedCerts(callback : AsyncCallback>) : void; + getRevokedCerts() : Promise>; + + /** + * Get the CRL information encoded by Der from this CRL. + * @return Returns DataBlob of tbs info. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getTbsInfo(callback : AsyncCallback) : void; + getTbsInfo() : Promise; + + /** + * Get signature value from CRL. + * @return Returns DataBlob of signature. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignature() : DataBlob; + + /** + * Get the signature algorithm name of the CRL signature algorithm. + * @return Returns string of signature algorithm name. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignatureAlgName() : string; + + /** + * Get the signature algorithm oid string from CRL. + * @return Returns string of signature algorithm oid. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignatureAlgOid() : string; + + /** + * Get the der encoded signature algorithm parameters from the CRL signature algorithm. + * @return Returns DataBlob of signature algorithm params. + * @throws { BusinessError } 801 - this operation is not supported. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + getSignatureAlgParams() : DataBlob; + } + + /** + * Provides the x509 CRL func. + * @param inStream Indicates the input CRL data. + * @return Returns the x509 CRL instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 801 - this operation is not supported. + * @throws { BusinessError } 19020001 - memory error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + function createX509Crl(inStream : EncodingBlob, callback : AsyncCallback) : void; + function createX509Crl(inStream : EncodingBlob) : Promise; + + /** + * Certification chain validator. + * @typedef CertChainValidator + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + interface CertChainValidator { + /** + * Validate the cert chain. + * @param certChain Indicates the cert chain validator data. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @throws { BusinessError } 19030002 - the certificate signature verification failed. + * @throws { BusinessError } 19030003 - the certificate has not taken effect. + * @throws { BusinessError } 19030004 - the certificate has expired. + * @throws { BusinessError } 19030005 - failed to obtain the certificate issuer. + * @throws { BusinessError } 19030006 - the key cannot be used for signing a certificate. + * @throws { BusinessError } 19030007 - the key cannot be used for digital signature. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + validate(certChain : CertChainData, callback : AsyncCallback) : void; + validate(certChain : CertChainData) : Promise; + + /** + * The cert chain related algorithm. + * @type { string } + * @readonly + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + readonly algorithm : string; + } + + /** + * Provides the cert chain validator func. + * @param algorithm Indicates the cert chain validator type. + * @return Returns the cert chain validator instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 801 - this operation is not supported. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19020002 - runtime error. + * @throws { BusinessError } 19030001 - crypto operation error. + * @syscap SystemCapability.Security.Cert + * @since 9 + */ + function createCertChainValidator(algorithm :string) : CertChainValidator; +} + +export default cert; diff --git a/interfaces/kits/js/@ohos.security.cryptoFramework.d.ts b/interfaces/kits/js/@ohos.security.cryptoFramework.d.ts index 69aab90..806a5f9 100644 --- a/interfaces/kits/js/@ohos.security.cryptoFramework.d.ts +++ b/interfaces/kits/js/@ohos.security.cryptoFramework.d.ts @@ -13,24 +13,24 @@ * limitations under the License. */ - import {AsyncCallback, Callback} from './basic'; /** * Provides a set of encryption and decryption algorithm library framework, shields the underlying differences, * encapsulates the relevant algorithm library, and provides a unified functional interface upward. - * + * @namespace cryptoFramework * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ declare namespace cryptoFramework { /** - * Enum for result code + * Enum for result code. + * @enum {number} + * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ enum Result { - /** Indicates that input params is invalid. + /** Indicates that input parameters is invalid. * @since 9 */ INVALID_PARAMS = 401, @@ -40,7 +40,7 @@ declare namespace cryptoFramework { */ NOT_SUPPORT = 801, - /** Indicates the out of memory error. + /** Indicates the memory error. * @since 9 */ ERR_OUT_OF_MEMORY = 17620001, @@ -50,145 +50,112 @@ declare namespace cryptoFramework { */ ERR_RUNTIME_ERROR = 17620002, - /** Indicates that crypto operation has something wrong. + /** Indicates that crypto operation error. * @since 9 */ ERR_CRYPTO_OPERATION = 17630001, - - /* Indicates that cert signature check fails. - * @since 9 - */ - ERR_CERT_SIGNATURE_FAILURE = 17630002, - - /* Indicates that cert is not yet valid. - * @since 9 - */ - ERR_CERT_NOT_YET_VALID = 17630003, - - /* Indicates that cert has expired. - * @since 9 - */ - ERR_CERT_HAS_EXPIRED = 17630004, - - /* Indicates that we can not get the untrusted cert's issuer. - * @since 9 - */ - ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 17630005, - - /* Key usage does not include certificate sign. - * @since 9 - */ - ERR_KEYUSAGE_NO_CERTSIGN = 17630006, - - /* Key usage does not include digital sign. - * @since 9 - */ - ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE = 17630007, } + /** + * Provides the data blob type. + * @typedef DataBlob + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface DataBlob { data : Uint8Array; } - interface DataArray { - data : Array; - } - /** - * Enum for supported cert encoding format + * Provides the ParamsSpec type, including the algorithm name. + * @typedef ParamsSpec + * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ - enum EncodingFormat { - /** - * The value of cert DER format - * @since 9 - */ - FORMAT_DER = 0, - - /** - * The value of cert PEM format - * @since 9 - */ - FORMAT_PEM = 1, - } - - interface EncodingBlob { - data : Uint8Array; - encodingFormat : EncodingFormat; - } - - interface CertChainData { - data: Uint8Array; - count : number; - encodingFormat: EncodingFormat; - } - interface ParamsSpec { /** - * Indicates the algorithm name. - * + * Indicates the algorithm name. Should be set before initialization of a cipher object. + * @type { string } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ algoName : string; } + /** + * Provides the IvParamsSpec type, including the parameter iv. + * @typedef IvParamsSpec + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface IvParamsSpec extends ParamsSpec { /** * Indicates the algorithm parameters such as iv. - * + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ iv : DataBlob; } + /** + * Provides the GcmParamsSpec type, including the parameter iv, aad and authTag. + * @typedef GcmParamsSpec + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface GcmParamsSpec extends ParamsSpec { /** * Indicates the GCM algorithm parameters such as iv. - * + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ iv : DataBlob; /** - * Indicates the GCM additional message for integrity check. - * + * Indicates the Additional Authenticated Data in GCM mode. + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ aad : DataBlob; /** - * Indicates the GCM Authenticated Data. - * + * Indicates the output tag from the encryption operation. The tag is used for integrity check. + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ authTag : DataBlob; } + /** + * Provides the CcmParamsSpec type, including the parameter iv, aad and authTag. + * @typedef CcmParamsSpec + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface CcmParamsSpec extends ParamsSpec { /** - * Indicates the GCM algorithm parameters such as iv. - * + * Indicates the GCM algorithm parameters such as IV. + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ iv : DataBlob; /** - * Indicates the CCM additional message for integrity check. - * + * Indicates the Additional Authenticated Data in CCM mode. + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ aad : DataBlob; /** - * Indicates the CCM Authenticated Data. - * + * Indicates the output tag from the encryption operation. The tag is used for integrity check. + * @type { DataBlob } * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ @@ -197,136 +164,153 @@ declare namespace cryptoFramework { /** * Enum for obtain the crypto operation. + * @enum { number } + * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ enum CryptoMode { /** - * The value of aes and 3des encrypt operation + * The value of encryption operation for AES, 3DES and RSA. + * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ ENCRYPT_MODE = 0, /** - * The value of aes and 3des decrypt operation + * The value of decryption operation for AES, 3DES and RSA. + * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ DECRYPT_MODE = 1, } /** - * The common parents class of key. - * + * Provides the Key type, which is the common parent class of keys. + * @typedef Key * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ interface Key { /** - * Encode key Object to bin. - * + * Encode the key object to binary data. + * @returns { DataBlob } the binary data of the key object. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ getEncoded() : DataBlob; /** - * Key format. - * + * Indicates the format of the key object. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ readonly format : string; /** - * Key algorithm name. - * + * Indicates the algorithm name of the key object. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ readonly algName : string; } + /** + * Provides the SymKey type, which is used for symmetric cryptography. + * @typedef SymKey + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface SymKey extends Key { + /** + * Reset the key data to zero in the memory. + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ clearMem() : void; } /** - * The private key class of asy-key. - * + * Provides the private key type. + * @typedef PriKey * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ interface PriKey extends Key { /** - * The function used to clear private key mem. - * + * Clear memory of private key. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ clearMem() : void; } /** - * The public key class of asy-key. - * + * Provides the public key type. + * @typedef PubKey * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ interface PubKey extends Key {} /** - * The keyPair class of asy-key. Include privateKey and publickey. - * + * Provides the asymetric keyPair type. + * @typedef KeyPair * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ interface KeyPair { /** - * Public key. - * + * KeyPair's private key. + * @type { PriKey } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ readonly priKey : PriKey; /** - * Private key. - * + * KeyPair's public key. + * @type { PubKey } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ readonly pubKey : PubKey; } + /** + * Provides the random interface. + * @typedef Random + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface Random { - /** - * Generate radom DataBlob by given length - * + * Generate radom DataBlob by given length. + * @param len Indicates the length of random DataBlob. + * @return Returns the generated random blob. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param len Indicates the length of random DataBlob */ generateRandom(len : number, callback: AsyncCallback) : void; generateRandom(len : number) : Promise; /** - * set seed by given DataBlob - * + * Set seed by given DataBlob. + * @param seed Indicates the seed DataBlob. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param seed Indicates the seed DataBlob */ setSeed(seed : DataBlob, callback : AsyncCallback) : void; setSeed(seed : DataBlob) : Promise; @@ -334,111 +318,178 @@ declare namespace cryptoFramework { /** * Provides the rand create func. - * + * @return Returns the created rand instance. + * @throws { BusinessError } 19020001 - memory error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @return Returns the rand create instance. */ function createRandom() : Random; /** - * The generator used to generate asy_key. - * + * The AsyKeyGenerator provides the ability to generate or convert keyPair. + * @typedef AsyKeyGenerator * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 */ - interface AsyKeyGenerator { - + interface AsyKeyGenerator { /** - * Generate keyPair by init params. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to generate asymetric key pair. + * @param { AsyncCallback } callback - the callback used to return keypair. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return The generated keyPair. */ generateKeyPair(callback : AsyncCallback) : void; + + /** + * Used to generate asymetric key pair. + * @returns { Promise } - the promise used to return keypair. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework. + * @since 9 + */ generateKeyPair() : Promise; /** - * Convert keyPair object from privateKey and publicKey binary data. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to convert asymetric key pair . + * @param { DataBlob } pubKey - the public key data blob. + * @param { DataBlob } priKey - the private key data blob. + * @param { AsyncCallback } callback - the callback used to return keypair. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @param pubKey The binary data of public key. - * @param priKey The binary data of private key. - * @return The Converted key pair. */ convertKey(pubKey : DataBlob, priKey : DataBlob, callback : AsyncCallback) : void; + + /** + * Used to convert asymetric key pair. + * @param { DataBlob } pubKey - the public key data blob. + * @param { DataBlob } priKey - the private key data blob. + * @returns { promise } - the promise used to return keypair. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework. + * @since 9 + */ convertKey(pubKey : DataBlob, priKey : DataBlob) : Promise; /** - * The algorothm name of generator. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * The algName of the AsyKeyGenerator. + * @type { string } + * @syscap SystemCapability.Security.CryptoFramework. + * @readonly * @since 9 */ readonly algName : string; } + /** + * Provides the SymKeyGenerator type, which is used for generating symmetric key. + * @typedef SymKeyGenerator + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface SymKeyGenerator { + /** + * Generate a symmetric key object randomly. + * @param { AsyncCallback } callback - the callback of generateSymKey. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ generateSymKey(callback : AsyncCallback) : void; + + /** + * Generate a symmetric key object randomly. + * @returns { Promise } the promise returned by the function. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ generateSymKey() : Promise; + + /** + * Generate a symmetric key object according to the provided binary key data. + * @param { AsyncCallback } callback - the callback of generateSymKey. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ convertKey(key : DataBlob, callback : AsyncCallback) : void; + + /** + * Generate a symmetric key object according to the provided binary key data. + * @returns { Promise } the promise returned by the function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ convertKey(key : DataBlob) : Promise; + + /** + * Indicates the algorithm name of the SymKeyGenerator object. + * @type { string } + * @readonly + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ readonly algName : string; } /** * Provides the asy key generator instance func. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * @param { string } algName Indicates the algorithm name. + * @returns {AsyKeyGenerator} the generator obj create by algName. + * @throws { BusinessError } 401 - invalid parameters. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @param algName This algName contains params of generateKeyPair, like bits, primes or ECC_curve; - * @return The generator object. */ - function createAsyKeyGenerator(algName : string) : AsyKeyGenerator; + function createAsyKeyGenerator(algName : string) : AsyKeyGenerator; /** - * Provides the sym key generator instance func. - * + * Create a symmetric key generator according to the given algorithm name. + * @param { string } algName - indicates the algorithm name. + * @returns { SymKeyGenerator } the symmetric key generator instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 801 - this operation is not supported. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param algName Indicates the algorithm name. - * @return Returns the sym key generator instance. */ function createSymKeyGenerator(algName : string) : SymKeyGenerator; interface Mac { - /** - * Init hmac with given SymKey - * + /** + * Init hmac with given SymKey. + * @param key Indicates the SymKey. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param key Indicates the SymKey */ init(key : SymKey, callback : AsyncCallback) : void; init(key : SymKey) : Promise; /** - * Update hmac with DataBlob - * + * Update hmac with DataBlob. + * @param input Indicates the DataBlob. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param input Indicates the DataBlob */ update(input : DataBlob, callback : AsyncCallback) : void; update(input : DataBlob) : Promise; /** - * Output the result of hmac calculation - * + * Output the result of hmac calculation. + * @return Returns the calculated hmac result. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ @@ -446,16 +497,18 @@ declare namespace cryptoFramework { doFinal() : Promise; /** - * Output the length of hmac result - * + * Output the length of hmac result. + * @return Returns the length of the hmac result. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ getMacLength() : number; /** - * Indicates the algorithm name - * + * Indicates the algorithm name. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ @@ -464,29 +517,32 @@ declare namespace cryptoFramework { /** * Provides the mac create func. - * + * @param algName Indicates the mac algorithm name. + * @return Returns the created mac instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param algName Indicates the mac algorithm name. - * @return Returns the mac create instance. */ function createMac(algName : string) : Mac; interface Md { /** - * Update md with DataBlob - * + * Update md with DataBlob. + * @param input Indicates the DataBlob. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param input Indicates the DataBlob */ update(input : DataBlob, callback : AsyncCallback) : void; update(input : DataBlob) : Promise; /** - * Output the result of md calculation - * + * Output the result of md calculation. + * @return Returns the calculated hmac result. + * @throws { BusinessError } 19020001 - memory error. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ @@ -494,16 +550,18 @@ declare namespace cryptoFramework { digest() : Promise; /** - * Output the length of md result - * + * Output the length of md result. + * @return Returns the length of the hmac result. + * @throws { BusinessError } 19030001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ getMdLength() : number; /** - * Indicates the algorithm name - * + * Indicates the algorithm name. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework * @since 9 */ @@ -512,684 +570,397 @@ declare namespace cryptoFramework { /** * Provides the md create func. - * + * @param algName Indicates the md algorithm name. + * @return Returns the created md instance. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 19020001 - memory error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param algorithm Indicates the md algorithm. - * @return Returns the md create instances. */ function createMd(algName : string) : Md; + /** + * Provides the Cipher type, which is used for encryption and decryption operations. + * @typedef Cipher + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ interface Cipher { /** - * Init cipher with given cipher mode, key and params. - * + * Init the crypto operation with the given crypto mode, key and parameters. + * @param { CryptoMode } opMode - indicates the crypto mode is encryption or decryption. + * @param { Key } key - indicates the symmetric key or the asymmetric key. + * @param { ParamsSpec } params - indicates the algorithm parameters such as IV. + * @param { AsyncCallback } callback - the callback of the init function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param opMode Indicates the cipher mode. - * @param key Indicates the SymKey or AsyKey. - * @param params Indicates the algorithm parameters such as IV. */ init(opMode : CryptoMode, key : Key, params : ParamsSpec, callback : AsyncCallback) : void; - init(opMode : CryptoMode, key : Key, params : ParamsSpec) : Promise; /** - * Update cipher with DataBlob. - * + * Init the crypto operation with the given crypto mode, key and parameters. + * @param { CryptoMode } opMode - indicates the crypto mode is encryption or decryption. + * @param { Key } key - indicates the symmetric key or the asymmetric key. + * @param { ParamsSpec } params - indicates the algorithm parameters such as IV. + * @returns { Promise } the promise returned by the function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework * @since 9 - * @param input Indicates the DataBlob */ - update(data : DataBlob, callback : AsyncCallback) : void; - update(data : DataBlob) : Promise; - - /** - * Output the result of cipher calculation. - * - * @syscap SystemCapability.Security.CryptoFramework - * @since 9 - */ - doFinal(data : DataBlob, callback : AsyncCallback) : void; - doFinal(data : DataBlob) : Promise; - - /** - * Indicates the algorithm name. - * - * @syscap SystemCapability.Security.CryptoFramework - * @since 9 - */ - readonly algName : string; - } - - /** - * Provides the cipher create func. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param transformation Indicates the transform type, and contains init params of cipher. - * @return Returns the cipher create instance. - */ - function createCipher(transformation : string) : Cipher; - - /** - * The sign class - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - */ - interface Sign { - /** - * This init function used to Initialize environment, must be invoked before update and sign. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param priKey The prikey object. - */ - init(priKey : PriKey, callback : AsyncCallback) : void; - init(priKey : PriKey) : Promise; + init(opMode : CryptoMode, key : Key, params : ParamsSpec) : Promise; /** - * This function used to update data. - * + * Update the crypto operation with the input data, and feed back the encrypted or decrypted data + * this time. RSA is not supported in this function. + * @param { DataBlob } data - indicates the data to be encrypted or decrypted. + * @param { AsyncCallback } callback - the callback of the update function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param data The data need to update. */ - update(data : DataBlob, callback : AsyncCallback) : void; - update(data : DataBlob) : Promise; + update(data : DataBlob, callback : AsyncCallback) : void; /** - * This function used to sign all data. - * + * Update the crypto operation with the input data, and feed back the encrypted or decrypted data + * this time. RSA is not supported in this function. + * @param { DataBlob } data - indicates the data to be encrypted or decrypted. + * @returns { Promise } the promise returned by the function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param data The data need to update. - * @return The sign data. */ - sign(data : DataBlob, callback : AsyncCallback) : void; - sign(data : DataBlob) : Promise; - readonly algName : string; - } + update(data : DataBlob) : Promise; - /** - * The verify class - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - */ - interface Verify { /** - * This init function used to Initialize environment, must be invoked before update and verify. - * + * Finish the crypto operation, encrypt or decrypt the input data, and then feed back the output data. + * Data cannot be updated after the crypto operation is finished. + * @param { DataBlob } data - indicates the data to be finally encrypted or decrypted. + * @param { AsyncCallback } callback - the callback of the doFinal function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param priKey The prikey object. */ - init(pubKey : PubKey, callback : AsyncCallback) : void; - init(pubKey : PubKey) : Promise; + doFinal(data : DataBlob, callback : AsyncCallback) : void; /** - * This function used to update data. - * + * Finish the crypto operation, encrypt or decrypt the input data, and then feed back the output data. + * Data cannot be updated after the crypto operation is finished. + * @param { DataBlob } data - indicates the data to be finally encrypted or decrypted. + * @returns { Promise } the promise returned by the function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param data The data need to update. */ - update(data : DataBlob, callback : AsyncCallback) : void; - update(data : DataBlob) : Promise; + doFinal(data : DataBlob) : Promise; /** - * This function used to sign all data. - * + * Indicates the algorithm name of the Cipher object. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param data The data need to update. - * @param signatureData The sign data. - * @return true means verify success. */ - verify(data : DataBlob, signatureData : DataBlob, callback : AsyncCallback) : void; - verify(data : DataBlob, signatureData : DataBlob) : Promise; readonly algName : string; } /** - * Provides the sign func. - * + * Create a cipher object for encryption and decryption operations according to the given specifications. + * Two different Cipher objects should be created when using RSA encryption and decryption, + * even with the same specifications. + * @param { string } transformation - Indicates the description to be transformed to cipher specifications. + * @returns { Cipher } the cipher object returned by the function. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 801 - this operation is not supported. * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param algName Indicates the sign algorithm name, include init detail params. */ - function createSign(algName : string) : Sign; - - /** - * Provides the verify func. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param algName Indicates the verify algorithm name, include init detail params. - */ - function createVerify(algName : string) : Verify; - - interface KeyAgreement { - /** - * Generate secret by init params. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return The generated secret. - */ - generateSecret(priKey : PriKey, pubKey : PubKey, callback : AsyncCallback) : void; - generateSecret(priKey : PriKey, pubKey : PubKey) : Promise; - - /** - * Indicates the algorithm name - * - * @syscap SystemCapability.Security.CryptoFramework - * @since 9 - */ - readonly algName : string; - } + function createCipher(transformation : string) : Cipher; /** - * Provides the key agree func. - * + * Provides sign function. + * @typedef Sign * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param algName Indicates the key agreement algorithm name. */ - function createKeyAgreement(algName : string) : KeyAgreement; - - interface X509Cert { - /** - * Verify the X509 cert. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param key Indicates the cert chain validator data. - */ - verify(key : PubKey, callback : AsyncCallback) : void; - verify(key : PubKey) : Promise; - + interface Sign { /** - * Get X509 cert encoded data. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to init environment. + * @param { PriKey } priKey - the private key. + * @param { AsyncCallback } callback - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert encoded data. */ - getEncoded(callback : AsyncCallback) : void; - getEncoded() : Promise; - - /** - * Get X509 cert public key. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns X509 cert pubKey. - */ - getPublicKey(callback : AsyncCallback) : void; - getPublicKey() : Promise; - - /** - * Check the X509 cert validity with date. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param date Indicates the cert date. - */ - checkValidityWithDate(date: string, callback : AsyncCallback) : void; - checkValidityWithDate(date: string) : Promise; - - /** - * Get X509 cert version. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns X509 cert version. - */ - getVersion() : number; - - /** - * Get X509 cert serial number. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns X509 cert serial number. - */ - getSerialNumber() : number; + init(priKey : PriKey, callback : AsyncCallback) : void; - /** - * Get X509 cert issuer name. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + /** + * Used to init environment. + * @param { PriKey } priKey - the private key. + * @returns { promise } - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert issuer name. */ - getIssuerName() : DataBlob; + init(priKey : PriKey) : Promise; /** - * Get X509 cert subject name. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to append the message need to be signed. + * @param { DataBlob } data - the data need to be signed. + * @param { AsyncCallback } callback - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert subject name. */ - getSubjectName() : DataBlob; + update(data : DataBlob, callback : AsyncCallback) : void; /** - * Get X509 cert not before time. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to append the message need to be signed. + * @param { DataBlob } data - the data need to be signed. + * @returns { promise } - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert not before time. */ - getNotBeforeTime() : string; + update(data : DataBlob) : Promise; /** - * Get X509 cert not after time. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to sign message, include the update data. + * @param { DataBlob } data - the data need to be signed. + * @param { AsyncCallback } callback - return the signed message. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert not after time. */ - getNotAfterTime() : string; + sign(data : DataBlob, callback : AsyncCallback) : void; /** - * Get X509 cert signature. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to append the message need to be signed. + * @param { DataBlob } data - the private key. + * @returns { promise } - return the signed message. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert signature. */ - getSignature() : DataBlob; + sign(data : DataBlob) : Promise; /** - * Get X509 cert signature's algorithm name. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * The algName of the AsyKeyGenerator. + * @type { string } + * @syscap SystemCapability.Security.CryptoFramework. + * @readonly * @since 9 - * @return Returns X509 cert signature's algorithm name. */ - getSignatureAlgName() : string; + readonly algName : string; + } + /** + * Provides verify function. + * @typedef Verify + * @syscap SystemCapability.Security.CryptoFramework + * @since 9 + */ + interface Verify { /** - * Get X509 cert signature's algorithm oid. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to init environment. + * @param { PubKey } pubKey - the public key. + * @param { AsyncCallback } callback - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert signature's algorithm oid. */ - getSignatureAlgOid() : string; + init(pubKey : PubKey, callback : AsyncCallback) : void; /** - * Get X509 cert signature's algorithm name. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to init environment. + * @param { PubKey } pubKey - the public key. + * @returns { promise } - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert signature's algorithm name. */ - getSignatureAlgParams() : DataBlob; + init(pubKey : PubKey) : Promise; /** - * Get X509 cert key usage. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to append the message need to be verified. + * @param { DataBlob } data - the data need to be verified. + * @param { AsyncCallback } callback - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert key usage. */ - getKeyUsage() : DataBlob; + update(data : DataBlob, callback : AsyncCallback) : void; /** - * Get X509 cert extended key usage. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to append the message need to be verified. + * @param { DataBlob } data - the data need to be verified. + * @returns { promise } - return nothing. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert extended key usage. */ - getExtKeyUsage() : DataArray; + update(data : DataBlob) : Promise; /** - * Get X509 cert basic constraints path len. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to verify message, include the update data. + * @param { DataBlob } data - the data need to be verified. + * @param { DataBlob } signatureData - the signature data. + * @param { AsyncCallback } callback - return the verify result. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert basic constraints path len. */ - getBasicConstraints() : number; + verify(data : DataBlob, signatureData : DataBlob, callback : AsyncCallback) : void; /** - * Get X509 cert subject alternative name. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to verify message, include the update data. + * @param { DataBlob } data - the data need to be verified. + * @param { DataBlob } signatureData - the signature data. + * @returns { Promise } callback - return the verify result. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns X509 cert subject alternative name. */ - getSubjectAltNames() : DataArray; + verify(data : DataBlob, signatureData : DataBlob) : Promise; /** - * Get X509 cert issuer alternative name. - * + * Indicates the verify algorithm name. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @return Returns X509 cert issuer alternative name. */ - getIssuerAltNames() : DataArray; + readonly algName : string; } /** - * Provides the x509 cert func. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Create sign class. + * @param { string } algName - Indicates the algorithm name and params. + * @returns { Sign } the sign class. + * @throws { BusinessError } 401 - invalid parameters. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @param inStream Indicates the input cert data. - * @return Returns X509 cert instance. */ - function createX509Cert(inStream : EncodingBlob, callback : AsyncCallback) : void; - function createX509Cert(inStream : EncodingBlob) : Promise; + function createSign(algName : string) : Sign; + + /** + * Create verify class. + * @param { string } algName - Indicates the algorithm name and params. + * @returns { Verify } the verify class. + * @throws { BusinessError } 401 - invalid parameters. + * @syscap SystemCapability.Security.CryptoFramework. + * @since 9 + */ + function createVerify(algName : string) : Verify; /** - * Interface of X509CrlEntry. - * @since 9 + * Provides key agreement function. + * @typedef KeyAgreement * @syscap SystemCapability.Security.CryptoFramework - */ - interface X509CrlEntry { - /** - * Returns the ASN of this CRL entry 1 der coding form, i.e. internal sequence. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns EncodingBlob of crl entry. - */ - getEncoded(callback : AsyncCallback) : void; - getEncoded() : Promise; - - /** - * Get the serial number from this x509crl entry. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns serial number of crl entry. - */ - getSerialNumber() : number; - - /** - * Get the issuer of the x509 certificate described by this entry. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns DataBlob of issuer. - */ - getCertIssuer(callback : AsyncCallback) : void; - getCertIssuer() : Promise; - - /** - * Get the revocation date from x509crl entry. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns string of revocation date. - */ - getRevocationDate(callback : AsyncCallback) : void; - getRevocationDate() : Promise; - } - - /** - * Interface of X509Crl. * @since 9 - * @syscap SystemCapability.Security.CryptoFramework */ - interface X509Crl { - /** - * Check if the given certificate is on this CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param X509Cert Input cert data. - * @return Returns result of Check cert is revoked or not. - */ - isRevoked(cert : X509Cert, callback : AsyncCallback) : void; - isRevoked(cert : X509Cert) : Promise; - - /** - * Returns the type of this CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns string of crl type. - */ - getType() : string; - - /** - * Get the der coding format. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns EncodingBlob of crl. - */ - getEncoded(callback : AsyncCallback) : void; - getEncoded() : Promise; - + interface KeyAgreement { /** - * Use the public key to verify the signature of CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to generate secret. + * @param { PriKey } priKey - the private key. + * @param { PubKey } pubKey - the public key. + * @param { AsyncCallback } callback - return the secret. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @param PubKey Input public Key. - * @return Returns verify result. */ - verify(key : PubKey, callback : AsyncCallback) : void; - verify(key : PubKey) : Promise; - - /** - * Get version number from CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns version of crl. - */ - getVersion() : number; - - /** - * Get the issuer name from CRL. Issuer means the entity that signs and publishes the CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns issuer name of crl. - */ - getIssuerName() : DataBlob; - - /** - * Get lastUpdate value from CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns last update of crl. - */ - getLastUpdate() : string; - - /** - * Get nextUpdate value from CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns next update of crl. - */ - getNextUpdate() : string; - - /** - * This method can be used to find CRL entries in indirect CRLs. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param serialNumber serial number of crl. - * @return Returns next update of crl. - */ - getRevokedCert(serialNumber : number, callback : AsyncCallback) : void; - getRevokedCert(serialNumber : number) : Promise; - - /** - * This method can be used to find CRL entries in indirect cert. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param X509Cert Cert of x509. - * @return Returns X509CrlEntry instance. - */ - getRevokedCertWithCert(cert : X509Cert, callback : AsyncCallback) : void; - getRevokedCertWithCert(cert : X509Cert) : Promise; - - /** - * Get all entries in this CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns Array of X509CrlEntry instance. - */ - getRevokedCerts(callback : AsyncCallback>) : void; - getRevokedCerts() : Promise>; - - /** - * Get the CRL information encoded by Der from this CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns DataBlob of tbs info. - */ - getTbsInfo(callback : AsyncCallback) : void; - getTbsInfo() : Promise; - - /** - * Get signature value from CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns DataBlob of signature. - */ - getSignature() : DataBlob; - - /** - * Get the signature algorithm name of the CRL signature algorithm. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns string of signature algorithm name. - */ - getSignatureAlgName() : string; - - /** - * Get the signature algorithm oid string from CRL. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @return Returns string of signature algorithm oid. - */ - getSignatureAlgOid() : string; + generateSecret(priKey : PriKey, pubKey : PubKey, callback : AsyncCallback) : void; /** - * Get the der encoded signature algorithm parameters from the CRL signature algorithm. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Used to generate secret. + * @param { PriKey } priKey - the private key. + * @param { PubKey } pubKey - the public key. + * @returns { Promise } the promise used to return secret. + * @throws { BusinessError } 401 - invalid parameters. + * @throws { BusinessError } 17620001 - memory error. + * @throws { BusinessError } 17620002 - runtime error. + * @throws { BusinessError } 17630001 - crypto operation error. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @return Returns DataBlob of signature algorithm params. */ - getSignatureAlgParams() : DataBlob; - } - - /** - * Provides the x509 CRL func. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' - * @since 9 - * @param inStream Indicates the input CRL data. - * @return Returns the x509 CRL instance. - */ - function createX509Crl(inStream : EncodingBlob, callback : AsyncCallback) : void; - function createX509Crl(inStream : EncodingBlob) : Promise; + generateSecret(priKey : PriKey, pubKey : PubKey) : Promise; - /** - * Certification chain validator. - * @since 9 - * @syscap SystemCapability.Security.CryptoFramework - */ - interface CertChainValidator { /** - * Validate the cert chain. - * + * Indicates the algorithm name. + * @type { string } + * @readonly * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' * @since 9 - * @param certChain Indicates the cert chain validator data. */ - validate(certChain : CertChainData, callback : AsyncCallback) : void; - validate(certChain : CertChainData) : Promise; - readonly algorithm : string; + readonly algName : string; } /** - * Provides the cert chain validator func. - * - * @syscap SystemCapability.Security.CryptoFramework - * @import import cryptoFramework from '@ohos.security.cryptoFramework' + * Create key agreement class. + * @param { string } algName - Indicates the algorithm name and params. + * @returns { KeyAgreement } the key agreement class. + * @throws { BusinessError } 401 - invalid parameters. + * @syscap SystemCapability.Security.CryptoFramework. * @since 9 - * @param algorithm Indicates the cert chain validator type. - * @return Returns the cert chain validator instance. */ - function createCertChainValidator(algorithm :string) : CertChainValidator; + function createKeyAgreement(algName : string) : KeyAgreement; } export default cryptoFramework; -- Gitee