cce4bc
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
cce4bc
index 9811af8..568627f 100644
cce4bc
--- a/modules/ssl/ssl_engine_init.c
cce4bc
+++ b/modules/ssl/ssl_engine_init.c
cce4bc
@@ -276,7 +276,7 @@ int ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
cce4bc
         return HTTP_INTERNAL_SERVER_ERROR;
cce4bc
     }
cce4bc
 #ifdef HAVE_OCSP_STAPLING
cce4bc
-    ssl_stapling_ex_init();
cce4bc
+    ssl_stapling_certinfo_hash_init(p);
cce4bc
 #endif
cce4bc
 
cce4bc
     /*
cce4bc
@@ -899,6 +899,8 @@ static void ssl_init_ctx(server_rec *s,
cce4bc
 }
cce4bc
 
cce4bc
 static int ssl_server_import_cert(server_rec *s,
cce4bc
+                                  apr_pool_t *p,
cce4bc
+                                  apr_pool_t *ptemp,
cce4bc
                                   modssl_ctx_t *mctx,
cce4bc
                                   const char *id,
cce4bc
                                   int idx)
cce4bc
@@ -933,7 +935,7 @@ static int ssl_server_import_cert(server_rec *s,
cce4bc
 
cce4bc
 #ifdef HAVE_OCSP_STAPLING
cce4bc
     if ((mctx->pkp == FALSE) && (mctx->stapling_enabled == TRUE)) {
cce4bc
-        if (!ssl_stapling_init_cert(s, mctx, cert)) {
cce4bc
+        if (!ssl_stapling_init_cert(s, p, ptemp, mctx, cert)) {
cce4bc
             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02235)
cce4bc
                          "Unable to configure server certificate for stapling");
cce4bc
         }
cce4bc
@@ -1081,10 +1083,10 @@ static void ssl_init_server_certs(server_rec *s,
cce4bc
     ecc_id = ssl_asn1_table_keyfmt(ptemp, vhost_id, SSL_AIDX_ECC);
cce4bc
 #endif
cce4bc
 
cce4bc
-    have_rsa = ssl_server_import_cert(s, mctx, rsa_id, SSL_AIDX_RSA);
cce4bc
-    have_dsa = ssl_server_import_cert(s, mctx, dsa_id, SSL_AIDX_DSA);
cce4bc
+    have_rsa = ssl_server_import_cert(s, p, ptemp, mctx, rsa_id, SSL_AIDX_RSA);
cce4bc
+    have_dsa = ssl_server_import_cert(s, p, ptemp, mctx, dsa_id, SSL_AIDX_DSA);
cce4bc
 #ifndef OPENSSL_NO_EC
cce4bc
-    have_ecc = ssl_server_import_cert(s, mctx, ecc_id, SSL_AIDX_ECC);
cce4bc
+    have_ecc = ssl_server_import_cert(s, p, ptemp, mctx, ecc_id, SSL_AIDX_ECC);
cce4bc
 #endif
cce4bc
 
cce4bc
     if (!(have_rsa || have_dsa
cce4bc
diff --git a/modules/ssl/ssl_private.h b/modules/ssl/ssl_private.h
cce4bc
index 80e1e8e..0cc6d3f 100644
cce4bc
--- a/modules/ssl/ssl_private.h
cce4bc
+++ b/modules/ssl/ssl_private.h
cce4bc
@@ -132,6 +132,13 @@
cce4bc
 #if OPENSSL_VERSION_NUMBER >= 0x00908080 && !defined(OPENSSL_NO_OCSP) \
cce4bc
     && !defined(OPENSSL_NO_TLSEXT)
cce4bc
 #define HAVE_OCSP_STAPLING
cce4bc
+/* backward compatibility with OpenSSL < 1.0 */
cce4bc
+#ifndef sk_OPENSSL_STRING_num 
cce4bc
+#define sk_OPENSSL_STRING_num sk_num
cce4bc
+#endif
cce4bc
+#ifndef sk_OPENSSL_STRING_value
cce4bc
+#define sk_OPENSSL_STRING_value sk_value
cce4bc
+#endif
cce4bc
 #if (OPENSSL_VERSION_NUMBER < 0x10000000)
cce4bc
 #define sk_OPENSSL_STRING_pop sk_pop
cce4bc
 #endif
