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