Blame SOURCES/openssl-1.1.1-system-cipherlist.patch

782d48
diff -up openssl-1.1.1c/Configurations/unix-Makefile.tmpl.system-cipherlist openssl-1.1.1c/Configurations/unix-Makefile.tmpl
782d48
--- openssl-1.1.1c/Configurations/unix-Makefile.tmpl.system-cipherlist	2019-05-29 15:42:27.951329271 +0200
782d48
+++ openssl-1.1.1c/Configurations/unix-Makefile.tmpl	2019-05-29 15:42:27.974328867 +0200
e4b8d1
@@ -180,6 +180,10 @@ MANDIR=$(INSTALLTOP)/share/man
e4b8d1
 DOCDIR=$(INSTALLTOP)/share/doc/$(BASENAME)
e4b8d1
 HTMLDIR=$(DOCDIR)/html
e4b8d1
 
e4b8d1
+{- output_off() if $config{system_ciphers_file} eq ""; "" -}
e4b8d1
+SYSTEM_CIPHERS_FILE_DEFINE=-DSYSTEM_CIPHERS_FILE="\"{- $config{system_ciphers_file} -}\""
e4b8d1
+{- output_on() if $config{system_ciphers_file} eq ""; "" -}
e4b8d1
+
e4b8d1
 # MANSUFFIX is for the benefit of anyone who may want to have a suffix
e4b8d1
 # appended after the manpage file section number.  "ssl" is popular,
e4b8d1
 # resulting in files such as config.5ssl rather than config.5.
e4b8d1
@@ -203,6 +207,7 @@ CC=$(CROSS_COMPILE){- $config{CC} -}
e4b8d1
 CXX={- $config{CXX} ? "\$(CROSS_COMPILE)$config{CXX}" : '' -}
e4b8d1
 CPPFLAGS={- our $cppflags1 = join(" ",
e4b8d1
                                   (map { "-D".$_} @{$config{CPPDEFINES}}),
e4b8d1
+                                  "\$(SYSTEM_CIPHERS_FILE_DEFINE)",
e4b8d1
                                   (map { "-I".$_} @{$config{CPPINCLUDES}}),
e4b8d1
                                   @{$config{CPPFLAGS}}) -}
e4b8d1
 CFLAGS={- join(' ', @{$config{CFLAGS}}) -}
782d48
diff -up openssl-1.1.1c/Configure.system-cipherlist openssl-1.1.1c/Configure
782d48
--- openssl-1.1.1c/Configure.system-cipherlist	2019-05-28 15:12:21.000000000 +0200
782d48
+++ openssl-1.1.1c/Configure	2019-05-29 15:45:10.465469533 +0200
e4b8d1
@@ -24,7 +24,7 @@ use OpenSSL::Glob;
e4b8d1
 my $orig_death_handler = $SIG{__DIE__};
e4b8d1
 $SIG{__DIE__} = \&death_handler;
e4b8d1
 
782d48
-my $usage="Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
782d48
+my $usage="Usage: Configure [no-<cipher> ...] [enable-<cipher> ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--system-ciphers-file=SYSTEMCIPHERFILE] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
e4b8d1
 
e4b8d1
 # Options:
e4b8d1
 #
e4b8d1
@@ -41,6 +41,9 @@ my $usage="Usage: Configure [no-<cipher>
e4b8d1
 #               This becomes the value of OPENSSLDIR in Makefile and in C.
e4b8d1
 #               (Default: PREFIX/ssl)
e4b8d1
 #
e4b8d1
+# --system-ciphers-file  A file to read cipher string from when the PROFILE=SYSTEM
e4b8d1
+#		cipher is specified (default).
e4b8d1
+#
e4b8d1
 # --cross-compile-prefix Add specified prefix to binutils components.
e4b8d1
 #
e4b8d1
 # --api         One of 0.9.8, 1.0.0 or 1.1.0.  Do not compile support for
e4b8d1
@@ -295,6 +298,7 @@ $config{prefix}="";
e4b8d1
 $config{openssldir}="";
e4b8d1
 $config{processor}="";
e4b8d1
 $config{libdir}="";
e4b8d1
+$config{system_ciphers_file}="";
e4b8d1
 my $auto_threads=1;    # enable threads automatically? true by default
e4b8d1
 my $default_ranlib;
e4b8d1
 
782d48
@@ -824,6 +828,10 @@ while (@argvcopy)
782d48
                             push @seed_sources, $x;
782d48
                             }
e4b8d1
                         }
e4b8d1
+		elsif (/^--system-ciphers-file=(.*)$/)
e4b8d1
+			{
e4b8d1
+			$config{system_ciphers_file}=$1;
e4b8d1
+			}
782d48
                 elsif (/^--cross-compile-prefix=(.*)$/)
