]> granicus.if.org Git - apache/blobdiff - modules/ssl/ssl_engine_init.c
Replace AcceptMutex, LockFile, RewriteLock, SSLMutex, SSLStaplingMutex,
[apache] / modules / ssl / ssl_engine_init.c
index 38cfa1df955cf2acb15b0cce340612bbeddcdac9..00d07967e51d26926cb2061889711fd5526e2301 100644 (file)
@@ -1,65 +1,32 @@
-/*                      _             _
-**  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
-** | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
-** | | | | | | (_) | (_| |   \__ \__ \ |  www.modssl.org
-** |_| |_| |_|\___/ \__,_|___|___/___/_|  ftp.modssl.org
-**                      |_____|
-**  ssl_engine_init.c
-**  Initialization of Servers
-*/
-
-/* ====================================================================
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowledgment may appear in the software itself,
- *    if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- *    nor may "Apache" appear in their name, without prior written
- *    permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*                      _             _
+ *  _ __ ___   ___   __| |    ___ ___| |  mod_ssl
+ * | '_ ` _ \ / _ \ / _` |   / __/ __| |  Apache Interface to OpenSSL
+ * | | | | | | (_) | (_| |   \__ \__ \ |
+ * |_| |_| |_|\___/ \__,_|___|___/___/_|
+ *                      |_____|
+ *  ssl_engine_init.c
+ *  Initialization of Servers
  */
                              /* ``Recursive, adj.;
                                   see Recursive.''
                                         -- Unknown   */
-#include "mod_ssl.h"
+#include "ssl_private.h"
 
 /*  _________________________________________________________________
 **
 **  _________________________________________________________________
 */
 
-static char *ssl_add_version_component(apr_pool_t *p,
-                                       server_rec *s,
-                                       char *name)
-{
-    char *val = ssl_var_lookup(p, s, NULL, NULL, name);
-
-    if (val && *val) {
-        ap_add_version_component(p, val);
-    }
-
-    return val;
-}
-
-static char *version_components[] = {
-    "SSL_VERSION_PRODUCT",
-    "SSL_VERSION_INTERFACE",
-    "SSL_VERSION_LIBRARY",
-    NULL
-};
 
 static void ssl_add_version_components(apr_pool_t *p,
                                        server_rec *s)
 {
-    char *vals[sizeof(version_components)/sizeof(char *)];
-    int i;
+    char *modver = ssl_var_lookup(p, s, NULL, NULL, "SSL_VERSION_INTERFACE");
+    char *libver = ssl_var_lookup(p, s, NULL, NULL, "SSL_VERSION_LIBRARY");
+    char *incver = ssl_var_lookup(p, s, NULL, NULL, 
+                                  "SSL_VERSION_LIBRARY_INTERFACE");
 
-    for (i=0; version_components[i]; i++) {
-        vals[i] = ssl_add_version_component(p, s,
-                                            version_components[i]);
-    }
+    ap_add_version_component(p, modver);
+    ap_add_version_component(p, libver);
 
-    ssl_log(s, SSL_LOG_INFO,
-            "Server: %s, Interface: %s, Library: %s",
-            AP_SERVER_BASEVERSION,
-            vals[1],  /* SSL_VERSION_INTERFACE */
-            vals[2]); /* SSL_VERSION_LIBRARY */
+    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                 "%s compiled against Server: %s, Library: %s",
+                 modver, AP_SERVER_BASEVERSION, incver);
 }
 
 
-/*
- *  Initialize SSL library
- */
-static void ssl_init_SSLLibrary(server_rec *s)
-{
-    ssl_log(s, SSL_LOG_INFO,
-            "Init: Initializing %s library", SSL_LIBRARY_NAME);
-
-    CRYPTO_malloc_init();
-    SSL_load_error_strings();
-    SSL_library_init();
-}
-
 /*
  * Handle the Temporary RSA Keys and DH Params
  */
@@ -141,35 +74,38 @@ static void ssl_tmp_keys_free(server_rec *s)
     MODSSL_TMP_KEYS_FREE(mc, DH);
 }
 
-static void ssl_tmp_key_init_rsa(server_rec *s,
-                                 int bits, int idx)
+static int ssl_tmp_key_init_rsa(server_rec *s,
+                                int bits, int idx)
 {
     SSLModConfigRec *mc = myModConfig(s);
 
     if (!(mc->pTmpKeys[idx] =
           RSA_generate_key(bits, RSA_F4, NULL, NULL)))
     {
-        ssl_log(s, SSL_LOG_ERROR,
-                "Init: Failed to generate temporary "
-                "%d bit RSA private key", bits);
-        ssl_die();
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                     "Init: Failed to generate temporary "
+                     "%d bit RSA private key", bits);
+        return !OK;
     }
 
+    return OK;
 }
 
-static void ssl_tmp_key_init_dh(server_rec *s,
-                                int bits, int idx)
+static int ssl_tmp_key_init_dh(server_rec *s,
+                               int bits, int idx)
 {
     SSLModConfigRec *mc = myModConfig(s);
 
     if (!(mc->pTmpKeys[idx] =
           ssl_dh_GetTmpParam(bits)))
     {
-        ssl_log(s, SSL_LOG_ERROR,
-                "Init: Failed to generate temporary "
-                "%d bit DH parameters", bits);
-        ssl_die();
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                     "Init: Failed to generate temporary "
+                     "%d bit DH parameters", bits);
+        return !OK;
     }
+
+    return OK;
 }
 
 #define MODSSL_TMP_KEY_INIT_RSA(s, bits) \
@@ -178,19 +114,25 @@ static void ssl_tmp_key_init_dh(server_rec *s,
 #define MODSSL_TMP_KEY_INIT_DH(s, bits) \
     ssl_tmp_key_init_dh(s, bits, SSL_TMP_KEY_DH_##bits)
 
-static void ssl_tmp_keys_init(server_rec *s)
+static int ssl_tmp_keys_init(server_rec *s)
 {
-    ssl_log(s, SSL_LOG_INFO,
-            "Init: Generating temporary RSA private keys (512/1024 bits)");
+    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                 "Init: Generating temporary RSA private keys (512/1024 bits)");
+
+    if (MODSSL_TMP_KEY_INIT_RSA(s, 512) ||
+        MODSSL_TMP_KEY_INIT_RSA(s, 1024)) {
+        return !OK;
+    }
 
-    MODSSL_TMP_KEY_INIT_RSA(s, 512);
-    MODSSL_TMP_KEY_INIT_RSA(s, 1024);
+    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                 "Init: Generating temporary DH parameters (512/1024 bits)");
 
-    ssl_log(s, SSL_LOG_INFO,
-            "Init: Generating temporary DH parameters (512/1024 bits)");
+    if (MODSSL_TMP_KEY_INIT_DH(s, 512) ||
+        MODSSL_TMP_KEY_INIT_DH(s, 1024)) {
+        return !OK;
+    }
 
-    MODSSL_TMP_KEY_INIT_DH(s, 512);
-    MODSSL_TMP_KEY_INIT_DH(s, 1024);
+    return OK;
 }
 
 /*
@@ -204,6 +146,12 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
     SSLSrvConfigRec *sc;
     server_rec *s;
 
+    /* We initialize mc->pid per-process in the child init,
+     * but it should be initialized for startup before we
+     * call ssl_rand_seed() below.
+     */
+    mc->pid = getpid();
+
     /*
      * Let us cleanup on restarts and exists
      */
