efdf842e2db449db69880356149789b06de606a5
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <string.h>
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
14 #include <openssl/err.h>
15
16 #include "ssltestlib.h"
17 #include "testutil.h"
18
19 static char *cert = NULL;
20 static char *privkey = NULL;
21 static unsigned int timer_cb_count;
22
23 #define NUM_TESTS   2
24
25
26 #define DUMMY_CERT_STATUS_LEN  12
27
28 static unsigned char certstatus[] = {
29     SSL3_RT_HANDSHAKE, /* Content type */
30     0xfe, 0xfd, /* Record version */
31     0, 1, /* Epoch */
32     0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
33     0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
34     SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
35     0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
36     0, 5, /* Message sequence */
37     0, 0, 0, /* Fragment offset */
38     0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
39     0x80, 0x80, 0x80, 0x80, 0x80,
40     0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
41 };
42
43 #define RECORD_SEQUENCE 10
44
45 static unsigned int timer_cb(SSL *s, unsigned int timer_us)
46 {
47     ++timer_cb_count;
48
49     if (timer_us == 0)
50         return 50000;
51     else
52         return 2 * timer_us;
53 }
54
55 static int test_dtls_unprocessed(int testidx)
56 {
57     SSL_CTX *sctx = NULL, *cctx = NULL;
58     SSL *serverssl1 = NULL, *clientssl1 = NULL;
59     BIO *c_to_s_fbio, *c_to_s_mempacket;
60     int testresult = 0;
61
62     timer_cb_count = 0;
63
64     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
65                                        DTLS_client_method(),
66                                        DTLS1_VERSION, 0,
67                                        &sctx, &cctx, cert, privkey)))
68         return 0;
69
70     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
71         goto end;
72
73     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
74     if (!TEST_ptr(c_to_s_fbio))
75         goto end;
76
77     /* BIO is freed by create_ssl_connection on error */
78     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
79                                       NULL, c_to_s_fbio)))
80         goto end;
81
82     DTLS_set_timer_cb(clientssl1, timer_cb);
83
84     if (testidx == 1)
85         certstatus[RECORD_SEQUENCE] = 0xff;
86
87     /*
88      * Inject a dummy record from the next epoch. In test 0, this should never
89      * get used because the message sequence number is too big. In test 1 we set
90      * the record sequence number to be way off in the future.
91      */
92     c_to_s_mempacket = SSL_get_wbio(clientssl1);
93     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
94     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
95                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
96
97     /*
98      * Create the connection. We use "create_bare_ssl_connection" here so that
99      * we can force the connection to not do "SSL_read" once partly connected.
100      * We don't want to accidentally read the dummy records we injected because
101      * they will fail to decrypt.
102      */
103     if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
104                                               SSL_ERROR_NONE, 0)))
105         goto end;
106
107     if (timer_cb_count == 0) {
108         printf("timer_callback was not called.\n");
109         goto end;
110     }
111
112     testresult = 1;
113  end:
114     SSL_free(serverssl1);
115     SSL_free(clientssl1);
116     SSL_CTX_free(sctx);
117     SSL_CTX_free(cctx);
118
119     return testresult;
120 }
121
122 #define CLI_TO_SRV_EPOCH_0_RECS 3
123 #define CLI_TO_SRV_EPOCH_1_RECS 1
124 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
125 # define SRV_TO_CLI_EPOCH_0_RECS 10
126 #else
127 /*
128  * In this case we have no ServerKeyExchange message, because we don't have
129  * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
130  * test.
131  */
132 # define SRV_TO_CLI_EPOCH_0_RECS 9
133 #endif
134 #define SRV_TO_CLI_EPOCH_1_RECS 1
135 #define TOTAL_FULL_HAND_RECORDS \
136             (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
137              SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
138
139 #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
140 #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
141 #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
142 #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
143 #define TOTAL_RESUME_HAND_RECORDS \
144             (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
145              SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
146
147 #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
148
149 static int test_dtls_drop_records(int idx)
150 {
151     SSL_CTX *sctx = NULL, *cctx = NULL;
152     SSL *serverssl = NULL, *clientssl = NULL;
153     BIO *c_to_s_fbio, *mempackbio;
154     int testresult = 0;
155     int epoch = 0;
156     SSL_SESSION *sess = NULL;
157     int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
158
159     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
160                                        DTLS_client_method(),
161                                        DTLS1_VERSION, 0,
162                                        &sctx, &cctx, cert, privkey)))
163         return 0;
164
165     if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
166         goto end;
167
168     if (idx >= TOTAL_FULL_HAND_RECORDS) {
169         /* We're going to do a resumption handshake. Get a session first. */
170         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
171                                           NULL, NULL))
172                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
173                               SSL_ERROR_NONE))
174                 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
175             goto end;
176
177         SSL_shutdown(clientssl);
178         SSL_shutdown(serverssl);
179         SSL_free(serverssl);
180         SSL_free(clientssl);
181         serverssl = clientssl = NULL;
182
183         cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
184         cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
185         srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
186         idx -= TOTAL_FULL_HAND_RECORDS;
187     } else {
188         cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
189         cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
190         srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
191     }
192
193     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
194     if (!TEST_ptr(c_to_s_fbio))
195         goto end;
196
197     /* BIO is freed by create_ssl_connection on error */
198     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
199                                       NULL, c_to_s_fbio)))
200         goto end;
201
202     if (sess != NULL) {
203         if (!TEST_true(SSL_set_session(clientssl, sess)))
204             goto end;
205     }
206
207     DTLS_set_timer_cb(clientssl, timer_cb);
208     DTLS_set_timer_cb(serverssl, timer_cb);
209
210     /* Work out which record to drop based on the test number */
211     if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
212         mempackbio = SSL_get_wbio(serverssl);
213         idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
214         if (idx >= srv_to_cli_epoch0) {
215             epoch = 1;
216             idx -= srv_to_cli_epoch0;
217         }
218     } else {
219         mempackbio = SSL_get_wbio(clientssl);
220         if (idx >= cli_to_srv_epoch0) {
221             epoch = 1;
222             idx -= cli_to_srv_epoch0;
223         }
224          mempackbio = BIO_next(mempackbio);
225     }
226     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
227     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
228
229     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
230         goto end;
231
232     if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
233         goto end;
234
235     /* If the test did what we planned then it should have dropped a record */
236     if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
237                                    NULL), -1))
238         goto end;
239
240     testresult = 1;
241  end:
242     SSL_SESSION_free(sess);
243     SSL_free(serverssl);
244     SSL_free(clientssl);
245     SSL_CTX_free(sctx);
246     SSL_CTX_free(cctx);
247
248     return testresult;
249 }
250
251 static const char dummy_cookie[] = "0123456";
252
253 static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
254                               unsigned int *cookie_len)
255 {
256     memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
257     *cookie_len = sizeof(dummy_cookie);
258     return 1;
259 }
260
261 static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
262                             unsigned int cookie_len)
263 {
264     return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
265 }
266
267 static int test_cookie(void)
268 {
269     SSL_CTX *sctx = NULL, *cctx = NULL;
270     SSL *serverssl = NULL, *clientssl = NULL;
271     int testresult = 0;
272
273     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
274                                        DTLS_client_method(),
275                                        DTLS1_VERSION, 0,
276                                        &sctx, &cctx, cert, privkey)))
277         return 0;
278
279     SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
280     SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
281     SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
282
283     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
284                                       NULL, NULL))
285             || !TEST_true(create_ssl_connection(serverssl, clientssl,
286                                                 SSL_ERROR_NONE)))
287         goto end;
288
289     testresult = 1;
290  end:
291     SSL_free(serverssl);
292     SSL_free(clientssl);
293     SSL_CTX_free(sctx);
294     SSL_CTX_free(cctx);
295
296     return testresult;
297 }
298
299 static int test_dtls_duplicate_records(void)
300 {
301     SSL_CTX *sctx = NULL, *cctx = NULL;
302     SSL *serverssl = NULL, *clientssl = NULL;
303     int testresult = 0;
304
305     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
306                                        DTLS_client_method(),
307                                        DTLS1_VERSION, 0,
308                                        &sctx, &cctx, cert, privkey)))
309         return 0;
310
311     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
312                                       NULL, NULL)))
313         goto end;
314
315     DTLS_set_timer_cb(clientssl, timer_cb);
316     DTLS_set_timer_cb(serverssl, timer_cb);
317
318     BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
319     BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
320
321     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
322         goto end;
323
324     testresult = 1;
325  end:
326     SSL_free(serverssl);
327     SSL_free(clientssl);
328     SSL_CTX_free(sctx);
329     SSL_CTX_free(cctx);
330
331     return testresult;
332 }
333
334 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
335
336 int setup_tests(void)
337 {
338     if (!test_skip_common_options()) {
339         TEST_error("Error parsing test options\n");
340         return 0;
341     }
342
343     if (!TEST_ptr(cert = test_get_argument(0))
344             || !TEST_ptr(privkey = test_get_argument(1)))
345         return 0;
346
347     ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
348     ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
349     ADD_TEST(test_cookie);
350     ADD_TEST(test_dtls_duplicate_records);
351
352     return 1;
353 }
354
355 void cleanup_tests(void)
356 {
357     bio_f_tls_dump_filter_free();
358     bio_s_mempacket_test_free();
359 }