67ea6c
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
67ea6c
index ab3865a..e6d8bb7 100644
67ea6c
--- a/src/event/ngx_event_openssl.c
67ea6c
+++ b/src/event/ngx_event_openssl.c
67ea6c
@@ -252,6 +252,8 @@ ngx_ssl_init(ngx_log_t *log)
67ea6c
 ngx_int_t
67ea6c
 ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
67ea6c
 {
67ea6c
+    ngx_uint_t prot = NGX_SSL_NO_PROT;
67ea6c
+
67ea6c
     ssl->ctx = SSL_CTX_new(SSLv23_method());
67ea6c
 
67ea6c
     if (ssl->ctx == NULL) {
67ea6c
@@ -316,39 +318,54 @@ ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data)
67ea6c
 
67ea6c
     SSL_CTX_set_options(ssl->ctx, SSL_OP_SINGLE_DH_USE);
67ea6c
 
67ea6c
-#if OPENSSL_VERSION_NUMBER >= 0x009080dfL
67ea6c
-    /* only in 0.9.8m+ */
67ea6c
-    SSL_CTX_clear_options(ssl->ctx,
67ea6c
-                          SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
67ea6c
+    if (protocols){
67ea6c
+#ifdef SSL_OP_NO_TLSv1_3
67ea6c
+        if (protocols & NGX_SSL_TLSv1_3) {
67ea6c
+            prot = TLS1_3_VERSION;
67ea6c
+        } else
67ea6c
+#endif
67ea6c
+#ifdef SSL_OP_NO_TLSv1_2
67ea6c
+        if (protocols & NGX_SSL_TLSv1_2) {
67ea6c
+            prot =  TLS1_2_VERSION;
67ea6c
+        } else
67ea6c
+#endif
67ea6c
+#ifdef SSL_OP_NO_TLSv1_1
67ea6c
+        if (protocols & NGX_SSL_TLSv1_1) {
67ea6c
+            prot = TLS1_1_VERSION;
67ea6c
+        } else
67ea6c
+#endif
67ea6c
+        if (protocols & NGX_SSL_TLSv1) {
67ea6c
+            prot = TLS1_VERSION;
67ea6c
+        }
67ea6c
+
67ea6c
+        if (prot == NGX_SSL_NO_PROT) {
67ea6c
+                    ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
67ea6c
+                      "No SSL protocols available [hint: ssl_protocols]");
67ea6c
+            return NGX_ERROR;
67ea6c
+        }
67ea6c
+
67ea6c
+        SSL_CTX_set_max_proto_version(ssl->ctx, prot);
67ea6c
+
67ea6c
+        /* Now, we have to scan for minimal protocol version,
67ea6c
+         *without allowing holes between min and max*/
67ea6c
+#if SSL_OP_NO_TLSv1_3
67ea6c
+        if ((prot == TLS1_3_VERSION) && (protocols & NGX_SSL_TLSv1_2)) {
67ea6c
+            prot = TLS1_2_VERSION;
67ea6c
+        }
67ea6c
 #endif
67ea6c
 
67ea6c
-    if (!(protocols & NGX_SSL_SSLv2)) {
67ea6c
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv2);
67ea6c
-    }
67ea6c
-    if (!(protocols & NGX_SSL_SSLv3)) {
67ea6c
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_SSLv3);
67ea6c
-    }
67ea6c
-    if (!(protocols & NGX_SSL_TLSv1)) {
67ea6c
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1);
67ea6c
-    }
67ea6c
 #ifdef SSL_OP_NO_TLSv1_1
67ea6c
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
67ea6c
-    if (!(protocols & NGX_SSL_TLSv1_1)) {
67ea6c
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_1);
67ea6c
-    }
67ea6c
+        if ((prot == TLS1_2_VERSION) && (protocols & NGX_SSL_TLSv1_1)) {
67ea6c
+            prot = TLS1_1_VERSION;
67ea6c
+        }
67ea6c
 #endif
67ea6c
 #ifdef SSL_OP_NO_TLSv1_2
