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