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

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