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 | |
| 127 | #include "ssl_locl.h" |
| 128 | |
| 129 | #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8) |
| 130 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 131 | #define RSMBLY_BITMASK_MARK(bitmask, start, end) \ |
| 132 | { \ |
| 133 | if ((end) - (start) <= 8) { \ |
| 134 | long ii; \ |
| 135 | for (ii = (start); ii < (end); ii++) \ |
| 136 | bitmask[((ii) >> 3)] |= (1 << ((ii)&7)); \ |
| 137 | } else { \ |
| 138 | long ii; \ |
| 139 | bitmask[((start) >> 3)] |= bitmask_start_values[((start)&7)]; \ |
| 140 | for (ii = (((start) >> 3) + 1); ii < ((((end)-1)) >> 3); ii++) \ |
| 141 | bitmask[ii] = 0xff; \ |
| 142 | bitmask[(((end)-1) >> 3)] |= bitmask_end_values[((end)&7)]; \ |
| 143 | } \ |
| 144 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 146 | #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) \ |
| 147 | { \ |
| 148 | long ii; \ |
| 149 | assert((msg_len) > 0); \ |
| 150 | is_complete = 1; \ |
| 151 | if (bitmask[(((msg_len)-1) >> 3)] != bitmask_end_values[((msg_len)&7)]) \ |
| 152 | is_complete = 0; \ |
| 153 | if (is_complete) \ |
| 154 | for (ii = (((msg_len)-1) >> 3) - 1; ii >= 0; ii--) \ |
| 155 | if (bitmask[ii] != 0xff) { \ |
| 156 | is_complete = 0; \ |
| 157 | break; \ |
| 158 | } \ |
| 159 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 161 | static const uint8_t bitmask_start_values[] = {0xff, 0xfe, 0xfc, 0xf8, |
| 162 | 0xf0, 0xe0, 0xc0, 0x80}; |
| 163 | static const uint8_t bitmask_end_values[] = {0xff, 0x01, 0x03, 0x07, |
| 164 | 0x0f, 0x1f, 0x3f, 0x7f}; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 166 | /* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable |
| 167 | * for these values? Notably, why is kMinMTU a function of the transport |
| 168 | * protocol's overhead rather than, say, what's needed to hold a minimally-sized |
| 169 | * handshake fragment plus protocol overhead. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 171 | /* kMinMTU is the minimum acceptable MTU value. */ |
| 172 | static const unsigned int kMinMTU = 256 - 28; |
| 173 | |
| 174 | /* kDefaultMTU is the default MTU value to use if neither the user nor |
| 175 | * the underlying BIO supplies one. */ |
| 176 | static const unsigned int kDefaultMTU = 1500 - 28; |
| 177 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 178 | static void dtls1_fix_message_header(SSL *s, unsigned long frag_off, |
| 179 | unsigned long frag_len); |
| 180 | static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 181 | static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 183 | static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len, |
| 184 | int reassembly) { |
| 185 | hm_fragment *frag = NULL; |
| 186 | unsigned char *buf = NULL; |
| 187 | unsigned char *bitmask = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 189 | frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment)); |
| 190 | if (frag == NULL) { |
| 191 | return NULL; |
| 192 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 194 | if (frag_len) { |
| 195 | buf = (unsigned char *)OPENSSL_malloc(frag_len); |
| 196 | if (buf == NULL) { |
| 197 | OPENSSL_free(frag); |
| 198 | return NULL; |
| 199 | } |
| 200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 202 | /* zero length fragment gets zero frag->fragment */ |
| 203 | frag->fragment = buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 204 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 205 | /* Initialize reassembly bitmask if necessary */ |
| 206 | if (reassembly) { |
| 207 | bitmask = (unsigned char *)OPENSSL_malloc(RSMBLY_BITMASK_SIZE(frag_len)); |
| 208 | if (bitmask == NULL) { |
| 209 | if (buf != NULL) { |
| 210 | OPENSSL_free(buf); |
| 211 | } |
| 212 | OPENSSL_free(frag); |
| 213 | return NULL; |
| 214 | } |
| 215 | memset(bitmask, 0, RSMBLY_BITMASK_SIZE(frag_len)); |
| 216 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 218 | frag->reassembly = bitmask; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 220 | return frag; |
| 221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 223 | void dtls1_hm_fragment_free(hm_fragment *frag) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 224 | if (frag->fragment) { |
| 225 | OPENSSL_free(frag->fragment); |
| 226 | } |
| 227 | if (frag->reassembly) { |
| 228 | OPENSSL_free(frag->reassembly); |
| 229 | } |
| 230 | OPENSSL_free(frag); |
| 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 233 | /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or |
| 234 | * SSL3_RT_CHANGE_CIPHER_SPEC) */ |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 235 | int dtls1_do_write(SSL *s, int type) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 236 | int ret; |
| 237 | int curr_mtu; |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 238 | unsigned int len, frag_off; |
| 239 | size_t max_overhead = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 241 | /* AHA! Figure out the MTU, and stick to the right size */ |
| 242 | if (s->d1->mtu < dtls1_min_mtu() && |
| 243 | !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 244 | long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
| 245 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 246 | s->d1->mtu = (unsigned)mtu; |
| 247 | } else { |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 248 | s->d1->mtu = kDefaultMTU; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 249 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, s->d1->mtu, NULL); |
| 250 | } |
| 251 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 253 | /* should have something reasonable now */ |
| 254 | assert(s->d1->mtu >= dtls1_min_mtu()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 256 | if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) { |
| 257 | assert(s->init_num == |
| 258 | (int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH); |
| 259 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 261 | /* Determine the maximum overhead of the current cipher. */ |
| 262 | if (s->aead_write_ctx != NULL) { |
| 263 | max_overhead = EVP_AEAD_max_overhead(s->aead_write_ctx->ctx.aead); |
| 264 | if (s->aead_write_ctx->variable_nonce_included_in_record) { |
| 265 | max_overhead += s->aead_write_ctx->variable_nonce_len; |
| 266 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 267 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 269 | frag_off = 0; |
| 270 | while (s->init_num) { |
David Benjamin | 17a5f85 | 2015-01-11 19:46:52 -0500 | [diff] [blame] | 271 | /* Account for data in the buffering BIO; multiple records may be packed |
| 272 | * into a single packet during the handshake. |
| 273 | * |
| 274 | * TODO(davidben): This is buggy; if the MTU is larger than the buffer size, |
| 275 | * the large record will be split across two packets. Moreover, in that |
| 276 | * case, the |dtls1_write_bytes| call may not return synchronously. This |
| 277 | * will break on retry as the |s->init_off| and |s->init_num| adjustment |
| 278 | * will run a second time. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 279 | curr_mtu = s->d1->mtu - BIO_wpending(SSL_get_wbio(s)) - |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 280 | DTLS1_RT_HEADER_LENGTH - max_overhead; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 282 | if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) { |
David Benjamin | 17a5f85 | 2015-01-11 19:46:52 -0500 | [diff] [blame] | 283 | /* Flush the buffer and continue with a fresh packet. |
| 284 | * |
| 285 | * TODO(davidben): If |BIO_flush| is not synchronous and requires multiple |
| 286 | * calls to |dtls1_do_write|, |frag_off| will be wrong. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 287 | ret = BIO_flush(SSL_get_wbio(s)); |
| 288 | if (ret <= 0) { |
| 289 | return ret; |
| 290 | } |
David Benjamin | 17a5f85 | 2015-01-11 19:46:52 -0500 | [diff] [blame] | 291 | assert(BIO_wpending(SSL_get_wbio(s)) == 0); |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 292 | curr_mtu = s->d1->mtu - DTLS1_RT_HEADER_LENGTH - max_overhead; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 293 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 295 | /* XDTLS: this function is too long. split out the CCS part */ |
| 296 | if (type == SSL3_RT_HANDSHAKE) { |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 297 | /* If this isn't the first fragment, reserve space to prepend a new |
| 298 | * fragment header. This will override the body of a previous fragment. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 299 | if (s->init_off != 0) { |
| 300 | assert(s->init_off > DTLS1_HM_HEADER_LENGTH); |
| 301 | s->init_off -= DTLS1_HM_HEADER_LENGTH; |
| 302 | s->init_num += DTLS1_HM_HEADER_LENGTH; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 303 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 304 | |
David Benjamin | d9778fb | 2015-01-11 17:24:51 -0500 | [diff] [blame] | 305 | if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) { |
| 306 | /* To make forward progress, the MTU must, at minimum, fit the handshake |
| 307 | * header and one byte of handshake body. */ |
| 308 | OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL); |
| 309 | return -1; |
| 310 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | |
David Benjamin | d9778fb | 2015-01-11 17:24:51 -0500 | [diff] [blame] | 312 | if (s->init_num > curr_mtu) { |
| 313 | len = curr_mtu; |
| 314 | } else { |
| 315 | len = s->init_num; |
| 316 | } |
| 317 | assert(len >= DTLS1_HM_HEADER_LENGTH); |
| 318 | |
| 319 | dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 320 | dtls1_write_message_header( |
| 321 | s, (uint8_t *)&s->init_buf->data[s->init_off]); |
David Benjamin | d9778fb | 2015-01-11 17:24:51 -0500 | [diff] [blame] | 322 | } else { |
| 323 | assert(type == SSL3_RT_CHANGE_CIPHER_SPEC); |
| 324 | /* ChangeCipherSpec cannot be fragmented. */ |
| 325 | if (s->init_num > curr_mtu) { |
| 326 | OPENSSL_PUT_ERROR(SSL, dtls1_do_write, SSL_R_MTU_TOO_SMALL); |
| 327 | return -1; |
| 328 | } |
| 329 | len = s->init_num; |
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 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 332 | ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len); |
| 333 | if (ret < 0) { |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 334 | return -1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 335 | } |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 336 | |
| 337 | /* bad if this assert fails, only part of the handshake message got sent. |
| 338 | * But why would this happen? */ |
| 339 | assert(len == (unsigned int)ret); |
| 340 | |
| 341 | if (ret == s->init_num) { |
| 342 | if (s->msg_callback) { |
| 343 | s->msg_callback(1, s->version, type, s->init_buf->data, |
| 344 | (size_t)(s->init_off + s->init_num), s, |
| 345 | s->msg_callback_arg); |
| 346 | } |
| 347 | |
| 348 | s->init_off = 0; /* done writing this message */ |
| 349 | s->init_num = 0; |
| 350 | |
| 351 | return 1; |
| 352 | } |
| 353 | s->init_off += ret; |
| 354 | s->init_num -= ret; |
| 355 | frag_off += (ret -= DTLS1_HM_HEADER_LENGTH); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 360 | |
| 361 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 362 | /* Obtain handshake message of message type 'mt' (any if mt == -1), maximum |
| 363 | * acceptable body length 'max'. Read an entire handshake message. Handshake |
| 364 | * messages arrive in fragments. */ |
| 365 | long dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, |
| 366 | int hash_message, int *ok) { |
| 367 | int i, al; |
| 368 | struct hm_header_st *msg_hdr; |
| 369 | uint8_t *p; |
| 370 | unsigned long msg_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 372 | /* s3->tmp is used to store messages that are unexpected, caused |
| 373 | * by the absence of an optional handshake message */ |
| 374 | if (s->s3->tmp.reuse_message) { |
| 375 | /* A SSL_GET_MESSAGE_DONT_HASH_MESSAGE call cannot be combined |
| 376 | * with reuse_message; the SSL_GET_MESSAGE_DONT_HASH_MESSAGE |
| 377 | * would have to have been applied to the previous call. */ |
| 378 | assert(hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE); |
| 379 | s->s3->tmp.reuse_message = 0; |
| 380 | if (mt >= 0 && s->s3->tmp.message_type != mt) { |
| 381 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 382 | OPENSSL_PUT_ERROR(SSL, dtls1_get_message, SSL_R_UNEXPECTED_MESSAGE); |
| 383 | goto f_err; |
| 384 | } |
| 385 | *ok = 1; |
| 386 | s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
| 387 | s->init_num = (int)s->s3->tmp.message_size; |
| 388 | return s->init_num; |
| 389 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 391 | msg_hdr = &s->d1->r_msg_hdr; |
| 392 | memset(msg_hdr, 0x00, sizeof(struct hm_header_st)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
| 394 | again: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 395 | i = dtls1_get_message_fragment(s, stn, max, ok); |
| 396 | if (i == DTLS1_HM_BAD_FRAGMENT || |
| 397 | i == DTLS1_HM_FRAGMENT_RETRY) { |
| 398 | /* bad fragment received */ |
| 399 | goto again; |
| 400 | } else if (i <= 0 && !*ok) { |
| 401 | return i; |
| 402 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 404 | p = (uint8_t *)s->init_buf->data; |
| 405 | msg_len = msg_hdr->msg_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 407 | /* reconstruct message header */ |
| 408 | *(p++) = msg_hdr->type; |
| 409 | l2n3(msg_len, p); |
| 410 | s2n(msg_hdr->seq, p); |
| 411 | l2n3(0, p); |
| 412 | l2n3(msg_len, p); |
| 413 | p -= DTLS1_HM_HEADER_LENGTH; |
| 414 | msg_len += DTLS1_HM_HEADER_LENGTH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 416 | s->init_msg = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 417 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 418 | if (hash_message != SSL_GET_MESSAGE_DONT_HASH_MESSAGE) { |
| 419 | ssl3_hash_current_message(s); |
| 420 | } |
| 421 | if (s->msg_callback) { |
| 422 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, s, |
| 423 | s->msg_callback_arg); |
| 424 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 426 | memset(msg_hdr, 0x00, sizeof(struct hm_header_st)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 427 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 428 | s->d1->handshake_read_seq++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 429 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 430 | return s->init_num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 431 | |
| 432 | f_err: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 433 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 434 | *ok = 0; |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr, |
| 439 | int max) { |
| 440 | size_t frag_off, frag_len, msg_len; |
| 441 | |
| 442 | msg_len = msg_hdr->msg_len; |
| 443 | frag_off = msg_hdr->frag_off; |
| 444 | frag_len = msg_hdr->frag_len; |
| 445 | |
| 446 | /* sanity checking */ |
| 447 | if ((frag_off + frag_len) > msg_len) { |
| 448 | OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, |
| 449 | SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 450 | return SSL_AD_ILLEGAL_PARAMETER; |
| 451 | } |
| 452 | |
| 453 | if ((frag_off + frag_len) > (unsigned long)max) { |
| 454 | OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, |
| 455 | SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 456 | return SSL_AD_ILLEGAL_PARAMETER; |
| 457 | } |
| 458 | |
| 459 | if (s->d1->r_msg_hdr.frag_off == 0) { |
| 460 | /* first fragment */ |
| 461 | /* msg_len is limited to 2^24, but is effectively checked |
| 462 | * against max above */ |
| 463 | if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) { |
| 464 | OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, ERR_R_BUF_LIB); |
| 465 | return SSL_AD_INTERNAL_ERROR; |
| 466 | } |
| 467 | |
| 468 | s->s3->tmp.message_size = msg_len; |
| 469 | s->d1->r_msg_hdr.msg_len = msg_len; |
| 470 | s->s3->tmp.message_type = msg_hdr->type; |
| 471 | s->d1->r_msg_hdr.type = msg_hdr->type; |
| 472 | s->d1->r_msg_hdr.seq = msg_hdr->seq; |
| 473 | } else if (msg_len != s->d1->r_msg_hdr.msg_len) { |
| 474 | /* They must be playing with us! BTW, failure to enforce |
| 475 | * upper limit would open possibility for buffer overrun. */ |
| 476 | OPENSSL_PUT_ERROR(SSL, dtls1_preprocess_fragment, |
| 477 | SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 478 | return SSL_AD_ILLEGAL_PARAMETER; |
| 479 | } |
| 480 | |
| 481 | return 0; /* no error */ |
| 482 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
| 484 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 485 | static int dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) { |
| 486 | /* (0) check whether the desired fragment is available |
| 487 | * if so: |
| 488 | * (1) copy over the fragment to s->init_buf->data[] |
| 489 | * (2) update s->init_num */ |
| 490 | pitem *item; |
| 491 | hm_fragment *frag; |
| 492 | int al; |
| 493 | unsigned long frag_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 494 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 495 | *ok = 0; |
| 496 | item = pqueue_peek(s->d1->buffered_messages); |
| 497 | if (item == NULL) { |
| 498 | return 0; |
| 499 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 501 | frag = (hm_fragment *)item->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 503 | /* Don't return if reassembly still in progress */ |
| 504 | if (frag->reassembly != NULL) { |
| 505 | return 0; |
| 506 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 508 | if (s->d1->handshake_read_seq != frag->msg_header.seq) { |
| 509 | return 0; |
| 510 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 512 | frag_len = frag->msg_header.frag_len; |
| 513 | pqueue_pop(s->d1->buffered_messages); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 515 | al = dtls1_preprocess_fragment(s, &frag->msg_header, max); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 517 | if (al == 0) { |
| 518 | /* no alert */ |
| 519 | uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
| 520 | memcpy(&p[frag->msg_header.frag_off], frag->fragment, |
| 521 | frag->msg_header.frag_len); |
| 522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 524 | dtls1_hm_fragment_free(frag); |
| 525 | pitem_free(item); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 527 | if (al == 0) { |
| 528 | *ok = 1; |
| 529 | return frag_len; |
| 530 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 531 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 532 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 533 | s->init_num = 0; |
| 534 | *ok = 0; |
| 535 | return -1; |
| 536 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | |
Matt Caswell | 2306fe5 | 2014-06-06 14:25:52 -0700 | [diff] [blame] | 538 | /* dtls1_max_handshake_message_len returns the maximum number of bytes |
| 539 | * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but may |
| 540 | * be greater if the maximum certificate list size requires it. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 541 | static unsigned long dtls1_max_handshake_message_len(const SSL *s) { |
| 542 | unsigned long max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH; |
| 543 | if (max_len < (unsigned long)s->max_cert_list) { |
| 544 | return s->max_cert_list; |
| 545 | } |
| 546 | return max_len; |
| 547 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 548 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 549 | static int dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr, |
| 550 | int *ok) { |
| 551 | hm_fragment *frag = NULL; |
| 552 | pitem *item = NULL; |
| 553 | int i = -1, is_complete; |
| 554 | uint8_t seq64be[8]; |
| 555 | unsigned long frag_len = msg_hdr->frag_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 557 | if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len || |
| 558 | msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) { |
| 559 | goto err; |
| 560 | } |
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 | if (frag_len == 0) { |
| 563 | return DTLS1_HM_FRAGMENT_RETRY; |
| 564 | } |
Adam Langley | e951ff4 | 2014-06-06 14:30:33 -0700 | [diff] [blame] | 565 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 566 | /* Try to find item in queue */ |
| 567 | memset(seq64be, 0, sizeof(seq64be)); |
| 568 | seq64be[6] = (uint8_t)(msg_hdr->seq >> 8); |
| 569 | seq64be[7] = (uint8_t)msg_hdr->seq; |
| 570 | item = pqueue_find(s->d1->buffered_messages, seq64be); |
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 | if (item == NULL) { |
| 573 | frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1); |
| 574 | if (frag == NULL) { |
| 575 | goto err; |
| 576 | } |
| 577 | memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); |
| 578 | frag->msg_header.frag_len = frag->msg_header.msg_len; |
| 579 | frag->msg_header.frag_off = 0; |
| 580 | } else { |
| 581 | frag = (hm_fragment *)item->data; |
| 582 | if (frag->msg_header.msg_len != msg_hdr->msg_len) { |
| 583 | item = NULL; |
| 584 | frag = NULL; |
| 585 | goto err; |
| 586 | } |
| 587 | } |
Adam Langley | bed2214 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 588 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 589 | /* If message is already reassembled, this must be a |
| 590 | * retransmit and can be dropped. In this case item != NULL and so frag |
| 591 | * does not need to be freed. */ |
| 592 | if (frag->reassembly == NULL) { |
| 593 | uint8_t devnull[256]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 595 | assert(item != NULL); |
| 596 | while (frag_len) { |
| 597 | i = s->method->ssl_read_bytes( |
| 598 | s, SSL3_RT_HANDSHAKE, devnull, |
| 599 | frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0); |
| 600 | if (i <= 0) { |
| 601 | goto err; |
| 602 | } |
| 603 | frag_len -= i; |
| 604 | } |
| 605 | return DTLS1_HM_FRAGMENT_RETRY; |
| 606 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 607 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 608 | /* read the body of the fragment (header has already been read */ |
| 609 | i = s->method->ssl_read_bytes( |
| 610 | s, SSL3_RT_HANDSHAKE, frag->fragment + msg_hdr->frag_off, frag_len, 0); |
| 611 | if ((unsigned long)i != frag_len) { |
| 612 | i = -1; |
| 613 | } |
| 614 | if (i <= 0) { |
| 615 | goto err; |
| 616 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 617 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 618 | RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off, |
| 619 | (long)(msg_hdr->frag_off + frag_len)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 620 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 621 | RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len, |
| 622 | is_complete); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 624 | if (is_complete) { |
| 625 | OPENSSL_free(frag->reassembly); |
| 626 | frag->reassembly = NULL; |
| 627 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 629 | if (item == NULL) { |
| 630 | item = pitem_new(seq64be, frag); |
| 631 | if (item == NULL) { |
| 632 | i = -1; |
| 633 | goto err; |
| 634 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 636 | item = pqueue_insert(s->d1->buffered_messages, item); |
| 637 | /* pqueue_insert fails iff a duplicate item is inserted. |
| 638 | * However, |item| cannot be a duplicate. If it were, |
| 639 | * |pqueue_find|, above, would have returned it and control |
| 640 | * would never have reached this branch. */ |
| 641 | assert(item != NULL); |
| 642 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 643 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 644 | return DTLS1_HM_FRAGMENT_RETRY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
| 646 | err: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 647 | if (frag != NULL && item == NULL) { |
| 648 | dtls1_hm_fragment_free(frag); |
| 649 | } |
| 650 | *ok = 0; |
| 651 | return i; |
| 652 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 653 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 654 | static int dtls1_process_out_of_seq_message(SSL *s, |
| 655 | const struct hm_header_st *msg_hdr, |
| 656 | int *ok) { |
| 657 | int i = -1; |
| 658 | hm_fragment *frag = NULL; |
| 659 | pitem *item = NULL; |
| 660 | uint8_t seq64be[8]; |
| 661 | unsigned long frag_len = msg_hdr->frag_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 662 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 663 | if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) { |
| 664 | goto err; |
| 665 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 666 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 667 | /* Try to find item in queue, to prevent duplicate entries */ |
| 668 | memset(seq64be, 0, sizeof(seq64be)); |
| 669 | seq64be[6] = (uint8_t)(msg_hdr->seq >> 8); |
| 670 | seq64be[7] = (uint8_t)msg_hdr->seq; |
| 671 | item = pqueue_find(s->d1->buffered_messages, seq64be); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 672 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 673 | /* If we already have an entry and this one is a fragment, |
| 674 | * don't discard it and rather try to reassemble it. */ |
| 675 | if (item != NULL && frag_len != msg_hdr->msg_len) { |
| 676 | item = NULL; |
| 677 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 678 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 679 | /* Discard the message if sequence number was already there, is |
David Benjamin | f95ef93 | 2015-01-31 20:18:39 -0500 | [diff] [blame] | 680 | * too far in the future, or already in the queue. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 681 | if (msg_hdr->seq <= s->d1->handshake_read_seq || |
David Benjamin | f95ef93 | 2015-01-31 20:18:39 -0500 | [diff] [blame] | 682 | msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 683 | uint8_t devnull[256]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 684 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 685 | while (frag_len) { |
| 686 | i = s->method->ssl_read_bytes( |
| 687 | s, SSL3_RT_HANDSHAKE, devnull, |
| 688 | frag_len > sizeof(devnull) ? sizeof(devnull) : frag_len, 0); |
| 689 | if (i <= 0) { |
| 690 | goto err; |
| 691 | } |
| 692 | frag_len -= i; |
| 693 | } |
| 694 | } else { |
| 695 | if (frag_len != msg_hdr->msg_len) { |
| 696 | return dtls1_reassemble_fragment(s, msg_hdr, ok); |
| 697 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 698 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 699 | if (frag_len > dtls1_max_handshake_message_len(s)) { |
| 700 | goto err; |
| 701 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 702 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 703 | frag = dtls1_hm_fragment_new(frag_len, 0); |
| 704 | if (frag == NULL) { |
| 705 | goto err; |
| 706 | } |
Matt Caswell | 2306fe5 | 2014-06-06 14:25:52 -0700 | [diff] [blame] | 707 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 708 | memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 709 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 710 | if (frag_len) { |
| 711 | /* read the body of the fragment (header has already been read */ |
| 712 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, frag->fragment, |
| 713 | frag_len, 0); |
| 714 | if ((unsigned long)i != frag_len) { |
| 715 | i = -1; |
| 716 | } |
| 717 | if (i <= 0) { |
| 718 | goto err; |
| 719 | } |
| 720 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 721 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 722 | item = pitem_new(seq64be, frag); |
| 723 | if (item == NULL) { |
| 724 | goto err; |
| 725 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 726 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 727 | item = pqueue_insert(s->d1->buffered_messages, item); |
| 728 | /* pqueue_insert fails iff a duplicate item is inserted. |
| 729 | * However, |item| cannot be a duplicate. If it were, |
| 730 | * |pqueue_find|, above, would have returned it. Then, either |
| 731 | * |frag_len| != |msg_hdr->msg_len| in which case |item| is set |
| 732 | * to NULL and it will have been processed with |
| 733 | * |dtls1_reassemble_fragment|, above, or the record will have |
| 734 | * been discarded. */ |
| 735 | assert(item != NULL); |
| 736 | } |
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 | return DTLS1_HM_FRAGMENT_RETRY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | |
| 740 | err: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 741 | if (frag != NULL && item == NULL) { |
| 742 | dtls1_hm_fragment_free(frag); |
| 743 | } |
| 744 | *ok = 0; |
| 745 | return i; |
| 746 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 747 | |
| 748 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 749 | static long dtls1_get_message_fragment(SSL *s, int stn, long max, int *ok) { |
| 750 | uint8_t wire[DTLS1_HM_HEADER_LENGTH]; |
| 751 | unsigned long len, frag_off, frag_len; |
| 752 | int i, al; |
| 753 | struct hm_header_st msg_hdr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 754 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 755 | redo: |
| 756 | /* see if we have the required fragment already */ |
| 757 | if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) { |
| 758 | if (*ok) { |
| 759 | s->init_num = frag_len; |
| 760 | } |
| 761 | return frag_len; |
| 762 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 763 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 764 | /* read handshake message header */ |
| 765 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire, |
| 766 | DTLS1_HM_HEADER_LENGTH, 0); |
| 767 | if (i <= 0) { |
| 768 | /* nbio, or an error */ |
| 769 | s->rwstate = SSL_READING; |
| 770 | *ok = 0; |
| 771 | return i; |
| 772 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 773 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 774 | /* Handshake fails if message header is incomplete */ |
| 775 | if (i != DTLS1_HM_HEADER_LENGTH) { |
| 776 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 777 | OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, |
| 778 | SSL_R_UNEXPECTED_MESSAGE); |
| 779 | goto f_err; |
| 780 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 781 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 782 | /* parse the message fragment header */ |
| 783 | dtls1_get_message_header(wire, &msg_hdr); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 784 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 785 | /* if this is a future (or stale) message it gets buffered |
| 786 | * (or dropped)--no further processing at this time. */ |
| 787 | if (msg_hdr.seq != s->d1->handshake_read_seq) { |
| 788 | return dtls1_process_out_of_seq_message(s, &msg_hdr, ok); |
| 789 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 790 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 791 | len = msg_hdr.msg_len; |
| 792 | frag_off = msg_hdr.frag_off; |
| 793 | frag_len = msg_hdr.frag_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 795 | if (frag_len && frag_len < len) { |
| 796 | return dtls1_reassemble_fragment(s, &msg_hdr, ok); |
| 797 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 798 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 799 | if (!s->server && s->d1->r_msg_hdr.frag_off == 0 && |
| 800 | wire[0] == SSL3_MT_HELLO_REQUEST) { |
| 801 | /* The server may always send 'Hello Request' messages -- |
| 802 | * we are doing a handshake anyway now, so ignore them |
| 803 | * if their format is correct. Does not count for |
| 804 | * 'Finished' MAC. */ |
| 805 | if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) { |
| 806 | if (s->msg_callback) { |
| 807 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, wire, |
| 808 | DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg); |
| 809 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 810 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 811 | s->init_num = 0; |
| 812 | goto redo; |
| 813 | } else { |
| 814 | /* Incorrectly formated Hello request */ |
| 815 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 816 | OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, |
| 817 | SSL_R_UNEXPECTED_MESSAGE); |
| 818 | goto f_err; |
| 819 | } |
| 820 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 821 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 822 | if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) { |
| 823 | goto f_err; |
| 824 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 826 | /* XDTLS: ressurect this when restart is in place */ |
| 827 | s->state = stn; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 828 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 829 | if (frag_len > 0) { |
| 830 | uint8_t *p = (uint8_t *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 831 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 832 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[frag_off], frag_len, |
| 833 | 0); |
| 834 | /* XDTLS: fix this--message fragments cannot span multiple packets */ |
| 835 | if (i <= 0) { |
| 836 | s->rwstate = SSL_READING; |
| 837 | *ok = 0; |
| 838 | return i; |
| 839 | } |
| 840 | } else { |
| 841 | i = 0; |
| 842 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 843 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 844 | /* XDTLS: an incorrectly formatted fragment should cause the |
| 845 | * handshake to fail */ |
| 846 | if (i != (int)frag_len) { |
| 847 | al = SSL3_AD_ILLEGAL_PARAMETER; |
| 848 | OPENSSL_PUT_ERROR(SSL, dtls1_get_message_fragment, |
| 849 | SSL3_AD_ILLEGAL_PARAMETER); |
| 850 | goto f_err; |
| 851 | } |
| 852 | |
| 853 | *ok = 1; |
| 854 | |
| 855 | /* Note that s->init_num is *not* used as current offset in |
| 856 | * s->init_buf->data, but as a counter summing up fragments' |
| 857 | * lengths: as soon as they sum up to handshake packet |
| 858 | * length, we assume we have got all the fragments. */ |
| 859 | s->init_num = frag_len; |
| 860 | return frag_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
| 862 | f_err: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 863 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 864 | s->init_num = 0; |
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 | *ok = 0; |
| 867 | return -1; |
| 868 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 869 | |
| 870 | /* for these 2 messages, we need to |
| 871 | * ssl->enc_read_ctx re-init |
| 872 | * ssl->s3->read_sequence zero |
| 873 | * ssl->s3->read_mac_secret re-init |
| 874 | * ssl->session->read_sym_enc assign |
| 875 | * ssl->session->read_compression assign |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 876 | * ssl->session->read_hash assign */ |
| 877 | int dtls1_send_change_cipher_spec(SSL *s, int a, int b) { |
| 878 | uint8_t *p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 879 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 880 | if (s->state == a) { |
| 881 | p = (uint8_t *)s->init_buf->data; |
| 882 | *p++ = SSL3_MT_CCS; |
| 883 | s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
| 884 | s->init_num = DTLS1_CCS_HEADER_LENGTH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 885 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 886 | s->init_off = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 887 | |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 888 | dtls1_set_message_header(s, SSL3_MT_CCS, 0, s->d1->handshake_write_seq, 0, |
| 889 | 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 890 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 891 | /* buffer the message to handle re-xmits */ |
| 892 | dtls1_buffer_message(s, 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 894 | s->state = b; |
| 895 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 897 | /* SSL3_ST_CW_CHANGE_B */ |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 898 | return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 899 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 900 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 901 | int dtls1_read_failed(SSL *s, int code) { |
| 902 | if (code > 0) { |
| 903 | fprintf(stderr, "invalid state reached %s:%d", __FILE__, __LINE__); |
| 904 | return 1; |
| 905 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 906 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 907 | if (!dtls1_is_timer_expired(s)) { |
| 908 | /* not a timeout, none of our business, let higher layers handle this. In |
| 909 | * fact, it's probably an error */ |
| 910 | return code; |
| 911 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 912 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 913 | if (!SSL_in_init(s)) { |
| 914 | /* done, no need to send a retransmit */ |
| 915 | BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ); |
| 916 | return code; |
| 917 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 918 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 919 | return dtls1_handle_timeout(s); |
| 920 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 921 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 922 | int dtls1_get_queue_priority(unsigned short seq, int is_ccs) { |
| 923 | /* The index of the retransmission queue actually is the message sequence |
| 924 | * number, since the queue only contains messages of a single handshake. |
| 925 | * However, the ChangeCipherSpec has no message sequence number and so using |
| 926 | * only the sequence will result in the CCS and Finished having the same |
| 927 | * index. To prevent this, the sequence number is multiplied by 2. In case of |
| 928 | * a CCS 1 is subtracted. This does not only differ CSS and Finished, it also |
| 929 | * maintains the order of the index (important for priority queues) and fits |
| 930 | * in the unsigned short variable. */ |
| 931 | return seq * 2 - is_ccs; |
| 932 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 933 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 934 | int dtls1_retransmit_buffered_messages(SSL *s) { |
| 935 | pqueue sent = s->d1->sent_messages; |
| 936 | piterator iter; |
| 937 | pitem *item; |
| 938 | hm_fragment *frag; |
| 939 | int found = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 940 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 941 | iter = pqueue_iterator(sent); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 942 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 943 | for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) { |
| 944 | frag = (hm_fragment *)item->data; |
| 945 | if (dtls1_retransmit_message( |
| 946 | s, (unsigned short)dtls1_get_queue_priority( |
| 947 | frag->msg_header.seq, frag->msg_header.is_ccs), |
| 948 | 0, &found) <= 0 && |
| 949 | found) { |
| 950 | fprintf(stderr, "dtls1_retransmit_message() failed\n"); |
| 951 | return -1; |
| 952 | } |
| 953 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 954 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 955 | return 1; |
| 956 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 957 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 958 | int dtls1_buffer_message(SSL *s, int is_ccs) { |
| 959 | pitem *item; |
| 960 | hm_fragment *frag; |
| 961 | uint8_t seq64be[8]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 962 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 963 | /* this function is called immediately after a message has |
| 964 | * been serialized */ |
| 965 | assert(s->init_off == 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 966 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 967 | frag = dtls1_hm_fragment_new(s->init_num, 0); |
| 968 | if (!frag) { |
| 969 | return 0; |
| 970 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 971 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 972 | memcpy(frag->fragment, s->init_buf->data, s->init_num); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 973 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 974 | if (is_ccs) { |
| 975 | assert(s->d1->w_msg_hdr.msg_len + DTLS1_CCS_HEADER_LENGTH == |
| 976 | (unsigned int)s->init_num); |
| 977 | } else { |
| 978 | assert(s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH == |
| 979 | (unsigned int)s->init_num); |
| 980 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 981 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 982 | frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len; |
| 983 | frag->msg_header.seq = s->d1->w_msg_hdr.seq; |
| 984 | frag->msg_header.type = s->d1->w_msg_hdr.type; |
| 985 | frag->msg_header.frag_off = 0; |
| 986 | frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len; |
| 987 | frag->msg_header.is_ccs = is_ccs; |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame^] | 988 | frag->msg_header.epoch = s->d1->w_epoch; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 989 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 990 | memset(seq64be, 0, sizeof(seq64be)); |
| 991 | seq64be[6] = (uint8_t)( |
| 992 | dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs) >> |
| 993 | 8); |
| 994 | seq64be[7] = (uint8_t)( |
| 995 | dtls1_get_queue_priority(frag->msg_header.seq, frag->msg_header.is_ccs)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 996 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 997 | item = pitem_new(seq64be, frag); |
| 998 | if (item == NULL) { |
| 999 | dtls1_hm_fragment_free(frag); |
| 1000 | return 0; |
| 1001 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1002 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1003 | pqueue_insert(s->d1->sent_messages, item); |
| 1004 | return 1; |
| 1005 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1006 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1007 | int dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off, |
| 1008 | int *found) { |
| 1009 | int ret; |
| 1010 | /* XDTLS: for now assuming that read/writes are blocking */ |
| 1011 | pitem *item; |
| 1012 | hm_fragment *frag; |
| 1013 | unsigned long header_length; |
| 1014 | uint8_t seq64be[8]; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1015 | uint8_t save_write_sequence[8]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1016 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1017 | /* assert(s->init_num == 0); |
| 1018 | assert(s->init_off == 0); */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1019 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1020 | /* XDTLS: the requested message ought to be found, otherwise error */ |
| 1021 | memset(seq64be, 0, sizeof(seq64be)); |
| 1022 | seq64be[6] = (uint8_t)(seq >> 8); |
| 1023 | seq64be[7] = (uint8_t)seq; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1024 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1025 | item = pqueue_find(s->d1->sent_messages, seq64be); |
| 1026 | if (item == NULL) { |
| 1027 | fprintf(stderr, "retransmit: message %d non-existant\n", seq); |
| 1028 | *found = 0; |
| 1029 | return 0; |
| 1030 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1031 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1032 | *found = 1; |
| 1033 | frag = (hm_fragment *)item->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1034 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1035 | if (frag->msg_header.is_ccs) { |
| 1036 | header_length = DTLS1_CCS_HEADER_LENGTH; |
| 1037 | } else { |
| 1038 | header_length = DTLS1_HM_HEADER_LENGTH; |
| 1039 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1041 | memcpy(s->init_buf->data, frag->fragment, |
| 1042 | frag->msg_header.msg_len + header_length); |
| 1043 | s->init_num = frag->msg_header.msg_len + header_length; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1044 | |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 1045 | dtls1_set_message_header(s, frag->msg_header.type, |
| 1046 | frag->msg_header.msg_len, frag->msg_header.seq, |
| 1047 | 0, frag->msg_header.frag_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1048 | |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame^] | 1049 | /* Save current state. */ |
| 1050 | SSL_AEAD_CTX *aead_write_ctx = s->aead_write_ctx; |
| 1051 | uint16_t epoch = s->d1->w_epoch; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1052 | |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame^] | 1053 | /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1 |
| 1054 | * (negotiated cipher) exist. */ |
| 1055 | assert(epoch == 0 || epoch == 1); |
| 1056 | assert(frag->msg_header.epoch <= epoch); |
| 1057 | const int fragment_from_previous_epoch = (epoch == 1 && |
| 1058 | frag->msg_header.epoch == 0); |
| 1059 | if (fragment_from_previous_epoch) { |
| 1060 | /* Rewind to the previous epoch. |
| 1061 | * |
| 1062 | * TODO(davidben): Instead of swapping out connection-global state, this |
| 1063 | * logic should pass a "use previous epoch" parameter down to lower-level |
| 1064 | * functions. */ |
| 1065 | s->d1->w_epoch = frag->msg_header.epoch; |
| 1066 | s->aead_write_ctx = NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1067 | memcpy(save_write_sequence, s->s3->write_sequence, |
| 1068 | sizeof(s->s3->write_sequence)); |
| 1069 | memcpy(s->s3->write_sequence, s->d1->last_write_sequence, |
| 1070 | sizeof(s->s3->write_sequence)); |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame^] | 1071 | } else { |
| 1072 | /* Otherwise the messages must be from the same epoch. */ |
| 1073 | assert(frag->msg_header.epoch == epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | 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] | 1077 | : SSL3_RT_HANDSHAKE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1078 | |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame^] | 1079 | if (fragment_from_previous_epoch) { |
| 1080 | /* Restore the current epoch. */ |
| 1081 | s->aead_write_ctx = aead_write_ctx; |
| 1082 | s->d1->w_epoch = epoch; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1083 | memcpy(s->d1->last_write_sequence, s->s3->write_sequence, |
| 1084 | sizeof(s->s3->write_sequence)); |
| 1085 | memcpy(s->s3->write_sequence, save_write_sequence, |
| 1086 | sizeof(s->s3->write_sequence)); |
| 1087 | } |
| 1088 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1089 | (void)BIO_flush(SSL_get_wbio(s)); |
| 1090 | return ret; |
| 1091 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1092 | |
| 1093 | /* call this function when the buffered messages are no longer needed */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1094 | void dtls1_clear_record_buffer(SSL *s) { |
| 1095 | pitem *item; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1096 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1097 | for (item = pqueue_pop(s->d1->sent_messages); item != NULL; |
| 1098 | item = pqueue_pop(s->d1->sent_messages)) { |
| 1099 | dtls1_hm_fragment_free((hm_fragment *)item->data); |
| 1100 | pitem_free(item); |
| 1101 | } |
| 1102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1104 | /* 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] | 1105 | void dtls1_set_message_header(SSL *s, uint8_t mt, unsigned long len, |
| 1106 | unsigned short seq_num, unsigned long frag_off, |
| 1107 | unsigned long frag_len) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1108 | struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1109 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1110 | msg_hdr->type = mt; |
| 1111 | msg_hdr->msg_len = len; |
| 1112 | msg_hdr->seq = seq_num; |
| 1113 | msg_hdr->frag_off = frag_off; |
| 1114 | msg_hdr->frag_len = frag_len; |
| 1115 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1116 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1117 | static void dtls1_fix_message_header(SSL *s, unsigned long frag_off, |
| 1118 | unsigned long frag_len) { |
| 1119 | struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1120 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1121 | msg_hdr->frag_off = frag_off; |
| 1122 | msg_hdr->frag_len = frag_len; |
| 1123 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1124 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1125 | static uint8_t *dtls1_write_message_header(SSL *s, uint8_t *p) { |
| 1126 | struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1127 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1128 | *p++ = msg_hdr->type; |
| 1129 | l2n3(msg_hdr->msg_len, p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1130 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1131 | s2n(msg_hdr->seq, p); |
| 1132 | l2n3(msg_hdr->frag_off, p); |
| 1133 | l2n3(msg_hdr->frag_len, p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1134 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1135 | return p; |
| 1136 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1137 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1138 | unsigned int dtls1_min_mtu(void) { |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 1139 | return kMinMTU; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1140 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1141 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1142 | void dtls1_get_message_header(uint8_t *data, |
| 1143 | struct hm_header_st *msg_hdr) { |
| 1144 | memset(msg_hdr, 0x00, sizeof(struct hm_header_st)); |
| 1145 | msg_hdr->type = *(data++); |
| 1146 | n2l3(data, msg_hdr->msg_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1147 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1148 | n2s(data, msg_hdr->seq); |
| 1149 | n2l3(data, msg_hdr->frag_off); |
| 1150 | n2l3(data, msg_hdr->frag_len); |
| 1151 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1152 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1153 | void dtls1_get_ccs_header(uint8_t *data, struct ccs_header_st *ccs_hdr) { |
| 1154 | memset(ccs_hdr, 0x00, sizeof(struct ccs_header_st)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1155 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1156 | ccs_hdr->type = *(data++); |
| 1157 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1158 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 1159 | int dtls1_shutdown(SSL *s) { |
| 1160 | int ret; |
| 1161 | ret = ssl3_shutdown(s); |
| 1162 | return ret; |
| 1163 | } |