9364074
diff -up openssl-1.1.0i/Configurations/unix-Makefile.tmpl.system-cipherlist openssl-1.1.0i/Configurations/unix-Makefile.tmpl
9364074
--- openssl-1.1.0i/Configurations/unix-Makefile.tmpl.system-cipherlist	2018-09-20 14:59:34.633951133 +0200
9364074
+++ openssl-1.1.0i/Configurations/unix-Makefile.tmpl	2018-09-20 14:59:34.654951621 +0200
9364074
@@ -164,6 +164,10 @@ MANDIR=$(INSTALLTOP)/share/man
510bcc2
 DOCDIR=$(INSTALLTOP)/share/doc/$(BASENAME)
510bcc2
 HTMLDIR=$(DOCDIR)/html
510bcc2
 
510bcc2
+{- output_off() if $config{system_ciphers_file} eq ""; "" -}
510bcc2
+SYSTEM_CIPHERS_FILE_DEFINE=-DSYSTEM_CIPHERS_FILE="\"{- $config{system_ciphers_file} -}\""
510bcc2
+{- output_on() if $config{system_ciphers_file} eq ""; "" -}
510bcc2
+
510bcc2
 # MANSUFFIX is for the benefit of anyone who may want to have a suffix
510bcc2
 # appended after the manpage file section number.  "ssl" is popular,
510bcc2
 # resulting in files such as config.5ssl rather than config.5.
9364074
@@ -174,7 +178,7 @@ HTMLSUFFIX=html
510bcc2
 
510bcc2
 CROSS_COMPILE= {- $config{cross_compile_prefix} -}
510bcc2
 CC= $(CROSS_COMPILE){- $target{cc} -}
510bcc2
-CFLAGS={- our $cflags2 = join(" ",(map { "-D".$_} @{$target{defines}}, @{$config{defines}}),"-DOPENSSLDIR=\"\\\"\$(OPENSSLDIR)\\\"\"","-DENGINESDIR=\"\\\"\$(ENGINESDIR)\\\"\"") -} {- $target{cflags} -} {- $config{cflags} -}
510bcc2
+CFLAGS={- our $cflags2 = join(" ",(map { "-D".$_} @{$target{defines}}, @{$config{defines}}),"\$(SYSTEM_CIPHERS_FILE_DEFINE)","-DOPENSSLDIR=\"\\\"\$(OPENSSLDIR)\\\"\"","-DENGINESDIR=\"\\\"\$(ENGINESDIR)\\\"\"") -} {- $target{cflags} -} {- $config{cflags} -}
510bcc2
 CFLAGS_Q={- $cflags2 =~ s|([\\"])|\\$1|g; $cflags2 -} {- $config{cflags} -}
510bcc2
 LDFLAGS= {- $target{lflags} -}
510bcc2
 PLIB_LDFLAGS= {- $target{plib_lflags} -}
9364074
diff -up openssl-1.1.0i/Configure.system-cipherlist openssl-1.1.0i/Configure
9364074
--- openssl-1.1.0i/Configure.system-cipherlist	2018-08-14 14:45:06.000000000 +0200
9364074
+++ openssl-1.1.0i/Configure	2018-09-20 15:00:20.494017123 +0200
9364074
@@ -23,7 +23,7 @@ use OpenSSL::Glob;
9364074
 my $orig_death_handler = $SIG{__DIE__};
9364074
 $SIG{__DIE__} = \&death_handler;
510bcc2
 
510bcc2
-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-dso] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
510bcc2
+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-dso] [no-egd] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--system-ciphers-file=SYSTEMCIPHERFILE] [--with-xxx[=vvv]] [--config=FILE] os/compiler[:flags]\n";
510bcc2
 
510bcc2
 # Options:
510bcc2
 #
9364074
@@ -40,6 +40,9 @@ my $usage="Usage: Configure [no-<cipher>
510bcc2
 #               This becomes the value of OPENSSLDIR in Makefile and in C.
510bcc2
 #               (Default: PREFIX/ssl)
510bcc2
 #
510bcc2
+# --system-ciphers-file  A file to read cipher string from when the PROFILE=SYSTEM
510bcc2
+#		cipher is specified (default).
510bcc2
+#
510bcc2
 # --cross-compile-prefix Add specified prefix to binutils components.
510bcc2
 #
510bcc2
 # --api         One of 0.9.8, 1.0.0 or 1.1.0.  Do not compile support for
9364074
@@ -298,6 +301,7 @@ $config{openssldir}="";
510bcc2
 $config{processor}="";
510bcc2
 $config{libdir}="";
510bcc2
 $config{cross_compile_prefix}="";
510bcc2
+$config{system_ciphers_file}="";
510bcc2
 $config{fipslibdir}="/usr/local/ssl/fips-2.0/lib/";
