Blame SOURCES/0054-Replace-size-check-with-more-meaningful-pubkey-check.patch

727bdf
From 2c0f7d46b8449423446cfe1e52fc1e1ecd506b62 Mon Sep 17 00:00:00 2001
727bdf
From: Tomas Mraz <tomas@openssl.org>
727bdf
Date: Wed, 2 Feb 2022 17:47:26 +0100
727bdf
Subject: [PATCH] Replace size check with more meaningful pubkey check
727bdf
727bdf
It does not make sense to check the size because this
727bdf
function can be used in other contexts than in TLS-1.3 and
727bdf
the value might not be padded to the size of p.
727bdf
727bdf
However it makes sense to do the partial pubkey check because
727bdf
there is no valid reason having the pubkey value outside the
727bdf
1 < pubkey < p-1 bounds.
727bdf
727bdf
Fixes #15465
727bdf
727bdf
Reviewed-by: Paul Dale <pauli@openssl.org>
727bdf
(Merged from https://github.com/openssl/openssl/pull/17630)
727bdf
---
727bdf
 crypto/dh/dh_key.c | 11 ++++-------
727bdf
 1 file changed, 4 insertions(+), 7 deletions(-)
727bdf
727bdf
diff --git a/crypto/dh/dh_key.c b/crypto/dh/dh_key.c
727bdf
index 6b8cd550f2..c78ed618bf 100644
727bdf
--- a/crypto/dh/dh_key.c
727bdf
+++ b/crypto/dh/dh_key.c
727bdf
@@ -375,20 +375,17 @@ int ossl_dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
727bdf
     int err_reason = DH_R_BN_ERROR;
727bdf
     BIGNUM *pubkey = NULL;
727bdf
     const BIGNUM *p;
727bdf
-    size_t p_size;
727bdf
+    int ret;
727bdf
 
727bdf
     if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
727bdf
         goto err;
727bdf
     DH_get0_pqg(dh, &p, NULL, NULL);
727bdf
-    if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
727bdf
+    if (p == NULL || BN_num_bytes(p) == 0) {
727bdf
         err_reason = DH_R_NO_PARAMETERS_SET;
727bdf
         goto err;
727bdf
     }
727bdf
-    /*
727bdf
-     * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
727bdf
-     * public key is of size not equal to size of p
727bdf
-     */
727bdf
-    if (BN_is_zero(pubkey) || p_size != len) {
727bdf
+    /* Prevent small subgroup attacks per RFC 8446 Section 4.2.8.1 */
727bdf
+    if (!ossl_dh_check_pub_key_partial(dh, pubkey, &ret)) {
727bdf
         err_reason = DH_R_INVALID_PUBKEY;
727bdf
         goto err;
727bdf
     }
727bdf
-- 
727bdf
2.35.1
727bdf