xref: /freebsd/crypto/openssl/test/fatalerrtest.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <openssl/ssl.h>
11*e0c4386eSCy Schubert #include <openssl/err.h>
12*e0c4386eSCy Schubert #include "helpers/ssltestlib.h"
13*e0c4386eSCy Schubert #include "testutil.h"
14*e0c4386eSCy Schubert #include <string.h>
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert static char *cert = NULL;
17*e0c4386eSCy Schubert static char *privkey = NULL;
18*e0c4386eSCy Schubert 
test_fatalerr(void)19*e0c4386eSCy Schubert static int test_fatalerr(void)
20*e0c4386eSCy Schubert {
21*e0c4386eSCy Schubert     SSL_CTX *sctx = NULL, *cctx = NULL;
22*e0c4386eSCy Schubert     SSL *sssl = NULL, *cssl = NULL;
23*e0c4386eSCy Schubert     const char *msg = "Dummy";
24*e0c4386eSCy Schubert     BIO *wbio = NULL;
25*e0c4386eSCy Schubert     int ret = 0, len;
26*e0c4386eSCy Schubert     char buf[80];
27*e0c4386eSCy Schubert     unsigned char dummyrec[] = {
28*e0c4386eSCy Schubert         0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y'
29*e0c4386eSCy Schubert     };
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert     if (!TEST_true(create_ssl_ctx_pair(NULL, TLS_method(), TLS_method(),
32*e0c4386eSCy Schubert                                        TLS1_VERSION, 0,
33*e0c4386eSCy Schubert                                        &sctx, &cctx, cert, privkey)))
34*e0c4386eSCy Schubert         goto err;
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert     /*
37*e0c4386eSCy Schubert      * Deliberately set the cipher lists for client and server to be different
38*e0c4386eSCy Schubert      * to force a handshake failure.
39*e0c4386eSCy Schubert      */
40*e0c4386eSCy Schubert     if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA"))
41*e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA"))
42*e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
43*e0c4386eSCy Schubert                                                    "TLS_AES_128_GCM_SHA256"))
44*e0c4386eSCy Schubert             || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
45*e0c4386eSCy Schubert                                                    "TLS_AES_256_GCM_SHA384"))
46*e0c4386eSCy Schubert             || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,
47*e0c4386eSCy Schubert                           NULL)))
48*e0c4386eSCy Schubert         goto err;
49*e0c4386eSCy Schubert 
50*e0c4386eSCy Schubert     wbio = SSL_get_wbio(cssl);
51*e0c4386eSCy Schubert     if (!TEST_ptr(wbio)) {
52*e0c4386eSCy Schubert         printf("Unexpected NULL bio received\n");
53*e0c4386eSCy Schubert         goto err;
54*e0c4386eSCy Schubert     }
55*e0c4386eSCy Schubert 
56*e0c4386eSCy Schubert     /* Connection should fail */
57*e0c4386eSCy Schubert     if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
58*e0c4386eSCy Schubert         goto err;
59*e0c4386eSCy Schubert 
60*e0c4386eSCy Schubert     ERR_clear_error();
61*e0c4386eSCy Schubert 
62*e0c4386eSCy Schubert     /* Inject a plaintext record from client to server */
63*e0c4386eSCy Schubert     if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))
64*e0c4386eSCy Schubert         goto err;
65*e0c4386eSCy Schubert 
66*e0c4386eSCy Schubert     /* SSL_read()/SSL_write should fail because of a previous fatal error */
67*e0c4386eSCy Schubert     if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {
68*e0c4386eSCy Schubert         buf[len] = '\0';
69*e0c4386eSCy Schubert         TEST_error("Unexpected success reading data: %s\n", buf);
70*e0c4386eSCy Schubert         goto err;
71*e0c4386eSCy Schubert     }
72*e0c4386eSCy Schubert     if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))
73*e0c4386eSCy Schubert         goto err;
74*e0c4386eSCy Schubert 
75*e0c4386eSCy Schubert     ret = 1;
76*e0c4386eSCy Schubert  err:
77*e0c4386eSCy Schubert     SSL_free(sssl);
78*e0c4386eSCy Schubert     SSL_free(cssl);
79*e0c4386eSCy Schubert     SSL_CTX_free(sctx);
80*e0c4386eSCy Schubert     SSL_CTX_free(cctx);
81*e0c4386eSCy Schubert 
82*e0c4386eSCy Schubert     return ret;
83*e0c4386eSCy Schubert }
84*e0c4386eSCy Schubert 
85*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
86*e0c4386eSCy Schubert 
setup_tests(void)87*e0c4386eSCy Schubert int setup_tests(void)
88*e0c4386eSCy Schubert {
89*e0c4386eSCy Schubert     if (!test_skip_common_options()) {
90*e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
91*e0c4386eSCy Schubert         return 0;
92*e0c4386eSCy Schubert     }
93*e0c4386eSCy Schubert 
94*e0c4386eSCy Schubert     if (!TEST_ptr(cert = test_get_argument(0))
95*e0c4386eSCy Schubert             || !TEST_ptr(privkey = test_get_argument(1)))
96*e0c4386eSCy Schubert         return 0;
97*e0c4386eSCy Schubert 
98*e0c4386eSCy Schubert     ADD_TEST(test_fatalerr);
99*e0c4386eSCy Schubert 
100*e0c4386eSCy Schubert     return 1;
101*e0c4386eSCy Schubert }
102