ab020e
From cc7b92c61a2833ff9dc2b4dfba4591966769da78 Mon Sep 17 00:00:00 2001
ab020e
From: =?UTF-8?q?Lubo=C5=A1=20Uhliarik?= <luhliari@redhat.com>
ab020e
Date: Tue, 21 Jun 2022 13:55:04 +0200
ab020e
Subject: [PATCH] Enable TLSv1.3 by default in nginx
ab020e
ab020e
---
ab020e
 src/event/ngx_event_openssl.c          | 77 ++++++++++++++------------
ab020e
 src/event/ngx_event_openssl.h          |  1 +
ab020e
 src/http/modules/ngx_http_ssl_module.c |  3 +-
ab020e
 src/mail/ngx_mail_ssl_module.c         |  3 +-
ab020e
 src/stream/ngx_stream_ssl_module.c     |  3 +-
ab020e
 5 files changed, 46 insertions(+), 41 deletions(-)
ab020e
ab020e
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
ab020e
index f813458..2e6a6c0 100644
ab020e
--- a/src/event/ngx_event_openssl.c
ab020e
+++ b/src/event/ngx_event_openssl.c
ab020e
@@ -258,6 +258,8 @@ ngx_ssl_init(ngx_log_t *log)
ab020e
 ngx_int_t
ab020e
 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
ab020e
 {
ab020e
+    ngx_uint_t prot = NGX_SSL_NO_PROT;
ab020e
+
ab020e
     ssl->ctx = SSL_CTX_new(SSLv23_method());
ab020e
 
ab020e
     if (ssl->ctx == NULL) {
ab020e
@@ -322,49 +324,54 @@ ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
ab020e
 
ab020e
     SSL_CTX_set_options(ssl->ctx, SSL_OP_SINGLE_DH_USE);
ab020e
 
ab020e
-#if OPENSSL_VERSION_NUMBER >= 0x009080dfL
ab020e
-    /* only in 0.9.8m+ */
ab020e
-    SSL_CTX_clear_options(ssl->ctx,
ab020e
-                          SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
ab020e
-#endif
ab020e
-
ab020e
-    if (!(protocols & NGX_SSL_SSLv2)) {
ab020e
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv2);
ab020e
-    }
ab020e
-    if (!(protocols & NGX_SSL_SSLv3)) {
ab020e
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv3);
ab020e
-    }
ab020e
-    if (!(protocols & NGX_SSL_TLSv1)) {
ab020e
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1);
ab020e
-    }
ab020e
-#ifdef SSL_OP_NO_TLSv1_1
ab020e
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
ab020e
-    if (!(protocols & NGX_SSL_TLSv1_1)) {
ab020e
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
ab020e
-    }
ab020e
+    if (protocols){
ab020e
+#ifdef SSL_OP_NO_TLSv1_3
ab020e
+        if (protocols & NGX_SSL_TLSv1_3) {
ab020e
+            prot = TLS1_3_VERSION;
ab020e
+        } else
ab020e
 #endif
ab020e
 #ifdef SSL_OP_NO_TLSv1_2
ab020e
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
ab020e
-    if (!(protocols & NGX_SSL_TLSv1_2)) {
ab020e
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
ab020e
-    }
ab020e
+        if (protocols & NGX_SSL_TLSv1_2) {
ab020e
+            prot =  TLS1_2_VERSION;
ab020e
+        } else
ab020e
 #endif
ab020e
-#ifdef SSL_OP_NO_TLSv1_3
ab020e
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_3);
ab020e
-    if (!(protocols & NGX_SSL_TLSv1_3)) {
ab020e
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_3);
ab020e
-    }
ab020e
+#ifdef SSL_OP_NO_TLSv1_1
ab020e
+        if (protocols & NGX_SSL_TLSv1_1) {
ab020e
+            prot = TLS1_1_VERSION;
ab020e
+        } else
ab020e
 #endif
ab020e
+        if (protocols & NGX_SSL_TLSv1) {
ab020e
+            prot = TLS1_VERSION;
ab020e
+        }
ab020e
+
ab020e
+        if (prot == NGX_SSL_NO_PROT) {
ab020e
+                    ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
ab020e
+                      "No SSL protocols available [hint: ssl_protocols]");
ab020e
+            return NGX_ERROR;
ab020e
+        }
ab020e
 
