blob: 2538d28df33751d1a3b26d7e2d9bd7c3be31ba1f [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 Benjamina18b6712015-01-11 19:31:22 -0500132/* 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 Benjamina18b6712015-01-11 19:31:22 -0500137/* kMinMTU is the minimum acceptable MTU value. */
138static const unsigned int kMinMTU = 256 - 28;
139
140/* kDefaultMTU is the default MTU value to use if neither the user nor
141 * the underlying BIO supplies one. */
142static const unsigned int kDefaultMTU = 1500 - 28;
143
David Benjamin9d632f42016-06-23 17:57:19 -0400144
145/* Receiving handshake messages. */
146
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 Benjamin528bd262016-07-08 09:34:05 -0700168 /* 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
187 /* If the handshake message is empty, |frag->reassembly| is NULL. */
188 if (msg_hdr->msg_len > 0) {
David Benjamin29a83c52016-06-17 19:12:54 -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 Benjaminee562b92015-03-02 20:52:52 -0500210/* 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
216/* 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. */
220static 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 }
228 /* A zero-length message will never have a pending reassembly. */
229 assert(msg_len > 0);
230
231 if ((start >> 3) == (end >> 3)) {
232 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
233 } else {
234 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
David Benjamin54091232016-09-05 12:47:25 -0400235 for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500236 frag->reassembly[i] = 0xff;
237 }
238 if ((end & 7) != 0) {
239 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
240 }
241 }
242
243 /* Check if the fragment is complete. */
David Benjamin54091232016-09-05 12:47:25 -0400244 for (size_t i = 0; i < (msg_len >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500245 if (frag->reassembly[i] != 0xff) {
246 return;
247 }
248 }
249 if ((msg_len & 7) != 0 &&
250 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
251 return;
252 }
253
254 OPENSSL_free(frag->reassembly);
255 frag->reassembly = NULL;
256}
257
David Benjamin528bd262016-07-08 09:34:05 -0700258/* dtls1_is_current_message_complete returns one if the current handshake
259 * message is complete and zero otherwise. */
David Benjamin61672812016-07-14 23:10:43 -0400260static int dtls1_is_current_message_complete(const SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400261 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
262 SSL_MAX_HANDSHAKE_FLIGHT];
263 return frag != NULL && frag->reassembly == NULL;
264}
265
266/* dtls1_get_incoming_message returns the incoming message corresponding to
267 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
268 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
269 * returns NULL on failure. The caller does not take ownership of the result. */
270static hm_fragment *dtls1_get_incoming_message(
271 SSL *ssl, const struct hm_header_st *msg_hdr) {
272 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
273 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
274 return NULL;
275 }
276
277 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
278 hm_fragment *frag = ssl->d1->incoming_messages[idx];
279 if (frag != NULL) {
David Benjamin528bd262016-07-08 09:34:05 -0700280 assert(frag->seq == msg_hdr->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400281 /* The new fragment must be compatible with the previous fragments from this
282 * message. */
David Benjamin528bd262016-07-08 09:34:05 -0700283 if (frag->type != msg_hdr->type ||
284 frag->msg_len != msg_hdr->msg_len) {
David Benjamin9d632f42016-06-23 17:57:19 -0400285 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
286 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
287 return NULL;
288 }
289 return frag;
290 }
291
292 /* This is the first fragment from this message. */
David Benjamin528bd262016-07-08 09:34:05 -0700293 frag = dtls1_hm_fragment_new(msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400294 if (frag == NULL) {
295 return NULL;
296 }
David Benjamin9d632f42016-06-23 17:57:19 -0400297 ssl->d1->incoming_messages[idx] = frag;
298 return frag;
299}
300
David Benjamin7934f082017-08-01 16:32:25 -0400301int dtls1_read_message(SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400302 SSL3_RECORD *rr = &ssl->s3->rrec;
David Benjamin9d632f42016-06-23 17:57:19 -0400303 if (rr->length == 0) {
304 int ret = dtls1_get_record(ssl);
305 if (ret <= 0) {
306 return ret;
307 }
308 }
309
David Benjaminb0c761e2017-06-25 22:42:55 -0400310 switch (rr->type) {
311 case SSL3_RT_APPLICATION_DATA:
312 /* Unencrypted application data records are always illegal. */
313 if (ssl->s3->aead_read_ctx->is_null_cipher()) {
314 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
315 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
316 return -1;
317 }
David Benjamin9d632f42016-06-23 17:57:19 -0400318
David Benjaminb0c761e2017-06-25 22:42:55 -0400319 /* Out-of-order application data may be received between ChangeCipherSpec
320 * and finished. Discard it. */
321 rr->length = 0;
322 ssl_read_buffer_discard(ssl);
323 return 1;
324
325 case SSL3_RT_CHANGE_CIPHER_SPEC:
326 /* We do not support renegotiation, so encrypted ChangeCipherSpec records
327 * are illegal. */
328 if (!ssl->s3->aead_read_ctx->is_null_cipher()) {
329 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
330 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
331 return -1;
332 }
333
334 if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
335 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
336 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
337 return -1;
338 }
339
340 /* Flag the ChangeCipherSpec for later. */
341 ssl->d1->has_change_cipher_spec = true;
342 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC,
343 rr->data, rr->length);
344
345 rr->length = 0;
346 ssl_read_buffer_discard(ssl);
347 return 1;
348
349 case SSL3_RT_HANDSHAKE:
350 /* Break out to main processing. */
351 break;
352
353 default:
354 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
355 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
356 return -1;
David Benjamin9d632f42016-06-23 17:57:19 -0400357 }
358
359 CBS cbs;
360 CBS_init(&cbs, rr->data, rr->length);
361
362 while (CBS_len(&cbs) > 0) {
363 /* Read a handshake fragment. */
364 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);
368 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
369 return -1;
370 }
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);
379 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
380 return -1;
381 }
382
David Benjamin02edcd02016-07-27 17:40:37 -0400383 /* The encrypted epoch in DTLS has only one handshake message. */
384 if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) {
385 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
386 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
387 return -1;
388 }
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) {
393 /* Ignore fragments from the past, or ones too far in the future. */
394 continue;
395 }
396
397 hm_fragment *frag = dtls1_get_incoming_message(ssl, &msg_hdr);
398 if (frag == NULL) {
399 return -1;
400 }
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) {
404 /* The message is already assembled. */
405 continue;
406 }
407 assert(msg_len > 0);
408
409 /* 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
415 rr->length = 0;
416 ssl_read_buffer_discard(ssl);
417 return 1;
418}
419
David Benjamin7934f082017-08-01 16:32:25 -0400420bool dtls1_get_message(SSL *ssl, SSLMessage *out) {
421 if (!dtls1_is_current_message_complete(ssl)) {
422 return false;
David Benjamin9d632f42016-06-23 17:57:19 -0400423 }
424
David Benjamin528bd262016-07-08 09:34:05 -0700425 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
426 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin7934f082017-08-01 16:32:25 -0400427 out->type = frag->type;
428 CBS_init(&out->body, frag->data + DTLS1_HM_HEADER_LENGTH, frag->msg_len);
429 CBS_init(&out->raw, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len);
430 out->is_v2_hello = false;
431 if (!ssl->s3->has_message) {
David Benjamin78b8b992017-08-01 18:38:41 -0400432 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, frag->data,
433 frag->msg_len + DTLS1_HM_HEADER_LENGTH);
David Benjamin7934f082017-08-01 16:32:25 -0400434 ssl->s3->has_message = 1;
David Benjamin78b8b992017-08-01 18:38:41 -0400435 }
David Benjamin7934f082017-08-01 16:32:25 -0400436 return true;
David Benjamin9d632f42016-06-23 17:57:19 -0400437}
438
David Benjamin8f94c312017-08-01 17:35:55 -0400439void dtls1_next_message(SSL *ssl) {
David Benjamin7934f082017-08-01 16:32:25 -0400440 assert(ssl->s3->has_message);
David Benjamin4497e582016-07-27 17:51:49 -0400441 assert(dtls1_is_current_message_complete(ssl));
442 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
443 dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]);
444 ssl->d1->incoming_messages[index] = NULL;
445 ssl->d1->handshake_read_seq++;
David Benjamin7934f082017-08-01 16:32:25 -0400446 ssl->s3->has_message = 0;
David Benjamin4497e582016-07-27 17:51:49 -0400447}
448
David Benjamin9d632f42016-06-23 17:57:19 -0400449void dtls_clear_incoming_messages(SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400450 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin9d632f42016-06-23 17:57:19 -0400451 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
452 ssl->d1->incoming_messages[i] = NULL;
453 }
454}
455
David Benjamin61672812016-07-14 23:10:43 -0400456int dtls_has_incoming_messages(const SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400457 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
458 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin4497e582016-07-27 17:51:49 -0400459 /* Skip the current message. */
David Benjamin7934f082017-08-01 16:32:25 -0400460 if (ssl->s3->has_message && i == current) {
David Benjamin4497e582016-07-27 17:51:49 -0400461 assert(dtls1_is_current_message_complete(ssl));
462 continue;
463 }
464 if (ssl->d1->incoming_messages[i] != NULL) {
David Benjamin61672812016-07-14 23:10:43 -0400465 return 1;
466 }
467 }
468 return 0;
469}
470
David Benjamin9d632f42016-06-23 17:57:19 -0400471int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
472 CBS *out_body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500473 OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st));
David Benjamin9d632f42016-06-23 17:57:19 -0400474
475 if (!CBS_get_u8(cbs, &out_hdr->type) ||
476 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
477 !CBS_get_u16(cbs, &out_hdr->seq) ||
478 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
479 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
480 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
481 return 0;
482 }
483
484 return 1;
485}
486
David Benjaminb0c761e2017-06-25 22:42:55 -0400487int dtls1_read_change_cipher_spec(SSL *ssl) {
488 /* Process handshake records until there is a ChangeCipherSpec. */
489 while (!ssl->d1->has_change_cipher_spec) {
David Benjamin7934f082017-08-01 16:32:25 -0400490 int ret = dtls1_read_message(ssl);
David Benjaminb0c761e2017-06-25 22:42:55 -0400491 if (ret <= 0) {
492 return ret;
493 }
494 }
495
496 ssl->d1->has_change_cipher_spec = false;
497 return 1;
498}
499
David Benjamin9d632f42016-06-23 17:57:19 -0400500
501/* Sending handshake messages. */
502
David Benjamin75836432016-06-17 18:48:29 -0400503void dtls_clear_outgoing_messages(SSL *ssl) {
David Benjamin54091232016-09-05 12:47:25 -0400504 for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) {
David Benjamin75836432016-06-17 18:48:29 -0400505 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
506 ssl->d1->outgoing_messages[i].data = NULL;
507 }
508 ssl->d1->outgoing_messages_len = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -0500509 ssl->d1->outgoing_written = 0;
510 ssl->d1->outgoing_offset = 0;
David Benjamin9bbdf582017-08-02 19:46:29 -0400511 ssl->d1->outgoing_messages_complete = false;
David Benjamin75836432016-06-17 18:48:29 -0400512}
513
514int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
515 /* Pick a modest size hint to save most of the |realloc| calls. */
516 if (!CBB_init(cbb, 64) ||
517 !CBB_add_u8(cbb, type) ||
518 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
519 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
520 !CBB_add_u24(cbb, 0 /* offset */) ||
521 !CBB_add_u24_length_prefixed(cbb, body)) {
522 return 0;
523 }
524
525 return 1;
526}
527
Steven Valdez5eead162016-11-11 22:23:25 -0500528int dtls1_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
529 size_t *out_len) {
David Benjaminba1660b2016-11-12 14:26:19 +0900530 *out_msg = NULL;
Steven Valdez5eead162016-11-11 22:23:25 -0500531 if (!CBB_finish(cbb, out_msg, out_len) ||
532 *out_len < DTLS1_HM_HEADER_LENGTH) {
David Benjamin75836432016-06-17 18:48:29 -0400533 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez5eead162016-11-11 22:23:25 -0500534 OPENSSL_free(*out_msg);
David Benjamin75836432016-06-17 18:48:29 -0400535 return 0;
536 }
537
538 /* Fix up the header. Copy the fragment length into the total message
539 * length. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500540 OPENSSL_memcpy(*out_msg + 1, *out_msg + DTLS1_HM_HEADER_LENGTH - 3, 3);
Steven Valdez5eead162016-11-11 22:23:25 -0500541 return 1;
542}
David Benjamin75836432016-06-17 18:48:29 -0400543
David Benjamin1a999cf2017-01-03 10:30:35 -0500544/* add_outgoing adds a new handshake message or ChangeCipherSpec to the current
545 * outgoing flight. It returns one on success and zero on error. In both cases,
546 * it takes ownership of |data| and releases it with |OPENSSL_free| when
547 * done. */
548static int add_outgoing(SSL *ssl, int is_ccs, uint8_t *data, size_t len) {
David Benjamin9bbdf582017-08-02 19:46:29 -0400549 if (ssl->d1->outgoing_messages_complete) {
550 /* If we've begun writing a new flight, we received the peer flight. Discard
551 * the timer and the our flight. */
552 dtls1_stop_timer(ssl);
553 dtls_clear_outgoing_messages(ssl);
554 }
555
David Benjamina3d76d02017-07-14 19:36:07 -0400556 static_assert(SSL_MAX_HANDSHAKE_FLIGHT <
557 (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)),
558 "outgoing_messages_len is too small");
David Benjamin1a999cf2017-01-03 10:30:35 -0500559 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
560 assert(0);
561 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
562 OPENSSL_free(data);
563 return 0;
564 }
David Benjamin75836432016-06-17 18:48:29 -0400565
David Benjamin1a999cf2017-01-03 10:30:35 -0500566 if (!is_ccs) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400567 /* TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript
Steven Valdez908ac192017-01-12 13:17:07 -0500568 * on hs. */
569 if (ssl->s3->hs != NULL &&
David Benjamin6dc8bf62017-07-19 16:38:21 -0400570 !ssl->s3->hs->transcript.Update(data, len)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500571 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
572 OPENSSL_free(data);
573 return 0;
574 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500575 ssl->d1->handshake_write_seq++;
576 }
577
578 DTLS_OUTGOING_MESSAGE *msg =
579 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
580 msg->data = data;
581 msg->len = len;
582 msg->epoch = ssl->d1->w_epoch;
583 msg->is_ccs = is_ccs;
584
585 ssl->d1->outgoing_messages_len++;
586 return 1;
587}
588
David Benjamindaf207a2017-01-03 18:37:41 -0500589int dtls1_add_message(SSL *ssl, uint8_t *data, size_t len) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500590 return add_outgoing(ssl, 0 /* handshake */, data, len);
David Benjamin75836432016-06-17 18:48:29 -0400591}
592
David Benjamindaf207a2017-01-03 18:37:41 -0500593int dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500594 return add_outgoing(ssl, 1 /* ChangeCipherSpec */, NULL, 0);
595}
596
David Benjamindaf207a2017-01-03 18:37:41 -0500597int dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
598 /* The |add_alert| path is only used for warning alerts for now, which DTLS
599 * never sends. This will be implemented later once closure alerts are
600 * converted. */
601 assert(0);
602 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
603 return 0;
604}
605
David Benjamin1a999cf2017-01-03 10:30:35 -0500606/* dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above
607 * the minimum. */
608static void dtls1_update_mtu(SSL *ssl) {
609 /* TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the
610 * only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use
611 * |SSL_set_mtu|. Does this need to be so complex? */
612 if (ssl->d1->mtu < dtls1_min_mtu() &&
613 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
614 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
615 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
616 ssl->d1->mtu = (unsigned)mtu;
617 } else {
618 ssl->d1->mtu = kDefaultMTU;
619 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
620 }
621 }
622
623 /* The MTU should be above the minimum now. */
624 assert(ssl->d1->mtu >= dtls1_min_mtu());
625}
626
627enum seal_result_t {
628 seal_error,
629 seal_no_progress,
630 seal_partial,
631 seal_success,
632};
633
634/* seal_next_message seals |msg|, which must be the next message, to |out|. If
635 * progress was made, it returns |seal_partial| or |seal_success| and sets
636 * |*out_len| to the number of bytes written. */
637static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
638 size_t *out_len, size_t max_out,
639 const DTLS_OUTGOING_MESSAGE *msg) {
640 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
641 assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
642
David Benjamin3e3090d2015-04-05 12:48:30 -0400643 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400644 if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400645 use_epoch = dtls1_use_previous_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400646 } else if (msg->epoch != ssl->d1->w_epoch) {
647 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
648 return seal_error;
Adam Langley71d8a082014-12-13 16:28:18 -0800649 }
David Benjamincfc11c22017-07-18 22:45:18 -0400650
David Benjamin1a999cf2017-01-03 10:30:35 -0500651 size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
652 size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800653
David Benjamin29a83c52016-06-17 19:12:54 -0400654 if (msg->is_ccs) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500655 /* Check there is room for the ChangeCipherSpec. */
656 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
657 if (max_out < sizeof(kChangeCipherSpec) + overhead) {
658 return seal_no_progress;
659 }
660
661 if (!dtls_seal_record(ssl, out, out_len, max_out,
662 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
663 sizeof(kChangeCipherSpec), use_epoch)) {
664 return seal_error;
665 }
666
667 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
668 kChangeCipherSpec, sizeof(kChangeCipherSpec));
669 return seal_success;
David Benjamina97b7372015-11-03 14:54:39 -0500670 }
671
David Benjamin1a999cf2017-01-03 10:30:35 -0500672 /* DTLS messages are serialized as a single fragment in |msg|. */
673 CBS cbs, body;
674 struct hm_header_st hdr;
675 CBS_init(&cbs, msg->data, msg->len);
676 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
677 hdr.frag_off != 0 ||
678 hdr.frag_len != CBS_len(&body) ||
679 hdr.msg_len != CBS_len(&body) ||
680 !CBS_skip(&body, ssl->d1->outgoing_offset) ||
681 CBS_len(&cbs) != 0) {
682 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
683 return seal_error;
684 }
685
686 /* Determine how much progress can be made. */
687 if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) {
688 return seal_no_progress;
689 }
690 size_t todo = CBS_len(&body);
691 if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) {
692 todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead;
693 }
694
695 /* Assemble a fragment, to be sealed in-place. */
David Benjamin1386aad2017-07-19 23:57:40 -0400696 ScopedCBB cbb;
David Benjamin1a999cf2017-01-03 10:30:35 -0500697 uint8_t *frag = out + prefix;
698 size_t max_frag = max_out - prefix, frag_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400699 if (!CBB_init_fixed(cbb.get(), frag, max_frag) ||
700 !CBB_add_u8(cbb.get(), hdr.type) ||
701 !CBB_add_u24(cbb.get(), hdr.msg_len) ||
702 !CBB_add_u16(cbb.get(), hdr.seq) ||
703 !CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) ||
704 !CBB_add_u24(cbb.get(), todo) ||
705 !CBB_add_bytes(cbb.get(), CBS_data(&body), todo) ||
706 !CBB_finish(cbb.get(), NULL, &frag_len)) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500707 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
708 return seal_error;
709 }
710
711 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, frag, frag_len);
712
713 if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
714 out + prefix, frag_len, use_epoch)) {
715 return seal_error;
716 }
717
718 if (todo == CBS_len(&body)) {
719 /* The next message is complete. */
720 ssl->d1->outgoing_offset = 0;
721 return seal_success;
722 }
723
724 ssl->d1->outgoing_offset += todo;
725 return seal_partial;
Adam Langley71d8a082014-12-13 16:28:18 -0800726}
Adam Langley95c29f32014-06-20 12:00:00 -0700727
David Benjamin1a999cf2017-01-03 10:30:35 -0500728/* seal_next_packet writes as much of the next flight as possible to |out| and
729 * advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as
730 * appropriate. */
731static int seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len,
732 size_t max_out) {
733 int made_progress = 0;
734 size_t total = 0;
735 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
736 for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len;
737 ssl->d1->outgoing_written++) {
738 const DTLS_OUTGOING_MESSAGE *msg =
739 &ssl->d1->outgoing_messages[ssl->d1->outgoing_written];
740 size_t len;
741 enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg);
742 switch (ret) {
743 case seal_error:
744 return 0;
745
746 case seal_no_progress:
747 goto packet_full;
748
749 case seal_partial:
750 case seal_success:
751 out += len;
752 max_out -= len;
753 total += len;
754 made_progress = 1;
755
756 if (ret == seal_partial) {
757 goto packet_full;
758 }
759 break;
760 }
David Benjamin30152fd2016-05-05 20:45:48 -0400761 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500762
763packet_full:
764 /* The MTU was too small to make any progress. */
765 if (!made_progress) {
766 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
767 return 0;
768 }
769
770 *out_len = total;
771 return 1;
772}
773
David Benjamin9bbdf582017-08-02 19:46:29 -0400774static int send_flight(SSL *ssl) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500775 dtls1_update_mtu(ssl);
Adam Langleybcc4e232015-02-19 15:51:58 -0800776
David Benjamin30152fd2016-05-05 20:45:48 -0400777 int ret = -1;
David Benjamine8703a32017-07-09 16:17:55 -0400778 uint8_t *packet = (uint8_t *)OPENSSL_malloc(ssl->d1->mtu);
David Benjamin1a999cf2017-01-03 10:30:35 -0500779 if (packet == NULL) {
780 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
781 goto err;
782 }
783
784 while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) {
785 uint8_t old_written = ssl->d1->outgoing_written;
786 uint32_t old_offset = ssl->d1->outgoing_offset;
787
788 size_t packet_len;
789 if (!seal_next_packet(ssl, packet, &packet_len, ssl->d1->mtu)) {
790 goto err;
791 }
792
793 int bio_ret = BIO_write(ssl->wbio, packet, packet_len);
794 if (bio_ret <= 0) {
795 /* Retry this packet the next time around. */
796 ssl->d1->outgoing_written = old_written;
797 ssl->d1->outgoing_offset = old_offset;
798 ssl->rwstate = SSL_WRITING;
799 ret = bio_ret;
David Benjamin30152fd2016-05-05 20:45:48 -0400800 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800801 }
802 }
803
David Benjamin42e3e192017-01-27 10:06:07 -0500804 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin27309552016-05-05 21:17:53 -0400805 ssl->rwstate = SSL_WRITING;
David Benjamin42e3e192017-01-27 10:06:07 -0500806 goto err;
David Benjamin27309552016-05-05 21:17:53 -0400807 }
David Benjamin30152fd2016-05-05 20:45:48 -0400808
David Benjamin42e3e192017-01-27 10:06:07 -0500809 ret = 1;
810
David Benjamin30152fd2016-05-05 20:45:48 -0400811err:
David Benjamin1a999cf2017-01-03 10:30:35 -0500812 OPENSSL_free(packet);
David Benjamin30152fd2016-05-05 20:45:48 -0400813 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800814}
815
David Benjamin9bbdf582017-08-02 19:46:29 -0400816int dtls1_flush_flight(SSL *ssl) {
817 ssl->d1->outgoing_messages_complete = true;
818 /* Start the retransmission timer for the next flight (if any). */
819 dtls1_start_timer(ssl);
820 return send_flight(ssl);
821}
822
David Benjamin1a999cf2017-01-03 10:30:35 -0500823int dtls1_retransmit_outgoing_messages(SSL *ssl) {
824 /* Rewind to the start of the flight and write it again.
825 *
826 * TODO(davidben): This does not allow retransmits to be resumed on
827 * non-blocking write. */
828 ssl->d1->outgoing_written = 0;
829 ssl->d1->outgoing_offset = 0;
830
David Benjamin9bbdf582017-08-02 19:46:29 -0400831 return send_flight(ssl);
David Benjamina97b7372015-11-03 14:54:39 -0500832}
833
Adam Langley71d8a082014-12-13 16:28:18 -0800834unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500835 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800836}
David Benjamin86e95b82017-07-18 16:34:25 -0400837
838} // namespace bssl