2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
14 #include <openssl/err.h>
16 #include "helpers/ssltestlib.h"
19 static char *cert
= NULL
;
20 static char *privkey
= NULL
;
21 static unsigned int timer_cb_count
;
26 #define DUMMY_CERT_STATUS_LEN 12
28 static unsigned char certstatus
[] = {
29 SSL3_RT_HANDSHAKE
, /* Content type */
30 0xfe, 0xfd, /* Record version */
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 */
43 #define RECORD_SEQUENCE 10
45 static unsigned int timer_cb(SSL
*s
, unsigned int timer_us
)
55 static int test_dtls_unprocessed(int testidx
)
57 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
58 SSL
*serverssl1
= NULL
, *clientssl1
= NULL
;
59 BIO
*c_to_s_fbio
, *c_to_s_mempacket
;
64 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
67 &sctx
, &cctx
, cert
, privkey
)))
70 #ifndef OPENSSL_NO_DTLS1_2
71 if (!TEST_true(SSL_CTX_set_cipher_list(cctx
, "AES128-SHA")))
74 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
75 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "AES128-SHA:@SECLEVEL=0"))
76 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
77 "AES128-SHA:@SECLEVEL=0")))
81 c_to_s_fbio
= BIO_new(bio_f_tls_dump_filter());
82 if (!TEST_ptr(c_to_s_fbio
))
85 /* BIO is freed by create_ssl_connection on error */
86 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl1
, &clientssl1
,
90 DTLS_set_timer_cb(clientssl1
, timer_cb
);
93 certstatus
[RECORD_SEQUENCE
] = 0xff;
96 * Inject a dummy record from the next epoch. In test 0, this should never
97 * get used because the message sequence number is too big. In test 1 we set
98 * the record sequence number to be way off in the future.
100 c_to_s_mempacket
= SSL_get_wbio(clientssl1
);
101 c_to_s_mempacket
= BIO_next(c_to_s_mempacket
);
102 mempacket_test_inject(c_to_s_mempacket
, (char *)certstatus
,
103 sizeof(certstatus
), 1, INJECT_PACKET_IGNORE_REC_SEQ
);
106 * Create the connection. We use "create_bare_ssl_connection" here so that
107 * we can force the connection to not do "SSL_read" once partly connected.
108 * We don't want to accidentally read the dummy records we injected because
109 * they will fail to decrypt.
111 if (!TEST_true(create_bare_ssl_connection(serverssl1
, clientssl1
,
112 SSL_ERROR_NONE
, 0, 0)))
115 if (timer_cb_count
== 0) {
116 printf("timer_callback was not called.\n");
122 SSL_free(serverssl1
);
123 SSL_free(clientssl1
);
130 #define CLI_TO_SRV_EPOCH_0_RECS 3
131 #define CLI_TO_SRV_EPOCH_1_RECS 1
132 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
133 # define SRV_TO_CLI_EPOCH_0_RECS 10
136 * In this case we have no ServerKeyExchange message, because we don't have
137 * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
140 # define SRV_TO_CLI_EPOCH_0_RECS 9
142 #define SRV_TO_CLI_EPOCH_1_RECS 1
143 #define TOTAL_FULL_HAND_RECORDS \
144 (CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
145 SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
147 #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
148 #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
149 #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
150 #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
151 #define TOTAL_RESUME_HAND_RECORDS \
152 (CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
153 SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
155 #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
158 * We are assuming a ServerKeyExchange message is sent in this test. If we don't
159 * have either DH or EC, then it won't be
161 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
162 static int test_dtls_drop_records(int idx
)
164 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
165 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
166 BIO
*c_to_s_fbio
, *mempackbio
;
169 SSL_SESSION
*sess
= NULL
;
170 int cli_to_srv_epoch0
, cli_to_srv_epoch1
, srv_to_cli_epoch0
;
172 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
173 DTLS_client_method(),
175 &sctx
, &cctx
, cert
, privkey
)))
178 #ifdef OPENSSL_NO_DTLS1_2
179 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
180 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "DEFAULT:@SECLEVEL=0"))
181 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
182 "DEFAULT:@SECLEVEL=0")))
186 if (!TEST_true(SSL_CTX_set_dh_auto(sctx
, 1)))
189 if (idx
>= TOTAL_FULL_HAND_RECORDS
) {
190 /* We're going to do a resumption handshake. Get a session first. */
191 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
193 || !TEST_true(create_ssl_connection(serverssl
, clientssl
,
195 || !TEST_ptr(sess
= SSL_get1_session(clientssl
)))
198 SSL_shutdown(clientssl
);
199 SSL_shutdown(serverssl
);
202 serverssl
= clientssl
= NULL
;
204 cli_to_srv_epoch0
= CLI_TO_SRV_RESUME_EPOCH_0_RECS
;
205 cli_to_srv_epoch1
= CLI_TO_SRV_RESUME_EPOCH_1_RECS
;
206 srv_to_cli_epoch0
= SRV_TO_CLI_RESUME_EPOCH_0_RECS
;
207 idx
-= TOTAL_FULL_HAND_RECORDS
;
209 cli_to_srv_epoch0
= CLI_TO_SRV_EPOCH_0_RECS
;
210 cli_to_srv_epoch1
= CLI_TO_SRV_EPOCH_1_RECS
;
211 srv_to_cli_epoch0
= SRV_TO_CLI_EPOCH_0_RECS
;
214 c_to_s_fbio
= BIO_new(bio_f_tls_dump_filter());
215 if (!TEST_ptr(c_to_s_fbio
))
218 /* BIO is freed by create_ssl_connection on error */
219 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
224 if (!TEST_true(SSL_set_session(clientssl
, sess
)))
228 DTLS_set_timer_cb(clientssl
, timer_cb
);
229 DTLS_set_timer_cb(serverssl
, timer_cb
);
231 /* Work out which record to drop based on the test number */
232 if (idx
>= cli_to_srv_epoch0
+ cli_to_srv_epoch1
) {
233 mempackbio
= SSL_get_wbio(serverssl
);
234 idx
-= cli_to_srv_epoch0
+ cli_to_srv_epoch1
;
235 if (idx
>= srv_to_cli_epoch0
) {
237 idx
-= srv_to_cli_epoch0
;
240 mempackbio
= SSL_get_wbio(clientssl
);
241 if (idx
>= cli_to_srv_epoch0
) {
243 idx
-= cli_to_srv_epoch0
;
245 mempackbio
= BIO_next(mempackbio
);
247 BIO_ctrl(mempackbio
, MEMPACKET_CTRL_SET_DROP_EPOCH
, epoch
, NULL
);
248 BIO_ctrl(mempackbio
, MEMPACKET_CTRL_SET_DROP_REC
, idx
, NULL
);
250 if (!TEST_true(create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)))
253 if (sess
!= NULL
&& !TEST_true(SSL_session_reused(clientssl
)))
256 /* If the test did what we planned then it should have dropped a record */
257 if (!TEST_int_eq((int)BIO_ctrl(mempackbio
, MEMPACKET_CTRL_GET_DROP_REC
, 0,
263 SSL_SESSION_free(sess
);
271 #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
273 static const char dummy_cookie
[] = "0123456";
275 static int generate_cookie_cb(SSL
*ssl
, unsigned char *cookie
,
276 unsigned int *cookie_len
)
278 memcpy(cookie
, dummy_cookie
, sizeof(dummy_cookie
));
279 *cookie_len
= sizeof(dummy_cookie
);
283 static int verify_cookie_cb(SSL
*ssl
, const unsigned char *cookie
,
284 unsigned int cookie_len
)
286 return TEST_mem_eq(cookie
, cookie_len
, dummy_cookie
, sizeof(dummy_cookie
));
289 static int test_cookie(void)
291 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
292 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
295 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
296 DTLS_client_method(),
298 &sctx
, &cctx
, cert
, privkey
)))
301 SSL_CTX_set_options(sctx
, SSL_OP_COOKIE_EXCHANGE
);
302 SSL_CTX_set_cookie_generate_cb(sctx
, generate_cookie_cb
);
303 SSL_CTX_set_cookie_verify_cb(sctx
, verify_cookie_cb
);
305 #ifdef OPENSSL_NO_DTLS1_2
306 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
307 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "DEFAULT:@SECLEVEL=0"))
308 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
309 "DEFAULT:@SECLEVEL=0")))
313 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
315 || !TEST_true(create_ssl_connection(serverssl
, clientssl
,
329 static int test_dtls_duplicate_records(void)
331 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
332 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
335 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
336 DTLS_client_method(),
338 &sctx
, &cctx
, cert
, privkey
)))
341 #ifdef OPENSSL_NO_DTLS1_2
342 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
343 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "DEFAULT:@SECLEVEL=0"))
344 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
345 "DEFAULT:@SECLEVEL=0")))
349 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
353 DTLS_set_timer_cb(clientssl
, timer_cb
);
354 DTLS_set_timer_cb(serverssl
, timer_cb
);
356 BIO_ctrl(SSL_get_wbio(clientssl
), MEMPACKET_CTRL_SET_DUPLICATE_REC
, 1, NULL
);
357 BIO_ctrl(SSL_get_wbio(serverssl
), MEMPACKET_CTRL_SET_DUPLICATE_REC
, 1, NULL
);
359 if (!TEST_true(create_ssl_connection(serverssl
, clientssl
, SSL_ERROR_NONE
)))
373 * Test just sending a Finished message as the first message. Should fail due
374 * to an unexpected message.
376 static int test_just_finished(void)
378 int testresult
= 0, ret
;
379 SSL_CTX
*sctx
= NULL
;
380 SSL
*serverssl
= NULL
;
381 BIO
*rbio
= NULL
, *wbio
= NULL
, *sbio
= NULL
;
382 unsigned char buf
[] = {
384 SSL3_RT_HANDSHAKE
, /* content type */
385 (DTLS1_2_VERSION
>> 8) & 0xff, /* protocol version hi byte */
386 DTLS1_2_VERSION
& 0xff, /* protocol version lo byte */
388 0, 0, 0, 0, 0, 0, /* record sequence */
389 0, DTLS1_HM_HEADER_LENGTH
+ SHA_DIGEST_LENGTH
, /* record length */
392 SSL3_MT_FINISHED
, /* message type */
393 0, 0, SHA_DIGEST_LENGTH
, /* message length */
394 0, 0, /* message sequence */
395 0, 0, 0, /* fragment offset */
396 0, 0, SHA_DIGEST_LENGTH
, /* fragment length */
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
403 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
405 &sctx
, NULL
, cert
, privkey
)))
408 #ifdef OPENSSL_NO_DTLS1_2
409 /* DTLSv1 is not allowed at the default security level */
410 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "DEFAULT:@SECLEVEL=0")))
414 serverssl
= SSL_new(sctx
);
415 rbio
= BIO_new(BIO_s_mem());
416 wbio
= BIO_new(BIO_s_mem());
418 if (!TEST_ptr(serverssl
) || !TEST_ptr(rbio
) || !TEST_ptr(wbio
))
422 SSL_set0_rbio(serverssl
, rbio
);
423 SSL_set0_wbio(serverssl
, wbio
);
425 DTLS_set_timer_cb(serverssl
, timer_cb
);
427 if (!TEST_int_eq(BIO_write(sbio
, buf
, sizeof(buf
)), sizeof(buf
)))
430 /* We expect the attempt to process the message to fail */
431 if (!TEST_int_le(ret
= SSL_accept(serverssl
), 0))
434 /* Check that we got the error we were expecting */
435 if (!TEST_int_eq(SSL_get_error(serverssl
, ret
), SSL_ERROR_SSL
))
438 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE
))
452 * Test that swapping a record from the next epoch into the current epoch still
453 * works. Libssl should buffer the record until it needs it.
455 static int test_swap_epoch(void)
457 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
458 SSL
*sssl
= NULL
, *cssl
= NULL
;
462 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
463 DTLS_client_method(),
465 &sctx
, &cctx
, cert
, privkey
)))
468 #ifndef OPENSSL_NO_DTLS1_2
469 if (!TEST_true(SSL_CTX_set_cipher_list(cctx
, "AES128-SHA")))
472 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
473 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "AES128-SHA:@SECLEVEL=0"))
474 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
475 "AES128-SHA:@SECLEVEL=0")))
480 /* BIO is freed by create_ssl_connection on error */
481 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &sssl
, &cssl
,
485 /* Send flight 1: ClientHello */
486 if (!TEST_int_le(SSL_connect(cssl
), 0))
489 /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */
490 if (!TEST_int_le(SSL_accept(sssl
), 0))
493 /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */
494 if (!TEST_int_le(SSL_connect(cssl
), 0))
497 bio
= SSL_get_wbio(cssl
);
499 || !TEST_true(mempacket_swap_epoch(bio
)))
503 * Recv flight 3, send flight 4: CCS, Finished.
504 * This should work in a single call because we have all the messages we
505 * need. Even though we had out of order packets, the record for the next
506 * epoch that we read early should be buffered and used later.
508 if (!TEST_int_gt(SSL_accept(sssl
), 0))
513 if (!TEST_int_gt(SSL_connect(cssl
), 0))
526 * Test that swapping an app data record so that it is received before the
527 * Finished message still works.
529 static int test_swap_app_data(void)
531 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
532 SSL
*sssl
= NULL
, *cssl
= NULL
;
535 char msg
[] = { 0x00, 0x01, 0x02, 0x03 };
538 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
539 DTLS_client_method(),
541 &sctx
, &cctx
, cert
, privkey
)))
544 #ifndef OPENSSL_NO_DTLS1_2
545 if (!TEST_true(SSL_CTX_set_cipher_list(cctx
, "AES128-SHA")))
548 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
549 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "AES128-SHA:@SECLEVEL=0"))
550 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
551 "AES128-SHA:@SECLEVEL=0")))
555 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &sssl
, &cssl
,
559 /* Send flight 1: ClientHello */
560 if (!TEST_int_le(SSL_connect(cssl
), 0))
563 /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */
564 if (!TEST_int_le(SSL_accept(sssl
), 0))
567 /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */
568 if (!TEST_int_le(SSL_connect(cssl
), 0))
571 /* Recv flight 3, send flight 4: datagram 1(NST, CCS) datagram 2(Finished) */
572 if (!TEST_int_gt(SSL_accept(sssl
), 0))
575 /* Send flight 5: app data */
576 if (!TEST_int_eq(SSL_write(sssl
, msg
, sizeof(msg
)), (int)sizeof(msg
)))
579 bio
= SSL_get_wbio(sssl
);
581 || !TEST_true(mempacket_swap_recent(bio
)))
585 * Recv flight 4 (datagram 1): NST, CCS, + flight 5: app data
586 * + flight 4 (datagram 2): Finished
588 if (!TEST_int_gt(SSL_connect(cssl
), 0))
591 /* The app data should be buffered already */
592 if (!TEST_int_eq(SSL_pending(cssl
), (int)sizeof(msg
))
593 || !TEST_true(SSL_has_pending(cssl
)))
597 * Recv flight 5 (app data)
598 * We already buffered this so it should be available.
600 if (!TEST_int_eq(SSL_read(cssl
, buf
, sizeof(buf
)), (int)sizeof(msg
)))
613 /* Confirm that we can create a connections using DTLSv1_listen() */
614 static int test_listen(void)
616 SSL_CTX
*sctx
= NULL
, *cctx
= NULL
;
617 SSL
*serverssl
= NULL
, *clientssl
= NULL
;
620 if (!TEST_true(create_ssl_ctx_pair(NULL
, DTLS_server_method(),
621 DTLS_client_method(),
623 &sctx
, &cctx
, cert
, privkey
)))
626 #ifdef OPENSSL_NO_DTLS1_2
627 /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
628 if (!TEST_true(SSL_CTX_set_cipher_list(sctx
, "DEFAULT:@SECLEVEL=0"))
629 || !TEST_true(SSL_CTX_set_cipher_list(cctx
,
630 "DEFAULT:@SECLEVEL=0")))
634 SSL_CTX_set_cookie_generate_cb(sctx
, generate_cookie_cb
);
635 SSL_CTX_set_cookie_verify_cb(sctx
, verify_cookie_cb
);
637 if (!TEST_true(create_ssl_objects(sctx
, cctx
, &serverssl
, &clientssl
,
641 DTLS_set_timer_cb(clientssl
, timer_cb
);
642 DTLS_set_timer_cb(serverssl
, timer_cb
);
645 * The last parameter to create_bare_ssl_connection() requests that
646 * DLTSv1_listen() is used.
648 if (!TEST_true(create_bare_ssl_connection(serverssl
, clientssl
,
649 SSL_ERROR_NONE
, 1, 1)))
662 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
664 int setup_tests(void)
666 if (!test_skip_common_options()) {
667 TEST_error("Error parsing test options\n");
671 if (!TEST_ptr(cert
= test_get_argument(0))
672 || !TEST_ptr(privkey
= test_get_argument(1)))
675 ADD_ALL_TESTS(test_dtls_unprocessed
, NUM_TESTS
);
676 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
677 ADD_ALL_TESTS(test_dtls_drop_records
, TOTAL_RECORDS
);
679 ADD_TEST(test_cookie
);
680 ADD_TEST(test_dtls_duplicate_records
);
681 ADD_TEST(test_just_finished
);
682 ADD_TEST(test_swap_epoch
);
683 ADD_TEST(test_swap_app_data
);
684 ADD_TEST(test_listen
);
689 void cleanup_tests(void)
691 bio_f_tls_dump_filter_free();
692 bio_s_mempacket_test_free();