Blame SOURCES/0007-Add-support-for-PROFILE-SYSTEM-system-default-cipher.patch

bf760f
From 736d709ec194b3a763e004696df22792c62a11fc Mon Sep 17 00:00:00 2001
bf760f
From: Tomas Mraz <tmraz@fedoraproject.org>
bf760f
Date: Thu, 24 Sep 2020 10:16:46 +0200
bf760f
Subject: Add support for PROFILE=SYSTEM system default cipherlist
bf760f
bf760f
(was openssl-1.1.1-system-cipherlist.patch)
bf760f
---
bf760f
 Configurations/unix-Makefile.tmpl |  5 ++
bf760f
 Configure                         | 10 +++-
bf760f
 doc/man1/openssl-ciphers.pod.in   |  9 ++++
bf760f
 include/openssl/ssl.h.in          |  5 ++
bf760f
 ssl/ssl_ciph.c                    | 88 +++++++++++++++++++++++++++----
bf760f
 ssl/ssl_lib.c                     |  4 +-
bf760f
 test/cipherlist_test.c            |  2 +
bf760f
 util/libcrypto.num                |  1 +
bf760f
 8 files changed, 110 insertions(+), 14 deletions(-)
bf760f
bf760f
diff --git a/Configurations/unix-Makefile.tmpl b/Configurations/unix-Makefile.tmpl
bf760f
index 9f369edf0e..c52389f831 100644
bf760f
--- a/Configurations/unix-Makefile.tmpl
bf760f
+++ b/Configurations/unix-Makefile.tmpl
bf760f
@@ -269,6 +269,10 @@ MANDIR=$(INSTALLTOP)/share/man
bf760f
 DOCDIR=$(INSTALLTOP)/share/doc/$(BASENAME)
bf760f
 HTMLDIR=$(DOCDIR)/html
bf760f
 
bf760f
+{- output_off() if $config{system_ciphers_file} eq ""; "" -}
bf760f
+SYSTEM_CIPHERS_FILE_DEFINE=-DSYSTEM_CIPHERS_FILE="\"{- $config{system_ciphers_file} -}\""
bf760f
+{- output_on() if $config{system_ciphers_file} eq ""; "" -}
bf760f
+
bf760f
 # MANSUFFIX is for the benefit of anyone who may want to have a suffix
bf760f
 # appended after the manpage file section number.  "ssl" is popular,
bf760f
 # resulting in files such as config.5ssl rather than config.5.
bf760f
@@ -292,6 +296,7 @@ CC=$(CROSS_COMPILE){- $config{CC} -}
bf760f
 CXX={- $config{CXX} ? "\$(CROSS_COMPILE)$config{CXX}" : '' -}
bf760f
 CPPFLAGS={- our $cppflags1 = join(" ",
bf760f
                                   (map { "-D".$_} @{$config{CPPDEFINES}}),
bf760f
+                                  "\$(SYSTEM_CIPHERS_FILE_DEFINE)",
bf760f
                                   (map { "-I".$_} @{$config{CPPINCLUDES}}),
bf760f
                                   @{$config{CPPFLAGS}}) -}
bf760f
 CFLAGS={- join(' ', @{$config{CFLAGS}}) -}
bf760f
diff --git a/doc/man1/openssl-ciphers.pod.in b/doc/man1/openssl-ciphers.pod.in
bf760f
index b4ed3e51d5..2122e6bdfd 100644
bf760f
--- a/doc/man1/openssl-ciphers.pod.in
bf760f
+++ b/doc/man1/openssl-ciphers.pod.in
bf760f
@@ -187,6 +187,15 @@ As of OpenSSL 1.0.0, the B<ALL> cipher suites are sensibly ordered by default.
bf760f
 
bf760f
 The cipher suites not enabled by B<ALL>, currently B<eNULL>.
bf760f
 
bf760f
+=item B<PROFILE=SYSTEM>
bf760f
+
bf760f
+The list of enabled cipher suites will be loaded from the system crypto policy
bf760f
+configuration file B</etc/crypto-policies/back-ends/openssl.config>.
bf760f
+See also L<update-crypto-policies(8)>.
bf760f
+This is the default behavior unless an application explicitly sets a cipher
bf760f
+list. If used in a cipher list configuration value this string must be at the
bf760f
+beginning of the cipher list, otherwise it will not be recognized.
bf760f
+
bf760f
 =item B<HIGH>
