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

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