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