[openssl-commits] [openssl] master update

kaduk at mit.edu kaduk at mit.edu
Tue Aug 15 15:54:12 UTC 2017


The branch master has been updated
       via  0aed6e449da5f06a23fd191bb86bfdd71bde6f9c (commit)
       via  5626f634c31cfde48ccbbee243be29e0eb77171e (commit)
      from  12997aa984cf0e5def1045fd22d7b0675caaa0a1 (commit)


- Log -----------------------------------------------------------------
commit 0aed6e449da5f06a23fd191bb86bfdd71bde6f9c
Author: Benjamin Kaduk <bkaduk at akamai.com>
Date:   Tue Aug 1 15:28:14 2017 -0500

    Add SSL_get_pending_cipher()
    
    The existing function SSL_get_current_cipher() queries the
    current session for the ciphersuite in use, but there is no way
    for application code to determine what ciphersuite has been
    negotiated and will be used in the future, prior to ChangeCipherState
    (or the TLS 1.3 equivalent) causing the new cipher to take effect and
    become visible in the session information.  Expose this information
    to appropriate application callbacks to use during the handshake.
    
    The name SSL_get_pending_cipher() was chosen for compatibility with
    BoringSSL's routine of that name.
    
    Improve the note on macro implementations in SSL_get_current_cipher.pod
    while here.
    
    Reviewed-by: Matt Caswell <matt at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/4070)

commit 5626f634c31cfde48ccbbee243be29e0eb77171e
Author: Benjamin Kaduk <bkaduk at akamai.com>
Date:   Tue Aug 1 14:50:22 2017 -0500

    Move ALPN handling from finalizer to delayed call
    
    Commit 02f0274e8c0596dcf7e2d104250232a42c650b96 moved ALPN processing
    into an extension finalization function, as the only documented ordering
    requirement from previous commits was that ALPN processing occur after
    SNI processing, and SNI processing is performed before the extension
    finalization step.  However, it is useful for applications'
    alpn_select callbacks to run after ciphersuite selection as well -- at
    least one application protocol specification (HTTP/2) imposes restrictions
    on which ciphersuites are usable with that protocol.  Since it is generally
    more preferrable to have a successful TLS connection with a default application
    protocol than to fail the TLS connection and not be able to have the preferred
    application protocol, it is good to give the alpn_select callback information
    about the ciphersuite to be used, so that appropriate restrctions can be
    enforced in application code.
    
    Accordingly, split the ALPN handling out into a separate tls_handl_alpn()
    function akin to tls_handle_status_request(), called from
    tls_post_process_client_hello().  This is an alternative to resuscitating
    ssl_check_clienthello_tlsext_late(), something of an awkwward name itself.
    
    Reviewed-by: Matt Caswell <matt at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/4070)

-----------------------------------------------------------------------

Summary of changes:
 doc/man3/SSL_get_current_cipher.pod | 20 ++++++++++++++--
 include/openssl/ssl.h               |  1 +
 ssl/ssl_lib.c                       |  5 ++++
 ssl/statem/extensions.c             | 41 +------------------------------
 ssl/statem/statem_srvr.c            | 48 +++++++++++++++++++++++++++++++++++++
 util/libssl.num                     |  1 +
 6 files changed, 74 insertions(+), 42 deletions(-)

diff --git a/doc/man3/SSL_get_current_cipher.pod b/doc/man3/SSL_get_current_cipher.pod
index cb7e999..e5b249f 100644
--- a/doc/man3/SSL_get_current_cipher.pod
+++ b/doc/man3/SSL_get_current_cipher.pod
@@ -3,13 +3,15 @@
 =head1 NAME
 
 SSL_get_current_cipher, SSL_get_cipher_name, SSL_get_cipher,
