Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | /* ==================================================================== |
| 109 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 110 | * ECC cipher suite support in OpenSSL originally developed by |
| 111 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ |
| 112 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 113 | #include <openssl/ssl.h> |
| 114 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | #include <assert.h> |
| 116 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | #include <string.h> |
| 118 | |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 119 | #include <tuple> |
| 120 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | #include <openssl/buf.h> |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 122 | #include <openssl/bytestring.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 123 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #include <openssl/evp.h> |
| 125 | #include <openssl/mem.h> |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 126 | #include <openssl/md5.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 127 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | #include <openssl/rand.h> |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 129 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 131 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 132 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 135 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 136 | |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 137 | static bool add_record_to_flight(SSL *ssl, uint8_t type, |
| 138 | Span<const uint8_t> in) { |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 139 | // The caller should have flushed |pending_hs_data| first. |
| 140 | assert(!ssl->s3->pending_hs_data); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 141 | // We'll never add a flight while in the process of writing it out. |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 142 | assert(ssl->s3->pending_flight_offset == 0); |
| 143 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 144 | if (ssl->s3->pending_flight == nullptr) { |
| 145 | ssl->s3->pending_flight.reset(BUF_MEM_new()); |
| 146 | if (ssl->s3->pending_flight == nullptr) { |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 147 | return false; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 148 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 151 | size_t max_out = in.size() + SSL_max_seal_overhead(ssl); |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 152 | size_t new_cap = ssl->s3->pending_flight->length + max_out; |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 153 | if (max_out < in.size() || new_cap < max_out) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 154 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 155 | return false; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | size_t len; |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 159 | if (!BUF_MEM_reserve(ssl->s3->pending_flight.get(), new_cap) || |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 160 | !tls_seal_record(ssl, |
| 161 | (uint8_t *)ssl->s3->pending_flight->data + |
| 162 | ssl->s3->pending_flight->length, |
| 163 | &len, max_out, type, in.data(), in.size())) { |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 164 | return false; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | ssl->s3->pending_flight->length += len; |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 168 | return true; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 169 | } |
| 170 | |
David Benjamin | bcef514 | 2021-06-02 11:24:26 -0400 | [diff] [blame] | 171 | bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 172 | // Pick a modest size hint to save most of the |realloc| calls. |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 173 | if (!CBB_init(cbb, 64) || |
| 174 | !CBB_add_u8(cbb, type) || |
| 175 | !CBB_add_u24_length_prefixed(cbb, body)) { |
| 176 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 177 | CBB_cleanup(cbb); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 178 | return false; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 179 | } |
| 180 | |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 181 | return true; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 182 | } |
| 183 | |
David Benjamin | bcef514 | 2021-06-02 11:24:26 -0400 | [diff] [blame] | 184 | bool tls_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) { |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 185 | return CBBFinishArray(cbb, out_msg); |
Steven Valdez | 5eead16 | 2016-11-11 22:23:25 -0500 | [diff] [blame] | 186 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 187 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 188 | bool tls_add_message(SSL *ssl, Array<uint8_t> msg) { |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 189 | // Pack handshake data into the minimal number of records. This avoids |
| 190 | // unnecessary encryption overhead, notably in TLS 1.3 where we send several |
| 191 | // encrypted messages in a row. For now, we do not do this for the null |
| 192 | // cipher. The benefit is smaller and there is a risk of breaking buggy |
Steven Valdez | b84674b | 2018-08-28 10:14:07 -0400 | [diff] [blame] | 193 | // implementations. |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 194 | // |
| 195 | // TODO(davidben): See if we can do this uniformly. |
| 196 | Span<const uint8_t> rest = msg; |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 197 | if (ssl->quic_method == nullptr && |
Steven Valdez | b84674b | 2018-08-28 10:14:07 -0400 | [diff] [blame] | 198 | ssl->s3->aead_write_ctx->is_null_cipher()) { |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 199 | while (!rest.empty()) { |
| 200 | Span<const uint8_t> chunk = rest.subspan(0, ssl->max_send_fragment); |
| 201 | rest = rest.subspan(chunk.size()); |
| 202 | |
| 203 | if (!add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, chunk)) { |
| 204 | return false; |
| 205 | } |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 206 | } |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 207 | } else { |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 208 | while (!rest.empty()) { |
| 209 | // Flush if |pending_hs_data| is full. |
| 210 | if (ssl->s3->pending_hs_data && |
| 211 | ssl->s3->pending_hs_data->length >= ssl->max_send_fragment && |
| 212 | !tls_flush_pending_hs_data(ssl)) { |
| 213 | return false; |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 214 | } |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 215 | |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 216 | size_t pending_len = |
| 217 | ssl->s3->pending_hs_data ? ssl->s3->pending_hs_data->length : 0; |
| 218 | Span<const uint8_t> chunk = |
| 219 | rest.subspan(0, ssl->max_send_fragment - pending_len); |
| 220 | assert(!chunk.empty()); |
| 221 | rest = rest.subspan(chunk.size()); |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 222 | |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 223 | if (!ssl->s3->pending_hs_data) { |
| 224 | ssl->s3->pending_hs_data.reset(BUF_MEM_new()); |
| 225 | } |
| 226 | if (!ssl->s3->pending_hs_data || |
| 227 | !BUF_MEM_append(ssl->s3->pending_hs_data.get(), chunk.data(), |
| 228 | chunk.size())) { |
| 229 | return false; |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 233 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 234 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, msg); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 235 | // TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript on |
| 236 | // hs. |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 237 | if (ssl->s3->hs != NULL && |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 238 | !ssl->s3->hs->transcript.Update(msg)) { |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 239 | return false; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 240 | } |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 241 | return true; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 242 | } |
| 243 | |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 244 | bool tls_flush_pending_hs_data(SSL *ssl) { |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 245 | if (!ssl->s3->pending_hs_data || ssl->s3->pending_hs_data->length == 0) { |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | |
| 249 | UniquePtr<BUF_MEM> pending_hs_data = std::move(ssl->s3->pending_hs_data); |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 250 | auto data = |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 251 | MakeConstSpan(reinterpret_cast<const uint8_t *>(pending_hs_data->data), |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 252 | pending_hs_data->length); |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 253 | if (ssl->quic_method) { |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 254 | if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) && |
| 255 | !ssl->quic_method->add_handshake_data(ssl, ssl->s3->write_level, |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 256 | data.data(), data.size())) { |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 257 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); |
| 258 | return false; |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | return add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, data); |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 264 | } |
| 265 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 266 | bool tls_add_change_cipher_spec(SSL *ssl) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 267 | static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
| 268 | |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 269 | if (!tls_flush_pending_hs_data(ssl)) { |
| 270 | return false; |
| 271 | } |
| 272 | |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 273 | if (!ssl->quic_method && |
David Benjamin | 700631b | 2018-05-24 17:17:34 -0400 | [diff] [blame] | 274 | !add_record_to_flight(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 275 | kChangeCipherSpec)) { |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 276 | return false; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 277 | } |
| 278 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 279 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 280 | kChangeCipherSpec); |
David Benjamin | 97250f4 | 2017-10-07 04:12:35 -0400 | [diff] [blame] | 281 | return true; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 282 | } |
| 283 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 284 | int tls_flush_flight(SSL *ssl) { |
David Benjamin | cc9d935 | 2018-10-30 19:45:22 -0500 | [diff] [blame] | 285 | if (!tls_flush_pending_hs_data(ssl)) { |
| 286 | return -1; |
| 287 | } |
| 288 | |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 289 | if (ssl->quic_method) { |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 290 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 291 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 292 | return -1; |
| 293 | } |
| 294 | |
Alessandro Ghedini | 3cbb029 | 2018-12-13 13:53:57 +0000 | [diff] [blame] | 295 | if (!ssl->quic_method->flush_flight(ssl)) { |
Steven Valdez | c8e0f90 | 2018-07-14 11:23:01 -0400 | [diff] [blame] | 296 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); |
| 297 | return -1; |
| 298 | } |
| 299 | } |
| 300 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 301 | if (ssl->s3->pending_flight == nullptr) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 302 | return 1; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 303 | } |
| 304 | |
David Benjamin | e8d0746 | 2017-10-12 18:09:20 -0400 | [diff] [blame] | 305 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 306 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 307 | return -1; |
| 308 | } |
| 309 | |
Adam Langley | f64c373 | 2018-05-08 15:04:35 -0700 | [diff] [blame] | 310 | static_assert(INT_MAX <= 0xffffffff, "int is larger than 32 bits"); |
| 311 | if (ssl->s3->pending_flight->length > INT_MAX) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 312 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 313 | return -1; |
| 314 | } |
| 315 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 316 | // If there is pending data in the write buffer, it must be flushed out before |
| 317 | // any new data in pending_flight. |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 318 | if (!ssl->s3->write_buffer.empty()) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 319 | int ret = ssl_write_buffer_flush(ssl); |
| 320 | if (ret <= 0) { |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 321 | ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 322 | return ret; |
| 323 | } |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 324 | } |
| 325 | |
David Benjamin | 084064b | 2021-03-29 18:04:19 -0400 | [diff] [blame] | 326 | if (ssl->wbio == nullptr) { |
| 327 | OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 328 | return -1; |
| 329 | } |
| 330 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 331 | // Write the pending flight. |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 332 | while (ssl->s3->pending_flight_offset < ssl->s3->pending_flight->length) { |
| 333 | int ret = BIO_write( |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 334 | ssl->wbio.get(), |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 335 | ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, |
| 336 | ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset); |
| 337 | if (ret <= 0) { |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 338 | ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | ssl->s3->pending_flight_offset += ret; |
| 343 | } |
| 344 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 345 | if (BIO_flush(ssl->wbio.get()) <= 0) { |
David Benjamin | f492830 | 2019-08-21 16:04:53 -0400 | [diff] [blame] | 346 | ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
David Benjamin | 42e3e19 | 2017-01-27 10:06:07 -0500 | [diff] [blame] | 347 | return -1; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 348 | } |
| 349 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 350 | ssl->s3->pending_flight.reset(); |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 351 | ssl->s3->pending_flight_offset = 0; |
David Benjamin | 2a08c8d | 2016-06-07 15:06:39 -0400 | [diff] [blame] | 352 | return 1; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 353 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 354 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 355 | static ssl_open_record_t read_v2_client_hello(SSL *ssl, size_t *out_consumed, |
| 356 | Span<const uint8_t> in) { |
| 357 | *out_consumed = 0; |
| 358 | assert(in.size() >= SSL3_RT_HEADER_LENGTH); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 359 | // Determine the length of the V2ClientHello. |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 360 | size_t msg_length = ((in[0] & 0x7f) << 8) | in[1]; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 361 | if (msg_length > (1024 * 4)) { |
| 362 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 363 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 364 | } |
| 365 | if (msg_length < SSL3_RT_HEADER_LENGTH - 2) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 366 | // Reject lengths that are too short early. We have already read |
| 367 | // |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an |
| 368 | // (invalid) V2ClientHello which would be shorter than that. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 369 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 370 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 371 | } |
| 372 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 373 | // Ask for the remainder of the V2ClientHello. |
| 374 | if (in.size() < 2 + msg_length) { |
| 375 | *out_consumed = 2 + msg_length; |
| 376 | return ssl_open_record_partial; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 377 | } |
| 378 | |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 379 | CBS v2_client_hello = CBS(ssl->s3->read_buffer.span().subspan(2, msg_length)); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 380 | // The V2ClientHello without the length is incorporated into the handshake |
| 381 | // hash. This is only ever called at the start of the handshake, so hs is |
| 382 | // guaranteed to be non-NULL. |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 383 | if (!ssl->s3->hs->transcript.Update(v2_client_hello)) { |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 384 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 385 | } |
| 386 | |
David Benjamin | c027999 | 2016-09-19 20:15:07 -0400 | [diff] [blame] | 387 | ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 388 | v2_client_hello); |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 389 | |
| 390 | uint8_t msg_type; |
| 391 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 392 | CBS cipher_specs, session_id, challenge; |
| 393 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 394 | !CBS_get_u16(&v2_client_hello, &version) || |
| 395 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 396 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 397 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 398 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 399 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 400 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 401 | CBS_len(&v2_client_hello) != 0) { |
| 402 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 403 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 404 | } |
| 405 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 406 | // msg_type has already been checked. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 407 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
| 408 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 409 | // The client_random is the V2ClientHello challenge. Truncate or left-pad with |
| 410 | // zeros as needed. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 411 | size_t rand_len = CBS_len(&challenge); |
| 412 | if (rand_len > SSL3_RANDOM_SIZE) { |
| 413 | rand_len = SSL3_RANDOM_SIZE; |
| 414 | } |
| 415 | uint8_t random[SSL3_RANDOM_SIZE]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 416 | OPENSSL_memset(random, 0, SSL3_RANDOM_SIZE); |
| 417 | OPENSSL_memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 418 | rand_len); |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 419 | |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 420 | // Write out an equivalent TLS ClientHello directly to the handshake buffer. |
David Benjamin | 481b9d2 | 2016-07-27 14:01:59 -0400 | [diff] [blame] | 421 | size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ + |
| 422 | SSL3_RANDOM_SIZE + 1 /* session ID length */ + |
| 423 | 2 /* cipher list length */ + |
| 424 | CBS_len(&cipher_specs) / 3 * 2 + |
| 425 | 1 /* compression length */ + 1 /* compression */; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 426 | ScopedCBB client_hello; |
| 427 | CBB hello_body, cipher_suites; |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 428 | if (!ssl->s3->hs_buf) { |
| 429 | ssl->s3->hs_buf.reset(BUF_MEM_new()); |
| 430 | } |
| 431 | if (!ssl->s3->hs_buf || |
| 432 | !BUF_MEM_reserve(ssl->s3->hs_buf.get(), max_v3_client_hello) || |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 433 | !CBB_init_fixed(client_hello.get(), (uint8_t *)ssl->s3->hs_buf->data, |
| 434 | ssl->s3->hs_buf->max) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 435 | !CBB_add_u8(client_hello.get(), SSL3_MT_CLIENT_HELLO) || |
| 436 | !CBB_add_u24_length_prefixed(client_hello.get(), &hello_body) || |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 437 | !CBB_add_u16(&hello_body, version) || |
| 438 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 439 | // No session id. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 440 | !CBB_add_u8(&hello_body, 0) || |
| 441 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 442 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 443 | } |
| 444 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 445 | // Copy the cipher suites. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 446 | while (CBS_len(&cipher_specs) > 0) { |
| 447 | uint32_t cipher_spec; |
| 448 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 449 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 450 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 451 | } |
| 452 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 453 | // Skip SSLv2 ciphers. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 454 | if ((cipher_spec & 0xff0000) != 0) { |
| 455 | continue; |
| 456 | } |
| 457 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 458 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 459 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 463 | // Add the null compression scheme and finish. |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 464 | if (!CBB_add_u8(&hello_body, 1) || |
| 465 | !CBB_add_u8(&hello_body, 0) || |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 466 | !CBB_finish(client_hello.get(), NULL, &ssl->s3->hs_buf->length)) { |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 467 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 468 | return ssl_open_record_error; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 469 | } |
| 470 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 471 | *out_consumed = 2 + msg_length; |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 472 | ssl->s3->is_v2_hello = true; |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 473 | return ssl_open_record_success; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 474 | } |
| 475 | |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 476 | static bool parse_message(const SSL *ssl, SSLMessage *out, |
| 477 | size_t *out_bytes_needed) { |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 478 | if (!ssl->s3->hs_buf) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 479 | *out_bytes_needed = 4; |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | CBS cbs; |
| 484 | uint32_t len; |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 485 | CBS_init(&cbs, reinterpret_cast<const uint8_t *>(ssl->s3->hs_buf->data), |
| 486 | ssl->s3->hs_buf->length); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 487 | if (!CBS_get_u8(&cbs, &out->type) || |
| 488 | !CBS_get_u24(&cbs, &len)) { |
| 489 | *out_bytes_needed = 4; |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | if (!CBS_get_bytes(&cbs, &out->body, len)) { |
| 494 | *out_bytes_needed = 4 + len; |
| 495 | return false; |
| 496 | } |
| 497 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 498 | CBS_init(&out->raw, reinterpret_cast<const uint8_t *>(ssl->s3->hs_buf->data), |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 499 | 4 + len); |
| 500 | out->is_v2_hello = ssl->s3->is_v2_hello; |
David Benjamin | 03a4b96 | 2017-10-06 17:54:10 -0400 | [diff] [blame] | 501 | return true; |
| 502 | } |
| 503 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 504 | bool tls_get_message(const SSL *ssl, SSLMessage *out) { |
David Benjamin | 03a4b96 | 2017-10-06 17:54:10 -0400 | [diff] [blame] | 505 | size_t unused; |
| 506 | if (!parse_message(ssl, out, &unused)) { |
| 507 | return false; |
| 508 | } |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 509 | if (!ssl->s3->has_message) { |
| 510 | if (!out->is_v2_hello) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 511 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 512 | } |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 513 | ssl->s3->has_message = true; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 514 | } |
| 515 | return true; |
| 516 | } |
| 517 | |
David Benjamin | 33febf6 | 2017-10-07 16:52:50 -0400 | [diff] [blame] | 518 | bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert) { |
| 519 | // If there is a complete message, the caller must have consumed it first. |
| 520 | SSLMessage msg; |
| 521 | size_t bytes_needed; |
| 522 | if (parse_message(ssl, &msg, &bytes_needed)) { |
| 523 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 524 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | // Enforce the limit so the peer cannot force us to buffer 16MB. |
| 529 | if (bytes_needed > 4 + ssl_max_handshake_message_len(ssl)) { |
| 530 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 531 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | return true; |
| 536 | } |
| 537 | |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 538 | bool tls_has_unprocessed_handshake_data(const SSL *ssl) { |
| 539 | size_t msg_len = 0; |
| 540 | if (ssl->s3->has_message) { |
| 541 | SSLMessage msg; |
| 542 | size_t unused; |
| 543 | if (parse_message(ssl, &msg, &unused)) { |
| 544 | msg_len = CBS_len(&msg.raw); |
| 545 | } |
| 546 | } |
| 547 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 548 | return ssl->s3->hs_buf && ssl->s3->hs_buf->length > msg_len; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 549 | } |
| 550 | |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 551 | bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 552 | // Re-create the handshake buffer if needed. |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 553 | if (!ssl->s3->hs_buf) { |
| 554 | ssl->s3->hs_buf.reset(BUF_MEM_new()); |
David Benjamin | 481b9d2 | 2016-07-27 14:01:59 -0400 | [diff] [blame] | 555 | } |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 556 | return ssl->s3->hs_buf && |
| 557 | BUF_MEM_append(ssl->s3->hs_buf.get(), data.data(), data.size()); |
| 558 | } |
David Benjamin | 481b9d2 | 2016-07-27 14:01:59 -0400 | [diff] [blame] | 559 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 560 | ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed, |
| 561 | uint8_t *out_alert, Span<uint8_t> in) { |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 562 | *out_consumed = 0; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 563 | // Bypass the record layer for the first message to handle V2ClientHello. |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 564 | if (ssl->server && !ssl->s3->v2_hello_done) { |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 565 | // Ask for the first 5 bytes, the size of the TLS record header. This is |
| 566 | // sufficient to detect a V2ClientHello and ensures that we never read |
| 567 | // beyond the first record. |
| 568 | if (in.size() < SSL3_RT_HEADER_LENGTH) { |
| 569 | *out_consumed = SSL3_RT_HEADER_LENGTH; |
| 570 | return ssl_open_record_partial; |
David Benjamin | 4dbdf94 | 2016-07-06 21:55:10 -0700 | [diff] [blame] | 571 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 572 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 573 | // Some dedicated error codes for protocol mixups should the application |
| 574 | // wish to interpret them differently. (These do not overlap with |
| 575 | // ClientHello or V2ClientHello.) |
| 576 | const char *str = reinterpret_cast<const char*>(in.data()); |
| 577 | if (strncmp("GET ", str, 4) == 0 || |
| 578 | strncmp("POST ", str, 5) == 0 || |
| 579 | strncmp("HEAD ", str, 5) == 0 || |
| 580 | strncmp("PUT ", str, 4) == 0) { |
| 581 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST); |
| 582 | *out_alert = 0; |
| 583 | return ssl_open_record_error; |
| 584 | } |
| 585 | if (strncmp("CONNE", str, 5) == 0) { |
| 586 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST); |
| 587 | *out_alert = 0; |
| 588 | return ssl_open_record_error; |
| 589 | } |
| 590 | |
| 591 | // Check for a V2ClientHello. |
| 592 | if ((in[0] & 0x80) != 0 && in[2] == SSL2_MT_CLIENT_HELLO && |
| 593 | in[3] == SSL3_VERSION_MAJOR) { |
| 594 | auto ret = read_v2_client_hello(ssl, out_consumed, in); |
| 595 | if (ret == ssl_open_record_error) { |
| 596 | *out_alert = 0; |
| 597 | } else if (ret == ssl_open_record_success) { |
| 598 | ssl->s3->v2_hello_done = true; |
| 599 | } |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 600 | return ret; |
| 601 | } |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 602 | |
| 603 | ssl->s3->v2_hello_done = true; |
| 604 | } |
| 605 | |
| 606 | uint8_t type; |
| 607 | Span<uint8_t> body; |
| 608 | auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in); |
| 609 | if (ret != ssl_open_record_success) { |
| 610 | return ret; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // WatchGuard's TLS 1.3 interference bug is very distinctive: they drop the |
| 614 | // ServerHello and send the remaining encrypted application data records |
| 615 | // as-is. This manifests as an application data record when we expect |
| 616 | // handshake. Report a dedicated error code for this case. |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 617 | if (!ssl->server && type == SSL3_RT_APPLICATION_DATA && |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 618 | ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 619 | OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_INSTEAD_OF_HANDSHAKE); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 620 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 621 | return ssl_open_record_error; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 622 | } |
| 623 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 624 | if (type != SSL3_RT_HANDSHAKE) { |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 625 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 626 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 627 | return ssl_open_record_error; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Append the entire handshake record to the buffer. |
David Benjamin | 7d10ab5 | 2018-11-02 18:31:17 -0500 | [diff] [blame] | 631 | if (!tls_append_handshake_data(ssl, body)) { |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 632 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 633 | return ssl_open_record_error; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 634 | } |
| 635 | |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 636 | return ssl_open_record_success; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 637 | } |
| 638 | |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 639 | void tls_next_message(SSL *ssl) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 640 | SSLMessage msg; |
David Benjamin | 82a4b22 | 2020-02-09 17:51:45 -0500 | [diff] [blame] | 641 | if (!tls_get_message(ssl, &msg) || |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 642 | !ssl->s3->hs_buf || |
| 643 | ssl->s3->hs_buf->length < CBS_len(&msg.raw)) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 644 | assert(0); |
| 645 | return; |
| 646 | } |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 647 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 648 | OPENSSL_memmove(ssl->s3->hs_buf->data, |
| 649 | ssl->s3->hs_buf->data + CBS_len(&msg.raw), |
| 650 | ssl->s3->hs_buf->length - CBS_len(&msg.raw)); |
| 651 | ssl->s3->hs_buf->length -= CBS_len(&msg.raw); |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 652 | ssl->s3->is_v2_hello = false; |
| 653 | ssl->s3->has_message = false; |
David Benjamin | e3dee27 | 2017-08-02 20:06:53 -0400 | [diff] [blame] | 654 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 655 | // Post-handshake messages are rare, so release the buffer after every |
| 656 | // message. During the handshake, |on_handshake_complete| will release it. |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 657 | if (!SSL_in_init(ssl) && ssl->s3->hs_buf->length == 0) { |
| 658 | ssl->s3->hs_buf.reset(); |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 662 | // CipherScorer produces a "score" for each possible cipher suite offered by |
| 663 | // the client. |
| 664 | class CipherScorer { |
| 665 | public: |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 666 | CipherScorer(bool has_aes_hw) : aes_is_fine_(has_aes_hw) {} |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 667 | |
Adam Langley | ec6425ca | 2023-01-08 16:41:34 -0800 | [diff] [blame] | 668 | typedef std::tuple<bool, bool> Score; |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 669 | |
| 670 | // MinScore returns a |Score| that will compare less than the score of all |
| 671 | // cipher suites. |
| 672 | Score MinScore() const { |
Adam Langley | ec6425ca | 2023-01-08 16:41:34 -0800 | [diff] [blame] | 673 | return Score(false, false); |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | Score Evaluate(const SSL_CIPHER *a) const { |
| 677 | return Score( |
| 678 | // Something is always preferable to nothing. |
| 679 | true, |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 680 | // Either AES is fine, or else ChaCha20 is preferred. |
| 681 | aes_is_fine_ || a->algorithm_enc == SSL_CHACHA20POLY1305); |
| 682 | } |
| 683 | |
| 684 | private: |
| 685 | const bool aes_is_fine_; |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 686 | }; |
| 687 | |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 688 | bool ssl_tls13_cipher_meets_policy(uint16_t cipher_id, |
| 689 | enum ssl_compliance_policy_t policy) { |
| 690 | switch (policy) { |
| 691 | case ssl_compliance_policy_none: |
| 692 | return true; |
| 693 | |
| 694 | case ssl_compliance_policy_fips_202205: |
| 695 | switch (cipher_id) { |
| 696 | case TLS1_3_CK_AES_128_GCM_SHA256 & 0xffff: |
| 697 | case TLS1_3_CK_AES_256_GCM_SHA384 & 0xffff: |
| 698 | return true; |
| 699 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff: |
| 700 | return false; |
| 701 | default: |
| 702 | assert(false); |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | case ssl_compliance_policy_wpa3_192_202304: |
| 707 | switch (cipher_id) { |
| 708 | case TLS1_3_CK_AES_256_GCM_SHA384 & 0xffff: |
| 709 | return true; |
| 710 | case TLS1_3_CK_AES_128_GCM_SHA256 & 0xffff: |
| 711 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff: |
| 712 | return false; |
| 713 | default: |
| 714 | assert(false); |
| 715 | return false; |
| 716 | } |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 719 | assert(false); |
| 720 | return false; |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 723 | const SSL_CIPHER *ssl_choose_tls13_cipher(CBS cipher_suites, bool has_aes_hw, |
| 724 | uint16_t version, uint16_t group_id, |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 725 | enum ssl_compliance_policy_t policy) { |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 726 | if (CBS_len(&cipher_suites) % 2 != 0) { |
| 727 | return nullptr; |
| 728 | } |
| 729 | |
| 730 | const SSL_CIPHER *best = nullptr; |
Bob Beck | 8cacbd9 | 2023-04-05 14:38:38 -0600 | [diff] [blame] | 731 | CipherScorer scorer(has_aes_hw); |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 732 | CipherScorer::Score best_score = scorer.MinScore(); |
| 733 | |
| 734 | while (CBS_len(&cipher_suites) > 0) { |
| 735 | uint16_t cipher_suite; |
| 736 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 737 | return nullptr; |
| 738 | } |
| 739 | |
| 740 | // Limit to TLS 1.3 ciphers we know about. |
| 741 | const SSL_CIPHER *candidate = SSL_get_cipher_by_value(cipher_suite); |
| 742 | if (candidate == nullptr || |
| 743 | SSL_CIPHER_get_min_version(candidate) > version || |
| 744 | SSL_CIPHER_get_max_version(candidate) < version) { |
| 745 | continue; |
| 746 | } |
| 747 | |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 748 | if (!ssl_tls13_cipher_meets_policy(SSL_CIPHER_get_protocol_id(candidate), |
Adam Langley | 2f6409e | 2023-04-10 21:09:11 +0000 | [diff] [blame] | 749 | policy)) { |
Adam Langley | 451ea3c | 2022-05-18 16:15:49 -0700 | [diff] [blame] | 750 | continue; |
| 751 | } |
| 752 | |
Steven Valdez | d6f9c35 | 2019-06-25 10:13:18 -0400 | [diff] [blame] | 753 | const CipherScorer::Score candidate_score = scorer.Evaluate(candidate); |
| 754 | // |candidate_score| must be larger to displace the current choice. That way |
| 755 | // the client's order controls between ciphers with an equal score. |
| 756 | if (candidate_score > best_score) { |
| 757 | best = candidate; |
| 758 | best_score = candidate_score; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | return best; |
| 763 | } |
| 764 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 765 | BSSL_NAMESPACE_END |