Blame SOURCES/mod_nss-ocsp-tuning-knobs.patch

7212c2
From 8d6121731175f3a9a1fa1fbe3752763b0b48a67d Mon Sep 17 00:00:00 2001
7212c2
From: Rob Crittenden <rcritten@redhat.com>
7212c2
Date: Mon, 31 Oct 2016 15:06:36 -0400
7212c2
Subject: [PATCH] Add OCSP cache and timeout tuning knobs
7212c2
7212c2
NSS provides functions to control the timeout for connecting to
7212c2
an OCSP server and for caching the results. This includes the
7212c2
number of responses to cache and the duration to cache them.
7212c2
7212c2
Based on a patch by Jack Magne
7212c2
---
7212c2
 docs/mod_nss.html   | 42 ++++++++++++++++++++++++++
7212c2
 mod_nss.c           | 12 ++++++++
7212c2
 mod_nss.h           |  8 +++++
7212c2
 nss_engine_config.c | 86 +++++++++++++++++++++++++++++++++++++++++++----------
7212c2
 nss_engine_init.c   | 47 +++++++++++++++++++++++++++++
7212c2
 5 files changed, 179 insertions(+), 16 deletions(-)
7212c2
7212c2
diff --git a/docs/mod_nss.html b/docs/mod_nss.html
7212c2
index 65d0bd8..655d2f2 100644
7212c2
--- a/docs/mod_nss.html
7212c2
+++ b/docs/mod_nss.html
7212c2
@@ -544,6 +544,48 @@ Example
7212c2
 
7212c2
 NSSOCSP on
7212c2
 
7212c2
+<big><big>NSSOCSPTimeout</big></big>
7212c2
+
7212c2
+Configure the maximum time to wait for an OCSP response in seconds.
7212c2
+There are no constraints or special meanings for this value. The default
7212c2
+is 60 seconds.
7212c2
+
7212c2
+
7212c2
+Example
7212c2
+
7212c2
+NSSOCSPTimeout 30
7212c2
+
7212c2
+<big><big>NSSOCSPCacheSize</big></big>
7212c2
+
7212c2
+Configures the maximum number of entries in the OCSP cache. A value of
7212c2
+-1 will disable the cache completely. A value of 0 configures an unlimited
7212c2
+number of cache entries. The default is 1000.
7212c2
+
7212c2
+
7212c2
+Example
7212c2
+
7212c2
+NSSOCSPCacheSize 300
7212c2
+
7212c2
+<big><big>NSSOCSPMinCacheEntryDuration</big></big>
7212c2
+
7212c2
+Configure the minimum amount of time an OCSP response is cached in seconds.
7212c2
+The default is 3600 seconds (1 hour).
7212c2
+
7212c2
+
7212c2
+Example
7212c2
+
7212c2
+NSSOCSPMinCacheEntryDuration 30
7212c2
+
7212c2
+<big><big>NSSOCSPMaxCacheEntryDuration</big></big>
7212c2
+
7212c2
+Configure the maximum amount of time an OCSP response is cached in seconds
7212c2
+before being updated. The default is 86400 seconds (24 hours).
7212c2
+
7212c2
+
7212c2
+Example
7212c2
+
7212c2
+NSSOCSPMaxCacheEntryDuration 300
7212c2
+
7212c2
 <big><big>NSSCipherSuite
7212c2
 </big></big>
7212c2
 There are two options for configuring the available ciphers. mod_nss
