blob: 3eb26c520015a249af4f70949e5e4b95228fb669 [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
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) {
David Benjamin5d1ec732015-04-22 13:38:00 -0400199 if (frag == NULL) {
200 return;
201 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400202 OPENSSL_free(frag->fragment);
203 OPENSSL_free(frag->reassembly);
Adam Langley71d8a082014-12-13 16:28:18 -0800204 OPENSSL_free(frag);
205}
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjaminee562b92015-03-02 20:52:52 -0500207#if !defined(inline)
208#define inline __inline
209#endif
210
211/* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|,
212 * exclusive, set. */
213static inline uint8_t bit_range(size_t start, size_t end) {
214 return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1));
215}
216
217/* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive,
218 * as received in |frag|. If |frag| becomes complete, it clears
219 * |frag->reassembly|. The range must be within the bounds of |frag|'s message
220 * and |frag->reassembly| must not be NULL. */
221static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start,
222 size_t end) {
223 size_t i;
224 size_t msg_len = frag->msg_header.msg_len;
225
226 if (frag->reassembly == NULL || start > end || end > msg_len) {
227 assert(0);
228 return;
229 }
230 /* A zero-length message will never have a pending reassembly. */
231 assert(msg_len > 0);
232
233 if ((start >> 3) == (end >> 3)) {
234 frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7);
235 } else {
236 frag->reassembly[start >> 3] |= bit_range(start & 7, 8);
237 for (i = (start >> 3) + 1; i < (end >> 3); i++) {
238 frag->reassembly[i] = 0xff;
239 }
240 if ((end & 7) != 0) {
241 frag->reassembly[end >> 3] |= bit_range(0, end & 7);
242 }
243 }
244
245 /* Check if the fragment is complete. */
246 for (i = 0; i < (msg_len >> 3); i++) {
247 if (frag->reassembly[i] != 0xff) {
248 return;
249 }
250 }
251 if ((msg_len & 7) != 0 &&
252 frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) {
253 return;
254 }
255
256 OPENSSL_free(frag->reassembly);
257 frag->reassembly = NULL;
258}
259
Adam Langley71d8a082014-12-13 16:28:18 -0800260/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
261 * SSL3_RT_CHANGE_CIPHER_SPEC) */
David Benjamin3e3090d2015-04-05 12:48:30 -0400262int dtls1_do_write(SSL *s, int type, enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800263 int ret;
264 int curr_mtu;
David Benjamine95d20d2014-12-23 11:16:01 -0500265 unsigned int len, frag_off;
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langley71d8a082014-12-13 16:28:18 -0800267 /* AHA! Figure out the MTU, and stick to the right size */
268 if (s->d1->mtu < dtls1_min_mtu() &&
269 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
David Benjamin80cee912015-01-11 19:36:58 -0500270 long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
271 if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
272 s->d1->mtu = (unsigned)mtu;
273 } else {
David Benjamina18b6712015-01-11 19:31:22 -0500274 s->d1->mtu = kDefaultMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800275 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL);
276 }
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langley71d8a082014-12-13 16:28:18 -0800279 /* should have something reasonable now */
280 assert(s->d1->mtu >= dtls1_min_mtu());
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
283 assert(s->init_num ==
284 (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286
David Benjamine95d20d2014-12-23 11:16:01 -0500287 /* Determine the maximum overhead of the current cipher. */
David Benjamin31a07792015-03-03 14:20:26 -0500288 size_t max_overhead = SSL_AEAD_CTX_max_overhead(s->aead_write_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley71d8a082014-12-13 16:28:18 -0800290 frag_off = 0;
291 while (s->init_num) {
David Benjamin17a5f852015-01-11 19:46:52 -0500292 /* Account for data in the buffering BIO; multiple records may be packed
293 * into a single packet during the handshake.
294 *
295 * TODO(davidben): This is buggy; if the MTU is larger than the buffer size,
296 * the large record will be split across two packets. Moreover, in that
297 * case, the |dtls1_write_bytes| call may not return synchronously. This
298 * will break on retry as the |s->init_off| and |s->init_num| adjustment
299 * will run a second time. */
Adam Langley71d8a082014-12-13 16:28:18 -0800300 curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) -
David Benjamine95d20d2014-12-23 11:16:01 -0500301 DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langley71d8a082014-12-13 16:28:18 -0800303 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
David Benjamin17a5f852015-01-11 19:46:52 -0500304 /* Flush the buffer and continue with a fresh packet.
305 *
306 * TODO(davidben): If |BIO_flush| is not synchronous and requires multiple
307 * calls to |dtls1_do_write|, |frag_off| will be wrong. */
Adam Langley71d8a082014-12-13 16:28:18 -0800308 ret = BIO_flush(SSL_get_wbio(s));
309 if (ret <= 0) {
310 return ret;
311 }
David Benjamin17a5f852015-01-11 19:46:52 -0500312 assert(BIO_wpending(SSL_get_wbio(s)) == 0);
David Benjamine95d20d2014-12-23 11:16:01 -0500313 curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead;
Adam Langley71d8a082014-12-13 16:28:18 -0800314 }
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Adam Langley71d8a082014-12-13 16:28:18 -0800316 /* XDTLS: this function is too long. split out the CCS part */
317 if (type == SSL3_RT_HANDSHAKE) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500318 /* If this isn't the first fragment, reserve space to prepend a new
319 * fragment header. This will override the body of a previous fragment. */
Adam Langley71d8a082014-12-13 16:28:18 -0800320 if (s->init_off != 0) {
321 assert(s->init_off > DTLS1_HM_HEADER_LENGTH);
322 s->init_off -= DTLS1_HM_HEADER_LENGTH;
323 s->init_num += DTLS1_HM_HEADER_LENGTH;
Adam Langley71d8a082014-12-13 16:28:18 -0800324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325
David Benjamind9778fb2015-01-11 17:24:51 -0500326 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
327 /* To make forward progress, the MTU must, at minimum, fit the handshake
328 * header and one byte of handshake body. */
329 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
330 return -1;
331 }
Adam Langley95c29f32014-06-20 12:00:00 -0700332
David Benjamind9778fb2015-01-11 17:24:51 -0500333 if (s->init_num > curr_mtu) {
334 len = curr_mtu;
335 } else {
336 len = s->init_num;
337 }
338 assert(len >= DTLS1_HM_HEADER_LENGTH);
339
340 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800341 dtls1_write_message_header(
342 s, (uint8_t *)&s->init_buf->data[s->init_off]);
David Benjamind9778fb2015-01-11 17:24:51 -0500343 } else {
344 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
345 /* ChangeCipherSpec cannot be fragmented. */
346 if (s->init_num > curr_mtu) {
347 OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL);
348 return -1;
349 }
350 len = s->init_num;
Adam Langley71d8a082014-12-13 16:28:18 -0800351 }
Adam Langley95c29f32014-06-20 12:00:00 -0700352
David Benjamin3e3090d2015-04-05 12:48:30 -0400353 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,
354 use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800355 if (ret < 0) {
David Benjamin5a3cc032015-01-11 19:25:32 -0500356 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800357 }
David Benjamin5a3cc032015-01-11 19:25:32 -0500358
359 /* bad if this assert fails, only part of the handshake message got sent.
360 * But why would this happen? */
361 assert(len == (unsigned int)ret);
362
363 if (ret == s->init_num) {
364 if (s->msg_callback) {
365 s->msg_callback(1, s->version, type, s->init_buf->data,
366 (size_t)(s->init_off + s->init_num), s,
367 s->msg_callback_arg);
368 }
369
370 s->init_off = 0; /* done writing this message */
371 s->init_num = 0;
372
373 return 1;
374 }
375 s->init_off += ret;
376 s->init_num -= ret;
377 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH);
Adam Langley71d8a082014-12-13 16:28:18 -0800378 }
379
380 return 0;
381}
Adam Langley95c29f32014-06-20 12:00:00 -0700382
David Benjamin75381222015-03-02 19:30:30 -0500383/* dtls1_is_next_message_complete returns one if the next handshake message is
384 * complete and zero otherwise. */
385static int dtls1_is_next_message_complete(SSL *s) {
386 pitem *item = pqueue_peek(s->d1->buffered_messages);
387 if (item == NULL) {
388 return 0;
389 }
390
391 hm_fragment *frag = (hm_fragment *)item->data;
392 assert(s->d1->handshake_read_seq <= frag->msg_header.seq);
393
394 return s->d1->handshake_read_seq == frag->msg_header.seq &&
395 frag->reassembly == NULL;
396}
397
398/* dtls1_discard_fragment_body discards a handshake fragment body of length
399 * |frag_len|. It returns one on success and zero on error.
400 *
401 * TODO(davidben): This function will go away when ssl_read_bytes is gone from
402 * the DTLS side. */
403static int dtls1_discard_fragment_body(SSL *s, size_t frag_len) {
404 uint8_t discard[256];
405 while (frag_len > 0) {
406 size_t chunk = frag_len < sizeof(discard) ? frag_len : sizeof(discard);
407 int ret = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, discard, chunk,
408 0);
409 if (ret != chunk) {
410 return 0;
411 }
412 frag_len -= chunk;
413 }
414 return 1;
415}
416
417/* dtls1_get_buffered_message returns the buffered message corresponding to
418 * |msg_hdr|. If none exists, it creates a new one and inserts it in the
419 * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It
420 * returns NULL on failure. The caller does not take ownership of the result. */
421static hm_fragment *dtls1_get_buffered_message(
422 SSL *s, const struct hm_header_st *msg_hdr) {
423 uint8_t seq64be[8];
424 memset(seq64be, 0, sizeof(seq64be));
425 seq64be[6] = (uint8_t)(msg_hdr->seq >> 8);
426 seq64be[7] = (uint8_t)msg_hdr->seq;
427 pitem *item = pqueue_find(s->d1->buffered_messages, seq64be);
428
429 hm_fragment *frag;
430 if (item == NULL) {
431 /* This is the first fragment from this message. */
432 frag = dtls1_hm_fragment_new(msg_hdr->msg_len,
433 1 /* reassembly buffer needed */);
434 if (frag == NULL) {
435 return NULL;
436 }
437 memcpy(&frag->msg_header, msg_hdr, sizeof(*msg_hdr));
438 item = pitem_new(seq64be, frag);
439 if (item == NULL) {
440 dtls1_hm_fragment_free(frag);
441 return NULL;
442 }
443 item = pqueue_insert(s->d1->buffered_messages, item);
444 /* |pqueue_insert| fails iff a duplicate item is inserted, but |item| cannot
445 * be a duplicate. */
446 assert(item != NULL);
447 } else {
448 frag = item->data;
449 assert(frag->msg_header.seq == msg_hdr->seq);
450 if (frag->msg_header.type != msg_hdr->type ||
451 frag->msg_header.msg_len != msg_hdr->msg_len) {
452 /* The new fragment must be compatible with the previous fragments from
453 * this message. */
454 OPENSSL_PUT_ERROR(SSL, dtls1_get_buffered_message,
455 SSL_R_FRAGMENT_MISMATCH);
456 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
457 return NULL;
458 }
459 }
460 return frag;
461}
462
463/* dtls1_max_handshake_message_len returns the maximum number of bytes
464 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may
465 * be greater if the maximum certificate list size requires it. */
466static size_t dtls1_max_handshake_message_len(const SSL *s) {
467 size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
David Benjamin1d0a1942015-04-26 15:35:35 -0400468 if (max_len < s->max_cert_list) {
469 return s->max_cert_list;
David Benjamin75381222015-03-02 19:30:30 -0500470 }
471 return max_len;
472}
473
474/* dtls1_process_fragment reads a handshake fragment and processes it. It
475 * returns one if a fragment was successfully processed and 0 or -1 on error. */
476static int dtls1_process_fragment(SSL *s) {
477 /* Read handshake message header.
478 *
479 * TODO(davidben): ssl_read_bytes allows splitting the fragment header and
480 * body across two records. Change this interface to consume the fragment in
481 * one pass. */
482 uint8_t header[DTLS1_HM_HEADER_LENGTH];
483 int ret = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, header,
484 DTLS1_HM_HEADER_LENGTH, 0);
485 if (ret <= 0) {
David Benjamin75381222015-03-02 19:30:30 -0500486 return ret;
487 }
488 if (ret != DTLS1_HM_HEADER_LENGTH) {
489 OPENSSL_PUT_ERROR(SSL, dtls1_process_fragment, SSL_R_UNEXPECTED_MESSAGE);
490 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
491 return -1;
492 }
493
494 /* Parse the message fragment header. */
495 struct hm_header_st msg_hdr;
496 dtls1_get_message_header(header, &msg_hdr);
497
498 const size_t frag_off = msg_hdr.frag_off;
499 const size_t frag_len = msg_hdr.frag_len;
500 const size_t msg_len = msg_hdr.msg_len;
501 if (frag_off > msg_len || frag_off + frag_len < frag_off ||
502 frag_off + frag_len > msg_len ||
503 msg_len > dtls1_max_handshake_message_len(s)) {
504 OPENSSL_PUT_ERROR(SSL, dtls1_process_fragment,
505 SSL_R_EXCESSIVE_MESSAGE_SIZE);
506 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
507 return -1;
508 }
509
510 if (msg_hdr.seq < s->d1->handshake_read_seq ||
511 msg_hdr.seq > (unsigned)s->d1->handshake_read_seq +
512 kHandshakeBufferSize) {
513 /* Ignore fragments from the past, or ones too far in the future. */
514 if (!dtls1_discard_fragment_body(s, frag_len)) {
515 return -1;
516 }
517 return 1;
518 }
519
520 hm_fragment *frag = dtls1_get_buffered_message(s, &msg_hdr);
521 if (frag == NULL) {
522 return -1;
523 }
524 assert(frag->msg_header.msg_len == msg_len);
525
526 if (frag->reassembly == NULL) {
527 /* The message is already assembled. */
528 if (!dtls1_discard_fragment_body(s, frag_len)) {
529 return -1;
530 }
531 return 1;
532 }
533 assert(msg_len > 0);
534
535 /* Read the body of the fragment. */
536 ret = s->method->ssl_read_bytes(
537 s, SSL3_RT_HANDSHAKE, frag->fragment + frag_off, frag_len, 0);
538 if (ret != frag_len) {
539 OPENSSL_PUT_ERROR(SSL, dtls1_process_fragment, SSL_R_UNEXPECTED_MESSAGE);
540 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
541 return -1;
542 }
David Benjaminee562b92015-03-02 20:52:52 -0500543 dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len);
David Benjamin75381222015-03-02 19:30:30 -0500544
David Benjamin75381222015-03-02 19:30:30 -0500545 return 1;
546}
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjaminbcb2d912015-02-24 23:45:43 -0500548/* dtls1_get_message reads a handshake message of message type |msg_type| (any
549 * if |msg_type| == -1), maximum acceptable body length |max|. Read an entire
550 * handshake message. Handshake messages arrive in fragments. */
551long dtls1_get_message(SSL *s, int st1, int stn, int msg_type, long max,
David Benjamin5ca39fb2015-03-01 23:57:54 -0500552 enum ssl_hash_message_t hash_message, int *ok) {
David Benjamin75381222015-03-02 19:30:30 -0500553 pitem *item = NULL;
554 hm_fragment *frag = NULL;
555 int al;
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langley71d8a082014-12-13 16:28:18 -0800557 /* s3->tmp is used to store messages that are unexpected, caused
558 * by the absence of an optional handshake message */
559 if (s->s3->tmp.reuse_message) {
David Benjamin5ca39fb2015-03-01 23:57:54 -0500560 /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
561 * ssl_dont_hash_message would have to have been applied to the previous
562 * call. */
563 assert(hash_message == ssl_hash_message);
Adam Langley71d8a082014-12-13 16:28:18 -0800564 s->s3->tmp.reuse_message = 0;
David Benjaminbcb2d912015-02-24 23:45:43 -0500565 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
Adam Langley71d8a082014-12-13 16:28:18 -0800566 al = SSL_AD_UNEXPECTED_MESSAGE;
567 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
568 goto f_err;
569 }
570 *ok = 1;
571 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
572 s->init_num = (int)s->s3->tmp.message_size;
573 return s->init_num;
574 }
Adam Langley95c29f32014-06-20 12:00:00 -0700575
David Benjamin75381222015-03-02 19:30:30 -0500576 /* Process fragments until one is found. */
577 while (!dtls1_is_next_message_complete(s)) {
578 int ret = dtls1_process_fragment(s);
579 if (ret <= 0) {
580 *ok = 0;
581 return ret;
582 }
Adam Langley71d8a082014-12-13 16:28:18 -0800583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
David Benjamin75381222015-03-02 19:30:30 -0500585 /* Read out the next complete handshake message. */
586 item = pqueue_pop(s->d1->buffered_messages);
587 assert(item != NULL);
588 frag = (hm_fragment *)item->data;
589 assert(s->d1->handshake_read_seq == frag->msg_header.seq);
590 assert(frag->reassembly == NULL);
591
David Benjaminbf0df922015-03-10 00:02:26 -0400592 if (frag->msg_header.msg_len > (size_t)max) {
David Benjamin75381222015-03-02 19:30:30 -0500593 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_EXCESSIVE_MESSAGE_SIZE);
594 goto err;
595 }
596
597 CBB cbb;
598 if (!BUF_MEM_grow(s->init_buf,
599 (size_t)frag->msg_header.msg_len +
600 DTLS1_HM_HEADER_LENGTH) ||
601 !CBB_init_fixed(&cbb, (uint8_t *)s->init_buf->data, s->init_buf->max)) {
602 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, ERR_R_MALLOC_FAILURE);
603 goto err;
604 }
605
606 /* Reconstruct the assembled message. */
607 size_t len;
608 if (!CBB_add_u8(&cbb, frag->msg_header.type) ||
609 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
610 !CBB_add_u16(&cbb, frag->msg_header.seq) ||
611 !CBB_add_u24(&cbb, 0 /* frag_off */) ||
612 !CBB_add_u24(&cbb, frag->msg_header.msg_len) ||
613 !CBB_add_bytes(&cbb, frag->fragment, frag->msg_header.msg_len) ||
614 !CBB_finish(&cbb, NULL, &len)) {
615 CBB_cleanup(&cbb);
616 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, ERR_R_INTERNAL_ERROR);
617 goto err;
618 }
619 assert(len == (size_t)frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH);
620
621 s->d1->handshake_read_seq++;
622
623 /* TODO(davidben): This function has a lot of implicit outputs. Simplify the
624 * |ssl_get_message| API. */
625 s->s3->tmp.message_type = frag->msg_header.type;
626 s->s3->tmp.message_size = frag->msg_header.msg_len;
627 s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
628 s->init_num = frag->msg_header.msg_len;
629
630 if (msg_type >= 0 && s->s3->tmp.message_type != msg_type) {
David Benjaminbcb2d912015-02-24 23:45:43 -0500631 al = SSL_AD_UNEXPECTED_MESSAGE;
632 OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE);
633 goto f_err;
634 }
David Benjamin5ca39fb2015-03-01 23:57:54 -0500635 if (hash_message == ssl_hash_message && !ssl3_hash_current_message(s)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500636 goto err;
Adam Langley71d8a082014-12-13 16:28:18 -0800637 }
638 if (s->msg_callback) {
David Benjamin75381222015-03-02 19:30:30 -0500639 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
640 s->init_num + DTLS1_HM_HEADER_LENGTH, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800641 s->msg_callback_arg);
642 }
Adam Langley95c29f32014-06-20 12:00:00 -0700643
David Benjamin75381222015-03-02 19:30:30 -0500644 pitem_free(item);
645 dtls1_hm_fragment_free(frag);
Adam Langley95c29f32014-06-20 12:00:00 -0700646
David Benjamin75381222015-03-02 19:30:30 -0500647 s->state = stn;
648 *ok = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800649 return s->init_num;
Adam Langley95c29f32014-06-20 12:00:00 -0700650
651f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800652 ssl3_send_alert(s, SSL3_AL_FATAL, al);
David Benjaminfbdfefb2015-02-16 19:33:53 -0500653err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400654 pitem_free(item);
655 dtls1_hm_fragment_free(frag);
Adam Langley71d8a082014-12-13 16:28:18 -0800656 *ok = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800657 return -1;
658}
Adam Langley95c29f32014-06-20 12:00:00 -0700659
660/* for these 2 messages, we need to
661 * ssl->enc_read_ctx re-init
662 * ssl->s3->read_sequence zero
663 * ssl->s3->read_mac_secret re-init
664 * ssl->session->read_sym_enc assign
665 * ssl->session->read_compression assign
Adam Langley71d8a082014-12-13 16:28:18 -0800666 * ssl->session->read_hash assign */
667int dtls1_send_change_cipher_spec(SSL *s, int a, int b) {
668 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700669
Adam Langley71d8a082014-12-13 16:28:18 -0800670 if (s->state == a) {
671 p = (uint8_t *)s->init_buf->data;
672 *p++ = SSL3_MT_CCS;
673 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
674 s->init_num = DTLS1_CCS_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langley71d8a082014-12-13 16:28:18 -0800676 s->init_off = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700677
David Benjamin16d031a2014-12-14 18:52:44 -0500678 dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0,
679 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Adam Langley71d8a082014-12-13 16:28:18 -0800681 /* buffer the message to handle re-xmits */
682 dtls1_buffer_message(s, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langley71d8a082014-12-13 16:28:18 -0800684 s->state = b;
685 }
Adam Langley95c29f32014-06-20 12:00:00 -0700686
Adam Langley71d8a082014-12-13 16:28:18 -0800687 /* SSL3_ST_CW_CHANGE_B */
David Benjamin3e3090d2015-04-05 12:48:30 -0400688 return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC, dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Adam Langley71d8a082014-12-13 16:28:18 -0800691int dtls1_read_failed(SSL *s, int code) {
692 if (code > 0) {
Adam Langleyd3459fb2015-02-13 14:56:28 -0800693 assert(0);
Adam Langley71d8a082014-12-13 16:28:18 -0800694 return 1;
695 }
Adam Langley95c29f32014-06-20 12:00:00 -0700696
Adam Langley71d8a082014-12-13 16:28:18 -0800697 if (!dtls1_is_timer_expired(s)) {
698 /* not a timeout, none of our business, let higher layers handle this. In
699 * fact, it's probably an error */
700 return code;
701 }
Adam Langley95c29f32014-06-20 12:00:00 -0700702
Adam Langley71d8a082014-12-13 16:28:18 -0800703 if (!SSL_in_init(s)) {
704 /* done, no need to send a retransmit */
705 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
706 return code;
707 }
Adam Langley95c29f32014-06-20 12:00:00 -0700708
David Benjamin8c249802015-05-05 09:44:18 -0400709 return DTLSv1_handle_timeout(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800710}
Adam Langley95c29f32014-06-20 12:00:00 -0700711
Adam Langley71d8a082014-12-13 16:28:18 -0800712int dtls1_get_queue_priority(unsigned short seq, int is_ccs) {
713 /* The index of the retransmission queue actually is the message sequence
714 * number, since the queue only contains messages of a single handshake.
715 * However, the ChangeCipherSpec has no message sequence number and so using
716 * only the sequence will result in the CCS and Finished having the same
717 * index. To prevent this, the sequence number is multiplied by 2. In case of
718 * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also
719 * maintains the order of the index (important for priority queues) and fits
720 * in the unsigned short variable. */
721 return seq * 2 - is_ccs;
722}
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langleybcc4e232015-02-19 15:51:58 -0800724static int dtls1_retransmit_message(SSL *s, hm_fragment *frag) {
Adam Langley71d8a082014-12-13 16:28:18 -0800725 int ret;
726 /* XDTLS: for now assuming that read/writes are blocking */
Adam Langley71d8a082014-12-13 16:28:18 -0800727 unsigned long header_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700728
Adam Langley71d8a082014-12-13 16:28:18 -0800729 /* assert(s->init_num == 0);
730 assert(s->init_off == 0); */
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langley71d8a082014-12-13 16:28:18 -0800732 if (frag->msg_header.is_ccs) {
733 header_length = DTLS1_CCS_HEADER_LENGTH;
734 } else {
735 header_length = DTLS1_HM_HEADER_LENGTH;
736 }
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langley71d8a082014-12-13 16:28:18 -0800738 memcpy(s->init_buf->data, frag->fragment,
739 frag->msg_header.msg_len + header_length);
740 s->init_num = frag->msg_header.msg_len + header_length;
Adam Langley95c29f32014-06-20 12:00:00 -0700741
David Benjamin16d031a2014-12-14 18:52:44 -0500742 dtls1_set_message_header(s, frag->msg_header.type,
743 frag->msg_header.msg_len, frag->msg_header.seq,
744 0, frag->msg_header.frag_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700745
David Benjaminafbc63f2015-02-01 02:33:59 -0500746 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
747 * (negotiated cipher) exist. */
David Benjamin3e3090d2015-04-05 12:48:30 -0400748 assert(s->d1->w_epoch == 0 || s->d1->w_epoch == 1);
749 assert(frag->msg_header.epoch <= s->d1->w_epoch);
750 enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch;
751 if (s->d1->w_epoch == 1 && frag->msg_header.epoch == 0) {
752 use_epoch = dtls1_use_previous_epoch;
Adam Langley71d8a082014-12-13 16:28:18 -0800753 }
754
755 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? SSL3_RT_CHANGE_CIPHER_SPEC
David Benjamin3e3090d2015-04-05 12:48:30 -0400756 : SSL3_RT_HANDSHAKE,
757 use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800758
Adam Langley71d8a082014-12-13 16:28:18 -0800759 (void)BIO_flush(SSL_get_wbio(s));
760 return ret;
761}
Adam Langley95c29f32014-06-20 12:00:00 -0700762
Adam Langleybcc4e232015-02-19 15:51:58 -0800763
764int dtls1_retransmit_buffered_messages(SSL *s) {
765 pqueue sent = s->d1->sent_messages;
766 piterator iter = pqueue_iterator(sent);
767 pitem *item;
768
769 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
770 hm_fragment *frag = (hm_fragment *)item->data;
771 if (dtls1_retransmit_message(s, frag) <= 0) {
772 return -1;
773 }
774 }
775
776 return 1;
777}
778
779int dtls1_buffer_message(SSL *s, int is_ccs) {
780 pitem *item;
781 hm_fragment *frag;
782 uint8_t seq64be[8];
783
784 /* this function is called immediately after a message has
785 * been serialized */
786 assert(s->init_off == 0);
787
788 frag = dtls1_hm_fragment_new(s->init_num, 0);
789 if (!frag) {
790 return 0;
791 }
792
793 memcpy(frag->fragment, s->init_buf->data, s->init_num);
794
795 if (is_ccs) {
796 assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH ==
797 (unsigned int)s->init_num);
798 } else {
799 assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH ==
800 (unsigned int)s->init_num);
801 }
802
803 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
804 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
805 frag->msg_header.type = s->d1->w_msg_hdr.type;
806 frag->msg_header.frag_off = 0;
807 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
808 frag->msg_header.is_ccs = is_ccs;
809 frag->msg_header.epoch = s->d1->w_epoch;
810
811 memset(seq64be, 0, sizeof(seq64be));
812 seq64be[6] = (uint8_t)(
813 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >>
814 8);
815 seq64be[7] = (uint8_t)(
816 dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs));
817
818 item = pitem_new(seq64be, frag);
819 if (item == NULL) {
820 dtls1_hm_fragment_free(frag);
821 return 0;
822 }
823
824 pqueue_insert(s->d1->sent_messages, item);
825 return 1;
826}
827
Adam Langley95c29f32014-06-20 12:00:00 -0700828/* call this function when the buffered messages are no longer needed */
Adam Langley71d8a082014-12-13 16:28:18 -0800829void dtls1_clear_record_buffer(SSL *s) {
830 pitem *item;
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langley71d8a082014-12-13 16:28:18 -0800832 for (item = pqueue_pop(s->d1->sent_messages); item != NULL;
833 item = pqueue_pop(s->d1->sent_messages)) {
834 dtls1_hm_fragment_free((hm_fragment *)item->data);
835 pitem_free(item);
836 }
837}
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langley95c29f32014-06-20 12:00:00 -0700839/* don't actually do the writing, wait till the MTU has been retrieved */
David Benjamin16d031a2014-12-14 18:52:44 -0500840void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len,
841 unsigned short seq_num, unsigned long frag_off,
842 unsigned long frag_len) {
Adam Langley71d8a082014-12-13 16:28:18 -0800843 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langley71d8a082014-12-13 16:28:18 -0800845 msg_hdr->type = mt;
846 msg_hdr->msg_len = len;
847 msg_hdr->seq = seq_num;
848 msg_hdr->frag_off = frag_off;
849 msg_hdr->frag_len = frag_len;
850}
Adam Langley95c29f32014-06-20 12:00:00 -0700851
Adam Langley71d8a082014-12-13 16:28:18 -0800852static void dtls1_fix_message_header(SSL *s, unsigned long frag_off,
853 unsigned long frag_len) {
854 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langley71d8a082014-12-13 16:28:18 -0800856 msg_hdr->frag_off = frag_off;
857 msg_hdr->frag_len = frag_len;
858}
Adam Langley95c29f32014-06-20 12:00:00 -0700859
Adam Langley71d8a082014-12-13 16:28:18 -0800860static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) {
861 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langley71d8a082014-12-13 16:28:18 -0800863 *p++ = msg_hdr->type;
864 l2n3(msg_hdr->msg_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langley71d8a082014-12-13 16:28:18 -0800866 s2n(msg_hdr->seq, p);
867 l2n3(msg_hdr->frag_off, p);
868 l2n3(msg_hdr->frag_len, p);
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langley71d8a082014-12-13 16:28:18 -0800870 return p;
871}
Adam Langley95c29f32014-06-20 12:00:00 -0700872
Adam Langley71d8a082014-12-13 16:28:18 -0800873unsigned int dtls1_min_mtu(void) {
David Benjamina18b6712015-01-11 19:31:22 -0500874 return kMinMTU;
Adam Langley71d8a082014-12-13 16:28:18 -0800875}
Adam Langley95c29f32014-06-20 12:00:00 -0700876
Adam Langley71d8a082014-12-13 16:28:18 -0800877void dtls1_get_message_header(uint8_t *data,
878 struct hm_header_st *msg_hdr) {
879 memset(msg_hdr, 0x00, sizeof(struct hm_header_st));
880 msg_hdr->type = *(data++);
881 n2l3(data, msg_hdr->msg_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langley71d8a082014-12-13 16:28:18 -0800883 n2s(data, msg_hdr->seq);
884 n2l3(data, msg_hdr->frag_off);
885 n2l3(data, msg_hdr->frag_len);
886}
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langley71d8a082014-12-13 16:28:18 -0800888int dtls1_shutdown(SSL *s) {
889 int ret;
890 ret = ssl3_shutdown(s);
891 return ret;
892}