blob: db8a536bb5c705d1462e1f78305d9e298e730fd6 [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 Benjamin4d2e7ce2015-05-08 13:29:45 -040084static void get_current_time(const SSL *ssl, struct timeval *out_clock);
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) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400103 pqueue_free(d1->buffered_messages);
104 pqueue_free(d1->sent_messages);
Adam Langley71d8a082014-12-13 16:28:18 -0800105 OPENSSL_free(d1);
106 ssl3_free(s);
107 return 0;
108 }
Adam Langley95c29f32014-06-20 12:00:00 -0700109
Adam Langley71d8a082014-12-13 16:28:18 -0800110 s->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500111
112 /* Set the version to the highest version for DTLS. This controls the initial
113 * state of |s->enc_method| and what the API reports as the version prior to
114 * negotiation.
115 *
116 * TODO(davidben): This is fragile and confusing. */
117 s->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800118 return 1;
119}
Adam Langley95c29f32014-06-20 12:00:00 -0700120
Adam Langley71d8a082014-12-13 16:28:18 -0800121static void dtls1_clear_queues(SSL *s) {
122 pitem *item = NULL;
123 hm_fragment *frag = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700124
Adam Langley71d8a082014-12-13 16:28:18 -0800125 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
126 frag = (hm_fragment *)item->data;
127 dtls1_hm_fragment_free(frag);
128 pitem_free(item);
129 }
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley71d8a082014-12-13 16:28:18 -0800131 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
132 frag = (hm_fragment *)item->data;
133 dtls1_hm_fragment_free(frag);
134 pitem_free(item);
135 }
Adam Langley71d8a082014-12-13 16:28:18 -0800136}
Adam Langley95c29f32014-06-20 12:00:00 -0700137
Adam Langley71d8a082014-12-13 16:28:18 -0800138void dtls1_free(SSL *s) {
139 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700140
David Benjamin62fd1622015-01-11 13:30:01 -0500141 if (s == NULL || s->d1 == NULL) {
142 return;
143 }
144
Adam Langley71d8a082014-12-13 16:28:18 -0800145 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700146
Adam Langley71d8a082014-12-13 16:28:18 -0800147 pqueue_free(s->d1->buffered_messages);
148 pqueue_free(s->d1->sent_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley71d8a082014-12-13 16:28:18 -0800150 OPENSSL_free(s->d1);
151 s->d1 = NULL;
152}
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamina1c90a52015-05-30 17:03:14 -0400154int dtls1_supports_cipher(const SSL_CIPHER *cipher) {
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700155 /* DTLS does not support stream ciphers. The NULL cipher is rejected because
156 * it's not needed. */
157 return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800158}
Adam Langley95c29f32014-06-20 12:00:00 -0700159
Adam Langley71d8a082014-12-13 16:28:18 -0800160void dtls1_start_timer(SSL *s) {
161 /* If timer is not set, initialize duration with 1 second */
162 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
163 s->d1->timeout_duration = 1;
164 }
Adam Langley95c29f32014-06-20 12:00:00 -0700165
Adam Langley71d8a082014-12-13 16:28:18 -0800166 /* Set timeout to current time */
David Benjamin377fc312015-01-26 00:22:12 -0500167 get_current_time(s, &s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langley71d8a082014-12-13 16:28:18 -0800169 /* Add duration to current time */
170 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
171 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
172 &s->d1->next_timeout);
173}
Adam Langley95c29f32014-06-20 12:00:00 -0700174
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400175int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
David Benjamin8c249802015-05-05 09:44:18 -0400176 if (!SSL_IS_DTLS(ssl)) {
177 return 0;
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langley71d8a082014-12-13 16:28:18 -0800180 /* If no timeout is set, just return NULL */
David Benjamin8c249802015-05-05 09:44:18 -0400181 if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
182 return 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800183 }
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langley71d8a082014-12-13 16:28:18 -0800185 /* Get current time */
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400186 struct timeval timenow;
David Benjamin8c249802015-05-05 09:44:18 -0400187 get_current_time(ssl, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langley71d8a082014-12-13 16:28:18 -0800189 /* If timer already expired, set remaining time to 0 */
David Benjamin8c249802015-05-05 09:44:18 -0400190 if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
191 (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
192 ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400193 memset(out, 0, sizeof(struct timeval));
David Benjamin8c249802015-05-05 09:44:18 -0400194 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800195 }
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langley71d8a082014-12-13 16:28:18 -0800197 /* Calculate time left until timer expires */
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400198 memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval));
David Benjamin8c249802015-05-05 09:44:18 -0400199 out->tv_sec -= timenow.tv_sec;
200 out->tv_usec -= timenow.tv_usec;
201 if (out->tv_usec < 0) {
202 out->tv_sec--;
203 out->tv_usec += 1000000;
Adam Langley71d8a082014-12-13 16:28:18 -0800204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley71d8a082014-12-13 16:28:18 -0800206 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
207 * because of small devergences with socket timeouts. */
David Benjamin8c249802015-05-05 09:44:18 -0400208 if (out->tv_sec == 0 && out->tv_usec < 15000) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400209 memset(out, 0, sizeof(struct timeval));
Adam Langley71d8a082014-12-13 16:28:18 -0800210 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin8c249802015-05-05 09:44:18 -0400212 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800213}
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley71d8a082014-12-13 16:28:18 -0800215int dtls1_is_timer_expired(SSL *s) {
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400216 struct timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley71d8a082014-12-13 16:28:18 -0800218 /* Get time left until timeout, return false if no timer running */
David Benjamin8c249802015-05-05 09:44:18 -0400219 if (!DTLSv1_get_timeout(s, &timeleft)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800220 return 0;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley71d8a082014-12-13 16:28:18 -0800223 /* Return false if timer is not expired yet */
224 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
225 return 0;
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
Adam Langley71d8a082014-12-13 16:28:18 -0800228 /* Timer expired, so return true */
229 return 1;
230}
Adam Langley95c29f32014-06-20 12:00:00 -0700231
Adam Langley71d8a082014-12-13 16:28:18 -0800232void dtls1_double_timeout(SSL *s) {
233 s->d1->timeout_duration *= 2;
234 if (s->d1->timeout_duration > 60) {
235 s->d1->timeout_duration = 60;
236 }
237 dtls1_start_timer(s);
238}
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley71d8a082014-12-13 16:28:18 -0800240void dtls1_stop_timer(SSL *s) {
241 /* Reset everything */
David Benjamine33b9b02015-01-26 01:27:22 -0500242 s->d1->num_timeouts = 0;
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400243 memset(&s->d1->next_timeout, 0, sizeof(struct timeval));
Adam Langley71d8a082014-12-13 16:28:18 -0800244 s->d1->timeout_duration = 1;
245 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
246 &s->d1->next_timeout);
247 /* Clear retransmission buffer */
248 dtls1_clear_record_buffer(s);
249}
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langley71d8a082014-12-13 16:28:18 -0800251int dtls1_check_timeout_num(SSL *s) {
David Benjamine33b9b02015-01-26 01:27:22 -0500252 s->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley71d8a082014-12-13 16:28:18 -0800254 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamine33b9b02015-01-26 01:27:22 -0500255 if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
David Benjamin7f18b132015-01-11 17:36:21 -0500256 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500257 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
258 NULL);
259 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
260 s->d1->mtu = (unsigned)mtu;
261 }
Adam Langley71d8a082014-12-13 16:28:18 -0800262 }
Adam Langley95c29f32014-06-20 12:00:00 -0700263
David Benjamine33b9b02015-01-26 01:27:22 -0500264 if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800265 /* fail the connection, enough alerts have been sent */
David Benjamin3570d732015-06-29 00:28:17 -0400266 OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED);
Adam Langley71d8a082014-12-13 16:28:18 -0800267 return -1;
268 }
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langley71d8a082014-12-13 16:28:18 -0800270 return 0;
271}
Adam Langley95c29f32014-06-20 12:00:00 -0700272
David Benjamin8c249802015-05-05 09:44:18 -0400273int DTLSv1_handle_timeout(SSL *ssl) {
274 if (!SSL_IS_DTLS(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800275 return -1;
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamin8c249802015-05-05 09:44:18 -0400278 /* if no timer is expired, don't do anything */
279 if (!dtls1_is_timer_expired(ssl)) {
280 return 0;
281 }
282
283 dtls1_double_timeout(ssl);
284
285 if (dtls1_check_timeout_num(ssl) < 0) {
286 return -1;
287 }
288
289 dtls1_start_timer(ssl);
290 return dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800291}
292
David Benjamin4d2e7ce2015-05-08 13:29:45 -0400293static void get_current_time(const SSL *ssl, struct timeval *out_clock) {
David Benjamin377fc312015-01-26 00:22:12 -0500294 if (ssl->ctx->current_time_cb != NULL) {
295 ssl->ctx->current_time_cb(ssl, out_clock);
296 return;
297 }
298
Adam Langleyded93582014-07-31 15:23:51 -0700299#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800300 struct _timeb time;
301 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500302 out_clock->tv_sec = time.time;
303 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700304#else
David Benjamin377fc312015-01-26 00:22:12 -0500305 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700306#endif
307}
308
David Benjamin2fa83de2015-02-08 01:40:08 -0500309int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
David Benjamine4824e82014-12-14 19:44:34 -0500310 uint8_t *message = (uint8_t *)s->init_buf->data;
311 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
312 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
313 uint8_t *p = serialised_header;
314
David Benjamin16d031a2014-12-14 18:52:44 -0500315 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
316 s->d1->next_handshake_write_seq++;
317
318 dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800319 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
320 s->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500321
Adam Langley71d8a082014-12-13 16:28:18 -0800322 /* Buffer the message to handle re-xmits */
323 dtls1_buffer_message(s, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700324
David Benjamine4824e82014-12-14 19:44:34 -0500325 /* Add the new message to the handshake hash. Serialize the message
326 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800327 *p++ = msg_hdr->type;
328 l2n3(msg_hdr->msg_len, p);
329 s2n(msg_hdr->seq, p);
330 l2n3(0, p);
331 l2n3(msg_hdr->msg_len, p);
David Benjamin9550c3a2015-08-05 08:50:34 -0400332 return ssl3_update_handshake_hash(s, serialised_header,
333 sizeof(serialised_header)) &&
334 ssl3_update_handshake_hash(s, message + DTLS1_HM_HEADER_LENGTH, len);
David Benjamine4824e82014-12-14 19:44:34 -0500335}
336
David Benjamin2fa83de2015-02-08 01:40:08 -0500337int dtls1_handshake_write(SSL *s) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400338 return dtls1_do_write(s, SSL3_RT_HANDSHAKE, dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800339}