From 1a01630cb6a24a7aa5e73a1f4edcd24a1e7722b0 Mon Sep 17 00:00:00 2001 From: hhhFun Date: Tue, 30 Jan 2024 14:16:12 +0800 Subject: [PATCH 1/2] support asymmetric key generator synchronization interface Signed-off-by: hhhFun --- .../napi/crypto/inc/napi_asy_key_generator.h | 2 + .../crypto/inc/napi_asy_key_spec_generator.h | 3 + .../crypto/src/napi_asy_key_generator.cpp | 118 ++++++++++++++ .../src/napi_asy_key_spec_generator.cpp | 152 ++++++++++++++++++ 4 files changed, 275 insertions(+) diff --git a/frameworks/js/napi/crypto/inc/napi_asy_key_generator.h b/frameworks/js/napi/crypto/inc/napi_asy_key_generator.h index 39923ae..4b87803 100644 --- a/frameworks/js/napi/crypto/inc/napi_asy_key_generator.h +++ b/frameworks/js/napi/crypto/inc/napi_asy_key_generator.h @@ -36,7 +36,9 @@ public: static napi_value CreateJsAsyKeyGenerator(napi_env env, napi_callback_info info); static napi_value JsGenerateKeyPair(napi_env env, napi_callback_info info); + static napi_value JsGenerateKeyPairSync(napi_env env, napi_callback_info info); static napi_value JsConvertKey(napi_env env, napi_callback_info info); + static napi_value JsConvertKeySync(napi_env env, napi_callback_info info); static thread_local napi_ref classRef_; diff --git a/frameworks/js/napi/crypto/inc/napi_asy_key_spec_generator.h b/frameworks/js/napi/crypto/inc/napi_asy_key_spec_generator.h index dd45e66..344022f 100644 --- a/frameworks/js/napi/crypto/inc/napi_asy_key_spec_generator.h +++ b/frameworks/js/napi/crypto/inc/napi_asy_key_spec_generator.h @@ -36,8 +36,11 @@ public: static napi_value CreateJsAsyKeyGeneratorBySpec(napi_env env, napi_callback_info info); static napi_value JsGenerateKeyPair(napi_env env, napi_callback_info info); + static napi_value JsGenerateKeyPairSync(napi_env env, napi_callback_info info); static napi_value JsGeneratePubKey(napi_env env, napi_callback_info info); + static napi_value JsGeneratePubKeySync(napi_env env, napi_callback_info info); static napi_value JsGeneratePriKey(napi_env env, napi_callback_info info); + static napi_value JsGeneratePriKeySync(napi_env env, napi_callback_info info); static napi_value JsGetAlgorithm(napi_env env, napi_callback_info info); static thread_local napi_ref classRef_; diff --git a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp index f42ea7c..db72d39 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -450,6 +450,62 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPair(napi_env env, napi_callback_in return NewGenKeyPairAsyncWork(env, ctx); } +napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t expectedArgc = PARAMS_NUM_ONE; + size_t argc = expectedArgc; + napi_value argv[PARAMS_NUM_ONE] = { nullptr }; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != expectedArgc - 1) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail.")); + LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc - 1, argc); + return nullptr; + } + + NapiAsyKeyGenerator *napiGenerator; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); + if (status != napi_ok || napiGenerator == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); + LOGE("failed to unwrap napi asyKeyGenerator obj."); + return nullptr; + } + + HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); + HcfParamsSpec *params = nullptr; + HcfKeyPair *returnKeyPair = nullptr; + + HcfResult errCode = generator->generateKeyPair(generator, params, &returnKeyPair); + if (errCode != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); + LOGE("generate key pair fail."); + return nullptr; + } + + napi_value instance = nullptr; + NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); + if (napiKeyPair == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); + LOGE("new napi key pair failed"); + return nullptr; + } + instance = napiKeyPair->ConvertToJsKeyPair(env); + + napi_status ret = napi_wrap( + env, instance, napiKeyPair, + [](napi_env env, void *data, void *hint) { + NapiKeyPair *keyPair = static_cast(data); + delete keyPair; + return; + }, nullptr, nullptr); + if (ret != napi_ok) { + LOGE("failed to wrap napiKeyPair obj!"); + delete napiKeyPair; + } + + return instance; +} + napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info info) { ConvertKeyCtx *ctx = static_cast(HcfMalloc(sizeof(ConvertKeyCtx), 0)); @@ -469,6 +525,66 @@ napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info in return NewConvertKeyAsyncWork(env, ctx); } +napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t expectedArgc = PARAMS_NUM_THREE; + size_t argc = expectedArgc; + napi_value argv[PARAMS_NUM_THREE] = { nullptr }; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != expectedArgc - 1) { + LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc - 1, argc); + return nullptr; + } + + NapiAsyKeyGenerator *napiGenerator; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); + if (status != napi_ok || napiGenerator == nullptr) { + LOGE("failed to unwrap napi asyKeyGenerator obj."); + return nullptr; + } + + HcfBlob *pubKey = nullptr; + HcfBlob *priKey = nullptr; + if (!GetPkAndSkBlobFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], &pubKey, &priKey)) { + return nullptr; + } + + HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); + HcfParamsSpec *params = nullptr; + HcfKeyPair *returnKeyPair = nullptr; + + HcfResult errCode = generator->convertKey(generator, params, pubKey, priKey, &(returnKeyPair)); + if (errCode != HCF_SUCCESS) { + LOGE("convert key fail."); + } + + napi_value instance = nullptr; + NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); + if (napiKeyPair == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); + LOGE("new napi key pair failed"); + return nullptr; + } + instance = napiKeyPair->ConvertToJsKeyPair(env); + + napi_status ret = napi_wrap( + env, instance, napiKeyPair, + [](napi_env env, void *data, void *hint) { + NapiKeyPair *keyPair = static_cast(data); + delete keyPair; + return; + }, nullptr, nullptr); + if (ret != napi_ok) { + LOGE("failed to wrap napiKeyPair obj!"); + errCode = HCF_INVALID_PARAMS; + delete napiKeyPair; + return nullptr; + } + + return instance; +} + napi_value NapiAsyKeyGenerator::AsyKeyGeneratorConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -552,7 +668,9 @@ void NapiAsyKeyGenerator::DefineAsyKeyGeneratorJSClass(napi_env env, napi_value napi_property_descriptor classDesc[] = { DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGenerator::JsGenerateKeyPair), + DECLARE_NAPI_FUNCTION("generateKeyPairSync", NapiAsyKeyGenerator::JsGenerateKeyPairSync), DECLARE_NAPI_FUNCTION("convertKey", NapiAsyKeyGenerator::JsConvertKey), + DECLARE_NAPI_FUNCTION("convertKeySync", NapiAsyKeyGenerator::JsConvertKeySync), }; napi_value constructor = nullptr; napi_define_class(env, "AsyKeyGenerator", NAPI_AUTO_LENGTH, NapiAsyKeyGenerator::AsyKeyGeneratorConstructor, diff --git a/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp index 525b012..a2d512c 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp @@ -93,6 +93,28 @@ static bool BuildAsyKeyCtx(napi_env env, napi_callback_info info, AsyKeyCtx *ctx } } +static bool BuildHcfAsyKeyGeneratorBySpec(napi_env env, napi_callback_info info, HcfAsyKeyGeneratorBySpec *generator) +{ + napi_value thisVar = nullptr; + size_t expectedArgc = PARAMS_NUM_ONE; + size_t argc = expectedArgc; + napi_value argv[PARAMS_NUM_ONE] = { nullptr }; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != expectedArgc - 1) { + LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc - 1, argc); + return false; + } + + NapiAsyKeyGeneratorBySpec *napiGenerator; + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); + if (status != napi_ok || napiGenerator == nullptr) { + LOGE("failed to unwrap napi asyKeyGenerator obj."); + return false; + } + generator = napiGenerator->GetAsyKeyGeneratorBySpec(); + return true; +} + static void ReturnAsyKeyCallbackResult(napi_env env, AsyKeyCtx *ctx, napi_value result) { napi_value businessError = nullptr; @@ -382,6 +404,47 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPair(napi_env env, napi_callb return NewGenKeyPairAsyncWork(env, ctx); } +napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync(napi_env env, napi_callback_info info) +{ + HcfAsyKeyGeneratorBySpec *generator = nullptr; + if (!BuildHcfAsyKeyGeneratorBySpec(env, info, generator) || generator == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); + LOGE("build generator fail."); + return nullptr; + } + HcfKeyPair *returnKeyPair = nullptr; + + HcfResult errCode = generator->generateKeyPair(generator, &(returnKeyPair)); + if (errCode != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); + LOGE("generate key pair fail."); + return nullptr; + } + + napi_value instance = nullptr; + NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); + if (napiKeyPair == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); + LOGE("new napi key pair failed"); + return nullptr; + } + instance = napiKeyPair->ConvertToJsKeyPair(env); + + napi_status ret = napi_wrap( + env, instance, napiKeyPair, + [](napi_env env, void *data, void *hint) { + NapiKeyPair *keyPair = static_cast(data); + delete keyPair; + return; + }, nullptr, nullptr); + if (ret != napi_ok) { + LOGE("failed to wrap napiKeyPair obj!"); + errCode = HCF_INVALID_PARAMS; + delete napiKeyPair; + } + return instance; +} + napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKey(napi_env env, napi_callback_info info) { AsyKeyCtx *ctx = static_cast(HcfMalloc(sizeof(AsyKeyCtx), 0)); @@ -401,6 +464,49 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKey(napi_env env, napi_callba return NewPubKeyAsyncWork(env, ctx); } +napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync(napi_env env, napi_callback_info info) +{ + HcfAsyKeyGeneratorBySpec *generator = nullptr; + if (!BuildHcfAsyKeyGeneratorBySpec(env, info, generator) || generator == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); + LOGE("build generator fail."); + return nullptr; + } + HcfPubKey *returnPubKey = nullptr; + + HcfResult errCode = generator->generatePubKey(generator, &(returnPubKey)); + if (errCode != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate PubKey fail.")); + LOGE("generate PubKey fail."); + return nullptr; + } + + napi_value instance = nullptr; + NapiPubKey *napiPubKey = new (std::nothrow) NapiPubKey(returnPubKey); + if (napiPubKey == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi pub key failed!")); + LOGE("new napi pub key failed"); + return nullptr; + } + instance = napiPubKey->ConvertToJsPubKey(env); + + napi_status ret = napi_wrap( + env, instance, napiPubKey, + [](napi_env env, void *data, void *hint) { + NapiPubKey *napiPubKey = static_cast(data); + HcfObjDestroy(napiPubKey->GetPubKey()); + delete napiPubKey; + return; + }, nullptr, nullptr); + if (ret != napi_ok) { + LOGE("failed to wrap napiPubKey obj!"); + errCode = HCF_INVALID_PARAMS; + delete napiPubKey; + } + + return instance; +} + napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKey(napi_env env, napi_callback_info info) { AsyKeyCtx *ctx = static_cast(HcfMalloc(sizeof(AsyKeyCtx), 0)); @@ -420,6 +526,49 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKey(napi_env env, napi_callba return NewPriKeyAsyncWork(env, ctx); } +napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync(napi_env env, napi_callback_info info) +{ + HcfAsyKeyGeneratorBySpec *generator = nullptr; + if (!BuildHcfAsyKeyGeneratorBySpec(env, info, generator) || generator == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); + LOGE("build generator fail."); + return nullptr; + } + HcfPriKey *returnPriKey = nullptr; + + HcfResult errCode = generator->generatePriKey(generator, &(returnPriKey)); + if (errCode != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate PriKey fail.")); + LOGE("generate PriKey fail."); + return nullptr; + } + + napi_value instance = nullptr; + NapiPriKey *napiPriKey = new (std::nothrow) NapiPriKey(returnPriKey); + if (napiPriKey == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi pri key failed!")); + LOGE("new napi pri key failed"); + return nullptr; + } + instance = napiPriKey->ConvertToJsPriKey(env); + + napi_status ret = napi_wrap( + env, instance, napiPriKey, + [](napi_env env, void *data, void *hint) { + NapiPriKey *napiPriKey = static_cast(data); + HcfObjDestroy(napiPriKey->GetPriKey()); + delete napiPriKey; + return; + }, nullptr, nullptr); + if (ret != napi_ok) { + LOGE("failed to wrap napiPriKey obj!"); + errCode = HCF_INVALID_PARAMS; + delete napiPriKey; + } + + return instance; +} + napi_value NapiAsyKeyGeneratorBySpec::AsyKeyGeneratorBySpecConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -517,8 +666,11 @@ void NapiAsyKeyGeneratorBySpec::DefineAsyKeyGeneratorBySpecJSClass(napi_env env, napi_property_descriptor classDesc[] = { DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPair), + DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync), DECLARE_NAPI_FUNCTION("generatePriKey", NapiAsyKeyGeneratorBySpec::JsGeneratePriKey), + DECLARE_NAPI_FUNCTION("generatePriKey", NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync), DECLARE_NAPI_FUNCTION("generatePubKey", NapiAsyKeyGeneratorBySpec::JsGeneratePubKey), + DECLARE_NAPI_FUNCTION("generatePubKey", NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync), { .utf8name = "algName", .getter = NapiAsyKeyGeneratorBySpec::JsGetAlgorithm }, }; napi_value constructor = nullptr; -- Gitee From f7255f3a1c5c7bf97232b80be8fee5c5cfadbe8a Mon Sep 17 00:00:00 2001 From: hhhFun Date: Fri, 8 Mar 2024 09:19:24 +0800 Subject: [PATCH 2/2] fix issues Signed-off-by: hhhFun --- .../crypto/src/napi_asy_key_generator.cpp | 125 ++++++++++-------- .../src/napi_asy_key_spec_generator.cpp | 66 ++++----- 2 files changed, 104 insertions(+), 87 deletions(-) diff --git a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp index db72d39..c3bfd57 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -450,58 +450,67 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPair(napi_env env, napi_callback_in return NewGenKeyPairAsyncWork(env, ctx); } +static bool GetHcfKeyPairInstance(napi_env env, HcfKeyPair *returnKeyPair, napi_value *instance) +{ + NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); + if (napiKeyPair == nullptr) { + HcfObjDestroy(returnKeyPair); + LOGE("new napi key pair failed"); + return false; + } + + *instance = napiKeyPair->ConvertToJsKeyPair(env); + napi_status ret = napi_wrap( + env, *instance, napiKeyPair, + [](napi_env env, void *data, void *hint) { + NapiKeyPair *keyPair = static_cast(data); + delete keyPair; + return; + }, nullptr, nullptr); + if (ret != napi_ok) { + LOGE("failed to wrap napiKeyPair obj!"); + delete napiKeyPair; + return false; + } + + return true; +} + napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; - size_t expectedArgc = PARAMS_NUM_ONE; - size_t argc = expectedArgc; - napi_value argv[PARAMS_NUM_ONE] = { nullptr }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (argc != expectedArgc - 1) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail.")); - LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc - 1, argc); - return nullptr; - } + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - NapiAsyKeyGenerator *napiGenerator; + NapiAsyKeyGenerator *napiGenerator = nullptr; napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok || napiGenerator == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); LOGE("failed to unwrap napi asyKeyGenerator obj."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); return nullptr; } HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); + if (generator == nullptr) { + LOGE("get generator fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get generator fail!")); + return nullptr; + } + HcfParamsSpec *params = nullptr; HcfKeyPair *returnKeyPair = nullptr; - HcfResult errCode = generator->generateKeyPair(generator, params, &returnKeyPair); if (errCode != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); LOGE("generate key pair fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); return nullptr; } napi_value instance = nullptr; - NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); - if (napiKeyPair == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); - LOGE("new napi key pair failed"); + if (GetHcfKeyPairInstance(env, returnKeyPair, &instance) != napi_ok) { + LOGE("failed to get generate key pair instance obj!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get generate key pair instance obj!")); return nullptr; } - instance = napiKeyPair->ConvertToJsKeyPair(env); - - napi_status ret = napi_wrap( - env, instance, napiKeyPair, - [](napi_env env, void *data, void *hint) { - NapiKeyPair *keyPair = static_cast(data); - delete keyPair; - return; - }, nullptr, nullptr); - if (ret != napi_ok) { - LOGE("failed to wrap napiKeyPair obj!"); - delete napiKeyPair; - } return instance; } @@ -525,60 +534,64 @@ napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info in return NewConvertKeyAsyncWork(env, ctx); } +static void HcfFreePubKeyAndPriKey(HcfBlob *pubKey, HcfBlob *priKey) +{ + HcfBlobDataFree(pubKey); + HcfFree(pubKey); + HcfBlobDataFree(priKey); + HcfFree(priKey); +} + napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; - size_t expectedArgc = PARAMS_NUM_THREE; - size_t argc = expectedArgc; - napi_value argv[PARAMS_NUM_THREE] = { nullptr }; + size_t argc = PARAMS_NUM_TWO; + napi_value argv[PARAMS_NUM_TWO] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (argc != expectedArgc - 1) { - LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc - 1, argc); + if (argc != PARAMS_NUM_TWO) { + LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", PARAMS_NUM_TWO, argc); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "wrong argument num.")); return nullptr; } - NapiAsyKeyGenerator *napiGenerator; + NapiAsyKeyGenerator *napiGenerator = nullptr; napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok || napiGenerator == nullptr) { LOGE("failed to unwrap napi asyKeyGenerator obj."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); return nullptr; } HcfBlob *pubKey = nullptr; HcfBlob *priKey = nullptr; if (!GetPkAndSkBlobFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], &pubKey, &priKey)) { + LOGE("failed to unwrap napi asyKeyGenerator obj."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); return nullptr; } HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); + if (generator == nullptr) { + HcfFreePubKeyAndPriKey(pubKey, priKey); + LOGE("get generator fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get generator fail!")); + return nullptr; + } + HcfParamsSpec *params = nullptr; HcfKeyPair *returnKeyPair = nullptr; - HcfResult errCode = generator->convertKey(generator, params, pubKey, priKey, &(returnKeyPair)); + HcfFreePubKeyAndPriKey(pubKey, priKey); if (errCode != HCF_SUCCESS) { LOGE("convert key fail."); - } - - napi_value instance = nullptr; - NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); - if (napiKeyPair == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); - LOGE("new napi key pair failed"); + napi_throw(env, GenerateBusinessError(env, errCode, "convert key fail.")); return nullptr; } - instance = napiKeyPair->ConvertToJsKeyPair(env); - napi_status ret = napi_wrap( - env, instance, napiKeyPair, - [](napi_env env, void *data, void *hint) { - NapiKeyPair *keyPair = static_cast(data); - delete keyPair; - return; - }, nullptr, nullptr); - if (ret != napi_ok) { - LOGE("failed to wrap napiKeyPair obj!"); - errCode = HCF_INVALID_PARAMS; - delete napiKeyPair; + napi_value instance = nullptr; + if (GetHcfKeyPairInstance(env, returnKeyPair, &instance) != napi_ok) { + LOGE("failed to get convert key instance obj!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get convert key instance obj!")); return nullptr; } diff --git a/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp index a2d512c..33fec79 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp @@ -93,17 +93,10 @@ static bool BuildAsyKeyCtx(napi_env env, napi_callback_info info, AsyKeyCtx *ctx } } -static bool BuildHcfAsyKeyGeneratorBySpec(napi_env env, napi_callback_info info, HcfAsyKeyGeneratorBySpec *generator) +static bool GetAsyKeyGenerator(napi_env env, napi_callback_info info, HcfAsyKeyGeneratorBySpec **generator) { napi_value thisVar = nullptr; - size_t expectedArgc = PARAMS_NUM_ONE; - size_t argc = expectedArgc; - napi_value argv[PARAMS_NUM_ONE] = { nullptr }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (argc != expectedArgc - 1) { - LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc - 1, argc); - return false; - } + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); NapiAsyKeyGeneratorBySpec *napiGenerator; napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); @@ -111,7 +104,7 @@ static bool BuildHcfAsyKeyGeneratorBySpec(napi_env env, napi_callback_info info, LOGE("failed to unwrap napi asyKeyGenerator obj."); return false; } - generator = napiGenerator->GetAsyKeyGeneratorBySpec(); + *generator = napiGenerator->GetAsyKeyGeneratorBySpec(); return true; } @@ -407,29 +400,30 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPair(napi_env env, napi_callb napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync(napi_env env, napi_callback_info info) { HcfAsyKeyGeneratorBySpec *generator = nullptr; - if (!BuildHcfAsyKeyGeneratorBySpec(env, info, generator) || generator == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); + if (!GetAsyKeyGenerator(env, info, &generator) || generator == nullptr) { LOGE("build generator fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); return nullptr; } - HcfKeyPair *returnKeyPair = nullptr; + HcfKeyPair *returnKeyPair = nullptr; HcfResult errCode = generator->generateKeyPair(generator, &(returnKeyPair)); if (errCode != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); LOGE("generate key pair fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); return nullptr; } napi_value instance = nullptr; NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); if (napiKeyPair == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); + HcfObjDestroy(returnKeyPair); LOGE("new napi key pair failed"); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!")); return nullptr; } - instance = napiKeyPair->ConvertToJsKeyPair(env); + instance = napiKeyPair->ConvertToJsKeyPair(env); napi_status ret = napi_wrap( env, instance, napiKeyPair, [](napi_env env, void *data, void *hint) { @@ -441,6 +435,8 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync(napi_env env, napi_c LOGE("failed to wrap napiKeyPair obj!"); errCode = HCF_INVALID_PARAMS; delete napiKeyPair; + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiKeyPair obj!")); + return nullptr; } return instance; } @@ -467,29 +463,30 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKey(napi_env env, napi_callba napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync(napi_env env, napi_callback_info info) { HcfAsyKeyGeneratorBySpec *generator = nullptr; - if (!BuildHcfAsyKeyGeneratorBySpec(env, info, generator) || generator == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); + if (!GetAsyKeyGenerator(env, info, &generator) || generator == nullptr) { LOGE("build generator fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); return nullptr; } - HcfPubKey *returnPubKey = nullptr; + HcfPubKey *returnPubKey = nullptr; HcfResult errCode = generator->generatePubKey(generator, &(returnPubKey)); if (errCode != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate PubKey fail.")); LOGE("generate PubKey fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate PubKey fail.")); return nullptr; } napi_value instance = nullptr; NapiPubKey *napiPubKey = new (std::nothrow) NapiPubKey(returnPubKey); if (napiPubKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi pub key failed!")); + HcfObjDestroy(returnPubKey); LOGE("new napi pub key failed"); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi pub key failed!")); return nullptr; } - instance = napiPubKey->ConvertToJsPubKey(env); + instance = napiPubKey->ConvertToJsPubKey(env); napi_status ret = napi_wrap( env, instance, napiPubKey, [](napi_env env, void *data, void *hint) { @@ -501,7 +498,10 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync(napi_env env, napi_ca if (ret != napi_ok) { LOGE("failed to wrap napiPubKey obj!"); errCode = HCF_INVALID_PARAMS; + HcfObjDestroy(napiPubKey->GetPubKey()); delete napiPubKey; + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiPubKey obj!")); + return nullptr; } return instance; @@ -529,29 +529,30 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKey(napi_env env, napi_callba napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync(napi_env env, napi_callback_info info) { HcfAsyKeyGeneratorBySpec *generator = nullptr; - if (!BuildHcfAsyKeyGeneratorBySpec(env, info, generator) || generator == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); + if (!GetAsyKeyGenerator(env, info, &generator) || generator == nullptr) { LOGE("build generator fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build generator fail!")); return nullptr; } - HcfPriKey *returnPriKey = nullptr; + HcfPriKey *returnPriKey = nullptr; HcfResult errCode = generator->generatePriKey(generator, &(returnPriKey)); if (errCode != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate PriKey fail.")); LOGE("generate PriKey fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate PriKey fail.")); return nullptr; } napi_value instance = nullptr; NapiPriKey *napiPriKey = new (std::nothrow) NapiPriKey(returnPriKey); if (napiPriKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi pri key failed!")); + HcfObjDestroy(returnPriKey); LOGE("new napi pri key failed"); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi pri key failed!")); return nullptr; } - instance = napiPriKey->ConvertToJsPriKey(env); + instance = napiPriKey->ConvertToJsPriKey(env); napi_status ret = napi_wrap( env, instance, napiPriKey, [](napi_env env, void *data, void *hint) { @@ -563,7 +564,10 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync(napi_env env, napi_ca if (ret != napi_ok) { LOGE("failed to wrap napiPriKey obj!"); errCode = HCF_INVALID_PARAMS; + HcfObjDestroy(napiPriKey->GetPriKey()); delete napiPriKey; + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiPriKey obj!")); + return nullptr; } return instance; @@ -666,11 +670,11 @@ void NapiAsyKeyGeneratorBySpec::DefineAsyKeyGeneratorBySpecJSClass(napi_env env, napi_property_descriptor classDesc[] = { DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPair), - DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync), + DECLARE_NAPI_FUNCTION("generateKeyPairSync", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync), DECLARE_NAPI_FUNCTION("generatePriKey", NapiAsyKeyGeneratorBySpec::JsGeneratePriKey), - DECLARE_NAPI_FUNCTION("generatePriKey", NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync), + DECLARE_NAPI_FUNCTION("generatePriKeySync", NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync), DECLARE_NAPI_FUNCTION("generatePubKey", NapiAsyKeyGeneratorBySpec::JsGeneratePubKey), - DECLARE_NAPI_FUNCTION("generatePubKey", NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync), + DECLARE_NAPI_FUNCTION("generatePubKeySync", NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync), { .utf8name = "algName", .getter = NapiAsyKeyGeneratorBySpec::JsGetAlgorithm }, }; napi_value constructor = nullptr; -- Gitee