cce4bc
@@ -862,10 +869,10 @@ const char *ssl_cmd_SSLStaplingErrorCacheTimeout(cmd_parms *, void *, const char
cce4bc
 const char *ssl_cmd_SSLStaplingReturnResponderErrors(cmd_parms *, void *, int);
cce4bc
 const char *ssl_cmd_SSLStaplingFakeTryLater(cmd_parms *, void *, int);
cce4bc
 const char *ssl_cmd_SSLStaplingResponderTimeout(cmd_parms *, void *, const char *);
cce4bc
-const char  *ssl_cmd_SSLStaplingForceURL(cmd_parms *, void *, const char *);
cce4bc
+const char *ssl_cmd_SSLStaplingForceURL(cmd_parms *, void *, const char *);
cce4bc
 void         modssl_init_stapling(server_rec *, apr_pool_t *, apr_pool_t *, modssl_ctx_t *);
cce4bc
-void         ssl_stapling_ex_init(void);
cce4bc
-int          ssl_stapling_init_cert(server_rec *s, modssl_ctx_t *mctx, X509 *x);
cce4bc
+void         ssl_stapling_certinfo_hash_init(apr_pool_t *);
cce4bc
+int          ssl_stapling_init_cert(server_rec *, apr_pool_t *, apr_pool_t *, modssl_ctx_t *, X509 *);
cce4bc
 #endif
cce4bc
 #ifndef OPENSSL_NO_SRP
cce4bc
 int          ssl_callback_SRPServerParams(SSL *, int *, void *);
cce4bc
diff --git a/modules/ssl/ssl_util_stapling.c b/modules/ssl/ssl_util_stapling.c
cce4bc
index 2be2c36..2387ae1 100644
cce4bc
--- a/modules/ssl/ssl_util_stapling.c
cce4bc
+++ b/modules/ssl/ssl_util_stapling.c
cce4bc
@@ -43,36 +43,32 @@
cce4bc
 
cce4bc
 #define MAX_STAPLING_DER 10240
cce4bc
 
cce4bc
-/* Cached info stored in certificate ex_info. */
cce4bc
+/* Cached info stored in the global stapling_certinfo hash. */
cce4bc
 typedef struct {
cce4bc
-    /* Index in session cache SHA1 hash of certificate */
cce4bc
-    UCHAR idx[20];
cce4bc
-    /* Certificate ID for OCSP requests or NULL if ID cannot be determined */
cce4bc
+    /* Index in session cache (SHA-1 digest of DER encoded certificate) */
cce4bc
+    UCHAR idx[SHA_DIGEST_LENGTH];
cce4bc
+    /* Certificate ID for OCSP request */
cce4bc
     OCSP_CERTID *cid;
cce4bc
-    /* Responder details */
cce4bc
+    /* URI of the OCSP responder */
cce4bc
     char *uri;
cce4bc
 } certinfo;
cce4bc
 
cce4bc
-static void certinfo_free(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
cce4bc
-                                        int idx, long argl, void *argp)
cce4bc
+static apr_status_t ssl_stapling_certid_free(void *data)
cce4bc
 {
cce4bc
-    certinfo *cinf = ptr;
cce4bc
+    OCSP_CERTID *cid = data;
cce4bc
 
cce4bc
-    if (!cinf)
cce4bc
-        return;
cce4bc
-    if (cinf->uri)
cce4bc
-        OPENSSL_free(cinf->uri);
cce4bc
-    OPENSSL_free(cinf);
cce4bc
+    if (cid) {
cce4bc
+        OCSP_CERTID_free(cid);
cce4bc
+    }
cce4bc
+
cce4bc
+    return APR_SUCCESS;
cce4bc
 }
cce4bc
 
cce4bc
-static int stapling_ex_idx = -1;
cce4bc
+static apr_hash_t *stapling_certinfo;
cce4bc
 
cce4bc
-void ssl_stapling_ex_init(void)
cce4bc
+void ssl_stapling_certinfo_hash_init(apr_pool_t *p)
cce4bc
 {
cce4bc
-    if (stapling_ex_idx != -1)
cce4bc
-        return;
cce4bc
-    stapling_ex_idx = X509_get_ex_new_index(0, "X509 cached OCSP info", 0, 0,
cce4bc
-                                            certinfo_free);
cce4bc
+    stapling_certinfo = apr_hash_make(p);
cce4bc
 }
cce4bc
 
cce4bc
 static X509 *stapling_get_issuer(modssl_ctx_t *mctx, X509 *x)
cce4bc
@@ -106,70 +102,97 @@ static X509 *stapling_get_issuer(modssl_ctx_t *mctx, X509 *x)
cce4bc
 
cce4bc
 }
