2 * Copyright 2016-2018 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 "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(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 conencted.
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 12
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)
149 static int test_dtls_drop_records(int idx)
151 SSL_CTX *sctx = NULL, *cctx = NULL;
152 SSL *serverssl = NULL, *clientssl = NULL;
153 BIO *c_to_s_fbio, *mempackbio;
156 SSL_SESSION *sess = NULL;
157 int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
159 if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
160 DTLS_client_method(),
162 &sctx, &cctx, cert, privkey)))
165 if (idx >= TOTAL_FULL_HAND_RECORDS) {
166 /* We're going to do a resumption handshake. Get a session first. */
167 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
169 || !TEST_true(create_ssl_connection(serverssl, clientssl,
171 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
174 SSL_shutdown(clientssl);
175 SSL_shutdown(serverssl);
178 serverssl = clientssl = NULL;
180 cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
181 cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
182 srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
183 idx -= TOTAL_FULL_HAND_RECORDS;
185 cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
186 cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
187 srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
190 c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
191 if (!TEST_ptr(c_to_s_fbio))
194 /* BIO is freed by create_ssl_connection on error */
195 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
200 if (!TEST_true(SSL_set_session(clientssl, sess)))
204 DTLS_set_timer_cb(clientssl, timer_cb);
205 DTLS_set_timer_cb(serverssl, timer_cb);
207 /* Work out which record to drop based on the test number */
208 if (idx >= cli_to_srv_epoch0 + cli_to_srv_epoch1) {
209 mempackbio = SSL_get_wbio(serverssl);
210 idx -= cli_to_srv_epoch0 + cli_to_srv_epoch1;
211 if (idx >= srv_to_cli_epoch0) {
213 idx -= srv_to_cli_epoch0;
216 mempackbio = SSL_get_wbio(clientssl);
217 if (idx >= cli_to_srv_epoch0) {
219 idx -= cli_to_srv_epoch0;
221 mempackbio = BIO_next(mempackbio);
223 BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
224 BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
226 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
229 if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
232 /* If the test did what we planned then it should have dropped a record */
233 if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
239 SSL_SESSION_free(sess);
248 static const char dummy_cookie[] = "0123456";
250 static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
251 unsigned int *cookie_len)
253 memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
254 *cookie_len = sizeof(dummy_cookie);
258 static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
259 unsigned int cookie_len)
261 return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
264 static int test_cookie(void)
266 SSL_CTX *sctx = NULL, *cctx = NULL;
267 SSL *serverssl = NULL, *clientssl = NULL;
270 if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
271 DTLS_client_method(),
273 &sctx, &cctx, cert, privkey)))
276 SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
277 SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
278 SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
280 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
282 || !TEST_true(create_ssl_connection(serverssl, clientssl,
296 static int test_dtls_duplicate_records(void)
298 SSL_CTX *sctx = NULL, *cctx = NULL;
299 SSL *serverssl = NULL, *clientssl = NULL;
302 if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
303 DTLS_client_method(),
305 &sctx, &cctx, cert, privkey)))
308 if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
312 DTLS_set_timer_cb(clientssl, timer_cb);
313 DTLS_set_timer_cb(serverssl, timer_cb);
315 BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
316 BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
318 if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
331 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
333 int setup_tests(void)
335 if (!TEST_ptr(cert = test_get_argument(0))
336 || !TEST_ptr(privkey = test_get_argument(1)))
339 ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
340 ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
341 ADD_TEST(test_cookie);
342 ADD_TEST(test_dtls_duplicate_records);
347 void cleanup_tests(void)
349 bio_f_tls_dump_filter_free();
350 bio_s_mempacket_test_free();