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