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,
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 serverssl = SSL_new(sctx);
409 rbio = BIO_new(BIO_s_mem());
410 wbio = BIO_new(BIO_s_mem());
412 if (!TEST_ptr(serverssl) || !TEST_ptr(rbio) || !TEST_ptr(wbio))
416 SSL_set0_rbio(serverssl, rbio);
417 SSL_set0_wbio(serverssl, wbio);
419 DTLS_set_timer_cb(serverssl, timer_cb);
421 if (!TEST_int_eq(BIO_write(sbio, buf, sizeof(buf)), sizeof(buf)))
424 /* We expect the attempt to process the message to fail */
425 if (!TEST_int_le(ret = SSL_accept(serverssl), 0))
428 /* Check that we got the error we were expecting */
429 if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_SSL))
432 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE))
445 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
447 int setup_tests(void)
449 if (!test_skip_common_options()) {
450 TEST_error("Error parsing test options\n");
454 if (!TEST_ptr(cert = test_get_argument(0))
455 || !TEST_ptr(privkey = test_get_argument(1)))
458 ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
459 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
460 ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
462 ADD_TEST(test_cookie);
463 ADD_TEST(test_dtls_duplicate_records);
464 ADD_TEST(test_just_finished);
469 void cleanup_tests(void)
471 bio_f_tls_dump_filter_free();
472 bio_s_mempacket_test_free();