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