Blame SOURCES/0049-Selectively-disallow-SHA1-signatures.patch

f57b16
From 243201772cc6d583fae9eba81cb2c2c7425bc564 Mon Sep 17 00:00:00 2001
f57b16
From: Clemens Lang <cllang@redhat.com>
f57b16
Date: Mon, 21 Feb 2022 17:24:44 +0100
f57b16
Subject: Selectively disallow SHA1 signatures
f57b16
f57b16
For RHEL 9.0, we want to phase out SHA1. One of the steps to do that is
f57b16
disabling SHA1 signatures. Introduce a new configuration option in the
f57b16
alg_section named 'rh-allow-sha1-signatures'. This option defaults to
f57b16
false. If set to false (or unset), any signature creation or
f57b16
verification operations that involve SHA1 as digest will fail.
f57b16
f57b16
This also affects TLS, where the signature_algorithms extension of any
f57b16
ClientHello message sent by OpenSSL will no longer include signatures
f57b16
with the SHA1 digest if rh-allow-sha1-signatures is false. For servers
f57b16
that request a client certificate, the same also applies for
f57b16
CertificateRequest messages sent by them.
f57b16
f57b16
For signatures created using the EVP_PKEY API, this is a best-effort
f57b16
check that will deny signatures in cases where the digest algorithm is
f57b16
known. This means, for example, that that following steps will still
f57b16
work:
f57b16
f57b16
 $> openssl dgst -sha1 -binary -out sha1 infile
f57b16
 $> openssl pkeyutl -inkey key.pem -sign -in sha1 -out sha1sig
f57b16
 $> openssl pkeyutl -inkey key.pem -verify -sigfile sha1sig -in sha1
f57b16
f57b16
whereas these will not:
f57b16
f57b16
 $> openssl dgst -sha1 -binary -out sha1 infile
f57b16
 $> openssl pkeyutl -inkey kem.pem -sign -in sha1 -out sha1sig -pkeyopt digest:sha1
f57b16
 $> openssl pkeyutl -inkey kem.pem -verify -sigfile sha1sig -in sha1 -pkeyopt digest:sha1
f57b16
f57b16
This happens because in the first case, OpenSSL's signature
f57b16
implementation does not know that it is signing a SHA1 hash (it could be
f57b16
signing arbitrary data).
f57b16
f57b16
Resolves: rhbz#2031742
f57b16
---
f57b16
 crypto/evp/evp_cnf.c                          | 13 ++++
f57b16
 crypto/evp/m_sigver.c                         | 77 +++++++++++++++++++
f57b16
 crypto/evp/pmeth_lib.c                        | 15 ++++
f57b16
 doc/man5/config.pod                           | 11 +++
f57b16
 include/internal/cryptlib.h                   |  3 +-
f57b16
 include/internal/sslconf.h                    |  4 +
f57b16
 providers/common/securitycheck.c              | 20 +++++
f57b16
 providers/common/securitycheck_default.c      |  9 ++-
f57b16
 providers/implementations/signature/dsa_sig.c | 11 ++-
f57b16
 .../implementations/signature/ecdsa_sig.c     |  4 +
f57b16
 providers/implementations/signature/rsa_sig.c | 20 ++++-
f57b16
 ssl/t1_lib.c                                  |  8 ++
f57b16
 util/libcrypto.num                            |  2 +
f57b16
 13 files changed, 188 insertions(+), 9 deletions(-)
f57b16
f57b16
diff --git a/crypto/evp/evp_cnf.c b/crypto/evp/evp_cnf.c
f57b16
index 0e7fe64cf9..b9d3b6d226 100644
f57b16
--- a/crypto/evp/evp_cnf.c
f57b16
+++ b/crypto/evp/evp_cnf.c
f57b16
@@ -10,6 +10,7 @@
f57b16
 #include <stdio.h>
f57b16
 #include <openssl/crypto.h>
f57b16
 #include "internal/cryptlib.h"
f57b16
+#include "internal/sslconf.h"
f57b16
 #include <openssl/conf.h>
