blob: 8d8784b4498c4da7ea3886de8ced80e2d839ef9f [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 Benjamin75381222015-03-02 19:30:30 -0500142/* kMaxHandshakeBuffer is the maximum number of handshake messages ahead of the
143 * current one to buffer. */
144static const unsigned int kHandshakeBufferSize = 10;
145
David Benjaminc99807d2015-09-12 18:21:35 -0400146static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly) {
147 hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800148 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400149 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800150 return NULL;
151 }
David Benjaminc99807d2015-09-12 18:21:35 -0400152 memset(frag, 0, sizeof(hm_fragment));
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjaminc99807d2015-09-12 18:21:35 -0400154 /* If the handshake message is empty, |frag->fragment| and |frag->reassembly|
155 * are NULL. */
156 if (frag_len > 0) {
157 frag->fragment = OPENSSL_malloc(frag_len);
158 if (frag->fragment == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400159 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminc99807d2015-09-12 18:21:35 -0400160 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800161 }
Adam Langley95c29f32014-06-20 12:00:00 -0700162
David Benjaminc99807d2015-09-12 18:21:35 -0400163 if (reassembly) {
164 /* Initialize reassembly bitmask. */
165 if (frag_len + 7 < frag_len) {
166 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
167 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800168 }
David Benjaminc99807d2015-09-12 18:21:35 -0400169 size_t bitmask_len = (frag_len + 7) / 8;
170 frag->reassembly = OPENSSL_malloc(bitmask_len);
171 if (frag->reassembly == NULL) {
172 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
173 goto err;
174 }
175 memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800176 }
Adam Langley71d8a082014-12-13 16:28:18 -0800177 }
Adam Langley95c29f32014-06-20 12:00:00 -0700178
Adam Langley71d8a082014-12-13 16:28:18 -0800179 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400180
181err:
182 dtls1_hm_fragment_free(frag);
183 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800184}
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langley71d8a082014-12-13 16:28:18 -0800186void dtls1_hm_fragment_free(hm_fragment *frag) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400187 if (frag == NULL) {
188 return;
189 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400190 OPENSSL_free(frag->fragment);
191 OPENSSL_free(frag->reassembly);
Adam Langley71d8a082014-12-13 16:28:18 -0800192 OPENSSL_free(frag);
193}
Adam Langley95c29f32014-06-20 12:00:00 -0700194
David Benjaminee562b92015-03-02 20:52:52 -0500195#if !defined(inline)
196#define inline __inline
197#endif
198
199/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
200 * exclusive, set. */
201static inline uint8_t bit_range(size_t start, size_t end) {
202 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
203}
204
205/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
206 * as received in |frag|. If |frag| becomes complete, it clears
207 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
208 * and |frag->reassembly| must not be NULL. */
209static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
210 size_t end) {
211 size_t i;
212 size_t msg_len = frag->msg_header.msg_len;
213
214 if (frag->reassembly == NULL || start > end || end > msg_len) {
215 assert(0);
216 return;
217 }
218 /* A zero-length message will never have a pending reassembly. */
219 assert(msg_len > 0);
220
221 if ((start >> 3) == (end >> 3)) {
222 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
223 } else {
224 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
225 for (i = (start >> 3) + 1; i < (end >> 3); i++) {
226 frag->reassembly[i] = 0xff;
227 }
228 if ((end & 7) != 0) {
229 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
230 }
231 }
232
233 /* Check if the fragment is complete. */
234 for (i = 0; i < (msg_len >> 3); i++) {
235 if (frag->reassembly[i] != 0xff) {
236 return;
237 }
238 }
239 if ((msg_len & 7) != 0 &&
240 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
241 return;
242 }
243
244 OPENSSL_free(frag->reassembly);
245 frag->reassembly = NULL;
246}
247
David Benjamina97b7372015-11-03 14:54:39 -0500248static void dtls1_update_mtu(SSL *ssl) {
249 /* TODO(davidben): What is this code doing and do we need it? */
250 if (ssl->d1->mtu < dtls1_min_mtu() &&
251 !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin2f871122016-05-20 14:27:17 -0400252 long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
David Benjamina97b7372015-11-03 14:54:39 -0500253 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
254 ssl->d1->mtu = (unsigned)mtu;
255 } else {
256 ssl->d1->mtu = kDefaultMTU;
David Benjamin2f871122016-05-20 14:27:17 -0400257 BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL);
David Benjamina97b7372015-11-03 14:54:39 -0500258 }
259 }
260
261 /* The MTU should be above the minimum now. */
262 assert(ssl->d1->mtu >= dtls1_min_mtu());
263}
264
David Benjamin16285ea2015-11-03 15:39:45 -0500265/* dtls1_max_record_size returns the maximum record body length that may be
266 * written without exceeding the MTU. It accounts for any buffering installed on
267 * the write BIO. If no record may be written, it returns zero. */
268static size_t dtls1_max_record_size(SSL *ssl) {
269 size_t ret = ssl->d1->mtu;
270
271 size_t overhead = ssl_max_seal_overhead(ssl);
272 if (ret <= overhead) {
273 return 0;
274 }
275 ret -= overhead;
276
David Benjamin2f871122016-05-20 14:27:17 -0400277 size_t pending = BIO_wpending(ssl->wbio);
David Benjamin16285ea2015-11-03 15:39:45 -0500278 if (ret <= pending) {
279 return 0;
280 }
281 ret -= pending;
282
283 return ret;
284}
285
David Benjamina97b7372015-11-03 14:54:39 -0500286static int dtls1_write_change_cipher_spec(SSL *ssl,
287 enum dtls1_use_epoch_t use_epoch) {
288 dtls1_update_mtu(ssl);
289
290 /* During the handshake, wbio is buffered to pack messages together. Flush the
291 * buffer if the ChangeCipherSpec would not fit in a packet. */
David Benjamin16285ea2015-11-03 15:39:45 -0500292 if (dtls1_max_record_size(ssl) == 0) {
David Benjamin2f871122016-05-20 14:27:17 -0400293 int ret = BIO_flush(ssl->wbio);
David Benjamina97b7372015-11-03 14:54:39 -0500294 if (ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500295 ssl->rwstate = SSL_WRITING;
David Benjamina97b7372015-11-03 14:54:39 -0500296 return ret;
297 }
David Benjamina97b7372015-11-03 14:54:39 -0500298 }
299
300 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
David Benjamin0d56f882015-12-19 17:05:56 -0500301 int ret =
302 dtls1_write_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
303 sizeof(kChangeCipherSpec), use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500304 if (ret <= 0) {
305 return ret;
306 }
307
308 if (ssl->msg_callback != NULL) {
309 ssl->msg_callback(1 /* write */, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC,
310 kChangeCipherSpec, sizeof(kChangeCipherSpec), ssl,
311 ssl->msg_callback_arg);
312 }
313
314 return 1;
315}
316
David Benjamin16285ea2015-11-03 15:39:45 -0500317int dtls1_do_handshake_write(SSL *ssl, enum dtls1_use_epoch_t use_epoch) {
318 dtls1_update_mtu(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700319
David Benjamin16285ea2015-11-03 15:39:45 -0500320 int ret = -1;
321 CBB cbb;
322 CBB_zero(&cbb);
323 /* Allocate a temporary buffer to hold the message fragments to avoid
324 * clobbering the message. */
325 uint8_t *buf = OPENSSL_malloc(ssl->d1->mtu);
326 if (buf == NULL) {
327 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjamin16285ea2015-11-03 15:39:45 -0500330 /* Consume the message header. Fragments will have different headers
331 * prepended. */
332 if (ssl->init_off == 0) {
333 ssl->init_off += DTLS1_HM_HEADER_LENGTH;
334 ssl->init_num -= DTLS1_HM_HEADER_LENGTH;
335 }
336 assert(ssl->init_off >= DTLS1_HM_HEADER_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin16285ea2015-11-03 15:39:45 -0500338 do {
339 /* During the handshake, wbio is buffered to pack messages together. Flush
340 * the buffer if there isn't enough room to make progress. */
341 if (dtls1_max_record_size(ssl) < DTLS1_HM_HEADER_LENGTH + 1) {
David Benjamin2f871122016-05-20 14:27:17 -0400342 int flush_ret = BIO_flush(ssl->wbio);
David Benjamin16285ea2015-11-03 15:39:45 -0500343 if (flush_ret <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500344 ssl->rwstate = SSL_WRITING;
David Benjamin16285ea2015-11-03 15:39:45 -0500345 ret = flush_ret;
346 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800347 }
David Benjamin2f871122016-05-20 14:27:17 -0400348 assert(BIO_wpending(ssl->wbio) == 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800349 }
Adam Langley95c29f32014-06-20 12:00:00 -0700350
David Benjamin16285ea2015-11-03 15:39:45 -0500351 size_t todo = dtls1_max_record_size(ssl);
352 if (todo < DTLS1_HM_HEADER_LENGTH + 1) {
David Benjamina97b7372015-11-03 14:54:39 -0500353 /* To make forward progress, the MTU must, at minimum, fit the handshake
354 * header and one byte of handshake body. */
355 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamin16285ea2015-11-03 15:39:45 -0500356 goto err;
357 }
358 todo -= DTLS1_HM_HEADER_LENGTH;
359
360 if (todo > (size_t)ssl->init_num) {
361 todo = ssl->init_num;
362 }
363 if (todo >= (1u << 24)) {
364 todo = (1u << 24) - 1;
David Benjamina97b7372015-11-03 14:54:39 -0500365 }
366
David Benjamin16285ea2015-11-03 15:39:45 -0500367 size_t len;
368 if (!CBB_init_fixed(&cbb, buf, ssl->d1->mtu) ||
369 !CBB_add_u8(&cbb, ssl->d1->w_msg_hdr.type) ||
370 !CBB_add_u24(&cbb, ssl->d1->w_msg_hdr.msg_len) ||
371 !CBB_add_u16(&cbb, ssl->d1->w_msg_hdr.seq) ||
372 !CBB_add_u24(&cbb, ssl->init_off - DTLS1_HM_HEADER_LENGTH) ||
373 !CBB_add_u24(&cbb, todo) ||
374 !CBB_add_bytes(
375 &cbb, (const uint8_t *)ssl->init_buf->data + ssl->init_off, todo) ||
376 !CBB_finish(&cbb, NULL, &len)) {
377 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
378 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800379 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500380
David Benjamin16285ea2015-11-03 15:39:45 -0500381 int write_ret = dtls1_write_bytes(ssl, SSL3_RT_HANDSHAKE, buf, len,
382 use_epoch);
383 if (write_ret <= 0) {
384 ret = write_ret;
385 goto err;
David Benjamin5a3cc032015-01-11 19:25:32 -0500386 }
David Benjamin16285ea2015-11-03 15:39:45 -0500387 ssl->init_off += todo;
388 ssl->init_num -= todo;
389 } while (ssl->init_num > 0);
390
391 if (ssl->msg_callback != NULL) {
392 ssl->msg_callback(
393 1 /* write */, ssl->version, SSL3_RT_HANDSHAKE, ssl->init_buf->data,
394 (size_t)(ssl->init_off + ssl->init_num), ssl, ssl->msg_callback_arg);
Adam Langley71d8a082014-12-13 16:28:18 -0800395 }
396
David Benjamin16285ea2015-11-03 15:39:45 -0500397 ssl->init_off = 0;
398 ssl->init_num = 0;
399
400 ret = 1;
401
402err:
403 CBB_cleanup(&cbb);
404 OPENSSL_free(buf);
405 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800406}
Adam Langley95c29f32014-06-20 12:00:00 -0700407
David Benjamin75381222015-03-02 19:30:30 -0500408/* dtls1_is_next_message_complete returns one if the next handshake message is
409 * complete and zero otherwise. */
David Benjamin0d56f882015-12-19 17:05:56 -0500410static int dtls1_is_next_message_complete(SSL *ssl) {
411 pitem *item = pqueue_peek(ssl->d1->buffered_messages);
David Benjamin75381222015-03-02 19:30:30 -0500412 if (item == NULL) {
413 return 0;
414 }
415
416 hm_fragment *frag = (hm_fragment *)item->data;
David Benjamin0d56f882015-12-19 17:05:56 -0500417 assert(ssl->d1->handshake_read_seq <= frag->msg_header.seq);
David Benjamin75381222015-03-02 19:30:30 -0500418
David Benjamin0d56f882015-12-19 17:05:56 -0500419 return ssl->d1->handshake_read_seq == frag->msg_header.seq &&
420 frag->reassembly == NULL;
David Benjamin75381222015-03-02 19:30:30 -0500421}
422
423/* dtls1_discard_fragment_body discards a handshake fragment body of length
424 * |frag_len|. It returns one on success and zero on error.
425 *
426 * TODO(davidben): This function will go away when ssl_read_bytes is gone from
427 * the DTLS side. */
David Benjamin0d56f882015-12-19 17:05:56 -0500428static int dtls1_discard_fragment_body(SSL *ssl, size_t frag_len) {
David Benjamin75381222015-03-02 19:30:30 -0500429 uint8_t discard[256];
430 while (frag_len > 0) {
431 size_t chunk = frag_len < sizeof(discard) ? frag_len : sizeof(discard);
David Benjamin0d56f882015-12-19 17:05:56 -0500432 int ret = dtls1_read_bytes(ssl, SSL3_RT_HANDSHAKE, discard, chunk, 0);
Adam Langley96c2a282015-06-02 14:16:44 -0700433 if (ret != (int) chunk) {
David Benjamin75381222015-03-02 19:30:30 -0500434 return 0;
435 }
436 frag_len -= chunk;
437 }
438 return 1;
439}
440
441/* dtls1_get_buffered_message returns the buffered message corresponding to
442 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
443 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
444 * returns NULL on failure. The caller does not take ownership of the result. */
445static hm_fragment *dtls1_get_buffered_message(
David Benjamin0d56f882015-12-19 17:05:56 -0500446 SSL *ssl, const struct hm_header_st *msg_hdr) {
David Benjamin75381222015-03-02 19:30:30 -0500447 uint8_t seq64be[8];
448 memset(seq64be, 0, sizeof(seq64be));
449 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
450 seq64be[7] = (uint8_t)msg_hdr->seq;
David Benjamin0d56f882015-12-19 17:05:56 -0500451 pitem *item = pqueue_find(ssl->d1->buffered_messages, seq64be);
David Benjamin75381222015-03-02 19:30:30 -0500452
453 hm_fragment *frag;
454 if (item == NULL) {
455 /* This is the first fragment from this message. */
456 frag = dtls1_hm_fragment_new(msg_hdr->msg_len,
457 1 /* reassembly buffer needed */);
458 if (frag == NULL) {
459 return NULL;
460 }
461 memcpy(&frag->msg_header, msg_hdr, sizeof(*msg_hdr));
462 item = pitem_new(seq64be, frag);
463 if (item == NULL) {
464 dtls1_hm_fragment_free(frag);
465 return NULL;
466 }
David Benjamin0d56f882015-12-19 17:05:56 -0500467 item = pqueue_insert(ssl->d1->buffered_messages, item);
David Benjamin75381222015-03-02 19:30:30 -0500468 /* |pqueue_insert| fails iff a duplicate item is inserted, but |item| cannot
469 * be a duplicate. */
470 assert(item != NULL);
471 } else {
472 frag = item->data;
473 assert(frag->msg_header.seq == msg_hdr->seq);
474 if (frag->msg_header.type != msg_hdr->type ||
475 frag->msg_header.msg_len != msg_hdr->msg_len) {
476 /* The new fragment must be compatible with the previous fragments from
477 * this message. */
David Benjamin3570d732015-06-29 00:28:17 -0400478 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
David Benjamin0d56f882015-12-19 17:05:56 -0500479 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin75381222015-03-02 19:30:30 -0500480 return NULL;
481 }
482 }
483 return frag;
484}
485
David Benjamin75381222015-03-02 19:30:30 -0500486/* dtls1_process_fragment reads a handshake fragment and processes it. It
487 * returns one if a fragment was successfully processed and 0 or -1 on error. */
David Benjamin0d56f882015-12-19 17:05:56 -0500488static int dtls1_process_fragment(SSL *ssl) {
David Benjamin11fc66a2015-06-16 11:40:24 -0400489 /* Read handshake message header. */
David Benjamin75381222015-03-02 19:30:30 -0500490 uint8_t header[DTLS1_HM_HEADER_LENGTH];
David Benjamin0d56f882015-12-19 17:05:56 -0500491 int ret = dtls1_read_bytes(ssl, SSL3_RT_HANDSHAKE, header,
David Benjamina6022772015-05-30 16:22:10 -0400492 DTLS1_HM_HEADER_LENGTH, 0);
David Benjamin75381222015-03-02 19:30:30 -0500493 if (ret <= 0) {
David Benjamin75381222015-03-02 19:30:30 -0500494 return ret;
495 }
496 if (ret != DTLS1_HM_HEADER_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400497 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -0500498 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin75381222015-03-02 19:30:30 -0500499 return -1;
500 }
501
502 /* Parse the message fragment header. */
503 struct hm_header_st msg_hdr;
504 dtls1_get_message_header(header, &msg_hdr);
505
David Benjamin11fc66a2015-06-16 11:40:24 -0400506 /* TODO(davidben): dtls1_read_bytes is the wrong abstraction for DTLS. There
David Benjamin0d56f882015-12-19 17:05:56 -0500507 * should be no need to reach into |ssl->s3->rrec.length|. */
David Benjamin75381222015-03-02 19:30:30 -0500508 const size_t frag_off = msg_hdr.frag_off;
509 const size_t frag_len = msg_hdr.frag_len;
510 const size_t msg_len = msg_hdr.msg_len;
511 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
512 frag_off + frag_len > msg_len ||
David Benjamin060cfb02016-05-12 00:43:05 -0400513 msg_len > ssl_max_handshake_message_len(ssl) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500514 frag_len > ssl->s3->rrec.length) {
David Benjamin3570d732015-06-29 00:28:17 -0400515 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
David Benjamin0d56f882015-12-19 17:05:56 -0500516 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin75381222015-03-02 19:30:30 -0500517 return -1;
518 }
519
David Benjamin0d56f882015-12-19 17:05:56 -0500520 if (msg_hdr.seq < ssl->d1->handshake_read_seq ||
521 msg_hdr.seq > (unsigned)ssl->d1->handshake_read_seq +
David Benjamin75381222015-03-02 19:30:30 -0500522 kHandshakeBufferSize) {
523 /* Ignore fragments from the past, or ones too far in the future. */
David Benjamin0d56f882015-12-19 17:05:56 -0500524 if (!dtls1_discard_fragment_body(ssl, frag_len)) {
David Benjamin75381222015-03-02 19:30:30 -0500525 return -1;
526 }
527 return 1;
528 }
529
David Benjamin0d56f882015-12-19 17:05:56 -0500530 hm_fragment *frag = dtls1_get_buffered_message(ssl, &msg_hdr);
David Benjamin75381222015-03-02 19:30:30 -0500531 if (frag == NULL) {
532 return -1;
533 }
534 assert(frag->msg_header.msg_len == msg_len);
535
536 if (frag->reassembly == NULL) {
537 /* The message is already assembled. */
David Benjamin0d56f882015-12-19 17:05:56 -0500538 if (!dtls1_discard_fragment_body(ssl, frag_len)) {
David Benjamin75381222015-03-02 19:30:30 -0500539 return -1;
540 }
541 return 1;
542 }
543 assert(msg_len > 0);
544
545 /* Read the body of the fragment. */
David Benjamin0d56f882015-12-19 17:05:56 -0500546 ret = dtls1_read_bytes(ssl, SSL3_RT_HANDSHAKE, frag->fragment + frag_off,
David Benjamina6022772015-05-30 16:22:10 -0400547 frag_len, 0);
Adam Langley96c2a282015-06-02 14:16:44 -0700548 if (ret != (int) frag_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400549 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin0d56f882015-12-19 17:05:56 -0500550 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin75381222015-03-02 19:30:30 -0500551 return -1;
552 }
David Benjaminee562b92015-03-02 20:52:52 -0500553 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
David Benjamin75381222015-03-02 19:30:30 -0500554
David Benjamin75381222015-03-02 19:30:30 -0500555 return 1;
556}
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjaminbcb2d912015-02-24 23:45:43 -0500558/* dtls1_get_message reads a handshake message of message type |msg_type| (any
David Benjamin060cfb02016-05-12 00:43:05 -0400559 * if |msg_type| == -1). Read an entire handshake message. Handshake messages
560 * arrive in fragments. */
David Benjamin1e6d6df2016-05-13 18:28:17 -0400561long dtls1_get_message(SSL *ssl, int msg_type,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500562 enum ssl_hash_message_t hash_message, int *ok) {
David Benjamin75381222015-03-02 19:30:30 -0500563 pitem *item = NULL;
564 hm_fragment *frag = NULL;
565 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700566
Adam Langley71d8a082014-12-13 16:28:18 -0800567 /* s3->tmp is used to store messages that are unexpected, caused
568 * by the absence of an optional handshake message */
David Benjamin0d56f882015-12-19 17:05:56 -0500569 if (ssl->s3->tmp.reuse_message) {
David Benjamin5ca39fb2015-03-01 23:57:54 -0500570 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
571 * ssl_dont_hash_message would have to have been applied to the previous
572 * call. */
573 assert(hash_message == ssl_hash_message);
David Benjamin0d56f882015-12-19 17:05:56 -0500574 ssl->s3->tmp.reuse_message = 0;
575 if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800576 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400577 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley71d8a082014-12-13 16:28:18 -0800578 goto f_err;
579 }
580 *ok = 1;
David Benjamina6338be2016-05-13 18:12:19 -0400581 assert(ssl->init_buf->length >= DTLS1_HM_HEADER_LENGTH);
David Benjamin0d56f882015-12-19 17:05:56 -0500582 ssl->init_msg = (uint8_t *)ssl->init_buf->data + DTLS1_HM_HEADER_LENGTH;
David Benjamina6338be2016-05-13 18:12:19 -0400583 ssl->init_num = (int)ssl->init_buf->length - DTLS1_HM_HEADER_LENGTH;
David Benjamin0d56f882015-12-19 17:05:56 -0500584 return ssl->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
David Benjamin75381222015-03-02 19:30:30 -0500587 /* Process fragments until one is found. */
David Benjamin0d56f882015-12-19 17:05:56 -0500588 while (!dtls1_is_next_message_complete(ssl)) {
589 int ret = dtls1_process_fragment(ssl);
David Benjamin75381222015-03-02 19:30:30 -0500590 if (ret <= 0) {
591 *ok = 0;
592 return ret;
593 }
Adam Langley71d8a082014-12-13 16:28:18 -0800594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
David Benjamin75381222015-03-02 19:30:30 -0500596 /* Read out the next complete handshake message. */
David Benjamin0d56f882015-12-19 17:05:56 -0500597 item = pqueue_pop(ssl->d1->buffered_messages);
David Benjamin75381222015-03-02 19:30:30 -0500598 assert(item != NULL);
599 frag = (hm_fragment *)item->data;
David Benjamin0d56f882015-12-19 17:05:56 -0500600 assert(ssl->d1->handshake_read_seq == frag->msg_header.seq);
David Benjamin75381222015-03-02 19:30:30 -0500601 assert(frag->reassembly == NULL);
602
David Benjamina8653202015-06-28 01:26:10 -0400603 /* Reconstruct the assembled message. */
David Benjamin75381222015-03-02 19:30:30 -0500604 CBB cbb;
David Benjamina8653202015-06-28 01:26:10 -0400605 CBB_zero(&cbb);
David Benjamina6338be2016-05-13 18:12:19 -0400606 if (!BUF_MEM_reserve(ssl->init_buf, (size_t)frag->msg_header.msg_len +
607 DTLS1_HM_HEADER_LENGTH) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500608 !CBB_init_fixed(&cbb, (uint8_t *)ssl->init_buf->data,
609 ssl->init_buf->max) ||
David Benjamina8653202015-06-28 01:26:10 -0400610 !CBB_add_u8(&cbb, frag->msg_header.type) ||
David Benjamin75381222015-03-02 19:30:30 -0500611 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
612 !CBB_add_u16(&cbb, frag->msg_header.seq) ||
613 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
614 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
615 !CBB_add_bytes(&cbb, frag->fragment, frag->msg_header.msg_len) ||
David Benjamina6338be2016-05-13 18:12:19 -0400616 !CBB_finish(&cbb, NULL, &ssl->init_buf->length)) {
David Benjamin75381222015-03-02 19:30:30 -0500617 CBB_cleanup(&cbb);
David Benjamin3570d732015-06-29 00:28:17 -0400618 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin75381222015-03-02 19:30:30 -0500619 goto err;
620 }
David Benjamina6338be2016-05-13 18:12:19 -0400621 assert(ssl->init_buf->length ==
622 (size_t)frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH);
David Benjamin75381222015-03-02 19:30:30 -0500623
David Benjamin0d56f882015-12-19 17:05:56 -0500624 ssl->d1->handshake_read_seq++;
David Benjamin75381222015-03-02 19:30:30 -0500625
626 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
627 * |ssl_get_message| API. */
David Benjamin0d56f882015-12-19 17:05:56 -0500628 ssl->s3->tmp.message_type = frag->msg_header.type;
David Benjamin0d56f882015-12-19 17:05:56 -0500629 ssl->init_msg = (uint8_t *)ssl->init_buf->data + DTLS1_HM_HEADER_LENGTH;
630 ssl->init_num = frag->msg_header.msg_len;
David Benjamin75381222015-03-02 19:30:30 -0500631
David Benjamin0d56f882015-12-19 17:05:56 -0500632 if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
David Benjaminbcb2d912015-02-24 23:45:43 -0500633 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400634 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminbcb2d912015-02-24 23:45:43 -0500635 goto f_err;
636 }
David Benjamin0d56f882015-12-19 17:05:56 -0500637 if (hash_message == ssl_hash_message && !ssl3_hash_current_message(ssl)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500638 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800639 }
David Benjamin0d56f882015-12-19 17:05:56 -0500640 if (ssl->msg_callback) {
641 ssl->msg_callback(0, ssl->version, SSL3_RT_HANDSHAKE, ssl->init_buf->data,
642 ssl->init_num + DTLS1_HM_HEADER_LENGTH, ssl,
643 ssl->msg_callback_arg);
Adam Langley71d8a082014-12-13 16:28:18 -0800644 }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
David Benjamin75381222015-03-02 19:30:30 -0500646 pitem_free(item);
647 dtls1_hm_fragment_free(frag);
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjamin75381222015-03-02 19:30:30 -0500649 *ok = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500650 return ssl->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700651
652f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500653 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500654err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400655 pitem_free(item);
656 dtls1_hm_fragment_free(frag);
Adam Langley71d8a082014-12-13 16:28:18 -0800657 *ok = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800658 return -1;
659}
Adam Langley95c29f32014-06-20 12:00:00 -0700660
David Benjamin0d56f882015-12-19 17:05:56 -0500661int dtls1_read_failed(SSL *ssl, int code) {
Adam Langley71d8a082014-12-13 16:28:18 -0800662 if (code > 0) {
Adam Langleyd3459fb2015-02-13 14:56:28 -0800663 assert(0);
Adam Langley71d8a082014-12-13 16:28:18 -0800664 return 1;
665 }
Adam Langley95c29f32014-06-20 12:00:00 -0700666
David Benjamin0d56f882015-12-19 17:05:56 -0500667 if (!dtls1_is_timer_expired(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800668 /* not a timeout, none of our business, let higher layers handle this. In
669 * fact, it's probably an error */
670 return code;
671 }
Adam Langley95c29f32014-06-20 12:00:00 -0700672
David Benjamin0d56f882015-12-19 17:05:56 -0500673 if (!SSL_in_init(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800674 /* done, no need to send a retransmit */
David Benjamin2f871122016-05-20 14:27:17 -0400675 BIO_set_flags(ssl->rbio, BIO_FLAGS_READ);
Adam Langley71d8a082014-12-13 16:28:18 -0800676 return code;
677 }
Adam Langley95c29f32014-06-20 12:00:00 -0700678
David Benjamin0d56f882015-12-19 17:05:56 -0500679 return DTLSv1_handle_timeout(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800680}
Adam Langley95c29f32014-06-20 12:00:00 -0700681
David Benjamina97b7372015-11-03 14:54:39 -0500682static uint16_t dtls1_get_queue_priority(uint16_t seq, int is_ccs) {
683 assert(seq * 2 >= seq);
684
Adam Langley71d8a082014-12-13 16:28:18 -0800685 /* The index of the retransmission queue actually is the message sequence
686 * number, since the queue only contains messages of a single handshake.
687 * However, the ChangeCipherSpec has no message sequence number and so using
688 * only the sequence will result in the CCS and Finished having the same
689 * index. To prevent this, the sequence number is multiplied by 2. In case of
690 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
691 * maintains the order of the index (important for priority queues) and fits
692 * in the unsigned short variable. */
693 return seq * 2 - is_ccs;
694}
Adam Langley95c29f32014-06-20 12:00:00 -0700695
David Benjamin0d56f882015-12-19 17:05:56 -0500696static int dtls1_retransmit_message(SSL *ssl, hm_fragment *frag) {
David Benjaminafbc63f2015-02-01 02:33:59 -0500697 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
698 * (negotiated cipher) exist. */
David Benjamin0d56f882015-12-19 17:05:56 -0500699 assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1);
700 assert(frag->msg_header.epoch <= ssl->d1->w_epoch);
David Benjamin3e3090d2015-04-05 12:48:30 -0400701 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
David Benjamin0d56f882015-12-19 17:05:56 -0500702 if (ssl->d1->w_epoch == 1 && frag->msg_header.epoch == 0) {
David Benjamin3e3090d2015-04-05 12:48:30 -0400703 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800704 }
705
David Benjamina97b7372015-11-03 14:54:39 -0500706 /* TODO(davidben): This cannot handle non-blocking writes. */
707 int ret;
708 if (frag->msg_header.is_ccs) {
David Benjamin0d56f882015-12-19 17:05:56 -0500709 ret = dtls1_write_change_cipher_spec(ssl, use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500710 } else {
711 /* Restore the message body.
712 * TODO(davidben): Make this less stateful. */
David Benjamin0d56f882015-12-19 17:05:56 -0500713 memcpy(ssl->init_buf->data, frag->fragment,
David Benjamina97b7372015-11-03 14:54:39 -0500714 frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH);
David Benjamin0d56f882015-12-19 17:05:56 -0500715 ssl->init_num = frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH;
Adam Langley71d8a082014-12-13 16:28:18 -0800716
David Benjamin0d56f882015-12-19 17:05:56 -0500717 dtls1_set_message_header(ssl, frag->msg_header.type,
David Benjamina97b7372015-11-03 14:54:39 -0500718 frag->msg_header.msg_len, frag->msg_header.seq,
719 0, frag->msg_header.frag_len);
David Benjamin0d56f882015-12-19 17:05:56 -0500720 ret = dtls1_do_handshake_write(ssl, use_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500721 }
722
Adam Langley71d8a082014-12-13 16:28:18 -0800723 return ret;
724}
Adam Langley95c29f32014-06-20 12:00:00 -0700725
David Benjamin0d56f882015-12-19 17:05:56 -0500726int dtls1_retransmit_buffered_messages(SSL *ssl) {
David Benjamin30152fd2016-05-05 20:45:48 -0400727 /* Ensure we are packing handshake messages. */
728 const int was_buffered = ssl_is_wbio_buffered(ssl);
729 assert(was_buffered == SSL_in_init(ssl));
David Benjaminb095f0f2016-05-05 21:50:24 -0400730 if (!was_buffered && !ssl_init_wbio_buffer(ssl)) {
David Benjamin30152fd2016-05-05 20:45:48 -0400731 return -1;
732 }
733 assert(ssl_is_wbio_buffered(ssl));
Adam Langleybcc4e232015-02-19 15:51:58 -0800734
David Benjamin30152fd2016-05-05 20:45:48 -0400735 int ret = -1;
736 piterator iter = pqueue_iterator(ssl->d1->sent_messages);
737 pitem *item;
Adam Langleybcc4e232015-02-19 15:51:58 -0800738 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
739 hm_fragment *frag = (hm_fragment *)item->data;
David Benjamin0d56f882015-12-19 17:05:56 -0500740 if (dtls1_retransmit_message(ssl, frag) <= 0) {
David Benjamin30152fd2016-05-05 20:45:48 -0400741 goto err;
Adam Langleybcc4e232015-02-19 15:51:58 -0800742 }
743 }
744
David Benjamin2f871122016-05-20 14:27:17 -0400745 ret = BIO_flush(ssl->wbio);
David Benjamin27309552016-05-05 21:17:53 -0400746 if (ret <= 0) {
747 ssl->rwstate = SSL_WRITING;
748 goto err;
749 }
David Benjamin30152fd2016-05-05 20:45:48 -0400750
751err:
752 if (!was_buffered) {
753 ssl_free_wbio_buffer(ssl);
754 }
755 return ret;
Adam Langleybcc4e232015-02-19 15:51:58 -0800756}
757
David Benjamina97b7372015-11-03 14:54:39 -0500758/* dtls1_buffer_change_cipher_spec adds a ChangeCipherSpec to the current
759 * handshake flight, ordered just before the handshake message numbered
760 * |seq|. */
761static int dtls1_buffer_change_cipher_spec(SSL *ssl, uint16_t seq) {
762 hm_fragment *frag = dtls1_hm_fragment_new(0 /* frag_len */,
763 0 /* no reassembly */);
764 if (frag == NULL) {
765 return 0;
766 }
767 frag->msg_header.is_ccs = 1;
768 frag->msg_header.epoch = ssl->d1->w_epoch;
Adam Langleybcc4e232015-02-19 15:51:58 -0800769
David Benjamina97b7372015-11-03 14:54:39 -0500770 uint16_t priority = dtls1_get_queue_priority(seq, 1 /* is_ccs */);
771 uint8_t seq64be[8];
772 memset(seq64be, 0, sizeof(seq64be));
773 seq64be[6] = (uint8_t)(priority >> 8);
774 seq64be[7] = (uint8_t)priority;
775
776 pitem *item = pitem_new(seq64be, frag);
777 if (item == NULL) {
778 dtls1_hm_fragment_free(frag);
779 return 0;
780 }
781
782 pqueue_insert(ssl->d1->sent_messages, item);
783 return 1;
784}
785
David Benjamin0d56f882015-12-19 17:05:56 -0500786int dtls1_buffer_message(SSL *ssl) {
Adam Langleybcc4e232015-02-19 15:51:58 -0800787 /* this function is called immediately after a message has
788 * been serialized */
David Benjamin0d56f882015-12-19 17:05:56 -0500789 assert(ssl->init_off == 0);
Adam Langleybcc4e232015-02-19 15:51:58 -0800790
David Benjamin0d56f882015-12-19 17:05:56 -0500791 hm_fragment *frag = dtls1_hm_fragment_new(ssl->init_num, 0);
Adam Langleybcc4e232015-02-19 15:51:58 -0800792 if (!frag) {
793 return 0;
794 }
795
David Benjamin0d56f882015-12-19 17:05:56 -0500796 memcpy(frag->fragment, ssl->init_buf->data, ssl->init_num);
Adam Langleybcc4e232015-02-19 15:51:58 -0800797
David Benjamin0d56f882015-12-19 17:05:56 -0500798 assert(ssl->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
799 (unsigned int)ssl->init_num);
Adam Langleybcc4e232015-02-19 15:51:58 -0800800
David Benjamin0d56f882015-12-19 17:05:56 -0500801 frag->msg_header.msg_len = ssl->d1->w_msg_hdr.msg_len;
802 frag->msg_header.seq = ssl->d1->w_msg_hdr.seq;
803 frag->msg_header.type = ssl->d1->w_msg_hdr.type;
Adam Langleybcc4e232015-02-19 15:51:58 -0800804 frag->msg_header.frag_off = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500805 frag->msg_header.frag_len = ssl->d1->w_msg_hdr.msg_len;
David Benjamina97b7372015-11-03 14:54:39 -0500806 frag->msg_header.is_ccs = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500807 frag->msg_header.epoch = ssl->d1->w_epoch;
Adam Langleybcc4e232015-02-19 15:51:58 -0800808
David Benjamina97b7372015-11-03 14:54:39 -0500809 uint16_t priority = dtls1_get_queue_priority(frag->msg_header.seq,
810 0 /* handshake */);
811 uint8_t seq64be[8];
Adam Langleybcc4e232015-02-19 15:51:58 -0800812 memset(seq64be, 0, sizeof(seq64be));
David Benjamina97b7372015-11-03 14:54:39 -0500813 seq64be[6] = (uint8_t)(priority >> 8);
814 seq64be[7] = (uint8_t)priority;
Adam Langleybcc4e232015-02-19 15:51:58 -0800815
David Benjamina97b7372015-11-03 14:54:39 -0500816 pitem *item = pitem_new(seq64be, frag);
Adam Langleybcc4e232015-02-19 15:51:58 -0800817 if (item == NULL) {
818 dtls1_hm_fragment_free(frag);
819 return 0;
820 }
821
David Benjamin0d56f882015-12-19 17:05:56 -0500822 pqueue_insert(ssl->d1->sent_messages, item);
Adam Langleybcc4e232015-02-19 15:51:58 -0800823 return 1;
824}
825
David Benjamin0d56f882015-12-19 17:05:56 -0500826int dtls1_send_change_cipher_spec(SSL *ssl, int a, int b) {
827 if (ssl->state == a) {
David Benjamina97b7372015-11-03 14:54:39 -0500828 /* Buffer the message to handle retransmits. */
David Benjamin0d56f882015-12-19 17:05:56 -0500829 ssl->d1->handshake_write_seq = ssl->d1->next_handshake_write_seq;
830 dtls1_buffer_change_cipher_spec(ssl, ssl->d1->handshake_write_seq);
831 ssl->state = b;
David Benjamina97b7372015-11-03 14:54:39 -0500832 }
833
David Benjamin0d56f882015-12-19 17:05:56 -0500834 return dtls1_write_change_cipher_spec(ssl, dtls1_use_current_epoch);
David Benjamina97b7372015-11-03 14:54:39 -0500835}
836
Adam Langley95c29f32014-06-20 12:00:00 -0700837/* call this function when the buffered messages are no longer needed */
David Benjamin0d56f882015-12-19 17:05:56 -0500838void dtls1_clear_record_buffer(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800839 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -0700840
David Benjamin0d56f882015-12-19 17:05:56 -0500841 for (item = pqueue_pop(ssl->d1->sent_messages); item != NULL;
842 item = pqueue_pop(ssl->d1->sent_messages)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800843 dtls1_hm_fragment_free((hm_fragment *)item->data);
844 pitem_free(item);
845 }
846}
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langley95c29f32014-06-20 12:00:00 -0700848/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin0d56f882015-12-19 17:05:56 -0500849void dtls1_set_message_header(SSL *ssl, uint8_t mt, unsigned long len,
David Benjamin16d031a2014-12-14 18:52:44 -0500850 unsigned short seq_num, unsigned long frag_off,
851 unsigned long frag_len) {
David Benjamin0d56f882015-12-19 17:05:56 -0500852 struct hm_header_st *msg_hdr = &ssl->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700853
Adam Langley71d8a082014-12-13 16:28:18 -0800854 msg_hdr->type = mt;
855 msg_hdr->msg_len = len;
856 msg_hdr->seq = seq_num;
857 msg_hdr->frag_off = frag_off;
858 msg_hdr->frag_len = frag_len;
859}
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langley71d8a082014-12-13 16:28:18 -0800861unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500862 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800863}
Adam Langley95c29f32014-06-20 12:00:00 -0700864
Adam Langley71d8a082014-12-13 16:28:18 -0800865void dtls1_get_message_header(uint8_t *data,
866 struct hm_header_st *msg_hdr) {
867 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
868 msg_hdr->type = *(data++);
869 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700870
Adam Langley71d8a082014-12-13 16:28:18 -0800871 n2s(data, msg_hdr->seq);
872 n2l3(data, msg_hdr->frag_off);
873 n2l3(data, msg_hdr->frag_len);
874}