blob: 0074855b557c63e40edc79c09ee6ef1825e59230 [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
David Benjamin17cf2cb2016-12-13 01:07:13 -050067#include "../crypto/internal.h"
David Benjamin9e4e01e2015-09-15 01:48:04 -040068#include "internal.h"
69
Adam Langley95c29f32014-06-20 12:00:00 -070070
David Benjamin17cf2cb2016-12-13 01:07:13 -050071
David Benjamine33b9b02015-01-26 01:27:22 -050072/* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
73 * before starting to decrease the MTU. */
74#define DTLS1_MTU_TIMEOUTS 2
75
76/* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
77 * before failing the DTLS handshake. */
78#define DTLS1_MAX_TIMEOUTS 12
79
David Benjamin0d56f882015-12-19 17:05:56 -050080int dtls1_new(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -050081 if (!ssl3_new(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -080082 return 0;
83 }
David Benjamine8703a32017-07-09 16:17:55 -040084 DTLS1_STATE *d1 = (DTLS1_STATE *)OPENSSL_malloc(sizeof *d1);
Adam Langley71d8a082014-12-13 16:28:18 -080085 if (d1 == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -050086 ssl3_free(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -080087 return 0;
88 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050089 OPENSSL_memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -070090
David Benjamin0d56f882015-12-19 17:05:56 -050091 ssl->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -050092
David Benjaminb83003e2015-12-30 17:39:26 -050093 /* Set the version to the highest supported version.
David Benjamin62fd1622015-01-11 13:30:01 -050094 *
David Benjaminb83003e2015-12-30 17:39:26 -050095 * TODO(davidben): Move this field into |s3|, have it store the normalized
96 * protocol version, and implement this pre-negotiation quirk in |SSL_version|
97 * at the API boundary rather than in internal state. */
David Benjamin0d56f882015-12-19 17:05:56 -050098 ssl->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -080099 return 1;
100}
Adam Langley95c29f32014-06-20 12:00:00 -0700101
David Benjamin0d56f882015-12-19 17:05:56 -0500102void dtls1_free(SSL *ssl) {
103 ssl3_free(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700104
David Benjamin0d56f882015-12-19 17:05:56 -0500105 if (ssl == NULL || ssl->d1 == NULL) {
David Benjamin62fd1622015-01-11 13:30:01 -0500106 return;
107 }
108
David Benjaminec847ce2016-06-17 19:30:47 -0400109 dtls_clear_incoming_messages(ssl);
David Benjamin29a83c52016-06-17 19:12:54 -0400110 dtls_clear_outgoing_messages(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700111
David Benjamin0d56f882015-12-19 17:05:56 -0500112 OPENSSL_free(ssl->d1);
113 ssl->d1 = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800114}
Adam Langley95c29f32014-06-20 12:00:00 -0700115
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700116void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) {
117 ssl->initial_timeout_duration_ms = duration_ms;
118}
119
David Benjamin0d56f882015-12-19 17:05:56 -0500120void dtls1_start_timer(SSL *ssl) {
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700121 /* If timer is not set, initialize duration (by default, 1 second) */
David Benjamin0d56f882015-12-19 17:05:56 -0500122 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700123 ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
Adam Langley71d8a082014-12-13 16:28:18 -0800124 }
Adam Langley95c29f32014-06-20 12:00:00 -0700125
Adam Langley71d8a082014-12-13 16:28:18 -0800126 /* Set timeout to current time */
David Benjamin721e8b72016-08-03 13:13:17 -0400127 ssl_get_current_time(ssl, &ssl->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Adam Langley71d8a082014-12-13 16:28:18 -0800129 /* Add duration to current time */
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700130 ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
131 ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000;
132 if (ssl->d1->next_timeout.tv_usec >= 1000000) {
133 ssl->d1->next_timeout.tv_sec++;
134 ssl->d1->next_timeout.tv_usec -= 1000000;
135 }
Adam Langley71d8a082014-12-13 16:28:18 -0800136}
Adam Langley95c29f32014-06-20 12:00:00 -0700137
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400138int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
David Benjamince079fd2016-08-02 16:22:34 -0400139 if (!SSL_is_dtls(ssl)) {
David Benjamin8c249802015-05-05 09:44:18 -0400140 return 0;
141 }
Adam Langley95c29f32014-06-20 12:00:00 -0700142
Adam Langley71d8a082014-12-13 16:28:18 -0800143 /* If no timeout is set, just return NULL */
David Benjamin8c249802015-05-05 09:44:18 -0400144 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
145 return 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800146 }
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjaminad8f5e12017-02-20 17:00:20 -0500148 struct OPENSSL_timeval timenow;
David Benjamin721e8b72016-08-03 13:13:17 -0400149 ssl_get_current_time(ssl, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langley71d8a082014-12-13 16:28:18 -0800151 /* If timer already expired, set remaining time to 0 */
David Benjamin8c249802015-05-05 09:44:18 -0400152 if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
153 (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
154 ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
David Benjaminad8f5e12017-02-20 17:00:20 -0500155 OPENSSL_memset(out, 0, sizeof(*out));
David Benjamin8c249802015-05-05 09:44:18 -0400156 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800157 }
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langley71d8a082014-12-13 16:28:18 -0800159 /* Calculate time left until timer expires */
David Benjaminad8f5e12017-02-20 17:00:20 -0500160 struct OPENSSL_timeval ret;
161 OPENSSL_memcpy(&ret, &ssl->d1->next_timeout, sizeof(ret));
162 ret.tv_sec -= timenow.tv_sec;
163 if (ret.tv_usec >= timenow.tv_usec) {
164 ret.tv_usec -= timenow.tv_usec;
165 } else {
166 ret.tv_usec = 1000000 + ret.tv_usec - timenow.tv_usec;
167 ret.tv_sec--;
Adam Langley71d8a082014-12-13 16:28:18 -0800168 }
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley71d8a082014-12-13 16:28:18 -0800170 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
David Benjaminad8f5e12017-02-20 17:00:20 -0500171 * because of small divergences with socket timeouts. */
172 if (ret.tv_sec == 0 && ret.tv_usec < 15000) {
173 OPENSSL_memset(&ret, 0, sizeof(ret));
Adam Langley71d8a082014-12-13 16:28:18 -0800174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjaminad8f5e12017-02-20 17:00:20 -0500176 /* Clamp the result in case of overflow. */
177 if (ret.tv_sec > INT_MAX) {
178 assert(0);
179 out->tv_sec = INT_MAX;
180 } else {
181 out->tv_sec = ret.tv_sec;
182 }
183
184 out->tv_usec = ret.tv_usec;
David Benjamin8c249802015-05-05 09:44:18 -0400185 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800186}
Adam Langley95c29f32014-06-20 12:00:00 -0700187
David Benjamin0d56f882015-12-19 17:05:56 -0500188int dtls1_is_timer_expired(SSL *ssl) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400189 struct timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley71d8a082014-12-13 16:28:18 -0800191 /* Get time left until timeout, return false if no timer running */
David Benjamin0d56f882015-12-19 17:05:56 -0500192 if (!DTLSv1_get_timeout(ssl, &timeleft)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800193 return 0;
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley71d8a082014-12-13 16:28:18 -0800196 /* Return false if timer is not expired yet */
197 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
198 return 0;
199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langley71d8a082014-12-13 16:28:18 -0800201 /* Timer expired, so return true */
202 return 1;
203}
Adam Langley95c29f32014-06-20 12:00:00 -0700204
David Benjamin0d56f882015-12-19 17:05:56 -0500205void dtls1_double_timeout(SSL *ssl) {
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700206 ssl->d1->timeout_duration_ms *= 2;
207 if (ssl->d1->timeout_duration_ms > 60000) {
208 ssl->d1->timeout_duration_ms = 60000;
Adam Langley71d8a082014-12-13 16:28:18 -0800209 }
David Benjamin0d56f882015-12-19 17:05:56 -0500210 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800211}
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin0d56f882015-12-19 17:05:56 -0500213void dtls1_stop_timer(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800214 /* Reset everything */
David Benjamin0d56f882015-12-19 17:05:56 -0500215 ssl->d1->num_timeouts = 0;
David Benjaminad8f5e12017-02-20 17:00:20 -0500216 OPENSSL_memset(&ssl->d1->next_timeout, 0, sizeof(ssl->d1->next_timeout));
Taylor Brandstetter376a0fe2016-05-10 19:30:28 -0700217 ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
David Benjamin11c82892017-02-23 20:40:31 -0500218
Adam Langley71d8a082014-12-13 16:28:18 -0800219 /* Clear retransmission buffer */
David Benjamin29a83c52016-06-17 19:12:54 -0400220 dtls_clear_outgoing_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800221}
Adam Langley95c29f32014-06-20 12:00:00 -0700222
David Benjamin0d56f882015-12-19 17:05:56 -0500223int dtls1_check_timeout_num(SSL *ssl) {
224 ssl->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langley71d8a082014-12-13 16:28:18 -0800226 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamin0d56f882015-12-19 17:05:56 -0500227 if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
228 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin2f871122016-05-20 14:27:17 -0400229 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
David Benjamin80cee912015-01-11 19:36:58 -0500230 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
David Benjamin0d56f882015-12-19 17:05:56 -0500231 ssl->d1->mtu = (unsigned)mtu;
David Benjamin80cee912015-01-11 19:36:58 -0500232 }
Adam Langley71d8a082014-12-13 16:28:18 -0800233 }
Adam Langley95c29f32014-06-20 12:00:00 -0700234
David Benjamin0d56f882015-12-19 17:05:56 -0500235 if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800236 /* fail the connection, enough alerts have been sent */
David Benjamin3570d732015-06-29 00:28:17 -0400237 OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
Adam Langley71d8a082014-12-13 16:28:18 -0800238 return -1;
239 }
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley71d8a082014-12-13 16:28:18 -0800241 return 0;
242}
Adam Langley95c29f32014-06-20 12:00:00 -0700243
David Benjamin8c249802015-05-05 09:44:18 -0400244int DTLSv1_handle_timeout(SSL *ssl) {
David Benjamin2be4aa72017-01-02 07:56:51 -0500245 ssl_reset_error_state(ssl);
David Benjamin15c14882016-03-14 14:25:46 -0400246
David Benjamince079fd2016-08-02 16:22:34 -0400247 if (!SSL_is_dtls(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800248 return -1;
249 }
Adam Langley95c29f32014-06-20 12:00:00 -0700250
David Benjamin8c249802015-05-05 09:44:18 -0400251 /* if no timer is expired, don't do anything */
252 if (!dtls1_is_timer_expired(ssl)) {
253 return 0;
254 }
255
256 dtls1_double_timeout(ssl);
257
258 if (dtls1_check_timeout_num(ssl) < 0) {
259 return -1;
260 }
261
262 dtls1_start_timer(ssl);
David Benjaminaad50db2016-06-23 17:49:12 -0400263 return dtls1_retransmit_outgoing_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800264}