Blame SOURCES/0052-Allow-SHA1-in-seclevel-2-if-rh-allow-sha1-signatures.patch

bf760f
From c63599ee9708d543205a9173207ee7167315c624 Mon Sep 17 00:00:00 2001
bf760f
From: Clemens Lang <cllang@redhat.com>
bf760f
Date: Tue, 1 Mar 2022 15:44:18 +0100
bf760f
Subject: [PATCH] Allow SHA1 in seclevel 2 if rh-allow-sha1-signatures = yes
bf760f
bf760f
References: rhbz#2055796
bf760f
---
bf760f
 crypto/x509/x509_vfy.c        | 19 ++++++++++-
bf760f
 doc/man5/config.pod           |  7 +++-
bf760f
 ssl/t1_lib.c                  | 64 ++++++++++++++++++++++++++++-------
bf760f
 test/recipes/25-test_verify.t |  7 ++--
bf760f
 4 files changed, 79 insertions(+), 18 deletions(-)
bf760f
bf760f
diff --git a/crypto/x509/x509_vfy.c b/crypto/x509/x509_vfy.c
bf760f
index ff3ca83de6..a549c1c111 100644
bf760f
--- a/crypto/x509/x509_vfy.c
bf760f
+++ b/crypto/x509/x509_vfy.c
bf760f
@@ -25,6 +25,7 @@
bf760f
 #include <openssl/objects.h>
bf760f
 #include <openssl/core_names.h>
bf760f
 #include "internal/dane.h"
bf760f
+#include "internal/sslconf.h"
bf760f
 #include "crypto/x509.h"
bf760f
 #include "x509_local.h"
bf760f
 
bf760f
@@ -3440,14 +3441,30 @@ static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert)
bf760f
 {
bf760f
     int secbits = -1;
bf760f
     int level = ctx->param->auth_level;
bf760f
+    int nid;
bf760f
+    OSSL_LIB_CTX *libctx = NULL;
bf760f
 
bf760f
     if (level <= 0)
bf760f
         return 1;
bf760f
     if (level > NUM_AUTH_LEVELS)
bf760f
         level = NUM_AUTH_LEVELS;
bf760f
 
bf760f
-    if (!X509_get_signature_info(cert, NULL, NULL, &secbits, NULL))
bf760f
+    if (ctx->libctx)
bf760f
+        libctx = ctx->libctx;
bf760f
+    else if (cert->libctx)
bf760f
+        libctx = cert->libctx;
bf760f
+    else
bf760f
+        libctx = OSSL_LIB_CTX_get0_global_default();
bf760f
+
bf760f
+    if (!X509_get_signature_info(cert, &nid, NULL, &secbits, NULL))
bf760f
         return 0;
bf760f
 
bf760f
+    if (nid == NID_sha1
bf760f
+            && ossl_ctx_legacy_digest_signatures_allowed(libctx, 0)
bf760f
+            && ctx->param->auth_level < 3)
bf760f
+        /* When rh-allow-sha1-signatures = yes and security level <= 2,
bf760f
+         * explicitly allow SHA1 for backwards compatibility. */
bf760f
+        return 1;
bf760f
+
bf760f
     return secbits >= minbits_table[level - 1];
bf760f
 }
bf760f
diff --git a/doc/man5/config.pod b/doc/man5/config.pod
bf760f
index aa1be5ca7f..aa69e2b844 100644
bf760f
--- a/doc/man5/config.pod
bf760f
+++ b/doc/man5/config.pod
bf760f
@@ -305,7 +305,12 @@ When set to B<no>, any attempt to create or verify a signature with a SHA1
bf760f
 digest will fail.  For compatibility with older versions of OpenSSL, set this
bf760f
 option to B<yes>.  This setting also affects TLS, where signature algorithms
bf760f
 that use SHA1 as digest will no longer be supported if this option is set to
bf760f
-B<no>.
bf760f
+B<no>.  Note that enabling B<rh-allow-sha1-signatures> will allow TLS signature
bf760f
+algorithms that use SHA1 in security level 2, despite the definition of
bf760f
+security level 2 of 112 bits of security, which SHA1 does not meet.  Because
bf760f
+TLS 1.1 or lower use MD5-SHA1 as pseudorandom function (PRF) to derive key
bf760f
+material, disabling B<rh-allow-sha1-signatures> requires the use of TLS 1.2 or
bf760f
+newer.
bf760f
 
bf760f
 =item B<fips_mode> (deprecated)
bf760f
 
bf760f
diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c
bf760f
index 4b74ee1a34..5f089de107 100644
bf760f
--- a/ssl/t1_lib.c
bf760f
+++ b/ssl/t1_lib.c
bf760f
@@ -20,6 +20,7 @@
bf760f
 #include <openssl/bn.h>
bf760f
 #include <openssl/provider.h>
bf760f
 #include <openssl/param_build.h>
bf760f
+#include "crypto/x509.h"
bf760f
 #include "internal/sslconf.h"
bf760f
 #include "internal/nelem.h"
bf760f
 #include "internal/sizes.h"
bf760f
@@ -1561,19 +1562,27 @@ int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
bf760f
         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST);
bf760f
         return 0;
bf760f
     }