ab020e
-#ifdef SSL_CTX_set_min_proto_version
ab020e
-    SSL_CTX_set_min_proto_version(ssl->ctx, 0);
ab020e
-    SSL_CTX_set_max_proto_version(ssl->ctx, TLS1_2_VERSION);
ab020e
+        SSL_CTX_set_max_proto_version(ssl->ctx, prot);
ab020e
+
ab020e
+        /* Now, we have to scan for minimal protocol version,
ab020e
+         *without allowing holes between min and max*/
ab020e
+#ifdef SSL_OP_NO_TLSv1_3
ab020e
+        if ((prot == TLS1_3_VERSION) && (protocols & NGX_SSL_TLSv1_2)) {
ab020e
+            prot = TLS1_2_VERSION;
ab020e
+        }
ab020e
 #endif
ab020e
 
ab020e
-#ifdef TLS1_3_VERSION
ab020e
-    SSL_CTX_set_min_proto_version(ssl->ctx, 0);
ab020e
-    SSL_CTX_set_max_proto_version(ssl->ctx, TLS1_3_VERSION);
ab020e
+#ifdef SSL_OP_NO_TLSv1_1
ab020e
+        if ((prot == TLS1_2_VERSION) && (protocols & NGX_SSL_TLSv1_1)) {
ab020e
+            prot = TLS1_1_VERSION;
ab020e
+        }
ab020e
+#endif
ab020e
+#ifdef SSL_OP_NO_TLSv1_2
ab020e
+        if ((prot == TLS1_1_VERSION) && (protocols & NGX_SSL_TLSv1)) {
ab020e
+            prot = TLS1_VERSION;
ab020e
+        }
ab020e
 #endif
ab020e
+        SSL_CTX_set_min_proto_version(ssl->ctx, prot);
ab020e
+    }
ab020e
 
ab020e
 #ifdef SSL_OP_NO_COMPRESSION
ab020e
     SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_COMPRESSION);
ab020e
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
ab020e
index 329760d..5cee113 100644
ab020e
--- a/src/event/ngx_event_openssl.h
ab020e
+++ b/src/event/ngx_event_openssl.h
ab020e
@@ -152,6 +152,7 @@ typedef struct {
ab020e
 #endif
ab020e
 
ab020e
 
ab020e
+#define NGX_SSL_NO_PROT  0x0000
ab020e
 #define NGX_SSL_SSLv2    0x0002
ab020e
 #define NGX_SSL_SSLv3    0x0004
ab020e
 #define NGX_SSL_TLSv1    0x0008
ab020e
diff --git a/src/http/modules/ngx_http_ssl_module.c b/src/http/modules/ngx_http_ssl_module.c
ab020e
index a47d696..94f30db 100644
ab020e
--- a/src/http/modules/ngx_http_ssl_module.c
ab020e
+++ b/src/http/modules/ngx_http_ssl_module.c
ab020e
@@ -671,8 +671,7 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
ab020e
     ngx_conf_merge_value(conf->reject_handshake, prev->reject_handshake, 0);
ab020e
 
ab020e
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
ab020e
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
ab020e
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
ab020e
+                         0)
ab020e
 
ab020e
     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
ab020e
                          NGX_SSL_BUFSIZE);
ab020e
diff --git a/src/mail/ngx_mail_ssl_module.c b/src/mail/ngx_mail_ssl_module.c
ab020e
index 7eae83e..8328560 100644
ab020e
--- a/src/mail/ngx_mail_ssl_module.c
ab020e
+++ b/src/mail/ngx_mail_ssl_module.c
ab020e
@@ -306,8 +306,7 @@ ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ab020e
                          prev->prefer_server_ciphers, 0);
ab020e
 
ab020e
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
ab020e
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
ab020e
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
ab020e
+                         0);
ab020e
 
ab020e
     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
ab020e
     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);
ab020e
diff --git a/src/stream/ngx_stream_ssl_module.c b/src/stream/ngx_stream_ssl_module.c
ab020e
index d8c0471..cef590d 100644
ab020e
--- a/src/stream/ngx_stream_ssl_module.c
ab020e
+++ b/src/stream/ngx_stream_ssl_module.c
ab020e
@@ -641,8 +641,7 @@ ngx_stream_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ab020e
                          prev->prefer_server_ciphers, 0);
ab020e
 
ab020e
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
ab020e
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
ab020e
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
ab020e
+                         0);
ab020e
 
ab020e
     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
ab020e
     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);
ab020e
-- 
ab020e
2.31.1
ab020e