blob: 3da70cf60f4437fcec4775abe0f26e3f8115fc31 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/*
2 * DTLS implementation written by Nagendra Modadugu
3 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com). */
56
57#include <openssl/base.h>
58
David Benjamin80cee912015-01-11 19:36:58 -050059#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -070060#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -040061#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070062
63#if defined(OPENSSL_WINDOWS)
64#include <sys/timeb.h>
65#else
66#include <sys/socket.h>
Adam Langleyded93582014-07-31 15:23:51 -070067#include <sys/time.h>
Adam Langley95c29f32014-06-20 12:00:00 -070068#endif
69
70#include <openssl/err.h>
71#include <openssl/mem.h>
72#include <openssl/obj.h>
73
David Benjamin2ee94aa2015-04-07 22:38:30 -040074#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070075
David Benjamine33b9b02015-01-26 01:27:22 -050076/* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
77 * before starting to decrease the MTU. */
78#define DTLS1_MTU_TIMEOUTS 2
79
80/* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
81 * before failing the DTLS handshake. */
82#define DTLS1_MAX_TIMEOUTS 12
83
David Benjamin377fc312015-01-26 00:22:12 -050084static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock);
Adam Langley71d8a082014-12-13 16:28:18 -080085static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft);
Adam Langley95c29f32014-06-20 12:00:00 -070086
Adam Langley71d8a082014-12-13 16:28:18 -080087int dtls1_new(SSL *s) {
88 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -070089
Adam Langley71d8a082014-12-13 16:28:18 -080090 if (!ssl3_new(s)) {
91 return 0;
92 }
93 d1 = OPENSSL_malloc(sizeof *d1);
94 if (d1 == NULL) {
95 ssl3_free(s);
96 return 0;
97 }
98 memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -070099
Adam Langley71d8a082014-12-13 16:28:18 -0800100 d1->buffered_messages = pqueue_new();
101 d1->sent_messages = pqueue_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700102
David Benjamin4417d052015-04-05 04:17:25 -0400103 if (!d1->buffered_messages || !d1->sent_messages) {
Adam Langley71d8a082014-12-13 16:28:18 -0800104 if (d1->buffered_messages) {
105 pqueue_free(d1->buffered_messages);
106 }
107 if (d1->sent_messages) {
108 pqueue_free(d1->sent_messages);
109 }
Adam Langley71d8a082014-12-13 16:28:18 -0800110 OPENSSL_free(d1);
111 ssl3_free(s);
112 return 0;
113 }
Adam Langley95c29f32014-06-20 12:00:00 -0700114
Adam Langley71d8a082014-12-13 16:28:18 -0800115 s->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500116
117 /* Set the version to the highest version for DTLS. This controls the initial
118 * state of |s->enc_method| and what the API reports as the version prior to
119 * negotiation.
120 *
121 * TODO(davidben): This is fragile and confusing. */
122 s->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800123 return 1;
124}
Adam Langley95c29f32014-06-20 12:00:00 -0700125
Adam Langley71d8a082014-12-13 16:28:18 -0800126static void dtls1_clear_queues(SSL *s) {
127 pitem *item = NULL;
128 hm_fragment *frag = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700129
Adam Langley71d8a082014-12-13 16:28:18 -0800130 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
131 frag = (hm_fragment *)item->data;
132 dtls1_hm_fragment_free(frag);
133 pitem_free(item);
134 }
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langley71d8a082014-12-13 16:28:18 -0800136 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
137 frag = (hm_fragment *)item->data;
138 dtls1_hm_fragment_free(frag);
139 pitem_free(item);
140 }
Adam Langley71d8a082014-12-13 16:28:18 -0800141}
Adam Langley95c29f32014-06-20 12:00:00 -0700142
Adam Langley71d8a082014-12-13 16:28:18 -0800143void dtls1_free(SSL *s) {
144 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin62fd1622015-01-11 13:30:01 -0500146 if (s == NULL || s->d1 == NULL) {
147 return;
148 }
149
Adam Langley71d8a082014-12-13 16:28:18 -0800150 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700151
Adam Langley71d8a082014-12-13 16:28:18 -0800152 pqueue_free(s->d1->buffered_messages);
153 pqueue_free(s->d1->sent_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langley71d8a082014-12-13 16:28:18 -0800155 OPENSSL_free(s->d1);
156 s->d1 = NULL;
157}
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langley71d8a082014-12-13 16:28:18 -0800159long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
160 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700161
Adam Langley71d8a082014-12-13 16:28:18 -0800162 switch (cmd) {
163 case DTLS_CTRL_GET_TIMEOUT:
164 if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
165 ret = 1;
166 }
167 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langley71d8a082014-12-13 16:28:18 -0800169 case DTLS_CTRL_HANDLE_TIMEOUT:
170 ret = dtls1_handle_timeout(s);
171 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley71d8a082014-12-13 16:28:18 -0800173 default:
174 ret = ssl3_ctrl(s, cmd, larg, parg);
175 break;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langley71d8a082014-12-13 16:28:18 -0800178 return ret;
179}
180
Adam Langley71d8a082014-12-13 16:28:18 -0800181const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
182 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
David Benjamine95d20d2014-12-23 11:16:01 -0500183 /* DTLS does not support stream ciphers. */
184 if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) {
185 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800186 }
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langley71d8a082014-12-13 16:28:18 -0800188 return ciph;
189}
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley71d8a082014-12-13 16:28:18 -0800191void dtls1_start_timer(SSL *s) {
192 /* If timer is not set, initialize duration with 1 second */
193 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
194 s->d1->timeout_duration = 1;
195 }
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langley71d8a082014-12-13 16:28:18 -0800197 /* Set timeout to current time */
David Benjamin377fc312015-01-26 00:22:12 -0500198 get_current_time(s, &s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700199
Adam Langley71d8a082014-12-13 16:28:18 -0800200 /* Add duration to current time */
201 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
202 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
203 &s->d1->next_timeout);
204}
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley71d8a082014-12-13 16:28:18 -0800206static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
207 OPENSSL_timeval timenow;
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langley71d8a082014-12-13 16:28:18 -0800209 /* If no timeout is set, just return NULL */
210 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
211 return NULL;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langley71d8a082014-12-13 16:28:18 -0800214 /* Get current time */
David Benjamin377fc312015-01-26 00:22:12 -0500215 get_current_time(s, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langley71d8a082014-12-13 16:28:18 -0800217 /* If timer already expired, set remaining time to 0 */
218 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
219 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
220 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
221 memset(timeleft, 0, sizeof(OPENSSL_timeval));
222 return timeleft;
223 }
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langley71d8a082014-12-13 16:28:18 -0800225 /* Calculate time left until timer expires */
226 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
227 timeleft->tv_sec -= timenow.tv_sec;
228 timeleft->tv_usec -= timenow.tv_usec;
229 if (timeleft->tv_usec < 0) {
230 timeleft->tv_sec--;
231 timeleft->tv_usec += 1000000;
232 }
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley71d8a082014-12-13 16:28:18 -0800234 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
235 * because of small devergences with socket timeouts. */
236 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
237 memset(timeleft, 0, sizeof(OPENSSL_timeval));
238 }
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley71d8a082014-12-13 16:28:18 -0800240 return timeleft;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langley71d8a082014-12-13 16:28:18 -0800243int dtls1_is_timer_expired(SSL *s) {
244 OPENSSL_timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langley71d8a082014-12-13 16:28:18 -0800246 /* Get time left until timeout, return false if no timer running */
247 if (dtls1_get_timeout(s, &timeleft) == NULL) {
248 return 0;
249 }
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langley71d8a082014-12-13 16:28:18 -0800251 /* Return false if timer is not expired yet */
252 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
253 return 0;
254 }
Adam Langley95c29f32014-06-20 12:00:00 -0700255
Adam Langley71d8a082014-12-13 16:28:18 -0800256 /* Timer expired, so return true */
257 return 1;
258}
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley71d8a082014-12-13 16:28:18 -0800260void dtls1_double_timeout(SSL *s) {
261 s->d1->timeout_duration *= 2;
262 if (s->d1->timeout_duration > 60) {
263 s->d1->timeout_duration = 60;
264 }
265 dtls1_start_timer(s);
266}
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langley71d8a082014-12-13 16:28:18 -0800268void dtls1_stop_timer(SSL *s) {
269 /* Reset everything */
David Benjamine33b9b02015-01-26 01:27:22 -0500270 s->d1->num_timeouts = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800271 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
272 s->d1->timeout_duration = 1;
273 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
274 &s->d1->next_timeout);
275 /* Clear retransmission buffer */
276 dtls1_clear_record_buffer(s);
277}
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langley71d8a082014-12-13 16:28:18 -0800279int dtls1_check_timeout_num(SSL *s) {
David Benjamine33b9b02015-01-26 01:27:22 -0500280 s->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamine33b9b02015-01-26 01:27:22 -0500283 if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
David Benjamin7f18b132015-01-11 17:36:21 -0500284 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500285 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
286 NULL);
287 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
288 s->d1->mtu = (unsigned)mtu;
289 }
Adam Langley71d8a082014-12-13 16:28:18 -0800290 }
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamine33b9b02015-01-26 01:27:22 -0500292 if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800293 /* fail the connection, enough alerts have been sent */
294 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
295 return -1;
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley71d8a082014-12-13 16:28:18 -0800298 return 0;
299}
Adam Langley95c29f32014-06-20 12:00:00 -0700300
Adam Langley71d8a082014-12-13 16:28:18 -0800301int dtls1_handle_timeout(SSL *s) {
302 /* if no timer is expired, don't do anything */
303 if (!dtls1_is_timer_expired(s)) {
304 return 0;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langley71d8a082014-12-13 16:28:18 -0800307 dtls1_double_timeout(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 if (dtls1_check_timeout_num(s) < 0) {
310 return -1;
311 }
Adam Langley95c29f32014-06-20 12:00:00 -0700312
Adam Langley71d8a082014-12-13 16:28:18 -0800313 dtls1_start_timer(s);
314 return dtls1_retransmit_buffered_messages(s);
315}
316
David Benjamin377fc312015-01-26 00:22:12 -0500317static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) {
318 if (ssl->ctx->current_time_cb != NULL) {
319 ssl->ctx->current_time_cb(ssl, out_clock);
320 return;
321 }
322
Adam Langleyded93582014-07-31 15:23:51 -0700323#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800324 struct _timeb time;
325 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500326 out_clock->tv_sec = time.time;
327 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700328#else
David Benjamin377fc312015-01-26 00:22:12 -0500329 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700330#endif
331}
332
David Benjamin2fa83de2015-02-08 01:40:08 -0500333int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
David Benjamine4824e82014-12-14 19:44:34 -0500334 uint8_t *message = (uint8_t *)s->init_buf->data;
335 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
336 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
337 uint8_t *p = serialised_header;
338
David Benjamin16d031a2014-12-14 18:52:44 -0500339 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
340 s->d1->next_handshake_write_seq++;
341
342 dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800343 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
344 s->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500345
Adam Langley71d8a082014-12-13 16:28:18 -0800346 /* Buffer the message to handle re-xmits */
347 dtls1_buffer_message(s, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700348
David Benjamine4824e82014-12-14 19:44:34 -0500349 /* Add the new message to the handshake hash. Serialize the message
350 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800351 *p++ = msg_hdr->type;
352 l2n3(msg_hdr->msg_len, p);
353 s2n(msg_hdr->seq, p);
354 l2n3(0, p);
355 l2n3(msg_hdr->msg_len, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500356 return ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)) &&
357 ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
David Benjamine4824e82014-12-14 19:44:34 -0500358}
359
David Benjamin2fa83de2015-02-08 01:40:08 -0500360int dtls1_handshake_write(SSL *s) {
David Benjamine4824e82014-12-14 19:44:34 -0500361 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800362}