@@ -224,35 +172,58 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
     for (s = base_server; s; s = s->next) {
         sc = mySrvConfig(s);
 
+        if (sc->server) {
+            sc->server->sc = sc;
+        }
+
+        if (sc->proxy) {
+            sc->proxy->sc = sc;
+        }
+
         /*
          * Create the server host:port string because we need it a lot
          */
-        sc->szVHostID = ssl_util_vhostid(p, s);
-        sc->nVHostID_length = strlen(sc->szVHostID);
+        sc->vhost_id = ssl_util_vhostid(p, s);
+        sc->vhost_id_len = strlen(sc->vhost_id);
+
+        if (ap_get_server_protocol(s) &&
+            strcmp("https", ap_get_server_protocol(s)) == 0) {
+            sc->enabled = SSL_ENABLED_TRUE;
+        }
 
+       /* If sc->enabled is UNSET, then SSL is optional on this vhost  */
         /* Fix up stuff that may not have been set */
-        if (sc->bEnabled == UNSET) {
-            sc->bEnabled = FALSE;
+        if (sc->enabled == SSL_ENABLED_UNSET) {
+            sc->enabled = SSL_ENABLED_FALSE;
+        }
+        if (sc->proxy_enabled == UNSET) {
+            sc->proxy_enabled = FALSE;
         }
 
-        if (sc->nSessionCacheTimeout == UNSET) {
-            sc->nSessionCacheTimeout = SSL_SESSION_CACHE_TIMEOUT;
+        if (sc->session_cache_timeout == UNSET) {
+            sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
         }
 
-        if (sc->nPassPhraseDialogType == SSL_PPTYPE_UNSET) {
-            sc->nPassPhraseDialogType = SSL_PPTYPE_BUILTIN;
+        if (sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
+            sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
         }
 
-        /* Open the dedicated SSL logfile */
-        ssl_log_open(base_server, s, p);
     }
 
-    ssl_init_SSLLibrary(base_server);
-
 #if APR_HAS_THREADS
-    ssl_util_thread_setup(base_server, p);
+    ssl_util_thread_setup(p);
+#endif
+
+    /*
+     * SSL external crypto device ("engine") support
+     */
+#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
+    ssl_init_Engine(base_server, p);
 #endif
 
+    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                 "Init: Initialized %s library", SSL_LIBRARY_NAME);
+
     /*
      * Seed the Pseudo Random Number Generator (PRNG)
      * only need ptemp here; nothing inside allocated from the pool
@@ -268,14 +239,9 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
      */
     ssl_pphrase_Handle(base_server, ptemp);
 
-    ssl_tmp_keys_init(base_server);
-
-    /*
-     * SSL external crypto device ("engine") support
-     */
-#ifdef SSL_EXPERIMENTAL_ENGINE
-    ssl_init_Engine(base_server, p);
-#endif
+    if (ssl_tmp_keys_init(base_server)) {
+        return !OK;
+    }
 
     /*
      * initialize the mutex handling
@@ -283,6 +249,13 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
     if (!ssl_mutex_init(base_server, p)) {
         return HTTP_INTERNAL_SERVER_ERROR;
     }
+#ifdef HAVE_OCSP_STAPLING
+    if (!ssl_stapling_mutex_init(base_server, p)) {
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    ssl_stapling_ex_init();
+#endif
 
     /*
      * initialize session caching
@@ -292,8 +265,8 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
     /*
      *  initialize servers
      */
-    ssl_log(base_server, SSL_LOG_INFO,
-            "Init: Initializing (virtual) servers for SSL");
+    ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
+                 "Init: Initializing (virtual) servers for SSL");
 
     for (s = base_server; s; s = s->next) {
         sc = mySrvConfig(s);
@@ -302,12 +275,6 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
          * it or give out some information about what we're
          * configuring.
          */
-        if (!sc->bEnabled) {
-            continue;
-        }
-
-        ssl_log(s, SSL_LOG_INFO|SSL_INIT,
-                "Configuring server for SSL protocol");
 
         /*
          * Read the server certificate and key
@@ -335,7 +302,7 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
  * Support for external a Crypto Device ("engine"), usually
  * a hardware accellerator card for crypto operations.
  */
-#ifdef SSL_EXPERIMENTAL_ENGINE
+#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
 void ssl_init_Engine(server_rec *s, apr_pool_t *p)
 {
     SSLModConfigRec *mc = myModConfig(s);
@@ -343,9 +310,10 @@ void ssl_init_Engine(server_rec *s, apr_pool_t *p)
 
     if (mc->szCryptoDevice) {
         if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
-            ssl_log(s, SSL_LOG_ERROR,
-                    "Init: Failed to load Crypto Device API `%s'",
-                    mc->szCryptoDevice);
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                         "Init: Failed to load Crypto Device API `%s'",
+                         mc->szCryptoDevice);
+            ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
             ssl_die();
         }
 
@@ -354,28 +322,32 @@ void ssl_init_Engine(server_rec *s, apr_pool_t *p)
         }
 
         if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
-            ssl_log(s, SSL_LOG_ERROR,
-                    "Init: Failed to enable Crypto Device API `%s'",
-                    mc->szCryptoDevice);
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                         "Init: Failed to enable Crypto Device API `%s'",
+                         mc->szCryptoDevice);
+            ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
             ssl_die();
         }
+        ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, 
+                     "Init: loaded Crypto Device API `%s'", 
+                     mc->szCryptoDevice);
 
         ENGINE_free(e);
     }
 }
 #endif
 
-static void ssl_init_check_server(server_rec *s,
+static void ssl_init_server_check(server_rec *s,
                                   apr_pool_t *p,
                                   apr_pool_t *ptemp,
-                                  SSLSrvConfigRec *sc)
+                                  modssl_ctx_t *mctx)
 {
     /*
      * check for important parameters and the
      * possibility that the user forgot to set them.
      */
-    if (!sc->szPublicCertFiles[0]) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
+    if (!mctx->pks->cert_files[0] && !mctx->pkcs7) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                 "No SSL Certificate set [hint: SSLCertificateFile]");
         ssl_die();
     }
@@ -383,30 +355,71 @@ static void ssl_init_check_server(server_rec *s,
     /*
      *  Check for problematic re-initializations
      */
-    if (sc->pPublicCert[SSL_AIDX_RSA] ||
-        sc->pPublicCert[SSL_AIDX_DSA])
+    if (mctx->pks->certs[SSL_AIDX_RSA] ||
+        mctx->pks->certs[SSL_AIDX_DSA]
+#ifndef OPENSSL_NO_EC
+      || mctx->pks->certs[SSL_AIDX_ECC]
+#endif
+        )
     {
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                 "Illegal attempt to re-initialise SSL for server "
                 "(theoretically shouldn't happen!)");
         ssl_die();
     }
 }
 
