From b9f832edcce9db2de31070e76c3cbe59ca9ef512 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Thu, 12 Oct 2023 16:00:38 +0200
Subject: [PATCH] openssl: avoid BN_num_bits() NULL pointer derefs

Reported-by: icy17 on github
Fixes #12099
Closes #12100

Conflict: context adapt
Reference: https://github.com/curl/curl/commit/b9f832edcce9db2de31070e76c3cbe59ca9ef512
---
 lib/vtls/openssl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/vtls/openssl.c b/lib/vtls/openssl.c
index 00b56e2..50c3553 100644
--- a/lib/vtls/openssl.c
+++ b/lib/vtls/openssl.c
@@ -3676,13 +3676,13 @@ static CURLcode get_cert_chain(struct Curl_easy *data,
           const BIGNUM *e;

           RSA_get0_key(rsa, &n, &e, NULL);
-          BIO_printf(mem, "%d", BN_num_bits(n));
+          BIO_printf(mem, "%d", n ? BN_num_bits(n) : 0);
           push_certinfo("RSA Public Key", i);
           print_pubkey_BN(rsa, n, i);
           print_pubkey_BN(rsa, e, i);
         }
 #else
-        BIO_printf(mem, "%d", BN_num_bits(rsa->n));
+        BIO_printf(mem, "%d", rsa->n ? BN_num_bits(rsa->n) : 0);
         push_certinfo("RSA Public Key", i);
         print_pubkey_BN(rsa, n, i);
         print_pubkey_BN(rsa, e, i);
--
2.33.0
