Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (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 "helpers/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 = OPENSSL_memdup(in, inl)))
     45      1.1  christos             return 0;
     46      1.1  christos         /* corrupt last bit of application data */
     47  1.1.1.2  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.1.2  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.1.2  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.1.2  christos         || !TEST_ptr(ssl = SSL_new(ctx))
    149  1.1.1.2  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.1.2  christos     cipher_list = OPENSSL_malloc(sk_SSL_CIPHER_num(sk_ciphers) * sizeof(cipher_list[0]));
    158      1.1  christos     if (!TEST_ptr(cipher_list))
    159      1.1  christos         goto err;
    160      1.1  christos 
    161      1.1  christos     for (j = 0, i = 0; i < sk_SSL_CIPHER_num(sk_ciphers); i++) {
    162      1.1  christos         const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(sk_ciphers, i);
    163      1.1  christos 
    164      1.1  christos         if (SSL_CIPHER_get_auth_nid(cipher) == NID_auth_rsa)
    165      1.1  christos             cipher_list[j++] = SSL_CIPHER_get_name(cipher);
    166      1.1  christos     }
    167      1.1  christos     if (TEST_int_ne(j, 0))
    168      1.1  christos         numciphers = j;
    169      1.1  christos 
    170      1.1  christos err:
    171      1.1  christos     sk_SSL_CIPHER_free(sk_ciphers);
    172      1.1  christos     SSL_free(ssl);
    173      1.1  christos     SSL_CTX_free(ctx);
    174      1.1  christos 
    175      1.1  christos     return numciphers;
    176      1.1  christos }
    177      1.1  christos 
    178      1.1  christos static char *cert = NULL;
    179      1.1  christos static char *privkey = NULL;
    180      1.1  christos 
    181      1.1  christos static int test_ssl_corrupt(int testidx)
    182      1.1  christos {
    183      1.1  christos     static unsigned char junk[16000] = { 0 };
    184      1.1  christos     SSL_CTX *sctx = NULL, *cctx = NULL;
    185      1.1  christos     SSL *server = NULL, *client = NULL;
    186      1.1  christos     BIO *c_to_s_fbio;
    187      1.1  christos     int testresult = 0;
    188      1.1  christos     STACK_OF(SSL_CIPHER) *ciphers;
    189      1.1  christos     const SSL_CIPHER *currcipher;
    190      1.1  christos     int err;
    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(NULL, TLS_server_method(),
    197  1.1.1.2  christos             TLS_client_method(),
    198  1.1.1.2  christos             TLS1_VERSION, 0,
    199  1.1.1.2  christos             &sctx, &cctx, cert, privkey)))
    200      1.1  christos         return 0;
    201      1.1  christos 
    202      1.1  christos     if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1))
    203  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_cipher_list(cctx, cipher_list[testidx]))
    204  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ""))
    205  1.1.1.2  christos         || !TEST_ptr(ciphers = SSL_CTX_get_ciphers(cctx))
    206  1.1.1.2  christos         || !TEST_int_eq(sk_SSL_CIPHER_num(ciphers), 1)
    207  1.1.1.2  christos         || !TEST_ptr(currcipher = sk_SSL_CIPHER_value(ciphers, 0)))
    208      1.1  christos         goto end;
    209      1.1  christos 
    210      1.1  christos     /*
    211      1.1  christos      * No ciphers we are using are TLSv1.3 compatible so we should not attempt
    212      1.1  christos      * to negotiate TLSv1.3
    213      1.1  christos      */
    214      1.1  christos     if (!TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION)))
    215      1.1  christos         goto end;
    216      1.1  christos 
    217      1.1  christos     if (!TEST_ptr(c_to_s_fbio = BIO_new(bio_f_tls_corrupt_filter())))
    218      1.1  christos         goto end;
    219      1.1  christos 
    220      1.1  christos     /* BIO is freed by create_ssl_connection on error */
    221      1.1  christos     if (!TEST_true(create_ssl_objects(sctx, cctx, &server, &client, NULL,
    222  1.1.1.2  christos             c_to_s_fbio)))
    223      1.1  christos         goto end;
    224      1.1  christos 
    225      1.1  christos     if (!TEST_true(create_ssl_connection(server, client, SSL_ERROR_NONE)))
    226      1.1  christos         goto end;
    227      1.1  christos 
    228      1.1  christos     docorrupt = 1;
    229      1.1  christos 
    230      1.1  christos     if (!TEST_int_ge(SSL_write(client, junk, sizeof(junk)), 0))
    231      1.1  christos         goto end;
    232      1.1  christos 
    233      1.1  christos     if (!TEST_int_lt(SSL_read(server, junk, sizeof(junk)), 0))
    234      1.1  christos         goto end;
    235      1.1  christos 
    236      1.1  christos     do {
    237      1.1  christos         err = ERR_get_error();
    238      1.1  christos 
    239      1.1  christos         if (err == 0) {
    240      1.1  christos             TEST_error("Decryption failed or bad record MAC not seen");
    241      1.1  christos             goto end;
    242      1.1  christos         }
    243      1.1  christos     } while (ERR_GET_REASON(err) != SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
    244      1.1  christos 
    245      1.1  christos     testresult = 1;
    246  1.1.1.2  christos end:
    247      1.1  christos     SSL_free(server);
    248      1.1  christos     SSL_free(client);
    249      1.1  christos     SSL_CTX_free(sctx);
    250      1.1  christos     SSL_CTX_free(cctx);
    251      1.1  christos     return testresult;
    252      1.1  christos }
    253      1.1  christos 
    254      1.1  christos OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
    255      1.1  christos 
    256      1.1  christos int setup_tests(void)
    257      1.1  christos {
    258      1.1  christos     int n;
    259      1.1  christos 
    260      1.1  christos     if (!test_skip_common_options()) {
    261      1.1  christos         TEST_error("Error parsing test options\n");
    262      1.1  christos         return 0;
    263      1.1  christos     }
    264      1.1  christos 
    265      1.1  christos     if (!TEST_ptr(cert = test_get_argument(0))
    266  1.1.1.2  christos         || !TEST_ptr(privkey = test_get_argument(1)))
    267      1.1  christos         return 0;
    268      1.1  christos 
    269      1.1  christos     n = setup_cipher_list();
    270      1.1  christos     if (n > 0)
    271      1.1  christos         ADD_ALL_TESTS(test_ssl_corrupt, n);
    272      1.1  christos     return 1;
    273      1.1  christos }
    274      1.1  christos 
    275      1.1  christos void cleanup_tests(void)
    276      1.1  christos {
    277      1.1  christos     bio_f_tls_corrupt_filter_free();
    278      1.1  christos     OPENSSL_free(cipher_list);
    279      1.1  christos }
    280