-static SSL_CTX *ssl_init_ctx(server_rec *s,
-                             apr_pool_t *p,
-                             apr_pool_t *ptemp,
-                             SSLSrvConfigRec *sc)
+#ifndef OPENSSL_NO_TLSEXT
+static void ssl_init_ctx_tls_extensions(server_rec *s,
+                                        apr_pool_t *p,
+                                        apr_pool_t *ptemp,
+                                        modssl_ctx_t *mctx)
+{
+    /*
+     * Configure TLS extensions support
+     */
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Configuring TLS extension handling");
+
+    /*
+     * Server name indication (SNI)
+     */
+    if (!SSL_CTX_set_tlsext_servername_callback(mctx->ssl_ctx,
+                          ssl_callback_ServerNameIndication) ||
+        !SSL_CTX_set_tlsext_servername_arg(mctx->ssl_ctx, mctx)) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                     "Unable to initialize TLS servername extension "
+                     "callback (incompatible OpenSSL version?)");
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
+        ssl_die();
+    }
+
+#ifdef HAVE_OCSP_STAPLING
+    /*
+     * OCSP Stapling support, status_request extension
+     */
+    if ((mctx->pkp == FALSE) && (mctx->stapling_enabled == TRUE)) {
+        modssl_init_stapling(s, p, ptemp, mctx);
+    }
+#endif
+}
+#endif
+
+static void ssl_init_ctx_protocol(server_rec *s,
+                                  apr_pool_t *p,
+                                  apr_pool_t *ptemp,
+                                  modssl_ctx_t *mctx)
 {
     SSL_CTX *ctx = NULL;
+    MODSSL_SSL_METHOD_CONST SSL_METHOD *method = NULL;
     char *cp;
-    int protocol = sc->nProtocol;
+    int protocol = mctx->protocol;
 
     /*
      *  Create the new per-server SSL context
      */
     if (protocol == SSL_PROTOCOL_NONE) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                 "No SSL protocols available [hint: SSLProtocol]");
         ssl_die();
     }
@@ -418,17 +431,32 @@ static SSL_CTX *ssl_init_ctx(server_rec *s,
                      NULL);
     cp[strlen(cp)-2] = NUL;
 
-    ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-            "Creating new SSL context (protocols: %s)", cp);
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Creating new SSL context (protocols: %s)", cp);
 
     if (protocol == SSL_PROTOCOL_SSLV2) {
-        ctx = SSL_CTX_new(SSLv2_server_method());  /* only SSLv2 is left */
+        method = mctx->pkp ?
+            SSLv2_client_method() : /* proxy */
+            SSLv2_server_method();  /* server */
     }
-    else {
-        ctx = SSL_CTX_new(SSLv23_server_method()); /* be more flexible */
+    else if (protocol == SSL_PROTOCOL_SSLV3) {
+        method = mctx->pkp ?
+            SSLv3_client_method() : /* proxy */
+            SSLv3_server_method();  /* server */
     }
+    else if (protocol == SSL_PROTOCOL_TLSV1) {
+        method = mctx->pkp ?
+            TLSv1_client_method() : /* proxy */
+            TLSv1_server_method();  /* server */
+    }
+    else { /* For multiple protocols, we need a flexible method */
+        method = mctx->pkp ?
+            SSLv23_client_method() : /* proxy */
+            SSLv23_server_method();  /* server */
+    }
+    ctx = SSL_CTX_new(method);
 
-    sc->pSSLCtx = ctx;
+    mctx->ssl_ctx = ctx;
 
     SSL_CTX_set_options(ctx, SSL_OP_ALL);
 
@@ -444,6 +472,15 @@ static SSL_CTX *ssl_init_ctx(server_rec *s,
         SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
     }
 
+#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
+    {
+        SSLSrvConfigRec *sc = mySrvConfig(s);
+        if (sc->cipher_server_pref == TRUE) {
+            SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
+        }
+    }
+#endif
+
     SSL_CTX_set_app_data(ctx, s);
 
     /*
@@ -451,119 +488,140 @@ static SSL_CTX *ssl_init_ctx(server_rec *s,
      */
     SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
 
-    return ctx;
+#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
+    /*
+     * Disallow a session from being resumed during a renegotiation,
+     * so that an acceptable cipher suite can be negotiated.
+     */
+    SSL_CTX_set_options(ctx, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
+#endif
 }
 
-static void ssl_init_session_cache_ctx(server_rec *s,
+static void ssl_init_ctx_session_cache(server_rec *s,
                                        apr_pool_t *p,
                                        apr_pool_t *ptemp,
-                                       SSLSrvConfigRec *sc)
+                                       modssl_ctx_t *mctx)
 {
-    SSL_CTX *ctx = sc->pSSLCtx;
+    SSL_CTX *ctx = mctx->ssl_ctx;
     SSLModConfigRec *mc = myModConfig(s);
-    long cache_mode = SSL_SESS_CACHE_OFF;
 
-    if (mc->nSessionCacheMode != SSL_SCMODE_NONE) {
-        /* SSL_SESS_CACHE_NO_INTERNAL_LOOKUP will force OpenSSL
-         * to ignore process local-caching and
-         * to always get/set/delete sessions using mod_ssl's callbacks.
-         */
-        cache_mode = SSL_SESS_CACHE_SERVER|SSL_SESS_CACHE_NO_INTERNAL_LOOKUP;
+    SSL_CTX_set_session_cache_mode(ctx, mc->sesscache_mode);
+
+    if (mc->sesscache) {
+        SSL_CTX_sess_set_new_cb(ctx,    ssl_callback_NewSessionCacheEntry);
+        SSL_CTX_sess_set_get_cb(ctx,    ssl_callback_GetSessionCacheEntry);
+        SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
     }
+}
+
+static void ssl_init_ctx_callbacks(server_rec *s,
+                                   apr_pool_t *p,
+                                   apr_pool_t *ptemp,
+                                   modssl_ctx_t *mctx)
+{
+    SSL_CTX *ctx = mctx->ssl_ctx;
 
-    SSL_CTX_set_session_cache_mode(ctx, cache_mode);
+    SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
+    SSL_CTX_set_tmp_dh_callback(ctx,  ssl_callback_TmpDH);
+#ifndef OPENSSL_NO_EC
+    SSL_CTX_set_tmp_ecdh_callback(ctx,ssl_callback_TmpECDH);
+#endif
 
-    SSL_CTX_sess_set_new_cb(ctx,    ssl_callback_NewSessionCacheEntry);
-    SSL_CTX_sess_set_get_cb(ctx,    ssl_callback_GetSessionCacheEntry);
-    SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
+    SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
 }
 
-static void ssl_init_verify(server_rec *s,
-                            apr_pool_t *p,
-                            apr_pool_t *ptemp,
-                            SSLSrvConfigRec *sc)
+static void ssl_init_ctx_verify(server_rec *s,
+                                apr_pool_t *p,
+                                apr_pool_t *ptemp,
+                                modssl_ctx_t *mctx)
 {
-    SSL_CTX *ctx = sc->pSSLCtx;
+    SSL_CTX *ctx = mctx->ssl_ctx;
 
     int verify = SSL_VERIFY_NONE;
     STACK_OF(X509_NAME) *ca_list;
 
-    if (sc->nVerifyClient == SSL_CVERIFY_UNSET) {
-        sc->nVerifyClient = SSL_CVERIFY_NONE;
+    if (mctx->auth.verify_mode == SSL_CVERIFY_UNSET) {
+        mctx->auth.verify_mode = SSL_CVERIFY_NONE;
     }
 
-    if (sc->nVerifyDepth == UNSET) {
-        sc->nVerifyDepth = 1;
+    if (mctx->auth.verify_depth == UNSET) {
+        mctx->auth.verify_depth = 1;
     }
 
     /*
      *  Configure callbacks for SSL context
      */
-    if (sc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
+    if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
         verify |= SSL_VERIFY_PEER_STRICT;
     }
 
-    if ((sc->nVerifyClient == SSL_CVERIFY_OPTIONAL) ||
-        (sc->nVerifyClient == SSL_CVERIFY_OPTIONAL_NO_CA))
+    if ((mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL) ||
+        (mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL_NO_CA))
     {
         verify |= SSL_VERIFY_PEER;
     }
 
-    SSL_CTX_set_verify(ctx, verify,  ssl_callback_SSLVerify);
+    SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
 
     /*
      * Configure Client Authentication details
      */
-    if (sc->szCACertificateFile || sc->szCACertificatePath) {
-        ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-                "Configuring client authentication");
+    if (mctx->auth.ca_cert_file || mctx->auth.ca_cert_path) {
+        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                     "Configuring client authentication");
 
         if (!SSL_CTX_load_verify_locations(ctx,
-                                           sc->szCACertificateFile,
-                                           sc->szCACertificatePath))
+                         MODSSL_PCHAR_CAST mctx->auth.ca_cert_file,
+                         MODSSL_PCHAR_CAST mctx->auth.ca_cert_path))
         {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                     "Unable to configure verify locations "
                     "for client authentication");
+            ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
             ssl_die();
         }
 
-        ca_list = ssl_init_FindCAList(s, ptemp,
-                                      sc->szCACertificateFile,
-                                      sc->szCACertificatePath);
+        if (mctx->pks && (mctx->pks->ca_name_file || mctx->pks->ca_name_path)) {
+            ca_list = ssl_init_FindCAList(s, ptemp,
+                                          mctx->pks->ca_name_file,
+                                          mctx->pks->ca_name_path);
+        } else
+            ca_list = ssl_init_FindCAList(s, ptemp,
+                                          mctx->auth.ca_cert_file,
+                                          mctx->auth.ca_cert_path);
         if (!ca_list) {
-            ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
-                    "Unable to determine list of available "
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                    "Unable to determine list of acceptable "
                     "CA certificates for client authentication");
             ssl_die();
         }
 
-        SSL_CTX_set_client_CA_list(ctx, (STACK *)ca_list);
+        SSL_CTX_set_client_CA_list(ctx, ca_list);
     }
 
     /*
      * Give a warning when no CAs were configured but client authentication
      * should take place. This cannot work.
      */
