576df0
diff --git a/modules/ssl/mod_ssl.h b/modules/ssl/mod_ssl.h
576df0
index 24a65a0..a360911 100644
576df0
--- a/modules/ssl/mod_ssl.h
576df0
+++ b/modules/ssl/mod_ssl.h
576df0
@@ -29,6 +29,7 @@
576df0
 #include "httpd.h"
576df0
 #include "http_config.h"
576df0
 #include "apr_optional.h"
576df0
+#include "apr_tables.h" /* for apr_array_header_t */
576df0
 
576df0
 /* Create a set of SSL_DECLARE(type), SSL_DECLARE_NONSTD(type) and
576df0
  * SSL_DECLARE_DATA with appropriate export and import tags for the platform
576df0
@@ -86,6 +87,34 @@ APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
576df0
 APR_DECLARE_OPTIONAL_FN(int, ssl_engine_set, (conn_rec *,
576df0
                                               ap_conf_vector_t *,
576df0
                                               int proxy, int enable));
576df0
+                                              
576df0
+/* Check for availability of new hooks */
576df0
+#define SSL_CERT_HOOKS
576df0
+#ifdef SSL_CERT_HOOKS
576df0
+
576df0
+/** Lets others add certificate and key files to the given server.
576df0
+ * For each cert a key must also be added.
576df0
+ * @param cert_file and array of const char* with the path to the certificate chain
576df0
+ * @param key_file and array of const char* with the path to the private key file
576df0
+ */
576df0
+APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, add_cert_files,
576df0
+                          (server_rec *s, apr_pool_t *p, 
576df0
+                           apr_array_header_t *cert_files,
576df0
+                           apr_array_header_t *key_files))
576df0
+
576df0
+/** In case no certificates are available for a server, this
576df0
+ * lets other modules add a fallback certificate for the time
576df0
+ * being. Regular requests against this server will be answered
576df0
+ * with a 503. 
576df0
+ * @param cert_file and array of const char* with the path to the certificate chain
576df0
+ * @param key_file and array of const char* with the path to the private key file
576df0
+ */
576df0
+APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, add_fallback_cert_files,
576df0
+                          (server_rec *s, apr_pool_t *p, 
576df0
+                           apr_array_header_t *cert_files,
576df0
+                           apr_array_header_t *key_files))
576df0
+
576df0
+#endif /* SSL_CERT_HOOKS */
576df0
 
576df0
 #endif /* __MOD_SSL_H__ */
576df0
 /** @} */
576df0
diff --git a/modules/ssl/mod_ssl_openssl.h b/modules/ssl/mod_ssl_openssl.h
576df0
index 0fa654a..d4f684f 100644
576df0
--- a/modules/ssl/mod_ssl_openssl.h
576df0
+++ b/modules/ssl/mod_ssl_openssl.h
576df0
@@ -69,5 +69,45 @@ APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, pre_handshake,
576df0
 APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, proxy_post_handshake,
576df0
                           (conn_rec *c, SSL *ssl))
576df0
 