782d48
                         {
782d48
                         $user{CROSS_COMPILE}=$1;
782d48
@@ -1016,6 +1024,8 @@ if ($target eq "HASH") {
e4b8d1
     exit 0;
e4b8d1
 }
e4b8d1
 
e4b8d1
+chop $config{system_ciphers_file} if $config{system_ciphers_file} =~ /\/$/;
e4b8d1
+
e4b8d1
 print "Configuring OpenSSL version $config{version} ($config{version_num}) ";
e4b8d1
 print "for $target\n";
e4b8d1
 
782d48
diff -up openssl-1.1.1c/doc/man1/ciphers.pod.system-cipherlist openssl-1.1.1c/doc/man1/ciphers.pod
782d48
--- openssl-1.1.1c/doc/man1/ciphers.pod.system-cipherlist	2019-05-28 15:12:21.000000000 +0200
782d48
+++ openssl-1.1.1c/doc/man1/ciphers.pod	2019-05-29 15:42:27.975328849 +0200
e4b8d1
@@ -182,6 +182,15 @@ As of OpenSSL 1.0.0, the B<ALL> cipher s
e4b8d1
 
e4b8d1
 The cipher suites not enabled by B<ALL>, currently B<eNULL>.
e4b8d1
 
e4b8d1
+=item B<PROFILE=SYSTEM>
e4b8d1
+
e4b8d1
+The list of enabled cipher suites will be loaded from the system crypto policy
e4b8d1
+configuration file B</etc/crypto-policies/back-ends/openssl.config>.
e4b8d1
+See also L<update-crypto-policies(8)>.
e4b8d1
+This is the default behavior unless an application explicitly sets a cipher
e4b8d1
+list. If used in a cipher list configuration value this string must be at the
e4b8d1
+beginning of the cipher list, otherwise it will not be recognized.
e4b8d1
+
e4b8d1
 =item B<HIGH>
e4b8d1
 
e4b8d1
 "High" encryption cipher suites. This currently means those with key lengths
782d48
diff -up openssl-1.1.1c/include/openssl/ssl.h.system-cipherlist openssl-1.1.1c/include/openssl/ssl.h
782d48
--- openssl-1.1.1c/include/openssl/ssl.h.system-cipherlist	2019-05-28 15:12:21.000000000 +0200
782d48
+++ openssl-1.1.1c/include/openssl/ssl.h	2019-05-29 15:42:27.975328849 +0200
e4b8d1
@@ -186,6 +186,11 @@ extern "C" {
e4b8d1
  * throwing out anonymous and unencrypted ciphersuites! (The latter are not
e4b8d1
  * actually enabled by ALL, but "ALL:RSA" would enable some of them.)
e4b8d1
  */
e4b8d1
+# ifdef SYSTEM_CIPHERS_FILE
e4b8d1
+#  define SSL_SYSTEM_DEFAULT_CIPHER_LIST "PROFILE=SYSTEM"
e4b8d1
+# else
e4b8d1
+#  define SSL_SYSTEM_DEFAULT_CIPHER_LIST SSL_DEFAULT_CIPHER_LIST
e4b8d1
+# endif
e4b8d1
 
e4b8d1
 /* Used in SSL_set_shutdown()/SSL_get_shutdown(); */
e4b8d1
 # define SSL_SENT_SHUTDOWN       1
782d48
diff -up openssl-1.1.1c/ssl/ssl_ciph.c.system-cipherlist openssl-1.1.1c/ssl/ssl_ciph.c
782d48
--- openssl-1.1.1c/ssl/ssl_ciph.c.system-cipherlist	2019-05-28 15:12:21.000000000 +0200
782d48
+++ openssl-1.1.1c/ssl/ssl_ciph.c	2019-05-29 15:42:27.976328831 +0200
e4b8d1
@@ -9,6 +9,8 @@
e4b8d1
  * https://www.openssl.org/source/license.html
e4b8d1
  */
e4b8d1
 
e4b8d1
+/* for secure_getenv */
e4b8d1
+#define _GNU_SOURCE
e4b8d1
 #include <stdio.h>
e4b8d1
 #include <ctype.h>
e4b8d1
 #include <openssl/objects.h>
782d48
@@ -1399,6 +1401,53 @@ int SSL_set_ciphersuites(SSL *s, const c
e4b8d1
     return ret;
e4b8d1
 }
e4b8d1
 
e4b8d1
+#ifdef SYSTEM_CIPHERS_FILE
e4b8d1
+static char *load_system_str(const char *suffix)
e4b8d1
+{
e4b8d1
+    FILE *fp;
e4b8d1
+    char buf[1024];
e4b8d1
+    char *new_rules;
e4b8d1
+    const char *ciphers_path;
e4b8d1
+    unsigned len, slen;
e4b8d1
+
e4b8d1
+    if ((ciphers_path = secure_getenv("OPENSSL_SYSTEM_CIPHERS_OVERRIDE")) == NULL)
e4b8d1
+        ciphers_path = SYSTEM_CIPHERS_FILE;
e4b8d1
+    fp = fopen(ciphers_path, "r");
e4b8d1
+    if (fp == NULL || fgets(buf, sizeof(buf), fp) == NULL) {
e4b8d1
+        /* cannot open or file is empty */
e4b8d1
+        snprintf(buf, sizeof(buf), "%s", SSL_DEFAULT_CIPHER_LIST);
e4b8d1
+    }
e4b8d1
+
e4b8d1
+    if (fp)
e4b8d1
+        fclose(fp);
e4b8d1
+
e4b8d1
+    slen = strlen(suffix);
e4b8d1
+    len = strlen(buf);
e4b8d1
+
e4b8d1
+    if (buf[len - 1] == '\n') {
e4b8d1
+        len--;
e4b8d1
+        buf[len] = 0;
e4b8d1
+    }
e4b8d1
+    if (buf[len - 1] == '\r') {
e4b8d1
+        len--;
e4b8d1
+        buf[len] = 0;
e4b8d1
+    }
e4b8d1
+
e4b8d1
+    new_rules = OPENSSL_malloc(len + slen + 1);
e4b8d1
+    if (new_rules == 0)
e4b8d1
+        return NULL;
e4b8d1
+
e4b8d1
+    memcpy(new_rules, buf, len);
e4b8d1
+    if (slen > 0) {
e4b8d1
+        memcpy(&new_rules[len], suffix, slen);
e4b8d1
+        len += slen;
e4b8d1
+    }
e4b8d1
+    new_rules[len] = 0;
e4b8d1
+
e4b8d1
+    return new_rules;
e4b8d1
+}
e4b8d1
+#endif
e4b8d1
+
e4b8d1
 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
e4b8d1
                                              STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
e4b8d1
                                              STACK_OF(SSL_CIPHER) **cipher_list,
782d48
@@ -1412,15 +1461,25 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
     const char *rule_p;
e4b8d1
     CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
e4b8d1
     const SSL_CIPHER **ca_list = NULL;
e4b8d1
+#ifdef SYSTEM_CIPHERS_FILE
e4b8d1
+    char *new_rules = NULL;
e4b8d1
+
e4b8d1
+    if (rule_str != NULL && strncmp(rule_str, "PROFILE=SYSTEM", 14) == 0) {
e4b8d1
+        char *p = rule_str + 14;
e4b8d1
+    
e4b8d1
+        new_rules = load_system_str(p);
e4b8d1
+        rule_str = new_rules;
e4b8d1
+    }
e4b8d1
+#endif
e4b8d1
 
e4b8d1
     /*
e4b8d1
      * Return with error if nothing to do.
e4b8d1
      */
e4b8d1
     if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
e4b8d1
-        return NULL;
e4b8d1
+        goto err;
e4b8d1
 #ifndef OPENSSL_NO_EC
e4b8d1
     if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
e4b8d1
-        return NULL;
e4b8d1
+        goto err;
e4b8d1
 #endif
e4b8d1
 
e4b8d1
     /*
782d48
@@ -1443,7 +1502,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
     co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
e4b8d1
     if (co_list == NULL) {
e4b8d1
         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
e4b8d1
-        return NULL;          /* Failure */
e4b8d1
+        goto err;
e4b8d1
     }
e4b8d1
 
e4b8d1
     ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
782d48
@@ -1509,8 +1568,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
      * in force within each class
e4b8d1
      */
e4b8d1
     if (!ssl_cipher_strength_sort(&head, &tail)) {
e4b8d1
-        OPENSSL_free(co_list);
e4b8d1
-        return NULL;
e4b8d1
+        goto err;
e4b8d1
     }
e4b8d1
 
e4b8d1
     /*
782d48
@@ -1555,9 +1613,8 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
     num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
e4b8d1
     ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
e4b8d1
     if (ca_list == NULL) {
e4b8d1
-        OPENSSL_free(co_list);
e4b8d1
         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
e4b8d1
-        return NULL;          /* Failure */
e4b8d1
+        goto err;
e4b8d1
     }
