Blame SOURCES/httpd-2.4.6-r1634529.patch

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