-    if (sc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
-        ca_list = (STACK_OF(X509_NAME) *)SSL_CTX_get_client_CA_list(ctx);
+    if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
+        ca_list = SSL_CTX_get_client_CA_list(ctx);
 
         if (sk_X509_NAME_num(ca_list) == 0) {
-            ssl_log(s, SSL_LOG_WARN,
-                    "Init: Oops, you want to request client authentication, "
-                    "but no CAs are known for verification!? "
-                    "[Hint: SSLCACertificate*]");
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                         "Init: Oops, you want to request client "
+                         "authentication, but no CAs are known for "
+                         "verification!?  [Hint: SSLCACertificate*]");
         }
     }
 }
 
-static void ssl_init_cipher_suite(server_rec *s,
-                                  apr_pool_t *p,
-                                  apr_pool_t *ptemp,
-                                  SSLSrvConfigRec *sc)
+static void ssl_init_ctx_cipher_suite(server_rec *s,
+                                      apr_pool_t *p,
+                                      apr_pool_t *ptemp,
+                                      modssl_ctx_t *mctx)
 {
-    SSL_CTX *ctx = sc->pSSLCtx;
-    const char *suite = sc->szCipherSuite;
+    SSL_CTX *ctx = mctx->ssl_ctx;
+    const char *suite = mctx->auth.cipher_suite;
 
     /*
      *  Configure SSL Cipher Suite
@@ -572,55 +630,72 @@ static void ssl_init_cipher_suite(server_rec *s,
         return;
     }
 
-    ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-            "Configuring permitted SSL ciphers [%s]", 
-            suite);
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Configuring permitted SSL ciphers [%s]",
+                 suite);
 
-    if (!SSL_CTX_set_cipher_list(ctx, suite)) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
+    if (!SSL_CTX_set_cipher_list(ctx, MODSSL_PCHAR_CAST suite)) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                 "Unable to configure permitted SSL ciphers");
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
         ssl_die();
     }
 }
 
-static void ssl_init_crl(server_rec *s,
-                         apr_pool_t *p,
-                         apr_pool_t *ptemp,
-                         SSLSrvConfigRec *sc)
+static void ssl_init_ctx_crl(server_rec *s,
+                             apr_pool_t *p,
+                             apr_pool_t *ptemp,
+                             modssl_ctx_t *mctx)
 {
     /*
      * Configure Certificate Revocation List (CRL) Details
      */
 
-    if (!(sc->szCARevocationFile || sc->szCARevocationPath)) {
+    if (!(mctx->crl_file || mctx->crl_path)) {
         return;
     }
 
-    ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-            "Configuring certificate revocation facility");
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Configuring certificate revocation facility");
 
-    sc->pRevocationStore =
-        SSL_X509_STORE_create((char *)sc->szCARevocationFile,
-                              (char *)sc->szCARevocationPath);
+    mctx->crl =
+        SSL_X509_STORE_create((char *)mctx->crl_file,
+                              (char *)mctx->crl_path);
 
-    if (!sc->pRevocationStore) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
+    if (!mctx->crl) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                 "Unable to configure X.509 CRL storage "
                 "for certificate revocation");
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
         ssl_die();
     }
 }
 
