blob: 9fa46b1383418d3ebabe58f2f2973bd056e53d1f [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
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700128
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
David Benjaminc99807d2015-09-12 18:21:35 -0400150static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly) {
151 hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment));
Adam Langley71d8a082014-12-13 16:28:18 -0800152 if (frag == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400153 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800154 return NULL;
155 }
David Benjaminc99807d2015-09-12 18:21:35 -0400156 memset(frag, 0, sizeof(hm_fragment));
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjaminc99807d2015-09-12 18:21:35 -0400158 /* If the handshake message is empty, |frag->fragment| and |frag->reassembly|
159 * are NULL. */
160 if (frag_len > 0) {
161 frag->fragment = OPENSSL_malloc(frag_len);
162 if (frag->fragment == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400163 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminc99807d2015-09-12 18:21:35 -0400164 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjaminc99807d2015-09-12 18:21:35 -0400167 if (reassembly) {
168 /* Initialize reassembly bitmask. */
169 if (frag_len + 7 < frag_len) {
170 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
171 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800172 }
David Benjaminc99807d2015-09-12 18:21:35 -0400173 size_t bitmask_len = (frag_len + 7) / 8;
174 frag->reassembly = OPENSSL_malloc(bitmask_len);
175 if (frag->reassembly == NULL) {
176 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
177 goto err;
178 }
179 memset(frag->reassembly, 0, bitmask_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800180 }
Adam Langley71d8a082014-12-13 16:28:18 -0800181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langley71d8a082014-12-13 16:28:18 -0800183 return frag;
David Benjaminc99807d2015-09-12 18:21:35 -0400184
185err:
186 dtls1_hm_fragment_free(frag);
187 return NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800188}
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley71d8a082014-12-13 16:28:18 -0800190void dtls1_hm_fragment_free(hm_fragment *frag) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400191 if (frag == NULL) {
192 return;
193 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400194 OPENSSL_free(frag->fragment);
195 OPENSSL_free(frag->reassembly);
Adam Langley71d8a082014-12-13 16:28:18 -0800196 OPENSSL_free(frag);
197}
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjaminee562b92015-03-02 20:52:52 -0500199#if !defined(inline)
200#define inline __inline
201#endif
202
203/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
204 * exclusive, set. */
205static inline uint8_t bit_range(size_t start, size_t end) {
206 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
207}
208
209/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
210 * as received in |frag|. If |frag| becomes complete, it clears
211 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
212 * and |frag->reassembly| must not be NULL. */
213static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
214 size_t end) {
215 size_t i;
216 size_t msg_len = frag->msg_header.msg_len;
217
218 if (frag->reassembly == NULL || start > end || end > msg_len) {
219 assert(0);
220 return;
221 }
222 /* A zero-length message will never have a pending reassembly. */
223 assert(msg_len > 0);
224
225 if ((start >> 3) == (end >> 3)) {
226 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
227 } else {
228 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
229 for (i = (start >> 3) + 1; i < (end >> 3); i++) {
230 frag->reassembly[i] = 0xff;
231 }
232 if ((end & 7) != 0) {
233 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
234 }
235 }
236
237 /* Check if the fragment is complete. */
238 for (i = 0; i < (msg_len >> 3); i++) {
239 if (frag->reassembly[i] != 0xff) {
240 return;
241 }
242 }
243 if ((msg_len & 7) != 0 &&
244 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
245 return;
246 }
247
248 OPENSSL_free(frag->reassembly);
249 frag->reassembly = NULL;
250}
251
Adam Langley71d8a082014-12-13 16:28:18 -0800252/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
253 * SSL3_RT_CHANGE_CIPHER_SPEC) */
David Benjamin3e3090d2015-04-05 12:48:30 -0400254int dtls1_do_write(SSL *s, int type, enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800255 int ret;
256 int curr_mtu;
David Benjamine95d20d2014-12-23 11:16:01 -0500257 unsigned int len, frag_off;
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley71d8a082014-12-13 16:28:18 -0800259 /* AHA! Figure out the MTU, and stick to the right size */
260 if (s->d1->mtu < dtls1_min_mtu() &&
261 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500262 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
263 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
264 s->d1->mtu = (unsigned)mtu;
265 } else {
David Benjamina18b6712015-01-11 19:31:22 -0500266 s->d1->mtu = kDefaultMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800267 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL);
268 }
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langley71d8a082014-12-13 16:28:18 -0800271 /* should have something reasonable now */
272 assert(s->d1->mtu >= dtls1_min_mtu());
Adam Langley95c29f32014-06-20 12:00:00 -0700273
Adam Langley71d8a082014-12-13 16:28:18 -0800274 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
275 assert(s->init_num ==
276 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
David Benjamine95d20d2014-12-23 11:16:01 -0500279 /* Determine the maximum overhead of the current cipher. */
David Benjamin31a07792015-03-03 14:20:26 -0500280 size_t max_overhead = SSL_AEAD_CTX_max_overhead(s->aead_write_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 frag_off = 0;
283 while (s->init_num) {
David Benjamin17a5f852015-01-11 19:46:52 -0500284 /* Account for data in the buffering BIO; multiple records may be packed
285 * into a single packet during the handshake.
286 *
287 * TODO(davidben): This is buggy; if the MTU is larger than the buffer size,
288 * the large record will be split across two packets. Moreover, in that
289 * case, the |dtls1_write_bytes| call may not return synchronously. This
290 * will break on retry as the |s->init_off| and |s->init_num| adjustment
291 * will run a second time. */
Adam Langley71d8a082014-12-13 16:28:18 -0800292 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
David Benjamine95d20d2014-12-23 11:16:01 -0500293 DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langley71d8a082014-12-13 16:28:18 -0800295 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
David Benjamin17a5f852015-01-11 19:46:52 -0500296 /* Flush the buffer and continue with a fresh packet.
297 *
298 * TODO(davidben): If |BIO_flush| is not synchronous and requires multiple
299 * calls to |dtls1_do_write|, |frag_off| will be wrong. */
Adam Langley71d8a082014-12-13 16:28:18 -0800300 ret = BIO_flush(SSL_get_wbio(s));
301 if (ret <= 0) {
302 return ret;
303 }
David Benjamin17a5f852015-01-11 19:46:52 -0500304 assert(BIO_wpending(SSL_get_wbio(s)) == 0);
David Benjamine95d20d2014-12-23 11:16:01 -0500305 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley71d8a082014-12-13 16:28:18 -0800306 }
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley71d8a082014-12-13 16:28:18 -0800308 /* XDTLS: this function is too long. split out the CCS part */
309 if (type == SSL3_RT_HANDSHAKE) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500310 /* If this isn't the first fragment, reserve space to prepend a new
311 * fragment header. This will override the body of a previous fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800312 if (s->init_off != 0) {
313 assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
314 s->init_off -= DTLS1_HM_HEADER_LENGTH;
315 s->init_num += DTLS1_HM_HEADER_LENGTH;
Adam Langley71d8a082014-12-13 16:28:18 -0800316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
David Benjamind9778fb2015-01-11 17:24:51 -0500318 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
319 /* To make forward progress, the MTU must, at minimum, fit the handshake
320 * header and one byte of handshake body. */
David Benjamin3570d732015-06-29 00:28:17 -0400321 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamind9778fb2015-01-11 17:24:51 -0500322 return -1;
323 }
Adam Langley95c29f32014-06-20 12:00:00 -0700324
David Benjamind9778fb2015-01-11 17:24:51 -0500325 if (s->init_num > curr_mtu) {
326 len = curr_mtu;
327 } else {
328 len = s->init_num;
329 }
330 assert(len >= DTLS1_HM_HEADER_LENGTH);
331
332 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800333 dtls1_write_message_header(
334 s, (uint8_t *)&s->init_buf->data[s->init_off]);
David Benjamind9778fb2015-01-11 17:24:51 -0500335 } else {
336 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
337 /* ChangeCipherSpec cannot be fragmented. */
338 if (s->init_num > curr_mtu) {
David Benjamin3570d732015-06-29 00:28:17 -0400339 OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL);
David Benjamind9778fb2015-01-11 17:24:51 -0500340 return -1;
341 }
342 len = s->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800343 }
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin3e3090d2015-04-05 12:48:30 -0400345 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,
346 use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800347 if (ret < 0) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500348 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800349 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500350
351 /* bad if this assert fails, only part of the handshake message got sent.
352 * But why would this happen? */
353 assert(len == (unsigned int)ret);
354
355 if (ret == s->init_num) {
356 if (s->msg_callback) {
357 s->msg_callback(1, s->version, type, s->init_buf->data,
358 (size_t)(s->init_off + s->init_num), s,
359 s->msg_callback_arg);
360 }
361
362 s->init_off = 0; /* done writing this message */
363 s->init_num = 0;
364
365 return 1;
366 }
367 s->init_off += ret;
368 s->init_num -= ret;
369 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800370 }
371
372 return 0;
373}
Adam Langley95c29f32014-06-20 12:00:00 -0700374
David Benjamin75381222015-03-02 19:30:30 -0500375/* dtls1_is_next_message_complete returns one if the next handshake message is
376 * complete and zero otherwise. */
377static int dtls1_is_next_message_complete(SSL *s) {
378 pitem *item = pqueue_peek(s->d1->buffered_messages);
379 if (item == NULL) {
380 return 0;
381 }
382
383 hm_fragment *frag = (hm_fragment *)item->data;
384 assert(s->d1->handshake_read_seq <= frag->msg_header.seq);
385
386 return s->d1->handshake_read_seq == frag->msg_header.seq &&
387 frag->reassembly == NULL;
388}
389
390/* dtls1_discard_fragment_body discards a handshake fragment body of length
391 * |frag_len|. It returns one on success and zero on error.
392 *
393 * TODO(davidben): This function will go away when ssl_read_bytes is gone from
394 * the DTLS side. */
395static int dtls1_discard_fragment_body(SSL *s, size_t frag_len) {
396 uint8_t discard[256];
397 while (frag_len > 0) {
398 size_t chunk = frag_len < sizeof(discard) ? frag_len : sizeof(discard);
David Benjamina6022772015-05-30 16:22:10 -0400399 int ret = dtls1_read_bytes(s, SSL3_RT_HANDSHAKE, discard, chunk, 0);
David Benjamin75381222015-03-02 19:30:30 -0500400 if (ret != chunk) {
401 return 0;
402 }
403 frag_len -= chunk;
404 }
405 return 1;
406}
407
408/* dtls1_get_buffered_message returns the buffered message corresponding to
409 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
410 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
411 * returns NULL on failure. The caller does not take ownership of the result. */
412static hm_fragment *dtls1_get_buffered_message(
413 SSL *s, const struct hm_header_st *msg_hdr) {
414 uint8_t seq64be[8];
415 memset(seq64be, 0, sizeof(seq64be));
416 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
417 seq64be[7] = (uint8_t)msg_hdr->seq;
418 pitem *item = pqueue_find(s->d1->buffered_messages, seq64be);
419
420 hm_fragment *frag;
421 if (item == NULL) {
422 /* This is the first fragment from this message. */
423 frag = dtls1_hm_fragment_new(msg_hdr->msg_len,
424 1 /* reassembly buffer needed */);
425 if (frag == NULL) {
426 return NULL;
427 }
428 memcpy(&frag->msg_header, msg_hdr, sizeof(*msg_hdr));
429 item = pitem_new(seq64be, frag);
430 if (item == NULL) {
431 dtls1_hm_fragment_free(frag);
432 return NULL;
433 }
434 item = pqueue_insert(s->d1->buffered_messages, item);
435 /* |pqueue_insert| fails iff a duplicate item is inserted, but |item| cannot
436 * be a duplicate. */
437 assert(item != NULL);
438 } else {
439 frag = item->data;
440 assert(frag->msg_header.seq == msg_hdr->seq);
441 if (frag->msg_header.type != msg_hdr->type ||
442 frag->msg_header.msg_len != msg_hdr->msg_len) {
443 /* The new fragment must be compatible with the previous fragments from
444 * this message. */
David Benjamin3570d732015-06-29 00:28:17 -0400445 OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH);
David Benjamin75381222015-03-02 19:30:30 -0500446 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
447 return NULL;
448 }
449 }
450 return frag;
451}
452
453/* dtls1_max_handshake_message_len returns the maximum number of bytes
454 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
455 * be greater if the maximum certificate list size requires it. */
456static size_t dtls1_max_handshake_message_len(const SSL *s) {
457 size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
David Benjamin1d0a1942015-04-26 15:35:35 -0400458 if (max_len < s->max_cert_list) {
459 return s->max_cert_list;
David Benjamin75381222015-03-02 19:30:30 -0500460 }
461 return max_len;
462}
463
464/* dtls1_process_fragment reads a handshake fragment and processes it. It
465 * returns one if a fragment was successfully processed and 0 or -1 on error. */
466static int dtls1_process_fragment(SSL *s) {
David Benjamin11fc66a2015-06-16 11:40:24 -0400467 /* Read handshake message header. */
David Benjamin75381222015-03-02 19:30:30 -0500468 uint8_t header[DTLS1_HM_HEADER_LENGTH];
David Benjamina6022772015-05-30 16:22:10 -0400469 int ret = dtls1_read_bytes(s, SSL3_RT_HANDSHAKE, header,
470 DTLS1_HM_HEADER_LENGTH, 0);
David Benjamin75381222015-03-02 19:30:30 -0500471 if (ret <= 0) {
David Benjamin75381222015-03-02 19:30:30 -0500472 return ret;
473 }
474 if (ret != DTLS1_HM_HEADER_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400475 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin75381222015-03-02 19:30:30 -0500476 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
477 return -1;
478 }
479
480 /* Parse the message fragment header. */
481 struct hm_header_st msg_hdr;
482 dtls1_get_message_header(header, &msg_hdr);
483
David Benjamin11fc66a2015-06-16 11:40:24 -0400484 /* TODO(davidben): dtls1_read_bytes is the wrong abstraction for DTLS. There
485 * should be no need to reach into |s->s3->rrec.length|. */
David Benjamin75381222015-03-02 19:30:30 -0500486 const size_t frag_off = msg_hdr.frag_off;
487 const size_t frag_len = msg_hdr.frag_len;
488 const size_t msg_len = msg_hdr.msg_len;
489 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
490 frag_off + frag_len > msg_len ||
David Benjamin11fc66a2015-06-16 11:40:24 -0400491 msg_len > dtls1_max_handshake_message_len(s) ||
492 frag_len > s->s3->rrec.length) {
David Benjamin3570d732015-06-29 00:28:17 -0400493 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
David Benjamin75381222015-03-02 19:30:30 -0500494 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
495 return -1;
496 }
497
498 if (msg_hdr.seq < s->d1->handshake_read_seq ||
499 msg_hdr.seq > (unsigned)s->d1->handshake_read_seq +
500 kHandshakeBufferSize) {
501 /* Ignore fragments from the past, or ones too far in the future. */
502 if (!dtls1_discard_fragment_body(s, frag_len)) {
503 return -1;
504 }
505 return 1;
506 }
507
508 hm_fragment *frag = dtls1_get_buffered_message(s, &msg_hdr);
509 if (frag == NULL) {
510 return -1;
511 }
512 assert(frag->msg_header.msg_len == msg_len);
513
514 if (frag->reassembly == NULL) {
515 /* The message is already assembled. */
516 if (!dtls1_discard_fragment_body(s, frag_len)) {
517 return -1;
518 }
519 return 1;
520 }
521 assert(msg_len > 0);
522
523 /* Read the body of the fragment. */
David Benjamina6022772015-05-30 16:22:10 -0400524 ret = dtls1_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment + frag_off,
525 frag_len, 0);
David Benjamin75381222015-03-02 19:30:30 -0500526 if (ret != frag_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400527 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin11fc66a2015-06-16 11:40:24 -0400528 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin75381222015-03-02 19:30:30 -0500529 return -1;
530 }
David Benjaminee562b92015-03-02 20:52:52 -0500531 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
David Benjamin75381222015-03-02 19:30:30 -0500532
David Benjamin75381222015-03-02 19:30:30 -0500533 return 1;
534}
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjaminbcb2d912015-02-24 23:45:43 -0500536/* dtls1_get_message reads a handshake message of message type |msg_type| (any
537 * if |msg_type| == -1), maximum acceptable body length |max|. Read an entire
538 * handshake message. Handshake messages arrive in fragments. */
539long dtls1_get_message(SSL *s, int st1, int stn, int msg_type, long max,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500540 enum ssl_hash_message_t hash_message, int *ok) {
David Benjamin75381222015-03-02 19:30:30 -0500541 pitem *item = NULL;
542 hm_fragment *frag = NULL;
543 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley71d8a082014-12-13 16:28:18 -0800545 /* s3->tmp is used to store messages that are unexpected, caused
546 * by the absence of an optional handshake message */
547 if (s->s3->tmp.reuse_message) {
David Benjamin5ca39fb2015-03-01 23:57:54 -0500548 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
549 * ssl_dont_hash_message would have to have been applied to the previous
550 * call. */
551 assert(hash_message == ssl_hash_message);
Adam Langley71d8a082014-12-13 16:28:18 -0800552 s->s3->tmp.reuse_message = 0;
David Benjaminbcb2d912015-02-24 23:45:43 -0500553 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800554 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400555 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley71d8a082014-12-13 16:28:18 -0800556 goto f_err;
557 }
558 *ok = 1;
559 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
560 s->init_num = (int)s->s3->tmp.message_size;
561 return s->init_num;
562 }
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamin75381222015-03-02 19:30:30 -0500564 /* Process fragments until one is found. */
565 while (!dtls1_is_next_message_complete(s)) {
566 int ret = dtls1_process_fragment(s);
567 if (ret <= 0) {
568 *ok = 0;
569 return ret;
570 }
Adam Langley71d8a082014-12-13 16:28:18 -0800571 }
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamin75381222015-03-02 19:30:30 -0500573 /* Read out the next complete handshake message. */
574 item = pqueue_pop(s->d1->buffered_messages);
575 assert(item != NULL);
576 frag = (hm_fragment *)item->data;
577 assert(s->d1->handshake_read_seq == frag->msg_header.seq);
578 assert(frag->reassembly == NULL);
579
David Benjaminbf0df922015-03-10 00:02:26 -0400580 if (frag->msg_header.msg_len > (size_t)max) {
David Benjamin3570d732015-06-29 00:28:17 -0400581 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
David Benjamin75381222015-03-02 19:30:30 -0500582 goto err;
583 }
584
David Benjamina8653202015-06-28 01:26:10 -0400585 /* Reconstruct the assembled message. */
586 size_t len;
David Benjamin75381222015-03-02 19:30:30 -0500587 CBB cbb;
David Benjamina8653202015-06-28 01:26:10 -0400588 CBB_zero(&cbb);
David Benjamin75381222015-03-02 19:30:30 -0500589 if (!BUF_MEM_grow(s->init_buf,
590 (size_t)frag->msg_header.msg_len +
591 DTLS1_HM_HEADER_LENGTH) ||
David Benjamina8653202015-06-28 01:26:10 -0400592 !CBB_init_fixed(&cbb, (uint8_t *)s->init_buf->data, s->init_buf->max) ||
593 !CBB_add_u8(&cbb, frag->msg_header.type) ||
David Benjamin75381222015-03-02 19:30:30 -0500594 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
595 !CBB_add_u16(&cbb, frag->msg_header.seq) ||
596 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
597 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
598 !CBB_add_bytes(&cbb, frag->fragment, frag->msg_header.msg_len) ||
599 !CBB_finish(&cbb, NULL, &len)) {
600 CBB_cleanup(&cbb);
David Benjamin3570d732015-06-29 00:28:17 -0400601 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin75381222015-03-02 19:30:30 -0500602 goto err;
603 }
604 assert(len == (size_t)frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH);
605
606 s->d1->handshake_read_seq++;
607
608 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
609 * |ssl_get_message| API. */
610 s->s3->tmp.message_type = frag->msg_header.type;
611 s->s3->tmp.message_size = frag->msg_header.msg_len;
612 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
613 s->init_num = frag->msg_header.msg_len;
614
615 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
David Benjaminbcb2d912015-02-24 23:45:43 -0500616 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400617 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminbcb2d912015-02-24 23:45:43 -0500618 goto f_err;
619 }
David Benjamin5ca39fb2015-03-01 23:57:54 -0500620 if (hash_message == ssl_hash_message && !ssl3_hash_current_message(s)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500621 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800622 }
623 if (s->msg_callback) {
David Benjamin75381222015-03-02 19:30:30 -0500624 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
625 s->init_num + DTLS1_HM_HEADER_LENGTH, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800626 s->msg_callback_arg);
627 }
Adam Langley95c29f32014-06-20 12:00:00 -0700628
David Benjamin75381222015-03-02 19:30:30 -0500629 pitem_free(item);
630 dtls1_hm_fragment_free(frag);
Adam Langley95c29f32014-06-20 12:00:00 -0700631
David Benjamin75381222015-03-02 19:30:30 -0500632 s->state = stn;
633 *ok = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800634 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
636f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800637 ssl3_send_alert(s, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500638err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400639 pitem_free(item);
640 dtls1_hm_fragment_free(frag);
Adam Langley71d8a082014-12-13 16:28:18 -0800641 *ok = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800642 return -1;
643}
Adam Langley95c29f32014-06-20 12:00:00 -0700644
645/* for these 2 messages, we need to
646 * ssl->enc_read_ctx re-init
647 * ssl->s3->read_sequence zero
648 * ssl->s3->read_mac_secret re-init
649 * ssl->session->read_sym_enc assign
650 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800651 * ssl->session->read_hash assign */
652int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
653 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langley71d8a082014-12-13 16:28:18 -0800655 if (s->state == a) {
656 p = (uint8_t *)s->init_buf->data;
657 *p++ = SSL3_MT_CCS;
658 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
659 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700660
Adam Langley71d8a082014-12-13 16:28:18 -0800661 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700662
David Benjamin16d031a2014-12-14 18:52:44 -0500663 dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
664 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langley71d8a082014-12-13 16:28:18 -0800666 /* buffer the message to handle re-xmits */
667 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langley71d8a082014-12-13 16:28:18 -0800669 s->state = b;
670 }
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langley71d8a082014-12-13 16:28:18 -0800672 /* SSL3_ST_CW_CHANGE_B */
David Benjamin3e3090d2015-04-05 12:48:30 -0400673 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC, dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800674}
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langley71d8a082014-12-13 16:28:18 -0800676int dtls1_read_failed(SSL *s, int code) {
677 if (code > 0) {
Adam Langleyd3459fb2015-02-13 14:56:28 -0800678 assert(0);
Adam Langley71d8a082014-12-13 16:28:18 -0800679 return 1;
680 }
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langley71d8a082014-12-13 16:28:18 -0800682 if (!dtls1_is_timer_expired(s)) {
683 /* not a timeout, none of our business, let higher layers handle this. In
684 * fact, it's probably an error */
685 return code;
686 }
Adam Langley95c29f32014-06-20 12:00:00 -0700687
Adam Langley71d8a082014-12-13 16:28:18 -0800688 if (!SSL_in_init(s)) {
689 /* done, no need to send a retransmit */
690 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
691 return code;
692 }
Adam Langley95c29f32014-06-20 12:00:00 -0700693
David Benjamin8c249802015-05-05 09:44:18 -0400694 return DTLSv1_handle_timeout(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800695}
Adam Langley95c29f32014-06-20 12:00:00 -0700696
Adam Langley71d8a082014-12-13 16:28:18 -0800697int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
698 /* The index of the retransmission queue actually is the message sequence
699 * number, since the queue only contains messages of a single handshake.
700 * However, the ChangeCipherSpec has no message sequence number and so using
701 * only the sequence will result in the CCS and Finished having the same
702 * index. To prevent this, the sequence number is multiplied by 2. In case of
703 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
704 * maintains the order of the index (important for priority queues) and fits
705 * in the unsigned short variable. */
706 return seq * 2 - is_ccs;
707}
Adam Langley95c29f32014-06-20 12:00:00 -0700708
Adam Langleybcc4e232015-02-19 15:51:58 -0800709static int dtls1_retransmit_message(SSL *s, hm_fragment *frag) {
Adam Langley71d8a082014-12-13 16:28:18 -0800710 int ret;
711 /* XDTLS: for now assuming that read/writes are blocking */
Adam Langley71d8a082014-12-13 16:28:18 -0800712 unsigned long header_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700713
Adam Langley71d8a082014-12-13 16:28:18 -0800714 /* assert(s->init_num == 0);
715 assert(s->init_off == 0); */
Adam Langley95c29f32014-06-20 12:00:00 -0700716
Adam Langley71d8a082014-12-13 16:28:18 -0800717 if (frag->msg_header.is_ccs) {
718 header_length = DTLS1_CCS_HEADER_LENGTH;
719 } else {
720 header_length = DTLS1_HM_HEADER_LENGTH;
721 }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
Adam Langley71d8a082014-12-13 16:28:18 -0800723 memcpy(s->init_buf->data, frag->fragment,
724 frag->msg_header.msg_len + header_length);
725 s->init_num = frag->msg_header.msg_len + header_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700726
David Benjamin16d031a2014-12-14 18:52:44 -0500727 dtls1_set_message_header(s, frag->msg_header.type,
728 frag->msg_header.msg_len, frag->msg_header.seq,
729 0, frag->msg_header.frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700730
David Benjaminafbc63f2015-02-01 02:33:59 -0500731 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
732 * (negotiated cipher) exist. */
David Benjamin3e3090d2015-04-05 12:48:30 -0400733 assert(s->d1->w_epoch == 0 || s->d1->w_epoch == 1);
734 assert(frag->msg_header.epoch <= s->d1->w_epoch);
735 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
736 if (s->d1->w_epoch == 1 && frag->msg_header.epoch == 0) {
737 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800738 }
739
740 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
David Benjamin3e3090d2015-04-05 12:48:30 -0400741 : SSL3_RT_HANDSHAKE,
742 use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800743
Adam Langley71d8a082014-12-13 16:28:18 -0800744 (void)BIO_flush(SSL_get_wbio(s));
745 return ret;
746}
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langleybcc4e232015-02-19 15:51:58 -0800748
749int dtls1_retransmit_buffered_messages(SSL *s) {
750 pqueue sent = s->d1->sent_messages;
751 piterator iter = pqueue_iterator(sent);
752 pitem *item;
753
754 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
755 hm_fragment *frag = (hm_fragment *)item->data;
756 if (dtls1_retransmit_message(s, frag) <= 0) {
757 return -1;
758 }
759 }
760
761 return 1;
762}
763
764int dtls1_buffer_message(SSL *s, int is_ccs) {
765 pitem *item;
766 hm_fragment *frag;
767 uint8_t seq64be[8];
768
769 /* this function is called immediately after a message has
770 * been serialized */
771 assert(s->init_off == 0);
772
773 frag = dtls1_hm_fragment_new(s->init_num, 0);
774 if (!frag) {
775 return 0;
776 }
777
778 memcpy(frag->fragment, s->init_buf->data, s->init_num);
779
780 if (is_ccs) {
781 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
782 (unsigned int)s->init_num);
783 } else {
784 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
785 (unsigned int)s->init_num);
786 }
787
788 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
789 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
790 frag->msg_header.type = s->d1->w_msg_hdr.type;
791 frag->msg_header.frag_off = 0;
792 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
793 frag->msg_header.is_ccs = is_ccs;
794 frag->msg_header.epoch = s->d1->w_epoch;
795
796 memset(seq64be, 0, sizeof(seq64be));
797 seq64be[6] = (uint8_t)(
798 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
799 8);
800 seq64be[7] = (uint8_t)(
801 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
802
803 item = pitem_new(seq64be, frag);
804 if (item == NULL) {
805 dtls1_hm_fragment_free(frag);
806 return 0;
807 }
808
809 pqueue_insert(s->d1->sent_messages, item);
810 return 1;
811}
812
Adam Langley95c29f32014-06-20 12:00:00 -0700813/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -0800814void dtls1_clear_record_buffer(SSL *s) {
815 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langley71d8a082014-12-13 16:28:18 -0800817 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
818 item = pqueue_pop(s->d1->sent_messages)) {
819 dtls1_hm_fragment_free((hm_fragment *)item->data);
820 pitem_free(item);
821 }
822}
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langley95c29f32014-06-20 12:00:00 -0700824/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin16d031a2014-12-14 18:52:44 -0500825void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
826 unsigned short seq_num, unsigned long frag_off,
827 unsigned long frag_len) {
Adam Langley71d8a082014-12-13 16:28:18 -0800828 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langley71d8a082014-12-13 16:28:18 -0800830 msg_hdr->type = mt;
831 msg_hdr->msg_len = len;
832 msg_hdr->seq = seq_num;
833 msg_hdr->frag_off = frag_off;
834 msg_hdr->frag_len = frag_len;
835}
Adam Langley95c29f32014-06-20 12:00:00 -0700836
Adam Langley71d8a082014-12-13 16:28:18 -0800837static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
838 unsigned long frag_len) {
839 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langley71d8a082014-12-13 16:28:18 -0800841 msg_hdr->frag_off = frag_off;
842 msg_hdr->frag_len = frag_len;
843}
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langley71d8a082014-12-13 16:28:18 -0800845static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
846 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langley71d8a082014-12-13 16:28:18 -0800848 *p++ = msg_hdr->type;
849 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700850
Adam Langley71d8a082014-12-13 16:28:18 -0800851 s2n(msg_hdr->seq, p);
852 l2n3(msg_hdr->frag_off, p);
853 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langley71d8a082014-12-13 16:28:18 -0800855 return p;
856}
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langley71d8a082014-12-13 16:28:18 -0800858unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500859 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800860}
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langley71d8a082014-12-13 16:28:18 -0800862void dtls1_get_message_header(uint8_t *data,
863 struct hm_header_st *msg_hdr) {
864 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
865 msg_hdr->type = *(data++);
866 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700867
Adam Langley71d8a082014-12-13 16:28:18 -0800868 n2s(data, msg_hdr->seq);
869 n2l3(data, msg_hdr->frag_off);
870 n2l3(data, msg_hdr->frag_len);
871}