From 2330d82fd82f467eda8a70dc720e3aa2ab1647ca Mon Sep 17 00:00:00 2001
From: Fedor Indutny <fedor@indutny.com>
Date: Sat, 30 Jan 2016 16:58:34 -0500
Subject: [PATCH] Allow downgrading when reusing sessions on client

When connecting to pool of diverse servers (both TLS1.0 and TLS1.2), a
following scenario may happen:

  1. Connect to TLS1.2 server, receive new session
  2. Store this session
  3. Attempt to reuse it later when connecting to server
  4. Connect to different server from the pool, which speaks only TLS1.0
  5. Get `SSL_R_WRONG_VERSION_NUMBER` error

Expected behavior would be scrapping off the session, and allowing
server to downgrade to supported protocol version the way it would do it
if no client session would be supplied.

This issue was discovered while working on following node.js bug:

https://github.com/nodejs/node/issues/3692
---
 ssl/s3_clnt.c | 37 ++++++++++++++++++++++++++++++++-----
 ssl/ssltest.c | 22 +++++++++++++++++++++-
 test/testssl  |  6 ++++++
 3 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c
index 04cc9f5..5e3e6d1 100644
--- a/ssl/s3_clnt.c
+++ b/ssl/s3_clnt.c
@@ -899,6 +899,7 @@ int ssl3_get_server_hello(SSL *s)
     int i, al = SSL_AD_INTERNAL_ERROR, ok;
     unsigned int j;
     long n;
+    unsigned short version;
 #ifndef OPENSSL_NO_COMP
     SSL_COMP *comp;
 #endif
@@ -906,8 +907,7 @@ int ssl3_get_server_hello(SSL *s)
      * Hello verify request and/or server hello version may not match so set
      * first packet if we're negotiating version.
      */
-    if (SSL_IS_DTLS(s))
-        s->first_packet = 1;
+    s->first_packet = 1;
 
     n = s->method->ssl_get_message(s,
                                    SSL3_ST_CR_SRVR_HELLO_A,
@@ -916,8 +916,8 @@ int ssl3_get_server_hello(SSL *s)
     if (!ok)
         return ((int)n);
 
+    s->first_packet = 0;
     if (SSL_IS_DTLS(s)) {
-        s->first_packet = 0;
         if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
             if (s->d1->send_cookie == 0) {
                 s->s3->tmp.reuse_message = 1;
@@ -961,9 +961,36 @@ int ssl3_get_server_hello(SSL *s)
         s->session->ssl_version = s->version = s->method->version;
     }
 
-    if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
+    version = (p[0] << 8) | p[1];
+
+    /* Client may attempt to reuse session with higher protocol version
+     * number than what is currently supported by the server
+     */
+    if (!s->new_session && version != s->version) {
+        if (version == TLS1_VERSION &&
+            !(s->options & SSL_OP_NO_TLSv1)) {
+            s->method = TLSv1_client_method();
+        } else if (version == TLS1_1_VERSION &&
+                   !(s->options & SSL_OP_NO_TLSv1_1)) {
+            s->method = TLSv1_1_client_method();
+        } else if (version == TLS1_2_VERSION &&
+                   !(s->options & SSL_OP_NO_TLSv1_2)) {
+            s->method = TLSv1_2_client_method();
+        }
+        /* Otherwise fallthrough to the next if block and error */
+
+        /* Make sure that the session won't be reused */
+        if (version == s->version) {
+            if (!ssl_get_new_session(s, 0))
+              goto f_err;
+            s->version = s->method->version;
+            s->client_version = s->version;
+        }
+    }
+
+    if (version != s->version) {
         SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
-        s->version = (s->version & 0xff00) | p[1];
+        s->version = (s->version & 0xff00) | (version & 0xff);
         al = SSL_AD_PROTOCOL_VERSION;
         goto f_err;
     }
diff --git a/ssl/ssltest.c b/ssl/ssltest.c
index aaf6c6b..f5494f1 100644
--- a/ssl/ssltest.c
+++ b/ssl/ssltest.c
@@ -688,6 +688,8 @@ static void sv_usage(void)
     fprintf(stderr, " -v            - more output\n");
     fprintf(stderr, " -d            - debug output\n");
     fprintf(stderr, " -reuse        - use session-id reuse\n");
+    fprintf(stderr, " -tls1_reuse   - downgrade to TLSv1 on session reuse\n");
+    fprintf(stderr, " -no_ticket    - do not issue TLS session ticket");
     fprintf(stderr, " -num <val>    - number of connections to perform\n");
     fprintf(stderr,
             " -bytes <val>  - number of bytes to swap between client/server\n");
@@ -900,7 +902,7 @@ int main(int argc, char *argv[])
     SSL_CTX *c_ctx = NULL;
     const SSL_METHOD *meth = NULL;
     SSL *c_ssl, *s_ssl;
-    int number = 1, reuse = 0;
+    int number = 1, reuse = 0, tls1_reuse = 0, no_ticket = 0;
     long bytes = 256L;
 #ifndef OPENSSL_NO_DH
     DH *dh;
@@ -984,6 +986,10 @@ int main(int argc, char *argv[])
             debug = 1;
         else if (strcmp(*argv, "-reuse") == 0)
             reuse = 1;
+        else if (strcmp(*argv, "-tls1_reuse") == 0)
+            tls1_reuse = 1;
+        else if (strcmp(*argv, "-no_ticket") == 0)
+            no_ticket = 1;
         else if (strcmp(*argv, "-dhe512") == 0) {
 #ifndef OPENSSL_NO_DH
             dhe512 = 1;
@@ -1214,6 +1220,13 @@ int main(int argc, char *argv[])
                 "to avoid protocol mismatch.\n");
         EXIT(1);
     }
+    if ((ssl2 || ssl3 || tls1 || dtls1 || dtls12 || !reuse || number <= 1) &&
+        tls1_reuse) {
+        fprintf(stderr, "Can\'t downgrade to TLSv1 on reuse without reuse, or\n"
+                        "from already low protocol version, or when number of\n"
+                        "tests is equal to 1\n");
+        EXIT(1);
+    }
 #ifdef OPENSSL_FIPS
     if (fips_mode) {
         if (!FIPS_mode_set(1)) {
@@ -1420,6 +1433,11 @@ int main(int argc, char *argv[])
                                        sizeof session_id_context);
     }
 
+    if (no_ticket) {
+        SSL_CTX_set_options(c_ctx, SSL_OP_NO_TICKET);
+        SSL_CTX_set_options(s_ctx, SSL_OP_NO_TICKET);
+    }
+
     /* Use PSK only if PSK key is given */
     if (psk_key != NULL) {
         /*
@@ -1553,6 +1571,8 @@ int main(int argc, char *argv[])
     for (i = 0; i < number; i++) {
         if (!reuse)
             SSL_set_session(c_ssl, NULL);
+        if ((i != 0) && tls1_reuse)
+            SSL_set_ssl_method(s_ssl, TLSv1_method());
         if (bio_pair)
             ret = doit_biopair(s_ssl, c_ssl, bytes, &s_time, &c_time);
         else
diff --git a/test/testssl b/test/testssl
index 747e4ba..b5c6680 100644
--- a/test/testssl
+++ b/test/testssl
@@ -274,3 +274,9 @@ if [ -z "$extra" -a `uname -m` = "x86_64" ]; then
 fi
 
 exit 0
+
+#############################################################################
+# Downgrade on reuse
+
+echo test downgrading to tls1 on session reuse
+$ssltest -reuse -tls1_reuse -num 2 || exit 1
-- 
2.7.0

