blob: 1390863d27bc075a0e0bd5e4fb1e73efeb232e61 [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
114#include <assert.h>
115#include <limits.h>
116#include <stdio.h>
117#include <string.h>
118
119#include <openssl/buf.h>
120#include <openssl/err.h>
121#include <openssl/evp.h>
122#include <openssl/mem.h>
123#include <openssl/obj.h>
124#include <openssl/rand.h>
125#include <openssl/x509.h>
126
127#include "ssl_locl.h"
128
129#define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
130
Adam Langley71d8a082014-12-13 16:28:18 -0800131#define RSMBLY_BITMASK_MARK(bitmask, start, end) \
132 { \
133 if ((end) - (start) <= 8) { \
134 long ii; \
135 for (ii = (start); ii < (end); ii++) \
136 bitmask[((ii) >> 3)] |= (1 << ((ii)&7)); \
137 } else { \
138 long ii; \
139 bitmask[((start) >> 3)] |= bitmask_start_values[((start)&7)]; \
140 for (ii = (((start) >> 3) + 1); ii < ((((end)-1)) >> 3); ii++) \
141 bitmask[ii] = 0xff; \
142 bitmask[(((end)-1) >> 3)] |= bitmask_end_values[((end)&7)]; \
143 } \
144 }
Adam Langley95c29f32014-06-20 12:00:00 -0700145
Adam Langley71d8a082014-12-13 16:28:18 -0800146#define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) \
147 { \
148 long ii; \
149 assert((msg_len) > 0); \
150 is_complete = 1; \
151 if (bitmask[(((msg_len)-1) >> 3)] != bitmask_end_values[((msg_len)&7)]) \
152 is_complete = 0; \
153 if (is_complete) \
154 for (ii = (((msg_len)-1) >> 3) - 1; ii >= 0; ii--) \
155 if (bitmask[ii] != 0xff) { \
156 is_complete = 0; \
157 break; \
158 } \
159 }
Adam Langley95c29f32014-06-20 12:00:00 -0700160
Adam Langley71d8a082014-12-13 16:28:18 -0800161static const uint8_t bitmask_start_values[] = {0xff, 0xfe, 0xfc, 0xf8,
162 0xf0, 0xe0, 0xc0, 0x80};
163static const uint8_t bitmask_end_values[] = {0xff, 0x01, 0x03, 0x07,
164 0x0f, 0x1f, 0x3f, 0x7f};
Adam Langley95c29f32014-06-20 12:00:00 -0700165
David Benjamina18b6712015-01-11 19:31:22 -0500166/* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
167 * for these values? Notably, why is kMinMTU a function of the transport
168 * protocol's overhead rather than, say, what's needed to hold a minimally-sized
169 * handshake fragment plus protocol overhead. */
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjamina18b6712015-01-11 19:31:22 -0500171/* kMinMTU is the minimum acceptable MTU value. */
172static const unsigned int kMinMTU = 256 - 28;
173
174/* kDefaultMTU is the default MTU value to use if neither the user nor
175 * the underlying BIO supplies one. */
176static const unsigned int kDefaultMTU = 1500 - 28;
177
Adam Langley71d8a082014-12-13 16:28:18 -0800178static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
179 unsigned long frag_len);
180static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
Adam Langley71d8a082014-12-13 16:28:18 -0800181static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langley71d8a082014-12-13 16:28:18 -0800183static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
184 int reassembly) {
185 hm_fragment *frag = NULL;
186 unsigned char *buf = NULL;
187 unsigned char *bitmask = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langley71d8a082014-12-13 16:28:18 -0800189 frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
190 if (frag == NULL) {
191 return NULL;
192 }
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley71d8a082014-12-13 16:28:18 -0800194 if (frag_len) {
195 buf = (unsigned char *)OPENSSL_malloc(frag_len);
196 if (buf == NULL) {
197 OPENSSL_free(frag);
198 return NULL;
199 }
200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley71d8a082014-12-13 16:28:18 -0800202 /* zero length fragment gets zero frag->fragment */
203 frag->fragment = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langley71d8a082014-12-13 16:28:18 -0800205 /* Initialize reassembly bitmask if necessary */
206 if (reassembly) {
207 bitmask = (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
208 if (bitmask == NULL) {
209 if (buf != NULL) {
210 OPENSSL_free(buf);
211 }
212 OPENSSL_free(frag);
213 return NULL;
214 }
215 memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley71d8a082014-12-13 16:28:18 -0800218 frag->reassembly = bitmask;
Adam Langley95c29f32014-06-20 12:00:00 -0700219
Adam Langley71d8a082014-12-13 16:28:18 -0800220 return frag;
221}
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley71d8a082014-12-13 16:28:18 -0800223void dtls1_hm_fragment_free(hm_fragment *frag) {
Adam Langley71d8a082014-12-13 16:28:18 -0800224 if (frag->fragment) {
225 OPENSSL_free(frag->fragment);
226 }
227 if (frag->reassembly) {
228 OPENSSL_free(frag->reassembly);
229 }
230 OPENSSL_free(frag);
231}
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langley71d8a082014-12-13 16:28:18 -0800233/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
234 * SSL3_RT_CHANGE_CIPHER_SPEC) */
David Benjamine4824e82014-12-14 19:44:34 -0500235int dtls1_do_write(SSL *s, int type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800236 int ret;
237 int curr_mtu;
David Benjamine95d20d2014-12-23 11:16:01 -0500238 unsigned int len, frag_off;
239 size_t max_overhead = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langley71d8a082014-12-13 16:28:18 -0800241 /* AHA! Figure out the MTU, and stick to the right size */
242 if (s->d1->mtu < dtls1_min_mtu() &&
243 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500244 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
245 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
246 s->d1->mtu = (unsigned)mtu;
247 } else {
David Benjamina18b6712015-01-11 19:31:22 -0500248 s->d1->mtu = kDefaultMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800249 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL);
250 }
251 }
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Adam Langley71d8a082014-12-13 16:28:18 -0800253 /* should have something reasonable now */
254 assert(s->d1->mtu >= dtls1_min_mtu());
Adam Langley95c29f32014-06-20 12:00:00 -0700255
Adam Langley71d8a082014-12-13 16:28:18 -0800256 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
257 assert(s->init_num ==
258 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
259 }
Adam Langley95c29f32014-06-20 12:00:00 -0700260
David Benjamine95d20d2014-12-23 11:16:01 -0500261 /* Determine the maximum overhead of the current cipher. */
262 if (s->aead_write_ctx != NULL) {
263 max_overhead = EVP_AEAD_max_overhead(s->aead_write_ctx->ctx.aead);
264 if (s->aead_write_ctx->variable_nonce_included_in_record) {
265 max_overhead += s->aead_write_ctx->variable_nonce_len;
266 }
Adam Langley71d8a082014-12-13 16:28:18 -0800267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langley71d8a082014-12-13 16:28:18 -0800269 frag_off = 0;
270 while (s->init_num) {
David Benjamin17a5f852015-01-11 19:46:52 -0500271 /* Account for data in the buffering BIO; multiple records may be packed
272 * into a single packet during the handshake.
273 *
274 * TODO(davidben): This is buggy; if the MTU is larger than the buffer size,
275 * the large record will be split across two packets. Moreover, in that
276 * case, the |dtls1_write_bytes| call may not return synchronously. This
277 * will break on retry as the |s->init_off| and |s->init_num| adjustment
278 * will run a second time. */
Adam Langley71d8a082014-12-13 16:28:18 -0800279 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
David Benjamine95d20d2014-12-23 11:16:01 -0500280 DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
David Benjamin17a5f852015-01-11 19:46:52 -0500283 /* Flush the buffer and continue with a fresh packet.
284 *
285 * TODO(davidben): If |BIO_flush| is not synchronous and requires multiple
286 * calls to |dtls1_do_write|, |frag_off| will be wrong. */
Adam Langley71d8a082014-12-13 16:28:18 -0800287 ret = BIO_flush(SSL_get_wbio(s));
288 if (ret <= 0) {
289 return ret;
290 }
David Benjamin17a5f852015-01-11 19:46:52 -0500291 assert(BIO_wpending(SSL_get_wbio(s)) == 0);
David Benjamine95d20d2014-12-23 11:16:01 -0500292 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley71d8a082014-12-13 16:28:18 -0800293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langley71d8a082014-12-13 16:28:18 -0800295 /* XDTLS: this function is too long. split out the CCS part */
296 if (type == SSL3_RT_HANDSHAKE) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500297 /* If this isn't the first fragment, reserve space to prepend a new
298 * fragment header. This will override the body of a previous fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800299 if (s->init_off != 0) {
300 assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
301 s->init_off -= DTLS1_HM_HEADER_LENGTH;
302 s->init_num += DTLS1_HM_HEADER_LENGTH;
Adam Langley71d8a082014-12-13 16:28:18 -0800303 }
Adam Langley95c29f32014-06-20 12:00:00 -0700304
David Benjamind9778fb2015-01-11 17:24:51 -0500305 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
306 /* To make forward progress, the MTU must, at minimum, fit the handshake
307 * header and one byte of handshake body. */
308 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
309 return -1;
310 }
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjamind9778fb2015-01-11 17:24:51 -0500312 if (s->init_num > curr_mtu) {
313 len = curr_mtu;
314 } else {
315 len = s->init_num;
316 }
317 assert(len >= DTLS1_HM_HEADER_LENGTH);
318
319 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800320 dtls1_write_message_header(
321 s, (uint8_t *)&s->init_buf->data[s->init_off]);
David Benjamind9778fb2015-01-11 17:24:51 -0500322 } else {
323 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
324 /* ChangeCipherSpec cannot be fragmented. */
325 if (s->init_num > curr_mtu) {
326 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
327 return -1;
328 }
329 len = s->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800330 }
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langley71d8a082014-12-13 16:28:18 -0800332 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len);
333 if (ret < 0) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500334 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800335 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500336
337 /* bad if this assert fails, only part of the handshake message got sent.
338 * But why would this happen? */
339 assert(len == (unsigned int)ret);
340
341 if (ret == s->init_num) {
342 if (s->msg_callback) {
343 s->msg_callback(1, s->version, type, s->init_buf->data,
344 (size_t)(s->init_off + s->init_num), s,
345 s->msg_callback_arg);
346 }
347
348 s->init_off = 0; /* done writing this message */
349 s->init_num = 0;
350
351 return 1;
352 }
353 s->init_off += ret;
354 s->init_num -= ret;
355 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800356 }
357
358 return 0;
359}
Adam Langley95c29f32014-06-20 12:00:00 -0700360
361
Adam Langley71d8a082014-12-13 16:28:18 -0800362/* Obtain handshake message of message type 'mt' (any if mt == -1), maximum
363 * acceptable body length 'max'. Read an entire handshake message. Handshake
364 * messages arrive in fragments. */
365long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max,
366 int hash_message, int *ok) {
367 int i, al;
368 struct hm_header_st *msg_hdr;
369 uint8_t *p;
370 unsigned long msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langley71d8a082014-12-13 16:28:18 -0800372 /* s3->tmp is used to store messages that are unexpected, caused
373 * by the absence of an optional handshake message */
374 if (s->s3->tmp.reuse_message) {
375 /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined
376 * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE
377 * would have to have been applied to the previous call. */
378 assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE);
379 s->s3->tmp.reuse_message = 0;
380 if (mt >= 0 && s->s3->tmp.message_type != mt) {
381 al = SSL_AD_UNEXPECTED_MESSAGE;
382 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
383 goto f_err;
384 }
385 *ok = 1;
386 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
387 s->init_num = (int)s->s3->tmp.message_size;
388 return s->init_num;
389 }
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langley71d8a082014-12-13 16:28:18 -0800391 msg_hdr = &s->d1->r_msg_hdr;
392 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700393
394again:
Adam Langley71d8a082014-12-13 16:28:18 -0800395 i = dtls1_get_message_fragment(s, stn, max, ok);
396 if (i == DTLS1_HM_BAD_FRAGMENT ||
397 i == DTLS1_HM_FRAGMENT_RETRY) {
398 /* bad fragment received */
399 goto again;
400 } else if (i <= 0 && !*ok) {
401 return i;
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley71d8a082014-12-13 16:28:18 -0800404 p = (uint8_t *)s->init_buf->data;
405 msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langley71d8a082014-12-13 16:28:18 -0800407 /* reconstruct message header */
408 *(p++) = msg_hdr->type;
409 l2n3(msg_len, p);
410 s2n(msg_hdr->seq, p);
411 l2n3(0, p);
412 l2n3(msg_len, p);
413 p -= DTLS1_HM_HEADER_LENGTH;
414 msg_len += DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langley71d8a082014-12-13 16:28:18 -0800416 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
David Benjamin590cbe92014-08-25 21:34:56 -0400417
David Benjaminfbdfefb2015-02-16 19:33:53 -0500418 if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE &&
419 !ssl3_hash_current_message(s)) {
420 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800421 }
422 if (s->msg_callback) {
423 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, s,
424 s->msg_callback_arg);
425 }
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langley71d8a082014-12-13 16:28:18 -0800427 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley71d8a082014-12-13 16:28:18 -0800429 s->d1->handshake_read_seq++;
Adam Langley95c29f32014-06-20 12:00:00 -0700430
Adam Langley71d8a082014-12-13 16:28:18 -0800431 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700432
433f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800434 ssl3_send_alert(s, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500435err:
Adam Langley71d8a082014-12-13 16:28:18 -0800436 *ok = 0;
437 return -1;
438}
439
440static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr,
441 int max) {
442 size_t frag_off, frag_len, msg_len;
443
444 msg_len = msg_hdr->msg_len;
445 frag_off = msg_hdr->frag_off;
446 frag_len = msg_hdr->frag_len;
447
448 /* sanity checking */
449 if ((frag_off + frag_len) > msg_len) {
450 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
451 SSL_R_EXCESSIVE_MESSAGE_SIZE);
452 return SSL_AD_ILLEGAL_PARAMETER;
453 }
454
455 if ((frag_off + frag_len) > (unsigned long)max) {
456 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
457 SSL_R_EXCESSIVE_MESSAGE_SIZE);
458 return SSL_AD_ILLEGAL_PARAMETER;
459 }
460
461 if (s->d1->r_msg_hdr.frag_off == 0) {
462 /* first fragment */
463 /* msg_len is limited to 2^24, but is effectively checked
464 * against max above */
465 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
466 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
467 return SSL_AD_INTERNAL_ERROR;
468 }
469
470 s->s3->tmp.message_size = msg_len;
471 s->d1->r_msg_hdr.msg_len = msg_len;
472 s->s3->tmp.message_type = msg_hdr->type;
473 s->d1->r_msg_hdr.type = msg_hdr->type;
474 s->d1->r_msg_hdr.seq = msg_hdr->seq;
475 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
476 /* They must be playing with us! BTW, failure to enforce
477 * upper limit would open possibility for buffer overrun. */
478 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
479 SSL_R_EXCESSIVE_MESSAGE_SIZE);
480 return SSL_AD_ILLEGAL_PARAMETER;
481 }
482
483 return 0; /* no error */
484}
Adam Langley95c29f32014-06-20 12:00:00 -0700485
486
Adam Langley71d8a082014-12-13 16:28:18 -0800487static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) {
488 /* (0) check whether the desired fragment is available
489 * if so:
490 * (1) copy over the fragment to s->init_buf->data[]
491 * (2) update s->init_num */
492 pitem *item;
493 hm_fragment *frag;
494 int al;
495 unsigned long frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langley71d8a082014-12-13 16:28:18 -0800497 *ok = 0;
498 item = pqueue_peek(s->d1->buffered_messages);
499 if (item == NULL) {
500 return 0;
501 }
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley71d8a082014-12-13 16:28:18 -0800503 frag = (hm_fragment *)item->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langley71d8a082014-12-13 16:28:18 -0800505 /* Don't return if reassembly still in progress */
506 if (frag->reassembly != NULL) {
507 return 0;
508 }
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langley71d8a082014-12-13 16:28:18 -0800510 if (s->d1->handshake_read_seq != frag->msg_header.seq) {
511 return 0;
512 }
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langley71d8a082014-12-13 16:28:18 -0800514 frag_len = frag->msg_header.frag_len;
515 pqueue_pop(s->d1->buffered_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langley71d8a082014-12-13 16:28:18 -0800517 al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langley71d8a082014-12-13 16:28:18 -0800519 if (al == 0) {
520 /* no alert */
521 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
522 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
523 frag->msg_header.frag_len);
524 }
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langley71d8a082014-12-13 16:28:18 -0800526 dtls1_hm_fragment_free(frag);
527 pitem_free(item);
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langley71d8a082014-12-13 16:28:18 -0800529 if (al == 0) {
530 *ok = 1;
531 return frag_len;
532 }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langley71d8a082014-12-13 16:28:18 -0800534 ssl3_send_alert(s, SSL3_AL_FATAL, al);
535 s->init_num = 0;
536 *ok = 0;
537 return -1;
538}
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Matt Caswell2306fe52014-06-06 14:25:52 -0700540/* dtls1_max_handshake_message_len returns the maximum number of bytes
541 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
542 * be greater if the maximum certificate list size requires it. */
Adam Langley71d8a082014-12-13 16:28:18 -0800543static unsigned long dtls1_max_handshake_message_len(const SSL *s) {
544 unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
545 if (max_len < (unsigned long)s->max_cert_list) {
546 return s->max_cert_list;
547 }
548 return max_len;
549}
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langley71d8a082014-12-13 16:28:18 -0800551static int dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr,
552 int *ok) {
553 hm_fragment *frag = NULL;
554 pitem *item = NULL;
555 int i = -1, is_complete;
556 uint8_t seq64be[8];
557 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langley71d8a082014-12-13 16:28:18 -0800559 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
560 msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) {
561 goto err;
562 }
Adam Langley95c29f32014-06-20 12:00:00 -0700563
Adam Langley71d8a082014-12-13 16:28:18 -0800564 if (frag_len == 0) {
565 return DTLS1_HM_FRAGMENT_RETRY;
566 }
Adam Langleye951ff42014-06-06 14:30:33 -0700567
Adam Langley71d8a082014-12-13 16:28:18 -0800568 /* Try to find item in queue */
569 memset(seq64be, 0, sizeof(seq64be));
570 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
571 seq64be[7] = (uint8_t)msg_hdr->seq;
572 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langley71d8a082014-12-13 16:28:18 -0800574 if (item == NULL) {
575 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
576 if (frag == NULL) {
577 goto err;
578 }
579 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
580 frag->msg_header.frag_len = frag->msg_header.msg_len;
581 frag->msg_header.frag_off = 0;
582 } else {
583 frag = (hm_fragment *)item->data;
584 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
585 item = NULL;
586 frag = NULL;
587 goto err;
588 }
589 }
Adam Langleybed22142014-06-20 12:00:00 -0700590
Adam Langley71d8a082014-12-13 16:28:18 -0800591 /* If message is already reassembled, this must be a
592 * retransmit and can be dropped. In this case item != NULL and so frag
593 * does not need to be freed. */
594 if (frag->reassembly == NULL) {
595 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langley71d8a082014-12-13 16:28:18 -0800597 assert(item != NULL);
598 while (frag_len) {
599 i = s->method->ssl_read_bytes(
600 s, SSL3_RT_HANDSHAKE, devnull,
601 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
602 if (i <= 0) {
603 goto err;
604 }
605 frag_len -= i;
606 }
607 return DTLS1_HM_FRAGMENT_RETRY;
608 }
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langley71d8a082014-12-13 16:28:18 -0800610 /* read the body of the fragment (header has already been read */
611 i = s->method->ssl_read_bytes(
612 s, SSL3_RT_HANDSHAKE, frag->fragment + msg_hdr->frag_off, frag_len, 0);
613 if ((unsigned long)i != frag_len) {
614 i = -1;
615 }
616 if (i <= 0) {
617 goto err;
618 }
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langley71d8a082014-12-13 16:28:18 -0800620 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
621 (long)(msg_hdr->frag_off + frag_len));
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langley71d8a082014-12-13 16:28:18 -0800623 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
624 is_complete);
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langley71d8a082014-12-13 16:28:18 -0800626 if (is_complete) {
627 OPENSSL_free(frag->reassembly);
628 frag->reassembly = NULL;
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langley71d8a082014-12-13 16:28:18 -0800631 if (item == NULL) {
632 item = pitem_new(seq64be, frag);
633 if (item == NULL) {
634 i = -1;
635 goto err;
636 }
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langley71d8a082014-12-13 16:28:18 -0800638 item = pqueue_insert(s->d1->buffered_messages, item);
639 /* pqueue_insert fails iff a duplicate item is inserted.
640 * However, |item| cannot be a duplicate. If it were,
641 * |pqueue_find|, above, would have returned it and control
642 * would never have reached this branch. */
643 assert(item != NULL);
644 }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langley71d8a082014-12-13 16:28:18 -0800646 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700647
648err:
Adam Langley71d8a082014-12-13 16:28:18 -0800649 if (frag != NULL && item == NULL) {
650 dtls1_hm_fragment_free(frag);
651 }
652 *ok = 0;
653 return i;
654}
Adam Langley95c29f32014-06-20 12:00:00 -0700655
Adam Langley71d8a082014-12-13 16:28:18 -0800656static int dtls1_process_out_of_seq_message(SSL *s,
657 const struct hm_header_st *msg_hdr,
658 int *ok) {
659 int i = -1;
660 hm_fragment *frag = NULL;
661 pitem *item = NULL;
662 uint8_t seq64be[8];
663 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langley71d8a082014-12-13 16:28:18 -0800665 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) {
666 goto err;
667 }
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langley71d8a082014-12-13 16:28:18 -0800669 /* Try to find item in queue, to prevent duplicate entries */
670 memset(seq64be, 0, sizeof(seq64be));
671 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
672 seq64be[7] = (uint8_t)msg_hdr->seq;
673 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langley71d8a082014-12-13 16:28:18 -0800675 /* If we already have an entry and this one is a fragment,
676 * don't discard it and rather try to reassemble it. */
677 if (item != NULL && frag_len != msg_hdr->msg_len) {
678 item = NULL;
679 }
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langley71d8a082014-12-13 16:28:18 -0800681 /* Discard the message if sequence number was already there, is
David Benjaminf95ef932015-01-31 20:18:39 -0500682 * too far in the future, or already in the queue. */
Adam Langley71d8a082014-12-13 16:28:18 -0800683 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
David Benjaminf95ef932015-01-31 20:18:39 -0500684 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800685 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langley71d8a082014-12-13 16:28:18 -0800687 while (frag_len) {
688 i = s->method->ssl_read_bytes(
689 s, SSL3_RT_HANDSHAKE, devnull,
690 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
691 if (i <= 0) {
692 goto err;
693 }
694 frag_len -= i;
695 }
696 } else {
697 if (frag_len != msg_hdr->msg_len) {
698 return dtls1_reassemble_fragment(s, msg_hdr, ok);
699 }
Adam Langley95c29f32014-06-20 12:00:00 -0700700
Adam Langley71d8a082014-12-13 16:28:18 -0800701 if (frag_len > dtls1_max_handshake_message_len(s)) {
702 goto err;
703 }
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langley71d8a082014-12-13 16:28:18 -0800705 frag = dtls1_hm_fragment_new(frag_len, 0);
706 if (frag == NULL) {
707 goto err;
708 }
Matt Caswell2306fe52014-06-06 14:25:52 -0700709
Adam Langley71d8a082014-12-13 16:28:18 -0800710 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langley71d8a082014-12-13 16:28:18 -0800712 if (frag_len) {
713 /* read the body of the fragment (header has already been read */
714 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment,
715 frag_len, 0);
716 if ((unsigned long)i != frag_len) {
717 i = -1;
718 }
719 if (i <= 0) {
720 goto err;
721 }
722 }
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langley71d8a082014-12-13 16:28:18 -0800724 item = pitem_new(seq64be, frag);
725 if (item == NULL) {
726 goto err;
727 }
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langley71d8a082014-12-13 16:28:18 -0800729 item = pqueue_insert(s->d1->buffered_messages, item);
730 /* pqueue_insert fails iff a duplicate item is inserted.
731 * However, |item| cannot be a duplicate. If it were,
732 * |pqueue_find|, above, would have returned it. Then, either
733 * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
734 * to NULL and it will have been processed with
735 * |dtls1_reassemble_fragment|, above, or the record will have
736 * been discarded. */
737 assert(item != NULL);
738 }
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Adam Langley71d8a082014-12-13 16:28:18 -0800740 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700741
742err:
Adam Langley71d8a082014-12-13 16:28:18 -0800743 if (frag != NULL && item == NULL) {
744 dtls1_hm_fragment_free(frag);
745 }
746 *ok = 0;
747 return i;
748}
Adam Langley95c29f32014-06-20 12:00:00 -0700749
750
Adam Langley71d8a082014-12-13 16:28:18 -0800751static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok) {
752 uint8_t wire[DTLS1_HM_HEADER_LENGTH];
753 unsigned long len, frag_off, frag_len;
754 int i, al;
755 struct hm_header_st msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langley71d8a082014-12-13 16:28:18 -0800757redo:
758 /* see if we have the required fragment already */
759 if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
760 if (*ok) {
761 s->init_num = frag_len;
762 }
763 return frag_len;
764 }
Adam Langley95c29f32014-06-20 12:00:00 -0700765
Adam Langley71d8a082014-12-13 16:28:18 -0800766 /* read handshake message header */
767 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
768 DTLS1_HM_HEADER_LENGTH, 0);
769 if (i <= 0) {
770 /* nbio, or an error */
771 s->rwstate = SSL_READING;
772 *ok = 0;
773 return i;
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775
Adam Langley71d8a082014-12-13 16:28:18 -0800776 /* Handshake fails if message header is incomplete */
777 if (i != DTLS1_HM_HEADER_LENGTH) {
778 al = SSL_AD_UNEXPECTED_MESSAGE;
779 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
780 SSL_R_UNEXPECTED_MESSAGE);
781 goto f_err;
782 }
Adam Langley95c29f32014-06-20 12:00:00 -0700783
Adam Langley71d8a082014-12-13 16:28:18 -0800784 /* parse the message fragment header */
785 dtls1_get_message_header(wire, &msg_hdr);
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langley71d8a082014-12-13 16:28:18 -0800787 /* if this is a future (or stale) message it gets buffered
788 * (or dropped)--no further processing at this time. */
789 if (msg_hdr.seq != s->d1->handshake_read_seq) {
790 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
791 }
Adam Langley95c29f32014-06-20 12:00:00 -0700792
Adam Langley71d8a082014-12-13 16:28:18 -0800793 len = msg_hdr.msg_len;
794 frag_off = msg_hdr.frag_off;
795 frag_len = msg_hdr.frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700796
Adam Langley71d8a082014-12-13 16:28:18 -0800797 if (frag_len && frag_len < len) {
798 return dtls1_reassemble_fragment(s, &msg_hdr, ok);
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langley71d8a082014-12-13 16:28:18 -0800801 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
802 wire[0] == SSL3_MT_HELLO_REQUEST) {
803 /* The server may always send 'Hello Request' messages --
804 * we are doing a handshake anyway now, so ignore them
805 * if their format is correct. Does not count for
806 * 'Finished' MAC. */
807 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
808 if (s->msg_callback) {
809 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, wire,
810 DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg);
811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langley71d8a082014-12-13 16:28:18 -0800813 s->init_num = 0;
814 goto redo;
815 } else {
816 /* Incorrectly formated Hello request */
817 al = SSL_AD_UNEXPECTED_MESSAGE;
818 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
819 SSL_R_UNEXPECTED_MESSAGE);
820 goto f_err;
821 }
822 }
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langley71d8a082014-12-13 16:28:18 -0800824 if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) {
825 goto f_err;
826 }
Adam Langley95c29f32014-06-20 12:00:00 -0700827
Adam Langley71d8a082014-12-13 16:28:18 -0800828 /* XDTLS: ressurect this when restart is in place */
829 s->state = stn;
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langley71d8a082014-12-13 16:28:18 -0800831 if (frag_len > 0) {
832 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langley71d8a082014-12-13 16:28:18 -0800834 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[frag_off], frag_len,
835 0);
836 /* XDTLS: fix this--message fragments cannot span multiple packets */
837 if (i <= 0) {
838 s->rwstate = SSL_READING;
839 *ok = 0;
840 return i;
841 }
842 } else {
843 i = 0;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langley71d8a082014-12-13 16:28:18 -0800846 /* XDTLS: an incorrectly formatted fragment should cause the
847 * handshake to fail */
848 if (i != (int)frag_len) {
849 al = SSL3_AD_ILLEGAL_PARAMETER;
850 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
851 SSL3_AD_ILLEGAL_PARAMETER);
852 goto f_err;
853 }
854
855 *ok = 1;
856
857 /* Note that s->init_num is *not* used as current offset in
858 * s->init_buf->data, but as a counter summing up fragments'
859 * lengths: as soon as they sum up to handshake packet
860 * length, we assume we have got all the fragments. */
861 s->init_num = frag_len;
862 return frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700863
864f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800865 ssl3_send_alert(s, SSL3_AL_FATAL, al);
866 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langley71d8a082014-12-13 16:28:18 -0800868 *ok = 0;
869 return -1;
870}
Adam Langley95c29f32014-06-20 12:00:00 -0700871
872/* for these 2 messages, we need to
873 * ssl->enc_read_ctx re-init
874 * ssl->s3->read_sequence zero
875 * ssl->s3->read_mac_secret re-init
876 * ssl->session->read_sym_enc assign
877 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800878 * ssl->session->read_hash assign */
879int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
880 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langley71d8a082014-12-13 16:28:18 -0800882 if (s->state == a) {
883 p = (uint8_t *)s->init_buf->data;
884 *p++ = SSL3_MT_CCS;
885 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
886 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langley71d8a082014-12-13 16:28:18 -0800888 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700889
David Benjamin16d031a2014-12-14 18:52:44 -0500890 dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
891 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langley71d8a082014-12-13 16:28:18 -0800893 /* buffer the message to handle re-xmits */
894 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700895
Adam Langley71d8a082014-12-13 16:28:18 -0800896 s->state = b;
897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langley71d8a082014-12-13 16:28:18 -0800899 /* SSL3_ST_CW_CHANGE_B */
David Benjamine4824e82014-12-14 19:44:34 -0500900 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
Adam Langley71d8a082014-12-13 16:28:18 -0800901}
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langley71d8a082014-12-13 16:28:18 -0800903int dtls1_read_failed(SSL *s, int code) {
904 if (code > 0) {
Adam Langleyd3459fb2015-02-13 14:56:28 -0800905 assert(0);
Adam Langley71d8a082014-12-13 16:28:18 -0800906 return 1;
907 }
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langley71d8a082014-12-13 16:28:18 -0800909 if (!dtls1_is_timer_expired(s)) {
910 /* not a timeout, none of our business, let higher layers handle this. In
911 * fact, it's probably an error */
912 return code;
913 }
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langley71d8a082014-12-13 16:28:18 -0800915 if (!SSL_in_init(s)) {
916 /* done, no need to send a retransmit */
917 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
918 return code;
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langley71d8a082014-12-13 16:28:18 -0800921 return dtls1_handle_timeout(s);
922}
Adam Langley95c29f32014-06-20 12:00:00 -0700923
Adam Langley71d8a082014-12-13 16:28:18 -0800924int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
925 /* The index of the retransmission queue actually is the message sequence
926 * number, since the queue only contains messages of a single handshake.
927 * However, the ChangeCipherSpec has no message sequence number and so using
928 * only the sequence will result in the CCS and Finished having the same
929 * index. To prevent this, the sequence number is multiplied by 2. In case of
930 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
931 * maintains the order of the index (important for priority queues) and fits
932 * in the unsigned short variable. */
933 return seq * 2 - is_ccs;
934}
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langleybcc4e232015-02-19 15:51:58 -0800936static int dtls1_retransmit_message(SSL *s, hm_fragment *frag) {
Adam Langley71d8a082014-12-13 16:28:18 -0800937 int ret;
938 /* XDTLS: for now assuming that read/writes are blocking */
Adam Langley71d8a082014-12-13 16:28:18 -0800939 unsigned long header_length;
Adam Langley71d8a082014-12-13 16:28:18 -0800940 uint8_t save_write_sequence[8];
Adam Langley95c29f32014-06-20 12:00:00 -0700941
Adam Langley71d8a082014-12-13 16:28:18 -0800942 /* assert(s->init_num == 0);
943 assert(s->init_off == 0); */
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Adam Langley71d8a082014-12-13 16:28:18 -0800945 if (frag->msg_header.is_ccs) {
946 header_length = DTLS1_CCS_HEADER_LENGTH;
947 } else {
948 header_length = DTLS1_HM_HEADER_LENGTH;
949 }
Adam Langley95c29f32014-06-20 12:00:00 -0700950
Adam Langley71d8a082014-12-13 16:28:18 -0800951 memcpy(s->init_buf->data, frag->fragment,
952 frag->msg_header.msg_len + header_length);
953 s->init_num = frag->msg_header.msg_len + header_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700954
David Benjamin16d031a2014-12-14 18:52:44 -0500955 dtls1_set_message_header(s, frag->msg_header.type,
956 frag->msg_header.msg_len, frag->msg_header.seq,
957 0, frag->msg_header.frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700958
David Benjaminafbc63f2015-02-01 02:33:59 -0500959 /* Save current state. */
960 SSL_AEAD_CTX *aead_write_ctx = s->aead_write_ctx;
961 uint16_t epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -0700962
David Benjaminafbc63f2015-02-01 02:33:59 -0500963 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
964 * (negotiated cipher) exist. */
965 assert(epoch == 0 || epoch == 1);
966 assert(frag->msg_header.epoch <= epoch);
967 const int fragment_from_previous_epoch = (epoch == 1 &&
968 frag->msg_header.epoch == 0);
969 if (fragment_from_previous_epoch) {
970 /* Rewind to the previous epoch.
971 *
972 * TODO(davidben): Instead of swapping out connection-global state, this
973 * logic should pass a "use previous epoch" parameter down to lower-level
974 * functions. */
975 s->d1->w_epoch = frag->msg_header.epoch;
976 s->aead_write_ctx = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800977 memcpy(save_write_sequence, s->s3->write_sequence,
978 sizeof(s->s3->write_sequence));
979 memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
980 sizeof(s->s3->write_sequence));
David Benjaminafbc63f2015-02-01 02:33:59 -0500981 } else {
982 /* Otherwise the messages must be from the same epoch. */
983 assert(frag->msg_header.epoch == epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800984 }
985
986 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
David Benjamine4824e82014-12-14 19:44:34 -0500987 : SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800988
David Benjaminafbc63f2015-02-01 02:33:59 -0500989 if (fragment_from_previous_epoch) {
990 /* Restore the current epoch. */
991 s->aead_write_ctx = aead_write_ctx;
992 s->d1->w_epoch = epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800993 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
994 sizeof(s->s3->write_sequence));
995 memcpy(s->s3->write_sequence, save_write_sequence,
996 sizeof(s->s3->write_sequence));
997 }
998
Adam Langley71d8a082014-12-13 16:28:18 -0800999 (void)BIO_flush(SSL_get_wbio(s));
1000 return ret;
1001}
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langleybcc4e232015-02-19 15:51:58 -08001003
1004int dtls1_retransmit_buffered_messages(SSL *s) {
1005 pqueue sent = s->d1->sent_messages;
1006 piterator iter = pqueue_iterator(sent);
1007 pitem *item;
1008
1009 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
1010 hm_fragment *frag = (hm_fragment *)item->data;
1011 if (dtls1_retransmit_message(s, frag) <= 0) {
1012 return -1;
1013 }
1014 }
1015
1016 return 1;
1017}
1018
1019int dtls1_buffer_message(SSL *s, int is_ccs) {
1020 pitem *item;
1021 hm_fragment *frag;
1022 uint8_t seq64be[8];
1023
1024 /* this function is called immediately after a message has
1025 * been serialized */
1026 assert(s->init_off == 0);
1027
1028 frag = dtls1_hm_fragment_new(s->init_num, 0);
1029 if (!frag) {
1030 return 0;
1031 }
1032
1033 memcpy(frag->fragment, s->init_buf->data, s->init_num);
1034
1035 if (is_ccs) {
1036 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
1037 (unsigned int)s->init_num);
1038 } else {
1039 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
1040 (unsigned int)s->init_num);
1041 }
1042
1043 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1044 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1045 frag->msg_header.type = s->d1->w_msg_hdr.type;
1046 frag->msg_header.frag_off = 0;
1047 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1048 frag->msg_header.is_ccs = is_ccs;
1049 frag->msg_header.epoch = s->d1->w_epoch;
1050
1051 memset(seq64be, 0, sizeof(seq64be));
1052 seq64be[6] = (uint8_t)(
1053 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
1054 8);
1055 seq64be[7] = (uint8_t)(
1056 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
1057
1058 item = pitem_new(seq64be, frag);
1059 if (item == NULL) {
1060 dtls1_hm_fragment_free(frag);
1061 return 0;
1062 }
1063
1064 pqueue_insert(s->d1->sent_messages, item);
1065 return 1;
1066}
1067
Adam Langley95c29f32014-06-20 12:00:00 -07001068/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -08001069void dtls1_clear_record_buffer(SSL *s) {
1070 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langley71d8a082014-12-13 16:28:18 -08001072 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
1073 item = pqueue_pop(s->d1->sent_messages)) {
1074 dtls1_hm_fragment_free((hm_fragment *)item->data);
1075 pitem_free(item);
1076 }
1077}
Adam Langley95c29f32014-06-20 12:00:00 -07001078
Adam Langley95c29f32014-06-20 12:00:00 -07001079/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin16d031a2014-12-14 18:52:44 -05001080void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
1081 unsigned short seq_num, unsigned long frag_off,
1082 unsigned long frag_len) {
Adam Langley71d8a082014-12-13 16:28:18 -08001083 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001084
Adam Langley71d8a082014-12-13 16:28:18 -08001085 msg_hdr->type = mt;
1086 msg_hdr->msg_len = len;
1087 msg_hdr->seq = seq_num;
1088 msg_hdr->frag_off = frag_off;
1089 msg_hdr->frag_len = frag_len;
1090}
Adam Langley95c29f32014-06-20 12:00:00 -07001091
Adam Langley71d8a082014-12-13 16:28:18 -08001092static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1093 unsigned long frag_len) {
1094 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langley71d8a082014-12-13 16:28:18 -08001096 msg_hdr->frag_off = frag_off;
1097 msg_hdr->frag_len = frag_len;
1098}
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langley71d8a082014-12-13 16:28:18 -08001100static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
1101 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langley71d8a082014-12-13 16:28:18 -08001103 *p++ = msg_hdr->type;
1104 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001105
Adam Langley71d8a082014-12-13 16:28:18 -08001106 s2n(msg_hdr->seq, p);
1107 l2n3(msg_hdr->frag_off, p);
1108 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langley71d8a082014-12-13 16:28:18 -08001110 return p;
1111}
Adam Langley95c29f32014-06-20 12:00:00 -07001112
Adam Langley71d8a082014-12-13 16:28:18 -08001113unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -05001114 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -08001115}
Adam Langley95c29f32014-06-20 12:00:00 -07001116
Adam Langley71d8a082014-12-13 16:28:18 -08001117void dtls1_get_message_header(uint8_t *data,
1118 struct hm_header_st *msg_hdr) {
1119 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
1120 msg_hdr->type = *(data++);
1121 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langley71d8a082014-12-13 16:28:18 -08001123 n2s(data, msg_hdr->seq);
1124 n2l3(data, msg_hdr->frag_off);
1125 n2l3(data, msg_hdr->frag_len);
1126}
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langley71d8a082014-12-13 16:28:18 -08001128void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) {
1129 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -07001130
Adam Langley71d8a082014-12-13 16:28:18 -08001131 ccs_hdr->type = *(data++);
1132}
Adam Langley95c29f32014-06-20 12:00:00 -07001133
Adam Langley71d8a082014-12-13 16:28:18 -08001134int dtls1_shutdown(SSL *s) {
1135 int ret;
1136 ret = ssl3_shutdown(s);
1137 return ret;
1138}