From 15edd875a158af8845f879213dfd04d14190a681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=9D=99?= Date: Sat, 2 Nov 2024 16:01:12 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=96=B0=E5=A2=9Ek1=E6=9B=B2=E7=BA=BF?= =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王静 --- .../src/ecc_asy_key_generator_openssl.c | 228 ++++++------------ .../ecc_common_param_spec_generator_openssl.c | 19 +- 2 files changed, 81 insertions(+), 166 deletions(-) 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 6dd7663..f49d3ca 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 @@ -58,14 +58,10 @@ typedef struct { static HcfResult CheckEc224CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_ecc224CorrectBigP, NID_secp224r1_len, NULL); - bStd = OpensslBin2Bn(g_ecc224CorrectBigB, NID_secp224r1_len, NULL); - xStd = OpensslBin2Bn(g_ecc224CorrectBigGX, NID_secp224r1_len, NULL); - yStd = OpensslBin2Bn(g_ecc224CorrectBigGY, NID_secp224r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_ecc224CorrectBigP, NID_secp224r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_ecc224CorrectBigB, NID_secp224r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_ecc224CorrectBigGX, NID_secp224r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_ecc224CorrectBigGY, NID_secp224r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] EC 224 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -83,14 +79,10 @@ static HcfResult CheckEc224CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckEc256CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_ecc256CorrectBigP, NID_X9_62_prime256v1_len, NULL); - bStd = OpensslBin2Bn(g_ecc256CorrectBigB, NID_X9_62_prime256v1_len, NULL); - xStd = OpensslBin2Bn(g_ecc256CorrectBigGX, NID_X9_62_prime256v1_len, NULL); - yStd = OpensslBin2Bn(g_ecc256CorrectBigGY, NID_X9_62_prime256v1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_ecc256CorrectBigP, NID_X9_62_prime256v1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_ecc256CorrectBigB, NID_X9_62_prime256v1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_ecc256CorrectBigGX, NID_X9_62_prime256v1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_ecc256CorrectBigGY, NID_X9_62_prime256v1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] EC 256 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -108,14 +100,10 @@ static HcfResult CheckEc256CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckEc384CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_ecc384CorrectBigP, NID_secp384r1_len, NULL); - bStd = OpensslBin2Bn(g_ecc384CorrectBigB, NID_secp384r1_len, NULL); - xStd = OpensslBin2Bn(g_ecc384CorrectBigGX, NID_secp384r1_len, NULL); - yStd = OpensslBin2Bn(g_ecc384CorrectBigGY, NID_secp384r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_ecc384CorrectBigP, NID_secp384r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_ecc384CorrectBigB, NID_secp384r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_ecc384CorrectBigGX, NID_secp384r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_ecc384CorrectBigGY, NID_secp384r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] EC 384 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -133,14 +121,10 @@ static HcfResult CheckEc384CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckEc521CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_ecc521CorrectBigP, NID_secp521r1_len, NULL); - bStd = OpensslBin2Bn(g_ecc521CorrectBigB, NID_secp521r1_len, NULL); - xStd = OpensslBin2Bn(g_ecc521CorrectBigGX, NID_secp521r1_len, NULL); - yStd = OpensslBin2Bn(g_ecc521CorrectBigGY, NID_secp521r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_ecc521CorrectBigP, NID_secp521r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_ecc521CorrectBigB, NID_secp521r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_ecc521CorrectBigGX, NID_secp521r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_ecc521CorrectBigGY, NID_secp521r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] EC 521 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -158,14 +142,10 @@ static HcfResult CheckEc521CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP160r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp160r1CorrectBigP, NID_brainpoolP160r1_len, NULL); - bStd = OpensslBin2Bn(g_bp160r1CorrectBigB, NID_brainpoolP160r1_len, NULL); - xStd = OpensslBin2Bn(g_bp160r1CorrectBigGX, NID_brainpoolP160r1_len, NULL); - yStd = OpensslBin2Bn(g_bp160r1CorrectBigGY, NID_brainpoolP160r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp160r1CorrectBigP, NID_brainpoolP160r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp160r1CorrectBigB, NID_brainpoolP160r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp160r1CorrectBigGX, NID_brainpoolP160r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp160r1CorrectBigGY, NID_brainpoolP160r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 160r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -183,14 +163,10 @@ static HcfResult CheckBP160r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP160t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp160t1CorrectBigP, NID_brainpoolP160t1_len, NULL); - bStd = OpensslBin2Bn(g_bp160t1CorrectBigB, NID_brainpoolP160t1_len, NULL); - xStd = OpensslBin2Bn(g_bp160t1CorrectBigGX, NID_brainpoolP160t1_len, NULL); - yStd = OpensslBin2Bn(g_bp160t1CorrectBigGY, NID_brainpoolP160t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp160t1CorrectBigP, NID_brainpoolP160t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp160t1CorrectBigB, NID_brainpoolP160t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp160t1CorrectBigGX, NID_brainpoolP160t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp160t1CorrectBigGY, NID_brainpoolP160t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 160t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -208,14 +184,10 @@ static HcfResult CheckBP160t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP192r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp192r1CorrectBigP, NID_brainpoolP192r1_len, NULL); - bStd = OpensslBin2Bn(g_bp192r1CorrectBigB, NID_brainpoolP192r1_len, NULL); - xStd = OpensslBin2Bn(g_bp192r1CorrectBigGX, NID_brainpoolP192r1_len, NULL); - yStd = OpensslBin2Bn(g_bp192r1CorrectBigGY, NID_brainpoolP192r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp192r1CorrectBigP, NID_brainpoolP192r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp192r1CorrectBigB, NID_brainpoolP192r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp192r1CorrectBigGX, NID_brainpoolP192r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp192r1CorrectBigGY, NID_brainpoolP192r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 192r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -233,14 +205,10 @@ static HcfResult CheckBP192r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP192t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp192t1CorrectBigP, NID_brainpoolP192t1_len, NULL); - bStd = OpensslBin2Bn(g_bp192t1CorrectBigB, NID_brainpoolP192t1_len, NULL); - xStd = OpensslBin2Bn(g_bp192t1CorrectBigGX, NID_brainpoolP192t1_len, NULL); - yStd = OpensslBin2Bn(g_bp192t1CorrectBigGY, NID_brainpoolP192t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp192t1CorrectBigP, NID_brainpoolP192t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp192t1CorrectBigB, NID_brainpoolP192t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp192t1CorrectBigGX, NID_brainpoolP192t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp192t1CorrectBigGY, NID_brainpoolP192t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 192t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -258,14 +226,10 @@ static HcfResult CheckBP192t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP224r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp224r1CorrectBigP, NID_brainpoolP224r1_len, NULL); - bStd = OpensslBin2Bn(g_bp224r1CorrectBigB, NID_brainpoolP224r1_len, NULL); - xStd = OpensslBin2Bn(g_bp224r1CorrectBigGX, NID_brainpoolP224r1_len, NULL); - yStd = OpensslBin2Bn(g_bp224r1CorrectBigGY, NID_brainpoolP224r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp224r1CorrectBigP, NID_brainpoolP224r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp224r1CorrectBigB, NID_brainpoolP224r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp224r1CorrectBigGX, NID_brainpoolP224r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp224r1CorrectBigGY, NID_brainpoolP224r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 224r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -283,14 +247,10 @@ static HcfResult CheckBP224r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP224t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp224t1CorrectBigP, NID_brainpoolP224t1_len, NULL); - bStd = OpensslBin2Bn(g_bp224t1CorrectBigB, NID_brainpoolP224t1_len, NULL); - xStd = OpensslBin2Bn(g_bp224t1CorrectBigGX, NID_brainpoolP224t1_len, NULL); - yStd = OpensslBin2Bn(g_bp224t1CorrectBigGY, NID_brainpoolP224t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp224t1CorrectBigP, NID_brainpoolP224t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp224t1CorrectBigB, NID_brainpoolP224t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp224t1CorrectBigGX, NID_brainpoolP224t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp224t1CorrectBigGY, NID_brainpoolP224t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 224t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -308,14 +268,10 @@ static HcfResult CheckBP224t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP256r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp256r1CorrectBigP, NID_brainpoolP256r1_len, NULL); - bStd = OpensslBin2Bn(g_bp256r1CorrectBigB, NID_brainpoolP256r1_len, NULL); - xStd = OpensslBin2Bn(g_bp256r1CorrectBigGX, NID_brainpoolP256r1_len, NULL); - yStd = OpensslBin2Bn(g_bp256r1CorrectBigGY, NID_brainpoolP256r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp256r1CorrectBigP, NID_brainpoolP256r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp256r1CorrectBigB, NID_brainpoolP256r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp256r1CorrectBigGX, NID_brainpoolP256r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp256r1CorrectBigGY, NID_brainpoolP256r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 256r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -333,14 +289,10 @@ static HcfResult CheckBP256r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP256t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp256t1CorrectBigP, NID_brainpoolP256t1_len, NULL); - bStd = OpensslBin2Bn(g_bp256t1CorrectBigB, NID_brainpoolP256t1_len, NULL); - xStd = OpensslBin2Bn(g_bp256t1CorrectBigGX, NID_brainpoolP256t1_len, NULL); - yStd = OpensslBin2Bn(g_bp256t1CorrectBigGY, NID_brainpoolP256t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp256t1CorrectBigP, NID_brainpoolP256t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp256t1CorrectBigB, NID_brainpoolP256t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp256t1CorrectBigGX, NID_brainpoolP256t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp256t1CorrectBigGY, NID_brainpoolP256t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 256t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -357,14 +309,10 @@ static HcfResult CheckBP256t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckSecp256k1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_secp256k1CorrectBigP, NID_secp256k1_len, NULL); - bStd = OpensslBin2Bn(g_secp256k1CorrectBigB, NID_secp256k1_len, NULL); - xStd = OpensslBin2Bn(g_secp256k1CorrectBigGX, NID_secp256k1_len, NULL); - yStd = OpensslBin2Bn(g_secp256k1CorrectBigGY, NID_secp256k1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_secp256k1CorrectBigP, NID_secp256k1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_secp256k1CorrectBigB, NID_secp256k1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_secp256k1CorrectBigGX, NID_secp256k1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_secp256k1CorrectBigGY, NID_secp256k1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] Secp256k1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -381,14 +329,10 @@ static HcfResult CheckSecp256k1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM * static HcfResult CheckBP320r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp320r1CorrectBigP, NID_brainpoolP320r1_len, NULL); - bStd = OpensslBin2Bn(g_bp320r1CorrectBigB, NID_brainpoolP320r1_len, NULL); - xStd = OpensslBin2Bn(g_bp320r1CorrectBigGX, NID_brainpoolP320r1_len, NULL); - yStd = OpensslBin2Bn(g_bp320r1CorrectBigGY, NID_brainpoolP320r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp320r1CorrectBigP, NID_brainpoolP320r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp320r1CorrectBigB, NID_brainpoolP320r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp320r1CorrectBigGX, NID_brainpoolP320r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp320r1CorrectBigGY, NID_brainpoolP320r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 320r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -405,14 +349,10 @@ static HcfResult CheckBP320r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP320t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp320t1CorrectBigP, NID_brainpoolP320t1_len, NULL); - bStd = OpensslBin2Bn(g_bp320t1CorrectBigB, NID_brainpoolP320t1_len, NULL); - xStd = OpensslBin2Bn(g_bp320t1CorrectBigGX, NID_brainpoolP320t1_len, NULL); - yStd = OpensslBin2Bn(g_bp320t1CorrectBigGY, NID_brainpoolP320t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp320t1CorrectBigP, NID_brainpoolP320t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp320t1CorrectBigB, NID_brainpoolP320t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp320t1CorrectBigGX, NID_brainpoolP320t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp320t1CorrectBigGY, NID_brainpoolP320t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 320t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -429,14 +369,10 @@ static HcfResult CheckBP320t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP384r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp384r1CorrectBigP, NID_brainpoolP384r1_len, NULL); - bStd = OpensslBin2Bn(g_bp384r1CorrectBigB, NID_brainpoolP384r1_len, NULL); - xStd = OpensslBin2Bn(g_bp384r1CorrectBigGX, NID_brainpoolP384r1_len, NULL); - yStd = OpensslBin2Bn(g_bp384r1CorrectBigGY, NID_brainpoolP384r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp384r1CorrectBigP, NID_brainpoolP384r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp384r1CorrectBigB, NID_brainpoolP384r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp384r1CorrectBigGX, NID_brainpoolP384r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp384r1CorrectBigGY, NID_brainpoolP384r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 384r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -453,14 +389,10 @@ static HcfResult CheckBP384r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP384t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp384t1CorrectBigP, NID_brainpoolP384t1_len, NULL); - bStd = OpensslBin2Bn(g_bp384t1CorrectBigB, NID_brainpoolP384t1_len, NULL); - xStd = OpensslBin2Bn(g_bp384t1CorrectBigGX, NID_brainpoolP384t1_len, NULL); - yStd = OpensslBin2Bn(g_bp384t1CorrectBigGY, NID_brainpoolP384t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp384t1CorrectBigP, NID_brainpoolP384t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp384t1CorrectBigB, NID_brainpoolP384t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp384t1CorrectBigGX, NID_brainpoolP384t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp384t1CorrectBigGY, NID_brainpoolP384t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 384t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -477,14 +409,10 @@ static HcfResult CheckBP384t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP512r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp512r1CorrectBigP, NID_brainpoolP512r1_len, NULL); - bStd = OpensslBin2Bn(g_bp512r1CorrectBigB, NID_brainpoolP512r1_len, NULL); - xStd = OpensslBin2Bn(g_bp512r1CorrectBigGX, NID_brainpoolP512r1_len, NULL); - yStd = OpensslBin2Bn(g_bp512r1CorrectBigGY, NID_brainpoolP512r1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp512r1CorrectBigP, NID_brainpoolP512r1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp512r1CorrectBigB, NID_brainpoolP512r1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp512r1CorrectBigGX, NID_brainpoolP512r1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp512r1CorrectBigGY, NID_brainpoolP512r1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 512r1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); @@ -501,14 +429,10 @@ static HcfResult CheckBP512r1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) static HcfResult CheckBP512t1CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { - BIGNUM *pStd = NULL; - BIGNUM *bStd = NULL; - BIGNUM *xStd = NULL; - BIGNUM *yStd = NULL; - pStd = OpensslBin2Bn(g_bp512t1CorrectBigP, NID_brainpoolP512t1_len, NULL); - bStd = OpensslBin2Bn(g_bp512t1CorrectBigB, NID_brainpoolP512t1_len, NULL); - xStd = OpensslBin2Bn(g_bp512t1CorrectBigGX, NID_brainpoolP512t1_len, NULL); - yStd = OpensslBin2Bn(g_bp512t1CorrectBigGY, NID_brainpoolP512t1_len, NULL); + BIGNUM *pStd = OpensslBin2Bn(g_bp512t1CorrectBigP, NID_brainpoolP512t1_len, NULL); + BIGNUM *bStd = OpensslBin2Bn(g_bp512t1CorrectBigB, NID_brainpoolP512t1_len, NULL); + BIGNUM *xStd = OpensslBin2Bn(g_bp512t1CorrectBigGX, NID_brainpoolP512t1_len, NULL); + BIGNUM *yStd = OpensslBin2Bn(g_bp512t1CorrectBigGY, NID_brainpoolP512t1_len, NULL); if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) { LOGD("[error] BP 512t1 Curve convert to BN fail"); FreeCurveBigNum(pStd, bStd, xStd, yStd); diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c index e7c6e35..59d64f1 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c @@ -95,22 +95,14 @@ static HcfResult BuildCommonParamPart(const EC_GROUP *ecGroup, HcfEccCommParamsS static HcfResult BuildCommonParamGFp(const EC_GROUP *ecGroup, HcfEccCommParamsSpecSpi *returnCommonParamSpec) { BIGNUM *p = OpensslBnNew(); - if (p == NULL) { - LOGE("New p failed."); - return HCF_ERR_MALLOC; - } BIGNUM *a = OpensslBnNew(); - if (a == NULL) { - LOGE("New a failed."); - OpensslBnFree(p); - return HCF_ERR_MALLOC; - } BIGNUM *b = OpensslBnNew(); - if (b == NULL) { - LOGE("New b failed."); + if (p == NULL || a == NULL || b == NULL) { + LOGD("[error] new BN failed."); OpensslBnFree(p); OpensslBnFree(a); - return HCF_ERR_MALLOC; + OpensslBnFree(b); + return HCF_ERR_CRYPTO_OPERATION; } if (!OpensslEcGroupGetCurveGfp(ecGroup, p, a, b, NULL)) { LOGE("EC_GROUP_get_curve_GFp failed."); @@ -120,9 +112,8 @@ static HcfResult BuildCommonParamGFp(const EC_GROUP *ecGroup, HcfEccCommParamsSp return HCF_ERR_CRYPTO_OPERATION; } HcfResult ret = HCF_SUCCESS; - int curveId = OpensslEcGroupGetCurveName(ecGroup); do { - if (curveId == NID_secp256k1) { + if (OpensslEcGroupGetCurveName(ecGroup) == NID_secp256k1) { if (BigNumToBigIntegerSecp256k1(a, &(returnCommonParamSpec->paramsSpec.a)) != HCF_SUCCESS) { LOGE("Build Secp256k1CommonParamSpec a failed."); ret = HCF_ERR_CRYPTO_OPERATION; -- Gitee From 24157d5514ef3ff8d783e88e237378f6419df900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E8=90=8C=E6=96=B0?= Date: Tue, 5 Nov 2024 10:24:29 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 萌萌萌新 --- frameworks/cj/include/cipher_impl.h | 2 +- frameworks/cj/include/crypto_ffi.h | 7 +++-- frameworks/cj/include/sign_impl.h | 2 +- frameworks/cj/include/verify_impl.h | 2 +- frameworks/cj/src/cipher_impl.cpp | 4 +-- frameworks/cj/src/crypto_ffi.cpp | 49 +++++++++++++++++++++-------- frameworks/cj/src/sign_impl.cpp | 4 +-- frameworks/cj/src/verify_impl.cpp | 4 +-- 8 files changed, 49 insertions(+), 25 deletions(-) diff --git a/frameworks/cj/include/cipher_impl.h b/frameworks/cj/include/cipher_impl.h index 28b0033..9f5eaa4 100644 --- a/frameworks/cj/include/cipher_impl.h +++ b/frameworks/cj/include/cipher_impl.h @@ -32,7 +32,7 @@ public: HcfResult CipherUpdate(HcfBlob *input, HcfBlob *output); HcfResult CipherDoFinal(HcfBlob *input, HcfBlob *output); HcfResult SetCipherSpec(CipherSpecItem item, HcfBlob pSource); - HcfResult GetCipherSpecString(CipherSpecItem item, char *returnString); + HcfResult GetCipherSpecString(CipherSpecItem item, char **returnString); HcfResult GetCipherSpecUint8Array(CipherSpecItem item, HcfBlob *returnUint8Array); const char *GetAlgorithm(int32_t* errCode); diff --git a/frameworks/cj/include/crypto_ffi.h b/frameworks/cj/include/crypto_ffi.h index 2d5d1d4..2aefc56 100644 --- a/frameworks/cj/include/crypto_ffi.h +++ b/frameworks/cj/include/crypto_ffi.h @@ -84,7 +84,7 @@ extern "C" { FFI_EXPORT int32_t FfiOHOSCipherUpdate(int64_t id, HcfBlob *input, HcfBlob *output); FFI_EXPORT int32_t FfiOHOSCipherDoFinal(int64_t id, HcfBlob *input, HcfBlob *output); FFI_EXPORT int32_t FfiOHOSSetCipherSpec(int64_t id, int32_t item, HcfBlob pSource); - FFI_EXPORT int32_t FfiOHOSGetCipherSpecString(int64_t id, int32_t item, char *returnString); + FFI_EXPORT char *FfiOHOSGetCipherSpecString(int64_t id, int32_t item, int32_t *errCode); FFI_EXPORT int32_t FfiOHOSGetCipherSpecUint8Array(int64_t id, int32_t item, HcfBlob *returnUint8Array); FFI_EXPORT const char *FfiOHOSCipherGetAlgName(int64_t id, int32_t* errCode); @@ -102,7 +102,7 @@ extern "C" { FFI_EXPORT int32_t FFiOHOSSignSign(int64_t id, HcfBlob *input, HcfBlob *output); FFI_EXPORT int32_t FFiOHOSSignSetSignSpecByNum(int64_t id, int32_t itemValue); FFI_EXPORT int32_t FFiOHOSSignSetSignSpecByArr(int64_t id, HcfBlob itemValue); - FFI_EXPORT int32_t FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, char *itemValue); + FFI_EXPORT char *FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, int32_t *errCode); FFI_EXPORT int32_t FFiOHOSSignGetSignSpecNum(int64_t id, SignSpecItem item, int32_t *itemValue); // verify @@ -113,7 +113,7 @@ extern "C" { FFI_EXPORT int32_t FFiOHOSVerifyRecover(int64_t id, HcfBlob input, HcfBlob *output); FFI_EXPORT int32_t FFiOHOSVerifySetVerifySpecByNum(int64_t id, int32_t itemValue); FFI_EXPORT int32_t FFiOHOSVerifySetVerifySpecByArr(int64_t id, HcfBlob itemValue); - FFI_EXPORT int32_t FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, char *itemValue); + FFI_EXPORT char *FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, int32_t *errCode); FFI_EXPORT int32_t FFiOHOSVerifyGetVerifySpecNum(int64_t id, SignSpecItem item, int32_t *itemValue); // asykeygenerator @@ -163,6 +163,7 @@ extern "C" { FFI_EXPORT HcfBigInteger FFiOHOSPubKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode); FFI_EXPORT const char *FfiOHOSPubKeyGetFormat(int64_t id, int32_t* errCode); FFI_EXPORT void *FfiOHOSPubKeyGetRawPointer(int64_t id); + FFI_EXPORT int64_t FfiOHOSPubKeyFromRawPointer(void *ptr, const char **retString, int32_t *errCode); // keypair FFI_EXPORT int64_t FFiOHOSKeyPairPubKey(int64_t id, int32_t *errCode); diff --git a/frameworks/cj/include/sign_impl.h b/frameworks/cj/include/sign_impl.h index 33d9219..48a8e82 100644 --- a/frameworks/cj/include/sign_impl.h +++ b/frameworks/cj/include/sign_impl.h @@ -34,7 +34,7 @@ public: HcfResult Sign(HcfBlob *input, HcfBlob *output); HcfResult SetSignSpecByNum(int32_t itemValue); HcfResult SetSignSpecByArr(HcfBlob itemValue); - HcfResult GetSignSpecString(SignSpecItem item, char *itemValue); + HcfResult GetSignSpecString(SignSpecItem item, char **itemValue); HcfResult GetSignSpecNum(SignSpecItem item, int32_t *itemValue); private: diff --git a/frameworks/cj/include/verify_impl.h b/frameworks/cj/include/verify_impl.h index 14b5b03..fea9818 100644 --- a/frameworks/cj/include/verify_impl.h +++ b/frameworks/cj/include/verify_impl.h @@ -33,7 +33,7 @@ public: HcfResult Recover(HcfBlob input, HcfBlob *output); HcfResult SetVerifySpecByNum(int32_t itemValue); HcfResult SetVerifySpecByArr(HcfBlob itemValue); - HcfResult GetVerifySpecString(SignSpecItem item, char *itemValue); + HcfResult GetVerifySpecString(SignSpecItem item, char **itemValue); HcfResult GetVerifySpecNum(SignSpecItem item, int32_t *itemValue); private: HcfVerify *verify_ = nullptr; diff --git a/frameworks/cj/src/cipher_impl.cpp b/frameworks/cj/src/cipher_impl.cpp index 1ff8660..44fc470 100644 --- a/frameworks/cj/src/cipher_impl.cpp +++ b/frameworks/cj/src/cipher_impl.cpp @@ -66,13 +66,13 @@ namespace OHOS { return cipher_->setCipherSpecUint8Array(cipher_, item, pSource); } - HcfResult CipherImpl::GetCipherSpecString(CipherSpecItem item, char *returnString) + HcfResult CipherImpl::GetCipherSpecString(CipherSpecItem item, char **returnString) { if (cipher_ == nullptr) { LOGE("fail to get cipher obj!"); return HCF_INVALID_PARAMS; } - return cipher_->getCipherSpecString(cipher_, item, &returnString); + return cipher_->getCipherSpecString(cipher_, item, returnString); } HcfResult CipherImpl::GetCipherSpecUint8Array(CipherSpecItem item, HcfBlob *returnUint8Array) diff --git a/frameworks/cj/src/crypto_ffi.cpp b/frameworks/cj/src/crypto_ffi.cpp index 535691e..9ba7662 100644 --- a/frameworks/cj/src/crypto_ffi.cpp +++ b/frameworks/cj/src/crypto_ffi.cpp @@ -552,18 +552,20 @@ namespace OHOS { return res; } - int32_t FfiOHOSGetCipherSpecString(int64_t id, int32_t item, char *returnString) + char *FfiOHOSGetCipherSpecString(int64_t id, int32_t item, int32_t *errCode) { - LOGD("[Cipher] GetCipherSpecString start"); + LOGD("[Cipher] FfiOHOSGetCipherSpecString start"); auto instance = FFIData::GetData(id); if (!instance) { LOGE("[Cipher] instance not exist."); - return HCF_ERR_MALLOC; + *errCode = HCF_ERR_MALLOC; + return nullptr; } - CipherSpecItem csi = CipherSpecItem(item); - HcfResult res = instance->GetCipherSpecString(csi, returnString); - LOGD("[Cipher] GetCipherSpecString success"); - return res; + CipherSpecItem specItem = CipherSpecItem(item); + char *returnString = nullptr; + *errCode = instance->GetCipherSpecString(specItem, &returnString); + LOGD("[Cipher] FfiOHOSGetCipherSpecString success"); + return returnString; } int32_t FfiOHOSGetCipherSpecUint8Array(int64_t id, int32_t item, HcfBlob *returnUint8Array) @@ -781,16 +783,19 @@ namespace OHOS { return sign->SetSignSpecByArr(itemValue); } - int32_t FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, char *itemValue) + char *FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, int32_t *errCode) { LOGD("[Sign] FFiOHOSSignGetSignSpecString start"); auto sign = FFIData::GetData(id); if (sign == nullptr) { LOGE("[Sign] FFiOHOSSignGetSignSpecString failed to get sign obj."); - return HCF_INVALID_PARAMS; + *errCode = HCF_INVALID_PARAMS; + return nullptr; } + char *returnString = nullptr; + *errCode = sign->GetSignSpecString(item, &returnString); LOGD("[Sign] FFiOHOSSignGetSignSpecString success"); - return sign->GetSignSpecString(item, itemValue); + return returnString; } int32_t FFiOHOSSignGetSignSpecNum(int64_t id, SignSpecItem item, int32_t *itemValue) @@ -910,16 +915,19 @@ namespace OHOS { return verify->SetVerifySpecByArr(itemValue); } - int32_t FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, char *itemValue) + char *FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, int32_t *errCode) { LOGD("[Verify] FFiOHOSVerifyGetVerifySpecString start"); auto verify = FFIData::GetData(id); if (verify == nullptr) { LOGE("[Verify] FFiOHOSVerifyGetVerifySpecString failed to get verify obj."); - return HCF_INVALID_PARAMS; + *errCode = HCF_INVALID_PARAMS; + return nullptr; } + char *returnString = nullptr; + *errCode = verify->GetVerifySpecString(item, &returnString); LOGD("[Verify] FFiOHOSVerifyGetVerifySpecString success"); - return verify->GetVerifySpecString(item, itemValue); + return returnString; } int32_t FFiOHOSVerifyGetVerifySpecNum(int64_t id, SignSpecItem item, int32_t *itemValue) @@ -1634,6 +1642,21 @@ namespace OHOS { return key; } + int64_t FfiOHOSPubKeyFromRawPointer(void *ptr, const char **retString, int32_t *errCode) + { + LOGD("[PubKey] FfiOHOSPubKeyFromRawPointer start"); + HcfPubKey *pubKey = static_cast(ptr); + auto pub = FFIData::Create(pubKey); + if (pub == nullptr) { + *errCode = HCF_ERR_MALLOC; + LOGE("new pub key failed"); + return 0; + } + *retString = pubKey->base.getAlgorithm(&pubKey->base); + LOGD("[PubKey] FfiOHOSPubKeyFromRawPointer success"); + return pub->GetID(); + } + // ------------------------------------keypair int64_t FFiOHOSKeyPairPubKey(int64_t id, int32_t *errCode) { diff --git a/frameworks/cj/src/sign_impl.cpp b/frameworks/cj/src/sign_impl.cpp index b278502..719e168 100644 --- a/frameworks/cj/src/sign_impl.cpp +++ b/frameworks/cj/src/sign_impl.cpp @@ -67,12 +67,12 @@ HcfResult SignImpl::SetSignSpecByArr(HcfBlob itemValue) return this->signObj_->setSignSpecUint8Array(signObj_, SM2_USER_ID_UINT8ARR, itemValue); } -HcfResult SignImpl::GetSignSpecString(SignSpecItem item, char *itemValue) +HcfResult SignImpl::GetSignSpecString(SignSpecItem item, char **itemValue) { if (this->signObj_ == nullptr) { return HCF_INVALID_PARAMS; } - return this->signObj_->getSignSpecString(signObj_, item, &itemValue); + return this->signObj_->getSignSpecString(signObj_, item, itemValue); } HcfResult SignImpl::GetSignSpecNum(SignSpecItem item, int32_t *itemValue) diff --git a/frameworks/cj/src/verify_impl.cpp b/frameworks/cj/src/verify_impl.cpp index 1ddfba8..1c9c0db 100644 --- a/frameworks/cj/src/verify_impl.cpp +++ b/frameworks/cj/src/verify_impl.cpp @@ -77,12 +77,12 @@ HcfResult VerifyImpl::SetVerifySpecByArr(HcfBlob itemValue) return this->verify_->setVerifySpecUint8Array(verify_, SM2_USER_ID_UINT8ARR, itemValue); } -HcfResult VerifyImpl::GetVerifySpecString(SignSpecItem item, char *itemValue) +HcfResult VerifyImpl::GetVerifySpecString(SignSpecItem item, char **itemValue) { if (this->verify_ == nullptr) { return HCF_INVALID_PARAMS; } - return this->verify_->getVerifySpecString(verify_, item, &itemValue); + return this->verify_->getVerifySpecString(verify_, item, itemValue); } HcfResult VerifyImpl::GetVerifySpecNum(SignSpecItem item, int32_t *itemValue) -- Gitee From f7f2520a23256c73da4b681fef59bf8e7a9b9e1b Mon Sep 17 00:00:00 2001 From: lcc Date: Mon, 11 Nov 2024 14:28:34 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lcc --- interfaces/kits/native/include/crypto_signature.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index 40281ff..eb77a1b 100644 --- a/interfaces/kits/native/include/crypto_signature.h +++ b/interfaces/kits/native/include/crypto_signature.h @@ -75,7 +75,7 @@ typedef struct OH_CryptoVerify OH_CryptoVerify; * @brief Create a verify context according to the given algorithm name. * * @param algoName Indicates the algorithm name for generating the verify context. Example RSA1024|PKCS1|SHA256. - * @param ctx Indicates the pointer to the verify context. + * @param verify Indicates the pointer to the verify context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. -- Gitee From 854de9c5eb86119a5a55f454001918aba8af77c0 Mon Sep 17 00:00:00 2001 From: lcc Date: Fri, 15 Nov 2024 09:07:06 +0800 Subject: [PATCH 4/7] =?UTF-8?q?AI=20Check=E5=91=8A=E8=AD=A6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lcc --- frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp | 7 +++++++ frameworks/js/napi/crypto/src/napi_kdf.cpp | 2 +- frameworks/key/sym_key_generator.c | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp index 8694588..8dc8055 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -15,6 +15,7 @@ #include "napi_asy_key_generator.h" +#include #include "securec.h" #include "log.h" #include "memory.h" @@ -700,6 +701,8 @@ static bool GetHcfKeyPairInstance(napi_env env, HcfKeyPair *returnKeyPair, napi_ napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callback_info info) { + clock_t begin = clock(); + napi_value thisVar = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); @@ -734,6 +737,10 @@ napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callbac return nullptr; } + clock_t end = clock(); + int duration = (end - begin)/CLOCKS_PER_SEC; + LOGD("duration num is:%d", duration); + return instance; } diff --git a/frameworks/js/napi/crypto/src/napi_kdf.cpp b/frameworks/js/napi/crypto/src/napi_kdf.cpp index 9a99858..a78dd85 100644 --- a/frameworks/js/napi/crypto/src/napi_kdf.cpp +++ b/frameworks/js/napi/crypto/src/napi_kdf.cpp @@ -384,7 +384,7 @@ static bool GetHkdfParamsSpec(napi_env env, napi_value arg, HcfKdfParamsSpec **p // get info、salt info = GetBlobFromKdfParamsSpec(env, arg, HKDF_PARAMS_INFO); salt = GetBlobFromKdfParamsSpec(env, arg, KDF_PARAMS_SALT); - if (info == nullptr or salt == nullptr) { + if (info == nullptr || salt == nullptr) { LOGE("fail to get info or salt"); break; } diff --git a/frameworks/key/sym_key_generator.c b/frameworks/key/sym_key_generator.c index 0b9f646..de3cba8 100644 --- a/frameworks/key/sym_key_generator.c +++ b/frameworks/key/sym_key_generator.c @@ -265,7 +265,7 @@ HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **re LOGE("Failed to allocate returnGenerator memory!"); return HCF_ERR_MALLOC; } - if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName)) { + if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) { LOGE("Failed to copy algoName!"); HcfFree(returnGenerator); return HCF_INVALID_PARAMS; -- Gitee From 501ffc6d8b806794d3a3e5b785126be9faf9ca9e Mon Sep 17 00:00:00 2001 From: lcc Date: Mon, 4 Nov 2024 16:05:26 +0800 Subject: [PATCH 5/7] =?UTF-8?q?RSA=E7=A7=81=E9=92=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E8=A7=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lcc --- .../inc/napi_crypto_framework_defines.h | 2 + frameworks/js/napi/crypto/inc/napi_utils.h | 2 + .../crypto/src/napi_asy_key_generator.cpp | 88 ++++--- .../js/napi/crypto/src/napi_pri_key.cpp | 45 +++- frameworks/js/napi/crypto/src/napi_utils.cpp | 162 ++++++++++++ interfaces/inner_api/key/asy_key_generator.h | 8 + interfaces/inner_api/key/key.h | 1 + interfaces/inner_api/key/pri_key.h | 12 + .../common/inc/openssl_adapter.h | 1 + .../common/inc/openssl_common.h | 1 + .../common/src/openssl_adapter.c | 5 + .../common/src/openssl_common.c | 1 + .../src/alg_25519_asy_key_generator_openssl.c | 6 +- .../src/dh_asy_key_generator_openssl.c | 6 +- .../src/dsa_asy_key_generator_openssl.c | 6 +- .../src/ecc_asy_key_generator_openssl.c | 6 +- .../src/rsa_asy_key_generator_openssl.c | 136 +++++++--- .../src/sm2_asy_key_generator_openssl.c | 6 +- .../src/crypto_rsa_asy_key_pem_test.cpp | 234 +++++++++++++++--- 19 files changed, 603 insertions(+), 125 deletions(-) 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 df7329d..0974a4e 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 1d415c7..2858380 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); 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 8dc8055..6f529e9 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_generator.cpp @@ -15,7 +15,6 @@ #include "napi_asy_key_generator.h" -#include #include "securec.h" #include "log.h" #include "memory.h" @@ -318,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; @@ -335,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) { @@ -701,8 +724,6 @@ static bool GetHcfKeyPairInstance(napi_env env, HcfKeyPair *returnKeyPair, napi_ napi_value NapiAsyKeyGenerator::JsGenerateKeyPairSync(napi_env env, napi_callback_info info) { - clock_t begin = clock(); - napi_value thisVar = nullptr; napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); @@ -736,11 +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; } - - clock_t end = clock(); - int duration = (end - begin)/CLOCKS_PER_SEC; - LOGD("duration num is:%d", duration); - return instance; } @@ -845,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!"); @@ -859,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; @@ -902,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); @@ -910,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_pri_key.cpp b/frameworks/js/napi/crypto/src/napi_pri_key.cpp index 25ae8f2..23d990b 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 b95168b..c97b6c0 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; diff --git a/interfaces/inner_api/key/asy_key_generator.h b/interfaces/inner_api/key/asy_key_generator.h index b89c518..f56f243 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 6f3b2fe..d5e8074 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 d2b2e59..aa1e89f 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 ab212bc..d624f9c 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 d96d65f..083ad8b 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 f38dd44..fb3b1e0 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 668741a..599792d 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 d29922b..13be947 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 8be2648..dc28494 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 2dff817..0a29419 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 f49d3ca..b2a29ab 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 fc31e1d..37f6d0f 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 0129be4..174221d 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 f734835..8ad2be8 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); +} } -- Gitee From 0d92f61bfc0f28ccedd0d9087397e397de4202ba Mon Sep 17 00:00:00 2001 From: zxz*3 Date: Tue, 3 Dec 2024 21:55:57 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=8B=86=E5=88=86ECC=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zxz*3 --- frameworks/js/napi/crypto/inc/napi_utils.h | 3 - .../js/napi/crypto/src/napi_dh_key_util.cpp | 105 +++++ .../js/napi/crypto/src/napi_ecc_key_util.cpp | 289 +++++++++++++ frameworks/js/napi/crypto/src/napi_utils.cpp | 391 ------------------ 4 files changed, 394 insertions(+), 394 deletions(-) diff --git a/frameworks/js/napi/crypto/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h index 2858380..3adf266 100644 --- a/frameworks/js/napi/crypto/inc/napi_utils.h +++ b/frameworks/js/napi/crypto/inc/napi_utils.h @@ -63,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_dh_key_util.cpp b/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp index 7174c1d..24e364b 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 0cd9cee..eb11d77 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_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp index c97b6c0..556f8a3 100644 --- a/frameworks/js/napi/crypto/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -1954,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); @@ -2141,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 -- Gitee From 7bb5daca042eaae31c3869d231daba802b26feb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E8=90=8C=E6=96=B0?= Date: Tue, 10 Dec 2024 09:24:30 +0800 Subject: [PATCH 7/7] fix getEncodedPem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 萌萌萌新 --- frameworks/cj/src/crypto_ffi.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/cj/src/crypto_ffi.cpp b/frameworks/cj/src/crypto_ffi.cpp index 9ba7662..3ecbf2c 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; } -- Gitee