blob: eb6a0f8295f1d46fa0e20de531b8fdd5ef7ecbbe [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Rich Salz846e33c2016-05-17 14:18:30 -04002 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
Ben Laurie36d16f82005-04-26 16:02:40 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (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
Ben Laurie36d16f82005-04-26 16:02:40 +00008 */
9
10#include <stdio.h>
Dr. Stephen Henson9289f212009-05-16 11:15:42 +000011#define USE_SOCKETS
Ben Laurie36d16f82005-04-26 16:02:40 +000012#include <openssl/objects.h>
Matt Caswell8ba708e2015-09-11 10:48:59 +010013#include <openssl/rand.h>
Ben Laurie36d16f82005-04-26 16:02:40 +000014#include "ssl_locl.h"
15
Richard Levitte14097b62017-02-28 14:07:08 +010016#if defined(OPENSSL_SYS_VXWORKS)
Matt Caswellfb456902015-04-30 09:40:55 +010017# include <sys/times.h>
18#elif !defined(OPENSSL_SYS_WIN32)
19# include <sys/time.h>
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +000020#endif
21
22static void get_current_time(struct timeval *t);
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +000023static int dtls1_handshake_write(SSL *s);
Matt Caswell7ee86272016-09-07 11:34:39 +010024static size_t dtls1_link_min_mtu(void);
Ben Laurie36d16f82005-04-26 16:02:40 +000025
Matt Caswell8ba708e2015-09-11 10:48:59 +010026/* XDTLS: figure out the right values */
Matt Caswell7ee86272016-09-07 11:34:39 +010027static const size_t g_probable_mtu[] = { 1500, 512, 256 };
Matt Caswell8ba708e2015-09-11 10:48:59 +010028
Matt Caswell0f113f32015-01-22 03:40:55 +000029const SSL3_ENC_METHOD DTLSv1_enc_data = {
30 tls1_enc,
31 tls1_mac,
32 tls1_setup_key_block,
33 tls1_generate_master_secret,
34 tls1_change_cipher_state,
35 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000036 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
37 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
38 tls1_alert_code,
39 tls1_export_keying_material,
40 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV,
Matt Caswella29fa982016-09-29 22:40:15 +010041 dtls1_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010042 dtls1_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000043 dtls1_handshake_write
44};
Ben Laurie36d16f82005-04-26 16:02:40 +000045
Matt Caswell0f113f32015-01-22 03:40:55 +000046const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
47 tls1_enc,
48 tls1_mac,
49 tls1_setup_key_block,
50 tls1_generate_master_secret,
51 tls1_change_cipher_state,
52 tls1_final_finish_mac,
Matt Caswell0f113f32015-01-22 03:40:55 +000053 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
54 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
55 tls1_alert_code,
56 tls1_export_keying_material,
57 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS
58 | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
Matt Caswella29fa982016-09-29 22:40:15 +010059 dtls1_set_handshake_header,
Matt Caswell2c7b4db2016-08-03 20:57:52 +010060 dtls1_close_construct_packet,
Matt Caswell0f113f32015-01-22 03:40:55 +000061 dtls1_handshake_write
62};
Dr. Stephen Hensonc3b344e2013-03-20 15:49:14 +000063
Dr. Stephen Hensonf3b656b2005-08-05 23:56:11 +000064long dtls1_default_timeout(void)
Matt Caswell0f113f32015-01-22 03:40:55 +000065{
66 /*
67 * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for
68 * http, the cache would over fill
69 */
70 return (60 * 60 * 2);
71}
Ben Laurie36d16f82005-04-26 16:02:40 +000072
Ben Laurie36d16f82005-04-26 16:02:40 +000073int dtls1_new(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +000074{
75 DTLS1_STATE *d1;
Ben Laurie36d16f82005-04-26 16:02:40 +000076
Viktor Dukhovni61986d32015-04-16 01:50:03 -040077 if (!DTLS_RECORD_LAYER_new(&s->rlayer)) {
Matt Caswell5fb6f802015-02-03 16:11:49 +000078 return 0;
79 }
FdaSilvaYY0485d542016-06-28 22:51:27 +020080
Matt Caswell0f113f32015-01-22 03:40:55 +000081 if (!ssl3_new(s))
82 return (0);
Rich Salzb51bce92015-08-25 13:25:58 -040083 if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +000084 ssl3_free(s);
85 return (0);
86 }
Ben Laurie36d16f82005-04-26 16:02:40 +000087
Matt Caswell0f113f32015-01-22 03:40:55 +000088 d1->buffered_messages = pqueue_new();
89 d1->sent_messages = pqueue_new();
Ben Laurie36d16f82005-04-26 16:02:40 +000090
Matt Caswell0f113f32015-01-22 03:40:55 +000091 if (s->server) {
92 d1->cookie_len = sizeof(s->d1->cookie);
93 }
Ben Laurie36d16f82005-04-26 16:02:40 +000094
Matt Caswell0f113f32015-01-22 03:40:55 +000095 d1->link_mtu = 0;
96 d1->mtu = 0;
Matt Caswell59669b62014-12-01 23:58:05 +000097
Matt Caswella71edf32015-10-30 10:05:53 +000098 if (d1->buffered_messages == NULL || d1->sent_messages == NULL) {
Rich Salz25aaa982015-05-01 14:37:16 -040099 pqueue_free(d1->buffered_messages);
100 pqueue_free(d1->sent_messages);
Matt Caswell0f113f32015-01-22 03:40:55 +0000101 OPENSSL_free(d1);
102 ssl3_free(s);
103 return (0);
104 }
Ben Laurie36d16f82005-04-26 16:02:40 +0000105
Matt Caswell0f113f32015-01-22 03:40:55 +0000106 s->d1 = d1;
107 s->method->ssl_clear(s);
108 return (1);
109}
Ben Laurie36d16f82005-04-26 16:02:40 +0000110
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000111static void dtls1_clear_queues(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000112{
Matt Caswellf5c7f5d2016-06-30 13:17:08 +0100113 dtls1_clear_received_buffer(s);
114 dtls1_clear_sent_buffer(s);
115}
116
117void dtls1_clear_received_buffer(SSL *s)
118{
Ben Laurie36d16f82005-04-26 16:02:40 +0000119 pitem *item = NULL;
120 hm_fragment *frag = NULL;
Dr. Stephen Henson006b54a2011-05-25 14:30:20 +0000121
Matt Caswell0f113f32015-01-22 03:40:55 +0000122 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
Ben Laurie36d16f82005-04-26 16:02:40 +0000123 frag = (hm_fragment *)item->data;
Matt Caswell8a35dbb2014-11-25 13:36:00 +0000124 dtls1_hm_fragment_free(frag);
Ben Laurie36d16f82005-04-26 16:02:40 +0000125 pitem_free(item);
Matt Caswell0f113f32015-01-22 03:40:55 +0000126 }
Matt Caswellf5c7f5d2016-06-30 13:17:08 +0100127}
128
129void dtls1_clear_sent_buffer(SSL *s)
130{
131 pitem *item = NULL;
132 hm_fragment *frag = NULL;
Ben Laurie36d16f82005-04-26 16:02:40 +0000133
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
Ben Laurie36d16f82005-04-26 16:02:40 +0000135 frag = (hm_fragment *)item->data;
Matt Caswell8a35dbb2014-11-25 13:36:00 +0000136 dtls1_hm_fragment_free(frag);
Ben Laurie36d16f82005-04-26 16:02:40 +0000137 pitem_free(item);
Matt Caswell0f113f32015-01-22 03:40:55 +0000138 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000139}
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000140
Matt Caswellf5c7f5d2016-06-30 13:17:08 +0100141
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000142void dtls1_free(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000143{
Matt Caswell40f37182015-02-03 14:54:13 +0000144 DTLS_RECORD_LAYER_free(&s->rlayer);
145
Matt Caswell0f113f32015-01-22 03:40:55 +0000146 ssl3_free(s);
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000147
Matt Caswell0f113f32015-01-22 03:40:55 +0000148 dtls1_clear_queues(s);
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000149
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000150 pqueue_free(s->d1->buffered_messages);
Matt Caswell0f113f32015-01-22 03:40:55 +0000151 pqueue_free(s->d1->sent_messages);
Dr. Stephen Hensone5fa8642009-04-15 15:27:03 +0000152
Matt Caswell0f113f32015-01-22 03:40:55 +0000153 OPENSSL_free(s->d1);
154 s->d1 = NULL;
155}
Ben Laurie36d16f82005-04-26 16:02:40 +0000156
157void dtls1_clear(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000158{
Rich Salzcf2cede2016-01-22 14:54:01 -0500159 pqueue *buffered_messages;
160 pqueue *sent_messages;
Matt Caswell7ee86272016-09-07 11:34:39 +0100161 size_t mtu;
162 size_t link_mtu;
Dr. Stephen Henson1d7392f2011-09-23 13:34:48 +0000163
Matt Caswell40f37182015-02-03 14:54:13 +0000164 DTLS_RECORD_LAYER_clear(&s->rlayer);
165
Matt Caswell0f113f32015-01-22 03:40:55 +0000166 if (s->d1) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000167 buffered_messages = s->d1->buffered_messages;
168 sent_messages = s->d1->sent_messages;
Matt Caswell0f113f32015-01-22 03:40:55 +0000169 mtu = s->d1->mtu;
170 link_mtu = s->d1->link_mtu;
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000171
Matt Caswell0f113f32015-01-22 03:40:55 +0000172 dtls1_clear_queues(s);
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000173
Rich Salz16f8d4e2015-05-04 18:00:15 -0400174 memset(s->d1, 0, sizeof(*s->d1));
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000175
Matt Caswell0f113f32015-01-22 03:40:55 +0000176 if (s->server) {
177 s->d1->cookie_len = sizeof(s->d1->cookie);
178 }
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000179
Matt Caswell0f113f32015-01-22 03:40:55 +0000180 if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) {
181 s->d1->mtu = mtu;
182 s->d1->link_mtu = link_mtu;
183 }
Dr. Stephen Henson1d7392f2011-09-23 13:34:48 +0000184
Matt Caswell0f113f32015-01-22 03:40:55 +0000185 s->d1->buffered_messages = buffered_messages;
186 s->d1->sent_messages = sent_messages;
Matt Caswell0f113f32015-01-22 03:40:55 +0000187 }
Dr. Stephen Henson7832d6a2011-05-25 12:28:06 +0000188
Matt Caswell0f113f32015-01-22 03:40:55 +0000189 ssl3_clear(s);
David Woodhouse032924c2016-07-25 18:03:27 +0100190
191 if (s->method->version == DTLS_ANY_VERSION)
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500192 s->version = DTLS_MAX_VERSION;
David Woodhouse032924c2016-07-25 18:03:27 +0100193#ifndef OPENSSL_NO_DTLS1_METHOD
194 else if (s->options & SSL_OP_CISCO_ANYCONNECT)
195 s->client_version = s->version = DTLS1_BAD_VER;
196#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000197 else
198 s->version = s->method->version;
199}
Andy Polyakov5d58f1b2007-10-05 21:04:56 +0000200
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000201long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000202{
203 int ret = 0;
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000204
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 switch (cmd) {
206 case DTLS_CTRL_GET_TIMEOUT:
207 if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) {
208 ret = 1;
209 }
210 break;
211 case DTLS_CTRL_HANDLE_TIMEOUT:
212 ret = dtls1_handle_timeout(s);
213 break;
Matt Caswell0f113f32015-01-22 03:40:55 +0000214 case DTLS_CTRL_SET_LINK_MTU:
215 if (larg < (long)dtls1_link_min_mtu())
216 return 0;
217 s->d1->link_mtu = larg;
218 return 1;
219 case DTLS_CTRL_GET_LINK_MIN_MTU:
220 return (long)dtls1_link_min_mtu();
221 case SSL_CTRL_SET_MTU:
222 /*
223 * We may not have a BIO set yet so can't call dtls1_min_mtu()
224 * We'll have to make do with dtls1_link_min_mtu() and max overhead
225 */
226 if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD)
227 return 0;
228 s->d1->mtu = larg;
229 return larg;
230 default:
231 ret = ssl3_ctrl(s, cmd, larg, parg);
232 break;
233 }
234 return (ret);
235}
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000236
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000237void dtls1_start_timer(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000238{
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000239#ifndef OPENSSL_NO_SCTP
Matt Caswell0f113f32015-01-22 03:40:55 +0000240 /* Disable timer for SCTP */
241 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
Rich Salz16f8d4e2015-05-04 18:00:15 -0400242 memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
Matt Caswell0f113f32015-01-22 03:40:55 +0000243 return;
244 }
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000245#endif
246
Matt Caswell0f113f32015-01-22 03:40:55 +0000247 /* If timer is not set, initialize duration with 1 second */
248 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
249 s->d1->timeout_duration = 1;
250 }
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000251
Matt Caswell0f113f32015-01-22 03:40:55 +0000252 /* Set timeout to current time */
253 get_current_time(&(s->d1->next_timeout));
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000254
Matt Caswell0f113f32015-01-22 03:40:55 +0000255 /* Add duration to current time */
256 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
257 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
258 &(s->d1->next_timeout));
259}
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000260
Matt Caswell0f113f32015-01-22 03:40:55 +0000261struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
262{
263 struct timeval timenow;
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000264
Matt Caswell0f113f32015-01-22 03:40:55 +0000265 /* If no timeout is set, just return NULL */
266 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
267 return NULL;
268 }
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000269
Matt Caswell0f113f32015-01-22 03:40:55 +0000270 /* Get current time */
271 get_current_time(&timenow);
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000272
Matt Caswell0f113f32015-01-22 03:40:55 +0000273 /* If timer already expired, set remaining time to 0 */
274 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
275 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
276 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
Rich Salz16f8d4e2015-05-04 18:00:15 -0400277 memset(timeleft, 0, sizeof(*timeleft));
Matt Caswell0f113f32015-01-22 03:40:55 +0000278 return timeleft;
279 }
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000280
Matt Caswell0f113f32015-01-22 03:40:55 +0000281 /* Calculate time left until timer expires */
282 memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval));
283 timeleft->tv_sec -= timenow.tv_sec;
284 timeleft->tv_usec -= timenow.tv_usec;
285 if (timeleft->tv_usec < 0) {
286 timeleft->tv_sec--;
287 timeleft->tv_usec += 1000000;
288 }
Dr. Stephen Henson87a37cb2010-04-06 12:29:31 +0000289
Matt Caswell0f113f32015-01-22 03:40:55 +0000290 /*
291 * If remaining time is less than 15 ms, set it to 0 to prevent issues
FdaSilvaYYf430ba32016-06-19 19:39:01 +0200292 * because of small divergences with socket timeouts.
Matt Caswell0f113f32015-01-22 03:40:55 +0000293 */
294 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
Rich Salz16f8d4e2015-05-04 18:00:15 -0400295 memset(timeleft, 0, sizeof(*timeleft));
Matt Caswell0f113f32015-01-22 03:40:55 +0000296 }
297
298 return timeleft;
299}
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000300
301int dtls1_is_timer_expired(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000302{
303 struct timeval timeleft;
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000304
Matt Caswell0f113f32015-01-22 03:40:55 +0000305 /* Get time left until timeout, return false if no timer running */
306 if (dtls1_get_timeout(s, &timeleft) == NULL) {
307 return 0;
308 }
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000309
Matt Caswell0f113f32015-01-22 03:40:55 +0000310 /* Return false if timer is not expired yet */
311 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
312 return 0;
313 }
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000314
Matt Caswell0f113f32015-01-22 03:40:55 +0000315 /* Timer expired, so return true */
316 return 1;
317}
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000318
319void dtls1_double_timeout(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000320{
321 s->d1->timeout_duration *= 2;
322 if (s->d1->timeout_duration > 60)
323 s->d1->timeout_duration = 60;
324 dtls1_start_timer(s);
325}
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000326
327void dtls1_stop_timer(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000328{
329 /* Reset everything */
Rich Salz16f8d4e2015-05-04 18:00:15 -0400330 memset(&s->d1->timeout, 0, sizeof(s->d1->timeout));
331 memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
Matt Caswell0f113f32015-01-22 03:40:55 +0000332 s->d1->timeout_duration = 1;
333 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
334 &(s->d1->next_timeout));
335 /* Clear retransmission buffer */
Matt Caswellf5c7f5d2016-06-30 13:17:08 +0100336 dtls1_clear_sent_buffer(s);
Matt Caswell0f113f32015-01-22 03:40:55 +0000337}
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000338
Dr. Stephen Hensonea6e3862012-03-09 15:52:33 +0000339int dtls1_check_timeout_num(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000340{
Matt Caswell7ee86272016-09-07 11:34:39 +0100341 size_t mtu;
Matt Caswell047f2152014-12-02 11:16:35 +0000342
Matt Caswell0f113f32015-01-22 03:40:55 +0000343 s->d1->timeout.num_alerts++;
Dr. Stephen Hensonea6e3862012-03-09 15:52:33 +0000344
Matt Caswell0f113f32015-01-22 03:40:55 +0000345 /* Reduce MTU after 2 unsuccessful retransmissions */
346 if (s->d1->timeout.num_alerts > 2
347 && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
348 mtu =
Emilia Kaspera230b262016-08-05 19:03:17 +0200349 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
Matt Caswell0f113f32015-01-22 03:40:55 +0000350 if (mtu < s->d1->mtu)
351 s->d1->mtu = mtu;
352 }
Dr. Stephen Hensonea6e3862012-03-09 15:52:33 +0000353
Matt Caswell0f113f32015-01-22 03:40:55 +0000354 if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
355 /* fail the connection, enough alerts have been sent */
356 SSLerr(SSL_F_DTLS1_CHECK_TIMEOUT_NUM, SSL_R_READ_TIMEOUT_EXPIRED);
357 return -1;
358 }
Dr. Stephen Hensonea6e3862012-03-09 15:52:33 +0000359
Matt Caswell0f113f32015-01-22 03:40:55 +0000360 return 0;
361}
Dr. Stephen Hensonea6e3862012-03-09 15:52:33 +0000362
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000363int dtls1_handle_timeout(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000364{
365 /* if no timer is expired, don't do anything */
366 if (!dtls1_is_timer_expired(s)) {
367 return 0;
368 }
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000369
Matt Caswell0f113f32015-01-22 03:40:55 +0000370 dtls1_double_timeout(s);
Dr. Stephen Hensonea6e3862012-03-09 15:52:33 +0000371
Matt Caswell0f113f32015-01-22 03:40:55 +0000372 if (dtls1_check_timeout_num(s) < 0)
373 return -1;
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000374
Matt Caswell0f113f32015-01-22 03:40:55 +0000375 s->d1->timeout.read_timeouts++;
376 if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
377 s->d1->timeout.read_timeouts = 1;
378 }
Dr. Stephen Henson48175042011-12-31 22:59:57 +0000379
Matt Caswell0f113f32015-01-22 03:40:55 +0000380 dtls1_start_timer(s);
381 return dtls1_retransmit_buffered_messages(s);
382}
Dr. Stephen Hensonb972fba2009-08-12 13:19:54 +0000383
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000384static void get_current_time(struct timeval *t)
385{
Andy Polyakova006fef2013-01-19 21:23:13 +0100386#if defined(_WIN32)
Matt Caswell0f113f32015-01-22 03:40:55 +0000387 SYSTEMTIME st;
388 union {
389 unsigned __int64 ul;
390 FILETIME ft;
391 } now;
Andy Polyakovf4698802012-12-16 19:02:59 +0000392
Matt Caswell0f113f32015-01-22 03:40:55 +0000393 GetSystemTime(&st);
394 SystemTimeToFileTime(&st, &now.ft);
Emilia Kaspera230b262016-08-05 19:03:17 +0200395 /* re-bias to 1/1/1970 */
Matt Caswell0f113f32015-01-22 03:40:55 +0000396# ifdef __MINGW32__
397 now.ul -= 116444736000000000ULL;
398# else
Emilia Kaspera230b262016-08-05 19:03:17 +0200399 /* *INDENT-OFF* */
400 now.ul -= 116444736000000000UI64;
401 /* *INDENT-ON* */
Matt Caswell0f113f32015-01-22 03:40:55 +0000402# endif
403 t->tv_sec = (long)(now.ul / 10000000);
404 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000405#else
Matt Caswell0f113f32015-01-22 03:40:55 +0000406 gettimeofday(t, NULL);
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000407#endif
408}
Dr. Stephen Henson1fc3ac82009-09-09 17:05:18 +0000409
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100410#define LISTEN_SUCCESS 2
411#define LISTEN_SEND_VERIFY_REQUEST 1
412
Matt Caswellf9e55032016-03-21 15:32:40 +0000413#ifndef OPENSSL_NO_SOCK
Matt Caswell3edeb622016-02-05 10:59:42 +0000414int DTLSv1_listen(SSL *s, BIO_ADDR *client)
Matt Caswell0f113f32015-01-22 03:40:55 +0000415{
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100416 int next, n, ret = 0, clearpkt = 0;
417 unsigned char cookie[DTLS1_COOKIE_LENGTH];
418 unsigned char seq[SEQ_NUM_SIZE];
Emilia Kasperb6981742016-02-01 15:26:18 +0100419 const unsigned char *data;
Matt Caswellc536b6b2016-09-21 11:26:47 +0100420 unsigned char *buf;
Matt Caswell8b0e9342016-10-06 19:17:54 +0100421 size_t fragoff, fraglen, msglen;
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100422 unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;
423 BIO *rbio, *wbio;
424 BUF_MEM *bufm;
Richard Levitted858c872016-02-03 00:27:44 +0100425 BIO_ADDR *tmpclient = NULL;
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100426 PACKET pkt, msgpkt, msgpayload, session, cookiepkt;
Dr. Stephen Henson1fc3ac82009-09-09 17:05:18 +0000427
Matt Caswell5bdcd362016-11-23 23:03:13 +0000428 if (s->handshake_func == NULL) {
429 /* Not properly initialized yet */
430 SSL_set_accept_state(s);
431 }
432
Matt Caswelle83ee042015-03-09 16:09:04 +0000433 /* Ensure there is no state left over from a previous invocation */
Viktor Dukhovni61986d32015-04-16 01:50:03 -0400434 if (!SSL_clear(s))
Matt Caswellc7f5b5d2015-03-23 15:27:40 +0000435 return -1;
Matt Caswelle83ee042015-03-09 16:09:04 +0000436
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100437 ERR_clear_error();
438
439 rbio = SSL_get_rbio(s);
440 wbio = SSL_get_wbio(s);
441
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200442 if (!rbio || !wbio) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000443 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100444 return -1;
445 }
446
447 /*
448 * We only peek at incoming ClientHello's until we're sure we are going to
449 * to respond with a HelloVerifyRequest. If its a ClientHello with a valid
Viktor Dukhovniaea145e2016-01-09 17:18:02 -0500450 * cookie then we leave it in the BIO for accept to handle.
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100451 */
452 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);
453
454 /*
455 * Note: This check deliberately excludes DTLS1_BAD_VER because that version
456 * requires the MAC to be calculated *including* the first ClientHello
457 * (without the cookie). Since DTLSv1_listen is stateless that cannot be
458 * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via
459 * SSL_accept)
460 */
461 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000462 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100463 return -1;
464 }
465
466 if (s->init_buf == NULL) {
467 if ((bufm = BUF_MEM_new()) == NULL) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000468 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100469 return -1;
470 }
471
472 if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) {
473 BUF_MEM_free(bufm);
Matt Caswell3edeb622016-02-05 10:59:42 +0000474 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100475 return -1;
476 }
477 s->init_buf = bufm;
478 }
479 buf = (unsigned char *)s->init_buf->data;
480
481 do {
482 /* Get a packet */
483
484 clear_sys_error();
485 /*
486 * Technically a ClientHello could be SSL3_RT_MAX_PLAIN_LENGTH
487 * + DTLS1_RT_HEADER_LENGTH bytes long. Normally init_buf does not store
488 * the record header as well, but we do here. We've set up init_buf to
489 * be the standard size for simplicity. In practice we shouldn't ever
490 * receive a ClientHello as long as this. If we do it will get dropped
491 * in the record length check below.
492 */
493 n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);
494
495 if (n <= 0) {
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200496 if (BIO_should_retry(rbio)) {
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100497 /* Non-blocking IO */
498 goto end;
499 }
500 return -1;
501 }
502
503 /* If we hit any problems we need to clear this packet from the BIO */
504 clearpkt = 1;
505
506 if (!PACKET_buf_init(&pkt, buf, n)) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000507 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100508 return -1;
509 }
510
511 /*
512 * Parse the received record. If there are any problems with it we just
513 * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is
514 * resilient in the face of invalid records (e.g., invalid formatting,
515 * length, MAC, etc.). In general, invalid records SHOULD be silently
516 * discarded, thus preserving the association; however, an error MAY be
517 * logged for diagnostic purposes."
518 */
519
520 /* this packet contained a partial record, dump it */
521 if (n < DTLS1_RT_HEADER_LENGTH) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000522 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100523 goto end;
524 }
525
526 if (s->msg_callback)
527 s->msg_callback(0, 0, SSL3_RT_HEADER, buf,
528 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
529
530 /* Get the record header */
531 if (!PACKET_get_1(&pkt, &rectype)
532 || !PACKET_get_1(&pkt, &versmajor)) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000533 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100534 goto end;
535 }
536
Emilia Kaspera230b262016-08-05 19:03:17 +0200537 if (rectype != SSL3_RT_HANDSHAKE) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000538 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100539 goto end;
540 }
541
542 /*
543 * Check record version number. We only check that the major version is
544 * the same.
545 */
546 if (versmajor != DTLS1_VERSION_MAJOR) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000547 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100548 goto end;
549 }
550
551 if (!PACKET_forward(&pkt, 1)
552 /* Save the sequence number: 64 bits, with top 2 bytes = epoch */
553 || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)
Matt Caswell4b1043e2015-11-04 13:53:57 +0000554 || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000555 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100556 goto end;
557 }
Matt Caswell4b1043e2015-11-04 13:53:57 +0000558 /*
559 * We allow data remaining at the end of the packet because there could
560 * be a second record (but we ignore it)
561 */
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100562
563 /* This is an initial ClientHello so the epoch has to be 0 */
564 if (seq[0] != 0 || seq[1] != 0) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000565 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100566 goto end;
567 }
568
569 /* Get a pointer to the raw message for the later callback */
570 data = PACKET_data(&msgpkt);
571
572 /* Finished processing the record header, now process the message */
573 if (!PACKET_get_1(&msgpkt, &msgtype)
Matt Caswell153703d2016-10-19 14:39:39 +0100574 || !PACKET_get_net_3_len(&msgpkt, &msglen)
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100575 || !PACKET_get_net_2(&msgpkt, &msgseq)
Matt Caswell153703d2016-10-19 14:39:39 +0100576 || !PACKET_get_net_3_len(&msgpkt, &fragoff)
577 || !PACKET_get_net_3_len(&msgpkt, &fraglen)
Matt Caswell4b1043e2015-11-04 13:53:57 +0000578 || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100579 || PACKET_remaining(&msgpkt) != 0) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000580 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100581 goto end;
582 }
583
584 if (msgtype != SSL3_MT_CLIENT_HELLO) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000585 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100586 goto end;
587 }
588
589 /* Message sequence number can only be 0 or 1 */
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200590 if (msgseq > 2) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000591 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100592 goto end;
593 }
594
Matt Caswell4b1043e2015-11-04 13:53:57 +0000595 /*
596 * We don't support fragment reassembly for ClientHellos whilst
597 * listening because that would require server side state (which is
598 * against the whole point of the ClientHello/HelloVerifyRequest
599 * mechanism). Instead we only look at the first ClientHello fragment
600 * and require that the cookie must be contained within it.
601 */
602 if (fragoff != 0 || fraglen > msglen) {
603 /* Non initial ClientHello fragment (or bad fragment) */
Matt Caswell3edeb622016-02-05 10:59:42 +0000604 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100605 goto end;
606 }
607
608 if (s->msg_callback)
609 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,
Matt Caswell4b1043e2015-11-04 13:53:57 +0000610 fraglen + DTLS1_HM_HEADER_LENGTH, s,
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100611 s->msg_callback_arg);
612
613 if (!PACKET_get_net_2(&msgpayload, &clientvers)) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000614 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100615 goto end;
616 }
617
618 /*
619 * Verify client version is supported
620 */
Viktor Dukhovni4fa52142015-12-29 03:24:17 -0500621 if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&
622 s->method->version != DTLS_ANY_VERSION) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000623 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100624 goto end;
625 }
626
627 if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)
628 || !PACKET_get_length_prefixed_1(&msgpayload, &session)
629 || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {
Matt Caswell4b1043e2015-11-04 13:53:57 +0000630 /*
631 * Could be malformed or the cookie does not fit within the initial
632 * ClientHello fragment. Either way we can't handle it.
633 */
Matt Caswell3edeb622016-02-05 10:59:42 +0000634 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100635 goto end;
636 }
637
638 /*
639 * Check if we have a cookie or not. If not we need to send a
640 * HelloVerifyRequest.
641 */
642 if (PACKET_remaining(&cookiepkt) == 0) {
643 next = LISTEN_SEND_VERIFY_REQUEST;
644 } else {
645 /*
646 * We have a cookie, so lets check it.
647 */
648 if (s->ctx->app_verify_cookie_cb == NULL) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000649 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100650 /* This is fatal */
651 return -1;
652 }
Emilia Kasper31011542015-10-06 17:20:32 +0200653 if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),
Matt Caswell8b0e9342016-10-06 19:17:54 +0100654 (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100655 /*
656 * We treat invalid cookies in the same was as no cookie as
657 * per RFC6347
658 */
659 next = LISTEN_SEND_VERIFY_REQUEST;
660 } else {
661 /* Cookie verification succeeded */
662 next = LISTEN_SUCCESS;
663 }
664 }
665
666 if (next == LISTEN_SEND_VERIFY_REQUEST) {
Matt Caswellc536b6b2016-09-21 11:26:47 +0100667 WPACKET wpkt;
668 unsigned int version;
669 size_t wreclen;
670
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100671 /*
672 * There was no cookie in the ClientHello so we need to send a
673 * HelloVerifyRequest. If this fails we do not worry about trying
674 * to resend, we just drop it.
675 */
676
677 /*
678 * Dump the read packet, we don't need it any more. Ignore return
679 * value
680 */
681 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);
682 BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);
683 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);
684
685 /* Generate the cookie */
686 if (s->ctx->app_gen_cookie_cb == NULL ||
Matt Caswell373dc6e2015-09-23 12:57:34 +0100687 s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||
688 cookielen > 255) {
Matt Caswell3edeb622016-02-05 10:59:42 +0000689 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100690 /* This is fatal */
691 return -1;
692 }
693
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100694 /*
695 * Special case: for hello verify request, client version 1.0 and we
696 * haven't decided which version to use yet send back using version
697 * 1.0 header: otherwise some clients will ignore it.
698 */
Matt Caswellc536b6b2016-09-21 11:26:47 +0100699 version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION
700 : s->version;
701
702 /* Construct the record and message headers */
703 if (!WPACKET_init(&wpkt, s->init_buf)
704 || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)
705 || !WPACKET_put_bytes_u16(&wpkt, version)
706 /*
707 * Record sequence number is always the same as in the
708 * received ClientHello
709 */
710 || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)
711 /* End of record, start sub packet for message */
712 || !WPACKET_start_sub_packet_u16(&wpkt)
713 /* Message type */
714 || !WPACKET_put_bytes_u8(&wpkt,
715 DTLS1_MT_HELLO_VERIFY_REQUEST)
716 /*
717 * Message length - doesn't follow normal TLS convention:
718 * the length isn't the last thing in the message header.
719 * We'll need to fill this in later when we know the
720 * length. Set it to zero for now
721 */
722 || !WPACKET_put_bytes_u24(&wpkt, 0)
723 /*
724 * Message sequence number is always 0 for a
725 * HelloVerifyRequest
726 */
727 || !WPACKET_put_bytes_u16(&wpkt, 0)
728 /*
729 * We never fragment a HelloVerifyRequest, so fragment
730 * offset is 0
731 */
732 || !WPACKET_put_bytes_u24(&wpkt, 0)
733 /*
734 * Fragment length is the same as message length, but
735 * this *is* the last thing in the message header so we
736 * can just start a sub-packet. No need to come back
737 * later for this one.
738 */
739 || !WPACKET_start_sub_packet_u24(&wpkt)
740 /* Create the actual HelloVerifyRequest body */
741 || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)
742 /* Close message body */
743 || !WPACKET_close(&wpkt)
744 /* Close record body */
745 || !WPACKET_close(&wpkt)
746 || !WPACKET_get_total_written(&wpkt, &wreclen)
747 || !WPACKET_finish(&wpkt)) {
748 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);
749 WPACKET_cleanup(&wpkt);
750 /* This is fatal */
751 return -1;
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100752 }
753
754 /*
Matt Caswellc536b6b2016-09-21 11:26:47 +0100755 * Fix up the message len in the message header. Its the same as the
756 * fragment len which has been filled in by WPACKET, so just copy
757 * that. Destination for the message len is after the record header
758 * plus one byte for the message content type. The source is the
759 * last 3 bytes of the message header
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100760 */
Matt Caswellc536b6b2016-09-21 11:26:47 +0100761 memcpy(&buf[DTLS1_RT_HEADER_LENGTH + 1],
762 &buf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],
763 3);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100764
765 if (s->msg_callback)
766 s->msg_callback(1, 0, SSL3_RT_HEADER, buf,
767 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
768
Matt Caswellce0865d2016-01-21 12:22:58 +0000769 if ((tmpclient = BIO_ADDR_new()) == NULL) {
770 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);
771 goto end;
772 }
773
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100774 /*
FdaSilvaYY8483a002016-03-10 21:34:48 +0100775 * This is unnecessary if rbio and wbio are one and the same - but
Matt Caswellce0865d2016-01-21 12:22:58 +0000776 * maybe they're not. We ignore errors here - some BIOs do not
777 * support this.
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100778 */
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200779 if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {
Matt Caswellce0865d2016-01-21 12:22:58 +0000780 (void)BIO_dgram_set_peer(wbio, tmpclient);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100781 }
Richard Levitted858c872016-02-03 00:27:44 +0100782 BIO_ADDR_free(tmpclient);
783 tmpclient = NULL;
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100784
Matt Caswell8b0e9342016-10-06 19:17:54 +0100785 /* TODO(size_t): convert this call */
Matt Caswellc536b6b2016-09-21 11:26:47 +0100786 if (BIO_write(wbio, buf, wreclen) < (int)wreclen) {
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200787 if (BIO_should_retry(wbio)) {
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100788 /*
789 * Non-blocking IO...but we're stateless, so we're just
790 * going to drop this packet.
791 */
792 goto end;
793 }
794 return -1;
795 }
796
797 if (BIO_flush(wbio) <= 0) {
FdaSilvaYYe8aa8b62016-06-29 00:18:50 +0200798 if (BIO_should_retry(wbio)) {
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100799 /*
800 * Non-blocking IO...but we're stateless, so we're just
801 * going to drop this packet.
802 */
803 goto end;
804 }
805 return -1;
806 }
807 }
808 } while (next != LISTEN_SUCCESS);
809
810 /*
811 * Set expected sequence numbers to continue the handshake.
812 */
813 s->d1->handshake_read_seq = 1;
814 s->d1->handshake_write_seq = 1;
815 s->d1->next_handshake_write_seq = 1;
816 DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);
817
818 /*
819 * We are doing cookie exchange, so make sure we set that option in the
820 * SSL object
821 */
Matt Caswell0f113f32015-01-22 03:40:55 +0000822 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
Dr. Stephen Henson1fc3ac82009-09-09 17:05:18 +0000823
Matt Caswell31fd10e2015-10-22 12:18:45 +0100824 /*
825 * Tell the state machine that we've done the initial hello verify
826 * exchange
827 */
828 ossl_statem_set_hello_verify_done(s);
Matt Caswell0f113f32015-01-22 03:40:55 +0000829
Emilia Kaspera230b262016-08-05 19:03:17 +0200830 /*
831 * Some BIOs may not support this. If we fail we clear the client address
832 */
Matt Caswellce0865d2016-01-21 12:22:58 +0000833 if (BIO_dgram_get_peer(rbio, client) <= 0)
834 BIO_ADDR_clear(client);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100835
836 ret = 1;
837 clearpkt = 0;
Emilia Kaspera230b262016-08-05 19:03:17 +0200838 end:
Richard Levitted858c872016-02-03 00:27:44 +0100839 BIO_ADDR_free(tmpclient);
Matt Caswelle3d0dae2015-09-14 22:49:35 +0100840 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);
841 if (clearpkt) {
842 /* Dump this packet. Ignore return value */
843 BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);
844 }
845 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000846}
Matt Caswellf9e55032016-03-21 15:32:40 +0000847#endif
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000848
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000849static int dtls1_handshake_write(SSL *s)
Matt Caswell0f113f32015-01-22 03:40:55 +0000850{
851 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
852}
Matt Caswell8ba708e2015-09-11 10:48:59 +0100853
Matt Caswell8ba708e2015-09-11 10:48:59 +0100854int dtls1_shutdown(SSL *s)
855{
856 int ret;
857#ifndef OPENSSL_NO_SCTP
858 BIO *wbio;
859
860 wbio = SSL_get_wbio(s);
861 if (wbio != NULL && BIO_dgram_is_sctp(wbio) &&
862 !(s->shutdown & SSL_SENT_SHUTDOWN)) {
863 ret = BIO_dgram_sctp_wait_for_dry(wbio);
864 if (ret < 0)
865 return -1;
866
867 if (ret == 0)
868 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1,
869 NULL);
870 }
871#endif
872 ret = ssl3_shutdown(s);
873#ifndef OPENSSL_NO_SCTP
874 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL);
875#endif
876 return ret;
877}
878
879int dtls1_query_mtu(SSL *s)
880{
881 if (s->d1->link_mtu) {
882 s->d1->mtu =
883 s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
884 s->d1->link_mtu = 0;
885 }
886
887 /* AHA! Figure out the MTU, and stick to the right size */
888 if (s->d1->mtu < dtls1_min_mtu(s)) {
889 if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
890 s->d1->mtu =
891 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
892
893 /*
894 * I've seen the kernel return bogus numbers when it doesn't know
895 * (initial write), so just make sure we have a reasonable number
896 */
897 if (s->d1->mtu < dtls1_min_mtu(s)) {
898 /* Set to min mtu */
899 s->d1->mtu = dtls1_min_mtu(s);
900 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
Matt Caswell8b0e9342016-10-06 19:17:54 +0100901 (long)s->d1->mtu, NULL);
Matt Caswell8ba708e2015-09-11 10:48:59 +0100902 }
903 } else
904 return 0;
905 }
906 return 1;
907}
908
Matt Caswell7ee86272016-09-07 11:34:39 +0100909static size_t dtls1_link_min_mtu(void)
Matt Caswell8ba708e2015-09-11 10:48:59 +0100910{
911 return (g_probable_mtu[(sizeof(g_probable_mtu) /
912 sizeof(g_probable_mtu[0])) - 1]);
913}
914
Matt Caswell7ee86272016-09-07 11:34:39 +0100915size_t dtls1_min_mtu(SSL *s)
Matt Caswell8ba708e2015-09-11 10:48:59 +0100916{
917 return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
918}
David Woodhouse045bd042016-10-06 00:44:59 +0100919
920size_t DTLS_get_data_mtu(const SSL *s)
921{
922 size_t mac_overhead, int_overhead, blocksize, ext_overhead;
923 const SSL_CIPHER *ciph = SSL_get_current_cipher(s);
924 size_t mtu = s->d1->mtu;
925
926 if (ciph == NULL)
927 return 0;
928
929 if (!ssl_cipher_get_overhead(ciph, &mac_overhead, &int_overhead,
930 &blocksize, &ext_overhead))
931 return 0;
932
Matt Caswell28a31a02017-02-03 14:06:20 +0000933 if (SSL_READ_ETM(s))
David Woodhouse045bd042016-10-06 00:44:59 +0100934 ext_overhead += mac_overhead;
935 else
936 int_overhead += mac_overhead;
937
938 /* Subtract external overhead (e.g. IV/nonce, separate MAC) */
939 if (ext_overhead + DTLS1_RT_HEADER_LENGTH >= mtu)
940 return 0;
941 mtu -= ext_overhead + DTLS1_RT_HEADER_LENGTH;
942
943 /* Round encrypted payload down to cipher block size (for CBC etc.)
944 * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */
945 if (blocksize)
946 mtu -= (mtu % blocksize);
947
948 /* Subtract internal overhead (e.g. CBC padding len byte) */
949 if (int_overhead >= mtu)
950 return 0;
951 mtu -= int_overhead;
952
953 return mtu;
954}