cce4bc
 
cce4bc
-int ssl_stapling_init_cert(server_rec *s, modssl_ctx_t *mctx, X509 *x)
cce4bc
+int ssl_stapling_init_cert(server_rec *s, apr_pool_t *p, apr_pool_t *ptemp,
cce4bc
+                           modssl_ctx_t *mctx, X509 *x)
cce4bc
 {
cce4bc
-    certinfo *cinf;
cce4bc
+    UCHAR idx[SHA_DIGEST_LENGTH];
cce4bc
+    certinfo *cinf = NULL;
cce4bc
     X509 *issuer = NULL;
cce4bc
+    OCSP_CERTID *cid = NULL;
cce4bc
     STACK_OF(OPENSSL_STRING) *aia = NULL;
cce4bc
 
cce4bc
-    if (x == NULL)
cce4bc
+    if ((x == NULL) || (X509_digest(x, EVP_sha1(), idx, NULL) != 1))
cce4bc
         return 0;
cce4bc
-    cinf  = X509_get_ex_data(x, stapling_ex_idx);
cce4bc
+
cce4bc
+    cinf = apr_hash_get(stapling_certinfo, idx, sizeof(idx));
cce4bc
     if (cinf) {
cce4bc
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02215)
cce4bc
-                     "ssl_stapling_init_cert: certificate already initialized!");
cce4bc
-        return 0;
cce4bc
-    }
cce4bc
-    cinf = OPENSSL_malloc(sizeof(certinfo));
cce4bc
-    if (!cinf) {
cce4bc
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02216)
cce4bc
-                     "ssl_stapling_init_cert: error allocating memory!");
cce4bc
-        return 0;
cce4bc
+        /*
cce4bc
+        * We already parsed the certificate, and no OCSP URI was found.
cce4bc
+        * The certificate might be used for multiple vhosts, though,
cce4bc
+        * so we check for a ForceURL for this vhost.
cce4bc
+        */
cce4bc
+        if (!cinf->uri && !mctx->stapling_force_url) {
cce4bc
+            ssl_log_xerror(SSLLOG_MARK, APLOG_ERR, 0, ptemp, s, x,
cce4bc
+                           APLOGNO(02814) "ssl_stapling_init_cert: no OCSP URI "
cce4bc
+                           "in certificate and no SSLStaplingForceURL "
cce4bc
+                           "configured for server %s", mctx->sc->vhost_id);
cce4bc
+            return 0;
cce4bc
+        }
cce4bc
+        return 1;
cce4bc
     }
cce4bc
-    cinf->cid = NULL;
cce4bc
-    cinf->uri = NULL;
cce4bc
-    X509_set_ex_data(x, stapling_ex_idx, cinf);
cce4bc
-
cce4bc
-    issuer = stapling_get_issuer(mctx, x);
cce4bc
 
cce4bc
-    if (issuer == NULL) {
cce4bc
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02217)
cce4bc
-                     "ssl_stapling_init_cert: Can't retrieve issuer certificate!");
cce4bc
+    if (!(issuer = stapling_get_issuer(mctx, x))) {
cce4bc
+        ssl_log_xerror(SSLLOG_MARK, APLOG_ERR, 0, ptemp, s, x, APLOGNO(02217)
cce4bc
+                       "ssl_stapling_init_cert: can't retrieve issuer "
cce4bc
+                       "certificate!");
cce4bc
         return 0;
cce4bc
     }
cce4bc
 
cce4bc
-    cinf->cid = OCSP_cert_to_id(NULL, x, issuer);
cce4bc
+    cid = OCSP_cert_to_id(NULL, x, issuer);
cce4bc
     X509_free(issuer);
cce4bc
-    if (!cinf->cid)
cce4bc
+    if (!cid) {
cce4bc
+        ssl_log_xerror(SSLLOG_MARK, APLOG_ERR, 0, ptemp, s, x, APLOGNO(02815)
cce4bc
+                       "ssl_stapling_init_cert: can't create CertID "
cce4bc
+                       "for OCSP request");
cce4bc
         return 0;
cce4bc
-    X509_digest(x, EVP_sha1(), cinf->idx, NULL);
cce4bc
+    }
cce4bc
 
