summaryrefslogtreecommitdiffstats
path: root/crypto/evp
diff options
context:
space:
mode:
authorTomas Mraz <tomas@openssl.org>2022-01-04 11:53:30 +0100
committerTomas Mraz <tomas@openssl.org>2022-01-07 09:51:45 +0100
commit86914ceadf2909204485605106cc121036ab091d (patch)
tree117369f33dc2f79f7b4bd0c10c3e34c371729a5e /crypto/evp
parent6bb8ef9d0fbe62ea39427eb0b1ffad916f6b8d16 (diff)
EVP_PKEY_fromdata(): Do not return newly allocated pkey on failure
Fixes #17407 Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/17411) (cherry picked from commit 5b03b89f7f925384c2768874c95f1af7053fd16f)
Diffstat (limited to 'crypto/evp')
-rw-r--r--crypto/evp/pmeth_gn.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/crypto/evp/pmeth_gn.c b/crypto/evp/pmeth_gn.c
index af3d990869..f9d001fdd0 100644
--- a/crypto/evp/pmeth_gn.c
+++ b/crypto/evp/pmeth_gn.c
@@ -365,6 +365,7 @@ int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
OSSL_PARAM params[])
{
void *keydata = NULL;
+ EVP_PKEY *allocated_pkey = NULL;
if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_FROMDATA) == 0) {
ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
@@ -375,7 +376,7 @@ int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
return -1;
if (*ppkey == NULL)
- *ppkey = EVP_PKEY_new();
+ allocated_pkey = *ppkey = EVP_PKEY_new();
if (*ppkey == NULL) {
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
@@ -383,8 +384,13 @@ int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
}
keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, params);
- if (keydata == NULL)
+ if (keydata == NULL) {
+ if (allocated_pkey != NULL) {
+ *ppkey = NULL;
+ EVP_PKEY_free(allocated_pkey);
+ }
return 0;
+ }
/* keydata is cached in *ppkey, so we need not bother with it further */
return 1;
}