SHA512/224 and SHA512/256
[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(create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL,
42                           NULL)))
43         goto err;
44
45     wbio = SSL_get_wbio(cssl);
46     if (!TEST_ptr(wbio)) {
47         printf("Unexpected NULL bio received\n");
48         goto err;
49     }
50
51     /* Connection should fail */
52     if (!TEST_false(create_ssl_connection(sssl, cssl, SSL_ERROR_NONE)))
53         goto err;
54
55     ERR_clear_error();
56
57     /* Inject a plaintext record from client to server */
58     if (!TEST_int_gt(BIO_write(wbio, dummyrec, sizeof(dummyrec)), 0))
59         goto err;
60
61     /* SSL_read()/SSL_write should fail because of a previous fatal error */
62     if (!TEST_int_le(len = SSL_read(sssl, buf, sizeof(buf) - 1), 0)) {
63         buf[len] = '\0';
64         TEST_error("Unexpected success reading data: %s\n", buf);
65         goto err;
66     }
67     if (!TEST_int_le(SSL_write(sssl, msg, strlen(msg)), 0))
68         goto err;
69
70     ret = 1;
71  err:
72     SSL_free(sssl);
73     SSL_free(cssl);
74     SSL_CTX_free(sctx);
75     SSL_CTX_free(cctx);
76
77     return ret;
78 }
79
80 int setup_tests(void)
81 {
82     if (!TEST_ptr(cert = test_get_argument(0))
83             || !TEST_ptr(privkey = test_get_argument(1)))
84         return 0;
85
86     ADD_TEST(test_fatalerr);
87
88     return 1;
89 }