f57b16
 #include <openssl/x509.h>
f57b16
 #include <openssl/x509v3.h>
f57b16
@@ -57,6 +58,18 @@ static int alg_module_init(CONF_IMODULE *md, const CONF *cnf)
f57b16
                 ERR_raise(ERR_LIB_EVP, EVP_R_SET_DEFAULT_PROPERTY_FAILURE);
f57b16
                 return 0;
f57b16
             }
f57b16
+        } else if (strcmp(oval->name, "rh-allow-sha1-signatures") == 0) {
f57b16
+            int m;
f57b16
+
f57b16
+            /* Detailed error already reported. */
f57b16
+            if (!X509V3_get_value_bool(oval, &m))
f57b16
+                return 0;
f57b16
+
f57b16
+            if (!ossl_ctx_legacy_digest_signatures_allowed_set(
f57b16
+                    NCONF_get0_libctx((CONF *)cnf), m > 0, 0)) {
f57b16
+                ERR_raise(ERR_LIB_EVP, EVP_R_SET_DEFAULT_PROPERTY_FAILURE);
f57b16
+                return 0;
f57b16
+            }
f57b16
         } else {
f57b16
             ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_OPTION,
f57b16
                            "name=%s, value=%s", oval->name, oval->value);
f57b16
diff --git a/crypto/evp/m_sigver.c b/crypto/evp/m_sigver.c
f57b16
index 9188edbc21..db1a1d7bc3 100644
f57b16
--- a/crypto/evp/m_sigver.c
f57b16
+++ b/crypto/evp/m_sigver.c
f57b16
@@ -16,6 +16,71 @@
f57b16
 #include "internal/numbers.h"   /* includes SIZE_MAX */
f57b16
 #include "evp_local.h"
f57b16
 
f57b16
+typedef struct ossl_legacy_digest_signatures_st {
f57b16
+    int allowed;
f57b16
+} OSSL_LEGACY_DIGEST_SIGNATURES;
f57b16
+
f57b16
+static void ossl_ctx_legacy_digest_signatures_free(void *vldsigs)
f57b16
+{
f57b16
+    OSSL_LEGACY_DIGEST_SIGNATURES *ldsigs = vldsigs;
f57b16
+
f57b16
+    if (ldsigs != NULL) {
f57b16
+        OPENSSL_free(ldsigs);
f57b16
+    }
f57b16
+}
f57b16
+
f57b16
+static void *ossl_ctx_legacy_digest_signatures_new(OSSL_LIB_CTX *ctx)
f57b16
+{
f57b16
+    return OPENSSL_zalloc(sizeof(OSSL_LEGACY_DIGEST_SIGNATURES));
f57b16
+}
f57b16
+
f57b16
+static const OSSL_LIB_CTX_METHOD ossl_ctx_legacy_digest_signatures_method = {
f57b16
+    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
f57b16
+    ossl_ctx_legacy_digest_signatures_new,
f57b16
+    ossl_ctx_legacy_digest_signatures_free,
f57b16
+};
f57b16
+
f57b16
+static OSSL_LEGACY_DIGEST_SIGNATURES *ossl_ctx_legacy_digest_signatures(
f57b16
+        OSSL_LIB_CTX *libctx, int loadconfig)
f57b16
+{
f57b16
+#ifndef FIPS_MODULE
f57b16
+    if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL))
f57b16
+        return 0;
f57b16
+#endif
f57b16
+
f57b16
+    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_LEGACY_DIGEST_SIGNATURES,
f57b16
+                                 &ossl_ctx_legacy_digest_signatures_method);
f57b16
+}
f57b16
+
f57b16
+int ossl_ctx_legacy_digest_signatures_allowed(OSSL_LIB_CTX *libctx, int loadconfig)
f57b16
+{
f57b16
+    OSSL_LEGACY_DIGEST_SIGNATURES *ldsigs
f57b16
+        = ossl_ctx_legacy_digest_signatures(libctx, loadconfig);
f57b16
+
f57b16
+#ifndef FIPS_MODULE
f57b16
+    if (ossl_safe_getenv("OPENSSL_ENABLE_SHA1_SIGNATURES") != NULL)
f57b16
+        /* used in tests */
f57b16
+        return 1;
f57b16
+#endif
f57b16
+
f57b16
+    return ldsigs != NULL ? ldsigs->allowed : 0;
f57b16
+}
f57b16
+
f57b16
+int ossl_ctx_legacy_digest_signatures_allowed_set(OSSL_LIB_CTX *libctx, int allow,
f57b16
+                                                  int loadconfig)
f57b16
+{
f57b16
+    OSSL_LEGACY_DIGEST_SIGNATURES *ldsigs
f57b16
+        = ossl_ctx_legacy_digest_signatures(libctx, loadconfig);
f57b16
+
f57b16
+    if (ldsigs == NULL) {
f57b16
+        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
f57b16
+        return 0;
f57b16
+    }
f57b16
+
f57b16
+    ldsigs->allowed = allow;
f57b16
+    return 1;
f57b16
+}
f57b16
+
f57b16
 #ifndef FIPS_MODULE
