blob: e0853cff67973b747c3cdfdbf941a003d37722f2 [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 Benjaminb5eb1952016-06-17 18:44:38 -040059#include <assert.h>
David Benjamin80cee912015-01-11 19:36:58 -050060#include <limits.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>
David Benjamin98193672016-03-25 18:07:11 -040065#include <openssl/nid.h>
David Benjamin9e4e01e2015-09-15 01:48:04 -040066
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
David Benjamin0d56f882015-12-19 17:05:56 -0500100 ssl->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500101
David Benjaminb83003e2015-12-30 17:39:26 -0500102 /* Set the version to the highest supported version.
David Benjamin62fd1622015-01-11 13:30:01 -0500103 *
David Benjaminb83003e2015-12-30 17:39:26 -0500104 * TODO(davidben): Move this field into |s3|, have it store the normalized
105 * protocol version, and implement this pre-negotiation quirk in |SSL_version|
106 * at the API boundary rather than in internal state. */
David Benjamin0d56f882015-12-19 17:05:56 -0500107 ssl->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800108 return 1;
109}
Adam Langley95c29f32014-06-20 12:00:00 -0700110
David Benjamin0d56f882015-12-19 17:05:56 -0500111void dtls1_free(SSL *ssl) {
112 ssl3_free(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin0d56f882015-12-19 17:05:56 -0500114 if (ssl == NULL || ssl->d1 == NULL) {
David Benjamin62fd1622015-01-11 13:30:01 -0500115 return;
116 }
117
David Benjaminec847ce2016-06-17 19:30:47 -0400118 dtls_clear_incoming_messages(ssl);
David Benjamin29a83c52016-06-17 19:12:54 -0400119 dtls_clear_outgoing_messages(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700120
David Benjamin0d56f882015-12-19 17:05:56 -0500121 OPENSSL_free(ssl->d1);
122 ssl->d1 = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800123}
Adam Langley95c29f32014-06-20 12:00:00 -0700124
David Benjamina1c90a52015-05-30 17:03:14 -0400125int dtls1_supports_cipher(const SSL_CIPHER *cipher) {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700126 /* DTLS does not support stream ciphers. The NULL cipher is rejected because
127 * it's not needed. */
128 return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800129}
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700131void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) {
132 ssl->initial_timeout_duration_ms = duration_ms;
133}
134
David Benjamin0d56f882015-12-19 17:05:56 -0500135void dtls1_start_timer(SSL *ssl) {
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700136 /* If timer is not set, initialize duration (by default, 1 second) */
David Benjamin0d56f882015-12-19 17:05:56 -0500137 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700138 ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
Adam Langley71d8a082014-12-13 16:28:18 -0800139 }
Adam Langley95c29f32014-06-20 12:00:00 -0700140
Adam Langley71d8a082014-12-13 16:28:18 -0800141 /* Set timeout to current time */
David Benjamin0d56f882015-12-19 17:05:56 -0500142 get_current_time(ssl, &ssl->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700143
Adam Langley71d8a082014-12-13 16:28:18 -0800144 /* Add duration to current time */
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700145 ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
146 ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000;
147 if (ssl->d1->next_timeout.tv_usec >= 1000000) {
148 ssl->d1->next_timeout.tv_sec++;
149 ssl->d1->next_timeout.tv_usec -= 1000000;
150 }
David Benjamin2f871122016-05-20 14:27:17 -0400151 BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
David Benjamin0d56f882015-12-19 17:05:56 -0500152 &ssl->d1->next_timeout);
Adam Langley71d8a082014-12-13 16:28:18 -0800153}
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400155int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
David Benjamin8c249802015-05-05 09:44:18 -0400156 if (!SSL_IS_DTLS(ssl)) {
157 return 0;
158 }
Adam Langley95c29f32014-06-20 12:00:00 -0700159
Adam Langley71d8a082014-12-13 16:28:18 -0800160 /* If no timeout is set, just return NULL */
David Benjamin8c249802015-05-05 09:44:18 -0400161 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
162 return 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800163 }
Adam Langley95c29f32014-06-20 12:00:00 -0700164
Adam Langley71d8a082014-12-13 16:28:18 -0800165 /* Get current time */
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400166 struct timeval timenow;
David Benjamin8c249802015-05-05 09:44:18 -0400167 get_current_time(ssl, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langley71d8a082014-12-13 16:28:18 -0800169 /* If timer already expired, set remaining time to 0 */
David Benjamin8c249802015-05-05 09:44:18 -0400170 if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
171 (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
172 ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400173 memset(out, 0, sizeof(struct timeval));
David Benjamin8c249802015-05-05 09:44:18 -0400174 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Adam Langley71d8a082014-12-13 16:28:18 -0800177 /* Calculate time left until timer expires */
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400178 memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval));
David Benjamin8c249802015-05-05 09:44:18 -0400179 out->tv_sec -= timenow.tv_sec;
180 out->tv_usec -= timenow.tv_usec;
181 if (out->tv_usec < 0) {
182 out->tv_sec--;
183 out->tv_usec += 1000000;
Adam Langley71d8a082014-12-13 16:28:18 -0800184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langley71d8a082014-12-13 16:28:18 -0800186 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
187 * because of small devergences with socket timeouts. */
David Benjamin8c249802015-05-05 09:44:18 -0400188 if (out->tv_sec == 0 && out->tv_usec < 15000) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400189 memset(out, 0, sizeof(struct timeval));
Adam Langley71d8a082014-12-13 16:28:18 -0800190 }
Adam Langley95c29f32014-06-20 12:00:00 -0700191
David Benjamin8c249802015-05-05 09:44:18 -0400192 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800193}
Adam Langley95c29f32014-06-20 12:00:00 -0700194
David Benjamin0d56f882015-12-19 17:05:56 -0500195int dtls1_is_timer_expired(SSL *ssl) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400196 struct timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley71d8a082014-12-13 16:28:18 -0800198 /* Get time left until timeout, return false if no timer running */
David Benjamin0d56f882015-12-19 17:05:56 -0500199 if (!DTLSv1_get_timeout(ssl, &timeleft)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800200 return 0;
201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langley71d8a082014-12-13 16:28:18 -0800203 /* Return false if timer is not expired yet */
204 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
205 return 0;
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langley71d8a082014-12-13 16:28:18 -0800208 /* Timer expired, so return true */
209 return 1;
210}
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin0d56f882015-12-19 17:05:56 -0500212void dtls1_double_timeout(SSL *ssl) {
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700213 ssl->d1->timeout_duration_ms *= 2;
214 if (ssl->d1->timeout_duration_ms > 60000) {
215 ssl->d1->timeout_duration_ms = 60000;
Adam Langley71d8a082014-12-13 16:28:18 -0800216 }
David Benjamin0d56f882015-12-19 17:05:56 -0500217 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800218}
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjamin0d56f882015-12-19 17:05:56 -0500220void dtls1_stop_timer(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800221 /* Reset everything */
David Benjamin0d56f882015-12-19 17:05:56 -0500222 ssl->d1->num_timeouts = 0;
223 memset(&ssl->d1->next_timeout, 0, sizeof(struct timeval));
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700224 ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
David Benjamin2f871122016-05-20 14:27:17 -0400225 BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
David Benjamin0d56f882015-12-19 17:05:56 -0500226 &ssl->d1->next_timeout);
Adam Langley71d8a082014-12-13 16:28:18 -0800227 /* Clear retransmission buffer */
David Benjamin29a83c52016-06-17 19:12:54 -0400228 dtls_clear_outgoing_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800229}
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjamin0d56f882015-12-19 17:05:56 -0500231int dtls1_check_timeout_num(SSL *ssl) {
232 ssl->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley71d8a082014-12-13 16:28:18 -0800234 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamin0d56f882015-12-19 17:05:56 -0500235 if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
236 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin2f871122016-05-20 14:27:17 -0400237 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
David Benjamin80cee912015-01-11 19:36:58 -0500238 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
David Benjamin0d56f882015-12-19 17:05:56 -0500239 ssl->d1->mtu = (unsigned)mtu;
David Benjamin80cee912015-01-11 19:36:58 -0500240 }
Adam Langley71d8a082014-12-13 16:28:18 -0800241 }
Adam Langley95c29f32014-06-20 12:00:00 -0700242
David Benjamin0d56f882015-12-19 17:05:56 -0500243 if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800244 /* fail the connection, enough alerts have been sent */
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
Adam Langley71d8a082014-12-13 16:28:18 -0800246 return -1;
247 }
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langley71d8a082014-12-13 16:28:18 -0800249 return 0;
250}
Adam Langley95c29f32014-06-20 12:00:00 -0700251
David Benjamin8c249802015-05-05 09:44:18 -0400252int DTLSv1_handle_timeout(SSL *ssl) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500253 ssl->rwstate = SSL_NOTHING;
David Benjamin15c14882016-03-14 14:25:46 -0400254 /* Functions which use SSL_get_error must clear the error queue on entry. */
255 ERR_clear_error();
256
David Benjamin8c249802015-05-05 09:44:18 -0400257 if (!SSL_IS_DTLS(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800258 return -1;
259 }
Adam Langley95c29f32014-06-20 12:00:00 -0700260
David Benjamin8c249802015-05-05 09:44:18 -0400261 /* if no timer is expired, don't do anything */
262 if (!dtls1_is_timer_expired(ssl)) {
263 return 0;
264 }
265
266 dtls1_double_timeout(ssl);
267
268 if (dtls1_check_timeout_num(ssl) < 0) {
269 return -1;
270 }
271
272 dtls1_start_timer(ssl);
273 return dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800274}
275
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400276static void get_current_time(const SSL *ssl, struct timeval *out_clock) {
David Benjamin377fc312015-01-26 00:22:12 -0500277 if (ssl->ctx->current_time_cb != NULL) {
278 ssl->ctx->current_time_cb(ssl, out_clock);
279 return;
280 }
281
Adam Langleyded93582014-07-31 15:23:51 -0700282#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800283 struct _timeb time;
284 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500285 out_clock->tv_sec = time.time;
286 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700287#else
David Benjamin377fc312015-01-26 00:22:12 -0500288 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700289#endif
290}
291
David Benjaminaa7734b2016-06-07 16:40:46 -0400292void dtls1_expect_flight(SSL *ssl) {
293 dtls1_start_timer(ssl);
294}
295
296void dtls1_received_flight(SSL *ssl) {
297 dtls1_stop_timer(ssl);
298}