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