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