8335b1
# ./pullrev.sh 1332643 1345599 1487772
8335b1
8335b1
https://bugzilla.redhat.com//show_bug.cgi?id=809599
8335b1
8335b1
http://svn.apache.org/viewvc?view=revision&revision=1332643
8335b1
http://svn.apache.org/viewvc?view=revision&revision=1345599
8335b1
http://svn.apache.org/viewvc?view=revision&revision=1487772
8335b1
8335b1
--- httpd-2.4.6/modules/ssl/mod_ssl.c.r1332643+
8335b1
+++ httpd-2.4.6/modules/ssl/mod_ssl.c
8335b1
@@ -413,6 +413,37 @@ int ssl_engine_disable(conn_rec *c)
8335b1
     return 1;
8335b1
 }
8335b1
 
8335b1
+static int modssl_register_npn(conn_rec *c, 
8335b1
+                               ssl_npn_advertise_protos advertisefn,
8335b1
+                               ssl_npn_proto_negotiated negotiatedfn)
8335b1
+{
8335b1
+#ifdef HAVE_TLS_NPN
8335b1
+    SSLConnRec *sslconn = myConnConfig(c);
8335b1
+
8335b1
+    if (!sslconn) {
8335b1
+        return DECLINED;
8335b1
+    }
8335b1
+
8335b1
+    if (!sslconn->npn_advertfns) {
8335b1
+        sslconn->npn_advertfns = 
8335b1
+            apr_array_make(c->pool, 5, sizeof(ssl_npn_advertise_protos));
8335b1
+        sslconn->npn_negofns = 
8335b1
+            apr_array_make(c->pool, 5, sizeof(ssl_npn_proto_negotiated));
8335b1
+    }
8335b1
+
8335b1
+    if (advertisefn)
8335b1
+        APR_ARRAY_PUSH(sslconn->npn_advertfns, ssl_npn_advertise_protos) =
8335b1
+            advertisefn;
8335b1
+    if (negotiatedfn)
8335b1
+        APR_ARRAY_PUSH(sslconn->npn_negofns, ssl_npn_proto_negotiated) =
8335b1
+            negotiatedfn;
8335b1
+
8335b1
+    return OK;
8335b1
+#else
8335b1
+    return DECLINED;
8335b1
+#endif
8335b1
+}
8335b1
+
8335b1
 int ssl_init_ssl_connection(conn_rec *c, request_rec *r)
