blob: b75249af54ccb36091f7d304ecc39b832ca01648 [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
166/* XDTLS: figure out the right values */
David Benjamincff64722014-08-19 19:54:46 -0400167static const unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28};
Adam Langley95c29f32014-06-20 12:00:00 -0700168
169static unsigned int dtls1_guess_mtu(unsigned int curr_mtu);
Adam Langley71d8a082014-12-13 16:28:18 -0800170static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
171 unsigned long frag_len);
172static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
Adam Langley71d8a082014-12-13 16:28:18 -0800173static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700174
Adam Langley71d8a082014-12-13 16:28:18 -0800175static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
176 int reassembly) {
177 hm_fragment *frag = NULL;
178 unsigned char *buf = NULL;
179 unsigned char *bitmask = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langley71d8a082014-12-13 16:28:18 -0800181 frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
182 if (frag == NULL) {
183 return NULL;
184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langley71d8a082014-12-13 16:28:18 -0800186 if (frag_len) {
187 buf = (unsigned char *)OPENSSL_malloc(frag_len);
188 if (buf == NULL) {
189 OPENSSL_free(frag);
190 return NULL;
191 }
192 }
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley71d8a082014-12-13 16:28:18 -0800194 /* zero length fragment gets zero frag->fragment */
195 frag->fragment = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langley71d8a082014-12-13 16:28:18 -0800197 /* Initialize reassembly bitmask if necessary */
198 if (reassembly) {
199 bitmask = (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
200 if (bitmask == NULL) {
201 if (buf != NULL) {
202 OPENSSL_free(buf);
203 }
204 OPENSSL_free(frag);
205 return NULL;
206 }
207 memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
Adam Langley71d8a082014-12-13 16:28:18 -0800210 frag->reassembly = bitmask;
Adam Langley95c29f32014-06-20 12:00:00 -0700211
Adam Langley71d8a082014-12-13 16:28:18 -0800212 return frag;
213}
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley71d8a082014-12-13 16:28:18 -0800215void dtls1_hm_fragment_free(hm_fragment *frag) {
216 if (frag->msg_header.is_ccs) {
David Benjamine95d20d2014-12-23 11:16:01 -0500217 /* TODO(davidben): Simplify aead_write_ctx ownership, probably by just
218 * forbidding DTLS renego. */
219 SSL_AEAD_CTX *aead_write_ctx =
220 frag->msg_header.saved_retransmit_state.aead_write_ctx;
221 if (aead_write_ctx) {
222 EVP_AEAD_CTX_cleanup(&aead_write_ctx->ctx);
223 OPENSSL_free(aead_write_ctx);
224 }
Adam Langley71d8a082014-12-13 16:28:18 -0800225 }
226 if (frag->fragment) {
227 OPENSSL_free(frag->fragment);
228 }
229 if (frag->reassembly) {
230 OPENSSL_free(frag->reassembly);
231 }
232 OPENSSL_free(frag);
233}
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langley71d8a082014-12-13 16:28:18 -0800235/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
236 * SSL3_RT_CHANGE_CIPHER_SPEC) */
David Benjamine4824e82014-12-14 19:44:34 -0500237int dtls1_do_write(SSL *s, int type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800238 int ret;
239 int curr_mtu;
David Benjamine95d20d2014-12-23 11:16:01 -0500240 unsigned int len, frag_off;
241 size_t max_overhead = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langley71d8a082014-12-13 16:28:18 -0800243 /* AHA! Figure out the MTU, and stick to the right size */
244 if (s->d1->mtu < dtls1_min_mtu() &&
245 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
246 s->d1->mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langley71d8a082014-12-13 16:28:18 -0800248 /* I've seen the kernel return bogus numbers when it doesn't know
249 * (initial write), so just make sure we have a reasonable number */
250 if (s->d1->mtu < dtls1_min_mtu()) {
251 s->d1->mtu = 0;
252 s->d1->mtu = dtls1_guess_mtu(s->d1->mtu);
253 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL);
254 }
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langley71d8a082014-12-13 16:28:18 -0800257 /* should have something reasonable now */
258 assert(s->d1->mtu >= dtls1_min_mtu());
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley71d8a082014-12-13 16:28:18 -0800260 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
261 assert(s->init_num ==
262 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
David Benjamine95d20d2014-12-23 11:16:01 -0500265 /* Determine the maximum overhead of the current cipher. */
266 if (s->aead_write_ctx != NULL) {
267 max_overhead = EVP_AEAD_max_overhead(s->aead_write_ctx->ctx.aead);
268 if (s->aead_write_ctx->variable_nonce_included_in_record) {
269 max_overhead += s->aead_write_ctx->variable_nonce_len;
270 }
Adam Langley71d8a082014-12-13 16:28:18 -0800271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langley71d8a082014-12-13 16:28:18 -0800273 frag_off = 0;
274 while (s->init_num) {
275 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
David Benjamine95d20d2014-12-23 11:16:01 -0500276 DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langley71d8a082014-12-13 16:28:18 -0800278 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
279 /* grr.. we could get an error if MTU picked was wrong */
280 ret = BIO_flush(SSL_get_wbio(s));
281 if (ret <= 0) {
282 return ret;
283 }
David Benjamine95d20d2014-12-23 11:16:01 -0500284 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley71d8a082014-12-13 16:28:18 -0800285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286
Adam Langley71d8a082014-12-13 16:28:18 -0800287 /* XDTLS: this function is too long. split out the CCS part */
288 if (type == SSL3_RT_HANDSHAKE) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500289 /* If this isn't the first fragment, reserve space to prepend a new
290 * fragment header. This will override the body of a previous fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800291 if (s->init_off != 0) {
292 assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
293 s->init_off -= DTLS1_HM_HEADER_LENGTH;
294 s->init_num += DTLS1_HM_HEADER_LENGTH;
Adam Langley71d8a082014-12-13 16:28:18 -0800295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
David Benjamind9778fb2015-01-11 17:24:51 -0500297 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
298 /* To make forward progress, the MTU must, at minimum, fit the handshake
299 * header and one byte of handshake body. */
300 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
301 return -1;
302 }
Adam Langley95c29f32014-06-20 12:00:00 -0700303
David Benjamind9778fb2015-01-11 17:24:51 -0500304 if (s->init_num > curr_mtu) {
305 len = curr_mtu;
306 } else {
307 len = s->init_num;
308 }
309 assert(len >= DTLS1_HM_HEADER_LENGTH);
310
311 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800312 dtls1_write_message_header(
313 s, (uint8_t *)&s->init_buf->data[s->init_off]);
David Benjamind9778fb2015-01-11 17:24:51 -0500314 } else {
315 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
316 /* ChangeCipherSpec cannot be fragmented. */
317 if (s->init_num > curr_mtu) {
318 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
319 return -1;
320 }
321 len = s->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langley71d8a082014-12-13 16:28:18 -0800324 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len);
325 if (ret < 0) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500326 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800327 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500328
329 /* bad if this assert fails, only part of the handshake message got sent.
330 * But why would this happen? */
331 assert(len == (unsigned int)ret);
332
333 if (ret == s->init_num) {
334 if (s->msg_callback) {
335 s->msg_callback(1, s->version, type, s->init_buf->data,
336 (size_t)(s->init_off + s->init_num), s,
337 s->msg_callback_arg);
338 }
339
340 s->init_off = 0; /* done writing this message */
341 s->init_num = 0;
342
343 return 1;
344 }
345 s->init_off += ret;
346 s->init_num -= ret;
347 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800348 }
349
350 return 0;
351}
Adam Langley95c29f32014-06-20 12:00:00 -0700352
353
Adam Langley71d8a082014-12-13 16:28:18 -0800354/* Obtain handshake message of message type 'mt' (any if mt == -1), maximum
355 * acceptable body length 'max'. Read an entire handshake message. Handshake
356 * messages arrive in fragments. */
357long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max,
358 int hash_message, int *ok) {
359 int i, al;
360 struct hm_header_st *msg_hdr;
361 uint8_t *p;
362 unsigned long msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley71d8a082014-12-13 16:28:18 -0800364 /* s3->tmp is used to store messages that are unexpected, caused
365 * by the absence of an optional handshake message */
366 if (s->s3->tmp.reuse_message) {
367 /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined
368 * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE
369 * would have to have been applied to the previous call. */
370 assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE);
371 s->s3->tmp.reuse_message = 0;
372 if (mt >= 0 && s->s3->tmp.message_type != mt) {
373 al = SSL_AD_UNEXPECTED_MESSAGE;
374 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
375 goto f_err;
376 }
377 *ok = 1;
378 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
379 s->init_num = (int)s->s3->tmp.message_size;
380 return s->init_num;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langley71d8a082014-12-13 16:28:18 -0800383 msg_hdr = &s->d1->r_msg_hdr;
384 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700385
386again:
Adam Langley71d8a082014-12-13 16:28:18 -0800387 i = dtls1_get_message_fragment(s, stn, max, ok);
388 if (i == DTLS1_HM_BAD_FRAGMENT ||
389 i == DTLS1_HM_FRAGMENT_RETRY) {
390 /* bad fragment received */
391 goto again;
392 } else if (i <= 0 && !*ok) {
393 return i;
394 }
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langley71d8a082014-12-13 16:28:18 -0800396 p = (uint8_t *)s->init_buf->data;
397 msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langley71d8a082014-12-13 16:28:18 -0800399 /* reconstruct message header */
400 *(p++) = msg_hdr->type;
401 l2n3(msg_len, p);
402 s2n(msg_hdr->seq, p);
403 l2n3(0, p);
404 l2n3(msg_len, p);
405 p -= DTLS1_HM_HEADER_LENGTH;
406 msg_len += DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langley71d8a082014-12-13 16:28:18 -0800408 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
David Benjamin590cbe92014-08-25 21:34:56 -0400409
Adam Langley71d8a082014-12-13 16:28:18 -0800410 if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE) {
411 ssl3_hash_current_message(s);
412 }
413 if (s->msg_callback) {
414 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, s,
415 s->msg_callback_arg);
416 }
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langley71d8a082014-12-13 16:28:18 -0800418 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langley71d8a082014-12-13 16:28:18 -0800420 s->d1->handshake_read_seq++;
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langley71d8a082014-12-13 16:28:18 -0800422 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
424f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800425 ssl3_send_alert(s, SSL3_AL_FATAL, al);
426 *ok = 0;
427 return -1;
428}
429
430static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr,
431 int max) {
432 size_t frag_off, frag_len, msg_len;
433
434 msg_len = msg_hdr->msg_len;
435 frag_off = msg_hdr->frag_off;
436 frag_len = msg_hdr->frag_len;
437
438 /* sanity checking */
439 if ((frag_off + frag_len) > msg_len) {
440 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
441 SSL_R_EXCESSIVE_MESSAGE_SIZE);
442 return SSL_AD_ILLEGAL_PARAMETER;
443 }
444
445 if ((frag_off + frag_len) > (unsigned long)max) {
446 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
447 SSL_R_EXCESSIVE_MESSAGE_SIZE);
448 return SSL_AD_ILLEGAL_PARAMETER;
449 }
450
451 if (s->d1->r_msg_hdr.frag_off == 0) {
452 /* first fragment */
453 /* msg_len is limited to 2^24, but is effectively checked
454 * against max above */
455 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
456 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
457 return SSL_AD_INTERNAL_ERROR;
458 }
459
460 s->s3->tmp.message_size = msg_len;
461 s->d1->r_msg_hdr.msg_len = msg_len;
462 s->s3->tmp.message_type = msg_hdr->type;
463 s->d1->r_msg_hdr.type = msg_hdr->type;
464 s->d1->r_msg_hdr.seq = msg_hdr->seq;
465 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
466 /* They must be playing with us! BTW, failure to enforce
467 * upper limit would open possibility for buffer overrun. */
468 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
469 SSL_R_EXCESSIVE_MESSAGE_SIZE);
470 return SSL_AD_ILLEGAL_PARAMETER;
471 }
472
473 return 0; /* no error */
474}
Adam Langley95c29f32014-06-20 12:00:00 -0700475
476
Adam Langley71d8a082014-12-13 16:28:18 -0800477static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) {
478 /* (0) check whether the desired fragment is available
479 * if so:
480 * (1) copy over the fragment to s->init_buf->data[]
481 * (2) update s->init_num */
482 pitem *item;
483 hm_fragment *frag;
484 int al;
485 unsigned long frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700486
Adam Langley71d8a082014-12-13 16:28:18 -0800487 *ok = 0;
488 item = pqueue_peek(s->d1->buffered_messages);
489 if (item == NULL) {
490 return 0;
491 }
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Adam Langley71d8a082014-12-13 16:28:18 -0800493 frag = (hm_fragment *)item->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langley71d8a082014-12-13 16:28:18 -0800495 /* Don't return if reassembly still in progress */
496 if (frag->reassembly != NULL) {
497 return 0;
498 }
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langley71d8a082014-12-13 16:28:18 -0800500 if (s->d1->handshake_read_seq != frag->msg_header.seq) {
501 return 0;
502 }
Adam Langley95c29f32014-06-20 12:00:00 -0700503
Adam Langley71d8a082014-12-13 16:28:18 -0800504 frag_len = frag->msg_header.frag_len;
505 pqueue_pop(s->d1->buffered_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langley71d8a082014-12-13 16:28:18 -0800507 al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langley71d8a082014-12-13 16:28:18 -0800509 if (al == 0) {
510 /* no alert */
511 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
512 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
513 frag->msg_header.frag_len);
514 }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langley71d8a082014-12-13 16:28:18 -0800516 dtls1_hm_fragment_free(frag);
517 pitem_free(item);
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langley71d8a082014-12-13 16:28:18 -0800519 if (al == 0) {
520 *ok = 1;
521 return frag_len;
522 }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley71d8a082014-12-13 16:28:18 -0800524 ssl3_send_alert(s, SSL3_AL_FATAL, al);
525 s->init_num = 0;
526 *ok = 0;
527 return -1;
528}
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Matt Caswell2306fe52014-06-06 14:25:52 -0700530/* dtls1_max_handshake_message_len returns the maximum number of bytes
531 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
532 * be greater if the maximum certificate list size requires it. */
Adam Langley71d8a082014-12-13 16:28:18 -0800533static unsigned long dtls1_max_handshake_message_len(const SSL *s) {
534 unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
535 if (max_len < (unsigned long)s->max_cert_list) {
536 return s->max_cert_list;
537 }
538 return max_len;
539}
Adam Langley95c29f32014-06-20 12:00:00 -0700540
Adam Langley71d8a082014-12-13 16:28:18 -0800541static int dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr,
542 int *ok) {
543 hm_fragment *frag = NULL;
544 pitem *item = NULL;
545 int i = -1, is_complete;
546 uint8_t seq64be[8];
547 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langley71d8a082014-12-13 16:28:18 -0800549 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
550 msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) {
551 goto err;
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langley71d8a082014-12-13 16:28:18 -0800554 if (frag_len == 0) {
555 return DTLS1_HM_FRAGMENT_RETRY;
556 }
Adam Langleye951ff42014-06-06 14:30:33 -0700557
Adam Langley71d8a082014-12-13 16:28:18 -0800558 /* Try to find item in queue */
559 memset(seq64be, 0, sizeof(seq64be));
560 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
561 seq64be[7] = (uint8_t)msg_hdr->seq;
562 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700563
Adam Langley71d8a082014-12-13 16:28:18 -0800564 if (item == NULL) {
565 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
566 if (frag == NULL) {
567 goto err;
568 }
569 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
570 frag->msg_header.frag_len = frag->msg_header.msg_len;
571 frag->msg_header.frag_off = 0;
572 } else {
573 frag = (hm_fragment *)item->data;
574 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
575 item = NULL;
576 frag = NULL;
577 goto err;
578 }
579 }
Adam Langleybed22142014-06-20 12:00:00 -0700580
Adam Langley71d8a082014-12-13 16:28:18 -0800581 /* If message is already reassembled, this must be a
582 * retransmit and can be dropped. In this case item != NULL and so frag
583 * does not need to be freed. */
584 if (frag->reassembly == NULL) {
585 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langley71d8a082014-12-13 16:28:18 -0800587 assert(item != NULL);
588 while (frag_len) {
589 i = s->method->ssl_read_bytes(
590 s, SSL3_RT_HANDSHAKE, devnull,
591 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
592 if (i <= 0) {
593 goto err;
594 }
595 frag_len -= i;
596 }
597 return DTLS1_HM_FRAGMENT_RETRY;
598 }
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langley71d8a082014-12-13 16:28:18 -0800600 /* read the body of the fragment (header has already been read */
601 i = s->method->ssl_read_bytes(
602 s, SSL3_RT_HANDSHAKE, frag->fragment + msg_hdr->frag_off, frag_len, 0);
603 if ((unsigned long)i != frag_len) {
604 i = -1;
605 }
606 if (i <= 0) {
607 goto err;
608 }
Adam Langley95c29f32014-06-20 12:00:00 -0700609
Adam Langley71d8a082014-12-13 16:28:18 -0800610 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
611 (long)(msg_hdr->frag_off + frag_len));
Adam Langley95c29f32014-06-20 12:00:00 -0700612
Adam Langley71d8a082014-12-13 16:28:18 -0800613 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
614 is_complete);
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langley71d8a082014-12-13 16:28:18 -0800616 if (is_complete) {
617 OPENSSL_free(frag->reassembly);
618 frag->reassembly = NULL;
619 }
Adam Langley95c29f32014-06-20 12:00:00 -0700620
Adam Langley71d8a082014-12-13 16:28:18 -0800621 if (item == NULL) {
622 item = pitem_new(seq64be, frag);
623 if (item == NULL) {
624 i = -1;
625 goto err;
626 }
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langley71d8a082014-12-13 16:28:18 -0800628 item = pqueue_insert(s->d1->buffered_messages, item);
629 /* pqueue_insert fails iff a duplicate item is inserted.
630 * However, |item| cannot be a duplicate. If it were,
631 * |pqueue_find|, above, would have returned it and control
632 * would never have reached this branch. */
633 assert(item != NULL);
634 }
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langley71d8a082014-12-13 16:28:18 -0800636 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700637
638err:
Adam Langley71d8a082014-12-13 16:28:18 -0800639 if (frag != NULL && item == NULL) {
640 dtls1_hm_fragment_free(frag);
641 }
642 *ok = 0;
643 return i;
644}
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langley71d8a082014-12-13 16:28:18 -0800646static int dtls1_process_out_of_seq_message(SSL *s,
647 const struct hm_header_st *msg_hdr,
648 int *ok) {
649 int i = -1;
650 hm_fragment *frag = NULL;
651 pitem *item = NULL;
652 uint8_t seq64be[8];
653 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langley71d8a082014-12-13 16:28:18 -0800655 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) {
656 goto err;
657 }
Adam Langley95c29f32014-06-20 12:00:00 -0700658
Adam Langley71d8a082014-12-13 16:28:18 -0800659 /* Try to find item in queue, to prevent duplicate entries */
660 memset(seq64be, 0, sizeof(seq64be));
661 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
662 seq64be[7] = (uint8_t)msg_hdr->seq;
663 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langley71d8a082014-12-13 16:28:18 -0800665 /* If we already have an entry and this one is a fragment,
666 * don't discard it and rather try to reassemble it. */
667 if (item != NULL && frag_len != msg_hdr->msg_len) {
668 item = NULL;
669 }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langley71d8a082014-12-13 16:28:18 -0800671 /* Discard the message if sequence number was already there, is
672 * too far in the future, already in the queue or if we received
673 * a FINISHED before the SERVER_HELLO, which then must be a stale
674 * retransmit. */
675 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
676 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
677 (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
678 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Adam Langley71d8a082014-12-13 16:28:18 -0800680 while (frag_len) {
681 i = s->method->ssl_read_bytes(
682 s, SSL3_RT_HANDSHAKE, devnull,
683 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
684 if (i <= 0) {
685 goto err;
686 }
687 frag_len -= i;
688 }
689 } else {
690 if (frag_len != msg_hdr->msg_len) {
691 return dtls1_reassemble_fragment(s, msg_hdr, ok);
692 }
Adam Langley95c29f32014-06-20 12:00:00 -0700693
Adam Langley71d8a082014-12-13 16:28:18 -0800694 if (frag_len > dtls1_max_handshake_message_len(s)) {
695 goto err;
696 }
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langley71d8a082014-12-13 16:28:18 -0800698 frag = dtls1_hm_fragment_new(frag_len, 0);
699 if (frag == NULL) {
700 goto err;
701 }
Matt Caswell2306fe52014-06-06 14:25:52 -0700702
Adam Langley71d8a082014-12-13 16:28:18 -0800703 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
Adam Langley95c29f32014-06-20 12:00:00 -0700704
Adam Langley71d8a082014-12-13 16:28:18 -0800705 if (frag_len) {
706 /* read the body of the fragment (header has already been read */
707 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment,
708 frag_len, 0);
709 if ((unsigned long)i != frag_len) {
710 i = -1;
711 }
712 if (i <= 0) {
713 goto err;
714 }
715 }
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langley71d8a082014-12-13 16:28:18 -0800717 item = pitem_new(seq64be, frag);
718 if (item == NULL) {
719 goto err;
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langley71d8a082014-12-13 16:28:18 -0800722 item = pqueue_insert(s->d1->buffered_messages, item);
723 /* pqueue_insert fails iff a duplicate item is inserted.
724 * However, |item| cannot be a duplicate. If it were,
725 * |pqueue_find|, above, would have returned it. Then, either
726 * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
727 * to NULL and it will have been processed with
728 * |dtls1_reassemble_fragment|, above, or the record will have
729 * been discarded. */
730 assert(item != NULL);
731 }
Adam Langley95c29f32014-06-20 12:00:00 -0700732
Adam Langley71d8a082014-12-13 16:28:18 -0800733 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700734
735err:
Adam Langley71d8a082014-12-13 16:28:18 -0800736 if (frag != NULL && item == NULL) {
737 dtls1_hm_fragment_free(frag);
738 }
739 *ok = 0;
740 return i;
741}
Adam Langley95c29f32014-06-20 12:00:00 -0700742
743
Adam Langley71d8a082014-12-13 16:28:18 -0800744static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok) {
745 uint8_t wire[DTLS1_HM_HEADER_LENGTH];
746 unsigned long len, frag_off, frag_len;
747 int i, al;
748 struct hm_header_st msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langley71d8a082014-12-13 16:28:18 -0800750redo:
751 /* see if we have the required fragment already */
752 if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
753 if (*ok) {
754 s->init_num = frag_len;
755 }
756 return frag_len;
757 }
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langley71d8a082014-12-13 16:28:18 -0800759 /* read handshake message header */
760 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
761 DTLS1_HM_HEADER_LENGTH, 0);
762 if (i <= 0) {
763 /* nbio, or an error */
764 s->rwstate = SSL_READING;
765 *ok = 0;
766 return i;
767 }
Adam Langley95c29f32014-06-20 12:00:00 -0700768
Adam Langley71d8a082014-12-13 16:28:18 -0800769 /* Handshake fails if message header is incomplete */
770 if (i != DTLS1_HM_HEADER_LENGTH) {
771 al = SSL_AD_UNEXPECTED_MESSAGE;
772 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
773 SSL_R_UNEXPECTED_MESSAGE);
774 goto f_err;
775 }
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langley71d8a082014-12-13 16:28:18 -0800777 /* parse the message fragment header */
778 dtls1_get_message_header(wire, &msg_hdr);
Adam Langley95c29f32014-06-20 12:00:00 -0700779
Adam Langley71d8a082014-12-13 16:28:18 -0800780 /* if this is a future (or stale) message it gets buffered
781 * (or dropped)--no further processing at this time. */
782 if (msg_hdr.seq != s->d1->handshake_read_seq) {
783 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
784 }
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langley71d8a082014-12-13 16:28:18 -0800786 len = msg_hdr.msg_len;
787 frag_off = msg_hdr.frag_off;
788 frag_len = msg_hdr.frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langley71d8a082014-12-13 16:28:18 -0800790 if (frag_len && frag_len < len) {
791 return dtls1_reassemble_fragment(s, &msg_hdr, ok);
792 }
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langley71d8a082014-12-13 16:28:18 -0800794 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
795 wire[0] == SSL3_MT_HELLO_REQUEST) {
796 /* The server may always send 'Hello Request' messages --
797 * we are doing a handshake anyway now, so ignore them
798 * if their format is correct. Does not count for
799 * 'Finished' MAC. */
800 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
801 if (s->msg_callback) {
802 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, wire,
803 DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg);
804 }
Adam Langley95c29f32014-06-20 12:00:00 -0700805
Adam Langley71d8a082014-12-13 16:28:18 -0800806 s->init_num = 0;
807 goto redo;
808 } else {
809 /* Incorrectly formated Hello request */
810 al = SSL_AD_UNEXPECTED_MESSAGE;
811 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
812 SSL_R_UNEXPECTED_MESSAGE);
813 goto f_err;
814 }
815 }
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langley71d8a082014-12-13 16:28:18 -0800817 if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) {
818 goto f_err;
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langley71d8a082014-12-13 16:28:18 -0800821 /* XDTLS: ressurect this when restart is in place */
822 s->state = stn;
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langley71d8a082014-12-13 16:28:18 -0800824 if (frag_len > 0) {
825 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700826
Adam Langley71d8a082014-12-13 16:28:18 -0800827 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[frag_off], frag_len,
828 0);
829 /* XDTLS: fix this--message fragments cannot span multiple packets */
830 if (i <= 0) {
831 s->rwstate = SSL_READING;
832 *ok = 0;
833 return i;
834 }
835 } else {
836 i = 0;
837 }
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langley71d8a082014-12-13 16:28:18 -0800839 /* XDTLS: an incorrectly formatted fragment should cause the
840 * handshake to fail */
841 if (i != (int)frag_len) {
842 al = SSL3_AD_ILLEGAL_PARAMETER;
843 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
844 SSL3_AD_ILLEGAL_PARAMETER);
845 goto f_err;
846 }
847
848 *ok = 1;
849
850 /* Note that s->init_num is *not* used as current offset in
851 * s->init_buf->data, but as a counter summing up fragments'
852 * lengths: as soon as they sum up to handshake packet
853 * length, we assume we have got all the fragments. */
854 s->init_num = frag_len;
855 return frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700856
857f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800858 ssl3_send_alert(s, SSL3_AL_FATAL, al);
859 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langley71d8a082014-12-13 16:28:18 -0800861 *ok = 0;
862 return -1;
863}
Adam Langley95c29f32014-06-20 12:00:00 -0700864
865/* for these 2 messages, we need to
866 * ssl->enc_read_ctx re-init
867 * ssl->s3->read_sequence zero
868 * ssl->s3->read_mac_secret re-init
869 * ssl->session->read_sym_enc assign
870 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800871 * ssl->session->read_hash assign */
872int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
873 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700874
Adam Langley71d8a082014-12-13 16:28:18 -0800875 if (s->state == a) {
876 p = (uint8_t *)s->init_buf->data;
877 *p++ = SSL3_MT_CCS;
878 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
879 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700880
Adam Langley71d8a082014-12-13 16:28:18 -0800881 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700882
David Benjamin16d031a2014-12-14 18:52:44 -0500883 dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
884 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700885
Adam Langley71d8a082014-12-13 16:28:18 -0800886 /* buffer the message to handle re-xmits */
887 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700888
Adam Langley71d8a082014-12-13 16:28:18 -0800889 s->state = b;
890 }
Adam Langley95c29f32014-06-20 12:00:00 -0700891
Adam Langley71d8a082014-12-13 16:28:18 -0800892 /* SSL3_ST_CW_CHANGE_B */
David Benjamine4824e82014-12-14 19:44:34 -0500893 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
Adam Langley71d8a082014-12-13 16:28:18 -0800894}
Adam Langley95c29f32014-06-20 12:00:00 -0700895
Adam Langley71d8a082014-12-13 16:28:18 -0800896int dtls1_read_failed(SSL *s, int code) {
897 if (code > 0) {
898 fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
899 return 1;
900 }
Adam Langley95c29f32014-06-20 12:00:00 -0700901
Adam Langley71d8a082014-12-13 16:28:18 -0800902 if (!dtls1_is_timer_expired(s)) {
903 /* not a timeout, none of our business, let higher layers handle this. In
904 * fact, it's probably an error */
905 return code;
906 }
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langley71d8a082014-12-13 16:28:18 -0800908 if (!SSL_in_init(s)) {
909 /* done, no need to send a retransmit */
910 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
911 return code;
912 }
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Adam Langley71d8a082014-12-13 16:28:18 -0800914 return dtls1_handle_timeout(s);
915}
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langley71d8a082014-12-13 16:28:18 -0800917int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
918 /* The index of the retransmission queue actually is the message sequence
919 * number, since the queue only contains messages of a single handshake.
920 * However, the ChangeCipherSpec has no message sequence number and so using
921 * only the sequence will result in the CCS and Finished having the same
922 * index. To prevent this, the sequence number is multiplied by 2. In case of
923 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
924 * maintains the order of the index (important for priority queues) and fits
925 * in the unsigned short variable. */
926 return seq * 2 - is_ccs;
927}
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langley71d8a082014-12-13 16:28:18 -0800929int dtls1_retransmit_buffered_messages(SSL *s) {
930 pqueue sent = s->d1->sent_messages;
931 piterator iter;
932 pitem *item;
933 hm_fragment *frag;
934 int found = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700935
Adam Langley71d8a082014-12-13 16:28:18 -0800936 iter = pqueue_iterator(sent);
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langley71d8a082014-12-13 16:28:18 -0800938 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
939 frag = (hm_fragment *)item->data;
940 if (dtls1_retransmit_message(
941 s, (unsigned short)dtls1_get_queue_priority(
942 frag->msg_header.seq, frag->msg_header.is_ccs),
943 0, &found) <= 0 &&
944 found) {
945 fprintf(stderr, "dtls1_retransmit_message() failed\n");
946 return -1;
947 }
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langley71d8a082014-12-13 16:28:18 -0800950 return 1;
951}
Adam Langley95c29f32014-06-20 12:00:00 -0700952
Adam Langley71d8a082014-12-13 16:28:18 -0800953int dtls1_buffer_message(SSL *s, int is_ccs) {
954 pitem *item;
955 hm_fragment *frag;
956 uint8_t seq64be[8];
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langley71d8a082014-12-13 16:28:18 -0800958 /* this function is called immediately after a message has
959 * been serialized */
960 assert(s->init_off == 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700961
Adam Langley71d8a082014-12-13 16:28:18 -0800962 frag = dtls1_hm_fragment_new(s->init_num, 0);
963 if (!frag) {
964 return 0;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langley71d8a082014-12-13 16:28:18 -0800967 memcpy(frag->fragment, s->init_buf->data, s->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langley71d8a082014-12-13 16:28:18 -0800969 if (is_ccs) {
970 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
971 (unsigned int)s->init_num);
972 } else {
973 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
974 (unsigned int)s->init_num);
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langley71d8a082014-12-13 16:28:18 -0800977 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
978 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
979 frag->msg_header.type = s->d1->w_msg_hdr.type;
980 frag->msg_header.frag_off = 0;
981 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
982 frag->msg_header.is_ccs = is_ccs;
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langley71d8a082014-12-13 16:28:18 -0800984 /* save current state*/
David Benjamine95d20d2014-12-23 11:16:01 -0500985 frag->msg_header.saved_retransmit_state.aead_write_ctx = s->aead_write_ctx;
Adam Langley71d8a082014-12-13 16:28:18 -0800986 frag->msg_header.saved_retransmit_state.session = s->session;
987 frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Adam Langley71d8a082014-12-13 16:28:18 -0800989 memset(seq64be, 0, sizeof(seq64be));
990 seq64be[6] = (uint8_t)(
991 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
992 8);
993 seq64be[7] = (uint8_t)(
994 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
Adam Langley95c29f32014-06-20 12:00:00 -0700995
Adam Langley71d8a082014-12-13 16:28:18 -0800996 item = pitem_new(seq64be, frag);
997 if (item == NULL) {
998 dtls1_hm_fragment_free(frag);
999 return 0;
1000 }
Adam Langley95c29f32014-06-20 12:00:00 -07001001
Adam Langley71d8a082014-12-13 16:28:18 -08001002 pqueue_insert(s->d1->sent_messages, item);
1003 return 1;
1004}
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langley71d8a082014-12-13 16:28:18 -08001006int dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
1007 int *found) {
1008 int ret;
1009 /* XDTLS: for now assuming that read/writes are blocking */
1010 pitem *item;
1011 hm_fragment *frag;
1012 unsigned long header_length;
1013 uint8_t seq64be[8];
1014 struct dtls1_retransmit_state saved_state;
1015 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
Adam Langley71d8a082014-12-13 16:28:18 -08001049 /* save current state */
David Benjamine95d20d2014-12-23 11:16:01 -05001050 saved_state.aead_write_ctx = s->aead_write_ctx;
Adam Langley71d8a082014-12-13 16:28:18 -08001051 saved_state.session = s->session;
1052 saved_state.epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langley71d8a082014-12-13 16:28:18 -08001054 /* restore state in which the message was originally sent */
David Benjamine95d20d2014-12-23 11:16:01 -05001055 s->aead_write_ctx = frag->msg_header.saved_retransmit_state.aead_write_ctx;
Adam Langley71d8a082014-12-13 16:28:18 -08001056 s->session = frag->msg_header.saved_retransmit_state.session;
1057 s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
1058
1059 if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1) {
1060 memcpy(save_write_sequence, s->s3->write_sequence,
1061 sizeof(s->s3->write_sequence));
1062 memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
1063 sizeof(s->s3->write_sequence));
1064 }
1065
1066 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
David Benjamine4824e82014-12-14 19:44:34 -05001067 : SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -08001068
1069 /* restore current state */
David Benjamine95d20d2014-12-23 11:16:01 -05001070 s->aead_write_ctx = saved_state.aead_write_ctx;
Adam Langley71d8a082014-12-13 16:28:18 -08001071 s->session = saved_state.session;
1072 s->d1->w_epoch = saved_state.epoch;
1073
1074 if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1) {
1075 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
1076 sizeof(s->s3->write_sequence));
1077 memcpy(s->s3->write_sequence, save_write_sequence,
1078 sizeof(s->s3->write_sequence));
1079 }
1080
Adam Langley71d8a082014-12-13 16:28:18 -08001081 (void)BIO_flush(SSL_get_wbio(s));
1082 return ret;
1083}
Adam Langley95c29f32014-06-20 12:00:00 -07001084
1085/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -08001086void dtls1_clear_record_buffer(SSL *s) {
1087 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -07001088
Adam Langley71d8a082014-12-13 16:28:18 -08001089 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
1090 item = pqueue_pop(s->d1->sent_messages)) {
1091 dtls1_hm_fragment_free((hm_fragment *)item->data);
1092 pitem_free(item);
1093 }
1094}
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langley95c29f32014-06-20 12:00:00 -07001096/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin16d031a2014-12-14 18:52:44 -05001097void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
1098 unsigned short seq_num, unsigned long frag_off,
1099 unsigned long frag_len) {
Adam Langley71d8a082014-12-13 16:28:18 -08001100 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001101
Adam Langley71d8a082014-12-13 16:28:18 -08001102 msg_hdr->type = mt;
1103 msg_hdr->msg_len = len;
1104 msg_hdr->seq = seq_num;
1105 msg_hdr->frag_off = frag_off;
1106 msg_hdr->frag_len = frag_len;
1107}
Adam Langley95c29f32014-06-20 12:00:00 -07001108
Adam Langley71d8a082014-12-13 16:28:18 -08001109static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1110 unsigned long frag_len) {
1111 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001112
Adam Langley71d8a082014-12-13 16:28:18 -08001113 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 uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
1118 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langley71d8a082014-12-13 16:28:18 -08001120 *p++ = msg_hdr->type;
1121 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001122
Adam Langley71d8a082014-12-13 16:28:18 -08001123 s2n(msg_hdr->seq, p);
1124 l2n3(msg_hdr->frag_off, p);
1125 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001126
Adam Langley71d8a082014-12-13 16:28:18 -08001127 return p;
1128}
Adam Langley95c29f32014-06-20 12:00:00 -07001129
Adam Langley71d8a082014-12-13 16:28:18 -08001130unsigned int dtls1_min_mtu(void) {
1131 return g_probable_mtu[(sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0])) -
1132 1];
1133}
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langley71d8a082014-12-13 16:28:18 -08001135static unsigned int dtls1_guess_mtu(unsigned int curr_mtu) {
1136 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langley71d8a082014-12-13 16:28:18 -08001138 if (curr_mtu == 0) {
1139 return g_probable_mtu[0];
1140 }
Adam Langley95c29f32014-06-20 12:00:00 -07001141
Adam Langley71d8a082014-12-13 16:28:18 -08001142 for (i = 0; i < sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0]); i++) {
1143 if (curr_mtu > g_probable_mtu[i]) {
1144 return g_probable_mtu[i];
1145 }
1146 }
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Adam Langley71d8a082014-12-13 16:28:18 -08001148 return curr_mtu;
1149}
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langley71d8a082014-12-13 16:28:18 -08001151void dtls1_get_message_header(uint8_t *data,
1152 struct hm_header_st *msg_hdr) {
1153 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
1154 msg_hdr->type = *(data++);
1155 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001156
Adam Langley71d8a082014-12-13 16:28:18 -08001157 n2s(data, msg_hdr->seq);
1158 n2l3(data, msg_hdr->frag_off);
1159 n2l3(data, msg_hdr->frag_len);
1160}
Adam Langley95c29f32014-06-20 12:00:00 -07001161
Adam Langley71d8a082014-12-13 16:28:18 -08001162void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) {
1163 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langley71d8a082014-12-13 16:28:18 -08001165 ccs_hdr->type = *(data++);
1166}
Adam Langley95c29f32014-06-20 12:00:00 -07001167
Adam Langley71d8a082014-12-13 16:28:18 -08001168int dtls1_shutdown(SSL *s) {
1169 int ret;
1170 ret = ssl3_shutdown(s);
1171 return ret;
1172}