blob: 1de75b8174ebe11f3bdd84165509b9ba05b68e85 [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
59#include <stdio.h>
60
61#if defined(OPENSSL_WINDOWS)
62#include <sys/timeb.h>
63#else
64#include <sys/socket.h>
Adam Langleyded93582014-07-31 15:23:51 -070065#include <sys/time.h>
Adam Langley95c29f32014-06-20 12:00:00 -070066#endif
67
68#include <openssl/err.h>
69#include <openssl/mem.h>
70#include <openssl/obj.h>
71
72#include "ssl_locl.h"
73
Adam Langleyded93582014-07-31 15:23:51 -070074static void get_current_time(OPENSSL_timeval *t);
Adam Langley71d8a082014-12-13 16:28:18 -080075static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft);
Adam Langley95c29f32014-06-20 12:00:00 -070076static void dtls1_set_handshake_header(SSL *s, int type, unsigned long len);
Adam Langley71d8a082014-12-13 16:28:18 -080077static int dtls1_handshake_write(
78 SSL *s, enum should_add_to_finished_hash should_add_to_finished_hash);
Adam Langley75712922014-10-10 16:23:43 -070079static void dtls1_add_to_finished_hash(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -070080
David Benjamin338fcaf2014-12-11 01:20:52 -050081const SSL3_ENC_METHOD DTLSv1_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -080082 tls1_enc,
83 tls1_mac,
84 tls1_setup_key_block,
85 tls1_generate_master_secret,
86 tls1_change_cipher_state,
87 tls1_final_finish_mac,
88 TLS1_FINISH_MAC_LENGTH,
89 tls1_cert_verify_mac,
90 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
91 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
92 tls1_alert_code,
93 tls1_export_keying_material,
94 SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV,
95 DTLS1_HM_HEADER_LENGTH,
96 dtls1_set_handshake_header,
97 dtls1_handshake_write,
98 dtls1_add_to_finished_hash,
99};
Adam Langley95c29f32014-06-20 12:00:00 -0700100
David Benjamin338fcaf2014-12-11 01:20:52 -0500101const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -0800102 tls1_enc,
103 tls1_mac,
104 tls1_setup_key_block,
105 tls1_generate_master_secret,
106 tls1_change_cipher_state,
107 tls1_final_finish_mac,
108 TLS1_FINISH_MAC_LENGTH,
109 tls1_cert_verify_mac,
110 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
111 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
112 tls1_alert_code,
113 tls1_export_keying_material,
114 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS |
115 SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
116 DTLS1_HM_HEADER_LENGTH,
117 dtls1_set_handshake_header,
118 dtls1_handshake_write,
119 dtls1_add_to_finished_hash,
120};
Adam Langley95c29f32014-06-20 12:00:00 -0700121
Adam Langley71d8a082014-12-13 16:28:18 -0800122int dtls1_new(SSL *s) {
123 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -0700124
Adam Langley71d8a082014-12-13 16:28:18 -0800125 if (!ssl3_new(s)) {
126 return 0;
127 }
128 d1 = OPENSSL_malloc(sizeof *d1);
129 if (d1 == NULL) {
130 ssl3_free(s);
131 return 0;
132 }
133 memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langley71d8a082014-12-13 16:28:18 -0800135 d1->unprocessed_rcds.q = pqueue_new();
136 d1->processed_rcds.q = pqueue_new();
137 d1->buffered_messages = pqueue_new();
138 d1->sent_messages = pqueue_new();
139 d1->buffered_app_data.q = pqueue_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700140
Adam Langley71d8a082014-12-13 16:28:18 -0800141 if (s->server) {
142 d1->cookie_len = sizeof(s->d1->cookie);
143 }
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langley71d8a082014-12-13 16:28:18 -0800145 if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q ||
146 !d1->buffered_messages || !d1->sent_messages ||
147 !d1->buffered_app_data.q) {
148 if (d1->unprocessed_rcds.q) {
149 pqueue_free(d1->unprocessed_rcds.q);
150 }
151 if (d1->processed_rcds.q) {
152 pqueue_free(d1->processed_rcds.q);
153 }
154 if (d1->buffered_messages) {
155 pqueue_free(d1->buffered_messages);
156 }
157 if (d1->sent_messages) {
158 pqueue_free(d1->sent_messages);
159 }
160 if (d1->buffered_app_data.q) {
161 pqueue_free(d1->buffered_app_data.q);
162 }
163 OPENSSL_free(d1);
164 ssl3_free(s);
165 return 0;
166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
Adam Langley71d8a082014-12-13 16:28:18 -0800168 s->d1 = d1;
169 s->method->ssl_clear(s);
170 return 1;
171}
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley71d8a082014-12-13 16:28:18 -0800173static void dtls1_clear_queues(SSL *s) {
174 pitem *item = NULL;
175 hm_fragment *frag = NULL;
176 DTLS1_RECORD_DATA *rdata;
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langley71d8a082014-12-13 16:28:18 -0800178 while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
179 rdata = (DTLS1_RECORD_DATA *)item->data;
180 if (rdata->rbuf.buf) {
181 OPENSSL_free(rdata->rbuf.buf);
182 }
183 OPENSSL_free(item->data);
184 pitem_free(item);
185 }
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langley71d8a082014-12-13 16:28:18 -0800187 while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
188 rdata = (DTLS1_RECORD_DATA *)item->data;
189 if (rdata->rbuf.buf) {
190 OPENSSL_free(rdata->rbuf.buf);
191 }
192 OPENSSL_free(item->data);
193 pitem_free(item);
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley71d8a082014-12-13 16:28:18 -0800196 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
197 frag = (hm_fragment *)item->data;
198 dtls1_hm_fragment_free(frag);
199 pitem_free(item);
200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley71d8a082014-12-13 16:28:18 -0800202 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
203 frag = (hm_fragment *)item->data;
204 dtls1_hm_fragment_free(frag);
205 pitem_free(item);
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langley71d8a082014-12-13 16:28:18 -0800208 while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
209 rdata = (DTLS1_RECORD_DATA *)item->data;
210 if (rdata->rbuf.buf) {
211 OPENSSL_free(rdata->rbuf.buf);
212 }
213 OPENSSL_free(item->data);
214 pitem_free(item);
215 }
216}
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley71d8a082014-12-13 16:28:18 -0800218void dtls1_free(SSL *s) {
219 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley71d8a082014-12-13 16:28:18 -0800221 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley71d8a082014-12-13 16:28:18 -0800223 pqueue_free(s->d1->unprocessed_rcds.q);
224 pqueue_free(s->d1->processed_rcds.q);
225 pqueue_free(s->d1->buffered_messages);
226 pqueue_free(s->d1->sent_messages);
227 pqueue_free(s->d1->buffered_app_data.q);
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langley71d8a082014-12-13 16:28:18 -0800229 OPENSSL_free(s->d1);
230 s->d1 = NULL;
231}
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langley71d8a082014-12-13 16:28:18 -0800233void dtls1_clear(SSL *s) {
234 pqueue unprocessed_rcds;
235 pqueue processed_rcds;
236 pqueue buffered_messages;
237 pqueue sent_messages;
238 pqueue buffered_app_data;
239 unsigned int mtu;
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley71d8a082014-12-13 16:28:18 -0800241 if (s->d1) {
242 unprocessed_rcds = s->d1->unprocessed_rcds.q;
243 processed_rcds = s->d1->processed_rcds.q;
244 buffered_messages = s->d1->buffered_messages;
245 sent_messages = s->d1->sent_messages;
246 buffered_app_data = s->d1->buffered_app_data.q;
247 mtu = s->d1->mtu;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langley71d8a082014-12-13 16:28:18 -0800249 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langley71d8a082014-12-13 16:28:18 -0800251 memset(s->d1, 0, sizeof(*(s->d1)));
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langley71d8a082014-12-13 16:28:18 -0800253 if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) {
254 s->d1->mtu = mtu;
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langley71d8a082014-12-13 16:28:18 -0800257 s->d1->unprocessed_rcds.q = unprocessed_rcds;
258 s->d1->processed_rcds.q = processed_rcds;
259 s->d1->buffered_messages = buffered_messages;
260 s->d1->sent_messages = sent_messages;
261 s->d1->buffered_app_data.q = buffered_app_data;
262 }
Adam Langley95c29f32014-06-20 12:00:00 -0700263
Adam Langley71d8a082014-12-13 16:28:18 -0800264 ssl3_clear(s);
265 s->version = DTLS1_2_VERSION;
266}
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langley71d8a082014-12-13 16:28:18 -0800268long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
269 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langley71d8a082014-12-13 16:28:18 -0800271 switch (cmd) {
272 case DTLS_CTRL_GET_TIMEOUT:
273 if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
274 ret = 1;
275 }
276 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langley71d8a082014-12-13 16:28:18 -0800278 case DTLS_CTRL_HANDLE_TIMEOUT:
279 ret = dtls1_handle_timeout(s);
280 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 default:
283 ret = ssl3_ctrl(s, cmd, larg, parg);
284 break;
285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286
Adam Langley71d8a082014-12-13 16:28:18 -0800287 return ret;
288}
289
290/* As it's impossible to use stream ciphers in "datagram" mode, this
Adam Langley95c29f32014-06-20 12:00:00 -0700291 * simple filter is designed to disengage them in DTLS. Unfortunately
292 * there is no universal way to identify stream SSL_CIPHER, so we have
293 * to explicitly list their SSL_* codes. Currently RC4 is the only one
Adam Langley71d8a082014-12-13 16:28:18 -0800294 * available, but if new ones emerge, they will have to be added... */
295const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
296 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley71d8a082014-12-13 16:28:18 -0800298 if (ciph != NULL) {
299 if (ciph->algorithm_enc == SSL_RC4) {
300 return NULL;
301 }
302 /* TODO(davidben): EVP_AEAD does not work in DTLS yet. */
303 if (ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD ||
304 ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) {
305 return NULL;
306 }
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 return ciph;
310}
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley71d8a082014-12-13 16:28:18 -0800312void dtls1_start_timer(SSL *s) {
313 /* If timer is not set, initialize duration with 1 second */
314 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
315 s->d1->timeout_duration = 1;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley71d8a082014-12-13 16:28:18 -0800318 /* Set timeout to current time */
319 get_current_time(&s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley71d8a082014-12-13 16:28:18 -0800321 /* Add duration to current time */
322 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
323 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
324 &s->d1->next_timeout);
325}
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley71d8a082014-12-13 16:28:18 -0800327static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
328 OPENSSL_timeval timenow;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langley71d8a082014-12-13 16:28:18 -0800330 /* If no timeout is set, just return NULL */
331 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
332 return NULL;
333 }
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langley71d8a082014-12-13 16:28:18 -0800335 /* Get current time */
336 get_current_time(&timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langley71d8a082014-12-13 16:28:18 -0800338 /* If timer already expired, set remaining time to 0 */
339 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
340 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
341 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
342 memset(timeleft, 0, sizeof(OPENSSL_timeval));
343 return timeleft;
344 }
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langley71d8a082014-12-13 16:28:18 -0800346 /* Calculate time left until timer expires */
347 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
348 timeleft->tv_sec -= timenow.tv_sec;
349 timeleft->tv_usec -= timenow.tv_usec;
350 if (timeleft->tv_usec < 0) {
351 timeleft->tv_sec--;
352 timeleft->tv_usec += 1000000;
353 }
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Adam Langley71d8a082014-12-13 16:28:18 -0800355 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
356 * because of small devergences with socket timeouts. */
357 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
358 memset(timeleft, 0, sizeof(OPENSSL_timeval));
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langley71d8a082014-12-13 16:28:18 -0800361 return timeleft;
362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley71d8a082014-12-13 16:28:18 -0800364int dtls1_is_timer_expired(SSL *s) {
365 OPENSSL_timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Adam Langley71d8a082014-12-13 16:28:18 -0800367 /* Get time left until timeout, return false if no timer running */
368 if (dtls1_get_timeout(s, &timeleft) == NULL) {
369 return 0;
370 }
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langley71d8a082014-12-13 16:28:18 -0800372 /* Return false if timer is not expired yet */
373 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
374 return 0;
375 }
Adam Langley95c29f32014-06-20 12:00:00 -0700376
Adam Langley71d8a082014-12-13 16:28:18 -0800377 /* Timer expired, so return true */
378 return 1;
379}
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langley71d8a082014-12-13 16:28:18 -0800381void dtls1_double_timeout(SSL *s) {
382 s->d1->timeout_duration *= 2;
383 if (s->d1->timeout_duration > 60) {
384 s->d1->timeout_duration = 60;
385 }
386 dtls1_start_timer(s);
387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langley71d8a082014-12-13 16:28:18 -0800389void dtls1_stop_timer(SSL *s) {
390 /* Reset everything */
391 memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
392 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
393 s->d1->timeout_duration = 1;
394 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
395 &s->d1->next_timeout);
396 /* Clear retransmission buffer */
397 dtls1_clear_record_buffer(s);
398}
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langley71d8a082014-12-13 16:28:18 -0800400int dtls1_check_timeout_num(SSL *s) {
401 s->d1->timeout.num_alerts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langley71d8a082014-12-13 16:28:18 -0800403 /* Reduce MTU after 2 unsuccessful retransmissions */
404 if (s->d1->timeout.num_alerts > 2) {
405 s->d1->mtu =
406 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
407 }
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langley71d8a082014-12-13 16:28:18 -0800409 if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
410 /* fail the connection, enough alerts have been sent */
411 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
412 return -1;
413 }
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langley71d8a082014-12-13 16:28:18 -0800415 return 0;
416}
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langley71d8a082014-12-13 16:28:18 -0800418int dtls1_handle_timeout(SSL *s) {
419 /* if no timer is expired, don't do anything */
420 if (!dtls1_is_timer_expired(s)) {
421 return 0;
422 }
Adam Langley95c29f32014-06-20 12:00:00 -0700423
Adam Langley71d8a082014-12-13 16:28:18 -0800424 dtls1_double_timeout(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langley71d8a082014-12-13 16:28:18 -0800426 if (dtls1_check_timeout_num(s) < 0) {
427 return -1;
428 }
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langley71d8a082014-12-13 16:28:18 -0800430 s->d1->timeout.read_timeouts++;
431 if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
432 s->d1->timeout.read_timeouts = 1;
433 }
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langley71d8a082014-12-13 16:28:18 -0800435 dtls1_start_timer(s);
436 return dtls1_retransmit_buffered_messages(s);
437}
438
439static void get_current_time(OPENSSL_timeval *t) {
Adam Langleyded93582014-07-31 15:23:51 -0700440#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800441 struct _timeb time;
442 _ftime(&time);
443 t->tv_sec = time.time;
444 t->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700445#else
Adam Langley71d8a082014-12-13 16:28:18 -0800446 gettimeofday(t, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700447#endif
448}
449
Adam Langley71d8a082014-12-13 16:28:18 -0800450static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
451 uint8_t *p = (uint8_t *)s->init_buf->data;
452 dtls1_set_message_header(s, p, htype, len, 0, len);
453 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
454 s->init_off = 0;
455 /* Buffer the message to handle re-xmits */
456 dtls1_buffer_message(s, 0);
457}
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langley71d8a082014-12-13 16:28:18 -0800459static int dtls1_handshake_write(
460 SSL *s, enum should_add_to_finished_hash should_add_to_finished_hash) {
461 return dtls1_do_write(s, SSL3_RT_HANDSHAKE, should_add_to_finished_hash);
462}
Adam Langley75712922014-10-10 16:23:43 -0700463
Adam Langley71d8a082014-12-13 16:28:18 -0800464static void dtls1_add_to_finished_hash(SSL *s) {
465 uint8_t *record = (uint8_t *)&s->init_buf->data[s->init_off];
466 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
467 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
468 uint8_t *p = serialised_header;
Adam Langley75712922014-10-10 16:23:43 -0700469
Adam Langley71d8a082014-12-13 16:28:18 -0800470 /* Construct the message header as if it were a single fragment. */
471 *p++ = msg_hdr->type;
472 l2n3(msg_hdr->msg_len, p);
473 s2n(msg_hdr->seq, p);
474 l2n3(0, p);
475 l2n3(msg_hdr->msg_len, p);
476 ssl3_finish_mac(s, serialised_header, sizeof(serialised_header));
477 ssl3_finish_mac(s, record + DTLS1_HM_HEADER_LENGTH,
478 s->init_num - DTLS1_HM_HEADER_LENGTH);
479}