diff --git a/frameworks/cj/src/crypto_ffi.cpp b/frameworks/cj/src/crypto_ffi.cpp index 9ba7662bc5ca145eed53922e8cdb5c9ff137d848..3ecbf2cb6262c30f38b5e4973feacb953a81d604 100644 --- a/frameworks/cj/src/crypto_ffi.cpp +++ b/frameworks/cj/src/crypto_ffi.cpp @@ -1382,7 +1382,8 @@ namespace OHOS { *errCode = HCF_INVALID_PARAMS; return ret; } - *errCode = priKey->base.getEncodedPem(&priKey->base, format, &ret); + HcfParamsSpec *paramsSpec = nullptr; + *errCode = priKey->getEncodedPem(priKey, paramsSpec, format, &ret); LOGD("[PriKey] FFiOHOSPriKeyGetEncodedPem end"); return ret; } diff --git a/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h index df7329d3a38e1614cd73b985121a8a443d3ac462..0974a4eaa290100f28c74ed07253234a8beaa03f 100644 --- a/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h +++ b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h @@ -55,6 +55,8 @@ const std::string IV_PARAMS = "iv"; const std::string AAD_PARAMS = "aad"; const std::string AUTHTAG_PARAMS = "authTag"; const std::string ALGO_PARAMS = "algName"; +const std::string PASSWD_PARAMS = "password"; +const std::string CIPHER_PARAMS = "cipherName"; const std::string ALGO_PARAMS_OLD = "algoName"; const std::string IV_PARAMS_SPEC = "IvParamsSpec"; const std::string GCM_PARAMS_SPEC = "GcmParamsSpec"; diff --git a/frameworks/js/napi/crypto/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h index 1d415c7511e6ff0aef512af7328740566cc4e575..3adf26637be30530dd3a5ccf49db3f6e9baa5d0c 100644 --- a/frameworks/js/napi/crypto/inc/napi_utils.h +++ b/frameworks/js/napi/crypto/inc/napi_utils.h @@ -54,6 +54,8 @@ HcfBlob *GetBlobFromNapiUint8Arr(napi_env env, napi_value data); HcfBlob *GetBlobFromNapiDataBlob(napi_env env, napi_value arg); bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec); +bool GetEncodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnSpec); +bool GetDecodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnSpec); napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob); napi_value ConvertObjectBlobToNapiValue(napi_env env, HcfBlob *blob); @@ -61,9 +63,6 @@ napi_value ConvertObjectBlobToNapiValue(napi_env env, HcfBlob *blob); bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec); bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *intence); napi_value ConvertBigIntToNapiValue(napi_env env, HcfBigInteger *blob); -napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpec *blob); -napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob); -napi_value ConvertEccPointToNapiValue(napi_env env, HcfPoint *p); bool GetPointFromNapiValue(napi_env env, napi_value arg, HcfPoint *point); bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr); 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 86945889aa63b703dd977e5aa8d2e46d07c01974..6f529e94c3b53a9faddf728cfe23f983c886666b 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -317,15 +317,44 @@ static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKey } } -static bool BuildConvertPemKeyCtx(napi_env env, napi_callback_info info, ConvertPemKeyCtx *ctx) +static bool ValidateAndGetParams(napi_env env, napi_callback_info info, std::string &pubKey, std::string &priKey, + HcfParamsSpec **paramsSpec) { napi_value thisVar = nullptr; - size_t expectedArgc = PARAMS_NUM_TWO; + size_t expectedArgc = PARAMS_NUM_THREE; size_t argc = expectedArgc; - napi_value argv[PARAMS_NUM_TWO] = { nullptr }; + napi_value argv[PARAMS_NUM_THREE] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (argc != expectedArgc) { + if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) { LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc, argc); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters.")); + return false; + } + + if (!GetPkAndSkStringFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], pubKey, priKey)) { + LOGE("GetPkAndSkStringFromNapiValueIfInput failed."); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "GetPkAndSkStringFromNapiValueIfInput failed.")); + return false; + } + + if (argc == expectedArgc) { + if (!GetDecodingParamsSpec(env, argv[PARAM2], paramsSpec)) { + LOGE("get params failed!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get napi paramsSpec failed!")); + return false; + } + } + return true; +} + +static bool BuildConvertPemKeyCtx(napi_env env, napi_callback_info info, ConvertPemKeyCtx *ctx) +{ + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + std::string pubKey; + std::string priKey; + HcfParamsSpec *paramsSpec = nullptr; + if (!ValidateAndGetParams(env, info, pubKey, priKey, ¶msSpec)) { return false; } NapiAsyKeyGenerator *napiGenerator = nullptr; @@ -334,14 +363,9 @@ 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 = ""; - if (!GetPkAndSkStringFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], pubKey, priKey)) { - LOGE("GetPkAndSkStringFromNapiValueIfInput failed."); - return false; - } + ctx->generator = napiGenerator->GetAsyKeyGenerator(); - ctx->params = nullptr; + ctx->params = paramsSpec; ctx->pubKey = pubKey; ctx->priKey = priKey; if (napi_create_reference(env, thisVar, 1, &ctx->generatorRef) != napi_ok) { @@ -733,7 +757,6 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callbac napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "failed to get generate key pair instance!")); return nullptr; } - return instance; } @@ -838,9 +861,9 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKey(napi_env env, napi_callback_info } static HcfResult ConvertPemKeySync(std::string &pubKey, std::string &priKey, HcfAsyKeyGenerator *generator, - HcfKeyPair **returnKeyPair) + HcfParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) { - HcfResult errCode = generator->convertPemKey(generator, nullptr, + HcfResult errCode = generator->convertPemKey(generator, paramsSpec, pubKey.c_str(), priKey.c_str(), returnKeyPair); if (errCode != HCF_SUCCESS) { LOGE("convertPemKey error!"); @@ -852,42 +875,36 @@ static HcfResult ConvertPemKeySync(std::string &pubKey, std::string &priKey, Hc napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; - size_t expectedArgc = PARAMS_NUM_TWO; - size_t argc = expectedArgc; - napi_value argv[PARAMS_NUM_TWO] = { nullptr }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (argc != expectedArgc) { - LOGE("wrong argument num. require %zu arguments. [Argc]: %zu!", expectedArgc, argc); + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + std::string pubKey; + std::string priKey; + HcfParamsSpec *paramsSpec = nullptr; + if (!ValidateAndGetParams(env, info, pubKey, priKey, ¶msSpec)) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "invalid parameters.")); - return nullptr; + return NapiGetNull(env); } NapiAsyKeyGenerator *napiGenerator = nullptr; napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiGenerator)); if (status != napi_ok || napiGenerator == nullptr) { + HcfFree(paramsSpec); LOGE("failed to unwrap napi asyKeyGenerator obj."); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap napi asyKeyGenerator obj.")); return nullptr; } - std::string pubKey = ""; - std::string priKey = ""; - if (!GetPkAndSkStringFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], pubKey, priKey)) { - LOGE("GetPkAndSkStringFromNapiValueIfInput failed."); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "GetPkAndSkStringFromNapiValueIfInput failed.")); - return nullptr; - } - HcfAsyKeyGenerator *generator = napiGenerator->GetAsyKeyGenerator(); if (generator == nullptr) { + HcfFree(paramsSpec); LOGE("GetAsyKeyGenerator failed!"); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "GetAsyKeyGenerator failed!")); return nullptr; } HcfKeyPair *returnKeyPair = nullptr; - HcfResult errCode = ConvertPemKeySync(pubKey, priKey, generator, &(returnKeyPair)); + HcfResult errCode = ConvertPemKeySync(pubKey, priKey, generator, paramsSpec, &(returnKeyPair)); if (errCode != HCF_SUCCESS) { + HcfFree(paramsSpec); LOGE("ConvertPemKeySync error!"); napi_throw(env, GenerateBusinessError(env, errCode, "ConvertPemKeySync error!")); return nullptr; @@ -895,6 +912,7 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(returnKeyPair); if (napiKeyPair == nullptr) { + HcfFree(paramsSpec); LOGE("new napi key pair failed"); napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc context failed.")); HcfObjDestroy(returnKeyPair); @@ -903,6 +921,9 @@ napi_value NapiAsyKeyGenerator::JsConvertPemKeySync(napi_env env, napi_callback_ } napi_value instance = napiKeyPair->ConvertToJsKeyPair(env); + if (paramsSpec != nullptr) { + HcfFree(paramsSpec); + } return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp b/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp index 7174c1dbea309fadc9346b7a47baccc55e75163f..24e364bb8b1bcf4a54c52501837116ea3dd07f74 100644 --- a/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp +++ b/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp @@ -23,6 +23,7 @@ #include "napi_key_pair.h" #include "napi_pri_key.h" #include "napi_pub_key.h" +#include "utils.h" namespace OHOS { namespace CryptoFramework { @@ -30,6 +31,110 @@ NapiDHKeyUtil::NapiDHKeyUtil() {} NapiDHKeyUtil::~NapiDHKeyUtil() {} + +static bool BuildDhInstanceToNapiValueSub(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance) +{ + if (!BuildSetNamedProperty(env, &(blob->p), "p", instance)) { + LOGE("build setNamedProperty a failed!"); + return false; + } + if (!BuildSetNamedProperty(env, &(blob->g), "g", instance)) { + LOGE("build setNamedProperty b failed!"); + return false; + } + napi_value length; + napi_status status = napi_create_int32(env, blob->length, &length); + if (status != napi_ok) { + LOGE("create length number failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "l", length); + if (status != napi_ok) { + LOGE("create length number failed!"); + return false; + } + return true; +} + +static bool BuildDhInstanceToNapiValue(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance) +{ + napi_value algName; + size_t algNameLength = HcfStrlen(blob->base.algName); + if (!algNameLength) { + LOGE("algName is empty!"); + return false; + } + napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName); + if (status != napi_ok) { + LOGE("create algName failed!"); + return false; + } + napi_value specType; + status = napi_create_uint32(env, blob->base.specType, &specType); + if (status != napi_ok) { + LOGE("create uint32 failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "algName", algName); + if (status != napi_ok) { + LOGE("create set algName failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "specType", specType); + if (status != napi_ok) { + LOGE("create set specType failed!"); + return false; + } + if (!BuildDhInstanceToNapiValueSub(env, blob, instance)) { + LOGE("create intance parter napi value failed!"); + return false; + } + return true; +} + +static bool CheckDhCommonParamSpec(napi_env env, HcfDhCommParamsSpec *blob) +{ + if (blob == nullptr) { + LOGE("Invalid blob!"); + return false; + } + if (blob->base.algName == nullptr) { + LOGE("Invalid blob algName!"); + return false; + } + if (blob->p.data == nullptr || blob->p.len == 0) { + LOGE("Invalid blob a!"); + return false; + } + if (blob->g.data == nullptr || blob->g.len == 0) { + LOGE("Invalid blob point x!"); + return false; + } + return true; +} + +static napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob) +{ + if (!CheckDhCommonParamSpec(env, blob)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!")); + LOGE("Invalid blob!"); + return NapiGetNull(env); + } + napi_value instance; + napi_status status = napi_create_object(env, &instance); + if (status != napi_ok) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!")); + LOGE("create object failed!"); + return NapiGetNull(env); + } + if (!BuildDhInstanceToNapiValue(env, blob, &instance)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!")); + LOGE("Build object failed!"); + return NapiGetNull(env); + } + return instance; +} + napi_value NapiDHKeyUtil::JsGenDHCommonParamsSpec(napi_env env, napi_callback_info info) { size_t expectedArgc = PARAMS_NUM_TWO; diff --git a/frameworks/js/napi/crypto/src/napi_ecc_key_util.cpp b/frameworks/js/napi/crypto/src/napi_ecc_key_util.cpp index 0cd9ceed90d52c09e6b4f0c5607b6e1e924e50f8..eb11d77b8acf89a6fa67f3ae0f73474d276a02f9 100644 --- a/frameworks/js/napi/crypto/src/napi_ecc_key_util.cpp +++ b/frameworks/js/napi/crypto/src/napi_ecc_key_util.cpp @@ -24,6 +24,7 @@ #include "napi_key_pair.h" #include "napi_pri_key.h" #include "napi_pub_key.h" +#include "utils.h" namespace OHOS { namespace CryptoFramework { @@ -31,6 +32,294 @@ NapiECCKeyUtil::NapiECCKeyUtil() {} NapiECCKeyUtil::~NapiECCKeyUtil() {} +static bool CheckEccCommonParamSpecBase(napi_env env, HcfEccCommParamsSpec *blob) +{ + if (blob->a.data == nullptr || blob->a.len == 0) { + LOGE("Invalid blob a!"); + return false; + } + if (blob->b.data == nullptr || blob->b.len == 0) { + LOGE("Invalid blob b!"); + return false; + } + if (blob->n.data == nullptr || blob->n.len == 0) { + LOGE("Invalid blob n!"); + return false; + } + return true; +} + +static bool CheckEccCommonParamSpec(napi_env env, HcfEccCommParamsSpec *blob) +{ + if (blob == nullptr) { + LOGE("Invalid blob!"); + return false; + } + if (!CheckEccCommonParamSpecBase(env, blob)) { + LOGE("Invalid blob ecc commonParamSpec base!"); + return false; + } + if (blob->base.algName == nullptr) { + LOGE("Invalid blob algName!"); + return false; + } + if (blob->field == nullptr) { + LOGE("Invalid blob field!"); + return false; + } + if (blob->field->fieldType == nullptr) { + LOGE("Invalid blob fieldType!"); + return false; + } + if (blob->g.x.data == nullptr || blob->g.x.len == 0) { + LOGE("Invalid blob point x!"); + return false; + } + if (blob->g.y.data == nullptr || blob->g.y.len == 0) { + LOGE("Invalid blob point y!"); + return false; + } + HcfECFieldFp *tmpField = reinterpret_cast(blob->field); + if (tmpField->p.data == nullptr || tmpField->p.len == 0) { + LOGE("Invalid blob p!"); + return false; + } + return true; +} + +static napi_value ConvertEccCommonParamFieldFpToNapiValue(napi_env env, HcfEccCommParamsSpec *blob) +{ + napi_value fieldFp; + napi_value fieldType; + napi_status status = napi_create_object(env, &fieldFp); + if (status != napi_ok) { + LOGE("create fieldFp failed!"); + return NapiGetNull(env); + } + size_t fieldTypeLength = HcfStrlen(blob->field->fieldType); + if (!fieldTypeLength) { + LOGE("fieldType is empty!"); + return NapiGetNull(env); + } + status = napi_create_string_utf8(env, blob->field->fieldType, fieldTypeLength, &fieldType); + if (status != napi_ok) { + LOGE("create object failed!"); + return NapiGetNull(env); + } + status = napi_set_named_property(env, fieldFp, "fieldType", fieldType); + if (status != napi_ok) { + LOGE("create object failed!"); + return NapiGetNull(env); + } + HcfECFieldFp *tmpField = reinterpret_cast(blob->field); + napi_value p = ConvertBigIntToNapiValue(env, &(tmpField->p)); + if (p == nullptr) { + LOGE("p is null!"); + return NapiGetNull(env); + } + status = napi_set_named_property(env, fieldFp, "p", p); + if (status != napi_ok) { + LOGE("create object failed!"); + return NapiGetNull(env); + } + return fieldFp; +} + +static bool IsNapiNull(napi_env env, napi_value value) +{ + napi_valuetype valueType; + napi_typeof(env, value, &valueType); + return (valueType == napi_null); +} + +static napi_value ConvertEccPointToNapiValue(napi_env env, HcfPoint *p) +{ + if (p == nullptr) { + LOGE("Invalid point data!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid point data!")); + return nullptr; + } + + napi_value point; + napi_status status = napi_create_object(env, &point); + if (status != napi_ok) { + LOGE("create object failed!"); + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "create object failed!")); + return nullptr; + } + + napi_value x = ConvertBigIntToNapiValue(env, &(p->x)); + if (x == nullptr || IsNapiNull(env, x)) { + LOGE("Failed to convert x to NapiValue!"); + return nullptr; + } + + napi_value y = ConvertBigIntToNapiValue(env, &(p->y)); + if (y == nullptr || IsNapiNull(env, y)) { + LOGE("Failed to convert y to NapiValue!"); + return nullptr; + } + + status = napi_set_named_property(env, point, "x", x); + if (status != napi_ok) { + LOGE("set x property failed!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set x property failed!")); + return nullptr; + } + + status = napi_set_named_property(env, point, "y", y); + if (status != napi_ok) { + LOGE("set y property failed!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set y property failed!")); + return nullptr; + } + + return point; +} + +static napi_value ConvertEccCommonParamPointToNapiValue(napi_env env, HcfEccCommParamsSpec *blob) +{ + napi_value point; + napi_status status = napi_create_object(env, &point); + if (status != napi_ok) { + LOGE("create object failed!"); + return NapiGetNull(env); + } + napi_value x = ConvertBigIntToNapiValue(env, &(blob->g.x)); + if (x == nullptr) { + LOGE("x is null!"); + return NapiGetNull(env); + } + + napi_value y = ConvertBigIntToNapiValue(env, &(blob->g.y)); + if (y == nullptr) { + LOGE("y is null!"); + return NapiGetNull(env); + } + status = napi_set_named_property(env, point, "x", x); + if (status != napi_ok) { + LOGE("create object failed!"); + return NapiGetNull(env); + } + status = napi_set_named_property(env, point, "y", y); + if (status != napi_ok) { + LOGE("create object failed!"); + return NapiGetNull(env); + } + return point; +} + +static bool BuildIntancePartertoNapiValueSon(napi_env env, napi_status status, HcfEccCommParamsSpec *blob, + napi_value *instance) +{ + if (!BuildSetNamedProperty(env, &(blob->a), "a", instance)) { + LOGE("build setNamedProperty a failed!"); + return false; + } + if (!BuildSetNamedProperty(env, &(blob->b), "b", instance)) { + LOGE("build setNamedProperty b failed!"); + return false; + } + if (!BuildSetNamedProperty(env, &(blob->n), "n", instance)) { + LOGE("build setNamedProperty n failed!"); + return false; + } + napi_value h; + status = napi_create_int32(env, blob->h, &h); + if (status != napi_ok) { + LOGE("create h uint32 failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "h", h); + if (status != napi_ok) { + LOGE("create h uint32 failed!"); + return false; + } + return true; +} + +static bool BuildInstanceParterToNapiValue(napi_env env, HcfEccCommParamsSpec *blob, napi_value *instance) +{ + napi_value algName; + size_t algNameLength = HcfStrlen(blob->base.algName); + if (!algNameLength) { + LOGE("algName is empty!"); + return false; + } + napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName); + if (status != napi_ok) { + LOGE("create algName failed!"); + return false; + } + napi_value specType; + status = napi_create_uint32(env, blob->base.specType, &specType); + if (status != napi_ok) { + LOGE("create uint32 failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "algName", algName); + if (status != napi_ok) { + LOGE("create set algName failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "specType", specType); + if (status != napi_ok) { + LOGE("create set specType failed!"); + return false; + } + if (!BuildIntancePartertoNapiValueSon(env, status, blob, instance)) { + LOGE("create intance parter napi value failed!"); + return false; + } + return true; +} + +static napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpec *blob) +{ + if (!CheckEccCommonParamSpec(env, blob)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!")); + LOGE("Invalid blob!"); + return NapiGetNull(env); + } + napi_value instance; + napi_status status = napi_create_object(env, &instance); + if (status != napi_ok) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!")); + LOGE("create object failed!"); + return NapiGetNull(env); + } + napi_value point = ConvertEccCommonParamPointToNapiValue(env, blob); + if (point == NapiGetNull(env)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "covert commonParam failed!")); + LOGE("Covert commonParam failed!"); + return NapiGetNull(env); + } + napi_value field = ConvertEccCommonParamFieldFpToNapiValue(env, blob); + if (field == NapiGetNull(env)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "covert commonParam fieldFp failed!")); + LOGE("Covert commonParam fieldFp failed!"); + return NapiGetNull(env); + } + if (!BuildInstanceParterToNapiValue(env, blob, &instance)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!")); + LOGE("Build object failed!"); + return NapiGetNull(env); + } + status = napi_set_named_property(env, instance, "field", field); + if (status != napi_ok) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set fieldFp failed!")); + LOGE("set fieldFp failed!"); + return NapiGetNull(env); + } + status = napi_set_named_property(env, instance, "g", point); + if (status != napi_ok) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set g failed!")); + LOGE("set g failed!"); + return NapiGetNull(env); + } + return instance; +} + napi_value NapiECCKeyUtil::JsGenECCCommonParamsSpec(napi_env env, napi_callback_info info) { size_t expectedArgc = ARGS_SIZE_ONE; diff --git a/frameworks/js/napi/crypto/src/napi_pri_key.cpp b/frameworks/js/napi/crypto/src/napi_pri_key.cpp index 25ae8f2c2310b7439bd7d7b824a5c8071b6b407c..23d990b6da55aa3a9dfc0a8e039079b29e2fc260 100644 --- a/frameworks/js/napi/crypto/src/napi_pri_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_pri_key.cpp @@ -100,44 +100,64 @@ napi_value NapiPriKey::JsGetEncoded(napi_env env, napi_callback_info info) return instance; } -napi_value NapiPriKey::JsGetEncodedPem(napi_env env, napi_callback_info info) +static bool ValidateAndGetParams(napi_env env, napi_callback_info info, std::string &format, + HcfParamsSpec **paramsSpec, NapiPriKey **napiPriKey) { - size_t expectedArgc = PARAMS_NUM_ONE; + size_t expectedArgc = PARAMS_NUM_TWO; size_t argc = expectedArgc; - napi_value argv[PARAMS_NUM_ONE] = { nullptr }; napi_value thisVar = nullptr; + napi_value argv[PARAMS_NUM_TWO] = { nullptr }; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - if (argc != expectedArgc) { + if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) { LOGE("The input args num is invalid."); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid.")); - return NapiGetNull(env); + return false; } - std::string format = ""; if (!GetStringFromJSParams(env, argv[0], format)) { LOGE("failed to get formatStr."); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get formatStr.")); - return NapiGetNull(env); + return false; } - NapiPriKey *napiPriKey = nullptr; - napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiPriKey)); + if (argc == expectedArgc) { + if (!GetEncodingParamsSpec(env, argv[1], paramsSpec)) { + LOGE("get params failed!"); + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "get napi paramsSpec failed!")); + return false; + } + } + + 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!")); - return nullptr; + return false; + } + return true; +} + +napi_value NapiPriKey::JsGetEncodedPem(napi_env env, napi_callback_info info) +{ + std::string format; + HcfParamsSpec *paramsSpec = nullptr; + NapiPriKey *napiPriKey = nullptr; + if (!ValidateAndGetParams(env, info, format, ¶msSpec, &napiPriKey)) { + return NapiGetNull(env); } HcfPriKey *priKey = napiPriKey->GetPriKey(); if (priKey == nullptr) { + HcfFree(paramsSpec); LOGE("failed to get priKey obj!"); napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get priKey obj!")); return nullptr; } char *returnString = nullptr; - HcfResult res = priKey->base.getEncodedPem(&priKey->base, format.c_str(), &returnString); + HcfResult res = priKey->getEncodedPem(priKey, paramsSpec, format.c_str(), &returnString); if (res != HCF_SUCCESS) { + HcfFree(paramsSpec); LOGE("getEncodedPem fail."); napi_throw(env, GenerateBusinessError(env, res, "getEncodedPem fail.")); return nullptr; @@ -145,6 +165,9 @@ napi_value NapiPriKey::JsGetEncodedPem(napi_env env, napi_callback_info info) napi_value instance = nullptr; napi_create_string_utf8(env, returnString, NAPI_AUTO_LENGTH, &instance); HcfFree(returnString); + if (paramsSpec != nullptr) { + HcfFree(paramsSpec); + } return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp index b95168b4423df91fa8154c8823f17be39d35aee1..556f8a3208ff76d0abc08e634a7f0323aeba4008 100644 --- a/frameworks/js/napi/crypto/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -28,11 +28,14 @@ #include "detailed_alg_25519_key_params.h" #include "detailed_dh_key_params.h" #include "utils.h" +#include "pri_key.h" +#include "asy_key_generator.h" namespace OHOS { namespace CryptoFramework { using namespace std; +constexpr int PASSWORD_MAX_LENGTH = 4096; struct AsyKeySpecItemRelationT { AsyKeySpecItem item; int32_t itemType; @@ -546,6 +549,165 @@ bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMo } } +static bool GetCharArrayFromJsString(napi_env env, napi_value arg, HcfBlob *retBlob) +{ + size_t length = 0; + if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) { + LOGE("can not get char string length"); + return false; + } + if (length > PASSWORD_MAX_LENGTH) { + LOGE("password length should not exceed 4096"); + return false; + } + if (length == 0) { + LOGD("empty string"); + return false; + } + char *tmpPassword = static_cast(HcfMalloc(length + 1, 0)); + if (tmpPassword == nullptr) { + LOGE("malloc string failed"); + return false; + } + if (napi_get_value_string_utf8(env, arg, tmpPassword, (length + 1), &length) != napi_ok) { + LOGE("can not get char string value"); + HcfFree(tmpPassword); + return false; + } + retBlob->data = reinterpret_cast(tmpPassword); + retBlob->len = length; + return true; +} + +static bool InitEncodingParams(napi_env env, napi_value arg, HcfKeyEncodingParamsSpec *spec, HcfBlob *tmpPw, + HcfBlob *tmpCipher) +{ + napi_value passWd = GetDetailAsyKeySpecValue(env, arg, PASSWD_PARAMS); + napi_value cipher = GetDetailAsyKeySpecValue(env, arg, CIPHER_PARAMS); + if ((passWd == nullptr) || (cipher == nullptr)) { + LOGE("Invalid params."); + return false; + } + + if (!GetCharArrayFromJsString(env, passWd, tmpPw)) { + LOGE("Failed to get passWord string from napi!"); + return false; + } + + if (!GetCharArrayFromJsString(env, cipher, tmpCipher)) { + LOGE("Failed to get cipher string from napi!"); + HcfBlobDataClearAndFree(tmpPw); + return false; + } + + spec->cipher = reinterpret_cast(tmpCipher->data); + spec->password = reinterpret_cast(tmpPw->data); + if ((spec->cipher == nullptr) || (spec->password == nullptr)) { + LOGE("cipher or password data is null!"); + return false; + } + return true; +} + +bool GetEncodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnSpec) +{ + if ((env == nullptr) || (arg == nullptr) || (returnSpec == nullptr)) { + LOGE("Invalid params."); + return false; + } + + HcfKeyEncodingParamsSpec *encodingParamsSpec = + reinterpret_cast(HcfMalloc(sizeof(HcfKeyEncodingParamsSpec), 0)); + if (encodingParamsSpec == nullptr) { + LOGE("encodingParamsSpec malloc failed!"); + return false; + } + + HcfBlob tmpPw = { .data = nullptr, .len = 0 }; + HcfBlob tmpCipher = { .data = nullptr, .len = 0 }; + if (!InitEncodingParams(env, arg, encodingParamsSpec, &tmpPw, &tmpCipher)) { + LOGE("Failed to get passWord string from napi!"); + HcfFree(encodingParamsSpec); + return false; + } + *returnSpec = reinterpret_cast(encodingParamsSpec); + return true; +} + +static HcfBlob *GetBlobFromStringJSParams(napi_env env, napi_value arg) +{ + napi_valuetype valueType; + napi_typeof(env, arg, &valueType); + if (valueType != napi_string) { + LOGE("wrong argument type. expect string type. [Type]: %d", valueType); + return nullptr; + } + + size_t length = 0; + if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) { + LOGE("can not get string length"); + return nullptr; + } + + if (length == 0) { + LOGE("string length is 0"); + return nullptr; + } + + HcfBlob *newBlob = static_cast(HcfMalloc(sizeof(HcfBlob), 0)); + if (newBlob == nullptr) { + LOGE("Failed to allocate newBlob memory!"); + return nullptr; + } + + newBlob->len = length + 1; + newBlob->data = static_cast(HcfMalloc(newBlob->len, 0)); + if (newBlob->data == nullptr) { + LOGE("malloc blob data failed!"); + HcfFree(newBlob); + return nullptr; + } + + if (napi_get_value_string_utf8(env, arg, reinterpret_cast(newBlob->data), newBlob->len, &length) != + napi_ok) { + LOGE("can not get string value"); + HcfBlobDataClearAndFree(newBlob); + HcfFree(newBlob); + return nullptr; + } + + return newBlob; +} + +bool GetDecodingParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **returnSpec) +{ + HcfKeyDecodingParamsSpec *decodingParamsSpec = + reinterpret_cast(HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0)); + if (decodingParamsSpec == nullptr) { + LOGE("decodingParamsSpec malloc failed!"); + return false; + } + + HcfBlob *tmpPw = GetBlobFromStringJSParams(env, arg); + if (tmpPw == nullptr) { + LOGE("Failed to get passWord string from napi!"); + HcfFree(decodingParamsSpec); + return false; + } + if (tmpPw->len > PASSWORD_MAX_LENGTH) { + LOGE("Password length exceeds max length limit of 4096 bytes!"); + HcfBlobDataClearAndFree(tmpPw); + HcfFree(decodingParamsSpec); + return false; + } + decodingParamsSpec->password = reinterpret_cast(tmpPw->data); + tmpPw->data = nullptr; + + *returnSpec = reinterpret_cast(decodingParamsSpec); + HcfFree(tmpPw); + return true; +} + napi_value GetDetailAsyKeySpecValue(napi_env env, napi_value arg, string argName) { napi_value data = nullptr; @@ -1792,183 +1954,6 @@ napi_value GetResourceName(napi_env env, const char *name) return resourceName; } -static bool CheckEccCommonParamSpecBase(napi_env env, HcfEccCommParamsSpec *blob) -{ - if (blob->a.data == nullptr || blob->a.len == 0) { - LOGE("Invalid blob a!"); - return false; - } - if (blob->b.data == nullptr || blob->b.len == 0) { - LOGE("Invalid blob b!"); - return false; - } - if (blob->n.data == nullptr || blob->n.len == 0) { - LOGE("Invalid blob n!"); - return false; - } - return true; -} - -static bool CheckEccCommonParamSpec(napi_env env, HcfEccCommParamsSpec *blob) -{ - if (blob == nullptr) { - LOGE("Invalid blob!"); - return false; - } - if (!CheckEccCommonParamSpecBase(env, blob)) { - LOGE("Invalid blob ecc commonParamSpec base!"); - return false; - } - if (blob->base.algName == nullptr) { - LOGE("Invalid blob algName!"); - return false; - } - if (blob->field == nullptr) { - LOGE("Invalid blob field!"); - return false; - } - if (blob->field->fieldType == nullptr) { - LOGE("Invalid blob fieldType!"); - return false; - } - if (blob->g.x.data == nullptr || blob->g.x.len == 0) { - LOGE("Invalid blob point x!"); - return false; - } - if (blob->g.y.data == nullptr || blob->g.y.len == 0) { - LOGE("Invalid blob point y!"); - return false; - } - HcfECFieldFp *tmpField = reinterpret_cast(blob->field); - if (tmpField->p.data == nullptr || tmpField->p.len == 0) { - LOGE("Invalid blob p!"); - return false; - } - return true; -} - -static napi_value ConvertEccCommonParamFieldFpToNapiValue(napi_env env, HcfEccCommParamsSpec *blob) -{ - napi_value fieldFp; - napi_value fieldType; - napi_status status = napi_create_object(env, &fieldFp); - if (status != napi_ok) { - LOGE("create fieldFp failed!"); - return NapiGetNull(env); - } - size_t fieldTypeLength = HcfStrlen(blob->field->fieldType); - if (!fieldTypeLength) { - LOGE("fieldType is empty!"); - return NapiGetNull(env); - } - status = napi_create_string_utf8(env, blob->field->fieldType, fieldTypeLength, &fieldType); - if (status != napi_ok) { - LOGE("create object failed!"); - return NapiGetNull(env); - } - status = napi_set_named_property(env, fieldFp, "fieldType", fieldType); - if (status != napi_ok) { - LOGE("create object failed!"); - return NapiGetNull(env); - } - HcfECFieldFp *tmpField = reinterpret_cast(blob->field); - napi_value p = ConvertBigIntToNapiValue(env, &(tmpField->p)); - if (p == nullptr) { - LOGE("p is null!"); - return NapiGetNull(env); - } - status = napi_set_named_property(env, fieldFp, "p", p); - if (status != napi_ok) { - LOGE("create object failed!"); - return NapiGetNull(env); - } - return fieldFp; -} - -static bool IsNapiNull(napi_env env, napi_value value) -{ - napi_valuetype valueType; - napi_typeof(env, value, &valueType); - return (valueType == napi_null); -} - -napi_value ConvertEccPointToNapiValue(napi_env env, HcfPoint *p) -{ - if (p == nullptr) { - LOGE("Invalid point data!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid point data!")); - return nullptr; - } - - napi_value point; - napi_status status = napi_create_object(env, &point); - if (status != napi_ok) { - LOGE("create object failed!"); - napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "create object failed!")); - return nullptr; - } - - napi_value x = ConvertBigIntToNapiValue(env, &(p->x)); - if (x == nullptr || IsNapiNull(env, x)) { - LOGE("Failed to convert x to NapiValue!"); - return nullptr; - } - - napi_value y = ConvertBigIntToNapiValue(env, &(p->y)); - if (y == nullptr || IsNapiNull(env, y)) { - LOGE("Failed to convert y to NapiValue!"); - return nullptr; - } - - status = napi_set_named_property(env, point, "x", x); - if (status != napi_ok) { - LOGE("set x property failed!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set x property failed!")); - return nullptr; - } - - status = napi_set_named_property(env, point, "y", y); - if (status != napi_ok) { - LOGE("set y property failed!"); - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set y property failed!")); - return nullptr; - } - - return point; -} - -static napi_value ConvertEccCommonParamPointToNapiValue(napi_env env, HcfEccCommParamsSpec *blob) -{ - napi_value point; - napi_status status = napi_create_object(env, &point); - if (status != napi_ok) { - LOGE("create object failed!"); - return NapiGetNull(env); - } - napi_value x = ConvertBigIntToNapiValue(env, &(blob->g.x)); - if (x == nullptr) { - LOGE("x is null!"); - return NapiGetNull(env); - } - - napi_value y = ConvertBigIntToNapiValue(env, &(blob->g.y)); - if (y == nullptr) { - LOGE("y is null!"); - return NapiGetNull(env); - } - status = napi_set_named_property(env, point, "x", x); - if (status != napi_ok) { - LOGE("create object failed!"); - return NapiGetNull(env); - } - status = napi_set_named_property(env, point, "y", y); - if (status != napi_ok) { - LOGE("create object failed!"); - return NapiGetNull(env); - } - return point; -} - bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *instance) { napi_value value = ConvertBigIntToNapiValue(env, number); @@ -1979,219 +1964,5 @@ bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name } return true; } - -static bool BuildIntancePartertoNapiValueSon(napi_env env, napi_status status, HcfEccCommParamsSpec *blob, - napi_value *instance) -{ - if (!BuildSetNamedProperty(env, &(blob->a), "a", instance)) { - LOGE("build setNamedProperty a failed!"); - return false; - } - if (!BuildSetNamedProperty(env, &(blob->b), "b", instance)) { - LOGE("build setNamedProperty b failed!"); - return false; - } - if (!BuildSetNamedProperty(env, &(blob->n), "n", instance)) { - LOGE("build setNamedProperty n failed!"); - return false; - } - napi_value h; - status = napi_create_int32(env, blob->h, &h); - if (status != napi_ok) { - LOGE("create h uint32 failed!"); - return false; - } - status = napi_set_named_property(env, *instance, "h", h); - if (status != napi_ok) { - LOGE("create h uint32 failed!"); - return false; - } - return true; -} - -static bool BuildInstanceParterToNapiValue(napi_env env, HcfEccCommParamsSpec *blob, napi_value *instance) -{ - napi_value algName; - size_t algNameLength = HcfStrlen(blob->base.algName); - if (!algNameLength) { - LOGE("algName is empty!"); - return false; - } - napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName); - if (status != napi_ok) { - LOGE("create algName failed!"); - return false; - } - napi_value specType; - status = napi_create_uint32(env, blob->base.specType, &specType); - if (status != napi_ok) { - LOGE("create uint32 failed!"); - return false; - } - status = napi_set_named_property(env, *instance, "algName", algName); - if (status != napi_ok) { - LOGE("create set algName failed!"); - return false; - } - status = napi_set_named_property(env, *instance, "specType", specType); - if (status != napi_ok) { - LOGE("create set specType failed!"); - return false; - } - if (!BuildIntancePartertoNapiValueSon(env, status, blob, instance)) { - LOGE("create intance parter napi value failed!"); - return false; - } - return true; -} - -napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpec *blob) -{ - if (!CheckEccCommonParamSpec(env, blob)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!")); - LOGE("Invalid blob!"); - return NapiGetNull(env); - } - napi_value instance; - napi_status status = napi_create_object(env, &instance); - if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!")); - LOGE("create object failed!"); - return NapiGetNull(env); - } - napi_value point = ConvertEccCommonParamPointToNapiValue(env, blob); - if (point == NapiGetNull(env)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "covert commonParam failed!")); - LOGE("Covert commonParam failed!"); - return NapiGetNull(env); - } - napi_value field = ConvertEccCommonParamFieldFpToNapiValue(env, blob); - if (field == NapiGetNull(env)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "covert commonParam fieldFp failed!")); - LOGE("Covert commonParam fieldFp failed!"); - return NapiGetNull(env); - } - if (!BuildInstanceParterToNapiValue(env, blob, &instance)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!")); - LOGE("Build object failed!"); - return NapiGetNull(env); - } - status = napi_set_named_property(env, instance, "field", field); - if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set fieldFp failed!")); - LOGE("set fieldFp failed!"); - return NapiGetNull(env); - } - status = napi_set_named_property(env, instance, "g", point); - if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set g failed!")); - LOGE("set g failed!"); - return NapiGetNull(env); - } - return instance; -} - -static bool BuildDhInstanceToNapiValueSub(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance) -{ - if (!BuildSetNamedProperty(env, &(blob->p), "p", instance)) { - LOGE("build setNamedProperty a failed!"); - return false; - } - if (!BuildSetNamedProperty(env, &(blob->g), "g", instance)) { - LOGE("build setNamedProperty b failed!"); - return false; - } - napi_value length; - napi_status status = napi_create_int32(env, blob->length, &length); - if (status != napi_ok) { - LOGE("create length number failed!"); - return false; - } - status = napi_set_named_property(env, *instance, "l", length); - if (status != napi_ok) { - LOGE("create length number failed!"); - return false; - } - return true; -} - -static bool BuildDhInstanceToNapiValue(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance) -{ - napi_value algName; - size_t algNameLength = HcfStrlen(blob->base.algName); - if (!algNameLength) { - LOGE("algName is empty!"); - return false; - } - napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName); - if (status != napi_ok) { - LOGE("create algName failed!"); - return false; - } - napi_value specType; - status = napi_create_uint32(env, blob->base.specType, &specType); - if (status != napi_ok) { - LOGE("create uint32 failed!"); - return false; - } - status = napi_set_named_property(env, *instance, "algName", algName); - if (status != napi_ok) { - LOGE("create set algName failed!"); - return false; - } - status = napi_set_named_property(env, *instance, "specType", specType); - if (status != napi_ok) { - LOGE("create set specType failed!"); - return false; - } - if (!BuildDhInstanceToNapiValueSub(env, blob, instance)) { - LOGE("create intance parter napi value failed!"); - return false; - } - return true; -} - -static bool CheckDhCommonParamSpec(napi_env env, HcfDhCommParamsSpec *blob) -{ - if (blob == nullptr) { - LOGE("Invalid blob!"); - return false; - } - if (blob->base.algName == nullptr) { - LOGE("Invalid blob algName!"); - return false; - } - if (blob->p.data == nullptr || blob->p.len == 0) { - LOGE("Invalid blob a!"); - return false; - } - if (blob->g.data == nullptr || blob->g.len == 0) { - LOGE("Invalid blob point x!"); - return false; - } - return true; -} - -napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob) -{ - if (!CheckDhCommonParamSpec(env, blob)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!")); - LOGE("Invalid blob!"); - return NapiGetNull(env); - } - napi_value instance; - napi_status status = napi_create_object(env, &instance); - if (status != napi_ok) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!")); - LOGE("create object failed!"); - return NapiGetNull(env); - } - if (!BuildDhInstanceToNapiValue(env, blob, &instance)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!")); - LOGE("Build object failed!"); - return NapiGetNull(env); - } - return instance; -} } // namespace CryptoFramework } // namespace OHOS diff --git a/interfaces/inner_api/key/asy_key_generator.h b/interfaces/inner_api/key/asy_key_generator.h index b89c518aba2e84eeb0495dc486bce8db4d3914d0..f56f243fe07d09c9d1837fc056ed0ac993397aad 100644 --- a/interfaces/inner_api/key/asy_key_generator.h +++ b/interfaces/inner_api/key/asy_key_generator.h @@ -90,6 +90,14 @@ struct HcfAsyKeyGeneratorBySpec { const char *(*getAlgName)(const HcfAsyKeyGeneratorBySpec *self); }; +typedef struct HcfKeyDecodingParamsSpec HcfKeyDecodingParamsSpec; + +struct HcfKeyDecodingParamsSpec { + HcfParamsSpec base; + + const char *password; +}; + #ifdef __cplusplus extern "C" { #endif diff --git a/interfaces/inner_api/key/key.h b/interfaces/inner_api/key/key.h index 6f3b2fe1b9449e04c79e7d87cf5748ce7f4abc6b..d5e8074fb07988dd659e44766902a1728dc71c6d 100644 --- a/interfaces/inner_api/key/key.h +++ b/interfaces/inner_api/key/key.h @@ -19,6 +19,7 @@ #include "blob.h" #include "result.h" #include "object_base.h" +#include "algorithm_parameter.h" typedef enum { DSA_P_BN = 101, diff --git a/interfaces/inner_api/key/pri_key.h b/interfaces/inner_api/key/pri_key.h index d2b2e5985f9cf8420ad51935decfd39da0cca992..aa1e89fdd0917c29cb5f35b11ec3babff81e7c2c 100644 --- a/interfaces/inner_api/key/pri_key.h +++ b/interfaces/inner_api/key/pri_key.h @@ -33,7 +33,19 @@ struct HcfPriKey { HcfResult (*getEncodedDer)(const HcfPriKey *self, const char *format, HcfBlob *returnBlob); + HcfResult (*getEncodedPem)(const HcfPriKey *self, HcfParamsSpec *params, const char *format, char **returnString); + void (*clearMem)(HcfPriKey *self); }; +typedef struct HcfKeyEncodingParamsSpec HcfKeyEncodingParamsSpec; +struct HcfKeyEncodingParamsSpec { + HcfParamsSpec base; + + const char *password; + + const char *cipher; +}; + + #endif diff --git a/plugin/openssl_plugin/common/inc/openssl_adapter.h b/plugin/openssl_plugin/common/inc/openssl_adapter.h index ab212bca0326ba2af75f6b742f371e131d16db8f..d624f9c8b52f44b1b19a245b223c56c4e343202a 100644 --- a/plugin/openssl_plugin/common/inc/openssl_adapter.h +++ b/plugin/openssl_plugin/common/inc/openssl_adapter.h @@ -383,6 +383,7 @@ int OpensslEcPointGetAffineCoordinates(const EC_GROUP *group, const EC_POINT *p, OSSL_ENCODER_CTX *OpensslOsslEncoderCtxNewForPkey(const EVP_PKEY *pkey, int selection, const char *outputType, const char *outputStruct, const char *propquery); int OpensslOsslEncoderToData(OSSL_ENCODER_CTX *ctx, unsigned char **pdata, size_t *len); +int OpensslOsslDecoderCtxSetPassPhrase(OSSL_DECODER_CTX *ctx, const unsigned char *kstr, size_t klen); void OpensslOsslEncoderCtxFree(OSSL_ENCODER_CTX *ctx); OSSL_DECODER_CTX *OpensslOsslDecoderCtxNewForPkey(EVP_PKEY **pkey, const char *inputType, const char *inputStructure, const char *keytype, int selection, OSSL_LIB_CTX *libctx, const char *propquery); diff --git a/plugin/openssl_plugin/common/inc/openssl_common.h b/plugin/openssl_plugin/common/inc/openssl_common.h index d96d65fdff1fcfe465c45d36113f8fc948c1fb5b..083ad8bc256d6bc838c69798a5ffa1d093827e15 100644 --- a/plugin/openssl_plugin/common/inc/openssl_common.h +++ b/plugin/openssl_plugin/common/inc/openssl_common.h @@ -27,6 +27,7 @@ #include "params_parser.h" #include "result.h" #include "utils.h" +#include "algorithm_parameter.h" #define HCF_OPENSSL_SUCCESS 1 /* openssl return 1: success */ #define HCF_BITS_PER_BYTE 8 diff --git a/plugin/openssl_plugin/common/src/openssl_adapter.c b/plugin/openssl_plugin/common/src/openssl_adapter.c index f38dd44a94823421e4e63da068d9b240a8a97795..fb3b1e0d571fc416d96746e517a9c79abaaeda98 100644 --- a/plugin/openssl_plugin/common/src/openssl_adapter.c +++ b/plugin/openssl_plugin/common/src/openssl_adapter.c @@ -1473,6 +1473,11 @@ int OpensslOsslEncoderToData(OSSL_ENCODER_CTX *ctx, unsigned char **pdata, size_ return OSSL_ENCODER_to_data(ctx, pdata, len); } +int OpensslOsslDecoderCtxSetPassPhrase(OSSL_DECODER_CTX *ctx, const unsigned char *kstr, size_t klen) +{ + return OSSL_DECODER_CTX_set_passphrase(ctx, kstr, klen); +} + void OpensslOsslEncoderCtxFree(OSSL_ENCODER_CTX *ctx) { OSSL_ENCODER_CTX_free(ctx); diff --git a/plugin/openssl_plugin/common/src/openssl_common.c b/plugin/openssl_plugin/common/src/openssl_common.c index 668741ac20fa6dbb44114684221f3b5c684b5149..599792d0a777fe6c24cd095ef4b279581e7123cc 100644 --- a/plugin/openssl_plugin/common/src/openssl_common.c +++ b/plugin/openssl_plugin/common/src/openssl_common.c @@ -26,6 +26,7 @@ #include "result.h" #include "params_parser.h" #include "utils.h" +#include "pri_key.h" #define HCF_OPENSSL_DIGEST_NONE_STR "NONE" #define HCF_OPENSSL_DIGEST_MD5_STR "MD5" 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 d29922be2082c2320ce5f35ca595865497124e2b..13be947085665971b2349468d30448624da11967 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 @@ -234,9 +234,11 @@ static HcfResult GetAlg25519PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return HCF_SUCCESS; } -static HcfResult GetAlg25519PriKeyEncodedPem(HcfKey *self, const char *format, char **returnString) +static HcfResult GetAlg25519PriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format, + char **returnString) { (void)self; + (void)paramsSpec; (void)format; (void)returnString; return HCF_INVALID_PARAMS; @@ -509,7 +511,7 @@ static void FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey *sk) sk->base.base.base.getClass = GetAlg25519PriKeyClass; sk->base.base.getAlgorithm = GetAlg25519PriKeyAlgorithm; sk->base.base.getEncoded = GetAlg25519PriKeyEncoded; - sk->base.base.getEncodedPem = GetAlg25519PriKeyEncodedPem; + sk->base.getEncodedPem = GetAlg25519PriKeyEncodedPem; sk->base.base.getFormat = GetAlg25519PriKeyFormat; sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PriKey; sk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PriKey; 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 8be26487e542e18faa14299ffc7d44319e8a9d6e..dc2849491e2fd5d64118161a6c929268c2c07e00 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 @@ -238,9 +238,11 @@ static HcfResult GetDhPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return HCF_SUCCESS; } -static HcfResult GetDhPriKeyEncodedPem(HcfKey *self, const char *format, char **returnString) +static HcfResult GetDhPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format, + char **returnString) { (void)self; + (void)paramsSpec; (void)format; (void)returnString; return HCF_INVALID_PARAMS; @@ -524,7 +526,7 @@ static void FillOpensslDhPriKeyFunc(HcfOpensslDhPriKey *sk) sk->base.base.base.getClass = GetDhPriKeyClass; sk->base.base.getAlgorithm = GetDhPriKeyAlgorithm; sk->base.base.getEncoded = GetDhPriKeyEncoded; - sk->base.base.getEncodedPem = GetDhPriKeyEncodedPem; + sk->base.getEncodedPem = GetDhPriKeyEncodedPem; sk->base.base.getFormat = GetDhPriKeyFormat; sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDhPriKey; sk->base.getAsyKeySpecInt = GetIntSpecFromDhPriKey; 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 2dff8179acfa43b078fcdbebce29ba17484cf115..0a29419b21b8146ca567f3c0049e267f3a9d2836 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 @@ -215,9 +215,11 @@ static HcfResult GetDsaPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return HCF_SUCCESS; } -static HcfResult GetDsaPriKeyEncodedPem(HcfKey *self, const char *format, char **returnString) +static HcfResult GetDsaPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format, + char **returnString) { (void)self; + (void)paramsSpec; (void)format; (void)returnString; return HCF_INVALID_PARAMS; @@ -449,7 +451,7 @@ static void FillOpensslDsaPriKeyFunc(HcfOpensslDsaPriKey *sk) sk->base.base.base.getClass = GetDsaPriKeyClass; sk->base.base.getAlgorithm = GetDsaPriKeyAlgorithm; sk->base.base.getEncoded = GetDsaPriKeyEncoded; - sk->base.base.getEncodedPem = GetDsaPriKeyEncodedPem; + sk->base.getEncodedPem = GetDsaPriKeyEncodedPem; sk->base.base.getFormat = GetDsaPriKeyFormat; sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDsaPriKey; sk->base.getAsyKeySpecInt = GetIntSpecFromDsaPriKey; 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 f49d3ca7563929d2836bb8bd1463945d4a14876e..b2a29ab75cdb3c94c1fdb18a7cceb47b1ffd8463 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 @@ -1257,9 +1257,11 @@ static HcfResult GetEccPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return HCF_SUCCESS; } -static HcfResult GetEccPriKeyEncodedPem(HcfKey *self, const char *format, char **returnString) +static HcfResult GetEccPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format, + char **returnString) { (void)self; + (void)paramsSpec; (void)format; (void)returnString; return HCF_INVALID_PARAMS; @@ -1631,7 +1633,7 @@ static HcfResult PackEccPriKey(int32_t curveId, EC_KEY *ecKey, const char *field returnPriKey->base.base.base.getClass = GetEccPriKeyClass; returnPriKey->base.base.getAlgorithm = GetEccPriKeyAlgorithm; returnPriKey->base.base.getEncoded = GetEccPriKeyEncoded; - returnPriKey->base.base.getEncodedPem = GetEccPriKeyEncodedPem; + returnPriKey->base.getEncodedPem = GetEccPriKeyEncodedPem; returnPriKey->base.base.getFormat = GetEccPriKeyFormat; returnPriKey->base.clearMem = EccPriKeyClearMem; returnPriKey->base.getAsyKeySpecBigInteger = GetECPriKeySpecBigInteger; 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 fc31e1d470aae18c3bd8a603cf85e16945ff8426..37f6d0f6510fbc36326dce4f713d7f6a016a5c30 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 @@ -29,6 +29,7 @@ #include "memory.h" #include "rsa_openssl_common.h" #include "utils.h" +#include "asy_key_generator.h" #include "rsa_asy_key_generator_openssl.h" @@ -72,6 +73,28 @@ typedef struct { HcfAsyKeyGenSpiRsaParams *params; } HcfAsyKeyGeneratorSpiRsaOpensslImpl; +#define CIPHER_LIST_SIZE 4 + +static const char *g_supportedCiphers[] = { + "DES-EDE3-CBC", + "AES-128-CBC", + "AES-192-CBC", + "AES-256-CBC" +}; + +static bool IsCipherSupported(const char *cipher) +{ + if (cipher == NULL) { + return false; + } + for (size_t i = 0; i < CIPHER_LIST_SIZE; i++) { + if (strcmp(cipher, g_supportedCiphers[i]) == 0) { + return true; + } + } + return false; +} + static HcfResult CheckRsaKeyGenParams(HcfAsyKeyGenSpiRsaParams *params) { switch (params->bits) { @@ -582,7 +605,7 @@ static HcfResult GetPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return EncodePriKeyToPKCS8(impl->sk, returnBlob); } -static HcfResult GetPrikeyPkcs8Pem(EVP_PKEY *pkey, char **returnString) +static HcfResult GetPrikeyPkcs8Pem(EVP_PKEY *pkey, const EVP_CIPHER *cipher, const char *passWord, char **returnString) { BIO *bio = OpensslBioNew(OpensslBioSMem()); if (bio == NULL) { @@ -590,7 +613,13 @@ static HcfResult GetPrikeyPkcs8Pem(EVP_PKEY *pkey, char **returnString) HcfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; } - int ret = PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL); + + size_t passLen = 0; + if (passWord != NULL) { + passLen = strlen(passWord); + } + + int ret = PEM_write_bio_PKCS8PrivateKey(bio, pkey, cipher, passWord, passLen, NULL, NULL); if (ret != HCF_OPENSSL_SUCCESS) { LOGE("OpensslPemWriteBioPkcs8PrivateKey fail."); HcfPrintOpensslError(); @@ -606,7 +635,7 @@ static HcfResult GetPrikeyPkcs8Pem(EVP_PKEY *pkey, char **returnString) return HCF_SUCCESS; } -static HcfResult GetPrikeyPkcs1Pem(RSA *sk, char **returnString) +static HcfResult GetPrikeyPkcs1Pem(EVP_PKEY *pkey, const EVP_CIPHER *cipher, const char *passWord, char **returnString) { BIO *bio = OpensslBioNew(OpensslBioSMem()); if (bio == NULL) { @@ -614,7 +643,13 @@ static HcfResult GetPrikeyPkcs1Pem(RSA *sk, char **returnString) HcfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; } - int ret = PEM_write_bio_RSAPrivateKey(bio, sk, NULL, NULL, 0, NULL, NULL); + + size_t passLen = 0; + if (passWord != NULL) { + passLen = strlen(passWord); + } + + int ret = PEM_write_bio_PrivateKey_traditional(bio, pkey, cipher, (unsigned char *)passWord, passLen, NULL, NULL); if (ret != HCF_OPENSSL_SUCCESS) { LOGE("OpensslPemWriteBioRsaPrivateKey fail."); HcfPrintOpensslError(); @@ -630,43 +665,55 @@ static HcfResult GetPrikeyPkcs1Pem(RSA *sk, char **returnString) return HCF_SUCCESS; } -static HcfResult GetPriKeyPem(const char *format, EVP_PKEY *pkey, RSA *sk, char **returnString) +static HcfResult GetPriKeyPem(const char *format, EVP_PKEY *pkey, const EVP_CIPHER *cipher, + const char *passWord, char **returnString) { HcfResult result; if (strcmp(format, "PKCS8") == 0) { - result = GetPrikeyPkcs8Pem(pkey, returnString); + result = GetPrikeyPkcs8Pem(pkey, cipher, passWord, returnString); if (result != HCF_SUCCESS) { return result; } - } - if (strcmp(format, "PKCS1") == 0) { - result = GetPrikeyPkcs1Pem(sk, returnString); + } else if (strcmp(format, "PKCS1") == 0) { + result = GetPrikeyPkcs1Pem(pkey, cipher, passWord, returnString); if (result != HCF_SUCCESS) { return result; } + } else { + LOGE("format is invalid."); + return HCF_INVALID_PARAMS; } return HCF_SUCCESS; } -static HcfResult GetPriKeyEncodedPem(HcfKey *self, const char *format, char **returnString) +static HcfResult ValidateInputParams(const HcfPriKey *self, const char *format, char **returnString) { - if (self == NULL || format == NULL|| returnString == NULL) { + if (self == NULL || format == NULL || returnString == NULL) { LOGE("param is null."); return HCF_INVALID_PARAMS; } - if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) { LOGE("Class not match."); return HCF_INVALID_PARAMS; } - char *outPutStruct = NULL; - if (strcmp(format, "PKCS1") == 0) { - outPutStruct = "type-specific"; - } else if (strcmp(format, "PKCS8") == 0) { - outPutStruct = "PrivateKeyInfo"; - } else { - LOGE("format is invalid."); - return HCF_INVALID_PARAMS; + return HCF_SUCCESS; +} + +static void CleanupParams(HcfParamsSpec *params) +{ + if (params != NULL) { + HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)params; + HcfFree((void *)spec->cipher); + HcfFree((void *)spec->password); + } +} + +static HcfResult GetPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *params, const char *format, + char **returnString) +{ + HcfResult result = ValidateInputParams(self, format, returnString); + if (result != HCF_SUCCESS) { + return result; } HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self; @@ -676,14 +723,29 @@ static HcfResult GetPriKeyEncodedPem(HcfKey *self, const char *format, char **re return HCF_ERR_CRYPTO_OPERATION; } - HcfResult result = GetKeyEncodedPem(pkey, outPutStruct, EVP_PKEY_KEYPAIR, returnString); - if (result != HCF_SUCCESS) { - if (GetPriKeyPem(format, pkey, impl->sk, returnString) != HCF_SUCCESS) { - LOGE("GetPriKeyPem failed."); + const EVP_CIPHER *cipher = NULL; + const char *passWord = NULL; + if (params != NULL) { + HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)params; + const char *cipherStr = spec->cipher; + if (!IsCipherSupported(cipherStr)) { + LOGE("Cipher algorithm %s not supported", cipherStr); + CleanupParams(params); OpensslEvpPkeyFree(pkey); - return HCF_ERR_CRYPTO_OPERATION; + return HCF_NOT_SUPPORT; } + cipher = EVP_CIPHER_fetch(NULL, cipherStr, NULL); + passWord = spec->password; + } + + result = GetPriKeyPem(format, pkey, cipher, passWord, returnString); + if (result != HCF_SUCCESS) { + LOGE("GetPriKeyPem failed."); + CleanupParams(params); + OpensslEvpPkeyFree(pkey); + return result; } + CleanupParams(params); OpensslEvpPkeyFree(pkey); return HCF_SUCCESS; } @@ -802,7 +864,7 @@ static HcfResult PackPriKey(RSA *rsaPriKey, HcfOpensslRsaPriKey **retPriKey) (*retPriKey)->base.clearMem = ClearPriKeyMem; (*retPriKey)->base.base.getAlgorithm = GetPriKeyAlgorithm; (*retPriKey)->base.base.getEncoded = GetPriKeyEncoded; - (*retPriKey)->base.base.getEncodedPem = GetPriKeyEncodedPem; + (*retPriKey)->base.getEncodedPem = GetPriKeyEncodedPem; (*retPriKey)->base.base.getFormat = GetPriKeyFormat; (*retPriKey)->base.base.base.getClass = GetOpensslPrikeyClass; (*retPriKey)->base.base.base.destroy = DestroyPriKey; @@ -1001,7 +1063,7 @@ ERR: return ret; } -static HcfResult ConvertPemKeyToKey(const char *keyStr, int selection, RSA **rsa) +static HcfResult ConvertPemKeyToKey(const char *keyStr, HcfParamsSpec *params, int selection, RSA **rsa) { EVP_PKEY *pkey = NULL; const char *inputType = "PEM"; @@ -1013,6 +1075,16 @@ static HcfResult ConvertPemKeyToKey(const char *keyStr, int selection, RSA **rsa HcfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; } + if (params != NULL) { + HcfKeyDecodingParamsSpec *spec = (HcfKeyDecodingParamsSpec *)params; + const unsigned char *passWd = (const unsigned char *)spec->password; + if (OpensslOsslDecoderCtxSetPassPhrase(ctx, passWd, strlen(spec->password)) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + OpensslOsslDecoderCtxFree(ctx); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfFree((void *)spec->password); + } size_t pdataLen = strlen(keyStr); const unsigned char *pdata = (const unsigned char *)keyStr; int ret = OpensslOsslDecoderFromData(ctx, &pdata, &pdataLen); @@ -1037,7 +1109,7 @@ static HcfResult ConvertPemPubKey(const char *pubKeyStr, int selection, HcfOpens { RSA *rsaPk = NULL; HcfResult ret; - ret = ConvertPemKeyToKey(pubKeyStr, selection, &rsaPk); + ret = ConvertPemKeyToKey(pubKeyStr, NULL, selection, &rsaPk); if (ret != HCF_SUCCESS) { LOGE("ConvertPemKeyToKey failed."); return ret; @@ -1074,11 +1146,12 @@ ERR: return ret; } -static HcfResult ConvertPemPriKey(const char *priKeyStr, int selection, HcfOpensslRsaPriKey **priKeyRet) +static HcfResult ConvertPemPriKey(const char *priKeyStr, HcfParamsSpec *params, int selection, + HcfOpensslRsaPriKey **priKeyRet) { RSA *rsaSk = NULL; HcfResult ret; - ret = ConvertPemKeyToKey(priKeyStr, selection, &rsaSk); + ret = ConvertPemKeyToKey(priKeyStr, params, selection, &rsaSk); if (ret != HCF_SUCCESS) { LOGE("ConvertPemKeyToKey failed."); return ret; @@ -1148,7 +1221,6 @@ static HcfResult EngineConvertKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *pa static HcfResult EngineConvertPemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, const char *pubKeyStr, const char *priKeyStr, HcfKeyPair **returnKeyPair) { - (void)params; if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyStr == NULL) && (priKeyStr == NULL))) { LOGE("ConvertPemKeyParams is invalid."); return HCF_INVALID_PARAMS; @@ -1166,7 +1238,7 @@ static HcfResult EngineConvertPemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec } HcfOpensslRsaPriKey *priKey = NULL; if (priKeyStr != NULL && strlen(priKeyStr) != 0) { - if (ConvertPemPriKey(priKeyStr, EVP_PKEY_KEYPAIR, &priKey) != HCF_SUCCESS) { + if (ConvertPemPriKey(priKeyStr, params, EVP_PKEY_KEYPAIR, &priKey) != HCF_SUCCESS) { LOGE("convert prikey fail."); HcfObjDestroy((HcfObjectBase *)pubKey); return HCF_ERR_CRYPTO_OPERATION; 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 0129be4836939488d0be6408eed36ab3a37922d3..174221daaaa1ccf816d8e95c29d6ebb8bae083f2 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 @@ -520,9 +520,11 @@ static HcfResult GetSm2PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return HCF_SUCCESS; } -static HcfResult GetSm2PriKeyEncodedPem(HcfKey *self, const char *format, char **returnString) +static HcfResult GetSm2PriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format, + char **returnString) { (void)self; + (void)paramsSpec; (void)format; (void)returnString; return HCF_INVALID_PARAMS; @@ -823,7 +825,7 @@ static HcfResult PackSm2PriKey(int32_t curveId, EC_KEY *ecKey, const char *field returnPriKey->base.base.base.getClass = GetSm2PriKeyClass; returnPriKey->base.base.getAlgorithm = GetSm2PriKeyAlgorithm; returnPriKey->base.base.getEncoded = GetSm2PriKeyEncoded; - returnPriKey->base.base.getEncodedPem = GetSm2PriKeyEncodedPem; + returnPriKey->base.getEncodedPem = GetSm2PriKeyEncodedPem; returnPriKey->base.base.getFormat = GetSm2PriKeyFormat; returnPriKey->base.getAsyKeySpecBigInteger = GetSm2PriKeySpecBigInteger; returnPriKey->base.getAsyKeySpecString = GetSm2PriKeySpecString; diff --git a/test/unittest/src/crypto_rsa_asy_key_pem_test.cpp b/test/unittest/src/crypto_rsa_asy_key_pem_test.cpp index f734835827da6c9b0fa638c5876e45bca8c4b44f..8ad2be8bfaf198a781c4042d00ecf6d792d669f8 100644 --- a/test/unittest/src/crypto_rsa_asy_key_pem_test.cpp +++ b/test/unittest/src/crypto_rsa_asy_key_pem_test.cpp @@ -22,6 +22,8 @@ #include "memory.h" #include "params_parser.h" #include "rsa_asy_key_generator_openssl.h" +#include "pri_key.h" +#include "securec.h" using namespace std; using namespace testing::ext; @@ -749,13 +751,13 @@ static void RsaAsyKeyPemTest1(const char *algoName, const char *priKeyPkcs1Str, EXPECT_EQ(res, HCF_SUCCESS); HcfPriKey *prikey = dupKeyPair->priKey; char *retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); int32_t cmpRes = strcmp(retStr, priKeyPkcs1Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs8Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -777,13 +779,13 @@ static void RsaAsyKeyPemTest2(const char *algoName, const char *priKeyPkcs8Str, EXPECT_EQ(res, HCF_SUCCESS); HcfPriKey *prikey = dupKeyPair->priKey; char *retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); int32_t cmpRes = strcmp(retStr, priKeyPkcs1Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs8Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -879,13 +881,13 @@ static void RsaAsyKeyPemTest5(const char *algoName, const char *pubKeyPkcs1Str, HcfPriKey *prikey = dupKeyPair->priKey; retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs1Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs8Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -923,13 +925,13 @@ static void RsaAsyKeyPemTest6(const char *algoName, const char *pubKeyPkcs1Str, HcfPriKey *prikey = dupKeyPair->priKey; retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs1Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs8Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -967,13 +969,13 @@ static void RsaAsyKeyPemTest7(const char *algoName, const char *pubKeyPkcs1Str, HcfPriKey *prikey = dupKeyPair->priKey; retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs1Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs8Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -1011,13 +1013,13 @@ static void RsaAsyKeyPemTest8(const char *algoName, const char *pubKeyPkcs1Str, HcfPriKey *prikey = dupKeyPair->priKey; retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs1Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, priKeyPkcs8Str); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -1227,15 +1229,15 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest003, TestSize.Level0 EXPECT_NE(res, HCF_SUCCESS); HcfPriKey *prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem(nullptr, "PKCS1", &retStr); + res = prikey->getEncodedPem(nullptr, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, nullptr, &retStr); + res = prikey->getEncodedPem(prikey, nullptr, nullptr, &retStr); EXPECT_NE(res, HCF_SUCCESS); prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", nullptr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", nullptr); EXPECT_NE(res, HCF_SUCCESS); HcfObjDestroy(dupKeyPair); @@ -1273,19 +1275,19 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest004, TestSize.Level0 EXPECT_NE(res, HCF_SUCCESS); HcfPriKey *prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "X509", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "X509", &retStr); EXPECT_NE(res, HCF_SUCCESS); prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "test", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "test", &retStr); EXPECT_NE(res, HCF_SUCCESS); prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "pkcs1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "pkcs1", &retStr); EXPECT_NE(res, HCF_SUCCESS); prikey = dupKeyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "pkcs8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "pkcs8", &retStr); EXPECT_NE(res, HCF_SUCCESS); HcfObjDestroy(dupKeyPair); @@ -1339,10 +1341,10 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest006, TestSize.Level0 EXPECT_EQ(retStr, nullptr); HcfPriKey *prikey = keyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); @@ -1372,10 +1374,10 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest007, TestSize.Level0 EXPECT_EQ(retStr, nullptr); HcfPriKey *prikey = keyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); @@ -1405,10 +1407,10 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest008, TestSize.Level0 EXPECT_EQ(retStr, nullptr); HcfPriKey *prikey = keyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); @@ -1438,10 +1440,10 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest009, TestSize.Level0 EXPECT_EQ(retStr, nullptr); HcfPriKey *prikey = keyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); @@ -1471,10 +1473,10 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest010, TestSize.Level0 EXPECT_EQ(retStr, nullptr); HcfPriKey *prikey = keyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); @@ -1504,10 +1506,10 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemErrorTest011, TestSize.Level0 EXPECT_EQ(retStr, nullptr); HcfPriKey *prikey = keyPair->priKey; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_NE(res, HCF_SUCCESS); EXPECT_EQ(retStr, nullptr); @@ -1552,14 +1554,14 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemSpiTest001, TestSize.Level0) HcfPriKey *prikey = keyPair->priKey; retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, g_testPrikeyPkcs1Str2048.c_str()); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, g_testPrikeyPkcs8Str2048.c_str()); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -1606,14 +1608,14 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyPemSpiTest002, TestSize.Level0) HcfPriKey *prikey = keyPair->priKey; retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS1", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS1", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, g_testPrikeyPkcs1Str1024.c_str()); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfFree(retStr); retStr = nullptr; - res = prikey->base.getEncodedPem((HcfKey *)prikey, "PKCS8", &retStr); + res = prikey->getEncodedPem(prikey, nullptr, "PKCS8", &retStr); EXPECT_EQ(res, HCF_SUCCESS); cmpRes = strcmp(retStr, g_testPrikeyPkcs8Str1024.c_str()); EXPECT_EQ(cmpRes, HCF_SUCCESS); @@ -1693,4 +1695,166 @@ HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeySpecApiTest, TestSize.Level0) HcfObjDestroy(generator); HcfFree(priKeyBlob.data); } + +HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyEncodeTest, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("RSA1024", &generator); + EXPECT_EQ(res, HCF_SUCCESS); + EXPECT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)HcfMalloc(sizeof(HcfKeyEncodingParamsSpec), 0); + ASSERT_NE(spec, nullptr); + spec->password = (char *)HcfMalloc(strlen("123456") + 1, 0); + ASSERT_NE(spec->password, nullptr); + (void)memcpy_s((void *)spec->password, strlen("123456") + 1, (const char *)"123456", strlen("123456") + 1); + spec->cipher = (char *)HcfMalloc(strlen("AES-128-CBC") + 1, 0); + ASSERT_NE(spec->cipher, nullptr); + (void)memcpy_s((void *)spec->cipher, strlen("AES-128-CBC") + 1, + (const char *)"AES-128-CBC", strlen("AES-128-CBC") + 1); + + HcfParamsSpec *params = reinterpret_cast(spec); + + char *retStr = nullptr; + HcfPriKey *prikey = keyPair->priKey; + res = prikey->getEncodedPem(prikey, params, "PKCS8", &retStr); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfFree(spec); + + HcfKeyDecodingParamsSpec *decSpec = (HcfKeyDecodingParamsSpec *)HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0); + ASSERT_NE(decSpec, nullptr); + decSpec->password = (char *)HcfMalloc(strlen("123456") + 1, 0); + ASSERT_NE(decSpec->password, nullptr); + (void)memcpy_s((void *)decSpec->password, strlen("123456") + 1, (const char *)"123456", strlen("123456") + 1); + + HcfParamsSpec *decParams = reinterpret_cast(decSpec); + HcfKeyPair *dupKeyPair = nullptr; + res = generator->convertPemKey(generator, decParams, nullptr, retStr, &dupKeyPair); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfPriKey *prikey1 = dupKeyPair->priKey; + char *pkcs8Str = nullptr; + res = prikey1->getEncodedPem((const HcfPriKey *)prikey1, nullptr, "PKCS8", &pkcs8Str); + EXPECT_EQ(res, HCF_SUCCESS); + EXPECT_NE(pkcs8Str, nullptr); + + HcfFree(decSpec); + HcfFree(pkcs8Str); + + + HcfFree(retStr); + HcfObjDestroy(keyPair); + HcfObjDestroy(dupKeyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyEncodeTest_DifferentCiphers, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("RSA1024", &generator); + EXPECT_EQ(res, HCF_SUCCESS); + EXPECT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + EXPECT_EQ(res, HCF_SUCCESS); + + const char* ciphers[] = {"AES-256-CBC", "DES-EDE3-CBC", "AES-192-CBC"}; + const char* passwords[] = {"test123", "password123!", "strongPW@999"}; + + for (int i = 0; i < 3; i++) { + HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)HcfMalloc(sizeof(HcfKeyEncodingParamsSpec), 0); + ASSERT_NE(spec, nullptr); + + spec->password = (char *)HcfMalloc(strlen(passwords[i]) + 1, 0); + ASSERT_NE(spec->password, nullptr); + (void)memcpy_s((void *)spec->password, strlen(passwords[i]) + 1, + (const char *)passwords[i], strlen(passwords[i]) + 1); + + spec->cipher = (char *)HcfMalloc(strlen(ciphers[i]) + 1, 0); + ASSERT_NE(spec->cipher, nullptr); + (void)memcpy_s((void *)spec->cipher, strlen(ciphers[i]) + 1, (const char *)ciphers[i], strlen(ciphers[i]) + 1); + + HcfParamsSpec *params = reinterpret_cast(spec); + + char *retStr = nullptr; + HcfPriKey *prikey = keyPair->priKey; + res = prikey->getEncodedPem((const HcfPriKey *)prikey, params, "PKCS8", &retStr); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfKeyDecodingParamsSpec *decSpec = (HcfKeyDecodingParamsSpec *)HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0); + ASSERT_NE(decSpec, nullptr); + decSpec->password = (char *)HcfMalloc(strlen(passwords[i]) + 1, 0); + ASSERT_NE(decSpec->password, nullptr); + (void)memcpy_s((void *)decSpec->password, strlen(passwords[i]) + 1, + (const char *)passwords[i], strlen(passwords[i]) + 1); + + HcfParamsSpec *decParams = reinterpret_cast(decSpec); + HcfKeyPair *dupKeyPair = nullptr; + res = generator->convertPemKey(generator, decParams, nullptr, retStr, &dupKeyPair); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfFree(spec); + HcfFree(decSpec); + HcfFree(retStr); + HcfObjDestroy(dupKeyPair); + } + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoRsaAsyKeyPemTest, CryptoRsaAsyKeyEncodeTest_WrongPassword, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("RSA1024", &generator); + EXPECT_EQ(res, HCF_SUCCESS); + EXPECT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)HcfMalloc(sizeof(HcfKeyEncodingParamsSpec), 0); + ASSERT_NE(spec, nullptr); + spec->password = (char *)HcfMalloc(strlen("correctPW") + 1, 0); + ASSERT_NE(spec->password, nullptr); + (void)memcpy_s((void *)spec->password, strlen("correctPW") + 1, + (const char *)"correctPW", strlen("correctPW") + 1); + + spec->cipher = (char *)HcfMalloc(strlen("AES-128-CBC") + 1, 0); + ASSERT_NE(spec->cipher, nullptr); + (void)memcpy_s((void *)spec->cipher, strlen("AES-128-CBC") + 1, + (const char *)"AES-128-CBC", strlen("AES-128-CBC") + 1); + + HcfParamsSpec *params = reinterpret_cast(spec); + + char *retStr = nullptr; + HcfPriKey *prikey = keyPair->priKey; + res = prikey->getEncodedPem((const HcfPriKey *)prikey, params, "PKCS8", &retStr); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfKeyDecodingParamsSpec *decSpec = (HcfKeyDecodingParamsSpec *)HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0); + ASSERT_NE(decSpec, nullptr); + decSpec->password = (char *)HcfMalloc(strlen("wrongPW") + 1, 0); + ASSERT_NE(decSpec->password, nullptr); + (void)memcpy_s((void *)decSpec->password, strlen("wrongPW") + 1, (const char *)"wrongPW", strlen("wrongPW") + 1); + + HcfParamsSpec *decParams = reinterpret_cast(decSpec); + HcfKeyPair *dupKeyPair = nullptr; + res = generator->convertPemKey(generator, decParams, nullptr, retStr, &dupKeyPair); + EXPECT_NE(res, HCF_SUCCESS); + EXPECT_EQ(dupKeyPair, nullptr); + + HcfFree(spec); + HcfFree(decSpec); + HcfFree(retStr); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} }