diff --git a/frameworks/js/napi/crypto/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h index a32d26cb6412b651756f8c23840d6f754a56d878..1d415c7511e6ff0aef512af7328740566cc4e575 100644 --- a/frameworks/js/napi/crypto/inc/napi_utils.h +++ b/frameworks/js/napi/crypto/inc/napi_utils.h @@ -81,6 +81,10 @@ int32_t GetCipherSpecType(CipherSpecItem targetItemType); napi_value GetDetailAsyKeySpecValue(napi_env env, napi_value arg, std::string argName); bool GetBigIntFromNapiValue(napi_env env, napi_value arg, HcfBigInteger *bigInt); + +HcfResult ConvertDataBlobToNapiValue(napi_env env, HcfBlob *blob, napi_value *napiValue); +HcfResult GetBlobFromNapiValue(napi_env env, napi_value arg, HcfBlob *blob); + } // 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 4015cbec7bbbd82cc61ab2c86dc147451a611168..a792a34dc5cbc2a126b608c4a7a0b4fa1079cc11 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -709,14 +709,14 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callbac 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.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napi asyKeyGenerator.")); return nullptr; } HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); if (generator == nullptr) { LOGE("get generator fail."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get generator fail!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "get generator fail!")); return nullptr; } @@ -725,14 +725,14 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callbac 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.")); + napi_throw(env, GenerateBusinessError(env, errCode, "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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to get generate key pair instance!")); return nullptr; } @@ -782,7 +782,8 @@ napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_inf 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.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, + "failed to unwrap napi asyKeyGenerator obj.")); return nullptr; } @@ -798,7 +799,7 @@ napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_inf if (generator == nullptr) { HcfFreePubKeyAndPriKey(pubKey, priKey); LOGE("get generator fail."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get generator fail!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "get generator fail!")); return nullptr; } @@ -815,7 +816,7 @@ napi_value NapiAsyKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_inf 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to get convert key instance!")); return nullptr; } @@ -868,7 +869,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) { LOGE("failed to unwrap napi asyKeyGenerator obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napi asyKeyGenerator.")); return nullptr; } @@ -891,7 +892,7 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ HcfResult errCode = ConvertPemKeySync(pubKey, priKey, generator, &(returnKeyPair)); if (errCode != HCF_SUCCESS) { LOGE("ConvertPemKeySync error!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "ConvertPemKeySync error!")); + napi_throw(env, GenerateBusinessError(env, errCode, "ConvertPemKeySync error!")); 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 02cfa2bb7440b04055ac3f7ede4287d1b230073f..3d3a29bdd4b01d5cb132a06665ceffba2591af0b 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 @@ -415,7 +415,7 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync(napi_env env, napi_c 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "build generator fail!")); return nullptr; } @@ -423,7 +423,7 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync(napi_env env, napi_c 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.")); + napi_throw(env, GenerateBusinessError(env, errCode, "generate key pair fail.")); return nullptr; } @@ -446,9 +446,8 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGenerateKeyPairSync(napi_env env, napi_c }, 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to wrap napiKeyPair obj!")); return nullptr; } return instance; @@ -478,7 +477,7 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync(napi_env env, napi_ca 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "build generator fail!")); return nullptr; } @@ -486,7 +485,7 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync(napi_env env, napi_ca 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.")); + napi_throw(env, GenerateBusinessError(env, errCode, "generate PubKey fail.")); return nullptr; } @@ -510,10 +509,9 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePubKeySync(napi_env env, napi_ca }, 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to wrap napiPubKey obj!")); return nullptr; } @@ -544,7 +542,7 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync(napi_env env, napi_ca 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "build generator fail!")); return nullptr; } @@ -552,7 +550,7 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync(napi_env env, napi_ca 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.")); + napi_throw(env, GenerateBusinessError(env, errCode, "generate PriKey fail.")); return nullptr; } @@ -576,10 +574,9 @@ napi_value NapiAsyKeyGeneratorBySpec::JsGeneratePriKeySync(napi_env env, napi_ca }, 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!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to wrap napiPriKey obj!")); return nullptr; } diff --git a/frameworks/js/napi/crypto/src/napi_cipher.cpp b/frameworks/js/napi/crypto/src/napi_cipher.cpp index 9e0763a6f2a9887fe30759da5c95ebd1b86e0f07..0e404dcb7964e21f9b20259e65f7187bcd5747a5 100644 --- a/frameworks/js/napi/crypto/src/napi_cipher.cpp +++ b/frameworks/js/napi/crypto/src/napi_cipher.cpp @@ -562,7 +562,7 @@ napi_value NapiCipher::JsCipherInitSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok || napiCipher == nullptr) { LOGE("failed to unwrap napi napiCipher obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "unwrap napi napiCipher failed!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "unwrap napi napiCipher failed!")); return nullptr; } HcfCipher *cipher = napiCipher->GetCipher(); @@ -634,29 +634,35 @@ napi_value NapiCipher::JsCipherUpdateSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok || napiCipher == nullptr) { LOGE("failed to unwrap napi napiCipher obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "unwrap napi cipher failed!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "unwrap napi cipher failed!")); return nullptr; } cipher = napiCipher->GetCipher(); // get input, type is blob - HcfBlob *input = nullptr; - input = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (input == nullptr) { + HcfBlob input = { 0 }; + HcfResult errCode = GetBlobFromNapiValue(env, argv[PARAM0], &input); + if (errCode != HCF_SUCCESS) { LOGE("failed to get input blob!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get input blob failed!")); + napi_throw(env, GenerateBusinessError(env, errCode, "get input blob failed!")); return nullptr; } HcfBlob output = { .data = nullptr, .len = 0 }; - HcfResult res = cipher->update(cipher, input, &output); - HcfFree(input->data); - HcfFree(input); - if (res != HCF_SUCCESS) { + errCode = cipher->update(cipher, &input, &output); + HcfFree(input.data); + if (errCode != HCF_SUCCESS) { LOGE("failed to update!"); - napi_throw(env, GenerateBusinessError(env, res, "update fail!")); + napi_throw(env, GenerateBusinessError(env, errCode, "update fail!")); return nullptr; } - napi_value instance = ConvertBlobToNapiValue(env, &output); + + napi_value instance = nullptr; + errCode = ConvertDataBlobToNapiValue(env, &output, &instance); HcfFree(output.data); + if (errCode != HCF_SUCCESS) { + LOGE("cipher update convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, errCode, "cipher update convert dataBlob to napi_value failed!")); + return nullptr; + } return instance; } @@ -695,35 +701,41 @@ napi_value NapiCipher::JsCipherDoFinalSync(napi_env env, napi_callback_info info napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiCipher)); if (status != napi_ok || napiCipher == nullptr) { LOGE("failed to unwrap napi cipher obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "unwrap napi cipher failed")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "unwrap napi cipher failed")); return nullptr; } cipher = napiCipher->GetCipher(); // get input, type is blob napi_valuetype valueType; HcfBlob *input = nullptr; + HcfBlob blob = { 0 }; napi_typeof(env, argv[PARAM0], &valueType); if (valueType != napi_null) { - input = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (input == nullptr) { + HcfResult ret = GetBlobFromNapiValue(env, argv[PARAM0], &blob); + if (ret != HCF_SUCCESS) { LOGE("failed to get input blob!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "get input blob failed!")); + napi_throw(env, GenerateBusinessError(env, ret, "get input blob failed!")); return nullptr; } + input = &blob; } HcfBlob output = { .data = nullptr, .len = 0 }; HcfResult res = cipher->doFinal(cipher, input, &output); - if (input != nullptr) { - HcfFree(input->data); - HcfFree(input); - } + HcfBlobDataFree(input); if (res != HCF_SUCCESS) { LOGE("failed to do final!"); napi_throw(env, GenerateBusinessError(env, res, "do final fail!")); return nullptr; } - napi_value instance = ConvertBlobToNapiValue(env, &output); + + napi_value instance = nullptr; + res = ConvertDataBlobToNapiValue(env, &output, &instance); HcfFree(output.data); + if (res != HCF_SUCCESS) { + LOGE("cipher convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, res, "cipher convert dataBlob to napi_value failed!")); + return nullptr; + } return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_kdf.cpp b/frameworks/js/napi/crypto/src/napi_kdf.cpp index f5d2efca0a6e151eb0a7d10e3785f0da89fa5fdd..b5f1b63418aacf9fa4c2f5c46dd51a21d39cc6ce 100644 --- a/frameworks/js/napi/crypto/src/napi_kdf.cpp +++ b/frameworks/js/napi/crypto/src/napi_kdf.cpp @@ -573,7 +573,7 @@ napi_value NapiKdf::JsKdfGenerateSecretSync(napi_env env, napi_callback_info inf napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiKdf)); if (status != napi_ok || napiKdf == nullptr) { LOGE("failed to unwrap NapiKdf obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiKdf obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "failed to unwrap NapiKdf obj!")); return nullptr; } HcfKdf *kdf = napiKdf->GetKdf(); @@ -594,7 +594,7 @@ napi_value NapiKdf::JsKdfGenerateSecretSync(napi_env env, napi_callback_info inf HcfResult errCode = kdf->generateSecret(kdf, paramsSpec); if (errCode != HCF_SUCCESS) { LOGE("KDF generateSecret failed!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "KDF generateSecret failed!")); + napi_throw(env, GenerateBusinessError(env, errCode, "KDF generateSecret failed!")); FreeKdfParamsSpec(paramsSpec); paramsSpec = nullptr; return nullptr; diff --git a/frameworks/js/napi/crypto/src/napi_key_agreement.cpp b/frameworks/js/napi/crypto/src/napi_key_agreement.cpp index c5fcf415caff0a3f9bbc0180456ab9b7ed8faeee..a1682d3b8cf377fb47c667832fe0d7b344afa012 100644 --- a/frameworks/js/napi/crypto/src/napi_key_agreement.cpp +++ b/frameworks/js/napi/crypto/src/napi_key_agreement.cpp @@ -279,6 +279,34 @@ napi_value NapiKeyAgreement::JsGenerateSecret(napi_env env, napi_callback_info i return NewKeyAgreementAsyncWork(env, ctx); } +static HcfResult GetPriKeyAndPubKeyFromParam(napi_env env, napi_value priKeyParam, napi_value pubKeyParam, + NapiPriKey **napiPriKey, NapiPubKey **napiPubKey) +{ + napi_status status = napi_unwrap(env, priKeyParam, reinterpret_cast(napiPriKey)); + if (status != napi_ok) { + LOGE("failed to unwrap priKey verify obj."); + return HCF_ERR_NAPI; + } + + if (*napiPriKey == nullptr) { + LOGE("priKey param is nullptr."); + return HCF_INVALID_PARAMS; + } + + status = napi_unwrap(env, pubKeyParam, reinterpret_cast(napiPubKey)); + if (status != napi_ok) { + LOGE("failed to unwrap pubKey verify obj."); + return HCF_ERR_NAPI; + } + + if (*napiPubKey == nullptr) { + LOGE("pubKey param is nullptr."); + return HCF_INVALID_PARAMS; + } + + return HCF_SUCCESS; +} + napi_value NapiKeyAgreement::JsGenerateSecretSync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -300,18 +328,10 @@ napi_value NapiKeyAgreement::JsGenerateSecretSync(napi_env env, napi_callback_in } NapiPriKey *napiPriKey = nullptr; - status = napi_unwrap(env, argv[PARAM0], reinterpret_cast(&napiPriKey)); - if (status != napi_ok || napiPriKey == nullptr) { - LOGE("failed to unwrap priKey verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "failed to unwrap priKey verify obj.")); - return nullptr; - } - NapiPubKey *napiPubKey = nullptr; - status = napi_unwrap(env, argv[PARAM1], reinterpret_cast(&napiPubKey)); - if (status != napi_ok || napiPubKey == nullptr) { - LOGE("failed to unwrap napi pubKey obj."); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "failed to unwrap napi pubKey obj.")); + HcfResult ret = GetPriKeyAndPubKeyFromParam(env, argv[PARAM0], argv[PARAM1], &napiPriKey, &napiPubKey); + if (ret != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, ret, "failed to parse priKey or pubKey.")); return nullptr; } @@ -319,15 +339,22 @@ napi_value NapiKeyAgreement::JsGenerateSecretSync(napi_env env, napi_callback_in HcfPriKey *priKey = napiPriKey->GetPriKey(); HcfPubKey *pubKey = napiPubKey->GetPubKey(); HcfBlob returnSecret = { .data = nullptr, .len = 0 }; - HcfResult ret = keyAgreement->generateSecret(keyAgreement, priKey, pubKey, &returnSecret); + ret = keyAgreement->generateSecret(keyAgreement, priKey, pubKey, &returnSecret); if (ret != HCF_SUCCESS) { LOGD("generate secret fail."); napi_throw(env, GenerateBusinessError(env, ret, "generate secret fail.")); return nullptr; } - napi_value instance = ConvertBlobToNapiValue(env, &returnSecret); + napi_value instance = nullptr; + ret = ConvertDataBlobToNapiValue(env, &returnSecret, &instance); HcfBlobDataFree(&returnSecret); + if (ret != HCF_SUCCESS) { + LOGE("key agreement convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, ret, "key agreement convert dataBlob to napi_value failed!")); + return nullptr; + } + return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_mac.cpp b/frameworks/js/napi/crypto/src/napi_mac.cpp index a5c9c5c46940ffb333fbc668b7817a315e07a1a0..45693841f16f61be5141be7d1859a1ced8ba506c 100644 --- a/frameworks/js/napi/crypto/src/napi_mac.cpp +++ b/frameworks/js/napi/crypto/src/napi_mac.cpp @@ -453,7 +453,7 @@ napi_value NapiMac::JsMacInitSync(napi_env env, napi_callback_info info) status = napi_unwrap(env, thisVar, reinterpret_cast(&napiMac)); if (status != napi_ok || napiMac == nullptr) { LOGE("failed to unwrap napiMac obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiMac obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napiMac obj!")); return nullptr; } HcfMac *mac = napiMac->GetMac(); @@ -508,7 +508,7 @@ napi_value NapiMac::JsMacUpdateSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiMac)); if (status != napi_ok || napiMac == nullptr) { LOGE("failed to unwrap napiMac obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiMac obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napiMac obj!")); return nullptr; } @@ -567,7 +567,7 @@ napi_value NapiMac::JsMacDoFinalSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiMac)); if (status != napi_ok || napiMac == nullptr) { LOGE("failed to unwrap napiMac obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "failed to unwrap napiMac obj.")); return nullptr; } HcfMac *mac = napiMac->GetMac(); @@ -580,17 +580,20 @@ napi_value NapiMac::JsMacDoFinalSync(napi_env env, napi_callback_info info) HcfResult errCode = mac->doFinal(mac, &outBlob); if (errCode != HCF_SUCCESS) { LOGE("mac doFinal failed!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "mac doFinal failed!")); + napi_throw(env, GenerateBusinessError(env, errCode, "mac doFinal failed!")); HcfBlobDataClearAndFree(&outBlob); return nullptr; } - napi_value returnOutBlob = ConvertBlobToNapiValue(env, &outBlob); + + napi_value returnOutBlob = nullptr; + errCode = ConvertDataBlobToNapiValue(env, &outBlob, &returnOutBlob); HcfBlobDataClearAndFree(&outBlob); - if (returnOutBlob == nullptr) { - LOGE("returnOutBlob is nullptr!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "returnOutBlob is nullptr!")); - returnOutBlob = NapiGetNull(env); + if (errCode != HCF_SUCCESS) { + LOGE("mac convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, errCode, "mac convert dataBlob to napi_value failed!")); + return nullptr; } + return returnOutBlob; } diff --git a/frameworks/js/napi/crypto/src/napi_md.cpp b/frameworks/js/napi/crypto/src/napi_md.cpp index 584535774f16031e7e30dc06c10866c15cfaee8f..d2334cc81d5ab7aed392b443a8f62b7f351de766 100644 --- a/frameworks/js/napi/crypto/src/napi_md.cpp +++ b/frameworks/js/napi/crypto/src/napi_md.cpp @@ -348,7 +348,7 @@ napi_value NapiMd::JsMdUpdateSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiMd)); if (status != napi_ok || napiMd == nullptr) { LOGE("failed to unwrap NapiMd obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "invalid parameters.")); HcfBlobDataClearAndFree(inBlob); HcfFree(inBlob); return nullptr; @@ -404,7 +404,7 @@ napi_value NapiMd::JsMdDoFinalSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiMd)); if (status != napi_ok || napiMd == nullptr) { LOGE("failed to unwrap NapiMd obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiMd obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "failed to unwrap NapiMd obj!")); return nullptr; } @@ -419,17 +419,18 @@ napi_value NapiMd::JsMdDoFinalSync(napi_env env, napi_callback_info info) HcfResult errCode = md->doFinal(md, &outBlob); if (errCode != HCF_SUCCESS) { LOGE("md doFinal failed!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "md doFinal failed!")); + napi_throw(env, GenerateBusinessError(env, errCode, "md doFinal failed!")); HcfBlobDataClearAndFree(&outBlob); return nullptr; } - napi_value instance = ConvertBlobToNapiValue(env, &outBlob); + napi_value instance = nullptr; + errCode = ConvertDataBlobToNapiValue(env, &outBlob, &instance); HcfBlobDataClearAndFree(&outBlob); - if (instance == nullptr) { - LOGE("instance is nullptr!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "instance is nullptr!")); - instance = NapiGetNull(env); + if (errCode != HCF_SUCCESS) { + LOGE("md convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, errCode, "md convert dataBlob to napi_value failed!")); + return nullptr; } return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_pri_key.cpp b/frameworks/js/napi/crypto/src/napi_pri_key.cpp index a8be5981ecdc43abf49dc3c322b0c0cfcaac46fe..b60b0085b2200abffabc46433fc69ebe998ec751 100644 --- a/frameworks/js/napi/crypto/src/napi_pri_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_pri_key.cpp @@ -118,14 +118,14 @@ napi_value NapiPriKey::JsGetEncodedPem(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiPriKey)); if (status != napi_ok || napiPriKey == nullptr) { LOGE("failed to unwrap napiPriKey obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPriKey obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napiPriKey obj!")); return nullptr; } HcfPriKey *priKey = napiPriKey->GetPriKey(); if (priKey == nullptr) { LOGE("failed to get priKey obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get priKey obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to get priKey obj!")); return nullptr; } @@ -279,21 +279,21 @@ napi_value NapiPriKey::JsGetEncodedDer(napi_env env, napi_callback_info info) } napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiPriKey)); if (status != napi_ok || napiPriKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap private key obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap private key obj!")); LOGE("failed to unwrap private key obj!"); return nullptr; } HcfPriKey *priKey = napiPriKey->GetPriKey(); if (priKey == nullptr) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get private key obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to get private key obj!")); LOGE("failed to get private key obj!"); return nullptr; } HcfBlob returnBlob = { .data = nullptr, .len = 0 }; HcfResult res = priKey->getEncodedDer(priKey, format.c_str(), &returnBlob); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "ecc get private key encodedDer fail.")); - LOGE("ecc get private key encodeDer fail."); + napi_throw(env, GenerateBusinessError(env, res, "get private key encodedDer fail.")); + LOGE("get private key encodeDer fail."); return nullptr; } napi_value instance = ConvertBlobToNapiValue(env, &returnBlob); diff --git a/frameworks/js/napi/crypto/src/napi_pub_key.cpp b/frameworks/js/napi/crypto/src/napi_pub_key.cpp index 50d805f141ea409dc49fc9d645cfbd3b17261174..fcc80b10ea52866d3e4fde42e311695cab130ed0 100644 --- a/frameworks/js/napi/crypto/src/napi_pub_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_pub_key.cpp @@ -116,14 +116,14 @@ napi_value NapiPubKey::JsGetEncodedDer(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiPubKey)); if (status != napi_ok || napiPubKey == nullptr) { LOGE("failed to unwrap napiPubKeyDer obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPubKeyDer obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napiPubKeyDer obj!")); return nullptr; } HcfPubKey *pubKey = napiPubKey->GetPubKey(); if (pubKey == nullptr) { LOGE("failed to get pubKeyDer obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get pubKeyDer obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to get pubKeyDer obj!")); return nullptr; } @@ -131,7 +131,7 @@ napi_value NapiPubKey::JsGetEncodedDer(napi_env env, napi_callback_info info) HcfResult res = pubKey->getEncodedDer(pubKey, format.c_str(), &returnBlob); if (res != HCF_SUCCESS) { LOGE("c getEncodedDer fail."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "c getEncodedDer fail.")); + napi_throw(env, GenerateBusinessError(env, res, "c getEncodedDer fail.")); return nullptr; } @@ -164,14 +164,14 @@ napi_value NapiPubKey::JsGetEncodedPem(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiPubKey)); if (status != napi_ok || napiPubKey == nullptr) { LOGE("failed to unwrap napiPriKey obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napiPubKey obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to unwrap napiPubKey obj!")); return nullptr; } HcfPubKey *pubKey = napiPubKey->GetPubKey(); if (pubKey == nullptr) { LOGE("failed to get pubKey obj!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get pubKey obj!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "failed to get pubKey obj!")); return nullptr; } diff --git a/frameworks/js/napi/crypto/src/napi_sign.cpp b/frameworks/js/napi/crypto/src/napi_sign.cpp index edbf5c3a381a2d0d59224e26f81b78bb2f7bdf59..cf7fc77a896a90c8a782143907ae0534ea7fe050 100644 --- a/frameworks/js/napi/crypto/src/napi_sign.cpp +++ b/frameworks/js/napi/crypto/src/napi_sign.cpp @@ -653,22 +653,23 @@ napi_value NapiSign::JsUpdateSync(napi_env env, napi_callback_info info) return nullptr; } - HcfBlob *blob = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (blob == nullptr) { - LOGE("failed to get data."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get data.")); + HcfBlob blob = { 0 }; + HcfResult ret = GetBlobFromNapiValue(env, argv[PARAM0], &blob); + if (ret != HCF_SUCCESS) { + LOGE("failed to get input blob!"); + napi_throw(env, GenerateBusinessError(env, ret, "failed to get data.")); return nullptr; } HcfSign *sign = napiSign->GetSign(); - HcfResult ret = sign->update(sign, blob); + ret = sign->update(sign, &blob); + HcfBlobDataFree(&blob); if (ret != HCF_SUCCESS) { LOGD("sign update fail."); napi_throw(env, GenerateBusinessError(env, ret, "sign update fail.")); return nullptr; } napi_value instance = NapiGetNull(env); - HcfBlobDataFree(blob); return instance; } @@ -714,26 +715,36 @@ napi_value NapiSign::JsSignSync(napi_env env, napi_callback_info info) napi_valuetype valueType; napi_typeof(env, argv[PARAM0], &valueType); HcfBlob *data = nullptr; + HcfBlob blob = { 0 }; if (valueType != napi_null) { - data = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (data == nullptr) { + HcfResult ret = GetBlobFromNapiValue(env, argv[PARAM0], &blob); + if (ret != HCF_SUCCESS) { LOGE("failed to get data."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get data.")); + napi_throw(env, GenerateBusinessError(env, ret, "failed to get data.")); return nullptr; } + data = &blob; } HcfSign *sign = napiSign->GetSign(); HcfBlob returnSignatureData = { .data = nullptr, .len = 0 }; HcfResult ret = sign->sign(sign, data, &returnSignatureData); + HcfBlobDataFree(data); if (ret != HCF_SUCCESS) { LOGD("sign doFinal fail."); napi_throw(env, GenerateBusinessError(env, ret, "sign doFinal fail.")); return nullptr; } - napi_value instance = ConvertBlobToNapiValue(env, &returnSignatureData); + napi_value instance = nullptr; + ret = ConvertDataBlobToNapiValue(env, &returnSignatureData, &instance); HcfBlobDataFree(&returnSignatureData); + if (ret != HCF_SUCCESS) { + LOGE("sign convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, ret, "sign convert dataBlob to napi_value failed!")); + return nullptr; + } + return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp b/frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp index 5ba0e3e0d307dd5a5811ebf554286f35b4a58614..1b51cd26e3ec1b44f32a2035905352e66afa047a 100644 --- a/frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_sym_key_generator.cpp @@ -386,6 +386,7 @@ napi_value NapiSymKeyGenerator::JsGenerateSymKeySync(napi_env env, napi_callback NapiSymKeyGenerator *napiGenerator = nullptr; napi_status unwrapStatus = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (unwrapStatus != napi_ok || napiGenerator == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to unwrap NapiSymKeyGenerator obj.")); LOGE("failed to unwrap NapiSymKeyGenerator obj!"); return nullptr; } @@ -447,6 +448,7 @@ napi_value NapiSymKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_inf napi_value argv[ARGS_SIZE_ONE] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != ARGS_SIZE_ONE) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "the input args num is invalid!")); LOGE("wrong argument num. require 1 argument. [Argc]: %zu!", argc); return nullptr; } @@ -455,6 +457,7 @@ napi_value NapiSymKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_inf napi_status unwrapStatus = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (unwrapStatus != napi_ok || napiGenerator == nullptr) { LOGE("failed to unwrap NapiSymKeyGenerator obj!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap NapiSymKeyGenerator obj!")); return nullptr; } @@ -477,14 +480,14 @@ napi_value NapiSymKeyGenerator::JsConvertKeySync(napi_env env, napi_callback_inf HcfBlobDataFree(keyMaterial); HcfFree(keyMaterial); if (ret != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "convertSymKey key failed!")); + napi_throw(env, GenerateBusinessError(env, ret, "convertSymKey key failed!")); LOGE("convertSymKey key failed!"); return nullptr; } napi_value instance = NapiSymKey::CreateSymKey(env); if (!napiGetInstance(env, key, instance)) { - napi_throw(env, GenerateBusinessError(env, HCF_NOT_SUPPORT, "get instance failed!")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "get instance failed!")); LOGE("get instance failed!"); return nullptr; } diff --git a/frameworks/js/napi/crypto/src/napi_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp index e697a92ef558cb55f45fe0203a9102cd862a58b2..cb55d6c7e9aecc704d889b6a154234a76f5e82c7 100644 --- a/frameworks/js/napi/crypto/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -191,6 +191,42 @@ HcfBlob *GetBlobFromNapiDataBlob(napi_env env, napi_value arg) return GetBlobFromNapiUint8Arr(env, data); } +HcfResult GetBlobFromNapiValue(napi_env env, napi_value arg, HcfBlob *blob) +{ + napi_value data = GetUint8ArrFromNapiDataBlob(env, arg); + if (data == nullptr) { + LOGE("failed to get data in DataBlob"); + return HCF_INVALID_PARAMS; + } + + void *rawData = nullptr; + napi_typedarray_type arrayType; + napi_status status = napi_get_typedarray_info(env, data, &arrayType, &(blob->len), + reinterpret_cast(&rawData), nullptr, nullptr); + if (status != napi_ok) { + LOGE("failed to get valid rawData."); + return HCF_ERR_NAPI; + } + if (arrayType != napi_uint8_array) { + LOGE("input data is not uint8 array."); + return HCF_INVALID_PARAMS; + } + + blob->data = nullptr; + if (blob->len == 0 || rawData == nullptr) { + LOGD("napi Uint8Arr is null"); + return HCF_SUCCESS; + } + + blob->data = static_cast(HcfMalloc(blob->len, 0)); + if (blob->data == nullptr) { + LOGE("malloc blob data failed!"); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(blob->data, blob->len, rawData, blob->len); + return HCF_SUCCESS; +} + static HcfBlob *GetAadFromParamsSpec(napi_env env, napi_value arg) { napi_value data = nullptr; @@ -1507,6 +1543,38 @@ napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob) return dataBlob; } +HcfResult ConvertDataBlobToNapiValue(napi_env env, HcfBlob *blob, napi_value *napiValue) +{ + if (blob->data == nullptr || blob->len == 0) { // inner api, allow empty data + *napiValue = NapiGetNull(env); + return HCF_SUCCESS; + } + + uint8_t *buffer = reinterpret_cast(HcfMalloc(blob->len, 0)); + if (buffer == nullptr) { + LOGE("malloc uint8 array buffer failed!"); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(buffer, blob->len, blob->data, blob->len); + + napi_value outBuffer = nullptr; + napi_status status = napi_create_external_arraybuffer( + env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer); + if (status != napi_ok) { + LOGE("create napi uint8 array buffer failed!"); + HcfFree(buffer); + return HCF_ERR_NAPI; + } + + napi_value outData = nullptr; + napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData); + napi_value dataBlob = nullptr; + napi_create_object(env, &dataBlob); + napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData); + *napiValue = dataBlob; + return HCF_SUCCESS; +} + napi_value ConvertObjectBlobToNapiValue(napi_env env, HcfBlob *blob) { if (blob == nullptr || blob->data == nullptr || blob->len == 0) { diff --git a/frameworks/js/napi/crypto/src/napi_verify.cpp b/frameworks/js/napi/crypto/src/napi_verify.cpp index 864523c6bd32474573b4e17d9492940f463b4665..a53dedea77cce168cbfe669c889bd2c1f8387b2d 100644 --- a/frameworks/js/napi/crypto/src/napi_verify.cpp +++ b/frameworks/js/napi/crypto/src/napi_verify.cpp @@ -329,6 +329,34 @@ static bool GetDataBlobAndSignatureFromInput(napi_env env, napi_value dataValue, return true; } +static HcfResult GetDataAndSignatureFromInput(napi_env env, napi_value dataValue, napi_value signatureDataValue, + HcfBlob *returnData, HcfBlob *returnSignatureData) +{ + HcfResult ret = GetBlobFromNapiValue(env, signatureDataValue, returnSignatureData); + if (ret != HCF_SUCCESS) { + LOGE("failed to get signature."); + return ret; + } + + napi_valuetype valueType; + napi_typeof(env, dataValue, &valueType); + if (valueType == napi_null) { // allow dataValue is empty + returnData->data = nullptr; + returnData->len = 0; + return HCF_SUCCESS; + } + + ret = GetBlobFromNapiValue(env, dataValue, returnData); + if (ret != HCF_SUCCESS) { + LOGE("failed to get data."); + HcfBlobDataFree(returnSignatureData); + returnSignatureData->data = nullptr; + returnSignatureData->len = 0; + } + + return ret; +} + static bool BuildVerifyJsDoFinalCtx(napi_env env, napi_callback_info info, VerifyDoFinalCtx *ctx) { napi_value thisVar = nullptr; @@ -779,16 +807,17 @@ napi_value NapiVerify::JsUpdateSync(napi_env env, napi_callback_info info) return nullptr; } - HcfBlob *blob = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (blob == nullptr) { - LOGE("failed to get blob data from napi."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get blob data from napi.")); + HcfBlob blob = { 0 }; + HcfResult ret = GetBlobFromNapiValue(env, argv[PARAM0], &blob); + if (ret != HCF_SUCCESS) { + LOGE("failed to get input blob!"); + napi_throw(env, GenerateBusinessError(env, ret, "failed to get input blob.")); return nullptr; } HcfVerify *verify = napiVerify->GetVerify(); - HcfResult ret = verify->update(verify, blob); - HcfBlobDataFree(blob); + ret = verify->update(verify, &blob); + HcfBlobDataFree(&blob); if (ret != HCF_SUCCESS) { LOGE("verify update fail."); napi_throw(env, GenerateBusinessError(env, ret, "verify update fail.")); @@ -836,16 +865,18 @@ napi_value NapiVerify::JsVerifySync(napi_env env, napi_callback_info info) return nullptr; } - HcfBlob *data = nullptr; - HcfBlob *signatureData = nullptr; - if (!GetDataBlobAndSignatureFromInput(env, argv[PARAM0], argv[PARAM1], &data, &signatureData)) { + HcfBlob data = { 0 }; + HcfBlob signatureData = { 0 }; + HcfResult ret = GetDataAndSignatureFromInput(env, argv[PARAM0], argv[PARAM1], &data, &signatureData); + if (ret != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, ret, "failed to parse param1 or param2.")); return nullptr; } HcfVerify *verify = napiVerify->GetVerify(); - bool isVerifySucc = verify->verify(verify, data, signatureData); - HcfBlobDataFree(data); - HcfBlobDataFree(signatureData); + bool isVerifySucc = verify->verify(verify, &data, &signatureData); + HcfBlobDataFree(&data); + HcfBlobDataFree(&signatureData); if (!isVerifySucc) { LOGD("verify doFinal fail."); } @@ -854,7 +885,7 @@ napi_value NapiVerify::JsVerifySync(napi_env env, napi_callback_info info) return result; } -static bool BuildVerifyJsRecoverCtx(napi_env env, napi_callback_info info, VerifyRecoverCtx *ctx) +HcfResult BuildVerifyJsRecoverCtx(napi_env env, napi_callback_info info, VerifyRecoverCtx *ctx) { napi_value thisVar = nullptr; size_t expectedArgc = PARAMS_NUM_ONE; @@ -863,37 +894,40 @@ static bool BuildVerifyJsRecoverCtx(napi_env env, napi_callback_info info, Verif napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); if (argc != expectedArgc) { LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc, argc); - return false; + return HCF_INVALID_PARAMS; } NapiVerify *napiVerify = nullptr; napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiVerify)); if (status != napi_ok || napiVerify == nullptr) { LOGE("failed to unwrap napi verify obj."); - return false; + return HCF_ERR_NAPI; } ctx->verify = napiVerify->GetVerify(); if (ctx->verify == nullptr) { LOGE("failed to get verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get verify obj.")); - return false; + return HCF_ERR_CRYPTO_OPERATION; } - HcfBlob *signatureData = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (signatureData == nullptr) { - LOGE("failed to get signature."); - return false; + ctx->signatureData = reinterpret_cast(HcfMalloc(sizeof(HcfBlob), 0)); + if (ctx->signatureData == nullptr) { + LOGE("failed to allocate newBlob memory!"); + return HCF_ERR_MALLOC; + } + + HcfResult ret = GetBlobFromNapiValue(env, argv[PARAM0], ctx->signatureData); + if (ret != HCF_SUCCESS) { + return ret; } - ctx->signatureData = signatureData; if (napi_create_reference(env, thisVar, 1, &ctx->verifyRef) != napi_ok) { LOGE("create verify ref failed when do verify recover!"); - return false; + return HCF_ERR_NAPI; } napi_create_promise(env, &ctx->deferred, &ctx->promise); - return true; + return HCF_SUCCESS; } napi_value NapiVerify::JsRecover(napi_env env, napi_callback_info info) @@ -905,9 +939,10 @@ napi_value NapiVerify::JsRecover(napi_env env, napi_callback_info info) return nullptr; } - if (!BuildVerifyJsRecoverCtx(env, info, ctx)) { + HcfResult ret = BuildVerifyJsRecoverCtx(env, info, ctx); + if (ret != HCF_SUCCESS) { LOGE("build context fail."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail.")); + napi_throw(env, GenerateBusinessError(env, ret, "build context fail.")); FreeVerifyRecoverCtx(env, ctx); return nullptr; } @@ -932,37 +967,43 @@ napi_value NapiVerify::JsRecoverSync(napi_env env, napi_callback_info info) napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiVerify)); if (status != napi_ok || napiVerify == nullptr) { LOGE("failed to unwrap napi verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi verify obj.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "failed to unwrap napi verify obj.")); return nullptr; } HcfVerify *verify = napiVerify->GetVerify(); if (verify == nullptr) { LOGE("failed to get verify obj."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "fail to get verify obj.")); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_CRYPTO_OPERATION, "fail to get verify obj.")); return nullptr; } - HcfBlob *signatureData = GetBlobFromNapiDataBlob(env, argv[PARAM0]); - if (signatureData == nullptr) { + HcfBlob signatureData = { 0 }; + HcfResult ret = GetBlobFromNapiValue(env, argv[PARAM0], &signatureData); + if (ret != HCF_SUCCESS) { LOGE("failed to get signature data."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get signature data.")); + napi_throw(env, GenerateBusinessError(env, ret, "failed to get signature data.")); return nullptr; } HcfBlob rawSignatureData = { .data = nullptr, .len = 0}; - HcfResult res = verify->recover(verify, signatureData, &rawSignatureData); - HcfBlobDataFree(signatureData); - HcfFree(signatureData); - signatureData = NULL; + HcfResult res = verify->recover(verify, &signatureData, &rawSignatureData); + HcfBlobDataFree(&signatureData); if (res != HCF_SUCCESS) { LOGE("failed to verify recover."); napi_throw(env, GenerateBusinessError(env, res, "failed to verify recover.")); return nullptr; } - napi_value instance = ConvertBlobToNapiValue(env, &rawSignatureData); - HcfBlobDataClearAndFree(&rawSignatureData); + napi_value instance = nullptr; + res = ConvertDataBlobToNapiValue(env, &rawSignatureData, &instance); + HcfBlobDataFree(&rawSignatureData); + if (res != HCF_SUCCESS) { + LOGE("verify recover convert dataBlob to napi_value failed!"); + napi_throw(env, GenerateBusinessError(env, res, "verify recover convert dataBlob to napi_value failed!")); + return nullptr; + } + return instance; } diff --git a/plugin/openssl_plugin/crypto_operation/signature/src/signature_rsa_openssl.c b/plugin/openssl_plugin/crypto_operation/signature/src/signature_rsa_openssl.c index 6ff173b053dbd0a7a2480d6d2d00089ee6362e5c..5d9ffcff7f3f889a87fc095b484278b4b0f8c45b 100644 --- a/plugin/openssl_plugin/crypto_operation/signature/src/signature_rsa_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/signature/src/signature_rsa_openssl.c @@ -444,7 +444,7 @@ static HcfResult EngineSignUpdate(HcfSignSpi *self, HcfBlob *data) } if (impl->operation == HCF_OPERATIOPN_ONLY_SIGN) { LOGE("Update cannot support in OnlySign"); - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } if (OpensslEvpDigestSignUpdate(impl->mdctx, data->data, data->len) != HCF_OPENSSL_SUCCESS) { LOGD("[error] OpensslEvpDigestSignUpdate fail"); @@ -471,7 +471,7 @@ static HcfResult EngineVerifyUpdate(HcfVerifySpi *self, HcfBlob *data) if (impl->operation != RSA_DIGEST_VERIFY) { LOGE("Invalid digest verify operation."); - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } if (OpensslEvpDigestVerifyUpdate(impl->mdctx, data->data, data->len) != HCF_OPENSSL_SUCCESS) { @@ -621,12 +621,12 @@ static HcfResult EngineRecover(HcfVerifySpi *self, HcfBlob *signatureData, HcfBl HcfVerifySpiRsaOpensslImpl *impl = (HcfVerifySpiRsaOpensslImpl *)self; if (impl->initFlag != INITIALIZED) { LOGE("The Sign has not been init."); - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } if (impl->operation != RSA_VERIFY_RECOVER) { LOGE("Invalid verify recover operation."); - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } size_t bufLen = 0; diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c index 738c533c8a4a203fc82a19098eb5c3293f048dbd..c318e6a6c7301b069c7c340bd6f671d17ce4e2f4 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c @@ -199,7 +199,7 @@ static HcfResult GetAlg25519PubKeyEncodedPem(HcfKey *self, const char *format, c (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GetAlg25519PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) @@ -234,7 +234,7 @@ static HcfResult GetAlg25519PriKeyEncodedPem(HcfKey *self, const char *format, c (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static const char *GetAlg25519PubKeyFormat(HcfKey *self) @@ -430,7 +430,7 @@ static HcfResult GetAlg25519PriKeyEncodedDer(const HcfPriKey *self, const char * (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static void ClearAlg25519PriKeyMem(HcfPriKey *self) @@ -481,7 +481,7 @@ static HcfResult GetAlg25519PubKeyEncodedDer(const HcfPubKey *self, const char * (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static void FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey *pk) diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c index 0a921b506c10091413df40bd6f5a05e8c4348ccf..63459e2e01c6e61c4f3dfe2ed163fc069e90602e 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c @@ -204,7 +204,7 @@ static HcfResult GetDhPubKeyEncodedPem(HcfKey *self, const char *format, char ** (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GetDhPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) @@ -243,7 +243,7 @@ static HcfResult GetDhPriKeyEncodedPem(HcfKey *self, const char *format, char ** (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static const char *GetDhPubKeyFormat(HcfKey *self) @@ -405,7 +405,7 @@ static HcfResult GetDhPriKeyEncodedDer(const HcfPriKey *self, const char *format (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static void ClearDhPriKeyMem(HcfPriKey *self) @@ -502,7 +502,7 @@ static HcfResult GetDhPubKeyEncodedDer(const HcfPubKey *self, const char *format (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static void FillOpensslDhPubKeyFunc(HcfOpensslDhPubKey *pk) diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c index 2e13155aef65f4ea0b50fd51d65bf38e8714fa12..f1541df0f88995119f6d94f61676a2f1801cb2a0 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c @@ -190,7 +190,7 @@ static HcfResult GetDsaPubKeyEncodedPem(HcfKey *self, const char *format, char * (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GetDsaPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) @@ -220,7 +220,7 @@ static HcfResult GetDsaPriKeyEncodedPem(HcfKey *self, const char *format, char * (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static const char *GetDsaPubKeyFormat(HcfKey *self) @@ -369,7 +369,7 @@ static HcfResult GetDsaPriKeyEncodedDer(const HcfPriKey *self, const char *forma (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GenerateDsaEvpKey(int32_t keyLen, EVP_PKEY **ppkey) @@ -426,7 +426,7 @@ static HcfResult GetDsaPubKeyEncodedDer(const HcfPubKey *self, const char *forma (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static void FillOpensslDsaPubKeyFunc(HcfOpensslDsaPubKey *pk) diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c index 9876672854b238b73632e6dd9667c1baa85e053f..8dc62f5f1afb438945dfd4b94e76557da3710fac 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c @@ -1265,7 +1265,7 @@ static HcfResult GetEccPubKeyEncodedPem(HcfKey *self, const char *format, char * (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GetEccPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) @@ -1307,7 +1307,7 @@ static HcfResult GetEccPriKeyEncodedPem(HcfKey *self, const char *format, char * (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult ParamCheck(const HcfPriKey *self, const char *format, const HcfBlob *returnBlob) diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c index a7d08aa4dd7b55a9908d517d59ee92117519d5b2..39eb85aa4b9bf9f89d2263b2414d0b9530ddb325 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c @@ -158,7 +158,7 @@ static HcfResult GetRsaPriKeyEncodedDer(const HcfPriKey *self, const char *forma (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GetRsaPriKeySpecBigInteger(const HcfPriKey *self, const AsyKeySpecItem item, @@ -751,7 +751,7 @@ static HcfResult GetRsaPubKeyEncodedDer(const HcfPubKey *self, const char *forma (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult PackPubKey(RSA *rsaPubKey, HcfOpensslRsaPubKey **retPubKey) diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c index 897b33497a21f43663dc75fc81e195bb3253ac60..f988defe50f5e6158caf228ac330446ccdc64e6f 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c @@ -480,7 +480,7 @@ static HcfResult GetSm2PubKeyEncodedPem(HcfKey *self, const char *format, char * (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult GetSm2PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) @@ -522,7 +522,7 @@ static HcfResult GetSm2PriKeyEncodedPem(HcfKey *self, const char *format, char * (void)self; (void)format; (void)returnString; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static void Sm2PriKeyClearMem(HcfPriKey *self) @@ -741,7 +741,7 @@ static HcfResult GetSm2PubKeyEncodedDer(const HcfPubKey *self, const char *forma (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult PackSm2PubKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType, @@ -792,7 +792,7 @@ static HcfResult GetSm2PriKeyEncodedDer(const HcfPriKey *self, const char *forma (void)self; (void)format; (void)returnBlob; - return HCF_INVALID_PARAMS; + return HCF_ERR_CRYPTO_OPERATION; } static HcfResult PackSm2PriKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,