-static void ssl_init_cert_chain(server_rec *s,
-                                apr_pool_t *p,
-                                apr_pool_t *ptemp,
-                                SSLSrvConfigRec *sc)
+static void ssl_init_ctx_pkcs7_cert_chain(server_rec *s, modssl_ctx_t *mctx)
 {
-    BOOL skip_first = TRUE;
+    STACK_OF(X509) *certs = ssl_read_pkcs7(s, mctx->pkcs7);
+    int n;
+
+    if (!mctx->ssl_ctx->extra_certs)
+        for (n = 1; n < sk_X509_num(certs); ++n)
+             SSL_CTX_add_extra_chain_cert(mctx->ssl_ctx, sk_X509_value(certs, n));
+}
+
+static void ssl_init_ctx_cert_chain(server_rec *s,
+                                    apr_pool_t *p,
+                                    apr_pool_t *ptemp,
+                                    modssl_ctx_t *mctx)
+{
+    BOOL skip_first = FALSE;
     int i, n;
-    const char *chain = sc->szCertificateChain;
+    const char *chain = mctx->cert_chain;
 
-    /* 
+    if (mctx->pkcs7) {
+        ssl_init_ctx_pkcs7_cert_chain(s, mctx);
+        return;
+    }
+
+    /*
      * Optionally configure extra server certificate chain certificates.
      * This is usually done by OpenSSL automatically when one of the
      * server cert issuers are found under SSLCACertificatePath or in
@@ -638,254 +713,387 @@ static void ssl_init_cert_chain(server_rec *s,
         return;
     }
 
-    for (i = 0; (i < SSL_AIDX_MAX) && sc->szPublicCertFiles[i]; i++) {
-        if (strEQ(sc->szPublicCertFiles[i], chain)) {
+    for (i = 0; (i < SSL_AIDX_MAX) && mctx->pks->cert_files[i]; i++) {
+        if (strEQ(mctx->pks->cert_files[i], chain)) {
             skip_first = TRUE;
             break;
         }
     }
 
-    n = SSL_CTX_use_certificate_chain(sc->pSSLCtx,
-                                      (char *)chain, 
+    n = SSL_CTX_use_certificate_chain(mctx->ssl_ctx,
+                                      (char *)chain,
                                       skip_first, NULL);
     if (n < 0) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                 "Failed to configure CA certificate chain!");
         ssl_die();
     }
 
-    ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-            "Configuring server certificate chain "
-            "(%d CA certificate%s)",
-            n, n == 1 ? "" : "s");
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Configuring server certificate chain "
+                 "(%d CA certificate%s)",
+                 n, n == 1 ? "" : "s");
 }
 
-/*
- * Configure a particular server
- */
-void ssl_init_ConfigureServer(server_rec *s,
-                              apr_pool_t *p,
-                              apr_pool_t *ptemp,
-                              SSLSrvConfigRec *sc)
+static void ssl_init_ctx(server_rec *s,
+                         apr_pool_t *p,
+                         apr_pool_t *ptemp,
+                         modssl_ctx_t *mctx)
 {
-    SSLModConfigRec *mc = myModConfig(s);
-    char *cp;
-    const char *rsa_id, *dsa_id;
-    const char *vhost_id = sc->szVHostID;
-    EVP_PKEY *pkey;
-    SSL_CTX *ctx;
-    ssl_asn1_t *asn1;
-    unsigned char *ptr;
-    BOOL ok = FALSE;
-    int is_ca, pathlen;
-    int i;
+    ssl_init_ctx_protocol(s, p, ptemp, mctx);
 
-    ssl_init_check_server(s, p, ptemp, sc);
+    ssl_init_ctx_session_cache(s, p, ptemp, mctx);
 
-    ctx = ssl_init_ctx(s, p, ptemp, sc);
+    ssl_init_ctx_callbacks(s, p, ptemp, mctx);
 
-    ssl_init_session_cache_ctx(s, p, ptemp, sc);
+    ssl_init_ctx_verify(s, p, ptemp, mctx);
 
-    ssl_init_verify(s, p, ptemp, sc);
+    ssl_init_ctx_cipher_suite(s, p, ptemp, mctx);
 
-    ssl_init_cipher_suite(s, p, ptemp, sc);
+    ssl_init_ctx_crl(s, p, ptemp, mctx);
 
-    ssl_init_crl(s, p, ptemp, sc);
-
-    ssl_init_cert_chain(s, p, ptemp, sc);
+    if (mctx->pks) {
+        /* XXX: proxy support? */
+        ssl_init_ctx_cert_chain(s, p, ptemp, mctx);
+#ifndef OPENSSL_NO_TLSEXT
+        ssl_init_ctx_tls_extensions(s, p, ptemp, mctx);
+#endif
+    }
+}
 
-    SSL_CTX_set_tmp_rsa_callback(ctx, ssl_callback_TmpRSA);
-    SSL_CTX_set_tmp_dh_callback(ctx,  ssl_callback_TmpDH);
+static int ssl_server_import_cert(server_rec *s,
+                                  modssl_ctx_t *mctx,
+                                  const char *id,
+                                  int idx)
+{
+    SSLModConfigRec *mc = myModConfig(s);
+    ssl_asn1_t *asn1;
+    MODSSL_D2I_X509_CONST unsigned char *ptr;
+    const char *type = ssl_asn1_keystr(idx);
+    X509 *cert;
 
-    if (sc->nLogLevel >= SSL_LOG_INFO) {
-        /* this callback only logs if SSLLogLevel >= info */
-        SSL_CTX_set_info_callback(ctx, ssl_callback_LogTracingState);
+    if (!(asn1 = ssl_asn1_table_get(mc->tPublicCert, id))) {
+        return FALSE;
     }
 
-    /*
-     *  Configure server certificate(s)
-     */
-    rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
-    dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Configuring %s server certificate", type);
 
-    if ((asn1 = ssl_asn1_table_get(mc->tPublicCert, rsa_id))) {
-        ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-                "Configuring RSA server certificate");
+    ptr = asn1->cpData;
+    if (!(cert = d2i_X509(NULL, &ptr, asn1->nData))) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                "Unable to import %s server certificate", type);
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
+        ssl_die();
+    }
 
-        ptr = asn1->cpData;
-        if (!(sc->pPublicCert[SSL_AIDX_RSA] =
-              d2i_X509(NULL, &ptr, asn1->nData)))
-        {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to import RSA server certificate");
-            ssl_die();
+    if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) <= 0) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                "Unable to configure %s server certificate", type);
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
+        ssl_die();
+    }
+  
+#ifdef HAVE_OCSP_STAPLING
+    if ((mctx->pkp == FALSE) && (mctx->stapling_enabled == TRUE)) {
+        if (!ssl_stapling_init_cert(s, mctx, cert)) {
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                         "Unable to configure server certificate for stapling");
         }
+    }
+#endif
 
-        if (SSL_CTX_use_certificate(ctx, sc->pPublicCert[SSL_AIDX_RSA]) <= 0) {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to configure RSA server certificate");
-            ssl_die();
-        }
+    mctx->pks->certs[idx] = cert;
 
-        ok = TRUE;
-    }
+    return TRUE;
+}
 
-    if ((asn1 = ssl_asn1_table_get(mc->tPublicCert, dsa_id))) {
-        ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-                "Configuring DSA server certificate");
+static int ssl_server_import_key(server_rec *s,
+                                 modssl_ctx_t *mctx,
+                                 const char *id,
+                                 int idx)
+{
+    SSLModConfigRec *mc = myModConfig(s);
+    ssl_asn1_t *asn1;
+    MODSSL_D2I_PrivateKey_CONST unsigned char *ptr;
+    const char *type = ssl_asn1_keystr(idx);
+    int pkey_type;
+    EVP_PKEY *pkey;
 
-        ptr = asn1->cpData;
-        if (!(sc->pPublicCert[SSL_AIDX_DSA] =
-              d2i_X509(NULL, &ptr, asn1->nData)))
-        {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to import DSA server certificate");
-            ssl_die();
-        }
+#ifndef OPENSSL_NO_EC
+    if (idx == SSL_AIDX_ECC)
+      pkey_type = EVP_PKEY_EC;
+    else
+#endif /* SSL_LIBRARY_VERSION */
+    pkey_type = (idx == SSL_AIDX_RSA) ? EVP_PKEY_RSA : EVP_PKEY_DSA;
 
-        if (SSL_CTX_use_certificate(ctx, sc->pPublicCert[SSL_AIDX_DSA]) <= 0) {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to configure DSA server certificate");
-            ssl_die();
-        }
+    if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, id))) {
+        return FALSE;
+    }
+
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "Configuring %s server private key", type);
 
-        ok = TRUE;
+    ptr = asn1->cpData;
+    if (!(pkey = d2i_PrivateKey(pkey_type, NULL, &ptr, asn1->nData)))
+    {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                "Unable to import %s server private key", type);
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
+        ssl_die();
     }
 
-    if (!ok) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
-                "Oops, no RSA or DSA server certificate found?!");
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
-                "You have to perform a *full* server restart "
-                "when you added or removed a certificate and/or key file");
+    if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) <= 0) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                "Unable to configure %s server private key", type);
+        ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
         ssl_die();
     }
 
     /*
-     * Some information about the certificate(s)
+     * XXX: wonder if this is still needed, this is old todo doc.
+     * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
      */
