blob: bbb6b328303f4b921115f04ff4f8aae1f7c56b63 [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>
David Benjamin1a999cf2017-01-03 10:30:35 -0500125#include <openssl/type_check.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/x509.h>
127
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 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 Benjaminc99807d2015-09-12 18:21:35 -0400157 hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800158 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400159 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800160 return NULL;
161 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500162 OPENSSL_memset(frag, 0, sizeof(hm_fragment));
David Benjamin528bd262016-07-08 09:34:05 -0700163 frag->type = msg_hdr->type;
164 frag->seq = msg_hdr->seq;
165 frag->msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin528bd262016-07-08 09:34:05 -0700167 /* Allocate space for the reassembled message and fill in the header. */
168 frag->data = OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len);
169 if (frag->data == NULL) {
170 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
171 goto err;
172 }
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin528bd262016-07-08 09:34:05 -0700174 CBB cbb;
175 if (!CBB_init_fixed(&cbb, frag->data, DTLS1_HM_HEADER_LENGTH) ||
176 !CBB_add_u8(&cbb, msg_hdr->type) ||
177 !CBB_add_u24(&cbb, msg_hdr->msg_len) ||
178 !CBB_add_u16(&cbb, msg_hdr->seq) ||
179 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
180 !CBB_add_u24(&cbb, msg_hdr->msg_len) ||
181 !CBB_finish(&cbb, NULL, NULL)) {
182 CBB_cleanup(&cbb);
183 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 Benjamin29a83c52016-06-17 19:12:54 -0400195 frag->reassembly = OPENSSL_malloc(bitmask_len);
196 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
301/* dtls1_process_handshake_record reads a handshake record and processes it. It
302 * returns one if the record was successfully processed and 0 or -1 on error. */
303static int dtls1_process_handshake_record(SSL *ssl) {
304 SSL3_RECORD *rr = &ssl->s3->rrec;
305
306start:
307 if (rr->length == 0) {
308 int ret = dtls1_get_record(ssl);
309 if (ret <= 0) {
310 return ret;
311 }
312 }
313
314 /* Cross-epoch records are discarded, but we may receive out-of-order
David Benjamin1a999cf2017-01-03 10:30:35 -0500315 * application data between ChangeCipherSpec and Finished or a
316 * ChangeCipherSpec before the appropriate point in the handshake. Those must
317 * be silently discarded.
David Benjamin9d632f42016-06-23 17:57:19 -0400318 *
319 * However, only allow the out-of-order records in the correct epoch.
320 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
321 * the unencrypted epoch (we never renegotiate). Other cases fall through and
322 * fail with a fatal error. */
323 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
324 ssl->s3->aead_read_ctx != NULL) ||
325 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
326 ssl->s3->aead_read_ctx == NULL)) {
327 rr->length = 0;
328 goto start;
329 }
330
331 if (rr->type != SSL3_RT_HANDSHAKE) {
332 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
333 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
334 return -1;
335 }
336
337 CBS cbs;
338 CBS_init(&cbs, rr->data, rr->length);
339
340 while (CBS_len(&cbs) > 0) {
341 /* Read a handshake fragment. */
342 struct hm_header_st msg_hdr;
343 CBS body;
344 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
345 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
346 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
347 return -1;
348 }
349
350 const size_t frag_off = msg_hdr.frag_off;
351 const size_t frag_len = msg_hdr.frag_len;
352 const size_t msg_len = msg_hdr.msg_len;
353 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
354 frag_off + frag_len > msg_len ||
355 msg_len > ssl_max_handshake_message_len(ssl)) {
356 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
357 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
358 return -1;
359 }
360
David Benjamin02edcd02016-07-27 17:40:37 -0400361 /* The encrypted epoch in DTLS has only one handshake message. */
362 if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) {
363 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
364 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
365 return -1;
366 }
367
David Benjamin9d632f42016-06-23 17:57:19 -0400368 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
369 msg_hdr.seq >
370 (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) {
371 /* Ignore fragments from the past, or ones too far in the future. */
372 continue;
373 }
374
375 hm_fragment *frag = dtls1_get_incoming_message(ssl, &msg_hdr);
376 if (frag == NULL) {
377 return -1;
378 }
David Benjamin528bd262016-07-08 09:34:05 -0700379 assert(frag->msg_len == msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400380
381 if (frag->reassembly == NULL) {
382 /* The message is already assembled. */
383 continue;
384 }
385 assert(msg_len > 0);
386
387 /* Copy the body into the fragment. */
David Benjamin1a999cf2017-01-03 10:30:35 -0500388 OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off,
389 CBS_data(&body), CBS_len(&body));
David Benjamin9d632f42016-06-23 17:57:19 -0400390 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
391 }
392
393 rr->length = 0;
394 ssl_read_buffer_discard(ssl);
395 return 1;
396}
397
David Benjamin09eb6552016-07-08 14:32:11 -0700398int dtls1_get_message(SSL *ssl, int msg_type,
399 enum ssl_hash_message_t hash_message) {
David Benjamin9d632f42016-06-23 17:57:19 -0400400 if (ssl->s3->tmp.reuse_message) {
401 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
402 * ssl_dont_hash_message would have to have been applied to the previous
403 * call. */
404 assert(hash_message == ssl_hash_message);
David Benjamin4497e582016-07-27 17:51:49 -0400405 assert(ssl->init_msg != NULL);
David Benjamin528bd262016-07-08 09:34:05 -0700406
David Benjamin9d632f42016-06-23 17:57:19 -0400407 ssl->s3->tmp.reuse_message = 0;
David Benjamin528bd262016-07-08 09:34:05 -0700408 hash_message = ssl_dont_hash_message;
David Benjamin4497e582016-07-27 17:51:49 -0400409 } else {
410 dtls1_release_current_message(ssl, 0 /* don't free buffer */);
David Benjamin9d632f42016-06-23 17:57:19 -0400411 }
412
David Benjamin528bd262016-07-08 09:34:05 -0700413 /* Process handshake records until the current message is ready. */
414 while (!dtls1_is_current_message_complete(ssl)) {
David Benjamin9d632f42016-06-23 17:57:19 -0400415 int ret = dtls1_process_handshake_record(ssl);
416 if (ret <= 0) {
David Benjamin9d632f42016-06-23 17:57:19 -0400417 return ret;
418 }
419 }
420
David Benjamin528bd262016-07-08 09:34:05 -0700421 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
422 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin9d632f42016-06-23 17:57:19 -0400423 assert(frag != NULL);
424 assert(frag->reassembly == NULL);
David Benjamin528bd262016-07-08 09:34:05 -0700425 assert(ssl->d1->handshake_read_seq == frag->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400426
427 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
428 * |ssl_get_message| API. */
David Benjamin528bd262016-07-08 09:34:05 -0700429 ssl->s3->tmp.message_type = frag->type;
430 ssl->init_msg = frag->data + DTLS1_HM_HEADER_LENGTH;
431 ssl->init_num = frag->msg_len;
David Benjamin9d632f42016-06-23 17:57:19 -0400432
433 if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
David Benjamin528bd262016-07-08 09:34:05 -0700434 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin9d632f42016-06-23 17:57:19 -0400435 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin528bd262016-07-08 09:34:05 -0700436 return -1;
David Benjamin9d632f42016-06-23 17:57:19 -0400437 }
David Benjaminced94792016-11-14 17:12:11 +0900438 if (hash_message == ssl_hash_message && !ssl_hash_current_message(ssl)) {
David Benjamin528bd262016-07-08 09:34:05 -0700439 return -1;
David Benjamin9d632f42016-06-23 17:57:19 -0400440 }
441
David Benjaminc0279992016-09-19 20:15:07 -0400442 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, frag->data,
443 ssl->init_num + DTLS1_HM_HEADER_LENGTH);
David Benjamin09eb6552016-07-08 14:32:11 -0700444 return 1;
David Benjamin528bd262016-07-08 09:34:05 -0700445}
David Benjamin9d632f42016-06-23 17:57:19 -0400446
David Benjaminced94792016-11-14 17:12:11 +0900447void dtls1_get_current_message(const SSL *ssl, CBS *out) {
David Benjamin528bd262016-07-08 09:34:05 -0700448 assert(dtls1_is_current_message_complete(ssl));
449
450 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
451 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjaminced94792016-11-14 17:12:11 +0900452 CBS_init(out, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400453}
454
David Benjamin4497e582016-07-27 17:51:49 -0400455void dtls1_release_current_message(SSL *ssl, int free_buffer) {
456 if (ssl->init_msg == NULL) {
457 return;
458 }
459
460 assert(dtls1_is_current_message_complete(ssl));
461 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
462 dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]);
463 ssl->d1->incoming_messages[index] = NULL;
464 ssl->d1->handshake_read_seq++;
465
466 ssl->init_msg = NULL;
467 ssl->init_num = 0;
468}
469
David Benjamin9d632f42016-06-23 17:57:19 -0400470void dtls_clear_incoming_messages(SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400471 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin9d632f42016-06-23 17:57:19 -0400472 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
473 ssl->d1->incoming_messages[i] = NULL;
474 }
475}
476
David Benjamin61672812016-07-14 23:10:43 -0400477int dtls_has_incoming_messages(const SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400478 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
479 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin4497e582016-07-27 17:51:49 -0400480 /* Skip the current message. */
481 if (ssl->init_msg != NULL && i == current) {
482 assert(dtls1_is_current_message_complete(ssl));
483 continue;
484 }
485 if (ssl->d1->incoming_messages[i] != NULL) {
David Benjamin61672812016-07-14 23:10:43 -0400486 return 1;
487 }
488 }
489 return 0;
490}
491
David Benjamin9d632f42016-06-23 17:57:19 -0400492int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr,
493 CBS *out_body) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500494 OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st));
David Benjamin9d632f42016-06-23 17:57:19 -0400495
496 if (!CBS_get_u8(cbs, &out_hdr->type) ||
497 !CBS_get_u24(cbs, &out_hdr->msg_len) ||
498 !CBS_get_u16(cbs, &out_hdr->seq) ||
499 !CBS_get_u24(cbs, &out_hdr->frag_off) ||
500 !CBS_get_u24(cbs, &out_hdr->frag_len) ||
501 !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) {
502 return 0;
503 }
504
505 return 1;
506}
507
508
509/* Sending handshake messages. */
510
David Benjamin75836432016-06-17 18:48:29 -0400511void dtls_clear_outgoing_messages(SSL *ssl) {
David Benjamin54091232016-09-05 12:47:25 -0400512 for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) {
David Benjamin75836432016-06-17 18:48:29 -0400513 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
514 ssl->d1->outgoing_messages[i].data = NULL;
515 }
516 ssl->d1->outgoing_messages_len = 0;
David Benjamin1a999cf2017-01-03 10:30:35 -0500517 ssl->d1->outgoing_written = 0;
518 ssl->d1->outgoing_offset = 0;
David Benjamin75836432016-06-17 18:48:29 -0400519}
520
521int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
522 /* Pick a modest size hint to save most of the |realloc| calls. */
523 if (!CBB_init(cbb, 64) ||
524 !CBB_add_u8(cbb, type) ||
525 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
526 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
527 !CBB_add_u24(cbb, 0 /* offset */) ||
528 !CBB_add_u24_length_prefixed(cbb, body)) {
529 return 0;
530 }
531
532 return 1;
533}
534
Steven Valdez5eead162016-11-11 22:23:25 -0500535int dtls1_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
536 size_t *out_len) {
David Benjaminba1660b2016-11-12 14:26:19 +0900537 *out_msg = NULL;
Steven Valdez5eead162016-11-11 22:23:25 -0500538 if (!CBB_finish(cbb, out_msg, out_len) ||
539 *out_len < DTLS1_HM_HEADER_LENGTH) {
David Benjamin75836432016-06-17 18:48:29 -0400540 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez5eead162016-11-11 22:23:25 -0500541 OPENSSL_free(*out_msg);
David Benjamin75836432016-06-17 18:48:29 -0400542 return 0;
543 }
544
545 /* Fix up the header. Copy the fragment length into the total message
546 * length. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500547 OPENSSL_memcpy(*out_msg + 1, *out_msg + DTLS1_HM_HEADER_LENGTH - 3, 3);
Steven Valdez5eead162016-11-11 22:23:25 -0500548 return 1;
549}
David Benjamin75836432016-06-17 18:48:29 -0400550
David Benjamin1a999cf2017-01-03 10:30:35 -0500551/* add_outgoing adds a new handshake message or ChangeCipherSpec to the current
552 * outgoing flight. It returns one on success and zero on error. In both cases,
553 * it takes ownership of |data| and releases it with |OPENSSL_free| when
554 * done. */
555static int add_outgoing(SSL *ssl, int is_ccs, uint8_t *data, size_t len) {
556 OPENSSL_COMPILE_ASSERT(SSL_MAX_HANDSHAKE_FLIGHT <
557 (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)),
558 outgoing_messages_len_is_too_small);
559 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) {
567 ssl3_update_handshake_hash(ssl, data, len);
568 ssl->d1->handshake_write_seq++;
569 }
570
571 DTLS_OUTGOING_MESSAGE *msg =
572 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
573 msg->data = data;
574 msg->len = len;
575 msg->epoch = ssl->d1->w_epoch;
576 msg->is_ccs = is_ccs;
577
578 ssl->d1->outgoing_messages_len++;
579 return 1;
580}
581
David Benjamindaf207a2017-01-03 18:37:41 -0500582int dtls1_add_message(SSL *ssl, uint8_t *data, size_t len) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500583 return add_outgoing(ssl, 0 /* handshake */, data, len);
David Benjamin75836432016-06-17 18:48:29 -0400584}
585
586int dtls1_write_message(SSL *ssl) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500587 /* The message is written in |dtls1_flush_flight|. */
588 return 1;
David Benjamin75836432016-06-17 18:48:29 -0400589}
590
David Benjamindaf207a2017-01-03 18:37:41 -0500591int dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500592 return add_outgoing(ssl, 1 /* ChangeCipherSpec */, NULL, 0);
593}
594
David Benjamindaf207a2017-01-03 18:37:41 -0500595int dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc) {
596 /* The |add_alert| path is only used for warning alerts for now, which DTLS
597 * never sends. This will be implemented later once closure alerts are
598 * converted. */
599 assert(0);
600 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
601 return 0;
602}
603
David Benjamin1a999cf2017-01-03 10:30:35 -0500604/* dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above
605 * the minimum. */
606static void dtls1_update_mtu(SSL *ssl) {
607 /* TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the
608 * only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use
609 * |SSL_set_mtu|. Does this need to be so complex? */
610 if (ssl->d1->mtu < dtls1_min_mtu() &&
611 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
612 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
613 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
614 ssl->d1->mtu = (unsigned)mtu;
615 } else {
616 ssl->d1->mtu = kDefaultMTU;
617 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
618 }
619 }
620
621 /* The MTU should be above the minimum now. */
622 assert(ssl->d1->mtu >= dtls1_min_mtu());
623}
624
625enum seal_result_t {
626 seal_error,
627 seal_no_progress,
628 seal_partial,
629 seal_success,
630};
631
632/* seal_next_message seals |msg|, which must be the next message, to |out|. If
633 * progress was made, it returns |seal_partial| or |seal_success| and sets
634 * |*out_len| to the number of bytes written. */
635static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out,
636 size_t *out_len, size_t max_out,
637 const DTLS_OUTGOING_MESSAGE *msg) {
638 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
639 assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]);
640
David Benjaminafbc63f2015-02-01 02:33:59 -0500641 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
642 * (negotiated cipher) exist. */
David Benjamin0d56f882015-12-19 17:05:56 -0500643 assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1);
David Benjamin29a83c52016-06-17 19:12:54 -0400644 assert(msg->epoch <= ssl->d1->w_epoch);
David Benjamin3e3090d2015-04-05 12:48:30 -0400645 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamin29a83c52016-06-17 19:12:54 -0400646 if (ssl->d1->w_epoch == 1 && msg->epoch == 0) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400647 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800648 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500649 size_t overhead = dtls_max_seal_overhead(ssl, use_epoch);
650 size_t prefix = dtls_seal_prefix_len(ssl, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800651
David Benjamin29a83c52016-06-17 19:12:54 -0400652 if (msg->is_ccs) {
David Benjamin1a999cf2017-01-03 10:30:35 -0500653 /* Check there is room for the ChangeCipherSpec. */
654 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
655 if (max_out < sizeof(kChangeCipherSpec) + overhead) {
656 return seal_no_progress;
657 }
658
659 if (!dtls_seal_record(ssl, out, out_len, max_out,
660 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
661 sizeof(kChangeCipherSpec), use_epoch)) {
662 return seal_error;
663 }
664
665 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
666 kChangeCipherSpec, sizeof(kChangeCipherSpec));
667 return seal_success;
David Benjamina97b7372015-11-03 14:54:39 -0500668 }
669
David Benjamin1a999cf2017-01-03 10:30:35 -0500670 /* DTLS messages are serialized as a single fragment in |msg|. */
671 CBS cbs, body;
672 struct hm_header_st hdr;
673 CBS_init(&cbs, msg->data, msg->len);
674 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
675 hdr.frag_off != 0 ||
676 hdr.frag_len != CBS_len(&body) ||
677 hdr.msg_len != CBS_len(&body) ||
678 !CBS_skip(&body, ssl->d1->outgoing_offset) ||
679 CBS_len(&cbs) != 0) {
680 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
681 return seal_error;
682 }
683
684 /* Determine how much progress can be made. */
685 if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) {
686 return seal_no_progress;
687 }
688 size_t todo = CBS_len(&body);
689 if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) {
690 todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead;
691 }
692
693 /* Assemble a fragment, to be sealed in-place. */
694 CBB cbb;
695 uint8_t *frag = out + prefix;
696 size_t max_frag = max_out - prefix, frag_len;
697 if (!CBB_init_fixed(&cbb, frag, max_frag) ||
698 !CBB_add_u8(&cbb, hdr.type) ||
699 !CBB_add_u24(&cbb, hdr.msg_len) ||
700 !CBB_add_u16(&cbb, hdr.seq) ||
701 !CBB_add_u24(&cbb, ssl->d1->outgoing_offset) ||
702 !CBB_add_u24(&cbb, todo) ||
703 !CBB_add_bytes(&cbb, CBS_data(&body), todo) ||
704 !CBB_finish(&cbb, NULL, &frag_len)) {
705 CBB_cleanup(&cbb);
706 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
707 return seal_error;
708 }
709
710 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, frag, frag_len);
711
712 if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE,
713 out + prefix, frag_len, use_epoch)) {
714 return seal_error;
715 }
716
717 if (todo == CBS_len(&body)) {
718 /* The next message is complete. */
719 ssl->d1->outgoing_offset = 0;
720 return seal_success;
721 }
722
723 ssl->d1->outgoing_offset += todo;
724 return seal_partial;
Adam Langley71d8a082014-12-13 16:28:18 -0800725}
Adam Langley95c29f32014-06-20 12:00:00 -0700726
David Benjamin1a999cf2017-01-03 10:30:35 -0500727/* seal_next_packet writes as much of the next flight as possible to |out| and
728 * advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as
729 * appropriate. */
730static int seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len,
731 size_t max_out) {
732 int made_progress = 0;
733 size_t total = 0;
734 assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len);
735 for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len;
736 ssl->d1->outgoing_written++) {
737 const DTLS_OUTGOING_MESSAGE *msg =
738 &ssl->d1->outgoing_messages[ssl->d1->outgoing_written];
739 size_t len;
740 enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg);
741 switch (ret) {
742 case seal_error:
743 return 0;
744
745 case seal_no_progress:
746 goto packet_full;
747
748 case seal_partial:
749 case seal_success:
750 out += len;
751 max_out -= len;
752 total += len;
753 made_progress = 1;
754
755 if (ret == seal_partial) {
756 goto packet_full;
757 }
758 break;
759 }
David Benjamin30152fd2016-05-05 20:45:48 -0400760 }
David Benjamin1a999cf2017-01-03 10:30:35 -0500761
762packet_full:
763 /* The MTU was too small to make any progress. */
764 if (!made_progress) {
765 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
766 return 0;
767 }
768
769 *out_len = total;
770 return 1;
771}
772
773int dtls1_flush_flight(SSL *ssl) {
774 dtls1_update_mtu(ssl);
Adam Langleybcc4e232015-02-19 15:51:58 -0800775
David Benjamin30152fd2016-05-05 20:45:48 -0400776 int ret = -1;
David Benjamin1a999cf2017-01-03 10:30:35 -0500777 uint8_t *packet = OPENSSL_malloc(ssl->d1->mtu);
778 if (packet == NULL) {
779 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
780 goto err;
781 }
782
783 while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) {
784 uint8_t old_written = ssl->d1->outgoing_written;
785 uint32_t old_offset = ssl->d1->outgoing_offset;
786
787 size_t packet_len;
788 if (!seal_next_packet(ssl, packet, &packet_len, ssl->d1->mtu)) {
789 goto err;
790 }
791
792 int bio_ret = BIO_write(ssl->wbio, packet, packet_len);
793 if (bio_ret <= 0) {
794 /* Retry this packet the next time around. */
795 ssl->d1->outgoing_written = old_written;
796 ssl->d1->outgoing_offset = old_offset;
797 ssl->rwstate = SSL_WRITING;
798 ret = bio_ret;
David Benjamin30152fd2016-05-05 20:45:48 -0400799 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800800 }
801 }
802
David Benjamin2f871122016-05-20 14:27:17 -0400803 ret = BIO_flush(ssl->wbio);
David Benjamin27309552016-05-05 21:17:53 -0400804 if (ret <= 0) {
805 ssl->rwstate = SSL_WRITING;
David Benjamin27309552016-05-05 21:17:53 -0400806 }
David Benjamin30152fd2016-05-05 20:45:48 -0400807
808err:
David Benjamin1a999cf2017-01-03 10:30:35 -0500809 OPENSSL_free(packet);
David Benjamin30152fd2016-05-05 20:45:48 -0400810 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800811}
812
David Benjamin1a999cf2017-01-03 10:30:35 -0500813int dtls1_retransmit_outgoing_messages(SSL *ssl) {
814 /* Rewind to the start of the flight and write it again.
815 *
816 * TODO(davidben): This does not allow retransmits to be resumed on
817 * non-blocking write. */
818 ssl->d1->outgoing_written = 0;
819 ssl->d1->outgoing_offset = 0;
820
821 return dtls1_flush_flight(ssl);
David Benjamina97b7372015-11-03 14:54:39 -0500822}
823
Adam Langley71d8a082014-12-13 16:28:18 -0800824unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500825 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800826}