blob: dec9bc4b34fb45d7efb9dccfed6b6170e37ec5a8 [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 Langley95c29f32014-06-20 12:00:00 -0700173static void dtls1_set_message_header_int(SSL *s, unsigned char mt,
Adam Langley71d8a082014-12-13 16:28:18 -0800174 unsigned long len,
175 unsigned short seq_num,
176 unsigned long frag_off,
177 unsigned long frag_len);
178static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langley71d8a082014-12-13 16:28:18 -0800180static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
181 int reassembly) {
182 hm_fragment *frag = NULL;
183 unsigned char *buf = NULL;
184 unsigned char *bitmask = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langley71d8a082014-12-13 16:28:18 -0800186 frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
187 if (frag == NULL) {
188 return NULL;
189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley71d8a082014-12-13 16:28:18 -0800191 if (frag_len) {
192 buf = (unsigned char *)OPENSSL_malloc(frag_len);
193 if (buf == NULL) {
194 OPENSSL_free(frag);
195 return NULL;
196 }
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langley71d8a082014-12-13 16:28:18 -0800199 /* zero length fragment gets zero frag->fragment */
200 frag->fragment = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley71d8a082014-12-13 16:28:18 -0800202 /* Initialize reassembly bitmask if necessary */
203 if (reassembly) {
204 bitmask = (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len));
205 if (bitmask == NULL) {
206 if (buf != NULL) {
207 OPENSSL_free(buf);
208 }
209 OPENSSL_free(frag);
210 return NULL;
211 }
212 memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len));
213 }
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley71d8a082014-12-13 16:28:18 -0800215 frag->reassembly = bitmask;
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langley71d8a082014-12-13 16:28:18 -0800217 return frag;
218}
Adam Langley95c29f32014-06-20 12:00:00 -0700219
Adam Langley71d8a082014-12-13 16:28:18 -0800220void dtls1_hm_fragment_free(hm_fragment *frag) {
221 if (frag->msg_header.is_ccs) {
222 EVP_CIPHER_CTX_free(frag->msg_header.saved_retransmit_state.enc_write_ctx);
223 EVP_MD_CTX_destroy(frag->msg_header.saved_retransmit_state.write_hash);
224 }
225 if (frag->fragment) {
226 OPENSSL_free(frag->fragment);
227 }
228 if (frag->reassembly) {
229 OPENSSL_free(frag->reassembly);
230 }
231 OPENSSL_free(frag);
232}
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley71d8a082014-12-13 16:28:18 -0800234/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
235 * SSL3_RT_CHANGE_CIPHER_SPEC) */
236int dtls1_do_write(
237 SSL *s, int type,
238 enum should_add_to_finished_hash should_add_to_finished_hash) {
239 int ret;
240 int curr_mtu;
241 unsigned int len, frag_off, mac_size = 0, blocksize = 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
Adam Langley71d8a082014-12-13 16:28:18 -0800265 if (s->write_hash &&
266 (s->enc_write_ctx == NULL ||
267 EVP_CIPHER_CTX_mode(s->enc_write_ctx) != EVP_CIPH_GCM_MODE)) {
268 mac_size = EVP_MD_CTX_size(s->write_hash);
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langley71d8a082014-12-13 16:28:18 -0800271 if (s->enc_write_ctx &&
272 (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE)) {
273 blocksize = 2 * EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
274 }
Adam Langley95c29f32014-06-20 12:00:00 -0700275
Adam Langley71d8a082014-12-13 16:28:18 -0800276 frag_off = 0;
277 while (s->init_num) {
278 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
279 DTLS1_RT_HEADER_LENGTH - mac_size - blocksize;
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langley71d8a082014-12-13 16:28:18 -0800281 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
282 /* grr.. we could get an error if MTU picked was wrong */
283 ret = BIO_flush(SSL_get_wbio(s));
284 if (ret <= 0) {
285 return ret;
286 }
287 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - mac_size - blocksize;
288 }
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley71d8a082014-12-13 16:28:18 -0800290 if (s->init_num > curr_mtu) {
291 len = curr_mtu;
292 } else {
293 len = s->init_num;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langley71d8a082014-12-13 16:28:18 -0800296 /* XDTLS: this function is too long. split out the CCS part */
297 if (type == SSL3_RT_HANDSHAKE) {
298 if (s->init_off != 0) {
299 assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
300 s->init_off -= DTLS1_HM_HEADER_LENGTH;
301 s->init_num += DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langley71d8a082014-12-13 16:28:18 -0800303 if (s->init_num > curr_mtu) {
304 len = curr_mtu;
305 } else {
306 len = s->init_num;
307 }
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langley71d8a082014-12-13 16:28:18 -0800310 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley71d8a082014-12-13 16:28:18 -0800312 dtls1_write_message_header(
313 s, (uint8_t *)&s->init_buf->data[s->init_off]);
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langley71d8a082014-12-13 16:28:18 -0800315 assert(len >= DTLS1_HM_HEADER_LENGTH);
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley71d8a082014-12-13 16:28:18 -0800318 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len);
319 if (ret < 0) {
320 /* might need to update MTU here, but we don't know which previous packet
321 * caused the failure -- so can't really retransmit anything. continue
322 * as if everything is fine and wait for an alert to handle the
323 * retransmit. */
324 if (BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
325 s->d1->mtu =
326 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
327 } else {
328 return (-1);
329 }
330 } else {
331 /* bad if this assert fails, only part of the handshake message got sent.
332 * But why would this happen? */
333 assert(len == (unsigned int)ret);
334
335 if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting &&
336 should_add_to_finished_hash == add_to_finished_hash) {
337 /* should not be done for 'Hello Request's, but in that case
338 * we'll ignore the result anyway */
339 uint8_t *p = (uint8_t *)&s->init_buf->data[s->init_off];
340 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
341 int xlen;
342
343 if (frag_off == 0) {
344 /* reconstruct message header is if it
345 * is being sent in single fragment */
346 *p++ = msg_hdr->type;
347 l2n3(msg_hdr->msg_len, p);
348 s2n(msg_hdr->seq, p);
349 l2n3(0, p);
350 l2n3(msg_hdr->msg_len, p);
351 p -= DTLS1_HM_HEADER_LENGTH;
352 xlen = ret;
353 } else {
354 p += DTLS1_HM_HEADER_LENGTH;
355 xlen = ret - DTLS1_HM_HEADER_LENGTH;
356 }
357
358 ssl3_finish_mac(s, p, xlen);
359 }
360
361 if (ret == s->init_num) {
362 if (s->msg_callback) {
363 s->msg_callback(1, s->version, type, s->init_buf->data,
364 (size_t)(s->init_off + s->init_num), s,
365 s->msg_callback_arg);
366 }
367
368 s->init_off = 0; /* done writing this message */
369 s->init_num = 0;
370
371 return 1;
372 }
373 s->init_off += ret;
374 s->init_num -= ret;
375 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
376 }
377 }
378
379 return 0;
380}
Adam Langley95c29f32014-06-20 12:00:00 -0700381
382
Adam Langley71d8a082014-12-13 16:28:18 -0800383/* Obtain handshake message of message type 'mt' (any if mt == -1), maximum
384 * acceptable body length 'max'. Read an entire handshake message. Handshake
385 * messages arrive in fragments. */
386long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max,
387 int hash_message, int *ok) {
388 int i, al;
389 struct hm_header_st *msg_hdr;
390 uint8_t *p;
391 unsigned long msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langley71d8a082014-12-13 16:28:18 -0800393 /* s3->tmp is used to store messages that are unexpected, caused
394 * by the absence of an optional handshake message */
395 if (s->s3->tmp.reuse_message) {
396 /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined
397 * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE
398 * would have to have been applied to the previous call. */
399 assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE);
400 s->s3->tmp.reuse_message = 0;
401 if (mt >= 0 && s->s3->tmp.message_type != mt) {
402 al = SSL_AD_UNEXPECTED_MESSAGE;
403 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
404 goto f_err;
405 }
406 *ok = 1;
407 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
408 s->init_num = (int)s->s3->tmp.message_size;
409 return s->init_num;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langley71d8a082014-12-13 16:28:18 -0800412 msg_hdr = &s->d1->r_msg_hdr;
413 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700414
415again:
Adam Langley71d8a082014-12-13 16:28:18 -0800416 i = dtls1_get_message_fragment(s, stn, max, ok);
417 if (i == DTLS1_HM_BAD_FRAGMENT ||
418 i == DTLS1_HM_FRAGMENT_RETRY) {
419 /* bad fragment received */
420 goto again;
421 } else if (i <= 0 && !*ok) {
422 return i;
423 }
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langley71d8a082014-12-13 16:28:18 -0800425 p = (uint8_t *)s->init_buf->data;
426 msg_len = msg_hdr->msg_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langley71d8a082014-12-13 16:28:18 -0800428 /* reconstruct message header */
429 *(p++) = msg_hdr->type;
430 l2n3(msg_len, p);
431 s2n(msg_hdr->seq, p);
432 l2n3(0, p);
433 l2n3(msg_len, p);
434 p -= DTLS1_HM_HEADER_LENGTH;
435 msg_len += DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langley71d8a082014-12-13 16:28:18 -0800437 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
David Benjamin590cbe92014-08-25 21:34:56 -0400438
Adam Langley71d8a082014-12-13 16:28:18 -0800439 if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE) {
440 ssl3_hash_current_message(s);
441 }
442 if (s->msg_callback) {
443 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, s,
444 s->msg_callback_arg);
445 }
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langley71d8a082014-12-13 16:28:18 -0800447 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langley71d8a082014-12-13 16:28:18 -0800449 s->d1->handshake_read_seq++;
Adam Langley95c29f32014-06-20 12:00:00 -0700450
Adam Langley71d8a082014-12-13 16:28:18 -0800451 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700452
453f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800454 ssl3_send_alert(s, SSL3_AL_FATAL, al);
455 *ok = 0;
456 return -1;
457}
458
459static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr,
460 int max) {
461 size_t frag_off, frag_len, msg_len;
462
463 msg_len = msg_hdr->msg_len;
464 frag_off = msg_hdr->frag_off;
465 frag_len = msg_hdr->frag_len;
466
467 /* sanity checking */
468 if ((frag_off + frag_len) > msg_len) {
469 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
470 SSL_R_EXCESSIVE_MESSAGE_SIZE);
471 return SSL_AD_ILLEGAL_PARAMETER;
472 }
473
474 if ((frag_off + frag_len) > (unsigned long)max) {
475 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
476 SSL_R_EXCESSIVE_MESSAGE_SIZE);
477 return SSL_AD_ILLEGAL_PARAMETER;
478 }
479
480 if (s->d1->r_msg_hdr.frag_off == 0) {
481 /* first fragment */
482 /* msg_len is limited to 2^24, but is effectively checked
483 * against max above */
484 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
485 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB);
486 return SSL_AD_INTERNAL_ERROR;
487 }
488
489 s->s3->tmp.message_size = msg_len;
490 s->d1->r_msg_hdr.msg_len = msg_len;
491 s->s3->tmp.message_type = msg_hdr->type;
492 s->d1->r_msg_hdr.type = msg_hdr->type;
493 s->d1->r_msg_hdr.seq = msg_hdr->seq;
494 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
495 /* They must be playing with us! BTW, failure to enforce
496 * upper limit would open possibility for buffer overrun. */
497 OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment,
498 SSL_R_EXCESSIVE_MESSAGE_SIZE);
499 return SSL_AD_ILLEGAL_PARAMETER;
500 }
501
502 return 0; /* no error */
503}
Adam Langley95c29f32014-06-20 12:00:00 -0700504
505
Adam Langley71d8a082014-12-13 16:28:18 -0800506static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) {
507 /* (0) check whether the desired fragment is available
508 * if so:
509 * (1) copy over the fragment to s->init_buf->data[]
510 * (2) update s->init_num */
511 pitem *item;
512 hm_fragment *frag;
513 int al;
514 unsigned long frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langley71d8a082014-12-13 16:28:18 -0800516 *ok = 0;
517 item = pqueue_peek(s->d1->buffered_messages);
518 if (item == NULL) {
519 return 0;
520 }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langley71d8a082014-12-13 16:28:18 -0800522 frag = (hm_fragment *)item->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley71d8a082014-12-13 16:28:18 -0800524 /* Don't return if reassembly still in progress */
525 if (frag->reassembly != NULL) {
526 return 0;
527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langley71d8a082014-12-13 16:28:18 -0800529 if (s->d1->handshake_read_seq != frag->msg_header.seq) {
530 return 0;
531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langley71d8a082014-12-13 16:28:18 -0800533 frag_len = frag->msg_header.frag_len;
534 pqueue_pop(s->d1->buffered_messages);
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley71d8a082014-12-13 16:28:18 -0800536 al = dtls1_preprocess_fragment(s, &frag->msg_header, max);
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langley71d8a082014-12-13 16:28:18 -0800538 if (al == 0) {
539 /* no alert */
540 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
541 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
542 frag->msg_header.frag_len);
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley71d8a082014-12-13 16:28:18 -0800545 dtls1_hm_fragment_free(frag);
546 pitem_free(item);
Adam Langley95c29f32014-06-20 12:00:00 -0700547
Adam Langley71d8a082014-12-13 16:28:18 -0800548 if (al == 0) {
549 *ok = 1;
550 return frag_len;
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley71d8a082014-12-13 16:28:18 -0800553 ssl3_send_alert(s, SSL3_AL_FATAL, al);
554 s->init_num = 0;
555 *ok = 0;
556 return -1;
557}
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Matt Caswell2306fe52014-06-06 14:25:52 -0700559/* dtls1_max_handshake_message_len returns the maximum number of bytes
560 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
561 * be greater if the maximum certificate list size requires it. */
Adam Langley71d8a082014-12-13 16:28:18 -0800562static unsigned long dtls1_max_handshake_message_len(const SSL *s) {
563 unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
564 if (max_len < (unsigned long)s->max_cert_list) {
565 return s->max_cert_list;
566 }
567 return max_len;
568}
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langley71d8a082014-12-13 16:28:18 -0800570static int dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr,
571 int *ok) {
572 hm_fragment *frag = NULL;
573 pitem *item = NULL;
574 int i = -1, is_complete;
575 uint8_t seq64be[8];
576 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700577
Adam Langley71d8a082014-12-13 16:28:18 -0800578 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
579 msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) {
580 goto err;
581 }
Adam Langley95c29f32014-06-20 12:00:00 -0700582
Adam Langley71d8a082014-12-13 16:28:18 -0800583 if (frag_len == 0) {
584 return DTLS1_HM_FRAGMENT_RETRY;
585 }
Adam Langleye951ff42014-06-06 14:30:33 -0700586
Adam Langley71d8a082014-12-13 16:28:18 -0800587 /* Try to find item in queue */
588 memset(seq64be, 0, sizeof(seq64be));
589 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
590 seq64be[7] = (uint8_t)msg_hdr->seq;
591 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700592
Adam Langley71d8a082014-12-13 16:28:18 -0800593 if (item == NULL) {
594 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
595 if (frag == NULL) {
596 goto err;
597 }
598 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
599 frag->msg_header.frag_len = frag->msg_header.msg_len;
600 frag->msg_header.frag_off = 0;
601 } else {
602 frag = (hm_fragment *)item->data;
603 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
604 item = NULL;
605 frag = NULL;
606 goto err;
607 }
608 }
Adam Langleybed22142014-06-20 12:00:00 -0700609
Adam Langley71d8a082014-12-13 16:28:18 -0800610 /* If message is already reassembled, this must be a
611 * retransmit and can be dropped. In this case item != NULL and so frag
612 * does not need to be freed. */
613 if (frag->reassembly == NULL) {
614 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langley71d8a082014-12-13 16:28:18 -0800616 assert(item != NULL);
617 while (frag_len) {
618 i = s->method->ssl_read_bytes(
619 s, SSL3_RT_HANDSHAKE, devnull,
620 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
621 if (i <= 0) {
622 goto err;
623 }
624 frag_len -= i;
625 }
626 return DTLS1_HM_FRAGMENT_RETRY;
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langley71d8a082014-12-13 16:28:18 -0800629 /* read the body of the fragment (header has already been read */
630 i = s->method->ssl_read_bytes(
631 s, SSL3_RT_HANDSHAKE, frag->fragment + msg_hdr->frag_off, frag_len, 0);
632 if ((unsigned long)i != frag_len) {
633 i = -1;
634 }
635 if (i <= 0) {
636 goto err;
637 }
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langley71d8a082014-12-13 16:28:18 -0800639 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
640 (long)(msg_hdr->frag_off + frag_len));
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langley71d8a082014-12-13 16:28:18 -0800642 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
643 is_complete);
Adam Langley95c29f32014-06-20 12:00:00 -0700644
Adam Langley71d8a082014-12-13 16:28:18 -0800645 if (is_complete) {
646 OPENSSL_free(frag->reassembly);
647 frag->reassembly = NULL;
648 }
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langley71d8a082014-12-13 16:28:18 -0800650 if (item == NULL) {
651 item = pitem_new(seq64be, frag);
652 if (item == NULL) {
653 i = -1;
654 goto err;
655 }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langley71d8a082014-12-13 16:28:18 -0800657 item = pqueue_insert(s->d1->buffered_messages, item);
658 /* pqueue_insert fails iff a duplicate item is inserted.
659 * However, |item| cannot be a duplicate. If it were,
660 * |pqueue_find|, above, would have returned it and control
661 * would never have reached this branch. */
662 assert(item != NULL);
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langley71d8a082014-12-13 16:28:18 -0800665 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700666
667err:
Adam Langley71d8a082014-12-13 16:28:18 -0800668 if (frag != NULL && item == NULL) {
669 dtls1_hm_fragment_free(frag);
670 }
671 *ok = 0;
672 return i;
673}
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langley71d8a082014-12-13 16:28:18 -0800675static int dtls1_process_out_of_seq_message(SSL *s,
676 const struct hm_header_st *msg_hdr,
677 int *ok) {
678 int i = -1;
679 hm_fragment *frag = NULL;
680 pitem *item = NULL;
681 uint8_t seq64be[8];
682 unsigned long frag_len = msg_hdr->frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langley71d8a082014-12-13 16:28:18 -0800684 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) {
685 goto err;
686 }
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langley71d8a082014-12-13 16:28:18 -0800688 /* Try to find item in queue, to prevent duplicate entries */
689 memset(seq64be, 0, sizeof(seq64be));
690 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
691 seq64be[7] = (uint8_t)msg_hdr->seq;
692 item = pqueue_find(s->d1->buffered_messages, seq64be);
Adam Langley95c29f32014-06-20 12:00:00 -0700693
Adam Langley71d8a082014-12-13 16:28:18 -0800694 /* If we already have an entry and this one is a fragment,
695 * don't discard it and rather try to reassemble it. */
696 if (item != NULL && frag_len != msg_hdr->msg_len) {
697 item = NULL;
698 }
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langley71d8a082014-12-13 16:28:18 -0800700 /* Discard the message if sequence number was already there, is
701 * too far in the future, already in the queue or if we received
702 * a FINISHED before the SERVER_HELLO, which then must be a stale
703 * retransmit. */
704 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
705 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
706 (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
707 uint8_t devnull[256];
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langley71d8a082014-12-13 16:28:18 -0800709 while (frag_len) {
710 i = s->method->ssl_read_bytes(
711 s, SSL3_RT_HANDSHAKE, devnull,
712 frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0);
713 if (i <= 0) {
714 goto err;
715 }
716 frag_len -= i;
717 }
718 } else {
719 if (frag_len != msg_hdr->msg_len) {
720 return dtls1_reassemble_fragment(s, msg_hdr, ok);
721 }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langley71d8a082014-12-13 16:28:18 -0800723 if (frag_len > dtls1_max_handshake_message_len(s)) {
724 goto err;
725 }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
Adam Langley71d8a082014-12-13 16:28:18 -0800727 frag = dtls1_hm_fragment_new(frag_len, 0);
728 if (frag == NULL) {
729 goto err;
730 }
Matt Caswell2306fe52014-06-06 14:25:52 -0700731
Adam Langley71d8a082014-12-13 16:28:18 -0800732 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langley71d8a082014-12-13 16:28:18 -0800734 if (frag_len) {
735 /* read the body of the fragment (header has already been read */
736 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment,
737 frag_len, 0);
738 if ((unsigned long)i != frag_len) {
739 i = -1;
740 }
741 if (i <= 0) {
742 goto err;
743 }
744 }
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Adam Langley71d8a082014-12-13 16:28:18 -0800746 item = pitem_new(seq64be, frag);
747 if (item == NULL) {
748 goto err;
749 }
Adam Langley95c29f32014-06-20 12:00:00 -0700750
Adam Langley71d8a082014-12-13 16:28:18 -0800751 item = pqueue_insert(s->d1->buffered_messages, item);
752 /* pqueue_insert fails iff a duplicate item is inserted.
753 * However, |item| cannot be a duplicate. If it were,
754 * |pqueue_find|, above, would have returned it. Then, either
755 * |frag_len| != |msg_hdr->msg_len| in which case |item| is set
756 * to NULL and it will have been processed with
757 * |dtls1_reassemble_fragment|, above, or the record will have
758 * been discarded. */
759 assert(item != NULL);
760 }
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langley71d8a082014-12-13 16:28:18 -0800762 return DTLS1_HM_FRAGMENT_RETRY;
Adam Langley95c29f32014-06-20 12:00:00 -0700763
764err:
Adam Langley71d8a082014-12-13 16:28:18 -0800765 if (frag != NULL && item == NULL) {
766 dtls1_hm_fragment_free(frag);
767 }
768 *ok = 0;
769 return i;
770}
Adam Langley95c29f32014-06-20 12:00:00 -0700771
772
Adam Langley71d8a082014-12-13 16:28:18 -0800773static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok) {
774 uint8_t wire[DTLS1_HM_HEADER_LENGTH];
775 unsigned long len, frag_off, frag_len;
776 int i, al;
777 struct hm_header_st msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langley71d8a082014-12-13 16:28:18 -0800779redo:
780 /* see if we have the required fragment already */
781 if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) {
782 if (*ok) {
783 s->init_num = frag_len;
784 }
785 return frag_len;
786 }
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langley71d8a082014-12-13 16:28:18 -0800788 /* read handshake message header */
789 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire,
790 DTLS1_HM_HEADER_LENGTH, 0);
791 if (i <= 0) {
792 /* nbio, or an error */
793 s->rwstate = SSL_READING;
794 *ok = 0;
795 return i;
796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langley71d8a082014-12-13 16:28:18 -0800798 /* Handshake fails if message header is incomplete */
799 if (i != DTLS1_HM_HEADER_LENGTH) {
800 al = SSL_AD_UNEXPECTED_MESSAGE;
801 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
802 SSL_R_UNEXPECTED_MESSAGE);
803 goto f_err;
804 }
Adam Langley95c29f32014-06-20 12:00:00 -0700805
Adam Langley71d8a082014-12-13 16:28:18 -0800806 /* parse the message fragment header */
807 dtls1_get_message_header(wire, &msg_hdr);
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langley71d8a082014-12-13 16:28:18 -0800809 /* if this is a future (or stale) message it gets buffered
810 * (or dropped)--no further processing at this time. */
811 if (msg_hdr.seq != s->d1->handshake_read_seq) {
812 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok);
813 }
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langley71d8a082014-12-13 16:28:18 -0800815 len = msg_hdr.msg_len;
816 frag_off = msg_hdr.frag_off;
817 frag_len = msg_hdr.frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700818
Adam Langley71d8a082014-12-13 16:28:18 -0800819 if (frag_len && frag_len < len) {
820 return dtls1_reassemble_fragment(s, &msg_hdr, ok);
821 }
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Adam Langley71d8a082014-12-13 16:28:18 -0800823 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
824 wire[0] == SSL3_MT_HELLO_REQUEST) {
825 /* The server may always send 'Hello Request' messages --
826 * we are doing a handshake anyway now, so ignore them
827 * if their format is correct. Does not count for
828 * 'Finished' MAC. */
829 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
830 if (s->msg_callback) {
831 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, wire,
832 DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg);
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langley71d8a082014-12-13 16:28:18 -0800835 s->init_num = 0;
836 goto redo;
837 } else {
838 /* Incorrectly formated Hello request */
839 al = SSL_AD_UNEXPECTED_MESSAGE;
840 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
841 SSL_R_UNEXPECTED_MESSAGE);
842 goto f_err;
843 }
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langley71d8a082014-12-13 16:28:18 -0800846 if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) {
847 goto f_err;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langley71d8a082014-12-13 16:28:18 -0800850 /* XDTLS: ressurect this when restart is in place */
851 s->state = stn;
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langley71d8a082014-12-13 16:28:18 -0800853 if (frag_len > 0) {
854 uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langley71d8a082014-12-13 16:28:18 -0800856 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[frag_off], frag_len,
857 0);
858 /* XDTLS: fix this--message fragments cannot span multiple packets */
859 if (i <= 0) {
860 s->rwstate = SSL_READING;
861 *ok = 0;
862 return i;
863 }
864 } else {
865 i = 0;
866 }
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langley71d8a082014-12-13 16:28:18 -0800868 /* XDTLS: an incorrectly formatted fragment should cause the
869 * handshake to fail */
870 if (i != (int)frag_len) {
871 al = SSL3_AD_ILLEGAL_PARAMETER;
872 OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment,
873 SSL3_AD_ILLEGAL_PARAMETER);
874 goto f_err;
875 }
876
877 *ok = 1;
878
879 /* Note that s->init_num is *not* used as current offset in
880 * s->init_buf->data, but as a counter summing up fragments'
881 * lengths: as soon as they sum up to handshake packet
882 * length, we assume we have got all the fragments. */
883 s->init_num = frag_len;
884 return frag_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700885
886f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800887 ssl3_send_alert(s, SSL3_AL_FATAL, al);
888 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langley71d8a082014-12-13 16:28:18 -0800890 *ok = 0;
891 return -1;
892}
Adam Langley95c29f32014-06-20 12:00:00 -0700893
894/* for these 2 messages, we need to
895 * ssl->enc_read_ctx re-init
896 * ssl->s3->read_sequence zero
897 * ssl->s3->read_mac_secret re-init
898 * ssl->session->read_sym_enc assign
899 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800900 * ssl->session->read_hash assign */
901int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
902 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langley71d8a082014-12-13 16:28:18 -0800904 if (s->state == a) {
905 p = (uint8_t *)s->init_buf->data;
906 *p++ = SSL3_MT_CCS;
907 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
908 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langley71d8a082014-12-13 16:28:18 -0800910 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langley71d8a082014-12-13 16:28:18 -0800912 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq,
913 0, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langley71d8a082014-12-13 16:28:18 -0800915 /* buffer the message to handle re-xmits */
916 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langley71d8a082014-12-13 16:28:18 -0800918 s->state = b;
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langley71d8a082014-12-13 16:28:18 -0800921 /* SSL3_ST_CW_CHANGE_B */
922 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC,
923 dont_add_to_finished_hash);
924}
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langley71d8a082014-12-13 16:28:18 -0800926int dtls1_read_failed(SSL *s, int code) {
927 if (code > 0) {
928 fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__);
929 return 1;
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langley71d8a082014-12-13 16:28:18 -0800932 if (!dtls1_is_timer_expired(s)) {
933 /* not a timeout, none of our business, let higher layers handle this. In
934 * fact, it's probably an error */
935 return code;
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langley71d8a082014-12-13 16:28:18 -0800938 if (!SSL_in_init(s)) {
939 /* done, no need to send a retransmit */
940 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
941 return code;
942 }
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langley71d8a082014-12-13 16:28:18 -0800944 return dtls1_handle_timeout(s);
945}
Adam Langley95c29f32014-06-20 12:00:00 -0700946
Adam Langley71d8a082014-12-13 16:28:18 -0800947int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
948 /* The index of the retransmission queue actually is the message sequence
949 * number, since the queue only contains messages of a single handshake.
950 * However, the ChangeCipherSpec has no message sequence number and so using
951 * only the sequence will result in the CCS and Finished having the same
952 * index. To prevent this, the sequence number is multiplied by 2. In case of
953 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
954 * maintains the order of the index (important for priority queues) and fits
955 * in the unsigned short variable. */
956 return seq * 2 - is_ccs;
957}
Adam Langley95c29f32014-06-20 12:00:00 -0700958
Adam Langley71d8a082014-12-13 16:28:18 -0800959int dtls1_retransmit_buffered_messages(SSL *s) {
960 pqueue sent = s->d1->sent_messages;
961 piterator iter;
962 pitem *item;
963 hm_fragment *frag;
964 int found = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langley71d8a082014-12-13 16:28:18 -0800966 iter = pqueue_iterator(sent);
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langley71d8a082014-12-13 16:28:18 -0800968 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
969 frag = (hm_fragment *)item->data;
970 if (dtls1_retransmit_message(
971 s, (unsigned short)dtls1_get_queue_priority(
972 frag->msg_header.seq, frag->msg_header.is_ccs),
973 0, &found) <= 0 &&
974 found) {
975 fprintf(stderr, "dtls1_retransmit_message() failed\n");
976 return -1;
977 }
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langley71d8a082014-12-13 16:28:18 -0800980 return 1;
981}
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langley71d8a082014-12-13 16:28:18 -0800983int dtls1_buffer_message(SSL *s, int is_ccs) {
984 pitem *item;
985 hm_fragment *frag;
986 uint8_t seq64be[8];
Adam Langley95c29f32014-06-20 12:00:00 -0700987
Adam Langley71d8a082014-12-13 16:28:18 -0800988 /* this function is called immediately after a message has
989 * been serialized */
990 assert(s->init_off == 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langley71d8a082014-12-13 16:28:18 -0800992 frag = dtls1_hm_fragment_new(s->init_num, 0);
993 if (!frag) {
994 return 0;
995 }
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langley71d8a082014-12-13 16:28:18 -0800997 memcpy(frag->fragment, s->init_buf->data, s->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langley71d8a082014-12-13 16:28:18 -0800999 if (is_ccs) {
1000 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
1001 (unsigned int)s->init_num);
1002 } else {
1003 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
1004 (unsigned int)s->init_num);
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langley71d8a082014-12-13 16:28:18 -08001007 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1008 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1009 frag->msg_header.type = s->d1->w_msg_hdr.type;
1010 frag->msg_header.frag_off = 0;
1011 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1012 frag->msg_header.is_ccs = is_ccs;
Adam Langley95c29f32014-06-20 12:00:00 -07001013
Adam Langley71d8a082014-12-13 16:28:18 -08001014 /* save current state*/
1015 frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1016 frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1017 frag->msg_header.saved_retransmit_state.session = s->session;
1018 frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langley71d8a082014-12-13 16:28:18 -08001020 memset(seq64be, 0, sizeof(seq64be));
1021 seq64be[6] = (uint8_t)(
1022 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
1023 8);
1024 seq64be[7] = (uint8_t)(
1025 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langley71d8a082014-12-13 16:28:18 -08001027 item = pitem_new(seq64be, frag);
1028 if (item == NULL) {
1029 dtls1_hm_fragment_free(frag);
1030 return 0;
1031 }
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langley71d8a082014-12-13 16:28:18 -08001033 pqueue_insert(s->d1->sent_messages, item);
1034 return 1;
1035}
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langley71d8a082014-12-13 16:28:18 -08001037int dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off,
1038 int *found) {
1039 int ret;
1040 /* XDTLS: for now assuming that read/writes are blocking */
1041 pitem *item;
1042 hm_fragment *frag;
1043 unsigned long header_length;
1044 uint8_t seq64be[8];
1045 struct dtls1_retransmit_state saved_state;
1046 uint8_t save_write_sequence[8];
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langley71d8a082014-12-13 16:28:18 -08001048 /* assert(s->init_num == 0);
1049 assert(s->init_off == 0); */
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Adam Langley71d8a082014-12-13 16:28:18 -08001051 /* XDTLS: the requested message ought to be found, otherwise error */
1052 memset(seq64be, 0, sizeof(seq64be));
1053 seq64be[6] = (uint8_t)(seq >> 8);
1054 seq64be[7] = (uint8_t)seq;
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Adam Langley71d8a082014-12-13 16:28:18 -08001056 item = pqueue_find(s->d1->sent_messages, seq64be);
1057 if (item == NULL) {
1058 fprintf(stderr, "retransmit: message %d non-existant\n", seq);
1059 *found = 0;
1060 return 0;
1061 }
Adam Langley95c29f32014-06-20 12:00:00 -07001062
Adam Langley71d8a082014-12-13 16:28:18 -08001063 *found = 1;
1064 frag = (hm_fragment *)item->data;
Adam Langley95c29f32014-06-20 12:00:00 -07001065
Adam Langley71d8a082014-12-13 16:28:18 -08001066 if (frag->msg_header.is_ccs) {
1067 header_length = DTLS1_CCS_HEADER_LENGTH;
1068 } else {
1069 header_length = DTLS1_HM_HEADER_LENGTH;
1070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
Adam Langley71d8a082014-12-13 16:28:18 -08001072 memcpy(s->init_buf->data, frag->fragment,
1073 frag->msg_header.msg_len + header_length);
1074 s->init_num = frag->msg_header.msg_len + header_length;
Adam Langley95c29f32014-06-20 12:00:00 -07001075
Adam Langley71d8a082014-12-13 16:28:18 -08001076 dtls1_set_message_header_int(s, frag->msg_header.type,
1077 frag->msg_header.msg_len, frag->msg_header.seq,
1078 0, frag->msg_header.frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001079
Adam Langley71d8a082014-12-13 16:28:18 -08001080 /* save current state */
1081 saved_state.enc_write_ctx = s->enc_write_ctx;
1082 saved_state.write_hash = s->write_hash;
1083 saved_state.session = s->session;
1084 saved_state.epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langley71d8a082014-12-13 16:28:18 -08001086 s->d1->retransmitting = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001087
Adam Langley71d8a082014-12-13 16:28:18 -08001088 /* restore state in which the message was originally sent */
1089 s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1090 s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1091 s->session = frag->msg_header.saved_retransmit_state.session;
1092 s->d1->w_epoch = frag->msg_header.saved_retransmit_state.epoch;
1093
1094 if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1) {
1095 memcpy(save_write_sequence, s->s3->write_sequence,
1096 sizeof(s->s3->write_sequence));
1097 memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
1098 sizeof(s->s3->write_sequence));
1099 }
1100
1101 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
1102 : SSL3_RT_HANDSHAKE,
1103 add_to_finished_hash);
1104
1105 /* restore current state */
1106 s->enc_write_ctx = saved_state.enc_write_ctx;
1107 s->write_hash = saved_state.write_hash;
1108 s->session = saved_state.session;
1109 s->d1->w_epoch = saved_state.epoch;
1110
1111 if (frag->msg_header.saved_retransmit_state.epoch == saved_state.epoch - 1) {
1112 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
1113 sizeof(s->s3->write_sequence));
1114 memcpy(s->s3->write_sequence, save_write_sequence,
1115 sizeof(s->s3->write_sequence));
1116 }
1117
1118 s->d1->retransmitting = 0;
1119
1120 (void)BIO_flush(SSL_get_wbio(s));
1121 return ret;
1122}
Adam Langley95c29f32014-06-20 12:00:00 -07001123
1124/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -08001125void dtls1_clear_record_buffer(SSL *s) {
1126 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langley71d8a082014-12-13 16:28:18 -08001128 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
1129 item = pqueue_pop(s->d1->sent_messages)) {
1130 dtls1_hm_fragment_free((hm_fragment *)item->data);
1131 pitem_free(item);
1132 }
1133}
Adam Langley95c29f32014-06-20 12:00:00 -07001134
Adam Langley71d8a082014-12-13 16:28:18 -08001135uint8_t *dtls1_set_message_header(SSL *s, uint8_t *p, uint8_t mt,
1136 unsigned long len, unsigned long frag_off,
1137 unsigned long frag_len) {
1138 if (frag_off == 0) {
1139 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1140 s->d1->next_handshake_write_seq++;
1141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langley71d8a082014-12-13 16:28:18 -08001143 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq, frag_off,
1144 frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langley71d8a082014-12-13 16:28:18 -08001146 return p += DTLS1_HM_HEADER_LENGTH;
1147}
Adam Langley95c29f32014-06-20 12:00:00 -07001148
1149/* don't actually do the writing, wait till the MTU has been retrieved */
Adam Langley71d8a082014-12-13 16:28:18 -08001150static void dtls1_set_message_header_int(SSL *s, uint8_t mt, unsigned long len,
1151 unsigned short seq_num,
1152 unsigned long frag_off,
1153 unsigned long frag_len) {
1154 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langley71d8a082014-12-13 16:28:18 -08001156 msg_hdr->type = mt;
1157 msg_hdr->msg_len = len;
1158 msg_hdr->seq = seq_num;
1159 msg_hdr->frag_off = frag_off;
1160 msg_hdr->frag_len = frag_len;
1161}
Adam Langley95c29f32014-06-20 12:00:00 -07001162
Adam Langley71d8a082014-12-13 16:28:18 -08001163static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
1164 unsigned long frag_len) {
1165 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001166
Adam Langley71d8a082014-12-13 16:28:18 -08001167 msg_hdr->frag_off = frag_off;
1168 msg_hdr->frag_len = frag_len;
1169}
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langley71d8a082014-12-13 16:28:18 -08001171static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
1172 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -07001173
Adam Langley71d8a082014-12-13 16:28:18 -08001174 *p++ = msg_hdr->type;
1175 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001176
Adam Langley71d8a082014-12-13 16:28:18 -08001177 s2n(msg_hdr->seq, p);
1178 l2n3(msg_hdr->frag_off, p);
1179 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001180
Adam Langley71d8a082014-12-13 16:28:18 -08001181 return p;
1182}
Adam Langley95c29f32014-06-20 12:00:00 -07001183
Adam Langley71d8a082014-12-13 16:28:18 -08001184unsigned int dtls1_min_mtu(void) {
1185 return g_probable_mtu[(sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0])) -
1186 1];
1187}
Adam Langley95c29f32014-06-20 12:00:00 -07001188
Adam Langley71d8a082014-12-13 16:28:18 -08001189static unsigned int dtls1_guess_mtu(unsigned int curr_mtu) {
1190 unsigned int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001191
Adam Langley71d8a082014-12-13 16:28:18 -08001192 if (curr_mtu == 0) {
1193 return g_probable_mtu[0];
1194 }
Adam Langley95c29f32014-06-20 12:00:00 -07001195
Adam Langley71d8a082014-12-13 16:28:18 -08001196 for (i = 0; i < sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0]); i++) {
1197 if (curr_mtu > g_probable_mtu[i]) {
1198 return g_probable_mtu[i];
1199 }
1200 }
Adam Langley95c29f32014-06-20 12:00:00 -07001201
Adam Langley71d8a082014-12-13 16:28:18 -08001202 return curr_mtu;
1203}
Adam Langley95c29f32014-06-20 12:00:00 -07001204
Adam Langley71d8a082014-12-13 16:28:18 -08001205void dtls1_get_message_header(uint8_t *data,
1206 struct hm_header_st *msg_hdr) {
1207 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
1208 msg_hdr->type = *(data++);
1209 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001210
Adam Langley71d8a082014-12-13 16:28:18 -08001211 n2s(data, msg_hdr->seq);
1212 n2l3(data, msg_hdr->frag_off);
1213 n2l3(data, msg_hdr->frag_len);
1214}
Adam Langley95c29f32014-06-20 12:00:00 -07001215
Adam Langley71d8a082014-12-13 16:28:18 -08001216void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) {
1217 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -07001218
Adam Langley71d8a082014-12-13 16:28:18 -08001219 ccs_hdr->type = *(data++);
1220}
Adam Langley95c29f32014-06-20 12:00:00 -07001221
Adam Langley71d8a082014-12-13 16:28:18 -08001222int dtls1_shutdown(SSL *s) {
1223 int ret;
1224 ret = ssl3_shutdown(s);
1225 return ret;
1226}