Blame SOURCES/0021-Introduce-support-for-DHE-based-cipher-suites.patch

bd689f
From 4eac1dbb5f70a652d31847eec7c28d245f36cdbb Mon Sep 17 00:00:00 2001
bd689f
From: Martin Sehnoutka <msehnout@redhat.com>
bd689f
Date: Thu, 17 Nov 2016 10:48:28 +0100
bd689f
Subject: [PATCH 21/59] Introduce support for DHE based cipher suites.
bd689f
bd689f
---
bd689f
 parseconf.c   |  1 +
bd689f
 ssl.c         | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
bd689f
 tunables.c    |  5 +++-
bd689f
 tunables.h    |  1 +
bd689f
 vsftpd.conf.5 |  6 ++++
bd689f
 5 files changed, 104 insertions(+), 2 deletions(-)
bd689f
bd689f
diff --git a/parseconf.c b/parseconf.c
bd689f
index 3e0dba4..38e3182 100644
bd689f
--- a/parseconf.c
bd689f
+++ b/parseconf.c
bd689f
@@ -176,6 +176,7 @@ parseconf_str_array[] =
bd689f
   { "email_password_file", &tunable_email_password_file },
bd689f
   { "rsa_cert_file", &tunable_rsa_cert_file },
bd689f
   { "dsa_cert_file", &tunable_dsa_cert_file },
bd689f
+  { "dh_param_file", &tunable_dh_param_file },
bd689f
   { "ssl_ciphers", &tunable_ssl_ciphers },
bd689f
   { "rsa_private_key_file", &tunable_rsa_private_key_file },
bd689f
   { "dsa_private_key_file", &tunable_dsa_private_key_file },
bd689f
diff --git a/ssl.c b/ssl.c
bd689f
index c362983..22b69b3 100644
bd689f
--- a/ssl.c
bd689f
+++ b/ssl.c
bd689f
@@ -28,6 +28,8 @@
bd689f
 #include <openssl/err.h>
bd689f
 #include <openssl/rand.h>
bd689f
 #include <openssl/bio.h>
bd689f
+#include <openssl/dh.h>
bd689f
+#include <openssl/bn.h>
bd689f
 #include <errno.h>
bd689f
 #include <limits.h>
bd689f
 
bd689f
@@ -38,6 +40,7 @@ static void setup_bio_callbacks();
bd689f
 static long bio_callback(
bd689f
   BIO* p_bio, int oper, const char* p_arg, int argi, long argl, long retval);
bd689f
 static int ssl_verify_callback(int verify_ok, X509_STORE_CTX* p_ctx);
bd689f
+static DH *ssl_tmp_dh_callback(SSL *ssl, int is_export, int keylength);
bd689f
 static int ssl_cert_digest(
bd689f
   SSL* p_ssl, struct vsf_session* p_sess, struct mystr* p_str);
bd689f
 static void maybe_log_shutdown_state(struct vsf_session* p_sess);
