Blame SOURCES/httpd-2.4.6-r1332643+.patch

af9b8b
# ./pullrev.sh 1332643 1345599 1487772
af9b8b
af9b8b
https://bugzilla.redhat.com//show_bug.cgi?id=809599
af9b8b
af9b8b
http://svn.apache.org/viewvc?view=revision&revision=1332643
af9b8b
http://svn.apache.org/viewvc?view=revision&revision=1345599
af9b8b
http://svn.apache.org/viewvc?view=revision&revision=1487772
af9b8b
af9b8b
--- httpd-2.4.6/modules/ssl/mod_ssl.c.r1332643+
af9b8b
+++ httpd-2.4.6/modules/ssl/mod_ssl.c
af9b8b
@@ -413,6 +413,37 @@ 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
+#ifdef HAVE_TLS_NPN
af9b8b
+    SSLConnRec *sslconn = myConnConfig(c);
af9b8b
+
af9b8b
+    if (!sslconn) {
af9b8b
+        return DECLINED;
af9b8b
+    }
af9b8b
+
af9b8b
+    if (!sslconn->npn_advertfns) {
af9b8b
+        sslconn->npn_advertfns = 
af9b8b
+            apr_array_make(c->pool, 5, sizeof(ssl_npn_advertise_protos));
af9b8b
+        sslconn->npn_negofns = 
af9b8b
+            apr_array_make(c->pool, 5, sizeof(ssl_npn_proto_negotiated));
af9b8b
+    }
af9b8b
+
af9b8b
+    if (advertisefn)
af9b8b
+        APR_ARRAY_PUSH(sslconn->npn_advertfns, ssl_npn_advertise_protos) =
af9b8b
+            advertisefn;
af9b8b
+    if (negotiatedfn)
af9b8b
+        APR_ARRAY_PUSH(sslconn->npn_negofns, ssl_npn_proto_negotiated) =
af9b8b
+            negotiatedfn;
af9b8b
+
af9b8b
+    return OK;
af9b8b
+#else
af9b8b
+    return DECLINED;
af9b8b
+#endif
af9b8b
+}
af9b8b
+
af9b8b
 int ssl_init_ssl_connection(conn_rec *c, request_rec *r)
