blob: cc1de90d24c1caab0c979118d80f4379b55abe95 [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 Benjamin86e95b82017-07-18 16:34:25 -0400114#define BORINGSSL_INTERNAL_CXX_TYPES
115
David Benjamin9e4e01e2015-09-15 01:48:04 -0400116#include <openssl/ssl.h>
117
Adam Langley95c29f32014-06-20 12:00:00 -0700118#include <assert.h>
119#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <string.h>
121
122#include <openssl/buf.h>
123#include <openssl/err.h>
124#include <openssl/evp.h>
125#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700127
David Benjamin17cf2cb2016-12-13 01:07:13 -0500128#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400129#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley95c29f32014-06-20 12:00:00 -0700131
David Benjamin86e95b82017-07-18 16:34:25 -0400132namespace bssl {
133
David Benjamina18b6712015-01-11 19:31:22 -0500134/* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
135 * for these values? Notably, why is kMinMTU a function of the transport
136 * protocol's overhead rather than, say, what's needed to hold a minimally-sized
137 * handshake fragment plus protocol overhead. */
Adam Langley95c29f32014-06-20 12:00:00 -0700138
David Benjamina18b6712015-01-11 19:31:22 -0500139/* kMinMTU is the minimum acceptable MTU value. */
140static const unsigned int kMinMTU = 256 - 28;
141
142/* kDefaultMTU is the default MTU value to use if neither the user nor
143 * the underlying BIO supplies one. */
144static const unsigned int kDefaultMTU = 1500 - 28;
145
David Benjamin9d632f42016-06-23 17:57:19 -0400146
147/* Receiving handshake messages. */
148
David Benjaminec847ce2016-06-17 19:30:47 -0400149static void dtls1_hm_fragment_free(hm_fragment *frag) {
150 if (frag == NULL) {
151 return;
152 }
David Benjamin528bd262016-07-08 09:34:05 -0700153 OPENSSL_free(frag->data);
David Benjaminec847ce2016-06-17 19:30:47 -0400154 OPENSSL_free(frag->reassembly);
155 OPENSSL_free(frag);
156}
157
David Benjamin528bd262016-07-08 09:34:05 -0700158static hm_fragment *dtls1_hm_fragment_new(const struct hm_header_st *msg_hdr) {
David Benjamine8703a32017-07-09 16:17:55 -0400159 hm_fragment *frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800160 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400161 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800162 return NULL;
163 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500164 OPENSSL_memset(frag, 0, sizeof(hm_fragment));
David Benjamin528bd262016-07-08 09:34:05 -0700165 frag->type = msg_hdr->type;
166 frag->seq = msg_hdr->seq;
167 frag->msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin528bd262016-07-08 09:34:05 -0700169 /* Allocate space for the reassembled message and fill in the header. */
David Benjamine8703a32017-07-09 16:17:55 -0400170 frag->data =
171 (uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len);
David Benjamin528bd262016-07-08 09:34:05 -0700172 if (frag->data == NULL) {
173 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
174 goto err;
175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
David Benjamin528bd262016-07-08 09:34:05 -0700177 CBB cbb;
178 if (!CBB_init_fixed(&cbb, frag->data, DTLS1_HM_HEADER_LENGTH) ||
179 !CBB_add_u8(&cbb, msg_hdr->type) ||
180 !CBB_add_u24(&cbb, msg_hdr->msg_len) ||
181 !CBB_add_u16(&cbb, msg_hdr->seq) ||
182 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
183 !CBB_add_u24(&cbb, msg_hdr->msg_len) ||
184 !CBB_finish(&cbb, NULL, NULL)) {
185 CBB_cleanup(&cbb);
186 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
187 goto err;
188 }
189
190 /* If the handshake message is empty, |frag->reassembly| is NULL. */
191 if (msg_hdr->msg_len > 0) {
David Benjamin29a83c52016-06-17 19:12:54 -0400192 /* Initialize reassembly bitmask. */
David Benjamin528bd262016-07-08 09:34:05 -0700193 if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) {
David Benjamin29a83c52016-06-17 19:12:54 -0400194 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
195 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800196 }
David Benjamin528bd262016-07-08 09:34:05 -0700197 size_t bitmask_len = (msg_hdr->msg_len + 7) / 8;
David Benjamine8703a32017-07-09 16:17:55 -0400198 frag->reassembly = (uint8_t *)OPENSSL_malloc(bitmask_len);
David Benjamin29a83c52016-06-17 19:12:54 -0400199 if (frag->reassembly == NULL) {
200 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
201 goto err;
202 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500203 OPENSSL_memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley71d8a082014-12-13 16:28:18 -0800206 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400207
208err:
209 dtls1_hm_fragment_free(frag);
210 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800211}
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjaminee562b92015-03-02 20:52:52 -0500213/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
214 * exclusive, set. */
David Benjamindca125e2016-06-23 17:52:47 -0400215static uint8_t bit_range(size_t start, size_t end) {
David Benjaminee562b92015-03-02 20:52:52 -0500216 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
217}
218
219/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
220 * as received in |frag|. If |frag| becomes complete, it clears
221 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
222 * and |frag->reassembly| must not be NULL. */
223static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
224 size_t end) {
David Benjamin528bd262016-07-08 09:34:05 -0700225 size_t msg_len = frag->msg_len;
David Benjaminee562b92015-03-02 20:52:52 -0500226
227 if (frag->reassembly == NULL || start > end || end > msg_len) {
228 assert(0);
229 return;
230 }
231 /* A zero-length message will never have a pending reassembly. */
232 assert(msg_len > 0);
233
234 if ((start >> 3) == (end >> 3)) {
235 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
236 } else {
237 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
David Benjamin54091232016-09-05 12:47:25 -0400238 for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500239 frag->reassembly[i] = 0xff;
240 }
241 if ((end & 7) != 0) {
242 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
243 }
244 }
245
246 /* Check if the fragment is complete. */
David Benjamin54091232016-09-05 12:47:25 -0400247 for (size_t i = 0; i < (msg_len >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500248 if (frag->reassembly[i] != 0xff) {
249 return;
250 }
251 }
252 if ((msg_len & 7) != 0 &&
253 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
254 return;
255 }
256
257 OPENSSL_free(frag->reassembly);
258 frag->reassembly = NULL;
259}
260
David Benjamin528bd262016-07-08 09:34:05 -0700261/* dtls1_is_current_message_complete returns one if the current handshake
262 * message is complete and zero otherwise. */
David Benjamin61672812016-07-14 23:10:43 -0400263static int dtls1_is_current_message_complete(const SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400264 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
265 SSL_MAX_HANDSHAKE_FLIGHT];
266 return frag != NULL && frag->reassembly == NULL;
267}
268
269/* dtls1_get_incoming_message returns the incoming message corresponding to
270 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
271 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
272 * returns NULL on failure. The caller does not take ownership of the result. */
273static hm_fragment *dtls1_get_incoming_message(
274 SSL *ssl, const struct hm_header_st *msg_hdr) {
275 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
276 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
277 return NULL;
278 }
279
280 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
281 hm_fragment *frag = ssl->d1->incoming_messages[idx];
282 if (frag != NULL) {
David Benjamin528bd262016-07-08 09:34:05 -0700283 assert(frag->seq == msg_hdr->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400284 /* The new fragment must be compatible with the previous fragments from this
285 * message. */
David Benjamin528bd262016-07-08 09:34:05 -0700286 if (frag->type != msg_hdr->type ||
287 frag->msg_len != msg_hdr->msg_len) {
David Benjamin9d632f42016-06-23 17:57:19 -0400288 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
289 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
290 return NULL;
291 }
292 return frag;
293 }
294
295 /* This is the first fragment from this message. */
David Benjamin528bd262016-07-08 09:34:05 -0700296 frag = dtls1_hm_fragment_new(msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400297 if (frag == NULL) {
298 return NULL;
299 }
David Benjamin9d632f42016-06-23 17:57:19 -0400300 ssl->d1->incoming_messages[idx] = frag;
301 return frag;
302}
303
304/* dtls1_process_handshake_record reads a handshake record and processes it. It
305 * returns one if the record was successfully processed and 0 or -1 on error. */
306static int dtls1_process_handshake_record(SSL *ssl) {
307 SSL3_RECORD *rr = &ssl->s3->rrec;
308
309start:
310 if (rr->length == 0) {
311 int ret = dtls1_get_record(ssl);
312 if (ret <= 0) {
313 return ret;
314 }
315 }
316
317 /* Cross-epoch records are discarded, but we may receive out-of-order
David Benjamin1a999cf2017-01-03 10:30:35 -0500318 * application data between ChangeCipherSpec and Finished or a
319 * ChangeCipherSpec before the appropriate point in the handshake. Those must
320 * be silently discarded.
David Benjamin9d632f42016-06-23 17:57:19 -0400321 *
322 * However, only allow the out-of-order records in the correct epoch.
323 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
324 * the unencrypted epoch (we never renegotiate). Other cases fall through and
325 * fail with a fatal error. */
326 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
327 ssl->s3->aead_read_ctx != NULL) ||
328 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
329 ssl->s3->aead_read_ctx == NULL)) {
330 rr->length = 0;
331 goto start;
332 }
333
334 if (rr->type != SSL3_RT_HANDSHAKE) {
335 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
336 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
337 return -1;
338 }
339
340 CBS cbs;
341 CBS_init(&cbs, rr->data, rr->length);
342
343 while (CBS_len(&cbs) > 0) {
344 /* Read a handshake fragment. */
345 struct hm_header_st msg_hdr;
346 CBS body;
347 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
348 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
349 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
350 return -1;
351 }
352
353 const size_t frag_off = msg_hdr.frag_off;
354 const size_t frag_len = msg_hdr.frag_len;
355 const size_t msg_len = msg_hdr.msg_len;
356 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
357 frag_off + frag_len > msg_len ||
358 msg_len > ssl_max_handshake_message_len(ssl)) {
359 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
360 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
361 return -1;
362 }
363
David Benjamin02edcd02016-07-27 17:40:37 -0400364 /* The encrypted epoch in DTLS has only one handshake message. */
365 if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) {
366 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
367 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
368 return -1;
369 }
370
David Benjamin9d632f42016-06-23 17:57:19 -0400371 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
372 msg_hdr.seq >
373 (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) {
374 /* Ignore fragments from the past, or ones too far in the future. */
375 continue;
376 }
377
378 hm_fragment *frag = dtls1_get_incoming_message(ssl, &msg_hdr);
379 if (frag == NULL) {
380 return -1;
381 }
David Benjamin528bd262016-07-08 09:34:05 -0700382 assert(frag->msg_len == msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400383
384 if (frag->reassembly == NULL) {
385 /* The message is already assembled. */
386 continue;
387 }
388 assert(msg_len > 0);
389
390 /* Copy the body into the fragment. */
David Benjamin1a999cf2017-01-03 10:30:35 -0500391 OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off,
392 CBS_data(&body), CBS_len(&body));
David Benjamin9d632f42016-06-23 17:57:19 -0400393 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
394 }
395
396 rr->length = 0;
397 ssl_read_buffer_discard(ssl);
398 return 1;
399}
400
David Benjaminf71036e2017-01-21 14:49:39 -0500401int dtls1_get_message(SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400402 if (ssl->s3->tmp.reuse_message) {
David Benjaminf71036e2017-01-21 14:49:39 -0500403 /* There must be a current message. */
David Benjamin4497e582016-07-27 17:51:49 -0400404 assert(ssl->init_msg != NULL);
David Benjamin9d632f42016-06-23 17:57:19 -0400405 ssl->s3->tmp.reuse_message = 0;
David Benjamin4497e582016-07-27 17:51:49 -0400406 } else {
407 dtls1_release_current_message(ssl, 0 /* don't free buffer */);
David Benjamin9d632f42016-06-23 17:57:19 -0400408 }
409
David Benjamin528bd262016-07-08 09:34:05 -0700410 /* Process handshake records until the current message is ready. */
411 while (!dtls1_is_current_message_complete(ssl)) {
David Benjamin9d632f42016-06-23 17:57:19 -0400412 int ret = dtls1_process_handshake_record(ssl);
413 if (ret <= 0) {
David Benjamin9d632f42016-06-23 17:57:19 -0400414 return ret;
415 }
416 }
417
David Benjamin528bd262016-07-08 09:34:05 -0700418 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
419 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin9d632f42016-06-23 17:57:19 -0400420 assert(frag != NULL);
421 assert(frag->reassembly == NULL);
David Benjamin528bd262016-07-08 09:34:05 -0700422 assert(ssl->d1->handshake_read_seq == frag->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400423
424 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
425 * |ssl_get_message| API. */
David Benjamin528bd262016-07-08 09:34:05 -0700426 ssl->s3->tmp.message_type = frag->type;
427 ssl->init_msg = frag->data + DTLS1_HM_HEADER_LENGTH;
428 ssl->init_num = frag->msg_len;
David Benjamin9d632f42016-06-23 17:57:19 -0400429
David Benjaminc0279992016-09-19 20:15:07 -0400430 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, frag->data,
431 ssl->init_num + DTLS1_HM_HEADER_LENGTH);
David Benjamin09eb6552016-07-08 14:32:11 -0700432 return 1;
David Benjamin528bd262016-07-08 09:34:05 -0700433}
David Benjamin9d632f42016-06-23 17:57:19 -0400434
David Benjaminced94792016-11-14 17:12:11 +0900435void dtls1_get_current_message(const SSL *ssl, CBS *out) {
David Benjamin528bd262016-07-08 09:34:05 -0700436 assert(dtls1_is_current_message_complete(ssl));
437
438 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
439 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjaminced94792016-11-14 17:12:11 +0900440 CBS_init(out, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400441}
442
David Benjamin4497e582016-07-27 17:51:49 -0400443void dtls1_release_current_message(SSL *ssl, int free_buffer) {
444 if (ssl->init_msg == NULL) {
445 return;
446 }
447
448 assert(dtls1_is_current_message_complete(ssl));
449 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
450 dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]);
451 ssl->d1->incoming_messages[index] = NULL;
452 ssl->d1->handshake_read_seq++;
453
454 ssl->init_msg = NULL;
455 ssl->init_num = 0;
456}
457
David Benjamin9d632f42016-06-23 17:57:19 -0400458void dtls_clear_incoming_messages(SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400459 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin9d632f42016-06-23 17:57:19 -0400460 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
461 ssl->d1->incoming_messages[i] = NULL;
462 }
463}
464
David Benjamin61672812016-07-14 23:10:43 -0400465int dtls_has_incoming_messages(const SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400466 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
467 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin4497e582016-07-27 17:51:49 -0400468 /* Skip the current message. */
469 if (ssl->init_msg != NULL && i == current) {
470 assert(dtls1_is_current_message_complete(ssl));
471 continue;
472 }
473 if (ssl->d1->incoming_messages[i] != NULL) {
David Benjamin61672812016-07-14 23:10:43 -0400474 return 1;
475 }
476 }
477 return 0;
478}
479
David Benjamin9d632f42016-06-23 17:57:19 -0400480int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
481 CBS *out_body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500482 OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st));
David Benjamin9d632f42016-06-23 17:57:19 -0400483
484 if (!CBS_get_u8(cbs, &out_hdr->type) ||
485 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
486 !CBS_get_u16(cbs, &out_hdr->seq) ||
487 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
488 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
489 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
490 return 0;
491 }
492
493 return 1;
494}
495
496
497/* Sending handshake messages. */
498
David Benjamin75836432016-06-17 18:48:29 -0400499void dtls_clear_outgoing_messages(SSL *ssl) {
David Benjamin54091232016-09-05 12:47:25 -0400500 for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) {
David Benjamin75836432016-06-17 18:48:29 -0400501 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
502 ssl->d1->outgoing_messages[i].data = NULL;
503 }
504 ssl->d1->outgoing_messages_len = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -0500505 ssl->d1->outgoing_written = 0;
506 ssl->d1->outgoing_offset = 0;
David Benjamin75836432016-06-17 18:48:29 -0400507}
508
509int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
510 /* Pick a modest size hint to save most of the |realloc| calls. */
511 if (!CBB_init(cbb, 64) ||
512 !CBB_add_u8(cbb, type) ||
513 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
514 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
515 !CBB_add_u24(cbb, 0 /* offset */) ||
516 !CBB_add_u24_length_prefixed(cbb, body)) {
517 return 0;
518 }
519
520 return 1;
521}
522
Steven Valdez5eead162016-11-11 22:23:25 -0500523int dtls1_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
524 size_t *out_len) {
David Benjaminba1660b2016-11-12 14:26:19 +0900525 *out_msg = NULL;
Steven Valdez5eead162016-11-11 22:23:25 -0500526 if (!CBB_finish(cbb, out_msg, out_len) ||
527 *out_len < DTLS1_HM_HEADER_LENGTH) {
David Benjamin75836432016-06-17 18:48:29 -0400528 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez5eead162016-11-11 22:23:25 -0500529 OPENSSL_free(*out_msg);
David Benjamin75836432016-06-17 18:48:29 -0400530 return 0;
531 }
532
533 /* Fix up the header. Copy the fragment length into the total message
534 * length. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500535 OPENSSL_memcpy(*out_msg + 1, *out_msg + DTLS1_HM_HEADER_LENGTH - 3, 3);
Steven Valdez5eead162016-11-11 22:23:25 -0500536 return 1;
537}
David Benjamin75836432016-06-17 18:48:29 -0400538
David Benjamin1a999cf2017-01-03 10:30:35 -0500539/* add_outgoing adds a new handshake message or ChangeCipherSpec to the current
540 * outgoing flight. It returns one on success and zero on error. In both cases,
541 * it takes ownership of |data| and releases it with |OPENSSL_free| when
542 * done. */
543static int add_outgoing(SSL *ssl, int is_ccs, uint8_t *data, size_t len) {
David Benjamina3d76d02017-07-14 19:36:07 -0400544 static_assert(SSL_MAX_HANDSHAKE_FLIGHT <
545 (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)),
546 "outgoing_messages_len is too small");
David Benjamin1a999cf2017-01-03 10:30:35 -0500547 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
548 assert(0);
549 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
550 OPENSSL_free(data);
551 return 0;
552 }
David Benjamin75836432016-06-17 18:48:29 -0400553
David Benjamin1a999cf2017-01-03 10:30:35 -0500554 if (!is_ccs) {
Steven Valdez908ac192017-01-12 13:17:07 -0500555 /* TODO(svaldez): Move this up a layer to fix abstraction for SSL_TRANSCRIPT
556 * on hs. */
557 if (ssl->s3->hs != NULL &&
558 !SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript, data, len)) {
559 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
560 OPENSSL_free(data);
561 return 0;
562 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500563 ssl->d1->handshake_write_seq++;
564 }
565
566 DTLS_OUTGOING_MESSAGE *msg =
567 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
568 msg->data = data;
569 msg->len = len;
570 msg->epoch = ssl->d1->w_epoch;
571 msg->is_ccs = is_ccs;
572
573 ssl->d1->outgoing_messages_len++;
574 return 1;
575}
576
David Benjamindaf207a2017-01-03 18:37:41 -0500577int dtls1_add_message(SSL *ssl, uint8_t *data, size_t len) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500578 return add_outgoing(ssl, 0 /* handshake */, data, len);
David Benjamin75836432016-06-17 18:48:29 -0400579}
580
David Benjamindaf207a2017-01-03 18:37:41 -0500581int dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500582 return add_outgoing(ssl, 1 /* ChangeCipherSpec */, NULL, 0);
583}
584
David Benjamindaf207a2017-01-03 18:37:41 -0500585int dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
586 /* The |add_alert| path is only used for warning alerts for now, which DTLS
587 * never sends. This will be implemented later once closure alerts are
588 * converted. */
589 assert(0);
590 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
591 return 0;
592}
593
David Benjamin1a999cf2017-01-03 10:30:35 -0500594/* dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above
595 * the minimum. */
596static void dtls1_update_mtu(SSL *ssl) {
597 /* TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the
598 * only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use
599 * |SSL_set_mtu|. Does this need to be so complex? */
600 if (ssl->d1->mtu < dtls1_min_mtu() &&
601 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
602 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
603 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
604 ssl->d1->mtu = (unsigned)mtu;
605 } else {
606 ssl->d1->mtu = kDefaultMTU;
607 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
608 }
609 }
610
611 /* The MTU should be above the minimum now. */
612 assert(ssl->d1->mtu >= dtls1_min_mtu());
613}
614
615enum seal_result_t {
616 seal_error,
617 seal_no_progress,
618 seal_partial,
619 seal_success,
620};
621
622/* seal_next_message seals |msg|, which must be the next message, to |out|. If
623 * progress was made, it returns |seal_partial| or |seal_success| and sets
624 * |*out_len| to the number of bytes written. */
625static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
626 size_t *out_len, size_t max_out,
627 const DTLS_OUTGOING_MESSAGE *msg) {
628 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
629 assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
630
David Benjaminafbc63f2015-02-01 02:33:59 -0500631 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
632 * (negotiated cipher) exist. */
David Benjamin0d56f882015-12-19 17:05:56 -0500633 assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1);
David Benjamin29a83c52016-06-17 19:12:54 -0400634 assert(msg->epoch <= ssl->d1->w_epoch);
David Benjamin3e3090d2015-04-05 12:48:30 -0400635 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamin29a83c52016-06-17 19:12:54 -0400636 if (ssl->d1->w_epoch == 1 && msg->epoch == 0) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400637 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800638 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500639 size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
640 size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800641
David Benjamin29a83c52016-06-17 19:12:54 -0400642 if (msg->is_ccs) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500643 /* Check there is room for the ChangeCipherSpec. */
644 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
645 if (max_out < sizeof(kChangeCipherSpec) + overhead) {
646 return seal_no_progress;
647 }
648
649 if (!dtls_seal_record(ssl, out, out_len, max_out,
650 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
651 sizeof(kChangeCipherSpec), use_epoch)) {
652 return seal_error;
653 }
654
655 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
656 kChangeCipherSpec, sizeof(kChangeCipherSpec));
657 return seal_success;
David Benjamina97b7372015-11-03 14:54:39 -0500658 }
659
David Benjamin1a999cf2017-01-03 10:30:35 -0500660 /* DTLS messages are serialized as a single fragment in |msg|. */
661 CBS cbs, body;
662 struct hm_header_st hdr;
663 CBS_init(&cbs, msg->data, msg->len);
664 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
665 hdr.frag_off != 0 ||
666 hdr.frag_len != CBS_len(&body) ||
667 hdr.msg_len != CBS_len(&body) ||
668 !CBS_skip(&body, ssl->d1->outgoing_offset) ||
669 CBS_len(&cbs) != 0) {
670 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
671 return seal_error;
672 }
673
674 /* Determine how much progress can be made. */
675 if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) {
676 return seal_no_progress;
677 }
678 size_t todo = CBS_len(&body);
679 if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) {
680 todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead;
681 }
682
683 /* Assemble a fragment, to be sealed in-place. */
684 CBB cbb;
685 uint8_t *frag = out + prefix;
686 size_t max_frag = max_out - prefix, frag_len;
687 if (!CBB_init_fixed(&cbb, frag, max_frag) ||
688 !CBB_add_u8(&cbb, hdr.type) ||
689 !CBB_add_u24(&cbb, hdr.msg_len) ||
690 !CBB_add_u16(&cbb, hdr.seq) ||
691 !CBB_add_u24(&cbb, ssl->d1->outgoing_offset) ||
692 !CBB_add_u24(&cbb, todo) ||
693 !CBB_add_bytes(&cbb, CBS_data(&body), todo) ||
694 !CBB_finish(&cbb, NULL, &frag_len)) {
695 CBB_cleanup(&cbb);
696 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
697 return seal_error;
698 }
699
700 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, frag, frag_len);
701
702 if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
703 out + prefix, frag_len, use_epoch)) {
704 return seal_error;
705 }
706
707 if (todo == CBS_len(&body)) {
708 /* The next message is complete. */
709 ssl->d1->outgoing_offset = 0;
710 return seal_success;
711 }
712
713 ssl->d1->outgoing_offset += todo;
714 return seal_partial;
Adam Langley71d8a082014-12-13 16:28:18 -0800715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
David Benjamin1a999cf2017-01-03 10:30:35 -0500717/* seal_next_packet writes as much of the next flight as possible to |out| and
718 * advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as
719 * appropriate. */
720static int seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len,
721 size_t max_out) {
722 int made_progress = 0;
723 size_t total = 0;
724 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
725 for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len;
726 ssl->d1->outgoing_written++) {
727 const DTLS_OUTGOING_MESSAGE *msg =
728 &ssl->d1->outgoing_messages[ssl->d1->outgoing_written];
729 size_t len;
730 enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg);
731 switch (ret) {
732 case seal_error:
733 return 0;
734
735 case seal_no_progress:
736 goto packet_full;
737
738 case seal_partial:
739 case seal_success:
740 out += len;
741 max_out -= len;
742 total += len;
743 made_progress = 1;
744
745 if (ret == seal_partial) {
746 goto packet_full;
747 }
748 break;
749 }
David Benjamin30152fd2016-05-05 20:45:48 -0400750 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500751
752packet_full:
753 /* The MTU was too small to make any progress. */
754 if (!made_progress) {
755 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
756 return 0;
757 }
758
759 *out_len = total;
760 return 1;
761}
762
763int dtls1_flush_flight(SSL *ssl) {
764 dtls1_update_mtu(ssl);
Adam Langleybcc4e232015-02-19 15:51:58 -0800765
David Benjamin30152fd2016-05-05 20:45:48 -0400766 int ret = -1;
David Benjamine8703a32017-07-09 16:17:55 -0400767 uint8_t *packet = (uint8_t *)OPENSSL_malloc(ssl->d1->mtu);
David Benjamin1a999cf2017-01-03 10:30:35 -0500768 if (packet == NULL) {
769 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
770 goto err;
771 }
772
773 while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) {
774 uint8_t old_written = ssl->d1->outgoing_written;
775 uint32_t old_offset = ssl->d1->outgoing_offset;
776
777 size_t packet_len;
778 if (!seal_next_packet(ssl, packet, &packet_len, ssl->d1->mtu)) {
779 goto err;
780 }
781
782 int bio_ret = BIO_write(ssl->wbio, packet, packet_len);
783 if (bio_ret <= 0) {
784 /* Retry this packet the next time around. */
785 ssl->d1->outgoing_written = old_written;
786 ssl->d1->outgoing_offset = old_offset;
787 ssl->rwstate = SSL_WRITING;
788 ret = bio_ret;
David Benjamin30152fd2016-05-05 20:45:48 -0400789 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800790 }
791 }
792
David Benjamin42e3e192017-01-27 10:06:07 -0500793 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin27309552016-05-05 21:17:53 -0400794 ssl->rwstate = SSL_WRITING;
David Benjamin42e3e192017-01-27 10:06:07 -0500795 goto err;
David Benjamin27309552016-05-05 21:17:53 -0400796 }
David Benjamin30152fd2016-05-05 20:45:48 -0400797
David Benjamin42e3e192017-01-27 10:06:07 -0500798 ret = 1;
799
David Benjamin30152fd2016-05-05 20:45:48 -0400800err:
David Benjamin1a999cf2017-01-03 10:30:35 -0500801 OPENSSL_free(packet);
David Benjamin30152fd2016-05-05 20:45:48 -0400802 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800803}
804
David Benjamin1a999cf2017-01-03 10:30:35 -0500805int dtls1_retransmit_outgoing_messages(SSL *ssl) {
806 /* Rewind to the start of the flight and write it again.
807 *
808 * TODO(davidben): This does not allow retransmits to be resumed on
809 * non-blocking write. */
810 ssl->d1->outgoing_written = 0;
811 ssl->d1->outgoing_offset = 0;
812
813 return dtls1_flush_flight(ssl);
David Benjamina97b7372015-11-03 14:54:39 -0500814}
815
Adam Langley71d8a082014-12-13 16:28:18 -0800816unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500817 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800818}
David Benjamin86e95b82017-07-18 16:34:25 -0400819
820} // namespace bssl