Commit 0cf6a4c0c5 for bind

commit 0cf6a4c0c5139155b64e27b4a0df064cc1fffe3b
Author: OndÅ™ej Surý <ondrej@isc.org>
Date:   Wed Nov 25 11:16:49 2026 +0100

    Use BN_bn2binpad() instead of the BN_bn2bin_fixed() helpers

    The ECDSA code carried three copies of a helper that writes a BIGNUM
    into a fixed-size buffer padded with leading zeros.  All the supported
    crypto libraries provide BN_bn2binpad() for that, and it reports a
    number that does not fit instead of triggering an assertion failure.

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

diff --git a/lib/dns/opensslecdsa_link.c b/lib/dns/opensslecdsa_link.c
index e15f12d75f..dd2b9d13b6 100644
--- a/lib/dns/opensslecdsa_link.c
+++ b/lib/dns/opensslecdsa_link.c
@@ -70,19 +70,6 @@ opensslecdsa_key_alg_to_publickey_size(unsigned int key_alg) {
 	}
 }

-static int
-BN_bn2bin_fixed(const BIGNUM *bn, unsigned char *buf, int size) {
-	int bytes = size - BN_num_bytes(bn);
-
-	INSIST(bytes >= 0);
-
-	while (bytes-- > 0) {
-		*buf++ = 0;
-	}
-	BN_bn2bin(bn, buf);
-	return size;
-}
-
 static isc_result_t
 opensslecdsa_createctx(dst_key_t *key, dst_context_t *dctx) {
 	isc_result_t result = ISC_R_SUCCESS;
@@ -233,15 +220,17 @@ opensslecdsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
 	}

 	ECDSA_SIG_get0(ecdsasig, &r, &s);
-	BN_bn2bin_fixed(r, region.base, siglen / 2);
-	isc_region_consume(&region, siglen / 2);
-	BN_bn2bin_fixed(s, region.base, siglen / 2);
-	isc_region_consume(&region, siglen / 2);
-	ECDSA_SIG_free(ecdsasig);
+	if (BN_bn2binpad(r, region.base, siglen / 2) < 0 ||
+	    BN_bn2binpad(s, region.base + siglen / 2, siglen / 2) < 0)
+	{
+		CLEANUP(dst__openssl_toresult3(dctx->category, "BN_bn2binpad",
+					       ISC_R_FAILURE));
+	}
 	isc_buffer_add(sig, siglen);
 	result = ISC_R_SUCCESS;

 cleanup:
+	ECDSA_SIG_free(ecdsasig);
 	if (sigder != NULL && sigder_alloced != 0) {
 		isc_mem_put(dctx->mctx, sigder, sigder_alloced);
 	}
diff --git a/lib/isc/ossl_wrap/ossl1_1.c b/lib/isc/ossl_wrap/ossl1_1.c
index 4726a6b1c4..ad06da106e 100644
--- a/lib/isc/ossl_wrap/ossl1_1.c
+++ b/lib/isc/ossl_wrap/ossl1_1.c
@@ -85,19 +85,6 @@ constexpr size_t p384_public_key_size = 96;
 constexpr size_t p256_secret_key_size = 32;
 constexpr size_t p384_secret_key_size = 48;

-static int
-BN_bn2bin_fixed(const BIGNUM *bn, unsigned char *buf, int size) {
-	int bytes = size - BN_num_bytes(bn);
-
-	INSIST(bytes >= 0);
-
-	while (bytes-- > 0) {
-		*buf++ = 0;
-	}
-	BN_bn2bin(bn, buf);
-	return size;
-}
-
 static int
 rsa_keygen_progress_cb(int p, int n, BN_GENCB *cb) {
 	void (*fptr)(int);
@@ -349,7 +336,9 @@ ec_secret_region(EVP_PKEY *pkey, isc_region_t pub) {
 		CLEANUP(OSSL_WRAP_ERROR("EC_KEY_get0_private_key"));
 	}

-	BN_bn2bin_fixed(private, pub.base, pub.length);
+	if (BN_bn2binpad(private, pub.base, pub.length) < 0) {
+		CLEANUP(OSSL_WRAP_ERROR("BN_bn2binpad"));
+	}

 	result = ISC_R_SUCCESS;

diff --git a/lib/isc/ossl_wrap/ossl3.c b/lib/isc/ossl_wrap/ossl3.c
index f87f451dcb..b9a318748d 100644
--- a/lib/isc/ossl_wrap/ossl3.c
+++ b/lib/isc/ossl_wrap/ossl3.c
@@ -119,18 +119,6 @@ static const OSSL_PARAM p384_params[] = {
 };
 /* clang-format on */

-static void
-BN_bn2bin_fixed(const BIGNUM *bn, unsigned char *buf, int size) {
-	int bytes = size - BN_num_bytes(bn);
-
-	INSIST(bytes >= 0);
-
-	while (bytes-- > 0) {
-		*buf++ = 0;
-	}
-	BN_bn2bin(bn, buf);
-}
-
 static int
 rsa_keygen_progress_cb(EVP_PKEY_CTX *ctx) {
 	void (*fptr)(int);
@@ -491,8 +479,11 @@ ec_public_region(EVP_PKEY *pkey, isc_region_t pub) {
 		CLEANUP(OSSL_WRAP_ERROR("EVP_PKEY_get_bn_param"));
 	}

-	BN_bn2bin_fixed(x, &pub.base[0], pub.length / 2);
-	BN_bn2bin_fixed(y, &pub.base[pub.length / 2], pub.length / 2);
+	if (BN_bn2binpad(x, &pub.base[0], pub.length / 2) < 0 ||
+	    BN_bn2binpad(y, &pub.base[pub.length / 2], pub.length / 2) < 0)
+	{
+		CLEANUP(OSSL_WRAP_ERROR("BN_bn2binpad"));
+	}

 	result = ISC_R_SUCCESS;

@@ -517,7 +508,9 @@ ec_secret_region(EVP_PKEY *pkey, isc_region_t sec) {
 		goto cleanup;
 	}

-	BN_bn2bin_fixed(priv, sec.base, sec.length);
+	if (BN_bn2binpad(priv, sec.base, sec.length) < 0) {
+		CLEANUP(OSSL_WRAP_ERROR("BN_bn2binpad"));
+	}

 	result = ISC_R_SUCCESS;