blob: 7f08b067b9d008903aa61b45f2a0333d7cbb16d2 [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
David Benjamin9e4e01e2015-09-15 01:48:04 -040057#include <openssl/ssl.h>
Adam Langley95c29f32014-06-20 12:00:00 -070058
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
David Benjamin9e4e01e2015-09-15 01:48:04 -040063#include <openssl/err.h>
64#include <openssl/mem.h>
65#include <openssl/obj.h>
66
67#include "internal.h"
68
Adam Langley95c29f32014-06-20 12:00:00 -070069#if defined(OPENSSL_WINDOWS)
70#include <sys/timeb.h>
71#else
72#include <sys/socket.h>
Adam Langleyded93582014-07-31 15:23:51 -070073#include <sys/time.h>
Adam Langley95c29f32014-06-20 12:00:00 -070074#endif
75
Adam Langley95c29f32014-06-20 12:00:00 -070076
David Benjamine33b9b02015-01-26 01:27:22 -050077/* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
78 * before starting to decrease the MTU. */
79#define DTLS1_MTU_TIMEOUTS 2
80
81/* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
82 * before failing the DTLS handshake. */
83#define DTLS1_MAX_TIMEOUTS 12
84
David Benjamin4d2e7ce2015-05-08 13:29:45 -040085static void get_current_time(const SSL *ssl, struct timeval *out_clock);
Adam Langley95c29f32014-06-20 12:00:00 -070086
David Benjamin0d56f882015-12-19 17:05:56 -050087int dtls1_new(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -080088 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -070089
David Benjamin0d56f882015-12-19 17:05:56 -050090 if (!ssl3_new(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -080091 return 0;
92 }
93 d1 = OPENSSL_malloc(sizeof *d1);
94 if (d1 == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -050095 ssl3_free(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -080096 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) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400104 pqueue_free(d1->buffered_messages);
105 pqueue_free(d1->sent_messages);
Adam Langley71d8a082014-12-13 16:28:18 -0800106 OPENSSL_free(d1);
David Benjamin0d56f882015-12-19 17:05:56 -0500107 ssl3_free(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800108 return 0;
109 }
Adam Langley95c29f32014-06-20 12:00:00 -0700110
David Benjamin0d56f882015-12-19 17:05:56 -0500111 ssl->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500112
113 /* Set the version to the highest version for DTLS. This controls the initial
David Benjamin0d56f882015-12-19 17:05:56 -0500114 * state of |ssl->enc_method| and what the API reports as the version prior to
David Benjamin62fd1622015-01-11 13:30:01 -0500115 * negotiation.
116 *
117 * TODO(davidben): This is fragile and confusing. */
David Benjamin0d56f882015-12-19 17:05:56 -0500118 ssl->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800119 return 1;
120}
Adam Langley95c29f32014-06-20 12:00:00 -0700121
David Benjamin0d56f882015-12-19 17:05:56 -0500122static void dtls1_clear_queues(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800123 pitem *item = NULL;
124 hm_fragment *frag = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin0d56f882015-12-19 17:05:56 -0500126 while ((item = pqueue_pop(ssl->d1->buffered_messages)) != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800127 frag = (hm_fragment *)item->data;
128 dtls1_hm_fragment_free(frag);
129 pitem_free(item);
130 }
Adam Langley95c29f32014-06-20 12:00:00 -0700131
David Benjamin0d56f882015-12-19 17:05:56 -0500132 while ((item = pqueue_pop(ssl->d1->sent_messages)) != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800133 frag = (hm_fragment *)item->data;
134 dtls1_hm_fragment_free(frag);
135 pitem_free(item);
136 }
Adam Langley71d8a082014-12-13 16:28:18 -0800137}
Adam Langley95c29f32014-06-20 12:00:00 -0700138
David Benjamin0d56f882015-12-19 17:05:56 -0500139void dtls1_free(SSL *ssl) {
140 ssl3_free(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700141
David Benjamin0d56f882015-12-19 17:05:56 -0500142 if (ssl == NULL || ssl->d1 == NULL) {
David Benjamin62fd1622015-01-11 13:30:01 -0500143 return;
144 }
145
David Benjamin0d56f882015-12-19 17:05:56 -0500146 dtls1_clear_queues(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjamin0d56f882015-12-19 17:05:56 -0500148 pqueue_free(ssl->d1->buffered_messages);
149 pqueue_free(ssl->d1->sent_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjamin0d56f882015-12-19 17:05:56 -0500151 OPENSSL_free(ssl->d1);
152 ssl->d1 = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800153}
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamina1c90a52015-05-30 17:03:14 -0400155int dtls1_supports_cipher(const SSL_CIPHER *cipher) {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700156 /* DTLS does not support stream ciphers. The NULL cipher is rejected because
157 * it's not needed. */
158 return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800159}
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin0d56f882015-12-19 17:05:56 -0500161void dtls1_start_timer(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800162 /* If timer is not set, initialize duration with 1 second */
David Benjamin0d56f882015-12-19 17:05:56 -0500163 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
164 ssl->d1->timeout_duration = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
Adam Langley71d8a082014-12-13 16:28:18 -0800167 /* Set timeout to current time */
David Benjamin0d56f882015-12-19 17:05:56 -0500168 get_current_time(ssl, &ssl->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley71d8a082014-12-13 16:28:18 -0800170 /* Add duration to current time */
David Benjamin0d56f882015-12-19 17:05:56 -0500171 ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration;
172 BIO_ctrl(SSL_get_rbio(ssl), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
173 &ssl->d1->next_timeout);
Adam Langley71d8a082014-12-13 16:28:18 -0800174}
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400176int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
David Benjamin8c249802015-05-05 09:44:18 -0400177 if (!SSL_IS_DTLS(ssl)) {
178 return 0;
179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langley71d8a082014-12-13 16:28:18 -0800181 /* If no timeout is set, just return NULL */
David Benjamin8c249802015-05-05 09:44:18 -0400182 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
183 return 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langley71d8a082014-12-13 16:28:18 -0800186 /* Get current time */
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400187 struct timeval timenow;
David Benjamin8c249802015-05-05 09:44:18 -0400188 get_current_time(ssl, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley71d8a082014-12-13 16:28:18 -0800190 /* If timer already expired, set remaining time to 0 */
David Benjamin8c249802015-05-05 09:44:18 -0400191 if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
192 (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
193 ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400194 memset(out, 0, sizeof(struct timeval));
David Benjamin8c249802015-05-05 09:44:18 -0400195 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley71d8a082014-12-13 16:28:18 -0800198 /* Calculate time left until timer expires */
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400199 memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval));
David Benjamin8c249802015-05-05 09:44:18 -0400200 out->tv_sec -= timenow.tv_sec;
201 out->tv_usec -= timenow.tv_usec;
202 if (out->tv_usec < 0) {
203 out->tv_sec--;
204 out->tv_usec += 1000000;
Adam Langley71d8a082014-12-13 16:28:18 -0800205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
Adam Langley71d8a082014-12-13 16:28:18 -0800207 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
208 * because of small devergences with socket timeouts. */
David Benjamin8c249802015-05-05 09:44:18 -0400209 if (out->tv_sec == 0 && out->tv_usec < 15000) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400210 memset(out, 0, sizeof(struct timeval));
Adam Langley71d8a082014-12-13 16:28:18 -0800211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin8c249802015-05-05 09:44:18 -0400213 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800214}
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin0d56f882015-12-19 17:05:56 -0500216int dtls1_is_timer_expired(SSL *ssl) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400217 struct timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langley71d8a082014-12-13 16:28:18 -0800219 /* Get time left until timeout, return false if no timer running */
David Benjamin0d56f882015-12-19 17:05:56 -0500220 if (!DTLSv1_get_timeout(ssl, &timeleft)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800221 return 0;
222 }
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langley71d8a082014-12-13 16:28:18 -0800224 /* Return false if timer is not expired yet */
225 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
226 return 0;
227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langley71d8a082014-12-13 16:28:18 -0800229 /* Timer expired, so return true */
230 return 1;
231}
Adam Langley95c29f32014-06-20 12:00:00 -0700232
David Benjamin0d56f882015-12-19 17:05:56 -0500233void dtls1_double_timeout(SSL *ssl) {
234 ssl->d1->timeout_duration *= 2;
235 if (ssl->d1->timeout_duration > 60) {
236 ssl->d1->timeout_duration = 60;
Adam Langley71d8a082014-12-13 16:28:18 -0800237 }
David Benjamin0d56f882015-12-19 17:05:56 -0500238 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800239}
Adam Langley95c29f32014-06-20 12:00:00 -0700240
David Benjamin0d56f882015-12-19 17:05:56 -0500241void dtls1_stop_timer(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800242 /* Reset everything */
David Benjamin0d56f882015-12-19 17:05:56 -0500243 ssl->d1->num_timeouts = 0;
244 memset(&ssl->d1->next_timeout, 0, sizeof(struct timeval));
245 ssl->d1->timeout_duration = 1;
246 BIO_ctrl(SSL_get_rbio(ssl), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
247 &ssl->d1->next_timeout);
Adam Langley71d8a082014-12-13 16:28:18 -0800248 /* Clear retransmission buffer */
David Benjamin0d56f882015-12-19 17:05:56 -0500249 dtls1_clear_record_buffer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800250}
Adam Langley95c29f32014-06-20 12:00:00 -0700251
David Benjamin0d56f882015-12-19 17:05:56 -0500252int dtls1_check_timeout_num(SSL *ssl) {
253 ssl->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langley71d8a082014-12-13 16:28:18 -0800255 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamin0d56f882015-12-19 17:05:56 -0500256 if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
257 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
258 long mtu = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
David Benjamin80cee912015-01-11 19:36:58 -0500259 NULL);
260 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
David Benjamin0d56f882015-12-19 17:05:56 -0500261 ssl->d1->mtu = (unsigned)mtu;
David Benjamin80cee912015-01-11 19:36:58 -0500262 }
Adam Langley71d8a082014-12-13 16:28:18 -0800263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
David Benjamin0d56f882015-12-19 17:05:56 -0500265 if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800266 /* fail the connection, enough alerts have been sent */
David Benjamin3570d732015-06-29 00:28:17 -0400267 OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
Adam Langley71d8a082014-12-13 16:28:18 -0800268 return -1;
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langley71d8a082014-12-13 16:28:18 -0800271 return 0;
272}
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamin8c249802015-05-05 09:44:18 -0400274int DTLSv1_handle_timeout(SSL *ssl) {
275 if (!SSL_IS_DTLS(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800276 return -1;
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
David Benjamin8c249802015-05-05 09:44:18 -0400279 /* if no timer is expired, don't do anything */
280 if (!dtls1_is_timer_expired(ssl)) {
281 return 0;
282 }
283
284 dtls1_double_timeout(ssl);
285
286 if (dtls1_check_timeout_num(ssl) < 0) {
287 return -1;
288 }
289
290 dtls1_start_timer(ssl);
291 return dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800292}
293
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400294static void get_current_time(const SSL *ssl, struct timeval *out_clock) {
David Benjamin377fc312015-01-26 00:22:12 -0500295 if (ssl->ctx->current_time_cb != NULL) {
296 ssl->ctx->current_time_cb(ssl, out_clock);
297 return;
298 }
299
Adam Langleyded93582014-07-31 15:23:51 -0700300#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800301 struct _timeb time;
302 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500303 out_clock->tv_sec = time.time;
304 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700305#else
David Benjamin377fc312015-01-26 00:22:12 -0500306 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700307#endif
308}
309
David Benjamin0d56f882015-12-19 17:05:56 -0500310int dtls1_set_handshake_header(SSL *ssl, int htype, unsigned long len) {
311 uint8_t *message = (uint8_t *)ssl->init_buf->data;
312 const struct hm_header_st *msg_hdr = &ssl->d1->w_msg_hdr;
David Benjamine4824e82014-12-14 19:44:34 -0500313 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
314 uint8_t *p = serialised_header;
315
David Benjamin0d56f882015-12-19 17:05:56 -0500316 ssl->d1->handshake_write_seq = ssl->d1->next_handshake_write_seq;
317 ssl->d1->next_handshake_write_seq++;
David Benjamin16d031a2014-12-14 18:52:44 -0500318
David Benjamin0d56f882015-12-19 17:05:56 -0500319 dtls1_set_message_header(ssl, htype, len, ssl->d1->handshake_write_seq, 0,
320 len);
321 ssl->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
322 ssl->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500323
Adam Langley71d8a082014-12-13 16:28:18 -0800324 /* Buffer the message to handle re-xmits */
David Benjamin0d56f882015-12-19 17:05:56 -0500325 dtls1_buffer_message(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamine4824e82014-12-14 19:44:34 -0500327 /* Add the new message to the handshake hash. Serialize the message
328 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800329 *p++ = msg_hdr->type;
330 l2n3(msg_hdr->msg_len, p);
331 s2n(msg_hdr->seq, p);
332 l2n3(0, p);
333 l2n3(msg_hdr->msg_len, p);
David Benjamin0d56f882015-12-19 17:05:56 -0500334 return ssl3_update_handshake_hash(ssl, serialised_header,
David Benjamin9550c3a2015-08-05 08:50:34 -0400335 sizeof(serialised_header)) &&
David Benjamin0d56f882015-12-19 17:05:56 -0500336 ssl3_update_handshake_hash(ssl, message + DTLS1_HM_HEADER_LENGTH, len);
David Benjamine4824e82014-12-14 19:44:34 -0500337}
338
David Benjamin0d56f882015-12-19 17:05:56 -0500339int dtls1_handshake_write(SSL *ssl) {
340 return dtls1_do_handshake_write(ssl, dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800341}