6953a2
diff --git a/docs/manual/mod/mod_ssl.html.en b/docs/manual/mod/mod_ssl.html.en
6953a2
index b543150..ab72d4f 100644
6953a2
--- a/docs/manual/mod/mod_ssl.html.en
6953a2
+++ b/docs/manual/mod/mod_ssl.html.en
6953a2
@@ -1524,6 +1524,32 @@ The available (case-insensitive) protocols are:

6953a2
 

Example

SSLProtocol TLSv1
6953a2
 
6953a2
 
6953a2
+
6953a2
+

SSLProtocol for name-based virtual hosts

6953a2
+

6953a2
+Before OpenSSL 1.1.1, even though the Server Name Indication (SNI) allowed to
6953a2
+determine the targeted virtual host early in the TLS handshake, it was not
6953a2
+possible to switch the TLS protocol version of the connection at this point,
6953a2
+and thus the SSLProtocol negotiated was always based off
6953a2
+the one of the base virtual host (first virtual host declared on the
6953a2
+listening IP:port of the connection).
6953a2
+

6953a2
+

6953a2
+Beginning with Apache HTTP server version 2.4.42, when built/linked against
6953a2
+OpenSSL 1.1.1 or later, and when the SNI is provided by the client in the TLS
6953a2
+handshake, the SSLProtocol of each (name-based) virtual
6953a2
+host can and will be honored.
6953a2
+

6953a2
+

6953a2
+For compatibility with previous versions, if no
6953a2
+SSLProtocol is configured in a name-based virtual host,
6953a2
+the one from the base virtual host still applies, unless
6953a2
+SSLProtocol is configured globally in which case the
6953a2
+global value applies (this latter exception is more sensible than compatible,
6953a2
+though).
6953a2
+

6953a2
+
6953a2
+
6953a2
 
6953a2
 
top
6953a2
 
6953a2
diff --git a/modules/ssl/ssl_engine_config.c b/modules/ssl/ssl_engine_config.c
6953a2
index 0c4bf1f..ca5f702 100644
6953a2
--- a/modules/ssl/ssl_engine_config.c
6953a2
+++ b/modules/ssl/ssl_engine_config.c
6953a2
@@ -269,6 +269,7 @@ static void modssl_ctx_cfg_merge(apr_pool_t *p,
6953a2
         mrg->protocol_set = 1;
6953a2
     }
6953a2
     else {
6953a2
+        mrg->protocol_set = base->protocol_set;
6953a2
         mrg->protocol = base->protocol;
6953a2
     }
6953a2
 
6953a2
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
6953a2
index 31062bc..70d151e 100644
6953a2
--- a/modules/ssl/ssl_engine_init.c
6953a2
+++ b/modules/ssl/ssl_engine_init.c
6953a2
@@ -520,7 +520,9 @@ static apr_status_t ssl_init_ctx_tls_extensions(server_rec *s,
6953a2
                  "Configuring TLS extension handling");
6953a2
 
6953a2
     /*
6953a2
-     * Server name indication (SNI)
6953a2
+     * The Server Name Indication (SNI) provided by the ClientHello can be
6953a2
+     * used to select the right (name-based-)vhost and its SSL configuration
6953a2
+     * before the handshake takes place.
6953a2
      */