bd689f
@@ -51,6 +54,60 @@ static int ssl_read_common(struct vsf_session* p_sess,
bd689f
 static int ssl_inited;
bd689f
 static struct mystr debug_str;
bd689f
 
bd689f
+
bd689f
+// Grab prime number from OpenSSL; <openssl/bn.h>
bd689f
+// (get_rfc*) for all available primes.
bd689f
+// wraps selection of comparable algorithm strength
bd689f
+#if !defined(match_dh_bits)
bd689f
+  #define match_dh_bits(keylen) \
bd689f
+    keylen >= 8191 ? 8192 : \
bd689f
+    keylen >= 6143 ? 6144 : \
bd689f
+    keylen >= 4095 ? 4096 : \
bd689f
+    keylen >= 3071 ? 3072 : \
bd689f
+    keylen >= 2047 ? 2048 : \
bd689f
+    keylen >= 1535 ? 1536 : \
bd689f
+    keylen >= 1023 ? 1024 : 768
bd689f
+#endif
bd689f
+
bd689f
+#if !defined(DH_get_prime)
bd689f
+  BIGNUM *
bd689f
+  DH_get_prime(int bits)
bd689f
+  {
bd689f
+    switch (bits) {
bd689f
+      case 768:  return get_rfc2409_prime_768(NULL);
bd689f
+      case 1024: return get_rfc2409_prime_1024(NULL);
bd689f
+      case 1536: return get_rfc3526_prime_1536(NULL);
bd689f
+      case 2048: return get_rfc3526_prime_2048(NULL);
bd689f
+      case 3072: return get_rfc3526_prime_3072(NULL);
bd689f
+      case 4096: return get_rfc3526_prime_4096(NULL);
bd689f
+      case 6144: return get_rfc3526_prime_6144(NULL);
bd689f
+      case 8192: return get_rfc3526_prime_8192(NULL);
bd689f
+      // shouldn't happen when used match_dh_bits; strict compiler
bd689f
+      default:   return NULL;
bd689f
+    }
bd689f
+}
bd689f
+#endif
bd689f
+
bd689f
+#if !defined(DH_get_dh)
bd689f
+  // Grab DH parameters
bd689f
+  DH *
bd689f
+  DH_get_dh(int size)
bd689f
+  {
bd689f
+    DH *dh = DH_new();
bd689f
+    if (!dh) {
bd689f
+      return NULL;
bd689f
+    }
bd689f
+    dh->p = DH_get_prime(match_dh_bits(size));
bd689f
+    BN_dec2bn(&dh->g, "2");
bd689f
+    if (!dh->p || !dh->g)
bd689f
+    {
bd689f
+      DH_free(dh);
bd689f
+      return NULL;
bd689f
+    }
bd689f
+    return dh;
bd689f
+  }
bd689f
+#endif
bd689f
+
bd689f
 void
bd689f
 ssl_init(struct vsf_session* p_sess)
bd689f
 {
bd689f
@@ -65,7 +122,7 @@ ssl_init(struct vsf_session* p_sess)
bd689f
     {
bd689f
       die("SSL: could not allocate SSL context");
bd689f
     }
bd689f
-    options = SSL_OP_ALL;
bd689f
+    options = SSL_OP_ALL | SSL_OP_SINGLE_DH_USE;
bd689f
     if (!tunable_sslv2)
bd689f
     {
bd689f
       options |= SSL_OP_NO_SSLv2;
bd689f
@@ -111,6 +168,25 @@ ssl_init(struct vsf_session* p_sess)
bd689f
         die("SSL: cannot load DSA private key");
bd689f
       }
bd689f
     }
bd689f
+    if (tunable_dh_param_file)
bd689f
+    {
bd689f
+      BIO *bio;
bd689f
+      DH *dhparams = NULL;
bd689f
+      if ((bio = BIO_new_file(tunable_dh_param_file, "r")) == NULL)
bd689f
+      {
bd689f
+        die("SSL: cannot load custom DH params");
bd689f
+      }
bd689f
+      else
bd689f
+      {
bd689f
+        dhparams = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
bd689f
+        BIO_free(bio);
bd689f
+
bd689f
+        if (!SSL_CTX_set_tmp_dh(p_ctx, dhparams))
bd689f
+	{
bd689f
+          die("SSL: setting custom DH params failed");
bd689f
+	}
bd689f
+      }
bd689f
+    }
bd689f
     if (tunable_ssl_ciphers &&
bd689f
         SSL_CTX_set_cipher_list(p_ctx, tunable_ssl_ciphers) != 1)
bd689f
     {
bd689f
@@ -165,6 +241,9 @@ ssl_init(struct vsf_session* p_sess)
bd689f
       /* Ensure cached session doesn't expire */
bd689f
       SSL_CTX_set_timeout(p_ctx, INT_MAX);
bd689f
     }
bd689f
+    
bd689f
+    SSL_CTX_set_tmp_dh_callback(p_ctx, ssl_tmp_dh_callback);
bd689f
+
bd689f
     p_sess->p_ssl_ctx = p_ctx;
bd689f
     ssl_inited = 1;
bd689f
   }
