Blame SOURCES/httpd-2.4.6-ssl-large-keys.patch

008793
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
008793
index 46769e9..0275452 100644
008793
--- a/modules/ssl/ssl_engine_init.c
008793
+++ b/modules/ssl/ssl_engine_init.c
008793
@@ -41,6 +41,79 @@
008793
 #define KEYTYPES "RSA or DSA"
008793
 #endif
008793
 
008793
+/*
008793
+ * Grab well-defined DH parameters from OpenSSL, see the get_rfc*
008793
+ * functions in <openssl/bn.h> for all available primes.
008793
+ */
008793
+static DH *make_dh_params(BIGNUM *(*prime)(BIGNUM *), const char *gen)
008793
+{
008793
+    DH *dh = DH_new();
008793
+
008793
+    if (!dh) {
008793
+        return NULL;
008793
+    }
008793
+    dh->p = prime(NULL);
008793
+    BN_dec2bn(&dh->g, gen);
008793
+    if (!dh->p || !dh->g) {
008793
+        DH_free(dh);
008793
+        return NULL;
008793
+    }
008793
+    return dh;
008793
+}
008793
+
008793
+/* Storage and initialization for DH parameters. */
008793
+static struct dhparam {
008793
+    BIGNUM *(*const prime)(BIGNUM *); /* function to generate... */
008793
+    DH *dh;                           /* ...this, used for keys.... */
008793
+    const unsigned int min;           /* ...of length >= this. */
008793
+} dhparams[] = {
008793
+    { get_rfc3526_prime_8192, NULL, 6145 },
008793
+    { get_rfc3526_prime_6144, NULL, 4097 },
008793
+    { get_rfc3526_prime_4096, NULL, 3073 },
008793
+    { get_rfc3526_prime_3072, NULL, 2049 },
008793
+    { get_rfc3526_prime_2048, NULL, 1025 },
008793
+    { get_rfc2409_prime_1024, NULL, 0 }
008793
+};
008793
+
008793
+static void init_dh_params(void)
008793
+{
008793
+    unsigned n;
008793
+
008793
+    for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++)
008793
+        dhparams[n].dh = make_dh_params(dhparams[n].prime, "2");
008793
+}
008793
+
008793
+static void free_dh_params(void)
008793
+{
008793
+    unsigned n;
008793
+
008793
+    /* DH_free() is a noop for a NULL parameter, so these are harmless
008793
+     * in the (unexpected) case where these variables are already
008793
+     * NULL. */
008793
+    for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++) {
008793
+        DH_free(dhparams[n].dh);
008793
+        dhparams[n].dh = NULL;
008793
+    }
008793
+}
008793
+
008793
+/* Hand out the same DH structure though once generated as we leak
008793
+ * memory otherwise and freeing the structure up after use would be
008793
+ * hard to track and in fact is not needed at all as it is safe to
008793
+ * use the same parameters over and over again security wise (in
008793
+ * contrast to the keys itself) and code safe as the returned structure
008793
+ * is duplicated by OpenSSL anyway. Hence no modification happens
008793
+ * to our copy. */
008793
+DH *modssl_get_dh_params(unsigned keylen)
008793
+{
008793
+    unsigned n;
008793
+
008793
+    for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++)
008793
+        if (keylen >= dhparams[n].min)
008793
+            return dhparams[n].dh;
008793
+        
008793
+    return NULL; /* impossible to reach. */
008793
+}
008793
+
008793
 static void ssl_add_version_components(apr_pool_t *p,
008793
                                        server_rec *s)
008793
 {
008793
@@ -244,6 +317,8 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
008793
 
008793
     SSL_init_app_data2_idx(); /* for SSL_get_app_data2() at request time */
008793
 
008793
+    init_dh_params();
008793
+
008793
     return OK;
008793
 }
008793
 
008793
@@ -1623,6 +1698,8 @@ apr_status_t ssl_init_ModuleKill(void *data)
008793
         ssl_init_ctx_cleanup_server(sc->server);
008793
     }
008793
 
008793
+    free_dh_params();
008793
+
008793
     return APR_SUCCESS;
008793
 }
008793
 
008793
diff --git a/modules/ssl/ssl_engine_kernel.c b/modules/ssl/ssl_engine_kernel.c
008793
index 2d6d59e..1ecbccd 100644
008793
--- a/modules/ssl/ssl_engine_kernel.c
008793
+++ b/modules/ssl/ssl_engine_kernel.c
008793
@@ -1287,34 +1287,6 @@ const authz_provider ssl_authz_provider_verify_client =
008793
 */
008793
 
008793
 /*
008793
- * Grab well-defined DH parameters from OpenSSL, see <openssl/bn.h>
008793
- * (get_rfc*) for all available primes.
008793
- */
008793
-#define make_get_dh(rfc,size,gen) \
008793
-static DH *get_dh##size(void) \
008793
-{ \
008793
-    DH *dh; \
008793
-    if (!(dh = DH_new())) { \
008793
-        return NULL; \
008793
-    } \
008793
-    dh->p = get_##rfc##_prime_##size(NULL); \
008793
-    BN_dec2bn(&dh->g, #gen); \
008793
-    if (!dh->p || !dh->g) { \
008793
-        DH_free(dh); \
008793
-        return NULL; \
008793
-    } \
008793
-    return dh; \
008793
-}
008793
-
008793
-/*
008793
- * Prepare DH parameters from 1024 to 4096 bits, in 1024-bit increments
008793
- */
008793
-make_get_dh(rfc2409, 1024, 2)
008793
-make_get_dh(rfc3526, 2048, 2)
008793
-make_get_dh(rfc3526, 3072, 2)
008793
-make_get_dh(rfc3526, 4096, 2)
008793
-
008793
-/*
008793
  * Hand out standard DH parameters, based on the authentication strength
008793
  */
008793
 DH *ssl_callback_TmpDH(SSL *ssl, int export, int keylen)
008793
@@ -1342,14 +1314,7 @@ DH *ssl_callback_TmpDH(SSL *ssl, int export, int keylen)
008793
     ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, c,
008793
                   "handing out built-in DH parameters for %d-bit authenticated connection", keylen);
008793
 
008793
-    if (keylen >= 4096)
008793
-        return get_dh4096();
008793
-    else if (keylen >= 3072)
008793
-        return get_dh3072();
008793
-    else if (keylen >= 2048)
008793
-        return get_dh2048();
008793
-    else
008793
-        return get_dh1024();
008793
+    return modssl_get_dh_params(keylen);
008793
 }
008793
 
008793
 /*
008793
diff --git a/modules/ssl/ssl_private.h b/modules/ssl/ssl_private.h
008793
index 744af9e..f47ed47 100644
008793
--- a/modules/ssl/ssl_private.h
008793
+++ b/modules/ssl/ssl_private.h
008793
@@ -990,6 +990,11 @@ OCSP_RESPONSE *modssl_dispatch_ocsp_request(const apr_uri_t *uri,
008793
                                             conn_rec *c, apr_pool_t *p);
008793
 #endif
008793
 
008793
+/* Retrieve DH parameters for given key length.  Return value should
008793
+ * be treated as unmutable, since it is stored in process-global
008793
+ * memory. */
008793
+DH *modssl_get_dh_params(unsigned keylen);
008793
+
008793
 #endif /* SSL_PRIVATE_H */
008793
 /** @} */
008793