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

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