32bf4d
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
32bf4d
index 570abd7..ac37936 100644
32bf4d
--- a/src/event/ngx_event_openssl.c
32bf4d
+++ b/src/event/ngx_event_openssl.c
32bf4d
@@ -232,6 +232,8 @@ ngx_ssl_init(ngx_log_t *log)
32bf4d
 ngx_int_t
32bf4d
 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
32bf4d
 {
32bf4d
+    ngx_uint_t prot = NGX_SSL_NO_PROT;
32bf4d
+
32bf4d
     ssl->ctx = SSL_CTX_new(SSLv23_method());
32bf4d
 
32bf4d
     if (ssl->ctx == NULL) {
32bf4d
@@ -296,39 +298,53 @@ ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
32bf4d
 
32bf4d
     SSL_CTX_set_options(ssl->ctx, SSL_OP_SINGLE_DH_USE);
32bf4d
 
32bf4d
-#ifdef SSL_CTRL_CLEAR_OPTIONS
32bf4d
-    /* only in 0.9.8m+ */
32bf4d
-    SSL_CTX_clear_options(ssl->ctx,
32bf4d
-                          SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
32bf4d
+    if (protocols){
32bf4d
+#ifdef SSL_OP_NO_TLSv1_3
32bf4d
+        if (protocols & NGX_SSL_TLSv1_3) {
32bf4d
+            prot = TLS1_3_VERSION;
32bf4d
+        } else
32bf4d
+#endif
32bf4d
+#ifdef SSL_OP_NO_TLSv1_2
32bf4d
+        if (protocols & NGX_SSL_TLSv1_2) {
32bf4d
+            prot =  TLS1_2_VERSION;
32bf4d
+        } else
32bf4d
+#endif
32bf4d
+#ifdef SSL_OP_NO_TLSv1_1
32bf4d
+        if (protocols & NGX_SSL_TLSv1_1) {
32bf4d
+            prot = TLS1_1_VERSION;
32bf4d
+        } else
32bf4d
 #endif
32bf4d
+        if (protocols & NGX_SSL_TLSv1) {
32bf4d
+            prot = TLS1_VERSION;
32bf4d
+        }
32bf4d
 
32bf4d
-    if (!(protocols & NGX_SSL_SSLv2)) {
32bf4d
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv2);
32bf4d
-    }
32bf4d
-    if (!(protocols & NGX_SSL_SSLv3)) {
32bf4d
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv3);
32bf4d
-    }
32bf4d
-    if (!(protocols & NGX_SSL_TLSv1)) {
32bf4d
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1);
32bf4d
-    }
32bf4d
+        if (prot == NGX_SSL_NO_PROT) {
32bf4d
+                    ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
32bf4d
+                      "No SSL protocols available [hint: ssl_protocols]");
32bf4d
+            return NGX_ERROR;
32bf4d
+        }
32bf4d
+
32bf4d
+        SSL_CTX_set_max_proto_version(ssl->ctx, prot);
32bf4d
+
32bf4d
+        /* Now, we have to scan for minimal protocol version,
32bf4d
+         *without allowing holes between min and max*/
32bf4d
+#if SSL_OP_NO_TLSv1_3
32bf4d
+        if ((prot == TLS1_3_VERSION) && (protocols & NGX_SSL_TLSv1_2)) {
32bf4d
+            prot = TLS1_2_VERSION;
32bf4d
+        }
32bf4d
+#endif
32bf4d
 #ifdef SSL_OP_NO_TLSv1_1
32bf4d
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
32bf4d
-    if (!(protocols & NGX_SSL_TLSv1_1)) {
32bf4d
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
32bf4d
-    }
32bf4d
+        if ((prot == TLS1_2_VERSION) && (protocols & NGX_SSL_TLSv1_1)) {
32bf4d
+            prot = TLS1_1_VERSION;
32bf4d
+        }
32bf4d
 #endif
32bf4d
 #ifdef SSL_OP_NO_TLSv1_2
32bf4d
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
32bf4d
-    if (!(protocols & NGX_SSL_TLSv1_2)) {
32bf4d
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
32bf4d
-    }
32bf4d
+        if ((prot == TLS1_1_VERSION) && (protocols & NGX_SSL_TLSv1)) {
32bf4d
+            prot = TLS1_VERSION;
32bf4d
+        }
32bf4d
 #endif
32bf4d
-#ifdef SSL_OP_NO_TLSv1_3
32bf4d
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_3);
32bf4d
-    if (!(protocols & NGX_SSL_TLSv1_3)) {
32bf4d
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_3);
32bf4d
+        SSL_CTX_set_min_proto_version(ssl->ctx, prot);
32bf4d
     }
32bf4d
-#endif
32bf4d
 
32bf4d
 #ifdef SSL_OP_NO_COMPRESSION
32bf4d
     SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_COMPRESSION);
32bf4d
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
32bf4d
index 623d851..6f3d7ee 100644
32bf4d
--- a/src/event/ngx_event_openssl.h
32bf4d
+++ b/src/event/ngx_event_openssl.h
32bf4d
@@ -132,6 +132,7 @@ typedef struct {
32bf4d
 #endif
32bf4d
 
32bf4d
 
32bf4d
+#define NGX_SSL_NO_PROT  0x0000
32bf4d
 #define NGX_SSL_SSLv2    0x0002
32bf4d
 #define NGX_SSL_SSLv3    0x0004
32bf4d
 #define NGX_SSL_TLSv1    0x0008
32bf4d
diff --git a/src/http/modules/ngx_http_ssl_module.c b/src/http/modules/ngx_http_ssl_module.c
32bf4d
index 7d62176..f9ef07d 100644
32bf4d
--- a/src/http/modules/ngx_http_ssl_module.c
32bf4d
+++ b/src/http/modules/ngx_http_ssl_module.c
32bf4d
@@ -590,8 +588,7 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
32bf4d
                          prev->prefer_server_ciphers, 0);
32bf4d
 
32bf4d
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
32bf4d
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
32bf4d
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
32bf4d
+                         0)
32bf4d
 
32bf4d
     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
32bf4d
                          NGX_SSL_BUFSIZE);
32bf4d
diff --git a/src/mail/ngx_mail_ssl_module.c b/src/mail/ngx_mail_ssl_module.c
32bf4d
index aebd179..50c7023 100644
32bf4d
--- a/src/mail/ngx_mail_ssl_module.c
32bf4d
+++ b/src/mail/ngx_mail_ssl_module.c
32bf4d
@@ -285,8 +283,7 @@ ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
32bf4d
                          prev->prefer_server_ciphers, 0);
32bf4d
 
32bf4d
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
32bf4d
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
32bf4d
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
32bf4d
+                         0);
32bf4d
 
32bf4d
     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
32bf4d
     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);
32bf4d
diff --git a/src/stream/ngx_stream_ssl_module.c b/src/stream/ngx_stream_ssl_module.c
32bf4d
index 3e5a1f2..c8fce57 100644
32bf4d
--- a/src/stream/ngx_stream_ssl_module.c
32bf4d
+++ b/src/stream/ngx_stream_ssl_module.c
32bf4d
@@ -554,8 +552,7 @@ ngx_stream_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
32bf4d
                          prev->prefer_server_ciphers, 0);
32bf4d
 
32bf4d
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
32bf4d
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
32bf4d
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
32bf4d
+                         0);
32bf4d
 
32bf4d
     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
32bf4d
     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);