Change the "offset too large" message to more generic wording
[openssl.git] / test / clienthellotest.c
index efdea942b736b4ee1d8791f375113fe756c85043..10e3b1b1b172408e7fffcbb893d5c4453cc3d44d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -20,7 +20,6 @@
 #include "../ssl/packet_locl.h"
 
 #include "testutil.h"
-#include "test_main_custom.h"
 
 #define CLIENT_VERSION_LEN      2
 
  */
 #define TEST_ADD_PADDING_AND_PSK                3
 
-#define F5_WORKAROUND_MIN_MSG_LEN   0xff
+#define F5_WORKAROUND_MIN_MSG_LEN   0x7f
 #define F5_WORKAROUND_MAX_MSG_LEN   0x200
 
-const char *sessionfile = NULL;
+static const char *sessionfile = NULL;
+/* Dummy ALPN protocols used to pad out the size of the ClientHello */
+static const char alpn_prots[] =
+    "0123456789012345678901234567890123456789012345678901234567890123456789"
+    "0123456789012345678901234567890123456789012345678901234567890123456789"
+    "01234567890123456789";
 
 static int test_client_hello(int currtest)
 {
@@ -54,45 +58,75 @@ static int test_client_hello(int currtest)
     BIO *wbio;
     long len;
     unsigned char *data;
-    PACKET pkt, pkt2, pkt3;
+    PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
     char *dummytick = "Hello World!";
-    unsigned int type;
+    unsigned int type = 0;
     int testresult = 0;
     size_t msglen;
     BIO *sessbio = NULL;
     SSL_SESSION *sess = NULL;
 
+#ifdef OPENSSL_NO_TLS1_3
+    if (currtest == TEST_ADD_PADDING_AND_PSK)
+        return 1;
+#endif
+
     /*
      * For each test set up an SSL_CTX and SSL and see what ClientHello gets
      * produced when we try to connect
      */
     ctx = SSL_CTX_new(TLS_method());
-    if (ctx == NULL)
+    if (!TEST_ptr(ctx))
+        goto end;
+    if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION)))
         goto end;
 
     switch(currtest) {
     case TEST_SET_SESSION_TICK_DATA_VER_NEG:
+#if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2)
+        /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */
+        return 1;
+#else
         /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
-        if (!SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION))
+        if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
             goto end;
+#endif
         break;
 
     case TEST_ADD_PADDING_AND_PSK:
+        /*
+         * In this case we're doing TLSv1.3 and we're sending a PSK so the
+         * ClientHello is already going to be quite long. To avoid getting one
+         * that is too long for this test we use a restricted ciphersuite list
+         */
+        if (!TEST_true(SSL_CTX_set_cipher_list(ctx, "")))
+            goto end;
+         /* Fall through */
     case TEST_ADD_PADDING:
     case TEST_PADDING_NOT_NEEDED:
         SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
+        /* Make sure we get a consistent size across TLS versions */
+        SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
         /*
-         * Add lots of ciphersuites so that the ClientHello is at least
+         * Add some dummy ALPN protocols so that the ClientHello is at least
          * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
          * needed.
-         * In the padding not needed case we assume the test will pass, but then
-         * set testresult to 0 if we see the padding extension.
          */
-        if (currtest == TEST_ADD_PADDING
-                && !SSL_CTX_set_cipher_list(ctx, "ALL"))
+        if (currtest == TEST_ADD_PADDING) {
+             if (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
+                                    (unsigned char *)alpn_prots,
+                                    sizeof(alpn_prots) - 1)))
+                goto end;
+        /*
+         * Otherwise we need to make sure we have a small enough message to
+         * not need padding.
+         */
+        } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx,
+                              "AES128-SHA"))
+                   || !TEST_true(SSL_CTX_set_ciphersuites(ctx,
+                                 "TLS_AES_128_GCM_SHA256"))) {
             goto end;
-        else if (currtest == TEST_PADDING_NOT_NEEDED)
-            testresult = 1;
+        }
         break;
 
     default:
@@ -100,37 +134,32 @@ static int test_client_hello(int currtest)
     }
 
     con = SSL_new(ctx);
-    if (con == NULL)
+    if (!TEST_ptr(con))
         goto end;
 
     if (currtest == TEST_ADD_PADDING_AND_PSK) {
         sessbio = BIO_new_file(sessionfile, "r");
-        if (sessbio == NULL) {
-            printf("Unable to open session.pem\n");
+        if (!TEST_ptr(sessbio)) {
+            TEST_info("Unable to open session.pem");
             goto end;
         }
         sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
-        if (sess == NULL) {
-            printf("Unable to load SSL_SESSION\n");
+        if (!TEST_ptr(sess)) {
+            TEST_info("Unable to load SSL_SESSION");
             goto end;
         }
         /*
          * We reset the creation time so that we don't discard the session as
          * too old.
          */
-        if (!SSL_SESSION_set_time(sess, time(NULL))) {
-            printf("Unable to set creation time on SSL_SESSION\n");
-            goto end;
-        }
-        if (!SSL_set_session(con, sess)) {
-            printf("Unable to set the session on the connection\n");
+        if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL)))
+                || !TEST_true(SSL_set_session(con, sess)))
             goto end;
