blob: d6f4e7be0ac1f4a59d19b9c67667608179667afa [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) 1998-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 */
58/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
59 * All rights reserved.
60 *
61 * This package is an SSL implementation written
62 * by Eric Young (eay@cryptsoft.com).
63 * The implementation was written so as to conform with Netscapes SSL.
64 *
65 * This library is free for commercial and non-commercial use as long as
66 * the following conditions are aheared to. The following conditions
67 * apply to all code found in this distribution, be it the RC4, RSA,
68 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
69 * included with this distribution is covered by the same copyright terms
70 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
71 *
72 * Copyright remains Eric Young's, and as such any Copyright notices in
73 * the code are not to be removed.
74 * If this package is used in a product, Eric Young should be given attribution
75 * as the author of the parts of the library used.
76 * This can be in the form of a textual message at program startup or
77 * in documentation (online or textual) provided with the package.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
81 * are met:
82 * 1. Redistributions of source code must retain the copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * "This product includes cryptographic software written by
90 * Eric Young (eay@cryptsoft.com)"
91 * The word 'cryptographic' can be left out if the rouines from the library
92 * being used are not cryptographic related :-).
93 * 4. If you include any Windows specific code (or a derivative thereof) from
94 * the apps directory (application code) you must include an acknowledgement:
95 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
96 *
97 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 * SUCH DAMAGE.
108 *
109 * The licence and distribution terms for any publically available version or
110 * derivative of this code cannot be changed. i.e. this code cannot simply be
111 * copied and put under another distribution licence
112 * [including the GNU Public Licence.] */
113
David Benjamin9e4e01e2015-09-15 01:48:04 -0400114#include <openssl/ssl.h>
115
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <assert.h>
117#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118#include <string.h>
119
120#include <openssl/buf.h>
121#include <openssl/err.h>
122#include <openssl/evp.h>
123#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124#include <openssl/rand.h>
125#include <openssl/x509.h>
126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Adam Langley95c29f32014-06-20 12:00:00 -0700129
David Benjamina18b6712015-01-11 19:31:22 -0500130/* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
131 * for these values? Notably, why is kMinMTU a function of the transport
132 * protocol's overhead rather than, say, what's needed to hold a minimally-sized
133 * handshake fragment plus protocol overhead. */
Adam Langley95c29f32014-06-20 12:00:00 -0700134
David Benjamina18b6712015-01-11 19:31:22 -0500135/* kMinMTU is the minimum acceptable MTU value. */
136static const unsigned int kMinMTU = 256 - 28;
137
138/* kDefaultMTU is the default MTU value to use if neither the user nor
139 * the underlying BIO supplies one. */
140static const unsigned int kDefaultMTU = 1500 - 28;
141
David Benjaminec847ce2016-06-17 19:30:47 -0400142static void dtls1_hm_fragment_free(hm_fragment *frag) {
143 if (frag == NULL) {
144 return;
145 }
146 OPENSSL_free(frag->fragment);
147 OPENSSL_free(frag->reassembly);
148 OPENSSL_free(frag);
149}
150
David Benjamin29a83c52016-06-17 19:12:54 -0400151static hm_fragment *dtls1_hm_fragment_new(size_t frag_len) {
David Benjaminc99807d2015-09-12 18:21:35 -0400152 hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800153 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400154 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800155 return NULL;
156 }
David Benjaminc99807d2015-09-12 18:21:35 -0400157 memset(frag, 0, sizeof(hm_fragment));
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjaminc99807d2015-09-12 18:21:35 -0400159 /* If the handshake message is empty, |frag->fragment| and |frag->reassembly|
160 * are NULL. */
161 if (frag_len > 0) {
162 frag->fragment = OPENSSL_malloc(frag_len);
163 if (frag->fragment == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400164 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminc99807d2015-09-12 18:21:35 -0400165 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin29a83c52016-06-17 19:12:54 -0400168 /* Initialize reassembly bitmask. */
169 if (frag_len + 7 < frag_len) {
170 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
171 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800172 }
David Benjamin29a83c52016-06-17 19:12:54 -0400173 size_t bitmask_len = (frag_len + 7) / 8;
174 frag->reassembly = OPENSSL_malloc(bitmask_len);
175 if (frag->reassembly == NULL) {
176 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
177 goto err;
178 }
179 memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
Adam Langley71d8a082014-12-13 16:28:18 -0800182 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400183
184err:
185 dtls1_hm_fragment_free(frag);
186 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800187}
Adam Langley95c29f32014-06-20 12:00:00 -0700188
David Benjaminee562b92015-03-02 20:52:52 -0500189#if !defined(inline)
190#define inline __inline
191#endif
192
193/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
194 * exclusive, set. */
195static inline uint8_t bit_range(size_t start, size_t end) {
196 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
197}
198
199/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
200 * as received in |frag|. If |frag| becomes complete, it clears
201 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
202 * and |frag->reassembly| must not be NULL. */
203static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
204 size_t end) {
205 size_t i;
206 size_t msg_len = frag->msg_header.msg_len;
207
208 if (frag->reassembly == NULL || start > end || end > msg_len) {
209 assert(0);
210 return;
211 }
212 /* A zero-length message will never have a pending reassembly. */
213 assert(msg_len > 0);
214
215 if ((start >> 3) == (end >> 3)) {
216 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
217 } else {
218 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
219 for (i = (start >> 3) + 1; i < (end >> 3); i++) {
220 frag->reassembly[i] = 0xff;
221 }
222 if ((end & 7) != 0) {
223 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
224 }
225 }
226
227 /* Check if the fragment is complete. */
228 for (i = 0; i < (msg_len >> 3); i++) {
229 if (frag->reassembly[i] != 0xff) {
230 return;
231 }
232 }
233 if ((msg_len & 7) != 0 &&
234 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
235 return;
236 }
237
238 OPENSSL_free(frag->reassembly);
239 frag->reassembly = NULL;
240}
241
David Benjamina97b7372015-11-03 14:54:39 -0500242static void dtls1_update_mtu(SSL *ssl) {
243 /* TODO(davidben): What is this code doing and do we need it? */
244 if (ssl->d1->mtu < dtls1_min_mtu() &&
245 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin2f871122016-05-20 14:27:17 -0400246 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
David Benjamina97b7372015-11-03 14:54:39 -0500247 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
248 ssl->d1->mtu = (unsigned)mtu;
249 } else {
250 ssl->d1->mtu = kDefaultMTU;
David Benjamin2f871122016-05-20 14:27:17 -0400251 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
David Benjamina97b7372015-11-03 14:54:39 -0500252 }
253 }
254
255 /* The MTU should be above the minimum now. */
256 assert(ssl->d1->mtu >= dtls1_min_mtu());
257}
258
David Benjamin16285ea2015-11-03 15:39:45 -0500259/* dtls1_max_record_size returns the maximum record body length that may be
260 * written without exceeding the MTU. It accounts for any buffering installed on
261 * the write BIO. If no record may be written, it returns zero. */
262static size_t dtls1_max_record_size(SSL *ssl) {
263 size_t ret = ssl->d1->mtu;
264
265 size_t overhead = ssl_max_seal_overhead(ssl);
266 if (ret <= overhead) {
267 return 0;
268 }
269 ret -= overhead;
270
David Benjamin2f871122016-05-20 14:27:17 -0400271 size_t pending = BIO_wpending(ssl->wbio);
David Benjamin16285ea2015-11-03 15:39:45 -0500272 if (ret <= pending) {
273 return 0;
274 }
275 ret -= pending;
276
277 return ret;
278}
279
David Benjamina97b7372015-11-03 14:54:39 -0500280static int dtls1_write_change_cipher_spec(SSL *ssl,
281 enum dtls1_use_epoch_t use_epoch) {
282 dtls1_update_mtu(ssl);
283
284 /* During the handshake, wbio is buffered to pack messages together. Flush the
285 * buffer if the ChangeCipherSpec would not fit in a packet. */
David Benjamin16285ea2015-11-03 15:39:45 -0500286 if (dtls1_max_record_size(ssl) == 0) {
David Benjamin2f871122016-05-20 14:27:17 -0400287 int ret = BIO_flush(ssl->wbio);
David Benjamina97b7372015-11-03 14:54:39 -0500288 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500289 ssl->rwstate = SSL_WRITING;
David Benjamina97b7372015-11-03 14:54:39 -0500290 return ret;
291 }
David Benjamina97b7372015-11-03 14:54:39 -0500292 }
293
294 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
David Benjamin0d56f882015-12-19 17:05:56 -0500295 int ret =
David Benjamin45d45c12016-06-07 15:20:49 -0400296 dtls1_write_record(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
297 sizeof(kChangeCipherSpec), use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500298 if (ret <= 0) {
299 return ret;
300 }
301
David Benjamin4e9cc712016-06-01 20:16:03 -0400302 ssl_do_msg_callback(ssl, 1 /* write */, ssl->version,
303 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
304 sizeof(kChangeCipherSpec));
David Benjamina97b7372015-11-03 14:54:39 -0500305 return 1;
306}
307
David Benjamin75836432016-06-17 18:48:29 -0400308/* dtls1_do_handshake_write writes handshake message |in| using the given epoch,
309 * starting |offset| bytes into the message body. It returns one on success. On
310 * error, it returns <= 0 and sets |*out_offset| to the number of bytes of body
311 * that were successfully written. This may be used to retry the write
312 * later. |in| must be a reassembled handshake message with the full DTLS
313 * handshake header. */
314static int dtls1_do_handshake_write(SSL *ssl, size_t *out_offset,
315 const uint8_t *in, size_t offset,
316 size_t len,
317 enum dtls1_use_epoch_t use_epoch) {
David Benjamin16285ea2015-11-03 15:39:45 -0500318 dtls1_update_mtu(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700319
David Benjamin16285ea2015-11-03 15:39:45 -0500320 int ret = -1;
321 CBB cbb;
322 CBB_zero(&cbb);
323 /* Allocate a temporary buffer to hold the message fragments to avoid
324 * clobbering the message. */
325 uint8_t *buf = OPENSSL_malloc(ssl->d1->mtu);
326 if (buf == NULL) {
327 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjaminb5eb1952016-06-17 18:44:38 -0400330 /* Although it may be sent as multiple fragments, a DTLS message must be sent
331 * serialized as a single fragment for purposes of |ssl_do_msg_callback| and
332 * the handshake hash. */
333 CBS cbs, body;
334 struct hm_header_st hdr;
335 CBS_init(&cbs, in, len);
336 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
337 hdr.frag_off != 0 ||
338 hdr.frag_len != CBS_len(&body) ||
339 hdr.msg_len != CBS_len(&body) ||
340 !CBS_skip(&body, offset) ||
341 CBS_len(&cbs) != 0) {
342 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
343 goto err;
David Benjamin16285ea2015-11-03 15:39:45 -0500344 }
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin16285ea2015-11-03 15:39:45 -0500346 do {
347 /* During the handshake, wbio is buffered to pack messages together. Flush
348 * the buffer if there isn't enough room to make progress. */
349 if (dtls1_max_record_size(ssl) < DTLS1_HM_HEADER_LENGTH + 1) {
David Benjamin2f871122016-05-20 14:27:17 -0400350 int flush_ret = BIO_flush(ssl->wbio);
David Benjamin16285ea2015-11-03 15:39:45 -0500351 if (flush_ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500352 ssl->rwstate = SSL_WRITING;
David Benjamin16285ea2015-11-03 15:39:45 -0500353 ret = flush_ret;
354 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800355 }
David Benjamin2f871122016-05-20 14:27:17 -0400356 assert(BIO_wpending(ssl->wbio) == 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800357 }
Adam Langley95c29f32014-06-20 12:00:00 -0700358
David Benjamin16285ea2015-11-03 15:39:45 -0500359 size_t todo = dtls1_max_record_size(ssl);
360 if (todo < DTLS1_HM_HEADER_LENGTH + 1) {
David Benjamina97b7372015-11-03 14:54:39 -0500361 /* To make forward progress, the MTU must, at minimum, fit the handshake
362 * header and one byte of handshake body. */
363 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamin16285ea2015-11-03 15:39:45 -0500364 goto err;
365 }
366 todo -= DTLS1_HM_HEADER_LENGTH;
367
David Benjaminb5eb1952016-06-17 18:44:38 -0400368 if (todo > CBS_len(&body)) {
369 todo = CBS_len(&body);
David Benjamin16285ea2015-11-03 15:39:45 -0500370 }
371 if (todo >= (1u << 24)) {
372 todo = (1u << 24) - 1;
David Benjamina97b7372015-11-03 14:54:39 -0500373 }
374
David Benjaminb5eb1952016-06-17 18:44:38 -0400375 size_t buf_len;
David Benjamin16285ea2015-11-03 15:39:45 -0500376 if (!CBB_init_fixed(&cbb, buf, ssl->d1->mtu) ||
David Benjaminb5eb1952016-06-17 18:44:38 -0400377 !CBB_add_u8(&cbb, hdr.type) ||
378 !CBB_add_u24(&cbb, hdr.msg_len) ||
379 !CBB_add_u16(&cbb, hdr.seq) ||
380 !CBB_add_u24(&cbb, offset) ||
David Benjamin16285ea2015-11-03 15:39:45 -0500381 !CBB_add_u24(&cbb, todo) ||
David Benjaminb5eb1952016-06-17 18:44:38 -0400382 !CBB_add_bytes(&cbb, CBS_data(&body), todo) ||
383 !CBB_finish(&cbb, NULL, &buf_len)) {
David Benjamin16285ea2015-11-03 15:39:45 -0500384 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
385 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800386 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500387
David Benjamin45d45c12016-06-07 15:20:49 -0400388 int write_ret =
David Benjaminb5eb1952016-06-17 18:44:38 -0400389 dtls1_write_record(ssl, SSL3_RT_HANDSHAKE, buf, buf_len, use_epoch);
David Benjamin16285ea2015-11-03 15:39:45 -0500390 if (write_ret <= 0) {
391 ret = write_ret;
392 goto err;
David Benjamin5a3cc032015-01-11 19:25:32 -0500393 }
David Benjamin16285ea2015-11-03 15:39:45 -0500394
David Benjaminb5eb1952016-06-17 18:44:38 -0400395 if (!CBS_skip(&body, todo)) {
396 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
397 goto err;
398 }
399 offset += todo;
400 } while (CBS_len(&body) != 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800401
David Benjaminb5eb1952016-06-17 18:44:38 -0400402 ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_HANDSHAKE, in,
403 len);
David Benjamin16285ea2015-11-03 15:39:45 -0500404
405 ret = 1;
406
407err:
David Benjaminb5eb1952016-06-17 18:44:38 -0400408 *out_offset = offset;
David Benjamin16285ea2015-11-03 15:39:45 -0500409 CBB_cleanup(&cbb);
410 OPENSSL_free(buf);
411 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800412}
Adam Langley95c29f32014-06-20 12:00:00 -0700413
David Benjamin75836432016-06-17 18:48:29 -0400414void dtls_clear_outgoing_messages(SSL *ssl) {
415 size_t i;
416 for (i = 0; i < ssl->d1->outgoing_messages_len; i++) {
417 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
418 ssl->d1->outgoing_messages[i].data = NULL;
419 }
420 ssl->d1->outgoing_messages_len = 0;
421}
422
423/* dtls1_buffer_change_cipher_spec adds a ChangeCipherSpec to the current
424 * handshake flight. */
425static int dtls1_buffer_change_cipher_spec(SSL *ssl) {
426 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
427 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
428 return 0;
429 }
430
431 DTLS_OUTGOING_MESSAGE *msg =
432 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
433 msg->data = NULL;
434 msg->len = 0;
435 msg->epoch = ssl->d1->w_epoch;
436 msg->is_ccs = 1;
437
438 ssl->d1->outgoing_messages_len++;
439 return 1;
440}
441
442static int dtls1_buffer_message(SSL *ssl, uint8_t *data, size_t len) {
443 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
444 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
445 OPENSSL_free(data);
446 return 0;
447 }
448
449 DTLS_OUTGOING_MESSAGE *msg =
450 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
451 msg->data = data;
452 msg->len = len;
453 msg->epoch = ssl->d1->w_epoch;
454 msg->is_ccs = 0;
455
456 ssl->d1->outgoing_messages_len++;
457 return 1;
458}
459
460int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
461 /* Pick a modest size hint to save most of the |realloc| calls. */
462 if (!CBB_init(cbb, 64) ||
463 !CBB_add_u8(cbb, type) ||
464 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
465 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
466 !CBB_add_u24(cbb, 0 /* offset */) ||
467 !CBB_add_u24_length_prefixed(cbb, body)) {
468 return 0;
469 }
470
471 return 1;
472}
473
474int dtls1_finish_message(SSL *ssl, CBB *cbb) {
475 uint8_t *msg = NULL;
476 size_t len;
477 if (!CBB_finish(cbb, &msg, &len) ||
478 len > 0xffffffffu ||
479 len < DTLS1_HM_HEADER_LENGTH) {
480 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
481 OPENSSL_free(msg);
482 return 0;
483 }
484
485 /* Fix up the header. Copy the fragment length into the total message
486 * length. */
487 memcpy(msg + 1, msg + DTLS1_HM_HEADER_LENGTH - 3, 3);
488
489 ssl3_update_handshake_hash(ssl, msg, len);
490
491 ssl->d1->handshake_write_seq++;
492 ssl->init_off = 0;
493 return dtls1_buffer_message(ssl, msg, len);
494}
495
496int dtls1_write_message(SSL *ssl) {
497 if (ssl->d1->outgoing_messages_len == 0) {
498 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
499 return -1;
500 }
501
502 const DTLS_OUTGOING_MESSAGE *msg =
503 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len - 1];
504 if (msg->is_ccs) {
505 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
506 return -1;
507 }
508
509 size_t offset = ssl->init_off;
510 int ret = dtls1_do_handshake_write(ssl, &offset, msg->data, offset, msg->len,
511 dtls1_use_current_epoch);
512 ssl->init_off = offset;
513 return ret;
514}
515
David Benjamin75381222015-03-02 19:30:30 -0500516/* dtls1_is_next_message_complete returns one if the next handshake message is
517 * complete and zero otherwise. */
David Benjamin0d56f882015-12-19 17:05:56 -0500518static int dtls1_is_next_message_complete(SSL *ssl) {
David Benjaminec847ce2016-06-17 19:30:47 -0400519 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
520 SSL_MAX_HANDSHAKE_FLIGHT];
521 return frag != NULL && frag->reassembly == NULL;
David Benjamin75381222015-03-02 19:30:30 -0500522}
523
David Benjamin75381222015-03-02 19:30:30 -0500524/* dtls1_get_buffered_message returns the buffered message corresponding to
525 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
526 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
527 * returns NULL on failure. The caller does not take ownership of the result. */
528static hm_fragment *dtls1_get_buffered_message(
David Benjamin0d56f882015-12-19 17:05:56 -0500529 SSL *ssl, const struct hm_header_st *msg_hdr) {
David Benjaminec847ce2016-06-17 19:30:47 -0400530 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
531 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
532 return NULL;
533 }
David Benjamin75381222015-03-02 19:30:30 -0500534
David Benjaminec847ce2016-06-17 19:30:47 -0400535 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
536 hm_fragment *frag = ssl->d1->incoming_messages[idx];
537 if (frag != NULL) {
David Benjamin75381222015-03-02 19:30:30 -0500538 assert(frag->msg_header.seq == msg_hdr->seq);
David Benjaminec847ce2016-06-17 19:30:47 -0400539 /* The new fragment must be compatible with the previous fragments from this
540 * message. */
David Benjamin75381222015-03-02 19:30:30 -0500541 if (frag->msg_header.type != msg_hdr->type ||
542 frag->msg_header.msg_len != msg_hdr->msg_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400543 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
David Benjamin0d56f882015-12-19 17:05:56 -0500544 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin75381222015-03-02 19:30:30 -0500545 return NULL;
546 }
David Benjaminec847ce2016-06-17 19:30:47 -0400547 return frag;
David Benjamin75381222015-03-02 19:30:30 -0500548 }
David Benjaminec847ce2016-06-17 19:30:47 -0400549
550 /* This is the first fragment from this message. */
551 frag = dtls1_hm_fragment_new(msg_hdr->msg_len);
552 if (frag == NULL) {
553 return NULL;
554 }
555 memcpy(&frag->msg_header, msg_hdr, sizeof(*msg_hdr));
556 ssl->d1->incoming_messages[idx] = frag;
David Benjamin75381222015-03-02 19:30:30 -0500557 return frag;
558}
559
David Benjaminc6604172016-06-02 16:38:35 -0400560/* dtls1_process_handshake_record reads a handshake record and processes it. It
561 * returns one if the record was successfully processed and 0 or -1 on error. */
562static int dtls1_process_handshake_record(SSL *ssl) {
563 SSL3_RECORD *rr = &ssl->s3->rrec;
564
565start:
566 if (rr->length == 0) {
567 int ret = dtls1_get_record(ssl);
568 if (ret <= 0) {
569 return ret;
570 }
David Benjamin75381222015-03-02 19:30:30 -0500571 }
David Benjaminc6604172016-06-02 16:38:35 -0400572
573 /* Cross-epoch records are discarded, but we may receive out-of-order
574 * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
575 * before the appropriate point in the handshake. Those must be silently
576 * discarded.
577 *
578 * However, only allow the out-of-order records in the correct epoch.
579 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
580 * the unencrypted epoch (we never renegotiate). Other cases fall through and
581 * fail with a fatal error. */
582 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
583 ssl->s3->aead_read_ctx != NULL) ||
584 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
585 ssl->s3->aead_read_ctx == NULL)) {
586 rr->length = 0;
587 goto start;
588 }
589
590 if (rr->type != SSL3_RT_HANDSHAKE) {
David Benjamin0d56f882015-12-19 17:05:56 -0500591 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjaminc6604172016-06-02 16:38:35 -0400592 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamin75381222015-03-02 19:30:30 -0500593 return -1;
594 }
595
David Benjaminc6604172016-06-02 16:38:35 -0400596 CBS cbs;
597 CBS_init(&cbs, rr->data, rr->length);
David Benjamin75381222015-03-02 19:30:30 -0500598
David Benjaminc6604172016-06-02 16:38:35 -0400599 while (CBS_len(&cbs) > 0) {
600 /* Read a handshake fragment. */
601 struct hm_header_st msg_hdr;
602 CBS body;
603 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
604 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
605 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin75381222015-03-02 19:30:30 -0500606 return -1;
607 }
David Benjamin75381222015-03-02 19:30:30 -0500608
David Benjaminc6604172016-06-02 16:38:35 -0400609 const size_t frag_off = msg_hdr.frag_off;
610 const size_t frag_len = msg_hdr.frag_len;
611 const size_t msg_len = msg_hdr.msg_len;
612 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
613 frag_off + frag_len > msg_len ||
614 msg_len > ssl_max_handshake_message_len(ssl)) {
615 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
616 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin75381222015-03-02 19:30:30 -0500617 return -1;
618 }
David Benjamin75381222015-03-02 19:30:30 -0500619
David Benjaminc6604172016-06-02 16:38:35 -0400620 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
621 msg_hdr.seq >
David Benjamin29a83c52016-06-17 19:12:54 -0400622 (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) {
David Benjaminc6604172016-06-02 16:38:35 -0400623 /* Ignore fragments from the past, or ones too far in the future. */
624 continue;
625 }
David Benjamin75381222015-03-02 19:30:30 -0500626
David Benjaminc6604172016-06-02 16:38:35 -0400627 hm_fragment *frag = dtls1_get_buffered_message(ssl, &msg_hdr);
628 if (frag == NULL) {
629 return -1;
630 }
631 assert(frag->msg_header.msg_len == msg_len);
632
633 if (frag->reassembly == NULL) {
634 /* The message is already assembled. */
635 continue;
636 }
637 assert(msg_len > 0);
638
639 /* Copy the body into the fragment. */
640 memcpy(frag->fragment + frag_off, CBS_data(&body), CBS_len(&body));
641 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
642 }
643
644 rr->length = 0;
645 ssl_read_buffer_discard(ssl);
David Benjamin75381222015-03-02 19:30:30 -0500646 return 1;
647}
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjaminbcb2d912015-02-24 23:45:43 -0500649/* dtls1_get_message reads a handshake message of message type |msg_type| (any
David Benjamin060cfb02016-05-12 00:43:05 -0400650 * if |msg_type| == -1). Read an entire handshake message. Handshake messages
651 * arrive in fragments. */
David Benjamin1e6d6df2016-05-13 18:28:17 -0400652long dtls1_get_message(SSL *ssl, int msg_type,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500653 enum ssl_hash_message_t hash_message, int *ok) {
David Benjamin75381222015-03-02 19:30:30 -0500654 hm_fragment *frag = NULL;
655 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langley71d8a082014-12-13 16:28:18 -0800657 /* s3->tmp is used to store messages that are unexpected, caused
658 * by the absence of an optional handshake message */
David Benjamin0d56f882015-12-19 17:05:56 -0500659 if (ssl->s3->tmp.reuse_message) {
David Benjamin5ca39fb2015-03-01 23:57:54 -0500660 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
661 * ssl_dont_hash_message would have to have been applied to the previous
662 * call. */
663 assert(hash_message == ssl_hash_message);
David Benjamin0d56f882015-12-19 17:05:56 -0500664 ssl->s3->tmp.reuse_message = 0;
665 if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800666 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley71d8a082014-12-13 16:28:18 -0800668 goto f_err;
669 }
670 *ok = 1;
David Benjamina6338be2016-05-13 18:12:19 -0400671 assert(ssl->init_buf->length >= DTLS1_HM_HEADER_LENGTH);
David Benjamin0d56f882015-12-19 17:05:56 -0500672 ssl->init_msg = (uint8_t *)ssl->init_buf->data + DTLS1_HM_HEADER_LENGTH;
David Benjamina6338be2016-05-13 18:12:19 -0400673 ssl->init_num = (int)ssl->init_buf->length - DTLS1_HM_HEADER_LENGTH;
David Benjamin0d56f882015-12-19 17:05:56 -0500674 return ssl->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800675 }
Adam Langley95c29f32014-06-20 12:00:00 -0700676
David Benjaminc6604172016-06-02 16:38:35 -0400677 /* Process handshake records until the next message is ready. */
David Benjamin0d56f882015-12-19 17:05:56 -0500678 while (!dtls1_is_next_message_complete(ssl)) {
David Benjaminc6604172016-06-02 16:38:35 -0400679 int ret = dtls1_process_handshake_record(ssl);
David Benjamin75381222015-03-02 19:30:30 -0500680 if (ret <= 0) {
681 *ok = 0;
682 return ret;
683 }
Adam Langley71d8a082014-12-13 16:28:18 -0800684 }
Adam Langley95c29f32014-06-20 12:00:00 -0700685
David Benjaminec847ce2016-06-17 19:30:47 -0400686 /* Pop an entry from the ring buffer. */
687 frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
688 SSL_MAX_HANDSHAKE_FLIGHT];
689 ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
690 SSL_MAX_HANDSHAKE_FLIGHT] = NULL;
691
692 assert(frag != NULL);
David Benjamin75381222015-03-02 19:30:30 -0500693 assert(frag->reassembly == NULL);
David Benjaminec847ce2016-06-17 19:30:47 -0400694 assert(ssl->d1->handshake_read_seq == frag->msg_header.seq);
695
696 ssl->d1->handshake_read_seq++;
David Benjamin75381222015-03-02 19:30:30 -0500697
David Benjamina8653202015-06-28 01:26:10 -0400698 /* Reconstruct the assembled message. */
David Benjamin75381222015-03-02 19:30:30 -0500699 CBB cbb;
David Benjamina8653202015-06-28 01:26:10 -0400700 CBB_zero(&cbb);
David Benjamina6338be2016-05-13 18:12:19 -0400701 if (!BUF_MEM_reserve(ssl->init_buf, (size_t)frag->msg_header.msg_len +
702 DTLS1_HM_HEADER_LENGTH) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500703 !CBB_init_fixed(&cbb, (uint8_t *)ssl->init_buf->data,
704 ssl->init_buf->max) ||
David Benjamina8653202015-06-28 01:26:10 -0400705 !CBB_add_u8(&cbb, frag->msg_header.type) ||
David Benjamin75381222015-03-02 19:30:30 -0500706 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
707 !CBB_add_u16(&cbb, frag->msg_header.seq) ||
708 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
709 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
710 !CBB_add_bytes(&cbb, frag->fragment, frag->msg_header.msg_len) ||
David Benjamina6338be2016-05-13 18:12:19 -0400711 !CBB_finish(&cbb, NULL, &ssl->init_buf->length)) {
David Benjamin75381222015-03-02 19:30:30 -0500712 CBB_cleanup(&cbb);
David Benjamin3570d732015-06-29 00:28:17 -0400713 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin75381222015-03-02 19:30:30 -0500714 goto err;
715 }
David Benjamina6338be2016-05-13 18:12:19 -0400716 assert(ssl->init_buf->length ==
717 (size_t)frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH);
David Benjamin75381222015-03-02 19:30:30 -0500718
David Benjamin75381222015-03-02 19:30:30 -0500719 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
720 * |ssl_get_message| API. */
David Benjamin0d56f882015-12-19 17:05:56 -0500721 ssl->s3->tmp.message_type = frag->msg_header.type;
David Benjamin0d56f882015-12-19 17:05:56 -0500722 ssl->init_msg = (uint8_t *)ssl->init_buf->data + DTLS1_HM_HEADER_LENGTH;
723 ssl->init_num = frag->msg_header.msg_len;
David Benjamin75381222015-03-02 19:30:30 -0500724
David Benjamin0d56f882015-12-19 17:05:56 -0500725 if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
David Benjaminbcb2d912015-02-24 23:45:43 -0500726 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400727 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminbcb2d912015-02-24 23:45:43 -0500728 goto f_err;
729 }
David Benjamin0d56f882015-12-19 17:05:56 -0500730 if (hash_message == ssl_hash_message && !ssl3_hash_current_message(ssl)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500731 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800732 }
David Benjamin4e9cc712016-06-01 20:16:03 -0400733
734 ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE,
735 ssl->init_buf->data,
736 ssl->init_num + DTLS1_HM_HEADER_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700737
David Benjamin75381222015-03-02 19:30:30 -0500738 dtls1_hm_fragment_free(frag);
Adam Langley95c29f32014-06-20 12:00:00 -0700739
David Benjamin75381222015-03-02 19:30:30 -0500740 *ok = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500741 return ssl->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700742
743f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500744 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500745err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400746 dtls1_hm_fragment_free(frag);
Adam Langley71d8a082014-12-13 16:28:18 -0800747 *ok = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800748 return -1;
749}
Adam Langley95c29f32014-06-20 12:00:00 -0700750
David Benjamin29a83c52016-06-17 19:12:54 -0400751static int dtls1_retransmit_message(SSL *ssl,
752 const DTLS_OUTGOING_MESSAGE *msg) {
David Benjaminafbc63f2015-02-01 02:33:59 -0500753 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
754 * (negotiated cipher) exist. */
David Benjamin0d56f882015-12-19 17:05:56 -0500755 assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1);
David Benjamin29a83c52016-06-17 19:12:54 -0400756 assert(msg->epoch <= ssl->d1->w_epoch);
David Benjamin3e3090d2015-04-05 12:48:30 -0400757 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamin29a83c52016-06-17 19:12:54 -0400758 if (ssl->d1->w_epoch == 1 && msg->epoch == 0) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400759 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800760 }
761
David Benjamina97b7372015-11-03 14:54:39 -0500762 /* TODO(davidben): This cannot handle non-blocking writes. */
763 int ret;
David Benjamin29a83c52016-06-17 19:12:54 -0400764 if (msg->is_ccs) {
David Benjamin0d56f882015-12-19 17:05:56 -0500765 ret = dtls1_write_change_cipher_spec(ssl, use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500766 } else {
David Benjaminb5eb1952016-06-17 18:44:38 -0400767 size_t offset = 0;
David Benjamin29a83c52016-06-17 19:12:54 -0400768 ret = dtls1_do_handshake_write(ssl, &offset, msg->data, offset, msg->len,
769 use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500770 }
771
Adam Langley71d8a082014-12-13 16:28:18 -0800772 return ret;
773}
Adam Langley95c29f32014-06-20 12:00:00 -0700774
David Benjamin0d56f882015-12-19 17:05:56 -0500775int dtls1_retransmit_buffered_messages(SSL *ssl) {
David Benjamin30152fd2016-05-05 20:45:48 -0400776 /* Ensure we are packing handshake messages. */
777 const int was_buffered = ssl_is_wbio_buffered(ssl);
778 assert(was_buffered == SSL_in_init(ssl));
David Benjaminb095f0f2016-05-05 21:50:24 -0400779 if (!was_buffered && !ssl_init_wbio_buffer(ssl)) {
David Benjamin30152fd2016-05-05 20:45:48 -0400780 return -1;
781 }
782 assert(ssl_is_wbio_buffered(ssl));
Adam Langleybcc4e232015-02-19 15:51:58 -0800783
David Benjamin30152fd2016-05-05 20:45:48 -0400784 int ret = -1;
David Benjamin29a83c52016-06-17 19:12:54 -0400785 size_t i;
786 for (i = 0; i < ssl->d1->outgoing_messages_len; i++) {
787 if (dtls1_retransmit_message(ssl, &ssl->d1->outgoing_messages[i]) <= 0) {
David Benjamin30152fd2016-05-05 20:45:48 -0400788 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800789 }
790 }
791
David Benjamin2f871122016-05-20 14:27:17 -0400792 ret = BIO_flush(ssl->wbio);
David Benjamin27309552016-05-05 21:17:53 -0400793 if (ret <= 0) {
794 ssl->rwstate = SSL_WRITING;
795 goto err;
796 }
David Benjamin30152fd2016-05-05 20:45:48 -0400797
798err:
799 if (!was_buffered) {
800 ssl_free_wbio_buffer(ssl);
801 }
802 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800803}
804
David Benjamin0d56f882015-12-19 17:05:56 -0500805int dtls1_send_change_cipher_spec(SSL *ssl, int a, int b) {
806 if (ssl->state == a) {
David Benjamin29a83c52016-06-17 19:12:54 -0400807 dtls1_buffer_change_cipher_spec(ssl);
David Benjamin0d56f882015-12-19 17:05:56 -0500808 ssl->state = b;
David Benjamina97b7372015-11-03 14:54:39 -0500809 }
810
David Benjamin0d56f882015-12-19 17:05:56 -0500811 return dtls1_write_change_cipher_spec(ssl, dtls1_use_current_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500812}
813
David Benjaminec847ce2016-06-17 19:30:47 -0400814void dtls_clear_incoming_messages(SSL *ssl) {
815 size_t i;
816 for (i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
817 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
818 ssl->d1->incoming_messages[i] = NULL;
819 }
820}
821
Adam Langley71d8a082014-12-13 16:28:18 -0800822unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500823 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800824}
Adam Langley95c29f32014-06-20 12:00:00 -0700825
David Benjaminc6604172016-06-02 16:38:35 -0400826int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
827 CBS *out_body) {
828 memset(out_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700829
David Benjaminc6604172016-06-02 16:38:35 -0400830 if (!CBS_get_u8(cbs, &out_hdr->type) ||
831 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
832 !CBS_get_u16(cbs, &out_hdr->seq) ||
833 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
834 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
835 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
836 return 0;
837 }
838
839 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800840}