blob: dcf86e5114077a9390ab31c4b73b438643f5980f [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>
61
62#if defined(OPENSSL_WINDOWS)
63#include <sys/timeb.h>
64#else
65#include <sys/socket.h>
Adam Langleyded93582014-07-31 15:23:51 -070066#include <sys/time.h>
Adam Langley95c29f32014-06-20 12:00:00 -070067#endif
68
69#include <openssl/err.h>
70#include <openssl/mem.h>
71#include <openssl/obj.h>
72
73#include "ssl_locl.h"
74
David Benjamine33b9b02015-01-26 01:27:22 -050075/* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
76 * before starting to decrease the MTU. */
77#define DTLS1_MTU_TIMEOUTS 2
78
79/* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
80 * before failing the DTLS handshake. */
81#define DTLS1_MAX_TIMEOUTS 12
82
David Benjamin377fc312015-01-26 00:22:12 -050083static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock);
Adam Langley71d8a082014-12-13 16:28:18 -080084static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft);
Adam Langley95c29f32014-06-20 12:00:00 -070085static void dtls1_set_handshake_header(SSL *s, int type, unsigned long len);
David Benjamine4824e82014-12-14 19:44:34 -050086static int dtls1_handshake_write(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -070087
David Benjamin338fcaf2014-12-11 01:20:52 -050088const SSL3_ENC_METHOD DTLSv1_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -080089 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -050090 tls1_prf,
Adam Langley71d8a082014-12-13 16:28:18 -080091 tls1_setup_key_block,
92 tls1_generate_master_secret,
93 tls1_change_cipher_state,
94 tls1_final_finish_mac,
95 TLS1_FINISH_MAC_LENGTH,
96 tls1_cert_verify_mac,
97 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
98 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
99 tls1_alert_code,
100 tls1_export_keying_material,
101 SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV,
102 DTLS1_HM_HEADER_LENGTH,
103 dtls1_set_handshake_header,
104 dtls1_handshake_write,
Adam Langley71d8a082014-12-13 16:28:18 -0800105};
Adam Langley95c29f32014-06-20 12:00:00 -0700106
David Benjamin338fcaf2014-12-11 01:20:52 -0500107const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -0800108 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -0500109 tls1_prf,
Adam Langley71d8a082014-12-13 16:28:18 -0800110 tls1_setup_key_block,
111 tls1_generate_master_secret,
112 tls1_change_cipher_state,
113 tls1_final_finish_mac,
114 TLS1_FINISH_MAC_LENGTH,
115 tls1_cert_verify_mac,
116 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
117 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
118 tls1_alert_code,
119 tls1_export_keying_material,
120 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS |
121 SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
122 DTLS1_HM_HEADER_LENGTH,
123 dtls1_set_handshake_header,
124 dtls1_handshake_write,
Adam Langley71d8a082014-12-13 16:28:18 -0800125};
Adam Langley95c29f32014-06-20 12:00:00 -0700126
Adam Langley71d8a082014-12-13 16:28:18 -0800127int dtls1_new(SSL *s) {
128 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -0700129
Adam Langley71d8a082014-12-13 16:28:18 -0800130 if (!ssl3_new(s)) {
131 return 0;
132 }
133 d1 = OPENSSL_malloc(sizeof *d1);
134 if (d1 == NULL) {
135 ssl3_free(s);
136 return 0;
137 }
138 memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -0700139
Adam Langley71d8a082014-12-13 16:28:18 -0800140 d1->unprocessed_rcds.q = pqueue_new();
141 d1->processed_rcds.q = pqueue_new();
142 d1->buffered_messages = pqueue_new();
143 d1->sent_messages = pqueue_new();
144 d1->buffered_app_data.q = pqueue_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700145
Adam Langley71d8a082014-12-13 16:28:18 -0800146 if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q ||
147 !d1->buffered_messages || !d1->sent_messages ||
148 !d1->buffered_app_data.q) {
149 if (d1->unprocessed_rcds.q) {
150 pqueue_free(d1->unprocessed_rcds.q);
151 }
152 if (d1->processed_rcds.q) {
153 pqueue_free(d1->processed_rcds.q);
154 }
155 if (d1->buffered_messages) {
156 pqueue_free(d1->buffered_messages);
157 }
158 if (d1->sent_messages) {
159 pqueue_free(d1->sent_messages);
160 }
161 if (d1->buffered_app_data.q) {
162 pqueue_free(d1->buffered_app_data.q);
163 }
164 OPENSSL_free(d1);
165 ssl3_free(s);
166 return 0;
167 }
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langley71d8a082014-12-13 16:28:18 -0800169 s->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500170
171 /* Set the version to the highest version for DTLS. This controls the initial
172 * state of |s->enc_method| and what the API reports as the version prior to
173 * negotiation.
174 *
175 * TODO(davidben): This is fragile and confusing. */
176 s->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800177 return 1;
178}
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langley71d8a082014-12-13 16:28:18 -0800180static void dtls1_clear_queues(SSL *s) {
181 pitem *item = NULL;
182 hm_fragment *frag = NULL;
183 DTLS1_RECORD_DATA *rdata;
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langley71d8a082014-12-13 16:28:18 -0800185 while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
186 rdata = (DTLS1_RECORD_DATA *)item->data;
187 if (rdata->rbuf.buf) {
188 OPENSSL_free(rdata->rbuf.buf);
189 }
190 OPENSSL_free(item->data);
191 pitem_free(item);
192 }
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley71d8a082014-12-13 16:28:18 -0800194 while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
195 rdata = (DTLS1_RECORD_DATA *)item->data;
196 if (rdata->rbuf.buf) {
197 OPENSSL_free(rdata->rbuf.buf);
198 }
199 OPENSSL_free(item->data);
200 pitem_free(item);
201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langley71d8a082014-12-13 16:28:18 -0800203 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
204 frag = (hm_fragment *)item->data;
205 dtls1_hm_fragment_free(frag);
206 pitem_free(item);
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langley71d8a082014-12-13 16:28:18 -0800209 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
210 frag = (hm_fragment *)item->data;
211 dtls1_hm_fragment_free(frag);
212 pitem_free(item);
213 }
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley71d8a082014-12-13 16:28:18 -0800215 while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
216 rdata = (DTLS1_RECORD_DATA *)item->data;
217 if (rdata->rbuf.buf) {
218 OPENSSL_free(rdata->rbuf.buf);
219 }
220 OPENSSL_free(item->data);
221 pitem_free(item);
222 }
223}
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langley71d8a082014-12-13 16:28:18 -0800225void dtls1_free(SSL *s) {
226 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700227
David Benjamin62fd1622015-01-11 13:30:01 -0500228 if (s == NULL || s->d1 == NULL) {
229 return;
230 }
231
Adam Langley71d8a082014-12-13 16:28:18 -0800232 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley71d8a082014-12-13 16:28:18 -0800234 pqueue_free(s->d1->unprocessed_rcds.q);
235 pqueue_free(s->d1->processed_rcds.q);
236 pqueue_free(s->d1->buffered_messages);
237 pqueue_free(s->d1->sent_messages);
238 pqueue_free(s->d1->buffered_app_data.q);
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley71d8a082014-12-13 16:28:18 -0800240 OPENSSL_free(s->d1);
241 s->d1 = NULL;
242}
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langley71d8a082014-12-13 16:28:18 -0800244long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
245 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langley71d8a082014-12-13 16:28:18 -0800247 switch (cmd) {
248 case DTLS_CTRL_GET_TIMEOUT:
249 if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
250 ret = 1;
251 }
252 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley71d8a082014-12-13 16:28:18 -0800254 case DTLS_CTRL_HANDLE_TIMEOUT:
255 ret = dtls1_handle_timeout(s);
256 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700257
Adam Langley71d8a082014-12-13 16:28:18 -0800258 default:
259 ret = ssl3_ctrl(s, cmd, larg, parg);
260 break;
261 }
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langley71d8a082014-12-13 16:28:18 -0800263 return ret;
264}
265
Adam Langley71d8a082014-12-13 16:28:18 -0800266const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
267 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
David Benjamine95d20d2014-12-23 11:16:01 -0500268 /* DTLS does not support stream ciphers. */
269 if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) {
270 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langley71d8a082014-12-13 16:28:18 -0800273 return ciph;
274}
Adam Langley95c29f32014-06-20 12:00:00 -0700275
Adam Langley71d8a082014-12-13 16:28:18 -0800276void dtls1_start_timer(SSL *s) {
277 /* If timer is not set, initialize duration with 1 second */
278 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
279 s->d1->timeout_duration = 1;
280 }
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 /* Set timeout to current time */
David Benjamin377fc312015-01-26 00:22:12 -0500283 get_current_time(s, &s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langley71d8a082014-12-13 16:28:18 -0800285 /* Add duration to current time */
286 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
287 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
288 &s->d1->next_timeout);
289}
Adam Langley95c29f32014-06-20 12:00:00 -0700290
Adam Langley71d8a082014-12-13 16:28:18 -0800291static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
292 OPENSSL_timeval timenow;
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langley71d8a082014-12-13 16:28:18 -0800294 /* If no timeout is set, just return NULL */
295 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
296 return NULL;
297 }
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Adam Langley71d8a082014-12-13 16:28:18 -0800299 /* Get current time */
David Benjamin377fc312015-01-26 00:22:12 -0500300 get_current_time(s, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langley71d8a082014-12-13 16:28:18 -0800302 /* If timer already expired, set remaining time to 0 */
303 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
304 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
305 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
306 memset(timeleft, 0, sizeof(OPENSSL_timeval));
307 return timeleft;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langley71d8a082014-12-13 16:28:18 -0800310 /* Calculate time left until timer expires */
311 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
312 timeleft->tv_sec -= timenow.tv_sec;
313 timeleft->tv_usec -= timenow.tv_usec;
314 if (timeleft->tv_usec < 0) {
315 timeleft->tv_sec--;
316 timeleft->tv_usec += 1000000;
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley71d8a082014-12-13 16:28:18 -0800319 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
320 * because of small devergences with socket timeouts. */
321 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
322 memset(timeleft, 0, sizeof(OPENSSL_timeval));
323 }
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langley71d8a082014-12-13 16:28:18 -0800325 return timeleft;
326}
Adam Langley95c29f32014-06-20 12:00:00 -0700327
Adam Langley71d8a082014-12-13 16:28:18 -0800328int dtls1_is_timer_expired(SSL *s) {
329 OPENSSL_timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley71d8a082014-12-13 16:28:18 -0800331 /* Get time left until timeout, return false if no timer running */
332 if (dtls1_get_timeout(s, &timeleft) == NULL) {
333 return 0;
334 }
Adam Langley95c29f32014-06-20 12:00:00 -0700335
Adam Langley71d8a082014-12-13 16:28:18 -0800336 /* Return false if timer is not expired yet */
337 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
338 return 0;
339 }
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langley71d8a082014-12-13 16:28:18 -0800341 /* Timer expired, so return true */
342 return 1;
343}
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langley71d8a082014-12-13 16:28:18 -0800345void dtls1_double_timeout(SSL *s) {
346 s->d1->timeout_duration *= 2;
347 if (s->d1->timeout_duration > 60) {
348 s->d1->timeout_duration = 60;
349 }
350 dtls1_start_timer(s);
351}
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langley71d8a082014-12-13 16:28:18 -0800353void dtls1_stop_timer(SSL *s) {
354 /* Reset everything */
David Benjamine33b9b02015-01-26 01:27:22 -0500355 s->d1->num_timeouts = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800356 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
357 s->d1->timeout_duration = 1;
358 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
359 &s->d1->next_timeout);
360 /* Clear retransmission buffer */
361 dtls1_clear_record_buffer(s);
362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley71d8a082014-12-13 16:28:18 -0800364int dtls1_check_timeout_num(SSL *s) {
David Benjamine33b9b02015-01-26 01:27:22 -0500365 s->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Adam Langley71d8a082014-12-13 16:28:18 -0800367 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamine33b9b02015-01-26 01:27:22 -0500368 if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
David Benjamin7f18b132015-01-11 17:36:21 -0500369 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500370 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
371 NULL);
372 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
373 s->d1->mtu = (unsigned)mtu;
374 }
Adam Langley71d8a082014-12-13 16:28:18 -0800375 }
Adam Langley95c29f32014-06-20 12:00:00 -0700376
David Benjamine33b9b02015-01-26 01:27:22 -0500377 if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800378 /* fail the connection, enough alerts have been sent */
379 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
380 return -1;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langley71d8a082014-12-13 16:28:18 -0800383 return 0;
384}
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley71d8a082014-12-13 16:28:18 -0800386int dtls1_handle_timeout(SSL *s) {
387 /* if no timer is expired, don't do anything */
388 if (!dtls1_is_timer_expired(s)) {
389 return 0;
390 }
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langley71d8a082014-12-13 16:28:18 -0800392 dtls1_double_timeout(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langley71d8a082014-12-13 16:28:18 -0800394 if (dtls1_check_timeout_num(s) < 0) {
395 return -1;
396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langley71d8a082014-12-13 16:28:18 -0800398 dtls1_start_timer(s);
399 return dtls1_retransmit_buffered_messages(s);
400}
401
David Benjamin377fc312015-01-26 00:22:12 -0500402static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) {
403 if (ssl->ctx->current_time_cb != NULL) {
404 ssl->ctx->current_time_cb(ssl, out_clock);
405 return;
406 }
407
Adam Langleyded93582014-07-31 15:23:51 -0700408#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800409 struct _timeb time;
410 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500411 out_clock->tv_sec = time.time;
412 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700413#else
David Benjamin377fc312015-01-26 00:22:12 -0500414 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700415#endif
416}
417
Adam Langley71d8a082014-12-13 16:28:18 -0800418static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
David Benjamine4824e82014-12-14 19:44:34 -0500419 uint8_t *message = (uint8_t *)s->init_buf->data;
420 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
421 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
422 uint8_t *p = serialised_header;
423
David Benjamin16d031a2014-12-14 18:52:44 -0500424 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
425 s->d1->next_handshake_write_seq++;
426
427 dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800428 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
429 s->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500430
Adam Langley71d8a082014-12-13 16:28:18 -0800431 /* Buffer the message to handle re-xmits */
432 dtls1_buffer_message(s, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700433
David Benjamine4824e82014-12-14 19:44:34 -0500434 /* Add the new message to the handshake hash. Serialize the message
435 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800436 *p++ = msg_hdr->type;
437 l2n3(msg_hdr->msg_len, p);
438 s2n(msg_hdr->seq, p);
439 l2n3(0, p);
440 l2n3(msg_hdr->msg_len, p);
441 ssl3_finish_mac(s, serialised_header, sizeof(serialised_header));
David Benjamine4824e82014-12-14 19:44:34 -0500442 ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
443}
444
445static int dtls1_handshake_write(SSL *s) {
446 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800447}