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