153060421Schristos /*
2*b0d17251Schristos * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
353060421Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
553060421Schristos * this file except in compliance with the License. You can obtain a copy
653060421Schristos * in the file LICENSE in the source distribution or at
753060421Schristos * https://www.openssl.org/source/license.html
853060421Schristos */
953060421Schristos
1053060421Schristos #include <openssl/ssl.h>
1153060421Schristos #include <openssl/err.h>
12*b0d17251Schristos #include "helpers/ssltestlib.h"
1353060421Schristos #include "testutil.h"
1453060421Schristos #include <string.h>
1553060421Schristos
1653060421Schristos static char *cert = NULL;
1753060421Schristos static char *privkey = NULL;
1853060421Schristos
test_fatalerr(void)1953060421Schristos static int test_fatalerr(void)
2053060421Schristos {
2153060421Schristos SSL_CTX *sctx = NULL, *cctx = NULL;
2253060421Schristos SSL *sssl = NULL, *cssl = NULL;
2353060421Schristos const char *msg = "Dummy";
2453060421Schristos BIO *wbio = NULL;
2553060421Schristos int ret = 0, len;
2653060421Schristos char buf[80];
2753060421Schristos unsigned char dummyrec[] = {
2853060421Schristos 0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y'
2953060421Schristos };
3053060421Schristos
31*b0d17251Schristos if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_method(), TLS_method(),
32*b0d17251Schristos TLS1_VERSION, 0,
3313d40330Schristos &sctx, &cctx, cert, privkey)))
3453060421Schristos goto err;
3553060421Schristos
3653060421Schristos /*
3753060421Schristos * Deliberately set the cipher lists for client and server to be different
3853060421Schristos * to force a handshake failure.
3953060421Schristos */
4013d40330Schristos if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA"))
4113d40330Schristos || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA"))
4213d40330Schristos || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
4313d40330Schristos "TLS_AES_128_GCM_SHA256"))
4413d40330Schristos || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
4513d40330Schristos "TLS_AES_256_GCM_SHA384"))
4613d40330Schristos || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,
4713d40330Schristos NULL)))
4853060421Schristos goto err;
4953060421Schristos
5053060421Schristos wbio = SSL_get_wbio(cssl);
5113d40330Schristos if (!TEST_ptr(wbio)) {
5253060421Schristos printf("Unexpected NULL bio received\n");
5353060421Schristos goto err;
5453060421Schristos }
5553060421Schristos
5613d40330Schristos /* Connection should fail */
5713d40330Schristos if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
5853060421Schristos goto err;
5953060421Schristos
6053060421Schristos ERR_clear_error();
6153060421Schristos
6253060421Schristos /* Inject a plaintext record from client to server */
6313d40330Schristos if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))
6453060421Schristos goto err;
6553060421Schristos
6653060421Schristos /* SSL_read()/SSL_write should fail because of a previous fatal error */
6713d40330Schristos if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {
6853060421Schristos buf[len] = '\0';
6913d40330Schristos TEST_error("Unexpected success reading data: %s\n", buf);
7053060421Schristos goto err;
7153060421Schristos }
7213d40330Schristos if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))
7353060421Schristos goto err;
7453060421Schristos
7553060421Schristos ret = 1;
7653060421Schristos err:
7753060421Schristos SSL_free(sssl);
7853060421Schristos SSL_free(cssl);
7953060421Schristos SSL_CTX_free(sctx);
8053060421Schristos SSL_CTX_free(cctx);
8153060421Schristos
8253060421Schristos return ret;
8353060421Schristos }
8453060421Schristos
85*b0d17251Schristos OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
86*b0d17251Schristos
setup_tests(void)8713d40330Schristos int setup_tests(void)
8853060421Schristos {
89*b0d17251Schristos if (!test_skip_common_options()) {
90*b0d17251Schristos TEST_error("Error parsing test options\n");
91*b0d17251Schristos return 0;
92*b0d17251Schristos }
93*b0d17251Schristos
9413d40330Schristos if (!TEST_ptr(cert = test_get_argument(0))
9513d40330Schristos || !TEST_ptr(privkey = test_get_argument(1)))
9613d40330Schristos return 0;
9753060421Schristos
9853060421Schristos ADD_TEST(test_fatalerr);
9953060421Schristos
10013d40330Schristos return 1;
10153060421Schristos }
102