Commit db655d8887 for bind

commit db655d88870482e4477086ff1b1a27af77aaa656
Author: OndÅ™ej Surý <ondrej@isc.org>
Date:   Wed Nov 25 11:18:34 2026 +0100

    Use a single implementation of the RSA public exponent range check

    The accepted range of the RSA public exponent was implemented twice:
    once in the DST code, comparing against two BIGNUM constants that had
    to be created when the library is initialized and destroyed when it is
    shut down, and once in the libcrypto wrappers, which allocated the same
    two constants on every call.  Replace both with one function that needs
    no constants at all.

    Assisted-by: Claude:claude-mythos-5-2

diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c
index 669b0ed9aa..9962d2e4c7 100644
--- a/lib/dns/dst_api.c
+++ b/lib/dns/dst_api.c
@@ -240,8 +240,6 @@ dst__lib_initialize(void) {

 void
 dst__lib_shutdown(void) {
-	dst__opensslrsa_shutdown();
-
 	isc_mem_detach(&dst__mctx);
 }

diff --git a/lib/dns/dst_internal.h b/lib/dns/dst_internal.h
index aa6024614e..36cf92c81c 100644
--- a/lib/dns/dst_internal.h
+++ b/lib/dns/dst_internal.h
@@ -196,8 +196,6 @@ dst__hmacsha512_init(struct dst_func **funcp);
 void
 dst__opensslrsa_init(struct dst_func **funcp, unsigned short algorithm);
 void
-dst__opensslrsa_shutdown(void);
-void
 dst__opensslecdsa_init(struct dst_func **funcp);
 void
 dst__openssleddsa_init(struct dst_func **funcp, unsigned char algorithm);
diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c
index fa9d199f4d..bc4063d13a 100644
--- a/lib/dns/opensslrsa_link.c
+++ b/lib/dns/opensslrsa_link.c
@@ -38,30 +38,6 @@ constexpr int rsa_min_modulus_bits = 512;
 constexpr unsigned int rsa_max_modulus_bytes = (rsa_max_modulus_bits + 7) / 8;
 constexpr unsigned int rsa_max_exponent_bytes = 5; /* 2^32 + 1 */

-static BIGNUM *rsa_exponent_min = NULL;
-static BIGNUM *rsa_exponent_max = NULL;
-
-/*
- * Accept odd public exponents in [3, 2^32 + 1].  That covers every Fermat
- * prime up to F5 and the odd intermediate values seen on the wire.
- */
-static bool
-rsa_exponent_in_range(const BIGNUM *e) {
-	if (!BN_is_odd(e)) {
-		return false;
-	}
-
-	if (BN_cmp(e, rsa_exponent_min) < 0) {
-		return false;
-	}
-
-	if (BN_cmp(e, rsa_exponent_max) > 0) {
-		return false;
-	}
-
-	return true;
-}
-
 /* length byte + 1.2.840.113549.1.1.11 BER encoded RFC 4055 */
 static unsigned char oid_rsasha256[] = { 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48,
 					 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b };
@@ -504,7 +480,7 @@ opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
 	{
 		CLEANUP(ISC_R_RANGE);
 	}
-	if (!rsa_exponent_in_range(c.e)) {
+	if (!isc_ossl_wrap_rsa_exponent_in_range(c.e)) {
 		CLEANUP(ISC_R_RANGE);
 	}
 	isc_buffer_forward(data, length);
@@ -753,7 +729,7 @@ opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
 	{
 		CLEANUP(ISC_R_RANGE);
 	}
-	if (!rsa_exponent_in_range(c.e)) {
+	if (!isc_ossl_wrap_rsa_exponent_in_range(c.e)) {
 		CLEANUP(ISC_R_RANGE);
 	}

@@ -974,31 +950,4 @@ dst__opensslrsa_init(dst_func_t **funcp, unsigned short algorithm) {
 			*funcp = &opensslrsa_functions;
 		}
 	}
-
-	if (rsa_exponent_min == NULL) {
-		rsa_exponent_min = BN_new();
-		INSIST(rsa_exponent_min != NULL);
-
-		RUNTIME_CHECK(BN_set_word(rsa_exponent_min, 3) == 1);
-	}
-
-	if (rsa_exponent_max == NULL) {
-		rsa_exponent_max = BN_new();
-		INSIST(rsa_exponent_max != NULL);
-
-		RUNTIME_CHECK(BN_set_bit(rsa_exponent_max, 0) == 1);
-		RUNTIME_CHECK(BN_set_bit(rsa_exponent_max, 32) == 1);
-	}
-}
-
-void
-dst__opensslrsa_shutdown(void) {
-	REQUIRE(rsa_exponent_min != NULL);
-	REQUIRE(rsa_exponent_max != NULL);
-
-	BN_free(rsa_exponent_min);
-	rsa_exponent_min = NULL;
-
-	BN_free(rsa_exponent_max);
-	rsa_exponent_max = NULL;
 }
diff --git a/lib/isc/include/isc/ossl_wrap.h b/lib/isc/include/isc/ossl_wrap.h
index 605f1c9af5..fc586a4abe 100644
--- a/lib/isc/include/isc/ossl_wrap.h
+++ b/lib/isc/include/isc/ossl_wrap.h
@@ -258,14 +258,23 @@ isc_ossl_wrap_generate_pkcs11_rsa_key(char *uri, size_t bit_size,
  * \li `uri != NULL` and is a NUL-terminated string
  */

+bool
+isc_ossl_wrap_rsa_exponent_in_range(const BIGNUM *e);
+/*%
+ * Returns true if the RSA public exponent `e` is odd and lies within
+ * the closed range [3, 2^32 + 1].  This covers every Fermat prime up to
+ * F5 plus all odd intermediate values seen in deployed DNSSEC keys.
+ *
+ * Requires:
+ * \li `e != NULL`
+ */
+
 bool
 isc_ossl_wrap_rsa_exponent_is_allowed(EVP_PKEY *pkey);
 /*%
- * Returns true if the RSA public exponent of `pkey` is odd and lies
- * within the closed range [3, 2^32 + 1].  This covers every Fermat
- * prime up to F5 plus all odd intermediate values seen in deployed
- * DNSSEC keys.  Returns false if the exponent cannot be retrieved or
- * falls outside that range.
+ * Returns true if the RSA public exponent of `pkey` is accepted by
+ * isc_ossl_wrap_rsa_exponent_in_range().  Returns false if the exponent
+ * cannot be retrieved or falls outside that range.
  */

 bool
diff --git a/lib/isc/ossl_wrap/ossl1_1.c b/lib/isc/ossl_wrap/ossl1_1.c
index ad06da106e..9e40254a4f 100644
--- a/lib/isc/ossl_wrap/ossl1_1.c
+++ b/lib/isc/ossl_wrap/ossl1_1.c
@@ -446,9 +446,6 @@ bool
 isc_ossl_wrap_rsa_exponent_is_allowed(EVP_PKEY *pkey) {
 	const RSA *rsa;
 	const BIGNUM *ce;
-	BIGNUM *emin = NULL;
-	BIGNUM *emax = NULL;
-	bool ok = false;

 	REQUIRE(pkey != NULL);

@@ -462,20 +459,7 @@ isc_ossl_wrap_rsa_exponent_is_allowed(EVP_PKEY *pkey) {
 		return false;
 	}

-	emin = BN_new();
-	if (emin == NULL || !BN_set_word(emin, 3)) {
-		goto cleanup;
-	}
-	if (BN_hex2bn(&emax, "100000001") == 0) {
-		goto cleanup;
-	}
-
-	ok = BN_is_odd(ce) && BN_cmp(ce, emin) >= 0 && BN_cmp(ce, emax) <= 0;
-
-cleanup:
-	BN_free(emin);
-	BN_free(emax);
-	return ok;
+	return isc_ossl_wrap_rsa_exponent_in_range(ce);
 }

 bool
diff --git a/lib/isc/ossl_wrap/ossl3.c b/lib/isc/ossl_wrap/ossl3.c
index b9a318748d..80b30f1899 100644
--- a/lib/isc/ossl_wrap/ossl3.c
+++ b/lib/isc/ossl_wrap/ossl3.c
@@ -660,28 +660,13 @@ cleanup:
 bool
 isc_ossl_wrap_rsa_exponent_is_allowed(EVP_PKEY *pkey) {
 	BIGNUM *e = NULL;
-	BIGNUM *emin = NULL;
-	BIGNUM *emax = NULL;
 	bool ok = false;

-	if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, &e) != 1) {
-		goto cleanup;
-	}
-
-	emin = BN_new();
-	if (emin == NULL || !BN_set_word(emin, 3)) {
-		goto cleanup;
-	}
-	if (BN_hex2bn(&emax, "100000001") == 0) {
-		goto cleanup;
+	if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, &e) == 1) {
+		ok = isc_ossl_wrap_rsa_exponent_in_range(e);
 	}

