62aa856761a96812fc8639bb9cb9d1ad062e2277
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016-2018 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(DTLS_server_method(),
65                                        DTLS_client_method(),
66                                        DTLS1_VERSION, DTLS_MAX_VERSION,
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. This should not
91      * have an impact on the record replay protection because the record should
92      * be dropped before it is marked as arrived
93      */
94     c_to_s_mempacket = SSL_get_wbio(clientssl1);
95     c_to_s_mempacket = BIO_next(c_to_s_mempacket);
96     mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
97                           sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
98
99     if (!TEST_true(create_ssl_connection(serverssl1, clientssl1,
100                                          SSL_ERROR_NONE)))
101         goto end;
102
103     if (timer_cb_count == 0) {
104         printf("timer_callback was not called.\n");
105         goto end;
106     }
107
108     testresult = 1;
109  end:
110     SSL_free(serverssl1);
111     SSL_free(clientssl1);
112     SSL_CTX_free(sctx);
113     SSL_CTX_free(cctx);
114
115     return testresult;
116 }
117
118 #define CLI_TO_SRV_EPOCH_0_RECS 3
119 #define CLI_TO_SRV_EPOCH_1_RECS 1
120 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
121 # define SRV_TO_CLI_EPOCH_0_RECS 12
122 #else
123 /*
124  * In this case we have no ServerKeyExchange message, because we don't have
125  * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
126  * test.
127  */
128 # define SRV_TO_CLI_EPOCH_0_RECS 9
129 #endif
130 #define SRV_TO_CLI_EPOCH_1_RECS 1
131 #define TOTAL_FULL_HAND_RECORDS \
132             (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
133              SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
134
135 #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
136 #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
137 #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
138 #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
139 #define TOTAL_RESUME_HAND_RECORDS \
140             (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
141              SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
142
143 #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
144
145 static int test_dtls_drop_records(int idx)
146 {
147     SSL_CTX *sctx = NULL, *cctx = NULL;
148     SSL *serverssl = NULL, *clientssl = NULL;
149     BIO *c_to_s_fbio, *mempackbio;
150     int testresult = 0;
151     int epoch = 0;
152     SSL_SESSION *sess = NULL;
153     int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
154
155     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
156                                        DTLS_client_method(),
157                                        DTLS1_VERSION, DTLS_MAX_VERSION,
158                                        &sctx, &cctx, cert, privkey)))
159         return 0;
160
161     if (idx >= TOTAL_FULL_HAND_RECORDS) {
162         /* We're going to do a resumption handshake. Get a session first. */
163         if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
164                                           NULL, NULL))
165                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
166                               SSL_ERROR_NONE))
167                 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
168             goto end;
169
170         SSL_shutdown(clientssl);
171         SSL_shutdown(serverssl);
172         SSL_free(serverssl);
173         SSL_free(clientssl);
174         serverssl = clientssl = NULL;
175
176         cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
177         cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
178         srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
179         idx -= TOTAL_FULL_HAND_RECORDS;
180     } else {
181         cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
182         cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
183         srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
184     }
185
186     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
187     if (!TEST_ptr(c_to_s_fbio))
188         goto end;
189
190     /* BIO is freed by create_ssl_connection on error */
191     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
192                                       NULL, c_to_s_fbio)))
193         goto end;
194
195     if (sess != NULL) {
196         if (!TEST_true(SSL_set_session(clientssl, sess)))
197             goto end;
198     }
199
200     DTLS_set_timer_cb(clientssl, timer_cb);
201     DTLS_set_timer_cb(serverssl, timer_cb);
202
203     /* Work out which record to drop based on the test number */
204     if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
205         mempackbio = SSL_get_wbio(serverssl);
206         idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
207         if (idx >= srv_to_cli_epoch0) {
208             epoch = 1;
209             idx -= srv_to_cli_epoch0;
210         }
211     } else {
212         mempackbio = SSL_get_wbio(clientssl);
213         if (idx >= cli_to_srv_epoch0) {
214             epoch = 1;
215             idx -= cli_to_srv_epoch0;
216         }
217          mempackbio = BIO_next(mempackbio);
218     }
219     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
220     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
221
222     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
223         goto end;
224
225     if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
226         goto end;
227
228     /* If the test did what we planned then it should have dropped a record */
229     if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
230                                    NULL), -1))
231         goto end;
232
233     testresult = 1;
234  end:
235     SSL_SESSION_free(sess);
236     SSL_free(serverssl);
237     SSL_free(clientssl);
238     SSL_CTX_free(sctx);
239     SSL_CTX_free(cctx);
240
241     return testresult;
242 }
243
244 static const char dummy_cookie[] = "0123456";
245
246 static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
247                               unsigned int *cookie_len)
248 {
249     memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
250     *cookie_len = sizeof(dummy_cookie);
251     return 1;
252 }
253
254 static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
255                             unsigned int cookie_len)
256 {
257     return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
258 }
259
260 static int test_cookie(void)
261 {
262     SSL_CTX *sctx = NULL, *cctx = NULL;
263     SSL *serverssl = NULL, *clientssl = NULL;
264     int testresult = 0;
265
266     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
267                                        DTLS_client_method(),
268                                        DTLS1_VERSION, DTLS_MAX_VERSION,
269                                        &sctx, &cctx, cert, privkey)))
270         return 0;
271
272     SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
273     SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
274     SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
275
276     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
277                                       NULL, NULL))
278             || !TEST_true(create_ssl_connection(serverssl, clientssl,
279                                                 SSL_ERROR_NONE)))
280         goto end;
281
282     testresult = 1;
283  end:
284     SSL_free(serverssl);
285     SSL_free(clientssl);
286     SSL_CTX_free(sctx);
287     SSL_CTX_free(cctx);
288
289     return testresult;
290 }
291
292 static int test_dtls_duplicate_records(void)
293 {
294     SSL_CTX *sctx = NULL, *cctx = NULL;
295     SSL *serverssl = NULL, *clientssl = NULL;
296     int testresult = 0;
297
298     if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
299                                        DTLS_client_method(),
300                                        DTLS1_VERSION, DTLS_MAX_VERSION,
301                                        &sctx, &cctx, cert, privkey)))
302         return 0;
303
304     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
305                                       NULL, NULL)))
306         goto end;
307
308     DTLS_set_timer_cb(clientssl, timer_cb);
309     DTLS_set_timer_cb(serverssl, timer_cb);
310
311     BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
312     BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
313
314     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
315         goto end;
316
317     testresult = 1;
318  end:
319     SSL_free(serverssl);
320     SSL_free(clientssl);
321     SSL_CTX_free(sctx);
322     SSL_CTX_free(cctx);
323
324     return testresult;
325 }
326
327 int setup_tests(void)
328 {
329     if (!TEST_ptr(cert = test_get_argument(0))
330             || !TEST_ptr(privkey = test_get_argument(1)))
331         return 0;
332
333     ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
334     ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
335     ADD_TEST(test_cookie);
336     ADD_TEST(test_dtls_duplicate_records);
337
338     return 1;
339 }
340
341 void cleanup_tests(void)
342 {
343     bio_f_tls_dump_filter_free();
344     bio_s_mempacket_test_free();
345 }