bf760f
 
bf760f
 "High" encryption cipher suites. This currently means those with key lengths
bf760f
diff --git a/include/openssl/ssl.h.in b/include/openssl/ssl.h.in
bf760f
index f9a61609e4..c6f95fed3f 100644
bf760f
--- a/include/openssl/ssl.h.in
bf760f
+++ b/include/openssl/ssl.h.in
bf760f
@@ -209,6 +209,11 @@ extern "C" {
bf760f
  * throwing out anonymous and unencrypted ciphersuites! (The latter are not
bf760f
  * actually enabled by ALL, but "ALL:RSA" would enable some of them.)
bf760f
  */
bf760f
+# ifdef SYSTEM_CIPHERS_FILE
bf760f
+#  define SSL_SYSTEM_DEFAULT_CIPHER_LIST "PROFILE=SYSTEM"
bf760f
+# else
bf760f
+#  define SSL_SYSTEM_DEFAULT_CIPHER_LIST OSSL_default_cipher_list()
bf760f
+# endif
bf760f
 
bf760f
 /* Used in SSL_set_shutdown()/SSL_get_shutdown(); */
bf760f
 # define SSL_SENT_SHUTDOWN       1
bf760f
diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c
bf760f
index b1d3f7919e..f7cc7fed48 100644
bf760f
--- a/ssl/ssl_ciph.c
bf760f
+++ b/ssl/ssl_ciph.c
bf760f
@@ -1411,6 +1411,53 @@ int SSL_set_ciphersuites(SSL *s, const char *str)
bf760f
     return ret;
bf760f
 }
bf760f
 
bf760f
+#ifdef SYSTEM_CIPHERS_FILE
bf760f
+static char *load_system_str(const char *suffix)
bf760f
+{
bf760f
+    FILE *fp;
bf760f
+    char buf[1024];
bf760f
+    char *new_rules;
bf760f
+    const char *ciphers_path;
bf760f
+    unsigned len, slen;
bf760f
+
bf760f
+    if ((ciphers_path = ossl_safe_getenv("OPENSSL_SYSTEM_CIPHERS_OVERRIDE")) == NULL)
bf760f
+        ciphers_path = SYSTEM_CIPHERS_FILE;
bf760f
+    fp = fopen(ciphers_path, "r");
bf760f
+    if (fp == NULL || fgets(buf, sizeof(buf), fp) == NULL) {
bf760f
+        /* cannot open or file is empty */
bf760f
+        snprintf(buf, sizeof(buf), "%s", SSL_DEFAULT_CIPHER_LIST);
bf760f
+    }
bf760f
+
bf760f
+    if (fp)
bf760f
+        fclose(fp);
bf760f
+
bf760f
+    slen = strlen(suffix);
bf760f
+    len = strlen(buf);
bf760f
+
bf760f
+    if (buf[len - 1] == '\n') {
bf760f
+        len--;
bf760f
+        buf[len] = 0;
bf760f
+    }
bf760f
+    if (buf[len - 1] == '\r') {
bf760f
+        len--;
bf760f
+        buf[len] = 0;
bf760f
+    }
bf760f
+
bf760f
+    new_rules = OPENSSL_malloc(len + slen + 1);
bf760f
+    if (new_rules == 0)
bf760f
+        return NULL;
bf760f
+
bf760f
+    memcpy(new_rules, buf, len);
bf760f
+    if (slen > 0) {
bf760f
+        memcpy(&new_rules[len], suffix, slen);
bf760f
+        len += slen;
bf760f
+    }
bf760f
+    new_rules[len] = 0;
bf760f
+
bf760f
+    return new_rules;
bf760f
+}
bf760f
+#endif
bf760f
+
bf760f
 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
                                              STACK_OF(SSL_CIPHER) *tls13_ciphersuites,
bf760f
                                              STACK_OF(SSL_CIPHER) **cipher_list,
bf760f
@@ -1425,15 +1472,25 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
     CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
bf760f
     const SSL_CIPHER **ca_list = NULL;
bf760f
     const SSL_METHOD *ssl_method = ctx->method;