576df0
+/** On TLS connections that do not relate to a configured virtual host,
576df0
+ * allow other modules to provide a X509 certificate and EVP_PKEY to
576df0
+ * be used on the connection. This first hook which does not
576df0
+ * return DECLINED will determine the outcome. */
576df0
+APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, answer_challenge,
576df0
+                          (conn_rec *c, const char *server_name, 
576df0
+                          X509 **pcert, EVP_PKEY **pkey))
576df0
+
576df0
+/** During post_config phase, ask around if someone wants to provide
576df0
+ * OCSP stapling status information for the given cert (with the also
576df0
+ * provided issuer certificate). The first hook which does not
576df0
+ * return DECLINED promises to take responsibility (and respond
576df0
+ * in later calls via hook ssl_get_stapling_status).
576df0
+ * If no hook takes over, mod_ssl's own stapling implementation will
576df0
+ * be applied (if configured).
576df0
+ */
576df0
+APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, init_stapling_status,
576df0
+                          (server_rec *s, apr_pool_t *p, 
576df0
+                          X509 *cert, X509 *issuer))
576df0
+
576df0
+/** Anyone answering positive to ssl_init_stapling_status for a 
576df0
+ * certificate, needs to register here and supply the actual OCSP stapling
576df0
+ * status data (OCSP_RESP) for a new connection.
576df0
+ * A hook supplying the response data must return APR_SUCCESS.
576df0
+ * The data is returned in DER encoded bytes via pder and pderlen. The
576df0
+ * returned pointer may be NULL, which indicates that data is (currently)
576df0
+ * unavailable.
576df0
+ * If DER data is returned, it MUST come from a response with
576df0
+ * status OCSP_RESPONSE_STATUS_SUCCESSFUL and V_OCSP_CERTSTATUS_GOOD
576df0
+ * or V_OCSP_CERTSTATUS_REVOKED, not V_OCSP_CERTSTATUS_UNKNOWN. This means
576df0
+ * errors in OCSP retrieval are to be handled/logged by the hook and
576df0
+ * are not done by mod_ssl.
576df0
+ * Any DER bytes returned MUST be allocated via malloc() and ownership
576df0
+ * passes to mod_ssl. Meaning, the hook must return a malloced copy of
576df0
+ * the data it has. mod_ssl (or OpenSSL) will free it. 
576df0
+ */
576df0
+APR_DECLARE_EXTERNAL_HOOK(ssl, SSL, int, get_stapling_status,
576df0
+                          (unsigned char **pder, int *pderlen, 
576df0
+                          conn_rec *c, server_rec *s, X509 *cert))
576df0
+                          
576df0
 #endif /* __MOD_SSL_OPENSSL_H__ */
576df0
 /** @} */
576df0
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
576df0
index 21e41e2..ef631c1 100644
576df0
--- a/modules/ssl/ssl_engine_init.c
576df0
+++ b/modules/ssl/ssl_engine_init.c
576df0
@@ -36,6 +36,25 @@ APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, init_server,
576df0
                                     (server_rec *s,apr_pool_t *p,int is_proxy,SSL_CTX *ctx),
576df0
                                     (s,p,is_proxy,ctx), OK, DECLINED)
576df0
 
576df0
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, add_cert_files,
576df0
+                                    (server_rec *s, apr_pool_t *p, 
576df0
+                                    apr_array_header_t *cert_files, apr_array_header_t *key_files),
576df0
+                                    (s, p, cert_files, key_files),
576df0
+                                    OK, DECLINED)
576df0
+
576df0
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, add_fallback_cert_files,
576df0
+                                    (server_rec *s, apr_pool_t *p, 
576df0
+                                    apr_array_header_t *cert_files, apr_array_header_t *key_files),
576df0
+                                    (s, p, cert_files, key_files),
576df0
+                                    OK, DECLINED)
576df0
+
576df0
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, answer_challenge,
576df0
+                                    (conn_rec *c, const char *server_name, 
576df0
+                                    X509 **pcert, EVP_PKEY **pkey),
576df0
+                                    (c, server_name, pcert, pkey),
576df0
+                                    DECLINED, DECLINED)
576df0
+
576df0
+
576df0
 /*  _________________________________________________________________
576df0
 **
576df0
 **  Module Initialization
576df0
@@ -165,18 +184,18 @@ static void ssl_add_version_components(apr_pool_t *p,
576df0
                  modver, AP_SERVER_BASEVERSION, incver);
576df0
 }
576df0
 
576df0
-/**************************************************************************************************/
576df0
-/* Managed Domains Interface */
576df0
-
576df0
-static APR_OPTIONAL_FN_TYPE(md_is_managed) *md_is_managed;
576df0
-static APR_OPTIONAL_FN_TYPE(md_get_certificate) *md_get_certificate;
576df0
-static APR_OPTIONAL_FN_TYPE(md_is_challenge) *md_is_challenge;
576df0
+/*  _________________________________________________________________
576df0
+**
576df0
+**  Let other answer special connection attempts. 
576df0
+**  Used in ACME challenge handling by mod_md.
576df0
+**  _________________________________________________________________
576df0
+*/
576df0
 
576df0
 int ssl_is_challenge(conn_rec *c, const char *servername, 
576df0
                      X509 **pcert, EVP_PKEY **pkey)
