From 0b0718fba3fb81507b7e82e6bca38007a94c475a Mon Sep 17 00:00:00 2001
From: Mark Andrews <marka@isc.org>
Date: Mon, 26 Sep 2022 11:52:55 +1000
Subject: [PATCH] Check that 'e' and 'n' are non-NULL in opensslrsa_todns

Conflict: NA
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/0b0718fba3fb81507b7e82e6bca38007a94c475a
(cherry picked from commit 5603cd69d170f49916bec3ca78ab3e4830170950)
---
 lib/dns/opensslrsa_link.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c
index 7aa743394b..b23b6323fa 100644
--- a/lib/dns/opensslrsa_link.c
+++ b/lib/dns/opensslrsa_link.c
@@ -585,13 +585,15 @@ opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
 	if (rsa == NULL) {
 		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
 	}
-
-	isc_buffer_availableregion(data, &r);
-
 	RSA_get0_key(rsa, &n, &e, NULL);
+	if (e == NULL || n == NULL) {
+		DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
+	}
 	mod_bytes = BN_num_bytes(n);
 	e_bytes = BN_num_bytes(e);
 
+	isc_buffer_availableregion(data, &r);
+
 	if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */
 		if (r.length < 1) {
 			DST_RET(ISC_R_NOSPACE);
-- 
2.23.0