f57b16
 
f57b16
 static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
f57b16
@@ -258,6 +323,18 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
f57b16
         }
f57b16
     }
f57b16
 
f57b16
+    if (ctx->reqdigest != NULL
f57b16
+            && !EVP_PKEY_is_a(locpctx->pkey, SN_hmac)
f57b16
+            && !EVP_PKEY_is_a(locpctx->pkey, SN_tls1_prf)
f57b16
+            && !EVP_PKEY_is_a(locpctx->pkey, SN_hkdf)) {
f57b16
+        int mdnid = EVP_MD_nid(ctx->reqdigest);
f57b16
+        if (!ossl_ctx_legacy_digest_signatures_allowed(locpctx->libctx, 0)
f57b16
+                && (mdnid == NID_sha1 || mdnid == NID_md5_sha1)) {
f57b16
+            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
f57b16
+            goto err;
f57b16
+        }
f57b16
+    }
f57b16
+
f57b16
     if (ver) {
f57b16
         if (signature->digest_verify_init == NULL) {
f57b16
             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
f57b16
diff --git a/crypto/evp/pmeth_lib.c b/crypto/evp/pmeth_lib.c
f57b16
index 2b9c6c2351..3c5a1e6f5d 100644
f57b16
--- a/crypto/evp/pmeth_lib.c
f57b16
+++ b/crypto/evp/pmeth_lib.c
f57b16
@@ -33,6 +33,7 @@
f57b16
 #include "internal/ffc.h"
f57b16
 #include "internal/numbers.h"
f57b16
 #include "internal/provider.h"
f57b16
+#include "internal/sslconf.h"
f57b16
 #include "evp_local.h"
f57b16
 
f57b16
 #ifndef FIPS_MODULE
f57b16
@@ -946,6 +947,20 @@ static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md,
f57b16
         return -2;
f57b16
     }
f57b16
 
f57b16
+    if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
f57b16
+            && md != NULL
f57b16
+            && ctx->pkey != NULL
f57b16
+            && !EVP_PKEY_is_a(ctx->pkey, SN_hmac)
f57b16
+            && !EVP_PKEY_is_a(ctx->pkey, SN_tls1_prf)
f57b16
+            && !EVP_PKEY_is_a(ctx->pkey, SN_hkdf)) {
f57b16
+        int mdnid = EVP_MD_nid(md);
f57b16
+        if ((mdnid == NID_sha1 || mdnid == NID_md5_sha1)
f57b16
+                && !ossl_ctx_legacy_digest_signatures_allowed(ctx->libctx, 0)) {
f57b16
+            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST);
f57b16
+            return -1;
f57b16
+        }
f57b16
+    }
f57b16
+
f57b16
     if (fallback)
f57b16
         return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md));
f57b16
 