-    for (i = 0; i < SSL_AIDX_MAX; i++) {
-        if (sc->pPublicCert[i]) {
-            if (SSL_X509_isSGC(sc->pPublicCert[i])) {
-                ssl_log(s, SSL_LOG_INFO|SSL_INIT,
-                        "%s server certificate enables "
-                        "Server Gated Cryptography (SGC)", 
-                        ssl_asn1_keystr(i));
-            }
+    if ((pkey_type == EVP_PKEY_DSA) && mctx->pks->certs[idx]) {
+        EVP_PKEY *pubkey = X509_get_pubkey(mctx->pks->certs[idx]);
+
+        if (pubkey && EVP_PKEY_missing_parameters(pubkey)) {
+            EVP_PKEY_copy_parameters(pubkey, pkey);
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+                    "Copying DSA parameters from private key to certificate");
+            ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, s);
+            EVP_PKEY_free(pubkey);
+        }
+    }
 
-            if (SSL_X509_getBC(sc->pPublicCert[i], &is_ca, &pathlen)) {
-                if (is_ca) {
-                    ssl_log(s, SSL_LOG_WARN|SSL_INIT,
-                            "%s server certificate "
-                            "is a CA certificate "
-                            "(BasicConstraints: CA == TRUE !?)",
-                            ssl_asn1_keystr(i));
-                }
-
-                if (pathlen > 0) {
-                    ssl_log(s, SSL_LOG_WARN|SSL_INIT,
-                            "%s server certificate "
-                            "is not a leaf certificate "
-                            "(BasicConstraints: pathlen == %d > 0 !?)",
-                            ssl_asn1_keystr(i), pathlen);
-                }
-            }
+    mctx->pks->keys[idx] = pkey;
 
-            if (SSL_X509_getCN(p, sc->pPublicCert[i], &cp)) {
-                int fnm_flags = FNM_PERIOD|FNM_CASE_BLIND;
-
-                if (apr_is_fnmatch(cp) &&
-                    (apr_fnmatch(cp, s->server_hostname,
-                                 fnm_flags) == FNM_NOMATCH))
-                {
-                    ssl_log(s, SSL_LOG_WARN|SSL_INIT,
-                            "%s server certificate "
-                            "wildcard CommonName (CN) `%s' "
-                            "does NOT match server name!?",
-                            ssl_asn1_keystr(i), cp);
-                }
-                else if (strNE(s->server_hostname, cp)) {
-                    ssl_log(s, SSL_LOG_WARN|SSL_INIT,
-                            "%s server certificate "
-                            "CommonName (CN) `%s' "
-                            "does NOT match server name!?",
-                            ssl_asn1_keystr(i), cp);
-                }
-            }
-        }
+    return TRUE;
+}
+
+static void ssl_check_public_cert(server_rec *s,
+                                  apr_pool_t *ptemp,
+                                  X509 *cert,
+                                  int type)
+{
+    int is_ca, pathlen;
+    char *cn;
+
+    if (!cert) {
+        return;
     }
 
     /*
-     *  Configure server private key(s)
+     * Some information about the certificate(s)
      */
-    ok = FALSE;
 
-    if ((asn1 = ssl_asn1_table_get(mc->tPrivateKey, rsa_id))) {
-        ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-                "Configuring RSA server private key");
+    if (SSL_X509_isSGC(cert)) {
+        ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                     "%s server certificate enables "
+                     "Server Gated Cryptography (SGC)",
+                     ssl_asn1_keystr(type));
+    }
 
-        ptr = asn1->cpData;
-        if (!(sc->pPrivateKey[SSL_AIDX_RSA] = 
-              d2i_PrivateKey(EVP_PKEY_RSA, NULL, &ptr, asn1->nData)))
-        {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to import RSA server private key");
-            ssl_die();
+    if (SSL_X509_getBC(cert, &is_ca, &pathlen)) {
+        if (is_ca) {
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                         "%s server certificate is a CA certificate "
+                         "(BasicConstraints: CA == TRUE !?)",
+                         ssl_asn1_keystr(type));
         }
 
-        if (SSL_CTX_use_PrivateKey(ctx, sc->pPrivateKey[SSL_AIDX_RSA]) <= 0) {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to configure RSA server private key");
-            ssl_die();
+        if (pathlen > 0) {
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                         "%s server certificate is not a leaf certificate "
+                         "(BasicConstraints: pathlen == %d > 0 !?)",
+                         ssl_asn1_keystr(type), pathlen);
         }
-
-        ok = TRUE;
     }
 
-    if ((asn1 = ssl_asn1_table_get(mc->tPrivateKey, dsa_id))) {
-        ssl_log(s, SSL_LOG_TRACE|SSL_INIT,
-                "Configuring DSA server private key");
+    if (SSL_X509_getCN(ptemp, cert, &cn)) {
+        int fnm_flags = APR_FNM_PERIOD|APR_FNM_CASE_BLIND;
 
-        ptr = asn1->cpData;
-        if (!(sc->pPrivateKey[SSL_AIDX_DSA] = 
-              d2i_PrivateKey(EVP_PKEY_DSA, NULL, &ptr, asn1->nData)))
-        {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to import DSA server private key");
-            ssl_die();
+        if (apr_fnmatch_test(cn)) {
+            if (apr_fnmatch(cn, s->server_hostname,
+                            fnm_flags) == APR_FNM_NOMATCH) {
+                ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                             "%s server certificate wildcard CommonName "
+                             "(CN) `%s' does NOT match server name!?",
+                             ssl_asn1_keystr(type), cn);
+            }
         }
-
-        if (SSL_CTX_use_PrivateKey(ctx, sc->pPrivateKey[SSL_AIDX_DSA]) <= 0) {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_SSLERR|SSL_INIT,
-                    "Unable to configure DSA server private key");
-            ssl_die();
+        else if (strNE(s->server_hostname, cn)) {
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                         "%s server certificate CommonName (CN) `%s' "
+                         "does NOT match server name!?",
+                         ssl_asn1_keystr(type), cn);
         }
+    }
+}
 
-        ok = TRUE;
+static void ssl_init_server_certs(server_rec *s,
+                                  apr_pool_t *p,
+                                  apr_pool_t *ptemp,
+                                  modssl_ctx_t *mctx)
+{
+    const char *rsa_id, *dsa_id;
+#ifndef OPENSSL_NO_EC
+    const char *ecc_id;
+#endif
+    const char *vhost_id = mctx->sc->vhost_id;
+    int i;
+    int have_rsa, have_dsa;
+#ifndef OPENSSL_NO_EC
+    int have_ecc;
+#endif
+
+    rsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_RSA);
+    dsa_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_DSA);
+#ifndef OPENSSL_NO_EC
+    ecc_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_ECC);
+#endif
+
+    have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
+    have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
+#ifndef OPENSSL_NO_EC
+    have_ecc = ssl_server_import_cert(s, mctx, ecc_id, SSL_AIDX_ECC);
+#endif
+
+    if (!(have_rsa || have_dsa
+#ifndef OPENSSL_NO_EC
+        || have_ecc
+#endif
+)) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+#ifndef OPENSSL_NO_EC
+                "Oops, no RSA, DSA or ECC server certificate found "
+#else
+                "Oops, no RSA or DSA server certificate found "
+#endif
+                "for '%s:%d'?!", s->server_hostname, s->port);
+        ssl_die();
     }
 
