diff --git a/frameworks/js/napi/crypto/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h index 53cec12a8bd295d45e1f89e615702c79e55717b4..79ff66b73ab39c5314c2af5f962ac4b7ba2893b9 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 bec90491656502f2fac29eb0190e5b9674ac87d8..425e15144c1ea0a93326a8dc13f34ab0aa4a71a3 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,11 +172,27 @@ 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); } +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; @@ -248,9 +264,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 +274,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 +343,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; @@ -361,6 +365,7 @@ static bool ValidateAndGetParams(napi_env env, napi_callback_info info, std::str 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; @@ -379,10 +384,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 +533,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."; @@ -803,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; @@ -884,11 +889,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 +913,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 +926,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 +935,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 +943,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 88c26b19ba7f8bd7087640a08448b5d3bc9b5294..1f52bfa5d184336558420402f5e77be5ec371eb1 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);