Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DTLS implementation written by Nagendra Modadugu |
| 3 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. |
| 4 | */ |
| 5 | /* ==================================================================== |
| 6 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * openssl-core@openssl.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). |
| 56 | * |
| 57 | */ |
| 58 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 59 | * All rights reserved. |
| 60 | * |
| 61 | * This package is an SSL implementation written |
| 62 | * by Eric Young (eay@cryptsoft.com). |
| 63 | * The implementation was written so as to conform with Netscapes SSL. |
| 64 | * |
| 65 | * This library is free for commercial and non-commercial use as long as |
| 66 | * the following conditions are aheared to. The following conditions |
| 67 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 68 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 69 | * included with this distribution is covered by the same copyright terms |
| 70 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 71 | * |
| 72 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 73 | * the code are not to be removed. |
| 74 | * If this package is used in a product, Eric Young should be given attribution |
| 75 | * as the author of the parts of the library used. |
| 76 | * This can be in the form of a textual message at program startup or |
| 77 | * in documentation (online or textual) provided with the package. |
| 78 | * |
| 79 | * Redistribution and use in source and binary forms, with or without |
| 80 | * modification, are permitted provided that the following conditions |
| 81 | * are met: |
| 82 | * 1. Redistributions of source code must retain the copyright |
| 83 | * notice, this list of conditions and the following disclaimer. |
| 84 | * 2. Redistributions in binary form must reproduce the above copyright |
| 85 | * notice, this list of conditions and the following disclaimer in the |
| 86 | * documentation and/or other materials provided with the distribution. |
| 87 | * 3. All advertising materials mentioning features or use of this software |
| 88 | * must display the following acknowledgement: |
| 89 | * "This product includes cryptographic software written by |
| 90 | * Eric Young (eay@cryptsoft.com)" |
| 91 | * The word 'cryptographic' can be left out if the rouines from the library |
| 92 | * being used are not cryptographic related :-). |
| 93 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 94 | * the apps directory (application code) you must include an acknowledgement: |
| 95 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 96 | * |
| 97 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 98 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 99 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 100 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 101 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 102 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 103 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 104 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 105 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 106 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 107 | * SUCH DAMAGE. |
| 108 | * |
| 109 | * The licence and distribution terms for any publically available version or |
| 110 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 111 | * copied and put under another distribution licence |
| 112 | * [including the GNU Public Licence.] */ |
| 113 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 114 | #include <openssl/ssl.h> |
| 115 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | #include <assert.h> |
| 117 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | #include <string.h> |
| 119 | |
| 120 | #include <openssl/buf.h> |
| 121 | #include <openssl/err.h> |
| 122 | #include <openssl/evp.h> |
| 123 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #include <openssl/rand.h> |
| 125 | #include <openssl/x509.h> |
| 126 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 127 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 130 | /* TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable |
| 131 | * for these values? Notably, why is kMinMTU a function of the transport |
| 132 | * protocol's overhead rather than, say, what's needed to hold a minimally-sized |
| 133 | * handshake fragment plus protocol overhead. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 135 | /* kMinMTU is the minimum acceptable MTU value. */ |
| 136 | static const unsigned int kMinMTU = 256 - 28; |
| 137 | |
| 138 | /* kDefaultMTU is the default MTU value to use if neither the user nor |
| 139 | * the underlying BIO supplies one. */ |
| 140 | static const unsigned int kDefaultMTU = 1500 - 28; |
| 141 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 142 | |
| 143 | /* Receiving handshake messages. */ |
| 144 | |
David Benjamin | ec847ce | 2016-06-17 19:30:47 -0400 | [diff] [blame] | 145 | static void dtls1_hm_fragment_free(hm_fragment *frag) { |
| 146 | if (frag == NULL) { |
| 147 | return; |
| 148 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 149 | OPENSSL_free(frag->data); |
David Benjamin | ec847ce | 2016-06-17 19:30:47 -0400 | [diff] [blame] | 150 | OPENSSL_free(frag->reassembly); |
| 151 | OPENSSL_free(frag); |
| 152 | } |
| 153 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 154 | static hm_fragment *dtls1_hm_fragment_new(const struct hm_header_st *msg_hdr) { |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 155 | hm_fragment *frag = OPENSSL_malloc(sizeof(hm_fragment)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 156 | if (frag == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 157 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 160 | memset(frag, 0, sizeof(hm_fragment)); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 161 | frag->type = msg_hdr->type; |
| 162 | frag->seq = msg_hdr->seq; |
| 163 | frag->msg_len = msg_hdr->msg_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 165 | /* Allocate space for the reassembled message and fill in the header. */ |
| 166 | frag->data = OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len); |
| 167 | if (frag->data == NULL) { |
| 168 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 169 | goto err; |
| 170 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 172 | CBB cbb; |
| 173 | if (!CBB_init_fixed(&cbb, frag->data, DTLS1_HM_HEADER_LENGTH) || |
| 174 | !CBB_add_u8(&cbb, msg_hdr->type) || |
| 175 | !CBB_add_u24(&cbb, msg_hdr->msg_len) || |
| 176 | !CBB_add_u16(&cbb, msg_hdr->seq) || |
| 177 | !CBB_add_u24(&cbb, 0 /* frag_off */) || |
| 178 | !CBB_add_u24(&cbb, msg_hdr->msg_len) || |
| 179 | !CBB_finish(&cbb, NULL, NULL)) { |
| 180 | CBB_cleanup(&cbb); |
| 181 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 182 | goto err; |
| 183 | } |
| 184 | |
| 185 | /* If the handshake message is empty, |frag->reassembly| is NULL. */ |
| 186 | if (msg_hdr->msg_len > 0) { |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 187 | /* Initialize reassembly bitmask. */ |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 188 | if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) { |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 189 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 190 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 191 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 192 | size_t bitmask_len = (msg_hdr->msg_len + 7) / 8; |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 193 | frag->reassembly = OPENSSL_malloc(bitmask_len); |
| 194 | if (frag->reassembly == NULL) { |
| 195 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 196 | goto err; |
| 197 | } |
| 198 | memset(frag->reassembly, 0, bitmask_len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 199 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 201 | return frag; |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 202 | |
| 203 | err: |
| 204 | dtls1_hm_fragment_free(frag); |
| 205 | return NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 206 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 208 | /* bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|, |
| 209 | * exclusive, set. */ |
David Benjamin | dca125e | 2016-06-23 17:52:47 -0400 | [diff] [blame] | 210 | static uint8_t bit_range(size_t start, size_t end) { |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 211 | return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1)); |
| 212 | } |
| 213 | |
| 214 | /* dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive, |
| 215 | * as received in |frag|. If |frag| becomes complete, it clears |
| 216 | * |frag->reassembly|. The range must be within the bounds of |frag|'s message |
| 217 | * and |frag->reassembly| must not be NULL. */ |
| 218 | static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start, |
| 219 | size_t end) { |
| 220 | size_t i; |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 221 | size_t msg_len = frag->msg_len; |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 222 | |
| 223 | if (frag->reassembly == NULL || start > end || end > msg_len) { |
| 224 | assert(0); |
| 225 | return; |
| 226 | } |
| 227 | /* A zero-length message will never have a pending reassembly. */ |
| 228 | assert(msg_len > 0); |
| 229 | |
| 230 | if ((start >> 3) == (end >> 3)) { |
| 231 | frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7); |
| 232 | } else { |
| 233 | frag->reassembly[start >> 3] |= bit_range(start & 7, 8); |
| 234 | for (i = (start >> 3) + 1; i < (end >> 3); i++) { |
| 235 | frag->reassembly[i] = 0xff; |
| 236 | } |
| 237 | if ((end & 7) != 0) { |
| 238 | frag->reassembly[end >> 3] |= bit_range(0, end & 7); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* Check if the fragment is complete. */ |
| 243 | for (i = 0; i < (msg_len >> 3); i++) { |
| 244 | if (frag->reassembly[i] != 0xff) { |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | if ((msg_len & 7) != 0 && |
| 249 | frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | OPENSSL_free(frag->reassembly); |
| 254 | frag->reassembly = NULL; |
| 255 | } |
| 256 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 257 | /* dtls1_is_current_message_complete returns one if the current handshake |
| 258 | * message is complete and zero otherwise. */ |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 259 | static int dtls1_is_current_message_complete(const SSL *ssl) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 260 | hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq % |
| 261 | SSL_MAX_HANDSHAKE_FLIGHT]; |
| 262 | return frag != NULL && frag->reassembly == NULL; |
| 263 | } |
| 264 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 265 | /* dtls1_pop_message removes the current handshake message, which must be |
| 266 | * complete, and advances to the next one. */ |
| 267 | static void dtls1_pop_message(SSL *ssl) { |
| 268 | assert(dtls1_is_current_message_complete(ssl)); |
| 269 | |
| 270 | size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 271 | dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]); |
| 272 | ssl->d1->incoming_messages[index] = NULL; |
| 273 | ssl->d1->handshake_read_seq++; |
| 274 | } |
| 275 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 276 | /* dtls1_get_incoming_message returns the incoming message corresponding to |
| 277 | * |msg_hdr|. If none exists, it creates a new one and inserts it in the |
| 278 | * queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It |
| 279 | * returns NULL on failure. The caller does not take ownership of the result. */ |
| 280 | static hm_fragment *dtls1_get_incoming_message( |
| 281 | SSL *ssl, const struct hm_header_st *msg_hdr) { |
| 282 | if (msg_hdr->seq < ssl->d1->handshake_read_seq || |
| 283 | msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) { |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 288 | hm_fragment *frag = ssl->d1->incoming_messages[idx]; |
| 289 | if (frag != NULL) { |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 290 | assert(frag->seq == msg_hdr->seq); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 291 | /* The new fragment must be compatible with the previous fragments from this |
| 292 | * message. */ |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 293 | if (frag->type != msg_hdr->type || |
| 294 | frag->msg_len != msg_hdr->msg_len) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 295 | OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH); |
| 296 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 297 | return NULL; |
| 298 | } |
| 299 | return frag; |
| 300 | } |
| 301 | |
| 302 | /* This is the first fragment from this message. */ |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 303 | frag = dtls1_hm_fragment_new(msg_hdr); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 304 | if (frag == NULL) { |
| 305 | return NULL; |
| 306 | } |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 307 | ssl->d1->incoming_messages[idx] = frag; |
| 308 | return frag; |
| 309 | } |
| 310 | |
| 311 | /* dtls1_process_handshake_record reads a handshake record and processes it. It |
| 312 | * returns one if the record was successfully processed and 0 or -1 on error. */ |
| 313 | static int dtls1_process_handshake_record(SSL *ssl) { |
| 314 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 315 | |
| 316 | start: |
| 317 | if (rr->length == 0) { |
| 318 | int ret = dtls1_get_record(ssl); |
| 319 | if (ret <= 0) { |
| 320 | return ret; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* Cross-epoch records are discarded, but we may receive out-of-order |
| 325 | * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec |
| 326 | * before the appropriate point in the handshake. Those must be silently |
| 327 | * discarded. |
| 328 | * |
| 329 | * However, only allow the out-of-order records in the correct epoch. |
| 330 | * Application data must come in the encrypted epoch, and ChangeCipherSpec in |
| 331 | * the unencrypted epoch (we never renegotiate). Other cases fall through and |
| 332 | * fail with a fatal error. */ |
| 333 | if ((rr->type == SSL3_RT_APPLICATION_DATA && |
| 334 | ssl->s3->aead_read_ctx != NULL) || |
| 335 | (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC && |
| 336 | ssl->s3->aead_read_ctx == NULL)) { |
| 337 | rr->length = 0; |
| 338 | goto start; |
| 339 | } |
| 340 | |
| 341 | if (rr->type != SSL3_RT_HANDSHAKE) { |
| 342 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 343 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | CBS cbs; |
| 348 | CBS_init(&cbs, rr->data, rr->length); |
| 349 | |
| 350 | while (CBS_len(&cbs) > 0) { |
| 351 | /* Read a handshake fragment. */ |
| 352 | struct hm_header_st msg_hdr; |
| 353 | CBS body; |
| 354 | if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
| 355 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
| 356 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | const size_t frag_off = msg_hdr.frag_off; |
| 361 | const size_t frag_len = msg_hdr.frag_len; |
| 362 | const size_t msg_len = msg_hdr.msg_len; |
| 363 | if (frag_off > msg_len || frag_off + frag_len < frag_off || |
| 364 | frag_off + frag_len > msg_len || |
| 365 | msg_len > ssl_max_handshake_message_len(ssl)) { |
| 366 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 367 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 368 | return -1; |
| 369 | } |
| 370 | |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame^] | 371 | /* The encrypted epoch in DTLS has only one handshake message. */ |
| 372 | if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) { |
| 373 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 374 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 375 | return -1; |
| 376 | } |
| 377 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 378 | if (msg_hdr.seq < ssl->d1->handshake_read_seq || |
| 379 | msg_hdr.seq > |
| 380 | (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) { |
| 381 | /* Ignore fragments from the past, or ones too far in the future. */ |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | hm_fragment *frag = dtls1_get_incoming_message(ssl, &msg_hdr); |
| 386 | if (frag == NULL) { |
| 387 | return -1; |
| 388 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 389 | assert(frag->msg_len == msg_len); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 390 | |
| 391 | if (frag->reassembly == NULL) { |
| 392 | /* The message is already assembled. */ |
| 393 | continue; |
| 394 | } |
| 395 | assert(msg_len > 0); |
| 396 | |
| 397 | /* Copy the body into the fragment. */ |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 398 | memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off, CBS_data(&body), |
| 399 | CBS_len(&body)); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 400 | dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len); |
| 401 | } |
| 402 | |
| 403 | rr->length = 0; |
| 404 | ssl_read_buffer_discard(ssl); |
| 405 | return 1; |
| 406 | } |
| 407 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 408 | int dtls1_get_message(SSL *ssl, int msg_type, |
| 409 | enum ssl_hash_message_t hash_message) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 410 | if (ssl->s3->tmp.reuse_message) { |
| 411 | /* A ssl_dont_hash_message call cannot be combined with reuse_message; the |
| 412 | * ssl_dont_hash_message would have to have been applied to the previous |
| 413 | * call. */ |
| 414 | assert(hash_message == ssl_hash_message); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 415 | assert(dtls1_is_current_message_complete(ssl)); |
| 416 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 417 | ssl->s3->tmp.reuse_message = 0; |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 418 | hash_message = ssl_dont_hash_message; |
| 419 | } else if (dtls1_is_current_message_complete(ssl)) { |
| 420 | dtls1_pop_message(ssl); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 421 | } |
| 422 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 423 | /* Process handshake records until the current message is ready. */ |
| 424 | while (!dtls1_is_current_message_complete(ssl)) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 425 | int ret = dtls1_process_handshake_record(ssl); |
| 426 | if (ret <= 0) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 427 | return ret; |
| 428 | } |
| 429 | } |
| 430 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 431 | hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq % |
| 432 | SSL_MAX_HANDSHAKE_FLIGHT]; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 433 | assert(frag != NULL); |
| 434 | assert(frag->reassembly == NULL); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 435 | assert(ssl->d1->handshake_read_seq == frag->seq); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 436 | |
| 437 | /* TODO(davidben): This function has a lot of implicit outputs. Simplify the |
| 438 | * |ssl_get_message| API. */ |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 439 | ssl->s3->tmp.message_type = frag->type; |
| 440 | ssl->init_msg = frag->data + DTLS1_HM_HEADER_LENGTH; |
| 441 | ssl->init_num = frag->msg_len; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 442 | |
| 443 | if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) { |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 444 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 445 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 446 | return -1; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 447 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 448 | if (hash_message == ssl_hash_message && !dtls1_hash_current_message(ssl)) { |
| 449 | return -1; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_HANDSHAKE, |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 453 | frag->data, ssl->init_num + DTLS1_HM_HEADER_LENGTH); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 454 | return 1; |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 455 | } |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 456 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 457 | int dtls1_hash_current_message(SSL *ssl) { |
| 458 | assert(dtls1_is_current_message_complete(ssl)); |
| 459 | |
| 460 | hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq % |
| 461 | SSL_MAX_HANDSHAKE_FLIGHT]; |
| 462 | return ssl3_update_handshake_hash(ssl, frag->data, |
| 463 | DTLS1_HM_HEADER_LENGTH + frag->msg_len); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void dtls_clear_incoming_messages(SSL *ssl) { |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 467 | for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 468 | dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]); |
| 469 | ssl->d1->incoming_messages[i] = NULL; |
| 470 | } |
| 471 | } |
| 472 | |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 473 | int dtls_has_incoming_messages(const SSL *ssl) { |
| 474 | /* This function may not be called if there is a pending |dtls1_get_message| |
| 475 | * operation. */ |
| 476 | assert(dtls1_is_current_message_complete(ssl)); |
| 477 | |
| 478 | size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 479 | for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) { |
| 480 | if (i != current && ssl->d1->incoming_messages[i] != NULL) { |
| 481 | return 1; |
| 482 | } |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 487 | int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
| 488 | CBS *out_body) { |
| 489 | memset(out_hdr, 0x00, sizeof(struct hm_header_st)); |
| 490 | |
| 491 | if (!CBS_get_u8(cbs, &out_hdr->type) || |
| 492 | !CBS_get_u24(cbs, &out_hdr->msg_len) || |
| 493 | !CBS_get_u16(cbs, &out_hdr->seq) || |
| 494 | !CBS_get_u24(cbs, &out_hdr->frag_off) || |
| 495 | !CBS_get_u24(cbs, &out_hdr->frag_len) || |
| 496 | !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) { |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | return 1; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /* Sending handshake messages. */ |
| 505 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 506 | static void dtls1_update_mtu(SSL *ssl) { |
| 507 | /* TODO(davidben): What is this code doing and do we need it? */ |
| 508 | if (ssl->d1->mtu < dtls1_min_mtu() && |
| 509 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 510 | long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 511 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 512 | ssl->d1->mtu = (unsigned)mtu; |
| 513 | } else { |
| 514 | ssl->d1->mtu = kDefaultMTU; |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 515 | BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /* The MTU should be above the minimum now. */ |
| 520 | assert(ssl->d1->mtu >= dtls1_min_mtu()); |
| 521 | } |
| 522 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 523 | /* dtls1_max_record_size returns the maximum record body length that may be |
| 524 | * written without exceeding the MTU. It accounts for any buffering installed on |
| 525 | * the write BIO. If no record may be written, it returns zero. */ |
| 526 | static size_t dtls1_max_record_size(SSL *ssl) { |
| 527 | size_t ret = ssl->d1->mtu; |
| 528 | |
| 529 | size_t overhead = ssl_max_seal_overhead(ssl); |
| 530 | if (ret <= overhead) { |
| 531 | return 0; |
| 532 | } |
| 533 | ret -= overhead; |
| 534 | |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 535 | size_t pending = BIO_wpending(ssl->wbio); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 536 | if (ret <= pending) { |
| 537 | return 0; |
| 538 | } |
| 539 | ret -= pending; |
| 540 | |
| 541 | return ret; |
| 542 | } |
| 543 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 544 | static int dtls1_write_change_cipher_spec(SSL *ssl, |
| 545 | enum dtls1_use_epoch_t use_epoch) { |
| 546 | dtls1_update_mtu(ssl); |
| 547 | |
| 548 | /* During the handshake, wbio is buffered to pack messages together. Flush the |
| 549 | * buffer if the ChangeCipherSpec would not fit in a packet. */ |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 550 | if (dtls1_max_record_size(ssl) == 0) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 551 | int ret = BIO_flush(ssl->wbio); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 552 | if (ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 553 | ssl->rwstate = SSL_WRITING; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 554 | return ret; |
| 555 | } |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 559 | int ret = |
David Benjamin | 45d45c1 | 2016-06-07 15:20:49 -0400 | [diff] [blame] | 560 | dtls1_write_record(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 561 | sizeof(kChangeCipherSpec), use_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 562 | if (ret <= 0) { |
| 563 | return ret; |
| 564 | } |
| 565 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 566 | ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, |
| 567 | SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 568 | sizeof(kChangeCipherSpec)); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 569 | return 1; |
| 570 | } |
| 571 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 572 | /* dtls1_do_handshake_write writes handshake message |in| using the given epoch, |
| 573 | * starting |offset| bytes into the message body. It returns one on success. On |
| 574 | * error, it returns <= 0 and sets |*out_offset| to the number of bytes of body |
| 575 | * that were successfully written. This may be used to retry the write |
| 576 | * later. |in| must be a reassembled handshake message with the full DTLS |
| 577 | * handshake header. */ |
| 578 | static int dtls1_do_handshake_write(SSL *ssl, size_t *out_offset, |
| 579 | const uint8_t *in, size_t offset, |
| 580 | size_t len, |
| 581 | enum dtls1_use_epoch_t use_epoch) { |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 582 | dtls1_update_mtu(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 583 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 584 | int ret = -1; |
| 585 | CBB cbb; |
| 586 | CBB_zero(&cbb); |
| 587 | /* Allocate a temporary buffer to hold the message fragments to avoid |
| 588 | * clobbering the message. */ |
| 589 | uint8_t *buf = OPENSSL_malloc(ssl->d1->mtu); |
| 590 | if (buf == NULL) { |
| 591 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 592 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 594 | /* Although it may be sent as multiple fragments, a DTLS message must be sent |
| 595 | * serialized as a single fragment for purposes of |ssl_do_msg_callback| and |
| 596 | * the handshake hash. */ |
| 597 | CBS cbs, body; |
| 598 | struct hm_header_st hdr; |
| 599 | CBS_init(&cbs, in, len); |
| 600 | if (!dtls1_parse_fragment(&cbs, &hdr, &body) || |
| 601 | hdr.frag_off != 0 || |
| 602 | hdr.frag_len != CBS_len(&body) || |
| 603 | hdr.msg_len != CBS_len(&body) || |
| 604 | !CBS_skip(&body, offset) || |
| 605 | CBS_len(&cbs) != 0) { |
| 606 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 607 | goto err; |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 608 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 609 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 610 | do { |
| 611 | /* During the handshake, wbio is buffered to pack messages together. Flush |
| 612 | * the buffer if there isn't enough room to make progress. */ |
| 613 | if (dtls1_max_record_size(ssl) < DTLS1_HM_HEADER_LENGTH + 1) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 614 | int flush_ret = BIO_flush(ssl->wbio); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 615 | if (flush_ret <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 616 | ssl->rwstate = SSL_WRITING; |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 617 | ret = flush_ret; |
| 618 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 619 | } |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 620 | assert(BIO_wpending(ssl->wbio) == 0); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 621 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 622 | |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 623 | size_t todo = dtls1_max_record_size(ssl); |
| 624 | if (todo < DTLS1_HM_HEADER_LENGTH + 1) { |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 625 | /* To make forward progress, the MTU must, at minimum, fit the handshake |
| 626 | * header and one byte of handshake body. */ |
| 627 | OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 628 | goto err; |
| 629 | } |
| 630 | todo -= DTLS1_HM_HEADER_LENGTH; |
| 631 | |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 632 | if (todo > CBS_len(&body)) { |
| 633 | todo = CBS_len(&body); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 634 | } |
| 635 | if (todo >= (1u << 24)) { |
| 636 | todo = (1u << 24) - 1; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 637 | } |
| 638 | |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 639 | size_t buf_len; |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 640 | if (!CBB_init_fixed(&cbb, buf, ssl->d1->mtu) || |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 641 | !CBB_add_u8(&cbb, hdr.type) || |
| 642 | !CBB_add_u24(&cbb, hdr.msg_len) || |
| 643 | !CBB_add_u16(&cbb, hdr.seq) || |
| 644 | !CBB_add_u24(&cbb, offset) || |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 645 | !CBB_add_u24(&cbb, todo) || |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 646 | !CBB_add_bytes(&cbb, CBS_data(&body), todo) || |
| 647 | !CBB_finish(&cbb, NULL, &buf_len)) { |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 648 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 649 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 650 | } |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 651 | |
David Benjamin | 45d45c1 | 2016-06-07 15:20:49 -0400 | [diff] [blame] | 652 | int write_ret = |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 653 | dtls1_write_record(ssl, SSL3_RT_HANDSHAKE, buf, buf_len, use_epoch); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 654 | if (write_ret <= 0) { |
| 655 | ret = write_ret; |
| 656 | goto err; |
David Benjamin | 5a3cc03 | 2015-01-11 19:25:32 -0500 | [diff] [blame] | 657 | } |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 658 | |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 659 | if (!CBS_skip(&body, todo)) { |
| 660 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 661 | goto err; |
| 662 | } |
| 663 | offset += todo; |
| 664 | } while (CBS_len(&body) != 0); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 665 | |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 666 | ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_HANDSHAKE, in, |
| 667 | len); |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 668 | |
| 669 | ret = 1; |
| 670 | |
| 671 | err: |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 672 | *out_offset = offset; |
David Benjamin | 16285ea | 2015-11-03 15:39:45 -0500 | [diff] [blame] | 673 | CBB_cleanup(&cbb); |
| 674 | OPENSSL_free(buf); |
| 675 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 676 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 678 | void dtls_clear_outgoing_messages(SSL *ssl) { |
| 679 | size_t i; |
| 680 | for (i = 0; i < ssl->d1->outgoing_messages_len; i++) { |
| 681 | OPENSSL_free(ssl->d1->outgoing_messages[i].data); |
| 682 | ssl->d1->outgoing_messages[i].data = NULL; |
| 683 | } |
| 684 | ssl->d1->outgoing_messages_len = 0; |
| 685 | } |
| 686 | |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 687 | /* dtls1_add_change_cipher_spec adds a ChangeCipherSpec to the current |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 688 | * handshake flight. */ |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 689 | static int dtls1_add_change_cipher_spec(SSL *ssl) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 690 | if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) { |
| 691 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | DTLS_OUTGOING_MESSAGE *msg = |
| 696 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len]; |
| 697 | msg->data = NULL; |
| 698 | msg->len = 0; |
| 699 | msg->epoch = ssl->d1->w_epoch; |
| 700 | msg->is_ccs = 1; |
| 701 | |
| 702 | ssl->d1->outgoing_messages_len++; |
| 703 | return 1; |
| 704 | } |
| 705 | |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 706 | static int dtls1_add_message(SSL *ssl, uint8_t *data, size_t len) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 707 | if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT) { |
| 708 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 709 | OPENSSL_free(data); |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | DTLS_OUTGOING_MESSAGE *msg = |
| 714 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len]; |
| 715 | msg->data = data; |
| 716 | msg->len = len; |
| 717 | msg->epoch = ssl->d1->w_epoch; |
| 718 | msg->is_ccs = 0; |
| 719 | |
| 720 | ssl->d1->outgoing_messages_len++; |
| 721 | return 1; |
| 722 | } |
| 723 | |
| 724 | int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
| 725 | /* Pick a modest size hint to save most of the |realloc| calls. */ |
| 726 | if (!CBB_init(cbb, 64) || |
| 727 | !CBB_add_u8(cbb, type) || |
| 728 | !CBB_add_u24(cbb, 0 /* length (filled in later) */) || |
| 729 | !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) || |
| 730 | !CBB_add_u24(cbb, 0 /* offset */) || |
| 731 | !CBB_add_u24_length_prefixed(cbb, body)) { |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | return 1; |
| 736 | } |
| 737 | |
| 738 | int dtls1_finish_message(SSL *ssl, CBB *cbb) { |
| 739 | uint8_t *msg = NULL; |
| 740 | size_t len; |
| 741 | if (!CBB_finish(cbb, &msg, &len) || |
| 742 | len > 0xffffffffu || |
| 743 | len < DTLS1_HM_HEADER_LENGTH) { |
| 744 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 745 | OPENSSL_free(msg); |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /* Fix up the header. Copy the fragment length into the total message |
| 750 | * length. */ |
| 751 | memcpy(msg + 1, msg + DTLS1_HM_HEADER_LENGTH - 3, 3); |
| 752 | |
| 753 | ssl3_update_handshake_hash(ssl, msg, len); |
| 754 | |
| 755 | ssl->d1->handshake_write_seq++; |
| 756 | ssl->init_off = 0; |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 757 | return dtls1_add_message(ssl, msg, len); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | int dtls1_write_message(SSL *ssl) { |
| 761 | if (ssl->d1->outgoing_messages_len == 0) { |
| 762 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 763 | return -1; |
| 764 | } |
| 765 | |
| 766 | const DTLS_OUTGOING_MESSAGE *msg = |
| 767 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len - 1]; |
| 768 | if (msg->is_ccs) { |
| 769 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | size_t offset = ssl->init_off; |
| 774 | int ret = dtls1_do_handshake_write(ssl, &offset, msg->data, offset, msg->len, |
| 775 | dtls1_use_current_epoch); |
| 776 | ssl->init_off = offset; |
| 777 | return ret; |
| 778 | } |
| 779 | |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 780 | static int dtls1_retransmit_message(SSL *ssl, |
| 781 | const DTLS_OUTGOING_MESSAGE *msg) { |
David Benjamin | afbc63f | 2015-02-01 02:33:59 -0500 | [diff] [blame] | 782 | /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1 |
| 783 | * (negotiated cipher) exist. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 784 | assert(ssl->d1->w_epoch == 0 || ssl->d1->w_epoch == 1); |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 785 | assert(msg->epoch <= ssl->d1->w_epoch); |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 786 | enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch; |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 787 | if (ssl->d1->w_epoch == 1 && msg->epoch == 0) { |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 788 | use_epoch = dtls1_use_previous_epoch; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 789 | } |
| 790 | |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 791 | /* TODO(davidben): This cannot handle non-blocking writes. */ |
| 792 | int ret; |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 793 | if (msg->is_ccs) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 794 | ret = dtls1_write_change_cipher_spec(ssl, use_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 795 | } else { |
David Benjamin | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 796 | size_t offset = 0; |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 797 | ret = dtls1_do_handshake_write(ssl, &offset, msg->data, offset, msg->len, |
| 798 | use_epoch); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 801 | return ret; |
| 802 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 803 | |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 804 | int dtls1_retransmit_outgoing_messages(SSL *ssl) { |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 805 | /* Ensure we are packing handshake messages. */ |
| 806 | const int was_buffered = ssl_is_wbio_buffered(ssl); |
| 807 | assert(was_buffered == SSL_in_init(ssl)); |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 808 | if (!was_buffered && !ssl_init_wbio_buffer(ssl)) { |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 809 | return -1; |
| 810 | } |
| 811 | assert(ssl_is_wbio_buffered(ssl)); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 812 | |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 813 | int ret = -1; |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 814 | size_t i; |
| 815 | for (i = 0; i < ssl->d1->outgoing_messages_len; i++) { |
| 816 | if (dtls1_retransmit_message(ssl, &ssl->d1->outgoing_messages[i]) <= 0) { |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 817 | goto err; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 821 | ret = BIO_flush(ssl->wbio); |
David Benjamin | 2730955 | 2016-05-05 21:17:53 -0400 | [diff] [blame] | 822 | if (ret <= 0) { |
| 823 | ssl->rwstate = SSL_WRITING; |
| 824 | goto err; |
| 825 | } |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 826 | |
| 827 | err: |
| 828 | if (!was_buffered) { |
| 829 | ssl_free_wbio_buffer(ssl); |
| 830 | } |
| 831 | return ret; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 832 | } |
| 833 | |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 834 | int dtls1_send_change_cipher_spec(SSL *ssl) { |
| 835 | int ret = dtls1_write_change_cipher_spec(ssl, dtls1_use_current_epoch); |
| 836 | if (ret <= 0) { |
| 837 | return ret; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 838 | } |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 839 | dtls1_add_change_cipher_spec(ssl); |
| 840 | return 1; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 843 | unsigned int dtls1_min_mtu(void) { |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 844 | return kMinMTU; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 845 | } |