8335b1
 {
8335b1
     SSLSrvConfigRec *sc;
8335b1
@@ -584,6 +615,7 @@ static void ssl_register_hooks(apr_pool_
8335b1
 
8335b1
     APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
8335b1
     APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
8335b1
+    APR_REGISTER_OPTIONAL_FN(modssl_register_npn);
8335b1
 
8335b1
     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ssl",
8335b1
                               AUTHZ_PROVIDER_VERSION,
8335b1
--- httpd-2.4.6/modules/ssl/mod_ssl.h.r1332643+
8335b1
+++ httpd-2.4.6/modules/ssl/mod_ssl.h
8335b1
@@ -63,5 +63,40 @@ APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_e
8335b1
 
8335b1
 APR_DECLARE_OPTIONAL_FN(int, ssl_engine_disable, (conn_rec *));
8335b1
 
8335b1
+/** The npn_advertise_protos callback allows another modules to add
8335b1
+ * entries to the list of protocol names advertised by the server
8335b1
+ * during the Next Protocol Negotiation (NPN) portion of the SSL
8335b1
+ * handshake.  The callback is given the connection and an APR array;
8335b1
+ * it should push one or more char*'s pointing to NUL-terminated
8335b1
+ * strings (such as "http/1.1" or "spdy/2") onto the array and return
8335b1
+ * OK.  To prevent further processing of (other modules') callbacks,
8335b1
+ * return DONE. */
8335b1
+typedef int (*ssl_npn_advertise_protos)(conn_rec *connection, 
8335b1
+                                        apr_array_header_t *protos);
8335b1
+
8335b1
+/** The npn_proto_negotiated callback allows other modules to discover
8335b1
+ * the name of the protocol that was chosen during the Next Protocol
8335b1
+ * Negotiation (NPN) portion of the SSL handshake.  Note that this may
8335b1
+ * be the empty string (in which case modules should probably assume
8335b1
+ * HTTP), or it may be a protocol that was never even advertised by
8335b1
+ * the server.  The callback is given the connection, a
8335b1
+ * non-NUL-terminated string containing the protocol name, and the
8335b1
+ * length of the string; it should do something appropriate
8335b1
+ * (i.e. insert or remove filters) and return OK.  To prevent further
8335b1
+ * processing of (other modules') callbacks, return DONE. */
8335b1
+typedef int (*ssl_npn_proto_negotiated)(conn_rec *connection, 
8335b1
+                                        const char *proto_name,
8335b1
+                                        apr_size_t proto_name_len);
8335b1
+
8335b1
+/* An optional function which can be used to register a pair of
8335b1
+ * callbacks for NPN handling.  This optional function should be
8335b1
+ * invoked from a pre_connection hook which runs *after* mod_ssl.c's
8335b1
+ * pre_connection hook.  The function returns OK if the callbacks are
8335b1
+ * register, or DECLINED otherwise (for example if mod_ssl does not
8335b1
+l * support NPN).  */
8335b1
+APR_DECLARE_OPTIONAL_FN(int, modssl_register_npn, (conn_rec *conn, 
8335b1
+                                                   ssl_npn_advertise_protos advertisefn,
8335b1
+                                                   ssl_npn_proto_negotiated negotiatedfn));
8335b1
+
8335b1
 #endif /* __MOD_SSL_H__ */
8335b1
 /** @} */
8335b1
--- httpd-2.4.6/modules/ssl/ssl_engine_init.c.r1332643+
8335b1
+++ httpd-2.4.6/modules/ssl/ssl_engine_init.c
8335b1
@@ -725,6 +725,11 @@ static void ssl_init_ctx_callbacks(serve
8335b1
 #endif
8335b1
 
8335b1
     SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
8335b1
+
8335b1
+#ifdef HAVE_TLS_NPN
8335b1
+    SSL_CTX_set_next_protos_advertised_cb(
8335b1
+        ctx, ssl_callback_AdvertiseNextProtos, NULL);
8335b1
+#endif
8335b1
 }
8335b1
 
8335b1
 static void ssl_init_ctx_verify(server_rec *s,
8335b1
--- httpd-2.4.6/modules/ssl/ssl_engine_io.c.r1332643+
8335b1
+++ httpd-2.4.6/modules/ssl/ssl_engine_io.c
8335b1
@@ -297,6 +297,7 @@ typedef struct {
8335b1
     apr_pool_t *pool;
8335b1
     char buffer[AP_IOBUFSIZE];
8335b1
     ssl_filter_ctx_t *filter_ctx;
8335b1
+    int npn_finished;  /* 1 if NPN has finished, 0 otherwise */
8335b1
 } bio_filter_in_ctx_t;
8335b1
 
8335b1
 /*
8335b1
@@ -1400,6 +1401,37 @@ static apr_status_t ssl_io_filter_input(
8335b1
         APR_BRIGADE_INSERT_TAIL(bb, bucket);
8335b1
     }
8335b1
 
8335b1
+#ifdef HAVE_TLS_NPN
8335b1
+    /* By this point, Next Protocol Negotiation (NPN) should be completed (if
8335b1
+     * our version of OpenSSL supports it).  If we haven't already, find out
8335b1
+     * which protocol was decided upon and inform other modules by calling
8335b1
+     * npn_proto_negotiated_hook. */
8335b1
+    if (!inctx->npn_finished) {
8335b1
+        SSLConnRec *sslconn = myConnConfig(f->c);
8335b1
+        const unsigned char *next_proto = NULL;
8335b1
+        unsigned next_proto_len = 0;
8335b1
+        int n;
8335b1
+
8335b1
+        if (sslconn->npn_negofns) {
8335b1
+            SSL_get0_next_proto_negotiated(
8335b1
+                inctx->ssl, &next_proto, &next_proto_len);
8335b1
+            ap_log_cerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, f->c,
8335b1
+                          APLOGNO(02306) "SSL NPN negotiated protocol: '%*s'",
8335b1
+                          next_proto_len, (const char*)next_proto);
8335b1
+            
8335b1
+            for (n = 0; n < sslconn->npn_negofns->nelts; n++) {
8335b1
+                ssl_npn_proto_negotiated fn = 
8335b1
+                    APR_ARRAY_IDX(sslconn->npn_negofns, n, ssl_npn_proto_negotiated);
8335b1
+                
8335b1
+                if (fn(f->c, (const char *)next_proto, next_proto_len) == DONE)
8335b1
+                    break;
8335b1
+            }
8335b1
+        }
8335b1
+            
8335b1
+        inctx->npn_finished = 1;
8335b1
+    }
8335b1
+#endif
8335b1
+
8335b1
     return APR_SUCCESS;
8335b1
 }
8335b1
 
8335b1
@@ -1881,6 +1913,7 @@ static void ssl_io_input_add_filter(ssl_
8335b1
     inctx->block = APR_BLOCK_READ;
8335b1
     inctx->pool = c->pool;
8335b1
     inctx->filter_ctx = filter_ctx;
8335b1
+    inctx->npn_finished = 0;
8335b1
 }
8335b1
 
8335b1
 /* The request_rec pointer is passed in here only to ensure that the
8335b1
--- httpd-2.4.6/modules/ssl/ssl_engine_kernel.c.r1332643+
8335b1
+++ httpd-2.4.6/modules/ssl/ssl_engine_kernel.c
8335b1
@@ -2161,6 +2161,97 @@ int ssl_callback_SessionTicket(SSL *ssl,
8335b1
 }
8335b1
 #endif /* HAVE_TLS_SESSION_TICKETS */
8335b1
 
8335b1
+#ifdef HAVE_TLS_NPN
8335b1
+/*
8335b1
+ * This callback function is executed when SSL needs to decide what protocols
8335b1
+ * to advertise during Next Protocol Negotiation (NPN).  It must produce a
8335b1
+ * string in wire format -- a sequence of length-prefixed strings -- indicating
8335b1
+ * the advertised protocols.  Refer to SSL_CTX_set_next_protos_advertised_cb
8335b1
+ * in OpenSSL for reference.
8335b1
+ */
8335b1
+int ssl_callback_AdvertiseNextProtos(SSL *ssl, const unsigned char **data_out,
8335b1
+                                     unsigned int *size_out, void *arg)
8335b1
+{
8335b1
+    conn_rec *c = (conn_rec*)SSL_get_app_data(ssl);
8335b1
+    SSLConnRec *sslconn = myConnConfig(c);
8335b1
+    apr_array_header_t *protos;
8335b1
+    int num_protos;
8335b1
+    unsigned int size;
8335b1
+    int i;
8335b1
+    unsigned char *data;
8335b1
+    unsigned char *start;
8335b1
+
8335b1
+    *data_out = NULL;
8335b1
+    *size_out = 0;
8335b1
+
8335b1
+    /* If the connection object is not available, or there are no NPN
8335b1
+     * hooks registered, then there's nothing for us to do. */
8335b1
+    if (c == NULL || sslconn->npn_advertfns == NULL) {
8335b1
+        return SSL_TLSEXT_ERR_NOACK;
8335b1
+    }
8335b1
+
8335b1
+    /* Invoke our npn_advertise_protos hook, giving other modules a chance to
8335b1
+     * add alternate protocol names to advertise. */
8335b1
+    protos = apr_array_make(c->pool, 0, sizeof(char *));
8335b1
+    for (i = 0; i < sslconn->npn_advertfns->nelts; i++) {
8335b1
+        ssl_npn_advertise_protos fn = 
8335b1
+            APR_ARRAY_IDX(sslconn->npn_advertfns, i, ssl_npn_advertise_protos);
8335b1
+        
8335b1
+        if (fn(c, protos) == DONE)
8335b1
+            break;
8335b1
+    }
8335b1
+    num_protos = protos->nelts;
8335b1
+
8335b1
+    /* We now have a list of null-terminated strings; we need to concatenate
8335b1
+     * them together into a single string, where each protocol name is prefixed
8335b1
+     * by its length.  First, calculate how long that string will be. */
8335b1
+    size = 0;
8335b1
+    for (i = 0; i < num_protos; ++i) {
8335b1
+        const char *string = APR_ARRAY_IDX(protos, i, const char*);
8335b1
+        unsigned int length = strlen(string);
8335b1
+        /* If the protocol name is too long (the length must fit in one byte),
8335b1
+         * then log an error and skip it. */
8335b1
+        if (length > 255) {
8335b1
+            ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(02307)
8335b1
+                          "SSL NPN protocol name too long (length=%u): %s",
8335b1
+                          length, string);
8335b1
+            continue;
8335b1
+        }
8335b1
+        /* Leave room for the length prefix (one byte) plus the protocol name
8335b1
+         * itself. */
8335b1
+        size += 1 + length;
8335b1
+    }
8335b1
+
8335b1
+    /* If there is nothing to advertise (either because no modules added
8335b1
+     * anything to the protos array, or because all strings added to the array
8335b1
+     * were skipped), then we're done. */
8335b1
+    if (size == 0) {
8335b1
+        return SSL_TLSEXT_ERR_NOACK;
8335b1
+    }
8335b1
+
8335b1
+    /* Now we can build the string.  Copy each protocol name string into the
8335b1
+     * larger string, prefixed by its length. */
8335b1
+    data = apr_palloc(c->pool, size * sizeof(unsigned char));
8335b1
+    start = data;
8335b1
+    for (i = 0; i < num_protos; ++i) {
8335b1
+        const char *string = APR_ARRAY_IDX(protos, i, const char*);
8335b1
+        apr_size_t length = strlen(string);
8335b1
+        if (length > 255)
8335b1
+            continue;
8335b1
+        *start = (unsigned char)length;
8335b1
+        ++start;
8335b1
+        memcpy(start, string, length * sizeof(unsigned char));
8335b1
+        start += length;
8335b1
+    }
8335b1
+
8335b1
+    /* Success. */
8335b1
+    *data_out = data;
8335b1
+    *size_out = size;
8335b1
+    return SSL_TLSEXT_ERR_OK;
8335b1
+}
8335b1
+
8335b1
+#endif /* HAVE_TLS_NPN */
8335b1
+
8335b1
 #ifndef OPENSSL_NO_SRP