f57b16
diff --git a/doc/man5/config.pod b/doc/man5/config.pod
f57b16
index 77a8055e81..aa1be5ca7f 100644
f57b16
--- a/doc/man5/config.pod
f57b16
+++ b/doc/man5/config.pod
f57b16
@@ -304,6 +304,17 @@ Within the algorithm properties section, the following names have meaning:
f57b16
 The value may be anything that is acceptable as a property query
f57b16
 string for EVP_set_default_properties().
f57b16
 
f57b16
+=item B<rh-allow-sha1-signatures>
f57b16
+
f57b16
+The value is a boolean that can be B<yes> or B<no>.  If the value is not set,
f57b16
+it behaves as if it was set to B<no>.
f57b16
+
f57b16
+When set to B<no>, any attempt to create or verify a signature with a SHA1
f57b16
+digest will fail.  For compatibility with older versions of OpenSSL, set this
f57b16
+option to B<yes>.  This setting also affects TLS, where signature algorithms
f57b16
+that use SHA1 as digest will no longer be supported if this option is set to
f57b16
+B<no>.
f57b16
+
f57b16
 =item B<fips_mode> (deprecated)
f57b16
 
f57b16
 The value is a boolean that can be B<yes> or B<no>.  If the value is
f57b16
diff --git a/include/internal/cryptlib.h b/include/internal/cryptlib.h
f57b16
index 1291299b6e..e234341e6a 100644
f57b16
--- a/include/internal/cryptlib.h
f57b16
+++ b/include/internal/cryptlib.h
f57b16
@@ -168,7 +168,8 @@ typedef struct ossl_ex_data_global_st {
f57b16
 # define OSSL_LIB_CTX_PROVIDER_CONF_INDEX           16
f57b16
 # define OSSL_LIB_CTX_BIO_CORE_INDEX                17
f57b16
 # define OSSL_LIB_CTX_CHILD_PROVIDER_INDEX          18
f57b16
-# define OSSL_LIB_CTX_MAX_INDEXES                   19
f57b16
+# define OSSL_LIB_CTX_LEGACY_DIGEST_SIGNATURES      19
f57b16
+# define OSSL_LIB_CTX_MAX_INDEXES                   20
f57b16
 
f57b16
 # define OSSL_LIB_CTX_METHOD_LOW_PRIORITY          -1
f57b16
 # define OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY       0
f57b16
diff --git a/include/internal/sslconf.h b/include/internal/sslconf.h
f57b16
index fd7f7e3331..05464b0655 100644
f57b16
--- a/include/internal/sslconf.h
f57b16
+++ b/include/internal/sslconf.h
f57b16
@@ -18,4 +18,8 @@ int conf_ssl_name_find(const char *name, size_t *idx);
f57b16
 void conf_ssl_get_cmd(const SSL_CONF_CMD *cmd, size_t idx, char **cmdstr,
f57b16
                       char **arg);
f57b16
 
f57b16
+/* Methods to support disabling all signatures with legacy digests */
f57b16
+int ossl_ctx_legacy_digest_signatures_allowed(OSSL_LIB_CTX *libctx, int loadconfig);
f57b16
+int ossl_ctx_legacy_digest_signatures_allowed_set(OSSL_LIB_CTX *libctx, int allow,
f57b16
+                                                  int loadconfig);
f57b16
 #endif
f57b16
diff --git a/providers/common/securitycheck.c b/providers/common/securitycheck.c
f57b16
index 699ada7c52..e534ad0a5f 100644
f57b16
--- a/providers/common/securitycheck.c
f57b16
+++ b/providers/common/securitycheck.c
f57b16
@@ -19,6 +19,7 @@
f57b16
 #include <openssl/core_names.h>
f57b16
 #include <openssl/obj_mac.h>
f57b16
 #include "prov/securitycheck.h"
f57b16
+#include "internal/sslconf.h"
f57b16
 
f57b16
 /*
f57b16
  * FIPS requires a minimum security strength of 112 bits (for encryption or
f57b16
@@ -235,6 +236,15 @@ int ossl_digest_get_approved_nid_with_sha1(OSSL_LIB_CTX *ctx, const EVP_MD *md,
f57b16
             mdnid = -1; /* disallowed by security checks */
f57b16
     }
