Blame SOURCES/httpd-2.4.6-r1332643+.patch

6670f0
diff --git a/modules/ssl/mod_ssl.c b/modules/ssl/mod_ssl.c
6670f0
index 987b256..c24c6e1 100644
6670f0
--- a/modules/ssl/mod_ssl.c
6670f0
+++ b/modules/ssl/mod_ssl.c
6670f0
@@ -514,6 +514,13 @@ int ssl_engine_disable(conn_rec *c)
af9b8b
     return 1;
af9b8b
 }
af9b8b
 
af9b8b
+static int modssl_register_npn(conn_rec *c, 
af9b8b
+                               ssl_npn_advertise_protos advertisefn,
af9b8b
+                               ssl_npn_proto_negotiated negotiatedfn)
af9b8b
+{
af9b8b
+    return DECLINED;
af9b8b
+}
af9b8b
+
af9b8b
 int ssl_init_ssl_connection(conn_rec *c, request_rec *r)
af9b8b
 {
af9b8b
     SSLSrvConfigRec *sc;
6670f0
@@ -706,6 +713,7 @@ static void ssl_register_hooks(apr_pool_t *p)
af9b8b
 
af9b8b
     APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
af9b8b
     APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
af9b8b
+    APR_REGISTER_OPTIONAL_FN(modssl_register_npn);
af9b8b
 
af9b8b
     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ssl",
af9b8b
                               AUTHZ_PROVIDER_VERSION,
6670f0
diff --git a/modules/ssl/mod_ssl.h b/modules/ssl/mod_ssl.h
6670f0
index db8ffaf..d381901 100644
6670f0
--- a/modules/ssl/mod_ssl.h
6670f0
+++ b/modules/ssl/mod_ssl.h
6670f0
@@ -84,5 +84,40 @@ APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_enable, (conn_rec *));
af9b8b
 
af9b8b
 APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
af9b8b
 
af9b8b
+/** The npn_advertise_protos callback allows another modules to add
af9b8b
+ * entries to the list of protocol names advertised by the server
af9b8b
+ * during the Next Protocol Negotiation (NPN) portion of the SSL
af9b8b
+ * handshake.  The callback is given the connection and an APR array;
af9b8b
+ * it should push one or more char*'s pointing to NUL-terminated
af9b8b
+ * strings (such as "http/1.1" or "spdy/2") onto the array and return
af9b8b
+ * OK.  To prevent further processing of (other modules') callbacks,
af9b8b
+ * return DONE. */
af9b8b
+typedef int (*ssl_npn_advertise_protos)(conn_rec *connection, 
af9b8b
+                                        apr_array_header_t *protos);
af9b8b
+
af9b8b
+/** The npn_proto_negotiated callback allows other modules to discover
af9b8b
+ * the name of the protocol that was chosen during the Next Protocol
af9b8b
+ * Negotiation (NPN) portion of the SSL handshake.  Note that this may
af9b8b
+ * be the empty string (in which case modules should probably assume
af9b8b
+ * HTTP), or it may be a protocol that was never even advertised by
af9b8b
+ * the server.  The callback is given the connection, a
af9b8b
+ * non-NUL-terminated string containing the protocol name, and the
af9b8b
+ * length of the string; it should do something appropriate
af9b8b
+ * (i.e. insert or remove filters) and return OK.  To prevent further
af9b8b
+ * processing of (other modules') callbacks, return DONE. */
af9b8b
+typedef int (*ssl_npn_proto_negotiated)(conn_rec *connection, 
af9b8b
+                                        const char *proto_name,
af9b8b
+                                        apr_size_t proto_name_len);
af9b8b
+
af9b8b
+/* An optional function which can be used to register a pair of
af9b8b
+ * callbacks for NPN handling.  This optional function should be
af9b8b
+ * invoked from a pre_connection hook which runs *after* mod_ssl.c's
af9b8b
+ * pre_connection hook.  The function returns OK if the callbacks are
af9b8b
+ * register, or DECLINED otherwise (for example if mod_ssl does not
af9b8b
+l * support NPN).  */
af9b8b
+APR_DECLARE_OPTIONAL_FN(int, modssl_register_npn, (conn_rec *conn, 
af9b8b
+                                                   ssl_npn_advertise_protos advertisefn,
af9b8b
+                                                   ssl_npn_proto_negotiated negotiatedfn));
af9b8b
+
af9b8b
 #endif /* __MOD_SSL_H__ */
