Update copyright year
[openssl.git] / test / fatalerrtest.c
1 /*
2  * Copyright 2017-2018 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 (!create_ssl_ctx_pair(SSLv23_method(), SSLv23_method(),
32                              SSL3_VERSION, TLS_MAX_VERSION, &sctx, &cctx,
33                              cert, privkey)) {
34         printf("Failed to create SSL_CTX pair\n");
35         goto err;
36     }
37
38     /*
39      * Deliberately set the cipher lists for client and server to be different
40      * to force a handshake failure.
41      */
42     if (!SSL_CTX_set_cipher_list(sctx, "AES128-SHA")
43             || !SSL_CTX_set_cipher_list(cctx, "AES256-SHA")) {
44         printf("Failed to set cipher lists\n");
45         goto err;
46     }
47
48     if (!create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) {
49         printf("Failed to create SSL objectx\n");
50         goto err;
51     }
52
53     wbio = SSL_get_wbio(cssl);
54     if (wbio == NULL) {
55         printf("Unexpected NULL bio received\n");
56         goto err;
57     }
58
59     if (create_ssl_connection(sssl, cssl)) {
60         printf("Unexpected success creating a connection\n");
61         goto err;
62     }
63
64     ERR_clear_error();
65
66     /* Inject a plaintext record from client to server */
67     if (BIO_write(wbio, dummyrec, sizeof(dummyrec)) <= 0) {
68         printf("Unexpected failure injecting dummy record\n");
69         goto err;
70     }
71
72     /* SSL_read()/SSL_write should fail because of a previous fatal error */
73     if ((len = SSL_read(sssl, buf, sizeof(buf) - 1)) > 0) {
74         buf[len] = '\0';
75         printf("Unexpected success reading data: %s\n", buf);
76         goto err;
77     }
78     if (SSL_write(sssl, msg, strlen(msg)) > 0) {
79         printf("Unexpected success writing data\n");
80         goto err;
81     }
82
83     ret = 1;
84  err:
85     SSL_free(sssl);
86     SSL_free(cssl);
87     SSL_CTX_free(sctx);
88     SSL_CTX_free(cctx);
89
90     return ret;
91 }
92
93 int main(int argc, char *argv[])
94 {
95     BIO *err = NULL;
96     int testresult = 1;
97
98     if (argc != 3) {
99         printf("Invalid argument count\n");
100         return 1;
101     }
102
103     cert = argv[1];
104     privkey = argv[2];
105
106     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
107
108     CRYPTO_set_mem_debug(1);
109     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
110
111     ADD_TEST(test_fatalerr);
112
113     testresult = run_tests(argv[0]);
114
115 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
116     if (CRYPTO_mem_leaks(err) <= 0)
117         testresult = 1;
118 #endif
119     BIO_free(err);
120
121     if (!testresult)
122         printf("PASS\n");
123
124     return testresult;
125 }