67ea6c
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
67ea6c
-    if (!(protocols & NGX_SSL_TLSv1_2)) {
67ea6c
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_2);
67ea6c
-    }
67ea6c
+        if ((prot == TLS1_1_VERSION) && (protocols & NGX_SSL_TLSv1)) {
67ea6c
+            prot = TLS1_VERSION;
67ea6c
+        }
67ea6c
 #endif
67ea6c
-#ifdef SSL_OP_NO_TLSv1_3
67ea6c
-    SSL_CTX_clear_options(ssl->ctx, SSL_OP_NO_TLSv1_3);
67ea6c
-    if (!(protocols & NGX_SSL_TLSv1_3)) {
67ea6c
-        SSL_CTX_set_options(ssl->ctx, SSL_OP_NO_TLSv1_3);
67ea6c
+        SSL_CTX_set_min_proto_version(ssl->ctx, prot);
67ea6c
     }
67ea6c
-#endif
67ea6c
 
67ea6c
 #ifdef SSL_CTX_set_min_proto_version
67ea6c
     SSL_CTX_set_min_proto_version(ssl->ctx, 0);
67ea6c
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
67ea6c
index 61da0c5..fa7ac41 100644
67ea6c
--- a/src/event/ngx_event_openssl.h
67ea6c
+++ b/src/event/ngx_event_openssl.h
67ea6c
@@ -145,6 +145,7 @@ typedef struct {
67ea6c
 #endif
67ea6c
 
67ea6c
 
67ea6c
+#define NGX_SSL_NO_PROT  0x0000
67ea6c
 #define NGX_SSL_SSLv2    0x0002
67ea6c
 #define NGX_SSL_SSLv3    0x0004
67ea6c
 #define NGX_SSL_TLSv1    0x0008
67ea6c
diff --git a/src/http/modules/ngx_http_ssl_module.c b/src/http/modules/ngx_http_ssl_module.c
67ea6c
index b3f8f47..8340a12 100644
67ea6c
--- a/src/http/modules/ngx_http_ssl_module.c
67ea6c
+++ b/src/http/modules/ngx_http_ssl_module.c
67ea6c
@@ -613,8 +613,7 @@ ngx_http_ssl_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
67ea6c
     ngx_conf_merge_value(conf->early_data, prev->early_data, 0);
67ea6c
 
67ea6c
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
67ea6c
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
67ea6c
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
67ea6c
+                         0)
67ea6c
 
67ea6c
     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
67ea6c
                          NGX_SSL_BUFSIZE);
67ea6c
diff --git a/src/mail/ngx_mail_ssl_module.c b/src/mail/ngx_mail_ssl_module.c
67ea6c
index 5544f75..3316a4b 100644
67ea6c
--- a/src/mail/ngx_mail_ssl_module.c
67ea6c
+++ b/src/mail/ngx_mail_ssl_module.c
67ea6c
@@ -291,8 +291,7 @@ ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
67ea6c
                          prev->prefer_server_ciphers, 0);
67ea6c
 
67ea6c
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
67ea6c
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
67ea6c
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
67ea6c
+                         0);
67ea6c
 
67ea6c
     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
67ea6c
     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);
67ea6c
diff --git a/src/stream/ngx_stream_ssl_module.c b/src/stream/ngx_stream_ssl_module.c
67ea6c
index ec9524e..37af046 100644
67ea6c
--- a/src/stream/ngx_stream_ssl_module.c
67ea6c
+++ b/src/stream/ngx_stream_ssl_module.c
67ea6c
@@ -625,8 +625,7 @@ ngx_stream_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
67ea6c
                          prev->prefer_server_ciphers, 0);
67ea6c
 
67ea6c
     ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
67ea6c
-                         (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1
67ea6c
-                          |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2));
67ea6c
+                         0);
67ea6c
 
67ea6c
     ngx_conf_merge_uint_value(conf->verify, prev->verify, 0);
67ea6c
     ngx_conf_merge_uint_value(conf->verify_depth, prev->verify_depth, 1);