576df0
 {
576df0
-    if (md_is_challenge) {
576df0
-        return md_is_challenge(c, servername, pcert, pkey);
576df0
+    if (APR_SUCCESS == ssl_run_answer_challenge(c, servername, pcert, pkey)) {
576df0
+        return 1;
576df0
     }
576df0
     *pcert = NULL;
576df0
     *pkey = NULL;
576df0
@@ -231,16 +250,6 @@ apr_status_t ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
576df0
     ssl_config_global_create(base_server); /* just to avoid problems */
576df0
     ssl_config_global_fix(mc);
576df0
 
576df0
-    /* Initialize our interface to mod_md, if it is loaded 
576df0
-     */
576df0
-    md_is_managed = APR_RETRIEVE_OPTIONAL_FN(md_is_managed);
576df0
-    md_get_certificate = APR_RETRIEVE_OPTIONAL_FN(md_get_certificate);
576df0
-    md_is_challenge = APR_RETRIEVE_OPTIONAL_FN(md_is_challenge);
576df0
-    if (!md_is_managed || !md_get_certificate) {
576df0
-        md_is_managed = NULL;
576df0
-        md_get_certificate = NULL;
576df0
-    }
576df0
-
576df0
     /*
576df0
      *  try to fix the configuration and open the dedicated SSL
576df0
      *  logfile as early as possible
576df0
@@ -1392,8 +1401,7 @@ static apr_status_t ssl_init_server_certs(server_rec *s,
576df0
          * loaded via SSLOpenSSLConfCmd Certificate), so for 1.0.2 and
576df0
          * later, we defer to the code in ssl_init_server_ctx.
576df0
          */
576df0
-        if ((mctx->stapling_enabled == TRUE) &&
576df0
-            !ssl_stapling_init_cert(s, p, ptemp, mctx, cert)) {
576df0
+        if (!ssl_stapling_init_cert(s, p, ptemp, mctx, cert)) {
576df0
             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02567)
576df0
                          "Unable to configure certificate %s for stapling",
576df0
                          key_id);
576df0
@@ -1788,11 +1796,13 @@ static apr_status_t ssl_init_server_ctx(server_rec *s,
576df0
                                         apr_array_header_t *pphrases)
576df0
 {
576df0
     apr_status_t rv;
576df0
+    modssl_pk_server_t *pks;
576df0
 #ifdef HAVE_SSL_CONF_CMD
576df0
     ssl_ctx_param_t *param = (ssl_ctx_param_t *)sc->server->ssl_ctx_param->elts;
576df0
     SSL_CONF_CTX *cctx = sc->server->ssl_ctx_config;
576df0
     int i;
576df0
 #endif
576df0
+    int n;
576df0
 
576df0
     /*
576df0
      *  Check for problematic re-initializations
576df0
@@ -1804,50 +1814,24 @@ static apr_status_t ssl_init_server_ctx(server_rec *s,
576df0
         return APR_EGENERAL;
576df0
     }
576df0
 
576df0
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(10083)
576df0
-                 "Init: (%s) mod_md support is %s.", ssl_util_vhostid(p, s),
576df0
-                 md_is_managed? "available" : "unavailable");
576df0
-    if (md_is_managed && md_is_managed(s)) {
576df0
-        modssl_pk_server_t *const pks = sc->server->pks;
576df0
-        if (pks->cert_files->nelts > 0 || pks->key_files->nelts > 0) {
576df0
-            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(10084)
576df0
-                         "Init: (%s) You configured certificate/key files on this host, but "
576df0
-                         "is is covered by a Managed Domain. You need to remove these directives "
576df0
-                         "for the Managed Domain to take over.", ssl_util_vhostid(p, s));
576df0
-        }
576df0
-        else {
576df0
-            const char *key_file, *cert_file, *chain_file;
576df0
-            
576df0
-            key_file = cert_file = chain_file = NULL;
576df0
-            
576df0
-            if (md_get_certificate) {
576df0
-                rv = md_get_certificate(s, p, &key_file, &cert_file);
576df0
-            }
576df0
-            else {
576df0
-                rv = APR_ENOTIMPL;
576df0
-            }
576df0
-            
576df0
-            if (key_file && cert_file) {
576df0
-                ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, s, 
576df0
-                             "%s: installing key=%s, cert=%s, chain=%s", 
576df0
-                             ssl_util_vhostid(p, s), key_file, cert_file, chain_file);
576df0
-                APR_ARRAY_PUSH(pks->key_files, const char *) = key_file;
576df0
-                APR_ARRAY_PUSH(pks->cert_files, const char *) = cert_file;
576df0
-                sc->server->cert_chain = chain_file;
576df0
-            }
576df0
-            
576df0
-            if (APR_STATUS_IS_EAGAIN(rv)) {
576df0
-                /* Managed Domain not ready yet. This is not a reason to fail the config */
576df0
-                ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(10085)
576df0
-                             "Init: %s will respond with '503 Service Unavailable' for now. This "
576df0
-                             "host is part of a Managed Domain, but no SSL certificate is "
576df0
-                             "available (yet).", ssl_util_vhostid(p, s));
576df0
-                pks->service_unavailable = 1;
576df0
-            }
576df0
-            else if (rv != APR_SUCCESS) {
576df0
-                return rv;
576df0
-            }
576df0
-        }
576df0
+    /* Allow others to provide certificate files */
576df0
+    pks = sc->server->pks;
576df0
+    n = pks->cert_files->nelts;
576df0
+    ssl_run_add_cert_files(s, p, pks->cert_files, pks->key_files);
576df0
+
576df0
+    if (n < pks->cert_files->nelts) {
576df0
+        /* this overrides any old chain configuration */
576df0
+        sc->server->cert_chain = NULL;
576df0
+    }
576df0
+    
576df0
+    if (apr_is_empty_array(pks->cert_files) && !sc->server->cert_chain) {
576df0
+        ssl_run_add_fallback_cert_files(s, p, pks->cert_files, pks->key_files);
576df0
+        
576df0
+        pks->service_unavailable = 1;
576df0
+        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(10085)
576df0
+                     "Init: %s will respond with '503 Service Unavailable' for now. There "
576df0
+                     "are no SSL certificates configured and no other module contributed any.",
576df0
+                     ssl_util_vhostid(p, s));
576df0
     }
