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