bd689f
@@ -702,6 +781,18 @@ ssl_verify_callback(int verify_ok, X509_STORE_CTX* p_ctx)
bd689f
   return 1;
bd689f
 }
bd689f
 
bd689f
+#define UNUSED(x) ( (void)(x) )
bd689f
+
bd689f
+static DH *
bd689f
+ssl_tmp_dh_callback(SSL *ssl, int is_export, int keylength)
bd689f
+{
bd689f
+  // strict compiler bypassing
bd689f
+  UNUSED(ssl);
bd689f
+  UNUSED(is_export);
bd689f
+  
bd689f
+  return DH_get_dh(keylength);
bd689f
+}
bd689f
+
bd689f
 void
bd689f
 ssl_add_entropy(struct vsf_session* p_sess)
bd689f
 {
bd689f
diff --git a/tunables.c b/tunables.c
bd689f
index c737465..1ea7227 100644
bd689f
--- a/tunables.c
bd689f
+++ b/tunables.c
bd689f
@@ -140,6 +140,7 @@ const char* tunable_user_sub_token;
bd689f
 const char* tunable_email_password_file;
bd689f
 const char* tunable_rsa_cert_file;
bd689f
 const char* tunable_dsa_cert_file;
bd689f
+const char* tunable_dh_param_file;
bd689f
 const char* tunable_ssl_ciphers;
bd689f
 const char* tunable_rsa_private_key_file;
bd689f
 const char* tunable_dsa_private_key_file;
bd689f
@@ -288,7 +289,9 @@ tunables_load_defaults()
bd689f
   install_str_setting("/usr/share/ssl/certs/vsftpd.pem",
bd689f
                       &tunable_rsa_cert_file);
bd689f
   install_str_setting(0, &tunable_dsa_cert_file);
bd689f
-  install_str_setting("ECDHE-RSA-AES256-GCM-SHA384", &tunable_ssl_ciphers);
bd689f
+  install_str_setting(0, &tunable_dh_param_file);
bd689f
+  install_str_setting("AES128-SHA:DES-CBC3-SHA:DHE-RSA-AES256-SHA",
bd689f
+                      &tunable_ssl_ciphers);
bd689f
   install_str_setting(0, &tunable_rsa_private_key_file);
bd689f
   install_str_setting(0, &tunable_dsa_private_key_file);
bd689f
   install_str_setting(0, &tunable_ca_certs_file);
bd689f
diff --git a/tunables.h b/tunables.h
bd689f
index 9553038..3995472 100644
bd689f
--- a/tunables.h
bd689f
+++ b/tunables.h
bd689f
@@ -142,6 +142,7 @@ extern const char* tunable_user_sub_token;
bd689f
 extern const char* tunable_email_password_file;
bd689f
 extern const char* tunable_rsa_cert_file;
bd689f
 extern const char* tunable_dsa_cert_file;
bd689f
+extern const char* tunable_dh_param_file;
bd689f
 extern const char* tunable_ssl_ciphers;
bd689f
 extern const char* tunable_rsa_private_key_file;
bd689f
 extern const char* tunable_dsa_private_key_file;
bd689f
diff --git a/vsftpd.conf.5 b/vsftpd.conf.5
bd689f
index fb6324e..ff94eca 100644
bd689f
--- a/vsftpd.conf.5
bd689f
+++ b/vsftpd.conf.5
bd689f
@@ -893,6 +893,12 @@ to be in the same file as the certificate.
bd689f
 
bd689f
 Default: (none)
bd689f
 .TP
bd689f
+.B dh_param_file
bd689f
+This option specifies the location of the custom parameters used for
bd689f
+ephemeral Diffie-Hellman key exchange in SSL.
bd689f
+
bd689f
+Default: (none - use built in parameters appropriate for certificate key size)
bd689f
+.TP
bd689f
 .B email_password_file
bd689f
 This option can be used to provide an alternate file for usage by the
bd689f
 .BR secure_email_list_enable
bd689f
-- 
bd689f
2.14.4
bd689f