Check whether the pubkey exists in ossl_ecx_key_dup

Signed-off-by: lan1120 <lanming@huawei.com>

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22984)
This commit is contained in:
lan1120 2023-12-13 19:02:29 +08:00 committed by Tomas Mraz
parent afd8e29c36
commit aac531e5da
2 changed files with 11 additions and 2 deletions

View File

@ -114,7 +114,7 @@ ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection)
return NULL;
ret->libctx = key->libctx;
ret->haspubkey = key->haspubkey;
ret->haspubkey = 0;
ret->keylen = key->keylen;
ret->type = key->type;
@ -127,8 +127,11 @@ ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection)
goto err;
}
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
&& key->haspubkey == 1) {
memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
ret->haspubkey = 1;
}
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
&& key->privkey != NULL) {

View File

@ -1523,6 +1523,12 @@ static int test_fromdata_ecx(int tst)
/* This should succeed because there are no parameters to copy */
|| !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk)))
goto err;
if (!TEST_ptr(ctx2 = EVP_PKEY_CTX_new_from_pkey(NULL, copy_pk, NULL))
/* This should fail because copy_pk has no pubkey */
|| !TEST_int_le(EVP_PKEY_public_check(ctx2), 0))
goto err;
EVP_PKEY_CTX_free(ctx2);
ctx2 = NULL;
EVP_PKEY_free(copy_pk);
copy_pk = NULL;