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> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 126 | #include "../crypto/internal.h" |
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 | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 130 | namespace bssl { |
| 131 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 132 | // TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable |
| 133 | // for these values? Notably, why is kMinMTU a function of the transport |
| 134 | // protocol's overhead rather than, say, what's needed to hold a minimally-sized |
| 135 | // handshake fragment plus protocol overhead. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 137 | // kMinMTU is the minimum acceptable MTU value. |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 138 | static const unsigned int kMinMTU = 256 - 28; |
| 139 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 140 | // kDefaultMTU is the default MTU value to use if neither the user nor |
| 141 | // the underlying BIO supplies one. |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 142 | static const unsigned int kDefaultMTU = 1500 - 28; |
| 143 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 144 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 145 | // Receiving handshake messages. |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 146 | |
David Benjamin | ec847ce | 2016-06-17 19:30:47 -0400 | [diff] [blame] | 147 | static void dtls1_hm_fragment_free(hm_fragment *frag) { |
| 148 | if (frag == NULL) { |
| 149 | return; |
| 150 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 151 | OPENSSL_free(frag->data); |
David Benjamin | ec847ce | 2016-06-17 19:30:47 -0400 | [diff] [blame] | 152 | OPENSSL_free(frag->reassembly); |
| 153 | OPENSSL_free(frag); |
| 154 | } |
| 155 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 156 | static hm_fragment *dtls1_hm_fragment_new(const struct hm_header_st *msg_hdr) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 157 | ScopedCBB cbb; |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 158 | hm_fragment *frag = (hm_fragment *)OPENSSL_malloc(sizeof(hm_fragment)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 159 | if (frag == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 160 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 163 | OPENSSL_memset(frag, 0, sizeof(hm_fragment)); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 164 | frag->type = msg_hdr->type; |
| 165 | frag->seq = msg_hdr->seq; |
| 166 | frag->msg_len = msg_hdr->msg_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 168 | // Allocate space for the reassembled message and fill in the header. |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 169 | frag->data = |
| 170 | (uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 171 | if (frag->data == NULL) { |
| 172 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 173 | goto err; |
| 174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 176 | if (!CBB_init_fixed(cbb.get(), frag->data, DTLS1_HM_HEADER_LENGTH) || |
| 177 | !CBB_add_u8(cbb.get(), msg_hdr->type) || |
| 178 | !CBB_add_u24(cbb.get(), msg_hdr->msg_len) || |
| 179 | !CBB_add_u16(cbb.get(), msg_hdr->seq) || |
| 180 | !CBB_add_u24(cbb.get(), 0 /* frag_off */) || |
| 181 | !CBB_add_u24(cbb.get(), msg_hdr->msg_len) || |
| 182 | !CBB_finish(cbb.get(), NULL, NULL)) { |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 183 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 184 | goto err; |
| 185 | } |
| 186 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 187 | // If the handshake message is empty, |frag->reassembly| is NULL. |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 188 | if (msg_hdr->msg_len > 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 189 | // Initialize reassembly bitmask. |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 190 | if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) { |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 191 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 192 | goto err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 193 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 194 | size_t bitmask_len = (msg_hdr->msg_len + 7) / 8; |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 195 | frag->reassembly = (uint8_t *)OPENSSL_malloc(bitmask_len); |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 196 | if (frag->reassembly == NULL) { |
| 197 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 198 | goto err; |
| 199 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 200 | OPENSSL_memset(frag->reassembly, 0, bitmask_len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 201 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 202 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 203 | return frag; |
David Benjamin | c99807d | 2015-09-12 18:21:35 -0400 | [diff] [blame] | 204 | |
| 205 | err: |
| 206 | dtls1_hm_fragment_free(frag); |
| 207 | return NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 210 | // bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|, |
| 211 | // exclusive, set. |
David Benjamin | dca125e | 2016-06-23 17:52:47 -0400 | [diff] [blame] | 212 | static uint8_t bit_range(size_t start, size_t end) { |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 213 | return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1)); |
| 214 | } |
| 215 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 216 | // dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive, |
| 217 | // as received in |frag|. If |frag| becomes complete, it clears |
| 218 | // |frag->reassembly|. The range must be within the bounds of |frag|'s message |
| 219 | // and |frag->reassembly| must not be NULL. |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 220 | static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start, |
| 221 | size_t end) { |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 222 | size_t msg_len = frag->msg_len; |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 223 | |
| 224 | if (frag->reassembly == NULL || start > end || end > msg_len) { |
| 225 | assert(0); |
| 226 | return; |
| 227 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 228 | // A zero-length message will never have a pending reassembly. |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 229 | assert(msg_len > 0); |
| 230 | |
David Benjamin | e51fb0f | 2017-09-07 11:51:46 -0400 | [diff] [blame] | 231 | if (start == end) { |
| 232 | return; |
| 233 | } |
| 234 | |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 235 | if ((start >> 3) == (end >> 3)) { |
| 236 | frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7); |
| 237 | } else { |
| 238 | frag->reassembly[start >> 3] |= bit_range(start & 7, 8); |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 239 | for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) { |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 240 | frag->reassembly[i] = 0xff; |
| 241 | } |
| 242 | if ((end & 7) != 0) { |
| 243 | frag->reassembly[end >> 3] |= bit_range(0, end & 7); |
| 244 | } |
| 245 | } |
| 246 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 247 | // Check if the fragment is complete. |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 248 | for (size_t i = 0; i < (msg_len >> 3); i++) { |
David Benjamin | ee562b9 | 2015-03-02 20:52:52 -0500 | [diff] [blame] | 249 | if (frag->reassembly[i] != 0xff) { |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | if ((msg_len & 7) != 0 && |
| 254 | frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | OPENSSL_free(frag->reassembly); |
| 259 | frag->reassembly = NULL; |
| 260 | } |
| 261 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 262 | // dtls1_is_current_message_complete returns one if the current handshake |
| 263 | // message is complete and zero otherwise. |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 264 | static int dtls1_is_current_message_complete(const SSL *ssl) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 265 | hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq % |
| 266 | SSL_MAX_HANDSHAKE_FLIGHT]; |
| 267 | return frag != NULL && frag->reassembly == NULL; |
| 268 | } |
| 269 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 270 | // dtls1_get_incoming_message returns the incoming message corresponding to |
| 271 | // |msg_hdr|. If none exists, it creates a new one and inserts it in the |
| 272 | // queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It |
| 273 | // returns NULL on failure. The caller does not take ownership of the result. |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 274 | static hm_fragment *dtls1_get_incoming_message( |
| 275 | SSL *ssl, const struct hm_header_st *msg_hdr) { |
| 276 | if (msg_hdr->seq < ssl->d1->handshake_read_seq || |
| 277 | msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) { |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 282 | hm_fragment *frag = ssl->d1->incoming_messages[idx]; |
| 283 | if (frag != NULL) { |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 284 | assert(frag->seq == msg_hdr->seq); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 285 | // The new fragment must be compatible with the previous fragments from this |
| 286 | // message. |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 287 | if (frag->type != msg_hdr->type || |
| 288 | frag->msg_len != msg_hdr->msg_len) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 289 | OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH); |
| 290 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 291 | return NULL; |
| 292 | } |
| 293 | return frag; |
| 294 | } |
| 295 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 296 | // This is the first fragment from this message. |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 297 | frag = dtls1_hm_fragment_new(msg_hdr); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 298 | if (frag == NULL) { |
| 299 | return NULL; |
| 300 | } |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 301 | ssl->d1->incoming_messages[idx] = frag; |
| 302 | return frag; |
| 303 | } |
| 304 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 305 | int dtls1_read_message(SSL *ssl) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 306 | SSL3_RECORD *rr = &ssl->s3->rrec; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 307 | if (rr->length == 0) { |
| 308 | int ret = dtls1_get_record(ssl); |
| 309 | if (ret <= 0) { |
| 310 | return ret; |
| 311 | } |
| 312 | } |
| 313 | |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 314 | switch (rr->type) { |
| 315 | case SSL3_RT_APPLICATION_DATA: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 316 | // Unencrypted application data records are always illegal. |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 317 | if (ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 318 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 319 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 320 | return -1; |
| 321 | } |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 322 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 323 | // Out-of-order application data may be received between ChangeCipherSpec |
| 324 | // and finished. Discard it. |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 325 | rr->length = 0; |
| 326 | ssl_read_buffer_discard(ssl); |
| 327 | return 1; |
| 328 | |
| 329 | case SSL3_RT_CHANGE_CIPHER_SPEC: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 330 | // We do not support renegotiation, so encrypted ChangeCipherSpec records |
| 331 | // are illegal. |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 332 | if (!ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 333 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 334 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) { |
| 339 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 340 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 341 | return -1; |
| 342 | } |
| 343 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 344 | // Flag the ChangeCipherSpec for later. |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 345 | ssl->d1->has_change_cipher_spec = true; |
| 346 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, |
| 347 | rr->data, rr->length); |
| 348 | |
| 349 | rr->length = 0; |
| 350 | ssl_read_buffer_discard(ssl); |
| 351 | return 1; |
| 352 | |
| 353 | case SSL3_RT_HANDSHAKE: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 354 | // Break out to main processing. |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 355 | break; |
| 356 | |
| 357 | default: |
| 358 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 359 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 360 | return -1; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | CBS cbs; |
| 364 | CBS_init(&cbs, rr->data, rr->length); |
| 365 | |
| 366 | while (CBS_len(&cbs) > 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 367 | // Read a handshake fragment. |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 368 | struct hm_header_st msg_hdr; |
| 369 | CBS body; |
| 370 | if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
| 371 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
| 372 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | const size_t frag_off = msg_hdr.frag_off; |
| 377 | const size_t frag_len = msg_hdr.frag_len; |
| 378 | const size_t msg_len = msg_hdr.msg_len; |
| 379 | if (frag_off > msg_len || frag_off + frag_len < frag_off || |
| 380 | frag_off + frag_len > msg_len || |
| 381 | msg_len > ssl_max_handshake_message_len(ssl)) { |
| 382 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 383 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 384 | return -1; |
| 385 | } |
| 386 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 387 | // The encrypted epoch in DTLS has only one handshake message. |
David Benjamin | 02edcd0 | 2016-07-27 17:40:37 -0400 | [diff] [blame] | 388 | if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) { |
| 389 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 390 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 391 | return -1; |
| 392 | } |
| 393 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 394 | if (msg_hdr.seq < ssl->d1->handshake_read_seq || |
| 395 | msg_hdr.seq > |
| 396 | (unsigned)ssl->d1->handshake_read_seq + SSL_MAX_HANDSHAKE_FLIGHT) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 397 | // Ignore fragments from the past, or ones too far in the future. |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 398 | continue; |
| 399 | } |
| 400 | |
| 401 | hm_fragment *frag = dtls1_get_incoming_message(ssl, &msg_hdr); |
| 402 | if (frag == NULL) { |
| 403 | return -1; |
| 404 | } |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 405 | assert(frag->msg_len == msg_len); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 406 | |
| 407 | if (frag->reassembly == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 408 | // The message is already assembled. |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 409 | continue; |
| 410 | } |
| 411 | assert(msg_len > 0); |
| 412 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 413 | // Copy the body into the fragment. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 414 | OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off, |
| 415 | CBS_data(&body), CBS_len(&body)); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 416 | dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len); |
| 417 | } |
| 418 | |
| 419 | rr->length = 0; |
| 420 | ssl_read_buffer_discard(ssl); |
| 421 | return 1; |
| 422 | } |
| 423 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 424 | bool dtls1_get_message(SSL *ssl, SSLMessage *out) { |
| 425 | if (!dtls1_is_current_message_complete(ssl)) { |
| 426 | return false; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 427 | } |
| 428 | |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 429 | hm_fragment *frag = ssl->d1->incoming_messages[ssl->d1->handshake_read_seq % |
| 430 | SSL_MAX_HANDSHAKE_FLIGHT]; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 431 | out->type = frag->type; |
| 432 | CBS_init(&out->body, frag->data + DTLS1_HM_HEADER_LENGTH, frag->msg_len); |
| 433 | CBS_init(&out->raw, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len); |
| 434 | out->is_v2_hello = false; |
| 435 | if (!ssl->s3->has_message) { |
David Benjamin | 78b8b99 | 2017-08-01 18:38:41 -0400 | [diff] [blame] | 436 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, frag->data, |
| 437 | frag->msg_len + DTLS1_HM_HEADER_LENGTH); |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 438 | ssl->s3->has_message = true; |
David Benjamin | 78b8b99 | 2017-08-01 18:38:41 -0400 | [diff] [blame] | 439 | } |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 440 | return true; |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 441 | } |
| 442 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 443 | void dtls1_next_message(SSL *ssl) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 444 | assert(ssl->s3->has_message); |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 445 | assert(dtls1_is_current_message_complete(ssl)); |
| 446 | size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 447 | dtls1_hm_fragment_free(ssl->d1->incoming_messages[index]); |
| 448 | ssl->d1->incoming_messages[index] = NULL; |
| 449 | ssl->d1->handshake_read_seq++; |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 450 | ssl->s3->has_message = false; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 451 | // If we previously sent a flight, mark it as having a reply, so |
| 452 | // |on_handshake_complete| can manage post-handshake retransmission. |
David Benjamin | 302b818 | 2017-08-22 14:47:22 -0700 | [diff] [blame] | 453 | if (ssl->d1->outgoing_messages_complete) { |
| 454 | ssl->d1->flight_has_reply = true; |
| 455 | } |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 456 | } |
| 457 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 458 | void dtls_clear_incoming_messages(SSL *ssl) { |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 459 | for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) { |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 460 | dtls1_hm_fragment_free(ssl->d1->incoming_messages[i]); |
| 461 | ssl->d1->incoming_messages[i] = NULL; |
| 462 | } |
| 463 | } |
| 464 | |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 465 | int dtls_has_incoming_messages(const SSL *ssl) { |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 466 | size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 467 | for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 468 | // Skip the current message. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 469 | if (ssl->s3->has_message && i == current) { |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 470 | assert(dtls1_is_current_message_complete(ssl)); |
| 471 | continue; |
| 472 | } |
| 473 | if (ssl->d1->incoming_messages[i] != NULL) { |
David Benjamin | 6167281 | 2016-07-14 23:10:43 -0400 | [diff] [blame] | 474 | return 1; |
| 475 | } |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 480 | int dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
| 481 | CBS *out_body) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 482 | OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st)); |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 483 | |
| 484 | if (!CBS_get_u8(cbs, &out_hdr->type) || |
| 485 | !CBS_get_u24(cbs, &out_hdr->msg_len) || |
| 486 | !CBS_get_u16(cbs, &out_hdr->seq) || |
| 487 | !CBS_get_u24(cbs, &out_hdr->frag_off) || |
| 488 | !CBS_get_u24(cbs, &out_hdr->frag_len) || |
| 489 | !CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) { |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | return 1; |
| 494 | } |
| 495 | |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 496 | int dtls1_read_change_cipher_spec(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 497 | // Process handshake records until there is a ChangeCipherSpec. |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 498 | while (!ssl->d1->has_change_cipher_spec) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 499 | int ret = dtls1_read_message(ssl); |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 500 | if (ret <= 0) { |
| 501 | return ret; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | ssl->d1->has_change_cipher_spec = false; |
| 506 | return 1; |
| 507 | } |
| 508 | |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 509 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 510 | // Sending handshake messages. |
David Benjamin | 9d632f4 | 2016-06-23 17:57:19 -0400 | [diff] [blame] | 511 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 512 | void dtls_clear_outgoing_messages(SSL *ssl) { |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 513 | for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 514 | OPENSSL_free(ssl->d1->outgoing_messages[i].data); |
| 515 | ssl->d1->outgoing_messages[i].data = NULL; |
| 516 | } |
| 517 | ssl->d1->outgoing_messages_len = 0; |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 518 | ssl->d1->outgoing_written = 0; |
| 519 | ssl->d1->outgoing_offset = 0; |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 520 | ssl->d1->outgoing_messages_complete = false; |
David Benjamin | 302b818 | 2017-08-22 14:47:22 -0700 | [diff] [blame] | 521 | ssl->d1->flight_has_reply = false; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | int dtls1_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 525 | // Pick a modest size hint to save most of the |realloc| calls. |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 526 | if (!CBB_init(cbb, 64) || |
| 527 | !CBB_add_u8(cbb, type) || |
| 528 | !CBB_add_u24(cbb, 0 /* length (filled in later) */) || |
| 529 | !CBB_add_u16(cbb, ssl->d1->handshake_write_seq) || |
| 530 | !CBB_add_u24(cbb, 0 /* offset */) || |
| 531 | !CBB_add_u24_length_prefixed(cbb, body)) { |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | return 1; |
| 536 | } |
| 537 | |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 538 | int dtls1_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) { |
| 539 | if (!CBBFinishArray(cbb, out_msg) || |
| 540 | out_msg->size() < DTLS1_HM_HEADER_LENGTH) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 541 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 545 | // Fix up the header. Copy the fragment length into the total message |
| 546 | // length. |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 547 | OPENSSL_memcpy(out_msg->data() + 1, |
| 548 | out_msg->data() + DTLS1_HM_HEADER_LENGTH - 3, 3); |
Steven Valdez | 5eead16 | 2016-11-11 22:23:25 -0500 | [diff] [blame] | 549 | return 1; |
| 550 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 551 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 552 | // add_outgoing adds a new handshake message or ChangeCipherSpec to the current |
| 553 | // outgoing flight. It returns one on success and zero on error. In both cases, |
| 554 | // it takes ownership of |data| and releases it with |OPENSSL_free| when |
| 555 | // done. |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 556 | static int add_outgoing(SSL *ssl, int is_ccs, Array<uint8_t> data) { |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 557 | if (ssl->d1->outgoing_messages_complete) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 558 | // If we've begun writing a new flight, we received the peer flight. Discard |
| 559 | // the timer and the our flight. |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 560 | dtls1_stop_timer(ssl); |
| 561 | dtls_clear_outgoing_messages(ssl); |
| 562 | } |
| 563 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 564 | static_assert(SSL_MAX_HANDSHAKE_FLIGHT < |
| 565 | (1 << 8 * sizeof(ssl->d1->outgoing_messages_len)), |
| 566 | "outgoing_messages_len is too small"); |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 567 | if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT || |
| 568 | data.size() > 0xffffffff) { |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 569 | assert(0); |
| 570 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 571 | return 0; |
| 572 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 573 | |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 574 | if (!is_ccs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 575 | // TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript |
| 576 | // on hs. |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 577 | if (ssl->s3->hs != NULL && |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 578 | !ssl->s3->hs->transcript.Update(data.data(), data.size())) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 579 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 580 | return 0; |
| 581 | } |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 582 | ssl->d1->handshake_write_seq++; |
| 583 | } |
| 584 | |
| 585 | DTLS_OUTGOING_MESSAGE *msg = |
| 586 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len]; |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 587 | size_t len; |
| 588 | data.Release(&msg->data, &len); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 589 | msg->len = len; |
| 590 | msg->epoch = ssl->d1->w_epoch; |
| 591 | msg->is_ccs = is_ccs; |
| 592 | |
| 593 | ssl->d1->outgoing_messages_len++; |
| 594 | return 1; |
| 595 | } |
| 596 | |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 597 | int dtls1_add_message(SSL *ssl, Array<uint8_t> data) { |
| 598 | return add_outgoing(ssl, 0 /* handshake */, std::move(data)); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 599 | } |
| 600 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 601 | int dtls1_add_change_cipher_spec(SSL *ssl) { |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 602 | return add_outgoing(ssl, 1 /* ChangeCipherSpec */, Array<uint8_t>()); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 603 | } |
| 604 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 605 | int dtls1_add_alert(SSL *ssl, uint8_t level, uint8_t desc) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 606 | // The |add_alert| path is only used for warning alerts for now, which DTLS |
| 607 | // never sends. This will be implemented later once closure alerts are |
| 608 | // converted. |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 609 | assert(0); |
| 610 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 611 | return 0; |
| 612 | } |
| 613 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 614 | // dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above |
| 615 | // the minimum. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 616 | static void dtls1_update_mtu(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 617 | // TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the |
| 618 | // only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use |
| 619 | // |SSL_set_mtu|. Does this need to be so complex? |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 620 | if (ssl->d1->mtu < dtls1_min_mtu() && |
| 621 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 622 | long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
| 623 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 624 | ssl->d1->mtu = (unsigned)mtu; |
| 625 | } else { |
| 626 | ssl->d1->mtu = kDefaultMTU; |
| 627 | BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, NULL); |
| 628 | } |
| 629 | } |
| 630 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 631 | // The MTU should be above the minimum now. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 632 | assert(ssl->d1->mtu >= dtls1_min_mtu()); |
| 633 | } |
| 634 | |
| 635 | enum seal_result_t { |
| 636 | seal_error, |
| 637 | seal_no_progress, |
| 638 | seal_partial, |
| 639 | seal_success, |
| 640 | }; |
| 641 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 642 | // seal_next_message seals |msg|, which must be the next message, to |out|. If |
| 643 | // progress was made, it returns |seal_partial| or |seal_success| and sets |
| 644 | // |*out_len| to the number of bytes written. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 645 | static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out, |
| 646 | size_t *out_len, size_t max_out, |
| 647 | const DTLS_OUTGOING_MESSAGE *msg) { |
| 648 | assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len); |
| 649 | assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]); |
| 650 | |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 651 | enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 652 | if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) { |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 653 | use_epoch = dtls1_use_previous_epoch; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 654 | } else if (msg->epoch != ssl->d1->w_epoch) { |
| 655 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 656 | return seal_error; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 657 | } |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 658 | |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 659 | size_t overhead = dtls_max_seal_overhead(ssl, use_epoch); |
| 660 | size_t prefix = dtls_seal_prefix_len(ssl, use_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 661 | |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 662 | if (msg->is_ccs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 663 | // Check there is room for the ChangeCipherSpec. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 664 | static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
| 665 | if (max_out < sizeof(kChangeCipherSpec) + overhead) { |
| 666 | return seal_no_progress; |
| 667 | } |
| 668 | |
| 669 | if (!dtls_seal_record(ssl, out, out_len, max_out, |
| 670 | SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 671 | sizeof(kChangeCipherSpec), use_epoch)) { |
| 672 | return seal_error; |
| 673 | } |
| 674 | |
| 675 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC, |
| 676 | kChangeCipherSpec, sizeof(kChangeCipherSpec)); |
| 677 | return seal_success; |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 678 | } |
| 679 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 680 | // DTLS messages are serialized as a single fragment in |msg|. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 681 | CBS cbs, body; |
| 682 | struct hm_header_st hdr; |
| 683 | CBS_init(&cbs, msg->data, msg->len); |
| 684 | if (!dtls1_parse_fragment(&cbs, &hdr, &body) || |
| 685 | hdr.frag_off != 0 || |
| 686 | hdr.frag_len != CBS_len(&body) || |
| 687 | hdr.msg_len != CBS_len(&body) || |
| 688 | !CBS_skip(&body, ssl->d1->outgoing_offset) || |
| 689 | CBS_len(&cbs) != 0) { |
| 690 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 691 | return seal_error; |
| 692 | } |
| 693 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 694 | // Determine how much progress can be made. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 695 | if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) { |
| 696 | return seal_no_progress; |
| 697 | } |
| 698 | size_t todo = CBS_len(&body); |
| 699 | if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) { |
| 700 | todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead; |
| 701 | } |
| 702 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 703 | // Assemble a fragment, to be sealed in-place. |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 704 | ScopedCBB cbb; |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 705 | uint8_t *frag = out + prefix; |
| 706 | size_t max_frag = max_out - prefix, frag_len; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 707 | if (!CBB_init_fixed(cbb.get(), frag, max_frag) || |
| 708 | !CBB_add_u8(cbb.get(), hdr.type) || |
| 709 | !CBB_add_u24(cbb.get(), hdr.msg_len) || |
| 710 | !CBB_add_u16(cbb.get(), hdr.seq) || |
| 711 | !CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) || |
| 712 | !CBB_add_u24(cbb.get(), todo) || |
| 713 | !CBB_add_bytes(cbb.get(), CBS_data(&body), todo) || |
| 714 | !CBB_finish(cbb.get(), NULL, &frag_len)) { |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 715 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 716 | return seal_error; |
| 717 | } |
| 718 | |
| 719 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, frag, frag_len); |
| 720 | |
| 721 | if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE, |
| 722 | out + prefix, frag_len, use_epoch)) { |
| 723 | return seal_error; |
| 724 | } |
| 725 | |
| 726 | if (todo == CBS_len(&body)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 727 | // The next message is complete. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 728 | ssl->d1->outgoing_offset = 0; |
| 729 | return seal_success; |
| 730 | } |
| 731 | |
| 732 | ssl->d1->outgoing_offset += todo; |
| 733 | return seal_partial; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 734 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 735 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 736 | // seal_next_packet writes as much of the next flight as possible to |out| and |
| 737 | // advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as |
| 738 | // appropriate. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 739 | static int seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len, |
| 740 | size_t max_out) { |
| 741 | int made_progress = 0; |
| 742 | size_t total = 0; |
| 743 | assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len); |
| 744 | for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len; |
| 745 | ssl->d1->outgoing_written++) { |
| 746 | const DTLS_OUTGOING_MESSAGE *msg = |
| 747 | &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]; |
| 748 | size_t len; |
| 749 | enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg); |
| 750 | switch (ret) { |
| 751 | case seal_error: |
| 752 | return 0; |
| 753 | |
| 754 | case seal_no_progress: |
| 755 | goto packet_full; |
| 756 | |
| 757 | case seal_partial: |
| 758 | case seal_success: |
| 759 | out += len; |
| 760 | max_out -= len; |
| 761 | total += len; |
| 762 | made_progress = 1; |
| 763 | |
| 764 | if (ret == seal_partial) { |
| 765 | goto packet_full; |
| 766 | } |
| 767 | break; |
| 768 | } |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 769 | } |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 770 | |
| 771 | packet_full: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 772 | // The MTU was too small to make any progress. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 773 | if (!made_progress) { |
| 774 | OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL); |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | *out_len = total; |
| 779 | return 1; |
| 780 | } |
| 781 | |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 782 | static int send_flight(SSL *ssl) { |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 783 | dtls1_update_mtu(ssl); |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 784 | |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 785 | int ret = -1; |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 786 | uint8_t *packet = (uint8_t *)OPENSSL_malloc(ssl->d1->mtu); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 787 | if (packet == NULL) { |
| 788 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 789 | goto err; |
| 790 | } |
| 791 | |
| 792 | while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) { |
| 793 | uint8_t old_written = ssl->d1->outgoing_written; |
| 794 | uint32_t old_offset = ssl->d1->outgoing_offset; |
| 795 | |
| 796 | size_t packet_len; |
| 797 | if (!seal_next_packet(ssl, packet, &packet_len, ssl->d1->mtu)) { |
| 798 | goto err; |
| 799 | } |
| 800 | |
| 801 | int bio_ret = BIO_write(ssl->wbio, packet, packet_len); |
| 802 | if (bio_ret <= 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 803 | // Retry this packet the next time around. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 804 | ssl->d1->outgoing_written = old_written; |
| 805 | ssl->d1->outgoing_offset = old_offset; |
| 806 | ssl->rwstate = SSL_WRITING; |
| 807 | ret = bio_ret; |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 808 | goto err; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
David Benjamin | 42e3e19 | 2017-01-27 10:06:07 -0500 | [diff] [blame] | 812 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 2730955 | 2016-05-05 21:17:53 -0400 | [diff] [blame] | 813 | ssl->rwstate = SSL_WRITING; |
David Benjamin | 42e3e19 | 2017-01-27 10:06:07 -0500 | [diff] [blame] | 814 | goto err; |
David Benjamin | 2730955 | 2016-05-05 21:17:53 -0400 | [diff] [blame] | 815 | } |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 816 | |
David Benjamin | 42e3e19 | 2017-01-27 10:06:07 -0500 | [diff] [blame] | 817 | ret = 1; |
| 818 | |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 819 | err: |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 820 | OPENSSL_free(packet); |
David Benjamin | 30152fd | 2016-05-05 20:45:48 -0400 | [diff] [blame] | 821 | return ret; |
Adam Langley | bcc4e23 | 2015-02-19 15:51:58 -0800 | [diff] [blame] | 822 | } |
| 823 | |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 824 | int dtls1_flush_flight(SSL *ssl) { |
| 825 | ssl->d1->outgoing_messages_complete = true; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 826 | // Start the retransmission timer for the next flight (if any). |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 827 | dtls1_start_timer(ssl); |
| 828 | return send_flight(ssl); |
| 829 | } |
| 830 | |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 831 | int dtls1_retransmit_outgoing_messages(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 832 | // Rewind to the start of the flight and write it again. |
| 833 | // |
| 834 | // TODO(davidben): This does not allow retransmits to be resumed on |
| 835 | // non-blocking write. |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 836 | ssl->d1->outgoing_written = 0; |
| 837 | ssl->d1->outgoing_offset = 0; |
| 838 | |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 839 | return send_flight(ssl); |
David Benjamin | a97b737 | 2015-11-03 14:54:39 -0500 | [diff] [blame] | 840 | } |
| 841 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 842 | unsigned int dtls1_min_mtu(void) { |
David Benjamin | a18b671 | 2015-01-11 19:31:22 -0500 | [diff] [blame] | 843 | return kMinMTU; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 844 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 845 | |
| 846 | } // namespace bssl |