fd2893
diff -up openssl-1.0.2j/crypto/asn1/a_verify.c.deprecate-algos openssl-1.0.2j/crypto/asn1/a_verify.c
fd2893
--- openssl-1.0.2j/crypto/asn1/a_verify.c.deprecate-algos	2016-09-26 11:49:07.000000000 +0200
fd2893
+++ openssl-1.0.2j/crypto/asn1/a_verify.c	2017-01-09 16:47:11.666994197 +0100
fd2893
@@ -56,6 +56,9 @@
fd2893
  * [including the GNU Public Licence.]
fd2893
  */
fd2893
 
fd2893
+/* for secure_getenv */
fd2893
+#define _GNU_SOURCE
fd2893
+
fd2893
 #include <stdio.h>
fd2893
 #include <time.h>
fd2893
 
fd2893
@@ -133,6 +136,30 @@ int ASN1_verify(i2d_of_void *i2d, X509_A
fd2893
 
fd2893
 #endif
fd2893
 
fd2893
+static int legacy_mds[] = { NID_md5, NID_sha, NID_md4, NID_md2, 0 };
fd2893
+extern int private_ossl_allowed_legacy_mds[];
fd2893
+
fd2893
+static int is_md_legacy_disallowed(int mdnid)
fd2893
+{
fd2893
+    int i;
fd2893
+
fd2893
+    if (mdnid == NID_md5 && secure_getenv("OPENSSL_ENABLE_MD5_VERIFY") != NULL)
fd2893
+        return 0;
fd2893
+
fd2893
+    for (i = 0; legacy_mds[i] != 0; ++i) {
fd2893
+         if (mdnid == legacy_mds[i]) {
fd2893
+            int j;
fd2893
+
fd2893
+            for (j = 0; private_ossl_allowed_legacy_mds[j] != 0; ++j) {
fd2893
+                 if (mdnid == private_ossl_allowed_legacy_mds[j])
fd2893
+                     return 0;
fd2893
+            }
fd2893
+            return 1;
fd2893
+        }
fd2893
+     }
fd2893
+     return 0;
fd2893
+}
fd2893
+
fd2893
 int ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a,
fd2893
                      ASN1_BIT_STRING *signature, void *asn, EVP_PKEY *pkey)