e4b8d1
     ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
e4b8d1
                                disabled_mkey, disabled_auth, disabled_enc,
782d48
@@ -1583,8 +1640,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
     OPENSSL_free(ca_list);      /* Not needed anymore */
e4b8d1
 
e4b8d1
     if (!ok) {                  /* Rule processing failure */
e4b8d1
-        OPENSSL_free(co_list);
e4b8d1
-        return NULL;
e4b8d1
+        goto err;
e4b8d1
     }
e4b8d1
 
e4b8d1
     /*
782d48
@@ -1592,14 +1648,18 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
      * if we cannot get one.
e4b8d1
      */
e4b8d1
     if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
e4b8d1
-        OPENSSL_free(co_list);
e4b8d1
-        return NULL;
e4b8d1
+        goto err;
e4b8d1
     }
e4b8d1
 
e4b8d1
+#ifdef SYSTEM_CIPHERS_FILE
e4b8d1
+    OPENSSL_free(new_rules);    /* Not needed anymore */
e4b8d1
+#endif
e4b8d1
+
e4b8d1
     /* Add TLSv1.3 ciphers first - we always prefer those if possible */
e4b8d1
     for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++) {
e4b8d1
         if (!sk_SSL_CIPHER_push(cipherstack,
e4b8d1
                                 sk_SSL_CIPHER_value(tls13_ciphersuites, i))) {
e4b8d1
+            OPENSSL_free(co_list);
e4b8d1
             sk_SSL_CIPHER_free(cipherstack);
e4b8d1
             return NULL;
e4b8d1
         }
782d48
@@ -1631,6 +1691,14 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
e4b8d1
     *cipher_list = cipherstack;
e4b8d1
 
e4b8d1
     return cipherstack;
e4b8d1
+
e4b8d1
+err:
e4b8d1
+    OPENSSL_free(co_list);
e4b8d1
+#ifdef SYSTEM_CIPHERS_FILE
e4b8d1
+    OPENSSL_free(new_rules);
e4b8d1
+#endif
e4b8d1
+    return NULL;
e4b8d1
+  
e4b8d1
 }
