blob: 798deb090ed84788c284118169ef537742255537 [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>
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin17cf2cb2016-12-13 01:07:13 -0500126#include "../crypto/internal.h"
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 Benjamin86e95b82017-07-18 16:34:25 -0400130namespace bssl {
131
David Benjaminc11ea9422017-08-29 16:33:21 -0400132// TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
133// for these values? Notably, why is kMinMTU a function of the transport
134// protocol's overhead rather than, say, what's needed to hold a minimally-sized
135// handshake fragment plus protocol overhead.
Adam Langley95c29f32014-06-20 12:00:00 -0700136
David Benjaminc11ea9422017-08-29 16:33:21 -0400137// kMinMTU is the minimum acceptable MTU value.
David Benjamina18b6712015-01-11 19:31:22 -0500138static const unsigned int kMinMTU = 256 - 28;
139
David Benjaminc11ea9422017-08-29 16:33:21 -0400140// kDefaultMTU is the default MTU value to use if neither the user nor
141// the underlying BIO supplies one.
David Benjamina18b6712015-01-11 19:31:22 -0500142static const unsigned int kDefaultMTU = 1500 - 28;
143
David Benjamin9d632f42016-06-23 17:57:19 -0400144
David Benjaminc11ea9422017-08-29 16:33:21 -0400145// Receiving handshake messages.
David Benjamin9d632f42016-06-23 17:57:19 -0400146
David Benjaminec847ce2016-06-17 19:30:47 -0400147static void dtls1_hm_fragment_free(hm_fragment *frag) {
148 if (frag == NULL) {
149 return;
150 }
David Benjamin528bd262016-07-08 09:34:05 -0700151 OPENSSL_free(frag->data);
David Benjaminec847ce2016-06-17 19:30:47 -0400152 OPENSSL_free(frag->reassembly);
153 OPENSSL_free(frag);
154}
155
David Benjamin528bd262016-07-08 09:34:05 -0700156static hm_fragment *dtls1_hm_fragment_new(const struct hm_header_st *msg_hdr) {
David Benjamin1386aad2017-07-19 23:57:40 -0400157 ScopedCBB cbb;
David Benjamine8703a32017-07-09 16:17:55 -0400158 hm_fragment *frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800159 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400160 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800161 return NULL;
162 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500163 OPENSSL_memset(frag, 0, sizeof(hm_fragment));
David Benjamin528bd262016-07-08 09:34:05 -0700164 frag->type = msg_hdr->type;
165 frag->seq = msg_hdr->seq;
166 frag->msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjaminc11ea9422017-08-29 16:33:21 -0400168 // Allocate space for the reassembled message and fill in the header.
David Benjamine8703a32017-07-09 16:17:55 -0400169 frag->data =
170 (uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len);
David Benjamin528bd262016-07-08 09:34:05 -0700171 if (frag->data == NULL) {
172 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
173 goto err;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin1386aad2017-07-19 23:57:40 -0400176 if (!CBB_init_fixed(cbb.get(), frag->data, DTLS1_HM_HEADER_LENGTH) ||
177 !CBB_add_u8(cbb.get(), msg_hdr->type) ||
178 !CBB_add_u24(cbb.get(), msg_hdr->msg_len) ||
179 !CBB_add_u16(cbb.get(), msg_hdr->seq) ||
180 !CBB_add_u24(cbb.get(), 0 /* frag_off */) ||
181 !CBB_add_u24(cbb.get(), msg_hdr->msg_len) ||
182 !CBB_finish(cbb.get(), NULL, NULL)) {
David Benjamin528bd262016-07-08 09:34:05 -0700183 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
184 goto err;
185 }
186
David Benjaminc11ea9422017-08-29 16:33:21 -0400187 // If the handshake message is empty, |frag->reassembly| is NULL.
David Benjamin528bd262016-07-08 09:34:05 -0700188 if (msg_hdr->msg_len > 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400189 // Initialize reassembly bitmask.
David Benjamin528bd262016-07-08 09:34:05 -0700190 if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) {
David Benjamin29a83c52016-06-17 19:12:54 -0400191 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
192 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800193 }
David Benjamin528bd262016-07-08 09:34:05 -0700194 size_t bitmask_len = (msg_hdr->msg_len + 7) / 8;
David Benjamine8703a32017-07-09 16:17:55 -0400195 frag->reassembly = (uint8_t *)OPENSSL_malloc(bitmask_len);
David Benjamin29a83c52016-06-17 19:12:54 -0400196 if (frag->reassembly == NULL) {
197 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
198 goto err;
199 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500200 OPENSSL_memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langley71d8a082014-12-13 16:28:18 -0800203 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400204
205err:
206 dtls1_hm_fragment_free(frag);
207 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800208}
Adam Langley95c29f32014-06-20 12:00:00 -0700209
David Benjaminc11ea9422017-08-29 16:33:21 -0400210// bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
211// exclusive, set.
David Benjamindca125e2016-06-23 17:52:47 -0400212static uint8_t bit_range(size_t start, size_t end) {
David Benjaminee562b92015-03-02 20:52:52 -0500213 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
214}
215
David Benjaminc11ea9422017-08-29 16:33:21 -0400216// dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
217// as received in |frag|. If |frag| becomes complete, it clears
218// |frag->reassembly|. The range must be within the bounds of |frag|'s message
219// and |frag->reassembly| must not be NULL.
David Benjaminee562b92015-03-02 20:52:52 -0500220static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
221 size_t end) {
David Benjamin528bd262016-07-08 09:34:05 -0700222 size_t msg_len = frag->msg_len;
David Benjaminee562b92015-03-02 20:52:52 -0500223
224 if (frag->reassembly == NULL || start > end || end > msg_len) {
225 assert(0);
226 return;
227 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400228 // A zero-length message will never have a pending reassembly.
David Benjaminee562b92015-03-02 20:52:52 -0500229 assert(msg_len > 0);
230
David Benjamine51fb0f2017-09-07 11:51:46 -0400231 if (start == end) {
232 return;
233 }
234
David Benjaminee562b92015-03-02 20:52:52 -0500235 if ((start >> 3) == (end >> 3)) {
236 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
237 } else {
238 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
David Benjamin54091232016-09-05 12:47:25 -0400239 for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500240 frag->reassembly[i] = 0xff;
241 }
242 if ((end & 7) != 0) {
243 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
244 }
245 }
246
David Benjaminc11ea9422017-08-29 16:33:21 -0400247 // Check if the fragment is complete.
David Benjamin54091232016-09-05 12:47:25 -0400248 for (size_t i = 0; i < (msg_len >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500249 if (frag->reassembly[i] != 0xff) {
250 return;
251 }
252 }
253 if ((msg_len & 7) != 0 &&
254 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
255 return;
256 }
257
258 OPENSSL_free(frag->reassembly);
259 frag->reassembly = NULL;
260}
261
David Benjamin97250f42017-10-07 04:12:35 -0400262// dtls1_is_current_message_complete returns whether the current handshake
263// message is complete.
264static bool dtls1_is_current_message_complete(const SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400265 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
266 SSL_MAX_HANDSHAKE_FLIGHT];
267 return frag != NULL && frag->reassembly == NULL;
268}
269
David Benjaminc11ea9422017-08-29 16:33:21 -0400270// dtls1_get_incoming_message returns the incoming message corresponding to
271// |msg_hdr|. If none exists, it creates a new one and inserts it in the
272// queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
273// returns NULL on failure. The caller does not take ownership of the result.
David Benjamin9d632f42016-06-23 17:57:19 -0400274static hm_fragment *dtls1_get_incoming_message(
David Benjamind9229f92017-10-06 17:36:20 -0400275 SSL *ssl, uint8_t *out_alert, const struct hm_header_st *msg_hdr) {
David Benjamin9d632f42016-06-23 17:57:19 -0400276 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
277 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
David Benjamind9229f92017-10-06 17:36:20 -0400278 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin9d632f42016-06-23 17:57:19 -0400279 return NULL;
280 }
281
282 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
283 hm_fragment *frag = ssl->d1->incoming_messages[idx];
284 if (frag != NULL) {
David Benjamin528bd262016-07-08 09:34:05 -0700285 assert(frag->seq == msg_hdr->seq);
David Benjaminc11ea9422017-08-29 16:33:21 -0400286 // The new fragment must be compatible with the previous fragments from this
287 // message.
David Benjamin528bd262016-07-08 09:34:05 -0700288 if (frag->type != msg_hdr->type ||
289 frag->msg_len != msg_hdr->msg_len) {
David Benjamin9d632f42016-06-23 17:57:19 -0400290 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
David Benjamind9229f92017-10-06 17:36:20 -0400291 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin9d632f42016-06-23 17:57:19 -0400292 return NULL;
293 }
294 return frag;
295 }
296
David Benjaminc11ea9422017-08-29 16:33:21 -0400297 // This is the first fragment from this message.
David Benjamin528bd262016-07-08 09:34:05 -0700298 frag = dtls1_hm_fragment_new(msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400299 if (frag == NULL) {
David Benjamind9229f92017-10-06 17:36:20 -0400300 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin9d632f42016-06-23 17:57:19 -0400301 return NULL;
302 }
David Benjamin9d632f42016-06-23 17:57:19 -0400303 ssl->d1->incoming_messages[idx] = frag;
304 return frag;
305}
306
David Benjamind9229f92017-10-06 17:36:20 -0400307ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
308 uint8_t *out_alert, Span<uint8_t> in) {
309 uint8_t type;
310 Span<uint8_t> record;
311 auto ret = dtls_open_record(ssl, &type, &record, out_consumed, out_alert, in);
312 if (ret != ssl_open_record_success) {
313 return ret;
David Benjamin9d632f42016-06-23 17:57:19 -0400314 }
315
David Benjamind9229f92017-10-06 17:36:20 -0400316 switch (type) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400317 case SSL3_RT_APPLICATION_DATA:
David Benjaminc11ea9422017-08-29 16:33:21 -0400318 // Unencrypted application data records are always illegal.
David Benjaminb0c761e2017-06-25 22:42:55 -0400319 if (ssl->s3->aead_read_ctx->is_null_cipher()) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400320 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400321 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
322 return ssl_open_record_error;
David Benjaminb0c761e2017-06-25 22:42:55 -0400323 }
David Benjamin9d632f42016-06-23 17:57:19 -0400324
David Benjaminc11ea9422017-08-29 16:33:21 -0400325 // Out-of-order application data may be received between ChangeCipherSpec
326 // and finished. Discard it.
David Benjamind9229f92017-10-06 17:36:20 -0400327 return ssl_open_record_discard;
David Benjaminb0c761e2017-06-25 22:42:55 -0400328
329 case SSL3_RT_CHANGE_CIPHER_SPEC:
David Benjaminc11ea9422017-08-29 16:33:21 -0400330 // We do not support renegotiation, so encrypted ChangeCipherSpec records
331 // are illegal.
David Benjaminb0c761e2017-06-25 22:42:55 -0400332 if (!ssl->s3->aead_read_ctx->is_null_cipher()) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400333 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400334 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
335 return ssl_open_record_error;
David Benjaminb0c761e2017-06-25 22:42:55 -0400336 }
337
David Benjamind9229f92017-10-06 17:36:20 -0400338 if (record.size() != 1u || record[0] != SSL3_MT_CCS) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400339 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
David Benjamind9229f92017-10-06 17:36:20 -0400340 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
341 return ssl_open_record_error;
David Benjaminb0c761e2017-06-25 22:42:55 -0400342 }
343
David Benjaminc11ea9422017-08-29 16:33:21 -0400344 // Flag the ChangeCipherSpec for later.
David Benjaminb0c761e2017-06-25 22:42:55 -0400345 ssl->d1->has_change_cipher_spec = true;
346 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC,
David Benjamind9229f92017-10-06 17:36:20 -0400347 record);
348 return ssl_open_record_success;
David Benjaminb0c761e2017-06-25 22:42:55 -0400349
350 case SSL3_RT_HANDSHAKE:
David Benjaminc11ea9422017-08-29 16:33:21 -0400351 // Break out to main processing.
David Benjaminb0c761e2017-06-25 22:42:55 -0400352 break;
353
354 default:
David Benjaminb0c761e2017-06-25 22:42:55 -0400355 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400356 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
357 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400358 }
359
360 CBS cbs;
David Benjamind9229f92017-10-06 17:36:20 -0400361 CBS_init(&cbs, record.data(), record.size());
David Benjamin9d632f42016-06-23 17:57:19 -0400362 while (CBS_len(&cbs) > 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400363 // Read a handshake fragment.
David Benjamin9d632f42016-06-23 17:57:19 -0400364 struct hm_header_st msg_hdr;
365 CBS body;
366 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
367 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400368 *out_alert = SSL_AD_DECODE_ERROR;
369 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400370 }
371
372 const size_t frag_off = msg_hdr.frag_off;
373 const size_t frag_len = msg_hdr.frag_len;
374 const size_t msg_len = msg_hdr.msg_len;
375 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
376 frag_off + frag_len > msg_len ||
377 msg_len > ssl_max_handshake_message_len(ssl)) {
378 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
David Benjamind9229f92017-10-06 17:36:20 -0400379 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
380 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400381 }
382
David Benjaminc11ea9422017-08-29 16:33:21 -0400383 // The encrypted epoch in DTLS has only one handshake message.
David Benjamin02edcd02016-07-27 17:40:37 -0400384 if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) {
385 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400386 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
387 return ssl_open_record_error;
David Benjamin02edcd02016-07-27 17:40:37 -0400388 }
389
David Benjamin9d632f42016-06-23 17:57:19 -0400390 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
391 msg_hdr.seq >
392 (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400393 // Ignore fragments from the past, or ones too far in the future.
David Benjamin9d632f42016-06-23 17:57:19 -0400394 continue;
395 }
396
David Benjamind9229f92017-10-06 17:36:20 -0400397 hm_fragment *frag = dtls1_get_incoming_message(ssl, out_alert, &msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400398 if (frag == NULL) {
David Benjamind9229f92017-10-06 17:36:20 -0400399 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400400 }
David Benjamin528bd262016-07-08 09:34:05 -0700401 assert(frag->msg_len == msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400402
403 if (frag->reassembly == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400404 // The message is already assembled.
David Benjamin9d632f42016-06-23 17:57:19 -0400405 continue;
406 }
407 assert(msg_len > 0);
408
David Benjaminc11ea9422017-08-29 16:33:21 -0400409 // Copy the body into the fragment.
David Benjamin1a999cf2017-01-03 10:30:35 -0500410 OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off,
411 CBS_data(&body), CBS_len(&body));
David Benjamin9d632f42016-06-23 17:57:19 -0400412 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
413 }
414
David Benjamind9229f92017-10-06 17:36:20 -0400415 return ssl_open_record_success;
David Benjamin9d632f42016-06-23 17:57:19 -0400416}
417
David Benjamin7934f082017-08-01 16:32:25 -0400418bool dtls1_get_message(SSL *ssl, SSLMessage *out) {
419 if (!dtls1_is_current_message_complete(ssl)) {
420 return false;
David Benjamin9d632f42016-06-23 17:57:19 -0400421 }
422
David Benjamin528bd262016-07-08 09:34:05 -0700423 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
424 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin7934f082017-08-01 16:32:25 -0400425 out->type = frag->type;
426 CBS_init(&out->body, frag->data + DTLS1_HM_HEADER_LENGTH, frag->msg_len);
427 CBS_init(&out->raw, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len);
428 out->is_v2_hello = false;
429 if (!ssl->s3->has_message) {
David Benjamin03a4b962017-10-06 17:54:10 -0400430 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw);
David Benjamin046bc1f2017-08-31 15:06:42 -0400431 ssl->s3->has_message = true;
David Benjamin78b8b992017-08-01 18:38:41 -0400432 }
David Benjamin7934f082017-08-01 16:32:25 -0400433 return true;
David Benjamin9d632f42016-06-23 17:57:19 -0400434}
435
David Benjamin8f94c312017-08-01 17:35:55 -0400436void dtls1_next_message(SSL *ssl) {
David Benjamin7934f082017-08-01 16:32:25 -0400437 assert(ssl->s3->has_message);
David Benjamin4497e582016-07-27 17:51:49 -0400438 assert(dtls1_is_current_message_complete(ssl));
439 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
440 dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]);
441 ssl->d1->incoming_messages[index] = NULL;
442 ssl->d1->handshake_read_seq++;
David Benjamin046bc1f2017-08-31 15:06:42 -0400443 ssl->s3->has_message = false;
David Benjaminc11ea9422017-08-29 16:33:21 -0400444 // If we previously sent a flight, mark it as having a reply, so
445 // |on_handshake_complete| can manage post-handshake retransmission.
David Benjamin302b8182017-08-22 14:47:22 -0700446 if (ssl->d1->outgoing_messages_complete) {
447 ssl->d1->flight_has_reply = true;
448 }
David Benjamin4497e582016-07-27 17:51:49 -0400449}
450
David Benjamin9d632f42016-06-23 17:57:19 -0400451void dtls_clear_incoming_messages(SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400452 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin9d632f42016-06-23 17:57:19 -0400453 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
454 ssl->d1->incoming_messages[i] = NULL;
455 }
456}
457
David Benjamin40e94702017-10-06 18:26:36 -0400458bool dtls_has_unprocessed_handshake_data(const SSL *ssl) {
459 if (ssl->d1->has_change_cipher_spec) {
460 return true;
461 }
462
David Benjamin61672812016-07-14 23:10:43 -0400463 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
464 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400465 // Skip the current message.
David Benjamin7934f082017-08-01 16:32:25 -0400466 if (ssl->s3->has_message && i == current) {
David Benjamin4497e582016-07-27 17:51:49 -0400467 assert(dtls1_is_current_message_complete(ssl));
468 continue;
469 }
470 if (ssl->d1->incoming_messages[i] != NULL) {
David Benjamin40e94702017-10-06 18:26:36 -0400471 return true;
David Benjamin61672812016-07-14 23:10:43 -0400472 }
473 }
David Benjamin40e94702017-10-06 18:26:36 -0400474 return false;
David Benjamin61672812016-07-14 23:10:43 -0400475}
476
David Benjamin97250f42017-10-07 04:12:35 -0400477bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
478 CBS *out_body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500479 OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st));
David Benjamin9d632f42016-06-23 17:57:19 -0400480
481 if (!CBS_get_u8(cbs, &out_hdr->type) ||
482 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
483 !CBS_get_u16(cbs, &out_hdr->seq) ||
484 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
485 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
486 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
David Benjamin97250f42017-10-07 04:12:35 -0400487 return false;
David Benjamin9d632f42016-06-23 17:57:19 -0400488 }
489
David Benjamin97250f42017-10-07 04:12:35 -0400490 return true;
David Benjamin9d632f42016-06-23 17:57:19 -0400491}
492
David Benjamind9229f92017-10-06 17:36:20 -0400493ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
494 uint8_t *out_alert,
495 Span<uint8_t> in) {
496 if (!ssl->d1->has_change_cipher_spec) {
497 // dtls1_open_handshake processes both handshake and ChangeCipherSpec.
498 auto ret = dtls1_open_handshake(ssl, out_consumed, out_alert, in);
499 if (ret != ssl_open_record_success) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400500 return ret;
501 }
502 }
David Benjamind9229f92017-10-06 17:36:20 -0400503 if (ssl->d1->has_change_cipher_spec) {
504 ssl->d1->has_change_cipher_spec = false;
505 return ssl_open_record_success;
506 }
507 return ssl_open_record_discard;
David Benjaminb0c761e2017-06-25 22:42:55 -0400508}
509
David Benjamin9d632f42016-06-23 17:57:19 -0400510
David Benjaminc11ea9422017-08-29 16:33:21 -0400511// Sending handshake messages.
David Benjamin9d632f42016-06-23 17:57:19 -0400512
David Benjamin75836432016-06-17 18:48:29 -0400513void dtls_clear_outgoing_messages(SSL *ssl) {
David Benjamin54091232016-09-05 12:47:25 -0400514 for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) {
David Benjamin75836432016-06-17 18:48:29 -0400515 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
516 ssl->d1->outgoing_messages[i].data = NULL;
517 }
518 ssl->d1->outgoing_messages_len = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -0500519 ssl->d1->outgoing_written = 0;
520 ssl->d1->outgoing_offset = 0;
David Benjamin9bbdf582017-08-02 19:46:29 -0400521 ssl->d1->outgoing_messages_complete = false;
David Benjamin302b8182017-08-22 14:47:22 -0700522 ssl->d1->flight_has_reply = false;
David Benjamin75836432016-06-17 18:48:29 -0400523}
524
David Benjamin97250f42017-10-07 04:12:35 -0400525bool dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400526 // Pick a modest size hint to save most of the |realloc| calls.
David Benjamin75836432016-06-17 18:48:29 -0400527 if (!CBB_init(cbb, 64) ||
528 !CBB_add_u8(cbb, type) ||
529 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
530 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
531 !CBB_add_u24(cbb, 0 /* offset */) ||
532 !CBB_add_u24_length_prefixed(cbb, body)) {
David Benjamin97250f42017-10-07 04:12:35 -0400533 return false;
David Benjamin75836432016-06-17 18:48:29 -0400534 }
535
David Benjamin97250f42017-10-07 04:12:35 -0400536 return true;
David Benjamin75836432016-06-17 18:48:29 -0400537}
538
David Benjamin97250f42017-10-07 04:12:35 -0400539bool dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) {
David Benjamin879efc32017-09-21 11:20:53 -0400540 if (!CBBFinishArray(cbb, out_msg) ||
541 out_msg->size() < DTLS1_HM_HEADER_LENGTH) {
David Benjamin75836432016-06-17 18:48:29 -0400542 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400543 return false;
David Benjamin75836432016-06-17 18:48:29 -0400544 }
545
David Benjaminc11ea9422017-08-29 16:33:21 -0400546 // Fix up the header. Copy the fragment length into the total message
547 // length.
David Benjamin879efc32017-09-21 11:20:53 -0400548 OPENSSL_memcpy(out_msg->data() + 1,
549 out_msg->data() + DTLS1_HM_HEADER_LENGTH - 3, 3);
David Benjamin97250f42017-10-07 04:12:35 -0400550 return true;
Steven Valdez5eead162016-11-11 22:23:25 -0500551}
David Benjamin75836432016-06-17 18:48:29 -0400552
David Benjaminc11ea9422017-08-29 16:33:21 -0400553// add_outgoing adds a new handshake message or ChangeCipherSpec to the current
David Benjamin97250f42017-10-07 04:12:35 -0400554// outgoing flight. It returns true on success and false on error.
555static bool add_outgoing(SSL *ssl, int is_ccs, Array<uint8_t> data) {
David Benjamin9bbdf582017-08-02 19:46:29 -0400556 if (ssl->d1->outgoing_messages_complete) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400557 // If we've begun writing a new flight, we received the peer flight. Discard
558 // the timer and the our flight.
David Benjamin9bbdf582017-08-02 19:46:29 -0400559 dtls1_stop_timer(ssl);
560 dtls_clear_outgoing_messages(ssl);
561 }
562
David Benjamina3d76d02017-07-14 19:36:07 -0400563 static_assert(SSL_MAX_HANDSHAKE_FLIGHT <
564 (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)),
565 "outgoing_messages_len is too small");
David Benjamin879efc32017-09-21 11:20:53 -0400566 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT ||
567 data.size() > 0xffffffff) {
David Benjamin97250f42017-10-07 04:12:35 -0400568 assert(false);
David Benjamin1a999cf2017-01-03 10:30:35 -0500569 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400570 return false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500571 }
David Benjamin75836432016-06-17 18:48:29 -0400572
David Benjamin1a999cf2017-01-03 10:30:35 -0500573 if (!is_ccs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400574 // TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript
575 // on hs.
Steven Valdez908ac192017-01-12 13:17:07 -0500576 if (ssl->s3->hs != NULL &&
David Benjamin75a1f232017-10-11 17:19:19 -0400577 !ssl->s3->hs->transcript.Update(data)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500578 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400579 return false;
Steven Valdez908ac192017-01-12 13:17:07 -0500580 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500581 ssl->d1->handshake_write_seq++;
582 }
583
584 DTLS_OUTGOING_MESSAGE *msg =
585 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
David Benjamin879efc32017-09-21 11:20:53 -0400586 size_t len;
587 data.Release(&msg->data, &len);
David Benjamin1a999cf2017-01-03 10:30:35 -0500588 msg->len = len;
589 msg->epoch = ssl->d1->w_epoch;
590 msg->is_ccs = is_ccs;
591
592 ssl->d1->outgoing_messages_len++;
David Benjamin97250f42017-10-07 04:12:35 -0400593 return true;
David Benjamin1a999cf2017-01-03 10:30:35 -0500594}
595
David Benjamin97250f42017-10-07 04:12:35 -0400596bool dtls1_add_message(SSL *ssl, Array<uint8_t> data) {
David Benjamin879efc32017-09-21 11:20:53 -0400597 return add_outgoing(ssl, 0 /* handshake */, std::move(data));
David Benjamin75836432016-06-17 18:48:29 -0400598}
599
David Benjamin97250f42017-10-07 04:12:35 -0400600bool dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin879efc32017-09-21 11:20:53 -0400601 return add_outgoing(ssl, 1 /* ChangeCipherSpec */, Array<uint8_t>());
David Benjamin1a999cf2017-01-03 10:30:35 -0500602}
603
David Benjamin97250f42017-10-07 04:12:35 -0400604bool dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400605 // The |add_alert| path is only used for warning alerts for now, which DTLS
606 // never sends. This will be implemented later once closure alerts are
607 // converted.
David Benjamin97250f42017-10-07 04:12:35 -0400608 assert(false);
David Benjamindaf207a2017-01-03 18:37:41 -0500609 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400610 return false;
David Benjamindaf207a2017-01-03 18:37:41 -0500611}
612
David Benjaminc11ea9422017-08-29 16:33:21 -0400613// dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above
614// the minimum.
David Benjamin1a999cf2017-01-03 10:30:35 -0500615static void dtls1_update_mtu(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400616 // TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the
617 // only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use
618 // |SSL_set_mtu|. Does this need to be so complex?
David Benjamin1a999cf2017-01-03 10:30:35 -0500619 if (ssl->d1->mtu < dtls1_min_mtu() &&
620 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
621 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
622 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
623 ssl->d1->mtu = (unsigned)mtu;
624 } else {
625 ssl->d1->mtu = kDefaultMTU;
626 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
627 }
628 }
629
David Benjaminc11ea9422017-08-29 16:33:21 -0400630 // The MTU should be above the minimum now.
David Benjamin1a999cf2017-01-03 10:30:35 -0500631 assert(ssl->d1->mtu >= dtls1_min_mtu());
632}
633
634enum seal_result_t {
635 seal_error,
636 seal_no_progress,
637 seal_partial,
638 seal_success,
639};
640
David Benjaminc11ea9422017-08-29 16:33:21 -0400641// seal_next_message seals |msg|, which must be the next message, to |out|. If
642// progress was made, it returns |seal_partial| or |seal_success| and sets
643// |*out_len| to the number of bytes written.
David Benjamin1a999cf2017-01-03 10:30:35 -0500644static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
645 size_t *out_len, size_t max_out,
646 const DTLS_OUTGOING_MESSAGE *msg) {
647 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
648 assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
649
David Benjamin3e3090d2015-04-05 12:48:30 -0400650 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400651 if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400652 use_epoch = dtls1_use_previous_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400653 } else if (msg->epoch != ssl->d1->w_epoch) {
654 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
655 return seal_error;
Adam Langley71d8a082014-12-13 16:28:18 -0800656 }
David Benjamincfc11c22017-07-18 22:45:18 -0400657
David Benjamin1a999cf2017-01-03 10:30:35 -0500658 size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
659 size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800660
David Benjamin29a83c52016-06-17 19:12:54 -0400661 if (msg->is_ccs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400662 // Check there is room for the ChangeCipherSpec.
David Benjamin1a999cf2017-01-03 10:30:35 -0500663 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
664 if (max_out < sizeof(kChangeCipherSpec) + overhead) {
665 return seal_no_progress;
666 }
667
668 if (!dtls_seal_record(ssl, out, out_len, max_out,
669 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
670 sizeof(kChangeCipherSpec), use_epoch)) {
671 return seal_error;
672 }
673
674 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
David Benjaminc64d1232017-10-04 18:14:28 -0400675 kChangeCipherSpec);
David Benjamin1a999cf2017-01-03 10:30:35 -0500676 return seal_success;
David Benjamina97b7372015-11-03 14:54:39 -0500677 }
678
David Benjaminc11ea9422017-08-29 16:33:21 -0400679 // DTLS messages are serialized as a single fragment in |msg|.
David Benjamin1a999cf2017-01-03 10:30:35 -0500680 CBS cbs, body;
681 struct hm_header_st hdr;
682 CBS_init(&cbs, msg->data, msg->len);
683 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
684 hdr.frag_off != 0 ||
685 hdr.frag_len != CBS_len(&body) ||
686 hdr.msg_len != CBS_len(&body) ||
687 !CBS_skip(&body, ssl->d1->outgoing_offset) ||
688 CBS_len(&cbs) != 0) {
689 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
690 return seal_error;
691 }
692
David Benjaminc11ea9422017-08-29 16:33:21 -0400693 // Determine how much progress can be made.
David Benjamin1a999cf2017-01-03 10:30:35 -0500694 if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) {
695 return seal_no_progress;
696 }
697 size_t todo = CBS_len(&body);
698 if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) {
699 todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead;
700 }
701
David Benjaminc11ea9422017-08-29 16:33:21 -0400702 // Assemble a fragment, to be sealed in-place.
David Benjamin1386aad2017-07-19 23:57:40 -0400703 ScopedCBB cbb;
David Benjamin1a999cf2017-01-03 10:30:35 -0500704 uint8_t *frag = out + prefix;
705 size_t max_frag = max_out - prefix, frag_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400706 if (!CBB_init_fixed(cbb.get(), frag, max_frag) ||
707 !CBB_add_u8(cbb.get(), hdr.type) ||
708 !CBB_add_u24(cbb.get(), hdr.msg_len) ||
709 !CBB_add_u16(cbb.get(), hdr.seq) ||
710 !CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) ||
711 !CBB_add_u24(cbb.get(), todo) ||
712 !CBB_add_bytes(cbb.get(), CBS_data(&body), todo) ||
713 !CBB_finish(cbb.get(), NULL, &frag_len)) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500714 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
715 return seal_error;
716 }
717
David Benjaminc64d1232017-10-04 18:14:28 -0400718 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE,
719 MakeSpan(frag, frag_len));
David Benjamin1a999cf2017-01-03 10:30:35 -0500720
721 if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
722 out + prefix, frag_len, use_epoch)) {
723 return seal_error;
724 }
725
726 if (todo == CBS_len(&body)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400727 // The next message is complete.
David Benjamin1a999cf2017-01-03 10:30:35 -0500728 ssl->d1->outgoing_offset = 0;
729 return seal_success;
730 }
731
732 ssl->d1->outgoing_offset += todo;
733 return seal_partial;
Adam Langley71d8a082014-12-13 16:28:18 -0800734}
Adam Langley95c29f32014-06-20 12:00:00 -0700735
David Benjaminc11ea9422017-08-29 16:33:21 -0400736// seal_next_packet writes as much of the next flight as possible to |out| and
737// advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as
738// appropriate.
David Benjamin97250f42017-10-07 04:12:35 -0400739static bool seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len,
740 size_t max_out) {
741 bool made_progress = false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500742 size_t total = 0;
743 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
744 for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len;
745 ssl->d1->outgoing_written++) {
746 const DTLS_OUTGOING_MESSAGE *msg =
747 &ssl->d1->outgoing_messages[ssl->d1->outgoing_written];
748 size_t len;
749 enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg);
750 switch (ret) {
751 case seal_error:
David Benjamin97250f42017-10-07 04:12:35 -0400752 return false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500753
754 case seal_no_progress:
755 goto packet_full;
756
757 case seal_partial:
758 case seal_success:
759 out += len;
760 max_out -= len;
761 total += len;
David Benjamin97250f42017-10-07 04:12:35 -0400762 made_progress = true;
David Benjamin1a999cf2017-01-03 10:30:35 -0500763
764 if (ret == seal_partial) {
765 goto packet_full;
766 }
767 break;
768 }
David Benjamin30152fd2016-05-05 20:45:48 -0400769 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500770
771packet_full:
David Benjaminc11ea9422017-08-29 16:33:21 -0400772 // The MTU was too small to make any progress.
David Benjamin1a999cf2017-01-03 10:30:35 -0500773 if (!made_progress) {
774 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamin97250f42017-10-07 04:12:35 -0400775 return false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500776 }
777
778 *out_len = total;
David Benjamin97250f42017-10-07 04:12:35 -0400779 return true;
David Benjamin1a999cf2017-01-03 10:30:35 -0500780}
781
David Benjamin9bbdf582017-08-02 19:46:29 -0400782static int send_flight(SSL *ssl) {
David Benjamine8d07462017-10-12 18:09:20 -0400783 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
784 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
785 return -1;
786 }
787
David Benjamin1a999cf2017-01-03 10:30:35 -0500788 dtls1_update_mtu(ssl);
Adam Langleybcc4e232015-02-19 15:51:58 -0800789
David Benjamin30152fd2016-05-05 20:45:48 -0400790 int ret = -1;
David Benjamine8703a32017-07-09 16:17:55 -0400791 uint8_t *packet = (uint8_t *)OPENSSL_malloc(ssl->d1->mtu);
David Benjamin1a999cf2017-01-03 10:30:35 -0500792 if (packet == NULL) {
793 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
794 goto err;
795 }
796
797 while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) {
798 uint8_t old_written = ssl->d1->outgoing_written;
799 uint32_t old_offset = ssl->d1->outgoing_offset;
800
801 size_t packet_len;
802 if (!seal_next_packet(ssl, packet, &packet_len, ssl->d1->mtu)) {
803 goto err;
804 }
805
806 int bio_ret = BIO_write(ssl->wbio, packet, packet_len);
807 if (bio_ret <= 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400808 // Retry this packet the next time around.
David Benjamin1a999cf2017-01-03 10:30:35 -0500809 ssl->d1->outgoing_written = old_written;
810 ssl->d1->outgoing_offset = old_offset;
811 ssl->rwstate = SSL_WRITING;
812 ret = bio_ret;
David Benjamin30152fd2016-05-05 20:45:48 -0400813 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800814 }
815 }
816
David Benjamin42e3e192017-01-27 10:06:07 -0500817 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin27309552016-05-05 21:17:53 -0400818 ssl->rwstate = SSL_WRITING;
David Benjamin42e3e192017-01-27 10:06:07 -0500819 goto err;
David Benjamin27309552016-05-05 21:17:53 -0400820 }
David Benjamin30152fd2016-05-05 20:45:48 -0400821
David Benjamin42e3e192017-01-27 10:06:07 -0500822 ret = 1;
823
David Benjamin30152fd2016-05-05 20:45:48 -0400824err:
David Benjamin1a999cf2017-01-03 10:30:35 -0500825 OPENSSL_free(packet);
David Benjamin30152fd2016-05-05 20:45:48 -0400826 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800827}
828
David Benjamin9bbdf582017-08-02 19:46:29 -0400829int dtls1_flush_flight(SSL *ssl) {
830 ssl->d1->outgoing_messages_complete = true;
David Benjaminc11ea9422017-08-29 16:33:21 -0400831 // Start the retransmission timer for the next flight (if any).
David Benjamin9bbdf582017-08-02 19:46:29 -0400832 dtls1_start_timer(ssl);
833 return send_flight(ssl);
834}
835
David Benjamin1a999cf2017-01-03 10:30:35 -0500836int dtls1_retransmit_outgoing_messages(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400837 // Rewind to the start of the flight and write it again.
838 //
839 // TODO(davidben): This does not allow retransmits to be resumed on
840 // non-blocking write.
David Benjamin1a999cf2017-01-03 10:30:35 -0500841 ssl->d1->outgoing_written = 0;
842 ssl->d1->outgoing_offset = 0;
843
David Benjamin9bbdf582017-08-02 19:46:29 -0400844 return send_flight(ssl);
David Benjamina97b7372015-11-03 14:54:39 -0500845}
846
Adam Langley71d8a082014-12-13 16:28:18 -0800847unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500848 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800849}
David Benjamin86e95b82017-07-18 16:34:25 -0400850
851} // namespace bssl