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 39923aec188a41d6ac226c92ba439a2fc34ff3d6..4b87803d42b60e102773770b26c167f8de19b896 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 dd45e66155b93359cc0d9f131b44f61469681ced..344022f447b88e4fc5d4a63f598b142bb8218648 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 f42ea7cba647d8e26a0e7083a34a7067dc39ff2a..88aa3cdd064f46729b59a34612bd15418dfe2fb5 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,71 @@ 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; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + + 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; + } + + 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) { + LOGE("generate key pair fail."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "generate key pair fail.")); + return nullptr; + } + + napi_value instance = nullptr; + if (!GetHcfKeyPairInstance(env, returnKeyPair, &instance)) { + LOGE("failed to get generate key pair instance!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get generate key pair instance!")); + return nullptr; + } + + return instance; +} + napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info info) { ConvertKeyCtx *ctx = static_cast(HcfMalloc(sizeof(ConvertKeyCtx), 0)); @@ -469,6 +534,70 @@ 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 argc = PARAMS_NUM_TWO; + napi_value argv[PARAMS_NUM_TWO] = { nullptr }; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + 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 = 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_throw(env, GenerateBusinessError(env, errCode, "convert key fail.")); + return nullptr; + } + + napi_value instance = nullptr; + if (!GetHcfKeyPairInstance(env, returnKeyPair, &instance)) { + LOGE("failed to get convert key instance!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get convert key instance!")); + return nullptr; + } + + return instance; +} + napi_value NapiAsyKeyGenerator::AsyKeyGeneratorConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -552,7 +681,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 525b01292647a05e388918d98e0412f512ed64ad..33fec798c21c7cd82e86262ba80afd6dec577961 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,21 @@ static bool BuildAsyKeyCtx(napi_env env, napi_callback_info info, AsyKeyCtx *ctx } } +static bool GetAsyKeyGenerator(napi_env env, napi_callback_info info, HcfAsyKeyGeneratorBySpec **generator) +{ + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + + 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 +397,50 @@ 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 (!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; + HcfResult errCode = generator->generateKeyPair(generator, &(returnKeyPair)); + if (errCode != HCF_SUCCESS) { + 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) { + 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); + 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_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiKeyPair obj!")); + return nullptr; + } + return instance; +} + napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKey(napi_env env, napi_callback_info info) { AsyKeyCtx *ctx = static_cast(HcfMalloc(sizeof(AsyKeyCtx), 0)); @@ -401,6 +460,53 @@ 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 (!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; + HcfResult errCode = generator->generatePubKey(generator, &(returnPubKey)); + if (errCode != HCF_SUCCESS) { + 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) { + 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); + 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; + HcfObjDestroy(napiPubKey->GetPubKey()); + delete napiPubKey; + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiPubKey obj!")); + return nullptr; + } + + return instance; +} + napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKey(napi_env env, napi_callback_info info) { AsyKeyCtx *ctx = static_cast(HcfMalloc(sizeof(AsyKeyCtx), 0)); @@ -420,6 +526,53 @@ 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 (!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; + HcfResult errCode = generator->generatePriKey(generator, &(returnPriKey)); + if (errCode != HCF_SUCCESS) { + 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) { + 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); + 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; + HcfObjDestroy(napiPriKey->GetPriKey()); + delete napiPriKey; + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiPriKey obj!")); + return nullptr; + } + + return instance; +} + napi_value NapiAsyKeyGeneratorBySpec::AsyKeyGeneratorBySpecConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -517,8 +670,11 @@ void NapiAsyKeyGeneratorBySpec::DefineAsyKeyGeneratorBySpecJSClass(napi_env env, napi_property_descriptor classDesc[] = { DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPair), + DECLARE_NAPI_FUNCTION("generateKeyPairSync", NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync), DECLARE_NAPI_FUNCTION("generatePriKey", NapiAsyKeyGeneratorBySpec::JsGeneratePriKey), + DECLARE_NAPI_FUNCTION("generatePriKeySync", NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync), DECLARE_NAPI_FUNCTION("generatePubKey", NapiAsyKeyGeneratorBySpec::JsGeneratePubKey), + DECLARE_NAPI_FUNCTION("generatePubKeySync", NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync), { .utf8name = "algName", .getter = NapiAsyKeyGeneratorBySpec::JsGetAlgorithm }, }; napi_value constructor = nullptr;