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

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