f57b16
 # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
f57b16
+
f57b16
+#ifndef FIPS_MODULE
f57b16
+    if (!ossl_ctx_legacy_digest_signatures_allowed(ctx, 0))
f57b16
+        /* SHA1 is globally disabled, check whether we want to locally allow
f57b16
+         * it. */
f57b16
+        if (mdnid == NID_sha1 && !sha1_allowed)
f57b16
+            mdnid = -1;
f57b16
+#endif
f57b16
+
f57b16
     return mdnid;
f57b16
 }
f57b16
 
f57b16
@@ -244,5 +254,15 @@ int ossl_digest_is_allowed(OSSL_LIB_CTX *ctx, const EVP_MD *md)
f57b16
     if (ossl_securitycheck_enabled(ctx))
f57b16
         return ossl_digest_get_approved_nid(md) != NID_undef;
f57b16
 # endif /* OPENSSL_NO_FIPS_SECURITYCHECKS */
f57b16
+
f57b16
+#ifndef FIPS_MODULE
f57b16
+    {
f57b16
+        int mdnid = EVP_MD_nid(md);
f57b16
+        if ((mdnid == NID_sha1 || mdnid == NID_md5_sha1)
f57b16
+                && !ossl_ctx_legacy_digest_signatures_allowed(ctx, 0))
f57b16
+            return 0;
f57b16
+    }
f57b16
+#endif
f57b16
+
f57b16
     return 1;
f57b16
 }
f57b16
diff --git a/providers/common/securitycheck_default.c b/providers/common/securitycheck_default.c
f57b16
index de7f0d3a0a..ce54a94fbc 100644
f57b16
--- a/providers/common/securitycheck_default.c
f57b16
+++ b/providers/common/securitycheck_default.c
f57b16
@@ -15,6 +15,7 @@
f57b16
 #include <openssl/obj_mac.h>
f57b16
 #include "prov/securitycheck.h"
f57b16
 #include "internal/nelem.h"
f57b16
+#include "internal/sslconf.h"
f57b16
 
f57b16
 /* Disable the security checks in the default provider */
f57b16
 int ossl_securitycheck_enabled(OSSL_LIB_CTX *libctx)
f57b16
@@ -23,9 +24,10 @@ int ossl_securitycheck_enabled(OSSL_LIB_CTX *libctx)
f57b16
 }
f57b16
 
