2 * Copyright 2016-2020 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 if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
73 c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
74 if (!TEST_ptr(c_to_s_fbio))
77 /* BIO is freed by create_ssl_connection on error */
78 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
82 DTLS_set_timer_cb(clientssl1, timer_cb);
85 certstatus[RECORD_SEQUENCE] = 0xff;
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.
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);
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.
103 if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
107 if (timer_cb_count == 0) {
108 printf("timer_callback was not called.\n");
114 SSL_free(serverssl1);
115 SSL_free(clientssl1);
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
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
132 # define SRV_TO_CLI_EPOCH_0_RECS 9
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)
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)
147 #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
150 * We are assuming a ServerKeyExchange message is sent in this test. If we don't
151 * have either DH or EC, then it won't be
153 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
154 static int test_dtls_drop_records(int idx)
156 SSL_CTX *sctx = NULL, *cctx = NULL;
157 SSL *serverssl = NULL, *clientssl = NULL;
158 BIO *c_to_s_fbio, *mempackbio;
161 SSL_SESSION *sess = NULL;
162 int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
164 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
165 DTLS_client_method(),
167 &sctx, &cctx, cert, privkey)))
170 if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
173 if (idx >= TOTAL_FULL_HAND_RECORDS) {
174 /* We're going to do a resumption handshake. Get a session first. */
175 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
177 || !TEST_true(create_ssl_connection(serverssl, clientssl,
179 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
182 SSL_shutdown(clientssl);
183 SSL_shutdown(serverssl);
186 serverssl = clientssl = NULL;
188 cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
189 cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
190 srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
191 idx -= TOTAL_FULL_HAND_RECORDS;
193 cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
194 cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
195 srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
198 c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
199 if (!TEST_ptr(c_to_s_fbio))
202 /* BIO is freed by create_ssl_connection on error */
203 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
208 if (!TEST_true(SSL_set_session(clientssl, sess)))
212 DTLS_set_timer_cb(clientssl, timer_cb);
213 DTLS_set_timer_cb(serverssl, timer_cb);
215 /* Work out which record to drop based on the test number */
216 if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
217 mempackbio = SSL_get_wbio(serverssl);
218 idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
219 if (idx >= srv_to_cli_epoch0) {
221 idx -= srv_to_cli_epoch0;
224 mempackbio = SSL_get_wbio(clientssl);
225 if (idx >= cli_to_srv_epoch0) {
227 idx -= cli_to_srv_epoch0;
229 mempackbio = BIO_next(mempackbio);
231 BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
232 BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
234 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
237 if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
240 /* If the test did what we planned then it should have dropped a record */
241 if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
247 SSL_SESSION_free(sess);
255 #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
257 static const char dummy_cookie[] = "0123456";
259 static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
260 unsigned int *cookie_len)
262 memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
263 *cookie_len = sizeof(dummy_cookie);
267 static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
268 unsigned int cookie_len)
270 return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
273 static int test_cookie(void)
275 SSL_CTX *sctx = NULL, *cctx = NULL;
276 SSL *serverssl = NULL, *clientssl = NULL;
279 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
280 DTLS_client_method(),
282 &sctx, &cctx, cert, privkey)))
285 SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
286 SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
287 SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
289 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
291 || !TEST_true(create_ssl_connection(serverssl, clientssl,
305 static int test_dtls_duplicate_records(void)
307 SSL_CTX *sctx = NULL, *cctx = NULL;
308 SSL *serverssl = NULL, *clientssl = NULL;
311 if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
312 DTLS_client_method(),
314 &sctx, &cctx, cert, privkey)))
317 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
321 DTLS_set_timer_cb(clientssl, timer_cb);
322 DTLS_set_timer_cb(serverssl, timer_cb);
324 BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
325 BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
327 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
340 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
342 int setup_tests(void)
344 if (!test_skip_common_options()) {
345 TEST_error("Error parsing test options\n");
349 if (!TEST_ptr(cert = test_get_argument(0))
350 || !TEST_ptr(privkey = test_get_argument(1)))
353 ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
354 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
355 ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
357 ADD_TEST(test_cookie);
358 ADD_TEST(test_dtls_duplicate_records);
363 void cleanup_tests(void)
365 bio_f_tls_dump_filter_free();
366 bio_s_mempacket_test_free();