blob: b910b96d9c9c63f93334e38f18be9bbcf6d6a8b6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/*
2 * DTLS implementation written by Nagendra Modadugu
Adam Langleye745b252018-02-26 14:02:17 -08003 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
Adam Langley95c29f32014-06-20 12:00:00 -07004 */
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
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/err.h>
121#include <openssl/evp.h>
122#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124
David Benjamin17cf2cb2016-12-13 01:07:13 -0500125#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400126#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700127
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700129BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400130
David Benjaminc11ea9422017-08-29 16:33:21 -0400131// TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
132// for these values? Notably, why is kMinMTU a function of the transport
133// protocol's overhead rather than, say, what's needed to hold a minimally-sized
134// handshake fragment plus protocol overhead.
Adam Langley95c29f32014-06-20 12:00:00 -0700135
David Benjaminc11ea9422017-08-29 16:33:21 -0400136// kMinMTU is the minimum acceptable MTU value.
David Benjamina18b6712015-01-11 19:31:22 -0500137static const unsigned int kMinMTU = 256 - 28;
138
David Benjaminc11ea9422017-08-29 16:33:21 -0400139// kDefaultMTU is the default MTU value to use if neither the user nor
140// the underlying BIO supplies one.
David Benjamina18b6712015-01-11 19:31:22 -0500141static const unsigned int kDefaultMTU = 1500 - 28;
142
David Benjamin9d632f42016-06-23 17:57:19 -0400143
David Benjaminc11ea9422017-08-29 16:33:21 -0400144// Receiving handshake messages.
David Benjamin9d632f42016-06-23 17:57:19 -0400145
David Benjamin049fdfc2017-10-17 01:12:53 -0400146hm_fragment::~hm_fragment() {
147 OPENSSL_free(data);
148 OPENSSL_free(reassembly);
David Benjaminec847ce2016-06-17 19:30:47 -0400149}
150
David Benjamin049fdfc2017-10-17 01:12:53 -0400151static UniquePtr<hm_fragment> dtls1_hm_fragment_new(
152 const struct hm_header_st *msg_hdr) {
David Benjamin1386aad2017-07-19 23:57:40 -0400153 ScopedCBB cbb;
David Benjamin049fdfc2017-10-17 01:12:53 -0400154 UniquePtr<hm_fragment> frag = MakeUnique<hm_fragment>();
155 if (!frag) {
156 return nullptr;
Adam Langley71d8a082014-12-13 16:28:18 -0800157 }
David Benjamin528bd262016-07-08 09:34:05 -0700158 frag->type = msg_hdr->type;
159 frag->seq = msg_hdr->seq;
160 frag->msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjaminc11ea9422017-08-29 16:33:21 -0400162 // Allocate space for the reassembled message and fill in the header.
David Benjamine8703a32017-07-09 16:17:55 -0400163 frag->data =
164 (uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len);
David Benjamin528bd262016-07-08 09:34:05 -0700165 if (frag->data == NULL) {
David Benjamin049fdfc2017-10-17 01:12:53 -0400166 return nullptr;
David Benjamin528bd262016-07-08 09:34:05 -0700167 }
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin1386aad2017-07-19 23:57:40 -0400169 if (!CBB_init_fixed(cbb.get(), frag->data, DTLS1_HM_HEADER_LENGTH) ||
170 !CBB_add_u8(cbb.get(), msg_hdr->type) ||
171 !CBB_add_u24(cbb.get(), msg_hdr->msg_len) ||
172 !CBB_add_u16(cbb.get(), msg_hdr->seq) ||
173 !CBB_add_u24(cbb.get(), 0 /* frag_off */) ||
174 !CBB_add_u24(cbb.get(), msg_hdr->msg_len) ||
175 !CBB_finish(cbb.get(), NULL, NULL)) {
David Benjamin049fdfc2017-10-17 01:12:53 -0400176 return nullptr;
David Benjamin528bd262016-07-08 09:34:05 -0700177 }
178
David Benjaminc11ea9422017-08-29 16:33:21 -0400179 // If the handshake message is empty, |frag->reassembly| is NULL.
David Benjamin528bd262016-07-08 09:34:05 -0700180 if (msg_hdr->msg_len > 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400181 // Initialize reassembly bitmask.
David Benjamin528bd262016-07-08 09:34:05 -0700182 if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) {
David Benjamin29a83c52016-06-17 19:12:54 -0400183 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin049fdfc2017-10-17 01:12:53 -0400184 return nullptr;
Adam Langley71d8a082014-12-13 16:28:18 -0800185 }
David Benjamin528bd262016-07-08 09:34:05 -0700186 size_t bitmask_len = (msg_hdr->msg_len + 7) / 8;
David Benjamindd68e4b2023-10-02 23:13:13 -0400187 frag->reassembly = (uint8_t *)OPENSSL_zalloc(bitmask_len);
David Benjamin29a83c52016-06-17 19:12:54 -0400188 if (frag->reassembly == NULL) {
David Benjamin049fdfc2017-10-17 01:12:53 -0400189 return nullptr;
David Benjamin29a83c52016-06-17 19:12:54 -0400190 }
Adam Langley71d8a082014-12-13 16:28:18 -0800191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley71d8a082014-12-13 16:28:18 -0800193 return frag;
194}
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjaminc11ea9422017-08-29 16:33:21 -0400196// bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
197// exclusive, set.
David Benjamindca125e2016-06-23 17:52:47 -0400198static uint8_t bit_range(size_t start, size_t end) {
David Benjaminee562b92015-03-02 20:52:52 -0500199 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
200}
201
David Benjaminc11ea9422017-08-29 16:33:21 -0400202// dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
203// as received in |frag|. If |frag| becomes complete, it clears
204// |frag->reassembly|. The range must be within the bounds of |frag|'s message
205// and |frag->reassembly| must not be NULL.
David Benjaminee562b92015-03-02 20:52:52 -0500206static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
207 size_t end) {
David Benjamin528bd262016-07-08 09:34:05 -0700208 size_t msg_len = frag->msg_len;
David Benjaminee562b92015-03-02 20:52:52 -0500209
210 if (frag->reassembly == NULL || start > end || end > msg_len) {
211 assert(0);
212 return;
213 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400214 // A zero-length message will never have a pending reassembly.
David Benjaminee562b92015-03-02 20:52:52 -0500215 assert(msg_len > 0);
216
David Benjamine51fb0f2017-09-07 11:51:46 -0400217 if (start == end) {
218 return;
219 }
220
David Benjaminee562b92015-03-02 20:52:52 -0500221 if ((start >> 3) == (end >> 3)) {
222 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
223 } else {
224 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
David Benjamin54091232016-09-05 12:47:25 -0400225 for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500226 frag->reassembly[i] = 0xff;
227 }
228 if ((end & 7) != 0) {
229 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
230 }
231 }
232
David Benjaminc11ea9422017-08-29 16:33:21 -0400233 // Check if the fragment is complete.
David Benjamin54091232016-09-05 12:47:25 -0400234 for (size_t i = 0; i < (msg_len >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500235 if (frag->reassembly[i] != 0xff) {
236 return;
237 }
238 }
239 if ((msg_len & 7) != 0 &&
240 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
241 return;
242 }
243
244 OPENSSL_free(frag->reassembly);
245 frag->reassembly = NULL;
246}
247
David Benjamin97250f42017-10-07 04:12:35 -0400248// dtls1_is_current_message_complete returns whether the current handshake
249// message is complete.
250static bool dtls1_is_current_message_complete(const SSL *ssl) {
David Benjamin800046f2017-10-17 01:37:51 -0400251 size_t idx = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
252 hm_fragment *frag = ssl->d1->incoming_messages[idx].get();
David Benjamin9d632f42016-06-23 17:57:19 -0400253 return frag != NULL && frag->reassembly == NULL;
254}
255
David Benjaminc11ea9422017-08-29 16:33:21 -0400256// dtls1_get_incoming_message returns the incoming message corresponding to
257// |msg_hdr|. If none exists, it creates a new one and inserts it in the
258// queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
259// returns NULL on failure. The caller does not take ownership of the result.
David Benjamin9d632f42016-06-23 17:57:19 -0400260static hm_fragment *dtls1_get_incoming_message(
David Benjamind9229f92017-10-06 17:36:20 -0400261 SSL *ssl, uint8_t *out_alert, const struct hm_header_st *msg_hdr) {
David Benjamin9d632f42016-06-23 17:57:19 -0400262 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
263 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
David Benjamind9229f92017-10-06 17:36:20 -0400264 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin9d632f42016-06-23 17:57:19 -0400265 return NULL;
266 }
267
268 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
David Benjamin800046f2017-10-17 01:37:51 -0400269 hm_fragment *frag = ssl->d1->incoming_messages[idx].get();
David Benjamin9d632f42016-06-23 17:57:19 -0400270 if (frag != NULL) {
David Benjamin528bd262016-07-08 09:34:05 -0700271 assert(frag->seq == msg_hdr->seq);
David Benjaminc11ea9422017-08-29 16:33:21 -0400272 // The new fragment must be compatible with the previous fragments from this
273 // message.
David Benjamin528bd262016-07-08 09:34:05 -0700274 if (frag->type != msg_hdr->type ||
275 frag->msg_len != msg_hdr->msg_len) {
David Benjamin9d632f42016-06-23 17:57:19 -0400276 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
David Benjamind9229f92017-10-06 17:36:20 -0400277 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin9d632f42016-06-23 17:57:19 -0400278 return NULL;
279 }
280 return frag;
281 }
282
David Benjaminc11ea9422017-08-29 16:33:21 -0400283 // This is the first fragment from this message.
David Benjamin800046f2017-10-17 01:37:51 -0400284 ssl->d1->incoming_messages[idx] = dtls1_hm_fragment_new(msg_hdr);
285 if (!ssl->d1->incoming_messages[idx]) {
David Benjamind9229f92017-10-06 17:36:20 -0400286 *out_alert = SSL_AD_INTERNAL_ERROR;
David Benjamin9d632f42016-06-23 17:57:19 -0400287 return NULL;
288 }
David Benjamin800046f2017-10-17 01:37:51 -0400289 return ssl->d1->incoming_messages[idx].get();
David Benjamin9d632f42016-06-23 17:57:19 -0400290}
291
David Benjamind9229f92017-10-06 17:36:20 -0400292ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed,
293 uint8_t *out_alert, Span<uint8_t> in) {
294 uint8_t type;
295 Span<uint8_t> record;
296 auto ret = dtls_open_record(ssl, &type, &record, out_consumed, out_alert, in);
297 if (ret != ssl_open_record_success) {
298 return ret;
David Benjamin9d632f42016-06-23 17:57:19 -0400299 }
300
David Benjamind9229f92017-10-06 17:36:20 -0400301 switch (type) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400302 case SSL3_RT_APPLICATION_DATA:
David Benjaminc11ea9422017-08-29 16:33:21 -0400303 // Unencrypted application data records are always illegal.
David Benjaminb0c761e2017-06-25 22:42:55 -0400304 if (ssl->s3->aead_read_ctx->is_null_cipher()) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400305 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400306 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
307 return ssl_open_record_error;
David Benjaminb0c761e2017-06-25 22:42:55 -0400308 }
David Benjamin9d632f42016-06-23 17:57:19 -0400309
David Benjaminc11ea9422017-08-29 16:33:21 -0400310 // Out-of-order application data may be received between ChangeCipherSpec
311 // and finished. Discard it.
David Benjamind9229f92017-10-06 17:36:20 -0400312 return ssl_open_record_discard;
David Benjaminb0c761e2017-06-25 22:42:55 -0400313
314 case SSL3_RT_CHANGE_CIPHER_SPEC:
David Benjaminc11ea9422017-08-29 16:33:21 -0400315 // We do not support renegotiation, so encrypted ChangeCipherSpec records
316 // are illegal.
David Benjaminb0c761e2017-06-25 22:42:55 -0400317 if (!ssl->s3->aead_read_ctx->is_null_cipher()) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400318 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400319 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
320 return ssl_open_record_error;
David Benjaminb0c761e2017-06-25 22:42:55 -0400321 }
322
David Benjamind9229f92017-10-06 17:36:20 -0400323 if (record.size() != 1u || record[0] != SSL3_MT_CCS) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400324 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
David Benjamind9229f92017-10-06 17:36:20 -0400325 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
326 return ssl_open_record_error;
David Benjaminb0c761e2017-06-25 22:42:55 -0400327 }
328
David Benjaminc11ea9422017-08-29 16:33:21 -0400329 // Flag the ChangeCipherSpec for later.
David Benjaminb0c761e2017-06-25 22:42:55 -0400330 ssl->d1->has_change_cipher_spec = true;
331 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC,
David Benjamind9229f92017-10-06 17:36:20 -0400332 record);
333 return ssl_open_record_success;
David Benjaminb0c761e2017-06-25 22:42:55 -0400334
335 case SSL3_RT_HANDSHAKE:
David Benjaminc11ea9422017-08-29 16:33:21 -0400336 // Break out to main processing.
David Benjaminb0c761e2017-06-25 22:42:55 -0400337 break;
338
339 default:
David Benjaminb0c761e2017-06-25 22:42:55 -0400340 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400341 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
342 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400343 }
344
345 CBS cbs;
David Benjamind9229f92017-10-06 17:36:20 -0400346 CBS_init(&cbs, record.data(), record.size());
David Benjamin9d632f42016-06-23 17:57:19 -0400347 while (CBS_len(&cbs) > 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400348 // Read a handshake fragment.
David Benjamin9d632f42016-06-23 17:57:19 -0400349 struct hm_header_st msg_hdr;
350 CBS body;
351 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
352 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400353 *out_alert = SSL_AD_DECODE_ERROR;
354 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400355 }
356
357 const size_t frag_off = msg_hdr.frag_off;
358 const size_t frag_len = msg_hdr.frag_len;
359 const size_t msg_len = msg_hdr.msg_len;
360 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
361 frag_off + frag_len > msg_len ||
362 msg_len > ssl_max_handshake_message_len(ssl)) {
363 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
David Benjamind9229f92017-10-06 17:36:20 -0400364 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
365 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400366 }
367
David Benjaminc11ea9422017-08-29 16:33:21 -0400368 // The encrypted epoch in DTLS has only one handshake message.
David Benjamin02edcd02016-07-27 17:40:37 -0400369 if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) {
370 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400371 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
372 return ssl_open_record_error;
David Benjamin02edcd02016-07-27 17:40:37 -0400373 }
374
David Benjamin9d632f42016-06-23 17:57:19 -0400375 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
376 msg_hdr.seq >
377 (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400378 // Ignore fragments from the past, or ones too far in the future.
David Benjamin9d632f42016-06-23 17:57:19 -0400379 continue;
380 }
381
David Benjamind9229f92017-10-06 17:36:20 -0400382 hm_fragment *frag = dtls1_get_incoming_message(ssl, out_alert, &msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400383 if (frag == NULL) {
David Benjamind9229f92017-10-06 17:36:20 -0400384 return ssl_open_record_error;
David Benjamin9d632f42016-06-23 17:57:19 -0400385 }
David Benjamin528bd262016-07-08 09:34:05 -0700386 assert(frag->msg_len == msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400387
388 if (frag->reassembly == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400389 // The message is already assembled.
David Benjamin9d632f42016-06-23 17:57:19 -0400390 continue;
391 }
392 assert(msg_len > 0);
393
David Benjaminc11ea9422017-08-29 16:33:21 -0400394 // Copy the body into the fragment.
David Benjamin1a999cf2017-01-03 10:30:35 -0500395 OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off,
396 CBS_data(&body), CBS_len(&body));
David Benjamin9d632f42016-06-23 17:57:19 -0400397 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
398 }
399
David Benjamind9229f92017-10-06 17:36:20 -0400400 return ssl_open_record_success;
David Benjamin9d632f42016-06-23 17:57:19 -0400401}
402
Adam Langleyc9827e02019-04-12 14:46:50 -0700403bool dtls1_get_message(const SSL *ssl, SSLMessage *out) {
David Benjamin7934f082017-08-01 16:32:25 -0400404 if (!dtls1_is_current_message_complete(ssl)) {
405 return false;
David Benjamin9d632f42016-06-23 17:57:19 -0400406 }
407
David Benjamin800046f2017-10-17 01:37:51 -0400408 size_t idx = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
409 hm_fragment *frag = ssl->d1->incoming_messages[idx].get();
David Benjamin7934f082017-08-01 16:32:25 -0400410 out->type = frag->type;
411 CBS_init(&out->body, frag->data + DTLS1_HM_HEADER_LENGTH, frag->msg_len);
412 CBS_init(&out->raw, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len);
413 out->is_v2_hello = false;
414 if (!ssl->s3->has_message) {
David Benjamin03a4b962017-10-06 17:54:10 -0400415 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw);
David Benjamin046bc1f2017-08-31 15:06:42 -0400416 ssl->s3->has_message = true;
David Benjamin78b8b992017-08-01 18:38:41 -0400417 }
David Benjamin7934f082017-08-01 16:32:25 -0400418 return true;
David Benjamin9d632f42016-06-23 17:57:19 -0400419}
420
David Benjamin8f94c312017-08-01 17:35:55 -0400421void dtls1_next_message(SSL *ssl) {
David Benjamin7934f082017-08-01 16:32:25 -0400422 assert(ssl->s3->has_message);
David Benjamin4497e582016-07-27 17:51:49 -0400423 assert(dtls1_is_current_message_complete(ssl));
424 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
David Benjamin800046f2017-10-17 01:37:51 -0400425 ssl->d1->incoming_messages[index].reset();
David Benjamin4497e582016-07-27 17:51:49 -0400426 ssl->d1->handshake_read_seq++;
David Benjamin046bc1f2017-08-31 15:06:42 -0400427 ssl->s3->has_message = false;
David Benjaminc11ea9422017-08-29 16:33:21 -0400428 // If we previously sent a flight, mark it as having a reply, so
429 // |on_handshake_complete| can manage post-handshake retransmission.
David Benjamin302b8182017-08-22 14:47:22 -0700430 if (ssl->d1->outgoing_messages_complete) {
431 ssl->d1->flight_has_reply = true;
432 }
David Benjamin4497e582016-07-27 17:51:49 -0400433}
434
David Benjamin40e94702017-10-06 18:26:36 -0400435bool dtls_has_unprocessed_handshake_data(const SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400436 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
437 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400438 // Skip the current message.
David Benjamin7934f082017-08-01 16:32:25 -0400439 if (ssl->s3->has_message && i == current) {
David Benjamin4497e582016-07-27 17:51:49 -0400440 assert(dtls1_is_current_message_complete(ssl));
441 continue;
442 }
David Benjamin800046f2017-10-17 01:37:51 -0400443 if (ssl->d1->incoming_messages[i] != nullptr) {
David Benjamin40e94702017-10-06 18:26:36 -0400444 return true;
David Benjamin61672812016-07-14 23:10:43 -0400445 }
446 }
David Benjamin40e94702017-10-06 18:26:36 -0400447 return false;
David Benjamin61672812016-07-14 23:10:43 -0400448}
449
David Benjamin97250f42017-10-07 04:12:35 -0400450bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
451 CBS *out_body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500452 OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st));
David Benjamin9d632f42016-06-23 17:57:19 -0400453
454 if (!CBS_get_u8(cbs, &out_hdr->type) ||
455 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
456 !CBS_get_u16(cbs, &out_hdr->seq) ||
457 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
458 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
459 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
David Benjamin97250f42017-10-07 04:12:35 -0400460 return false;
David Benjamin9d632f42016-06-23 17:57:19 -0400461 }
462
David Benjamin97250f42017-10-07 04:12:35 -0400463 return true;
David Benjamin9d632f42016-06-23 17:57:19 -0400464}
465
David Benjamind9229f92017-10-06 17:36:20 -0400466ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
467 uint8_t *out_alert,
468 Span<uint8_t> in) {
469 if (!ssl->d1->has_change_cipher_spec) {
470 // dtls1_open_handshake processes both handshake and ChangeCipherSpec.
471 auto ret = dtls1_open_handshake(ssl, out_consumed, out_alert, in);
472 if (ret != ssl_open_record_success) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400473 return ret;
474 }
475 }
David Benjamind9229f92017-10-06 17:36:20 -0400476 if (ssl->d1->has_change_cipher_spec) {
477 ssl->d1->has_change_cipher_spec = false;
478 return ssl_open_record_success;
479 }
480 return ssl_open_record_discard;
David Benjaminb0c761e2017-06-25 22:42:55 -0400481}
482
David Benjamin9d632f42016-06-23 17:57:19 -0400483
David Benjaminc11ea9422017-08-29 16:33:21 -0400484// Sending handshake messages.
David Benjamin9d632f42016-06-23 17:57:19 -0400485
David Benjamin46af2432022-09-04 15:12:57 -0400486void DTLS_OUTGOING_MESSAGE::Clear() { data.Reset(); }
David Benjamin049fdfc2017-10-17 01:12:53 -0400487
David Benjamin75836432016-06-17 18:48:29 -0400488void dtls_clear_outgoing_messages(SSL *ssl) {
David Benjamin54091232016-09-05 12:47:25 -0400489 for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) {
David Benjamin049fdfc2017-10-17 01:12:53 -0400490 ssl->d1->outgoing_messages[i].Clear();
David Benjamin75836432016-06-17 18:48:29 -0400491 }
492 ssl->d1->outgoing_messages_len = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -0500493 ssl->d1->outgoing_written = 0;
494 ssl->d1->outgoing_offset = 0;
David Benjamin9bbdf582017-08-02 19:46:29 -0400495 ssl->d1->outgoing_messages_complete = false;
David Benjamin302b8182017-08-22 14:47:22 -0700496 ssl->d1->flight_has_reply = false;
David Benjamin75836432016-06-17 18:48:29 -0400497}
498
David Benjaminbcef5142021-06-02 11:24:26 -0400499bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400500 // Pick a modest size hint to save most of the |realloc| calls.
David Benjamin75836432016-06-17 18:48:29 -0400501 if (!CBB_init(cbb, 64) ||
502 !CBB_add_u8(cbb, type) ||
503 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
504 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
505 !CBB_add_u24(cbb, 0 /* offset */) ||
506 !CBB_add_u24_length_prefixed(cbb, body)) {
David Benjamin97250f42017-10-07 04:12:35 -0400507 return false;
David Benjamin75836432016-06-17 18:48:29 -0400508 }
509
David Benjamin97250f42017-10-07 04:12:35 -0400510 return true;
David Benjamin75836432016-06-17 18:48:29 -0400511}
512
David Benjaminbcef5142021-06-02 11:24:26 -0400513bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) {
David Benjamin879efc32017-09-21 11:20:53 -0400514 if (!CBBFinishArray(cbb, out_msg) ||
515 out_msg->size() < DTLS1_HM_HEADER_LENGTH) {
David Benjamin75836432016-06-17 18:48:29 -0400516 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400517 return false;
David Benjamin75836432016-06-17 18:48:29 -0400518 }
519
David Benjaminc11ea9422017-08-29 16:33:21 -0400520 // Fix up the header. Copy the fragment length into the total message
521 // length.
David Benjamin879efc32017-09-21 11:20:53 -0400522 OPENSSL_memcpy(out_msg->data() + 1,
523 out_msg->data() + DTLS1_HM_HEADER_LENGTH - 3, 3);
David Benjamin97250f42017-10-07 04:12:35 -0400524 return true;
Steven Valdez5eead162016-11-11 22:23:25 -0500525}
David Benjamin75836432016-06-17 18:48:29 -0400526
Adam Langleyf64c3732018-05-08 15:04:35 -0700527// ssl_size_t_greater_than_32_bits returns whether |v| exceeds the bounds of a
528// 32-bit value. The obvious thing doesn't work because, in some 32-bit build
529// configurations, the compiler warns that the test is always false and breaks
530// the build.
531static bool ssl_size_t_greater_than_32_bits(size_t v) {
532#if defined(OPENSSL_64_BIT)
533 return v > 0xffffffff;
534#elif defined(OPENSSL_32_BIT)
535 return false;
536#else
537#error "Building for neither 32- nor 64-bits."
538#endif
539}
540
David Benjaminc11ea9422017-08-29 16:33:21 -0400541// add_outgoing adds a new handshake message or ChangeCipherSpec to the current
David Benjamin97250f42017-10-07 04:12:35 -0400542// outgoing flight. It returns true on success and false on error.
David Benjamin049fdfc2017-10-17 01:12:53 -0400543static bool add_outgoing(SSL *ssl, bool is_ccs, Array<uint8_t> data) {
David Benjamin9bbdf582017-08-02 19:46:29 -0400544 if (ssl->d1->outgoing_messages_complete) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400545 // If we've begun writing a new flight, we received the peer flight. Discard
546 // the timer and the our flight.
David Benjamin9bbdf582017-08-02 19:46:29 -0400547 dtls1_stop_timer(ssl);
548 dtls_clear_outgoing_messages(ssl);
549 }
550
David Benjamina3d76d02017-07-14 19:36:07 -0400551 static_assert(SSL_MAX_HANDSHAKE_FLIGHT <
552 (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)),
553 "outgoing_messages_len is too small");
David Benjamin879efc32017-09-21 11:20:53 -0400554 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT ||
Adam Langleyf64c3732018-05-08 15:04:35 -0700555 ssl_size_t_greater_than_32_bits(data.size())) {
David Benjamin97250f42017-10-07 04:12:35 -0400556 assert(false);
David Benjamin1a999cf2017-01-03 10:30:35 -0500557 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400558 return false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500559 }
David Benjamin75836432016-06-17 18:48:29 -0400560
David Benjamin1a999cf2017-01-03 10:30:35 -0500561 if (!is_ccs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400562 // TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript
563 // on hs.
Steven Valdez908ac192017-01-12 13:17:07 -0500564 if (ssl->s3->hs != NULL &&
David Benjamin75a1f232017-10-11 17:19:19 -0400565 !ssl->s3->hs->transcript.Update(data)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500566 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin97250f42017-10-07 04:12:35 -0400567 return false;
Steven Valdez908ac192017-01-12 13:17:07 -0500568 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500569 ssl->d1->handshake_write_seq++;
570 }
571
572 DTLS_OUTGOING_MESSAGE *msg =
573 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
David Benjamin46af2432022-09-04 15:12:57 -0400574 msg->data = std::move(data);
David Benjamin1a999cf2017-01-03 10:30:35 -0500575 msg->epoch = ssl->d1->w_epoch;
576 msg->is_ccs = is_ccs;
577
578 ssl->d1->outgoing_messages_len++;
David Benjamin97250f42017-10-07 04:12:35 -0400579 return true;
David Benjamin1a999cf2017-01-03 10:30:35 -0500580}
581
David Benjamin97250f42017-10-07 04:12:35 -0400582bool dtls1_add_message(SSL *ssl, Array<uint8_t> data) {
David Benjamin049fdfc2017-10-17 01:12:53 -0400583 return add_outgoing(ssl, false /* handshake */, std::move(data));
David Benjamin75836432016-06-17 18:48:29 -0400584}
585
David Benjamin97250f42017-10-07 04:12:35 -0400586bool dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin049fdfc2017-10-17 01:12:53 -0400587 return add_outgoing(ssl, true /* ChangeCipherSpec */, Array<uint8_t>());
David Benjamin1a999cf2017-01-03 10:30:35 -0500588}
589
David Benjaminc11ea9422017-08-29 16:33:21 -0400590// dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above
591// the minimum.
David Benjamin1a999cf2017-01-03 10:30:35 -0500592static void dtls1_update_mtu(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400593 // TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the
594 // only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use
595 // |SSL_set_mtu|. Does this need to be so complex?
David Benjamin1a999cf2017-01-03 10:30:35 -0500596 if (ssl->d1->mtu < dtls1_min_mtu() &&
597 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin50596f82018-07-02 19:47:27 -0400598 long mtu = BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
David Benjamin1a999cf2017-01-03 10:30:35 -0500599 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
600 ssl->d1->mtu = (unsigned)mtu;
601 } else {
602 ssl->d1->mtu = kDefaultMTU;
David Benjamin50596f82018-07-02 19:47:27 -0400603 BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
David Benjamin1a999cf2017-01-03 10:30:35 -0500604 }
605 }
606
David Benjaminc11ea9422017-08-29 16:33:21 -0400607 // The MTU should be above the minimum now.
David Benjamin1a999cf2017-01-03 10:30:35 -0500608 assert(ssl->d1->mtu >= dtls1_min_mtu());
609}
610
611enum seal_result_t {
612 seal_error,
613 seal_no_progress,
614 seal_partial,
615 seal_success,
616};
617
David Benjaminc11ea9422017-08-29 16:33:21 -0400618// seal_next_message seals |msg|, which must be the next message, to |out|. If
619// progress was made, it returns |seal_partial| or |seal_success| and sets
620// |*out_len| to the number of bytes written.
David Benjamin1a999cf2017-01-03 10:30:35 -0500621static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
622 size_t *out_len, size_t max_out,
623 const DTLS_OUTGOING_MESSAGE *msg) {
624 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
625 assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
626
David Benjamin3e3090d2015-04-05 12:48:30 -0400627 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400628 if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400629 use_epoch = dtls1_use_previous_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400630 } else if (msg->epoch != ssl->d1->w_epoch) {
631 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
632 return seal_error;
Adam Langley71d8a082014-12-13 16:28:18 -0800633 }
David Benjamincfc11c22017-07-18 22:45:18 -0400634
David Benjamin1a999cf2017-01-03 10:30:35 -0500635 size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
636 size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800637
David Benjamin29a83c52016-06-17 19:12:54 -0400638 if (msg->is_ccs) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400639 // Check there is room for the ChangeCipherSpec.
David Benjamin1a999cf2017-01-03 10:30:35 -0500640 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
641 if (max_out < sizeof(kChangeCipherSpec) + overhead) {
642 return seal_no_progress;
643 }
644
645 if (!dtls_seal_record(ssl, out, out_len, max_out,
646 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
647 sizeof(kChangeCipherSpec), use_epoch)) {
648 return seal_error;
649 }
650
651 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
David Benjaminc64d1232017-10-04 18:14:28 -0400652 kChangeCipherSpec);
David Benjamin1a999cf2017-01-03 10:30:35 -0500653 return seal_success;
David Benjamina97b7372015-11-03 14:54:39 -0500654 }
655
David Benjaminc11ea9422017-08-29 16:33:21 -0400656 // DTLS messages are serialized as a single fragment in |msg|.
David Benjamin1a999cf2017-01-03 10:30:35 -0500657 CBS cbs, body;
658 struct hm_header_st hdr;
David Benjamin46af2432022-09-04 15:12:57 -0400659 CBS_init(&cbs, msg->data.data(), msg->data.size());
David Benjamin1a999cf2017-01-03 10:30:35 -0500660 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
661 hdr.frag_off != 0 ||
662 hdr.frag_len != CBS_len(&body) ||
663 hdr.msg_len != CBS_len(&body) ||
664 !CBS_skip(&body, ssl->d1->outgoing_offset) ||
665 CBS_len(&cbs) != 0) {
666 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
667 return seal_error;
668 }
669
David Benjaminc11ea9422017-08-29 16:33:21 -0400670 // Determine how much progress can be made.
David Benjamin1a999cf2017-01-03 10:30:35 -0500671 if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) {
672 return seal_no_progress;
673 }
674 size_t todo = CBS_len(&body);
675 if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) {
676 todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead;
677 }
678
David Benjaminc11ea9422017-08-29 16:33:21 -0400679 // Assemble a fragment, to be sealed in-place.
David Benjamin1386aad2017-07-19 23:57:40 -0400680 ScopedCBB cbb;
David Benjamin7ac94aa2022-10-24 15:44:37 -0400681 CBB child;
David Benjamin1a999cf2017-01-03 10:30:35 -0500682 uint8_t *frag = out + prefix;
683 size_t max_frag = max_out - prefix, frag_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400684 if (!CBB_init_fixed(cbb.get(), frag, max_frag) ||
685 !CBB_add_u8(cbb.get(), hdr.type) ||
686 !CBB_add_u24(cbb.get(), hdr.msg_len) ||
687 !CBB_add_u16(cbb.get(), hdr.seq) ||
688 !CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) ||
David Benjamin7ac94aa2022-10-24 15:44:37 -0400689 !CBB_add_u24_length_prefixed(cbb.get(), &child) ||
690 !CBB_add_bytes(&child, CBS_data(&body), todo) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400691 !CBB_finish(cbb.get(), NULL, &frag_len)) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500692 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
693 return seal_error;
694 }
695
David Benjaminc64d1232017-10-04 18:14:28 -0400696 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE,
697 MakeSpan(frag, frag_len));
David Benjamin1a999cf2017-01-03 10:30:35 -0500698
699 if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
700 out + prefix, frag_len, use_epoch)) {
701 return seal_error;
702 }
703
704 if (todo == CBS_len(&body)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400705 // The next message is complete.
David Benjamin1a999cf2017-01-03 10:30:35 -0500706 ssl->d1->outgoing_offset = 0;
707 return seal_success;
708 }
709
710 ssl->d1->outgoing_offset += todo;
711 return seal_partial;
Adam Langley71d8a082014-12-13 16:28:18 -0800712}
Adam Langley95c29f32014-06-20 12:00:00 -0700713
David Benjaminc11ea9422017-08-29 16:33:21 -0400714// seal_next_packet writes as much of the next flight as possible to |out| and
715// advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as
716// appropriate.
David Benjamin97250f42017-10-07 04:12:35 -0400717static bool seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len,
718 size_t max_out) {
719 bool made_progress = false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500720 size_t total = 0;
721 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
722 for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len;
723 ssl->d1->outgoing_written++) {
724 const DTLS_OUTGOING_MESSAGE *msg =
725 &ssl->d1->outgoing_messages[ssl->d1->outgoing_written];
726 size_t len;
727 enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg);
728 switch (ret) {
729 case seal_error:
David Benjamin97250f42017-10-07 04:12:35 -0400730 return false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500731
732 case seal_no_progress:
733 goto packet_full;
734
735 case seal_partial:
736 case seal_success:
737 out += len;
738 max_out -= len;
739 total += len;
David Benjamin97250f42017-10-07 04:12:35 -0400740 made_progress = true;
David Benjamin1a999cf2017-01-03 10:30:35 -0500741
742 if (ret == seal_partial) {
743 goto packet_full;
744 }
745 break;
746 }
David Benjamin30152fd2016-05-05 20:45:48 -0400747 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500748
749packet_full:
David Benjaminc11ea9422017-08-29 16:33:21 -0400750 // The MTU was too small to make any progress.
David Benjamin1a999cf2017-01-03 10:30:35 -0500751 if (!made_progress) {
752 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamin97250f42017-10-07 04:12:35 -0400753 return false;
David Benjamin1a999cf2017-01-03 10:30:35 -0500754 }
755
756 *out_len = total;
David Benjamin97250f42017-10-07 04:12:35 -0400757 return true;
David Benjamin1a999cf2017-01-03 10:30:35 -0500758}
759
David Benjamin9bbdf582017-08-02 19:46:29 -0400760static int send_flight(SSL *ssl) {
David Benjamine8d07462017-10-12 18:09:20 -0400761 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
762 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
763 return -1;
764 }
765
David Benjamin084064b2021-03-29 18:04:19 -0400766 if (ssl->wbio == nullptr) {
767 OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
768 return -1;
769 }
770
David Benjamin1a999cf2017-01-03 10:30:35 -0500771 dtls1_update_mtu(ssl);
Adam Langleybcc4e232015-02-19 15:51:58 -0800772
David Benjaminb86dcfe2021-07-12 17:15:01 -0400773 Array<uint8_t> packet;
774 if (!packet.Init(ssl->d1->mtu)) {
775 return -1;
David Benjamin1a999cf2017-01-03 10:30:35 -0500776 }
777
778 while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) {
779 uint8_t old_written = ssl->d1->outgoing_written;
780 uint32_t old_offset = ssl->d1->outgoing_offset;
781
782 size_t packet_len;
David Benjaminb86dcfe2021-07-12 17:15:01 -0400783 if (!seal_next_packet(ssl, packet.data(), &packet_len, packet.size())) {
784 return -1;
David Benjamin1a999cf2017-01-03 10:30:35 -0500785 }
786
David Benjaminb86dcfe2021-07-12 17:15:01 -0400787 int bio_ret = BIO_write(ssl->wbio.get(), packet.data(), packet_len);
David Benjamin1a999cf2017-01-03 10:30:35 -0500788 if (bio_ret <= 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400789 // Retry this packet the next time around.
David Benjamin1a999cf2017-01-03 10:30:35 -0500790 ssl->d1->outgoing_written = old_written;
791 ssl->d1->outgoing_offset = old_offset;
David Benjaminf4928302019-08-21 16:04:53 -0400792 ssl->s3->rwstate = SSL_ERROR_WANT_WRITE;
David Benjaminb86dcfe2021-07-12 17:15:01 -0400793 return bio_ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800794 }
795 }
796
David Benjamin50596f82018-07-02 19:47:27 -0400797 if (BIO_flush(ssl->wbio.get()) <= 0) {
David Benjaminf4928302019-08-21 16:04:53 -0400798 ssl->s3->rwstate = SSL_ERROR_WANT_WRITE;
David Benjaminb86dcfe2021-07-12 17:15:01 -0400799 return -1;
David Benjamin27309552016-05-05 21:17:53 -0400800 }
David Benjamin30152fd2016-05-05 20:45:48 -0400801
David Benjaminb86dcfe2021-07-12 17:15:01 -0400802 return 1;
Adam Langleybcc4e232015-02-19 15:51:58 -0800803}
804
David Benjamin9bbdf582017-08-02 19:46:29 -0400805int dtls1_flush_flight(SSL *ssl) {
806 ssl->d1->outgoing_messages_complete = true;
David Benjaminc11ea9422017-08-29 16:33:21 -0400807 // Start the retransmission timer for the next flight (if any).
David Benjamin9bbdf582017-08-02 19:46:29 -0400808 dtls1_start_timer(ssl);
809 return send_flight(ssl);
810}
811
David Benjamin1a999cf2017-01-03 10:30:35 -0500812int dtls1_retransmit_outgoing_messages(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400813 // Rewind to the start of the flight and write it again.
814 //
815 // TODO(davidben): This does not allow retransmits to be resumed on
816 // non-blocking write.
David Benjamin1a999cf2017-01-03 10:30:35 -0500817 ssl->d1->outgoing_written = 0;
818 ssl->d1->outgoing_offset = 0;
819
David Benjamin9bbdf582017-08-02 19:46:29 -0400820 return send_flight(ssl);
David Benjamina97b7372015-11-03 14:54:39 -0500821}
822
Adam Langley71d8a082014-12-13 16:28:18 -0800823unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500824 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800825}
David Benjamin86e95b82017-07-18 16:34:25 -0400826
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700827BSSL_NAMESPACE_END