Home | History | Annotate | Line # | Download | only in test
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2016-2018 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 #include "ssltestlib.h"
     12  1.1  christos #include "testutil.h"
     13  1.1  christos 
     14  1.1  christos static int docorrupt = 0;
     15  1.1  christos 
     16  1.1  christos static void copy_flags(BIO *bio)
     17  1.1  christos {
     18  1.1  christos     int flags;
     19  1.1  christos     BIO *next = BIO_next(bio);
     20  1.1  christos 
     21  1.1  christos     flags = BIO_test_flags(next, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
     22  1.1  christos     BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_RWS);
     23  1.1  christos     BIO_set_flags(bio, flags);
     24  1.1  christos }
     25  1.1  christos 
     26  1.1  christos static int tls_corrupt_read(BIO *bio, char *out, int outl)
     27  1.1  christos {
     28  1.1  christos     int ret;
     29  1.1  christos     BIO *next = BIO_next(bio);
     30  1.1  christos 
     31  1.1  christos     ret = BIO_read(next, out, outl);
     32  1.1  christos     copy_flags(bio);
     33  1.1  christos 
     34  1.1  christos     return ret;
     35  1.1  christos }
     36  1.1  christos 
     37  1.1  christos static int tls_corrupt_write(BIO *bio, const char *in, int inl)
     38  1.1  christos {
     39  1.1  christos     int ret;
     40  1.1  christos     BIO *next = BIO_next(bio);
     41  1.1  christos     char *copy;
     42  1.1  christos 
     43  1.1  christos     if (docorrupt) {
     44  1.1  christos         if (!TEST_ptr(copy = BUF_memdup(in, inl)))
     45  1.1  christos             return 0;
     46  1.1  christos         /* corrupt last bit of application data */
     47  1.1  christos         copy[inl-1] ^= 1;
     48  1.1  christos         ret = BIO_write(next, copy, inl);
     49  1.1  christos         OPENSSL_free(copy);
     50  1.1  christos     } else {
     51  1.1  christos         ret = BIO_write(next, in, inl);
     52  1.1  christos     }
     53  1.1  christos     copy_flags(bio);
     54  1.1  christos 
     55  1.1  christos     return ret;
     56  1.1  christos }
     57  1.1  christos 
     58  1.1  christos static long tls_corrupt_ctrl(BIO *bio, int cmd, long num, void *ptr)
     59  1.1  christos {
     60  1.1  christos     long ret;
     61  1.1  christos     BIO *next = BIO_next(bio);
     62  1.1  christos 
     63  1.1  christos     if (next == NULL)
     64  1.1  christos         return 0;
     65  1.1  christos 
     66  1.1  christos     switch (cmd) {
     67  1.1  christos     case BIO_CTRL_DUP:
     68  1.1  christos         ret = 0L;
     69  1.1  christos         break;
     70  1.1  christos     default:
     71  1.1  christos         ret = BIO_ctrl(next, cmd, num, ptr);
     72  1.1  christos         break;
     73  1.1  christos     }
     74  1.1  christos     return ret;
     75  1.1  christos }
     76  1.1  christos 
     77  1.1  christos static int tls_corrupt_gets(BIO *bio, char *buf, int size)
     78  1.1  christos {
     79  1.1  christos     /* We don't support this - not needed anyway */
     80  1.1  christos     return -1;
     81  1.1  christos }
     82  1.1  christos 
     83  1.1  christos static int tls_corrupt_puts(BIO *bio, const char *str)
     84  1.1  christos {
     85  1.1  christos     /* We don't support this - not needed anyway */
     86  1.1  christos     return -1;
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos static int tls_corrupt_new(BIO *bio)
     90  1.1  christos {
     91  1.1  christos     BIO_set_init(bio, 1);
     92  1.1  christos 
     93  1.1  christos     return 1;
     94  1.1  christos }
     95  1.1  christos 
     96  1.1  christos static int tls_corrupt_free(BIO *bio)
     97  1.1  christos {
     98  1.1  christos     BIO_set_init(bio, 0);
     99  1.1  christos 
    100  1.1  christos     return 1;
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos #define BIO_TYPE_CUSTOM_FILTER  (0x80 | BIO_TYPE_FILTER)
    104  1.1  christos 
    105  1.1  christos static BIO_METHOD *method_tls_corrupt = NULL;
    106  1.1  christos 
    107  1.1  christos /* Note: Not thread safe! */
    108  1.1  christos static const BIO_METHOD *bio_f_tls_corrupt_filter(void)
    109  1.1  christos {
    110  1.1  christos     if (method_tls_corrupt == NULL) {
    111  1.1  christos         method_tls_corrupt = BIO_meth_new(BIO_TYPE_CUSTOM_FILTER,
    112  1.1  christos                                           "TLS corrupt filter");
    113  1.1  christos         if (   method_tls_corrupt == NULL
    114  1.1  christos             || !BIO_meth_set_write(method_tls_corrupt, tls_corrupt_write)
    115  1.1  christos             || !BIO_meth_set_read(method_tls_corrupt, tls_corrupt_read)
    116  1.1  christos             || !BIO_meth_set_puts(method_tls_corrupt, tls_corrupt_puts)
    117  1.1  christos             || !BIO_meth_set_gets(method_tls_corrupt, tls_corrupt_gets)
    118  1.1  christos             || !BIO_meth_set_ctrl(method_tls_corrupt, tls_corrupt_ctrl)
    119  1.1  christos             || !BIO_meth_set_create(method_tls_corrupt, tls_corrupt_new)
    120  1.1  christos             || !BIO_meth_set_destroy(method_tls_corrupt, tls_corrupt_free))
    121  1.1  christos             return NULL;
    122  1.1  christos     }
    123  1.1  christos     return method_tls_corrupt;
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos static void bio_f_tls_corrupt_filter_free(void)
    127  1.1  christos {
    128  1.1  christos     BIO_meth_free(method_tls_corrupt);
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos /*
    132  1.1  christos  * The test is supposed to be executed with RSA key, customarily
    133  1.1  christos  * with apps/server.pem used even in other tests. For this reason
    134  1.1  christos  * |cipher_list| is initialized with RSA ciphers' names. This
    135  1.1  christos  * naturally means that if test is to be re-purposed for other
    136  1.1  christos  * type of key, then NID_auth_* filter below would need adjustment.
    137  1.1  christos  */
    138  1.1  christos static const char **cipher_list = NULL;
    139  1.1  christos 
    140  1.1  christos static int setup_cipher_list(void)
    141  1.1  christos {
    142  1.1  christos     SSL_CTX *ctx = NULL;
    143  1.1  christos     SSL *ssl = NULL;
    144  1.1  christos     STACK_OF(SSL_CIPHER) *sk_ciphers = NULL;
    145  1.1  christos     int i, j, numciphers = 0;
    146  1.1  christos 
    147  1.1  christos     if (!TEST_ptr(ctx = SSL_CTX_new(TLS_server_method()))
    148  1.1  christos             || !TEST_ptr(ssl = SSL_new(ctx))
    149  1.1  christos             || !TEST_ptr(sk_ciphers = SSL_get1_supported_ciphers(ssl)))
    150  1.1  christos         goto err;
    151  1.1  christos 
    152  1.1  christos     /*
    153  1.1  christos      * The |cipher_list| will be filled only with names of RSA ciphers,
    154  1.1  christos      * so that some of the allocated space will be wasted, but the loss
    155  1.1  christos      * is deemed acceptable...
    156  1.1  christos      */
    157  1.1  christos     cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) *
    158  1.1  christos                                  sizeof(cipher_list[0]));
    159  1.1  christos     if (!TEST_ptr(cipher_list))
    160  1.1  christos         goto err;
    161  1.1  christos 
    162  1.1  christos     for (j = 0, i = 0; i < sk_SSL_CIPHER_num(sk_ciphers); i++) {
    163  1.1  christos         const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(sk_ciphers, i);
    164  1.1  christos 
    165  1.1  christos         if (SSL_CIPHER_get_auth_nid(cipher) == NID_auth_rsa)
    166  1.1  christos             cipher_list[j++] = SSL_CIPHER_get_name(cipher);
    167  1.1  christos     }
    168  1.1  christos     if (TEST_int_ne(j, 0))
    169  1.1  christos         numciphers = j;
    170  1.1  christos 
    171  1.1  christos err:
    172  1.1  christos     sk_SSL_CIPHER_free(sk_ciphers);
    173  1.1  christos     SSL_free(ssl);
    174  1.1  christos     SSL_CTX_free(ctx);
    175  1.1  christos 
    176  1.1  christos     return numciphers;
    177  1.1  christos }
    178  1.1  christos 
    179  1.1  christos static char *cert = NULL;
    180  1.1  christos static char *privkey = NULL;
    181  1.1  christos 
    182  1.1  christos static int test_ssl_corrupt(int testidx)
    183  1.1  christos {
    184  1.1  christos     static unsigned char junk[16000] = { 0 };
    185  1.1  christos     SSL_CTX *sctx = NULL, *cctx = NULL;
    186  1.1  christos     SSL *server = NULL, *client = NULL;
    187  1.1  christos     BIO *c_to_s_fbio;
    188  1.1  christos     int testresult = 0;
    189  1.1  christos     STACK_OF(SSL_CIPHER) *ciphers;
    190  1.1  christos     const SSL_CIPHER *currcipher;
    191  1.1  christos 
    192  1.1  christos     docorrupt = 0;
    193  1.1  christos 
    194  1.1  christos     TEST_info("Starting #%d, %s", testidx, cipher_list[testidx]);
    195  1.1  christos 
    196  1.1  christos     if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
    197  1.1  christos                                        TLS1_VERSION, TLS_MAX_VERSION,
    198  1.1  christos                                        &sctx, &cctx, cert, privkey)))
    199  1.1  christos         return 0;
    200  1.1  christos 
    201  1.1  christos     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher_list[testidx]))
    202  1.1  christos             || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ""))
    203  1.1  christos             || !TEST_ptr(ciphers = SSL_CTX_get_ciphers(cctx))
    204  1.1  christos             || !TEST_int_eq(sk_SSL_CIPHER_num(ciphers), 1)
    205  1.1  christos             || !TEST_ptr(currcipher = sk_SSL_CIPHER_value(ciphers, 0)))
    206  1.1  christos         goto end;
    207  1.1  christos 
    208  1.1  christos     /*
    209  1.1  christos      * No ciphers we are using are TLSv1.3 compatible so we should not attempt
    210  1.1  christos      * to negotiate TLSv1.3
    211  1.1  christos      */
    212  1.1  christos     if (!TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)))
    213  1.1  christos         goto end;
    214  1.1  christos 
    215  1.1  christos     if (!TEST_ptr(c_to_s_fbio = BIO_new(bio_f_tls_corrupt_filter())))
    216  1.1  christos         goto end;
    217  1.1  christos 
    218  1.1  christos     /* BIO is freed by create_ssl_connection on error */
    219  1.1  christos     if (!TEST_true(create_ssl_objects(sctx, cctx, &server, &client, NULL,
    220  1.1  christos                                       c_to_s_fbio)))
    221  1.1  christos         goto end;
    222  1.1  christos 
    223  1.1  christos     if (!TEST_true(create_ssl_connection(server, client, SSL_ERROR_NONE)))
    224  1.1  christos         goto end;
    225  1.1  christos 
    226  1.1  christos     docorrupt = 1;
    227  1.1  christos 
    228  1.1  christos     if (!TEST_int_ge(SSL_write(client, junk, sizeof(junk)), 0))
    229  1.1  christos         goto end;
    230  1.1  christos 
    231  1.1  christos     if (!TEST_int_lt(SSL_read(server, junk, sizeof(junk)), 0))
    232  1.1  christos         goto end;
    233  1.1  christos 
    234  1.1  christos     if (!TEST_int_eq(ERR_GET_REASON(ERR_peek_error()),
    235  1.1  christos                      SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC))
    236  1.1  christos         goto end;
    237  1.1  christos 
    238  1.1  christos     testresult = 1;
    239  1.1  christos  end:
    240  1.1  christos     SSL_free(server);
    241  1.1  christos     SSL_free(client);
    242  1.1  christos     SSL_CTX_free(sctx);
    243  1.1  christos     SSL_CTX_free(cctx);
    244  1.1  christos     return testresult;
    245  1.1  christos }
    246  1.1  christos 
    247  1.1  christos int setup_tests(void)
    248  1.1  christos {
    249  1.1  christos     int n;
    250  1.1  christos 
    251  1.1  christos     if (!TEST_ptr(cert = test_get_argument(0))
    252  1.1  christos             || !TEST_ptr(privkey = test_get_argument(1))) {
    253  1.1  christos         TEST_note("Usage error: require cert and private key files");
    254  1.1  christos         return 0;
    255  1.1  christos     }
    256  1.1  christos 
    257  1.1  christos     n = setup_cipher_list();
    258  1.1  christos     if (n > 0)
    259  1.1  christos         ADD_ALL_TESTS(test_ssl_corrupt, n);
    260  1.1  christos     return 1;
    261  1.1  christos }
    262  1.1  christos 
    263  1.1  christos void cleanup_tests(void)
    264  1.1  christos {
    265  1.1  christos     bio_f_tls_corrupt_filter_free();
    266  1.1  christos     OPENSSL_free(cipher_list);
    267  1.1  christos }
    268