blob: a19ee3d32df52c6c62870189e70f880d8700e5b5 [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
Adam Langley95c29f32014-06-20 12:00:00 -0700129
David Benjamina18b6712015-01-11 19:31:22 -0500130/* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable
131 * for these values? Notably, why is kMinMTU a function of the transport
132 * protocol's overhead rather than, say, what's needed to hold a minimally-sized
133 * handshake fragment plus protocol overhead. */
Adam Langley95c29f32014-06-20 12:00:00 -0700134
David Benjamina18b6712015-01-11 19:31:22 -0500135/* kMinMTU is the minimum acceptable MTU value. */
136static const unsigned int kMinMTU = 256 - 28;
137
138/* kDefaultMTU is the default MTU value to use if neither the user nor
139 * the underlying BIO supplies one. */
140static const unsigned int kDefaultMTU = 1500 - 28;
141
David Benjamin75381222015-03-02 19:30:30 -0500142/* kMaxHandshakeBuffer is the maximum number of handshake messages ahead of the
143 * current one to buffer. */
144static const unsigned int kHandshakeBufferSize = 10;
145
Adam Langley71d8a082014-12-13 16:28:18 -0800146static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
147 unsigned long frag_len);
148static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p);
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley71d8a082014-12-13 16:28:18 -0800150static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
151 int reassembly) {
152 hm_fragment *frag = NULL;
David Benjamin75381222015-03-02 19:30:30 -0500153 uint8_t *buf = NULL;
154 uint8_t *bitmask = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langley71d8a082014-12-13 16:28:18 -0800156 frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment));
157 if (frag == NULL) {
David Benjaminee562b92015-03-02 20:52:52 -0500158 OPENSSL_PUT_ERROR(SSL, dtls1_hm_fragment_new, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800159 return NULL;
160 }
Adam Langley95c29f32014-06-20 12:00:00 -0700161
Adam Langley71d8a082014-12-13 16:28:18 -0800162 if (frag_len) {
David Benjamin75381222015-03-02 19:30:30 -0500163 buf = (uint8_t *)OPENSSL_malloc(frag_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800164 if (buf == NULL) {
David Benjaminee562b92015-03-02 20:52:52 -0500165 OPENSSL_PUT_ERROR(SSL, dtls1_hm_fragment_new, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800166 OPENSSL_free(frag);
167 return NULL;
168 }
169 }
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Adam Langley71d8a082014-12-13 16:28:18 -0800171 /* zero length fragment gets zero frag->fragment */
172 frag->fragment = buf;
Adam Langley95c29f32014-06-20 12:00:00 -0700173
Adam Langley71d8a082014-12-13 16:28:18 -0800174 /* Initialize reassembly bitmask if necessary */
David Benjamin75381222015-03-02 19:30:30 -0500175 if (reassembly && frag_len > 0) {
David Benjaminee562b92015-03-02 20:52:52 -0500176 if (frag_len + 7 < frag_len) {
177 OPENSSL_PUT_ERROR(SSL, dtls1_hm_fragment_new, ERR_R_OVERFLOW);
178 return NULL;
179 }
180 size_t bitmask_len = (frag_len + 7) / 8;
181 bitmask = (uint8_t *)OPENSSL_malloc(bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800182 if (bitmask == NULL) {
David Benjaminee562b92015-03-02 20:52:52 -0500183 OPENSSL_PUT_ERROR(SSL, dtls1_hm_fragment_new, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800184 if (buf != NULL) {
185 OPENSSL_free(buf);
186 }
187 OPENSSL_free(frag);
188 return NULL;
189 }
David Benjaminee562b92015-03-02 20:52:52 -0500190 memset(bitmask, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley71d8a082014-12-13 16:28:18 -0800193 frag->reassembly = bitmask;
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley71d8a082014-12-13 16:28:18 -0800195 return frag;
196}
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley71d8a082014-12-13 16:28:18 -0800198void dtls1_hm_fragment_free(hm_fragment *frag) {
Adam Langley71d8a082014-12-13 16:28:18 -0800199 if (frag->fragment) {
200 OPENSSL_free(frag->fragment);
201 }
202 if (frag->reassembly) {
203 OPENSSL_free(frag->reassembly);
204 }
205 OPENSSL_free(frag);
206}
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjaminee562b92015-03-02 20:52:52 -0500208#if !defined(inline)
209#define inline __inline
210#endif
211
212/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
213 * exclusive, set. */
214static inline uint8_t bit_range(size_t start, size_t end) {
215 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
216}
217
218/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
219 * as received in |frag|. If |frag| becomes complete, it clears
220 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
221 * and |frag->reassembly| must not be NULL. */
222static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
223 size_t end) {
224 size_t i;
225 size_t msg_len = frag->msg_header.msg_len;
226
227 if (frag->reassembly == NULL || start > end || end > msg_len) {
228 assert(0);
229 return;
230 }
231 /* A zero-length message will never have a pending reassembly. */
232 assert(msg_len > 0);
233
234 if ((start >> 3) == (end >> 3)) {
235 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
236 } else {
237 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
238 for (i = (start >> 3) + 1; i < (end >> 3); i++) {
239 frag->reassembly[i] = 0xff;
240 }
241 if ((end & 7) != 0) {
242 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
243 }
244 }
245
246 /* Check if the fragment is complete. */
247 for (i = 0; i < (msg_len >> 3); i++) {
248 if (frag->reassembly[i] != 0xff) {
249 return;
250 }
251 }
252 if ((msg_len & 7) != 0 &&
253 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
254 return;
255 }
256
257 OPENSSL_free(frag->reassembly);
258 frag->reassembly = NULL;
259}
260
Adam Langley71d8a082014-12-13 16:28:18 -0800261/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
262 * SSL3_RT_CHANGE_CIPHER_SPEC) */
David Benjamine4824e82014-12-14 19:44:34 -0500263int dtls1_do_write(SSL *s, int type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800264 int ret;
265 int curr_mtu;
David Benjamine95d20d2014-12-23 11:16:01 -0500266 unsigned int len, frag_off;
267 size_t max_overhead = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langley71d8a082014-12-13 16:28:18 -0800269 /* AHA! Figure out the MTU, and stick to the right size */
270 if (s->d1->mtu < dtls1_min_mtu() &&
271 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500272 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
273 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
274 s->d1->mtu = (unsigned)mtu;
275 } else {
David Benjamina18b6712015-01-11 19:31:22 -0500276 s->d1->mtu = kDefaultMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800277 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL);
278 }
279 }
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langley71d8a082014-12-13 16:28:18 -0800281 /* should have something reasonable now */
282 assert(s->d1->mtu >= dtls1_min_mtu());
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langley71d8a082014-12-13 16:28:18 -0800284 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
285 assert(s->init_num ==
286 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288
David Benjamine95d20d2014-12-23 11:16:01 -0500289 /* Determine the maximum overhead of the current cipher. */
290 if (s->aead_write_ctx != NULL) {
291 max_overhead = EVP_AEAD_max_overhead(s->aead_write_ctx->ctx.aead);
292 if (s->aead_write_ctx->variable_nonce_included_in_record) {
293 max_overhead += s->aead_write_ctx->variable_nonce_len;
294 }
Adam Langley71d8a082014-12-13 16:28:18 -0800295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley71d8a082014-12-13 16:28:18 -0800297 frag_off = 0;
298 while (s->init_num) {
David Benjamin17a5f852015-01-11 19:46:52 -0500299 /* Account for data in the buffering BIO; multiple records may be packed
300 * into a single packet during the handshake.
301 *
302 * TODO(davidben): This is buggy; if the MTU is larger than the buffer size,
303 * the large record will be split across two packets. Moreover, in that
304 * case, the |dtls1_write_bytes| call may not return synchronously. This
305 * will break on retry as the |s->init_off| and |s->init_num| adjustment
306 * will run a second time. */
Adam Langley71d8a082014-12-13 16:28:18 -0800307 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
David Benjamine95d20d2014-12-23 11:16:01 -0500308 DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langley71d8a082014-12-13 16:28:18 -0800310 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
David Benjamin17a5f852015-01-11 19:46:52 -0500311 /* Flush the buffer and continue with a fresh packet.
312 *
313 * TODO(davidben): If |BIO_flush| is not synchronous and requires multiple
314 * calls to |dtls1_do_write|, |frag_off| will be wrong. */
Adam Langley71d8a082014-12-13 16:28:18 -0800315 ret = BIO_flush(SSL_get_wbio(s));
316 if (ret <= 0) {
317 return ret;
318 }
David Benjamin17a5f852015-01-11 19:46:52 -0500319 assert(BIO_wpending(SSL_get_wbio(s)) == 0);
David Benjamine95d20d2014-12-23 11:16:01 -0500320 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley71d8a082014-12-13 16:28:18 -0800321 }
Adam Langley95c29f32014-06-20 12:00:00 -0700322
Adam Langley71d8a082014-12-13 16:28:18 -0800323 /* XDTLS: this function is too long. split out the CCS part */
324 if (type == SSL3_RT_HANDSHAKE) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500325 /* If this isn't the first fragment, reserve space to prepend a new
326 * fragment header. This will override the body of a previous fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800327 if (s->init_off != 0) {
328 assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
329 s->init_off -= DTLS1_HM_HEADER_LENGTH;
330 s->init_num += DTLS1_HM_HEADER_LENGTH;
Adam Langley71d8a082014-12-13 16:28:18 -0800331 }
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamind9778fb2015-01-11 17:24:51 -0500333 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
334 /* To make forward progress, the MTU must, at minimum, fit the handshake
335 * header and one byte of handshake body. */
336 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
337 return -1;
338 }
Adam Langley95c29f32014-06-20 12:00:00 -0700339
David Benjamind9778fb2015-01-11 17:24:51 -0500340 if (s->init_num > curr_mtu) {
341 len = curr_mtu;
342 } else {
343 len = s->init_num;
344 }
345 assert(len >= DTLS1_HM_HEADER_LENGTH);
346
347 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800348 dtls1_write_message_header(
349 s, (uint8_t *)&s->init_buf->data[s->init_off]);
David Benjamind9778fb2015-01-11 17:24:51 -0500350 } else {
351 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
352 /* ChangeCipherSpec cannot be fragmented. */
353 if (s->init_num > curr_mtu) {
354 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
355 return -1;
356 }
357 len = s->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800358 }
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley71d8a082014-12-13 16:28:18 -0800360 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len);
361 if (ret < 0) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500362 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800363 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500364
365 /* bad if this assert fails, only part of the handshake message got sent.
366 * But why would this happen? */
367 assert(len == (unsigned int)ret);
368
369 if (ret == s->init_num) {
370 if (s->msg_callback) {
371 s->msg_callback(1, s->version, type, s->init_buf->data,
372 (size_t)(s->init_off + s->init_num), s,
373 s->msg_callback_arg);
374 }
375
376 s->init_off = 0; /* done writing this message */
377 s->init_num = 0;
378
379 return 1;
380 }
381 s->init_off += ret;
382 s->init_num -= ret;
383 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800384 }
385
386 return 0;
387}
Adam Langley95c29f32014-06-20 12:00:00 -0700388
David Benjamin75381222015-03-02 19:30:30 -0500389/* dtls1_is_next_message_complete returns one if the next handshake message is
390 * complete and zero otherwise. */
391static int dtls1_is_next_message_complete(SSL *s) {
392 pitem *item = pqueue_peek(s->d1->buffered_messages);
393 if (item == NULL) {
394 return 0;
395 }
396
397 hm_fragment *frag = (hm_fragment *)item->data;
398 assert(s->d1->handshake_read_seq <= frag->msg_header.seq);
399
400 return s->d1->handshake_read_seq == frag->msg_header.seq &&
401 frag->reassembly == NULL;
402}
403
404/* dtls1_discard_fragment_body discards a handshake fragment body of length
405 * |frag_len|. It returns one on success and zero on error.
406 *
407 * TODO(davidben): This function will go away when ssl_read_bytes is gone from
408 * the DTLS side. */
409static int dtls1_discard_fragment_body(SSL *s, size_t frag_len) {
410 uint8_t discard[256];
411 while (frag_len > 0) {
412 size_t chunk = frag_len < sizeof(discard) ? frag_len : sizeof(discard);
413 int ret = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, discard, chunk,
414 0);
415 if (ret != chunk) {
416 return 0;
417 }
418 frag_len -= chunk;
419 }
420 return 1;
421}
422
423/* dtls1_get_buffered_message returns the buffered message corresponding to
424 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
425 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
426 * returns NULL on failure. The caller does not take ownership of the result. */
427static hm_fragment *dtls1_get_buffered_message(
428 SSL *s, const struct hm_header_st *msg_hdr) {
429 uint8_t seq64be[8];
430 memset(seq64be, 0, sizeof(seq64be));
431 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
432 seq64be[7] = (uint8_t)msg_hdr->seq;
433 pitem *item = pqueue_find(s->d1->buffered_messages, seq64be);
434
435 hm_fragment *frag;
436 if (item == NULL) {
437 /* This is the first fragment from this message. */
438 frag = dtls1_hm_fragment_new(msg_hdr->msg_len,
439 1 /* reassembly buffer needed */);
440 if (frag == NULL) {
441 return NULL;
442 }
443 memcpy(&frag->msg_header, msg_hdr, sizeof(*msg_hdr));
444 item = pitem_new(seq64be, frag);
445 if (item == NULL) {
446 dtls1_hm_fragment_free(frag);
447 return NULL;
448 }
449 item = pqueue_insert(s->d1->buffered_messages, item);
450 /* |pqueue_insert| fails iff a duplicate item is inserted, but |item| cannot
451 * be a duplicate. */
452 assert(item != NULL);
453 } else {
454 frag = item->data;
455 assert(frag->msg_header.seq == msg_hdr->seq);
456 if (frag->msg_header.type != msg_hdr->type ||
457 frag->msg_header.msg_len != msg_hdr->msg_len) {
458 /* The new fragment must be compatible with the previous fragments from
459 * this message. */
460 OPENSSL_PUT_ERROR(SSL, dtls1_get_buffered_message,
461 SSL_R_FRAGMENT_MISMATCH);
462 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
463 return NULL;
464 }
465 }
466 return frag;
467}
468
469/* dtls1_max_handshake_message_len returns the maximum number of bytes
470 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
471 * be greater if the maximum certificate list size requires it. */
472static size_t dtls1_max_handshake_message_len(const SSL *s) {
473 size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
474 if (max_len < (size_t)s->max_cert_list) {
475 return (size_t)s->max_cert_list;
476 }
477 return max_len;
478}
479
480/* dtls1_process_fragment reads a handshake fragment and processes it. It
481 * returns one if a fragment was successfully processed and 0 or -1 on error. */
482static int dtls1_process_fragment(SSL *s) {
483 /* Read handshake message header.
484 *
485 * TODO(davidben): ssl_read_bytes allows splitting the fragment header and
486 * body across two records. Change this interface to consume the fragment in
487 * one pass. */
488 uint8_t header[DTLS1_HM_HEADER_LENGTH];
489 int ret = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, header,
490 DTLS1_HM_HEADER_LENGTH, 0);
491 if (ret <= 0) {
492 s->rwstate = SSL_READING;
493 return ret;
494 }
495 if (ret != DTLS1_HM_HEADER_LENGTH) {
496 OPENSSL_PUT_ERROR(SSL, dtls1_process_fragment, SSL_R_UNEXPECTED_MESSAGE);
497 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
498 return -1;
499 }
500
501 /* Parse the message fragment header. */
502 struct hm_header_st msg_hdr;
503 dtls1_get_message_header(header, &msg_hdr);
504
505 const size_t frag_off = msg_hdr.frag_off;
506 const size_t frag_len = msg_hdr.frag_len;
507 const size_t msg_len = msg_hdr.msg_len;
508 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
509 frag_off + frag_len > msg_len ||
510 msg_len > dtls1_max_handshake_message_len(s)) {
511 OPENSSL_PUT_ERROR(SSL, dtls1_process_fragment,
512 SSL_R_EXCESSIVE_MESSAGE_SIZE);
513 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
514 return -1;
515 }
516
517 if (msg_hdr.seq < s->d1->handshake_read_seq ||
518 msg_hdr.seq > (unsigned)s->d1->handshake_read_seq +
519 kHandshakeBufferSize) {
520 /* Ignore fragments from the past, or ones too far in the future. */
521 if (!dtls1_discard_fragment_body(s, frag_len)) {
522 return -1;
523 }
524 return 1;
525 }
526
527 hm_fragment *frag = dtls1_get_buffered_message(s, &msg_hdr);
528 if (frag == NULL) {
529 return -1;
530 }
531 assert(frag->msg_header.msg_len == msg_len);
532
533 if (frag->reassembly == NULL) {
534 /* The message is already assembled. */
535 if (!dtls1_discard_fragment_body(s, frag_len)) {
536 return -1;
537 }
538 return 1;
539 }
540 assert(msg_len > 0);
541
542 /* Read the body of the fragment. */
543 ret = s->method->ssl_read_bytes(
544 s, SSL3_RT_HANDSHAKE, frag->fragment + frag_off, frag_len, 0);
545 if (ret != frag_len) {
546 OPENSSL_PUT_ERROR(SSL, dtls1_process_fragment, SSL_R_UNEXPECTED_MESSAGE);
547 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
548 return -1;
549 }
David Benjaminee562b92015-03-02 20:52:52 -0500550 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
David Benjamin75381222015-03-02 19:30:30 -0500551
David Benjamin75381222015-03-02 19:30:30 -0500552 return 1;
553}
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjaminbcb2d912015-02-24 23:45:43 -0500555/* dtls1_get_message reads a handshake message of message type |msg_type| (any
556 * if |msg_type| == -1), maximum acceptable body length |max|. Read an entire
557 * handshake message. Handshake messages arrive in fragments. */
558long dtls1_get_message(SSL *s, int st1, int stn, int msg_type, long max,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500559 enum ssl_hash_message_t hash_message, int *ok) {
David Benjamin75381222015-03-02 19:30:30 -0500560 pitem *item = NULL;
561 hm_fragment *frag = NULL;
562 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700563
Adam Langley71d8a082014-12-13 16:28:18 -0800564 /* s3->tmp is used to store messages that are unexpected, caused
565 * by the absence of an optional handshake message */
566 if (s->s3->tmp.reuse_message) {
David Benjamin5ca39fb2015-03-01 23:57:54 -0500567 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
568 * ssl_dont_hash_message would have to have been applied to the previous
569 * call. */
570 assert(hash_message == ssl_hash_message);
Adam Langley71d8a082014-12-13 16:28:18 -0800571 s->s3->tmp.reuse_message = 0;
David Benjaminbcb2d912015-02-24 23:45:43 -0500572 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800573 al = SSL_AD_UNEXPECTED_MESSAGE;
574 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
575 goto f_err;
576 }
577 *ok = 1;
578 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
579 s->init_num = (int)s->s3->tmp.message_size;
580 return s->init_num;
581 }
Adam Langley95c29f32014-06-20 12:00:00 -0700582
David Benjamin75381222015-03-02 19:30:30 -0500583 /* Process fragments until one is found. */
584 while (!dtls1_is_next_message_complete(s)) {
585 int ret = dtls1_process_fragment(s);
586 if (ret <= 0) {
587 *ok = 0;
588 return ret;
589 }
Adam Langley71d8a082014-12-13 16:28:18 -0800590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamin75381222015-03-02 19:30:30 -0500592 /* Read out the next complete handshake message. */
593 item = pqueue_pop(s->d1->buffered_messages);
594 assert(item != NULL);
595 frag = (hm_fragment *)item->data;
596 assert(s->d1->handshake_read_seq == frag->msg_header.seq);
597 assert(frag->reassembly == NULL);
598
599 if (frag->msg_header.msg_len > max) {
600 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_EXCESSIVE_MESSAGE_SIZE);
601 goto err;
602 }
603
604 CBB cbb;
605 if (!BUF_MEM_grow(s->init_buf,
606 (size_t)frag->msg_header.msg_len +
607 DTLS1_HM_HEADER_LENGTH) ||
608 !CBB_init_fixed(&cbb, (uint8_t *)s->init_buf->data, s->init_buf->max)) {
609 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, ERR_R_MALLOC_FAILURE);
610 goto err;
611 }
612
613 /* Reconstruct the assembled message. */
614 size_t len;
615 if (!CBB_add_u8(&cbb, frag->msg_header.type) ||
616 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
617 !CBB_add_u16(&cbb, frag->msg_header.seq) ||
618 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
619 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
620 !CBB_add_bytes(&cbb, frag->fragment, frag->msg_header.msg_len) ||
621 !CBB_finish(&cbb, NULL, &len)) {
622 CBB_cleanup(&cbb);
623 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, ERR_R_INTERNAL_ERROR);
624 goto err;
625 }
626 assert(len == (size_t)frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH);
627
628 s->d1->handshake_read_seq++;
629
630 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
631 * |ssl_get_message| API. */
632 s->s3->tmp.message_type = frag->msg_header.type;
633 s->s3->tmp.message_size = frag->msg_header.msg_len;
634 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
635 s->init_num = frag->msg_header.msg_len;
636
637 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
David Benjaminbcb2d912015-02-24 23:45:43 -0500638 al = SSL_AD_UNEXPECTED_MESSAGE;
639 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
640 goto f_err;
641 }
David Benjamin5ca39fb2015-03-01 23:57:54 -0500642 if (hash_message == ssl_hash_message && !ssl3_hash_current_message(s)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500643 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800644 }
645 if (s->msg_callback) {
David Benjamin75381222015-03-02 19:30:30 -0500646 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
647 s->init_num + DTLS1_HM_HEADER_LENGTH, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800648 s->msg_callback_arg);
649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
David Benjamin75381222015-03-02 19:30:30 -0500651 pitem_free(item);
652 dtls1_hm_fragment_free(frag);
Adam Langley95c29f32014-06-20 12:00:00 -0700653
David Benjamin75381222015-03-02 19:30:30 -0500654 s->state = stn;
655 *ok = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800656 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700657
658f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800659 ssl3_send_alert(s, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500660err:
David Benjamin75381222015-03-02 19:30:30 -0500661 if (item != NULL) {
662 pitem_free(item);
Adam Langley71d8a082014-12-13 16:28:18 -0800663 }
David Benjamin75381222015-03-02 19:30:30 -0500664 if (frag != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800665 dtls1_hm_fragment_free(frag);
666 }
667 *ok = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800668 return -1;
669}
Adam Langley95c29f32014-06-20 12:00:00 -0700670
671/* for these 2 messages, we need to
672 * ssl->enc_read_ctx re-init
673 * ssl->s3->read_sequence zero
674 * ssl->s3->read_mac_secret re-init
675 * ssl->session->read_sym_enc assign
676 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800677 * ssl->session->read_hash assign */
678int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
679 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langley71d8a082014-12-13 16:28:18 -0800681 if (s->state == a) {
682 p = (uint8_t *)s->init_buf->data;
683 *p++ = SSL3_MT_CCS;
684 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
685 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langley71d8a082014-12-13 16:28:18 -0800687 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700688
David Benjamin16d031a2014-12-14 18:52:44 -0500689 dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
690 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700691
Adam Langley71d8a082014-12-13 16:28:18 -0800692 /* buffer the message to handle re-xmits */
693 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langley71d8a082014-12-13 16:28:18 -0800695 s->state = b;
696 }
Adam Langley95c29f32014-06-20 12:00:00 -0700697
Adam Langley71d8a082014-12-13 16:28:18 -0800698 /* SSL3_ST_CW_CHANGE_B */
David Benjamine4824e82014-12-14 19:44:34 -0500699 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC);
Adam Langley71d8a082014-12-13 16:28:18 -0800700}
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langley71d8a082014-12-13 16:28:18 -0800702int dtls1_read_failed(SSL *s, int code) {
703 if (code > 0) {
Adam Langleyd3459fb2015-02-13 14:56:28 -0800704 assert(0);
Adam Langley71d8a082014-12-13 16:28:18 -0800705 return 1;
706 }
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langley71d8a082014-12-13 16:28:18 -0800708 if (!dtls1_is_timer_expired(s)) {
709 /* not a timeout, none of our business, let higher layers handle this. In
710 * fact, it's probably an error */
711 return code;
712 }
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langley71d8a082014-12-13 16:28:18 -0800714 if (!SSL_in_init(s)) {
715 /* done, no need to send a retransmit */
716 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
717 return code;
718 }
Adam Langley95c29f32014-06-20 12:00:00 -0700719
Adam Langley71d8a082014-12-13 16:28:18 -0800720 return dtls1_handle_timeout(s);
721}
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langley71d8a082014-12-13 16:28:18 -0800723int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
724 /* The index of the retransmission queue actually is the message sequence
725 * number, since the queue only contains messages of a single handshake.
726 * However, the ChangeCipherSpec has no message sequence number and so using
727 * only the sequence will result in the CCS and Finished having the same
728 * index. To prevent this, the sequence number is multiplied by 2. In case of
729 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
730 * maintains the order of the index (important for priority queues) and fits
731 * in the unsigned short variable. */
732 return seq * 2 - is_ccs;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleybcc4e232015-02-19 15:51:58 -0800735static int dtls1_retransmit_message(SSL *s, hm_fragment *frag) {
Adam Langley71d8a082014-12-13 16:28:18 -0800736 int ret;
737 /* XDTLS: for now assuming that read/writes are blocking */
Adam Langley71d8a082014-12-13 16:28:18 -0800738 unsigned long header_length;
Adam Langley71d8a082014-12-13 16:28:18 -0800739 uint8_t save_write_sequence[8];
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Adam Langley71d8a082014-12-13 16:28:18 -0800741 /* assert(s->init_num == 0);
742 assert(s->init_off == 0); */
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langley71d8a082014-12-13 16:28:18 -0800744 if (frag->msg_header.is_ccs) {
745 header_length = DTLS1_CCS_HEADER_LENGTH;
746 } else {
747 header_length = DTLS1_HM_HEADER_LENGTH;
748 }
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langley71d8a082014-12-13 16:28:18 -0800750 memcpy(s->init_buf->data, frag->fragment,
751 frag->msg_header.msg_len + header_length);
752 s->init_num = frag->msg_header.msg_len + header_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700753
David Benjamin16d031a2014-12-14 18:52:44 -0500754 dtls1_set_message_header(s, frag->msg_header.type,
755 frag->msg_header.msg_len, frag->msg_header.seq,
756 0, frag->msg_header.frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700757
David Benjaminafbc63f2015-02-01 02:33:59 -0500758 /* Save current state. */
759 SSL_AEAD_CTX *aead_write_ctx = s->aead_write_ctx;
760 uint16_t epoch = s->d1->w_epoch;
Adam Langley95c29f32014-06-20 12:00:00 -0700761
David Benjaminafbc63f2015-02-01 02:33:59 -0500762 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
763 * (negotiated cipher) exist. */
764 assert(epoch == 0 || epoch == 1);
765 assert(frag->msg_header.epoch <= epoch);
766 const int fragment_from_previous_epoch = (epoch == 1 &&
767 frag->msg_header.epoch == 0);
768 if (fragment_from_previous_epoch) {
769 /* Rewind to the previous epoch.
770 *
771 * TODO(davidben): Instead of swapping out connection-global state, this
772 * logic should pass a "use previous epoch" parameter down to lower-level
773 * functions. */
774 s->d1->w_epoch = frag->msg_header.epoch;
775 s->aead_write_ctx = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800776 memcpy(save_write_sequence, s->s3->write_sequence,
777 sizeof(s->s3->write_sequence));
778 memcpy(s->s3->write_sequence, s->d1->last_write_sequence,
779 sizeof(s->s3->write_sequence));
David Benjaminafbc63f2015-02-01 02:33:59 -0500780 } else {
781 /* Otherwise the messages must be from the same epoch. */
782 assert(frag->msg_header.epoch == epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800783 }
784
785 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
David Benjamine4824e82014-12-14 19:44:34 -0500786 : SSL3_RT_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800787
David Benjaminafbc63f2015-02-01 02:33:59 -0500788 if (fragment_from_previous_epoch) {
789 /* Restore the current epoch. */
790 s->aead_write_ctx = aead_write_ctx;
791 s->d1->w_epoch = epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800792 memcpy(s->d1->last_write_sequence, s->s3->write_sequence,
793 sizeof(s->s3->write_sequence));
794 memcpy(s->s3->write_sequence, save_write_sequence,
795 sizeof(s->s3->write_sequence));
796 }
797
Adam Langley71d8a082014-12-13 16:28:18 -0800798 (void)BIO_flush(SSL_get_wbio(s));
799 return ret;
800}
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleybcc4e232015-02-19 15:51:58 -0800802
803int dtls1_retransmit_buffered_messages(SSL *s) {
804 pqueue sent = s->d1->sent_messages;
805 piterator iter = pqueue_iterator(sent);
806 pitem *item;
807
808 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
809 hm_fragment *frag = (hm_fragment *)item->data;
810 if (dtls1_retransmit_message(s, frag) <= 0) {
811 return -1;
812 }
813 }
814
815 return 1;
816}
817
818int dtls1_buffer_message(SSL *s, int is_ccs) {
819 pitem *item;
820 hm_fragment *frag;
821 uint8_t seq64be[8];
822
823 /* this function is called immediately after a message has
824 * been serialized */
825 assert(s->init_off == 0);
826
827 frag = dtls1_hm_fragment_new(s->init_num, 0);
828 if (!frag) {
829 return 0;
830 }
831
832 memcpy(frag->fragment, s->init_buf->data, s->init_num);
833
834 if (is_ccs) {
835 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
836 (unsigned int)s->init_num);
837 } else {
838 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
839 (unsigned int)s->init_num);
840 }
841
842 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
843 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
844 frag->msg_header.type = s->d1->w_msg_hdr.type;
845 frag->msg_header.frag_off = 0;
846 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
847 frag->msg_header.is_ccs = is_ccs;
848 frag->msg_header.epoch = s->d1->w_epoch;
849
850 memset(seq64be, 0, sizeof(seq64be));
851 seq64be[6] = (uint8_t)(
852 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
853 8);
854 seq64be[7] = (uint8_t)(
855 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
856
857 item = pitem_new(seq64be, frag);
858 if (item == NULL) {
859 dtls1_hm_fragment_free(frag);
860 return 0;
861 }
862
863 pqueue_insert(s->d1->sent_messages, item);
864 return 1;
865}
866
Adam Langley95c29f32014-06-20 12:00:00 -0700867/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -0800868void dtls1_clear_record_buffer(SSL *s) {
869 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -0700870
Adam Langley71d8a082014-12-13 16:28:18 -0800871 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
872 item = pqueue_pop(s->d1->sent_messages)) {
873 dtls1_hm_fragment_free((hm_fragment *)item->data);
874 pitem_free(item);
875 }
876}
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langley95c29f32014-06-20 12:00:00 -0700878/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin16d031a2014-12-14 18:52:44 -0500879void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
880 unsigned short seq_num, unsigned long frag_off,
881 unsigned long frag_len) {
Adam Langley71d8a082014-12-13 16:28:18 -0800882 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700883
Adam Langley71d8a082014-12-13 16:28:18 -0800884 msg_hdr->type = mt;
885 msg_hdr->msg_len = len;
886 msg_hdr->seq = seq_num;
887 msg_hdr->frag_off = frag_off;
888 msg_hdr->frag_len = frag_len;
889}
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Adam Langley71d8a082014-12-13 16:28:18 -0800891static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
892 unsigned long frag_len) {
893 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700894
Adam Langley71d8a082014-12-13 16:28:18 -0800895 msg_hdr->frag_off = frag_off;
896 msg_hdr->frag_len = frag_len;
897}
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langley71d8a082014-12-13 16:28:18 -0800899static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
900 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700901
Adam Langley71d8a082014-12-13 16:28:18 -0800902 *p++ = msg_hdr->type;
903 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700904
Adam Langley71d8a082014-12-13 16:28:18 -0800905 s2n(msg_hdr->seq, p);
906 l2n3(msg_hdr->frag_off, p);
907 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langley71d8a082014-12-13 16:28:18 -0800909 return p;
910}
Adam Langley95c29f32014-06-20 12:00:00 -0700911
Adam Langley71d8a082014-12-13 16:28:18 -0800912unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500913 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800914}
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langley71d8a082014-12-13 16:28:18 -0800916void dtls1_get_message_header(uint8_t *data,
917 struct hm_header_st *msg_hdr) {
918 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
919 msg_hdr->type = *(data++);
920 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langley71d8a082014-12-13 16:28:18 -0800922 n2s(data, msg_hdr->seq);
923 n2l3(data, msg_hdr->frag_off);
924 n2l3(data, msg_hdr->frag_len);
925}
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langley71d8a082014-12-13 16:28:18 -0800927void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) {
928 memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st));
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langley71d8a082014-12-13 16:28:18 -0800930 ccs_hdr->type = *(data++);
931}
Adam Langley95c29f32014-06-20 12:00:00 -0700932
Adam Langley71d8a082014-12-13 16:28:18 -0800933int dtls1_shutdown(SSL *s) {
934 int ret;
935 ret = ssl3_shutdown(s);
936 return ret;
937}