f57b16
 int ossl_digest_rsa_sign_get_md_nid(OSSL_LIB_CTX *ctx, const EVP_MD *md,
f57b16
-                                    ossl_unused int sha1_allowed)
f57b16
+                                    int sha1_allowed)
f57b16
 {
f57b16
     int mdnid;
f57b16
+    int ldsigs_allowed;
f57b16
 
f57b16
     static const OSSL_ITEM name_to_nid[] = {
f57b16
         { NID_md5,       OSSL_DIGEST_NAME_MD5       },
f57b16
@@ -36,8 +38,11 @@ int ossl_digest_rsa_sign_get_md_nid(OSSL_LIB_CTX *ctx, const EVP_MD *md,
f57b16
         { NID_ripemd160, OSSL_DIGEST_NAME_RIPEMD160 },
f57b16
     };
f57b16
 
f57b16
-    mdnid = ossl_digest_get_approved_nid_with_sha1(ctx, md, 1);
f57b16
+    ldsigs_allowed = ossl_ctx_legacy_digest_signatures_allowed(ctx, 0);
f57b16
+    mdnid = ossl_digest_get_approved_nid_with_sha1(ctx, md, sha1_allowed || ldsigs_allowed);
f57b16
     if (mdnid == NID_undef)
f57b16
         mdnid = ossl_digest_md_to_nid(md, name_to_nid, OSSL_NELEM(name_to_nid));
f57b16
+    if (mdnid == NID_md5_sha1 && !ldsigs_allowed)
f57b16
+        mdnid = -1;
f57b16
     return mdnid;
f57b16
 }
f57b16
diff --git a/providers/implementations/signature/dsa_sig.c b/providers/implementations/signature/dsa_sig.c
f57b16
index 28fd7c498e..fa3822f39f 100644
f57b16
--- a/providers/implementations/signature/dsa_sig.c
f57b16
+++ b/providers/implementations/signature/dsa_sig.c
f57b16
@@ -124,12 +124,17 @@ static int dsa_setup_md(PROV_DSA_CTX *ctx,
f57b16
         mdprops = ctx->propq;
f57b16
 
f57b16
     if (mdname != NULL) {
f57b16
-        int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
f57b16
         WPACKET pkt;
f57b16
         EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
f57b16
-        int md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,
f57b16
-                                                            sha1_allowed);
f57b16
+        int md_nid;
f57b16
         size_t mdname_len = strlen(mdname);
f57b16
+#ifdef FIPS_MODULE
f57b16
+        int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
f57b16
+#else
f57b16
+        int sha1_allowed = 0;
f57b16
+#endif
f57b16
+        md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,
f57b16
+                                                            sha1_allowed);
f57b16
 
f57b16
         if (md == NULL || md_nid < 0) {
f57b16
             if (md == NULL)
f57b16
diff --git a/providers/implementations/signature/ecdsa_sig.c b/providers/implementations/signature/ecdsa_sig.c
f57b16
index 865d49d100..99b228e82c 100644
f57b16
--- a/providers/implementations/signature/ecdsa_sig.c
f57b16
+++ b/providers/implementations/signature/ecdsa_sig.c
f57b16
@@ -237,7 +237,11 @@ static int ecdsa_setup_md(PROV_ECDSA_CTX *ctx, const char *mdname,
f57b16
                        "%s could not be fetched", mdname);
f57b16
         return 0;
f57b16
     }
f57b16
+#ifdef FIPS_MODULE
f57b16
     sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
f57b16
+#else
f57b16
+    sha1_allowed = 0;
f57b16
+#endif
f57b16
     md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,
f57b16
                                                     sha1_allowed);