-SSL_get_cipher_bits, SSL_get_cipher_version - get SSL_CIPHER of a connection
+SSL_get_cipher_bits, SSL_get_cipher_version,
+SSL_get_pending_cipher - get SSL_CIPHER of a connection
 
 =head1 SYNOPSIS
 
  #include <openssl/ssl.h>
 
  SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl);
+ SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl);
 
  const char *SSL_get_cipher_name(const SSL *s);
  const char *SSL_get_cipher(const SSL *s);
@@ -30,14 +32,28 @@ SSL_get_cipher_bits() is a
 macro to obtain the number of secret/algorithm bits used and
 SSL_get_cipher_version() returns the protocol name.
 
+SSL_get_pending_cipher() returns a pointer to an SSL_CIPHER object containing
+the description of the cipher (if any) that has been negotiated for future use
+on the connection established with the B<ssl> object, but is not yet in use.
+This may be the case during handshake processing, when control flow can be
+returned to the application via any of several callback methods.  The internal
+sequencing of handshake processing and callback invocation is not guaranteed
+to be stable from release to release, and at present only the callback set
+by SSL_CTX_set_alpn_select_cb() is guaranteed to have a non-NULL return value.
+Other callbacks may be added to this list over time.
+
 =head1 RETURN VALUES
 
 SSL_get_current_cipher() returns the cipher actually used, or NULL if
 no session has been established.
 
+SSL_get_pending_cipher() returns the cipher to be used at the next change
+of cipher suite, or NULL if no such cipher is known.
+
 =head1 NOTES
 
-These are implemented as macros.
+SSL_get_cipher, SSL_get_cipher_bits, SSL_get_cipher_version, and
+SSL_get_cipher_name are implemented as macros.
 
 =head1 SEE ALSO
 
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index b4c6644..0438881 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -1430,6 +1430,7 @@ __owur int SSL_clear(SSL *s);
 void SSL_CTX_flush_sessions(SSL_CTX *ctx, long tm);
 
 __owur const SSL_CIPHER *SSL_get_current_cipher(const SSL *s);
+__owur const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s);
 __owur int SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits);
 __owur const char *SSL_CIPHER_get_version(const SSL_CIPHER *c);
 __owur const char *SSL_CIPHER_get_name(const SSL_CIPHER *c);
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 3abb271..ed2113c 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -3613,6 +3613,11 @@ const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
     return (NULL);
 }
 
+const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
+{
+    return s->s3->tmp.new_cipher;
+}
+
 const COMP_METHOD *SSL_get_current_compression(SSL *s)
 {
 #ifndef OPENSSL_NO_COMP
diff --git a/ssl/statem/extensions.c b/ssl/statem/extensions.c
index 2268b27..a5dda45 100644
--- a/ssl/statem/extensions.c
+++ b/ssl/statem/extensions.c
@@ -28,7 +28,6 @@ static int init_status_request(SSL *s, unsigned int context);
 static int init_npn(SSL *s, unsigned int context);
 #endif
 static int init_alpn(SSL *s, unsigned int context);
-static int final_alpn(SSL *s, unsigned int context, int sent, int *al);
 static int init_sig_algs(SSL *s, unsigned int context);
 static int init_certificate_authorities(SSL *s, unsigned int context);
 static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
@@ -207,7 +206,7 @@ static const EXTENSION_DEFINITION ext_defs[] = {
         SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
         | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
         init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
-        tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
+        tls_construct_stoc_alpn, tls_construct_ctos_alpn, NULL
     },
 #ifndef OPENSSL_NO_SRTP
     {
@@ -937,44 +936,6 @@ static int init_alpn(SSL *s, unsigned int context)
     return 1;
 }
 
-static int final_alpn(SSL *s, unsigned int context, int sent, int *al)
-{
-    const unsigned char *selected = NULL;
-    unsigned char selected_len = 0;
-
-    if (!s->server)
-        return 1;
-
-    if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
-        int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
-                                           s->s3->alpn_proposed,
-                                           (unsigned int)s->s3->alpn_proposed_len,
-                                           s->ctx->ext.alpn_select_cb_arg);
-
-        if (r == SSL_TLSEXT_ERR_OK) {
-            OPENSSL_free(s->s3->alpn_selected);
-            s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
-            if (s->s3->alpn_selected == NULL) {
-                *al = SSL_AD_INTERNAL_ERROR;
-                return 0;
-            }
-            s->s3->alpn_selected_len = selected_len;
-#ifndef OPENSSL_NO_NEXTPROTONEG
-            /* ALPN takes precedence over NPN. */
-            s->s3->npn_seen = 0;
-#endif
-        } else if (r == SSL_TLSEXT_ERR_NOACK) {
-            /* Behave as if no callback was present. */
-            return 1;
-        } else {
-            *al = SSL_AD_NO_APPLICATION_PROTOCOL;
-            return 0;
-        }
-    }
-
-    return 1;
-}
-
 static int init_sig_algs(SSL *s, unsigned int context)
 {
     /* Clear any signature algorithms extension received */
diff --git a/ssl/statem/statem_srvr.c b/ssl/statem/statem_srvr.c
index fad339a..8c5f77b 100644
--- a/ssl/statem/statem_srvr.c
+++ b/ssl/statem/statem_srvr.c
@@ -1934,6 +1934,45 @@ static int tls_handle_status_request(SSL *s, int *al)
     return 1;
 }
 