-        }
     }
 
     rbio = BIO_new(BIO_s_mem());
     wbio = BIO_new(BIO_s_mem());
-    if (rbio == NULL || wbio == NULL) {
+    if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
         BIO_free(rbio);
         BIO_free(wbio);
         goto end;
@@ -140,95 +169,82 @@ static int test_client_hello(int currtest)
     SSL_set_connect_state(con);
 
     if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
-        if (!SSL_set_session_ticket_ext(con, dummytick, strlen(dummytick)))
+        if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
+                                                  strlen(dummytick))))
             goto end;
     }
 
-    if (SSL_connect(con) > 0) {
+    if (!TEST_int_le(SSL_connect(con), 0)) {
         /* This shouldn't succeed because we don't have a server! */
         goto end;
     }
 
     len = BIO_get_mem_data(wbio, (char **)&data);
-    if (!PACKET_buf_init(&pkt, data, len))
-        goto end;
-
-    /* Skip the record header */
-    if (!PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
+    if (!TEST_true(PACKET_buf_init(&pkt, data, len))
+               /* Skip the record header */
+            || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
         goto end;
 
     msglen = PACKET_remaining(&pkt);
 
     /* Skip the handshake message header */
-    if (!PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
-        goto end;
-
-    /* Skip client version and random */
-    if (!PACKET_forward(&pkt, CLIENT_VERSION_LEN + SSL3_RANDOM_SIZE))
-        goto end;
-
-    /* Skip session id */
-    if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
-        goto end;
-
-    /* Skip ciphers */
-    if (!PACKET_get_length_prefixed_2(&pkt, &pkt2))
-        goto end;
-
-    /* Skip compression */
-    if (!PACKET_get_length_prefixed_1(&pkt, &pkt2))
-        goto end;
-
-    /* Extensions len */
-    if (!PACKET_as_length_prefixed_2(&pkt, &pkt2))
+    if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
+               /* Skip client version and random */
+            || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
+                                               + SSL3_RANDOM_SIZE))
+               /* Skip session id */
+            || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
+               /* Skip ciphers */
+            || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
+               /* Skip compression */
+            || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
+               /* Extensions len */
+            || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
         goto end;
 
     /* Loop through all extensions */
     while (PACKET_remaining(&pkt2)) {
 
-        if (!PACKET_get_net_2(&pkt2, &type) ||
-            !PACKET_get_length_prefixed_2(&pkt2, &pkt3))
+        if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
+                || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
             goto end;
 
         if (type == TLSEXT_TYPE_session_ticket) {
             if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
-                if (PACKET_equal(&pkt3, dummytick, strlen(dummytick))) {
+                if (TEST_true(PACKET_equal(&pkt3, dummytick,
+                                           strlen(dummytick)))) {
                     /* Ticket data is as we expected */
                     testresult = 1;
-                } else {
-                    printf("Received session ticket is not as expected\n");
                 }
-                break;
+                goto end;
             }
         }
         if (type == TLSEXT_TYPE_padding) {
-            if (currtest == TEST_ADD_PADDING
-                    || currtest == TEST_ADD_PADDING_AND_PSK)
-                testresult = (msglen == F5_WORKAROUND_MAX_MSG_LEN);
-            else
-                testresult = 0;
+            if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
+                goto end;
+            else if (TEST_true(currtest == TEST_ADD_PADDING
+                    || currtest == TEST_ADD_PADDING_AND_PSK))
+                testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
         }
     }
 
+    if (currtest == TEST_PADDING_NOT_NEEDED)
+        testresult = 1;
+
 end:
     SSL_free(con);
     SSL_CTX_free(ctx);
     SSL_SESSION_free(sess);
     BIO_free(sessbio);
-    if (!testresult)
-        printf("ClientHello test: FAILED (Test %d)\n", currtest);
 
     return testresult;
 }
 
-int test_main(int argc, char *argv[])
+int setup_tests(void)
 {
-    if (argc != 2)
+    if (!TEST_ptr(sessionfile = test_get_argument(0)))
         return 0;
 
-    sessionfile = argv[1];
-
     ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
-
-    return run_tests(argv[0]);
+    return 1;
 }