576df0
     
576df0
     if ((rv = ssl_init_ctx(s, p, ptemp, sc->server)) != APR_SUCCESS) {
576df0
@@ -1900,7 +1884,7 @@ static apr_status_t ssl_init_server_ctx(server_rec *s,
576df0
      * (late) point makes sure that we catch both certificates loaded
576df0
      * via SSLCertificateFile and SSLOpenSSLConfCmd Certificate.
576df0
      */
576df0
-    if (sc->server->stapling_enabled == TRUE) {
576df0
+    do {
576df0
         X509 *cert;
576df0
         int i = 0;
576df0
         int ret = SSL_CTX_set_current_cert(sc->server->ssl_ctx,
576df0
@@ -1917,7 +1901,7 @@ static apr_status_t ssl_init_server_ctx(server_rec *s,
576df0
                                            SSL_CERT_SET_NEXT);
576df0
             i++;
576df0
         }
576df0
-    }
576df0
+    } while(0);
576df0
 #endif
576df0
 
576df0
 #ifdef HAVE_TLS_SESSION_TICKETS
576df0
diff --git a/modules/ssl/ssl_engine_kernel.c b/modules/ssl/ssl_engine_kernel.c
576df0
index e6a9f67..a5e86e4 100644
576df0
--- a/modules/ssl/ssl_engine_kernel.c
576df0
+++ b/modules/ssl/ssl_engine_kernel.c
576df0
@@ -2303,6 +2303,37 @@ void ssl_callback_Info(const SSL *ssl, int where, int rc)
576df0
 }
576df0
 
576df0
 #ifdef HAVE_TLSEXT
576df0
+
576df0
+static apr_status_t set_challenge_creds(conn_rec *c, const char *servername,
576df0
+                                        SSL *ssl, X509 *cert, EVP_PKEY *key)
576df0
+{
576df0
+    SSLConnRec *sslcon = myConnConfig(c);
576df0
+    
576df0
+    sslcon->service_unavailable = 1;
576df0
+    if ((SSL_use_certificate(ssl, cert) < 1)) {
576df0
+        ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(10086)
576df0
+                      "Failed to configure challenge certificate %s",
576df0
+                      servername);
576df0
+        return APR_EGENERAL;
576df0
+    }
576df0
+    
576df0
+    if (!SSL_use_PrivateKey(ssl, key)) {
576df0
+        ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(10087)
576df0
+                      "error '%s' using Challenge key: %s",
576df0
+                      ERR_error_string(ERR_peek_last_error(), NULL), 
576df0
+                      servername);
576df0
+        return APR_EGENERAL;
576df0
+    }
576df0
+    
576df0
+    if (SSL_check_private_key(ssl) < 1) {
576df0
+        ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(10088)
576df0
+                      "Challenge certificate and private key %s "
576df0
+                      "do not match", servername);
576df0
+        return APR_EGENERAL;
576df0
+    }
576df0
+    return APR_SUCCESS;
576df0
+}
576df0
+  
576df0
 /*
576df0
  * This function sets the virtual host from an extended
576df0
  * client hello with a server name indication extension ("SNI", cf. RFC 6066).
576df0
@@ -2332,30 +2363,12 @@ static apr_status_t init_vhost(conn_rec *c, SSL *ssl)
576df0
                 return APR_SUCCESS;
576df0
             }
576df0
             else if (ssl_is_challenge(c, servername, &cert, &key)) {
576df0
-            
576df0
-                sslcon->service_unavailable = 1;
576df0
-                if ((SSL_use_certificate(ssl, cert) < 1)) {
576df0
-                    ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(10086)
576df0
-                                  "Failed to configure challenge certificate %s",
576df0
-                                  servername);
576df0
+                /* With ACMEv1 we can have challenge connections to a unknown domains
576df0
+                 * that need to be answered with a special certificate and will
576df0
+                 * otherwise not answer any requests. */
576df0
+                if (set_challenge_creds(c, servername, ssl, cert, key) != APR_SUCCESS) {
576df0
                     return APR_EGENERAL;
576df0
                 }
