blob: 591189f4ad8d9f21065fc5b0823dedc2e4438e81 [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 Benjamin1386aad2017-07-19 23:57:40 -0400159 ScopedCBB cbb;
David Benjamine8703a32017-07-09 16:17:55 -0400160 hm_fragment *frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800161 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400162 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800163 return NULL;
164 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500165 OPENSSL_memset(frag, 0, sizeof(hm_fragment));
David Benjamin528bd262016-07-08 09:34:05 -0700166 frag->type = msg_hdr->type;
167 frag->seq = msg_hdr->seq;
168 frag->msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin528bd262016-07-08 09:34:05 -0700170 /* Allocate space for the reassembled message and fill in the header. */
David Benjamine8703a32017-07-09 16:17:55 -0400171 frag->data =
172 (uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len);
David Benjamin528bd262016-07-08 09:34:05 -0700173 if (frag->data == NULL) {
174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
175 goto err;
176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjamin1386aad2017-07-19 23:57:40 -0400178 if (!CBB_init_fixed(cbb.get(), frag->data, DTLS1_HM_HEADER_LENGTH) ||
179 !CBB_add_u8(cbb.get(), msg_hdr->type) ||
180 !CBB_add_u24(cbb.get(), msg_hdr->msg_len) ||
181 !CBB_add_u16(cbb.get(), msg_hdr->seq) ||
182 !CBB_add_u24(cbb.get(), 0 /* frag_off */) ||
183 !CBB_add_u24(cbb.get(), msg_hdr->msg_len) ||
184 !CBB_finish(cbb.get(), NULL, NULL)) {
David Benjamin528bd262016-07-08 09:34:05 -0700185 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
186 goto err;
187 }
188
189 /* If the handshake message is empty, |frag->reassembly| is NULL. */
190 if (msg_hdr->msg_len > 0) {
David Benjamin29a83c52016-06-17 19:12:54 -0400191 /* Initialize reassembly bitmask. */
David Benjamin528bd262016-07-08 09:34:05 -0700192 if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) {
David Benjamin29a83c52016-06-17 19:12:54 -0400193 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
194 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800195 }
David Benjamin528bd262016-07-08 09:34:05 -0700196 size_t bitmask_len = (msg_hdr->msg_len + 7) / 8;
David Benjamine8703a32017-07-09 16:17:55 -0400197 frag->reassembly = (uint8_t *)OPENSSL_malloc(bitmask_len);
David Benjamin29a83c52016-06-17 19:12:54 -0400198 if (frag->reassembly == NULL) {
199 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
200 goto err;
201 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500202 OPENSSL_memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langley71d8a082014-12-13 16:28:18 -0800205 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400206
207err:
208 dtls1_hm_fragment_free(frag);
209 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800210}
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjaminee562b92015-03-02 20:52:52 -0500212/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
213 * exclusive, set. */
David Benjamindca125e2016-06-23 17:52:47 -0400214static uint8_t bit_range(size_t start, size_t end) {
David Benjaminee562b92015-03-02 20:52:52 -0500215 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
216}
217
218/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
219 * as received in |frag|. If |frag| becomes complete, it clears
220 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
221 * and |frag->reassembly| must not be NULL. */
222static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
223 size_t end) {
David Benjamin528bd262016-07-08 09:34:05 -0700224 size_t msg_len = frag->msg_len;
David Benjaminee562b92015-03-02 20:52:52 -0500225
226 if (frag->reassembly == NULL || start > end || end > msg_len) {
227 assert(0);
228 return;
229 }
230 /* A zero-length message will never have a pending reassembly. */
231 assert(msg_len > 0);
232
233 if ((start >> 3) == (end >> 3)) {
234 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
235 } else {
236 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
David Benjamin54091232016-09-05 12:47:25 -0400237 for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500238 frag->reassembly[i] = 0xff;
239 }
240 if ((end & 7) != 0) {
241 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
242 }
243 }
244
245 /* Check if the fragment is complete. */
David Benjamin54091232016-09-05 12:47:25 -0400246 for (size_t i = 0; i < (msg_len >> 3); i++) {
David Benjaminee562b92015-03-02 20:52:52 -0500247 if (frag->reassembly[i] != 0xff) {
248 return;
249 }
250 }
251 if ((msg_len & 7) != 0 &&
252 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
253 return;
254 }
255
256 OPENSSL_free(frag->reassembly);
257 frag->reassembly = NULL;
258}
259
David Benjamin528bd262016-07-08 09:34:05 -0700260/* dtls1_is_current_message_complete returns one if the current handshake
261 * message is complete and zero otherwise. */
David Benjamin61672812016-07-14 23:10:43 -0400262static int dtls1_is_current_message_complete(const SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400263 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
264 SSL_MAX_HANDSHAKE_FLIGHT];
265 return frag != NULL && frag->reassembly == NULL;
266}
267
268/* dtls1_get_incoming_message returns the incoming message corresponding to
269 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
270 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
271 * returns NULL on failure. The caller does not take ownership of the result. */
272static hm_fragment *dtls1_get_incoming_message(
273 SSL *ssl, const struct hm_header_st *msg_hdr) {
274 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
275 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
276 return NULL;
277 }
278
279 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
280 hm_fragment *frag = ssl->d1->incoming_messages[idx];
281 if (frag != NULL) {
David Benjamin528bd262016-07-08 09:34:05 -0700282 assert(frag->seq == msg_hdr->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400283 /* The new fragment must be compatible with the previous fragments from this
284 * message. */
David Benjamin528bd262016-07-08 09:34:05 -0700285 if (frag->type != msg_hdr->type ||
286 frag->msg_len != msg_hdr->msg_len) {
David Benjamin9d632f42016-06-23 17:57:19 -0400287 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
288 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
289 return NULL;
290 }
291 return frag;
292 }
293
294 /* This is the first fragment from this message. */
David Benjamin528bd262016-07-08 09:34:05 -0700295 frag = dtls1_hm_fragment_new(msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400296 if (frag == NULL) {
297 return NULL;
298 }
David Benjamin9d632f42016-06-23 17:57:19 -0400299 ssl->d1->incoming_messages[idx] = frag;
300 return frag;
301}
302
303/* dtls1_process_handshake_record reads a handshake record and processes it. It
304 * returns one if the record was successfully processed and 0 or -1 on error. */
305static int dtls1_process_handshake_record(SSL *ssl) {
306 SSL3_RECORD *rr = &ssl->s3->rrec;
307
308start:
309 if (rr->length == 0) {
310 int ret = dtls1_get_record(ssl);
311 if (ret <= 0) {
312 return ret;
313 }
314 }
315
316 /* Cross-epoch records are discarded, but we may receive out-of-order
David Benjamin1a999cf2017-01-03 10:30:35 -0500317 * application data between ChangeCipherSpec and Finished or a
318 * ChangeCipherSpec before the appropriate point in the handshake. Those must
319 * be silently discarded.
David Benjamin9d632f42016-06-23 17:57:19 -0400320 *
321 * However, only allow the out-of-order records in the correct epoch.
322 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
323 * the unencrypted epoch (we never renegotiate). Other cases fall through and
324 * fail with a fatal error. */
325 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
David Benjamincfc11c22017-07-18 22:45:18 -0400326 !ssl->s3->aead_read_ctx->is_null_cipher()) ||
David Benjamin9d632f42016-06-23 17:57:19 -0400327 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
David Benjamincfc11c22017-07-18 22:45:18 -0400328 ssl->s3->aead_read_ctx->is_null_cipher())) {
David Benjamin9d632f42016-06-23 17:57:19 -0400329 rr->length = 0;
330 goto start;
331 }
332
333 if (rr->type != SSL3_RT_HANDSHAKE) {
334 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
335 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
336 return -1;
337 }
338
339 CBS cbs;
340 CBS_init(&cbs, rr->data, rr->length);
341
342 while (CBS_len(&cbs) > 0) {
343 /* Read a handshake fragment. */
344 struct hm_header_st msg_hdr;
345 CBS body;
346 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
347 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
348 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
349 return -1;
350 }
351
352 const size_t frag_off = msg_hdr.frag_off;
353 const size_t frag_len = msg_hdr.frag_len;
354 const size_t msg_len = msg_hdr.msg_len;
355 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
356 frag_off + frag_len > msg_len ||
357 msg_len > ssl_max_handshake_message_len(ssl)) {
358 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
359 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
360 return -1;
361 }
362
David Benjamin02edcd02016-07-27 17:40:37 -0400363 /* The encrypted epoch in DTLS has only one handshake message. */
364 if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) {
365 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
366 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
367 return -1;
368 }
369
David Benjamin9d632f42016-06-23 17:57:19 -0400370 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
371 msg_hdr.seq >
372 (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) {
373 /* Ignore fragments from the past, or ones too far in the future. */
374 continue;
375 }
376
377 hm_fragment *frag = dtls1_get_incoming_message(ssl, &msg_hdr);
378 if (frag == NULL) {
379 return -1;
380 }
David Benjamin528bd262016-07-08 09:34:05 -0700381 assert(frag->msg_len == msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400382
383 if (frag->reassembly == NULL) {
384 /* The message is already assembled. */
385 continue;
386 }
387 assert(msg_len > 0);
388
389 /* Copy the body into the fragment. */
David Benjamin1a999cf2017-01-03 10:30:35 -0500390 OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off,
391 CBS_data(&body), CBS_len(&body));
David Benjamin9d632f42016-06-23 17:57:19 -0400392 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
393 }
394
395 rr->length = 0;
396 ssl_read_buffer_discard(ssl);
397 return 1;
398}
399
David Benjaminf71036e2017-01-21 14:49:39 -0500400int dtls1_get_message(SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400401 if (ssl->s3->tmp.reuse_message) {
David Benjaminf71036e2017-01-21 14:49:39 -0500402 /* There must be a current message. */
David Benjamin4497e582016-07-27 17:51:49 -0400403 assert(ssl->init_msg != NULL);
David Benjamin9d632f42016-06-23 17:57:19 -0400404 ssl->s3->tmp.reuse_message = 0;
David Benjamin4497e582016-07-27 17:51:49 -0400405 } else {
406 dtls1_release_current_message(ssl, 0 /* don't free buffer */);
David Benjamin9d632f42016-06-23 17:57:19 -0400407 }
408
David Benjamin528bd262016-07-08 09:34:05 -0700409 /* Process handshake records until the current message is ready. */
410 while (!dtls1_is_current_message_complete(ssl)) {
David Benjamin9d632f42016-06-23 17:57:19 -0400411 int ret = dtls1_process_handshake_record(ssl);
412 if (ret <= 0) {
David Benjamin9d632f42016-06-23 17:57:19 -0400413 return ret;
414 }
415 }
416
David Benjamin528bd262016-07-08 09:34:05 -0700417 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
418 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin9d632f42016-06-23 17:57:19 -0400419 assert(frag != NULL);
420 assert(frag->reassembly == NULL);
David Benjamin528bd262016-07-08 09:34:05 -0700421 assert(ssl->d1->handshake_read_seq == frag->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400422
423 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
424 * |ssl_get_message| API. */
David Benjamin528bd262016-07-08 09:34:05 -0700425 ssl->s3->tmp.message_type = frag->type;
426 ssl->init_msg = frag->data + DTLS1_HM_HEADER_LENGTH;
427 ssl->init_num = frag->msg_len;
David Benjamin9d632f42016-06-23 17:57:19 -0400428
David Benjaminc0279992016-09-19 20:15:07 -0400429 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, frag->data,
430 ssl->init_num + DTLS1_HM_HEADER_LENGTH);
David Benjamin09eb6552016-07-08 14:32:11 -0700431 return 1;
David Benjamin528bd262016-07-08 09:34:05 -0700432}
David Benjamin9d632f42016-06-23 17:57:19 -0400433
David Benjaminced94792016-11-14 17:12:11 +0900434void dtls1_get_current_message(const SSL *ssl, CBS *out) {
David Benjamin528bd262016-07-08 09:34:05 -0700435 assert(dtls1_is_current_message_complete(ssl));
436
437 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
438 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjaminced94792016-11-14 17:12:11 +0900439 CBS_init(out, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400440}
441
David Benjamin4497e582016-07-27 17:51:49 -0400442void dtls1_release_current_message(SSL *ssl, int free_buffer) {
443 if (ssl->init_msg == NULL) {
444 return;
445 }
446
447 assert(dtls1_is_current_message_complete(ssl));
448 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
449 dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]);
450 ssl->d1->incoming_messages[index] = NULL;
451 ssl->d1->handshake_read_seq++;
452
453 ssl->init_msg = NULL;
454 ssl->init_num = 0;
455}
456
David Benjamin9d632f42016-06-23 17:57:19 -0400457void dtls_clear_incoming_messages(SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400458 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin9d632f42016-06-23 17:57:19 -0400459 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
460 ssl->d1->incoming_messages[i] = NULL;
461 }
462}
463
David Benjamin61672812016-07-14 23:10:43 -0400464int dtls_has_incoming_messages(const SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400465 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
466 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin4497e582016-07-27 17:51:49 -0400467 /* Skip the current message. */
468 if (ssl->init_msg != NULL && i == current) {
469 assert(dtls1_is_current_message_complete(ssl));
470 continue;
471 }
472 if (ssl->d1->incoming_messages[i] != NULL) {
David Benjamin61672812016-07-14 23:10:43 -0400473 return 1;
474 }
475 }
476 return 0;
477}
478
David Benjamin9d632f42016-06-23 17:57:19 -0400479int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
480 CBS *out_body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500481 OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st));
David Benjamin9d632f42016-06-23 17:57:19 -0400482
483 if (!CBS_get_u8(cbs, &out_hdr->type) ||
484 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
485 !CBS_get_u16(cbs, &out_hdr->seq) ||
486 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
487 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
488 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
489 return 0;
490 }
491
492 return 1;
493}
494
495
496/* Sending handshake messages. */
497
David Benjamin75836432016-06-17 18:48:29 -0400498void dtls_clear_outgoing_messages(SSL *ssl) {
David Benjamin54091232016-09-05 12:47:25 -0400499 for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) {
David Benjamin75836432016-06-17 18:48:29 -0400500 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
501 ssl->d1->outgoing_messages[i].data = NULL;
502 }
503 ssl->d1->outgoing_messages_len = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -0500504 ssl->d1->outgoing_written = 0;
505 ssl->d1->outgoing_offset = 0;
David Benjamin75836432016-06-17 18:48:29 -0400506}
507
508int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
509 /* Pick a modest size hint to save most of the |realloc| calls. */
510 if (!CBB_init(cbb, 64) ||
511 !CBB_add_u8(cbb, type) ||
512 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
513 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
514 !CBB_add_u24(cbb, 0 /* offset */) ||
515 !CBB_add_u24_length_prefixed(cbb, body)) {
516 return 0;
517 }
518
519 return 1;
520}
521
Steven Valdez5eead162016-11-11 22:23:25 -0500522int dtls1_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
523 size_t *out_len) {
David Benjaminba1660b2016-11-12 14:26:19 +0900524 *out_msg = NULL;
Steven Valdez5eead162016-11-11 22:23:25 -0500525 if (!CBB_finish(cbb, out_msg, out_len) ||
526 *out_len < DTLS1_HM_HEADER_LENGTH) {
David Benjamin75836432016-06-17 18:48:29 -0400527 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez5eead162016-11-11 22:23:25 -0500528 OPENSSL_free(*out_msg);
David Benjamin75836432016-06-17 18:48:29 -0400529 return 0;
530 }
531
532 /* Fix up the header. Copy the fragment length into the total message
533 * length. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500534 OPENSSL_memcpy(*out_msg + 1, *out_msg + DTLS1_HM_HEADER_LENGTH - 3, 3);
Steven Valdez5eead162016-11-11 22:23:25 -0500535 return 1;
536}
David Benjamin75836432016-06-17 18:48:29 -0400537
David Benjamin1a999cf2017-01-03 10:30:35 -0500538/* add_outgoing adds a new handshake message or ChangeCipherSpec to the current
539 * outgoing flight. It returns one on success and zero on error. In both cases,
540 * it takes ownership of |data| and releases it with |OPENSSL_free| when
541 * done. */
542static int add_outgoing(SSL *ssl, int is_ccs, uint8_t *data, size_t len) {
David Benjamina3d76d02017-07-14 19:36:07 -0400543 static_assert(SSL_MAX_HANDSHAKE_FLIGHT <
544 (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)),
545 "outgoing_messages_len is too small");
David Benjamin1a999cf2017-01-03 10:30:35 -0500546 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
547 assert(0);
548 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
549 OPENSSL_free(data);
550 return 0;
551 }
David Benjamin75836432016-06-17 18:48:29 -0400552
David Benjamin1a999cf2017-01-03 10:30:35 -0500553 if (!is_ccs) {
Steven Valdez908ac192017-01-12 13:17:07 -0500554 /* TODO(svaldez): Move this up a layer to fix abstraction for SSL_TRANSCRIPT
555 * on hs. */
556 if (ssl->s3->hs != NULL &&
557 !SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript, data, len)) {
558 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
559 OPENSSL_free(data);
560 return 0;
561 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500562 ssl->d1->handshake_write_seq++;
563 }
564
565 DTLS_OUTGOING_MESSAGE *msg =
566 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
567 msg->data = data;
568 msg->len = len;
569 msg->epoch = ssl->d1->w_epoch;
570 msg->is_ccs = is_ccs;
571
572 ssl->d1->outgoing_messages_len++;
573 return 1;
574}
575
David Benjamindaf207a2017-01-03 18:37:41 -0500576int dtls1_add_message(SSL *ssl, uint8_t *data, size_t len) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500577 return add_outgoing(ssl, 0 /* handshake */, data, len);
David Benjamin75836432016-06-17 18:48:29 -0400578}
579
David Benjamindaf207a2017-01-03 18:37:41 -0500580int dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500581 return add_outgoing(ssl, 1 /* ChangeCipherSpec */, NULL, 0);
582}
583
David Benjamindaf207a2017-01-03 18:37:41 -0500584int dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
585 /* The |add_alert| path is only used for warning alerts for now, which DTLS
586 * never sends. This will be implemented later once closure alerts are
587 * converted. */
588 assert(0);
589 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
590 return 0;
591}
592
David Benjamin1a999cf2017-01-03 10:30:35 -0500593/* dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above
594 * the minimum. */
595static void dtls1_update_mtu(SSL *ssl) {
596 /* TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the
597 * only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use
598 * |SSL_set_mtu|. Does this need to be so complex? */
599 if (ssl->d1->mtu < dtls1_min_mtu() &&
600 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
601 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
602 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
603 ssl->d1->mtu = (unsigned)mtu;
604 } else {
605 ssl->d1->mtu = kDefaultMTU;
606 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
607 }
608 }
609
610 /* The MTU should be above the minimum now. */
611 assert(ssl->d1->mtu >= dtls1_min_mtu());
612}
613
614enum seal_result_t {
615 seal_error,
616 seal_no_progress,
617 seal_partial,
618 seal_success,
619};
620
621/* seal_next_message seals |msg|, which must be the next message, to |out|. If
622 * progress was made, it returns |seal_partial| or |seal_success| and sets
623 * |*out_len| to the number of bytes written. */
624static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
625 size_t *out_len, size_t max_out,
626 const DTLS_OUTGOING_MESSAGE *msg) {
627 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
628 assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
629
David Benjamin3e3090d2015-04-05 12:48:30 -0400630 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400631 if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400632 use_epoch = dtls1_use_previous_epoch;
David Benjamincfc11c22017-07-18 22:45:18 -0400633 } else if (msg->epoch != ssl->d1->w_epoch) {
634 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
635 return seal_error;
Adam Langley71d8a082014-12-13 16:28:18 -0800636 }
David Benjamincfc11c22017-07-18 22:45:18 -0400637
David Benjamin1a999cf2017-01-03 10:30:35 -0500638 size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
639 size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800640
David Benjamin29a83c52016-06-17 19:12:54 -0400641 if (msg->is_ccs) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500642 /* Check there is room for the ChangeCipherSpec. */
643 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
644 if (max_out < sizeof(kChangeCipherSpec) + overhead) {
645 return seal_no_progress;
646 }
647
648 if (!dtls_seal_record(ssl, out, out_len, max_out,
649 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
650 sizeof(kChangeCipherSpec), use_epoch)) {
651 return seal_error;
652 }
653
654 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
655 kChangeCipherSpec, sizeof(kChangeCipherSpec));
656 return seal_success;
David Benjamina97b7372015-11-03 14:54:39 -0500657 }
658
David Benjamin1a999cf2017-01-03 10:30:35 -0500659 /* DTLS messages are serialized as a single fragment in |msg|. */
660 CBS cbs, body;
661 struct hm_header_st hdr;
662 CBS_init(&cbs, msg->data, msg->len);
663 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
664 hdr.frag_off != 0 ||
665 hdr.frag_len != CBS_len(&body) ||
666 hdr.msg_len != CBS_len(&body) ||
667 !CBS_skip(&body, ssl->d1->outgoing_offset) ||
668 CBS_len(&cbs) != 0) {
669 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
670 return seal_error;
671 }
672
673 /* Determine how much progress can be made. */
674 if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) {
675 return seal_no_progress;
676 }
677 size_t todo = CBS_len(&body);
678 if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) {
679 todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead;
680 }
681
682 /* Assemble a fragment, to be sealed in-place. */
David Benjamin1386aad2017-07-19 23:57:40 -0400683 ScopedCBB cbb;
David Benjamin1a999cf2017-01-03 10:30:35 -0500684 uint8_t *frag = out + prefix;
685 size_t max_frag = max_out - prefix, frag_len;
David Benjamin1386aad2017-07-19 23:57:40 -0400686 if (!CBB_init_fixed(cbb.get(), frag, max_frag) ||
687 !CBB_add_u8(cbb.get(), hdr.type) ||
688 !CBB_add_u24(cbb.get(), hdr.msg_len) ||
689 !CBB_add_u16(cbb.get(), hdr.seq) ||
690 !CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) ||
691 !CBB_add_u24(cbb.get(), todo) ||
692 !CBB_add_bytes(cbb.get(), CBS_data(&body), todo) ||
693 !CBB_finish(cbb.get(), NULL, &frag_len)) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500694 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
695 return seal_error;
696 }
697
698 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, frag, frag_len);
699
700 if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
701 out + prefix, frag_len, use_epoch)) {
702 return seal_error;
703 }
704
705 if (todo == CBS_len(&body)) {
706 /* The next message is complete. */
707 ssl->d1->outgoing_offset = 0;
708 return seal_success;
709 }
710
711 ssl->d1->outgoing_offset += todo;
712 return seal_partial;
Adam Langley71d8a082014-12-13 16:28:18 -0800713}
Adam Langley95c29f32014-06-20 12:00:00 -0700714
David Benjamin1a999cf2017-01-03 10:30:35 -0500715/* seal_next_packet writes as much of the next flight as possible to |out| and
716 * advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as
717 * appropriate. */
718static int seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len,
719 size_t max_out) {
720 int made_progress = 0;
721 size_t total = 0;
722 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
723 for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len;
724 ssl->d1->outgoing_written++) {
725 const DTLS_OUTGOING_MESSAGE *msg =
726 &ssl->d1->outgoing_messages[ssl->d1->outgoing_written];
727 size_t len;
728 enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg);
729 switch (ret) {
730 case seal_error:
731 return 0;
732
733 case seal_no_progress:
734 goto packet_full;
735
736 case seal_partial:
737 case seal_success:
738 out += len;
739 max_out -= len;
740 total += len;
741 made_progress = 1;
742
743 if (ret == seal_partial) {
744 goto packet_full;
745 }
746 break;
747 }
David Benjamin30152fd2016-05-05 20:45:48 -0400748 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500749
750packet_full:
751 /* The MTU was too small to make any progress. */
752 if (!made_progress) {
753 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
754 return 0;
755 }
756
757 *out_len = total;
758 return 1;
759}
760
761int dtls1_flush_flight(SSL *ssl) {
762 dtls1_update_mtu(ssl);
Adam Langleybcc4e232015-02-19 15:51:58 -0800763
David Benjamin30152fd2016-05-05 20:45:48 -0400764 int ret = -1;
David Benjamine8703a32017-07-09 16:17:55 -0400765 uint8_t *packet = (uint8_t *)OPENSSL_malloc(ssl->d1->mtu);
David Benjamin1a999cf2017-01-03 10:30:35 -0500766 if (packet == NULL) {
767 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
768 goto err;
769 }
770
771 while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) {
772 uint8_t old_written = ssl->d1->outgoing_written;
773 uint32_t old_offset = ssl->d1->outgoing_offset;
774
775 size_t packet_len;
776 if (!seal_next_packet(ssl, packet, &packet_len, ssl->d1->mtu)) {
777 goto err;
778 }
779
780 int bio_ret = BIO_write(ssl->wbio, packet, packet_len);
781 if (bio_ret <= 0) {
782 /* Retry this packet the next time around. */
783 ssl->d1->outgoing_written = old_written;
784 ssl->d1->outgoing_offset = old_offset;
785 ssl->rwstate = SSL_WRITING;
786 ret = bio_ret;
David Benjamin30152fd2016-05-05 20:45:48 -0400787 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800788 }
789 }
790
David Benjamin42e3e192017-01-27 10:06:07 -0500791 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin27309552016-05-05 21:17:53 -0400792 ssl->rwstate = SSL_WRITING;
David Benjamin42e3e192017-01-27 10:06:07 -0500793 goto err;
David Benjamin27309552016-05-05 21:17:53 -0400794 }
David Benjamin30152fd2016-05-05 20:45:48 -0400795
David Benjamin42e3e192017-01-27 10:06:07 -0500796 ret = 1;
797
David Benjamin30152fd2016-05-05 20:45:48 -0400798err:
David Benjamin1a999cf2017-01-03 10:30:35 -0500799 OPENSSL_free(packet);
David Benjamin30152fd2016-05-05 20:45:48 -0400800 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800801}
802
David Benjamin1a999cf2017-01-03 10:30:35 -0500803int dtls1_retransmit_outgoing_messages(SSL *ssl) {
804 /* Rewind to the start of the flight and write it again.
805 *
806 * TODO(davidben): This does not allow retransmits to be resumed on
807 * non-blocking write. */
808 ssl->d1->outgoing_written = 0;
809 ssl->d1->outgoing_offset = 0;
810
811 return dtls1_flush_flight(ssl);
David Benjamina97b7372015-11-03 14:54:39 -0500812}
813
Adam Langley71d8a082014-12-13 16:28:18 -0800814unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500815 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800816}
David Benjamin86e95b82017-07-18 16:34:25 -0400817
818} // namespace bssl