bf760f
+#ifdef SYSTEM_CIPHERS_FILE
bf760f
+    char *new_rules = NULL;
bf760f
+
bf760f
+    if (rule_str != NULL && strncmp(rule_str, "PROFILE=SYSTEM", 14) == 0) {
bf760f
+        char *p = rule_str + 14;
bf760f
+
bf760f
+        new_rules = load_system_str(p);
bf760f
+        rule_str = new_rules;
bf760f
+    }
bf760f
+#endif
bf760f
 
bf760f
     /*
bf760f
      * Return with error if nothing to do.
bf760f
      */
bf760f
     if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
bf760f
-        return NULL;
bf760f
+        goto err;
bf760f
bf760f
     if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
bf760f
-        return NULL;
bf760f
+        goto err;
bf760f
 
bf760f
     /*
bf760f
      * To reduce the work to do we only want to process the compiled
bf760f
@@ -1456,7 +1513,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
     co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
bf760f
     if (co_list == NULL) {
bf760f
         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
bf760f
-        return NULL;          /* Failure */
bf760f
+        goto err;
bf760f
     }
bf760f
 
bf760f
     ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
bf760f
@@ -1522,8 +1579,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
      * in force within each class
bf760f
      */
bf760f
     if (!ssl_cipher_strength_sort(&head, &tail)) {
bf760f
-        OPENSSL_free(co_list);
bf760f
-        return NULL;
bf760f
+        goto err;
bf760f
     }
bf760f
 
bf760f
     /*
bf760f
@@ -1568,9 +1624,8 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
     num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
bf760f
     ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
bf760f
     if (ca_list == NULL) {
bf760f
-        OPENSSL_free(co_list);
bf760f
         ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
bf760f
-        return NULL;          /* Failure */
bf760f
+        goto err;
bf760f
     }
bf760f
     ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
bf760f
                                disabled_mkey, disabled_auth, disabled_enc,
bf760f
@@ -1596,8 +1651,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
     OPENSSL_free(ca_list);      /* Not needed anymore */
bf760f
 
bf760f
     if (!ok) {                  /* Rule processing failure */
bf760f
-        OPENSSL_free(co_list);
bf760f
-        return NULL;
bf760f
+        goto err;
bf760f
     }
bf760f
 
bf760f
     /*
bf760f
@@ -1605,10 +1659,13 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
      * if we cannot get one.
bf760f
      */
bf760f
     if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
bf760f
-        OPENSSL_free(co_list);
bf760f
-        return NULL;
bf760f
+        goto err;
bf760f
     }
bf760f
 
bf760f
+#ifdef SYSTEM_CIPHERS_FILE
bf760f
+    OPENSSL_free(new_rules);    /* Not needed anymore */
bf760f
+#endif
bf760f
+
bf760f
     /* Add TLSv1.3 ciphers first - we always prefer those if possible */
bf760f
     for (i = 0; i < sk_SSL_CIPHER_num(tls13_ciphersuites); i++) {
bf760f
         const SSL_CIPHER *sslc = sk_SSL_CIPHER_value(tls13_ciphersuites, i);
bf760f
@@ -1656,6 +1714,14 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(SSL_CTX *ctx,
bf760f
     *cipher_list = cipherstack;
bf760f
 
bf760f
     return cipherstack;
bf760f
+
bf760f
+err:
bf760f
+    OPENSSL_free(co_list);
bf760f
+#ifdef SYSTEM_CIPHERS_FILE
bf760f
+    OPENSSL_free(new_rules);
bf760f
+#endif
bf760f
+    return NULL;
bf760f
+  
bf760f
 }
bf760f
 
bf760f
 char *SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
bf760f
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
bf760f
index d14d5819ba..48d491219a 100644
bf760f
--- a/ssl/ssl_lib.c
bf760f
+++ b/ssl/ssl_lib.c
bf760f
@@ -660,7 +660,7 @@ int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
bf760f
                                 ctx->tls13_ciphersuites,
bf760f
                                 &(ctx->cipher_list),
bf760f
                                 &(ctx->cipher_list_by_id),
bf760f
-                                OSSL_default_cipher_list(), ctx->cert);
bf760f
+                                SSL_SYSTEM_DEFAULT_CIPHER_LIST, ctx->cert);
bf760f
     if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
bf760f
         ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
bf760f
         return 0;
