blob: 43a3f4a80ad9bad405ba8d8e754cfe34c13cdd35 [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>
61
62#if defined(OPENSSL_WINDOWS)
63#include <sys/timeb.h>
64#else
65#include <sys/socket.h>
Adam Langleyded93582014-07-31 15:23:51 -070066#include <sys/time.h>
Adam Langley95c29f32014-06-20 12:00:00 -070067#endif
68
69#include <openssl/err.h>
70#include <openssl/mem.h>
71#include <openssl/obj.h>
72
David Benjamin2ee94aa2015-04-07 22:38:30 -040073#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070074
David Benjamine33b9b02015-01-26 01:27:22 -050075/* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
76 * before starting to decrease the MTU. */
77#define DTLS1_MTU_TIMEOUTS 2
78
79/* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
80 * before failing the DTLS handshake. */
81#define DTLS1_MAX_TIMEOUTS 12
82
David Benjamin377fc312015-01-26 00:22:12 -050083static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock);
Adam Langley71d8a082014-12-13 16:28:18 -080084static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft);
Adam Langley95c29f32014-06-20 12:00:00 -070085
Adam Langley71d8a082014-12-13 16:28:18 -080086int dtls1_new(SSL *s) {
87 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -070088
Adam Langley71d8a082014-12-13 16:28:18 -080089 if (!ssl3_new(s)) {
90 return 0;
91 }
92 d1 = OPENSSL_malloc(sizeof *d1);
93 if (d1 == NULL) {
94 ssl3_free(s);
95 return 0;
96 }
97 memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -070098
Adam Langley71d8a082014-12-13 16:28:18 -080099 d1->buffered_messages = pqueue_new();
100 d1->sent_messages = pqueue_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700101
David Benjamin4417d052015-04-05 04:17:25 -0400102 if (!d1->buffered_messages || !d1->sent_messages) {
Adam Langley71d8a082014-12-13 16:28:18 -0800103 if (d1->buffered_messages) {
104 pqueue_free(d1->buffered_messages);
105 }
106 if (d1->sent_messages) {
107 pqueue_free(d1->sent_messages);
108 }
Adam Langley71d8a082014-12-13 16:28:18 -0800109 OPENSSL_free(d1);
110 ssl3_free(s);
111 return 0;
112 }
Adam Langley95c29f32014-06-20 12:00:00 -0700113
Adam Langley71d8a082014-12-13 16:28:18 -0800114 s->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500115
116 /* Set the version to the highest version for DTLS. This controls the initial
117 * state of |s->enc_method| and what the API reports as the version prior to
118 * negotiation.
119 *
120 * TODO(davidben): This is fragile and confusing. */
121 s->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800122 return 1;
123}
Adam Langley95c29f32014-06-20 12:00:00 -0700124
Adam Langley71d8a082014-12-13 16:28:18 -0800125static void dtls1_clear_queues(SSL *s) {
126 pitem *item = NULL;
127 hm_fragment *frag = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Adam Langley71d8a082014-12-13 16:28:18 -0800129 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
130 frag = (hm_fragment *)item->data;
131 dtls1_hm_fragment_free(frag);
132 pitem_free(item);
133 }
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langley71d8a082014-12-13 16:28:18 -0800135 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
136 frag = (hm_fragment *)item->data;
137 dtls1_hm_fragment_free(frag);
138 pitem_free(item);
139 }
Adam Langley71d8a082014-12-13 16:28:18 -0800140}
Adam Langley95c29f32014-06-20 12:00:00 -0700141
Adam Langley71d8a082014-12-13 16:28:18 -0800142void dtls1_free(SSL *s) {
143 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin62fd1622015-01-11 13:30:01 -0500145 if (s == NULL || s->d1 == NULL) {
146 return;
147 }
148
Adam Langley71d8a082014-12-13 16:28:18 -0800149 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langley71d8a082014-12-13 16:28:18 -0800151 pqueue_free(s->d1->buffered_messages);
152 pqueue_free(s->d1->sent_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley71d8a082014-12-13 16:28:18 -0800154 OPENSSL_free(s->d1);
155 s->d1 = NULL;
156}
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langley71d8a082014-12-13 16:28:18 -0800158long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
159 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700160
Adam Langley71d8a082014-12-13 16:28:18 -0800161 switch (cmd) {
162 case DTLS_CTRL_GET_TIMEOUT:
163 if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
164 ret = 1;
165 }
166 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700167
Adam Langley71d8a082014-12-13 16:28:18 -0800168 case DTLS_CTRL_HANDLE_TIMEOUT:
169 ret = dtls1_handle_timeout(s);
170 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley71d8a082014-12-13 16:28:18 -0800172 default:
173 ret = ssl3_ctrl(s, cmd, larg, parg);
174 break;
175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Adam Langley71d8a082014-12-13 16:28:18 -0800177 return ret;
178}
179
Adam Langley71d8a082014-12-13 16:28:18 -0800180const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
181 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
David Benjamine95d20d2014-12-23 11:16:01 -0500182 /* DTLS does not support stream ciphers. */
183 if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) {
184 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800185 }
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langley71d8a082014-12-13 16:28:18 -0800187 return ciph;
188}
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley71d8a082014-12-13 16:28:18 -0800190void dtls1_start_timer(SSL *s) {
191 /* If timer is not set, initialize duration with 1 second */
192 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
193 s->d1->timeout_duration = 1;
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley71d8a082014-12-13 16:28:18 -0800196 /* Set timeout to current time */
David Benjamin377fc312015-01-26 00:22:12 -0500197 get_current_time(s, &s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langley71d8a082014-12-13 16:28:18 -0800199 /* Add duration to current time */
200 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
201 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
202 &s->d1->next_timeout);
203}
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langley71d8a082014-12-13 16:28:18 -0800205static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
206 OPENSSL_timeval timenow;
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langley71d8a082014-12-13 16:28:18 -0800208 /* If no timeout is set, just return NULL */
209 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
210 return NULL;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
Adam Langley71d8a082014-12-13 16:28:18 -0800213 /* Get current time */
David Benjamin377fc312015-01-26 00:22:12 -0500214 get_current_time(s, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700215
Adam Langley71d8a082014-12-13 16:28:18 -0800216 /* If timer already expired, set remaining time to 0 */
217 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
218 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
219 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
220 memset(timeleft, 0, sizeof(OPENSSL_timeval));
221 return timeleft;
222 }
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langley71d8a082014-12-13 16:28:18 -0800224 /* Calculate time left until timer expires */
225 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
226 timeleft->tv_sec -= timenow.tv_sec;
227 timeleft->tv_usec -= timenow.tv_usec;
228 if (timeleft->tv_usec < 0) {
229 timeleft->tv_sec--;
230 timeleft->tv_usec += 1000000;
231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langley71d8a082014-12-13 16:28:18 -0800233 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
234 * because of small devergences with socket timeouts. */
235 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
236 memset(timeleft, 0, sizeof(OPENSSL_timeval));
237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langley71d8a082014-12-13 16:28:18 -0800239 return timeleft;
240}
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langley71d8a082014-12-13 16:28:18 -0800242int dtls1_is_timer_expired(SSL *s) {
243 OPENSSL_timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langley71d8a082014-12-13 16:28:18 -0800245 /* Get time left until timeout, return false if no timer running */
246 if (dtls1_get_timeout(s, &timeleft) == NULL) {
247 return 0;
248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langley71d8a082014-12-13 16:28:18 -0800250 /* Return false if timer is not expired yet */
251 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
252 return 0;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langley71d8a082014-12-13 16:28:18 -0800255 /* Timer expired, so return true */
256 return 1;
257}
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley71d8a082014-12-13 16:28:18 -0800259void dtls1_double_timeout(SSL *s) {
260 s->d1->timeout_duration *= 2;
261 if (s->d1->timeout_duration > 60) {
262 s->d1->timeout_duration = 60;
263 }
264 dtls1_start_timer(s);
265}
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langley71d8a082014-12-13 16:28:18 -0800267void dtls1_stop_timer(SSL *s) {
268 /* Reset everything */
David Benjamine33b9b02015-01-26 01:27:22 -0500269 s->d1->num_timeouts = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800270 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
271 s->d1->timeout_duration = 1;
272 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
273 &s->d1->next_timeout);
274 /* Clear retransmission buffer */
275 dtls1_clear_record_buffer(s);
276}
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langley71d8a082014-12-13 16:28:18 -0800278int dtls1_check_timeout_num(SSL *s) {
David Benjamine33b9b02015-01-26 01:27:22 -0500279 s->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langley71d8a082014-12-13 16:28:18 -0800281 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamine33b9b02015-01-26 01:27:22 -0500282 if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
David Benjamin7f18b132015-01-11 17:36:21 -0500283 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500284 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
285 NULL);
286 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
287 s->d1->mtu = (unsigned)mtu;
288 }
Adam Langley71d8a082014-12-13 16:28:18 -0800289 }
Adam Langley95c29f32014-06-20 12:00:00 -0700290
David Benjamine33b9b02015-01-26 01:27:22 -0500291 if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800292 /* fail the connection, enough alerts have been sent */
293 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
294 return -1;
295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley71d8a082014-12-13 16:28:18 -0800297 return 0;
298}
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley71d8a082014-12-13 16:28:18 -0800300int dtls1_handle_timeout(SSL *s) {
301 /* if no timer is expired, don't do anything */
302 if (!dtls1_is_timer_expired(s)) {
303 return 0;
304 }
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley71d8a082014-12-13 16:28:18 -0800306 dtls1_double_timeout(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley71d8a082014-12-13 16:28:18 -0800308 if (dtls1_check_timeout_num(s) < 0) {
309 return -1;
310 }
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley71d8a082014-12-13 16:28:18 -0800312 dtls1_start_timer(s);
313 return dtls1_retransmit_buffered_messages(s);
314}
315
David Benjamin377fc312015-01-26 00:22:12 -0500316static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) {
317 if (ssl->ctx->current_time_cb != NULL) {
318 ssl->ctx->current_time_cb(ssl, out_clock);
319 return;
320 }
321
Adam Langleyded93582014-07-31 15:23:51 -0700322#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800323 struct _timeb time;
324 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500325 out_clock->tv_sec = time.time;
326 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700327#else
David Benjamin377fc312015-01-26 00:22:12 -0500328 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700329#endif
330}
331
David Benjamin2fa83de2015-02-08 01:40:08 -0500332int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
David Benjamine4824e82014-12-14 19:44:34 -0500333 uint8_t *message = (uint8_t *)s->init_buf->data;
334 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
335 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
336 uint8_t *p = serialised_header;
337
David Benjamin16d031a2014-12-14 18:52:44 -0500338 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
339 s->d1->next_handshake_write_seq++;
340
341 dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800342 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
343 s->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500344
Adam Langley71d8a082014-12-13 16:28:18 -0800345 /* Buffer the message to handle re-xmits */
346 dtls1_buffer_message(s, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamine4824e82014-12-14 19:44:34 -0500348 /* Add the new message to the handshake hash. Serialize the message
349 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800350 *p++ = msg_hdr->type;
351 l2n3(msg_hdr->msg_len, p);
352 s2n(msg_hdr->seq, p);
353 l2n3(0, p);
354 l2n3(msg_hdr->msg_len, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500355 return ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)) &&
356 ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
David Benjamine4824e82014-12-14 19:44:34 -0500357}
358
David Benjamin2fa83de2015-02-08 01:40:08 -0500359int dtls1_handshake_write(SSL *s) {
David Benjamine4824e82014-12-14 19:44:34 -0500360 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800361}