576df0
-                
576df0
-                if (!SSL_use_PrivateKey(ssl, key)) {
576df0
-                    ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(10087)
576df0
-                                  "error '%s' using Challenge key: %s",
576df0
-                                  ERR_error_string(ERR_peek_last_error(), NULL), 
576df0
-                                  servername);
576df0
-                    return APR_EGENERAL;
576df0
-                }
576df0
-                
576df0
-                if (SSL_check_private_key(ssl) < 1) {
576df0
-                    ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, c, APLOGNO(10088)
576df0
-                                  "Challenge certificate and private key %s "
576df0
-                                  "do not match", servername);
576df0
-                    return APR_EGENERAL;
576df0
-                }
576df0
-                
576df0
             }
576df0
             else {
576df0
                 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(02044)
576df0
@@ -2648,6 +2661,23 @@ int ssl_callback_alpn_select(SSL *ssl,
576df0
                           proposed);
576df0
             return SSL_TLSEXT_ERR_ALERT_FATAL;
576df0
         }
576df0
+        
576df0
+        /* protocol was switched, this could be a challenge protocol such as "acme-tls/1".
576df0
+         * For that to work, we need to allow overrides to our ssl certificate. 
576df0
+         * However, exclude challenge checks on our best known traffic protocol.
576df0
+         * (http/1.1 is the default, we never switch to it anyway.)
576df0
+         */
576df0
+        if (strcmp("h2", proposed)) {
576df0
+            const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
576df0
+            X509 *cert;
576df0
+            EVP_PKEY *key;
576df0
+            
576df0
+            if (ssl_is_challenge(c, servername, &cert, &key)) {
576df0
+                if (set_challenge_creds(c, servername, ssl, cert, key) != APR_SUCCESS) {
576df0
+                    return SSL_TLSEXT_ERR_ALERT_FATAL;
576df0
+                }
576df0
+            }
576df0
+        }
576df0
     }
576df0
 
576df0
     return SSL_TLSEXT_ERR_OK;
576df0
diff --git a/modules/ssl/ssl_util_stapling.c b/modules/ssl/ssl_util_stapling.c
576df0
index c3e2cfa..4df0a9a 100644
576df0
--- a/modules/ssl/ssl_util_stapling.c
576df0
+++ b/modules/ssl/ssl_util_stapling.c
576df0
@@ -31,12 +31,28 @@
576df0
 #include "ssl_private.h"