8335b1
 
8335b1
 int ssl_callback_SRPServerParams(SSL *ssl, int *ad, void *arg)
8335b1
--- httpd-2.4.6/modules/ssl/ssl_private.h.r1332643+
8335b1
+++ httpd-2.4.6/modules/ssl/ssl_private.h
8335b1
@@ -98,6 +98,8 @@
8335b1
 #include <openssl/x509_vfy.h>
8335b1
 #include <openssl/ocsp.h>
8335b1
 
8335b1
+#include "mod_ssl.h"
8335b1
+
8335b1
 /* Avoid tripping over an engine build installed globally and detected
8335b1
  * when the user points at an explicit non-engine flavor of OpenSSL
8335b1
  */
8335b1
@@ -139,6 +141,11 @@
8335b1
 #define HAVE_FIPS
8335b1
 #endif
8335b1
 
8335b1
+#if OPENSSL_VERSION_NUMBER >= 0x10001000L && !defined(OPENSSL_NO_NEXTPROTONEG) \
8335b1
+    && !defined(OPENSSL_NO_TLSEXT)
8335b1
+#define HAVE_TLS_NPN
8335b1
+#endif
8335b1
+
8335b1
 #if (OPENSSL_VERSION_NUMBER >= 0x10000000)
8335b1
 #define MODSSL_SSL_CIPHER_CONST const
