Fix bio callback backward compatibility
[openssl.git] / test / fatalerrtest.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "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(TLS_method(), TLS_method(), &sctx, &cctx,
32                                        cert, privkey)))
33         goto err;
34
35     /*
36      * Deliberately set the cipher lists for client and server to be different
37      * to force a handshake failure.
38      */
39     if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA"))
40             || !TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-SHA"))
41             || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
42                                                    "TLS_AES_128_GCM_SHA256"))
43             || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
44                                                    "TLS_AES_256_GCM_SHA384"))
45             || !TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,
46                           NULL)))
47         goto err;
48
49     wbio = SSL_get_wbio(cssl);
50     if (!TEST_ptr(wbio)) {
51         printf("Unexpected NULL bio received\n");
52         goto err;
53     }
54
55     /* Connection should fail */
56     if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
57         goto err;
58
59     ERR_clear_error();
60
61     /* Inject a plaintext record from client to server */
62     if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))
63         goto err;
64
65     /* SSL_read()/SSL_write should fail because of a previous fatal error */
66     if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {
67         buf[len] = '\0';
68         TEST_error("Unexpected success reading data: %s\n", buf);
69         goto err;
70     }
71     if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))
72         goto err;
73
74     ret = 1;
75  err:
76     SSL_free(sssl);
77     SSL_free(cssl);
78     SSL_CTX_free(sctx);
79     SSL_CTX_free(cctx);
80
81     return ret;
82 }
83
84 int setup_tests(void)
85 {
86     if (!TEST_ptr(cert = test_get_argument(0))
87             || !TEST_ptr(privkey = test_get_argument(1)))
88         return 0;
89
90     ADD_TEST(test_fatalerr);
91
92     return 1;
93 }