ca8514
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
ca8514
index b286053..8b6c34f 100644
ca8514
--- a/modules/ssl/ssl_engine_init.c
ca8514
+++ b/modules/ssl/ssl_engine_init.c
ca8514
@@ -824,6 +824,13 @@ static apr_status_t ssl_init_ctx_protocol(server_rec *s,
ca8514
         SSL_CTX_set_keylog_callback(ctx, modssl_callback_keylog);
ca8514
     }
ca8514
 #endif
ca8514
+
ca8514
+#ifdef SSL_OP_NO_RENEGOTIATION
ca8514
+    /* For server-side SSL_CTX, disable renegotiation by default.. */
ca8514
+    if (!mctx->pkp) {
ca8514
+        SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION);
ca8514
+    }
ca8514
+#endif
ca8514
     
ca8514
     return APR_SUCCESS;
ca8514
 }
ca8514
@@ -845,6 +852,14 @@ static void ssl_init_ctx_session_cache(server_rec *s,
ca8514
     }
ca8514
 }
ca8514
 
ca8514
+#ifdef SSL_OP_NO_RENEGOTIATION
ca8514
+/* OpenSSL-level renegotiation protection. */
ca8514
+#define MODSSL_BLOCKS_RENEG (0)
ca8514
+#else
ca8514
+/* mod_ssl-level renegotiation protection. */
ca8514
+#define MODSSL_BLOCKS_RENEG (1)
ca8514
+#endif
ca8514
+
ca8514
 static void ssl_init_ctx_callbacks(server_rec *s,
ca8514
                                    apr_pool_t *p,
ca8514
                                    apr_pool_t *ptemp,
ca8514
@@ -854,7 +869,13 @@ static void ssl_init_ctx_callbacks(server_rec *s,
ca8514
 
ca8514
     SSL_CTX_set_tmp_dh_callback(ctx,  ssl_callback_TmpDH);
ca8514
 
ca8514
-    SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
ca8514
+    /* The info callback is used for debug-level tracing.  For OpenSSL
ca8514
+     * versions where SSL_OP_NO_RENEGOTIATION is not available, the
ca8514
+     * callback is also used to prevent use of client-initiated
ca8514
+     * renegotiation.  Enable it in either case. */
ca8514
+    if (APLOGdebug(s) || MODSSL_BLOCKS_RENEG) {
ca8514
+        SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
ca8514
+    }
ca8514
 
ca8514
 #ifdef HAVE_TLS_ALPN
ca8514
     SSL_CTX_set_alpn_select_cb(ctx, ssl_callback_alpn_select, NULL);
ca8514
diff --git a/modules/ssl/ssl_engine_io.c b/modules/ssl/ssl_engine_io.c
ca8514
index 836bfdb..0958135 100644
ca8514
--- a/modules/ssl/ssl_engine_io.c
ca8514
+++ b/modules/ssl/ssl_engine_io.c
ca8514
@@ -200,11 +200,13 @@ static int bio_filter_out_write(BIO *bio, const char *in, int inl)
ca8514
     apr_bucket *e;
ca8514
     int need_flush;
ca8514
 
ca8514
+#ifndef SSL_OP_NO_RENEGOTIATION
ca8514
     /* Abort early if the client has initiated a renegotiation. */
ca8514
     if (outctx->filter_ctx->config->reneg_state == RENEG_ABORT) {
ca8514
         outctx->rc = APR_ECONNABORTED;
ca8514
         return -1;
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     /* when handshaking we'll have a small number of bytes.
ca8514
      * max size SSL will pass us here is about 16k.
ca8514
@@ -458,11 +460,13 @@ static int bio_filter_in_read(BIO *bio, char *in, int inlen)
ca8514
     if (!in)
ca8514
         return 0;
ca8514
 
ca8514
+#ifndef SSL_OP_NO_RENEGOTIATION
ca8514
     /* Abort early if the client has initiated a renegotiation. */
ca8514
     if (inctx->filter_ctx->config->reneg_state == RENEG_ABORT) {
ca8514
         inctx->rc = APR_ECONNABORTED;
ca8514
         return -1;
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     BIO_clear_retry_flags(bio);
ca8514
 
ca8514
diff --git a/modules/ssl/ssl_engine_kernel.c b/modules/ssl/ssl_engine_kernel.c
ca8514
index e217b9d..21f701f 100644
ca8514
--- a/modules/ssl/ssl_engine_kernel.c
ca8514
+++ b/modules/ssl/ssl_engine_kernel.c
ca8514
@@ -992,7 +992,7 @@ static int ssl_hook_Access_classic(request_rec *r, SSLSrvConfigRec *sc, SSLDirCo
ca8514
 
ca8514
             /* Toggle the renegotiation state to allow the new
ca8514
              * handshake to proceed. */
ca8514
-            sslconn->reneg_state = RENEG_ALLOW;
ca8514
+            modssl_set_reneg_state(sslconn, RENEG_ALLOW);
ca8514
 
ca8514
             SSL_renegotiate(ssl);
ca8514
             SSL_do_handshake(ssl);
ca8514
@@ -1019,7 +1019,7 @@ static int ssl_hook_Access_classic(request_rec *r, SSLSrvConfigRec *sc, SSLDirCo
ca8514
              */
ca8514
             SSL_peek(ssl, peekbuf, 0);
ca8514
 
ca8514
-            sslconn->reneg_state = RENEG_REJECT;
ca8514
+            modssl_set_reneg_state(sslconn, RENEG_REJECT);
ca8514
 
ca8514
             if (!SSL_is_init_finished(ssl)) {
ca8514
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02261)
ca8514
@@ -1078,7 +1078,7 @@ static int ssl_hook_Access_modern(request_rec *r, SSLSrvConfigRec *sc, SSLDirCon
ca8514
         (sc->server->auth.verify_mode != SSL_CVERIFY_UNSET)) {
ca8514
         int vmode_inplace, vmode_needed;
ca8514
         int change_vmode = FALSE;
ca8514
-        int old_state, n, rc;
ca8514
+        int n, rc;
ca8514
 
ca8514
         vmode_inplace = SSL_get_verify_mode(ssl);
ca8514
         vmode_needed = SSL_VERIFY_NONE;
ca8514
@@ -1180,8 +1180,6 @@ static int ssl_hook_Access_modern(request_rec *r, SSLSrvConfigRec *sc, SSLDirCon
ca8514
                 return HTTP_FORBIDDEN;
ca8514
             }
ca8514
             
ca8514
-            old_state = sslconn->reneg_state;
ca8514
-            sslconn->reneg_state = RENEG_ALLOW;
ca8514
             modssl_set_app_data2(ssl, r);
ca8514
 
ca8514
             SSL_do_handshake(ssl);
ca8514
@@ -1191,7 +1189,6 @@ static int ssl_hook_Access_modern(request_rec *r, SSLSrvConfigRec *sc, SSLDirCon
ca8514
              */
ca8514
             SSL_peek(ssl, peekbuf, 0);
ca8514
 
ca8514
-            sslconn->reneg_state = old_state;
ca8514
             modssl_set_app_data2(ssl, NULL);
ca8514
 
ca8514
             /*
ca8514
@@ -2271,8 +2268,8 @@ static void log_tracing_state(const SSL *ssl, conn_rec *c,
ca8514
 /*
ca8514
  * This callback function is executed while OpenSSL processes the SSL
ca8514
  * handshake and does SSL record layer stuff.  It's used to trap
ca8514
- * client-initiated renegotiations, and for dumping everything to the
ca8514
- * log.
ca8514
+ * client-initiated renegotiations (where SSL_OP_NO_RENEGOTIATION is
ca8514
+ * not available), and for dumping everything to the log.
ca8514
  */
ca8514
 void ssl_callback_Info(const SSL *ssl, int where, int rc)
ca8514
 {
ca8514
@@ -2284,14 +2281,12 @@ void ssl_callback_Info(const SSL *ssl, int where, int rc)
ca8514
         return;
ca8514
     }
ca8514
 
ca8514
-    /* With TLS 1.3 this callback may be called multiple times on the first
ca8514
-     * negotiation, so the below logic to detect renegotiations can't work.
ca8514
-     * Fortunately renegotiations are forbidden starting with TLS 1.3, and
ca8514
-     * this is enforced by OpenSSL so there's nothing to be done here.
ca8514
-     */
ca8514
-#if SSL_HAVE_PROTOCOL_TLSV1_3
ca8514
-    if (SSL_version(ssl) < TLS1_3_VERSION)
ca8514
-#endif
ca8514
+#ifndef SSL_OP_NO_RENEGOTIATION
ca8514
+    /* With OpenSSL < 1.1.1 (implying TLS v1.2 or earlier), this
ca8514
+     * callback is used to block client-initiated renegotiation.  With
ca8514
+     * TLSv1.3 it is unnecessary since renegotiation is forbidden at
ca8514
+     * protocol level.  Otherwise (TLSv1.2 with OpenSSL >=1.1.1),
ca8514
+     * SSL_OP_NO_RENEGOTIATION is used to block renegotiation. */
ca8514
     {
ca8514
         SSLConnRec *sslconn;
ca8514
 
ca8514
@@ -2316,6 +2311,7 @@ void ssl_callback_Info(const SSL *ssl, int where, int rc)
ca8514
             sslconn->reneg_state = RENEG_REJECT;
ca8514
         }
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     s = mySrvFromConn(c);
ca8514
     if (s && APLOGdebug(s)) {
ca8514
diff --git a/modules/ssl/ssl_private.h b/modules/ssl/ssl_private.h
ca8514
index 2514407..f4e5ac9 100644
ca8514
--- a/modules/ssl/ssl_private.h
ca8514
+++ b/modules/ssl/ssl_private.h
ca8514
@@ -508,6 +508,16 @@ typedef struct {
ca8514
     apr_time_t     source_mtime;
ca8514
 } ssl_asn1_t;
ca8514
 
ca8514
+typedef enum {
ca8514
+    RENEG_INIT = 0, /* Before initial handshake */
ca8514
+    RENEG_REJECT,   /* After initial handshake; any client-initiated
ca8514
+                     * renegotiation should be rejected */
ca8514
+    RENEG_ALLOW,    /* A server-initiated renegotiation is taking
ca8514
+                     * place (as dictated by configuration) */
ca8514
+    RENEG_ABORT     /* Renegotiation initiated by client, abort the
ca8514
+                     * connection */
ca8514
+} modssl_reneg_state;
ca8514
+
ca8514
 /**
ca8514
  * Define the mod_ssl per-module configuration structure
ca8514
  * (i.e. the global configuration for each httpd process)
ca8514
@@ -540,18 +550,13 @@ typedef struct {
ca8514
         NON_SSL_SET_ERROR_MSG  /* Need to set the error message */
ca8514
     } non_ssl_request;
ca8514
 
ca8514
-    /* Track the handshake/renegotiation state for the connection so
ca8514
-     * that all client-initiated renegotiations can be rejected, as a
ca8514
-     * partial fix for CVE-2009-3555. */
ca8514
-    enum {
ca8514
-        RENEG_INIT = 0, /* Before initial handshake */
ca8514
-        RENEG_REJECT,   /* After initial handshake; any client-initiated
ca8514
-                         * renegotiation should be rejected */
ca8514
-        RENEG_ALLOW,    /* A server-initiated renegotiation is taking
ca8514
-                         * place (as dictated by configuration) */
ca8514
-        RENEG_ABORT     /* Renegotiation initiated by client, abort the
ca8514
-                         * connection */
ca8514
-    } reneg_state;
ca8514
+#ifndef SSL_OP_NO_RENEGOTIATION
ca8514
+    /* For OpenSSL < 1.1.1, track the handshake/renegotiation state
ca8514
+     * for the connection to block client-initiated renegotiations.
ca8514
+     * For OpenSSL >=1.1.1, the SSL_OP_NO_RENEGOTIATION flag is used in
ca8514
+     * the SSL * options state with equivalent effect. */
ca8514
+    modssl_reneg_state reneg_state;
ca8514
+#endif
ca8514
 
ca8514
     server_rec *server;
ca8514
     SSLDirConfigRec *dc;
ca8514
@@ -1130,6 +1135,9 @@ int ssl_is_challenge(conn_rec *c, const char *servername,
ca8514
  * the configured ENGINE. */
ca8514
 int modssl_is_engine_id(const char *name);
ca8514
 
ca8514
+/* Set the renegotation state for connection. */
ca8514
+void modssl_set_reneg_state(SSLConnRec *sslconn, modssl_reneg_state state);
ca8514
+
ca8514
 #endif /* SSL_PRIVATE_H */
ca8514
 /** @} */
ca8514
 
ca8514
diff --git a/modules/ssl/ssl_util_ssl.c b/modules/ssl/ssl_util_ssl.c
ca8514
index 4fa089b..3413d83 100644
ca8514
--- a/modules/ssl/ssl_util_ssl.c
ca8514
+++ b/modules/ssl/ssl_util_ssl.c
ca8514
@@ -504,3 +504,19 @@ char *modssl_SSL_SESSION_id2sz(IDCONST unsigned char *id, int idlen,
ca8514
 
ca8514
     return str;
ca8514
 }
ca8514
+
ca8514
+void modssl_set_reneg_state(SSLConnRec *sslconn, modssl_reneg_state state)
ca8514
+{
ca8514
+#ifdef SSL_OP_NO_RENEGOTIATION
ca8514
+    switch (state) {
ca8514
+    case RENEG_ALLOW:
ca8514
+        SSL_clear_options(sslconn->ssl, SSL_OP_NO_RENEGOTIATION);
ca8514
+        break;
ca8514
+    default:
ca8514
+        SSL_set_options(sslconn->ssl, SSL_OP_NO_RENEGOTIATION);
ca8514
+        break;
ca8514
+    }
ca8514
+#else
ca8514
+    sslconn->reneg_state = state;
ca8514
+#endif
ca8514
+}