-    if (!ok) {
-        ssl_log(s, SSL_LOG_ERROR|SSL_INIT,
+    for (i = 0; i < SSL_AIDX_MAX; i++) {
+        ssl_check_public_cert(s, ptemp, mctx->pks->certs[i], i);
+    }
+
+    have_rsa = ssl_server_import_key(s, mctx, rsa_id, SSL_AIDX_RSA);
+    have_dsa = ssl_server_import_key(s, mctx, dsa_id, SSL_AIDX_DSA);
+#ifndef OPENSSL_NO_EC
+    have_ecc = ssl_server_import_key(s, mctx, ecc_id, SSL_AIDX_ECC);
+#endif
+
+    if (!(have_rsa || have_dsa
+#ifndef OPENSSL_NO_EC
+        || have_ecc
+#endif
+          )) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
+#ifndef OPENSSL_NO_EC
+                "Oops, no RSA, DSA or ECC server private key found?!");
+#else
                 "Oops, no RSA or DSA server private key found?!");
+#endif
         ssl_die();
     }
+}
 
-    /*
-     * Optionally copy DSA parameters for certificate from private key
-     * (see http://www.psy.uq.edu.au/~ftp/Crypto/ssleay/TODO.html)
-     */
-    if (sc->pPublicCert[SSL_AIDX_DSA] &&
-        sc->pPrivateKey[SSL_AIDX_DSA])
-    {
-        pkey = X509_get_pubkey(sc->pPublicCert[SSL_AIDX_DSA]);
+static void ssl_init_proxy_certs(server_rec *s,
+                                 apr_pool_t *p,
+                                 apr_pool_t *ptemp,
+                                 modssl_ctx_t *mctx)
+{
+    int n, ncerts = 0;
+    STACK_OF(X509_INFO) *sk;
+    modssl_pk_proxy_t *pkp = mctx->pkp;
 
-        if (pkey && (EVP_PKEY_key_type(pkey) == EVP_PKEY_DSA) &&
-            EVP_PKEY_missing_parameters(pkey))
-        {
-            EVP_PKEY_copy_parameters(pkey,
-                                     sc->pPrivateKey[SSL_AIDX_DSA]);
+    SSL_CTX_set_client_cert_cb(mctx->ssl_ctx,
+                               ssl_callback_proxy_cert);
+
+    if (!(pkp->cert_file || pkp->cert_path)) {
+        return;
+    }
+
+    sk = sk_X509_INFO_new_null();
+
+    if (pkp->cert_file) {
+        SSL_X509_INFO_load_file(ptemp, sk, pkp->cert_file);
+    }
+
+    if (pkp->cert_path) {
+        SSL_X509_INFO_load_path(ptemp, sk, pkp->cert_path);
+    }
+
+    if ((ncerts = sk_X509_INFO_num(sk)) <= 0) {
+        sk_X509_INFO_free(sk);
+        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                     "no client certs found for SSL proxy");
+        return;
+    }
+
+    /* Check that all client certs have got certificates and private
+     * keys. */
+    for (n = 0; n < ncerts; n++) {
+        X509_INFO *inf = sk_X509_INFO_value(sk, n);
+
+        if (!inf->x509 || !inf->x_pkey) {
+            sk_X509_INFO_free(sk);
+            ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
+                         "incomplete client cert configured for SSL proxy "
+                         "(missing or encrypted private key?)");
+            ssl_die();
+            return;
         }
     }
+
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                 "loaded %d client certs for SSL proxy",
+                 ncerts);
+    pkp->certs = sk;
+}
+
+static void ssl_init_proxy_ctx(server_rec *s,
+                               apr_pool_t *p,
+                               apr_pool_t *ptemp,
+                               SSLSrvConfigRec *sc)
+{
+    ssl_init_ctx(s, p, ptemp, sc->proxy);
+
+    ssl_init_proxy_certs(s, p, ptemp, sc->proxy);
+}
+
+static void ssl_init_server_ctx(server_rec *s,
+                                apr_pool_t *p,
+                                apr_pool_t *ptemp,
+                                SSLSrvConfigRec *sc)
+{
+    ssl_init_server_check(s, p, ptemp, sc->server);
+
+    ssl_init_ctx(s, p, ptemp, sc->server);
+
+    ssl_init_server_certs(s, p, ptemp, sc->server);
+}
+
+/*
+ * Configure a particular server
+ */
+void ssl_init_ConfigureServer(server_rec *s,
+                              apr_pool_t *p,
+                              apr_pool_t *ptemp,
+                              SSLSrvConfigRec *sc)
+{
+    /* Initialize the server if SSL is enabled or optional.
+     */
+    if ((sc->enabled == SSL_ENABLED_TRUE) || (sc->enabled == SSL_ENABLED_OPTIONAL)) {
+        ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
+                     "Configuring server for SSL protocol");
+        ssl_init_server_ctx(s, p, ptemp, sc);
+    }
+
+    if (sc->proxy_enabled) {
+        ssl_init_proxy_ctx(s, p, ptemp, sc);
+    }
 }
 
 void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
@@ -899,26 +1107,28 @@ void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
     BOOL conflict = FALSE;
 
     /*
-     * Give out warnings when a server has HTTPS configured 
+     * Give out warnings when a server has HTTPS configured
      * for the HTTP port or vice versa
      */
     for (s = base_server; s; s = s->next) {
         sc = mySrvConfig(s);
 
-        if (sc->bEnabled && (s->port == DEFAULT_HTTP_PORT)) {
-            ssl_log(base_server, SSL_LOG_WARN,
-                    "Init: (%s) You configured HTTPS(%d) "
-                    "on the standard HTTP(%d) port!",
-                    ssl_util_vhostid(p, s),
-                    DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
+        if ((sc->enabled == SSL_ENABLED_TRUE) && (s->port == DEFAULT_HTTP_PORT)) {
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
+                         base_server,
+                         "Init: (%s) You configured HTTPS(%d) "
+                         "on the standard HTTP(%d) port!",
+                         ssl_util_vhostid(p, s),
+                         DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
         }
 
-        if (!sc->bEnabled && (s->port == DEFAULT_HTTPS_PORT)) {
-            ssl_log(base_server, SSL_LOG_WARN,
-                    "Init: (%s) You configured HTTP(%d) "
-                    "on the standard HTTPS(%d) port!",
-                    ssl_util_vhostid(p, s),
-                    DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
+        if ((sc->enabled == SSL_ENABLED_FALSE) && (s->port == DEFAULT_HTTPS_PORT)) {
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
+                         base_server,
+                         "Init: (%s) You configured HTTP(%d) "
+                         "on the standard HTTPS(%d) port!",
+                         ssl_util_vhostid(p, s),
+                         DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
         }
     }
 
@@ -931,26 +1141,39 @@ void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
     table = apr_hash_make(p);
 
     for (s = base_server; s; s = s->next) {
+        char *addr;
+
         sc = mySrvConfig(s);
 
-        if (!sc->bEnabled) {
+        if (!((sc->enabled == SSL_ENABLED_TRUE) && s->addrs)) {
             continue;
         }
 
-        key = apr_psprintf(p, "%pA:%u",
-                           &s->addrs->host_addr, s->addrs->host_port);
+        apr_sockaddr_ip_get(&addr, s->addrs->host_addr);
+        key = apr_psprintf(p, "%s:%u", addr, s->addrs->host_port);
         klen = strlen(key);
 
         if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
-            ssl_log(base_server, SSL_LOG_WARN,
-                    "Init: SSL server IP/port conflict: "
-                    "%s (%s:%d) vs. %s (%s:%d)",
-                    ssl_util_vhostid(p, s), 
-                    (s->defn_name ? s->defn_name : "unknown"),
-                    s->defn_line_number,
-                    ssl_util_vhostid(p, ps),
-                    (ps->defn_name ? ps->defn_name : "unknown"), 
-                    ps->defn_line_number);
+            ap_log_error(APLOG_MARK, 
+#ifdef OPENSSL_NO_TLSEXT
+                         APLOG_WARNING, 
+#else
+                         APLOG_DEBUG, 
+#endif
+                         0,
+                         base_server,
+#ifdef OPENSSL_NO_TLSEXT
+                         "Init: SSL server IP/port conflict: "
+#else
+                         "Init: SSL server IP/port overlap: "
+#endif
+                         "%s (%s:%d) vs. %s (%s:%d)",
+                         ssl_util_vhostid(p, s),
+                         (s->defn_name ? s->defn_name : "unknown"),
+                         s->defn_line_number,
+                         ssl_util_vhostid(p, ps),
+                         (ps->defn_name ? ps->defn_name : "unknown"),
+                         ps->defn_line_number);
             conflict = TRUE;
             continue;
         }
@@ -959,16 +1182,30 @@ void ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
     }
 
     if (conflict) {
-        ssl_log(base_server, SSL_LOG_WARN,
-                "Init: You should not use name-based "
-                "virtual hosts in conjunction with SSL!!");
+        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server,
+#ifdef OPENSSL_NO_TLSEXT
+                     "Init: You should not use name-based "
+                     "virtual hosts in conjunction with SSL!!");
+#else
+                     "Init: Name-based SSL virtual hosts only "
+                     "work for clients with TLS server name indication "
+                     "support (RFC 4366)");
+#endif
     }
 }
 
-static int ssl_init_FindCAList_X509NameCmp(X509_NAME **a, X509_NAME **b)
+#ifdef SSLC_VERSION_NUMBER
+static int ssl_init_FindCAList_X509NameCmp(char **a, char **b)
+{
+    return(X509_NAME_cmp((void*)*a, (void*)*b));
+}
+#else
+static int ssl_init_FindCAList_X509NameCmp(const X509_NAME * const *a, 
+                                           const X509_NAME * const *b)
 {
     return(X509_NAME_cmp(*a, *b));
 }
+#endif
 
 static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
                                 server_rec *s, const char *file)
