diff --git a/src/we_ecc.c b/src/we_ecc.c index c39bb3ac..1c21dda4 100644 --- a/src/we_ecc.c +++ b/src/we_ecc.c @@ -321,6 +321,8 @@ typedef struct we_Ecc #endif } we_Ecc; +static void we_ec_cleanup(EVP_PKEY_CTX *ctx); + /** * Initialize and set the data required to complete an EC operation. * @@ -332,6 +334,9 @@ static int we_ec_init(EVP_PKEY_CTX *ctx) int ret, rc; we_Ecc *ecc; int keyInited = 0; +#ifndef WE_ECC_USE_GLOBAL_RNG + int rngInited = 0; +#endif WOLFENGINE_ENTER(WE_LOG_PK, "we_ec_init"); WOLFENGINE_MSG_VERBOSE(WE_LOG_PK, "ARGS [ctx = %p]", ctx); @@ -358,7 +363,10 @@ static int we_ec_init(EVP_PKEY_CTX *ctx) #ifndef WE_ECC_USE_GLOBAL_RNG if (ret == 1) { rc = wc_InitRng(&ecc->rng); - if (rc != 0) { + if (rc == 0) { + rngInited = 1; + } + else { ret = 0; } } @@ -385,7 +393,9 @@ static int we_ec_init(EVP_PKEY_CTX *ctx) if (ret == 0 && ecc != NULL) { #ifndef WE_ECC_USE_GLOBAL_RNG - wc_FreeRng(&ecc->rng); + if (rngInited) { + wc_FreeRng(&ecc->rng); + } #endif /* Make sure wolfSSL EC key is freed if initialized. */ if (keyInited) { @@ -430,6 +440,7 @@ static int we_ec_p192_init(EVP_PKEY_CTX *ctx) WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name", ecc->group); ret = 0; + we_ec_cleanup(ctx); } } @@ -466,6 +477,7 @@ static int we_ec_p224_init(EVP_PKEY_CTX *ctx) WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name", ecc->group); ret = 0; + we_ec_cleanup(ctx); } } @@ -503,6 +515,7 @@ static int we_ec_p256_init(EVP_PKEY_CTX *ctx) WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name", ecc->group); ret = 0; + we_ec_cleanup(ctx); } } @@ -540,6 +553,7 @@ static int we_ec_p384_init(EVP_PKEY_CTX *ctx) WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name", ecc->group); ret = 0; + we_ec_cleanup(ctx); } } @@ -578,6 +592,7 @@ static int we_ec_p521_init(EVP_PKEY_CTX *ctx) WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name", ecc->group); ret = 0; + we_ec_cleanup(ctx); } } @@ -602,15 +617,88 @@ static int we_ec_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src) static int we_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) #endif { + int ret = 1; + we_Ecc *srcEcc; + we_Ecc *dstEcc = NULL; + WOLFENGINE_ENTER(WE_LOG_PK, "we_ec_copy"); - /* Nothing to copy as src is empty. */ - (void)src; - (void)dst; + srcEcc = (we_Ecc *)EVP_PKEY_CTX_get_data(src); + EVP_PKEY_CTX_set_data(dst, NULL); + + if (srcEcc != NULL) { + ret = we_ec_init(dst); + } + if (ret == 1 && srcEcc != NULL) { + dstEcc = (we_Ecc *)EVP_PKEY_CTX_get_data(dst); + dstEcc->curveId = srcEcc->curveId; + dstEcc->curveName = srcEcc->curveName; +#ifdef WE_HAVE_ECDSA + dstEcc->md = srcEcc->md; +#endif +#ifdef WE_HAVE_ECKEYGEN + if (srcEcc->group != NULL) { + dstEcc->group = EC_GROUP_dup(srcEcc->group); + if (dstEcc->group == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_dup", + dstEcc->group); + ret = 0; + } + } +#endif +#ifdef WE_HAVE_ECDH + dstEcc->coFactor = srcEcc->coFactor; + dstEcc->kdfType = srcEcc->kdfType; + dstEcc->kdfMd = srcEcc->kdfMd; + dstEcc->kdfOutLen = srcEcc->kdfOutLen; + if (ret == 1 && srcEcc->peerKey != NULL) { + if (srcEcc->peerKeyLen <= 0) { + WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Invalid peer key length"); + ret = 0; + } + else { + dstEcc->peerKey = OPENSSL_malloc(srcEcc->peerKeyLen); + if (dstEcc->peerKey == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "OPENSSL_malloc", + dstEcc->peerKey); + ret = 0; + } + else { + XMEMCPY(dstEcc->peerKey, srcEcc->peerKey, + srcEcc->peerKeyLen); + dstEcc->peerKeyLen = srcEcc->peerKeyLen; + } + } + } + if (ret == 1 && srcEcc->kdfUkm != NULL) { + if (srcEcc->kdfUkmLen <= 0) { + WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Invalid KDF UKM length"); + ret = 0; + } + else { + dstEcc->kdfUkm = OPENSSL_malloc(srcEcc->kdfUkmLen); + if (dstEcc->kdfUkm == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "OPENSSL_malloc", + dstEcc->kdfUkm); + ret = 0; + } + else { + XMEMCPY(dstEcc->kdfUkm, srcEcc->kdfUkm, + srcEcc->kdfUkmLen); + dstEcc->kdfUkmLen = srcEcc->kdfUkmLen; + } + } + } +#endif + } + + if (ret == 0 && dstEcc != NULL) { + we_ec_cleanup(dst); + } - WOLFENGINE_LEAVE(WE_LOG_PK, "we_ec_copy", 1); + WOLFENGINE_LEAVE(WE_LOG_PK, "we_ec_copy", ret); - return 1; + return ret; } /** @@ -638,6 +726,10 @@ static void we_ec_cleanup(EVP_PKEY_CTX *ctx) OPENSSL_free(ecc->peerKey); ecc->peerKey = NULL; } + if (ecc->kdfUkm != NULL) { + OPENSSL_free(ecc->kdfUkm); + ecc->kdfUkm = NULL; + } #endif #ifndef WE_ECC_USE_GLOBAL_RNG wc_FreeRng(&ecc->rng); diff --git a/test/test_ecc.c b/test/test_ecc.c index 8eb852ce..83791be6 100644 --- a/test/test_ecc.c +++ b/test/test_ecc.c @@ -402,6 +402,60 @@ int test_eckeygen_p224(ENGINE *e, void *data) #endif /* WE_HAVE_EC_P224 */ #ifdef WE_HAVE_EC_P256 +#if defined(WE_HAVE_EC_P384) && OPENSSL_VERSION_NUMBER < 0x30000000L +static int test_eckeygen_copy(ENGINE *e) +{ + int err; + int shared = 0; + EVP_PKEY_CTX *src = NULL; + EVP_PKEY_CTX *dst = NULL; + EVP_PKEY *key = NULL; + EC_KEY *ecKey = NULL; + const EC_GROUP *group = NULL; + + err = (src = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, e)) == NULL; + if (err == 0) { + err = EVP_PKEY_keygen_init(src) != 1; + } + if (err == 0) { + err = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(src, + NID_X9_62_prime256v1) != 1; + } + if (err == 0) { + dst = EVP_PKEY_CTX_dup(src); + err = dst == NULL; + } + if (err == 0) { + shared = EVP_PKEY_CTX_get_data(src) == EVP_PKEY_CTX_get_data(dst); + err = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(src, NID_secp384r1) != 1; + } + if (err == 0) { + err = EVP_PKEY_keygen(dst, &key) != 1; + } + if (err == 0) { + ecKey = EVP_PKEY_get1_EC_KEY(key); + err = ecKey == NULL; + } + if (err == 0) { + group = EC_KEY_get0_group(ecKey); + err = group == NULL; + } + if (err == 0) { + err = EC_GROUP_get_curve_name(group) != NID_X9_62_prime256v1; + } + + if (shared) { + EVP_PKEY_CTX_set_data(dst, NULL); + } + EC_KEY_free(ecKey); + EVP_PKEY_free(key); + EVP_PKEY_CTX_free(dst); + EVP_PKEY_CTX_free(src); + + return err; +} +#endif + int test_eckeygen_p256_by_nid(ENGINE *e, void *data) { int err; @@ -420,6 +474,11 @@ int test_eckeygen_p256_by_nid(ENGINE *e, void *data) PRINT_MSG("Generate key"); err = EVP_PKEY_keygen(ctx, &key) != 1; } +#if defined(WE_HAVE_EC_P384) && OPENSSL_VERSION_NUMBER < 0x30000000L + if (err == 0) { + err = test_eckeygen_copy(e); + } +#endif EVP_PKEY_free(key); EVP_PKEY_CTX_free(ctx);