f57b16
     if (md_nid < 0) {
f57b16
diff --git a/providers/implementations/signature/rsa_sig.c b/providers/implementations/signature/rsa_sig.c
f57b16
index 325e855333..bea397f0c1 100644
f57b16
--- a/providers/implementations/signature/rsa_sig.c
f57b16
+++ b/providers/implementations/signature/rsa_sig.c
f57b16
@@ -26,6 +26,7 @@
f57b16
 #include "internal/cryptlib.h"
f57b16
 #include "internal/nelem.h"
f57b16
 #include "internal/sizes.h"
f57b16
+#include "internal/sslconf.h"
f57b16
 #include "crypto/rsa.h"
f57b16
 #include "prov/providercommon.h"
f57b16
 #include "prov/implementations.h"
f57b16
@@ -34,6 +35,7 @@
f57b16
 #include "prov/securitycheck.h"
f57b16
 
f57b16
 #define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
f57b16
+#define RSA_DEFAULT_DIGEST_NAME_NONLEGACY OSSL_DIGEST_NAME_SHA2_256
f57b16
 
f57b16
 static OSSL_FUNC_signature_newctx_fn rsa_newctx;
f57b16
 static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
f57b16
@@ -289,10 +291,15 @@ static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
f57b16
 
f57b16
     if (mdname != NULL) {
f57b16
         EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
f57b16
+        int md_nid;
f57b16
+        size_t mdname_len = strlen(mdname);
f57b16
+#ifdef FIPS_MODULE
f57b16
         int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
f57b16
-        int md_nid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md,
f57b16
+#else
f57b16
+        int sha1_allowed = 0;
f57b16
+#endif
f57b16
+        md_nid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md,
f57b16
                                                      sha1_allowed);
f57b16
-        size_t mdname_len = strlen(mdname);
f57b16
 
f57b16
         if (md == NULL
f57b16
             || md_nid <= 0
f57b16
@@ -1348,8 +1355,15 @@ static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
f57b16
     prsactx->pad_mode = pad_mode;
f57b16
 
f57b16
     if (prsactx->md == NULL && pmdname == NULL
f57b16
-        && pad_mode == RSA_PKCS1_PSS_PADDING)
f57b16
+        && pad_mode == RSA_PKCS1_PSS_PADDING) {
f57b16
         pmdname = RSA_DEFAULT_DIGEST_NAME;
f57b16
+#ifndef FIPS_MODULE
f57b16
+        if (!ossl_ctx_legacy_digest_signatures_allowed(prsactx->libctx, 0)) {
f57b16
+            pmdname = RSA_DEFAULT_DIGEST_NAME_NONLEGACY;
f57b16
+        }
f57b16
+#endif
f57b16
+    }
f57b16
+
f57b16
 
f57b16
     if (pmgf1mdname != NULL
f57b16
         && !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
f57b16
diff --git a/ssl/t1_lib.c b/ssl/t1_lib.c
f57b16
index fc32bb3556..4b74ee1a34 100644
f57b16
--- a/ssl/t1_lib.c
f57b16
+++ b/ssl/t1_lib.c
f57b16
@@ -20,6 +20,7 @@
f57b16
 #include <openssl/bn.h>
f57b16
 #include <openssl/provider.h>
f57b16
 #include <openssl/param_build.h>
f57b16
+#include "internal/sslconf.h"
f57b16
 #include "internal/nelem.h"
f57b16
 #include "internal/sizes.h"
f57b16
 #include "internal/tlsgroups.h"
f57b16
@@ -1145,11 +1146,13 @@ int ssl_setup_sig_algs(SSL_CTX *ctx)
f57b16
         = OPENSSL_malloc(sizeof(*lu) * OSSL_NELEM(sigalg_lookup_tbl));
f57b16
     EVP_PKEY *tmpkey = EVP_PKEY_new();
f57b16
     int ret = 0;
f57b16
+    int ldsigs_allowed;
f57b16
 
f57b16
     if (cache == NULL || tmpkey == NULL)
f57b16
         goto err;
f57b16
 
f57b16
     ERR_set_mark();
f57b16
+    ldsigs_allowed = ossl_ctx_legacy_digest_signatures_allowed(ctx->libctx, 0);
f57b16
     for (i = 0, lu = sigalg_lookup_tbl;
f57b16
          i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) {
f57b16
         EVP_PKEY_CTX *pctx;
f57b16
@@ -1169,6 +1172,11 @@ int ssl_setup_sig_algs(SSL_CTX *ctx)
f57b16
             cache[i].enabled = 0;
f57b16
             continue;
f57b16
         }
f57b16
+        if ((lu->hash == NID_sha1 || lu->hash == NID_md5_sha1)
f57b16
+                && !ldsigs_allowed) {
f57b16
+            cache[i].enabled = 0;
f57b16
+            continue;
f57b16
+        }
f57b16
 
f57b16
         if (!EVP_PKEY_set_type(tmpkey, lu->sig)) {
f57b16
             cache[i].enabled = 0;
f57b16
diff --git a/util/libcrypto.num b/util/libcrypto.num
f57b16
index 10b4e57d79..2d3c363bb0 100644
f57b16
--- a/util/libcrypto.num
f57b16
+++ b/util/libcrypto.num
f57b16
@@ -5426,3 +5426,5 @@ ASN1_TIME_print_ex                      5553	3_0_0	EXIST::FUNCTION:
1ac26c
 OPENSSL_strcasecmp                      5556	3_0_3	EXIST::FUNCTION:
1ac26c
 OPENSSL_strncasecmp                     5557	3_0_3	EXIST::FUNCTION:
f57b16
 ossl_safe_getenv                        ?	3_0_0	EXIST::FUNCTION:
f57b16
+ossl_ctx_legacy_digest_signatures_allowed ?	3_0_1	EXIST::FUNCTION:
f57b16
+ossl_ctx_legacy_digest_signatures_allowed_set ?	3_0_1	EXIST::FUNCTION:
f57b16
-- 
f57b16
2.35.1
f57b16