@@ -976,7 +1213,8 @@ static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
     int n;
     STACK_OF(X509_NAME) *sk;
 
-    sk = (STACK_OF(X509_NAME) *)SSL_load_client_CA_file(file);
+    sk = (STACK_OF(X509_NAME) *)
+             SSL_load_client_CA_file(MODSSL_PCHAR_CAST file);
 
     if (!sk) {
         return;
@@ -986,9 +1224,9 @@ static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
         char name_buf[256];
         X509_NAME *name = sk_X509_NAME_value(sk, n);
 
-        ssl_log(s, SSL_LOG_TRACE,
-                "CA certificate: %s",
-                X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
+        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
+                     "CA certificate: %s",
+                     X509_NAME_oneline(name, name_buf, sizeof(name_buf)));
 
         /*
          * note that SSL_load_client_CA_file() checks for duplicates,
@@ -1035,11 +1273,12 @@ STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
     if (ca_path) {
         apr_dir_t *dir;
         apr_finfo_t direntry;
-        apr_int32_t finfo_flags = APR_FINFO_MIN|APR_FINFO_NAME;
+        apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
+        apr_status_t rv;
 
-        if (apr_dir_open(&dir, ca_path, ptemp) != APR_SUCCESS) {
-            ssl_log(s, SSL_LOG_ERROR|SSL_ADD_ERRNO|SSL_INIT,
-                    "Failed to open SSLCACertificatePath `%s'",
+        if ((rv = apr_dir_open(&dir, ca_path, ptemp)) != APR_SUCCESS) {
+            ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                    "Failed to open Certificate Path `%s'",
                     ca_path);
             ssl_die();
         }
@@ -1059,7 +1298,7 @@ STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
     /*
      * Cleanup
      */
-    sk_X509_NAME_set_cmp_func(ca_list, NULL);
+    (void) sk_X509_NAME_set_cmp_func(ca_list, NULL);
 
     return ca_list;
 }
@@ -1074,6 +1313,9 @@ void ssl_init_Child(apr_pool_t *p, server_rec *s)
 
     /* open the mutex lockfile */
     ssl_mutex_reinit(s, p);
+#ifdef HAVE_OCSP_STAPLING
+    ssl_stapling_mutex_reinit(s, p);
+#endif
 }
 
 #define MODSSL_CFG_ITEM_FREE(func, item) \
@@ -1082,6 +1324,38 @@ void ssl_init_Child(apr_pool_t *p, server_rec *s)
         item = NULL; \
     }
 
+static void ssl_init_ctx_cleanup(modssl_ctx_t *mctx)
+{
+    MODSSL_CFG_ITEM_FREE(X509_STORE_free, mctx->crl);
+
+    MODSSL_CFG_ITEM_FREE(SSL_CTX_free, mctx->ssl_ctx);
+}
+
+static void ssl_init_ctx_cleanup_proxy(modssl_ctx_t *mctx)
+{
+    ssl_init_ctx_cleanup(mctx);
+
+    if (mctx->pkp->certs) {
+        sk_X509_INFO_pop_free(mctx->pkp->certs, X509_INFO_free);
+        mctx->pkp->certs = NULL;
+    }
+}
+
+static void ssl_init_ctx_cleanup_server(modssl_ctx_t *mctx)
+{
+    int i;
+
+    ssl_init_ctx_cleanup(mctx);
+
+    for (i=0; i < SSL_AIDX_MAX; i++) {
+        MODSSL_CFG_ITEM_FREE(X509_free,
+                             mctx->pks->certs[i]);
+
+        MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
+                             mctx->pks->keys[i]);
+    }
+}
+
 apr_status_t ssl_init_ModuleKill(void *data)
 {
     SSLSrvConfigRec *sc;
@@ -1093,7 +1367,7 @@ apr_status_t ssl_init_ModuleKill(void *data)
      */
     ssl_scache_kill(base_server);
 
-    /* 
+    /*
      * Destroy the temporary keys and params
      */
     ssl_tmp_keys_free(base_server);
@@ -1103,31 +1377,13 @@ apr_status_t ssl_init_ModuleKill(void *data)
      * in the per-server configurations
      */
     for (s = base_server; s; s = s->next) {
-        int i;
         sc = mySrvConfig(s);
 
-        for (i=0; i < SSL_AIDX_MAX; i++) {
-            MODSSL_CFG_ITEM_FREE(X509_free,
-                                 sc->pPublicCert[i]);
-
-            MODSSL_CFG_ITEM_FREE(EVP_PKEY_free,
-                                 sc->pPrivateKey[i]);
-        }
+        ssl_init_ctx_cleanup_proxy(sc->proxy);
 
-        MODSSL_CFG_ITEM_FREE(X509_STORE_free,
-                             sc->pRevocationStore);
-
-        MODSSL_CFG_ITEM_FREE(SSL_CTX_free,
-                             sc->pSSLCtx);
+        ssl_init_ctx_cleanup_server(sc->server);
     }
 
-    /*
-     * Try to kill the internals of the SSL library.
-     */
-    ERR_free_strings();
-    ERR_remove_state(0);
-    EVP_cleanup();
-
     return APR_SUCCESS;
 }