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