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

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