-	ok = BN_is_odd(e) && BN_cmp(e, emin) >= 0 && BN_cmp(e, emax) <= 0;
-
-cleanup:
 	BN_free(e);
-	BN_free(emin);
-	BN_free(emax);
 	return ok;
 }

diff --git a/lib/isc/ossl_wrap/ossl_common.c b/lib/isc/ossl_wrap/ossl_common.c
index 0828a37ff0..637892076d 100644
--- a/lib/isc/ossl_wrap/ossl_common.c
+++ b/lib/isc/ossl_wrap/ossl_common.c
@@ -45,6 +45,48 @@ isc_ossl_wrap_rsa_components_cleanup(isc_ossl_wrap_rsa_components_t *c) {
 	c->needs_cleanup = false;
 }

+bool
+isc_ossl_wrap_rsa_exponent_in_range(const BIGNUM *e) {
+	int bits;
+
+	REQUIRE(e != NULL);
+
+	/*
+	 * The accepted public exponents are the odd numbers in the closed
+	 * range [3, 2^32 + 1].  The range is checked by looking at the bits
+	 * of the number, so no BIGNUM constants are needed to compare with.
+	 */
+	if (BN_is_negative(e) || !BN_is_odd(e)) {
+		return false;
+	}
+
+	bits = BN_num_bits(e);
+
+	/* One bit means e == 1, more than 33 bits mean e >= 2^33. */
+	if (bits < 2 || bits > 33) {
+		return false;
+	}
+
+	/* An odd number with 2 to 32 bits is within [3, 2^32 - 1]. */
+	if (bits <= 32) {
+		return true;
+	}
+
+	/*
+	 * That leaves the numbers with exactly 33 bits, [2^32, 2^33 - 1].
+	 * Bit 32 is set in all of them, and bit 0 is set because the number
+	 * is odd.  The only one still in the range is 2^32 + 1, which is the
+	 * one that has none of the bits in between set.
+	 */
+	for (int bit = 1; bit < 32; bit++) {
+		if (BN_is_bit_set(e, bit)) {
+			return false;
+		}
+	}
+
+	return true;
+}
+
 isc_result_t
 isc_ossl_wrap_toresult(isc_result_t fallback) {
 	isc_result_t result = fallback;
diff --git a/tests/dns/rsa_test.c b/tests/dns/rsa_test.c
index 4146aa00c3..a1790963aa 100644
--- a/tests/dns/rsa_test.c
+++ b/tests/dns/rsa_test.c
@@ -397,10 +397,85 @@ ISC_RUN_TEST_IMPL(isc_rsa_fromdns_short_modulus) {
 	assert_null(key);
 }

+/*
+ * dst_key_fromdns accepts exactly the odd RSA public exponents in the
+ * closed range [3, 2^32 + 1].
+ */
+ISC_RUN_TEST_IMPL(isc_rsa_fromdns_exponent_bounds) {
+	const struct {
+		size_t len;
+		unsigned char e[5];
+		isc_result_t expect;
+	} testcases[] = {
+		/* 1 */
+		{ 1, { 0x01 }, ISC_R_RANGE },
+		/* 3 */
+		{ 1, { 0x03 }, ISC_R_SUCCESS },
+		/* 2^16 + 1 */
+		{ 3, { 0x01, 0x00, 0x01 }, ISC_R_SUCCESS },
+		/* 2^32 - 1 */
+		{ 4, { 0xff, 0xff, 0xff, 0xff }, ISC_R_SUCCESS },
+		/* 2^32 */
+		{ 5, { 0x01, 0x00, 0x00, 0x00, 0x00 }, ISC_R_RANGE },
+		/* 2^32 + 1 */
+		{ 5, { 0x01, 0x00, 0x00, 0x00, 0x01 }, ISC_R_SUCCESS },
+		/* 2^32 + 3 */
+		{ 5, { 0x01, 0x00, 0x00, 0x00, 0x03 }, ISC_R_RANGE },
+		/* 2^32 + 2^16 + 1 */
+		{ 5, { 0x01, 0x00, 0x01, 0x00, 0x01 }, ISC_R_RANGE },
+		/* 2^33 + 1 */
+		{ 5, { 0x02, 0x00, 0x00, 0x00, 0x01 }, ISC_R_RANGE },
+	};
+	isc_result_t result;
+	dns_fixedname_t fname;
+	dns_name_t *name;
+	isc_buffer_t buf;
+
+	UNUSED(state);
+
+	name = dns_fixedname_initname(&fname);
+	isc_buffer_constinit(&buf, "rsa.", 4);
+	isc_buffer_add(&buf, 4);
+	result = dns_name_fromtext(name, &buf, NULL, 0);
+	assert_int_equal(result, ISC_R_SUCCESS);
+
+	for (size_t t = 0; t < ARRAY_SIZE(testcases); t++) {
+		unsigned char rdata[300] = { 0 };
+		dst_key_t *key = NULL;
+		size_t i = 0;
+
+		/* DNSKEY rdata: flags(2) + proto(1) + alg(1) + key */
+		rdata[i++] = 0x01; /* flags hi (KSK) */
+		rdata[i++] = 0x00; /* flags lo */
+		rdata[i++] = 0x03; /* protocol */
+		rdata[i++] = DST_ALG_RSASHA256;
+		/* RSA wire key: e_bytes + e + n. */
+		rdata[i++] = testcases[t].len;
+		for (size_t j = 0; j < testcases[t].len; j++) {
+			rdata[i++] = testcases[t].e[j];
+		}
+		/* 256 bytes of arbitrary modulus (2048-bit). */
+		for (size_t j = 0; j < 256; j++) {
+			rdata[i++] = 0xAB;
+		}
+
+		isc_buffer_init(&buf, rdata, i);
+		isc_buffer_add(&buf, i);
+
+		result = dst_key_fromdns(name, dns_rdataclass_in, &buf,
+					 isc_g_mctx, &key);
+		assert_int_equal(result, testcases[t].expect);
+		if (key != NULL) {
+			dst_key_free(&key);
+		}
+	}
+}
+
 ISC_TEST_LIST_START
 ISC_TEST_ENTRY(isc_rsa_verify)
 ISC_TEST_ENTRY(isc_rsa_fromdns_oversized_exponent)
 ISC_TEST_ENTRY(isc_rsa_fromdns_even_exponent)
+ISC_TEST_ENTRY(isc_rsa_fromdns_exponent_bounds)
 ISC_TEST_ENTRY(isc_rsa_fromdns_oversized_modulus)
 ISC_TEST_ENTRY(isc_rsa_fromdns_short_modulus)
 ISC_TEST_LIST_END