Fix DTLS unprocessed records bug
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016 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/bio.h>
11 #include <openssl/crypto.h>
12 #include <openssl/ssl.h>
13 #include <openssl/err.h>
14
15 #include "ssltestlib.h"
16 #include "testutil.h"
17
18 static char *cert = NULL;
19 static char *privkey = NULL;
20
21
22 #define DUMMY_CERT_STATUS_LEN  12
23
24 unsigned char certstatus[] = {
25     SSL3_RT_HANDSHAKE, /* Content type */
26     0xfe, 0xfd, /* Record version */
27     0, 1, /* Epoch */
28     0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
29     0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
30     SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
31     0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
32     0, 5, /* Message sequence */
33     0, 0, 0, /* Fragment offset */
34     0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
35     0x80, 0x80, 0x80, 0x80, 0x80,
36     0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
37 };
38
39 static int test_dtls_unprocessed(void)
40 {
41     SSL_CTX *sctx = NULL, *cctx = NULL;
42     SSL *serverssl1 = NULL, *clientssl1 = NULL;
43     BIO *c_to_s_fbio, *c_to_s_mempacket;
44     int testresult = 0;
45
46     if (!create_ssl_ctx_pair(DTLS_server_method(), DTLS_client_method(), &sctx,
47                              &cctx, cert, privkey)) {
48         printf("Unable to create SSL_CTX pair\n");
49         return 0;
50     }
51
52     if (!SSL_CTX_set_cipher_list(cctx, "ECDHE-RSA-AES256-SHA384")) {
53         printf("Failed setting cipher list\n");
54     }
55
56     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
57     if (c_to_s_fbio == NULL) {
58         printf("Failed to create filter BIO\n");
59         goto end;
60     }
61
62     /* BIO is freed by create_ssl_connection on error */
63     if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,
64                                c_to_s_fbio)) {
65         printf("Unable to create SSL objects\n");
66         ERR_print_errors_fp(stdout);
67         goto end;
68     }
69
70     /*
71      * Inject a dummy record from the next epoch. This should never get used
72      * because the message sequence number is too big
73      */
74     c_to_s_mempacket = SSL_get_wbio(clientssl1);
75     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
76     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
77                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
78
79     if (!create_ssl_connection(serverssl1, clientssl1)) {
80         printf("Unable to create SSL connection\n");
81         ERR_print_errors_fp(stdout);
82         goto end;
83     }
84
85     testresult = 1;
86  end:
87     SSL_free(serverssl1);
88     SSL_free(clientssl1);
89     SSL_CTX_free(sctx);
90     SSL_CTX_free(cctx);
91
92     return testresult;
93 }
94
95 int main(int argc, char *argv[])
96 {
97     BIO *err = NULL;
98     int testresult = 1;
99
100     if (argc != 3) {
101         printf("Invalid argument count\n");
102         return 1;
103     }
104
105     cert = argv[1];
106     privkey = argv[2];
107
108     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
109
110     CRYPTO_set_mem_debug(1);
111     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
112
113     ADD_TEST(test_dtls_unprocessed);
114
115     testresult = run_tests(argv[0]);
116
117     bio_f_tls_dump_filter_free();
118     bio_s_mempacket_test_free();
119
120 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
121     if (CRYPTO_mem_leaks(err) <= 0)
122         testresult = 1;
123 #endif
124     BIO_free(err);
125
126     if (!testresult)
127         printf("PASS\n");
128
129     return testresult;
130 }