576df0
 #include "ap_mpm.h"
576df0
 #include "apr_thread_mutex.h"
576df0
+#include "mod_ssl_openssl.h"
576df0
+
576df0
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, init_stapling_status,
576df0
+                                    (server_rec *s, apr_pool_t *p, 
576df0
+                                     X509 *cert, X509 *issuer),
576df0
+                                     (s, p, cert, issuer),
576df0
+                                    DECLINED, DECLINED)
576df0
+
576df0
+APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, get_stapling_status,
576df0
+                                    (unsigned char **pder, int *pderlen, 
576df0
+                                     conn_rec *c, server_rec *s, X509 *cert),
576df0
+                                     (pder, pderlen, c, s, cert), 
576df0
+                                    DECLINED, DECLINED)
576df0
+                         
576df0
 
576df0
 #ifdef HAVE_OCSP_STAPLING
576df0
 
576df0
 static int stapling_cache_mutex_on(server_rec *s);
576df0
 static int stapling_cache_mutex_off(server_rec *s);
576df0
 
576df0
+static int stapling_cb(SSL *ssl, void *arg);
576df0
+
576df0
 /**
576df0
  * Maxiumum OCSP stapling response size. This should be the response for a
576df0
  * single certificate and will typically include the responder certificate chain
576df0
@@ -119,7 +135,38 @@ int ssl_stapling_init_cert(server_rec *s, apr_pool_t *p, apr_pool_t *ptemp,
576df0
     OCSP_CERTID *cid = NULL;
576df0
     STACK_OF(OPENSSL_STRING) *aia = NULL;
576df0
 
576df0
-    if ((x == NULL) || (X509_digest(x, EVP_sha1(), idx, NULL) != 1))
576df0
+    if (x == NULL)
576df0
+        return 0;
576df0
+
576df0
+    if (!(issuer = stapling_get_issuer(mctx, x))) {
576df0
+        /* In Apache pre 2.4.40, we use to come here only when mod_ssl stapling
576df0
+         * was enabled. With the new hooks, we give other modules the chance
576df0
+         * to provide stapling status. However, we do not want to log ssl errors
576df0
+         * where we did not do so in the past. */
576df0
+        if (mctx->stapling_enabled == TRUE) {
576df0
+            ssl_log_xerror(SSLLOG_MARK, APLOG_ERR, 0, ptemp, s, x, APLOGNO(02217)
576df0
+                           "ssl_stapling_init_cert: can't retrieve issuer "
576df0
+                           "certificate!");
576df0
+            return 0;
576df0
+        }
576df0
+        return 1;
576df0
+    }
576df0
+
576df0
+    if (ssl_run_init_stapling_status(s, p, x, issuer) == APR_SUCCESS) {
576df0
+        /* Someone's taken over or mod_ssl's own implementation is not enabled */
576df0
+        if (mctx->stapling_enabled != TRUE) {
576df0
+            SSL_CTX_set_tlsext_status_cb(mctx->ssl_ctx, stapling_cb);
576df0
+            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO() "OCSP stapling added via hook");
576df0
+        }
576df0
+        return 1;
576df0
+    }
576df0
+    
576df0
+    if (mctx->stapling_enabled != TRUE) {
576df0
+        /* mod_ssl's own implementation is not enabled */
576df0
+        return 1;
576df0
+    }
576df0
+    
576df0
+    if (X509_digest(x, EVP_sha1(), idx, NULL) != 1)
576df0
         return 0;
576df0
 
576df0
     cinf = apr_hash_get(stapling_certinfo, idx, sizeof(idx));
576df0
@@ -139,13 +186,6 @@ int ssl_stapling_init_cert(server_rec *s, apr_pool_t *p, apr_pool_t *ptemp,
576df0
         return 1;
576df0
     }
576df0
 
576df0
-    if (!(issuer = stapling_get_issuer(mctx, x))) {
576df0
-        ssl_log_xerror(SSLLOG_MARK, APLOG_ERR, 0, ptemp, s, x, APLOGNO(02217)
576df0
-                       "ssl_stapling_init_cert: can't retrieve issuer "
576df0
-                       "certificate!");
576df0
-        return 0;
576df0
-    }
576df0
-
576df0
     cid = OCSP_cert_to_id(NULL, x, issuer);
