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