af9b8b
 /** @} */
6670f0
diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c
6670f0
index 9526f1b..12b2115 100644
6670f0
--- a/modules/ssl/ssl_engine_init.c
6670f0
+++ b/modules/ssl/ssl_engine_init.c
6670f0
@@ -736,6 +736,11 @@ static void ssl_init_ctx_callbacks(server_rec *s,
f0c688
 #ifdef HAVE_TLS_ALPN
f0c688
     SSL_CTX_set_alpn_select_cb(ctx, ssl_callback_alpn_select, NULL);
f0c688
 #endif
af9b8b
+
af9b8b
+#ifdef HAVE_TLS_NPN
af9b8b
+    SSL_CTX_set_next_protos_advertised_cb(
af9b8b
+        ctx, ssl_callback_AdvertiseNextProtos, NULL);
af9b8b
+#endif
af9b8b
 }
af9b8b
 
33b929
 static apr_status_t ssl_init_ctx_verify(server_rec *s,
6670f0
diff --git a/modules/ssl/ssl_engine_io.c b/modules/ssl/ssl_engine_io.c
6670f0
index d1f44e9..26ee301 100644
6670f0
--- a/modules/ssl/ssl_engine_io.c
6670f0
+++ b/modules/ssl/ssl_engine_io.c
6670f0
@@ -317,6 +317,7 @@ typedef struct {
af9b8b
     apr_pool_t *pool;
af9b8b
     char buffer[AP_IOBUFSIZE];
af9b8b
     ssl_filter_ctx_t *filter_ctx;
af9b8b
+    int npn_finished;  /* 1 if NPN has finished, 0 otherwise */
af9b8b
 } bio_filter_in_ctx_t;
af9b8b
 
af9b8b
 /*
6670f0
@@ -1517,6 +1518,38 @@ static apr_status_t ssl_io_filter_input(ap_filter_t *f,
6670f0
         return ssl_io_filter_error(inctx, bb, status, is_init);
af9b8b
     }
af9b8b
 
af9b8b
+#ifdef HAVE_TLS_NPN
af9b8b
+    /* By this point, Next Protocol Negotiation (NPN) should be completed (if
af9b8b
+     * our version of OpenSSL supports it).  If we haven't already, find out
af9b8b
+     * which protocol was decided upon and inform other modules by calling
af9b8b
+     * npn_proto_negotiated_hook. */
af9b8b
+    if (!inctx->npn_finished) {
af9b8b
+        SSLConnRec *sslconn = myConnConfig(f->c);
f0c688
+        apr_array_header_t *protos = apr_array_make(f->c->pool, 1, sizeof(char *));
af9b8b
+        const unsigned char *next_proto = NULL;
af9b8b
+        unsigned next_proto_len = 0;
f0c688
+        const unsigned char *out;
f0c688
+        unsigned char outlen;
f0c688
+        
f0c688
+        SSL_get0_next_proto_negotiated(inctx->ssl, &next_proto, &next_proto_len);
f0c688
+        if (next_proto_len) {
f0c688
+            ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, f->c,
f0c688
+                          APLOGNO(02306) "SSL NPN negotiated protocol: '%.*s'",
af9b8b
+                          next_proto_len, (const char*)next_proto);
f0c688
+
f0c688
+            APR_ARRAY_PUSH(protos, char *) =
f0c688
+              apr_pstrmemdup(f->c->pool, (const char *)next_proto, next_proto_len);
af9b8b
+            
f0c688
+            if (modssl_select_protocol(inctx->ssl, f->c, sslconn, protos,
f0c688
+                                       &out, &outlen) != SSL_TLSEXT_ERR_OK)
f0c688
+                ap_log_cerror(APLOG_MARK, APLOG_INFO, 0, f->c,
f0c688
+                              "SSL NPN negotiation failed");
af9b8b
+        }
af9b8b
+            
af9b8b
+        inctx->npn_finished = 1;
af9b8b
+    }
af9b8b
+#endif
af9b8b
+
f0c688
     if (is_init) {
f0c688
         /* protocol module needs to handshake before sending
f0c688
          * data to client (e.g. NNTP or FTP)
6670f0
@@ -2041,6 +2074,7 @@ static void ssl_io_input_add_filter(ssl_filter_ctx_t *filter_ctx, conn_rec *c,
af9b8b
     inctx->block = APR_BLOCK_READ;
af9b8b
     inctx->pool = c->pool;
af9b8b
     inctx->filter_ctx = filter_ctx;
af9b8b
+    inctx->npn_finished = 0;
af9b8b
 }
af9b8b
 
af9b8b
 /* The request_rec pointer is passed in here only to ensure that the
6670f0
diff --git a/modules/ssl/ssl_engine_kernel.c b/modules/ssl/ssl_engine_kernel.c
6670f0
index 02acd22..dced334 100644
6670f0
--- a/modules/ssl/ssl_engine_kernel.c
6670f0
+++ b/modules/ssl/ssl_engine_kernel.c
6670f0
@@ -2332,6 +2332,142 @@ int ssl_callback_SessionTicket(SSL *ssl,
af9b8b
 }
af9b8b
 #endif /* HAVE_TLS_SESSION_TICKETS */
af9b8b
 
f0c688
+#if defined(HAVE_TLS_NPN) || defined(HAVE_TLS_ALPN)
f0c688
+int modssl_select_protocol(SSL *ssl, conn_rec *c, SSLConnRec *sslconn,
f0c688
+			   apr_array_header_t *client_protos,
f0c688
+			   const unsigned char **out, unsigned char *outlen)
f0c688
+{
f0c688
+    const char *proposed;
f0c688
+    size_t len;
f0c688
+
f0c688
+    /* The order the callbacks are invoked from TLS extensions is, unfortunately
f0c688
+     * not defined and older openssl versions do call ALPN selection before
f0c688
+     * they callback the SNI. We need to make sure that we know which vhost
f0c688
+     * we are dealing with so we respect the correct protocols.
f0c688
+     */
f0c688
+    init_vhost(c, ssl);
f0c688
+    
f0c688
+    proposed = ap_select_protocol(c, NULL, sslconn->server, client_protos);
f0c688
+    if (!proposed) {
f0c688
+        proposed = ap_get_protocol(c);
f0c688
+    }
f0c688
+    
f0c688
+    len = strlen(proposed);
f0c688
+    if (len > 255) {
f0c688
+        ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02840)
f0c688
+                      "ALPN negotiated protocol name too long");
f0c688
+        return SSL_TLSEXT_ERR_ALERT_FATAL;
f0c688
+    }
f0c688
+    *out = (const unsigned char *)proposed;
f0c688
+    *outlen = (unsigned char)len;
f0c688
+
f0c688
+    ap_log_cerror(APLOG_MARK, APLOG_TRACE8, 0, c,
f0c688
+		  "protocol switching from '%s' to '%s'",
f0c688
+		  ap_get_protocol(c), proposed);
f0c688
+        
f0c688
+    if (strcmp(proposed, ap_get_protocol(c))) {
f0c688
+        apr_status_t status;
f0c688
+        
f0c688
+        status = ap_switch_protocol(c, NULL, sslconn->server, proposed);
f0c688
+        if (status != APR_SUCCESS) {
f0c688
+            ap_log_cerror(APLOG_MARK, APLOG_ERR, status, c,
f0c688
+                          APLOGNO(02908) "protocol switch to '%s' failed",
f0c688
+                          proposed);
f0c688
+            return SSL_TLSEXT_ERR_ALERT_FATAL;
f0c688
+        }
f0c688
+    }
f0c688
+
f0c688
+    return SSL_TLSEXT_ERR_OK;
f0c688
+}
f0c688
+#endif /* defined(HAVE_TLS_NPN) || defined(HAVE_TLS_ALPN) */
f0c688
+
af9b8b
+#ifdef HAVE_TLS_NPN
af9b8b
+/*
af9b8b
+ * This callback function is executed when SSL needs to decide what protocols
af9b8b
+ * to advertise during Next Protocol Negotiation (NPN).  It must produce a
af9b8b
+ * string in wire format -- a sequence of length-prefixed strings -- indicating
af9b8b
+ * the advertised protocols.  Refer to SSL_CTX_set_next_protos_advertised_cb
af9b8b
+ * in OpenSSL for reference.
af9b8b
+ */
af9b8b
+int ssl_callback_AdvertiseNextProtos(SSL *ssl, const unsigned char **data_out,
af9b8b
+                                     unsigned int *size_out, void *arg)
af9b8b
+{
af9b8b
+    conn_rec *c = (conn_rec*)SSL_get_app_data(ssl);
af9b8b
+    SSLConnRec *sslconn = myConnConfig(c);
f0c688
+    const apr_array_header_t *protos;
af9b8b
+    int num_protos;
af9b8b
+    unsigned int size;
af9b8b
+    int i;
af9b8b
+    unsigned char *data;
af9b8b
+    unsigned char *start;
af9b8b
+
af9b8b
+    *data_out = NULL;
af9b8b
+    *size_out = 0;
af9b8b
+
f0c688
+    /* If the connection object is not available, or there are no
f0c688
+       upgrades available, do nothing. */
f0c688
+    if (!c || ap_get_protocol_upgrades(c, NULL, sslconn->server, 0, &protos)) {
f0c688
+	return SSL_TLSEXT_ERR_NOACK;
af9b8b
+    }
af9b8b
+
f0c688
+    num_protos = protos ? protos->nelts : 0;
f0c688
+
f0c688
+    if (num_protos == 0) {
f0c688
+        return SSL_TLSEXT_ERR_NOACK;
af9b8b
+    }
af9b8b
+
af9b8b
+    /* We now have a list of null-terminated strings; we need to concatenate
af9b8b
+     * them together into a single string, where each protocol name is prefixed
af9b8b
+     * by its length.  First, calculate how long that string will be. */
af9b8b
+    size = 0;
af9b8b
+    for (i = 0; i < num_protos; ++i) {
af9b8b
+        const char *string = APR_ARRAY_IDX(protos, i, const char*);
af9b8b
+        unsigned int length = strlen(string);
af9b8b
+        /* If the protocol name is too long (the length must fit in one byte),
af9b8b
+         * then log an error and skip it. */
af9b8b
+        if (length > 255) {
af9b8b
+            ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02307)
af9b8b
+                          "SSL NPN protocol name too long (length=%u): %s",
af9b8b
+                          length, string);
af9b8b
+            continue;
af9b8b
+        }
af9b8b
+        /* Leave room for the length prefix (one byte) plus the protocol name
af9b8b
+         * itself. */
af9b8b
+        size += 1 + length;
af9b8b
+    }
af9b8b
+
af9b8b
+    /* If there is nothing to advertise (either because no modules added
af9b8b
+     * anything to the protos array, or because all strings added to the array
af9b8b
+     * were skipped), then we're done. */
af9b8b
+    if (size == 0) {
af9b8b
+        return SSL_TLSEXT_ERR_OK;
af9b8b
+    }
af9b8b
+
af9b8b
+    /* Now we can build the string.  Copy each protocol name string into the
af9b8b
+     * larger string, prefixed by its length. */
af9b8b
+    data = apr_palloc(c->pool, size * sizeof(unsigned char));
af9b8b
+    start = data;
af9b8b
+    for (i = 0; i < num_protos; ++i) {
af9b8b
+        const char *string = APR_ARRAY_IDX(protos, i, const char*);
af9b8b
+        apr_size_t length = strlen(string);
af9b8b
+        if (length > 255)
af9b8b
+            continue;
af9b8b
+        *start = (unsigned char)length;
af9b8b
+        ++start;
af9b8b
+        memcpy(start, string, length * sizeof(unsigned char));
af9b8b
+        start += length;
f0c688
+	ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, 
f0c688
+		      "SSL NPN protocol advertising: %s", string);
af9b8b
+    }
af9b8b
+
af9b8b
+    /* Success. */
af9b8b
+    *data_out = data;
af9b8b
+    *size_out = size;
af9b8b
+    return SSL_TLSEXT_ERR_OK;
af9b8b
+}
af9b8b
+
af9b8b
+#endif /* HAVE_TLS_NPN */
af9b8b
+
f0c688
 #ifdef HAVE_TLS_ALPN