e4b8d1
 
e4b8d1
 char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
782d48
diff -up openssl-1.1.1c/ssl/ssl_lib.c.system-cipherlist openssl-1.1.1c/ssl/ssl_lib.c
782d48
--- openssl-1.1.1c/ssl/ssl_lib.c.system-cipherlist	2019-05-29 15:42:27.970328937 +0200
782d48
+++ openssl-1.1.1c/ssl/ssl_lib.c	2019-05-29 15:42:27.977328814 +0200
782d48
@@ -662,7 +662,7 @@ int SSL_CTX_set_ssl_version(SSL_CTX *ctx
e4b8d1
                                 ctx->tls13_ciphersuites,
e4b8d1
                                 &(ctx->cipher_list),
e4b8d1
                                 &(ctx->cipher_list_by_id),
e4b8d1
-                                SSL_DEFAULT_CIPHER_LIST, ctx->cert);
e4b8d1
+                                SSL_SYSTEM_DEFAULT_CIPHER_LIST, ctx->cert);
e4b8d1
     if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
e4b8d1
         SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
e4b8d1
         return 0;
782d48
@@ -2954,7 +2954,7 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *m
e4b8d1
     if (!ssl_create_cipher_list(ret->method,
e4b8d1
                                 ret->tls13_ciphersuites,
e4b8d1
                                 &ret->cipher_list, &ret->cipher_list_by_id,
e4b8d1
-                                SSL_DEFAULT_CIPHER_LIST, ret->cert)
e4b8d1
+                                SSL_SYSTEM_DEFAULT_CIPHER_LIST, ret->cert)
e4b8d1
         || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
e4b8d1
         SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
e4b8d1
         goto err2;
782d48
diff -up openssl-1.1.1c/test/cipherlist_test.c.system-cipherlist openssl-1.1.1c/test/cipherlist_test.c
782d48
--- openssl-1.1.1c/test/cipherlist_test.c.system-cipherlist	2019-05-28 15:12:21.000000000 +0200
782d48
+++ openssl-1.1.1c/test/cipherlist_test.c	2019-05-29 15:42:27.977328814 +0200
782d48
@@ -251,7 +251,9 @@ end:
e4b8d1
 
e4b8d1
 int setup_tests(void)
e4b8d1
 {
e4b8d1
+#ifndef SYSTEM_CIPHERS_FILE
e4b8d1
     ADD_TEST(test_default_cipherlist_implicit);
e4b8d1
+#endif
e4b8d1
     ADD_TEST(test_default_cipherlist_explicit);
782d48
     ADD_TEST(test_default_cipherlist_clear);
e4b8d1
     return 1;