Fix no-tls1_2
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016-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/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 static unsigned int timer_cb_count;
21
22 #define NUM_TESTS   2
23
24
25 #define DUMMY_CERT_STATUS_LEN  12
26
27 static unsigned char certstatus[] = {
28     SSL3_RT_HANDSHAKE, /* Content type */
29     0xfe, 0xfd, /* Record version */
30     0, 1, /* Epoch */
31     0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
32     0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
33     SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
34     0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
35     0, 5, /* Message sequence */
36     0, 0, 0, /* Fragment offset */
37     0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
38     0x80, 0x80, 0x80, 0x80, 0x80,
39     0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
40 };
41
42 #define RECORD_SEQUENCE 10
43
44 static unsigned int timer_cb(SSL *s, unsigned int timer_us)
45 {
46     ++timer_cb_count;
47
48     if (timer_us == 0)
49         return 50000;
50     else
51         return 2 * timer_us;
52 }
53
54 static int test_dtls_unprocessed(int testidx)
55 {
56     SSL_CTX *sctx = NULL, *cctx = NULL;
57     SSL *serverssl1 = NULL, *clientssl1 = NULL;
58     BIO *c_to_s_fbio, *c_to_s_mempacket;
59     int testresult = 0;
60
61     timer_cb_count = 0;
62
63     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
64                                        DTLS_client_method(),
65                                        DTLS1_VERSION, DTLS_MAX_VERSION,
66                                        &sctx, &cctx, cert, privkey)))
67         return 0;
68
69     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
70         goto end;
71
72     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
73     if (!TEST_ptr(c_to_s_fbio))
74         goto end;
75
76     /* BIO is freed by create_ssl_connection on error */
77     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
78                                       NULL, c_to_s_fbio)))
79         goto end;
80
81     DTLS_set_timer_cb(clientssl1, timer_cb);
82
83     if (testidx == 1)
84         certstatus[RECORD_SEQUENCE] = 0xff;
85
86     /*
87      * Inject a dummy record from the next epoch. In test 0, this should never
88      * get used because the message sequence number is too big. In test 1 we set
89      * the record sequence number to be way off in the future. This should not
90      * have an impact on the record replay protection because the record should
91      * be dropped before it is marked as arrived
92      */
93     c_to_s_mempacket = SSL_get_wbio(clientssl1);
94     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
95     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
96                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
97
98     if (!TEST_true(create_ssl_connection(serverssl1, clientssl1,
99                                          SSL_ERROR_NONE)))
100         goto end;
101
102     if (timer_cb_count == 0) {
103         printf("timer_callback was not called.\n");
104         goto end;
105     }
106
107     testresult = 1;
108  end:
109     SSL_free(serverssl1);
110     SSL_free(clientssl1);
111     SSL_CTX_free(sctx);
112     SSL_CTX_free(cctx);
113
114     return testresult;
115 }
116
117 #define CLI_TO_SRV_EPOCH_0_RECS 3
118 #define CLI_TO_SRV_EPOCH_1_RECS 1
119 #define SRV_TO_CLI_EPOCH_0_RECS 12
120 #define SRV_TO_CLI_EPOCH_1_RECS 1
121 #define TOTAL_FULL_HAND_RECORDS \
122             (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
123              SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
124
125 #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
126 #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
127 #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
128 #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
129 #define TOTAL_RESUME_HAND_RECORDS \
130             (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
131              SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
132
133 #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
134
135 static int test_dtls_drop_records(int idx)
136 {
137     SSL_CTX *sctx = NULL, *cctx = NULL;
138     SSL *serverssl = NULL, *clientssl = NULL;
139     BIO *c_to_s_fbio, *mempackbio;
140     int testresult = 0;
141     int epoch = 0;
142     SSL_SESSION *sess = NULL;
143     int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
144
145     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
146                                        DTLS_client_method(),
147                                        DTLS1_VERSION, DTLS_MAX_VERSION,
148                                        &sctx, &cctx, cert, privkey)))
149         return 0;
150
151     if (idx >= TOTAL_FULL_HAND_RECORDS) {
152         /* We're going to do a resumption handshake. Get a session first. */
153         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
154                                           NULL, NULL))
155                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
156                               SSL_ERROR_NONE))
157                 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
158             goto end;
159
160         SSL_shutdown(clientssl);
161         SSL_shutdown(serverssl);
162         SSL_free(serverssl);
163         SSL_free(clientssl);
164         serverssl = clientssl = NULL;
165
166         cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
167         cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
168         srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
169         idx -= TOTAL_FULL_HAND_RECORDS;
170     } else {
171         cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
172         cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
173         srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
174     }
175
176     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
177     if (!TEST_ptr(c_to_s_fbio))
178         goto end;
179
180     /* BIO is freed by create_ssl_connection on error */
181     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
182                                       NULL, c_to_s_fbio)))
183         goto end;
184
185     if (sess != NULL) {
186         if (!TEST_true(SSL_set_session(clientssl, sess)))
187             goto end;
188     }
189
190     DTLS_set_timer_cb(clientssl, timer_cb);
191     DTLS_set_timer_cb(serverssl, timer_cb);
192
193     /* Work out which record to drop based on the test number */
194     if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
195         mempackbio = SSL_get_wbio(serverssl);
196         idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
197         if (idx >= srv_to_cli_epoch0) {
198             epoch = 1;
199             idx -= srv_to_cli_epoch0;
200         }
201     } else {
202         mempackbio = SSL_get_wbio(clientssl);
203         if (idx >= cli_to_srv_epoch0) {
204             epoch = 1;
205             idx -= cli_to_srv_epoch0;
206         }
207          mempackbio = BIO_next(mempackbio);
208     }
209     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
210     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
211
212     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
213         goto end;
214
215     if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
216         goto end;
217
218     /* If the test did what we planned then it should have dropped a record */
219     if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
220                                    NULL), -1))
221         goto end;
222
223     testresult = 1;
224  end:
225     SSL_SESSION_free(sess);
226     SSL_free(serverssl);
227     SSL_free(clientssl);
228     SSL_CTX_free(sctx);
229     SSL_CTX_free(cctx);
230
231     return testresult;
232 }
233
234 int setup_tests(void)
235 {
236     if (!TEST_ptr(cert = test_get_argument(0))
237             || !TEST_ptr(privkey = test_get_argument(1)))
238         return 0;
239
240     ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
241     ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
242
243     return 1;
244 }
245
246 void cleanup_tests(void)
247 {
248     bio_f_tls_dump_filter_free();
249     bio_s_mempacket_test_free();
250 }