af9b8b
 
f0c688
 /*
6670f0
@@ -2354,8 +2490,6 @@ int ssl_callback_alpn_select(SSL *ssl,
f0c688
     conn_rec *c = (conn_rec*)SSL_get_app_data(ssl);
f0c688
     SSLConnRec *sslconn = myConnConfig(c);
f0c688
     apr_array_header_t *client_protos;
f0c688
-    const char *proposed;
f0c688
-    size_t len;
f0c688
     int i;
f0c688
 
f0c688
     /* If the connection object is not available,
6670f0
@@ -2385,40 +2519,7 @@ int ssl_callback_alpn_select(SSL *ssl,
f0c688
         i += plen;
f0c688
     }
f0c688
 
f0c688
-    /* The order the callbacks are invoked from TLS extensions is, unfortunately
f0c688
-     * not defined and older openssl versions do call ALPN selection before
f0c688
-     * they callback the SNI. We need to make sure that we know which vhost
f0c688
-     * we are dealing with so we respect the correct protocols.
f0c688
-     */
f0c688
-    init_vhost(c, ssl);
f0c688
-    
f0c688
-    proposed = ap_select_protocol(c, NULL, sslconn->server, client_protos);
f0c688
-    if (!proposed) {
f0c688
-        proposed = ap_get_protocol(c);
f0c688
-    }
f0c688
-    
f0c688
-    len = strlen(proposed);
f0c688
-    if (len > 255) {
f0c688
-        ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02840)
f0c688
-                      "ALPN negotiated protocol name too long");
f0c688
-        return SSL_TLSEXT_ERR_ALERT_FATAL;
f0c688
-    }
f0c688
-    *out = (const unsigned char *)proposed;
f0c688
-    *outlen = (unsigned char)len;
f0c688
-        
f0c688
-    if (strcmp(proposed, ap_get_protocol(c))) {
f0c688
-        apr_status_t status;
f0c688
-        
f0c688
-        status = ap_switch_protocol(c, NULL, sslconn->server, proposed);
f0c688
-        if (status != APR_SUCCESS) {
f0c688
-            ap_log_cerror(APLOG_MARK, APLOG_ERR, status, c,
f0c688
-                          APLOGNO(02908) "protocol switch to '%s' failed",
f0c688
-                          proposed);
f0c688
-            return SSL_TLSEXT_ERR_ALERT_FATAL;
f0c688
-        }
f0c688
-    }
f0c688
-
f0c688
-    return SSL_TLSEXT_ERR_OK;
f0c688
+    return modssl_select_protocol(ssl, c, sslconn, client_protos, out, outlen);
f0c688
 }
