Blame SOURCES/httpd-2.4.34-mod-md-mod-ssl-hooks.patch

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