+/*
+ * Call the alpn_select callback if needed. Upon success, returns 1.
+ * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert.
+ */
+static int tls_handle_alpn(SSL *s, int *al)
+{
+    const unsigned char *selected = NULL;
+    unsigned char selected_len = 0;
+
+    if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
+        int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
+                                           s->s3->alpn_proposed,
+                                           (unsigned int)s->s3->alpn_proposed_len,
+                                           s->ctx->ext.alpn_select_cb_arg);
+
+        if (r == SSL_TLSEXT_ERR_OK) {
+            OPENSSL_free(s->s3->alpn_selected);
+            s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
+            if (s->s3->alpn_selected == NULL) {
+                *al = SSL_AD_INTERNAL_ERROR;
+                return 0;
+            }
+            s->s3->alpn_selected_len = selected_len;
+#ifndef OPENSSL_NO_NEXTPROTONEG
+            /* ALPN takes precedence over NPN. */
+            s->s3->npn_seen = 0;
+#endif
+        } else if (r == SSL_TLSEXT_ERR_NOACK) {
+            /* Behave as if no callback was present. */
+            return 1;
+        } else {
+            *al = SSL_AD_NO_APPLICATION_PROTOCOL;
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
 WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
 {
     int al = SSL_AD_HANDSHAKE_FAILURE;
@@ -2018,6 +2057,15 @@ WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
                    SSL_R_CLIENTHELLO_TLSEXT);
             goto f_err;
         }
+        /*
+         * Call alpn_select callback if needed.  Has to be done after SNI and
+         * cipher negotiation (HTTP/2 restricts permitted ciphers).
+         */
+        if (!tls_handle_alpn(s, &al)) {
+            SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
+                   SSL_R_CLIENTHELLO_TLSEXT);
+            goto f_err;
+        }
 
         wst = WORK_MORE_C;
     }
diff --git a/util/libssl.num b/util/libssl.num
index 78fb65a..7d4c01e 100644
--- a/util/libssl.num
+++ b/util/libssl.num
@@ -463,3 +463,4 @@ OPENSSL_cipher_name                     463	1_1_1	EXIST::FUNCTION:
 SSL_alloc_buffers                       464	1_1_1	EXIST::FUNCTION:
 SSL_free_buffers                        465	1_1_1	EXIST::FUNCTION:
 SSL_SESSION_dup                         466	1_1_1	EXIST::FUNCTION:
+SSL_get_pending_cipher                  467	1_1_1	EXIST::FUNCTION:


More information about the openssl-commits mailing list