cce4bc
     aia = X509_get1_ocsp(x);
cce4bc
-    if (aia) {
cce4bc
-        cinf->uri = sk_OPENSSL_STRING_pop(aia);
cce4bc
-        X509_email_free(aia);
cce4bc
-    }
cce4bc
-    if (!cinf->uri && !mctx->stapling_force_url) {
cce4bc
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02218)
cce4bc
-                     "ssl_stapling_init_cert: no responder URL");
cce4bc
+
cce4bc
+    if (!aia && !mctx->stapling_force_url) {
cce4bc
+        OCSP_CERTID_free(cid);
cce4bc
+        ssl_log_xerror(SSLLOG_MARK, APLOG_ERR, 0, ptemp, s, x,
cce4bc
+                       APLOGNO(02218) "ssl_stapling_init_cert: no OCSP URI "
cce4bc
+                       "in certificate and no SSLStaplingForceURL set");
cce4bc
         return 0;
cce4bc
     }
cce4bc
+
cce4bc
+    /* At this point, we have determined that there's something to store */
cce4bc
+    cinf = apr_pcalloc(p, sizeof(certinfo));
cce4bc
+    memcpy (cinf->idx, idx, sizeof(idx));
cce4bc
+    cinf->cid = cid;
cce4bc
+    /* make sure cid is also freed at pool cleanup */
cce4bc
+    apr_pool_cleanup_register(p, cid, ssl_stapling_certid_free,
cce4bc
+                              apr_pool_cleanup_null);
cce4bc
+    if (aia) {
cce4bc
+       /* allocate uri from the pconf pool */
cce4bc
+       cinf->uri = apr_pstrdup(p, sk_OPENSSL_STRING_value(aia, 0));
cce4bc
+       X509_email_free(aia);
cce4bc
+    }
cce4bc
+
cce4bc
+    ssl_log_xerror(SSLLOG_MARK, APLOG_TRACE1, 0, ptemp, s, x,
cce4bc
+                   "ssl_stapling_init_cert: storing certinfo for server %s",
cce4bc
+                   mctx->sc->vhost_id);
cce4bc
+
cce4bc
+    apr_hash_set(stapling_certinfo, cinf->idx, sizeof(cinf->idx), cinf);
cce4bc
+
cce4bc
     return 1;
cce4bc
 }
cce4bc
 
cce4bc
-static certinfo *stapling_get_cert_info(server_rec *s, modssl_ctx_t *mctx,
cce4bc
+static certinfo *stapling_get_certinfo(server_rec *s, modssl_ctx_t *mctx,
cce4bc
                                         SSL *ssl)
cce4bc
 {
cce4bc
     certinfo *cinf;
cce4bc
     X509 *x;
cce4bc
+    UCHAR idx[SHA_DIGEST_LENGTH];
cce4bc
     x = SSL_get_certificate(ssl);
cce4bc
-    if (x == NULL)
cce4bc
+    if ((x == NULL) || (X509_digest(x, EVP_sha1(), idx, NULL) != 1))
cce4bc
         return NULL;
cce4bc
-    cinf = X509_get_ex_data(x, stapling_ex_idx);
cce4bc
+    cinf = apr_hash_get(stapling_certinfo, idx, sizeof(idx));
cce4bc
     if (cinf && cinf->cid)
cce4bc
         return cinf;
cce4bc
     ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01926)
cce4bc
-                 "stapling_get_cert_info: stapling not supported for certificate");
cce4bc
+                 "stapling_get_certinfo: stapling not supported for certificate");
cce4bc
     return NULL;
cce4bc
 }
cce4bc
 
cce4bc
@@ -585,7 +608,7 @@ static int stapling_cb(SSL *ssl, void *arg)
cce4bc
     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01951)
cce4bc
                  "stapling_cb: OCSP Stapling callback called");
cce4bc
 
cce4bc
-    cinf = stapling_get_cert_info(s, mctx, ssl);
cce4bc
+    cinf = stapling_get_certinfo(s, mctx, ssl);
cce4bc
     if (cinf == NULL) {
cce4bc
         return SSL_TLSEXT_ERR_NOACK;
cce4bc
     }