Home | History | Annotate | Line # | Download | only in test
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the OpenSSL license (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #include <string.h>
     11  1.1  christos 
     12  1.1  christos #include <openssl/opensslconf.h>
     13  1.1  christos #include <openssl/bio.h>
     14  1.1  christos #include <openssl/crypto.h>
     15  1.1  christos #include <openssl/evp.h>
     16  1.1  christos #include <openssl/ssl.h>
     17  1.1  christos #include <openssl/err.h>
     18  1.1  christos #include <time.h>
     19  1.1  christos 
     20  1.1  christos #include "../ssl/packet_local.h"
     21  1.1  christos 
     22  1.1  christos #include "testutil.h"
     23  1.1  christos 
     24  1.1  christos #define CLIENT_VERSION_LEN      2
     25  1.1  christos 
     26  1.1  christos #define TOTAL_NUM_TESTS                         4
     27  1.1  christos 
     28  1.1  christos /*
     29  1.1  christos  * Test that explicitly setting ticket data results in it appearing in the
     30  1.1  christos  * ClientHello for a negotiated SSL/TLS version
     31  1.1  christos  */
     32  1.1  christos #define TEST_SET_SESSION_TICK_DATA_VER_NEG      0
     33  1.1  christos /* Enable padding and make sure ClientHello is long enough to require it */
     34  1.1  christos #define TEST_ADD_PADDING                        1
     35  1.1  christos /* Enable padding and make sure ClientHello is short enough to not need it */
     36  1.1  christos #define TEST_PADDING_NOT_NEEDED                 2
     37  1.1  christos /*
     38  1.1  christos  * Enable padding and add a PSK to the ClientHello (this will also ensure the
     39  1.1  christos  * ClientHello is long enough to need padding)
     40  1.1  christos  */
     41  1.1  christos #define TEST_ADD_PADDING_AND_PSK                3
     42  1.1  christos 
     43  1.1  christos #define F5_WORKAROUND_MIN_MSG_LEN   0x7f
     44  1.1  christos #define F5_WORKAROUND_MAX_MSG_LEN   0x200
     45  1.1  christos 
     46  1.1  christos static const char *sessionfile = NULL;
     47  1.1  christos /* Dummy ALPN protocols used to pad out the size of the ClientHello */
     48  1.1  christos /* ASCII 'O' = 79 = 0x4F = EBCDIC '|'*/
     49  1.1  christos #ifdef CHARSET_EBCDIC
     50  1.1  christos static const char alpn_prots[] =
     51  1.1  christos     "|1234567890123456789012345678901234567890123456789012345678901234567890123456789"
     52  1.1  christos     "|1234567890123456789012345678901234567890123456789012345678901234567890123456789";
     53  1.1  christos #else
     54  1.1  christos static const char alpn_prots[] =
     55  1.1  christos     "O1234567890123456789012345678901234567890123456789012345678901234567890123456789"
     56  1.1  christos     "O1234567890123456789012345678901234567890123456789012345678901234567890123456789";
     57  1.1  christos #endif
     58  1.1  christos 
     59  1.1  christos static int test_client_hello(int currtest)
     60  1.1  christos {
     61  1.1  christos     SSL_CTX *ctx;
     62  1.1  christos     SSL *con = NULL;
     63  1.1  christos     BIO *rbio;
     64  1.1  christos     BIO *wbio;
     65  1.1  christos     long len;
     66  1.1  christos     unsigned char *data;
     67  1.1  christos     PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
     68  1.1  christos     char *dummytick = "Hello World!";
     69  1.1  christos     unsigned int type = 0;
     70  1.1  christos     int testresult = 0;
     71  1.1  christos     size_t msglen;
     72  1.1  christos     BIO *sessbio = NULL;
     73  1.1  christos     SSL_SESSION *sess = NULL;
     74  1.1  christos 
     75  1.1  christos #ifdef OPENSSL_NO_TLS1_3
     76  1.1  christos     if (currtest == TEST_ADD_PADDING_AND_PSK)
     77  1.1  christos         return 1;
     78  1.1  christos #endif
     79  1.1  christos 
     80  1.1  christos     /*
     81  1.1  christos      * For each test set up an SSL_CTX and SSL and see what ClientHello gets
     82  1.1  christos      * produced when we try to connect
     83  1.1  christos      */
     84  1.1  christos     ctx = SSL_CTX_new(TLS_method());
     85  1.1  christos     if (!TEST_ptr(ctx))
     86  1.1  christos         goto end;
     87  1.1  christos     if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION)))
     88  1.1  christos         goto end;
     89  1.1  christos 
     90  1.1  christos     switch(currtest) {
     91  1.1  christos     case TEST_SET_SESSION_TICK_DATA_VER_NEG:
     92  1.1  christos #if !defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_TLS1_2)
     93  1.1  christos         /* TLSv1.3 is enabled and TLSv1.2 is disabled so can't do this test */
     94  1.1  christos         return 1;
     95  1.1  christos #else
     96  1.1  christos         /* Testing for session tickets <= TLS1.2; not relevant for 1.3 */
     97  1.1  christos         if (!TEST_true(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)))
     98  1.1  christos             goto end;
     99  1.1  christos #endif
    100  1.1  christos         break;
    101  1.1  christos 
    102  1.1  christos     case TEST_ADD_PADDING_AND_PSK:
    103  1.1  christos         /*
    104  1.1  christos          * In this case we're doing TLSv1.3 and we're sending a PSK so the
    105  1.1  christos          * ClientHello is already going to be quite long. To avoid getting one
    106  1.1  christos          * that is too long for this test we use a restricted ciphersuite list
    107  1.1  christos          */
    108  1.1  christos         if (!TEST_false(SSL_CTX_set_cipher_list(ctx, "")))
    109  1.1  christos             goto end;
    110  1.1  christos         ERR_clear_error();
    111  1.1  christos          /* Fall through */
    112  1.1  christos     case TEST_ADD_PADDING:
    113  1.1  christos     case TEST_PADDING_NOT_NEEDED:
    114  1.1  christos         SSL_CTX_set_options(ctx, SSL_OP_TLSEXT_PADDING);
    115  1.1  christos         /* Make sure we get a consistent size across TLS versions */
    116  1.1  christos         SSL_CTX_clear_options(ctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
    117  1.1  christos         /*
    118  1.1  christos          * Add some dummy ALPN protocols so that the ClientHello is at least
    119  1.1  christos          * F5_WORKAROUND_MIN_MSG_LEN bytes long - meaning padding will be
    120  1.1  christos          * needed.
    121  1.1  christos          */
    122  1.1  christos         if (currtest == TEST_ADD_PADDING) {
    123  1.1  christos              if (!TEST_false(SSL_CTX_set_alpn_protos(ctx,
    124  1.1  christos                                     (unsigned char *)alpn_prots,
    125  1.1  christos                                     sizeof(alpn_prots) - 1)))
    126  1.1  christos                 goto end;
    127  1.1  christos         /*
    128  1.1  christos          * Otherwise we need to make sure we have a small enough message to
    129  1.1  christos          * not need padding.
    130  1.1  christos          */
    131  1.1  christos         } else if (!TEST_true(SSL_CTX_set_cipher_list(ctx,
    132  1.1  christos                               "AES128-SHA"))
    133  1.1  christos                    || !TEST_true(SSL_CTX_set_ciphersuites(ctx,
    134  1.1  christos                                  "TLS_AES_128_GCM_SHA256"))) {
    135  1.1  christos             goto end;
    136  1.1  christos         }
    137  1.1  christos         break;
    138  1.1  christos 
    139  1.1  christos     default:
    140  1.1  christos         goto end;
    141  1.1  christos     }
    142  1.1  christos 
    143  1.1  christos     con = SSL_new(ctx);
    144  1.1  christos     if (!TEST_ptr(con))
    145  1.1  christos         goto end;
    146  1.1  christos 
    147  1.1  christos     if (currtest == TEST_ADD_PADDING_AND_PSK) {
    148  1.1  christos         sessbio = BIO_new_file(sessionfile, "r");
    149  1.1  christos         if (!TEST_ptr(sessbio)) {
    150  1.1  christos             TEST_info("Unable to open session.pem");
    151  1.1  christos             goto end;
    152  1.1  christos         }
    153  1.1  christos         sess = PEM_read_bio_SSL_SESSION(sessbio, NULL, NULL, NULL);
    154  1.1  christos         if (!TEST_ptr(sess)) {
    155  1.1  christos             TEST_info("Unable to load SSL_SESSION");
    156  1.1  christos             goto end;
    157  1.1  christos         }
    158  1.1  christos         /*
    159  1.1  christos          * We reset the creation time so that we don't discard the session as
    160  1.1  christos          * too old.
    161  1.1  christos          */
    162  1.1  christos         if (!TEST_true(SSL_SESSION_set_time(sess, (long)time(NULL)))
    163  1.1  christos                 || !TEST_true(SSL_set_session(con, sess)))
    164  1.1  christos             goto end;
    165  1.1  christos     }
    166  1.1  christos 
    167  1.1  christos     rbio = BIO_new(BIO_s_mem());
    168  1.1  christos     wbio = BIO_new(BIO_s_mem());
    169  1.1  christos     if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
    170  1.1  christos         BIO_free(rbio);
    171  1.1  christos         BIO_free(wbio);
    172  1.1  christos         goto end;
    173  1.1  christos     }
    174  1.1  christos 
    175  1.1  christos     SSL_set_bio(con, rbio, wbio);
    176  1.1  christos     SSL_set_connect_state(con);
    177  1.1  christos 
    178  1.1  christos     if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
    179  1.1  christos         if (!TEST_true(SSL_set_session_ticket_ext(con, dummytick,
    180  1.1  christos                                                   strlen(dummytick))))
    181  1.1  christos             goto end;
    182  1.1  christos     }
    183  1.1  christos 
    184  1.1  christos     if (!TEST_int_le(SSL_connect(con), 0)) {
    185  1.1  christos         /* This shouldn't succeed because we don't have a server! */
    186  1.1  christos         goto end;
    187  1.1  christos     }
    188  1.1  christos 
    189  1.1  christos     len = BIO_get_mem_data(wbio, (char **)&data);
    190  1.1  christos     if (!TEST_true(PACKET_buf_init(&pkt, data, len))
    191  1.1  christos                /* Skip the record header */
    192  1.1  christos             || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH))
    193  1.1  christos         goto end;
    194  1.1  christos 
    195  1.1  christos     msglen = PACKET_remaining(&pkt);
    196  1.1  christos 
    197  1.1  christos     /* Skip the handshake message header */
    198  1.1  christos     if (!TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
    199  1.1  christos                /* Skip client version and random */
    200  1.1  christos             || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
    201  1.1  christos                                                + SSL3_RANDOM_SIZE))
    202  1.1  christos                /* Skip session id */
    203  1.1  christos             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
    204  1.1  christos                /* Skip ciphers */
    205  1.1  christos             || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
    206  1.1  christos                /* Skip compression */
    207  1.1  christos             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
    208  1.1  christos                /* Extensions len */
    209  1.1  christos             || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
    210  1.1  christos         goto end;
    211  1.1  christos 
    212  1.1  christos     /* Loop through all extensions */
    213  1.1  christos     while (PACKET_remaining(&pkt2)) {
    214  1.1  christos 
    215  1.1  christos         if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
    216  1.1  christos                 || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
    217  1.1  christos             goto end;
    218  1.1  christos 
    219  1.1  christos         if (type == TLSEXT_TYPE_session_ticket) {
    220  1.1  christos             if (currtest == TEST_SET_SESSION_TICK_DATA_VER_NEG) {
    221  1.1  christos                 if (TEST_true(PACKET_equal(&pkt3, dummytick,
    222  1.1  christos                                            strlen(dummytick)))) {
    223  1.1  christos                     /* Ticket data is as we expected */
    224  1.1  christos                     testresult = 1;
    225  1.1  christos                 }
    226  1.1  christos                 goto end;
    227  1.1  christos             }
    228  1.1  christos         }
    229  1.1  christos         if (type == TLSEXT_TYPE_padding) {
    230  1.1  christos             if (!TEST_false(currtest == TEST_PADDING_NOT_NEEDED))
    231  1.1  christos                 goto end;
    232  1.1  christos             else if (TEST_true(currtest == TEST_ADD_PADDING
    233  1.1  christos                     || currtest == TEST_ADD_PADDING_AND_PSK))
    234  1.1  christos                 testresult = TEST_true(msglen == F5_WORKAROUND_MAX_MSG_LEN);
    235  1.1  christos         }
    236  1.1  christos     }
    237  1.1  christos 
    238  1.1  christos     if (currtest == TEST_PADDING_NOT_NEEDED)
    239  1.1  christos         testresult = 1;
    240  1.1  christos 
    241  1.1  christos end:
    242  1.1  christos     SSL_free(con);
    243  1.1  christos     SSL_CTX_free(ctx);
    244  1.1  christos     SSL_SESSION_free(sess);
    245  1.1  christos     BIO_free(sessbio);
    246  1.1  christos 
    247  1.1  christos     return testresult;
    248  1.1  christos }
    249  1.1  christos 
    250  1.1  christos int setup_tests(void)
    251  1.1  christos {
    252  1.1  christos     if (!TEST_ptr(sessionfile = test_get_argument(0)))
    253  1.1  christos         return 0;
    254  1.1  christos 
    255  1.1  christos     ADD_ALL_TESTS(test_client_hello, TOTAL_NUM_TESTS);
    256  1.1  christos     return 1;
    257  1.1  christos }
    258