af9b8b
 {
af9b8b
     SSLSrvConfigRec *sc;
af9b8b
@@ -584,6 +615,7 @@ static void ssl_register_hooks(apr_pool_
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,
af9b8b
--- httpd-2.4.6/modules/ssl/mod_ssl.h.r1332643+
af9b8b
+++ httpd-2.4.6/modules/ssl/mod_ssl.h
af9b8b
@@ -63,5 +63,40 @@ APR_DECLARE_OPTIONAL_FN(int, ssl_proxy_e
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
 /** @} */
af9b8b
--- httpd-2.4.6/modules/ssl/ssl_engine_init.c.r1332643+
af9b8b
+++ httpd-2.4.6/modules/ssl/ssl_engine_init.c
af9b8b
@@ -725,6 +725,11 @@ static void ssl_init_ctx_callbacks(serve
af9b8b
 #endif
af9b8b
 
af9b8b
     SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
af9b8b
+
af9b8b
+#ifdef HAVE_TLS_NPN
af9b8b
+    SSL_CTX_set_next_protos_advertised_cb(
af9b8b
+        ctx, ssl_callback_AdvertiseNextProtos, NULL);
af9b8b
+#endif
af9b8b
 }
af9b8b
 
af9b8b
 static void ssl_init_ctx_verify(server_rec *s,
af9b8b
--- httpd-2.4.6/modules/ssl/ssl_engine_io.c.r1332643+
af9b8b
+++ httpd-2.4.6/modules/ssl/ssl_engine_io.c
af9b8b
@@ -297,6 +297,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
 /*
af9b8b
@@ -1400,6 +1401,37 @@ static apr_status_t ssl_io_filter_input(
af9b8b
         APR_BRIGADE_INSERT_TAIL(bb, bucket);
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);
af9b8b
+        const unsigned char *next_proto = NULL;
af9b8b
+        unsigned next_proto_len = 0;
af9b8b
+        int n;
af9b8b
+
af9b8b
+        if (sslconn->npn_negofns) {
af9b8b
+            SSL_get0_next_proto_negotiated(
af9b8b
+                inctx->ssl, &next_proto, &next_proto_len);
af9b8b
+            ap_log_cerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, f->c,
af9b8b
+                          APLOGNO(02306) "SSL NPN negotiated protocol: '%*s'",
af9b8b
+                          next_proto_len, (const char*)next_proto);
af9b8b
+            
af9b8b
+            for (n = 0; n < sslconn->npn_negofns->nelts; n++) {
af9b8b
+                ssl_npn_proto_negotiated fn = 
af9b8b
+                    APR_ARRAY_IDX(sslconn->npn_negofns, n, ssl_npn_proto_negotiated);
af9b8b
+                
af9b8b
+                if (fn(f->c, (const char *)next_proto, next_proto_len) == DONE)
af9b8b
+                    break;
af9b8b
+            }
af9b8b
+        }
af9b8b
+            
af9b8b
+        inctx->npn_finished = 1;
af9b8b
+    }
af9b8b
+#endif
af9b8b
+
af9b8b
     return APR_SUCCESS;
af9b8b
 }
af9b8b
 
af9b8b
@@ -1881,6 +1913,7 @@ static void ssl_io_input_add_filter(ssl_
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
af9b8b
--- httpd-2.4.6/modules/ssl/ssl_engine_kernel.c.r1332643+
af9b8b
+++ httpd-2.4.6/modules/ssl/ssl_engine_kernel.c
af9b8b
@@ -2161,6 +2161,97 @@ int ssl_callback_SessionTicket(SSL *ssl,
af9b8b
 }
af9b8b
 #endif /* HAVE_TLS_SESSION_TICKETS */
af9b8b
 
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);
af9b8b
+    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
+
af9b8b
+    /* If the connection object is not available, or there are no NPN
af9b8b
+     * hooks registered, then there's nothing for us to do. */
af9b8b
+    if (c == NULL || sslconn->npn_advertfns == NULL) {
af9b8b
+        return SSL_TLSEXT_ERR_OK;
af9b8b
+    }
af9b8b
+
af9b8b
+    /* Invoke our npn_advertise_protos hook, giving other modules a chance to
af9b8b
+     * add alternate protocol names to advertise. */
af9b8b
+    protos = apr_array_make(c->pool, 0, sizeof(char *));
af9b8b
+    for (i = 0; i < sslconn->npn_advertfns->nelts; i++) {
af9b8b
+        ssl_npn_advertise_protos fn = 
af9b8b
+            APR_ARRAY_IDX(sslconn->npn_advertfns, i, ssl_npn_advertise_protos);
af9b8b
+        
af9b8b
+        if (fn(c, protos) == DONE)
af9b8b
+            break;
af9b8b
+    }
af9b8b
+    num_protos = protos->nelts;
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;
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
+
af9b8b
 #ifndef OPENSSL_NO_SRP
af9b8b
 
af9b8b
 int ssl_callback_SRPServerParams(SSL *ssl, int *ad, void *arg)
af9b8b
--- httpd-2.4.6/modules/ssl/ssl_private.h.r1332643+
af9b8b
+++ httpd-2.4.6/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
  */
af9b8b
@@ -139,6 +141,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
+
af9b8b
 #if (OPENSSL_VERSION_NUMBER >= 0x10000000)
af9b8b
 #define MODSSL_SSL_CIPHER_CONST const
af9b8b
 #define MODSSL_SSL_METHOD_CONST const
af9b8b
@@ -487,6 +494,12 @@ typedef struct {
af9b8b
                      * connection */
af9b8b
     } reneg_state;
af9b8b
 
af9b8b
+#ifdef HAVE_TLS_NPN
af9b8b
+    /* Poor man's inter-module optional hooks for NPN. */
af9b8b
+    apr_array_header_t *npn_advertfns; /* list of ssl_npn_advertise_protos callbacks */
af9b8b
+    apr_array_header_t *npn_negofns; /* list of ssl_npn_proto_negotiated callbacks. */
af9b8b
+#endif
af9b8b
+
af9b8b
     server_rec *server;
af9b8b
 } SSLConnRec;
af9b8b
 
af9b8b
@@ -842,6 +855,7 @@ int          ssl_callback_ServerNameIndi
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);
af9b8b
 
af9b8b
 /**  Session Cache Support  */
af9b8b
 void         ssl_scache_init(server_rec *, apr_pool_t *);