Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / test / dtlstest.c
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <string.h>
11 #include <openssl/bio.h>
12 #include <openssl/crypto.h>
13 #include <openssl/ssl.h>
14 #include <openssl/err.h>
15
16 #include "helpers/ssltestlib.h"
17 #include "testutil.h"
18
19 static char *cert = NULL;
20 static char *privkey = NULL;
21 static unsigned int timer_cb_count;
22
23 #define NUM_TESTS   2
24
25
26 #define DUMMY_CERT_STATUS_LEN  12
27
28 static unsigned char certstatus[] = {
29     SSL3_RT_HANDSHAKE, /* Content type */
30     0xfe, 0xfd, /* Record version */
31     0, 1, /* Epoch */
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 */
41 };
42
43 #define RECORD_SEQUENCE 10
44
45 static unsigned int timer_cb(SSL *s, unsigned int timer_us)
46 {
47     ++timer_cb_count;
48
49     if (timer_us == 0)
50         return 50000;
51     else
52         return 2 * timer_us;
53 }
54
55 static int test_dtls_unprocessed(int testidx)
56 {
57     SSL_CTX *sctx = NULL, *cctx = NULL;
58     SSL *serverssl1 = NULL, *clientssl1 = NULL;
59     BIO *c_to_s_fbio, *c_to_s_mempacket;
60     int testresult = 0;
61
62     timer_cb_count = 0;
63
64     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
65                                        DTLS_client_method(),
66                                        DTLS1_VERSION, 0,
67                                        &sctx, &cctx, cert, privkey)))
68         return 0;
69
70     if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA")))
71         goto end;
72
73     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
74     if (!TEST_ptr(c_to_s_fbio))
75         goto end;
76
77     /* BIO is freed by create_ssl_connection on error */
78     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
79                                       NULL, c_to_s_fbio)))
80         goto end;
81
82     DTLS_set_timer_cb(clientssl1, timer_cb);
83
84     if (testidx == 1)
85         certstatus[RECORD_SEQUENCE] = 0xff;
86
87     /*
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.
91      */
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);
96
97     /*
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.
102      */
103     if (!TEST_true(create_bare_ssl_connection(serverssl1, clientssl1,
104                                               SSL_ERROR_NONE, 0)))
105         goto end;
106
107     if (timer_cb_count == 0) {
108         printf("timer_callback was not called.\n");
109         goto end;
110     }
111
112     testresult = 1;
113  end:
114     SSL_free(serverssl1);
115     SSL_free(clientssl1);
116     SSL_CTX_free(sctx);
117     SSL_CTX_free(cctx);
118
119     return testresult;
120 }
121
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
126 #else
127 /*
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
130  * test.
131  */
132 # define SRV_TO_CLI_EPOCH_0_RECS 9
133 #endif
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)
138
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)
146
147 #define TOTAL_RECORDS (TOTAL_FULL_HAND_RECORDS + TOTAL_RESUME_HAND_RECORDS)
148
149 /*
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
152  */
153 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
154 static int test_dtls_drop_records(int idx)
155 {
156     SSL_CTX *sctx = NULL, *cctx = NULL;
157     SSL *serverssl = NULL, *clientssl = NULL;
158     BIO *c_to_s_fbio, *mempackbio;
159     int testresult = 0;
160     int epoch = 0;
161     SSL_SESSION *sess = NULL;
162     int cli_to_srv_epoch0, cli_to_srv_epoch1, srv_to_cli_epoch0;
163
164     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
165                                        DTLS_client_method(),
166                                        DTLS1_VERSION, 0,
167                                        &sctx, &cctx, cert, privkey)))
168         return 0;
169
170     if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
171         goto end;
172
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,
176                                           NULL, NULL))
177                 || !TEST_true(create_ssl_connection(serverssl, clientssl,
178                               SSL_ERROR_NONE))
179                 || !TEST_ptr(sess = SSL_get1_session(clientssl)))
180             goto end;
181
182         SSL_shutdown(clientssl);
183         SSL_shutdown(serverssl);
184         SSL_free(serverssl);
185         SSL_free(clientssl);
186         serverssl = clientssl = NULL;
187
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;
192     } else {
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;
196     }
197
198     c_to_s_fbio = BIO_new(bio_f_tls_dump_filter());
199     if (!TEST_ptr(c_to_s_fbio))
200         goto end;
201
202     /* BIO is freed by create_ssl_connection on error */
203     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
204                                       NULL, c_to_s_fbio)))
205         goto end;
206
207     if (sess != NULL) {
208         if (!TEST_true(SSL_set_session(clientssl, sess)))
209             goto end;
210     }
211
212     DTLS_set_timer_cb(clientssl, timer_cb);
213     DTLS_set_timer_cb(serverssl, timer_cb);
214
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) {
220             epoch = 1;
221             idx -= srv_to_cli_epoch0;
222         }
223     } else {
224         mempackbio = SSL_get_wbio(clientssl);
225         if (idx >= cli_to_srv_epoch0) {
226             epoch = 1;
227             idx -= cli_to_srv_epoch0;
228         }
229          mempackbio = BIO_next(mempackbio);
230     }
231     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_EPOCH, epoch, NULL);
232     BIO_ctrl(mempackbio, MEMPACKET_CTRL_SET_DROP_REC, idx, NULL);
233
234     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
235         goto end;
236
237     if (sess != NULL && !TEST_true(SSL_session_reused(clientssl)))
238         goto end;
239
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,
242                                    NULL), -1))
243         goto end;
244
245     testresult = 1;
246  end:
247     SSL_SESSION_free(sess);
248     SSL_free(serverssl);
249     SSL_free(clientssl);
250     SSL_CTX_free(sctx);
251     SSL_CTX_free(cctx);
252
253     return testresult;
254 }
255 #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
256
257 static const char dummy_cookie[] = "0123456";
258
259 static int generate_cookie_cb(SSL *ssl, unsigned char *cookie,
260                               unsigned int *cookie_len)
261 {
262     memcpy(cookie, dummy_cookie, sizeof(dummy_cookie));
263     *cookie_len = sizeof(dummy_cookie);
264     return 1;
265 }
266
267 static int verify_cookie_cb(SSL *ssl, const unsigned char *cookie,
268                             unsigned int cookie_len)
269 {
270     return TEST_mem_eq(cookie, cookie_len, dummy_cookie, sizeof(dummy_cookie));
271 }
272
273 static int test_cookie(void)
274 {
275     SSL_CTX *sctx = NULL, *cctx = NULL;
276     SSL *serverssl = NULL, *clientssl = NULL;
277     int testresult = 0;
278
279     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
280                                        DTLS_client_method(),
281                                        DTLS1_VERSION, 0,
282                                        &sctx, &cctx, cert, privkey)))
283         return 0;
284
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);
288
289     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
290                                       NULL, NULL))
291             || !TEST_true(create_ssl_connection(serverssl, clientssl,
292                                                 SSL_ERROR_NONE)))
293         goto end;
294
295     testresult = 1;
296  end:
297     SSL_free(serverssl);
298     SSL_free(clientssl);
299     SSL_CTX_free(sctx);
300     SSL_CTX_free(cctx);
301
302     return testresult;
303 }
304
305 static int test_dtls_duplicate_records(void)
306 {
307     SSL_CTX *sctx = NULL, *cctx = NULL;
308     SSL *serverssl = NULL, *clientssl = NULL;
309     int testresult = 0;
310
311     if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(),
312                                        DTLS_client_method(),
313                                        DTLS1_VERSION, 0,
314                                        &sctx, &cctx, cert, privkey)))
315         return 0;
316
317     if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
318                                       NULL, NULL)))
319         goto end;
320
321     DTLS_set_timer_cb(clientssl, timer_cb);
322     DTLS_set_timer_cb(serverssl, timer_cb);
323
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);
326
327     if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
328         goto end;
329
330     testresult = 1;
331  end:
332     SSL_free(serverssl);
333     SSL_free(clientssl);
334     SSL_CTX_free(sctx);
335     SSL_CTX_free(cctx);
336
337     return testresult;
338 }
339
340 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
341
342 int setup_tests(void)
343 {
344     if (!test_skip_common_options()) {
345         TEST_error("Error parsing test options\n");
346         return 0;
347     }
348
349     if (!TEST_ptr(cert = test_get_argument(0))
350             || !TEST_ptr(privkey = test_get_argument(1)))
351         return 0;
352
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);
356 #endif
357     ADD_TEST(test_cookie);
358     ADD_TEST(test_dtls_duplicate_records);
359
360     return 1;
361 }
362
363 void cleanup_tests(void)
364 {
365     bio_f_tls_dump_filter_free();
366     bio_s_mempacket_test_free();
367 }