fatalerrtest.c revision 326663
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
14int main(int argc, char *argv[])
15{
16    SSL_CTX *sctx, *cctx;
17    SSL *sssl, *cssl;
18    const char *msg = "Dummy";
19    BIO *err = NULL, *wbio = NULL;
20    int ret = 1, len;
21    char buf[80];
22    unsigned char dummyrec[] = {
23        0x17, 0x03, 0x03, 0x00, 0x05, 'D', 'u', 'm', 'm', 'y'
24    };
25
26    if (argc != 3) {
27        printf("Incorrect number of parameters\n");
28        return 1;
29    }
30
31    SSL_library_init();
32    SSL_load_error_strings();
33    err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
34    CRYPTO_malloc_debug_init();
35    CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
36    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
37
38    if (!create_ssl_ctx_pair(SSLv23_method(), SSLv23_method(), &sctx, &cctx,
39                             argv[1], argv[2])) {
40        printf("Failed to create SSL_CTX pair\n");
41        goto err;
42    }
43
44    /*
45     * Deliberately set the cipher lists for client and server to be different
46     * to force a handshake failure.
47     */
48    if (!SSL_CTX_set_cipher_list(sctx, "AES128-SHA")
49            || !SSL_CTX_set_cipher_list(cctx, "AES256-SHA")) {
50        printf("Failed to set cipher lists\n");
51        goto err;
52    }
53
54    if (!create_ssl_objects(sctx, cctx, &sssl, &cssl, NULL, NULL)) {
55        printf("Failed to create SSL objectx\n");
56        goto err;
57    }
58
59    wbio = SSL_get_wbio(cssl);
60    if (wbio == NULL) {
61        printf("Unexpected NULL bio received\n");
62        goto err;
63    }
64
65    if (create_ssl_connection(sssl, cssl)) {
66        printf("Unexpected success creating a connection\n");
67        goto err;
68    }
69
70    ERR_clear_error();
71
72    /* Inject a plaintext record from client to server */
73    if (BIO_write(wbio, dummyrec, sizeof(dummyrec)) <= 0) {
74        printf("Unexpected failure injecting dummy record\n");
75        goto err;
76    }
77
78    /* SSL_read()/SSL_write should fail because of a previous fatal error */
79    if ((len = SSL_read(sssl, buf, sizeof(buf - 1))) > 0) {
80        buf[len] = '\0';
81        printf("Unexpected success reading data: %s\n", buf);
82        goto err;
83    }
84    if (SSL_write(sssl, msg, strlen(msg)) > 0) {
85        printf("Unexpected success writing data\n");
86        goto err;
87    }
88
89    ret = 0;
90 err:
91    SSL_free(sssl);
92    SSL_free(cssl);
93    SSL_CTX_free(sctx);
94    SSL_CTX_free(cctx);
95    ERR_print_errors_fp(stderr);
96
97    if (ret) {
98        printf("Fatal err test: FAILED\n");
99    }
100
101    ERR_free_strings();
102    ERR_remove_thread_state(NULL);
103    EVP_cleanup();
104    CRYPTO_cleanup_all_ex_data();
105    CRYPTO_mem_leaks(err);
106    BIO_free(err);
107
108    return ret;
109}
110