bf760f
@@ -3193,7 +3193,7 @@ SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
bf760f
     if (!ssl_create_cipher_list(ret,
bf760f
                                 ret->tls13_ciphersuites,
bf760f
                                 &ret->cipher_list, &ret->cipher_list_by_id,
bf760f
-                                OSSL_default_cipher_list(), ret->cert)
bf760f
+                                SSL_SYSTEM_DEFAULT_CIPHER_LIST, ret->cert)
bf760f
         || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
bf760f
         ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
bf760f
         goto err2;
bf760f
diff --git a/test/cipherlist_test.c b/test/cipherlist_test.c
bf760f
index 380f0727fc..6922a87c30 100644
bf760f
--- a/test/cipherlist_test.c
bf760f
+++ b/test/cipherlist_test.c
bf760f
@@ -244,7 +244,9 @@ end:
bf760f
 
bf760f
 int setup_tests(void)
bf760f
 {
bf760f
+#ifndef SYSTEM_CIPHERS_FILE
bf760f
     ADD_TEST(test_default_cipherlist_implicit);
bf760f
+#endif
bf760f
     ADD_TEST(test_default_cipherlist_explicit);
bf760f
     ADD_TEST(test_default_cipherlist_clear);
bf760f
     return 1;
bf760f
diff --git a/util/libcrypto.num b/util/libcrypto.num
bf760f
index 404a706fab..e81fa9ec3e 100644
bf760f
--- a/util/libcrypto.num
bf760f
+++ b/util/libcrypto.num
bf760f
@@ -5282,3 +5282,4 @@ OSSL_DECODER_CTX_set_input_structure    ?	3_0_0	EXIST::FUNCTION:
bf760f
 ASN1_TIME_print_ex                      5553	3_0_0	EXIST::FUNCTION:
bf760f
 EVP_PKEY_get0_provider                  5554	3_0_0	EXIST::FUNCTION:
bf760f
 EVP_PKEY_CTX_get0_provider              5555	3_0_0	EXIST::FUNCTION:
bf760f
+ossl_safe_getenv                        ?	3_0_0	EXIST::FUNCTION:
bf760f
-- 
bf760f
2.26.2
bf760f
bf760f
diff -up openssl-3.0.0-beta1/Configure.sys-default openssl-3.0.0-beta1/Configure
bf760f
--- openssl-3.0.0-beta1/Configure.sys-default	2021-06-29 11:47:58.978144386 +0200
bf760f
+++ openssl-3.0.0-beta1/Configure	2021-06-29 11:52:01.631126260 +0200
bf760f
@@ -27,7 +27,7 @@ use OpenSSL::config;
bf760f
 my $orig_death_handler = $SIG{__DIE__};
bf760f
 $SIG{__DIE__} = \&death_handler;
bf760f
 
bf760f
-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";
bf760f
+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";
bf760f
 
bf760f
 my $banner = <<"EOF";
bf760f
 
bf760f
@@ -61,6 +61,10 @@ EOF
bf760f
 #               given with --prefix.
bf760f
 #               This becomes the value of OPENSSLDIR in Makefile and in C.
bf760f
 #               (Default: PREFIX/ssl)
bf760f
+#
bf760f
+# --system-ciphers-file  A file to read cipher string from when the PROFILE=SYSTEM
bf760f
+#		cipher is specified (default).
bf760f
+#
bf760f
 # --banner=".." Output specified text instead of default completion banner
bf760f
 #
bf760f
 # -w            Don't wait after showing a Configure warning
bf760f
@@ -385,6 +389,7 @@ $config{prefix}="";
bf760f
 $config{openssldir}="";
bf760f
 $config{processor}="";
bf760f
 $config{libdir}="";
bf760f
+$config{system_ciphers_file}="";
bf760f
 my $auto_threads=1;    # enable threads automatically? true by default
bf760f
 my $default_ranlib;
bf760f
 
bf760f
@@ -987,6 +992,10 @@ while (@argvcopy)
bf760f
                         die "FIPS key too long (64 bytes max)\n"
bf760f
                            if length $1 > 64;
bf760f
                         }
bf760f
+		elsif (/^--system-ciphers-file=(.*)$/)
bf760f
+			{
bf760f
+			$config{system_ciphers_file}=$1;
bf760f
+			}
bf760f
                 elsif (/^--banner=(.*)$/)
bf760f
                         {
bf760f
                         $banner = $1 . "\n";