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