7212c2
diff --git a/mod_nss.c b/mod_nss.c
7212c2
index 38098c8..dca5a73 100644
7212c2
--- a/mod_nss.c
7212c2
+++ b/mod_nss.c
7212c2
@@ -66,6 +66,18 @@ static const command_rec nss_config_cmds[] = {
7212c2
     SSL_CMD_SRV(OCSP, FLAG,
7212c2
                 "OCSP (Online Certificate Status Protocol)"
7212c2
                 "(`on', `off')")
7212c2
+    SSL_CMD_SRV(OCSPTimeout, TAKE1,
7212c2
+                "OCSP Timeout"
7212c2
+                "(`N' - Max number of seconds to wait for an OCSP response.)")
7212c2
+    SSL_CMD_SRV(OCSPCacheSize, TAKE1,
7212c2
+                "OCSP Cache size"
7212c2
+                "(`N' - number of entries -1 for no cache)")
7212c2
+    SSL_CMD_SRV(OCSPMinCacheEntryDuration, TAKE1,
7212c2
+                "OCSP Minimum time until next fetch attempt"
7212c2
+                "(`N' - Time in seconds)")
7212c2
+    SSL_CMD_SRV(OCSPMaxCacheEntryDuration, TAKE1,
7212c2
+                "OCSP Maximum time until next fetch attempt"
7212c2
+                "(`N' - Time in seconds)")
7212c2
     SSL_CMD_SRV(OCSPDefaultResponder, FLAG,
7212c2
                 "Use a default OCSP Responder"
7212c2
                 "(`on', `off')")
7212c2
diff --git a/mod_nss.h b/mod_nss.h
7212c2
index 226f7a8..8643e88 100644
7212c2
--- a/mod_nss.h
7212c2
+++ b/mod_nss.h
7212c2
@@ -325,6 +325,10 @@ struct SSLSrvConfigRec {
7212c2
     const char      *ocsp_url;
7212c2
     const char      *ocsp_name;
7212c2
     BOOL             ocsp;
7212c2
+    int              ocsp_timeout;
7212c2
+    int              ocsp_cache_size;
7212c2
+    int              ocsp_min_cache_entry_duration;
7212c2
+    int              ocsp_max_cache_entry_duration;
7212c2
     BOOL             enabled;
7212c2
     BOOL             sni;
7212c2
     BOOL             strict_sni_vhost_check;
7212c2
@@ -398,6 +402,10 @@ const char *nss_cmd_NSSSNI(cmd_parms *, void *, int);
7212c2
 const char *nss_cmd_NSSStrictSNIVHostCheck(cmd_parms *, void *, int);
7212c2
 const char *nss_cmd_NSSEngine(cmd_parms *, void *, int);
7212c2
 const char *nss_cmd_NSSOCSP(cmd_parms *, void *, int);
7212c2
+const char *nss_cmd_NSSOCSPTimeout(cmd_parms *, void *, const char *arg);
7212c2
+const char *nss_cmd_NSSOCSPCacheSize(cmd_parms *, void *, const char *arg);
7212c2
+const char *nss_cmd_NSSOCSPMinCacheEntryDuration(cmd_parms *, void *, const char *arg);
7212c2
+const char *nss_cmd_NSSOCSPMaxCacheEntryDuration(cmd_parms *, void *, const char *arg);
7212c2
 const char *nss_cmd_NSSOCSPDefaultResponder(cmd_parms *, void *, int);
7212c2
 const char *nss_cmd_NSSOCSPDefaultURL(cmd_parms *, void *dcfg, const char *arg);
7212c2
 const char *nss_cmd_NSSOCSPDefaultName(cmd_parms *, void *, const char *arg);
7212c2
diff --git a/nss_engine_config.c b/nss_engine_config.c
7212c2
index e1fbe41..597d56d 100644
7212c2
--- a/nss_engine_config.c
7212c2
+++ b/nss_engine_config.c
7212c2
@@ -129,22 +129,26 @@ static SSLSrvConfigRec *nss_config_server_new(apr_pool_t *p)
7212c2
 {
7212c2
     SSLSrvConfigRec *sc = apr_palloc(p, sizeof(*sc));
7212c2
 
7212c2
-    sc->mc                          = NULL;
7212c2
-    sc->ocsp                        = UNSET;
7212c2
-    sc->ocsp_default                = UNSET;
7212c2
-    sc->ocsp_url                    = NULL;
7212c2
-    sc->ocsp_name                   = NULL;
7212c2
-    sc->fips                        = UNSET;
7212c2
-    sc->enabled                     = UNSET;
7212c2
-    sc->sni                         = TRUE;
7212c2
-    sc->strict_sni_vhost_check      = TRUE;
7212c2
-    sc->proxy_enabled               = UNSET;
7212c2
-    sc->vhost_id                    = NULL;  /* set during module init */
7212c2
-    sc->vhost_id_len                = 0;     /* set during module init */
7212c2
-    sc->proxy                       = NULL;
7212c2
-    sc->server                      = NULL;
7212c2
-    sc->proxy_ssl_check_peer_cn     = TRUE;
7212c2
-    sc->session_tickets             = FALSE;
7212c2
+    sc->mc                            = NULL;
7212c2
+    sc->ocsp                          = UNSET;
7212c2
+    sc->ocsp_timeout                  = 60;
7212c2
+    sc->ocsp_cache_size               = 1000;
7212c2
+    sc->ocsp_min_cache_entry_duration = 1*60*60L;
7212c2
+    sc->ocsp_max_cache_entry_duration = 24*60*60L;
7212c2
+    sc->ocsp_default                  = UNSET;
7212c2
+    sc->ocsp_url                      = NULL;
7212c2
+    sc->ocsp_name                     = NULL;
7212c2
+    sc->fips                          = UNSET;
7212c2
+    sc->enabled                       = UNSET;
7212c2
+    sc->sni                           = TRUE;
7212c2
+    sc->strict_sni_vhost_check        = TRUE;
7212c2
+    sc->proxy_enabled                 = UNSET;
7212c2
+    sc->vhost_id                      = NULL;  /* set during module init */
7212c2
+    sc->vhost_id_len                  = 0;     /* set during module init */
7212c2
+    sc->proxy                         = NULL;
7212c2
+    sc->server                        = NULL;
7212c2
+    sc->proxy_ssl_check_peer_cn       = TRUE;
7212c2
+    sc->session_tickets               = FALSE;
7212c2
 
7212c2
     modnss_ctx_init_proxy(sc, p);
7212c2
 
7212c2
@@ -213,6 +217,10 @@ void *nss_config_server_merge(apr_pool_t *p, void *basev, void *addv) {
7212c2
 
7212c2
     cfgMerge(mc, NULL);
7212c2
     cfgMergeBool(ocsp);
7212c2
+    cfgMergeInt(ocsp_timeout);
7212c2
+    cfgMergeInt(ocsp_cache_size);
7212c2
+    cfgMergeInt(ocsp_min_cache_entry_duration);
7212c2
+    cfgMergeInt(ocsp_max_cache_entry_duration);
7212c2
     cfgMergeBool(ocsp_default);
7212c2
     cfgMerge(ocsp_url, NULL);
7212c2
     cfgMerge(ocsp_name, NULL);
7212c2
@@ -376,6 +384,52 @@ const char *nss_cmd_NSSOCSP(cmd_parms *cmd, void *dcfg, int flag)
7212c2
     return NULL;
7212c2
 }
7212c2
 
7212c2
+const char *nss_cmd_NSSOCSPTimeout(cmd_parms *cmd, void *dcfg, const char *arg)
7212c2
+{
7212c2
+
7212c2
+    SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
7212c2
+
7212c2
+    sc->ocsp_timeout = atoi(arg);
7212c2
+
7212c2
+    return NULL;
7212c2
+}
7212c2
+
7212c2
+const char *nss_cmd_NSSOCSPCacheSize(cmd_parms *cmd, void *dcfg, const char *arg)
7212c2
+{
7212c2
+
7212c2
+    SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
7212c2
+
7212c2
+    sc->ocsp_cache_size = atoi(arg);
7212c2
+
7212c2
+    if (sc->ocsp_cache_size < -1) {
7212c2
+        return "NSSOCSPCacheSize: must be >= -1";
7212c2
+    }
7212c2
+
7212c2
+    return NULL;
7212c2
+}
7212c2
+
7212c2
+const char *nss_cmd_NSSOCSPMinCacheEntryDuration(cmd_parms *cmd, void *dcfg,
7212c2
+                                                 const char *arg)
7212c2
+{
7212c2
+
7212c2
+    SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
7212c2
+
7212c2
+    sc->ocsp_min_cache_entry_duration = atoi(arg);
7212c2
+
7212c2
+    return NULL;
7212c2
+}
7212c2
+
7212c2
+const char *nss_cmd_NSSOCSPMaxCacheEntryDuration(cmd_parms *cmd, void *dcfg,
7212c2
+                                                 const char *arg)
7212c2
+{
7212c2
+
7212c2
+    SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
7212c2
+
7212c2
+    sc->ocsp_max_cache_entry_duration = atoi(arg);
7212c2
+
7212c2
+    return NULL;
7212c2
+}
7212c2
+
7212c2
 const char *nss_cmd_NSSOCSPDefaultResponder(cmd_parms *cmd, void *dcfg, int flag)
7212c2
 {
7212c2
     SSLSrvConfigRec *sc = mySrvConfig(cmd->server);
7212c2
diff --git a/nss_engine_init.c b/nss_engine_init.c
7212c2
index 14f86d8..2571591 100644
7212c2
--- a/nss_engine_init.c
7212c2
+++ b/nss_engine_init.c
7212c2
@@ -174,6 +174,18 @@ static void nss_init_SSLLibrary(server_rec *base_server, apr_pool_t *p)
7212c2
         }
7212c2
     }
7212c2
 
7212c2
+    if (ocspenabled) {
7212c2
+        if (sc->ocsp_min_cache_entry_duration > sc->ocsp_max_cache_entry_duration)  {
7212c2
+            ap_log_error(APLOG_MARK,APLOG_ERR, 0, base_server,
7212c2
+                "OCSP minimum cache duration must be less than the maximum.");
7212c2
+
7212c2
+            if (mc->nInitCount == 1)
7212c2
+                nss_die();
7212c2
+            else
7212c2
+                return;
7212c2
+        }
7212c2
+    }
7212c2
+
7212c2
     if (strncasecmp(mc->pCertificateDatabase, "sql:", 4) == 0)
7212c2
         dbdir = (char *)mc->pCertificateDatabase + 4;
7212c2
     else
7212c2
@@ -343,10 +355,45 @@ static void nss_init_SSLLibrary(server_rec *base_server, apr_pool_t *p)
7212c2
     }
7212c2
 
7212c2
     if (ocspenabled) {
7212c2
+        SECStatus rv;
7212c2
+
7212c2
         CERT_EnableOCSPChecking(CERT_GetDefaultCertDB());
7212c2
         ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
7212c2
             "OCSP is enabled.");
7212c2
 
7212c2
+        /* Set desired OCSP Cache Settings, values already checked. */
7212c2
+        rv = CERT_OCSPCacheSettings((PRInt32)sc->ocsp_cache_size,
7212c2
+                                    (PRUint32)sc->ocsp_min_cache_entry_duration,
7212c2
+                                    (PRUint32)sc->ocsp_max_cache_entry_duration);
7212c2
+
7212c2
+        if (rv == SECFailure) {
7212c2
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, base_server,
7212c2
+                        "Unable to set the OCSP cache settings.");
7212c2
+            nss_log_nss_error(APLOG_MARK, APLOG_ERR, base_server);
7212c2
+            if (mc->nInitCount == 1)
7212c2
+                nss_die();
7212c2
+            else
7212c2
+                return;
7212c2
+        } else {
7212c2
+            ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
7212c2
+                         "OCSP cache size %d, duration %d - %d seconds.", sc->ocsp_cache_size, sc->ocsp_min_cache_entry_duration, sc->ocsp_max_cache_entry_duration);
7212c2
+        }
7212c2
+
7212c2
+        /* Set OCSP timeout. */
7212c2
+        rv = CERT_SetOCSPTimeout((PRUint32) sc->ocsp_timeout);
7212c2
+        if (rv == SECFailure) {
7212c2
+            ap_log_error(APLOG_MARK, APLOG_ERR, 0, base_server,
7212c2
+                         "Unable to set the OCSP timeout. (this shouldn't happen.");
7212c2
+            nss_log_nss_error(APLOG_MARK, APLOG_ERR, base_server);
7212c2
+            if (mc->nInitCount == 1)
7212c2
+                nss_die();
7212c2
+            else
7212c2
+                return;
7212c2
+        } else {
7212c2
+            ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server,
7212c2
+                         "OCSP timeout set to %d.", sc->ocsp_timeout);
7212c2
+        }
7212c2
+
7212c2
         /* We ensure that ocspname and ocspurl are not NULL above. */
7212c2
         if (ocspdefault) {
7212c2
             SECStatus sv;
7212c2
-- 
7212c2
2.9.3
7212c2