blob: 72dadc6e6fe7bc557a2d2e01b567991da30d796c [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
Adam Langley71d8a082014-12-13 16:28:18 -0800418 if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE) {
419 ssl3_hash_current_message(s);
420 }
421 if (s->msg_callback) {
422 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, s,
423 s->msg_callback_arg);
424 }
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langley71d8a082014-12-13 16:28:18 -0800426 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langley71d8a082014-12-13 16:28:18 -0800428 s->d1->handshake_read_seq++;
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langley71d8a082014-12-13 16:28:18 -0800430 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
432f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800433 ssl3_send_alert(s, SSL3_AL_FATAL, al);
434 *ok = 0;
435 return -1;
436}
437
438static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr,
439 int max) {
440 size_t frag_off, frag_len, msg_len;
441
442 msg_len = msg_hdr->msg_len;
443 frag_off = msg_hdr->frag_off;
444 frag_len = msg_hdr->frag_len;
445
446 /* sanity checking */
447 if ((frag_off + frag_len) > msg_len) {
448 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
449 SSL_R_EXCESSIVE_MESSAGE_SIZE);
450 return SSL_AD_ILLEGAL_PARAMETER;
451 }
452
453 if ((frag_off + frag_len) > (unsigned long)max) {
454 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
455 SSL_R_EXCESSIVE_MESSAGE_SIZE);
456 return SSL_AD_ILLEGAL_PARAMETER;
457 }
458
459 if (s->d1->r_msg_hdr.frag_off == 0) {
460 /* first fragment */
461 /* msg_len is limited to 2^24, but is effectively checked
462 * against max above */
463 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
464 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
465 return SSL_AD_INTERNAL_ERROR;
466 }
467
468 s->s3->tmp.message_size = msg_len;
469 s->d1->r_msg_hdr.msg_len = msg_len;
470 s->s3->tmp.message_type = msg_hdr->type;
471 s->d1->r_msg_hdr.type = msg_hdr->type;
472 s->d1->r_msg_hdr.seq = msg_hdr->seq;
473 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
474 /* They must be playing with us! BTW, failure to enforce
475 * upper limit would open possibility for buffer overrun. */
476 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
477 SSL_R_EXCESSIVE_MESSAGE_SIZE);
478 return SSL_AD_ILLEGAL_PARAMETER;
479 }
480
481 return 0; /* no error */
482}
Adam Langley95c29f32014-06-20 12:00:00 -0700483
484
Adam Langley71d8a082014-12-13 16:28:18 -0800485static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) {
486 /* (0) check whether the desired fragment is available
487 * if so:
488 * (1) copy over the fragment to s->init_buf->data[]
489 * (2) update s->init_num */
490 pitem *item;
491 hm_fragment *frag;
492 int al;
493 unsigned long frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langley71d8a082014-12-13 16:28:18 -0800495 *ok = 0;
496 item = pqueue_peek(s->d1->buffered_messages);
497 if (item == NULL) {
498 return 0;
499 }
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langley71d8a082014-12-13 16:28:18 -0800501 frag = (hm_fragment *)item->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley71d8a082014-12-13 16:28:18 -0800503 /* Don't return if reassembly still in progress */
504 if (frag->reassembly != NULL) {
505 return 0;
506 }
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langley71d8a082014-12-13 16:28:18 -0800508 if (s->d1->handshake_read_seq != frag->msg_header.seq) {
509 return 0;
510 }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langley71d8a082014-12-13 16:28:18 -0800512 frag_len = frag->msg_header.frag_len;
513 pqueue_pop(s->d1->buffered_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langley71d8a082014-12-13 16:28:18 -0800515 al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langley71d8a082014-12-13 16:28:18 -0800517 if (al == 0) {
518 /* no alert */
519 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
520 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
521 frag->msg_header.frag_len);
522 }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley71d8a082014-12-13 16:28:18 -0800524 dtls1_hm_fragment_free(frag);
525 pitem_free(item);
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langley71d8a082014-12-13 16:28:18 -0800527 if (al == 0) {
528 *ok = 1;
529 return frag_len;
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langley71d8a082014-12-13 16:28:18 -0800532 ssl3_send_alert(s, SSL3_AL_FATAL, al);
533 s->init_num = 0;
534 *ok = 0;
535 return -1;
536}
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Matt Caswell2306fe52014-06-06 14:25:52 -0700538/* dtls1_max_handshake_message_len returns the maximum number of bytes
539 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
540 * be greater if the maximum certificate list size requires it. */
Adam Langley71d8a082014-12-13 16:28:18 -0800541static unsigned long dtls1_max_handshake_message_len(const SSL *s) {
542 unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
543 if (max_len < (unsigned long)s->max_cert_list) {
544 return s->max_cert_list;
545 }
546 return max_len;
547}
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langley71d8a082014-12-13 16:28:18 -0800549static int dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr,
550 int *ok) {
551 hm_fragment *frag = NULL;
552 pitem *item = NULL;
553 int i = -1, is_complete;
554 uint8_t seq64be[8];
555 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langley71d8a082014-12-13 16:28:18 -0800557 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
558 msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) {
559 goto err;
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
Adam Langley71d8a082014-12-13 16:28:18 -0800562 if (frag_len == 0) {
563 return DTLS1_HM_FRAGMENT_RETRY;
564 }
Adam Langleye951ff42014-06-06 14:30:33 -0700565
Adam Langley71d8a082014-12-13 16:28:18 -0800566 /* Try to find item in queue */
567 memset(seq64be, 0, sizeof(seq64be));
568 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
569 seq64be[7] = (uint8_t)msg_hdr->seq;
570 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700571
Adam Langley71d8a082014-12-13 16:28:18 -0800572 if (item == NULL) {
573 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
574 if (frag == NULL) {
575 goto err;
576 }
577 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
578 frag->msg_header.frag_len = frag->msg_header.msg_len;
579 frag->msg_header.frag_off = 0;
580 } else {
581 frag = (hm_fragment *)item->data;
582 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
583 item = NULL;
584 frag = NULL;
585 goto err;
586 }
587 }
Adam Langleybed22142014-06-20 12:00:00 -0700588
Adam Langley71d8a082014-12-13 16:28:18 -0800589 /* If message is already reassembled, this must be a
590 * retransmit and can be dropped. In this case item != NULL and so frag
591 * does not need to be freed. */
592 if (frag->reassembly == NULL) {
593 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700594
Adam Langley71d8a082014-12-13 16:28:18 -0800595 assert(item != NULL);
596 while (frag_len) {
597 i = s->method->ssl_read_bytes(
598 s, SSL3_RT_HANDSHAKE, devnull,
599 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
600 if (i <= 0) {
601 goto err;
602 }
603 frag_len -= i;
604 }
605 return DTLS1_HM_FRAGMENT_RETRY;
606 }
Adam Langley95c29f32014-06-20 12:00:00 -0700607
Adam Langley71d8a082014-12-13 16:28:18 -0800608 /* read the body of the fragment (header has already been read */
609 i = s->method->ssl_read_bytes(
610 s, SSL3_RT_HANDSHAKE, frag->fragment + msg_hdr->frag_off, frag_len, 0);
611 if ((unsigned long)i != frag_len) {
612 i = -1;
613 }
614 if (i <= 0) {
615 goto err;
616 }
Adam Langley95c29f32014-06-20 12:00:00 -0700617
Adam Langley71d8a082014-12-13 16:28:18 -0800618 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
619 (long)(msg_hdr->frag_off + frag_len));
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langley71d8a082014-12-13 16:28:18 -0800621 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
622 is_complete);
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langley71d8a082014-12-13 16:28:18 -0800624 if (is_complete) {
625 OPENSSL_free(frag->reassembly);
626 frag->reassembly = NULL;
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langley71d8a082014-12-13 16:28:18 -0800629 if (item == NULL) {
630 item = pitem_new(seq64be, frag);
631 if (item == NULL) {
632 i = -1;
633 goto err;
634 }
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley71d8a082014-12-13 16:28:18 -0800636 item = pqueue_insert(s->d1->buffered_messages, item);
637 /* pqueue_insert fails iff a duplicate item is inserted.
638 * However, |item| cannot be a duplicate. If it were,
639 * |pqueue_find|, above, would have returned it and control
640 * would never have reached this branch. */
641 assert(item != NULL);
642 }
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langley71d8a082014-12-13 16:28:18 -0800644 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700645
646err:
Adam Langley71d8a082014-12-13 16:28:18 -0800647 if (frag != NULL && item == NULL) {
648 dtls1_hm_fragment_free(frag);
649 }
650 *ok = 0;
651 return i;
652}
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langley71d8a082014-12-13 16:28:18 -0800654static int dtls1_process_out_of_seq_message(SSL *s,
655 const struct hm_header_st *msg_hdr,
656 int *ok) {
657 int i = -1;
658 hm_fragment *frag = NULL;
659 pitem *item = NULL;
660 uint8_t seq64be[8];
661 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langley71d8a082014-12-13 16:28:18 -0800663 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) {
664 goto err;
665 }
Adam Langley95c29f32014-06-20 12:00:00 -0700666
Adam Langley71d8a082014-12-13 16:28:18 -0800667 /* Try to find item in queue, to prevent duplicate entries */
668 memset(seq64be, 0, sizeof(seq64be));
669 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
670 seq64be[7] = (uint8_t)msg_hdr->seq;
671 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700672
Adam Langley71d8a082014-12-13 16:28:18 -0800673 /* If we already have an entry and this one is a fragment,
674 * don't discard it and rather try to reassemble it. */
675 if (item != NULL && frag_len != msg_hdr->msg_len) {
676 item = NULL;
677 }
Adam Langley95c29f32014-06-20 12:00:00 -0700678
Adam Langley71d8a082014-12-13 16:28:18 -0800679 /* Discard the message if sequence number was already there, is
David Benjaminf95ef932015-01-31 20:18:39 -0500680 * too far in the future, or already in the queue. */
Adam Langley71d8a082014-12-13 16:28:18 -0800681 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
David Benjaminf95ef932015-01-31 20:18:39 -0500682 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800683 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langley71d8a082014-12-13 16:28:18 -0800685 while (frag_len) {
686 i = s->method->ssl_read_bytes(
687 s, SSL3_RT_HANDSHAKE, devnull,
688 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
689 if (i <= 0) {
690 goto err;
691 }
692 frag_len -= i;
693 }
694 } else {
695 if (frag_len != msg_hdr->msg_len) {
696 return dtls1_reassemble_fragment(s, msg_hdr, ok);
697 }
Adam Langley95c29f32014-06-20 12:00:00 -0700698
Adam Langley71d8a082014-12-13 16:28:18 -0800699 if (frag_len > dtls1_max_handshake_message_len(s)) {
700 goto err;
701 }
Adam Langley95c29f32014-06-20 12:00:00 -0700702
Adam Langley71d8a082014-12-13 16:28:18 -0800703 frag = dtls1_hm_fragment_new(frag_len, 0);
704 if (frag == NULL) {
705 goto err;
706 }
Matt Caswell2306fe52014-06-06 14:25:52 -0700707
Adam Langley71d8a082014-12-13 16:28:18 -0800708 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langley71d8a082014-12-13 16:28:18 -0800710 if (frag_len) {
711 /* read the body of the fragment (header has already been read */
712 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment,
713 frag_len, 0);
714 if ((unsigned long)i != frag_len) {
715 i = -1;
716 }
717 if (i <= 0) {
718 goto err;
719 }
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langley71d8a082014-12-13 16:28:18 -0800722 item = pitem_new(seq64be, frag);
723 if (item == NULL) {
724 goto err;
725 }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langley71d8a082014-12-13 16:28:18 -0800727 item = pqueue_insert(s->d1->buffered_messages, item);
728 /* pqueue_insert fails iff a duplicate item is inserted.
729 * However, |item| cannot be a duplicate. If it were,
730 * |pqueue_find|, above, would have returned it. Then, either
731 * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
732 * to NULL and it will have been processed with
733 * |dtls1_reassemble_fragment|, above, or the record will have
734 * been discarded. */
735 assert(item != NULL);
736 }
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langley71d8a082014-12-13 16:28:18 -0800738 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700739
740err:
Adam Langley71d8a082014-12-13 16:28:18 -0800741 if (frag != NULL && item == NULL) {
742 dtls1_hm_fragment_free(frag);
743 }
744 *ok = 0;
745 return i;
746}
Adam Langley95c29f32014-06-20 12:00:00 -0700747
748
Adam Langley71d8a082014-12-13 16:28:18 -0800749static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok) {
750 uint8_t wire[DTLS1_HM_HEADER_LENGTH];
751 unsigned long len, frag_off, frag_len;
752 int i, al;
753 struct hm_header_st msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700754
Adam Langley71d8a082014-12-13 16:28:18 -0800755redo:
756 /* see if we have the required fragment already */
757 if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
758 if (*ok) {
759 s->init_num = frag_len;
760 }
761 return frag_len;
762 }
Adam Langley95c29f32014-06-20 12:00:00 -0700763
Adam Langley71d8a082014-12-13 16:28:18 -0800764 /* read handshake message header */
765 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
766 DTLS1_HM_HEADER_LENGTH, 0);
767 if (i <= 0) {
768 /* nbio, or an error */
769 s->rwstate = SSL_READING;
770 *ok = 0;
771 return i;
772 }
Adam Langley95c29f32014-06-20 12:00:00 -0700773
Adam Langley71d8a082014-12-13 16:28:18 -0800774 /* Handshake fails if message header is incomplete */
775 if (i != DTLS1_HM_HEADER_LENGTH) {
776 al = SSL_AD_UNEXPECTED_MESSAGE;
777 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
778 SSL_R_UNEXPECTED_MESSAGE);
779 goto f_err;
780 }
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langley71d8a082014-12-13 16:28:18 -0800782 /* parse the message fragment header */
783 dtls1_get_message_header(wire, &msg_hdr);
Adam Langley95c29f32014-06-20 12:00:00 -0700784
Adam Langley71d8a082014-12-13 16:28:18 -0800785 /* if this is a future (or stale) message it gets buffered
786 * (or dropped)--no further processing at this time. */
787 if (msg_hdr.seq != s->d1->handshake_read_seq) {
788 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
789 }
Adam Langley95c29f32014-06-20 12:00:00 -0700790
Adam Langley71d8a082014-12-13 16:28:18 -0800791 len = msg_hdr.msg_len;
792 frag_off = msg_hdr.frag_off;
793 frag_len = msg_hdr.frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langley71d8a082014-12-13 16:28:18 -0800795 if (frag_len && frag_len < len) {
796 return dtls1_reassemble_fragment(s, &msg_hdr, ok);
797 }
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langley71d8a082014-12-13 16:28:18 -0800799 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
800 wire[0] == SSL3_MT_HELLO_REQUEST) {
801 /* The server may always send 'Hello Request' messages --
802 * we are doing a handshake anyway now, so ignore them
803 * if their format is correct. Does not count for
804 * 'Finished' MAC. */
805 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
806 if (s->msg_callback) {
807 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, wire,
808 DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg);
809 }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langley71d8a082014-12-13 16:28:18 -0800811 s->init_num = 0;
812 goto redo;
813 } else {
814 /* Incorrectly formated Hello request */
815 al = SSL_AD_UNEXPECTED_MESSAGE;
816 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
817 SSL_R_UNEXPECTED_MESSAGE);
818 goto f_err;
819 }
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langley71d8a082014-12-13 16:28:18 -0800822 if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) {
823 goto f_err;
824 }
Adam Langley95c29f32014-06-20 12:00:00 -0700825
Adam Langley71d8a082014-12-13 16:28:18 -0800826 /* XDTLS: ressurect this when restart is in place */
827 s->state = stn;
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langley71d8a082014-12-13 16:28:18 -0800829 if (frag_len > 0) {
830 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langley71d8a082014-12-13 16:28:18 -0800832 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[frag_off], frag_len,
833 0);
834 /* XDTLS: fix this--message fragments cannot span multiple packets */
835 if (i <= 0) {
836 s->rwstate = SSL_READING;
837 *ok = 0;
838 return i;
839 }
840 } else {
841 i = 0;
842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langley71d8a082014-12-13 16:28:18 -0800844 /* XDTLS: an incorrectly formatted fragment should cause the
845 * handshake to fail */
846 if (i != (int)frag_len) {
847 al = SSL3_AD_ILLEGAL_PARAMETER;
848 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
849 SSL3_AD_ILLEGAL_PARAMETER);
850 goto f_err;
851 }
852
853 *ok = 1;
854
855 /* Note that s->init_num is *not* used as current offset in
856 * s->init_buf->data, but as a counter summing up fragments'
857 * lengths: as soon as they sum up to handshake packet
858 * length, we assume we have got all the fragments. */
859 s->init_num = frag_len;
860 return frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700861
862f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800863 ssl3_send_alert(s, SSL3_AL_FATAL, al);
864 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langley71d8a082014-12-13 16:28:18 -0800866 *ok = 0;
867 return -1;
868}
Adam Langley95c29f32014-06-20 12:00:00 -0700869
870/* for these 2 messages, we need to
871 * ssl->enc_read_ctx re-init
872 * ssl->s3->read_sequence zero
873 * ssl->s3->read_mac_secret re-init
874 * ssl->session->read_sym_enc assign
875 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800876 * ssl->session->read_hash assign */
877int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
878 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700879
Adam Langley71d8a082014-12-13 16:28:18 -0800880 if (s->state == a) {
881 p = (uint8_t *)s->init_buf->data;
882 *p++ = SSL3_MT_CCS;
883 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
884 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langley71d8a082014-12-13 16:28:18 -0800886 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700887
David Benjamin16d031a2014-12-14 18:52:44 -0500888 dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
889 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Adam Langley71d8a082014-12-13 16:28:18 -0800891 /* buffer the message to handle re-xmits */
892 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langley71d8a082014-12-13 16:28:18 -0800894 s->state = b;
895 }
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langley71d8a082014-12-13 16:28:18 -0800897 /* SSL3_ST_CW_CHANGE_B */
David Benjamine4824e82014-12-14 19:44:34 -0500898 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
Adam Langley71d8a082014-12-13 16:28:18 -0800899}
Adam Langley95c29f32014-06-20 12:00:00 -0700900
Adam Langley71d8a082014-12-13 16:28:18 -0800901int dtls1_read_failed(SSL *s, int code) {
902 if (code > 0) {
903 fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
904 return 1;
905 }
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langley71d8a082014-12-13 16:28:18 -0800907 if (!dtls1_is_timer_expired(s)) {
908 /* not a timeout, none of our business, let higher layers handle this. In
909 * fact, it's probably an error */
910 return code;
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langley71d8a082014-12-13 16:28:18 -0800913 if (!SSL_in_init(s)) {
914 /* done, no need to send a retransmit */
915 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
916 return code;
917 }
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langley71d8a082014-12-13 16:28:18 -0800919 return dtls1_handle_timeout(s);
920}
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langley71d8a082014-12-13 16:28:18 -0800922int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
923 /* The index of the retransmission queue actually is the message sequence
924 * number, since the queue only contains messages of a single handshake.
925 * However, the ChangeCipherSpec has no message sequence number and so using
926 * only the sequence will result in the CCS and Finished having the same
927 * index. To prevent this, the sequence number is multiplied by 2. In case of
928 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
929 * maintains the order of the index (important for priority queues) and fits
930 * in the unsigned short variable. */
931 return seq * 2 - is_ccs;
932}
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langley71d8a082014-12-13 16:28:18 -0800934int dtls1_retransmit_buffered_messages(SSL *s) {
935 pqueue sent = s->d1->sent_messages;
936 piterator iter;
937 pitem *item;
938 hm_fragment *frag;
939 int found = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langley71d8a082014-12-13 16:28:18 -0800941 iter = pqueue_iterator(sent);
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langley71d8a082014-12-13 16:28:18 -0800943 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
944 frag = (hm_fragment *)item->data;
945 if (dtls1_retransmit_message(
946 s, (unsigned short)dtls1_get_queue_priority(
947 frag->msg_header.seq, frag->msg_header.is_ccs),
948 0, &found) <= 0 &&
949 found) {
950 fprintf(stderr, "dtls1_retransmit_message() failed\n");
951 return -1;
952 }
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langley71d8a082014-12-13 16:28:18 -0800955 return 1;
956}
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langley71d8a082014-12-13 16:28:18 -0800958int dtls1_buffer_message(SSL *s, int is_ccs) {
959 pitem *item;
960 hm_fragment *frag;
961 uint8_t seq64be[8];
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langley71d8a082014-12-13 16:28:18 -0800963 /* this function is called immediately after a message has
964 * been serialized */
965 assert(s->init_off == 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langley71d8a082014-12-13 16:28:18 -0800967 frag = dtls1_hm_fragment_new(s->init_num, 0);
968 if (!frag) {
969 return 0;
970 }
Adam Langley95c29f32014-06-20 12:00:00 -0700971
Adam Langley71d8a082014-12-13 16:28:18 -0800972 memcpy(frag->fragment, s->init_buf->data, s->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langley71d8a082014-12-13 16:28:18 -0800974 if (is_ccs) {
975 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
976 (unsigned int)s->init_num);
977 } else {
978 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
979 (unsigned int)s->init_num);
980 }
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langley71d8a082014-12-13 16:28:18 -0800982 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
983 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
984 frag->msg_header.type = s->d1->w_msg_hdr.type;
985 frag->msg_header.frag_off = 0;
986 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
987 frag->msg_header.is_ccs = is_ccs;
David Benjaminafbc63f2015-02-01 02:33:59 -0500988 frag->msg_header.epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langley71d8a082014-12-13 16:28:18 -0800990 memset(seq64be, 0, sizeof(seq64be));
991 seq64be[6] = (uint8_t)(
992 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
993 8);
994 seq64be[7] = (uint8_t)(
995 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langley71d8a082014-12-13 16:28:18 -0800997 item = pitem_new(seq64be, frag);
998 if (item == NULL) {
999 dtls1_hm_fragment_free(frag);
1000 return 0;
1001 }
Adam Langley95c29f32014-06-20 12:00:00 -07001002
Adam Langley71d8a082014-12-13 16:28:18 -08001003 pqueue_insert(s->d1->sent_messages, item);
1004 return 1;
1005}
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langley71d8a082014-12-13 16:28:18 -08001007int dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
1008 int *found) {
1009 int ret;
1010 /* XDTLS: for now assuming that read/writes are blocking */
1011 pitem *item;
1012 hm_fragment *frag;
1013 unsigned long header_length;
1014 uint8_t seq64be[8];
Adam Langley71d8a082014-12-13 16:28:18 -08001015 uint8_t save_write_sequence[8];
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langley71d8a082014-12-13 16:28:18 -08001017 /* assert(s->init_num == 0);
1018 assert(s->init_off == 0); */
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langley71d8a082014-12-13 16:28:18 -08001020 /* XDTLS: the requested message ought to be found, otherwise error */
1021 memset(seq64be, 0, sizeof(seq64be));
1022 seq64be[6] = (uint8_t)(seq >> 8);
1023 seq64be[7] = (uint8_t)seq;
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langley71d8a082014-12-13 16:28:18 -08001025 item = pqueue_find(s->d1->sent_messages, seq64be);
1026 if (item == NULL) {
1027 fprintf(stderr, "retransmit: message %d non-existant\n", seq);
1028 *found = 0;
1029 return 0;
1030 }
Adam Langley95c29f32014-06-20 12:00:00 -07001031
Adam Langley71d8a082014-12-13 16:28:18 -08001032 *found = 1;
1033 frag = (hm_fragment *)item->data;
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langley71d8a082014-12-13 16:28:18 -08001035 if (frag->msg_header.is_ccs) {
1036 header_length = DTLS1_CCS_HEADER_LENGTH;
1037 } else {
1038 header_length = DTLS1_HM_HEADER_LENGTH;
1039 }
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langley71d8a082014-12-13 16:28:18 -08001041 memcpy(s->init_buf->data, frag->fragment,
1042 frag->msg_header.msg_len + header_length);
1043 s->init_num = frag->msg_header.msg_len + header_length;
Adam Langley95c29f32014-06-20 12:00:00 -07001044
David Benjamin16d031a2014-12-14 18:52:44 -05001045 dtls1_set_message_header(s, frag->msg_header.type,
1046 frag->msg_header.msg_len, frag->msg_header.seq,
1047 0, frag->msg_header.frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001048
David Benjaminafbc63f2015-02-01 02:33:59 -05001049 /* Save current state. */
1050 SSL_AEAD_CTX *aead_write_ctx = s->aead_write_ctx;
1051 uint16_t epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -07001052
David Benjaminafbc63f2015-02-01 02:33:59 -05001053 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
1054 * (negotiated cipher) exist. */
1055 assert(epoch == 0 || epoch == 1);
1056 assert(frag->msg_header.epoch <= epoch);
1057 const int fragment_from_previous_epoch = (epoch == 1 &&
1058 frag->msg_header.epoch == 0);
1059 if (fragment_from_previous_epoch) {
1060 /* Rewind to the previous epoch.
1061 *
1062 * TODO(davidben): Instead of swapping out connection-global state, this
1063 * logic should pass a "use previous epoch" parameter down to lower-level
1064 * functions. */
1065 s->d1->w_epoch = frag->msg_header.epoch;
1066 s->aead_write_ctx = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -08001067 memcpy(save_write_sequence, s->s3->write_sequence,
1068 sizeof(s->s3->write_sequence));
1069 memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
1070 sizeof(s->s3->write_sequence));
David Benjaminafbc63f2015-02-01 02:33:59 -05001071 } else {
1072 /* Otherwise the messages must be from the same epoch. */
1073 assert(frag->msg_header.epoch == epoch);
Adam Langley71d8a082014-12-13 16:28:18 -08001074 }
1075
1076 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
David Benjamine4824e82014-12-14 19:44:34 -05001077 : SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -08001078
David Benjaminafbc63f2015-02-01 02:33:59 -05001079 if (fragment_from_previous_epoch) {
1080 /* Restore the current epoch. */
1081 s->aead_write_ctx = aead_write_ctx;
1082 s->d1->w_epoch = epoch;
Adam Langley71d8a082014-12-13 16:28:18 -08001083 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
1084 sizeof(s->s3->write_sequence));
1085 memcpy(s->s3->write_sequence, save_write_sequence,
1086 sizeof(s->s3->write_sequence));
1087 }
1088
Adam Langley71d8a082014-12-13 16:28:18 -08001089 (void)BIO_flush(SSL_get_wbio(s));
1090 return ret;
1091}
Adam Langley95c29f32014-06-20 12:00:00 -07001092
1093/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -08001094void dtls1_clear_record_buffer(SSL *s) {
1095 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -07001096
Adam Langley71d8a082014-12-13 16:28:18 -08001097 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
1098 item = pqueue_pop(s->d1->sent_messages)) {
1099 dtls1_hm_fragment_free((hm_fragment *)item->data);
1100 pitem_free(item);
1101 }
1102}
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langley95c29f32014-06-20 12:00:00 -07001104/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin16d031a2014-12-14 18:52:44 -05001105void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
1106 unsigned short seq_num, unsigned long frag_off,
1107 unsigned long frag_len) {
Adam Langley71d8a082014-12-13 16:28:18 -08001108 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langley71d8a082014-12-13 16:28:18 -08001110 msg_hdr->type = mt;
1111 msg_hdr->msg_len = len;
1112 msg_hdr->seq = seq_num;
1113 msg_hdr->frag_off = frag_off;
1114 msg_hdr->frag_len = frag_len;
1115}
Adam Langley95c29f32014-06-20 12:00:00 -07001116
Adam Langley71d8a082014-12-13 16:28:18 -08001117static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1118 unsigned long frag_len) {
1119 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001120
Adam Langley71d8a082014-12-13 16:28:18 -08001121 msg_hdr->frag_off = frag_off;
1122 msg_hdr->frag_len = frag_len;
1123}
Adam Langley95c29f32014-06-20 12:00:00 -07001124
Adam Langley71d8a082014-12-13 16:28:18 -08001125static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
1126 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langley71d8a082014-12-13 16:28:18 -08001128 *p++ = msg_hdr->type;
1129 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001130
Adam Langley71d8a082014-12-13 16:28:18 -08001131 s2n(msg_hdr->seq, p);
1132 l2n3(msg_hdr->frag_off, p);
1133 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langley71d8a082014-12-13 16:28:18 -08001135 return p;
1136}
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langley71d8a082014-12-13 16:28:18 -08001138unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -05001139 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -08001140}
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langley71d8a082014-12-13 16:28:18 -08001142void dtls1_get_message_header(uint8_t *data,
1143 struct hm_header_st *msg_hdr) {
1144 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
1145 msg_hdr->type = *(data++);
1146 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langley71d8a082014-12-13 16:28:18 -08001148 n2s(data, msg_hdr->seq);
1149 n2l3(data, msg_hdr->frag_off);
1150 n2l3(data, msg_hdr->frag_len);
1151}
Adam Langley95c29f32014-06-20 12:00:00 -07001152
Adam Langley71d8a082014-12-13 16:28:18 -08001153void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) {
1154 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langley71d8a082014-12-13 16:28:18 -08001156 ccs_hdr->type = *(data++);
1157}
Adam Langley95c29f32014-06-20 12:00:00 -07001158
Adam Langley71d8a082014-12-13 16:28:18 -08001159int dtls1_shutdown(SSL *s) {
1160 int ret;
1161 ret = ssl3_shutdown(s);
1162 return ret;
1163}