Blame SOURCES/0024-tls-Parse-spice.cnf-OpenSSL-configuration-file.patch

fc8cd1
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
fc8cd1
From: Christophe Fergeau <cfergeau@redhat.com>
fc8cd1
Date: Mon, 18 Jun 2018 12:39:37 +0200
c56f30
Subject: [PATCH] tls: Parse spice.cnf OpenSSL configuration file
fc8cd1
fc8cd1
SPICE tries to use the OpenSSL system-wide defaults as much as possible
fc8cd1
for the TLS ciphers and protocols it uses. However, this is not enough
fc8cd1
for some customers who want it to use a more restrictive set of TLS
fc8cd1
features. spice-server should not try to override the system defaults
fc8cd1
OpenSSL uses, so this is not going to be hardcoded in spice-server code.
fc8cd1
fc8cd1
This is addressed with crypto policies in recent fedoras, and is being
fc8cd1
solved upstream through https://github.com/openssl/openssl/pull/4848
fc8cd1
This issue has become pressing enough that we need to solve it in el7
fc8cd1
which unfortunately does not have any of these system-wide settings.
fc8cd1
fc8cd1
As a stop-gap measure, this downstream-only patch adds a
fc8cd1
/etc/pki/tls/spice.cnf configuration file which can be used to configure
fc8cd1
the TLS ciphers/protocols used for SPICE. This is only meant to be a
fc8cd1
temporary solution, and will be superseded by crypto-policies when they
fc8cd1
land in RHEL.
fc8cd1
fc8cd1
Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
fc8cd1
---
c56f30
 docs/Makefile.am      |   1 +
c56f30
 docs/spice.cnf.sample |  15 +++++++
c56f30
 server/reds.c         | 102 ++++++++++++++++++++++++++++++++++++++++++
c56f30
 3 files changed, 118 insertions(+)
c56f30
 create mode 100644 docs/spice.cnf.sample
fc8cd1
fc8cd1
diff --git a/docs/Makefile.am b/docs/Makefile.am
fc8cd1
index 45667a6..909ed15 100644
fc8cd1
--- a/docs/Makefile.am
fc8cd1
+++ b/docs/Makefile.am
fc8cd1
@@ -6,6 +6,7 @@ EXTRA_DIST =					\
fc8cd1
 	spice_style.txt				\
fc8cd1
 	spice_threading_model.html		\
fc8cd1
 	spice_threading_model.txt		\
fc8cd1
+	spice.cnf.sample			\
fc8cd1
 	$(NULL)
fc8cd1
 
fc8cd1
 if BUILD_MANUAL
c56f30
diff --git a/docs/spice.cnf.sample b/docs/spice.cnf.sample
c56f30
new file mode 100644
c56f30
index 0000000..e5404ae
c56f30
--- /dev/null
c56f30
+++ b/docs/spice.cnf.sample
c56f30
@@ -0,0 +1,15 @@
c56f30
+# SPICE OpenSSL configuration file
c56f30
+#
c56f30
+# Sample configuration file for SPICE TLS communication
c56f30
+# Edit the file according to your needs, and put it in
c56f30
+# /etc/pki/tls/spice.cnf
c56f30
+#
c56f30
+# See "SUPPORTED CONFIGURATION FILE COMMANDS" in SSL_CONF_cmd(3)
c56f30
+# for the valid options, as well a ciphers(1) for the format
c56f30
+# of CipherString
c56f30
+
c56f30
+# Configure available ciphers
c56f30
+CipherString = DEFAULT:-RC4:-3DES:-DES
c56f30
+
c56f30
+# Only enable TLSv1.2 (and newer TLS versions the day OpenSSL supports them)
c56f30
+Protocol = ALL,-SSLv2,-SSLv3,-TLSv1,-TLSv1.1
fc8cd1
diff --git a/server/reds.c b/server/reds.c
fc8cd1
index 0af5643..846e44d 100644
fc8cd1
--- a/server/reds.c
fc8cd1
+++ b/server/reds.c
fc8cd1
@@ -33,6 +33,7 @@
fc8cd1
 #include <sys/mman.h>
fc8cd1
 #include <ctype.h>
fc8cd1
 
fc8cd1
+#include <openssl/conf.h>
fc8cd1
 #include <openssl/err.h>
fc8cd1
 
fc8cd1
 #if HAVE_SASL
fc8cd1
@@ -2827,6 +2828,102 @@ static gpointer openssl_global_init(gpointer arg)
fc8cd1
     return NULL;
fc8cd1
 }
fc8cd1
 
