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

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