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

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