blob: 331703ad19914a47cdfcd50551552f15fef51169 [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>
125#include <openssl/x509.h>
126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Adam Langley95c29f32014-06-20 12:00:00 -0700129
David Benjamina18b6712015-01-11 19:31:22 -0500130/* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
131 * for these values? Notably, why is kMinMTU a function of the transport
132 * protocol's overhead rather than, say, what's needed to hold a minimally-sized
133 * handshake fragment plus protocol overhead. */
Adam Langley95c29f32014-06-20 12:00:00 -0700134
David Benjamina18b6712015-01-11 19:31:22 -0500135/* kMinMTU is the minimum acceptable MTU value. */
136static const unsigned int kMinMTU = 256 - 28;
137
138/* kDefaultMTU is the default MTU value to use if neither the user nor
139 * the underlying BIO supplies one. */
140static const unsigned int kDefaultMTU = 1500 - 28;
141
David Benjamin9d632f42016-06-23 17:57:19 -0400142
143/* Receiving handshake messages. */
144
David Benjaminec847ce2016-06-17 19:30:47 -0400145static void dtls1_hm_fragment_free(hm_fragment *frag) {
146 if (frag == NULL) {
147 return;
148 }
David Benjamin528bd262016-07-08 09:34:05 -0700149 OPENSSL_free(frag->data);
David Benjaminec847ce2016-06-17 19:30:47 -0400150 OPENSSL_free(frag->reassembly);
151 OPENSSL_free(frag);
152}
153
David Benjamin528bd262016-07-08 09:34:05 -0700154static hm_fragment *dtls1_hm_fragment_new(const struct hm_header_st *msg_hdr) {
David Benjaminc99807d2015-09-12 18:21:35 -0400155 hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800156 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400157 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800158 return NULL;
159 }
David Benjaminc99807d2015-09-12 18:21:35 -0400160 memset(frag, 0, sizeof(hm_fragment));
David Benjamin528bd262016-07-08 09:34:05 -0700161 frag->type = msg_hdr->type;
162 frag->seq = msg_hdr->seq;
163 frag->msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamin528bd262016-07-08 09:34:05 -0700165 /* Allocate space for the reassembled message and fill in the header. */
166 frag->data = OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len);
167 if (frag->data == NULL) {
168 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
169 goto err;
170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjamin528bd262016-07-08 09:34:05 -0700172 CBB cbb;
173 if (!CBB_init_fixed(&cbb, frag->data, DTLS1_HM_HEADER_LENGTH) ||
174 !CBB_add_u8(&cbb, msg_hdr->type) ||
175 !CBB_add_u24(&cbb, msg_hdr->msg_len) ||
176 !CBB_add_u16(&cbb, msg_hdr->seq) ||
177 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
178 !CBB_add_u24(&cbb, msg_hdr->msg_len) ||
179 !CBB_finish(&cbb, NULL, NULL)) {
180 CBB_cleanup(&cbb);
181 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
182 goto err;
183 }
184
185 /* If the handshake message is empty, |frag->reassembly| is NULL. */
186 if (msg_hdr->msg_len > 0) {
David Benjamin29a83c52016-06-17 19:12:54 -0400187 /* Initialize reassembly bitmask. */
David Benjamin528bd262016-07-08 09:34:05 -0700188 if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) {
David Benjamin29a83c52016-06-17 19:12:54 -0400189 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
190 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800191 }
David Benjamin528bd262016-07-08 09:34:05 -0700192 size_t bitmask_len = (msg_hdr->msg_len + 7) / 8;
David Benjamin29a83c52016-06-17 19:12:54 -0400193 frag->reassembly = OPENSSL_malloc(bitmask_len);
194 if (frag->reassembly == NULL) {
195 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
196 goto err;
197 }
198 memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langley71d8a082014-12-13 16:28:18 -0800201 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400202
203err:
204 dtls1_hm_fragment_free(frag);
205 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800206}
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjaminee562b92015-03-02 20:52:52 -0500208/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
209 * exclusive, set. */
David Benjamindca125e2016-06-23 17:52:47 -0400210static uint8_t bit_range(size_t start, size_t end) {
David Benjaminee562b92015-03-02 20:52:52 -0500211 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
212}
213
214/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
215 * as received in |frag|. If |frag| becomes complete, it clears
216 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
217 * and |frag->reassembly| must not be NULL. */
218static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
219 size_t end) {
220 size_t i;
David Benjamin528bd262016-07-08 09:34:05 -0700221 size_t msg_len = frag->msg_len;
David Benjaminee562b92015-03-02 20:52:52 -0500222
223 if (frag->reassembly == NULL || start > end || end > msg_len) {
224 assert(0);
225 return;
226 }
227 /* A zero-length message will never have a pending reassembly. */
228 assert(msg_len > 0);
229
230 if ((start >> 3) == (end >> 3)) {
231 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
232 } else {
233 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
234 for (i = (start >> 3) + 1; i < (end >> 3); i++) {
235 frag->reassembly[i] = 0xff;
236 }
237 if ((end & 7) != 0) {
238 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
239 }
240 }
241
242 /* Check if the fragment is complete. */
243 for (i = 0; i < (msg_len >> 3); i++) {
244 if (frag->reassembly[i] != 0xff) {
245 return;
246 }
247 }
248 if ((msg_len & 7) != 0 &&
249 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
250 return;
251 }
252
253 OPENSSL_free(frag->reassembly);
254 frag->reassembly = NULL;
255}
256
David Benjamin528bd262016-07-08 09:34:05 -0700257/* dtls1_is_current_message_complete returns one if the current handshake
258 * message is complete and zero otherwise. */
David Benjamin61672812016-07-14 23:10:43 -0400259static int dtls1_is_current_message_complete(const SSL *ssl) {
David Benjamin9d632f42016-06-23 17:57:19 -0400260 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
261 SSL_MAX_HANDSHAKE_FLIGHT];
262 return frag != NULL && frag->reassembly == NULL;
263}
264
David Benjamin528bd262016-07-08 09:34:05 -0700265/* dtls1_pop_message removes the current handshake message, which must be
266 * complete, and advances to the next one. */
267static void dtls1_pop_message(SSL *ssl) {
268 assert(dtls1_is_current_message_complete(ssl));
269
270 size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
271 dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]);
272 ssl->d1->incoming_messages[index] = NULL;
273 ssl->d1->handshake_read_seq++;
274}
275
David Benjamin9d632f42016-06-23 17:57:19 -0400276/* dtls1_get_incoming_message returns the incoming message corresponding to
277 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
278 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
279 * returns NULL on failure. The caller does not take ownership of the result. */
280static hm_fragment *dtls1_get_incoming_message(
281 SSL *ssl, const struct hm_header_st *msg_hdr) {
282 if (msg_hdr->seq < ssl->d1->handshake_read_seq ||
283 msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) {
284 return NULL;
285 }
286
287 size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT;
288 hm_fragment *frag = ssl->d1->incoming_messages[idx];
289 if (frag != NULL) {
David Benjamin528bd262016-07-08 09:34:05 -0700290 assert(frag->seq == msg_hdr->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400291 /* The new fragment must be compatible with the previous fragments from this
292 * message. */
David Benjamin528bd262016-07-08 09:34:05 -0700293 if (frag->type != msg_hdr->type ||
294 frag->msg_len != msg_hdr->msg_len) {
David Benjamin9d632f42016-06-23 17:57:19 -0400295 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
296 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
297 return NULL;
298 }
299 return frag;
300 }
301
302 /* This is the first fragment from this message. */
David Benjamin528bd262016-07-08 09:34:05 -0700303 frag = dtls1_hm_fragment_new(msg_hdr);
David Benjamin9d632f42016-06-23 17:57:19 -0400304 if (frag == NULL) {
305 return NULL;
306 }
David Benjamin9d632f42016-06-23 17:57:19 -0400307 ssl->d1->incoming_messages[idx] = frag;
308 return frag;
309}
310
311/* dtls1_process_handshake_record reads a handshake record and processes it. It
312 * returns one if the record was successfully processed and 0 or -1 on error. */
313static int dtls1_process_handshake_record(SSL *ssl) {
314 SSL3_RECORD *rr = &ssl->s3->rrec;
315
316start:
317 if (rr->length == 0) {
318 int ret = dtls1_get_record(ssl);
319 if (ret <= 0) {
320 return ret;
321 }
322 }
323
324 /* Cross-epoch records are discarded, but we may receive out-of-order
325 * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
326 * before the appropriate point in the handshake. Those must be silently
327 * discarded.
328 *
329 * However, only allow the out-of-order records in the correct epoch.
330 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
331 * the unencrypted epoch (we never renegotiate). Other cases fall through and
332 * fail with a fatal error. */
333 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
334 ssl->s3->aead_read_ctx != NULL) ||
335 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
336 ssl->s3->aead_read_ctx == NULL)) {
337 rr->length = 0;
338 goto start;
339 }
340
341 if (rr->type != SSL3_RT_HANDSHAKE) {
342 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
343 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
344 return -1;
345 }
346
347 CBS cbs;
348 CBS_init(&cbs, rr->data, rr->length);
349
350 while (CBS_len(&cbs) > 0) {
351 /* Read a handshake fragment. */
352 struct hm_header_st msg_hdr;
353 CBS body;
354 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
355 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
356 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
357 return -1;
358 }
359
360 const size_t frag_off = msg_hdr.frag_off;
361 const size_t frag_len = msg_hdr.frag_len;
362 const size_t msg_len = msg_hdr.msg_len;
363 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
364 frag_off + frag_len > msg_len ||
365 msg_len > ssl_max_handshake_message_len(ssl)) {
366 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
367 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
368 return -1;
369 }
370
371 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 Benjamin528bd262016-07-08 09:34:05 -0700391 memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off, CBS_data(&body),
392 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 Benjamin09eb6552016-07-08 14:32:11 -0700401int dtls1_get_message(SSL *ssl, int msg_type,
402 enum ssl_hash_message_t hash_message) {
David Benjamin9d632f42016-06-23 17:57:19 -0400403 if (ssl->s3->tmp.reuse_message) {
404 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
405 * ssl_dont_hash_message would have to have been applied to the previous
406 * call. */
407 assert(hash_message == ssl_hash_message);
David Benjamin528bd262016-07-08 09:34:05 -0700408 assert(dtls1_is_current_message_complete(ssl));
409
David Benjamin9d632f42016-06-23 17:57:19 -0400410 ssl->s3->tmp.reuse_message = 0;
David Benjamin528bd262016-07-08 09:34:05 -0700411 hash_message = ssl_dont_hash_message;
412 } else if (dtls1_is_current_message_complete(ssl)) {
413 dtls1_pop_message(ssl);
David Benjamin9d632f42016-06-23 17:57:19 -0400414 }
415
David Benjamin528bd262016-07-08 09:34:05 -0700416 /* Process handshake records until the current message is ready. */
417 while (!dtls1_is_current_message_complete(ssl)) {
David Benjamin9d632f42016-06-23 17:57:19 -0400418 int ret = dtls1_process_handshake_record(ssl);
419 if (ret <= 0) {
David Benjamin9d632f42016-06-23 17:57:19 -0400420 return ret;
421 }
422 }
423
David Benjamin528bd262016-07-08 09:34:05 -0700424 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
425 SSL_MAX_HANDSHAKE_FLIGHT];
David Benjamin9d632f42016-06-23 17:57:19 -0400426 assert(frag != NULL);
427 assert(frag->reassembly == NULL);
David Benjamin528bd262016-07-08 09:34:05 -0700428 assert(ssl->d1->handshake_read_seq == frag->seq);
David Benjamin9d632f42016-06-23 17:57:19 -0400429
430 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
431 * |ssl_get_message| API. */
David Benjamin528bd262016-07-08 09:34:05 -0700432 ssl->s3->tmp.message_type = frag->type;
433 ssl->init_msg = frag->data + DTLS1_HM_HEADER_LENGTH;
434 ssl->init_num = frag->msg_len;
David Benjamin9d632f42016-06-23 17:57:19 -0400435
436 if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
David Benjamin528bd262016-07-08 09:34:05 -0700437 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin9d632f42016-06-23 17:57:19 -0400438 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin528bd262016-07-08 09:34:05 -0700439 return -1;
David Benjamin9d632f42016-06-23 17:57:19 -0400440 }
David Benjamin528bd262016-07-08 09:34:05 -0700441 if (hash_message == ssl_hash_message && !dtls1_hash_current_message(ssl)) {
442 return -1;
David Benjamin9d632f42016-06-23 17:57:19 -0400443 }
444
445 ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE,
David Benjamin528bd262016-07-08 09:34:05 -0700446 frag->data, ssl->init_num + DTLS1_HM_HEADER_LENGTH);
David Benjamin09eb6552016-07-08 14:32:11 -0700447 return 1;
David Benjamin528bd262016-07-08 09:34:05 -0700448}
David Benjamin9d632f42016-06-23 17:57:19 -0400449
David Benjamin528bd262016-07-08 09:34:05 -0700450int dtls1_hash_current_message(SSL *ssl) {
451 assert(dtls1_is_current_message_complete(ssl));
452
453 hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq %
454 SSL_MAX_HANDSHAKE_FLIGHT];
455 return ssl3_update_handshake_hash(ssl, frag->data,
456 DTLS1_HM_HEADER_LENGTH + frag->msg_len);
David Benjamin9d632f42016-06-23 17:57:19 -0400457}
458
459void dtls_clear_incoming_messages(SSL *ssl) {
David Benjamin61672812016-07-14 23:10:43 -0400460 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
David Benjamin9d632f42016-06-23 17:57:19 -0400461 dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]);
462 ssl->d1->incoming_messages[i] = NULL;
463 }
464}
465
David Benjamin61672812016-07-14 23:10:43 -0400466int dtls_has_incoming_messages(const SSL *ssl) {
467 /* This function may not be called if there is a pending |dtls1_get_message|
468 * operation. */
469 assert(dtls1_is_current_message_complete(ssl));
470
471 size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT;
472 for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) {
473 if (i != current && ssl->d1->incoming_messages[i] != NULL) {
474 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) {
482 memset(out_hdr, 0x00, sizeof(struct hm_header_st));
483
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 Benjamina97b7372015-11-03 14:54:39 -0500499static void dtls1_update_mtu(SSL *ssl) {
500 /* TODO(davidben): What is this code doing and do we need it? */
501 if (ssl->d1->mtu < dtls1_min_mtu() &&
502 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin2f871122016-05-20 14:27:17 -0400503 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
David Benjamina97b7372015-11-03 14:54:39 -0500504 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
505 ssl->d1->mtu = (unsigned)mtu;
506 } else {
507 ssl->d1->mtu = kDefaultMTU;
David Benjamin2f871122016-05-20 14:27:17 -0400508 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
David Benjamina97b7372015-11-03 14:54:39 -0500509 }
510 }
511
512 /* The MTU should be above the minimum now. */
513 assert(ssl->d1->mtu >= dtls1_min_mtu());
514}
515
David Benjamin16285ea2015-11-03 15:39:45 -0500516/* dtls1_max_record_size returns the maximum record body length that may be
517 * written without exceeding the MTU. It accounts for any buffering installed on
518 * the write BIO. If no record may be written, it returns zero. */
519static size_t dtls1_max_record_size(SSL *ssl) {
520 size_t ret = ssl->d1->mtu;
521
522 size_t overhead = ssl_max_seal_overhead(ssl);
523 if (ret <= overhead) {
524 return 0;
525 }
526 ret -= overhead;
527
David Benjamin2f871122016-05-20 14:27:17 -0400528 size_t pending = BIO_wpending(ssl->wbio);
David Benjamin16285ea2015-11-03 15:39:45 -0500529 if (ret <= pending) {
530 return 0;
531 }
532 ret -= pending;
533
534 return ret;
535}
536
David Benjamina97b7372015-11-03 14:54:39 -0500537static int dtls1_write_change_cipher_spec(SSL *ssl,
538 enum dtls1_use_epoch_t use_epoch) {
539 dtls1_update_mtu(ssl);
540
541 /* During the handshake, wbio is buffered to pack messages together. Flush the
542 * buffer if the ChangeCipherSpec would not fit in a packet. */
David Benjamin16285ea2015-11-03 15:39:45 -0500543 if (dtls1_max_record_size(ssl) == 0) {
David Benjamin2f871122016-05-20 14:27:17 -0400544 int ret = BIO_flush(ssl->wbio);
David Benjamina97b7372015-11-03 14:54:39 -0500545 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500546 ssl->rwstate = SSL_WRITING;
David Benjamina97b7372015-11-03 14:54:39 -0500547 return ret;
548 }
David Benjamina97b7372015-11-03 14:54:39 -0500549 }
550
551 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
David Benjamin0d56f882015-12-19 17:05:56 -0500552 int ret =
David Benjamin45d45c12016-06-07 15:20:49 -0400553 dtls1_write_record(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
554 sizeof(kChangeCipherSpec), use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500555 if (ret <= 0) {
556 return ret;
557 }
558
David Benjamin4e9cc712016-06-01 20:16:03 -0400559 ssl_do_msg_callback(ssl, 1 /* write */, ssl->version,
560 SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
561 sizeof(kChangeCipherSpec));
David Benjamina97b7372015-11-03 14:54:39 -0500562 return 1;
563}
564
David Benjamin75836432016-06-17 18:48:29 -0400565/* dtls1_do_handshake_write writes handshake message |in| using the given epoch,
566 * starting |offset| bytes into the message body. It returns one on success. On
567 * error, it returns <= 0 and sets |*out_offset| to the number of bytes of body
568 * that were successfully written. This may be used to retry the write
569 * later. |in| must be a reassembled handshake message with the full DTLS
570 * handshake header. */
571static int dtls1_do_handshake_write(SSL *ssl, size_t *out_offset,
572 const uint8_t *in, size_t offset,
573 size_t len,
574 enum dtls1_use_epoch_t use_epoch) {
David Benjamin16285ea2015-11-03 15:39:45 -0500575 dtls1_update_mtu(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700576
David Benjamin16285ea2015-11-03 15:39:45 -0500577 int ret = -1;
578 CBB cbb;
579 CBB_zero(&cbb);
580 /* Allocate a temporary buffer to hold the message fragments to avoid
581 * clobbering the message. */
582 uint8_t *buf = OPENSSL_malloc(ssl->d1->mtu);
583 if (buf == NULL) {
584 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
David Benjaminb5eb1952016-06-17 18:44:38 -0400587 /* Although it may be sent as multiple fragments, a DTLS message must be sent
588 * serialized as a single fragment for purposes of |ssl_do_msg_callback| and
589 * the handshake hash. */
590 CBS cbs, body;
591 struct hm_header_st hdr;
592 CBS_init(&cbs, in, len);
593 if (!dtls1_parse_fragment(&cbs, &hdr, &body) ||
594 hdr.frag_off != 0 ||
595 hdr.frag_len != CBS_len(&body) ||
596 hdr.msg_len != CBS_len(&body) ||
597 !CBS_skip(&body, offset) ||
598 CBS_len(&cbs) != 0) {
599 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
600 goto err;
David Benjamin16285ea2015-11-03 15:39:45 -0500601 }
Adam Langley95c29f32014-06-20 12:00:00 -0700602
David Benjamin16285ea2015-11-03 15:39:45 -0500603 do {
604 /* During the handshake, wbio is buffered to pack messages together. Flush
605 * the buffer if there isn't enough room to make progress. */
606 if (dtls1_max_record_size(ssl) < DTLS1_HM_HEADER_LENGTH + 1) {
David Benjamin2f871122016-05-20 14:27:17 -0400607 int flush_ret = BIO_flush(ssl->wbio);
David Benjamin16285ea2015-11-03 15:39:45 -0500608 if (flush_ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500609 ssl->rwstate = SSL_WRITING;
David Benjamin16285ea2015-11-03 15:39:45 -0500610 ret = flush_ret;
611 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800612 }
David Benjamin2f871122016-05-20 14:27:17 -0400613 assert(BIO_wpending(ssl->wbio) == 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800614 }
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjamin16285ea2015-11-03 15:39:45 -0500616 size_t todo = dtls1_max_record_size(ssl);
617 if (todo < DTLS1_HM_HEADER_LENGTH + 1) {
David Benjamina97b7372015-11-03 14:54:39 -0500618 /* To make forward progress, the MTU must, at minimum, fit the handshake
619 * header and one byte of handshake body. */
620 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamin16285ea2015-11-03 15:39:45 -0500621 goto err;
622 }
623 todo -= DTLS1_HM_HEADER_LENGTH;
624
David Benjaminb5eb1952016-06-17 18:44:38 -0400625 if (todo > CBS_len(&body)) {
626 todo = CBS_len(&body);
David Benjamin16285ea2015-11-03 15:39:45 -0500627 }
628 if (todo >= (1u << 24)) {
629 todo = (1u << 24) - 1;
David Benjamina97b7372015-11-03 14:54:39 -0500630 }
631
David Benjaminb5eb1952016-06-17 18:44:38 -0400632 size_t buf_len;
David Benjamin16285ea2015-11-03 15:39:45 -0500633 if (!CBB_init_fixed(&cbb, buf, ssl->d1->mtu) ||
David Benjaminb5eb1952016-06-17 18:44:38 -0400634 !CBB_add_u8(&cbb, hdr.type) ||
635 !CBB_add_u24(&cbb, hdr.msg_len) ||
636 !CBB_add_u16(&cbb, hdr.seq) ||
637 !CBB_add_u24(&cbb, offset) ||
David Benjamin16285ea2015-11-03 15:39:45 -0500638 !CBB_add_u24(&cbb, todo) ||
David Benjaminb5eb1952016-06-17 18:44:38 -0400639 !CBB_add_bytes(&cbb, CBS_data(&body), todo) ||
640 !CBB_finish(&cbb, NULL, &buf_len)) {
David Benjamin16285ea2015-11-03 15:39:45 -0500641 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
642 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800643 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500644
David Benjamin45d45c12016-06-07 15:20:49 -0400645 int write_ret =
David Benjaminb5eb1952016-06-17 18:44:38 -0400646 dtls1_write_record(ssl, SSL3_RT_HANDSHAKE, buf, buf_len, use_epoch);
David Benjamin16285ea2015-11-03 15:39:45 -0500647 if (write_ret <= 0) {
648 ret = write_ret;
649 goto err;
David Benjamin5a3cc032015-01-11 19:25:32 -0500650 }
David Benjamin16285ea2015-11-03 15:39:45 -0500651
David Benjaminb5eb1952016-06-17 18:44:38 -0400652 if (!CBS_skip(&body, todo)) {
653 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
654 goto err;
655 }
656 offset += todo;
657 } while (CBS_len(&body) != 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800658
David Benjaminb5eb1952016-06-17 18:44:38 -0400659 ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_HANDSHAKE, in,
660 len);
David Benjamin16285ea2015-11-03 15:39:45 -0500661
662 ret = 1;
663
664err:
David Benjaminb5eb1952016-06-17 18:44:38 -0400665 *out_offset = offset;
David Benjamin16285ea2015-11-03 15:39:45 -0500666 CBB_cleanup(&cbb);
667 OPENSSL_free(buf);
668 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800669}
Adam Langley95c29f32014-06-20 12:00:00 -0700670
David Benjamin75836432016-06-17 18:48:29 -0400671void dtls_clear_outgoing_messages(SSL *ssl) {
672 size_t i;
673 for (i = 0; i < ssl->d1->outgoing_messages_len; i++) {
674 OPENSSL_free(ssl->d1->outgoing_messages[i].data);
675 ssl->d1->outgoing_messages[i].data = NULL;
676 }
677 ssl->d1->outgoing_messages_len = 0;
678}
679
David Benjaminaad50db2016-06-23 17:49:12 -0400680/* dtls1_add_change_cipher_spec adds a ChangeCipherSpec to the current
David Benjamin75836432016-06-17 18:48:29 -0400681 * handshake flight. */
David Benjaminaad50db2016-06-23 17:49:12 -0400682static int dtls1_add_change_cipher_spec(SSL *ssl) {
David Benjamin75836432016-06-17 18:48:29 -0400683 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
684 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
685 return 0;
686 }
687
688 DTLS_OUTGOING_MESSAGE *msg =
689 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
690 msg->data = NULL;
691 msg->len = 0;
692 msg->epoch = ssl->d1->w_epoch;
693 msg->is_ccs = 1;
694
695 ssl->d1->outgoing_messages_len++;
696 return 1;
697}
698
David Benjaminaad50db2016-06-23 17:49:12 -0400699static int dtls1_add_message(SSL *ssl, uint8_t *data, size_t len) {
David Benjamin75836432016-06-17 18:48:29 -0400700 if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) {
701 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
702 OPENSSL_free(data);
703 return 0;
704 }
705
706 DTLS_OUTGOING_MESSAGE *msg =
707 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len];
708 msg->data = data;
709 msg->len = len;
710 msg->epoch = ssl->d1->w_epoch;
711 msg->is_ccs = 0;
712
713 ssl->d1->outgoing_messages_len++;
714 return 1;
715}
716
717int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
718 /* Pick a modest size hint to save most of the |realloc| calls. */
719 if (!CBB_init(cbb, 64) ||
720 !CBB_add_u8(cbb, type) ||
721 !CBB_add_u24(cbb, 0 /* length (filled in later) */) ||
722 !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) ||
723 !CBB_add_u24(cbb, 0 /* offset */) ||
724 !CBB_add_u24_length_prefixed(cbb, body)) {
725 return 0;
726 }
727
728 return 1;
729}
730
731int dtls1_finish_message(SSL *ssl, CBB *cbb) {
732 uint8_t *msg = NULL;
733 size_t len;
734 if (!CBB_finish(cbb, &msg, &len) ||
735 len > 0xffffffffu ||
736 len < DTLS1_HM_HEADER_LENGTH) {
737 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
738 OPENSSL_free(msg);
739 return 0;
740 }
741
742 /* Fix up the header. Copy the fragment length into the total message
743 * length. */
744 memcpy(msg + 1, msg + DTLS1_HM_HEADER_LENGTH - 3, 3);
745
746 ssl3_update_handshake_hash(ssl, msg, len);
747
748 ssl->d1->handshake_write_seq++;
749 ssl->init_off = 0;
David Benjaminaad50db2016-06-23 17:49:12 -0400750 return dtls1_add_message(ssl, msg, len);
David Benjamin75836432016-06-17 18:48:29 -0400751}
752
753int dtls1_write_message(SSL *ssl) {
754 if (ssl->d1->outgoing_messages_len == 0) {
755 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
756 return -1;
757 }
758
759 const DTLS_OUTGOING_MESSAGE *msg =
760 &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len - 1];
761 if (msg->is_ccs) {
762 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
763 return -1;
764 }
765
766 size_t offset = ssl->init_off;
767 int ret = dtls1_do_handshake_write(ssl, &offset, msg->data, offset, msg->len,
768 dtls1_use_current_epoch);
769 ssl->init_off = offset;
770 return ret;
771}
772
David Benjamin29a83c52016-06-17 19:12:54 -0400773static int dtls1_retransmit_message(SSL *ssl,
774 const DTLS_OUTGOING_MESSAGE *msg) {
David Benjaminafbc63f2015-02-01 02:33:59 -0500775 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
776 * (negotiated cipher) exist. */
David Benjamin0d56f882015-12-19 17:05:56 -0500777 assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1);
David Benjamin29a83c52016-06-17 19:12:54 -0400778 assert(msg->epoch <= ssl->d1->w_epoch);
David Benjamin3e3090d2015-04-05 12:48:30 -0400779 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamin29a83c52016-06-17 19:12:54 -0400780 if (ssl->d1->w_epoch == 1 && msg->epoch == 0) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400781 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800782 }
783
David Benjamina97b7372015-11-03 14:54:39 -0500784 /* TODO(davidben): This cannot handle non-blocking writes. */
785 int ret;
David Benjamin29a83c52016-06-17 19:12:54 -0400786 if (msg->is_ccs) {
David Benjamin0d56f882015-12-19 17:05:56 -0500787 ret = dtls1_write_change_cipher_spec(ssl, use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500788 } else {
David Benjaminb5eb1952016-06-17 18:44:38 -0400789 size_t offset = 0;
David Benjamin29a83c52016-06-17 19:12:54 -0400790 ret = dtls1_do_handshake_write(ssl, &offset, msg->data, offset, msg->len,
791 use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500792 }
793
Adam Langley71d8a082014-12-13 16:28:18 -0800794 return ret;
795}
Adam Langley95c29f32014-06-20 12:00:00 -0700796
David Benjaminaad50db2016-06-23 17:49:12 -0400797int dtls1_retransmit_outgoing_messages(SSL *ssl) {
David Benjamin30152fd2016-05-05 20:45:48 -0400798 /* Ensure we are packing handshake messages. */
799 const int was_buffered = ssl_is_wbio_buffered(ssl);
800 assert(was_buffered == SSL_in_init(ssl));
David Benjaminb095f0f2016-05-05 21:50:24 -0400801 if (!was_buffered && !ssl_init_wbio_buffer(ssl)) {
David Benjamin30152fd2016-05-05 20:45:48 -0400802 return -1;
803 }
804 assert(ssl_is_wbio_buffered(ssl));
Adam Langleybcc4e232015-02-19 15:51:58 -0800805
David Benjamin30152fd2016-05-05 20:45:48 -0400806 int ret = -1;
David Benjamin29a83c52016-06-17 19:12:54 -0400807 size_t i;
808 for (i = 0; i < ssl->d1->outgoing_messages_len; i++) {
809 if (dtls1_retransmit_message(ssl, &ssl->d1->outgoing_messages[i]) <= 0) {
David Benjamin30152fd2016-05-05 20:45:48 -0400810 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800811 }
812 }
813
David Benjamin2f871122016-05-20 14:27:17 -0400814 ret = BIO_flush(ssl->wbio);
David Benjamin27309552016-05-05 21:17:53 -0400815 if (ret <= 0) {
816 ssl->rwstate = SSL_WRITING;
817 goto err;
818 }
David Benjamin30152fd2016-05-05 20:45:48 -0400819
820err:
821 if (!was_buffered) {
822 ssl_free_wbio_buffer(ssl);
823 }
824 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800825}
826
David Benjamin352d0a92016-06-28 11:22:02 -0400827int dtls1_send_change_cipher_spec(SSL *ssl) {
828 int ret = dtls1_write_change_cipher_spec(ssl, dtls1_use_current_epoch);
829 if (ret <= 0) {
830 return ret;
David Benjamina97b7372015-11-03 14:54:39 -0500831 }
David Benjamin352d0a92016-06-28 11:22:02 -0400832 dtls1_add_change_cipher_spec(ssl);
833 return 1;
David Benjamina97b7372015-11-03 14:54:39 -0500834}
835
Adam Langley71d8a082014-12-13 16:28:18 -0800836unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500837 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800838}