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