576df0
     X509_free(issuer);
576df0
     if (!cid) {
576df0
@@ -182,18 +222,16 @@ int ssl_stapling_init_cert(server_rec *s, apr_pool_t *p, apr_pool_t *ptemp,
576df0
                    mctx->sc->vhost_id);
576df0
 
576df0
     apr_hash_set(stapling_certinfo, cinf->idx, sizeof(cinf->idx), cinf);
576df0
-
576df0
+    
576df0
     return 1;
576df0
 }
576df0
 
576df0
-static certinfo *stapling_get_certinfo(server_rec *s, modssl_ctx_t *mctx,
576df0
+static certinfo *stapling_get_certinfo(server_rec *s, X509 *x, modssl_ctx_t *mctx,
576df0
                                         SSL *ssl)
576df0
 {
576df0
     certinfo *cinf;
576df0
-    X509 *x;
576df0
     UCHAR idx[SHA_DIGEST_LENGTH];
576df0
-    x = SSL_get_certificate(ssl);
576df0
-    if ((x == NULL) || (X509_digest(x, EVP_sha1(), idx, NULL) != 1))
576df0
+    if (X509_digest(x, EVP_sha1(), idx, NULL) != 1)
576df0
         return NULL;
576df0
     cinf = apr_hash_get(stapling_certinfo, idx, sizeof(idx));
576df0
     if (cinf && cinf->cid)
576df0
@@ -750,18 +788,34 @@ static int stapling_cb(SSL *ssl, void *arg)
576df0
     OCSP_RESPONSE *rsp = NULL;
576df0
     int rv;
576df0
     BOOL ok = TRUE;
576df0
+    X509 *x;
576df0
+    unsigned char *rspder = NULL;
576df0
+    int rspderlen;
576df0
 
576df0
+    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01951)
576df0
+                 "stapling_cb: OCSP Stapling callback called");
576df0
+
576df0
+    x = SSL_get_certificate(ssl);
576df0
+    if (x == NULL) {
576df0
+        return SSL_TLSEXT_ERR_NOACK;
576df0
+    }
576df0
+
576df0
+    if (ssl_run_get_stapling_status(&rspder, &rspderlen, conn, s, x) == APR_SUCCESS) {
576df0
+        /* a hook handles stapling for this certicate and determines the response */
576df0
+        if (rspder == NULL || rspderlen <= 0) {
576df0
+            return SSL_TLSEXT_ERR_NOACK;
576df0
+        }
576df0
+        SSL_set_tlsext_status_ocsp_resp(ssl, rspder, rspderlen);
576df0
+        return SSL_TLSEXT_ERR_OK;
576df0
+    }
576df0
+    
576df0
     if (sc->server->stapling_enabled != TRUE) {
576df0
         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01950)
576df0
                      "stapling_cb: OCSP Stapling disabled");
576df0
         return SSL_TLSEXT_ERR_NOACK;
576df0
     }
576df0
 
576df0
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01951)
576df0
-                 "stapling_cb: OCSP Stapling callback called");
576df0
-
576df0
-    cinf = stapling_get_certinfo(s, mctx, ssl);
576df0
-    if (cinf == NULL) {
576df0
+    if ((cinf = stapling_get_certinfo(s, x, mctx, ssl)) == NULL) {
576df0
         return SSL_TLSEXT_ERR_NOACK;
576df0
     }
576df0
 
576df0
@@ -864,9 +918,10 @@ apr_status_t modssl_init_stapling(server_rec *s, apr_pool_t *p,
576df0
     if (mctx->stapling_responder_timeout == UNSET) {
576df0
         mctx->stapling_responder_timeout = 10 * APR_USEC_PER_SEC;
576df0
     }
576df0
+
576df0
     SSL_CTX_set_tlsext_status_cb(ctx, stapling_cb);
576df0
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01960) "OCSP stapling initialized");
576df0
-
576df0
+    
576df0
     return APR_SUCCESS;
576df0
 }
576df0