diff --git a/BUILD.gn b/BUILD.gn index 71683215d60b1181725bb9a4cdf7bf25ff4d5e5a..468f63366461ce0cbf08f788b27633b1fcc42973 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 289dc5729743cb12e0606158dcec6b7fccd1faf1..6d68c57543d3a86c71a9f3550815b4e1b7007647 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 e92dc0bc1aa3a5ec9c6e708bf598315cf040e84a..3ff987cfa52e012e141b7800dc4fe84189d6dd76 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 4fc5765dae2028eebc846ebc400c2b34bff92dc4..b7fb0f1c9ed8978f5443d7068bd9fea274000871 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 590080c9dc40aa0067abf4adcaf62439d712a510..357fb1b7180a9f0fd041c4ab74e3e6e12e76665e 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 1ec15e8194c26148a6745a1e815b9019242b4aef..2630fb4ef8dd5e24953672e344b32d84aa504607 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 de6c3d1d739ffc9eaff9d59809a320a38ad29114..fe180ca688c6da3dab51beeb6ad476ecf983d4c4 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 0000000000000000000000000000000000000000..554b3084d30491ffe5fd7a2900960927200cd290 --- /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 94% rename from frameworks/js/napi/src/napi_cert_chain_validator.cpp rename to frameworks/js/napi/certificate/src/napi_cert_chain_validator.cpp index 0f5976b6d7dd4e460597999cc13dd81c9f7d0fcd..8f9a13f0f4719eeaa82e38deb9d6bb106f570f62 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; @@ -166,7 +167,7 @@ napi_value NapiCertChainValidator::Validate(napi_env env, napi_callback_info inf env, nullptr, GetResourceName(env, "Validate"), ValidateExecute, ValidateComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -236,7 +237,7 @@ napi_value NapiCertChainValidator::CreateCertChainValidator(napi_env env, napi_c napi_wrap( env, validatorInstance, ccvClass, [](napi_env env, void* data, void *hint) { - NapiCertChainValidator *ccv = (NapiCertChainValidator *)data; + NapiCertChainValidator *ccv = static_cast(data); delete ccv; }, 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 0000000000000000000000000000000000000000..30b1745be02d0021b9aa85d1a61853f0e4943a07 --- /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 89% rename from frameworks/js/napi/src/napi_x509_certificate.cpp rename to frameworks/js/napi/certificate/src/napi_x509_certificate.cpp index 798292dbefdbc8b729ad570914dabb08cd78c396..1c788f84f9b662ef44a1f9f9023bc631e4e3fe4e 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; } @@ -139,7 +140,7 @@ static void VerifyExecute(napi_env env, void *data) LOGI("start to verify."); CfCtx *context = static_cast(data); HcfX509Certificate *cert = context->certClass->GetX509Cert(); - context->errCode = cert->base.verify((HcfCertificate *)cert, context->pubKey); + context->errCode = cert->base.verify(&(cert->base), context->pubKey); if (context->errCode != HCF_SUCCESS) { LOGE("verify cert failed!"); context->errMsg = "verify cert failed"; @@ -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)); @@ -176,14 +177,14 @@ static void GetEncodedExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509Certificate *cert = context->certClass->GetX509Cert(); - HcfEncodingBlob *encodingBlob = (HcfEncodingBlob *)HcfMalloc(sizeof(HcfEncodingBlob), 0); + HcfEncodingBlob *encodingBlob = static_cast(HcfMalloc(sizeof(HcfEncodingBlob), 0)); if (encodingBlob == nullptr) { LOGE("malloc encoding blob failed!"); context->errCode = HCF_ERR_MALLOC; context->errMsg = "malloc encoding blob failed"; return; } - context->errCode = cert->base.getEncoded((HcfCertificate *)cert, encodingBlob); + context->errCode = cert->base.getEncoded(&(cert->base), encodingBlob); if (context->errCode != HCF_SUCCESS) { LOGE("get cert encoded failed!"); context->errMsg = "get cert encoded failed"; @@ -208,7 +209,7 @@ void GetPublicKeyExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509Certificate *cert = context->certClass->GetX509Cert(); - context->errCode = cert->base.getPublicKey((HcfCertificate *)cert, &context->returnPubKey); + context->errCode = cert->base.getPublicKey(&(cert->base), &context->returnPubKey); if (context->errCode != HCF_SUCCESS) { LOGE("get cert public key failed!"); context->errMsg = "get cert public key failed"; @@ -228,7 +229,7 @@ void GetPublicKeyComplete(napi_env env, napi_status status, void *data) napi_wrap( env, instance, pubKeyClass, [](napi_env env, void *data, void *hint) { - NapiPubKey *pubKeyClass = (NapiPubKey *)data; + NapiPubKey *pubKeyClass = static_cast(data); delete pubKeyClass; return; }, @@ -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; @@ -273,7 +274,7 @@ napi_value NapiX509Certificate::Verify(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "Verify"), VerifyExecute, VerifyComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -310,7 +311,7 @@ napi_value NapiX509Certificate::GetEncoded(napi_env env, napi_callback_info info env, nullptr, GetResourceName(env, "GetEncoded"), GetEncodedExecute, GetEncodedComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -347,7 +348,7 @@ napi_value NapiX509Certificate::GetPublicKey(napi_env env, napi_callback_info in env, nullptr, GetResourceName(env, "GetPublicKey"), GetPublicKeyExecute, GetPublicKeyComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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,9 +389,9 @@ napi_value NapiX509Certificate::CheckValidityWithDate(napi_env env, napi_callbac napi_create_async_work( env, nullptr, GetResourceName(env, "CheckValidityWithDate"), - checkValidityWithDateExecute, - checkValidityWithDateComplete, - (void *)context, + CheckValidityWithDateExecute, + CheckValidityWithDateComplete, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, 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,11 +438,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); 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,11 +468,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); 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,11 +498,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; @@ -509,14 +510,14 @@ 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -528,11 +529,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; @@ -540,14 +541,14 @@ 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -559,11 +560,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); 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,11 +590,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; @@ -601,14 +602,14 @@ 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -620,10 +621,10 @@ 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 = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; @@ -631,14 +632,14 @@ 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -650,11 +651,11 @@ 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; } - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); 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,10 +681,10 @@ 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 = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; @@ -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,11 +709,11 @@ 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; } - HcfArray *array = (HcfArray *)HcfMalloc(sizeof(HcfArray), 0); + HcfArray *array = reinterpret_cast(HcfMalloc(sizeof(HcfArray), 0)); if (array == nullptr) { LOGE("malloc array failed!"); 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,11 +755,11 @@ 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; } - HcfArray *array = (HcfArray *)HcfMalloc(sizeof(HcfArray), 0); + HcfArray *array = reinterpret_cast(HcfMalloc(sizeof(HcfArray), 0)); if (array == nullptr) { LOGE("malloc array failed!"); 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,11 +784,11 @@ 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; } - HcfArray *array = (HcfArray *)HcfMalloc(sizeof(HcfArray), 0); + HcfArray *array = reinterpret_cast(HcfMalloc(sizeof(HcfArray), 0)); if (array == nullptr) { LOGE("malloc array failed!"); 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; @@ -1078,7 +1079,7 @@ void NapiX509Certificate::CreateX509CertComplete(napi_env env, napi_status statu napi_wrap( env, instance, x509CertClass, [](napi_env env, void *data, void *hint) { - NapiX509Certificate *certClass = (NapiX509Certificate *)data; + NapiX509Certificate *certClass = static_cast(data); delete certClass; return; }, @@ -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; } @@ -1117,7 +1118,7 @@ napi_value NapiX509Certificate::NapiCreateX509Cert(napi_env env, napi_callback_i env, nullptr, GetResourceName(env, "CreateX509Cert"), CreateX509CertExecute, CreateX509CertComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); diff --git a/frameworks/js/napi/src/napi_x509_crl.cpp b/frameworks/js/napi/certificate/src/napi_x509_crl.cpp similarity index 90% rename from frameworks/js/napi/src/napi_x509_crl.cpp rename to frameworks/js/napi/certificate/src/napi_x509_crl.cpp index 641576883ca9411f88218baa2db2a8c4d3e861ff..656b6a9c72ae78c54a7180b58aa459a4b8c71ae2 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; } @@ -155,7 +156,7 @@ void IsInvokedExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509Crl *x509Crl = context->crlClass->GetX509Crl(); - context->isRevoked = x509Crl->base.isRevoked((HcfCrl *)x509Crl, (HcfCertificate *)context->certificate); + context->isRevoked = x509Crl->base.isRevoked(&(x509Crl->base), &(context->certificate->base)); context->errCode = HCF_SUCCESS; } @@ -176,7 +177,7 @@ static void GetEncodedExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509Crl *x509Crl = context->crlClass->GetX509Crl(); - HcfEncodingBlob *encodingBlob = (HcfEncodingBlob *)HcfMalloc(sizeof(HcfEncodingBlob), 0); + HcfEncodingBlob *encodingBlob = static_cast(HcfMalloc(sizeof(HcfEncodingBlob), 0)); if (encodingBlob == nullptr) { LOGE("malloc encoding blob failed!"); context->errCode = HCF_ERR_MALLOC; @@ -246,7 +247,7 @@ void GetRevokedCertificateComplete(napi_env env, napi_status status, void *data) napi_wrap( env, instance, x509CrlEntryClass, [](napi_env env, void *data, void *hint) { - NapiX509CrlEntry *x509CrlEntryClass = (NapiX509CrlEntry *)data; + NapiX509CrlEntry *x509CrlEntryClass = static_cast(data); delete x509CrlEntryClass; return; }, @@ -279,7 +280,7 @@ void GetRevokedCertificateWithCertComplete(napi_env env, napi_status status, voi napi_wrap( env, instance, x509CrlEntryClass, [](napi_env env, void *data, void *hint) { - NapiX509CrlEntry *x509CrlEntryClass = (NapiX509CrlEntry *)data; + NapiX509CrlEntry *x509CrlEntryClass = static_cast(data); delete x509CrlEntryClass; return; }, @@ -292,7 +293,7 @@ void GetRevokedCertificatesExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509Crl *x509Crl = context->crlClass->GetX509Crl(); - HcfArray *array = (HcfArray *)HcfMalloc(sizeof(HcfArray), 0); + HcfArray *array = reinterpret_cast(HcfMalloc(sizeof(HcfArray), 0)); if (array == nullptr) { LOGE("malloc array failed!"); context->errCode = HCF_ERR_MALLOC; @@ -320,14 +321,14 @@ static napi_value GenerateCrlEntryArray(napi_env env, HcfArray *array) napi_value returnArray = nullptr; napi_create_array(env, &returnArray); for (uint32_t i = 0; i < array->count; i++) { - HcfBlob *blob = (HcfBlob *)(array->data + i); - HcfX509CrlEntry *entry = (HcfX509CrlEntry *)blob->data; + HcfBlob *blob = reinterpret_cast(array->data + i); + HcfX509CrlEntry *entry = reinterpret_cast(blob->data); napi_value instance = NapiX509CrlEntry::CreateX509CrlEntry(env); NapiX509CrlEntry *x509CrlEntryClass = new NapiX509CrlEntry(entry); napi_wrap( env, instance, x509CrlEntryClass, [](napi_env env, void *data, void *hint) { - NapiX509CrlEntry *x509CrlEntryClass = (NapiX509CrlEntry *)data; + NapiX509CrlEntry *x509CrlEntryClass = static_cast(data); delete x509CrlEntryClass; return; }, @@ -354,7 +355,7 @@ void GetTBSCertListExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509Crl *x509Crl = context->crlClass->GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); context->errCode = HCF_ERR_MALLOC; @@ -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; } @@ -416,7 +417,7 @@ napi_value NapiX509Crl::IsRevoked(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "IsRevoked"), IsInvokedExecute, IsInvokedComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -431,12 +432,12 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - const char *type = x509Crl->base.getType((HcfCrl *)x509Crl); + const char *type = x509Crl->base.getType(&(x509Crl->base)); napi_value result = nullptr; napi_create_string_utf8(env, type, strlen(type), &result); return result; @@ -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; } @@ -468,7 +469,7 @@ napi_value NapiX509Crl::GetEncoded(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "GetEncoded"), GetEncodedExecute, GetEncodedComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -514,7 +515,7 @@ napi_value NapiX509Crl::Verify(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "Verify"), VerifyExecute, VerifyComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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,19 +545,19 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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,26 +574,26 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -603,26 +604,26 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); 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; @@ -661,7 +662,7 @@ napi_value NapiX509Crl::GetRevokedCertificate(napi_env env, napi_callback_info i env, nullptr, GetResourceName(env, "GetRevokedCertificate"), GetRevokedCertificateExecute, GetRevokedCertificateComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -707,7 +708,7 @@ napi_value NapiX509Crl::GetRevokedCertificateWithCert(napi_env env, napi_callbac env, nullptr, GetResourceName(env, "GetRevokedCertificateWithCert"), GetRevokedCertificateWithCertExecute, GetRevokedCertificateWithCertComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -744,7 +745,7 @@ napi_value NapiX509Crl::GetRevokedCertificates(napi_env env, napi_callback_info env, nullptr, GetResourceName(env, "GetRevokedCertificates"), GetRevokedCertificatesExecute, GetRevokedCertificatesComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -781,7 +782,7 @@ napi_value NapiX509Crl::GetTBSCertList(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "GetTBSCertList"), GetTBSCertListExecute, GetTBSCertListComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -796,19 +797,19 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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,26 +826,26 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -855,26 +856,26 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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; return nullptr; } napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); HcfBlobDataFree(blob); HcfFree(blob); blob = nullptr; @@ -885,19 +886,19 @@ 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; } HcfX509Crl *x509Crl = GetX509Crl(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); return nullptr; } 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; @@ -1143,7 +1144,7 @@ void NapiX509Crl::CreateX509CrlComplete(napi_env env, napi_status status, void * napi_wrap( env, instance, x509CrlClass, [](napi_env env, void *data, void *hint) { - NapiX509Crl *crlClass = (NapiX509Crl *)data; + NapiX509Crl *crlClass = static_cast(data); delete crlClass; return; }, @@ -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; } @@ -1182,7 +1183,7 @@ napi_value NapiX509Crl::NapiCreateX509Crl(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "createX509Crl"), CreateX509CrlExecute, CreateX509CrlComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); 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 93% rename from frameworks/js/napi/src/napi_x509_crl_entry.cpp rename to frameworks/js/napi/certificate/src/napi_x509_crl_entry.cpp index dec22de00a68823d9ab0899c181f38eaef52a8cb..53c48ea68b7e1b0f774e03eedf303dc0ca3f9196 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; } @@ -133,7 +134,7 @@ static void GetEncodedExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry(); - HcfEncodingBlob *encodingBlob = (HcfEncodingBlob *)HcfMalloc(sizeof(HcfEncodingBlob), 0); + HcfEncodingBlob *encodingBlob = static_cast(HcfMalloc(sizeof(HcfEncodingBlob), 0)); if (encodingBlob == nullptr) { LOGE("malloc encoding blob failed!"); context->errCode = HCF_ERR_MALLOC; @@ -166,7 +167,7 @@ static void GetCertificateIssuerExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); context->errCode = HCF_ERR_MALLOC; @@ -199,7 +200,7 @@ static void GetRevocationDateExecute(napi_env env, void *data) { CfCtx *context = static_cast(data); HcfX509CrlEntry *x509CrlEntry = context->crlEntryClass->GetX509CrlEntry(); - HcfBlob *blob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *blob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (blob == nullptr) { LOGE("malloc blob failed!"); context->errCode = HCF_ERR_MALLOC; @@ -224,7 +225,7 @@ static void GetRevocationDateComplete(napi_env env, napi_status status, void *da } HcfBlob *blob = context->blob; napi_value result = nullptr; - napi_create_string_utf8(env, (char *)blob->data, blob->len, &result); + napi_create_string_utf8(env, reinterpret_cast(blob->data), blob->len, &result); ReturnResult(env, context, result); FreeCryptoFwkCtx(env, context); } @@ -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; } @@ -255,7 +256,7 @@ napi_value NapiX509CrlEntry::GetEncoded(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "GetEncoded"), GetEncodedExecute, GetEncodedComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -307,7 +308,7 @@ napi_value NapiX509CrlEntry::GetCertificateIssuer(napi_env env, napi_callback_in env, nullptr, GetResourceName(env, "GetCertificateIssuer"), GetCertificateIssuerExecute, GetCertificateIssuerComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; } @@ -345,7 +346,7 @@ napi_value NapiX509CrlEntry::GetRevocationDate(napi_env env, napi_callback_info env, nullptr, GetResourceName(env, "GetRevocationDate"), GetRevocationDateExecute, GetRevocationDateComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); diff --git a/frameworks/js/napi/crypto/BUILD.gn b/frameworks/js/napi/crypto/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e24a50170c04db3e6cc5fa6105ebebd29b4e1ac4 --- /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 620707da96414c9686fa58328f80e9a8ea08af6a..39923aec188a41d6ac226c92ba439a2fc34ff3d6 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 14e0773f00ae9dd9daeeb6fd76e6bcf691524aea..6c36b67001ff02640d7fceb05a07a945ddda5525 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 c60707c060f95cf56a040631d77481caf5a88d53..e76f3973372d7f4f80074cb6f2bdff2a5945acc4 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 683206e0375856d893daeb6c92822238111f5417..95a45dc6cf49830ec4b9c0c2c8eb0f46e6049516 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 94e24916cff29599e545687bad86aa11a2678327..7d3529a6361e968dd3bad6fbdbe0cd7217b8e791 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 a08e7644b69f649dbfd9ee62b5bac4bc7924bedd..e5db6c0965f50dee8d88d0f31034312568d3b990 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 0af1c42d6b1f8b1b228b12e9be6666b07e547381..674c80535d463a51fe82ae2e3f31eb0eec497e18 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 158eec336b511eaad5b9bed3ebf8430e66bbb5ad..4467807f5339459d03f89e6ac29a06e4a1e77d48 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 011fbfb18496f599c0ad3ed4868ff8bcb775b24a..b87dd948d16898abb787d60ae7d093532013fb45 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 bcbca925f307095236887426e5084d8b1b109595..389aaca587cb8172b811d1dbc4b2adb932503984 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 a4b2ebfa34d373de9114dc903aeaf49e8e617538..dc854dc0c3642003751114436c0e9bc727b7cbc8 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 b1557d1f89dd4b4a0f5257067cb0747e20cf2826..e4f77763f4ba7ddc5b1ed82ef97b651c51bee122 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 4a3c646a59011e2159cee0fd72b8d59988141a59..a0613b0ec7eaee008653a4cd53d9dc2676f79926 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 68401eed0f60e0a2551fb82d86c5c641ce309075..92f6591e7782f0d6866d405cf873912c1798b938 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 493b4c77b92d707af5cc9faaee6803b3041e2021..46a4f0758f1440f713bc978ca7896627691f70db 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 ac0aec976eb272c14ff1652680ab0e4daa61988b..2d2aa0702a4283f66d2483dfba44e5b50a0123b0 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 92% rename from frameworks/js/napi/src/napi_asy_key_generator.cpp rename to frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp index 539b2401ee52736290962aaae46f634295fdcd94..c967e9c5a641a0cf4f075c4d9c2c56fd428226bf 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)); } } @@ -358,7 +362,7 @@ static napi_value NewGenKeyPairAsyncWork(napi_env env, GenKeyPairCtx *ctx) GenKeyPairAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -386,7 +390,7 @@ static napi_value NewConvertKeyAsyncWork(napi_env env, ConvertKeyCtx *ctx) ConvertKeyAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -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."); @@ -498,7 +502,7 @@ napi_value NapiAsyKeyGenerator::CreateJsAsyKeyGenerator(napi_env env, napi_callb napi_wrap( env, instance, napiAsyKeyGenerator, [](napi_env env, void *data, void *hint) { - NapiAsyKeyGenerator *napiAsyKeyGenerator = (NapiAsyKeyGenerator *)(data); + NapiAsyKeyGenerator *napiAsyKeyGenerator = static_cast(data); delete napiAsyKeyGenerator; return; }, diff --git a/frameworks/js/napi/src/napi_cipher.cpp b/frameworks/js/napi/crypto/src/napi_cipher.cpp similarity index 90% rename from frameworks/js/napi/src/napi_cipher.cpp rename to frameworks/js/napi/crypto/src/napi_cipher.cpp index a80418a6cabd262d284d7a12f7860f99c2d20760..a172e4bfae29ef4e71437c98b7fef5c058350881 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; @@ -57,13 +57,13 @@ static void FreeParamsSpec(HcfParamsSpec *paramsSpec) return; } if (IV_PARAMS_SPEC.compare(paramsSpec->getType()) == 0) { - HcfIvParamsSpec *iv = (HcfIvParamsSpec *)paramsSpec; + HcfIvParamsSpec *iv = reinterpret_cast(paramsSpec); HcfFree(iv->iv.data); iv->iv.data = nullptr; iv->iv.len = 0; } if (GCM_PARAMS_SPEC.compare(paramsSpec->getType()) == 0) { - HcfGcmParamsSpec *gcm = (HcfGcmParamsSpec *)paramsSpec; + HcfGcmParamsSpec *gcm = reinterpret_cast(paramsSpec); HcfFree(gcm->iv.data); HcfFree(gcm->aad.data); HcfFree(gcm->tag.data); @@ -75,7 +75,7 @@ static void FreeParamsSpec(HcfParamsSpec *paramsSpec) gcm->tag.data = nullptr; } if (CCM_PARAMS_SPEC.compare(paramsSpec->getType()) == 0) { - HcfCcmParamsSpec *ccm = (HcfCcmParamsSpec *)paramsSpec; + HcfCcmParamsSpec *ccm = reinterpret_cast(paramsSpec); HcfFree(ccm->iv.data); HcfFree(ccm->aad.data); HcfFree(ccm->tag.data); @@ -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; @@ -428,7 +431,7 @@ napi_value NewAsyncInit(napi_env env, CipherFwkCtx context) AsyncInitReturn(env, status, data); return; }, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -454,7 +457,7 @@ napi_value NewAsyncUpdate(napi_env env, CipherFwkCtx context) AsyncUpdateReturn(env, status, data); return; }, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -480,7 +483,7 @@ napi_value NewAsyncDoFinal(napi_env env, CipherFwkCtx context) AsyncDoFinalReturn(env, status, data); return; }, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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; @@ -579,7 +581,7 @@ napi_value NapiCipher::JsGetAlgorithm(napi_env env, napi_callback_info info) // execute C function const char *algo = cipher->getAlgorithm(cipher); napi_value instance = nullptr; - napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance); + napi_create_string_utf8(env, algo, NAPI_AUTO_LENGTH, &instance); return instance; } @@ -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 449d5489e1cd08ce63fdf09527c4646e29c0dd84..96d20c2003f5fa4fe139c2271d22dff5607c7767 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 8323ff1814d65fd4870ef5b4efc23850dea65d74..ae80d9ae5f2129b343e143263151c7b5983d62a6 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 b88413bd78dfbbf0eca10de72e554d8e6c0ec3b1..183592c927caa8ee36c04a5d2a65b6a05236d78d 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)); } } @@ -194,7 +195,7 @@ static napi_value NewKeyAgreementAsyncWork(napi_env env, KeyAgreementCtx *ctx) KeyAgreementAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -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 93% rename from frameworks/js/napi/src/napi_mac.cpp rename to frameworks/js/napi/crypto/src/napi_mac.cpp index e7254be1281c98f221bc9cca2424881e90c87b49..bf1ad5b3c5300071e828317843741661819d4090 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; } @@ -130,7 +131,7 @@ static void MacInitExecute(napi_env env, void *data) MacCtx *context = static_cast(data); NapiMac *macClass = context->macClass; HcfMac *macObj = macClass->GetMac(); - HcfSymKey *symKey = (HcfSymKey *)context->symKey; + HcfSymKey *symKey = context->symKey; context->errCode = macObj->init(macObj, symKey); if (context->errCode != HCF_SUCCESS) { LOGE("init failed!"); @@ -156,7 +157,7 @@ static void MacUpdateExecute(napi_env env, void *data) MacCtx *context = static_cast(data); NapiMac *macClass = context->macClass; HcfMac *macObj = macClass->GetMac(); - HcfBlob *inBlob = static_cast(context->inBlob); + HcfBlob *inBlob = reinterpret_cast(context->inBlob); context->errCode = macObj->update(macObj, inBlob); if (context->errCode != HCF_SUCCESS) { LOGE("update failed!"); @@ -182,7 +183,7 @@ static void MacDoFinalExecute(napi_env env, void *data) MacCtx *context = static_cast(data); NapiMac *macClass = context->macClass; HcfMac *macObj = macClass->GetMac(); - HcfBlob *outBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *outBlob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (outBlob == nullptr) { LOGE("outBlob is null!"); context->errCode = HCF_ERR_MALLOC; @@ -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; @@ -250,7 +251,7 @@ napi_value NapiMac::MacInit(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "Init"), MacInitExecute, MacInitComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -291,7 +292,7 @@ napi_value NapiMac::MacUpdate(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "MacUpate"), MacUpdateExecute, MacUpdateComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -327,7 +328,7 @@ napi_value NapiMac::MacDoFinal(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "MacDoFinal"), MacDoFinalExecute, MacDoFinalComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -449,7 +450,7 @@ napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info) napi_wrap( env, instance, macNapiObj, [](napi_env env, void *data, void *hint) { - NapiMac *mac = (NapiMac *)(data); + NapiMac *mac = static_cast(data); delete mac; return; }, diff --git a/frameworks/js/napi/src/napi_md.cpp b/frameworks/js/napi/crypto/src/napi_md.cpp similarity index 93% rename from frameworks/js/napi/src/napi_md.cpp rename to frameworks/js/napi/crypto/src/napi_md.cpp index 9f07afe7ddb52da2958e1004d9fac476e5278f89..1fb9a34b77b3c760dfd07975fca45beec8be8cee 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; } @@ -151,7 +152,7 @@ static void MdDoFinalExecute(napi_env env, void *data) MdCtx *context = static_cast(data); NapiMd *mdClass = context->mdClass; HcfMd *mdObj = mdClass->GetMd(); - HcfBlob *outBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *outBlob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (outBlob == nullptr) { LOGE("outBlob is null!"); context->errCode = HCF_ERR_MALLOC; @@ -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; } @@ -214,7 +215,7 @@ napi_value NapiMd::MdUpdate(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "MdUpdate"), MdUpdateExecute, MdUpdateComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -249,7 +250,7 @@ napi_value NapiMd::MdDoFinal(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "MdDoFinal"), MdDoFinalExecute, MdDoFinalComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -361,7 +362,7 @@ napi_value NapiMd::CreateMd(napi_env env, napi_callback_info info) napi_wrap( env, instance, mdNapiObj, [](napi_env env, void *data, void *hint) { - NapiMd *md = (NapiMd *)(data); + NapiMd *md = static_cast(data); delete md; return; }, 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 6c406eca929b549dabb7836966355497c2691742..2a42a3afb621a1b1ef38555e3dd28d3484d9a855 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 7acd8072ae933d323a05205c5b82717d408c2d57..14f0228236eaf2196bfc8d639309e1ad5601e6b5 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 94% rename from frameworks/js/napi/src/napi_rand.cpp rename to frameworks/js/napi/crypto/src/napi_rand.cpp index 5e867ee6a45cac34b6429fe9ec3321b0cb1d22f8..05b32be08de89466c49a8a847f052ed9d50976ae 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; } @@ -127,7 +128,7 @@ static void GenerateRandomExecute(napi_env env, void *data) RandCtx *context = static_cast(data); NapiRand *randClass = context->randClass; HcfRand *randObj = randClass->GetRand(); - HcfBlob *randBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); + HcfBlob *randBlob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); if (randBlob == nullptr) { LOGE("randBlob is null!"); context->errCode = HCF_ERR_MALLOC; @@ -165,7 +166,7 @@ static void SetSeedExecute(napi_env env, void *data) RandCtx *context = static_cast(data); NapiRand *randClass = context->randClass; HcfRand *randObj = randClass->GetRand(); - HcfBlob *seedBlob = static_cast(context->seedBlob); + HcfBlob *seedBlob = reinterpret_cast(context->seedBlob); context->errCode = randObj->setSeed(randObj, seedBlob); if (context->errCode != HCF_SUCCESS) { LOGE("setSeed failed!"); @@ -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; @@ -221,7 +222,7 @@ napi_value NapiRand::GenerateRandom(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "GenerateRandom"), GenerateRandomExecute, GenerateRandomComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -265,7 +266,7 @@ napi_value NapiRand::SetSeed(napi_env env, napi_callback_info info) env, nullptr, GetResourceName(env, "SetSeed"), SetSeedExecute, SetSeedComplete, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); if (context->asyncType == ASYNC_TYPE_PROMISE) { @@ -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; } @@ -338,7 +339,7 @@ napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info) napi_wrap( env, instance, randNapiObj, [](napi_env env, void *data, void *hint) { - NapiRand *rand = (NapiRand *)(data); + NapiRand *rand = static_cast(data); delete rand; return; }, diff --git a/frameworks/js/napi/src/napi_sign.cpp b/frameworks/js/napi/crypto/src/napi_sign.cpp similarity index 91% rename from frameworks/js/napi/src/napi_sign.cpp rename to frameworks/js/napi/crypto/src/napi_sign.cpp index 508671d7986ab750385625cce396e3e533ddd5c1..0b29f75f71af654ee617ebf75d54f1159035cc38 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)); } } @@ -441,7 +446,7 @@ static napi_value NewSignJsInitAsyncWork(napi_env env, SignInitCtx *ctx) SignJsInitAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -469,7 +474,7 @@ static napi_value NewSignJsUpdateAsyncWork(napi_env env, SignUpdateCtx *ctx) SignJsUpdateAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -497,7 +502,7 @@ static napi_value NewSignJsDoFinalAsyncWork(napi_env env, SignDoFinalCtx *ctx) SignJsDoFinalAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -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."); @@ -625,7 +630,7 @@ napi_value NapiSign::CreateJsSign(napi_env env, napi_callback_info info) napi_wrap( env, instance, napiSign, [](napi_env env, void *data, void *hint) { - NapiSign *napiSign = (NapiSign *)(data); + NapiSign *napiSign = static_cast(data); delete napiSign; return; }, 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 842a9bad049ca296752ef4c08d543eb2fc4f4779..3aa64e5e057137f8925e675ae057ffc4e89d19ea 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 91% rename from frameworks/js/napi/src/napi_sym_key_generator.cpp rename to frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp index 41540cc1506d9ae17702f01f453a5582dbac21a9..1d2b862a9478b38262543b71dfe0a358e2e68707 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!"); @@ -262,7 +266,7 @@ static napi_value NewConvertKeyAsyncWork(napi_env env, SymKeyGeneratorFwkCtx con AsyncKeyReturn(env, status, data); return; }, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -290,7 +294,7 @@ static napi_value NewGenKeyAsyncWork(napi_env env, SymKeyGeneratorFwkCtx context AsyncKeyReturn(env, status, data); return; }, - (void *)context, + static_cast(context), &context->asyncWork); napi_queue_async_work(env, context->asyncWork); @@ -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,12 +432,12 @@ 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); napi_value instance = nullptr; - napi_create_string_utf8(env, (const char *)algo, NAPI_AUTO_LENGTH, &instance); + napi_create_string_utf8(env, algo, NAPI_AUTO_LENGTH, &instance); return instance; } diff --git a/frameworks/js/napi/src/napi_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp similarity index 85% rename from frameworks/js/napi/src/napi_utils.cpp rename to frameworks/js/napi/crypto/src/napi_utils.cpp index 2d4d5d468db5e00e9d0b9faea95b200996124855..416fa3be1e72736fd029f1ec4f86d16dd157f1d0 100644 --- a/frameworks/js/napi/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -52,7 +52,7 @@ napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array) return nullptr; } for (uint32_t i = 0; i < array->count; i++) { - HcfBlob *blob = (HcfBlob *)(array->data + i); + HcfBlob *blob = reinterpret_cast(array->data + i); napi_value outBuffer = GenerateArrayBuffer(env, blob->data, blob->len); if (outBuffer == nullptr) { LOGE("generate array buffer failed!"); @@ -70,7 +70,7 @@ napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array) napi_value GenerateArrayBuffer(napi_env env, uint8_t *data, uint32_t size) { - uint8_t *buffer = (uint8_t *)HcfMalloc(size, 0); + uint8_t *buffer = static_cast(HcfMalloc(size, 0)); if (buffer == nullptr) { LOGE("malloc uint8 array buffer failed!"); return nullptr; @@ -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; } @@ -119,7 +119,7 @@ static bool GetDataOfEncodingBlob(napi_env env, napi_value data, HcfEncodingBlob LOGE("input data length is 0"); return false; } - encodingBlob->data = (uint8_t *)HcfMalloc(length, 0); + encodingBlob->data = static_cast(HcfMalloc(length, 0)); if (encodingBlob->data == nullptr) { LOGE("malloc encoding blob data failed!"); return false; @@ -136,7 +136,7 @@ static bool GetDataOfEncodingBlob(napi_env env, napi_value data, HcfEncodingBlob bool GetEncodingBlobFromValue(napi_env env, napi_value obj, HcfEncodingBlob **encodingBlob) { - *encodingBlob = (HcfEncodingBlob *)HcfMalloc(sizeof(HcfEncodingBlob), 0); + *encodingBlob = static_cast(HcfMalloc(sizeof(HcfEncodingBlob), 0)); if (*encodingBlob == nullptr) { LOGE("malloc encoding blob failed!"); 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; @@ -217,13 +218,13 @@ HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg) return nullptr; } - HcfBlob *newBlob = (HcfBlob *)HcfMalloc(sizeof(HcfBlob), 0); - if (newBlob == NULL) { + HcfBlob *newBlob = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); + if (newBlob == nullptr) { LOGE("Failed to allocate newBlob memory!"); return nullptr; } newBlob->len = length; - newBlob->data = (uint8_t *)HcfMalloc(length, 0); + newBlob->data = static_cast(HcfMalloc(length, 0)); if (newBlob->data == nullptr) { LOGE("malloc blob data failed!"); HcfFree(newBlob); @@ -254,7 +255,7 @@ static const char *GetCcmParamsSpecType() return CCM_PARAMS_SPEC.c_str(); } -static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, string type) +static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, const string &type) { napi_value data = nullptr; HcfBlob *blob = nullptr; @@ -274,7 +275,7 @@ static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, string type) static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **paramsSpec) { - HcfIvParamsSpec *ivParamsSpec = (HcfIvParamsSpec *)HcfMalloc(sizeof(HcfIvParamsSpec), 0); + HcfIvParamsSpec *ivParamsSpec = reinterpret_cast(HcfMalloc(sizeof(HcfIvParamsSpec), 0)); if (ivParamsSpec == nullptr) { LOGE("ivParamsSpec malloc failed!"); return false; @@ -288,7 +289,7 @@ static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **params } ivParamsSpec->base.getType = GetIvParamsSpecType; ivParamsSpec->iv = *iv; - *paramsSpec = (HcfParamsSpec *)ivParamsSpec; + *paramsSpec = reinterpret_cast(ivParamsSpec); HcfFree(iv); return true; } @@ -319,7 +320,7 @@ static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfBlob authTag = {}; bool ret = false; - HcfGcmParamsSpec *gcmParamsSpec = (HcfGcmParamsSpec *)HcfMalloc(sizeof(HcfGcmParamsSpec), 0); + HcfGcmParamsSpec *gcmParamsSpec = reinterpret_cast(HcfMalloc(sizeof(HcfGcmParamsSpec), 0)); if (gcmParamsSpec == nullptr) { LOGE("gcmParamsSpec malloc failed!"); return false; @@ -338,7 +339,7 @@ static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, goto clearup; } } else if (opMode == ENCRYPT_MODE) { - authTag.data = (uint8_t *)HcfMalloc(GCM_AUTH_TAG_LEN, 0); + authTag.data = static_cast(HcfMalloc(GCM_AUTH_TAG_LEN, 0)); if (authTag.data == nullptr) { LOGE("get tag failed!"); goto clearup; @@ -352,7 +353,7 @@ static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, gcmParamsSpec->iv = *iv; gcmParamsSpec->aad = *aad; gcmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag; - *paramsSpec = (HcfParamsSpec *)gcmParamsSpec; + *paramsSpec = reinterpret_cast(gcmParamsSpec); ret = true; clearup: if (!ret) { @@ -393,7 +394,7 @@ static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, goto clearup; } } else if (opMode == ENCRYPT_MODE) { - authTag.data = (uint8_t *)HcfMalloc(CCM_AUTH_TAG_LEN, 0); + authTag.data = static_cast(HcfMalloc(CCM_AUTH_TAG_LEN, 0)); if (authTag.data == nullptr) { LOGE("get tag failed!"); goto clearup; @@ -406,7 +407,7 @@ static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, ccmParamsSpec->iv = *iv; ccmParamsSpec->aad = *aad; ccmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag; - *paramsSpec = (HcfParamsSpec *)ccmParamsSpec; + *paramsSpec = reinterpret_cast(ccmParamsSpec); ret = true; clearup: if (!ret) { @@ -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; } @@ -456,7 +457,7 @@ napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob) LOGE("Invalid blob!"); return nullptr; } - uint8_t *buffer = (uint8_t *)HcfMalloc(blob->len, 0); + uint8_t *buffer = static_cast(HcfMalloc(blob->len, 0)); if (buffer == nullptr) { LOGE("malloc uint8 array buffer failed!"); return nullptr; @@ -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; } @@ -512,7 +513,7 @@ static bool GetDataOfCertChain(napi_env env, napi_value data, HcfCertChainData * LOGE("input data length is 0"); return false; } - certChain->data = (uint8_t *)HcfMalloc(length, 0); + certChain->data = static_cast(HcfMalloc(length, 0)); if (certChain->data == nullptr) { LOGE("malloc cert chain data failed!"); return false; @@ -529,7 +530,7 @@ static bool GetDataOfCertChain(napi_env env, napi_value data, HcfCertChainData * bool GetCertChainFromValue(napi_env env, napi_value obj, HcfCertChainData **certChainData) { - *certChainData = (HcfCertChainData *)HcfMalloc(sizeof(HcfCertChainData), 0); + *certChainData = static_cast(HcfMalloc(sizeof(HcfCertChainData), 0)); if (*certChainData == nullptr) { LOGE("malloc certChainData failed!"); 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 91% rename from frameworks/js/napi/src/napi_verify.cpp rename to frameworks/js/napi/crypto/src/napi_verify.cpp index 08864172835eab689cd1a3a85c4d71d41be37c96..7a9fe14928ff71ebe167d84908cef709366bb3ef 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)); } } @@ -468,7 +471,7 @@ static napi_value NewVerifyJsInitAsyncWork(napi_env env, VerifyInitCtx *ctx) VerifyJsInitAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -496,7 +499,7 @@ static napi_value NewVerifyJsUpdateAsyncWork(napi_env env, VerifyUpdateCtx *ctx) VerifyJsUpdateAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -524,7 +527,7 @@ static napi_value NewVerifyJsDoFinalAsyncWork(napi_env env, VerifyDoFinalCtx *ct VerifyJsDoFinalAsyncWorkReturn(env, status, data); return; }, - (void *)ctx, + static_cast(ctx), &ctx->asyncWork); napi_queue_async_work(env, ctx->asyncWork); @@ -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."); @@ -653,7 +656,7 @@ napi_value NapiVerify::CreateJsVerify(napi_env env, napi_callback_info info) napi_wrap( env, instance, napiVerify, [](napi_env env, void *data, void *hint) { - NapiVerify *napiVerify = (NapiVerify *)(data); + NapiVerify *napiVerify = static_cast(data); delete napiVerify; return; }, diff --git a/frameworks/rand/rand.c b/frameworks/rand/rand.c index 4681bfac51534169286784d6818ae5b01a7c7842..aac2781608dafa36494a578d4afa9785a37bb36e 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 0000000000000000000000000000000000000000..ffca88f0761c17fea6e61637d48f455538bf6439 --- /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 69aab9025d880f6b6e6528612c3331ceacec4b0a..806a5f9d23792ef6e527988ba22e817858e065af 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;