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