510bcc2
 my $nofipscanistercheck=0;
510bcc2
 $config{baseaddr}="0xFB00000";
9364074
@@ -704,6 +708,10 @@ while (@argvcopy)
510bcc2
 			{
510bcc2
 			$config{baseaddr}="$1";
510bcc2
 			}
510bcc2
+		elsif (/^--system-ciphers-file=(.*)$/)
510bcc2
+			{
510bcc2
+			$config{system_ciphers_file}=$1;
510bcc2
+			}
510bcc2
 		elsif (/^--cross-compile-prefix=(.*)$/)
510bcc2
 			{
510bcc2
 			$config{cross_compile_prefix}=$1;
9364074
@@ -837,6 +845,8 @@ if ($target =~ m/^CygWin32(-.*)$/) {
510bcc2
     $target = "Cygwin".$1;
510bcc2
 }
510bcc2
 
510bcc2
+chop $config{system_ciphers_file} if $config{system_ciphers_file} =~ /\/$/;
510bcc2
+
510bcc2
 foreach (sort (keys %disabled))
510bcc2
 	{
510bcc2
 	$config{options} .= " no-$_";
9364074
diff -up openssl-1.1.0i/doc/apps/ciphers.pod.system-cipherlist openssl-1.1.0i/doc/apps/ciphers.pod
9364074
--- openssl-1.1.0i/doc/apps/ciphers.pod.system-cipherlist	2018-08-14 14:45:08.000000000 +0200
9364074
+++ openssl-1.1.0i/doc/apps/ciphers.pod	2018-09-20 14:59:34.655951644 +0200
9364074
@@ -176,6 +176,15 @@ As of OpenSSL 1.0.0, the B<ALL> cipher s
c676ac3
 
c676ac3
 The cipher suites not enabled by B<ALL>, currently B<eNULL>.
c676ac3
 
c676ac3
+=item B<PROFILE=SYSTEM>
c676ac3
+
c676ac3
+The list of enabled cipher suites will be loaded from the system crypto policy
c676ac3
+configuration file B</etc/crypto-policies/back-ends/openssl.config>.
c676ac3
+See also L<update-crypto-policies(8)>.
c676ac3
+This is the default behavior unless an application explicitly sets a cipher
c676ac3
+list. If used in a cipher list configuration value this string must be at the
c676ac3
+beginning of the cipher list, otherwise it will not be recognized.
c676ac3
+
c676ac3
 =item B<HIGH>
c676ac3
 
c676ac3
 "high" encryption cipher suites. This currently means those with key lengths
9364074
diff -up openssl-1.1.0i/include/openssl/ssl.h.system-cipherlist openssl-1.1.0i/include/openssl/ssl.h
9364074
--- openssl-1.1.0i/include/openssl/ssl.h.system-cipherlist	2018-08-14 14:45:09.000000000 +0200
9364074
+++ openssl-1.1.0i/include/openssl/ssl.h	2018-09-20 14:59:34.655951644 +0200
510bcc2
@@ -201,6 +201,11 @@ extern "C" {
510bcc2
  * throwing out anonymous and unencrypted ciphersuites! (The latter are not
510bcc2
  * actually enabled by ALL, but "ALL:RSA" would enable some of them.)
510bcc2
  */
510bcc2
+# ifdef SYSTEM_CIPHERS_FILE
510bcc2
+#  define SSL_SYSTEM_DEFAULT_CIPHER_LIST "PROFILE=SYSTEM"
510bcc2
+# else
510bcc2
+#  define SSL_SYSTEM_DEFAULT_CIPHER_LIST SSL_DEFAULT_CIPHER_LIST
510bcc2
+# endif
510bcc2
 
510bcc2
 /* Used in SSL_set_shutdown()/SSL_get_shutdown(); */
510bcc2
 # define SSL_SENT_SHUTDOWN       1
9364074
diff -up openssl-1.1.0i/ssl/ssl_ciph.c.system-cipherlist openssl-1.1.0i/ssl/ssl_ciph.c
9364074
--- openssl-1.1.0i/ssl/ssl_ciph.c.system-cipherlist	2018-08-14 14:45:09.000000000 +0200
9364074
+++ openssl-1.1.0i/ssl/ssl_ciph.c	2018-09-20 14:59:34.656951667 +0200
9364074
@@ -1287,6 +1287,50 @@ static int check_suiteb_cipher_list(cons
510bcc2
 }
510bcc2
 #endif
510bcc2
 
510bcc2
+#ifdef SYSTEM_CIPHERS_FILE
510bcc2
+static char *load_system_str(const char *suffix)
510bcc2
+{
510bcc2
+    FILE *fp;
510bcc2
+    char buf[1024];
510bcc2
+    char *new_rules;
510bcc2
+    unsigned len, slen;
510bcc2
+
510bcc2
+    fp = fopen(SYSTEM_CIPHERS_FILE, "r");
510bcc2
+    if (fp == NULL || fgets(buf, sizeof(buf), fp) == NULL) {
510bcc2
+        /* cannot open or file is empty */
510bcc2
+        snprintf(buf, sizeof(buf), "%s", SSL_DEFAULT_CIPHER_LIST);
510bcc2
+    }
510bcc2
+
510bcc2
+    if (fp)
510bcc2
+        fclose(fp);
510bcc2
+
510bcc2
+    slen = strlen(suffix);
510bcc2
+    len = strlen(buf);
510bcc2
+
510bcc2
+    if (buf[len - 1] == '\n') {
510bcc2
+        len--;
510bcc2
+        buf[len] = 0;
510bcc2
+    }
510bcc2
+    if (buf[len - 1] == '\r') {
510bcc2
+        len--;
510bcc2
+        buf[len] = 0;
510bcc2
+    }
510bcc2
+
510bcc2
+    new_rules = OPENSSL_malloc(len + slen + 1);
510bcc2
+    if (new_rules == 0)
510bcc2
+        return NULL;
510bcc2
+
510bcc2
+    memcpy(new_rules, buf, len);
510bcc2
+    if (slen > 0) {
510bcc2
+        memcpy(&new_rules[len], suffix, slen);
510bcc2
+        len += slen;
510bcc2
+    }
510bcc2
+    new_rules[len] = 0;
510bcc2
+
510bcc2
+    return new_rules;
510bcc2
+}
510bcc2
+#endif
510bcc2
+
510bcc2
 STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method, STACK_OF(SSL_CIPHER)
510bcc2
                                              **cipher_list, STACK_OF(SSL_CIPHER)
510bcc2
                                              **cipher_list_by_id,
9364074
@@ -1294,19 +1338,29 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
 {
510bcc2
     int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
510bcc2
     uint32_t disabled_mkey, disabled_auth, disabled_enc, disabled_mac;
510bcc2
-    STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
510bcc2
+    STACK_OF(SSL_CIPHER) *cipherstack = NULL, *tmp_cipher_list;
510bcc2
     const char *rule_p;
510bcc2
     CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
510bcc2
     const SSL_CIPHER **ca_list = NULL;
510bcc2
+#ifdef SYSTEM_CIPHERS_FILE
510bcc2
+    char *new_rules = NULL;
510bcc2
+
510bcc2
+    if (rule_str != NULL && strncmp(rule_str, "PROFILE=SYSTEM", 14) == 0) {
510bcc2
+        char *p = rule_str + 14;
510bcc2
+    
510bcc2
+        new_rules = load_system_str(p);
510bcc2
+        rule_str = new_rules;
510bcc2
+    }
510bcc2
+#endif
510bcc2
 
510bcc2
     /*
510bcc2
      * Return with error if nothing to do.
510bcc2
      */
510bcc2
     if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
510bcc2
-        return NULL;
510bcc2
+        goto end;
510bcc2
 #ifndef OPENSSL_NO_EC
510bcc2
     if (!check_suiteb_cipher_list(ssl_method, c, &rule_str))
510bcc2
-        return NULL;
510bcc2
+        goto end;
510bcc2
 #endif
510bcc2
 
510bcc2
     /*
9364074
@@ -1329,7 +1383,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
     co_list = OPENSSL_malloc(sizeof(*co_list) * num_of_ciphers);
510bcc2
     if (co_list == NULL) {
510bcc2
         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
510bcc2
-        return (NULL);          /* Failure */
510bcc2
+        goto end;
510bcc2
     }
510bcc2
 
510bcc2
     ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
9364074
@@ -1395,8 +1449,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
      * in force within each class
510bcc2
      */
510bcc2
     if (!ssl_cipher_strength_sort(&head, &tail)) {
510bcc2
-        OPENSSL_free(co_list);
510bcc2
-        return NULL;
510bcc2
+        goto end;
510bcc2
     }
510bcc2
 
510bcc2
     /*
9364074
@@ -1441,9 +1494,8 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
     num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
510bcc2
     ca_list = OPENSSL_malloc(sizeof(*ca_list) * num_of_alias_max);
510bcc2
     if (ca_list == NULL) {
510bcc2
-        OPENSSL_free(co_list);
510bcc2
         SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
510bcc2
-        return (NULL);          /* Failure */
510bcc2
+        goto end;
510bcc2
     }
510bcc2
     ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
510bcc2
                                disabled_mkey, disabled_auth, disabled_enc,
9364074
@@ -1469,8 +1521,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
     OPENSSL_free(ca_list);      /* Not needed anymore */
510bcc2
 
510bcc2
     if (!ok) {                  /* Rule processing failure */
510bcc2
-        OPENSSL_free(co_list);
510bcc2
-        return (NULL);
510bcc2
+        goto end;
510bcc2
     }
510bcc2
 
510bcc2
     /*
9364074
@@ -1478,8 +1529,7 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
      * if we cannot get one.
510bcc2
      */
510bcc2
     if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
510bcc2
-        OPENSSL_free(co_list);
510bcc2
-        return (NULL);
510bcc2
+        goto end;
510bcc2
     }
510bcc2
 
510bcc2
     /*
9364074
@@ -1490,21 +1540,21 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
         if (curr->active
510bcc2
             && (!FIPS_mode() || curr->cipher->algo_strength & SSL_FIPS)) {
510bcc2
             if (!sk_SSL_CIPHER_push(cipherstack, curr->cipher)) {
510bcc2
-                OPENSSL_free(co_list);
510bcc2
                 sk_SSL_CIPHER_free(cipherstack);
510bcc2
-                return NULL;
510bcc2
+                cipherstack = NULL;
510bcc2
+                goto end;
510bcc2
             }
510bcc2
 #ifdef CIPHER_DEBUG
510bcc2
             fprintf(stderr, "<%s>\n", curr->cipher->name);
510bcc2
 #endif
510bcc2
         }
510bcc2
     }
510bcc2
-    OPENSSL_free(co_list);      /* Not needed any longer */
510bcc2
 
510bcc2
     tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
510bcc2
     if (tmp_cipher_list == NULL) {
510bcc2
         sk_SSL_CIPHER_free(cipherstack);
510bcc2
-        return NULL;
510bcc2
+        cipherstack = NULL;
510bcc2
+        goto end;
510bcc2
     }
510bcc2
     sk_SSL_CIPHER_free(*cipher_list);
510bcc2
     *cipher_list = cipherstack;
9364074
@@ -1514,6 +1564,12 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_
510bcc2
     (void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id, ssl_cipher_ptr_id_cmp);
510bcc2
 
510bcc2
     sk_SSL_CIPHER_sort(*cipher_list_by_id);
510bcc2
+
510bcc2
+ end:
510bcc2
+    OPENSSL_free(co_list);
510bcc2
+#ifdef SYSTEM_CIPHERS_FILE
510bcc2
+    OPENSSL_free(new_rules);
510bcc2
+#endif
510bcc2
     return (cipherstack);
510bcc2
 }
