Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 114 | #include <openssl/ssl.h> |
| 115 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | #include <assert.h> |
| 117 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | #include <string.h> |
| 119 | |
| 120 | #include <openssl/buf.h> |
| 121 | #include <openssl/err.h> |
| 122 | #include <openssl/evp.h> |
| 123 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #include <openssl/rand.h> |
| 125 | #include <openssl/x509.h> |
| 126 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 127 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 130 | /* 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 135 | /* kMinMTU is the minimum acceptable MTU value. */ |
| 136 | static 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. */ |
| 140 | static const unsigned int kDefaultMTU = 1500 - 28; |
| 141 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 142 | /* kMaxHandshakeBuffer is the maximum number of handshake messages ahead of the |
| 143 | * current one to buffer. */ |
| 144 | static const unsigned int kHandshakeBufferSize = 10; |
| 145 | |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 146 | static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly) { |
| 147 | hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 148 | if (frag == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 149 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 152 | memset(frag, 0, sizeof(hm_fragment)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 154 | /* If the handshake message is empty, |frag->fragment| and |frag->reassembly| |
| 155 | * are NULL. */ |
| 156 | if (frag_len > 0) { |
| 157 | frag->fragment = OPENSSL_malloc(frag_len); |
| 158 | if (frag->fragment == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 159 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 160 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 161 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 163 | if (reassembly) { |
| 164 | /* Initialize reassembly bitmask. */ |
| 165 | if (frag_len + 7 < frag_len) { |
| 166 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 167 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 168 | } |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 169 | size_t bitmask_len = (frag_len + 7) / 8; |
| 170 | frag->reassembly = OPENSSL_malloc(bitmask_len); |
| 171 | if (frag->reassembly == NULL) { |
| 172 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 173 | goto err; |
| 174 | } |
| 175 | memset(frag->reassembly, 0, bitmask_len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 176 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 177 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 178 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 179 | return frag; |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 180 | |
| 181 | err: |
| 182 | dtls1_hm_fragment_free(frag); |
| 183 | return NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 184 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 186 | void dtls1_hm_fragment_free(hm_fragment *frag) { |
David Benjamin | 5d1ec73 | 2015-04-22 13:38:00 -0400 | [diff] [blame] | 187 | if (frag == NULL) { |
| 188 | return; |
| 189 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 190 | OPENSSL_free(frag->fragment); |
| 191 | OPENSSL_free(frag->reassembly); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 192 | OPENSSL_free(frag); |
| 193 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 195 | #if !defined(inline) |
| 196 | #define inline __inline |
| 197 | #endif |
| 198 | |
| 199 | /* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|, |
| 200 | * exclusive, set. */ |
| 201 | static inline uint8_t bit_range(size_t start, size_t end) { |
| 202 | return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1)); |
| 203 | } |
| 204 | |
| 205 | /* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive, |
| 206 | * as received in |frag|. If |frag| becomes complete, it clears |
| 207 | * |frag->reassembly|. The range must be within the bounds of |frag|'s message |
| 208 | * and |frag->reassembly| must not be NULL. */ |
| 209 | static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start, |
| 210 | size_t end) { |
| 211 | size_t i; |
| 212 | size_t msg_len = frag->msg_header.msg_len; |
| 213 | |
| 214 | if (frag->reassembly == NULL || start > end || end > msg_len) { |
| 215 | assert(0); |
| 216 | return; |
| 217 | } |
| 218 | /* A zero-length message will never have a pending reassembly. */ |
| 219 | assert(msg_len > 0); |
| 220 | |
| 221 | if ((start >> 3) == (end >> 3)) { |
| 222 | frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7); |
| 223 | } else { |
| 224 | frag->reassembly[start >> 3] |= bit_range(start & 7, 8); |
| 225 | for (i = (start >> 3) + 1; i < (end >> 3); i++) { |
| 226 | frag->reassembly[i] = 0xff; |
| 227 | } |
| 228 | if ((end & 7) != 0) { |
| 229 | frag->reassembly[end >> 3] |= bit_range(0, end & 7); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* Check if the fragment is complete. */ |
| 234 | for (i = 0; i < (msg_len >> 3); i++) { |
| 235 | if (frag->reassembly[i] != 0xff) { |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | if ((msg_len & 7) != 0 && |
| 240 | frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | OPENSSL_free(frag->reassembly); |
| 245 | frag->reassembly = NULL; |
| 246 | } |
| 247 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 248 | static void dtls1_update_mtu(SSL *ssl) { |
| 249 | /* TODO(davidben): What is this code doing and do we need it? */ |
| 250 | if (ssl->d1->mtu < dtls1_min_mtu() && |
| 251 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 252 | long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 253 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 254 | ssl->d1->mtu = (unsigned)mtu; |
| 255 | } else { |
| 256 | ssl->d1->mtu = kDefaultMTU; |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 257 | BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | /* The MTU should be above the minimum now. */ |
| 262 | assert(ssl->d1->mtu >= dtls1_min_mtu()); |
| 263 | } |
| 264 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 265 | /* dtls1_max_record_size returns the maximum record body length that may be |
| 266 | * written without exceeding the MTU. It accounts for any buffering installed on |
| 267 | * the write BIO. If no record may be written, it returns zero. */ |
| 268 | static size_t dtls1_max_record_size(SSL *ssl) { |
| 269 | size_t ret = ssl->d1->mtu; |
| 270 | |
| 271 | size_t overhead = ssl_max_seal_overhead(ssl); |
| 272 | if (ret <= overhead) { |
| 273 | return 0; |
| 274 | } |
| 275 | ret -= overhead; |
| 276 | |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 277 | size_t pending = BIO_wpending(ssl->wbio); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 278 | if (ret <= pending) { |
| 279 | return 0; |
| 280 | } |
| 281 | ret -= pending; |
| 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 286 | static int dtls1_write_change_cipher_spec(SSL *ssl, |
| 287 | enum dtls1_use_epoch_t use_epoch) { |
| 288 | dtls1_update_mtu(ssl); |
| 289 | |
| 290 | /* During the handshake, wbio is buffered to pack messages together. Flush the |
| 291 | * buffer if the ChangeCipherSpec would not fit in a packet. */ |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 292 | if (dtls1_max_record_size(ssl) == 0) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 293 | int ret = BIO_flush(ssl->wbio); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 294 | if (ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 295 | ssl->rwstate = SSL_WRITING; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 296 | return ret; |
| 297 | } |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 301 | int ret = |
David Benjamin | 45d45c1 | 2016-06-07 15:20:49 -0400 | [diff] [blame^] | 302 | dtls1_write_record(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 303 | sizeof(kChangeCipherSpec), use_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 304 | if (ret <= 0) { |
| 305 | return ret; |
| 306 | } |
| 307 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 308 | ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, |
| 309 | SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 310 | sizeof(kChangeCipherSpec)); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 311 | return 1; |
| 312 | } |
| 313 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 314 | int dtls1_do_handshake_write(SSL *ssl, enum dtls1_use_epoch_t use_epoch) { |
| 315 | dtls1_update_mtu(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 317 | int ret = -1; |
| 318 | CBB cbb; |
| 319 | CBB_zero(&cbb); |
| 320 | /* Allocate a temporary buffer to hold the message fragments to avoid |
| 321 | * clobbering the message. */ |
| 322 | uint8_t *buf = OPENSSL_malloc(ssl->d1->mtu); |
| 323 | if (buf == NULL) { |
| 324 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 325 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 327 | /* Consume the message header. Fragments will have different headers |
| 328 | * prepended. */ |
| 329 | if (ssl->init_off == 0) { |
| 330 | ssl->init_off += DTLS1_HM_HEADER_LENGTH; |
| 331 | ssl->init_num -= DTLS1_HM_HEADER_LENGTH; |
| 332 | } |
| 333 | assert(ssl->init_off >= DTLS1_HM_HEADER_LENGTH); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 335 | do { |
| 336 | /* During the handshake, wbio is buffered to pack messages together. Flush |
| 337 | * the buffer if there isn't enough room to make progress. */ |
| 338 | if (dtls1_max_record_size(ssl) < DTLS1_HM_HEADER_LENGTH + 1) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 339 | int flush_ret = BIO_flush(ssl->wbio); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 340 | if (flush_ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 341 | ssl->rwstate = SSL_WRITING; |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 342 | ret = flush_ret; |
| 343 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 344 | } |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 345 | assert(BIO_wpending(ssl->wbio) == 0); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 346 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 348 | size_t todo = dtls1_max_record_size(ssl); |
| 349 | if (todo < DTLS1_HM_HEADER_LENGTH + 1) { |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 350 | /* To make forward progress, the MTU must, at minimum, fit the handshake |
| 351 | * header and one byte of handshake body. */ |
| 352 | OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 353 | goto err; |
| 354 | } |
| 355 | todo -= DTLS1_HM_HEADER_LENGTH; |
| 356 | |
| 357 | if (todo > (size_t)ssl->init_num) { |
| 358 | todo = ssl->init_num; |
| 359 | } |
| 360 | if (todo >= (1u << 24)) { |
| 361 | todo = (1u << 24) - 1; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 362 | } |
| 363 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 364 | size_t len; |
| 365 | if (!CBB_init_fixed(&cbb, buf, ssl->d1->mtu) || |
| 366 | !CBB_add_u8(&cbb, ssl->d1->w_msg_hdr.type) || |
| 367 | !CBB_add_u24(&cbb, ssl->d1->w_msg_hdr.msg_len) || |
| 368 | !CBB_add_u16(&cbb, ssl->d1->w_msg_hdr.seq) || |
| 369 | !CBB_add_u24(&cbb, ssl->init_off - DTLS1_HM_HEADER_LENGTH) || |
| 370 | !CBB_add_u24(&cbb, todo) || |
| 371 | !CBB_add_bytes( |
| 372 | &cbb, (const uint8_t *)ssl->init_buf->data + ssl->init_off, todo) || |
| 373 | !CBB_finish(&cbb, NULL, &len)) { |
| 374 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 375 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 376 | } |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 377 | |
David Benjamin | 45d45c1 | 2016-06-07 15:20:49 -0400 | [diff] [blame^] | 378 | int write_ret = |
| 379 | dtls1_write_record(ssl, SSL3_RT_HANDSHAKE, buf, len, use_epoch); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 380 | if (write_ret <= 0) { |
| 381 | ret = write_ret; |
| 382 | goto err; |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 383 | } |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 384 | ssl->init_off += todo; |
| 385 | ssl->init_num -= todo; |
| 386 | } while (ssl->init_num > 0); |
| 387 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 388 | ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_HANDSHAKE, |
| 389 | ssl->init_buf->data, |
| 390 | (size_t)(ssl->init_off + ssl->init_num)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 391 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 392 | ssl->init_off = 0; |
| 393 | ssl->init_num = 0; |
| 394 | |
| 395 | ret = 1; |
| 396 | |
| 397 | err: |
| 398 | CBB_cleanup(&cbb); |
| 399 | OPENSSL_free(buf); |
| 400 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 401 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 403 | /* dtls1_is_next_message_complete returns one if the next handshake message is |
| 404 | * complete and zero otherwise. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 405 | static int dtls1_is_next_message_complete(SSL *ssl) { |
| 406 | pitem *item = pqueue_peek(ssl->d1->buffered_messages); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 407 | if (item == NULL) { |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | hm_fragment *frag = (hm_fragment *)item->data; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 412 | assert(ssl->d1->handshake_read_seq <= frag->msg_header.seq); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 413 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 414 | return ssl->d1->handshake_read_seq == frag->msg_header.seq && |
| 415 | frag->reassembly == NULL; |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 416 | } |
| 417 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 418 | /* dtls1_get_buffered_message returns the buffered message corresponding to |
| 419 | * |msg_hdr|. If none exists, it creates a new one and inserts it in the |
| 420 | * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It |
| 421 | * returns NULL on failure. The caller does not take ownership of the result. */ |
| 422 | static hm_fragment *dtls1_get_buffered_message( |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 423 | SSL *ssl, const struct hm_header_st *msg_hdr) { |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 424 | uint8_t seq64be[8]; |
| 425 | memset(seq64be, 0, sizeof(seq64be)); |
| 426 | seq64be[6] = (uint8_t)(msg_hdr->seq >> 8); |
| 427 | seq64be[7] = (uint8_t)msg_hdr->seq; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 428 | pitem *item = pqueue_find(ssl->d1->buffered_messages, seq64be); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 429 | |
| 430 | hm_fragment *frag; |
| 431 | if (item == NULL) { |
| 432 | /* This is the first fragment from this message. */ |
| 433 | frag = dtls1_hm_fragment_new(msg_hdr->msg_len, |
| 434 | 1 /* reassembly buffer needed */); |
| 435 | if (frag == NULL) { |
| 436 | return NULL; |
| 437 | } |
| 438 | memcpy(&frag->msg_header, msg_hdr, sizeof(*msg_hdr)); |
| 439 | item = pitem_new(seq64be, frag); |
| 440 | if (item == NULL) { |
| 441 | dtls1_hm_fragment_free(frag); |
| 442 | return NULL; |
| 443 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 444 | item = pqueue_insert(ssl->d1->buffered_messages, item); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 445 | /* |pqueue_insert| fails iff a duplicate item is inserted, but |item| cannot |
| 446 | * be a duplicate. */ |
| 447 | assert(item != NULL); |
| 448 | } else { |
| 449 | frag = item->data; |
| 450 | assert(frag->msg_header.seq == msg_hdr->seq); |
| 451 | if (frag->msg_header.type != msg_hdr->type || |
| 452 | frag->msg_header.msg_len != msg_hdr->msg_len) { |
| 453 | /* The new fragment must be compatible with the previous fragments from |
| 454 | * this message. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 455 | OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 456 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 457 | return NULL; |
| 458 | } |
| 459 | } |
| 460 | return frag; |
| 461 | } |
| 462 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 463 | /* dtls1_process_handshake_record reads a handshake record and processes it. It |
| 464 | * returns one if the record was successfully processed and 0 or -1 on error. */ |
| 465 | static int dtls1_process_handshake_record(SSL *ssl) { |
| 466 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 467 | |
| 468 | start: |
| 469 | if (rr->length == 0) { |
| 470 | int ret = dtls1_get_record(ssl); |
| 471 | if (ret <= 0) { |
| 472 | return ret; |
| 473 | } |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 474 | } |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 475 | |
| 476 | /* Cross-epoch records are discarded, but we may receive out-of-order |
| 477 | * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec |
| 478 | * before the appropriate point in the handshake. Those must be silently |
| 479 | * discarded. |
| 480 | * |
| 481 | * However, only allow the out-of-order records in the correct epoch. |
| 482 | * Application data must come in the encrypted epoch, and ChangeCipherSpec in |
| 483 | * the unencrypted epoch (we never renegotiate). Other cases fall through and |
| 484 | * fail with a fatal error. */ |
| 485 | if ((rr->type == SSL3_RT_APPLICATION_DATA && |
| 486 | ssl->s3->aead_read_ctx != NULL) || |
| 487 | (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC && |
| 488 | ssl->s3->aead_read_ctx == NULL)) { |
| 489 | rr->length = 0; |
| 490 | goto start; |
| 491 | } |
| 492 | |
| 493 | if (rr->type != SSL3_RT_HANDSHAKE) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 494 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 495 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 496 | return -1; |
| 497 | } |
| 498 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 499 | CBS cbs; |
| 500 | CBS_init(&cbs, rr->data, rr->length); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 501 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 502 | while (CBS_len(&cbs) > 0) { |
| 503 | /* Read a handshake fragment. */ |
| 504 | struct hm_header_st msg_hdr; |
| 505 | CBS body; |
| 506 | if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
| 507 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
| 508 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 509 | return -1; |
| 510 | } |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 511 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 512 | const size_t frag_off = msg_hdr.frag_off; |
| 513 | const size_t frag_len = msg_hdr.frag_len; |
| 514 | const size_t msg_len = msg_hdr.msg_len; |
| 515 | if (frag_off > msg_len || frag_off + frag_len < frag_off || |
| 516 | frag_off + frag_len > msg_len || |
| 517 | msg_len > ssl_max_handshake_message_len(ssl)) { |
| 518 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 519 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 520 | return -1; |
| 521 | } |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 522 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 523 | if (msg_hdr.seq < ssl->d1->handshake_read_seq || |
| 524 | msg_hdr.seq > |
| 525 | (unsigned)ssl->d1->handshake_read_seq + kHandshakeBufferSize) { |
| 526 | /* Ignore fragments from the past, or ones too far in the future. */ |
| 527 | continue; |
| 528 | } |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 529 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 530 | hm_fragment *frag = dtls1_get_buffered_message(ssl, &msg_hdr); |
| 531 | if (frag == NULL) { |
| 532 | return -1; |
| 533 | } |
| 534 | assert(frag->msg_header.msg_len == msg_len); |
| 535 | |
| 536 | if (frag->reassembly == NULL) { |
| 537 | /* The message is already assembled. */ |
| 538 | continue; |
| 539 | } |
| 540 | assert(msg_len > 0); |
| 541 | |
| 542 | /* Copy the body into the fragment. */ |
| 543 | memcpy(frag->fragment + frag_off, CBS_data(&body), CBS_len(&body)); |
| 544 | dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len); |
| 545 | } |
| 546 | |
| 547 | rr->length = 0; |
| 548 | ssl_read_buffer_discard(ssl); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 549 | return 1; |
| 550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 552 | /* dtls1_get_message reads a handshake message of message type |msg_type| (any |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 553 | * if |msg_type| == -1). Read an entire handshake message. Handshake messages |
| 554 | * arrive in fragments. */ |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 555 | long dtls1_get_message(SSL *ssl, int msg_type, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 556 | enum ssl_hash_message_t hash_message, int *ok) { |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 557 | pitem *item = NULL; |
| 558 | hm_fragment *frag = NULL; |
| 559 | int al; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 560 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 561 | /* s3->tmp is used to store messages that are unexpected, caused |
| 562 | * by the absence of an optional handshake message */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 563 | if (ssl->s3->tmp.reuse_message) { |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 564 | /* A ssl_dont_hash_message call cannot be combined with reuse_message; the |
| 565 | * ssl_dont_hash_message would have to have been applied to the previous |
| 566 | * call. */ |
| 567 | assert(hash_message == ssl_hash_message); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 568 | ssl->s3->tmp.reuse_message = 0; |
| 569 | if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 570 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 571 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 572 | goto f_err; |
| 573 | } |
| 574 | *ok = 1; |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 575 | assert(ssl->init_buf->length >= DTLS1_HM_HEADER_LENGTH); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 576 | ssl->init_msg = (uint8_t *)ssl->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 577 | ssl->init_num = (int)ssl->init_buf->length - DTLS1_HM_HEADER_LENGTH; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 578 | return ssl->init_num; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 579 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 580 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 581 | /* Process handshake records until the next message is ready. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 582 | while (!dtls1_is_next_message_complete(ssl)) { |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 583 | int ret = dtls1_process_handshake_record(ssl); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 584 | if (ret <= 0) { |
| 585 | *ok = 0; |
| 586 | return ret; |
| 587 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 588 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 589 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 590 | /* Read out the next complete handshake message. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 591 | item = pqueue_pop(ssl->d1->buffered_messages); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 592 | assert(item != NULL); |
| 593 | frag = (hm_fragment *)item->data; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 594 | assert(ssl->d1->handshake_read_seq == frag->msg_header.seq); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 595 | assert(frag->reassembly == NULL); |
| 596 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 597 | /* Reconstruct the assembled message. */ |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 598 | CBB cbb; |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 599 | CBB_zero(&cbb); |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 600 | if (!BUF_MEM_reserve(ssl->init_buf, (size_t)frag->msg_header.msg_len + |
| 601 | DTLS1_HM_HEADER_LENGTH) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 602 | !CBB_init_fixed(&cbb, (uint8_t *)ssl->init_buf->data, |
| 603 | ssl->init_buf->max) || |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 604 | !CBB_add_u8(&cbb, frag->msg_header.type) || |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 605 | !CBB_add_u24(&cbb, frag->msg_header.msg_len) || |
| 606 | !CBB_add_u16(&cbb, frag->msg_header.seq) || |
| 607 | !CBB_add_u24(&cbb, 0 /* frag_off */) || |
| 608 | !CBB_add_u24(&cbb, frag->msg_header.msg_len) || |
| 609 | !CBB_add_bytes(&cbb, frag->fragment, frag->msg_header.msg_len) || |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 610 | !CBB_finish(&cbb, NULL, &ssl->init_buf->length)) { |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 611 | CBB_cleanup(&cbb); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 612 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 613 | goto err; |
| 614 | } |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 615 | assert(ssl->init_buf->length == |
| 616 | (size_t)frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH); |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 617 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 618 | ssl->d1->handshake_read_seq++; |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 619 | |
| 620 | /* TODO(davidben): This function has a lot of implicit outputs. Simplify the |
| 621 | * |ssl_get_message| API. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 622 | ssl->s3->tmp.message_type = frag->msg_header.type; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 623 | ssl->init_msg = (uint8_t *)ssl->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
| 624 | ssl->init_num = frag->msg_header.msg_len; |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 625 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 626 | if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) { |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 627 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 628 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 629 | goto f_err; |
| 630 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 631 | if (hash_message == ssl_hash_message && !ssl3_hash_current_message(ssl)) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 632 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 633 | } |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 634 | |
| 635 | ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE, |
| 636 | ssl->init_buf->data, |
| 637 | ssl->init_num + DTLS1_HM_HEADER_LENGTH); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 639 | pitem_free(item); |
| 640 | dtls1_hm_fragment_free(frag); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 641 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 642 | *ok = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 643 | return ssl->init_num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 644 | |
| 645 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 646 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 647 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 648 | pitem_free(item); |
| 649 | dtls1_hm_fragment_free(frag); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 650 | *ok = 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 651 | return -1; |
| 652 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 653 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 654 | static uint16_t dtls1_get_queue_priority(uint16_t seq, int is_ccs) { |
| 655 | assert(seq * 2 >= seq); |
| 656 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 657 | /* The index of the retransmission queue actually is the message sequence |
| 658 | * number, since the queue only contains messages of a single handshake. |
| 659 | * However, the ChangeCipherSpec has no message sequence number and so using |
| 660 | * only the sequence will result in the CCS and Finished having the same |
| 661 | * index. To prevent this, the sequence number is multiplied by 2. In case of |
| 662 | * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also |
| 663 | * maintains the order of the index (important for priority queues) and fits |
| 664 | * in the unsigned short variable. */ |
| 665 | return seq * 2 - is_ccs; |
| 666 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 668 | static int dtls1_retransmit_message(SSL *ssl, hm_fragment *frag) { |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame] | 669 | /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1 |
| 670 | * (negotiated cipher) exist. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 671 | assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1); |
| 672 | assert(frag->msg_header.epoch <= ssl->d1->w_epoch); |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 673 | enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 674 | if (ssl->d1->w_epoch == 1 && frag->msg_header.epoch == 0) { |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 675 | use_epoch = dtls1_use_previous_epoch; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 676 | } |
| 677 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 678 | /* TODO(davidben): This cannot handle non-blocking writes. */ |
| 679 | int ret; |
| 680 | if (frag->msg_header.is_ccs) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 681 | ret = dtls1_write_change_cipher_spec(ssl, use_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 682 | } else { |
| 683 | /* Restore the message body. |
| 684 | * TODO(davidben): Make this less stateful. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 685 | memcpy(ssl->init_buf->data, frag->fragment, |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 686 | frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 687 | ssl->init_num = frag->msg_header.msg_len + DTLS1_HM_HEADER_LENGTH; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 688 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 689 | dtls1_set_message_header(ssl, frag->msg_header.type, |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 690 | frag->msg_header.msg_len, frag->msg_header.seq, |
| 691 | 0, frag->msg_header.frag_len); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 692 | ret = dtls1_do_handshake_write(ssl, use_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 693 | } |
| 694 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 695 | return ret; |
| 696 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 697 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 698 | int dtls1_retransmit_buffered_messages(SSL *ssl) { |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 699 | /* Ensure we are packing handshake messages. */ |
| 700 | const int was_buffered = ssl_is_wbio_buffered(ssl); |
| 701 | assert(was_buffered == SSL_in_init(ssl)); |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 702 | if (!was_buffered && !ssl_init_wbio_buffer(ssl)) { |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 703 | return -1; |
| 704 | } |
| 705 | assert(ssl_is_wbio_buffered(ssl)); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 706 | |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 707 | int ret = -1; |
| 708 | piterator iter = pqueue_iterator(ssl->d1->sent_messages); |
| 709 | pitem *item; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 710 | for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) { |
| 711 | hm_fragment *frag = (hm_fragment *)item->data; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 712 | if (dtls1_retransmit_message(ssl, frag) <= 0) { |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 713 | goto err; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 717 | ret = BIO_flush(ssl->wbio); |
David Benjamin | 2730955 | 2016-05-05 21:17:53 -0400 | [diff] [blame] | 718 | if (ret <= 0) { |
| 719 | ssl->rwstate = SSL_WRITING; |
| 720 | goto err; |
| 721 | } |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 722 | |
| 723 | err: |
| 724 | if (!was_buffered) { |
| 725 | ssl_free_wbio_buffer(ssl); |
| 726 | } |
| 727 | return ret; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 728 | } |
| 729 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 730 | /* dtls1_buffer_change_cipher_spec adds a ChangeCipherSpec to the current |
| 731 | * handshake flight, ordered just before the handshake message numbered |
| 732 | * |seq|. */ |
| 733 | static int dtls1_buffer_change_cipher_spec(SSL *ssl, uint16_t seq) { |
| 734 | hm_fragment *frag = dtls1_hm_fragment_new(0 /* frag_len */, |
| 735 | 0 /* no reassembly */); |
| 736 | if (frag == NULL) { |
| 737 | return 0; |
| 738 | } |
| 739 | frag->msg_header.is_ccs = 1; |
| 740 | frag->msg_header.epoch = ssl->d1->w_epoch; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 741 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 742 | uint16_t priority = dtls1_get_queue_priority(seq, 1 /* is_ccs */); |
| 743 | uint8_t seq64be[8]; |
| 744 | memset(seq64be, 0, sizeof(seq64be)); |
| 745 | seq64be[6] = (uint8_t)(priority >> 8); |
| 746 | seq64be[7] = (uint8_t)priority; |
| 747 | |
| 748 | pitem *item = pitem_new(seq64be, frag); |
| 749 | if (item == NULL) { |
| 750 | dtls1_hm_fragment_free(frag); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | pqueue_insert(ssl->d1->sent_messages, item); |
| 755 | return 1; |
| 756 | } |
| 757 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 758 | int dtls1_buffer_message(SSL *ssl) { |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 759 | /* this function is called immediately after a message has |
| 760 | * been serialized */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 761 | assert(ssl->init_off == 0); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 762 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 763 | hm_fragment *frag = dtls1_hm_fragment_new(ssl->init_num, 0); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 764 | if (!frag) { |
| 765 | return 0; |
| 766 | } |
| 767 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 768 | memcpy(frag->fragment, ssl->init_buf->data, ssl->init_num); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 769 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 770 | assert(ssl->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH == |
| 771 | (unsigned int)ssl->init_num); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 772 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 773 | frag->msg_header.msg_len = ssl->d1->w_msg_hdr.msg_len; |
| 774 | frag->msg_header.seq = ssl->d1->w_msg_hdr.seq; |
| 775 | frag->msg_header.type = ssl->d1->w_msg_hdr.type; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 776 | frag->msg_header.frag_off = 0; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 777 | frag->msg_header.frag_len = ssl->d1->w_msg_hdr.msg_len; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 778 | frag->msg_header.is_ccs = 0; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 779 | frag->msg_header.epoch = ssl->d1->w_epoch; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 780 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 781 | uint16_t priority = dtls1_get_queue_priority(frag->msg_header.seq, |
| 782 | 0 /* handshake */); |
| 783 | uint8_t seq64be[8]; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 784 | memset(seq64be, 0, sizeof(seq64be)); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 785 | seq64be[6] = (uint8_t)(priority >> 8); |
| 786 | seq64be[7] = (uint8_t)priority; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 787 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 788 | pitem *item = pitem_new(seq64be, frag); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 789 | if (item == NULL) { |
| 790 | dtls1_hm_fragment_free(frag); |
| 791 | return 0; |
| 792 | } |
| 793 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 794 | pqueue_insert(ssl->d1->sent_messages, item); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 795 | return 1; |
| 796 | } |
| 797 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 798 | int dtls1_send_change_cipher_spec(SSL *ssl, int a, int b) { |
| 799 | if (ssl->state == a) { |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 800 | /* Buffer the message to handle retransmits. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 801 | ssl->d1->handshake_write_seq = ssl->d1->next_handshake_write_seq; |
| 802 | dtls1_buffer_change_cipher_spec(ssl, ssl->d1->handshake_write_seq); |
| 803 | ssl->state = b; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 804 | } |
| 805 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 806 | return dtls1_write_change_cipher_spec(ssl, dtls1_use_current_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 807 | } |
| 808 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 809 | /* call this function when the buffered messages are no longer needed */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 810 | void dtls1_clear_record_buffer(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 811 | pitem *item; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 812 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 813 | for (item = pqueue_pop(ssl->d1->sent_messages); item != NULL; |
| 814 | item = pqueue_pop(ssl->d1->sent_messages)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 815 | dtls1_hm_fragment_free((hm_fragment *)item->data); |
| 816 | pitem_free(item); |
| 817 | } |
| 818 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 819 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 820 | /* don't actually do the writing, wait till the MTU has been retrieved */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 821 | void dtls1_set_message_header(SSL *ssl, uint8_t mt, unsigned long len, |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 822 | unsigned short seq_num, unsigned long frag_off, |
| 823 | unsigned long frag_len) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 824 | struct hm_header_st *msg_hdr = &ssl->d1->w_msg_hdr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 826 | msg_hdr->type = mt; |
| 827 | msg_hdr->msg_len = len; |
| 828 | msg_hdr->seq = seq_num; |
| 829 | msg_hdr->frag_off = frag_off; |
| 830 | msg_hdr->frag_len = frag_len; |
| 831 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 832 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 833 | unsigned int dtls1_min_mtu(void) { |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 834 | return kMinMTU; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 835 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 836 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 837 | int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
| 838 | CBS *out_body) { |
| 839 | memset(out_hdr, 0x00, sizeof(struct hm_header_st)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 840 | |
David Benjamin | c660417 | 2016-06-02 16:38:35 -0400 | [diff] [blame] | 841 | if (!CBS_get_u8(cbs, &out_hdr->type) || |
| 842 | !CBS_get_u24(cbs, &out_hdr->msg_len) || |
| 843 | !CBS_get_u16(cbs, &out_hdr->seq) || |
| 844 | !CBS_get_u24(cbs, &out_hdr->frag_off) || |
| 845 | !CBS_get_u24(cbs, &out_hdr->frag_len) || |
| 846 | !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) { |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | return 1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 851 | } |