Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2017-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 <openssl/ssl.h>
     11      1.1  christos #include <openssl/err.h>
     12      1.1  christos #include "helpers/ssltestlib.h"
     13      1.1  christos #include "testutil.h"
     14      1.1  christos #include <string.h>
     15      1.1  christos 
     16      1.1  christos static char *cert = NULL;
     17      1.1  christos static char *privkey = NULL;
     18      1.1  christos 
     19      1.1  christos static int test_fatalerr(void)
     20      1.1  christos {
     21      1.1  christos     SSL_CTX *sctx = NULL, *cctx = NULL;
     22      1.1  christos     SSL *sssl = NULL, *cssl = NULL;
     23      1.1  christos     const char *msg = "Dummy";
     24      1.1  christos     BIO *wbio = NULL;
     25      1.1  christos     int ret = 0, len;
     26      1.1  christos     char buf[80];
     27      1.1  christos     unsigned char dummyrec[] = {
     28      1.1  christos         0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y'
     29      1.1  christos     };
     30      1.1  christos 
     31      1.1  christos     if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_method(), TLS_method(),
     32  1.1.1.2  christos             TLS1_VERSION, 0,
     33  1.1.1.2  christos             &sctx, &cctx, cert, privkey)))
     34      1.1  christos         goto err;
     35      1.1  christos 
     36      1.1  christos     /*
     37      1.1  christos      * Deliberately set the cipher lists for client and server to be different
     38      1.1  christos      * to force a handshake failure.
     39      1.1  christos      */
     40      1.1  christos     if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA"))
     41  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA"))
     42  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
     43  1.1.1.2  christos             "TLS_AES_128_GCM_SHA256"))
     44  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
     45  1.1.1.2  christos             "TLS_AES_256_GCM_SHA384"))
     46  1.1.1.2  christos         || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,
     47  1.1.1.2  christos             NULL)))
     48      1.1  christos         goto err;
     49      1.1  christos 
     50      1.1  christos     wbio = SSL_get_wbio(cssl);
     51      1.1  christos     if (!TEST_ptr(wbio)) {
     52      1.1  christos         printf("Unexpected NULL bio received\n");
     53      1.1  christos         goto err;
     54      1.1  christos     }
     55      1.1  christos 
     56      1.1  christos     /* Connection should fail */
     57      1.1  christos     if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
     58      1.1  christos         goto err;
     59      1.1  christos 
     60      1.1  christos     ERR_clear_error();
     61      1.1  christos 
     62      1.1  christos     /* Inject a plaintext record from client to server */
     63      1.1  christos     if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))
     64      1.1  christos         goto err;
     65      1.1  christos 
     66      1.1  christos     /* SSL_read()/SSL_write should fail because of a previous fatal error */
     67      1.1  christos     if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {
     68      1.1  christos         buf[len] = '\0';
     69      1.1  christos         TEST_error("Unexpected success reading data: %s\n", buf);
     70      1.1  christos         goto err;
     71      1.1  christos     }
     72      1.1  christos     if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))
     73      1.1  christos         goto err;
     74      1.1  christos 
     75      1.1  christos     ret = 1;
     76  1.1.1.2  christos err:
     77      1.1  christos     SSL_free(sssl);
     78      1.1  christos     SSL_free(cssl);
     79      1.1  christos     SSL_CTX_free(sctx);
     80      1.1  christos     SSL_CTX_free(cctx);
     81      1.1  christos 
     82      1.1  christos     return ret;
     83      1.1  christos }
     84      1.1  christos 
     85      1.1  christos OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
     86      1.1  christos 
     87      1.1  christos int setup_tests(void)
     88      1.1  christos {
     89      1.1  christos     if (!test_skip_common_options()) {
     90      1.1  christos         TEST_error("Error parsing test options\n");
     91      1.1  christos         return 0;
     92      1.1  christos     }
     93      1.1  christos 
     94      1.1  christos     if (!TEST_ptr(cert = test_get_argument(0))
     95  1.1.1.2  christos         || !TEST_ptr(privkey = test_get_argument(1)))
     96      1.1  christos         return 0;
     97      1.1  christos 
     98      1.1  christos     ADD_TEST(test_fatalerr);
     99      1.1  christos 
    100      1.1  christos     return 1;
    101      1.1  christos }
    102