510bcc2
 
9364074
diff -up openssl-1.1.0i/ssl/ssl_lib.c.system-cipherlist openssl-1.1.0i/ssl/ssl_lib.c
9364074
--- openssl-1.1.0i/ssl/ssl_lib.c.system-cipherlist	2018-09-20 14:59:34.653951597 +0200
9364074
+++ openssl-1.1.0i/ssl/ssl_lib.c	2018-09-20 14:59:34.656951667 +0200
9364074
@@ -662,7 +662,7 @@ int SSL_CTX_set_ssl_version(SSL_CTX *ctx
510bcc2
 
510bcc2
     sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list),
510bcc2
                                 &(ctx->cipher_list_by_id),
510bcc2
-                                SSL_DEFAULT_CIPHER_LIST, ctx->cert);
510bcc2
+                                SSL_SYSTEM_DEFAULT_CIPHER_LIST, ctx->cert);
510bcc2
     if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
510bcc2
         SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
510bcc2
         return (0);
9364074
@@ -2587,7 +2587,7 @@ SSL_CTX *SSL_CTX_new(const SSL_METHOD *m
510bcc2
 #endif
510bcc2
     if (!ssl_create_cipher_list(ret->method,
510bcc2
                                 &ret->cipher_list, &ret->cipher_list_by_id,
510bcc2
-                                SSL_DEFAULT_CIPHER_LIST, ret->cert)
510bcc2
+                                SSL_SYSTEM_DEFAULT_CIPHER_LIST, ret->cert)
510bcc2
         || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
510bcc2
         SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
510bcc2
         goto err2;
9364074
diff -up openssl-1.1.0i/test/cipherlist_test.c.system-cipherlist openssl-1.1.0i/test/cipherlist_test.c
9364074
--- openssl-1.1.0i/test/cipherlist_test.c.system-cipherlist	2018-08-14 14:45:10.000000000 +0200
9364074
+++ openssl-1.1.0i/test/cipherlist_test.c	2018-09-20 14:59:34.656951667 +0200
510bcc2
@@ -190,7 +190,9 @@ int main(int argc, char **argv)
510bcc2
 {
510bcc2
     int result = 0;
510bcc2
 
510bcc2
+#ifndef SYSTEM_CIPHERS_FILE
510bcc2
     ADD_TEST(test_default_cipherlist_implicit);
510bcc2
+#endif
510bcc2
     ADD_TEST(test_default_cipherlist_explicit);
510bcc2
 
510bcc2
     result = run_tests(argv[0]);