fc8cd1
+#define SPICE_OPENSSL_CNF_FILENAME "/etc/pki/tls/spice.cnf"
fc8cd1
+
fc8cd1
+static int reds_ssl_config_file_apply(RedsState *reds, STACK_OF(CONF_VALUE) *sect)
fc8cd1
+{
fc8cd1
+    int openssl_status;
fc8cd1
+    int return_value = 0;
fc8cd1
+    SSL_CONF_CTX *cctx = NULL;
fc8cd1
+    unsigned int i;
fc8cd1
+
fc8cd1
+    cctx = SSL_CONF_CTX_new();
fc8cd1
+    SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
fc8cd1
+    SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
fc8cd1
+    SSL_CONF_CTX_set_ssl_ctx(cctx, reds->ctx);
fc8cd1
+
fc8cd1
+    for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
fc8cd1
+        CONF_VALUE *option_value;
fc8cd1
+        option_value = sk_CONF_VALUE_value(sect, i);
fc8cd1
+        g_message("setting TLS option '%s' to '%s' from %s configuration file",
fc8cd1
+                   option_value->name, option_value->value,
fc8cd1
+                   SPICE_OPENSSL_CNF_FILENAME);
fc8cd1
+        openssl_status = SSL_CONF_cmd(cctx, option_value->name, option_value->value);
fc8cd1
+        switch(openssl_status) {
fc8cd1
+        case 1: /* fallthrough */
fc8cd1
+        case 2:
fc8cd1
+            /* The option was successfully processed */
fc8cd1
+            break;
fc8cd1
+        case 0:
fc8cd1
+            g_warning("failure to set option '%s'", option_value->name);
fc8cd1
+            return_value = -1;
fc8cd1
+            break;
fc8cd1
+        case -2:
fc8cd1
+            g_warning("unknown option '%s'", option_value->name);
fc8cd1
+            return_value = -1;
fc8cd1
+            break;
fc8cd1
+        case -3:
fc8cd1
+            g_warning("missing value for option '%s'", option_value->name);
fc8cd1
+            return_value = -1;
fc8cd1
+            break;
fc8cd1
+        default:
fc8cd1
+            g_warning("unknown SSL_CONF_cmd return value: %d", openssl_status);
fc8cd1
+            return_value = -1;
fc8cd1
+            break;
fc8cd1
+        }
fc8cd1
+    }
fc8cd1
+
fc8cd1
+    openssl_status = SSL_CONF_CTX_finish(cctx);
fc8cd1
+    if (!openssl_status) {
fc8cd1
+        g_warning("SSL_CONF_CTX_finish() failed");
fc8cd1
+        return_value = -1;
fc8cd1
+    }
fc8cd1
+
fc8cd1
+    SSL_CONF_CTX_free(cctx);
fc8cd1
+
fc8cd1
+    return return_value;
fc8cd1
+}
fc8cd1
+
fc8cd1
+static int reds_ssl_config_file_try_load(RedsState *reds)
fc8cd1
+{
fc8cd1
+    int status = -1;
fc8cd1
+    int openssl_status;
fc8cd1
+    CONF *ssl_conf = NULL;
fc8cd1
+    STACK_OF(CONF_VALUE) *default_section;
fc8cd1
+    long error_line = -1;
fc8cd1
+
fc8cd1
+    if (!g_file_test(SPICE_OPENSSL_CNF_FILENAME, G_FILE_TEST_IS_REGULAR)) {
fc8cd1
+        /* The configuration file is not mandatory, it's only meant to be used
fc8cd1
+         * when the sysadmin does not want to use the system-wide OpenSSL defaults
fc8cd1
+         */
fc8cd1
+        return 0;
fc8cd1
+    }
fc8cd1
+
fc8cd1
+    ssl_conf = NCONF_new(NULL);
fc8cd1
+    openssl_status = NCONF_load(ssl_conf, SPICE_OPENSSL_CNF_FILENAME, &error_line);
fc8cd1
+    if (openssl_status <= 0) {
fc8cd1
+        if (error_line <= 0) {
fc8cd1
+            g_warning("error loading config file %s", SPICE_OPENSSL_CNF_FILENAME);
fc8cd1
+        } else {
fc8cd1
+            g_warning("error parsing config file %s at %ld", SPICE_OPENSSL_CNF_FILENAME, error_line);
fc8cd1
+        }
fc8cd1
+        goto end;
fc8cd1
+    }
fc8cd1
+
fc8cd1
+    default_section = NCONF_get_section(ssl_conf, "default");
fc8cd1
+    if (default_section == NULL) {
fc8cd1
+        g_warning("could not find any content in %s config file (no toplevel section?)", SPICE_OPENSSL_CNF_FILENAME);
fc8cd1
+        goto end;
fc8cd1
+    }
fc8cd1
+
fc8cd1
+    status = reds_ssl_config_file_apply(reds, default_section);
fc8cd1
+
fc8cd1
+end:
fc8cd1
+    NCONF_free(ssl_conf);
fc8cd1
+
fc8cd1
+    return status;
fc8cd1
+}
fc8cd1
+
fc8cd1
 static int reds_init_ssl(RedsState *reds)
fc8cd1
 {
fc8cd1
     static GOnce openssl_once = G_ONCE_INIT;
fc8cd1
@@ -2911,6 +3008,11 @@ static int reds_init_ssl(RedsState *reds)
fc8cd1
     sk_zero(cmp_stack);
fc8cd1
 #endif
fc8cd1
 
fc8cd1
+    /* must be last to override whatever was configured previously */
fc8cd1
+    if (reds_ssl_config_file_try_load(reds) != 0) {
fc8cd1
+        return -1;
fc8cd1
+    }
fc8cd1
+
fc8cd1
     return 0;
fc8cd1
 }
fc8cd1