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