8335b1
 #define MODSSL_SSL_METHOD_CONST const
8335b1
@@ -487,6 +494,12 @@ typedef struct {
8335b1
                      * connection */
8335b1
     } reneg_state;
8335b1
 
8335b1
+#ifdef HAVE_TLS_NPN
8335b1
+    /* Poor man's inter-module optional hooks for NPN. */
8335b1
+    apr_array_header_t *npn_advertfns; /* list of ssl_npn_advertise_protos callbacks */
8335b1
+    apr_array_header_t *npn_negofns; /* list of ssl_npn_proto_negotiated callbacks. */
8335b1
+#endif
8335b1
+
8335b1
     server_rec *server;
8335b1
 } SSLConnRec;
8335b1
 
8335b1
@@ -842,6 +855,7 @@ int          ssl_callback_ServerNameIndi
8335b1
 int         ssl_callback_SessionTicket(SSL *, unsigned char *, unsigned char *,
8335b1
                                        EVP_CIPHER_CTX *, HMAC_CTX *, int);
8335b1
 #endif
8335b1
+int ssl_callback_AdvertiseNextProtos(SSL *ssl, const unsigned char **data, unsigned int *len, void *arg);
8335b1
 
8335b1
 /**  Session Cache Support  */
8335b1
 void         ssl_scache_init(server_rec *, apr_pool_t *);