1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/bio.h>
12*e0c4386eSCy Schubert #include <openssl/crypto.h>
13*e0c4386eSCy Schubert #include <openssl/ssl.h>
14*e0c4386eSCy Schubert #include <openssl/err.h>
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include "helpers/ssltestlib.h"
17*e0c4386eSCy Schubert #include "testutil.h"
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubert static char *cert = NULL;
20*e0c4386eSCy Schubert static char *privkey = NULL;
21*e0c4386eSCy Schubert static unsigned int timer_cb_count;
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert #define NUM_TESTS 2
24*e0c4386eSCy Schubert
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert #define DUMMY_CERT_STATUS_LEN 12
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert static unsigned char certstatus[] = {
29*e0c4386eSCy Schubert SSL3_RT_HANDSHAKE, /* Content type */
30*e0c4386eSCy Schubert 0xfe, 0xfd, /* Record version */
31*e0c4386eSCy Schubert 0, 1, /* Epoch */
32*e0c4386eSCy Schubert 0, 0, 0, 0, 0, 0x0f, /* Record sequence number */
33*e0c4386eSCy Schubert 0, DTLS1_HM_HEADER_LENGTH + DUMMY_CERT_STATUS_LEN - 2,
34*e0c4386eSCy Schubert SSL3_MT_CERTIFICATE_STATUS, /* Cert Status handshake message type */
35*e0c4386eSCy Schubert 0, 0, DUMMY_CERT_STATUS_LEN, /* Message len */
36*e0c4386eSCy Schubert 0, 5, /* Message sequence */
37*e0c4386eSCy Schubert 0, 0, 0, /* Fragment offset */
38*e0c4386eSCy Schubert 0, 0, DUMMY_CERT_STATUS_LEN - 2, /* Fragment len */
39*e0c4386eSCy Schubert 0x80, 0x80, 0x80, 0x80, 0x80,
40*e0c4386eSCy Schubert 0x80, 0x80, 0x80, 0x80, 0x80 /* Dummy data */
41*e0c4386eSCy Schubert };
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert #define RECORD_SEQUENCE 10
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert static const char dummy_cookie[] = "0123456";
46*e0c4386eSCy Schubert
generate_cookie_cb(SSL * ssl,unsigned char * cookie,unsigned int * cookie_len)47*e0c4386eSCy Schubert static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
48*e0c4386eSCy Schubert unsigned int *cookie_len)
49*e0c4386eSCy Schubert {
50*e0c4386eSCy Schubert memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
51*e0c4386eSCy Schubert *cookie_len = sizeof(dummy_cookie);
52*e0c4386eSCy Schubert return 1;
53*e0c4386eSCy Schubert }
54*e0c4386eSCy Schubert
verify_cookie_cb(SSL * ssl,const unsigned char * cookie,unsigned int cookie_len)55*e0c4386eSCy Schubert static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
56*e0c4386eSCy Schubert unsigned int cookie_len)
57*e0c4386eSCy Schubert {
58*e0c4386eSCy Schubert return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert
timer_cb(SSL * s,unsigned int timer_us)61*e0c4386eSCy Schubert static unsigned int timer_cb(SSL *s, unsigned int timer_us)
62*e0c4386eSCy Schubert {
63*e0c4386eSCy Schubert ++timer_cb_count;
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schubert if (timer_us == 0)
66*e0c4386eSCy Schubert return 50000;
67*e0c4386eSCy Schubert else
68*e0c4386eSCy Schubert return 2 * timer_us;
69*e0c4386eSCy Schubert }
70*e0c4386eSCy Schubert
test_dtls_unprocessed(int testidx)71*e0c4386eSCy Schubert static int test_dtls_unprocessed(int testidx)
72*e0c4386eSCy Schubert {
73*e0c4386eSCy Schubert SSL_CTX *sctx = NULL, *cctx = NULL;
74*e0c4386eSCy Schubert SSL *serverssl1 = NULL, *clientssl1 = NULL;
75*e0c4386eSCy Schubert BIO *c_to_s_fbio, *c_to_s_mempacket;
76*e0c4386eSCy Schubert int testresult = 0;
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert timer_cb_count = 0;
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
81*e0c4386eSCy Schubert DTLS_client_method(),
82*e0c4386eSCy Schubert DTLS1_VERSION, 0,
83*e0c4386eSCy Schubert &sctx, &cctx, cert, privkey)))
84*e0c4386eSCy Schubert return 0;
85*e0c4386eSCy Schubert
86*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS1_2
87*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
88*e0c4386eSCy Schubert goto end;
89*e0c4386eSCy Schubert #else
90*e0c4386eSCy Schubert /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
91*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
92*e0c4386eSCy Schubert || !TEST_true(SSL_CTX_set_cipher_list(cctx,
93*e0c4386eSCy Schubert "AES128-SHA:@SECLEVEL=0")))
94*e0c4386eSCy Schubert goto end;
95*e0c4386eSCy Schubert #endif
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
98*e0c4386eSCy Schubert if (!TEST_ptr(c_to_s_fbio))
99*e0c4386eSCy Schubert goto end;
100*e0c4386eSCy Schubert
101*e0c4386eSCy Schubert /* BIO is freed by create_ssl_connection on error */
102*e0c4386eSCy Schubert if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
103*e0c4386eSCy Schubert NULL, c_to_s_fbio)))
104*e0c4386eSCy Schubert goto end;
105*e0c4386eSCy Schubert
106*e0c4386eSCy Schubert DTLS_set_timer_cb(clientssl1, timer_cb);
107*e0c4386eSCy Schubert
108*e0c4386eSCy Schubert if (testidx == 1)
109*e0c4386eSCy Schubert certstatus[RECORD_SEQUENCE] = 0xff;
110*e0c4386eSCy Schubert
111*e0c4386eSCy Schubert /*
112*e0c4386eSCy Schubert * Inject a dummy record from the next epoch. In test 0, this should never
113*e0c4386eSCy Schubert * get used because the message sequence number is too big. In test 1 we set
114*e0c4386eSCy Schubert * the record sequence number to be way off in the future.
115*e0c4386eSCy Schubert */
116*e0c4386eSCy Schubert c_to_s_mempacket = SSL_get_wbio(clientssl1);
117*e0c4386eSCy Schubert c_to_s_mempacket = BIO_next(c_to_s_mempacket);
118*e0c4386eSCy Schubert mempacket_test_inject(c_to_s_mempacket, (char *)certstatus,
119*e0c4386eSCy Schubert sizeof(certstatus), 1, INJECT_PACKET_IGNORE_REC_SEQ);
120*e0c4386eSCy Schubert
121*e0c4386eSCy Schubert /*
122*e0c4386eSCy Schubert * Create the connection. We use "create_bare_ssl_connection" here so that
123*e0c4386eSCy Schubert * we can force the connection to not do "SSL_read" once partly connected.
124*e0c4386eSCy Schubert * We don't want to accidentally read the dummy records we injected because
125*e0c4386eSCy Schubert * they will fail to decrypt.
126*e0c4386eSCy Schubert */
127*e0c4386eSCy Schubert if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
128*e0c4386eSCy Schubert SSL_ERROR_NONE, 0)))
129*e0c4386eSCy Schubert goto end;
130*e0c4386eSCy Schubert
131*e0c4386eSCy Schubert if (timer_cb_count == 0) {
132*e0c4386eSCy Schubert printf("timer_callback was not called.\n");
133*e0c4386eSCy Schubert goto end;
134*e0c4386eSCy Schubert }
135*e0c4386eSCy Schubert
136*e0c4386eSCy Schubert testresult = 1;
137*e0c4386eSCy Schubert end:
138*e0c4386eSCy Schubert SSL_free(serverssl1);
139*e0c4386eSCy Schubert SSL_free(clientssl1);
140*e0c4386eSCy Schubert SSL_CTX_free(sctx);
141*e0c4386eSCy Schubert SSL_CTX_free(cctx);
142*e0c4386eSCy Schubert
143*e0c4386eSCy Schubert return testresult;
144*e0c4386eSCy Schubert }
145*e0c4386eSCy Schubert
146*e0c4386eSCy Schubert /* One record for the cookieless initial ClientHello */
147*e0c4386eSCy Schubert #define CLI_TO_SRV_COOKIE_EXCH 1
148*e0c4386eSCy Schubert
149*e0c4386eSCy Schubert /*
150*e0c4386eSCy Schubert * In a resumption handshake we use 2 records for the initial ClientHello in
151*e0c4386eSCy Schubert * this test because we are using a very small MTU and the ClientHello is
152*e0c4386eSCy Schubert * bigger than in the non resumption case.
153*e0c4386eSCy Schubert */
154*e0c4386eSCy Schubert #define CLI_TO_SRV_RESUME_COOKIE_EXCH 2
155*e0c4386eSCy Schubert #define SRV_TO_CLI_COOKIE_EXCH 1
156*e0c4386eSCy Schubert
157*e0c4386eSCy Schubert #define CLI_TO_SRV_EPOCH_0_RECS 3
158*e0c4386eSCy Schubert #define CLI_TO_SRV_EPOCH_1_RECS 1
159*e0c4386eSCy Schubert #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
160*e0c4386eSCy Schubert # define SRV_TO_CLI_EPOCH_0_RECS 10
161*e0c4386eSCy Schubert #else
162*e0c4386eSCy Schubert /*
163*e0c4386eSCy Schubert * In this case we have no ServerKeyExchange message, because we don't have
164*e0c4386eSCy Schubert * ECDHE or DHE. When it is present it gets fragmented into 3 records in this
165*e0c4386eSCy Schubert * test.
166*e0c4386eSCy Schubert */
167*e0c4386eSCy Schubert # define SRV_TO_CLI_EPOCH_0_RECS 9
168*e0c4386eSCy Schubert #endif
169*e0c4386eSCy Schubert #define SRV_TO_CLI_EPOCH_1_RECS 1
170*e0c4386eSCy Schubert #define TOTAL_FULL_HAND_RECORDS \
171*e0c4386eSCy Schubert (CLI_TO_SRV_COOKIE_EXCH + SRV_TO_CLI_COOKIE_EXCH + \
172*e0c4386eSCy Schubert CLI_TO_SRV_EPOCH_0_RECS + CLI_TO_SRV_EPOCH_1_RECS + \
173*e0c4386eSCy Schubert SRV_TO_CLI_EPOCH_0_RECS + SRV_TO_CLI_EPOCH_1_RECS)
174*e0c4386eSCy Schubert
175*e0c4386eSCy Schubert #define CLI_TO_SRV_RESUME_EPOCH_0_RECS 3
176*e0c4386eSCy Schubert #define CLI_TO_SRV_RESUME_EPOCH_1_RECS 1
177*e0c4386eSCy Schubert #define SRV_TO_CLI_RESUME_EPOCH_0_RECS 2
178*e0c4386eSCy Schubert #define SRV_TO_CLI_RESUME_EPOCH_1_RECS 1
179*e0c4386eSCy Schubert #define TOTAL_RESUME_HAND_RECORDS \
180*e0c4386eSCy Schubert (CLI_TO_SRV_RESUME_COOKIE_EXCH + SRV_TO_CLI_COOKIE_EXCH + \
181*e0c4386eSCy Schubert CLI_TO_SRV_RESUME_EPOCH_0_RECS + CLI_TO_SRV_RESUME_EPOCH_1_RECS + \
182*e0c4386eSCy Schubert SRV_TO_CLI_RESUME_EPOCH_0_RECS + SRV_TO_CLI_RESUME_EPOCH_1_RECS)
183*e0c4386eSCy Schubert
184*e0c4386eSCy Schubert #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
185*e0c4386eSCy Schubert
186*e0c4386eSCy Schubert /*
187*e0c4386eSCy Schubert * We are assuming a ServerKeyExchange message is sent in this test. If we don't
188*e0c4386eSCy Schubert * have either DH or EC, then it won't be
189*e0c4386eSCy Schubert */
190*e0c4386eSCy Schubert #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
test_dtls_drop_records(int idx)191*e0c4386eSCy Schubert static int test_dtls_drop_records(int idx)
192*e0c4386eSCy Schubert {
193*e0c4386eSCy Schubert SSL_CTX *sctx = NULL, *cctx = NULL;
194*e0c4386eSCy Schubert SSL *serverssl = NULL, *clientssl = NULL;
195*e0c4386eSCy Schubert BIO *c_to_s_fbio, *mempackbio;
196*e0c4386eSCy Schubert int testresult = 0;
197*e0c4386eSCy Schubert int epoch = 0;
198*e0c4386eSCy Schubert SSL_SESSION *sess = NULL;
199*e0c4386eSCy Schubert int cli_to_srv_cookie, cli_to_srv_epoch0, cli_to_srv_epoch1;
200*e0c4386eSCy Schubert int srv_to_cli_epoch0;
201*e0c4386eSCy Schubert
202*e0c4386eSCy Schubert if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
203*e0c4386eSCy Schubert DTLS_client_method(),
204*e0c4386eSCy Schubert DTLS1_VERSION, 0,
205*e0c4386eSCy Schubert &sctx, &cctx, cert, privkey)))
206*e0c4386eSCy Schubert return 0;
207*e0c4386eSCy Schubert
208*e0c4386eSCy Schubert #ifdef OPENSSL_NO_DTLS1_2
209*e0c4386eSCy Schubert /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
210*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
211*e0c4386eSCy Schubert || !TEST_true(SSL_CTX_set_cipher_list(cctx,
212*e0c4386eSCy Schubert "DEFAULT:@SECLEVEL=0")))
213*e0c4386eSCy Schubert goto end;
214*e0c4386eSCy Schubert #endif
215*e0c4386eSCy Schubert
216*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
217*e0c4386eSCy Schubert goto end;
218*e0c4386eSCy Schubert
219*e0c4386eSCy Schubert SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
220*e0c4386eSCy Schubert SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
221*e0c4386eSCy Schubert SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
222*e0c4386eSCy Schubert
223*e0c4386eSCy Schubert if (idx >= TOTAL_FULL_HAND_RECORDS) {
224*e0c4386eSCy Schubert /* We're going to do a resumption handshake. Get a session first. */
225*e0c4386eSCy Schubert if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
226*e0c4386eSCy Schubert NULL, NULL))
227*e0c4386eSCy Schubert || !TEST_true(create_ssl_connection(serverssl, clientssl,
228*e0c4386eSCy Schubert SSL_ERROR_NONE))
229*e0c4386eSCy Schubert || !TEST_ptr(sess = SSL_get1_session(clientssl)))
230*e0c4386eSCy Schubert goto end;
231*e0c4386eSCy Schubert
232*e0c4386eSCy Schubert SSL_shutdown(clientssl);
233*e0c4386eSCy Schubert SSL_shutdown(serverssl);
234*e0c4386eSCy Schubert SSL_free(serverssl);
235*e0c4386eSCy Schubert SSL_free(clientssl);
236*e0c4386eSCy Schubert serverssl = clientssl = NULL;
237*e0c4386eSCy Schubert
238*e0c4386eSCy Schubert cli_to_srv_epoch0 = CLI_TO_SRV_RESUME_EPOCH_0_RECS;
239*e0c4386eSCy Schubert cli_to_srv_epoch1 = CLI_TO_SRV_RESUME_EPOCH_1_RECS;
240*e0c4386eSCy Schubert srv_to_cli_epoch0 = SRV_TO_CLI_RESUME_EPOCH_0_RECS;
241*e0c4386eSCy Schubert cli_to_srv_cookie = CLI_TO_SRV_RESUME_COOKIE_EXCH;
242*e0c4386eSCy Schubert idx -= TOTAL_FULL_HAND_RECORDS;
243*e0c4386eSCy Schubert } else {
244*e0c4386eSCy Schubert cli_to_srv_epoch0 = CLI_TO_SRV_EPOCH_0_RECS;
245*e0c4386eSCy Schubert cli_to_srv_epoch1 = CLI_TO_SRV_EPOCH_1_RECS;
246*e0c4386eSCy Schubert srv_to_cli_epoch0 = SRV_TO_CLI_EPOCH_0_RECS;
247*e0c4386eSCy Schubert cli_to_srv_cookie = CLI_TO_SRV_COOKIE_EXCH;
248*e0c4386eSCy Schubert }
249*e0c4386eSCy Schubert
250*e0c4386eSCy Schubert c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
251*e0c4386eSCy Schubert if (!TEST_ptr(c_to_s_fbio))
252*e0c4386eSCy Schubert goto end;
253*e0c4386eSCy Schubert
254*e0c4386eSCy Schubert /* BIO is freed by create_ssl_connection on error */
255*e0c4386eSCy Schubert if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
256*e0c4386eSCy Schubert NULL, c_to_s_fbio)))
257*e0c4386eSCy Schubert goto end;
258*e0c4386eSCy Schubert
259*e0c4386eSCy Schubert if (sess != NULL) {
260*e0c4386eSCy Schubert if (!TEST_true(SSL_set_session(clientssl, sess)))
261*e0c4386eSCy Schubert goto end;
262*e0c4386eSCy Schubert }
263*e0c4386eSCy Schubert
264*e0c4386eSCy Schubert DTLS_set_timer_cb(clientssl, timer_cb);
265*e0c4386eSCy Schubert DTLS_set_timer_cb(serverssl, timer_cb);
266*e0c4386eSCy Schubert
267*e0c4386eSCy Schubert /* Work out which record to drop based on the test number */
268*e0c4386eSCy Schubert if (idx >= cli_to_srv_cookie + cli_to_srv_epoch0 + cli_to_srv_epoch1) {
269*e0c4386eSCy Schubert mempackbio = SSL_get_wbio(serverssl);
270*e0c4386eSCy Schubert idx -= cli_to_srv_cookie + cli_to_srv_epoch0 + cli_to_srv_epoch1;
271*e0c4386eSCy Schubert if (idx >= SRV_TO_CLI_COOKIE_EXCH + srv_to_cli_epoch0) {
272*e0c4386eSCy Schubert epoch = 1;
273*e0c4386eSCy Schubert idx -= SRV_TO_CLI_COOKIE_EXCH + srv_to_cli_epoch0;
274*e0c4386eSCy Schubert }
275*e0c4386eSCy Schubert } else {
276*e0c4386eSCy Schubert mempackbio = SSL_get_wbio(clientssl);
277*e0c4386eSCy Schubert if (idx >= cli_to_srv_cookie + cli_to_srv_epoch0) {
278*e0c4386eSCy Schubert epoch = 1;
279*e0c4386eSCy Schubert idx -= cli_to_srv_cookie + cli_to_srv_epoch0;
280*e0c4386eSCy Schubert }
281*e0c4386eSCy Schubert mempackbio = BIO_next(mempackbio);
282*e0c4386eSCy Schubert }
283*e0c4386eSCy Schubert BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
284*e0c4386eSCy Schubert BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
285*e0c4386eSCy Schubert
286*e0c4386eSCy Schubert if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
287*e0c4386eSCy Schubert goto end;
288*e0c4386eSCy Schubert
289*e0c4386eSCy Schubert if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
290*e0c4386eSCy Schubert goto end;
291*e0c4386eSCy Schubert
292*e0c4386eSCy Schubert /* If the test did what we planned then it should have dropped a record */
293*e0c4386eSCy Schubert if (!TEST_int_eq((int)BIO_ctrl(mempackbio, MEMPACKET_CTRL_GET_DROP_REC, 0,
294*e0c4386eSCy Schubert NULL), -1))
295*e0c4386eSCy Schubert goto end;
296*e0c4386eSCy Schubert
297*e0c4386eSCy Schubert testresult = 1;
298*e0c4386eSCy Schubert end:
299*e0c4386eSCy Schubert SSL_SESSION_free(sess);
300*e0c4386eSCy Schubert SSL_free(serverssl);
301*e0c4386eSCy Schubert SSL_free(clientssl);
302*e0c4386eSCy Schubert SSL_CTX_free(sctx);
303*e0c4386eSCy Schubert SSL_CTX_free(cctx);
304*e0c4386eSCy Schubert
305*e0c4386eSCy Schubert return testresult;
306*e0c4386eSCy Schubert }
307*e0c4386eSCy Schubert #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
308*e0c4386eSCy Schubert
test_cookie(void)309*e0c4386eSCy Schubert static int test_cookie(void)
310*e0c4386eSCy Schubert {
311*e0c4386eSCy Schubert SSL_CTX *sctx = NULL, *cctx = NULL;
312*e0c4386eSCy Schubert SSL *serverssl = NULL, *clientssl = NULL;
313*e0c4386eSCy Schubert int testresult = 0;
314*e0c4386eSCy Schubert
315*e0c4386eSCy Schubert if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
316*e0c4386eSCy Schubert DTLS_client_method(),
317*e0c4386eSCy Schubert DTLS1_VERSION, 0,
318*e0c4386eSCy Schubert &sctx, &cctx, cert, privkey)))
319*e0c4386eSCy Schubert return 0;
320*e0c4386eSCy Schubert
321*e0c4386eSCy Schubert SSL_CTX_set_options(sctx, SSL_OP_COOKIE_EXCHANGE);
322*e0c4386eSCy Schubert SSL_CTX_set_cookie_generate_cb(sctx, generate_cookie_cb);
323*e0c4386eSCy Schubert SSL_CTX_set_cookie_verify_cb(sctx, verify_cookie_cb);
324*e0c4386eSCy Schubert
325*e0c4386eSCy Schubert #ifdef OPENSSL_NO_DTLS1_2
326*e0c4386eSCy Schubert /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
327*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
328*e0c4386eSCy Schubert || !TEST_true(SSL_CTX_set_cipher_list(cctx,
329*e0c4386eSCy Schubert "DEFAULT:@SECLEVEL=0")))
330*e0c4386eSCy Schubert goto end;
331*e0c4386eSCy Schubert #endif
332*e0c4386eSCy Schubert
333*e0c4386eSCy Schubert if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
334*e0c4386eSCy Schubert NULL, NULL))
335*e0c4386eSCy Schubert || !TEST_true(create_ssl_connection(serverssl, clientssl,
336*e0c4386eSCy Schubert SSL_ERROR_NONE)))
337*e0c4386eSCy Schubert goto end;
338*e0c4386eSCy Schubert
339*e0c4386eSCy Schubert testresult = 1;
340*e0c4386eSCy Schubert end:
341*e0c4386eSCy Schubert SSL_free(serverssl);
342*e0c4386eSCy Schubert SSL_free(clientssl);
343*e0c4386eSCy Schubert SSL_CTX_free(sctx);
344*e0c4386eSCy Schubert SSL_CTX_free(cctx);
345*e0c4386eSCy Schubert
346*e0c4386eSCy Schubert return testresult;
347*e0c4386eSCy Schubert }
348*e0c4386eSCy Schubert
test_dtls_duplicate_records(void)349*e0c4386eSCy Schubert static int test_dtls_duplicate_records(void)
350*e0c4386eSCy Schubert {
351*e0c4386eSCy Schubert SSL_CTX *sctx = NULL, *cctx = NULL;
352*e0c4386eSCy Schubert SSL *serverssl = NULL, *clientssl = NULL;
353*e0c4386eSCy Schubert int testresult = 0;
354*e0c4386eSCy Schubert
355*e0c4386eSCy Schubert if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
356*e0c4386eSCy Schubert DTLS_client_method(),
357*e0c4386eSCy Schubert DTLS1_VERSION, 0,
358*e0c4386eSCy Schubert &sctx, &cctx, cert, privkey)))
359*e0c4386eSCy Schubert return 0;
360*e0c4386eSCy Schubert
361*e0c4386eSCy Schubert #ifdef OPENSSL_NO_DTLS1_2
362*e0c4386eSCy Schubert /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
363*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
364*e0c4386eSCy Schubert || !TEST_true(SSL_CTX_set_cipher_list(cctx,
365*e0c4386eSCy Schubert "DEFAULT:@SECLEVEL=0")))
366*e0c4386eSCy Schubert goto end;
367*e0c4386eSCy Schubert #endif
368*e0c4386eSCy Schubert
369*e0c4386eSCy Schubert if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
370*e0c4386eSCy Schubert NULL, NULL)))
371*e0c4386eSCy Schubert goto end;
372*e0c4386eSCy Schubert
373*e0c4386eSCy Schubert DTLS_set_timer_cb(clientssl, timer_cb);
374*e0c4386eSCy Schubert DTLS_set_timer_cb(serverssl, timer_cb);
375*e0c4386eSCy Schubert
376*e0c4386eSCy Schubert BIO_ctrl(SSL_get_wbio(clientssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
377*e0c4386eSCy Schubert BIO_ctrl(SSL_get_wbio(serverssl), MEMPACKET_CTRL_SET_DUPLICATE_REC, 1, NULL);
378*e0c4386eSCy Schubert
379*e0c4386eSCy Schubert if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
380*e0c4386eSCy Schubert goto end;
381*e0c4386eSCy Schubert
382*e0c4386eSCy Schubert testresult = 1;
383*e0c4386eSCy Schubert end:
384*e0c4386eSCy Schubert SSL_free(serverssl);
385*e0c4386eSCy Schubert SSL_free(clientssl);
386*e0c4386eSCy Schubert SSL_CTX_free(sctx);
387*e0c4386eSCy Schubert SSL_CTX_free(cctx);
388*e0c4386eSCy Schubert
389*e0c4386eSCy Schubert return testresult;
390*e0c4386eSCy Schubert }
391*e0c4386eSCy Schubert
392*e0c4386eSCy Schubert /*
393*e0c4386eSCy Schubert * Test just sending a Finished message as the first message. Should fail due
394*e0c4386eSCy Schubert * to an unexpected message.
395*e0c4386eSCy Schubert */
test_just_finished(void)396*e0c4386eSCy Schubert static int test_just_finished(void)
397*e0c4386eSCy Schubert {
398*e0c4386eSCy Schubert int testresult = 0, ret;
399*e0c4386eSCy Schubert SSL_CTX *sctx = NULL;
400*e0c4386eSCy Schubert SSL *serverssl = NULL;
401*e0c4386eSCy Schubert BIO *rbio = NULL, *wbio = NULL, *sbio = NULL;
402*e0c4386eSCy Schubert unsigned char buf[] = {
403*e0c4386eSCy Schubert /* Record header */
404*e0c4386eSCy Schubert SSL3_RT_HANDSHAKE, /* content type */
405*e0c4386eSCy Schubert (DTLS1_2_VERSION >> 8) & 0xff, /* protocol version hi byte */
406*e0c4386eSCy Schubert DTLS1_2_VERSION & 0xff, /* protocol version lo byte */
407*e0c4386eSCy Schubert 0, 0, /* epoch */
408*e0c4386eSCy Schubert 0, 0, 0, 0, 0, 0, /* record sequence */
409*e0c4386eSCy Schubert 0, DTLS1_HM_HEADER_LENGTH + SHA_DIGEST_LENGTH, /* record length */
410*e0c4386eSCy Schubert
411*e0c4386eSCy Schubert /* Message header */
412*e0c4386eSCy Schubert SSL3_MT_FINISHED, /* message type */
413*e0c4386eSCy Schubert 0, 0, SHA_DIGEST_LENGTH, /* message length */
414*e0c4386eSCy Schubert 0, 0, /* message sequence */
415*e0c4386eSCy Schubert 0, 0, 0, /* fragment offset */
416*e0c4386eSCy Schubert 0, 0, SHA_DIGEST_LENGTH, /* fragment length */
417*e0c4386eSCy Schubert
418*e0c4386eSCy Schubert /* Message body */
419*e0c4386eSCy Schubert 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
420*e0c4386eSCy Schubert };
421*e0c4386eSCy Schubert
422*e0c4386eSCy Schubert
423*e0c4386eSCy Schubert if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
424*e0c4386eSCy Schubert NULL, 0, 0,
425*e0c4386eSCy Schubert &sctx, NULL, cert, privkey)))
426*e0c4386eSCy Schubert return 0;
427*e0c4386eSCy Schubert
428*e0c4386eSCy Schubert serverssl = SSL_new(sctx);
429*e0c4386eSCy Schubert rbio = BIO_new(BIO_s_mem());
430*e0c4386eSCy Schubert wbio = BIO_new(BIO_s_mem());
431*e0c4386eSCy Schubert
432*e0c4386eSCy Schubert if (!TEST_ptr(serverssl) || !TEST_ptr(rbio) || !TEST_ptr(wbio))
433*e0c4386eSCy Schubert goto end;
434*e0c4386eSCy Schubert
435*e0c4386eSCy Schubert sbio = rbio;
436*e0c4386eSCy Schubert SSL_set0_rbio(serverssl, rbio);
437*e0c4386eSCy Schubert SSL_set0_wbio(serverssl, wbio);
438*e0c4386eSCy Schubert rbio = wbio = NULL;
439*e0c4386eSCy Schubert DTLS_set_timer_cb(serverssl, timer_cb);
440*e0c4386eSCy Schubert
441*e0c4386eSCy Schubert if (!TEST_int_eq(BIO_write(sbio, buf, sizeof(buf)), sizeof(buf)))
442*e0c4386eSCy Schubert goto end;
443*e0c4386eSCy Schubert
444*e0c4386eSCy Schubert /* We expect the attempt to process the message to fail */
445*e0c4386eSCy Schubert if (!TEST_int_le(ret = SSL_accept(serverssl), 0))
446*e0c4386eSCy Schubert goto end;
447*e0c4386eSCy Schubert
448*e0c4386eSCy Schubert /* Check that we got the error we were expecting */
449*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_SSL))
450*e0c4386eSCy Schubert goto end;
451*e0c4386eSCy Schubert
452*e0c4386eSCy Schubert if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_UNEXPECTED_MESSAGE))
453*e0c4386eSCy Schubert goto end;
454*e0c4386eSCy Schubert
455*e0c4386eSCy Schubert testresult = 1;
456*e0c4386eSCy Schubert end:
457*e0c4386eSCy Schubert BIO_free(rbio);
458*e0c4386eSCy Schubert BIO_free(wbio);
459*e0c4386eSCy Schubert SSL_free(serverssl);
460*e0c4386eSCy Schubert SSL_CTX_free(sctx);
461*e0c4386eSCy Schubert
462*e0c4386eSCy Schubert return testresult;
463*e0c4386eSCy Schubert }
464*e0c4386eSCy Schubert
465*e0c4386eSCy Schubert /*
466*e0c4386eSCy Schubert * Test that swapping later records before Finished or CCS still works
467*e0c4386eSCy Schubert * Test 0: Test receiving a handshake record early from next epoch on server side
468*e0c4386eSCy Schubert * Test 1: Test receiving a handshake record early from next epoch on client side
469*e0c4386eSCy Schubert * Test 2: Test receiving an app data record early from next epoch on client side
470*e0c4386eSCy Schubert * Test 3: Test receiving an app data before Finished on client side
471*e0c4386eSCy Schubert */
test_swap_records(int idx)472*e0c4386eSCy Schubert static int test_swap_records(int idx)
473*e0c4386eSCy Schubert {
474*e0c4386eSCy Schubert SSL_CTX *sctx = NULL, *cctx = NULL;
475*e0c4386eSCy Schubert SSL *sssl = NULL, *cssl = NULL;
476*e0c4386eSCy Schubert int testresult = 0;
477*e0c4386eSCy Schubert BIO *bio;
478*e0c4386eSCy Schubert char msg[] = { 0x00, 0x01, 0x02, 0x03 };
479*e0c4386eSCy Schubert char buf[10];
480*e0c4386eSCy Schubert
481*e0c4386eSCy Schubert if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
482*e0c4386eSCy Schubert DTLS_client_method(),
483*e0c4386eSCy Schubert DTLS1_VERSION, 0,
484*e0c4386eSCy Schubert &sctx, &cctx, cert, privkey)))
485*e0c4386eSCy Schubert return 0;
486*e0c4386eSCy Schubert
487*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DTLS1_2
488*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
489*e0c4386eSCy Schubert goto end;
490*e0c4386eSCy Schubert #else
491*e0c4386eSCy Schubert /* Default sigalgs are SHA1 based in <DTLS1.2 which is in security level 0 */
492*e0c4386eSCy Schubert if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "AES128-SHA:@SECLEVEL=0"))
493*e0c4386eSCy Schubert || !TEST_true(SSL_CTX_set_cipher_list(cctx,
494*e0c4386eSCy Schubert "AES128-SHA:@SECLEVEL=0")))
495*e0c4386eSCy Schubert goto end;
496*e0c4386eSCy Schubert #endif
497*e0c4386eSCy Schubert
498*e0c4386eSCy Schubert if (!TEST_true(create_ssl_objects(sctx, cctx, &sssl, &cssl,
499*e0c4386eSCy Schubert NULL, NULL)))
500*e0c4386eSCy Schubert goto end;
501*e0c4386eSCy Schubert
502*e0c4386eSCy Schubert /* Send flight 1: ClientHello */
503*e0c4386eSCy Schubert if (!TEST_int_le(SSL_connect(cssl), 0))
504*e0c4386eSCy Schubert goto end;
505*e0c4386eSCy Schubert
506*e0c4386eSCy Schubert /* Recv flight 1, send flight 2: ServerHello, Certificate, ServerHelloDone */
507*e0c4386eSCy Schubert if (!TEST_int_le(SSL_accept(sssl), 0))
508*e0c4386eSCy Schubert goto end;
509*e0c4386eSCy Schubert
510*e0c4386eSCy Schubert /* Recv flight 2, send flight 3: ClientKeyExchange, CCS, Finished */
511*e0c4386eSCy Schubert if (!TEST_int_le(SSL_connect(cssl), 0))
512*e0c4386eSCy Schubert goto end;
513*e0c4386eSCy Schubert
514*e0c4386eSCy Schubert if (idx == 0) {
515*e0c4386eSCy Schubert /* Swap Finished and CCS within the datagram */
516*e0c4386eSCy Schubert bio = SSL_get_wbio(cssl);
517*e0c4386eSCy Schubert if (!TEST_ptr(bio)
518*e0c4386eSCy Schubert || !TEST_true(mempacket_swap_epoch(bio)))
519*e0c4386eSCy Schubert goto end;
520*e0c4386eSCy Schubert }
521*e0c4386eSCy Schubert
522*e0c4386eSCy Schubert /* Recv flight 3, send flight 4: datagram 0(NST, CCS) datagram 1(Finished) */
523*e0c4386eSCy Schubert if (!TEST_int_gt(SSL_accept(sssl), 0))
524*e0c4386eSCy Schubert goto end;
525*e0c4386eSCy Schubert
526*e0c4386eSCy Schubert /* Send flight 4 (cont'd): datagram 2(app data) */
527*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_write(sssl, msg, sizeof(msg)), (int)sizeof(msg)))
528*e0c4386eSCy Schubert goto end;
529*e0c4386eSCy Schubert
530*e0c4386eSCy Schubert bio = SSL_get_wbio(sssl);
531*e0c4386eSCy Schubert if (!TEST_ptr(bio))
532*e0c4386eSCy Schubert goto end;
533*e0c4386eSCy Schubert if (idx == 1) {
534*e0c4386eSCy Schubert /* Finished comes before NST/CCS */
535*e0c4386eSCy Schubert if (!TEST_true(mempacket_move_packet(bio, 0, 1)))
536*e0c4386eSCy Schubert goto end;
537*e0c4386eSCy Schubert } else if (idx == 2) {
538*e0c4386eSCy Schubert /* App data comes before NST/CCS */
539*e0c4386eSCy Schubert if (!TEST_true(mempacket_move_packet(bio, 0, 2)))
540*e0c4386eSCy Schubert goto end;
541*e0c4386eSCy Schubert } else if (idx == 3) {
542*e0c4386eSCy Schubert /* App data comes before Finished */
543*e0c4386eSCy Schubert bio = SSL_get_wbio(sssl);
544*e0c4386eSCy Schubert if (!TEST_true(mempacket_move_packet(bio, 1, 2)))
545*e0c4386eSCy Schubert goto end;
546*e0c4386eSCy Schubert }
547*e0c4386eSCy Schubert
548*e0c4386eSCy Schubert /*
549*e0c4386eSCy Schubert * Recv flight 4 (datagram 1): NST, CCS, + flight 5: app data
550*e0c4386eSCy Schubert * + flight 4 (datagram 2): Finished
551*e0c4386eSCy Schubert */
552*e0c4386eSCy Schubert if (!TEST_int_gt(SSL_connect(cssl), 0))
553*e0c4386eSCy Schubert goto end;
554*e0c4386eSCy Schubert
555*e0c4386eSCy Schubert if (idx == 0 || idx == 1) {
556*e0c4386eSCy Schubert /* App data was not received early, so it should not be pending */
557*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_pending(cssl), 0)
558*e0c4386eSCy Schubert || !TEST_false(SSL_has_pending(cssl)))
559*e0c4386eSCy Schubert goto end;
560*e0c4386eSCy Schubert
561*e0c4386eSCy Schubert } else {
562*e0c4386eSCy Schubert /* We received the app data early so it should be buffered already */
563*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_pending(cssl), (int)sizeof(msg))
564*e0c4386eSCy Schubert || !TEST_true(SSL_has_pending(cssl)))
565*e0c4386eSCy Schubert goto end;
566*e0c4386eSCy Schubert }
567*e0c4386eSCy Schubert
568*e0c4386eSCy Schubert /*
569*e0c4386eSCy Schubert * Recv flight 5 (app data)
570*e0c4386eSCy Schubert */
571*e0c4386eSCy Schubert if (!TEST_int_eq(SSL_read(cssl, buf, sizeof(buf)), (int)sizeof(msg)))
572*e0c4386eSCy Schubert goto end;
573*e0c4386eSCy Schubert
574*e0c4386eSCy Schubert testresult = 1;
575*e0c4386eSCy Schubert end:
576*e0c4386eSCy Schubert SSL_free(cssl);
577*e0c4386eSCy Schubert SSL_free(sssl);
578*e0c4386eSCy Schubert SSL_CTX_free(cctx);
579*e0c4386eSCy Schubert SSL_CTX_free(sctx);
580*e0c4386eSCy Schubert return testresult;
581*e0c4386eSCy Schubert }
582*e0c4386eSCy Schubert
583*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
584*e0c4386eSCy Schubert
setup_tests(void)585*e0c4386eSCy Schubert int setup_tests(void)
586*e0c4386eSCy Schubert {
587*e0c4386eSCy Schubert if (!test_skip_common_options()) {
588*e0c4386eSCy Schubert TEST_error("Error parsing test options\n");
589*e0c4386eSCy Schubert return 0;
590*e0c4386eSCy Schubert }
591*e0c4386eSCy Schubert
592*e0c4386eSCy Schubert if (!TEST_ptr(cert = test_get_argument(0))
593*e0c4386eSCy Schubert || !TEST_ptr(privkey = test_get_argument(1)))
594*e0c4386eSCy Schubert return 0;
595*e0c4386eSCy Schubert
596*e0c4386eSCy Schubert ADD_ALL_TESTS(test_dtls_unprocessed, NUM_TESTS);
597*e0c4386eSCy Schubert #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
598*e0c4386eSCy Schubert ADD_ALL_TESTS(test_dtls_drop_records, TOTAL_RECORDS);
599*e0c4386eSCy Schubert #endif
600*e0c4386eSCy Schubert ADD_TEST(test_cookie);
601*e0c4386eSCy Schubert ADD_TEST(test_dtls_duplicate_records);
602*e0c4386eSCy Schubert ADD_TEST(test_just_finished);
603*e0c4386eSCy Schubert ADD_ALL_TESTS(test_swap_records, 4);
604*e0c4386eSCy Schubert
605*e0c4386eSCy Schubert return 1;
606*e0c4386eSCy Schubert }
607*e0c4386eSCy Schubert
cleanup_tests(void)608*e0c4386eSCy Schubert void cleanup_tests(void)
609*e0c4386eSCy Schubert {
610*e0c4386eSCy Schubert bio_f_tls_dump_filter_free();
611*e0c4386eSCy Schubert bio_s_mempacket_test_free();
612*e0c4386eSCy Schubert }
613