diff options
author | Milan Broz <gmazyland@gmail.com> | 2022-10-29 11:48:51 +0200 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2022-11-10 18:11:46 +0100 |
commit | 7abe06cbb52cf4c214f8eac1b5f9f3c6a38ed9fa (patch) | |
tree | e0f8de0ed239411fe5f143e34c6f4a4de018a666 /crypto/pem/pem_pkey.c | |
parent | c8df0736e42b909a158282ffc7c04fec6acd18fe (diff) |
pem: avoid segfault if PKEY is NULL in PEM_write_bio_PrivateKey
Make the code more robust and correctly handle EVP_PKEY set to NULL
instead of dereferencing null pointer.
Signed-off-by: Milan Broz <gmazyland@gmail.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19536)
(cherry picked from commit 373d90128042cb0409e347827d80b50a99d3965a)
Diffstat (limited to 'crypto/pem/pem_pkey.c')
-rw-r--r-- | crypto/pem/pem_pkey.c | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/crypto/pem/pem_pkey.c b/crypto/pem/pem_pkey.c index 53367c03db..8ffeed9d78 100644 --- a/crypto/pem/pem_pkey.c +++ b/crypto/pem/pem_pkey.c @@ -311,7 +311,7 @@ PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio) IMPLEMENT_PEM_provided_write_body_main(pkey, bio); legacy: - if (x->ameth == NULL || x->ameth->priv_encode != NULL) + if (x != NULL && (x->ameth == NULL || x->ameth->priv_encode != NULL)) return PEM_write_bio_PKCS8PrivateKey(out, x, enc, (const char *)kstr, klen, cb, u); return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u); @@ -336,6 +336,9 @@ int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x, EVP_PKEY *copy = NULL; int ret; + if (x == NULL) + return 0; + if (evp_pkey_is_assigned(x) && evp_pkey_is_provided(x) && evp_pkey_copy_downgraded(©, x)) |