fd2893
 {
fd2893
@@ -174,6 +201,10 @@ int ASN1_item_verify(const ASN1_ITEM *it
fd2893
         if (ret != 2)
fd2893
             goto err;
fd2893
         ret = -1;
fd2893
+    } else if (is_md_legacy_disallowed(mdnid)) {
fd2893
+        ASN1err(ASN1_F_ASN1_ITEM_VERIFY,
fd2893
+                ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM);
fd2893
+        goto err;
fd2893
     } else {
fd2893
         const EVP_MD *type;
fd2893
         type = EVP_get_digestbynid(mdnid);
fd2893
diff -up openssl-1.0.2j/crypto/o_init.c.deprecate-algos openssl-1.0.2j/crypto/o_init.c
fd2893
--- openssl-1.0.2j/crypto/o_init.c.deprecate-algos	2017-01-05 17:49:00.000000000 +0100
fd2893
+++ openssl-1.0.2j/crypto/o_init.c	2017-01-09 16:52:29.018298611 +0100
fd2893
@@ -64,11 +64,21 @@
fd2893
 # include <unistd.h>
fd2893
 # include <errno.h>
fd2893
 # include <stdlib.h>
fd2893
+# include <stdio.h>
fd2893
+# include <string.h>
fd2893
+# include <strings.h>
fd2893
+# include <ctype.h>
fd2893
 # include <openssl/fips.h>
fd2893
 # include <openssl/rand.h>
fd2893
+# include <openssl/dh.h>
fd2893
+# include <openssl/objects.h>
fd2893
 
fd2893
 # define FIPS_MODE_SWITCH_FILE "/proc/sys/crypto/fips_enabled"
fd2893
 
fd2893
+# define LEGACY_SETTINGS_FILE "/etc/pki/tls/legacy-settings"
fd2893
+
fd2893
+# define NUM_MAX_LEGACY_MDS 8
fd2893
+
fd2893
 static void init_fips_mode(void)
fd2893
 {
fd2893
     char buf[2] = "0";
fd2893
@@ -98,6 +108,115 @@ static void init_fips_mode(void)
fd2893
 }
fd2893
 #endif
fd2893
 
fd2893
+int private_ossl_allowed_legacy_mds[NUM_MAX_LEGACY_MDS + 1]; /* zero terminated */
fd2893
+
fd2893
+int private_ossl_minimum_dh_bits;
fd2893
+
fd2893
+static void parse_legacy_mds(char *p)
fd2893
+{
fd2893
+    int idx = 0;
fd2893
+    char *e = p;
fd2893
+
fd2893
+    while (p[0] != '\0') {
fd2893
+        while (e[0] != '\0' && !isspace(e[0]) && e[0] != ',') {
fd2893
+            ++e;
fd2893
+        }
fd2893
+        if (e[0] != '\0') {
fd2893
+            e[0] = '\0';
fd2893
+            ++e;
fd2893
+        }
fd2893
+
fd2893
+        if (strcasecmp(p, "md5") == 0) {
fd2893
+            private_ossl_allowed_legacy_mds[idx++] = NID_md5;
fd2893
+        } else if (strcasecmp(p, "md4") == 0) {
fd2893
+            private_ossl_allowed_legacy_mds[idx++] = NID_md4;
fd2893
+        } else if (strcasecmp(p, "sha") == 0) {
fd2893
+            private_ossl_allowed_legacy_mds[idx++] = NID_sha;
fd2893
+        } else if (strcasecmp(p, "md2") == 0) {
fd2893
+            private_ossl_allowed_legacy_mds[idx++] = NID_md2;
fd2893
+        }
fd2893
+
fd2893
+        if (idx >=
fd2893
+            sizeof(private_ossl_allowed_legacy_mds) /
fd2893
+            sizeof(private_ossl_allowed_legacy_mds[0])) {
fd2893
+            break;
fd2893
+        }
fd2893
+
fd2893
+        while (e[0] == ',' || isspace(e[0])) {
fd2893
+            ++e;
fd2893
+        }
fd2893
+
fd2893
+        p = e;
fd2893
+    }
fd2893
+}
fd2893
+
fd2893
+static void parse_minimum_dh_bits(char *p)
fd2893
+{
fd2893
+    private_ossl_minimum_dh_bits = strtol(p, NULL, 10);
fd2893
+    if (private_ossl_minimum_dh_bits < 512
fd2893
+        || private_ossl_minimum_dh_bits > OPENSSL_DH_MAX_MODULUS_BITS) {
fd2893
+        /* use default */
fd2893
+        private_ossl_minimum_dh_bits = 0;
fd2893
+    }
fd2893
+}
fd2893
+
fd2893
+static void load_legacy_settings(void)
fd2893
+{
fd2893
+    FILE *f;
fd2893
+    char *line = NULL;
fd2893
+    size_t len = 0;
fd2893
+
fd2893
+    if ((f = fopen(LEGACY_SETTINGS_FILE, "r")) == NULL) {
fd2893
+        return;
fd2893
+    }
fd2893
+
fd2893
+    while (getline(&line, &len, f) > 0) {
fd2893
+        char *p = line, *e, *val;
fd2893
+
fd2893
+        /* skip initial whitespace */
fd2893
+        while (isspace(p[0])) {
fd2893
+            ++p;
fd2893
+        }
fd2893
+
fd2893
+        e = p;
fd2893
+
fd2893
+        while (e[0] != '\0' && !isspace(e[0])) {
fd2893
+            ++e;
fd2893
+        }
fd2893
+
fd2893
+        /* terminate name, skip whitespace between name and value */
fd2893
+        if (e[0] != '\0') {
fd2893
+            e[0] = '\0';
fd2893
+            ++e;
fd2893
+            while (isspace(e[0])) {
fd2893
+                ++e;
fd2893
+            }
fd2893
+        }
fd2893
+
fd2893
+        val = e;
fd2893
+
fd2893
+        e = e + strlen(val);
fd2893
+
fd2893
+        /* trim terminating whitespace */
fd2893
+        while (e > val) {
fd2893
+            --e;
fd2893
+            if (isspace(e[0])) {
fd2893
+                e[0] = '\0';
fd2893
+            } else {
fd2893
+                break;
fd2893
+            }
fd2893
+        }
fd2893
+
fd2893
+        if (strcasecmp(p, "LegacySigningMDs") == 0) {
fd2893
+            parse_legacy_mds(val);
fd2893
+        } else if (strcasecmp(line, "MinimumDHBits") == 0) {
fd2893
+            parse_minimum_dh_bits(val);
fd2893
+        }
fd2893
+        /* simply skip other unrecognized lines */
fd2893
+    }
fd2893
+    (void)fclose(f);
fd2893
+}
fd2893
+
fd2893
 /*
fd2893
  * Perform any essential OpenSSL initialization operations. Currently only
fd2893
  * sets FIPS callbacks
fd2893
@@ -109,6 +228,7 @@ void __attribute__ ((constructor)) OPENS
fd2893
     if (done)
fd2893
         return;
fd2893
     done = 1;
fd2893
+    load_legacy_settings();
fd2893
 #ifdef OPENSSL_FIPS
fd2893
     if (!FIPS_module_installed()) {
fd2893
         return;
fd2893
diff -up openssl-1.0.2j/ssl/s3_clnt.c.deprecate-algos openssl-1.0.2j/ssl/s3_clnt.c
fd2893
--- openssl-1.0.2j/ssl/s3_clnt.c.deprecate-algos	2016-09-26 11:49:07.000000000 +0200
fd2893
+++ openssl-1.0.2j/ssl/s3_clnt.c	2017-01-09 17:01:19.428506961 +0100
fd2893
@@ -3478,6 +3478,8 @@ int ssl3_send_client_certificate(SSL *s)
fd2893
 
fd2893
 #define has_bits(i,m)   (((i)&(m)) == (m))
fd2893
 
fd2893
+extern int private_ossl_minimum_dh_bits;
fd2893
+
fd2893
 int ssl3_check_cert_and_algorithm(SSL *s)
fd2893
 {
fd2893
     int i, idx;
fd2893
@@ -3608,8 +3610,7 @@ int ssl3_check_cert_and_algorithm(SSL *s
fd2893
             DH_free(dh_srvr);
fd2893
         }
fd2893
 
fd2893
-        if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024)
fd2893
-            || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
fd2893
+        if (dh_size < (private_ossl_minimum_dh_bits ? private_ossl_minimum_dh_bits : 1024)) {
fd2893
             SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
fd2893
             goto f_err;
fd2893
         }