f0c688
 #endif /* HAVE_TLS_ALPN */
f0c688
 
6670f0
diff --git a/modules/ssl/ssl_private.h b/modules/ssl/ssl_private.h
6670f0
index 41b4387..7c440ef 100644
6670f0
--- a/modules/ssl/ssl_private.h
6670f0
+++ b/modules/ssl/ssl_private.h
af9b8b
@@ -98,6 +98,8 @@
af9b8b
 #include <openssl/x509_vfy.h>
af9b8b
 #include <openssl/ocsp.h>
af9b8b
 
af9b8b
+#include "mod_ssl.h"
af9b8b
+
af9b8b
 /* Avoid tripping over an engine build installed globally and detected
af9b8b
  * when the user points at an explicit non-engine flavor of OpenSSL
af9b8b
  */
f0c688
@@ -127,6 +129,11 @@
af9b8b
 #define HAVE_FIPS
af9b8b
 #endif
af9b8b
 
af9b8b
+#if OPENSSL_VERSION_NUMBER >= 0x10001000L && !defined(OPENSSL_NO_NEXTPROTONEG) \
af9b8b
+    && !defined(OPENSSL_NO_TLSEXT)
af9b8b
+#define HAVE_TLS_NPN
af9b8b
+#endif
af9b8b
+
33b929
 #if defined(SSL_OP_NO_TLSv1_2)
33b929
 #define HAVE_TLSV1_X
33b929
 #endif
6670f0
@@ -872,6 +879,10 @@ int          ssl_callback_ServerNameIndication(SSL *, int *, modssl_ctx_t *);
af9b8b
 int         ssl_callback_SessionTicket(SSL *, unsigned char *, unsigned char *,
af9b8b
                                        EVP_CIPHER_CTX *, HMAC_CTX *, int);
af9b8b
 #endif
af9b8b
+int ssl_callback_AdvertiseNextProtos(SSL *ssl, const unsigned char **data, unsigned int *len, void *arg);
f0c688
+int modssl_select_protocol(SSL *ssl, conn_rec *c, SSLConnRec *sslconn,
f0c688
+			   apr_array_header_t *client_protos,
f0c688
+			   const unsigned char **out, unsigned char *outlen);
af9b8b
 
f0c688
 #ifdef HAVE_TLS_ALPN
f0c688
 int ssl_callback_alpn_select(SSL *ssl, const unsigned char **out,