blob: ce05b891cf2abb82a5c395af5fe6cdd9f0e924d1 [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);
David Benjamine4824e82014-12-14 19:44:34 -050077static int dtls1_handshake_write(SSL *s);
Adam Langley95c29f32014-06-20 12:00:00 -070078
David Benjamin338fcaf2014-12-11 01:20:52 -050079const SSL3_ENC_METHOD DTLSv1_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -080080 tls1_enc,
81 tls1_mac,
82 tls1_setup_key_block,
83 tls1_generate_master_secret,
84 tls1_change_cipher_state,
85 tls1_final_finish_mac,
86 TLS1_FINISH_MAC_LENGTH,
87 tls1_cert_verify_mac,
88 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
89 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
90 tls1_alert_code,
91 tls1_export_keying_material,
92 SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV,
93 DTLS1_HM_HEADER_LENGTH,
94 dtls1_set_handshake_header,
95 dtls1_handshake_write,
Adam Langley71d8a082014-12-13 16:28:18 -080096};
Adam Langley95c29f32014-06-20 12:00:00 -070097
David Benjamin338fcaf2014-12-11 01:20:52 -050098const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -080099 tls1_enc,
100 tls1_mac,
101 tls1_setup_key_block,
102 tls1_generate_master_secret,
103 tls1_change_cipher_state,
104 tls1_final_finish_mac,
105 TLS1_FINISH_MAC_LENGTH,
106 tls1_cert_verify_mac,
107 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
108 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
109 tls1_alert_code,
110 tls1_export_keying_material,
111 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS |
112 SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
113 DTLS1_HM_HEADER_LENGTH,
114 dtls1_set_handshake_header,
115 dtls1_handshake_write,
Adam Langley71d8a082014-12-13 16:28:18 -0800116};
Adam Langley95c29f32014-06-20 12:00:00 -0700117
Adam Langley71d8a082014-12-13 16:28:18 -0800118int dtls1_new(SSL *s) {
119 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -0700120
Adam Langley71d8a082014-12-13 16:28:18 -0800121 if (!ssl3_new(s)) {
122 return 0;
123 }
124 d1 = OPENSSL_malloc(sizeof *d1);
125 if (d1 == NULL) {
126 ssl3_free(s);
127 return 0;
128 }
129 memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley71d8a082014-12-13 16:28:18 -0800131 d1->unprocessed_rcds.q = pqueue_new();
132 d1->processed_rcds.q = pqueue_new();
133 d1->buffered_messages = pqueue_new();
134 d1->sent_messages = pqueue_new();
135 d1->buffered_app_data.q = pqueue_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700136
Adam Langley71d8a082014-12-13 16:28:18 -0800137 if (s->server) {
138 d1->cookie_len = sizeof(s->d1->cookie);
139 }
Adam Langley95c29f32014-06-20 12:00:00 -0700140
Adam Langley71d8a082014-12-13 16:28:18 -0800141 if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q ||
142 !d1->buffered_messages || !d1->sent_messages ||
143 !d1->buffered_app_data.q) {
144 if (d1->unprocessed_rcds.q) {
145 pqueue_free(d1->unprocessed_rcds.q);
146 }
147 if (d1->processed_rcds.q) {
148 pqueue_free(d1->processed_rcds.q);
149 }
150 if (d1->buffered_messages) {
151 pqueue_free(d1->buffered_messages);
152 }
153 if (d1->sent_messages) {
154 pqueue_free(d1->sent_messages);
155 }
156 if (d1->buffered_app_data.q) {
157 pqueue_free(d1->buffered_app_data.q);
158 }
159 OPENSSL_free(d1);
160 ssl3_free(s);
161 return 0;
162 }
Adam Langley95c29f32014-06-20 12:00:00 -0700163
Adam Langley71d8a082014-12-13 16:28:18 -0800164 s->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500165
166 /* Set the version to the highest version for DTLS. This controls the initial
167 * state of |s->enc_method| and what the API reports as the version prior to
168 * negotiation.
169 *
170 * TODO(davidben): This is fragile and confusing. */
171 s->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800172 return 1;
173}
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langley71d8a082014-12-13 16:28:18 -0800175static void dtls1_clear_queues(SSL *s) {
176 pitem *item = NULL;
177 hm_fragment *frag = NULL;
178 DTLS1_RECORD_DATA *rdata;
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langley71d8a082014-12-13 16:28:18 -0800180 while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
181 rdata = (DTLS1_RECORD_DATA *)item->data;
182 if (rdata->rbuf.buf) {
183 OPENSSL_free(rdata->rbuf.buf);
184 }
185 OPENSSL_free(item->data);
186 pitem_free(item);
187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langley71d8a082014-12-13 16:28:18 -0800189 while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
190 rdata = (DTLS1_RECORD_DATA *)item->data;
191 if (rdata->rbuf.buf) {
192 OPENSSL_free(rdata->rbuf.buf);
193 }
194 OPENSSL_free(item->data);
195 pitem_free(item);
196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley71d8a082014-12-13 16:28:18 -0800198 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
199 frag = (hm_fragment *)item->data;
200 dtls1_hm_fragment_free(frag);
201 pitem_free(item);
202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley71d8a082014-12-13 16:28:18 -0800204 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
205 frag = (hm_fragment *)item->data;
206 dtls1_hm_fragment_free(frag);
207 pitem_free(item);
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
Adam Langley71d8a082014-12-13 16:28:18 -0800210 while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
211 rdata = (DTLS1_RECORD_DATA *)item->data;
212 if (rdata->rbuf.buf) {
213 OPENSSL_free(rdata->rbuf.buf);
214 }
215 OPENSSL_free(item->data);
216 pitem_free(item);
217 }
218}
Adam Langley95c29f32014-06-20 12:00:00 -0700219
Adam Langley71d8a082014-12-13 16:28:18 -0800220void dtls1_free(SSL *s) {
221 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700222
David Benjamin62fd1622015-01-11 13:30:01 -0500223 if (s == NULL || s->d1 == NULL) {
224 return;
225 }
226
Adam Langley71d8a082014-12-13 16:28:18 -0800227 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langley71d8a082014-12-13 16:28:18 -0800229 pqueue_free(s->d1->unprocessed_rcds.q);
230 pqueue_free(s->d1->processed_rcds.q);
231 pqueue_free(s->d1->buffered_messages);
232 pqueue_free(s->d1->sent_messages);
233 pqueue_free(s->d1->buffered_app_data.q);
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langley71d8a082014-12-13 16:28:18 -0800235 OPENSSL_free(s->d1);
236 s->d1 = NULL;
237}
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langley71d8a082014-12-13 16:28:18 -0800239long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
240 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langley71d8a082014-12-13 16:28:18 -0800242 switch (cmd) {
243 case DTLS_CTRL_GET_TIMEOUT:
244 if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
245 ret = 1;
246 }
247 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langley71d8a082014-12-13 16:28:18 -0800249 case DTLS_CTRL_HANDLE_TIMEOUT:
250 ret = dtls1_handle_timeout(s);
251 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langley71d8a082014-12-13 16:28:18 -0800253 default:
254 ret = ssl3_ctrl(s, cmd, larg, parg);
255 break;
256 }
Adam Langley95c29f32014-06-20 12:00:00 -0700257
Adam Langley71d8a082014-12-13 16:28:18 -0800258 return ret;
259}
260
261/* As it's impossible to use stream ciphers in "datagram" mode, this
Adam Langley95c29f32014-06-20 12:00:00 -0700262 * simple filter is designed to disengage them in DTLS. Unfortunately
263 * there is no universal way to identify stream SSL_CIPHER, so we have
264 * to explicitly list their SSL_* codes. Currently RC4 is the only one
Adam Langley71d8a082014-12-13 16:28:18 -0800265 * available, but if new ones emerge, they will have to be added... */
266const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
267 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langley71d8a082014-12-13 16:28:18 -0800269 if (ciph != NULL) {
270 if (ciph->algorithm_enc == SSL_RC4) {
271 return NULL;
272 }
273 /* TODO(davidben): EVP_AEAD does not work in DTLS yet. */
274 if (ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD ||
275 ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) {
276 return NULL;
277 }
278 }
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langley71d8a082014-12-13 16:28:18 -0800280 return ciph;
281}
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langley71d8a082014-12-13 16:28:18 -0800283void dtls1_start_timer(SSL *s) {
284 /* If timer is not set, initialize duration with 1 second */
285 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
286 s->d1->timeout_duration = 1;
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288
Adam Langley71d8a082014-12-13 16:28:18 -0800289 /* Set timeout to current time */
290 get_current_time(&s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley71d8a082014-12-13 16:28:18 -0800292 /* Add duration to current time */
293 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
294 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
295 &s->d1->next_timeout);
296}
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley71d8a082014-12-13 16:28:18 -0800298static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
299 OPENSSL_timeval timenow;
Adam Langley95c29f32014-06-20 12:00:00 -0700300
Adam Langley71d8a082014-12-13 16:28:18 -0800301 /* If no timeout is set, just return NULL */
302 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
303 return NULL;
304 }
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley71d8a082014-12-13 16:28:18 -0800306 /* Get current time */
307 get_current_time(&timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 /* If timer already expired, set remaining time to 0 */
310 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
311 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
312 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
313 memset(timeleft, 0, sizeof(OPENSSL_timeval));
314 return timeleft;
315 }
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langley71d8a082014-12-13 16:28:18 -0800317 /* Calculate time left until timer expires */
318 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
319 timeleft->tv_sec -= timenow.tv_sec;
320 timeleft->tv_usec -= timenow.tv_usec;
321 if (timeleft->tv_usec < 0) {
322 timeleft->tv_sec--;
323 timeleft->tv_usec += 1000000;
324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325
Adam Langley71d8a082014-12-13 16:28:18 -0800326 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
327 * because of small devergences with socket timeouts. */
328 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
329 memset(timeleft, 0, sizeof(OPENSSL_timeval));
330 }
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langley71d8a082014-12-13 16:28:18 -0800332 return timeleft;
333}
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langley71d8a082014-12-13 16:28:18 -0800335int dtls1_is_timer_expired(SSL *s) {
336 OPENSSL_timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langley71d8a082014-12-13 16:28:18 -0800338 /* Get time left until timeout, return false if no timer running */
339 if (dtls1_get_timeout(s, &timeleft) == NULL) {
340 return 0;
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langley71d8a082014-12-13 16:28:18 -0800343 /* Return false if timer is not expired yet */
344 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
345 return 0;
346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley71d8a082014-12-13 16:28:18 -0800348 /* Timer expired, so return true */
349 return 1;
350}
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langley71d8a082014-12-13 16:28:18 -0800352void dtls1_double_timeout(SSL *s) {
353 s->d1->timeout_duration *= 2;
354 if (s->d1->timeout_duration > 60) {
355 s->d1->timeout_duration = 60;
356 }
357 dtls1_start_timer(s);
358}
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley71d8a082014-12-13 16:28:18 -0800360void dtls1_stop_timer(SSL *s) {
361 /* Reset everything */
362 memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
363 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
364 s->d1->timeout_duration = 1;
365 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
366 &s->d1->next_timeout);
367 /* Clear retransmission buffer */
368 dtls1_clear_record_buffer(s);
369}
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langley71d8a082014-12-13 16:28:18 -0800371int dtls1_check_timeout_num(SSL *s) {
372 s->d1->timeout.num_alerts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langley71d8a082014-12-13 16:28:18 -0800374 /* Reduce MTU after 2 unsuccessful retransmissions */
375 if (s->d1->timeout.num_alerts > 2) {
376 s->d1->mtu =
377 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
378 }
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langley71d8a082014-12-13 16:28:18 -0800380 if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
381 /* fail the connection, enough alerts have been sent */
382 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
383 return -1;
384 }
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley71d8a082014-12-13 16:28:18 -0800386 return 0;
387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langley71d8a082014-12-13 16:28:18 -0800389int dtls1_handle_timeout(SSL *s) {
390 /* if no timer is expired, don't do anything */
391 if (!dtls1_is_timer_expired(s)) {
392 return 0;
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langley71d8a082014-12-13 16:28:18 -0800395 dtls1_double_timeout(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langley71d8a082014-12-13 16:28:18 -0800397 if (dtls1_check_timeout_num(s) < 0) {
398 return -1;
399 }
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langley71d8a082014-12-13 16:28:18 -0800401 s->d1->timeout.read_timeouts++;
402 if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
403 s->d1->timeout.read_timeouts = 1;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langley71d8a082014-12-13 16:28:18 -0800406 dtls1_start_timer(s);
407 return dtls1_retransmit_buffered_messages(s);
408}
409
410static void get_current_time(OPENSSL_timeval *t) {
Adam Langleyded93582014-07-31 15:23:51 -0700411#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800412 struct _timeb time;
413 _ftime(&time);
414 t->tv_sec = time.time;
415 t->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700416#else
Adam Langley71d8a082014-12-13 16:28:18 -0800417 gettimeofday(t, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700418#endif
419}
420
Adam Langley71d8a082014-12-13 16:28:18 -0800421static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
David Benjamine4824e82014-12-14 19:44:34 -0500422 uint8_t *message = (uint8_t *)s->init_buf->data;
423 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
424 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
425 uint8_t *p = serialised_header;
426
David Benjamin16d031a2014-12-14 18:52:44 -0500427 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
428 s->d1->next_handshake_write_seq++;
429
430 dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800431 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
432 s->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500433
Adam Langley71d8a082014-12-13 16:28:18 -0800434 /* Buffer the message to handle re-xmits */
435 dtls1_buffer_message(s, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700436
David Benjamine4824e82014-12-14 19:44:34 -0500437 /* Add the new message to the handshake hash. Serialize the message
438 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800439 *p++ = msg_hdr->type;
440 l2n3(msg_hdr->msg_len, p);
441 s2n(msg_hdr->seq, p);
442 l2n3(0, p);
443 l2n3(msg_hdr->msg_len, p);
444 ssl3_finish_mac(s, serialised_header, sizeof(serialised_header));
David Benjamine4824e82014-12-14 19:44:34 -0500445 ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
446}
447
448static int dtls1_handshake_write(SSL *s) {
449 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800450}