From e1af939047bda1e9cc8462792873d66ca1d621a8 Mon Sep 17 00:00:00 2001 From: jing-wang177 Date: Tue, 19 Aug 2025 17:56:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?string=E7=B1=BB=E5=9E=8B=E5=AD=98=E8=B4=AE?= =?UTF-8?q?=E7=A7=81=E9=92=A5=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jing-wang177 --- frameworks/js/napi/crypto/inc/napi_utils.h | 2 + .../crypto/src/napi_asy_key_generator.cpp | 95 +++++++++++-------- frameworks/js/napi/crypto/src/napi_utils.cpp | 2 +- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/frameworks/js/napi/crypto/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h index 53cec12..79ff66b 100644 --- a/frameworks/js/napi/crypto/inc/napi_utils.h +++ b/frameworks/js/napi/crypto/inc/napi_utils.h @@ -89,6 +89,8 @@ HcfResult GetNapiUint8ArrayDataNoCopy(napi_env env, napi_value arg, HcfBlob *blo HcfResult CreateNapiUint8ArrayNoCopy(napi_env env, HcfBlob *blob, napi_value *napiValue); +HcfBlob *GetBlobFromStringJSParams(napi_env env, napi_value arg); + } // namespace CryptoFramework } // namespace OHOS #endif 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 bec9049..54ebba2 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -75,8 +75,8 @@ struct ConvertPemKeyCtx { HcfAsyKeyGenerator *generator = nullptr; HcfParamsSpec *params = nullptr; - std::string pubKey = ""; - std::string priKey = ""; + HcfBlob *pubKey = nullptr; + HcfBlob *priKey = nullptr; HcfResult errCode = HCF_SUCCESS; const char *errMsg = nullptr; @@ -172,8 +172,16 @@ static void FreeConvertPemKeyCtx(napi_env env, ConvertPemKeyCtx *ctx) FreeDecodeParamsSpec(ctx->params); ctx->errMsg = nullptr; - ctx->pubKey = ""; - ctx->priKey = ""; + if (ctx->pubKey != nullptr) { + HcfBlobDataFree(ctx->pubKey); + HcfFree(ctx->pubKey); + ctx->pubKey = nullptr; + } + if (ctx->priKey != nullptr) { + HcfBlobDataClearAndFree(ctx->priKey); + HcfFree(ctx->priKey); + ctx->priKey = nullptr; + } HcfFree(ctx); } @@ -248,9 +256,8 @@ static bool GetPkAndSkBlobFromNapiValueIfInput(napi_env env, napi_value pkValue, } static bool GetPkAndSkStringFromNapiValueIfInput(napi_env env, napi_value pkValue, napi_value skValue, - std::string &returnPubKey, std::string &returnPriKey) + HcfBlob **returnPubKey, HcfBlob **returnPriKey) { - size_t length = 0; napi_valuetype valueTypePk; napi_valuetype valueTypeSk; napi_typeof(env, pkValue, &valueTypePk); @@ -259,38 +266,27 @@ static bool GetPkAndSkStringFromNapiValueIfInput(napi_env env, napi_value pkValu LOGE("valueTypePk and valueTypeSk is all null."); return false; } + HcfBlob *pubKey = nullptr; if (valueTypePk != napi_null) { - if (valueTypePk != napi_string) { - LOGE("valueTypePk wrong argument type, expect string type."); - return false; - } - if (napi_get_value_string_utf8(env, pkValue, nullptr, 0, &length) != napi_ok) { - LOGE("pkValue can not get string length."); - return false; - } - returnPubKey.reserve(length + 1); - returnPubKey.resize(length); - if (napi_get_value_string_utf8(env, pkValue, returnPubKey.data(), (length + 1), &length) != napi_ok) { - LOGE("pkValue can not get string value."); + pubKey = GetBlobFromStringJSParams(env, pkValue); + if (pubKey == nullptr) { + LOGE("GetBlobFromStringJSParams failed for pubKey."); return false; } } + HcfBlob *priKey = nullptr; if (valueTypeSk != napi_null) { - if (valueTypeSk != napi_string) { - LOGE("valueTypeSk wrong argument type. expect string type."); - return false; - } - if (napi_get_value_string_utf8(env, skValue, nullptr, 0, &length) != napi_ok) { - LOGE("skValue can not get string length."); - return false; - } - returnPriKey.reserve(length + 1); - returnPriKey.resize(length); - if (napi_get_value_string_utf8(env, skValue, returnPriKey.data(), (length + 1), &length) != napi_ok) { - LOGE("skValue can not get string value."); + priKey = GetBlobFromStringJSParams(env, skValue); + if (priKey == nullptr) { + HcfBlobDataFree(pubKey); + HcfFree(pubKey); + pubKey = nullptr; + LOGE("GetBlobFromStringJSParams failed for priKey."); return false; } } + *returnPubKey = pubKey; + *returnPriKey = priKey; return true; } @@ -339,7 +335,7 @@ static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKey } } -static bool ValidateAndGetParams(napi_env env, napi_callback_info info, std::string &pubKey, std::string &priKey, +static bool ValidateAndGetParams(napi_env env, napi_callback_info info, HcfBlob **pubKey, HcfBlob **priKey, HcfParamsSpec **paramsSpec) { napi_value thisVar = nullptr; @@ -379,10 +375,10 @@ static bool BuildConvertPemKeyCtx(napi_env env, napi_callback_info info, Convert LOGE("failed to unwrap napi asyKeyGenerator obj."); return false; } - std::string pubKey; - std::string priKey; + HcfBlob *pubKey = nullptr; + HcfBlob *priKey = nullptr; HcfParamsSpec *paramsSpec = nullptr; - if (!ValidateAndGetParams(env, info, pubKey, priKey, ¶msSpec)) { + if (!ValidateAndGetParams(env, info, &pubKey, &priKey, ¶msSpec)) { return false; } @@ -528,8 +524,16 @@ static void ConvertKeyAsyncWorkProcess(napi_env env, void *data) static void ConvertPemKeyAsyncWorkProcess(napi_env env, void *data) { ConvertPemKeyCtx *ctx = static_cast(data); + const char *pubKeyStr = nullptr; + const char *priKeyStr = nullptr; + if (ctx->pubKey != nullptr) { + pubKeyStr = reinterpret_cast(ctx->pubKey->data); + } + if (ctx->priKey != nullptr) { + priKeyStr = reinterpret_cast(ctx->priKey->data); + } ctx->errCode = ctx->generator->convertPemKey(ctx->generator, ctx->params, - ctx->pubKey.c_str(), ctx->priKey.c_str(), &(ctx->returnKeyPair)); + pubKeyStr, priKeyStr, &(ctx->returnKeyPair)); if (ctx->errCode != HCF_SUCCESS) { LOGE("ConvertPemKey fail."); ctx->errMsg = "ConvertPemKey fail."; @@ -884,11 +888,19 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKey(napi_env env, napi_callback_info return NewConvertPemKeyAsyncWork(env, ctx); } -static HcfResult ConvertPemKeySync(std::string &pubKey, std::string &priKey, HcfAsyKeyGenerator *generator, +static HcfResult ConvertPemKeySync(HcfBlob *pubKey, HcfBlob *priKey, HcfAsyKeyGenerator *generator, HcfParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) { + const char *pubKeyStr = nullptr; + const char *priKeyStr = nullptr; + if (pubKey != nullptr) { + pubKeyStr = reinterpret_cast(pubKey->data); + } + if (priKey != nullptr) { + priKeyStr = reinterpret_cast(priKey->data); + } HcfResult errCode = generator->convertPemKey(generator, paramsSpec, - pubKey.c_str(), priKey.c_str(), returnKeyPair); + pubKeyStr, priKeyStr, returnKeyPair); if (errCode != HCF_SUCCESS) { LOGE("convertPemKey error!"); return errCode; @@ -900,10 +912,10 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ { napi_value thisVar = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - std::string pubKey; - std::string priKey; + HcfBlob *pubKey = nullptr; + HcfBlob *priKey = nullptr; HcfParamsSpec *paramsSpec = nullptr; - if (!ValidateAndGetParams(env, info, pubKey, priKey, ¶msSpec)) { + if (!ValidateAndGetParams(env, info, &pubKey, &priKey, ¶msSpec)) { FreeDecodeParamsSpec(paramsSpec); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters.")); return NapiGetNull(env); @@ -913,6 +925,7 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok || napiGenerator == nullptr) { FreeDecodeParamsSpec(paramsSpec); + HcfFreePubKeyAndPriKey(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; @@ -921,6 +934,7 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); if (generator == nullptr) { FreeDecodeParamsSpec(paramsSpec); + HcfFreePubKeyAndPriKey(pubKey, priKey); LOGE("GetAsyKeyGenerator failed!"); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "GetAsyKeyGenerator failed!")); return nullptr; @@ -928,6 +942,7 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ HcfKeyPair *returnKeyPair = nullptr; HcfResult errCode = ConvertPemKeySync(pubKey, priKey, generator, paramsSpec, &(returnKeyPair)); + HcfFreePubKeyAndPriKey(pubKey, priKey); if (errCode != HCF_SUCCESS) { FreeDecodeParamsSpec(paramsSpec); LOGE("ConvertPemKeySync error!"); diff --git a/frameworks/js/napi/crypto/src/napi_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp index 88c26b1..1f52bfa 100644 --- a/frameworks/js/napi/crypto/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -662,7 +662,7 @@ bool GetEncodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnS return true; } -static HcfBlob *GetBlobFromStringJSParams(napi_env env, napi_value arg) +HcfBlob *GetBlobFromStringJSParams(napi_env env, napi_value arg) { napi_valuetype valueType; napi_typeof(env, arg, &valueType); -- Gitee From 91e99efa0cf3d9596f7c83837bd7d6d6c59cfe2f Mon Sep 17 00:00:00 2001 From: jing-wang177 Date: Fri, 22 Aug 2025 10:46:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=88=86=E6=94=AF=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jing-wang177 --- .../napi/crypto/src/napi_asy_key_generator.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 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 54ebba2..425e151 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -185,6 +185,14 @@ static void FreeConvertPemKeyCtx(napi_env env, ConvertPemKeyCtx *ctx) HcfFree(ctx); } +static void HcfFreePubKeyAndPriKey(HcfBlob *pubKey, HcfBlob *priKey) +{ + HcfBlobDataFree(pubKey); + HCF_FREE_PTR(pubKey); + HcfBlobDataClearAndFree(priKey); + HCF_FREE_PTR(priKey); +} + static bool BuildGenKeyPairCtx(napi_env env, napi_callback_info info, GenKeyPairCtx *ctx) { napi_value thisVar = nullptr; @@ -357,6 +365,7 @@ static bool ValidateAndGetParams(napi_env env, napi_callback_info info, HcfBlob if (argc == expectedArgc) { if (!GetDecodingParamsSpec(env, argv[PARAM2], paramsSpec)) { + HcfFreePubKeyAndPriKey(*pubKey, *priKey); LOGE("get params failed!"); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get napi paramsSpec failed!")); return false; @@ -807,14 +816,6 @@ napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info in return NewConvertKeyAsyncWork(env, ctx); } -static void HcfFreePubKeyAndPriKey(HcfBlob *pubKey, HcfBlob *priKey) -{ - HcfBlobDataFree(pubKey); - HCF_FREE_PTR(pubKey); - HcfBlobDataClearAndFree(priKey); - HCF_FREE_PTR(priKey); -} - napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; -- Gitee