blob: 48cc81f0198fb1d4a2ccd5a378b8e4c2f6894730 [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 -070085
David Benjamin338fcaf2014-12-11 01:20:52 -050086const SSL3_ENC_METHOD DTLSv1_enc_data = {
Adam Langley71d8a082014-12-13 16:28:18 -080087 tls1_enc,
David Benjamin41ac9792014-12-23 10:41:06 -050088 tls1_prf,
Adam Langley71d8a082014-12-13 16:28:18 -080089 tls1_setup_key_block,
90 tls1_generate_master_secret,
91 tls1_change_cipher_state,
92 tls1_final_finish_mac,
Adam Langley71d8a082014-12-13 16:28:18 -080093 tls1_cert_verify_mac,
94 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
95 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
96 tls1_alert_code,
97 tls1_export_keying_material,
98 SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV,
Adam Langley71d8a082014-12-13 16:28:18 -080099};
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,
David Benjamin41ac9792014-12-23 10:41:06 -0500103 tls1_prf,
Adam Langley71d8a082014-12-13 16:28:18 -0800104 tls1_setup_key_block,
105 tls1_generate_master_secret,
106 tls1_change_cipher_state,
107 tls1_final_finish_mac,
Adam Langley71d8a082014-12-13 16:28:18 -0800108 tls1_cert_verify_mac,
109 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
110 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
111 tls1_alert_code,
112 tls1_export_keying_material,
113 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS |
114 SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
Adam Langley71d8a082014-12-13 16:28:18 -0800115};
Adam Langley95c29f32014-06-20 12:00:00 -0700116
Adam Langley71d8a082014-12-13 16:28:18 -0800117int dtls1_new(SSL *s) {
118 DTLS1_STATE *d1;
Adam Langley95c29f32014-06-20 12:00:00 -0700119
Adam Langley71d8a082014-12-13 16:28:18 -0800120 if (!ssl3_new(s)) {
121 return 0;
122 }
123 d1 = OPENSSL_malloc(sizeof *d1);
124 if (d1 == NULL) {
125 ssl3_free(s);
126 return 0;
127 }
128 memset(d1, 0, sizeof *d1);
Adam Langley95c29f32014-06-20 12:00:00 -0700129
Adam Langley71d8a082014-12-13 16:28:18 -0800130 d1->unprocessed_rcds.q = pqueue_new();
131 d1->processed_rcds.q = pqueue_new();
132 d1->buffered_messages = pqueue_new();
133 d1->sent_messages = pqueue_new();
134 d1->buffered_app_data.q = pqueue_new();
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langley71d8a082014-12-13 16:28:18 -0800136 if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q ||
137 !d1->buffered_messages || !d1->sent_messages ||
138 !d1->buffered_app_data.q) {
139 if (d1->unprocessed_rcds.q) {
140 pqueue_free(d1->unprocessed_rcds.q);
141 }
142 if (d1->processed_rcds.q) {
143 pqueue_free(d1->processed_rcds.q);
144 }
145 if (d1->buffered_messages) {
146 pqueue_free(d1->buffered_messages);
147 }
148 if (d1->sent_messages) {
149 pqueue_free(d1->sent_messages);
150 }
151 if (d1->buffered_app_data.q) {
152 pqueue_free(d1->buffered_app_data.q);
153 }
154 OPENSSL_free(d1);
155 ssl3_free(s);
156 return 0;
157 }
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langley71d8a082014-12-13 16:28:18 -0800159 s->d1 = d1;
David Benjamin62fd1622015-01-11 13:30:01 -0500160
161 /* Set the version to the highest version for DTLS. This controls the initial
162 * state of |s->enc_method| and what the API reports as the version prior to
163 * negotiation.
164 *
165 * TODO(davidben): This is fragile and confusing. */
166 s->version = DTLS1_2_VERSION;
Adam Langley71d8a082014-12-13 16:28:18 -0800167 return 1;
168}
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley71d8a082014-12-13 16:28:18 -0800170static void dtls1_clear_queues(SSL *s) {
171 pitem *item = NULL;
172 hm_fragment *frag = NULL;
173 DTLS1_RECORD_DATA *rdata;
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langley71d8a082014-12-13 16:28:18 -0800175 while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
176 rdata = (DTLS1_RECORD_DATA *)item->data;
177 if (rdata->rbuf.buf) {
178 OPENSSL_free(rdata->rbuf.buf);
179 }
180 OPENSSL_free(item->data);
181 pitem_free(item);
182 }
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley71d8a082014-12-13 16:28:18 -0800184 while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
185 rdata = (DTLS1_RECORD_DATA *)item->data;
186 if (rdata->rbuf.buf) {
187 OPENSSL_free(rdata->rbuf.buf);
188 }
189 OPENSSL_free(item->data);
190 pitem_free(item);
191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley71d8a082014-12-13 16:28:18 -0800193 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
194 frag = (hm_fragment *)item->data;
195 dtls1_hm_fragment_free(frag);
196 pitem_free(item);
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langley71d8a082014-12-13 16:28:18 -0800199 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
200 frag = (hm_fragment *)item->data;
201 dtls1_hm_fragment_free(frag);
202 pitem_free(item);
203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langley71d8a082014-12-13 16:28:18 -0800205 while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
206 rdata = (DTLS1_RECORD_DATA *)item->data;
207 if (rdata->rbuf.buf) {
208 OPENSSL_free(rdata->rbuf.buf);
209 }
210 OPENSSL_free(item->data);
211 pitem_free(item);
212 }
213}
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley71d8a082014-12-13 16:28:18 -0800215void dtls1_free(SSL *s) {
216 ssl3_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin62fd1622015-01-11 13:30:01 -0500218 if (s == NULL || s->d1 == NULL) {
219 return;
220 }
221
Adam Langley71d8a082014-12-13 16:28:18 -0800222 dtls1_clear_queues(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langley71d8a082014-12-13 16:28:18 -0800224 pqueue_free(s->d1->unprocessed_rcds.q);
225 pqueue_free(s->d1->processed_rcds.q);
226 pqueue_free(s->d1->buffered_messages);
227 pqueue_free(s->d1->sent_messages);
228 pqueue_free(s->d1->buffered_app_data.q);
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langley71d8a082014-12-13 16:28:18 -0800230 OPENSSL_free(s->d1);
231 s->d1 = NULL;
232}
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley71d8a082014-12-13 16:28:18 -0800234long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) {
235 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langley71d8a082014-12-13 16:28:18 -0800237 switch (cmd) {
238 case DTLS_CTRL_GET_TIMEOUT:
239 if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) {
240 ret = 1;
241 }
242 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langley71d8a082014-12-13 16:28:18 -0800244 case DTLS_CTRL_HANDLE_TIMEOUT:
245 ret = dtls1_handle_timeout(s);
246 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langley71d8a082014-12-13 16:28:18 -0800248 default:
249 ret = ssl3_ctrl(s, cmd, larg, parg);
250 break;
251 }
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langley71d8a082014-12-13 16:28:18 -0800253 return ret;
254}
255
Adam Langley71d8a082014-12-13 16:28:18 -0800256const SSL_CIPHER *dtls1_get_cipher(unsigned int u) {
257 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
David Benjamine95d20d2014-12-23 11:16:01 -0500258 /* DTLS does not support stream ciphers. */
259 if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) {
260 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800261 }
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langley71d8a082014-12-13 16:28:18 -0800263 return ciph;
264}
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langley71d8a082014-12-13 16:28:18 -0800266void dtls1_start_timer(SSL *s) {
267 /* If timer is not set, initialize duration with 1 second */
268 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
269 s->d1->timeout_duration = 1;
270 }
Adam Langley95c29f32014-06-20 12:00:00 -0700271
Adam Langley71d8a082014-12-13 16:28:18 -0800272 /* Set timeout to current time */
David Benjamin377fc312015-01-26 00:22:12 -0500273 get_current_time(s, &s->d1->next_timeout);
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langley71d8a082014-12-13 16:28:18 -0800275 /* Add duration to current time */
276 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
277 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
278 &s->d1->next_timeout);
279}
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langley71d8a082014-12-13 16:28:18 -0800281static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) {
282 OPENSSL_timeval timenow;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langley71d8a082014-12-13 16:28:18 -0800284 /* If no timeout is set, just return NULL */
285 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
286 return NULL;
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288
Adam Langley71d8a082014-12-13 16:28:18 -0800289 /* Get current time */
David Benjamin377fc312015-01-26 00:22:12 -0500290 get_current_time(s, &timenow);
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley71d8a082014-12-13 16:28:18 -0800292 /* If timer already expired, set remaining time to 0 */
293 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
294 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
295 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
296 memset(timeleft, 0, sizeof(OPENSSL_timeval));
297 return timeleft;
298 }
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley71d8a082014-12-13 16:28:18 -0800300 /* Calculate time left until timer expires */
301 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
302 timeleft->tv_sec -= timenow.tv_sec;
303 timeleft->tv_usec -= timenow.tv_usec;
304 if (timeleft->tv_usec < 0) {
305 timeleft->tv_sec--;
306 timeleft->tv_usec += 1000000;
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 /* If remaining time is less than 15 ms, set it to 0 to prevent issues
310 * because of small devergences with socket timeouts. */
311 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
312 memset(timeleft, 0, sizeof(OPENSSL_timeval));
313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langley71d8a082014-12-13 16:28:18 -0800315 return timeleft;
316}
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley71d8a082014-12-13 16:28:18 -0800318int dtls1_is_timer_expired(SSL *s) {
319 OPENSSL_timeval timeleft;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley71d8a082014-12-13 16:28:18 -0800321 /* Get time left until timeout, return false if no timer running */
322 if (dtls1_get_timeout(s, &timeleft) == NULL) {
323 return 0;
324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325
Adam Langley71d8a082014-12-13 16:28:18 -0800326 /* Return false if timer is not expired yet */
327 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
328 return 0;
329 }
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley71d8a082014-12-13 16:28:18 -0800331 /* Timer expired, so return true */
332 return 1;
333}
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langley71d8a082014-12-13 16:28:18 -0800335void dtls1_double_timeout(SSL *s) {
336 s->d1->timeout_duration *= 2;
337 if (s->d1->timeout_duration > 60) {
338 s->d1->timeout_duration = 60;
339 }
340 dtls1_start_timer(s);
341}
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langley71d8a082014-12-13 16:28:18 -0800343void dtls1_stop_timer(SSL *s) {
344 /* Reset everything */
David Benjamine33b9b02015-01-26 01:27:22 -0500345 s->d1->num_timeouts = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800346 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
347 s->d1->timeout_duration = 1;
348 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
349 &s->d1->next_timeout);
350 /* Clear retransmission buffer */
351 dtls1_clear_record_buffer(s);
352}
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langley71d8a082014-12-13 16:28:18 -0800354int dtls1_check_timeout_num(SSL *s) {
David Benjamine33b9b02015-01-26 01:27:22 -0500355 s->d1->num_timeouts++;
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langley71d8a082014-12-13 16:28:18 -0800357 /* Reduce MTU after 2 unsuccessful retransmissions */
David Benjamine33b9b02015-01-26 01:27:22 -0500358 if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
David Benjamin7f18b132015-01-11 17:36:21 -0500359 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500360 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
361 NULL);
362 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
363 s->d1->mtu = (unsigned)mtu;
364 }
Adam Langley71d8a082014-12-13 16:28:18 -0800365 }
Adam Langley95c29f32014-06-20 12:00:00 -0700366
David Benjamine33b9b02015-01-26 01:27:22 -0500367 if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800368 /* fail the connection, enough alerts have been sent */
369 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
370 return -1;
371 }
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langley71d8a082014-12-13 16:28:18 -0800373 return 0;
374}
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley71d8a082014-12-13 16:28:18 -0800376int dtls1_handle_timeout(SSL *s) {
377 /* if no timer is expired, don't do anything */
378 if (!dtls1_is_timer_expired(s)) {
379 return 0;
380 }
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley71d8a082014-12-13 16:28:18 -0800382 dtls1_double_timeout(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langley71d8a082014-12-13 16:28:18 -0800384 if (dtls1_check_timeout_num(s) < 0) {
385 return -1;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley71d8a082014-12-13 16:28:18 -0800388 dtls1_start_timer(s);
389 return dtls1_retransmit_buffered_messages(s);
390}
391
David Benjamin377fc312015-01-26 00:22:12 -0500392static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) {
393 if (ssl->ctx->current_time_cb != NULL) {
394 ssl->ctx->current_time_cb(ssl, out_clock);
395 return;
396 }
397
Adam Langleyded93582014-07-31 15:23:51 -0700398#if defined(OPENSSL_WINDOWS)
Adam Langley71d8a082014-12-13 16:28:18 -0800399 struct _timeb time;
400 _ftime(&time);
David Benjamin377fc312015-01-26 00:22:12 -0500401 out_clock->tv_sec = time.time;
402 out_clock->tv_usec = time.millitm * 1000;
Adam Langley95c29f32014-06-20 12:00:00 -0700403#else
David Benjamin377fc312015-01-26 00:22:12 -0500404 gettimeofday(out_clock, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700405#endif
406}
407
David Benjamin2fa83de2015-02-08 01:40:08 -0500408int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) {
David Benjamine4824e82014-12-14 19:44:34 -0500409 uint8_t *message = (uint8_t *)s->init_buf->data;
410 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
411 uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH];
412 uint8_t *p = serialised_header;
413
David Benjamin16d031a2014-12-14 18:52:44 -0500414 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
415 s->d1->next_handshake_write_seq++;
416
417 dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800418 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
419 s->init_off = 0;
David Benjamin16d031a2014-12-14 18:52:44 -0500420
Adam Langley71d8a082014-12-13 16:28:18 -0800421 /* Buffer the message to handle re-xmits */
422 dtls1_buffer_message(s, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjamine4824e82014-12-14 19:44:34 -0500424 /* Add the new message to the handshake hash. Serialize the message
425 * header as if it were a single fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800426 *p++ = msg_hdr->type;
427 l2n3(msg_hdr->msg_len, p);
428 s2n(msg_hdr->seq, p);
429 l2n3(0, p);
430 l2n3(msg_hdr->msg_len, p);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500431 return ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)) &&
432 ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len);
David Benjamine4824e82014-12-14 19:44:34 -0500433}
434
David Benjamin2fa83de2015-02-08 01:40:08 -0500435int dtls1_handshake_write(SSL *s) {
David Benjamine4824e82014-12-14 19:44:34 -0500436 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800437}