6953a2
     if (!SSL_CTX_set_tlsext_servername_callback(mctx->ssl_ctx,
6953a2
                           ssl_callback_ServerNameIndication) ||
6953a2
@@ -532,6 +534,16 @@ static apr_status_t ssl_init_ctx_tls_extensions(server_rec *s,
6953a2
         return ssl_die(s);
6953a2
     }
6953a2
 
6953a2
+#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6953a2
+    /*
6953a2
+     * The ClientHello callback also allows to retrieve the SNI, but since it
6953a2
+     * runs at the earliest possible connection stage we can even set the TLS
6953a2
+     * protocol version(s) according to the selected (name-based-)vhost, which
6953a2
+     * is not possible at the SNI callback stage (due to OpenSSL internals).
6953a2
+     */
6953a2
+    SSL_CTX_set_client_hello_cb(mctx->ssl_ctx, ssl_callback_ClientHello, NULL);
6953a2
+#endif
6953a2
+
6953a2
 #ifdef HAVE_OCSP_STAPLING
6953a2
     /*
6953a2
      * OCSP Stapling support, status_request extension
6953a2
@@ -708,7 +720,7 @@ static apr_status_t ssl_init_ctx_protocol(server_rec *s,
6953a2
 #else /* #if OPENSSL_VERSION_NUMBER < 0x10100000L */
6953a2
     /* We first determine the maximum protocol version we should provide */
6953a2
 #if SSL_HAVE_PROTOCOL_TLSV1_3
6953a2
-    if (SSL_HAVE_PROTOCOL_TLSV1_3 && (protocol & SSL_PROTOCOL_TLSV1_3)) {
6953a2
+    if (protocol & SSL_PROTOCOL_TLSV1_3) {
6953a2
         prot = TLS1_3_VERSION;
6953a2
     } else  
6953a2
 #endif
6953a2
diff --git a/modules/ssl/ssl_engine_kernel.c b/modules/ssl/ssl_engine_kernel.c
6953a2
index 8b44674..7313a55 100644
6953a2
--- a/modules/ssl/ssl_engine_kernel.c
6953a2
+++ b/modules/ssl/ssl_engine_kernel.c
6953a2
@@ -2357,28 +2357,31 @@ static apr_status_t set_challenge_creds(conn_rec *c, const char *servername,
6953a2
  * This function sets the virtual host from an extended
6953a2
  * client hello with a server name indication extension ("SNI", cf. RFC 6066).
6953a2
  */
6953a2
-static apr_status_t init_vhost(conn_rec *c, SSL *ssl)
6953a2
+static apr_status_t init_vhost(conn_rec *c, SSL *ssl, const char *servername)
6953a2
 {
6953a2
-    const char *servername;
6953a2
     X509 *cert;
6953a2
     EVP_PKEY *key;
6953a2
     
6953a2
     if (c) {
6953a2
         SSLConnRec *sslcon = myConnConfig(c);
6953a2
-        
6953a2
-        if (sslcon->server != c->base_server) {
6953a2
-            /* already found the vhost */
6953a2
-            return APR_SUCCESS;
6953a2
+
6953a2
+        if (sslcon->vhost_found) {
6953a2
+            /* already found the vhost? */
6953a2
+            return sslcon->vhost_found > 0 ? APR_SUCCESS : APR_NOTFOUND;
6953a2
         }
6953a2
+        sslcon->vhost_found = -1;
6953a2
         
6953a2
-        servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
6953a2
+        if (!servername) {
6953a2
+            servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
6953a2
+        }
6953a2
         if (servername) {
6953a2
             if (ap_vhost_iterate_given_conn(c, ssl_find_vhost,
6953a2
                                             (void *)servername)) {
6953a2
                 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, APLOGNO(02043)
6953a2
                               "SSL virtual host for servername %s found",
6953a2
                               servername);
6953a2
-                
6953a2
+
6953a2
+                sslcon->vhost_found = +1;
6953a2
                 return APR_SUCCESS;
6953a2
             }
6953a2
             else if (ssl_is_challenge(c, servername, &cert, &key)) {
6953a2
@@ -2428,11 +2431,72 @@ static apr_status_t init_vhost(conn_rec *c, SSL *ssl)
6953a2
 int ssl_callback_ServerNameIndication(SSL *ssl, int *al, modssl_ctx_t *mctx)
6953a2
 {
6953a2
     conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
6953a2
-    apr_status_t status = init_vhost(c, ssl);
6953a2
+    apr_status_t status = init_vhost(c, ssl, NULL);
6953a2
     
6953a2
     return (status == APR_SUCCESS)? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_NOACK;
6953a2
 }
6953a2
 
6953a2
+#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6953a2
+/*
6953a2
+ * This callback function is called when the ClientHello is received.
6953a2
+ */
6953a2
+int ssl_callback_ClientHello(SSL *ssl, int *al, void *arg)
6953a2
+{
6953a2
+    char *servername = NULL;
6953a2
+    conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
6953a2
+    const unsigned char *pos;
6953a2
+    size_t len, remaining;
6953a2
+    (void)arg;
6953a2
+ 
6953a2
+    /* We can't use SSL_get_servername() at this earliest OpenSSL connection
6953a2
+     * stage, and there is no SSL_client_hello_get0_servername() provided as
6953a2
+     * of OpenSSL 1.1.1. So the code below, that extracts the SNI from the
6953a2
+     * ClientHello's TLS extensions, is taken from some test code in OpenSSL,
6953a2
+     * i.e. client_hello_select_server_ctx() in "test/handshake_helper.c".
6953a2
+     */
6953a2
+
6953a2
+    /*
6953a2
+     * The server_name extension was given too much extensibility when it
6953a2
+     * was written, so parsing the normal case is a bit complex.
6953a2
+     */
6953a2
+    if (!SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &pos,
6953a2
+                                   &remaining)
6953a2
+            || remaining <= 2) 
6953a2
+        goto give_up;
6953a2
+
6953a2
+    /* Extract the length of the supplied list of names. */
6953a2
+    len = (*(pos++) << 8);
6953a2
+    len += *(pos++);
6953a2
+    if (len + 2 != remaining)
6953a2
+        goto give_up;
6953a2
+    remaining = len;
6953a2
+
6953a2
+    /*
6953a2
+     * The list in practice only has a single element, so we only consider
6953a2
+     * the first one.
6953a2
+     */
6953a2
+    if (remaining <= 3 || *pos++ != TLSEXT_NAMETYPE_host_name)
6953a2
+        goto give_up;
6953a2
+    remaining--;
6953a2
+
6953a2
+    /* Now we can finally pull out the byte array with the actual hostname. */
6953a2
+    len = (*(pos++) << 8);
6953a2
+    len += *(pos++);
6953a2
+    if (len + 2 != remaining)
6953a2
+        goto give_up;
6953a2
+
6953a2
+    /* Use the SNI to switch to the relevant vhost, should it differ from
6953a2
+     * c->base_server.
6953a2
+     */
6953a2
+    servername = apr_pstrmemdup(c->pool, (const char *)pos, len);
6953a2
+
6953a2
+give_up:
6953a2
+    init_vhost(c, ssl, servername);
6953a2
+    return SSL_CLIENT_HELLO_SUCCESS;
6953a2
+}
6953a2
+#endif /* OPENSSL_VERSION_NUMBER < 0x10101000L */
6953a2
+
6953a2
+
6953a2
 /*
6953a2
  * Find a (name-based) SSL virtual host where either the ServerName
6953a2
  * or one of the ServerAliases matches the supplied name (to be used
6953a2
@@ -2452,12 +2516,25 @@ static int ssl_find_vhost(void *servername, conn_rec *c, server_rec *s)
6953a2
     if (found && (ssl = sslcon->ssl) &&
6953a2
         (sc = mySrvConfig(s))) {
6953a2
         SSL_CTX *ctx = SSL_set_SSL_CTX(ssl, sc->server->ssl_ctx);
6953a2
+
6953a2
         /*
6953a2
          * SSL_set_SSL_CTX() only deals with the server cert,
6953a2
          * so we need to duplicate a few additional settings
6953a2
          * from the ctx by hand
6953a2
          */
6953a2
         SSL_set_options(ssl, SSL_CTX_get_options(ctx));
6953a2
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L \
6953a2
+        && (!defined(LIBRESSL_VERSION_NUMBER) \
6953a2
+            || LIBRESSL_VERSION_NUMBER >= 0x20800000L)
6953a2
+        /*
6953a2
+         * Don't switch the protocol if none is configured for this vhost,
6953a2
+         * the default in this case is still the base server's SSLProtocol.
6953a2
+         */
6953a2
+        if (myCtxConfig(sslcon, sc)->protocol_set) {
6953a2
+            SSL_set_min_proto_version(ssl, SSL_CTX_get_min_proto_version(ctx));
6953a2
+            SSL_set_max_proto_version(ssl, SSL_CTX_get_max_proto_version(ctx));
6953a2
+        }
6953a2
+#endif
6953a2
         if ((SSL_get_verify_mode(ssl) == SSL_VERIFY_NONE) ||
6953a2
             (SSL_num_renegotiations(ssl) == 0)) {
6953a2
            /*
6953a2
@@ -2654,7 +2731,7 @@ int ssl_callback_alpn_select(SSL *ssl,
6953a2
      * they callback the SNI. We need to make sure that we know which vhost
6953a2
      * we are dealing with so we respect the correct protocols.
6953a2
      */
6953a2
-    init_vhost(c, ssl);
6953a2
+    init_vhost(c, ssl, NULL);
6953a2
     
6953a2
     proposed = ap_select_protocol(c, NULL, sslconn->server, client_protos);
6953a2
     if (!proposed) {
6953a2
diff --git a/modules/ssl/ssl_private.h b/modules/ssl/ssl_private.h
6953a2
index 8055200..f8a1db7 100644
6953a2
--- a/modules/ssl/ssl_private.h
6953a2
+++ b/modules/ssl/ssl_private.h
6953a2
@@ -563,6 +563,7 @@ typedef struct {
6953a2
     
6953a2
     const char *cipher_suite; /* cipher suite used in last reneg */
6953a2
     int service_unavailable;  /* thouugh we negotiate SSL, no requests will be served */
6953a2
+    int vhost_found;          /* whether we found vhost from SNI already */
6953a2
 } SSLConnRec;
6953a2
 
6953a2
 /* BIG FAT WARNING: SSLModConfigRec has unusual memory lifetime: it is
6953a2
@@ -946,6 +947,9 @@ void         ssl_callback_Info(const SSL *, int, int);
6953a2
 #ifdef HAVE_TLSEXT
6953a2
 int          ssl_callback_ServerNameIndication(SSL *, int *, modssl_ctx_t *);
6953a2
 #endif
6953a2
+#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
6953a2
+int          ssl_callback_ClientHello(SSL *, int *, void *);
6953a2
+#endif
6953a2
 #ifdef HAVE_TLS_SESSION_TICKETS
6953a2
 int         ssl_callback_SessionTicket(SSL *, unsigned char *, unsigned char *,
6953a2
                                        EVP_CIPHER_CTX *, HMAC_CTX *, int);