bf760f
-    /*
bf760f
-     * Make sure security callback allows algorithm. For historical
bf760f
-     * reasons we have to pass the sigalg as a two byte char array.
bf760f
-     */
bf760f
-    sigalgstr[0] = (sig >> 8) & 0xff;
bf760f
-    sigalgstr[1] = sig & 0xff;
bf760f
-    secbits = sigalg_security_bits(s->ctx, lu);
bf760f
-    if (secbits == 0 ||
bf760f
-        !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits,
bf760f
-                      md != NULL ? EVP_MD_get_type(md) : NID_undef,
bf760f
-                      (void *)sigalgstr)) {
bf760f
-        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);
bf760f
-        return 0;
bf760f
+
bf760f
+    if (lu->hash == NID_sha1
bf760f
+            && ossl_ctx_legacy_digest_signatures_allowed(s->ctx->libctx, 0)
bf760f
+            && SSL_get_security_level(s) < 3) {
bf760f
+        /* when rh-allow-sha1-signatures = yes and security level <= 2,
bf760f
+         * explicitly allow SHA1 for backwards compatibility */
bf760f
+    } else {
bf760f
+        /*
bf760f
+         * Make sure security callback allows algorithm. For historical
bf760f
+         * reasons we have to pass the sigalg as a two byte char array.
bf760f
+         */
bf760f
+        sigalgstr[0] = (sig >> 8) & 0xff;
bf760f
+        sigalgstr[1] = sig & 0xff;
bf760f
+        secbits = sigalg_security_bits(s->ctx, lu);
bf760f
+        if (secbits == 0 ||
bf760f
+            !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits,
bf760f
+                          md != NULL ? EVP_MD_get_type(md) : NID_undef,
bf760f
+                          (void *)sigalgstr)) {
bf760f
+            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE);
bf760f
+            return 0;
bf760f
+        }
bf760f
     }
bf760f
     /* Store the sigalg the peer uses */
bf760f
     s->s3.tmp.peer_sigalg = lu;
bf760f
@@ -2106,6 +2115,14 @@ static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu)
bf760f
         }
bf760f
     }
bf760f
 
bf760f
+    if (lu->hash == NID_sha1
bf760f
+            && ossl_ctx_legacy_digest_signatures_allowed(s->ctx->libctx, 0)
bf760f
+            && SSL_get_security_level(s) < 3) {
bf760f
+        /* when rh-allow-sha1-signatures = yes and security level <= 2,
bf760f
+         * explicitly allow SHA1 for backwards compatibility */
bf760f
+        return 1;
bf760f
+    }
bf760f
+
bf760f
     /* Finally see if security callback allows it */
bf760f
     secbits = sigalg_security_bits(s->ctx, lu);
bf760f
     sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
bf760f
@@ -2977,6 +2994,8 @@ static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
bf760f
 {
bf760f
     /* Lookup signature algorithm digest */
bf760f
     int secbits, nid, pknid;
bf760f
+    OSSL_LIB_CTX *libctx = NULL;
bf760f
+
bf760f
     /* Don't check signature if self signed */
bf760f
     if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
bf760f
         return 1;
bf760f
@@ -2985,6 +3004,25 @@ static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
bf760f
     /* If digest NID not defined use signature NID */
bf760f
     if (nid == NID_undef)
bf760f
         nid = pknid;
bf760f
+
bf760f
+    if (x && x->libctx)
bf760f
+        libctx = x->libctx;
bf760f
+    else if (ctx && ctx->libctx)
bf760f
+        libctx = ctx->libctx;
bf760f
+    else if (s && s->ctx && s->ctx->libctx)
bf760f
+        libctx = s->ctx->libctx;
bf760f
+    else
bf760f
+        libctx = OSSL_LIB_CTX_get0_global_default();
bf760f
+
bf760f
+    if (nid == NID_sha1
bf760f
+            && ossl_ctx_legacy_digest_signatures_allowed(libctx, 0)
bf760f
+            && ((s != NULL && SSL_get_security_level(s) < 3)
bf760f
+                || (ctx != NULL && SSL_CTX_get_security_level(ctx) < 3)
bf760f
+            ))
bf760f
+        /* When rh-allow-sha1-signatures = yes and security level <= 2,
bf760f
+         * explicitly allow SHA1 for backwards compatibility. */
bf760f
+        return 1;
bf760f
+
bf760f
     if (s)
bf760f
         return ssl_security(s, op, secbits, nid, x);
bf760f
     else
bf760f
diff --git a/test/recipes/25-test_verify.t b/test/recipes/25-test_verify.t
bf760f
index 700bbd849c..2de1d76b5e 100644
bf760f
--- a/test/recipes/25-test_verify.t
bf760f
+++ b/test/recipes/25-test_verify.t
bf760f
@@ -29,7 +29,7 @@ sub verify {
bf760f
     run(app([@args]));
bf760f
 }
bf760f
 
727bdf
-plan tests => 163;
727bdf
+plan tests => 162;
bf760f
 
bf760f
 # Canonical success
bf760f
 ok(verify("ee-cert", "sslserver", ["root-cert"], ["ca-cert"]),
bf760f
@@ -387,8 +387,9 @@ ok(verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "0"
bf760f
 ok(verify("ee-pss-sha256-cert", "", ["root-cert"], ["ca-cert"], ),
bf760f
     "CA with PSS signature using SHA256");
bf760f
 
bf760f
-ok(!verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "1"),
bf760f
-    "Reject PSS signature using SHA1 and auth level 1");
bf760f
+## rh-allow-sha1-signatures=yes allows this to pass despite -auth_level 1
bf760f
+#ok(!verify("ee-pss-sha1-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "1"),
bf760f
+#    "Reject PSS signature using SHA1 and auth level 1");
bf760f
 
bf760f
 ok(verify("ee-pss-sha256-cert", "", ["root-cert"], ["ca-cert"], "-auth_level", "2"),
bf760f
     "PSS signature using SHA256 and auth level 2");
bf760f
-- 
bf760f
2.35.1
bf760f