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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 7 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 14 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 21 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 :-). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 39 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 51 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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-2007 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 |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 65 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 109 | #include <openssl/ssl.h> |
| 110 | |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 111 | #include <assert.h> |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 112 | #include <limits.h> |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 113 | #include <stdlib.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 114 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 116 | #include <algorithm> |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 117 | #include <utility> |
| 118 | |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 119 | #include <openssl/aead.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 120 | #include <openssl/bytestring.h> |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 121 | #include <openssl/chacha.h> |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 122 | #include <openssl/curve25519.h> |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 123 | #include <openssl/digest.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 124 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | #include <openssl/evp.h> |
| 126 | #include <openssl/hmac.h> |
David Benjamin | 070a6c3 | 2021-05-05 15:39:27 -0400 | [diff] [blame] | 127 | #include <openssl/hpke.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 129 | #include <openssl/nid.h> |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 130 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 132 | #include "../crypto/internal.h" |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 133 | #include "internal.h" |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 134 | |
| 135 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 136 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 137 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 138 | static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs); |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 139 | static bool ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 141 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 142 | uint16_t u1 = *((const uint16_t *)p1); |
| 143 | uint16_t u2 = *((const uint16_t *)p2); |
| 144 | if (u1 < u2) { |
| 145 | return -1; |
| 146 | } else if (u1 > u2) { |
| 147 | return 1; |
| 148 | } else { |
| 149 | return 0; |
| 150 | } |
| 151 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 152 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 153 | // Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 154 | // more than one extension of the same type in a ClientHello or ServerHello. |
| 155 | // This function does an initial scan over the extensions block to filter those |
| 156 | // out. |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 157 | static bool tls1_check_duplicate_extensions(const CBS *cbs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 158 | // First pass: count the extensions. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 159 | size_t num_extensions = 0; |
| 160 | CBS extensions = *cbs; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 161 | while (CBS_len(&extensions) > 0) { |
| 162 | uint16_t type; |
| 163 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | if (!CBS_get_u16(&extensions, &type) || |
| 166 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 167 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 168 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 169 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 170 | num_extensions++; |
| 171 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 172 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 173 | if (num_extensions == 0) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 174 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 175 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 176 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 177 | Array<uint16_t> extension_types; |
| 178 | if (!extension_types.Init(num_extensions)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 179 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 180 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 181 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 182 | // Second pass: gather the extension types. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 183 | extensions = *cbs; |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 184 | for (size_t i = 0; i < extension_types.size(); i++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 185 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 186 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 187 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 188 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 189 | // This should not happen. |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 190 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 194 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 195 | // Sort the extensions and make sure there are no duplicates. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 196 | qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t), |
| 197 | compare_uint16_t); |
| 198 | for (size_t i = 1; i < num_extensions; i++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | if (extension_types[i - 1] == extension_types[i]) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 200 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 203 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 204 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 205 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 206 | |
Kris Kwiatkowski | 78c88c9 | 2019-03-11 14:40:59 +0000 | [diff] [blame] | 207 | static bool is_post_quantum_group(uint16_t id) { |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 208 | switch (id) { |
David Benjamin | 335523a | 2023-05-26 21:55:56 -0400 | [diff] [blame] | 209 | case SSL_GROUP_X25519_KYBER768_DRAFT00: |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 210 | return true; |
| 211 | default: |
| 212 | return false; |
| 213 | } |
Kris Kwiatkowski | 78c88c9 | 2019-03-11 14:40:59 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 216 | bool ssl_client_hello_init(const SSL *ssl, SSL_CLIENT_HELLO *out, |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 217 | Span<const uint8_t> body) { |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 218 | CBS cbs = body; |
| 219 | if (!ssl_parse_client_hello_with_trailing_data(ssl, &cbs, out) || |
| 220 | CBS_len(&cbs) != 0) { |
| 221 | return false; |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | bool ssl_parse_client_hello_with_trailing_data(const SSL *ssl, CBS *cbs, |
| 227 | SSL_CLIENT_HELLO *out) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 228 | OPENSSL_memset(out, 0, sizeof(*out)); |
Adam Langley | c9827e0 | 2019-04-12 14:46:50 -0700 | [diff] [blame] | 229 | out->ssl = const_cast<SSL *>(ssl); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 230 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 231 | CBS copy = *cbs; |
| 232 | CBS random, session_id; |
| 233 | if (!CBS_get_u16(cbs, &out->version) || |
| 234 | !CBS_get_bytes(cbs, &random, SSL3_RANDOM_SIZE) || |
| 235 | !CBS_get_u8_length_prefixed(cbs, &session_id) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 236 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 237 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 238 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 240 | out->random = CBS_data(&random); |
| 241 | out->random_len = CBS_len(&random); |
| 242 | out->session_id = CBS_data(&session_id); |
| 243 | out->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 245 | // Skip past DTLS cookie |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 246 | if (SSL_is_dtls(out->ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 247 | CBS cookie; |
David Benjamin | 361e3e0 | 2022-09-11 13:58:38 -0400 | [diff] [blame] | 248 | if (!CBS_get_u8_length_prefixed(cbs, &cookie)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 249 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 253 | CBS cipher_suites, compression_methods; |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 254 | if (!CBS_get_u16_length_prefixed(cbs, &cipher_suites) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 255 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 || |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 256 | !CBS_get_u8_length_prefixed(cbs, &compression_methods) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | CBS_len(&compression_methods) < 1) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 258 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 259 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 260 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 261 | out->cipher_suites = CBS_data(&cipher_suites); |
| 262 | out->cipher_suites_len = CBS_len(&cipher_suites); |
| 263 | out->compression_methods = CBS_data(&compression_methods); |
| 264 | out->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 266 | // If the ClientHello ends here then it's valid, but doesn't have any |
David Benjamin | 9bb15f5 | 2018-06-26 00:07:40 -0400 | [diff] [blame] | 267 | // extensions. |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 268 | if (CBS_len(cbs) == 0) { |
| 269 | out->extensions = nullptr; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 270 | out->extensions_len = 0; |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 271 | } else { |
| 272 | // Extract extensions and check it is valid. |
| 273 | CBS extensions; |
| 274 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 275 | !tls1_check_duplicate_extensions(&extensions)) { |
| 276 | return false; |
| 277 | } |
| 278 | out->extensions = CBS_data(&extensions); |
| 279 | out->extensions_len = CBS_len(&extensions); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 280 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 282 | out->client_hello = CBS_data(©); |
| 283 | out->client_hello_len = CBS_len(©) - CBS_len(cbs); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 284 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 285 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 287 | bool ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
| 288 | CBS *out, uint16_t extension_type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 289 | CBS extensions; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 290 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 291 | while (CBS_len(&extensions) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 292 | // Decode the next extension. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 293 | uint16_t type; |
| 294 | CBS extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 295 | if (!CBS_get_u16(&extensions, &type) || |
| 296 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 297 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 298 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 300 | if (type == extension_type) { |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 301 | *out = extension; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 302 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 306 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 309 | static const uint16_t kDefaultGroups[] = { |
David Benjamin | 335523a | 2023-05-26 21:55:56 -0400 | [diff] [blame] | 310 | SSL_GROUP_X25519, |
| 311 | SSL_GROUP_SECP256R1, |
| 312 | SSL_GROUP_SECP384R1, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 313 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 315 | Span<const uint16_t> tls1_get_grouplist(const SSL_HANDSHAKE *hs) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 316 | if (!hs->config->supported_group_list.empty()) { |
| 317 | return hs->config->supported_group_list; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 318 | } |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 319 | return Span<const uint16_t>(kDefaultGroups); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 321 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 322 | bool tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 323 | SSL *const ssl = hs->ssl; |
David Benjamin | f04976b | 2016-10-07 00:37:55 -0400 | [diff] [blame] | 324 | assert(ssl->server); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 325 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 326 | // Clients are not required to send a supported_groups extension. In this |
| 327 | // case, the server is free to pick any group it likes. See RFC 4492, |
| 328 | // section 4, paragraph 3. |
| 329 | // |
| 330 | // However, in the interests of compatibility, we will skip ECDH if the |
| 331 | // client didn't send an extension because we can't be sure that they'll |
| 332 | // support our favoured group. Thus we do not special-case an emtpy |
| 333 | // |peer_supported_group_list|. |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 334 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 335 | Span<const uint16_t> groups = tls1_get_grouplist(hs); |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 336 | Span<const uint16_t> pref, supp; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 337 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 338 | pref = groups; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 339 | supp = hs->peer_supported_group_list; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 340 | } else { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 341 | pref = hs->peer_supported_group_list; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 342 | supp = groups; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 343 | } |
| 344 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 345 | for (uint16_t pref_group : pref) { |
| 346 | for (uint16_t supp_group : supp) { |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 347 | if (pref_group == supp_group && |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 348 | // Post-quantum key agreements don't fit in the u8-length-prefixed |
| 349 | // ECPoint field in TLS 1.2 and below. |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 350 | (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
Kris Kwiatkowski | 78c88c9 | 2019-03-11 14:40:59 +0000 | [diff] [blame] | 351 | !is_post_quantum_group(pref_group))) { |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 352 | *out_group_id = pref_group; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 353 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 358 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 359 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 360 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 361 | bool tls1_check_group_id(const SSL_HANDSHAKE *hs, uint16_t group_id) { |
Kris Kwiatkowski | 78c88c9 | 2019-03-11 14:40:59 +0000 | [diff] [blame] | 362 | if (is_post_quantum_group(group_id) && |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 363 | ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
Adam Langley | 4ae4fb7 | 2023-04-11 18:25:37 +0000 | [diff] [blame] | 364 | // Post-quantum "groups" require TLS 1.3. |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 368 | // We internally assume zero is never allocated as a group ID. |
| 369 | if (group_id == 0) { |
| 370 | return false; |
| 371 | } |
| 372 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 373 | for (uint16_t supported : tls1_get_grouplist(hs)) { |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 374 | if (supported == group_id) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 375 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 376 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 377 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 378 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 379 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 381 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 382 | // kVerifySignatureAlgorithms is the default list of accepted signature |
| 383 | // algorithms for verifying. |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 384 | static const uint16_t kVerifySignatureAlgorithms[] = { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 385 | // List our preferred algorithms first. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 386 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame] | 387 | SSL_SIGN_RSA_PSS_RSAE_SHA256, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 388 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 389 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 390 | // Larger hashes are acceptable. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 391 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame] | 392 | SSL_SIGN_RSA_PSS_RSAE_SHA384, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 393 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 394 | |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame] | 395 | SSL_SIGN_RSA_PSS_RSAE_SHA512, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 396 | SSL_SIGN_RSA_PKCS1_SHA512, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 397 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 398 | // For now, SHA-1 is still accepted but least preferable. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 399 | SSL_SIGN_RSA_PKCS1_SHA1, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 400 | }; |
| 401 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 402 | // kSignSignatureAlgorithms is the default list of supported signature |
| 403 | // algorithms for signing. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 404 | static const uint16_t kSignSignatureAlgorithms[] = { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 405 | // List our preferred algorithms first. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 406 | SSL_SIGN_ED25519, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 407 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame] | 408 | SSL_SIGN_RSA_PSS_RSAE_SHA256, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 409 | SSL_SIGN_RSA_PKCS1_SHA256, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 410 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 411 | // If needed, sign larger hashes. |
| 412 | // |
| 413 | // TODO(davidben): Determine which of these may be pruned. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 414 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame] | 415 | SSL_SIGN_RSA_PSS_RSAE_SHA384, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 416 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 417 | |
| 418 | SSL_SIGN_ECDSA_SECP521R1_SHA512, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame] | 419 | SSL_SIGN_RSA_PSS_RSAE_SHA512, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 420 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 421 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 422 | // If the peer supports nothing else, sign with SHA-1. |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 423 | SSL_SIGN_ECDSA_SHA1, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 424 | SSL_SIGN_RSA_PKCS1_SHA1, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 425 | }; |
| 426 | |
David Benjamin | 1766935 | 2020-02-05 17:33:36 -0500 | [diff] [blame] | 427 | static Span<const uint16_t> tls12_get_verify_sigalgs(const SSL_HANDSHAKE *hs) { |
| 428 | if (hs->config->verify_sigalgs.empty()) { |
| 429 | return Span<const uint16_t>(kVerifySignatureAlgorithms); |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 430 | } |
David Benjamin | 1766935 | 2020-02-05 17:33:36 -0500 | [diff] [blame] | 431 | return hs->config->verify_sigalgs; |
David Benjamin | e28552d | 2018-04-08 13:59:25 -0400 | [diff] [blame] | 432 | } |
| 433 | |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 434 | bool tls12_add_verify_sigalgs(const SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 1766935 | 2020-02-05 17:33:36 -0500 | [diff] [blame] | 435 | for (uint16_t sigalg : tls12_get_verify_sigalgs(hs)) { |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 436 | if (!CBB_add_u16(out, sigalg)) { |
| 437 | return false; |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 438 | } |
| 439 | } |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 440 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 441 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 443 | bool tls12_check_peer_sigalg(const SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 444 | uint16_t sigalg) { |
David Benjamin | 1766935 | 2020-02-05 17:33:36 -0500 | [diff] [blame] | 445 | for (uint16_t verify_sigalg : tls12_get_verify_sigalgs(hs)) { |
David Benjamin | e28552d | 2018-04-08 13:59:25 -0400 | [diff] [blame] | 446 | if (verify_sigalg == sigalg) { |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 447 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 451 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 452 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 453 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | } |
| 455 | |
David Benjamin | 52b3638 | 2021-05-19 13:26:19 -0400 | [diff] [blame] | 456 | // tls_extension represents a TLS extension that is handled internally. |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 457 | // |
| 458 | // The parse callbacks receive a |CBS| that contains the contents of the |
| 459 | // extension (i.e. not including the type and length bytes). If an extension is |
| 460 | // not received then the parse callbacks will be called with a NULL CBS so that |
| 461 | // they can do any processing needed to handle the absence of an extension. |
| 462 | // |
| 463 | // The add callbacks receive a |CBB| to which the extension can be appended but |
| 464 | // the function is responsible for appending the type and length bytes too. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 465 | // |
| 466 | // |add_clienthello| may be called multiple times and must not mutate |hs|. It |
| 467 | // is additionally passed two output |CBB|s. If the extension is the same |
| 468 | // independent of the value of |type|, the callback may write to |
| 469 | // |out_compressible| instead of |out|. When serializing the ClientHelloInner, |
| 470 | // all compressible extensions will be made continguous and replaced with |
| 471 | // ech_outer_extensions when encrypted. When serializing the ClientHelloOuter |
| 472 | // or not offering ECH, |out| will be equal to |out_compressible|, so writing to |
| 473 | // |out_compressible| still works. |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 474 | // |
David Benjamin | 9052286 | 2021-05-27 14:50:31 -0400 | [diff] [blame] | 475 | // Note the |parse_serverhello| and |add_serverhello| callbacks refer to the |
| 476 | // TLS 1.2 ServerHello. In TLS 1.3, these callbacks act on EncryptedExtensions, |
| 477 | // with ServerHello extensions handled elsewhere in the handshake. |
| 478 | // |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 479 | // All callbacks return true for success and false for error. If a parse |
| 480 | // function returns zero then a fatal alert with value |*out_alert| will be |
| 481 | // sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 482 | struct tls_extension { |
| 483 | uint16_t value; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 484 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 485 | bool (*add_clienthello)(const SSL_HANDSHAKE *hs, CBB *out, |
| 486 | CBB *out_compressible, ssl_client_hello_type_t type); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 487 | bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 488 | CBS *contents); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 489 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 490 | bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 491 | CBS *contents); |
| 492 | bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 495 | static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 496 | CBS *contents) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 497 | if (contents != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 498 | // Servers MUST NOT send this extension. |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 499 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 500 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 501 | return false; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 502 | } |
| 503 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 504 | return true; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 505 | } |
| 506 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 507 | static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 508 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 509 | // This extension from the client is handled elsewhere. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 510 | return true; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 511 | } |
| 512 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 513 | static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 514 | return true; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 515 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 516 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 517 | // Server name indication (SNI). |
| 518 | // |
| 519 | // https://tools.ietf.org/html/rfc6066#section-3. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 520 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 521 | static bool ext_sni_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 522 | CBB *out_compressible, |
| 523 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 524 | const SSL *const ssl = hs->ssl; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 525 | // If offering ECH, send the public name instead of the configured name. |
| 526 | Span<const uint8_t> hostname; |
| 527 | if (type == ssl_client_hello_outer) { |
| 528 | hostname = hs->selected_ech_config->public_name; |
| 529 | } else { |
| 530 | if (ssl->hostname == nullptr) { |
| 531 | return true; |
| 532 | } |
| 533 | hostname = |
| 534 | MakeConstSpan(reinterpret_cast<const uint8_t *>(ssl->hostname.get()), |
| 535 | strlen(ssl->hostname.get())); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | CBB contents, server_name_list, name; |
| 539 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 540 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 541 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 542 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 543 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 544 | !CBB_add_bytes(&name, hostname.data(), hostname.size()) || |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 545 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 546 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 547 | } |
| 548 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 549 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 550 | } |
| 551 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 552 | static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 553 | CBS *contents) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 554 | // The server may acknowledge SNI with an empty extension. We check the syntax |
| 555 | // but otherwise ignore this signal. |
| 556 | return contents == NULL || CBS_len(contents) == 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 557 | } |
| 558 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 559 | static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 560 | CBS *contents) { |
David Benjamin | ef0183c | 2019-07-20 09:11:05 -0400 | [diff] [blame] | 561 | // SNI has already been parsed earlier in the handshake. See |extract_sni|. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 562 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 563 | } |
| 564 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 565 | static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 566 | if (hs->ssl->s3->session_reused || |
| 567 | !hs->should_ack_sni) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 568 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 572 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 573 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 574 | } |
| 575 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 576 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 580 | // Encrypted ClientHello (ECH) |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 581 | // |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 582 | // https://tools.ietf.org/html/draft-ietf-tls-esni-13 |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 583 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 584 | static bool ext_ech_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 585 | CBB *out_compressible, |
| 586 | ssl_client_hello_type_t type) { |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 587 | if (type == ssl_client_hello_inner) { |
| 588 | if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) || |
| 589 | !CBB_add_u16(out, /* length */ 1) || |
| 590 | !CBB_add_u8(out, ECH_CLIENT_INNER)) { |
| 591 | return false; |
| 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | if (hs->ech_client_outer.empty()) { |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 597 | return true; |
| 598 | } |
| 599 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 600 | CBB ech_body; |
| 601 | if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) || |
| 602 | !CBB_add_u16_length_prefixed(out, &ech_body) || |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 603 | !CBB_add_u8(&ech_body, ECH_CLIENT_OUTER) || |
| 604 | !CBB_add_bytes(&ech_body, hs->ech_client_outer.data(), |
| 605 | hs->ech_client_outer.size()) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 606 | !CBB_flush(out)) { |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 607 | return false; |
| 608 | } |
David Benjamin | 246c556 | 2021-05-20 13:42:25 -0400 | [diff] [blame] | 609 | return true; |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 610 | } |
| 611 | |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 612 | static bool ext_ech_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 613 | CBS *contents) { |
David Benjamin | 5b7ec83 | 2021-06-02 11:12:13 -0400 | [diff] [blame] | 614 | SSL *const ssl = hs->ssl; |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 615 | if (contents == NULL) { |
| 616 | return true; |
| 617 | } |
| 618 | |
David Benjamin | 5b7ec83 | 2021-06-02 11:12:13 -0400 | [diff] [blame] | 619 | // The ECH extension may not be sent in TLS 1.2 ServerHello, only TLS 1.3 |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 620 | // EncryptedExtensions. It also may not be sent in response to an inner ECH |
| 621 | // extension. |
| 622 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION || |
| 623 | ssl->s3->ech_status == ssl_ech_accepted) { |
David Benjamin | 5b7ec83 | 2021-06-02 11:12:13 -0400 | [diff] [blame] | 624 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 625 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 626 | return false; |
| 627 | } |
| 628 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 629 | if (!ssl_is_valid_ech_config_list(*contents)) { |
| 630 | *out_alert = SSL_AD_DECODE_ERROR; |
| 631 | return false; |
| 632 | } |
| 633 | |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 634 | if (ssl->s3->ech_status == ssl_ech_rejected && |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 635 | !hs->ech_retry_configs.CopyFrom(*contents)) { |
| 636 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 637 | return false; |
| 638 | } |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 639 | |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 640 | return true; |
| 641 | } |
| 642 | |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 643 | static bool ext_ech_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 644 | CBS *contents) { |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 645 | if (contents == nullptr) { |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 646 | return true; |
| 647 | } |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 648 | |
| 649 | uint8_t type; |
| 650 | if (!CBS_get_u8(contents, &type)) { |
| 651 | return false; |
| 652 | } |
| 653 | if (type == ECH_CLIENT_OUTER) { |
| 654 | // Outer ECH extensions are handled outside the callback. |
| 655 | return true; |
| 656 | } |
| 657 | if (type != ECH_CLIENT_INNER || CBS_len(contents) != 0) { |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | hs->ech_is_inner = true; |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 662 | return true; |
| 663 | } |
| 664 | |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 665 | static bool ext_ech_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 666 | SSL *const ssl = hs->ssl; |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 667 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION || |
| 668 | ssl->s3->ech_status == ssl_ech_accepted || // |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 669 | hs->ech_keys == nullptr) { |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 670 | return true; |
| 671 | } |
| 672 | |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 673 | // Write the list of retry configs to |out|. Note |SSL_CTX_set1_ech_keys| |
| 674 | // ensures |ech_keys| contains at least one retry config. |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 675 | CBB body, retry_configs; |
| 676 | if (!CBB_add_u16(out, TLSEXT_TYPE_encrypted_client_hello) || |
| 677 | !CBB_add_u16_length_prefixed(out, &body) || |
| 678 | !CBB_add_u16_length_prefixed(&body, &retry_configs)) { |
| 679 | return false; |
| 680 | } |
David Benjamin | c3b373b | 2021-06-06 13:04:26 -0400 | [diff] [blame] | 681 | for (const auto &config : hs->ech_keys->configs) { |
David Benjamin | 1d58cd1 | 2021-05-04 15:24:24 -0400 | [diff] [blame] | 682 | if (!config->is_retry_config()) { |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 683 | continue; |
| 684 | } |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 685 | if (!CBB_add_bytes(&retry_configs, config->ech_config().raw.data(), |
| 686 | config->ech_config().raw.size())) { |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 687 | return false; |
| 688 | } |
| 689 | } |
| 690 | return CBB_flush(out); |
| 691 | } |
| 692 | |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 693 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 694 | // Renegotiation indication. |
| 695 | // |
| 696 | // https://tools.ietf.org/html/rfc5746 |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 697 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 698 | static bool ext_ri_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 699 | CBB *out_compressible, |
| 700 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 701 | const SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 702 | // Renegotiation indication is not necessary in TLS 1.3. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 703 | if (hs->min_version >= TLS1_3_VERSION || |
| 704 | type == ssl_client_hello_inner) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 705 | return true; |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 706 | } |
| 707 | |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 708 | assert(ssl->s3->initial_handshake_complete == |
| 709 | (ssl->s3->previous_client_finished_len != 0)); |
| 710 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 711 | CBB contents, prev_finished; |
| 712 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 713 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 714 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 715 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 716 | ssl->s3->previous_client_finished_len) || |
| 717 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 718 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 719 | } |
| 720 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 721 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 722 | } |
| 723 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 724 | static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 725 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 726 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 727 | if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 246eeee | 2017-03-26 12:49:17 -0500 | [diff] [blame] | 728 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 729 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 730 | } |
| 731 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 732 | // Servers may not switch between omitting the extension and supporting it. |
| 733 | // See RFC 5746, sections 3.5 and 4.2. |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 734 | if (ssl->s3->initial_handshake_complete && |
| 735 | (contents != NULL) != ssl->s3->send_connection_binding) { |
| 736 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 737 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 738 | return false; |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 739 | } |
| 740 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 741 | if (contents == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 742 | // Strictly speaking, if we want to avoid an attack we should *always* see |
| 743 | // RI even on initial ServerHello because the client doesn't see any |
| 744 | // renegotiation during an attack. However this would mean we could not |
| 745 | // connect to any server which doesn't support RI. |
| 746 | // |
| 747 | // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 748 | // practical terms every client sets it so it's just assumed here. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 749 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 753 | ssl->s3->previous_server_finished_len; |
| 754 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 755 | // Check for logic errors |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 756 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 757 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 758 | assert(ssl->s3->initial_handshake_complete == |
| 759 | (ssl->s3->previous_client_finished_len != 0)); |
| 760 | assert(ssl->s3->initial_handshake_complete == |
| 761 | (ssl->s3->previous_server_finished_len != 0)); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 762 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 763 | // Parse out the extension contents. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 764 | CBS renegotiated_connection; |
| 765 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 766 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 767 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 768 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 769 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 770 | } |
| 771 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 772 | // Check that the extension matches. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 773 | if (CBS_len(&renegotiated_connection) != expected_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 774 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 775 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 776 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | const uint8_t *d = CBS_data(&renegotiated_connection); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 780 | bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 781 | ssl->s3->previous_client_finished_len) == 0; |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 782 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 783 | ok = true; |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 784 | #endif |
| 785 | if (!ok) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 786 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 787 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 788 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 789 | } |
| 790 | d += ssl->s3->previous_client_finished_len; |
| 791 | |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 792 | ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 793 | ssl->s3->previous_server_finished_len) == 0; |
| 794 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 795 | ok = true; |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 796 | #endif |
| 797 | if (!ok) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 798 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 10e1060 | 2017-07-25 13:33:21 -0700 | [diff] [blame] | 799 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 800 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 801 | } |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 802 | ssl->s3->send_connection_binding = true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 803 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 804 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 805 | } |
| 806 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 807 | static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 808 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 809 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 810 | // Renegotiation isn't supported as a server so this function should never be |
| 811 | // called after the initial handshake. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 812 | assert(!ssl->s3->initial_handshake_complete); |
| 813 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 814 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 815 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 816 | } |
| 817 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 818 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 819 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | CBS renegotiated_connection; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 823 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 824 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 825 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 826 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 827 | } |
| 828 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 829 | // Check that the extension matches. We do not support renegotiation as a |
| 830 | // server, so this must be empty. |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 831 | if (CBS_len(&renegotiated_connection) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 832 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 833 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 834 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 835 | } |
| 836 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 837 | ssl->s3->send_connection_binding = true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 838 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 839 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 840 | } |
| 841 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 842 | static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 843 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 844 | // Renegotiation isn't supported as a server so this function should never be |
| 845 | // called after the initial handshake. |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 846 | assert(!ssl->s3->initial_handshake_complete); |
| 847 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 848 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 849 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 850 | } |
| 851 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 852 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 853 | !CBB_add_u16(out, 1 /* length */) || |
| 854 | !CBB_add_u8(out, 0 /* empty renegotiation info */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 855 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 856 | } |
| 857 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 858 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 861 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 862 | // Extended Master Secret. |
| 863 | // |
| 864 | // https://tools.ietf.org/html/rfc7627 |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 865 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 866 | static bool ext_ems_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 867 | CBB *out_compressible, |
| 868 | ssl_client_hello_type_t type) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 869 | // Extended master secret is not necessary in TLS 1.3. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 870 | if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 871 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 875 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 876 | return false; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 877 | } |
| 878 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 879 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 880 | } |
| 881 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 882 | static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 883 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 884 | SSL *const ssl = hs->ssl; |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 885 | |
| 886 | if (contents != NULL) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 887 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 888 | CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 889 | return false; |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 890 | } |
| 891 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 892 | hs->extended_master_secret = true; |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 893 | } |
| 894 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 895 | // Whether EMS is negotiated may not change on renegotiation. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 896 | if (ssl->s3->established_session != nullptr && |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 897 | hs->extended_master_secret != |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 898 | !!ssl->s3->established_session->extended_master_secret) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 899 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH); |
| 900 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 901 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 902 | } |
| 903 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 904 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 905 | } |
| 906 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 907 | static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 908 | CBS *contents) { |
David Benjamin | 9bb15f5 | 2018-06-26 00:07:40 -0400 | [diff] [blame] | 909 | if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 910 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 914 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 918 | return false; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 919 | } |
| 920 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 921 | hs->extended_master_secret = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 922 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 923 | } |
| 924 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 925 | static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 926 | if (!hs->extended_master_secret) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 927 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 931 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 932 | return false; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 933 | } |
| 934 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 935 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 938 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 939 | // Session tickets. |
| 940 | // |
| 941 | // https://tools.ietf.org/html/rfc5077 |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 942 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 943 | static bool ext_ticket_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 944 | CBB *out_compressible, |
| 945 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 946 | const SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 947 | // TLS 1.3 uses a different ticket extension. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 948 | if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner || |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 949 | SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 950 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 951 | } |
| 952 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 953 | Span<const uint8_t> ticket; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 954 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 955 | // Renegotiation does not participate in session resumption. However, still |
| 956 | // advertise the extension to avoid potentially breaking servers which carry |
| 957 | // over the state from the previous handshake, such as OpenSSL servers |
| 958 | // without upstream's 3c3f0259238594d77264a78944d409f2127642c4. |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 959 | if (!ssl->s3->initial_handshake_complete && |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 960 | ssl->session != nullptr && |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 961 | !ssl->session->ticket.empty() && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 962 | // Don't send TLS 1.3 session tickets in the ticket extension. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 963 | ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 964 | ticket = ssl->session->ticket; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 965 | } |
| 966 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 967 | CBB ticket_cbb; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 968 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 969 | !CBB_add_u16_length_prefixed(out, &ticket_cbb) || |
| 970 | !CBB_add_bytes(&ticket_cbb, ticket.data(), ticket.size()) || |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 971 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 972 | return false; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 973 | } |
| 974 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 975 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 976 | } |
| 977 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 978 | static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 979 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 980 | SSL *const ssl = hs->ssl; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 981 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 982 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 983 | } |
| 984 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 985 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 986 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 987 | } |
| 988 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 989 | // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 990 | // this function should never be called, even if the server tries to send the |
| 991 | // extension. |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 992 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 993 | |
| 994 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 995 | return false; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 996 | } |
| 997 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 998 | hs->ticket_expected = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 999 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1002 | static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1003 | if (!hs->ticket_expected) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1004 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1007 | // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1008 | assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0); |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1009 | |
| 1010 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1011 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1012 | return false; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1015 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1019 | // Signature Algorithms. |
| 1020 | // |
| 1021 | // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1022 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1023 | static bool ext_sigalgs_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1024 | CBB *out_compressible, |
| 1025 | ssl_client_hello_type_t type) { |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1026 | if (hs->max_version < TLS1_2_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1027 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1030 | CBB contents, sigalgs_cbb; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1031 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_signature_algorithms) || |
| 1032 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 1033 | !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) || |
David Benjamin | ebad508 | 2020-02-03 19:32:19 -0500 | [diff] [blame] | 1034 | !tls12_add_verify_sigalgs(hs, &sigalgs_cbb) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1035 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1036 | return false; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1039 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1042 | static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1043 | CBS *contents) { |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 1044 | hs->peer_sigalgs.Reset(); |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1045 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1046 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | CBS supported_signature_algorithms; |
| 1050 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1051 | CBS_len(contents) != 0 || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1052 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1053 | return false; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1056 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1059 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1060 | // OCSP Stapling. |
| 1061 | // |
| 1062 | // https://tools.ietf.org/html/rfc6066#section-8 |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1063 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1064 | static bool ext_ocsp_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1065 | CBB *out_compressible, |
| 1066 | ssl_client_hello_type_t type) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1067 | if (!hs->config->ocsp_stapling_enabled) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1068 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | CBB contents; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1072 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_status_request) || |
| 1073 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1074 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1075 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1076 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1077 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1078 | return false; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1081 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1084 | static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1085 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1086 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1087 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1088 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1091 | // TLS 1.3 OCSP responses are included in the Certificate extensions. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1092 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1093 | return false; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1096 | // OCSP stapling is forbidden on non-certificate ciphers. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1097 | if (CBS_len(contents) != 0 || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1098 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1099 | return false; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1100 | } |
| 1101 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1102 | // Note this does not check for resumption in TLS 1.2. Sending |
| 1103 | // status_request here does not make sense, but OpenSSL does so and the |
| 1104 | // specification does not say anything. Tolerate it but ignore it. |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1105 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1106 | hs->certificate_status_expected = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1107 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1110 | static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1111 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1112 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1113 | return true; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | uint8_t status_type; |
| 1117 | if (!CBS_get_u8(contents, &status_type)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1118 | return false; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1121 | // We cannot decide whether OCSP stapling will occur yet because the correct |
| 1122 | // SSL_CTX might not have been selected. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1123 | hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1124 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1125 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1128 | static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1129 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1130 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1131 | !hs->ocsp_stapling_requested || hs->config->cert->ocsp_response == NULL || |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1132 | ssl->s3->session_reused || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1133 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1134 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1137 | hs->certificate_status_expected = true; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1138 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1139 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1140 | CBB_add_u16(out, 0 /* length */); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1144 | // Next protocol negotiation. |
| 1145 | // |
| 1146 | // https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1147 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1148 | static bool ext_npn_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1149 | CBB *out_compressible, |
| 1150 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 1151 | const SSL *const ssl = hs->ssl; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1152 | if (ssl->ctx->next_proto_select_cb == NULL || |
| 1153 | // Do not allow NPN to change on renegotiation. |
| 1154 | ssl->s3->initial_handshake_complete || |
| 1155 | // NPN is not defined in DTLS or TLS 1.3. |
| 1156 | SSL_is_dtls(ssl) || hs->min_version >= TLS1_3_VERSION || |
| 1157 | type == ssl_client_hello_inner) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1158 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1162 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1163 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1166 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1169 | static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1170 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1171 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1172 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1173 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1176 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1177 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1178 | } |
| 1179 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1180 | // If any of these are false then we should never have sent the NPN |
| 1181 | // extension in the ClientHello and thus this function should never have been |
| 1182 | // called. |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1183 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1184 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1185 | assert(ssl->ctx->next_proto_select_cb != NULL); |
| 1186 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1187 | if (!ssl->s3->alpn_selected.empty()) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1188 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1189 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1190 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1191 | return false; |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1194 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1195 | const size_t orig_len = CBS_len(contents); |
| 1196 | |
| 1197 | while (CBS_len(contents) != 0) { |
| 1198 | CBS proto; |
| 1199 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1200 | CBS_len(&proto) == 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1201 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
David Benjamin | 9d64d8d | 2022-08-31 19:27:56 -0400 | [diff] [blame] | 1205 | // |orig_len| fits in |unsigned| because TLS extensions use 16-bit lengths. |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1206 | uint8_t *selected; |
| 1207 | uint8_t selected_len; |
| 1208 | if (ssl->ctx->next_proto_select_cb( |
David Benjamin | 9d64d8d | 2022-08-31 19:27:56 -0400 | [diff] [blame] | 1209 | ssl, &selected, &selected_len, orig_contents, |
| 1210 | static_cast<unsigned>(orig_len), |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1211 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK || |
| 1212 | !ssl->s3->next_proto_negotiated.CopyFrom( |
| 1213 | MakeConstSpan(selected, selected_len))) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1214 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1215 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1218 | hs->next_proto_neg_seen = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1219 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1222 | static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1223 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1224 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1225 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1226 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1227 | } |
| 1228 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1229 | if (contents != NULL && CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1230 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | if (contents == NULL || |
| 1234 | ssl->s3->initial_handshake_complete || |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1235 | ssl->ctx->next_protos_advertised_cb == NULL || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1236 | SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1237 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1240 | hs->next_proto_neg_seen = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1241 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1244 | static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1245 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1246 | // |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1247 | // parsed. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1248 | if (!hs->next_proto_neg_seen) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1249 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | const uint8_t *npa; |
| 1253 | unsigned npa_len; |
| 1254 | |
| 1255 | if (ssl->ctx->next_protos_advertised_cb( |
| 1256 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1257 | SSL_TLSEXT_ERR_OK) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1258 | hs->next_proto_neg_seen = false; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1259 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | CBB contents; |
| 1263 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1264 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1265 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1266 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1267 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1270 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1274 | // Signed certificate timestamps. |
| 1275 | // |
| 1276 | // https://tools.ietf.org/html/rfc6962#section-3.3.1 |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1277 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1278 | static bool ext_sct_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1279 | CBB *out_compressible, |
| 1280 | ssl_client_hello_type_t type) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1281 | if (!hs->config->signed_cert_timestamps_enabled) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1282 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1285 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_certificate_timestamp) || |
| 1286 | !CBB_add_u16(out_compressible, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1287 | return false; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1290 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1293 | static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1294 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1295 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1296 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1297 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1300 | // TLS 1.3 SCTs are included in the Certificate extensions. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1301 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1302 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1303 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1304 | } |
| 1305 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1306 | // If this is false then we should never have sent the SCT extension in the |
| 1307 | // ClientHello and thus this function should never have been called. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1308 | assert(hs->config->signed_cert_timestamps_enabled); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1309 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1310 | if (!ssl_is_sct_list_valid(contents)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1311 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1312 | return false; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1315 | // Session resumption uses the original session information. The extension |
| 1316 | // should not be sent on resumption, but RFC 6962 did not make it a |
| 1317 | // requirement, so tolerate this. |
| 1318 | // |
| 1319 | // TODO(davidben): Enforce this anyway. |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1320 | if (!ssl->s3->session_reused) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1321 | hs->new_session->signed_cert_timestamp_list.reset( |
| 1322 | CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool)); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1323 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
| 1324 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1325 | return false; |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1326 | } |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1329 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1332 | static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1333 | CBS *contents) { |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1334 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1335 | return true; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1339 | return false; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1340 | } |
| 1341 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1342 | hs->scts_requested = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1343 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1346 | static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1347 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1348 | // The extension shouldn't be sent when resuming sessions. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1349 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || ssl->s3->session_reused || |
| 1350 | hs->config->cert->signed_cert_timestamp_list == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1351 | return true; |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | CBB contents; |
| 1355 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1356 | CBB_add_u16_length_prefixed(out, &contents) && |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1357 | CBB_add_bytes( |
| 1358 | &contents, |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1359 | CRYPTO_BUFFER_data( |
| 1360 | hs->config->cert->signed_cert_timestamp_list.get()), |
| 1361 | CRYPTO_BUFFER_len( |
| 1362 | hs->config->cert->signed_cert_timestamp_list.get())) && |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1363 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1367 | // Application-level Protocol Negotiation. |
| 1368 | // |
| 1369 | // https://tools.ietf.org/html/rfc7301 |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1370 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1371 | static bool ext_alpn_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1372 | CBB *out_compressible, |
| 1373 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 1374 | const SSL *const ssl = hs->ssl; |
Nick Harper | 74161f4 | 2020-07-24 15:35:27 -0700 | [diff] [blame] | 1375 | if (hs->config->alpn_client_proto_list.empty() && ssl->quic_method) { |
| 1376 | // ALPN MUST be used with QUIC. |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1377 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
Nick Harper | 74161f4 | 2020-07-24 15:35:27 -0700 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1381 | if (hs->config->alpn_client_proto_list.empty() || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1382 | ssl->s3->initial_handshake_complete) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1383 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | CBB contents, proto_list; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1387 | if (!CBB_add_u16(out_compressible, |
| 1388 | TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1389 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1390 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1391 | !CBB_add_bytes(&proto_list, hs->config->alpn_client_proto_list.data(), |
| 1392 | hs->config->alpn_client_proto_list.size()) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1393 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1394 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1397 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1400 | static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1401 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1402 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1403 | if (contents == NULL) { |
Nick Harper | 74161f4 | 2020-07-24 15:35:27 -0700 | [diff] [blame] | 1404 | if (ssl->quic_method) { |
| 1405 | // ALPN is required when QUIC is used. |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1406 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
Nick Harper | 74161f4 | 2020-07-24 15:35:27 -0700 | [diff] [blame] | 1407 | *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL; |
| 1408 | return false; |
| 1409 | } |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1410 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1414 | assert(!hs->config->alpn_client_proto_list.empty()); |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1415 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1416 | if (hs->next_proto_neg_seen) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1417 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1418 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1419 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1420 | return false; |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1421 | } |
| 1422 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1423 | // The extension data consists of a ProtocolNameList which must have |
| 1424 | // exactly one ProtocolName. Each of these is length-prefixed. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1425 | CBS protocol_name_list, protocol_name; |
| 1426 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1427 | CBS_len(contents) != 0 || |
| 1428 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1429 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1430 | CBS_len(&protocol_name) == 0 || |
| 1431 | CBS_len(&protocol_name_list) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1432 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1435 | if (!ssl_is_alpn_protocol_allowed(hs, protocol_name)) { |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1436 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1437 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1438 | return false; |
David Benjamin | 3e51757d | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1441 | if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) { |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1442 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1443 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1446 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
David Benjamin | 12a3e7e | 2021-04-13 11:47:36 -0400 | [diff] [blame] | 1449 | bool ssl_is_valid_alpn_list(Span<const uint8_t> in) { |
| 1450 | CBS protocol_name_list = in; |
| 1451 | if (CBS_len(&protocol_name_list) == 0) { |
| 1452 | return false; |
| 1453 | } |
| 1454 | while (CBS_len(&protocol_name_list) > 0) { |
| 1455 | CBS protocol_name; |
| 1456 | if (!CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1457 | // Empty protocol names are forbidden. |
| 1458 | CBS_len(&protocol_name) == 0) { |
| 1459 | return false; |
| 1460 | } |
| 1461 | } |
| 1462 | return true; |
| 1463 | } |
| 1464 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1465 | bool ssl_is_alpn_protocol_allowed(const SSL_HANDSHAKE *hs, |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1466 | Span<const uint8_t> protocol) { |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1467 | if (hs->config->alpn_client_proto_list.empty()) { |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1468 | return false; |
| 1469 | } |
| 1470 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1471 | if (hs->ssl->ctx->allow_unknown_alpn_protos) { |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1472 | return true; |
| 1473 | } |
| 1474 | |
| 1475 | // Check that the protocol name is one of the ones we advertised. |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 1476 | CBS client_protocol_name_list = |
| 1477 | MakeConstSpan(hs->config->alpn_client_proto_list), |
| 1478 | client_protocol_name; |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1479 | while (CBS_len(&client_protocol_name_list) > 0) { |
| 1480 | if (!CBS_get_u8_length_prefixed(&client_protocol_name_list, |
| 1481 | &client_protocol_name)) { |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
| 1485 | if (client_protocol_name == protocol) { |
| 1486 | return true; |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1493 | bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1494 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1495 | SSL *const ssl = hs->ssl; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1496 | CBS contents; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1497 | if (ssl->ctx->alpn_select_cb == NULL || |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 1498 | !ssl_client_hello_get_extension( |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1499 | client_hello, &contents, |
| 1500 | TLSEXT_TYPE_application_layer_protocol_negotiation)) { |
Nick Harper | 74161f4 | 2020-07-24 15:35:27 -0700 | [diff] [blame] | 1501 | if (ssl->quic_method) { |
| 1502 | // ALPN is required when QUIC is used. |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1503 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
Nick Harper | 74161f4 | 2020-07-24 15:35:27 -0700 | [diff] [blame] | 1504 | *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL; |
| 1505 | return false; |
| 1506 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1507 | // Ignore ALPN if not configured or no extension was supplied. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1508 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1511 | // ALPN takes precedence over NPN. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1512 | hs->next_proto_neg_seen = false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1513 | |
| 1514 | CBS protocol_name_list; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1515 | if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) || |
| 1516 | CBS_len(&contents) != 0 || |
David Benjamin | 12a3e7e | 2021-04-13 11:47:36 -0400 | [diff] [blame] | 1517 | !ssl_is_valid_alpn_list(protocol_name_list)) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1518 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1519 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1520 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
David Benjamin | 9d64d8d | 2022-08-31 19:27:56 -0400 | [diff] [blame] | 1523 | // |protocol_name_list| fits in |unsigned| because TLS extensions use 16-bit |
| 1524 | // lengths. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1525 | const uint8_t *selected; |
| 1526 | uint8_t selected_len; |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1527 | int ret = ssl->ctx->alpn_select_cb( |
| 1528 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
David Benjamin | 9d64d8d | 2022-08-31 19:27:56 -0400 | [diff] [blame] | 1529 | static_cast<unsigned>(CBS_len(&protocol_name_list)), |
| 1530 | ssl->ctx->alpn_select_cb_arg); |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1531 | // ALPN is required when QUIC is used. |
| 1532 | if (ssl->quic_method && |
| 1533 | (ret == SSL_TLSEXT_ERR_NOACK || ret == SSL_TLSEXT_ERR_ALERT_WARNING)) { |
| 1534 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 1535 | } |
| 1536 | switch (ret) { |
| 1537 | case SSL_TLSEXT_ERR_OK: |
| 1538 | if (selected_len == 0) { |
| 1539 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1540 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1541 | return false; |
| 1542 | } |
| 1543 | if (!ssl->s3->alpn_selected.CopyFrom( |
| 1544 | MakeConstSpan(selected, selected_len))) { |
| 1545 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1546 | return false; |
| 1547 | } |
| 1548 | break; |
| 1549 | case SSL_TLSEXT_ERR_NOACK: |
| 1550 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 1551 | break; |
| 1552 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 1553 | *out_alert = SSL_AD_NO_APPLICATION_PROTOCOL; |
| 1554 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_APPLICATION_PROTOCOL); |
David Benjamin | fa544f1 | 2018-05-15 15:06:28 -0400 | [diff] [blame] | 1555 | return false; |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1556 | default: |
| 1557 | // Invalid return value. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1558 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | c02c19e | 2021-02-10 17:49:20 -0500 | [diff] [blame] | 1559 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1560 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1563 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1566 | static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1567 | SSL *const ssl = hs->ssl; |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1568 | if (ssl->s3->alpn_selected.empty()) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1569 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | CBB contents, proto_list, proto; |
| 1573 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1574 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1575 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1576 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1577 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(), |
| 1578 | ssl->s3->alpn_selected.size()) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1579 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1580 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1583 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1587 | // Channel ID. |
| 1588 | // |
| 1589 | // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1590 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1591 | static bool ext_channel_id_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1592 | CBB *out_compressible, |
| 1593 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 1594 | const SSL *const ssl = hs->ssl; |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 1595 | if (!hs->config->channel_id_private || SSL_is_dtls(ssl) || |
| 1596 | // Don't offer Channel ID in ClientHelloOuter. ClientHelloOuter handshakes |
| 1597 | // are not authenticated for the name that can learn the Channel ID. |
| 1598 | // |
| 1599 | // We could alternatively offer the extension but sign with a random key. |
| 1600 | // For other extensions, we try to align |ssl_client_hello_outer| and |
| 1601 | // |ssl_client_hello_unencrypted|, to improve the effectiveness of ECH |
| 1602 | // GREASE. However, Channel ID is deprecated and unlikely to be used with |
| 1603 | // ECH, so do the simplest thing. |
| 1604 | type == ssl_client_hello_outer) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1605 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
David Benjamin | ba423c9 | 2021-06-15 16:26:58 -0400 | [diff] [blame] | 1608 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1609 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1610 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1613 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1616 | static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1617 | uint8_t *out_alert, |
| 1618 | CBS *contents) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1619 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1620 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 1623 | assert(!SSL_is_dtls(hs->ssl)); |
David Benjamin | b587911 | 2021-05-18 17:10:18 -0400 | [diff] [blame] | 1624 | assert(hs->config->channel_id_private); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1625 | |
| 1626 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1627 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 1630 | hs->channel_id_negotiated = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1631 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1634 | static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1635 | uint8_t *out_alert, |
| 1636 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1637 | SSL *const ssl = hs->ssl; |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 1638 | if (contents == NULL || !hs->config->channel_id_enabled || SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1639 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1643 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 1646 | hs->channel_id_negotiated = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1647 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1650 | static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 1651 | if (!hs->channel_id_negotiated) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1652 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1656 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1657 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1660 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1663 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1664 | // Secure Real-time Transport Protocol (SRTP) extension. |
| 1665 | // |
| 1666 | // https://tools.ietf.org/html/rfc5764 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1667 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1668 | static bool ext_srtp_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1669 | CBB *out_compressible, |
| 1670 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 1671 | const SSL *const ssl = hs->ssl; |
David Benjamin | b778b9c | 2021-05-20 11:31:05 -0400 | [diff] [blame] | 1672 | const STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = |
| 1673 | SSL_get_srtp_profiles(ssl); |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1674 | if (profiles == NULL || |
David Benjamin | bc4c91a | 2021-05-18 16:06:30 -0400 | [diff] [blame] | 1675 | sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0 || |
| 1676 | !SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1677 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | CBB contents, profile_ids; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1681 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_srtp) || |
| 1682 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1683 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1684 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1687 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
| 1688 | if (!CBB_add_u16(&profile_ids, profile->id)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1689 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1694 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1695 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1698 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1701 | static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1702 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1703 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1704 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1705 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1708 | // The extension consists of a u16-prefixed profile ID list containing a |
| 1709 | // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1710 | // |
| 1711 | // See https://tools.ietf.org/html/rfc5764#section-4.1.1 |
David Benjamin | bc4c91a | 2021-05-18 16:06:30 -0400 | [diff] [blame] | 1712 | assert(SSL_is_dtls(ssl)); |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1713 | CBS profile_ids, srtp_mki; |
| 1714 | uint16_t profile_id; |
| 1715 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1716 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1717 | CBS_len(&profile_ids) != 0 || |
| 1718 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1719 | CBS_len(contents) != 0) { |
| 1720 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1721 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | if (CBS_len(&srtp_mki) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1725 | // Must be no MKI, since we never offer one. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1726 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1727 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1728 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
David Benjamin | b778b9c | 2021-05-20 11:31:05 -0400 | [diff] [blame] | 1731 | // Check to see if the server gave us something we support and offered. |
| 1732 | for (const SRTP_PROTECTION_PROFILE *profile : SSL_get_srtp_profiles(ssl)) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1733 | if (profile->id == profile_id) { |
David Benjamin | fceca8e | 2018-04-12 16:37:19 -0400 | [diff] [blame] | 1734 | ssl->s3->srtp_profile = profile; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1735 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1740 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1741 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1744 | static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1745 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1746 | SSL *const ssl = hs->ssl; |
David Benjamin | bc4c91a | 2021-05-18 16:06:30 -0400 | [diff] [blame] | 1747 | // DTLS-SRTP is only defined for DTLS. |
| 1748 | if (contents == NULL || !SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1749 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | CBS profile_ids, srtp_mki; |
| 1753 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1754 | CBS_len(&profile_ids) < 2 || |
| 1755 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1756 | CBS_len(contents) != 0) { |
| 1757 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1758 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1759 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1760 | // Discard the MKI value for now. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1761 | |
| 1762 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1763 | SSL_get_srtp_profiles(ssl); |
| 1764 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1765 | // Pick the server's most preferred profile. |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1766 | for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1767 | CBS profile_ids_tmp; |
| 1768 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1769 | |
| 1770 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1771 | uint16_t profile_id; |
| 1772 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1773 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | if (server_profile->id == profile_id) { |
David Benjamin | fceca8e | 2018-04-12 16:37:19 -0400 | [diff] [blame] | 1777 | ssl->s3->srtp_profile = server_profile; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1778 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1783 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1786 | static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1787 | SSL *const ssl = hs->ssl; |
David Benjamin | fceca8e | 2018-04-12 16:37:19 -0400 | [diff] [blame] | 1788 | if (ssl->s3->srtp_profile == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1789 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
David Benjamin | bc4c91a | 2021-05-18 16:06:30 -0400 | [diff] [blame] | 1792 | assert(SSL_is_dtls(ssl)); |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1793 | CBB contents, profile_ids; |
| 1794 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1795 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1796 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
David Benjamin | fceca8e | 2018-04-12 16:37:19 -0400 | [diff] [blame] | 1797 | !CBB_add_u16(&profile_ids, ssl->s3->srtp_profile->id) || |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1798 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1799 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1800 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1803 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1806 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1807 | // EC point formats. |
| 1808 | // |
| 1809 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1810 | |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 1811 | static bool ext_ec_point_add_extension(const SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1812 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1813 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1814 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1815 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1816 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1817 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1818 | return false; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1821 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1824 | static bool ext_ec_point_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 1825 | CBB *out_compressible, |
| 1826 | ssl_client_hello_type_t type) { |
Adam Langley | ffe384c | 2019-05-01 11:13:12 -0700 | [diff] [blame] | 1827 | // The point format extension is unnecessary in TLS 1.3. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1828 | if (hs->min_version >= TLS1_3_VERSION || type == ssl_client_hello_inner) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1829 | return true; |
David Benjamin | 70aba26 | 2016-11-01 12:08:15 -0400 | [diff] [blame] | 1830 | } |
| 1831 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1832 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1835 | static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1836 | CBS *contents) { |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1837 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1838 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1841 | if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1842 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1843 | } |
| 1844 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1845 | CBS ec_point_format_list; |
| 1846 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1847 | CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1848 | return false; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1851 | // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1852 | // point format. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1853 | if (OPENSSL_memchr(CBS_data(&ec_point_format_list), |
| 1854 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1855 | CBS_len(&ec_point_format_list)) == NULL) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1856 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1857 | return false; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1860 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1863 | static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1864 | CBS *contents) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1865 | if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1866 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1867 | } |
| 1868 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1869 | return ext_ec_point_parse_serverhello(hs, out_alert, contents); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1872 | static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1873 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1874 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1875 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1876 | } |
| 1877 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1878 | const uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1879 | const uint32_t alg_a = hs->new_cipher->algorithm_auth; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1880 | const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1881 | |
| 1882 | if (!using_ecc) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1883 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1886 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1889 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1890 | // Pre Shared Key |
| 1891 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 1892 | // https://tools.ietf.org/html/rfc8446#section-4.2.11 |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1893 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1894 | static bool should_offer_psk(const SSL_HANDSHAKE *hs, |
| 1895 | ssl_client_hello_type_t type) { |
David Benjamin | 350fe3b | 2021-06-02 17:58:53 -0400 | [diff] [blame] | 1896 | const SSL *const ssl = hs->ssl; |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 1897 | if (hs->max_version < TLS1_3_VERSION || ssl->session == nullptr || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1898 | ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION || |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 1899 | // TODO(https://crbug.com/boringssl/275): Should we synthesize a |
| 1900 | // placeholder PSK, at least when we offer early data? Otherwise |
| 1901 | // ClientHelloOuter will contain an early_data extension without a |
| 1902 | // pre_shared_key extension and potentially break the recovery flow. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1903 | type == ssl_client_hello_outer) { |
David Benjamin | 350fe3b | 2021-06-02 17:58:53 -0400 | [diff] [blame] | 1904 | return false; |
| 1905 | } |
| 1906 | |
| 1907 | // Per RFC 8446 section 4.1.4, skip offering the session if the selected |
| 1908 | // cipher in HelloRetryRequest does not match. This avoids performing the |
| 1909 | // transcript hash transformation for multiple hashes. |
| 1910 | if (ssl->s3->used_hello_retry_request && |
| 1911 | ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) { |
| 1912 | return false; |
| 1913 | } |
| 1914 | |
| 1915 | return true; |
| 1916 | } |
| 1917 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1918 | static size_t ext_pre_shared_key_clienthello_length( |
| 1919 | const SSL_HANDSHAKE *hs, ssl_client_hello_type_t type) { |
David Benjamin | 350fe3b | 2021-06-02 17:58:53 -0400 | [diff] [blame] | 1920 | const SSL *const ssl = hs->ssl; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1921 | if (!should_offer_psk(hs, type)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1922 | return 0; |
| 1923 | } |
| 1924 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 1925 | size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get())); |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1926 | return 15 + ssl->session->ticket.size() + binder_len; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1927 | } |
| 1928 | |
David Benjamin | 5acf9f4 | 2021-05-24 14:23:38 -0400 | [diff] [blame] | 1929 | static bool ext_pre_shared_key_add_clienthello(const SSL_HANDSHAKE *hs, |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1930 | CBB *out, bool *out_needs_binder, |
| 1931 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 1932 | const SSL *const ssl = hs->ssl; |
David Benjamin | 5acf9f4 | 2021-05-24 14:23:38 -0400 | [diff] [blame] | 1933 | *out_needs_binder = false; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 1934 | if (!should_offer_psk(hs, type)) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1935 | return true; |
| 1936 | } |
| 1937 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1938 | struct OPENSSL_timeval now; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1939 | ssl_get_current_time(ssl, &now); |
| 1940 | uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time); |
| 1941 | uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add; |
| 1942 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1943 | // Fill in a placeholder zero binder of the appropriate length. It will be |
| 1944 | // computed and filled in later after length prefixes are computed. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 1945 | size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session.get())); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1946 | |
| 1947 | CBB contents, identity, ticket, binders, binder; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1948 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1949 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1950 | !CBB_add_u16_length_prefixed(&contents, &identity) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1951 | !CBB_add_u16_length_prefixed(&identity, &ticket) || |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1952 | !CBB_add_bytes(&ticket, ssl->session->ticket.data(), |
| 1953 | ssl->session->ticket.size()) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1954 | !CBB_add_u32(&identity, obfuscated_ticket_age) || |
| 1955 | !CBB_add_u16_length_prefixed(&contents, &binders) || |
| 1956 | !CBB_add_u8_length_prefixed(&binders, &binder) || |
David Benjamin | 9545062 | 2021-07-16 18:24:02 -0400 | [diff] [blame] | 1957 | !CBB_add_zeros(&binder, binder_len)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1958 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1959 | } |
| 1960 | |
David Benjamin | 5acf9f4 | 2021-05-24 14:23:38 -0400 | [diff] [blame] | 1961 | *out_needs_binder = true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1962 | return CBB_flush(out); |
| 1963 | } |
| 1964 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1965 | bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1966 | uint8_t *out_alert, |
| 1967 | CBS *contents) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1968 | uint16_t psk_id; |
| 1969 | if (!CBS_get_u16(contents, &psk_id) || |
| 1970 | CBS_len(contents) != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1971 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1972 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1973 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1974 | } |
| 1975 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1976 | // We only advertise one PSK identity, so the only legal index is zero. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1977 | if (psk_id != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1978 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1979 | *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1980 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1981 | } |
| 1982 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1983 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1984 | } |
| 1985 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1986 | bool ssl_ext_pre_shared_key_parse_clienthello( |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1987 | SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
David Benjamin | 9806ae0 | 2019-08-16 15:32:03 -0400 | [diff] [blame] | 1988 | uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, |
| 1989 | const SSL_CLIENT_HELLO *client_hello, CBS *contents) { |
| 1990 | // Verify that the pre_shared_key extension is the last extension in |
| 1991 | // ClientHello. |
| 1992 | if (CBS_data(contents) + CBS_len(contents) != |
| 1993 | client_hello->extensions + client_hello->extensions_len) { |
| 1994 | OPENSSL_PUT_ERROR(SSL, SSL_R_PRE_SHARED_KEY_MUST_BE_LAST); |
| 1995 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1999 | // We only process the first PSK identity since we don't support pure PSK. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 2000 | CBS identities, binders; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2001 | if (!CBS_get_u16_length_prefixed(contents, &identities) || |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 2002 | !CBS_get_u16_length_prefixed(&identities, out_ticket) || |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 2003 | !CBS_get_u32(&identities, out_obfuscated_ticket_age) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2004 | !CBS_get_u16_length_prefixed(contents, &binders) || |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2005 | CBS_len(&binders) == 0 || |
Steven Valdez | af3b8a9 | 2016-11-01 12:49:22 -0400 | [diff] [blame] | 2006 | CBS_len(contents) != 0) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2007 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2008 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2009 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2010 | } |
| 2011 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2012 | *out_binders = binders; |
| 2013 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2014 | // Check the syntax of the remaining identities, but do not process them. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2015 | size_t num_identities = 1; |
| 2016 | while (CBS_len(&identities) != 0) { |
| 2017 | CBS unused_ticket; |
| 2018 | uint32_t unused_obfuscated_ticket_age; |
| 2019 | if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) || |
| 2020 | !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) { |
| 2021 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2022 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2023 | return false; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | num_identities++; |
| 2027 | } |
| 2028 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2029 | // Check the syntax of the binders. The value will be checked later if |
| 2030 | // resuming. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2031 | size_t num_binders = 0; |
| 2032 | while (CBS_len(&binders) != 0) { |
| 2033 | CBS binder; |
| 2034 | if (!CBS_get_u8_length_prefixed(&binders, &binder)) { |
| 2035 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2036 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2037 | return false; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | num_binders++; |
| 2041 | } |
| 2042 | |
| 2043 | if (num_identities != num_binders) { |
| 2044 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH); |
| 2045 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2046 | return false; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 2047 | } |
| 2048 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2049 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2052 | bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 2053 | if (!hs->ssl->s3->session_reused) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2054 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | CBB contents; |
| 2058 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 2059 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2060 | // We only consider the first identity for resumption |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2061 | !CBB_add_u16(&contents, 0) || |
| 2062 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2063 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2064 | } |
| 2065 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2066 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2070 | // Pre-Shared Key Exchange Modes |
| 2071 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2072 | // https://tools.ietf.org/html/rfc8446#section-4.2.9 |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2073 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2074 | static bool ext_psk_key_exchange_modes_add_clienthello( |
| 2075 | const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2076 | ssl_client_hello_type_t type) { |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2077 | if (hs->max_version < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2078 | return true; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | CBB contents, ke_modes; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2082 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_psk_key_exchange_modes) || |
| 2083 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2084 | !CBB_add_u8_length_prefixed(&contents, &ke_modes) || |
| 2085 | !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2086 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2087 | } |
| 2088 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2089 | return CBB_flush(out_compressible); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2090 | } |
| 2091 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2092 | static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2093 | uint8_t *out_alert, |
| 2094 | CBS *contents) { |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2095 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2096 | return true; |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2097 | } |
| 2098 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2099 | CBS ke_modes; |
| 2100 | if (!CBS_get_u8_length_prefixed(contents, &ke_modes) || |
| 2101 | CBS_len(&ke_modes) == 0 || |
| 2102 | CBS_len(contents) != 0) { |
| 2103 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2104 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2105 | } |
| 2106 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2107 | // We only support tickets with PSK_DHE_KE. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2108 | hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, |
| 2109 | CBS_len(&ke_modes)) != NULL; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2110 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2111 | return true; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2115 | // Early Data Indication |
| 2116 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2117 | // https://tools.ietf.org/html/rfc8446#section-4.2.10 |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2118 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2119 | static bool ext_early_data_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2120 | CBB *out_compressible, |
| 2121 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2122 | const SSL *const ssl = hs->ssl; |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2123 | // The second ClientHello never offers early data, and we must have already |
| 2124 | // filled in |early_data_reason| by this point. |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 2125 | if (ssl->s3->used_hello_retry_request) { |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2126 | assert(ssl->s3->early_data_reason != ssl_early_data_unknown); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2127 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2128 | } |
| 2129 | |
David Benjamin | 4e93cd4 | 2021-05-18 13:38:25 -0400 | [diff] [blame] | 2130 | if (!hs->early_data_offered) { |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2131 | return true; |
| 2132 | } |
| 2133 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2134 | // If offering ECH, the extension only applies to ClientHelloInner, but we |
| 2135 | // send the extension in both ClientHellos. This ensures that, if the server |
| 2136 | // handshakes with ClientHelloOuter, it can skip past early data. See |
David Benjamin | 0fa3030 | 2021-09-03 17:23:28 -0400 | [diff] [blame] | 2137 | // draft-ietf-tls-esni-13, section 6.1. |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2138 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_early_data) || |
| 2139 | !CBB_add_u16(out_compressible, 0) || |
| 2140 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2141 | return false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2142 | } |
| 2143 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2144 | return true; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2145 | } |
| 2146 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2147 | static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs, |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2148 | uint8_t *out_alert, |
| 2149 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2150 | SSL *const ssl = hs->ssl; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2151 | if (contents == NULL) { |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 2152 | if (hs->early_data_offered && !ssl->s3->used_hello_retry_request) { |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2153 | ssl->s3->early_data_reason = ssl->s3->session_reused |
| 2154 | ? ssl_early_data_peer_declined |
| 2155 | : ssl_early_data_session_not_resumed; |
| 2156 | } else { |
| 2157 | // We already filled in |early_data_reason| when declining to offer 0-RTT |
| 2158 | // or handling the implicit HelloRetryRequest reject. |
| 2159 | assert(ssl->s3->early_data_reason != ssl_early_data_unknown); |
| 2160 | } |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2161 | return true; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2162 | } |
| 2163 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2164 | // If we received an HRR, the second ClientHello never offers early data, so |
| 2165 | // the extensions logic will automatically reject early data extensions as |
| 2166 | // unsolicited. This covered by the ServerAcceptsEarlyDataOnHRR test. |
Kris Kwiatkowski | b11902a | 2019-08-24 11:01:04 +0100 | [diff] [blame] | 2167 | assert(!ssl->s3->used_hello_retry_request); |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2168 | |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2169 | if (CBS_len(contents) != 0) { |
| 2170 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2171 | return false; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2172 | } |
| 2173 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2174 | if (!ssl->s3->session_reused) { |
| 2175 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2176 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2177 | return false; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2178 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2179 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 2180 | ssl->s3->early_data_reason = ssl_early_data_accepted; |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 2181 | ssl->s3->early_data_accepted = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2182 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2183 | } |
| 2184 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2185 | static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2186 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2187 | SSL *const ssl = hs->ssl; |
| 2188 | if (contents == NULL || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 2189 | ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2190 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | if (CBS_len(contents) != 0) { |
| 2194 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2195 | return false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2196 | } |
| 2197 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2198 | hs->early_data_offered = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2199 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2200 | } |
| 2201 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2202 | static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 2203 | if (!hs->ssl->s3->early_data_accepted) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2204 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2208 | !CBB_add_u16(out, 0) || |
| 2209 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2210 | return false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2211 | } |
| 2212 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2213 | return true; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2214 | } |
| 2215 | |
| 2216 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2217 | // Key Share |
| 2218 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2219 | // https://tools.ietf.org/html/rfc8446#section-4.2.8 |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2220 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2221 | bool ssl_setup_key_shares(SSL_HANDSHAKE *hs, uint16_t override_group_id) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2222 | SSL *const ssl = hs->ssl; |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2223 | hs->key_shares[0].reset(); |
| 2224 | hs->key_shares[1].reset(); |
| 2225 | hs->key_share_bytes.Reset(); |
| 2226 | |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2227 | if (hs->max_version < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2228 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2229 | } |
| 2230 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2231 | bssl::ScopedCBB cbb; |
| 2232 | if (!CBB_init(cbb.get(), 64)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2233 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2234 | } |
| 2235 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2236 | if (override_group_id == 0 && ssl->ctx->grease_enabled) { |
David Benjamin | 3675eb3 | 2021-05-18 14:01:07 -0400 | [diff] [blame] | 2237 | // Add a fake group. See RFC 8701. |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2238 | if (!CBB_add_u16(cbb.get(), ssl_get_grease_value(hs, ssl_grease_group)) || |
| 2239 | !CBB_add_u16(cbb.get(), 1 /* length */) || |
| 2240 | !CBB_add_u8(cbb.get(), 0 /* one byte key share */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2241 | return false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2242 | } |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2243 | } |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2244 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2245 | uint16_t group_id = override_group_id; |
| 2246 | uint16_t second_group_id = 0; |
| 2247 | if (override_group_id == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2248 | // Predict the most preferred group. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2249 | Span<const uint16_t> groups = tls1_get_grouplist(hs); |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2250 | if (groups.empty()) { |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2251 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2252 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2253 | } |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2254 | |
| 2255 | group_id = groups[0]; |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2256 | |
Adam Langley | 1e97ce3 | 2023-01-23 21:11:44 +0000 | [diff] [blame] | 2257 | // We'll try to include one post-quantum and one classical initial key |
| 2258 | // share. |
| 2259 | for (size_t i = 1; i < groups.size() && second_group_id == 0; i++) { |
| 2260 | if (is_post_quantum_group(group_id) != is_post_quantum_group(groups[i])) { |
| 2261 | second_group_id = groups[i]; |
| 2262 | assert(second_group_id != group_id); |
| 2263 | } |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2264 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2265 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2266 | |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2267 | CBB key_exchange; |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2268 | hs->key_shares[0] = SSLKeyShare::Create(group_id); |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2269 | if (!hs->key_shares[0] || // |
| 2270 | !CBB_add_u16(cbb.get(), group_id) || |
| 2271 | !CBB_add_u16_length_prefixed(cbb.get(), &key_exchange) || |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2272 | !hs->key_shares[0]->Generate(&key_exchange)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2273 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2274 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2275 | |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2276 | if (second_group_id != 0) { |
| 2277 | hs->key_shares[1] = SSLKeyShare::Create(second_group_id); |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2278 | if (!hs->key_shares[1] || // |
| 2279 | !CBB_add_u16(cbb.get(), second_group_id) || |
| 2280 | !CBB_add_u16_length_prefixed(cbb.get(), &key_exchange) || |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2281 | !hs->key_shares[1]->Generate(&key_exchange)) { |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2282 | return false; |
| 2283 | } |
| 2284 | } |
| 2285 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2286 | return CBBFinishArray(cbb.get(), &hs->key_share_bytes); |
| 2287 | } |
| 2288 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2289 | static bool ext_key_share_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2290 | CBB *out_compressible, |
| 2291 | ssl_client_hello_type_t type) { |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2292 | if (hs->max_version < TLS1_3_VERSION) { |
| 2293 | return true; |
| 2294 | } |
| 2295 | |
| 2296 | assert(!hs->key_share_bytes.empty()); |
| 2297 | CBB contents, kse_bytes; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2298 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_key_share) || |
| 2299 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2300 | !CBB_add_u16_length_prefixed(&contents, &kse_bytes) || |
| 2301 | !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(), |
| 2302 | hs->key_share_bytes.size()) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2303 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2304 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2305 | } |
| 2306 | |
David Benjamin | 97ede40 | 2021-05-18 14:17:52 -0400 | [diff] [blame] | 2307 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2308 | } |
| 2309 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2310 | bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2311 | Array<uint8_t> *out_secret, |
| 2312 | uint8_t *out_alert, CBS *contents) { |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2313 | CBS ciphertext; |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2314 | uint16_t group_id; |
| 2315 | if (!CBS_get_u16(contents, &group_id) || |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2316 | !CBS_get_u16_length_prefixed(contents, &ciphertext) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2317 | CBS_len(contents) != 0) { |
David Benjamin | ac4d534 | 2017-11-17 01:42:04 +0800 | [diff] [blame] | 2318 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2319 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2320 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2321 | } |
| 2322 | |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2323 | SSLKeyShare *key_share = hs->key_shares[0].get(); |
| 2324 | if (key_share->GroupID() != group_id) { |
| 2325 | if (!hs->key_shares[1] || hs->key_shares[1]->GroupID() != group_id) { |
| 2326 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2327 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 2328 | return false; |
| 2329 | } |
| 2330 | key_share = hs->key_shares[1].get(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2331 | } |
| 2332 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2333 | if (!key_share->Decap(out_secret, out_alert, ciphertext)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2334 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2335 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2338 | hs->new_session->group_id = group_id; |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2339 | hs->key_shares[0].reset(); |
| 2340 | hs->key_shares[1].reset(); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2341 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2342 | } |
| 2343 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2344 | bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2345 | Span<const uint8_t> *out_peer_key, |
| 2346 | uint8_t *out_alert, |
| 2347 | const SSL_CLIENT_HELLO *client_hello) { |
| 2348 | // We only support connections that include an ECDHE key exchange. |
| 2349 | CBS contents; |
| 2350 | if (!ssl_client_hello_get_extension(client_hello, &contents, |
| 2351 | TLSEXT_TYPE_key_share)) { |
| 2352 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 2353 | *out_alert = SSL_AD_MISSING_EXTENSION; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2354 | return false; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2355 | } |
| 2356 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2357 | CBS key_shares; |
| 2358 | if (!CBS_get_u16_length_prefixed(&contents, &key_shares) || |
| 2359 | CBS_len(&contents) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2360 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2361 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2362 | } |
| 2363 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2364 | // Find the corresponding key share. |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2365 | const uint16_t group_id = hs->new_session->group_id; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2366 | CBS peer_key; |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2367 | CBS_init(&peer_key, nullptr, 0); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2368 | while (CBS_len(&key_shares) > 0) { |
| 2369 | uint16_t id; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2370 | CBS peer_key_tmp; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2371 | if (!CBS_get_u16(&key_shares, &id) || |
Steven Valdez | 619c8ce | 2017-10-16 13:12:33 -0400 | [diff] [blame] | 2372 | !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) || |
| 2373 | CBS_len(&peer_key_tmp) == 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2374 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2375 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2376 | } |
| 2377 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2378 | if (id == group_id) { |
Steven Valdez | 619c8ce | 2017-10-16 13:12:33 -0400 | [diff] [blame] | 2379 | if (CBS_len(&peer_key) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2380 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE); |
| 2381 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2382 | return false; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2383 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2384 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2385 | peer_key = peer_key_tmp; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2386 | // Continue parsing the structure to keep peers honest. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2387 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2388 | } |
| 2389 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2390 | if (out_peer_key != nullptr) { |
| 2391 | *out_peer_key = peer_key; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2392 | } |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2393 | *out_found = CBS_len(&peer_key) != 0; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2394 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2395 | } |
| 2396 | |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2397 | bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2398 | CBB entry, ciphertext; |
David Benjamin | 3b8c5ec | 2021-04-12 17:43:23 -0400 | [diff] [blame] | 2399 | if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 2400 | !CBB_add_u16_length_prefixed(out, &entry) || |
| 2401 | !CBB_add_u16(&entry, hs->new_session->group_id) || |
| 2402 | !CBB_add_u16_length_prefixed(&entry, &ciphertext) || |
| 2403 | !CBB_add_bytes(&ciphertext, hs->key_share_ciphertext.data(), |
| 2404 | hs->key_share_ciphertext.size()) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2405 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2406 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2407 | } |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2408 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2412 | // Supported Versions |
| 2413 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2414 | // https://tools.ietf.org/html/rfc8446#section-4.2.1 |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2415 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2416 | static bool ext_supported_versions_add_clienthello( |
| 2417 | const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2418 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2419 | const SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2420 | if (hs->max_version <= TLS1_2_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2421 | return true; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2422 | } |
| 2423 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2424 | // supported_versions is compressible in ECH if ClientHelloOuter already |
| 2425 | // requires TLS 1.3. Otherwise the extensions differ in the older versions. |
| 2426 | if (hs->min_version >= TLS1_3_VERSION) { |
| 2427 | out = out_compressible; |
| 2428 | } |
| 2429 | |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2430 | CBB contents, versions; |
| 2431 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 2432 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2433 | !CBB_add_u8_length_prefixed(&contents, &versions)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2434 | return false; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2435 | } |
| 2436 | |
David Benjamin | 3675eb3 | 2021-05-18 14:01:07 -0400 | [diff] [blame] | 2437 | // Add a fake version. See RFC 8701. |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2438 | if (ssl->ctx->grease_enabled && |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2439 | !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2440 | return false; |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2441 | } |
| 2442 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2443 | // Encrypted ClientHellos requires TLS 1.3 or later. |
| 2444 | uint16_t extra_min_version = |
| 2445 | type == ssl_client_hello_inner ? TLS1_3_VERSION : 0; |
| 2446 | if (!ssl_add_supported_versions(hs, &versions, extra_min_version) || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 2447 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2448 | return false; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2449 | } |
| 2450 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2451 | return true; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2455 | // Cookie |
| 2456 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2457 | // https://tools.ietf.org/html/rfc8446#section-4.2.2 |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2458 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2459 | static bool ext_cookie_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2460 | CBB *out_compressible, |
| 2461 | ssl_client_hello_type_t type) { |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2462 | if (hs->cookie.empty()) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2463 | return true; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | CBB contents, cookie; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2467 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_cookie) || |
| 2468 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2469 | !CBB_add_u16_length_prefixed(&contents, &cookie) || |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2470 | !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2471 | !CBB_flush(out_compressible)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2472 | return false; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2473 | } |
| 2474 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2475 | return true; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2479 | // Supported Groups |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2480 | // |
David Benjamin | a130ce0 | 2018-08-14 22:26:39 -0500 | [diff] [blame] | 2481 | // https://tools.ietf.org/html/rfc4492#section-5.1.1 |
| 2482 | // https://tools.ietf.org/html/rfc8446#section-4.2.7 |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2483 | |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2484 | static bool ext_supported_groups_add_clienthello(const SSL_HANDSHAKE *hs, |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2485 | CBB *out, |
| 2486 | CBB *out_compressible, |
| 2487 | ssl_client_hello_type_t type) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2488 | const SSL *const ssl = hs->ssl; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2489 | CBB contents, groups_bytes; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2490 | if (!CBB_add_u16(out_compressible, TLSEXT_TYPE_supported_groups) || |
| 2491 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2492 | !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2493 | return false; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
David Benjamin | 3675eb3 | 2021-05-18 14:01:07 -0400 | [diff] [blame] | 2496 | // Add a fake group. See RFC 8701. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2497 | if (ssl->ctx->grease_enabled && |
| 2498 | !CBB_add_u16(&groups_bytes, |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2499 | ssl_get_grease_value(hs, ssl_grease_group))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2500 | return false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2501 | } |
| 2502 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2503 | for (uint16_t group : tls1_get_grouplist(hs)) { |
Kris Kwiatkowski | 78c88c9 | 2019-03-11 14:40:59 +0000 | [diff] [blame] | 2504 | if (is_post_quantum_group(group) && |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 2505 | hs->max_version < TLS1_3_VERSION) { |
| 2506 | continue; |
| 2507 | } |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2508 | if (!CBB_add_u16(&groups_bytes, group)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2509 | return false; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2513 | return CBB_flush(out_compressible); |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2516 | static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2517 | uint8_t *out_alert, |
| 2518 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2519 | // This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2520 | // BigIP servers send it nonetheless, so do not enforce this. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2521 | return true; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2524 | static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) { |
| 2525 | CBS copy = *cbs; |
| 2526 | if ((CBS_len(©) & 1) != 0) { |
| 2527 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2528 | return false; |
| 2529 | } |
| 2530 | |
| 2531 | Array<uint16_t> ret; |
| 2532 | if (!ret.Init(CBS_len(©) / 2)) { |
| 2533 | return false; |
| 2534 | } |
| 2535 | for (size_t i = 0; i < ret.size(); i++) { |
| 2536 | if (!CBS_get_u16(©, &ret[i])) { |
| 2537 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2538 | return false; |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | assert(CBS_len(©) == 0); |
| 2543 | *out = std::move(ret); |
Anton Bikineev | 50e7ea5 | 2022-01-23 22:35:48 +0100 | [diff] [blame] | 2544 | return true; |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2545 | } |
| 2546 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2547 | static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2548 | uint8_t *out_alert, |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2549 | CBS *contents) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2550 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2551 | return true; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2554 | CBS supported_group_list; |
| 2555 | if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2556 | CBS_len(&supported_group_list) == 0 || |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2557 | CBS_len(contents) != 0 || |
| 2558 | !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2559 | return false; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2562 | return true; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2566 | // QUIC Transport Parameters |
| 2567 | |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2568 | static bool ext_quic_transport_params_add_clienthello_impl( |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2569 | const SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) { |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2570 | if (hs->config->quic_transport_params.empty() && !hs->ssl->quic_method) { |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2571 | return true; |
| 2572 | } |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2573 | if (hs->config->quic_transport_params.empty() || !hs->ssl->quic_method) { |
| 2574 | // QUIC Transport Parameters must be sent over QUIC, and they must not be |
| 2575 | // sent over non-QUIC transports. If transport params are set, then |
| 2576 | // SSL(_CTX)_set_quic_method must also be called. |
| 2577 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED); |
| 2578 | return false; |
| 2579 | } |
| 2580 | assert(hs->min_version > TLS1_2_VERSION); |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2581 | if (use_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2582 | // Do nothing, we'll send the other codepoint. |
| 2583 | return true; |
| 2584 | } |
| 2585 | |
David Benjamin | a1d3bfb | 2021-06-01 12:12:44 -0400 | [diff] [blame] | 2586 | uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters; |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2587 | if (hs->config->quic_use_legacy_codepoint) { |
| 2588 | extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy; |
| 2589 | } |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2590 | |
| 2591 | CBB contents; |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2592 | if (!CBB_add_u16(out, extension_type) || |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2593 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2594 | !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(), |
| 2595 | hs->config->quic_transport_params.size()) || |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2596 | !CBB_flush(out)) { |
| 2597 | return false; |
| 2598 | } |
| 2599 | return true; |
| 2600 | } |
| 2601 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2602 | static bool ext_quic_transport_params_add_clienthello( |
| 2603 | const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2604 | ssl_client_hello_type_t type) { |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2605 | return ext_quic_transport_params_add_clienthello_impl( |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2606 | hs, out_compressible, /*use_legacy_codepoint=*/false); |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2609 | static bool ext_quic_transport_params_add_clienthello_legacy( |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2610 | const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2611 | ssl_client_hello_type_t type) { |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2612 | return ext_quic_transport_params_add_clienthello_impl( |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2613 | hs, out_compressible, /*use_legacy_codepoint=*/true); |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | static bool ext_quic_transport_params_parse_serverhello_impl( |
| 2617 | SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents, |
| 2618 | bool used_legacy_codepoint) { |
Adam Langley | ca058c0 | 2020-12-16 10:11:08 -0800 | [diff] [blame] | 2619 | SSL *const ssl = hs->ssl; |
| 2620 | if (contents == nullptr) { |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2621 | if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2622 | // Silently ignore because we expect the other QUIC codepoint. |
| 2623 | return true; |
| 2624 | } |
Adam Langley | ca058c0 | 2020-12-16 10:11:08 -0800 | [diff] [blame] | 2625 | if (!ssl->quic_method) { |
| 2626 | return true; |
| 2627 | } |
Adam Langley | ca058c0 | 2020-12-16 10:11:08 -0800 | [diff] [blame] | 2628 | *out_alert = SSL_AD_MISSING_EXTENSION; |
| 2629 | return false; |
David Schinazi | 7ba96a6 | 2020-12-14 14:46:56 -0800 | [diff] [blame] | 2630 | } |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2631 | // The extensions parser will check for unsolicited extensions before |
| 2632 | // calling the callback. |
| 2633 | assert(ssl->quic_method != nullptr); |
Adam Langley | ca058c0 | 2020-12-16 10:11:08 -0800 | [diff] [blame] | 2634 | assert(ssl_protocol_version(ssl) == TLS1_3_VERSION); |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2635 | assert(used_legacy_codepoint == hs->config->quic_use_legacy_codepoint); |
Adam Langley | ca058c0 | 2020-12-16 10:11:08 -0800 | [diff] [blame] | 2636 | return ssl->s3->peer_quic_transport_params.CopyFrom(*contents); |
| 2637 | } |
| 2638 | |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2639 | static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs, |
Adam Langley | ca058c0 | 2020-12-16 10:11:08 -0800 | [diff] [blame] | 2640 | uint8_t *out_alert, |
| 2641 | CBS *contents) { |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2642 | return ext_quic_transport_params_parse_serverhello_impl( |
| 2643 | hs, out_alert, contents, /*used_legacy_codepoint=*/false); |
| 2644 | } |
| 2645 | |
| 2646 | static bool ext_quic_transport_params_parse_serverhello_legacy( |
| 2647 | SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents) { |
| 2648 | return ext_quic_transport_params_parse_serverhello_impl( |
| 2649 | hs, out_alert, contents, /*used_legacy_codepoint=*/true); |
| 2650 | } |
| 2651 | |
| 2652 | static bool ext_quic_transport_params_parse_clienthello_impl( |
| 2653 | SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents, |
| 2654 | bool used_legacy_codepoint) { |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2655 | SSL *const ssl = hs->ssl; |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2656 | if (!contents) { |
| 2657 | if (!ssl->quic_method) { |
| 2658 | if (hs->config->quic_transport_params.empty()) { |
| 2659 | return true; |
| 2660 | } |
| 2661 | // QUIC transport parameters must not be set if |ssl| is not configured |
| 2662 | // for QUIC. |
| 2663 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED); |
| 2664 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2665 | return false; |
| 2666 | } |
| 2667 | if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2668 | // Silently ignore because we expect the other QUIC codepoint. |
| 2669 | return true; |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2670 | } |
| 2671 | *out_alert = SSL_AD_MISSING_EXTENSION; |
| 2672 | return false; |
| 2673 | } |
| 2674 | if (!ssl->quic_method) { |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2675 | if (used_legacy_codepoint) { |
| 2676 | // Ignore the legacy private-use codepoint because that could be sent |
| 2677 | // to mean something else than QUIC transport parameters. |
| 2678 | return true; |
| 2679 | } |
| 2680 | // Fail if we received the codepoint registered with IANA for QUIC |
| 2681 | // because that is not allowed outside of QUIC. |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2682 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2683 | return false; |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2684 | } |
Nick Harper | 80ddfc7 | 2020-03-11 18:26:31 -0700 | [diff] [blame] | 2685 | assert(ssl_protocol_version(ssl) == TLS1_3_VERSION); |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2686 | if (used_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2687 | // Silently ignore because we expect the other QUIC codepoint. |
| 2688 | return true; |
| 2689 | } |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2690 | return ssl->s3->peer_quic_transport_params.CopyFrom(*contents); |
| 2691 | } |
| 2692 | |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2693 | static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2694 | uint8_t *out_alert, |
| 2695 | CBS *contents) { |
| 2696 | return ext_quic_transport_params_parse_clienthello_impl( |
| 2697 | hs, out_alert, contents, /*used_legacy_codepoint=*/false); |
| 2698 | } |
| 2699 | |
| 2700 | static bool ext_quic_transport_params_parse_clienthello_legacy( |
| 2701 | SSL_HANDSHAKE *hs, uint8_t *out_alert, CBS *contents) { |
| 2702 | return ext_quic_transport_params_parse_clienthello_impl( |
| 2703 | hs, out_alert, contents, /*used_legacy_codepoint=*/true); |
| 2704 | } |
| 2705 | |
| 2706 | static bool ext_quic_transport_params_add_serverhello_impl( |
| 2707 | SSL_HANDSHAKE *hs, CBB *out, bool use_legacy_codepoint) { |
| 2708 | if (hs->ssl->quic_method == nullptr && use_legacy_codepoint) { |
| 2709 | // Ignore the legacy private-use codepoint because that could be sent |
| 2710 | // to mean something else than QUIC transport parameters. |
| 2711 | return true; |
| 2712 | } |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2713 | assert(hs->ssl->quic_method != nullptr); |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2714 | if (hs->config->quic_transport_params.empty()) { |
Nick Harper | 72cff81 | 2020-03-26 18:06:16 -0700 | [diff] [blame] | 2715 | // Transport parameters must be set when using QUIC. |
| 2716 | OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_TRANSPORT_PARAMETERS_MISCONFIGURED); |
| 2717 | return false; |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2718 | } |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2719 | if (use_legacy_codepoint != hs->config->quic_use_legacy_codepoint) { |
| 2720 | // Do nothing, we'll send the other codepoint. |
| 2721 | return true; |
| 2722 | } |
| 2723 | |
David Benjamin | a1d3bfb | 2021-06-01 12:12:44 -0400 | [diff] [blame] | 2724 | uint16_t extension_type = TLSEXT_TYPE_quic_transport_parameters; |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2725 | if (hs->config->quic_use_legacy_codepoint) { |
| 2726 | extension_type = TLSEXT_TYPE_quic_transport_parameters_legacy; |
| 2727 | } |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2728 | |
| 2729 | CBB contents; |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2730 | if (!CBB_add_u16(out, extension_type) || |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2731 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 2732 | !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(), |
| 2733 | hs->config->quic_transport_params.size()) || |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2734 | !CBB_flush(out)) { |
| 2735 | return false; |
| 2736 | } |
| 2737 | |
| 2738 | return true; |
| 2739 | } |
| 2740 | |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 2741 | static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs, |
| 2742 | CBB *out) { |
| 2743 | return ext_quic_transport_params_add_serverhello_impl( |
| 2744 | hs, out, /*use_legacy_codepoint=*/false); |
| 2745 | } |
| 2746 | |
| 2747 | static bool ext_quic_transport_params_add_serverhello_legacy(SSL_HANDSHAKE *hs, |
| 2748 | CBB *out) { |
| 2749 | return ext_quic_transport_params_add_serverhello_impl( |
| 2750 | hs, out, /*use_legacy_codepoint=*/true); |
| 2751 | } |
| 2752 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 2753 | // Delegated credentials. |
| 2754 | // |
| 2755 | // https://tools.ietf.org/html/draft-ietf-tls-subcerts |
| 2756 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2757 | static bool ext_delegated_credential_add_clienthello( |
| 2758 | const SSL_HANDSHAKE *hs, CBB *out, CBB *out_compressible, |
| 2759 | ssl_client_hello_type_t type) { |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 2760 | return true; |
| 2761 | } |
| 2762 | |
| 2763 | static bool ext_delegated_credential_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2764 | uint8_t *out_alert, |
| 2765 | CBS *contents) { |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 2766 | if (contents == nullptr || ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
| 2767 | // Don't use delegated credentials unless we're negotiating TLS 1.3 or |
| 2768 | // higher. |
| 2769 | return true; |
| 2770 | } |
| 2771 | |
Watson Ladd | dcd6e44 | 2020-08-10 15:12:45 -0400 | [diff] [blame] | 2772 | // The contents of the extension are the signature algorithms the client will |
| 2773 | // accept for a delegated credential. |
| 2774 | CBS sigalg_list; |
| 2775 | if (!CBS_get_u16_length_prefixed(contents, &sigalg_list) || |
| 2776 | CBS_len(&sigalg_list) == 0 || |
| 2777 | CBS_len(contents) != 0 || |
| 2778 | !parse_u16_array(&sigalg_list, &hs->peer_delegated_credential_sigalgs)) { |
| 2779 | return false; |
| 2780 | } |
| 2781 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 2782 | hs->delegated_credential_requested = true; |
| 2783 | return true; |
| 2784 | } |
| 2785 | |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2786 | // Certificate compression |
| 2787 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2788 | static bool cert_compression_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2789 | CBB *out_compressible, |
| 2790 | ssl_client_hello_type_t type) { |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 2791 | bool first = true; |
| 2792 | CBB contents, algs; |
| 2793 | |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2794 | for (const auto &alg : hs->ssl->ctx->cert_compression_algs) { |
| 2795 | if (alg.decompress == nullptr) { |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 2796 | continue; |
| 2797 | } |
| 2798 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2799 | if (first && |
| 2800 | (!CBB_add_u16(out_compressible, TLSEXT_TYPE_cert_compression) || |
| 2801 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
| 2802 | !CBB_add_u8_length_prefixed(&contents, &algs))) { |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 2803 | return false; |
| 2804 | } |
| 2805 | first = false; |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2806 | if (!CBB_add_u16(&algs, alg.alg_id)) { |
Adam Langley | 0080d83 | 2018-06-07 16:39:49 -0700 | [diff] [blame] | 2807 | return false; |
| 2808 | } |
| 2809 | } |
| 2810 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2811 | return first || CBB_flush(out_compressible); |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2815 | uint8_t *out_alert, |
| 2816 | CBS *contents) { |
| 2817 | if (contents == nullptr) { |
| 2818 | return true; |
| 2819 | } |
| 2820 | |
| 2821 | // The server may not echo this extension. Any server to client negotiation is |
| 2822 | // advertised in the CertificateRequest message. |
| 2823 | return false; |
| 2824 | } |
| 2825 | |
| 2826 | static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2827 | uint8_t *out_alert, |
| 2828 | CBS *contents) { |
| 2829 | if (contents == nullptr) { |
| 2830 | return true; |
| 2831 | } |
| 2832 | |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2833 | const SSL_CTX *ctx = hs->ssl->ctx.get(); |
| 2834 | const size_t num_algs = ctx->cert_compression_algs.size(); |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2835 | |
| 2836 | CBS alg_ids; |
| 2837 | if (!CBS_get_u8_length_prefixed(contents, &alg_ids) || |
| 2838 | CBS_len(contents) != 0 || |
| 2839 | CBS_len(&alg_ids) == 0 || |
| 2840 | CBS_len(&alg_ids) % 2 == 1) { |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
| 2844 | const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2; |
| 2845 | Array<uint16_t> given_alg_ids; |
| 2846 | if (!given_alg_ids.Init(num_given_alg_ids)) { |
| 2847 | return false; |
| 2848 | } |
| 2849 | |
| 2850 | size_t best_index = num_algs; |
| 2851 | size_t given_alg_idx = 0; |
| 2852 | |
| 2853 | while (CBS_len(&alg_ids) > 0) { |
| 2854 | uint16_t alg_id; |
| 2855 | if (!CBS_get_u16(&alg_ids, &alg_id)) { |
| 2856 | return false; |
| 2857 | } |
| 2858 | |
| 2859 | given_alg_ids[given_alg_idx++] = alg_id; |
| 2860 | |
| 2861 | for (size_t i = 0; i < num_algs; i++) { |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2862 | const auto &alg = ctx->cert_compression_algs[i]; |
| 2863 | if (alg.alg_id == alg_id && alg.compress != nullptr) { |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2864 | if (i < best_index) { |
| 2865 | best_index = i; |
| 2866 | } |
| 2867 | break; |
| 2868 | } |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t), |
| 2873 | compare_uint16_t); |
| 2874 | for (size_t i = 1; i < num_given_alg_ids; i++) { |
| 2875 | if (given_alg_ids[i - 1] == given_alg_ids[i]) { |
| 2876 | return false; |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | if (best_index < num_algs && |
| 2881 | ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
| 2882 | hs->cert_compression_negotiated = true; |
David Benjamin | 8fe1584 | 2019-10-08 16:57:38 -0400 | [diff] [blame] | 2883 | hs->cert_compression_alg_id = ctx->cert_compression_algs[best_index].alg_id; |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | return true; |
| 2887 | } |
| 2888 | |
| 2889 | static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2890 | return true; |
| 2891 | } |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2892 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2893 | // Application-level Protocol Settings |
| 2894 | // |
| 2895 | // https://tools.ietf.org/html/draft-vvv-tls-alps-01 |
| 2896 | |
David Benjamin | 4e93cd4 | 2021-05-18 13:38:25 -0400 | [diff] [blame] | 2897 | bool ssl_get_local_application_settings(const SSL_HANDSHAKE *hs, |
| 2898 | Span<const uint8_t> *out_settings, |
| 2899 | Span<const uint8_t> protocol) { |
| 2900 | for (const ALPSConfig &config : hs->config->alps_configs) { |
| 2901 | if (protocol == config.protocol) { |
| 2902 | *out_settings = config.settings; |
| 2903 | return true; |
| 2904 | } |
| 2905 | } |
| 2906 | return false; |
| 2907 | } |
| 2908 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2909 | static bool ext_alps_add_clienthello_impl(const SSL_HANDSHAKE *hs, CBB *out, |
| 2910 | CBB *out_compressible, |
| 2911 | ssl_client_hello_type_t type, |
| 2912 | bool use_new_codepoint) { |
David Benjamin | 14e51ad | 2021-05-19 15:24:34 -0400 | [diff] [blame] | 2913 | const SSL *const ssl = hs->ssl; |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2914 | if (// ALPS requires TLS 1.3. |
| 2915 | hs->max_version < TLS1_3_VERSION || |
| 2916 | // Do not offer ALPS without ALPN. |
| 2917 | hs->config->alpn_client_proto_list.empty() || |
| 2918 | // Do not offer ALPS if not configured. |
| 2919 | hs->config->alps_configs.empty() || |
| 2920 | // Do not offer ALPS on renegotiation handshakes. |
| 2921 | ssl->s3->initial_handshake_complete) { |
| 2922 | return true; |
| 2923 | } |
| 2924 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2925 | if (use_new_codepoint != hs->config->alps_use_new_codepoint) { |
| 2926 | // Do nothing, we'll send the other codepoint. |
| 2927 | return true; |
| 2928 | } |
| 2929 | |
| 2930 | uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 2931 | if (hs->config->alps_use_new_codepoint) { |
| 2932 | extension_type = TLSEXT_TYPE_application_settings; |
| 2933 | } |
| 2934 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2935 | CBB contents, proto_list, proto; |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2936 | if (!CBB_add_u16(out_compressible, extension_type) || |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2937 | !CBB_add_u16_length_prefixed(out_compressible, &contents) || |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2938 | !CBB_add_u16_length_prefixed(&contents, &proto_list)) { |
| 2939 | return false; |
| 2940 | } |
| 2941 | |
| 2942 | for (const ALPSConfig &config : hs->config->alps_configs) { |
| 2943 | if (!CBB_add_u8_length_prefixed(&proto_list, &proto) || |
| 2944 | !CBB_add_bytes(&proto, config.protocol.data(), |
| 2945 | config.protocol.size())) { |
| 2946 | return false; |
| 2947 | } |
| 2948 | } |
| 2949 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 2950 | return CBB_flush(out_compressible); |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2951 | } |
| 2952 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2953 | static bool ext_alps_add_clienthello(const SSL_HANDSHAKE *hs, CBB *out, |
| 2954 | CBB *out_compressible, |
| 2955 | ssl_client_hello_type_t type) { |
| 2956 | return ext_alps_add_clienthello_impl(hs, out, out_compressible, type, |
| 2957 | /*use_new_codepoint=*/true); |
| 2958 | } |
| 2959 | |
| 2960 | static bool ext_alps_add_clienthello_old(const SSL_HANDSHAKE *hs, CBB *out, |
| 2961 | CBB *out_compressible, |
| 2962 | ssl_client_hello_type_t type) { |
| 2963 | return ext_alps_add_clienthello_impl(hs, out, out_compressible, type, |
| 2964 | /*use_new_codepoint=*/false); |
| 2965 | } |
| 2966 | |
| 2967 | static bool ext_alps_parse_serverhello_impl(SSL_HANDSHAKE *hs, |
| 2968 | uint8_t *out_alert, |
| 2969 | CBS *contents, |
| 2970 | bool use_new_codepoint) { |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2971 | SSL *const ssl = hs->ssl; |
| 2972 | if (contents == nullptr) { |
| 2973 | return true; |
| 2974 | } |
| 2975 | |
| 2976 | assert(!ssl->s3->initial_handshake_complete); |
| 2977 | assert(!hs->config->alpn_client_proto_list.empty()); |
| 2978 | assert(!hs->config->alps_configs.empty()); |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2979 | assert(use_new_codepoint == hs->config->alps_use_new_codepoint); |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 2980 | |
| 2981 | // ALPS requires TLS 1.3. |
| 2982 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2983 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2984 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2985 | return false; |
| 2986 | } |
| 2987 | |
| 2988 | // Note extension callbacks may run in any order, so we defer checking |
| 2989 | // consistency with ALPN to |ssl_check_serverhello_tlsext|. |
| 2990 | if (!hs->new_session->peer_application_settings.CopyFrom(*contents)) { |
| 2991 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2992 | return false; |
| 2993 | } |
| 2994 | |
| 2995 | hs->new_session->has_application_settings = true; |
| 2996 | return true; |
| 2997 | } |
| 2998 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 2999 | static bool ext_alps_parse_serverhello(SSL_HANDSHAKE *hs, |
| 3000 | uint8_t *out_alert, |
| 3001 | CBS *contents) { |
| 3002 | return ext_alps_parse_serverhello_impl(hs, out_alert, contents, |
| 3003 | /*use_new_codepoint=*/true); |
| 3004 | } |
| 3005 | |
| 3006 | static bool ext_alps_parse_serverhello_old(SSL_HANDSHAKE *hs, |
| 3007 | uint8_t *out_alert, |
| 3008 | CBS *contents) { |
| 3009 | return ext_alps_parse_serverhello_impl(hs, out_alert, contents, |
| 3010 | /*use_new_codepoint=*/false); |
| 3011 | } |
| 3012 | |
| 3013 | static bool ext_alps_add_serverhello_impl(SSL_HANDSHAKE *hs, CBB *out, |
| 3014 | bool use_new_codepoint) { |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3015 | SSL *const ssl = hs->ssl; |
| 3016 | // If early data is accepted, we omit the ALPS extension. It is implicitly |
| 3017 | // carried over from the previous connection. |
| 3018 | if (hs->new_session == nullptr || |
| 3019 | !hs->new_session->has_application_settings || |
| 3020 | ssl->s3->early_data_accepted) { |
| 3021 | return true; |
| 3022 | } |
| 3023 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3024 | if (use_new_codepoint != hs->config->alps_use_new_codepoint) { |
| 3025 | // Do nothing, we'll send the other codepoint. |
| 3026 | return true; |
| 3027 | } |
| 3028 | |
| 3029 | uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 3030 | if (hs->config->alps_use_new_codepoint) { |
| 3031 | extension_type = TLSEXT_TYPE_application_settings; |
| 3032 | } |
| 3033 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3034 | CBB contents; |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3035 | if (!CBB_add_u16(out, extension_type) || |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3036 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 3037 | !CBB_add_bytes(&contents, |
| 3038 | hs->new_session->local_application_settings.data(), |
| 3039 | hs->new_session->local_application_settings.size()) || |
| 3040 | !CBB_flush(out)) { |
| 3041 | return false; |
| 3042 | } |
| 3043 | |
| 3044 | return true; |
| 3045 | } |
| 3046 | |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3047 | static bool ext_alps_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 3048 | return ext_alps_add_serverhello_impl(hs, out, /*use_new_codepoint=*/true); |
| 3049 | } |
| 3050 | |
| 3051 | static bool ext_alps_add_serverhello_old(SSL_HANDSHAKE *hs, CBB *out) { |
| 3052 | return ext_alps_add_serverhello_impl(hs, out, /*use_new_codepoint=*/false); |
| 3053 | } |
| 3054 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3055 | bool ssl_negotiate_alps(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 3056 | const SSL_CLIENT_HELLO *client_hello) { |
| 3057 | SSL *const ssl = hs->ssl; |
| 3058 | if (ssl->s3->alpn_selected.empty()) { |
| 3059 | return true; |
| 3060 | } |
| 3061 | |
| 3062 | // If we negotiate ALPN over TLS 1.3, try to negotiate ALPS. |
| 3063 | CBS alps_contents; |
| 3064 | Span<const uint8_t> settings; |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3065 | uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 3066 | if (hs->config->alps_use_new_codepoint) { |
| 3067 | extension_type = TLSEXT_TYPE_application_settings; |
| 3068 | } |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3069 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION && |
| 3070 | ssl_get_local_application_settings(hs, &settings, |
| 3071 | ssl->s3->alpn_selected) && |
| 3072 | ssl_client_hello_get_extension(client_hello, &alps_contents, |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3073 | extension_type)) { |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3074 | // Check if the client supports ALPS with the selected ALPN. |
| 3075 | bool found = false; |
| 3076 | CBS alps_list; |
| 3077 | if (!CBS_get_u16_length_prefixed(&alps_contents, &alps_list) || |
| 3078 | CBS_len(&alps_contents) != 0 || |
| 3079 | CBS_len(&alps_list) == 0) { |
| 3080 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3081 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3082 | return false; |
| 3083 | } |
| 3084 | while (CBS_len(&alps_list) > 0) { |
| 3085 | CBS protocol_name; |
| 3086 | if (!CBS_get_u8_length_prefixed(&alps_list, &protocol_name) || |
| 3087 | // Empty protocol names are forbidden. |
| 3088 | CBS_len(&protocol_name) == 0) { |
| 3089 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3090 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3091 | return false; |
| 3092 | } |
| 3093 | if (protocol_name == MakeConstSpan(ssl->s3->alpn_selected)) { |
| 3094 | found = true; |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | // Negotiate ALPS if both client also supports ALPS for this protocol. |
| 3099 | if (found) { |
| 3100 | hs->new_session->has_application_settings = true; |
| 3101 | if (!hs->new_session->local_application_settings.CopyFrom(settings)) { |
| 3102 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 3103 | return false; |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | return true; |
| 3109 | } |
Adam Langley | a86c698 | 2019-07-16 15:26:21 -0700 | [diff] [blame] | 3110 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3111 | // kExtensions contains all the supported extensions. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3112 | static const struct tls_extension kExtensions[] = { |
| 3113 | { |
| 3114 | TLSEXT_TYPE_server_name, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3115 | ext_sni_add_clienthello, |
| 3116 | ext_sni_parse_serverhello, |
| 3117 | ext_sni_parse_clienthello, |
| 3118 | ext_sni_add_serverhello, |
| 3119 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 3120 | { |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 3121 | TLSEXT_TYPE_encrypted_client_hello, |
Dan McArdle | 1920c6f | 2020-03-11 17:29:40 -0400 | [diff] [blame] | 3122 | ext_ech_add_clienthello, |
| 3123 | ext_ech_parse_serverhello, |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 3124 | ext_ech_parse_clienthello, |
Daniel McArdle | 00e434d | 2021-02-18 11:47:18 -0500 | [diff] [blame] | 3125 | ext_ech_add_serverhello, |
Dan McArdle | c295935 | 2020-10-29 14:31:31 -0400 | [diff] [blame] | 3126 | }, |
| 3127 | { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 3128 | TLSEXT_TYPE_extended_master_secret, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 3129 | ext_ems_add_clienthello, |
| 3130 | ext_ems_parse_serverhello, |
| 3131 | ext_ems_parse_clienthello, |
| 3132 | ext_ems_add_serverhello, |
| 3133 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 3134 | { |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3135 | TLSEXT_TYPE_renegotiate, |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3136 | ext_ri_add_clienthello, |
| 3137 | ext_ri_parse_serverhello, |
| 3138 | ext_ri_parse_clienthello, |
| 3139 | ext_ri_add_serverhello, |
| 3140 | }, |
| 3141 | { |
| 3142 | TLSEXT_TYPE_supported_groups, |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3143 | ext_supported_groups_add_clienthello, |
| 3144 | ext_supported_groups_parse_serverhello, |
| 3145 | ext_supported_groups_parse_clienthello, |
| 3146 | dont_add_serverhello, |
| 3147 | }, |
| 3148 | { |
| 3149 | TLSEXT_TYPE_ec_point_formats, |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3150 | ext_ec_point_add_clienthello, |
| 3151 | ext_ec_point_parse_serverhello, |
| 3152 | ext_ec_point_parse_clienthello, |
| 3153 | ext_ec_point_add_serverhello, |
| 3154 | }, |
| 3155 | { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 3156 | TLSEXT_TYPE_session_ticket, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 3157 | ext_ticket_add_clienthello, |
| 3158 | ext_ticket_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3159 | // Ticket extension client parsing is handled in ssl_session.c |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 3160 | ignore_parse_clienthello, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 3161 | ext_ticket_add_serverhello, |
| 3162 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 3163 | { |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3164 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3165 | ext_alpn_add_clienthello, |
| 3166 | ext_alpn_parse_serverhello, |
| 3167 | // ALPN is negotiated late in |ssl_negotiate_alpn|. |
| 3168 | ignore_parse_clienthello, |
| 3169 | ext_alpn_add_serverhello, |
| 3170 | }, |
| 3171 | { |
| 3172 | TLSEXT_TYPE_status_request, |
Adam Langley | d668095 | 2018-08-23 08:01:23 -0700 | [diff] [blame] | 3173 | ext_ocsp_add_clienthello, |
| 3174 | ext_ocsp_parse_serverhello, |
| 3175 | ext_ocsp_parse_clienthello, |
| 3176 | ext_ocsp_add_serverhello, |
| 3177 | }, |
| 3178 | { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 3179 | TLSEXT_TYPE_signature_algorithms, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 3180 | ext_sigalgs_add_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 3181 | forbid_parse_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 3182 | ext_sigalgs_parse_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 3183 | dont_add_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 3184 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 3185 | { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 3186 | TLSEXT_TYPE_next_proto_neg, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 3187 | ext_npn_add_clienthello, |
| 3188 | ext_npn_parse_serverhello, |
| 3189 | ext_npn_parse_clienthello, |
| 3190 | ext_npn_add_serverhello, |
| 3191 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 3192 | { |
| 3193 | TLSEXT_TYPE_certificate_timestamp, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 3194 | ext_sct_add_clienthello, |
| 3195 | ext_sct_parse_serverhello, |
| 3196 | ext_sct_parse_clienthello, |
| 3197 | ext_sct_add_serverhello, |
| 3198 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 3199 | { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 3200 | TLSEXT_TYPE_channel_id, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 3201 | ext_channel_id_add_clienthello, |
| 3202 | ext_channel_id_parse_serverhello, |
| 3203 | ext_channel_id_parse_clienthello, |
| 3204 | ext_channel_id_add_serverhello, |
| 3205 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 3206 | { |
| 3207 | TLSEXT_TYPE_srtp, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 3208 | ext_srtp_add_clienthello, |
| 3209 | ext_srtp_parse_serverhello, |
| 3210 | ext_srtp_parse_clienthello, |
| 3211 | ext_srtp_add_serverhello, |
| 3212 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 3213 | { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 3214 | TLSEXT_TYPE_key_share, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3215 | ext_key_share_add_clienthello, |
| 3216 | forbid_parse_serverhello, |
| 3217 | ignore_parse_clienthello, |
| 3218 | dont_add_serverhello, |
| 3219 | }, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3220 | { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 3221 | TLSEXT_TYPE_psk_key_exchange_modes, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 3222 | ext_psk_key_exchange_modes_add_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3223 | forbid_parse_serverhello, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 3224 | ext_psk_key_exchange_modes_parse_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3225 | dont_add_serverhello, |
| 3226 | }, |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 3227 | { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 3228 | TLSEXT_TYPE_early_data, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 3229 | ext_early_data_add_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 3230 | ext_early_data_parse_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 3231 | ext_early_data_parse_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 3232 | ext_early_data_add_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 3233 | }, |
| 3234 | { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 3235 | TLSEXT_TYPE_supported_versions, |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 3236 | ext_supported_versions_add_clienthello, |
| 3237 | forbid_parse_serverhello, |
| 3238 | ignore_parse_clienthello, |
| 3239 | dont_add_serverhello, |
| 3240 | }, |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 3241 | { |
| 3242 | TLSEXT_TYPE_cookie, |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 3243 | ext_cookie_add_clienthello, |
| 3244 | forbid_parse_serverhello, |
| 3245 | ignore_parse_clienthello, |
| 3246 | dont_add_serverhello, |
| 3247 | }, |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 3248 | { |
David Benjamin | a1d3bfb | 2021-06-01 12:12:44 -0400 | [diff] [blame] | 3249 | TLSEXT_TYPE_quic_transport_parameters, |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 3250 | ext_quic_transport_params_add_clienthello, |
| 3251 | ext_quic_transport_params_parse_serverhello, |
| 3252 | ext_quic_transport_params_parse_clienthello, |
| 3253 | ext_quic_transport_params_add_serverhello, |
| 3254 | }, |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 3255 | { |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 3256 | TLSEXT_TYPE_quic_transport_parameters_legacy, |
David Schinazi | 3d8b8c3 | 2021-01-14 11:25:49 -0800 | [diff] [blame] | 3257 | ext_quic_transport_params_add_clienthello_legacy, |
| 3258 | ext_quic_transport_params_parse_serverhello_legacy, |
| 3259 | ext_quic_transport_params_parse_clienthello_legacy, |
| 3260 | ext_quic_transport_params_add_serverhello_legacy, |
| 3261 | }, |
| 3262 | { |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 3263 | TLSEXT_TYPE_cert_compression, |
Adam Langley | a307cb7 | 2018-05-02 09:06:48 -0700 | [diff] [blame] | 3264 | cert_compression_add_clienthello, |
| 3265 | cert_compression_parse_serverhello, |
| 3266 | cert_compression_parse_clienthello, |
| 3267 | cert_compression_add_serverhello, |
| 3268 | }, |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 3269 | { |
| 3270 | TLSEXT_TYPE_delegated_credential, |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 3271 | ext_delegated_credential_add_clienthello, |
| 3272 | forbid_parse_serverhello, |
| 3273 | ext_delegated_credential_parse_clienthello, |
| 3274 | dont_add_serverhello, |
| 3275 | }, |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3276 | { |
| 3277 | TLSEXT_TYPE_application_settings, |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3278 | ext_alps_add_clienthello, |
| 3279 | ext_alps_parse_serverhello, |
| 3280 | // ALPS is negotiated late in |ssl_negotiate_alpn|. |
| 3281 | ignore_parse_clienthello, |
| 3282 | ext_alps_add_serverhello, |
| 3283 | }, |
Victor Tan | 558960d | 2023-06-23 15:04:33 +0000 | [diff] [blame] | 3284 | { |
| 3285 | TLSEXT_TYPE_application_settings_old, |
| 3286 | ext_alps_add_clienthello_old, |
| 3287 | ext_alps_parse_serverhello_old, |
| 3288 | // ALPS is negotiated late in |ssl_negotiate_alpn|. |
| 3289 | ignore_parse_clienthello, |
| 3290 | ext_alps_add_serverhello_old, |
| 3291 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3292 | }; |
| 3293 | |
| 3294 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 3295 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3296 | static_assert(kNumExtensions <= |
| 3297 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8, |
| 3298 | "too many extensions for sent bitset"); |
| 3299 | static_assert(kNumExtensions <= |
| 3300 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8, |
| 3301 | "too many extensions for received bitset"); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 3302 | |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 3303 | bool ssl_setup_extension_permutation(SSL_HANDSHAKE *hs) { |
| 3304 | if (!hs->config->permute_extensions) { |
| 3305 | return true; |
| 3306 | } |
| 3307 | |
| 3308 | static_assert(kNumExtensions <= UINT8_MAX, |
| 3309 | "extensions_permutation type is too small"); |
| 3310 | uint32_t seeds[kNumExtensions - 1]; |
| 3311 | Array<uint8_t> permutation; |
| 3312 | if (!RAND_bytes(reinterpret_cast<uint8_t *>(seeds), sizeof(seeds)) || |
| 3313 | !permutation.Init(kNumExtensions)) { |
| 3314 | return false; |
| 3315 | } |
| 3316 | for (size_t i = 0; i < kNumExtensions; i++) { |
| 3317 | permutation[i] = i; |
| 3318 | } |
| 3319 | for (size_t i = kNumExtensions - 1; i > 0; i--) { |
| 3320 | // Set element |i| to a randomly-selected element 0 <= j <= i. |
| 3321 | std::swap(permutation[i], permutation[seeds[i - 1] % (i + 1)]); |
| 3322 | } |
| 3323 | hs->extension_permutation = std::move(permutation); |
| 3324 | return true; |
| 3325 | } |
| 3326 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3327 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 3328 | uint16_t value) { |
| 3329 | unsigned i; |
| 3330 | for (i = 0; i < kNumExtensions; i++) { |
| 3331 | if (kExtensions[i].value == value) { |
| 3332 | *out_index = i; |
| 3333 | return &kExtensions[i]; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | return NULL; |
| 3338 | } |
| 3339 | |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3340 | static bool add_padding_extension(CBB *cbb, uint16_t ext, size_t len) { |
| 3341 | CBB child; |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3342 | if (!CBB_add_u16(cbb, ext) || // |
| 3343 | !CBB_add_u16_length_prefixed(cbb, &child) || |
David Benjamin | 9545062 | 2021-07-16 18:24:02 -0400 | [diff] [blame] | 3344 | !CBB_add_zeros(&child, len)) { |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3345 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3346 | return false; |
| 3347 | } |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3348 | return CBB_flush(cbb); |
| 3349 | } |
| 3350 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3351 | static bool ssl_add_clienthello_tlsext_inner(SSL_HANDSHAKE *hs, CBB *out, |
| 3352 | CBB *out_encoded, |
| 3353 | bool *out_needs_psk_binder) { |
| 3354 | // When writing ClientHelloInner, we construct the real and encoded |
| 3355 | // ClientHellos concurrently, to handle compression. Uncompressed extensions |
| 3356 | // are written to |extensions| and copied to |extensions_encoded|. Compressed |
| 3357 | // extensions are buffered in |compressed| and written to the end. (ECH can |
| 3358 | // only compress continguous extensions.) |
| 3359 | SSL *const ssl = hs->ssl; |
| 3360 | bssl::ScopedCBB compressed, outer_extensions; |
| 3361 | CBB extensions, extensions_encoded; |
| 3362 | if (!CBB_add_u16_length_prefixed(out, &extensions) || |
| 3363 | !CBB_add_u16_length_prefixed(out_encoded, &extensions_encoded) || |
| 3364 | !CBB_init(compressed.get(), 64) || |
| 3365 | !CBB_init(outer_extensions.get(), 64)) { |
| 3366 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3367 | return false; |
| 3368 | } |
| 3369 | |
| 3370 | hs->inner_extensions_sent = 0; |
| 3371 | |
| 3372 | if (ssl->ctx->grease_enabled) { |
| 3373 | // Add a fake empty extension. See RFC 8701. This always matches |
| 3374 | // |ssl_add_clienthello_tlsext|, so compress it. |
| 3375 | uint16_t grease_ext = ssl_get_grease_value(hs, ssl_grease_extension1); |
| 3376 | if (!add_padding_extension(compressed.get(), grease_ext, 0) || |
| 3377 | !CBB_add_u16(outer_extensions.get(), grease_ext)) { |
| 3378 | return false; |
| 3379 | } |
| 3380 | } |
| 3381 | |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 3382 | for (size_t unpermuted = 0; unpermuted < kNumExtensions; unpermuted++) { |
| 3383 | size_t i = hs->extension_permutation.empty() |
| 3384 | ? unpermuted |
| 3385 | : hs->extension_permutation[unpermuted]; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3386 | const size_t len_before = CBB_len(&extensions); |
| 3387 | const size_t len_compressed_before = CBB_len(compressed.get()); |
| 3388 | if (!kExtensions[i].add_clienthello(hs, &extensions, compressed.get(), |
| 3389 | ssl_client_hello_inner)) { |
| 3390 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 3391 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3392 | return false; |
| 3393 | } |
| 3394 | |
| 3395 | const size_t bytes_written = CBB_len(&extensions) - len_before; |
| 3396 | const size_t bytes_written_compressed = |
| 3397 | CBB_len(compressed.get()) - len_compressed_before; |
| 3398 | // The callback may write to at most one output. |
| 3399 | assert(bytes_written == 0 || bytes_written_compressed == 0); |
| 3400 | if (bytes_written != 0 || bytes_written_compressed != 0) { |
| 3401 | hs->inner_extensions_sent |= (1u << i); |
| 3402 | } |
| 3403 | // If compressed, update the running ech_outer_extensions extension. |
| 3404 | if (bytes_written_compressed != 0 && |
| 3405 | !CBB_add_u16(outer_extensions.get(), kExtensions[i].value)) { |
| 3406 | return false; |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | if (ssl->ctx->grease_enabled) { |
| 3411 | // Add a fake non-empty extension. See RFC 8701. This always matches |
| 3412 | // |ssl_add_clienthello_tlsext|, so compress it. |
| 3413 | uint16_t grease_ext = ssl_get_grease_value(hs, ssl_grease_extension2); |
| 3414 | if (!add_padding_extension(compressed.get(), grease_ext, 1) || |
| 3415 | !CBB_add_u16(outer_extensions.get(), grease_ext)) { |
| 3416 | return false; |
| 3417 | } |
| 3418 | } |
| 3419 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3420 | // Uncompressed extensions are encoded as-is. |
| 3421 | if (!CBB_add_bytes(&extensions_encoded, CBB_data(&extensions), |
| 3422 | CBB_len(&extensions))) { |
| 3423 | return false; |
| 3424 | } |
| 3425 | |
| 3426 | // Flush all the compressed extensions. |
| 3427 | if (CBB_len(compressed.get()) != 0) { |
| 3428 | CBB extension, child; |
| 3429 | // Copy them as-is in the real ClientHelloInner. |
| 3430 | if (!CBB_add_bytes(&extensions, CBB_data(compressed.get()), |
| 3431 | CBB_len(compressed.get())) || |
| 3432 | // Replace with ech_outer_extensions in the encoded form. |
| 3433 | !CBB_add_u16(&extensions_encoded, TLSEXT_TYPE_ech_outer_extensions) || |
| 3434 | !CBB_add_u16_length_prefixed(&extensions_encoded, &extension) || |
| 3435 | !CBB_add_u8_length_prefixed(&extension, &child) || |
| 3436 | !CBB_add_bytes(&child, CBB_data(outer_extensions.get()), |
| 3437 | CBB_len(outer_extensions.get())) || |
| 3438 | !CBB_flush(&extensions_encoded)) { |
| 3439 | return false; |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | // The PSK extension must be last. It is never compressed. Note, if there is a |
| 3444 | // binder, the caller will need to update both ClientHelloInner and |
| 3445 | // EncodedClientHelloInner after computing it. |
| 3446 | const size_t len_before = CBB_len(&extensions); |
| 3447 | if (!ext_pre_shared_key_add_clienthello(hs, &extensions, out_needs_psk_binder, |
| 3448 | ssl_client_hello_inner) || |
| 3449 | !CBB_add_bytes(&extensions_encoded, CBB_data(&extensions) + len_before, |
| 3450 | CBB_len(&extensions) - len_before) || |
| 3451 | !CBB_flush(out) || // |
| 3452 | !CBB_flush(out_encoded)) { |
| 3453 | return false; |
| 3454 | } |
| 3455 | |
| 3456 | return true; |
| 3457 | } |
| 3458 | |
| 3459 | bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, CBB *out_encoded, |
| 3460 | bool *out_needs_psk_binder, |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 3461 | ssl_client_hello_type_t type, |
| 3462 | size_t header_len) { |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3463 | *out_needs_psk_binder = false; |
| 3464 | |
| 3465 | if (type == ssl_client_hello_inner) { |
| 3466 | return ssl_add_clienthello_tlsext_inner(hs, out, out_encoded, |
| 3467 | out_needs_psk_binder); |
| 3468 | } |
| 3469 | |
| 3470 | assert(out_encoded == nullptr); // Only ClientHelloInner needs two outputs. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3471 | SSL *const ssl = hs->ssl; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 3472 | CBB extensions; |
| 3473 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3474 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3475 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3476 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3477 | |
David Benjamin | 6477012 | 2019-05-04 11:00:04 -0500 | [diff] [blame] | 3478 | // Note we may send multiple ClientHellos for DTLS HelloVerifyRequest and TLS |
| 3479 | // 1.3 HelloRetryRequest. For the latter, the extensions may change, so it is |
| 3480 | // important to reset this value. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3481 | hs->extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3482 | |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3483 | // Add a fake empty extension. See RFC 8701. |
| 3484 | if (ssl->ctx->grease_enabled && |
| 3485 | !add_padding_extension( |
| 3486 | &extensions, ssl_get_grease_value(hs, ssl_grease_extension1), 0)) { |
| 3487 | return false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 3488 | } |
| 3489 | |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3490 | bool last_was_empty = false; |
David Benjamin | e9c5d72 | 2021-06-09 17:43:16 -0400 | [diff] [blame] | 3491 | for (size_t unpermuted = 0; unpermuted < kNumExtensions; unpermuted++) { |
| 3492 | size_t i = hs->extension_permutation.empty() |
| 3493 | ? unpermuted |
| 3494 | : hs->extension_permutation[unpermuted]; |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 3495 | const size_t len_before = CBB_len(&extensions); |
| 3496 | if (!kExtensions[i].add_clienthello(hs, &extensions, &extensions, type)) { |
| 3497 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 3498 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
| 3499 | return false; |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3500 | } |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 3501 | |
| 3502 | const size_t bytes_written = CBB_len(&extensions) - len_before; |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3503 | if (bytes_written != 0) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3504 | hs->extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3505 | } |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3506 | // If the difference in lengths is only four bytes then the extension had |
| 3507 | // an empty body. |
| 3508 | last_was_empty = (bytes_written == 4); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3509 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 3510 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 3511 | if (ssl->ctx->grease_enabled) { |
David Benjamin | 3675eb3 | 2021-05-18 14:01:07 -0400 | [diff] [blame] | 3512 | // Add a fake non-empty extension. See RFC 8701. |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3513 | if (!add_padding_extension( |
| 3514 | &extensions, ssl_get_grease_value(hs, ssl_grease_extension2), 1)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3515 | return false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 3516 | } |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3517 | last_was_empty = false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 3518 | } |
| 3519 | |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3520 | // In cleartext ClientHellos, we add the padding extension to work around |
| 3521 | // bugs. We also apply this padding to ClientHelloOuter, to keep the wire |
| 3522 | // images aligned. |
| 3523 | size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs, type); |
David Benjamin | cd89004 | 2021-06-02 18:35:31 -0400 | [diff] [blame] | 3524 | if (!SSL_is_dtls(ssl) && !ssl->quic_method && |
| 3525 | !ssl->s3->used_hello_retry_request) { |
David Benjamin | 18b6836 | 2021-06-18 23:13:46 -0400 | [diff] [blame] | 3526 | header_len += |
| 3527 | SSL3_HM_HEADER_LENGTH + 2 + CBB_len(&extensions) + psk_extension_len; |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3528 | size_t padding_len = 0; |
| 3529 | |
| 3530 | // The final extension must be non-empty. WebSphere Application |
| 3531 | // Server 7.0 is intolerant to the last extension being zero-length. See |
| 3532 | // https://crbug.com/363583. |
| 3533 | if (last_was_empty && psk_extension_len == 0) { |
| 3534 | padding_len = 1; |
| 3535 | // The addition of the padding extension may push us into the F5 bug. |
| 3536 | header_len += 4 + padding_len; |
| 3537 | } |
| 3538 | |
| 3539 | // Add padding to workaround bugs in F5 terminators. See RFC 7685. |
| 3540 | // |
| 3541 | // NB: because this code works out the length of all existing extensions |
| 3542 | // it MUST always appear last (save for any PSK extension). |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3543 | if (header_len > 0xff && header_len < 0x200) { |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3544 | // If our calculations already included a padding extension, remove that |
| 3545 | // factor because we're about to change its length. |
| 3546 | if (padding_len != 0) { |
| 3547 | header_len -= 4 + padding_len; |
| 3548 | } |
| 3549 | padding_len = 0x200 - header_len; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3550 | // Extensions take at least four bytes to encode. Always include at least |
| 3551 | // one byte of data if including the extension. WebSphere Application |
| 3552 | // Server 7.0 is intolerant to the last extension being zero-length. See |
| 3553 | // https://crbug.com/363583. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3554 | if (padding_len >= 4 + 1) { |
| 3555 | padding_len -= 4; |
| 3556 | } else { |
| 3557 | padding_len = 1; |
| 3558 | } |
Adam Langley | 7f4f41f | 2018-08-23 08:58:49 -0700 | [diff] [blame] | 3559 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3560 | |
David Benjamin | fb4d257 | 2021-06-01 15:58:30 -0400 | [diff] [blame] | 3561 | if (padding_len != 0 && |
| 3562 | !add_padding_extension(&extensions, TLSEXT_TYPE_padding, padding_len)) { |
| 3563 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3564 | } |
| 3565 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 3566 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3567 | // The PSK extension must be last, including after the padding. |
David Benjamin | 350fe3b | 2021-06-02 17:58:53 -0400 | [diff] [blame] | 3568 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | 83a4993 | 2021-05-20 15:57:09 -0400 | [diff] [blame] | 3569 | if (!ext_pre_shared_key_add_clienthello(hs, &extensions, out_needs_psk_binder, |
| 3570 | type)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3571 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3572 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 3573 | } |
David Benjamin | 350fe3b | 2021-06-02 17:58:53 -0400 | [diff] [blame] | 3574 | assert(psk_extension_len == CBB_len(&extensions) - len_before); |
| 3575 | (void)len_before; // |assert| is omitted in release builds. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 3576 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3577 | // Discard empty extensions blocks. |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 3578 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 3579 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3580 | } |
| 3581 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 3582 | return CBB_flush(out); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3583 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3584 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3585 | bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3586 | SSL *const ssl = hs->ssl; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3587 | CBB extensions; |
| 3588 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3589 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3592 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 3593 | if (!(hs->extensions.received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3594 | // Don't send extensions that were not received. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3595 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3596 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3597 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3598 | if (!kExtensions[i].add_serverhello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3599 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3600 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3601 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3602 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3603 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3604 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3605 | // Discard empty extensions blocks before TLS 1.3. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3606 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION && |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3607 | CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3608 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3611 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3612 | |
| 3613 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3614 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3615 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3616 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3617 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3618 | static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3619 | const SSL_CLIENT_HELLO *client_hello, |
| 3620 | int *out_alert) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3621 | hs->extensions.received = 0; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3622 | CBS extensions; |
| 3623 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 3624 | while (CBS_len(&extensions) != 0) { |
| 3625 | uint16_t type; |
| 3626 | CBS extension; |
| 3627 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3628 | // Decode the next extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3629 | if (!CBS_get_u16(&extensions, &type) || |
| 3630 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3631 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3632 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3633 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3634 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3635 | unsigned ext_index; |
| 3636 | const struct tls_extension *const ext = |
| 3637 | tls_extension_find(&ext_index, type); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3638 | if (ext == NULL) { |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3639 | continue; |
| 3640 | } |
| 3641 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3642 | hs->extensions.received |= (1u << ext_index); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3643 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3644 | if (!ext->parse_clienthello(hs, &alert, &extension)) { |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3645 | *out_alert = alert; |
| 3646 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3647 | ERR_add_error_dataf("extension %u", (unsigned)type); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3648 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3649 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3650 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 3651 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3652 | for (size_t i = 0; i < kNumExtensions; i++) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3653 | if (hs->extensions.received & (1u << i)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3654 | continue; |
| 3655 | } |
| 3656 | |
| 3657 | CBS *contents = NULL, fake_contents; |
| 3658 | static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 3659 | if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 3660 | ssl_client_cipher_list_contains_cipher(client_hello, |
| 3661 | SSL3_CK_SCSV & 0xffff)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3662 | // The renegotiation SCSV was received so pretend that we received a |
| 3663 | // renegotiation extension. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3664 | CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 3665 | sizeof(kFakeRenegotiateExtension)); |
| 3666 | contents = &fake_contents; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3667 | hs->extensions.received |= (1u << i); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3668 | } |
| 3669 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3670 | // Extension wasn't observed so call the callback with a NULL |
| 3671 | // parameter. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3672 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3673 | if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3674 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3675 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3676 | *out_alert = alert; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3677 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3681 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3682 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3683 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3684 | bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3685 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3686 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 3687 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3688 | if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3689 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3690 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3691 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3692 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3693 | if (!ssl_check_clienthello_tlsext(hs)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 3694 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3695 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3696 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3697 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3698 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3699 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3700 | |
David Benjamin | e2cb423 | 2021-06-23 18:14:22 -0400 | [diff] [blame] | 3701 | static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *cbs, |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3702 | int *out_alert) { |
David Benjamin | e2cb423 | 2021-06-23 18:14:22 -0400 | [diff] [blame] | 3703 | CBS extensions = *cbs; |
| 3704 | if (!tls1_check_duplicate_extensions(&extensions)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3705 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3706 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | uint32_t received = 0; |
| 3710 | while (CBS_len(&extensions) != 0) { |
| 3711 | uint16_t type; |
| 3712 | CBS extension; |
| 3713 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3714 | // Decode the next extension. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3715 | if (!CBS_get_u16(&extensions, &type) || |
| 3716 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3717 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3718 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3719 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3720 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3721 | unsigned ext_index; |
| 3722 | const struct tls_extension *const ext = |
| 3723 | tls_extension_find(&ext_index, type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3724 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3725 | if (ext == NULL) { |
David Benjamin | 0a3e07a | 2018-07-09 16:28:22 -0400 | [diff] [blame] | 3726 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 3727 | ERR_add_error_dataf("extension %u", (unsigned)type); |
| 3728 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3729 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3730 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3731 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3732 | static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8, |
| 3733 | "too many bits"); |
David Benjamin | 5db7c9b | 2017-01-24 16:17:03 -0500 | [diff] [blame] | 3734 | |
David Benjamin | c59b9aa | 2018-07-16 21:34:03 -0400 | [diff] [blame] | 3735 | if (!(hs->extensions.sent & (1u << ext_index))) { |
| 3736 | // If the extension was never sent then it is illegal. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3737 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 3738 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 3739 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3740 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3741 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3742 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3743 | received |= (1u << ext_index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 3744 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3745 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3746 | if (!ext->parse_serverhello(hs, &alert, &extension)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3747 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3748 | ERR_add_error_dataf("extension %u", (unsigned)type); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3749 | *out_alert = alert; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3750 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3751 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3752 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3753 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 3754 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3755 | if (!(received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3756 | // Extension wasn't observed so call the callback with a NULL |
| 3757 | // parameter. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3758 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3759 | if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3760 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3761 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3762 | *out_alert = alert; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3763 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3764 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3765 | } |
| 3766 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3767 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3768 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3769 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3770 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3771 | static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3772 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3773 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 3774 | int al = SSL_AD_UNRECOGNIZED_NAME; |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 3775 | if (ssl->ctx->servername_callback != 0) { |
| 3776 | ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg); |
| 3777 | } else if (ssl->session_ctx->servername_callback != 0) { |
| 3778 | ret = ssl->session_ctx->servername_callback( |
| 3779 | ssl, &al, ssl->session_ctx->servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3780 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3781 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3782 | switch (ret) { |
| 3783 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3784 | ssl_send_alert(ssl, SSL3_AL_FATAL, al); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3785 | return false; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3786 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3787 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3788 | hs->should_ack_sni = false; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3789 | return true; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3790 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3791 | default: |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3792 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3793 | } |
| 3794 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 3795 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3796 | static bool ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) { |
| 3797 | SSL *const ssl = hs->ssl; |
| 3798 | // ALPS and ALPN have a dependency between each other, so we defer checking |
| 3799 | // consistency to after the callbacks run. |
| 3800 | if (hs->new_session != nullptr && hs->new_session->has_application_settings) { |
| 3801 | // ALPN must be negotiated. |
| 3802 | if (ssl->s3->alpn_selected.empty()) { |
| 3803 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_ALPS_WITHOUT_ALPN); |
| 3804 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
| 3808 | // The negotiated protocol must be one of the ones we advertised for ALPS. |
| 3809 | Span<const uint8_t> settings; |
| 3810 | if (!ssl_get_local_application_settings(hs, &settings, |
| 3811 | ssl->s3->alpn_selected)) { |
| 3812 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 3813 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 3814 | return false; |
| 3815 | } |
| 3816 | |
| 3817 | if (!hs->new_session->local_application_settings.CopyFrom(settings)) { |
| 3818 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3819 | return false; |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | return true; |
| 3824 | } |
| 3825 | |
David Benjamin | e2cb423 | 2021-06-23 18:14:22 -0400 | [diff] [blame] | 3826 | bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, const CBS *cbs) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3827 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 3828 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3829 | if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3830 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3831 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3832 | } |
| 3833 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 3834 | if (!ssl_check_serverhello_tlsext(hs)) { |
| 3835 | return false; |
| 3836 | } |
| 3837 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3838 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3839 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3840 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3841 | static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx( |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3842 | Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx, |
| 3843 | Span<const uint8_t> ticket) { |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3844 | size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3845 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3846 | // Check the MAC at the end of the ticket. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3847 | uint8_t mac[EVP_MAX_MD_SIZE]; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3848 | size_t mac_len = HMAC_size(hmac_ctx); |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3849 | if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3850 | // The ticket must be large enough for key name, IV, data, and MAC. |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3851 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3852 | } |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3853 | // Split the ticket into the ticket and the MAC. |
David Benjamin | 006f20a | 2021-06-22 23:00:49 -0400 | [diff] [blame] | 3854 | auto ticket_mac = ticket.last(mac_len); |
| 3855 | ticket = ticket.first(ticket.size() - mac_len); |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3856 | HMAC_Update(hmac_ctx, ticket.data(), ticket.size()); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3857 | HMAC_Final(hmac_ctx, mac, NULL); |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3858 | assert(mac_len == ticket_mac.size()); |
| 3859 | bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3860 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 3861 | mac_ok = true; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3862 | #endif |
| 3863 | if (!mac_ok) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3864 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3865 | } |
| 3866 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3867 | // Decrypt the session data. |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3868 | auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len); |
| 3869 | Array<uint8_t> plaintext; |
| 3870 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3871 | if (!plaintext.CopyFrom(ciphertext)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3872 | return ssl_ticket_aead_error; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3873 | } |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3874 | #else |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3875 | if (ciphertext.size() >= INT_MAX) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3876 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3877 | } |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3878 | if (!plaintext.Init(ciphertext.size())) { |
| 3879 | return ssl_ticket_aead_error; |
| 3880 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3881 | int len1, len2; |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3882 | if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(), |
| 3883 | (int)ciphertext.size()) || |
| 3884 | !EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3885 | ERR_clear_error(); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3886 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3887 | } |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3888 | plaintext.Shrink(static_cast<size_t>(len1) + len2); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3889 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3890 | |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3891 | *out = std::move(plaintext); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3892 | return ssl_ticket_aead_success; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3893 | } |
| 3894 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3895 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb( |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3896 | SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket, |
| 3897 | Span<const uint8_t> ticket) { |
| 3898 | assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3899 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3900 | ScopedHMAC_CTX hmac_ctx; |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3901 | auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN); |
| 3902 | // The actual IV is shorter, but the length is determined by the callback's |
| 3903 | // chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure |
| 3904 | // the callback has enough. |
| 3905 | auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH); |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 3906 | int cb_ret = hs->ssl->session_ctx->ticket_key_cb( |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3907 | hs->ssl, const_cast<uint8_t *>(name.data()), |
| 3908 | const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(), |
| 3909 | 0 /* decrypt */); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3910 | if (cb_ret < 0) { |
| 3911 | return ssl_ticket_aead_error; |
| 3912 | } else if (cb_ret == 0) { |
| 3913 | return ssl_ticket_aead_ignore_ticket; |
| 3914 | } else if (cb_ret == 2) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3915 | *out_renew_ticket = true; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3916 | } else { |
| 3917 | assert(cb_ret == 1); |
| 3918 | } |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3919 | return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(), |
| 3920 | ticket); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys( |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3924 | SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) { |
| 3925 | assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 3926 | SSL_CTX *ctx = hs->ssl->session_ctx.get(); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3927 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3928 | // Rotate the ticket key if necessary. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3929 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 3930 | return ssl_ticket_aead_error; |
| 3931 | } |
| 3932 | |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3933 | const EVP_CIPHER *cipher = EVP_aes_128_cbc(); |
| 3934 | auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN); |
| 3935 | auto iv = |
| 3936 | ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher)); |
| 3937 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3938 | // Pick the matching ticket key and decrypt. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3939 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3940 | ScopedHMAC_CTX hmac_ctx; |
| 3941 | { |
| 3942 | MutexReadLock lock(&ctx->lock); |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 3943 | const TicketKey *key; |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3944 | if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) { |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 3945 | key = ctx->ticket_key_current.get(); |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3946 | } else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) { |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 3947 | key = ctx->ticket_key_prev.get(); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3948 | } else { |
| 3949 | return ssl_ticket_aead_ignore_ticket; |
| 3950 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3951 | if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key), |
| 3952 | tlsext_tick_md(), NULL) || |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3953 | !EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL, |
| 3954 | key->aes_key, iv.data())) { |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3955 | return ssl_ticket_aead_error; |
| 3956 | } |
| 3957 | } |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3958 | return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(), |
| 3959 | ticket); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3960 | } |
| 3961 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3962 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method( |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3963 | SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket, |
| 3964 | Span<const uint8_t> ticket) { |
| 3965 | Array<uint8_t> plaintext; |
| 3966 | if (!plaintext.Init(ticket.size())) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3967 | return ssl_ticket_aead_error; |
| 3968 | } |
| 3969 | |
| 3970 | size_t plaintext_len; |
| 3971 | const enum ssl_ticket_aead_result_t result = |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 3972 | hs->ssl->session_ctx->ticket_aead_method->open( |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3973 | hs->ssl, plaintext.data(), &plaintext_len, ticket.size(), |
| 3974 | ticket.data(), ticket.size()); |
| 3975 | if (result != ssl_ticket_aead_success) { |
| 3976 | return result; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3977 | } |
| 3978 | |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3979 | plaintext.Shrink(plaintext_len); |
| 3980 | *out = std::move(plaintext); |
| 3981 | return ssl_ticket_aead_success; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | enum ssl_ticket_aead_result_t ssl_process_ticket( |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3985 | SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3986 | bool *out_renew_ticket, Span<const uint8_t> ticket, |
| 3987 | Span<const uint8_t> session_id) { |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 3988 | SSL *const ssl = hs->ssl; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3989 | *out_renew_ticket = false; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3990 | out_session->reset(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3991 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 3992 | if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) || |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 3993 | session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3994 | return ssl_ticket_aead_ignore_ticket; |
| 3995 | } |
| 3996 | |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 3997 | // Tickets in TLS 1.3 are tied into pre-shared keys (PSKs), unlike in TLS 1.2 |
| 3998 | // where that concept doesn't exist. The |decrypted_psk| and |ignore_psk| |
| 3999 | // hints only apply to PSKs. We check the version to determine which this is. |
| 4000 | const bool is_psk = ssl_protocol_version(ssl) >= TLS1_3_VERSION; |
| 4001 | |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 4002 | Array<uint8_t> plaintext; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 4003 | enum ssl_ticket_aead_result_t result; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 4004 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 4005 | if (is_psk && hints && !hs->hints_requested && |
| 4006 | !hints->decrypted_psk.empty()) { |
| 4007 | result = plaintext.CopyFrom(hints->decrypted_psk) ? ssl_ticket_aead_success |
| 4008 | : ssl_ticket_aead_error; |
| 4009 | } else if (is_psk && hints && !hs->hints_requested && hints->ignore_psk) { |
| 4010 | result = ssl_ticket_aead_ignore_ticket; |
David Benjamin | adaa322 | 2022-08-02 17:10:53 -0700 | [diff] [blame] | 4011 | } else if (!is_psk && hints && !hs->hints_requested && |
| 4012 | !hints->decrypted_ticket.empty()) { |
| 4013 | if (plaintext.CopyFrom(hints->decrypted_ticket)) { |
| 4014 | result = ssl_ticket_aead_success; |
| 4015 | *out_renew_ticket = hints->renew_ticket; |
| 4016 | } else { |
| 4017 | result = ssl_ticket_aead_error; |
| 4018 | } |
| 4019 | } else if (!is_psk && hints && !hs->hints_requested && hints->ignore_ticket) { |
| 4020 | result = ssl_ticket_aead_ignore_ticket; |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 4021 | } else if (ssl->session_ctx->ticket_aead_method != NULL) { |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 4022 | result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket, |
| 4023 | ticket); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 4024 | } else { |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 4025 | // Ensure there is room for the key name and the largest IV |ticket_key_cb| |
| 4026 | // may try to consume. The real limit may be lower, but the maximum IV |
| 4027 | // length should be well under the minimum size for the session material and |
| 4028 | // HMAC. |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 4029 | if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 4030 | result = ssl_ticket_aead_ignore_ticket; |
| 4031 | } else if (ssl->session_ctx->ticket_key_cb != NULL) { |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 4032 | result = |
| 4033 | ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 4034 | } else { |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 4035 | result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 4036 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 4037 | } |
| 4038 | |
David Benjamin | adaa322 | 2022-08-02 17:10:53 -0700 | [diff] [blame] | 4039 | if (hints && hs->hints_requested) { |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 4040 | if (result == ssl_ticket_aead_ignore_ticket) { |
David Benjamin | adaa322 | 2022-08-02 17:10:53 -0700 | [diff] [blame] | 4041 | if (is_psk) { |
| 4042 | hints->ignore_psk = true; |
| 4043 | } else { |
| 4044 | hints->ignore_ticket = true; |
| 4045 | } |
| 4046 | } else if (result == ssl_ticket_aead_success) { |
| 4047 | if (is_psk) { |
| 4048 | if (!hints->decrypted_psk.CopyFrom(plaintext)) { |
| 4049 | return ssl_ticket_aead_error; |
| 4050 | } |
| 4051 | } else { |
| 4052 | if (!hints->decrypted_ticket.CopyFrom(plaintext)) { |
| 4053 | return ssl_ticket_aead_error; |
| 4054 | } |
| 4055 | hints->renew_ticket = *out_renew_ticket; |
| 4056 | } |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 4057 | } |
| 4058 | } |
| 4059 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 4060 | if (result != ssl_ticket_aead_success) { |
| 4061 | return result; |
| 4062 | } |
| 4063 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4064 | // Decode the session. |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 4065 | UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes( |
David Benjamin | b571e77 | 2021-03-25 19:42:16 -0400 | [diff] [blame] | 4066 | plaintext.data(), plaintext.size(), ssl->ctx.get())); |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 4067 | if (!session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4068 | ERR_clear_error(); // Don't leave an error on the queue. |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 4069 | return ssl_ticket_aead_ignore_ticket; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 4070 | } |
| 4071 | |
Adam Langley | 47cefed | 2021-05-26 13:36:40 -0700 | [diff] [blame] | 4072 | // Envoy's tests expect the session to have a session ID that matches the |
| 4073 | // placeholder used by the client. It's unclear whether this is a good idea, |
| 4074 | // but we maintain it for now. |
| 4075 | SHA256(ticket.data(), ticket.size(), session->session_id); |
| 4076 | // Other consumers may expect a non-empty session ID to indicate resumption. |
| 4077 | session->session_id_length = SHA256_DIGEST_LENGTH; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 4078 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 4079 | *out_session = std::move(session); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 4080 | return ssl_ticket_aead_success; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4081 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4082 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4083 | bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4084 | // Extension ignored for inappropriate versions |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 4085 | if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) { |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4086 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4087 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 4088 | |
David Benjamin | 602f466 | 2018-12-07 12:06:22 -0600 | [diff] [blame] | 4089 | // In all contexts, the signature algorithms list may not be empty. (It may be |
| 4090 | // omitted by clients in TLS 1.2, but then the entire extension is omitted.) |
| 4091 | return CBS_len(in_sigalgs) != 0 && |
| 4092 | parse_u16_array(in_sigalgs, &hs->peer_sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4093 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 4094 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4095 | bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) { |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 4096 | switch (EVP_PKEY_id(pkey)) { |
| 4097 | case EVP_PKEY_RSA: |
| 4098 | *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4099 | return true; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 4100 | case EVP_PKEY_EC: |
| 4101 | *out = SSL_SIGN_ECDSA_SHA1; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4102 | return true; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 4103 | default: |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4104 | return false; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 4105 | } |
| 4106 | } |
| 4107 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4108 | bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 4109 | SSL *const ssl = hs->ssl; |
David Benjamin | 0ce090a | 2018-07-02 20:24:40 -0400 | [diff] [blame] | 4110 | CERT *cert = hs->config->cert.get(); |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 4111 | DC *dc = cert->dc.get(); |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 4112 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4113 | // Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 4114 | // handshake. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 4115 | if (ssl_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 4116 | if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) { |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 4117 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4118 | return false; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 4119 | } |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4120 | return true; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 4121 | } |
| 4122 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 4123 | Span<const uint16_t> sigalgs = kSignSignatureAlgorithms; |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 4124 | if (ssl_signing_with_dc(hs)) { |
| 4125 | sigalgs = MakeConstSpan(&dc->expected_cert_verify_algorithm, 1); |
| 4126 | } else if (!cert->sigalgs.empty()) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 4127 | sigalgs = cert->sigalgs; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 4128 | } |
| 4129 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 4130 | Span<const uint16_t> peer_sigalgs = tls1_get_peer_verify_algorithms(hs); |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 4131 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 4132 | for (uint16_t sigalg : sigalgs) { |
David Benjamin | e8f57ca | 2022-11-29 18:40:11 -0500 | [diff] [blame] | 4133 | if (!ssl_private_key_supports_signature_algorithm(hs, sigalg)) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 4134 | continue; |
| 4135 | } |
| 4136 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 4137 | for (uint16_t peer_sigalg : peer_sigalgs) { |
| 4138 | if (sigalg == peer_sigalg) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 4139 | *out = sigalg; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4140 | return true; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 4141 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4142 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4143 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4144 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 4145 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 4146 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4147 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4148 | |
Christopher Patton | 6c1b376 | 2018-07-17 12:49:41 -0700 | [diff] [blame] | 4149 | Span<const uint16_t> tls1_get_peer_verify_algorithms(const SSL_HANDSHAKE *hs) { |
| 4150 | Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs; |
| 4151 | if (peer_sigalgs.empty() && ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) { |
| 4152 | // If the client didn't specify any signature_algorithms extension then |
| 4153 | // we can assume that it supports SHA1. See |
| 4154 | // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
| 4155 | static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 4156 | SSL_SIGN_ECDSA_SHA1}; |
| 4157 | peer_sigalgs = kDefaultPeerAlgorithms; |
| 4158 | } |
| 4159 | return peer_sigalgs; |
| 4160 | } |
| 4161 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4162 | bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4163 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4164 | // A Channel ID handshake message is structured to contain multiple |
| 4165 | // extensions, but the only one that can be present is Channel ID. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 4166 | uint16_t extension_type; |
| 4167 | CBS channel_id = msg.body, extension; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4168 | if (!CBS_get_u16(&channel_id, &extension_type) || |
| 4169 | !CBS_get_u16_length_prefixed(&channel_id, &extension) || |
| 4170 | CBS_len(&channel_id) != 0 || |
| 4171 | extension_type != TLSEXT_TYPE_channel_id || |
| 4172 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 4173 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 4174 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4175 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
David Benjamin | 70be012 | 2023-02-13 19:05:19 -0500 | [diff] [blame] | 4178 | const EC_GROUP *p256 = EC_group_p256(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 4179 | UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new()); |
| 4180 | UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 4181 | if (!sig || !x || !y) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4182 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | const uint8_t *p = CBS_data(&extension); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 4186 | if (BN_bin2bn(p + 0, 32, x.get()) == NULL || |
| 4187 | BN_bin2bn(p + 32, 32, y.get()) == NULL || |
| 4188 | BN_bin2bn(p + 64, 32, sig->r) == NULL || |
| 4189 | BN_bin2bn(p + 96, 32, sig->s) == NULL) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4190 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4191 | } |
| 4192 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 4193 | UniquePtr<EC_KEY> key(EC_KEY_new()); |
David Benjamin | 70be012 | 2023-02-13 19:05:19 -0500 | [diff] [blame] | 4194 | UniquePtr<EC_POINT> point(EC_POINT_new(p256)); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 4195 | if (!key || !point || |
David Benjamin | 70be012 | 2023-02-13 19:05:19 -0500 | [diff] [blame] | 4196 | !EC_POINT_set_affine_coordinates_GFp(p256, point.get(), x.get(), y.get(), |
| 4197 | nullptr) || |
| 4198 | !EC_KEY_set_group(key.get(), p256) || |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 4199 | !EC_KEY_set_public_key(key.get(), point.get())) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4200 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 4204 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4205 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4206 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4207 | } |
| 4208 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4209 | bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get()); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4210 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4211 | sig_ok = true; |
David Benjamin | d90b803 | 2017-12-18 16:47:51 -0500 | [diff] [blame] | 4212 | ERR_clear_error(); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4213 | #endif |
| 4214 | if (!sig_ok) { |
| 4215 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 4216 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4217 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4218 | } |
| 4219 | |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 4220 | OPENSSL_memcpy(ssl->s3->channel_id, p, 64); |
David Benjamin | 8acec00 | 2021-05-19 13:03:34 -0400 | [diff] [blame] | 4221 | ssl->s3->channel_id_valid = true; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4222 | return true; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4223 | } |
| 4224 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4225 | bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4226 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 4227 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4228 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4229 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
David Benjamin | 4685376 | 2018-07-03 14:01:26 -0400 | [diff] [blame] | 4232 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get()); |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4233 | if (ec_key == nullptr) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4234 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4235 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4238 | UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
| 4239 | if (!x || !y || |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4240 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 4241 | EC_KEY_get0_public_key(ec_key), |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4242 | x.get(), y.get(), nullptr)) { |
| 4243 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4246 | UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key)); |
| 4247 | if (!sig) { |
| 4248 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | CBB child; |
| 4252 | if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) || |
| 4253 | !CBB_add_u16_length_prefixed(cbb, &child) || |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4254 | !BN_bn2cbb_padded(&child, 32, x.get()) || |
| 4255 | !BN_bn2cbb_padded(&child, 32, y.get()) || |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4256 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 4257 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
| 4258 | !CBB_flush(cbb)) { |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4259 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4260 | } |
| 4261 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 4262 | return true; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4263 | } |
| 4264 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4265 | bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4266 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 4267 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 4268 | Array<uint8_t> msg; |
| 4269 | if (!tls13_get_cert_verify_signature_input(hs, &msg, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4270 | ssl_cert_verify_channel_id)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4271 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4272 | } |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 4273 | SHA256(msg.data(), msg.size(), out); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4274 | *out_len = SHA256_DIGEST_LENGTH; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4275 | return true; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 4278 | SHA256_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4279 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 4280 | SHA256_Init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4281 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 4282 | SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4283 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 4284 | if (ssl->session != NULL) { |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4285 | static const char kResumptionMagic[] = "Resumption"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 4286 | SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4287 | if (ssl->session->original_handshake_hash_len == 0) { |
| 4288 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4289 | return false; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4290 | } |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 4291 | SHA256_Update(&ctx, ssl->session->original_handshake_hash, |
| 4292 | ssl->session->original_handshake_hash_len); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4293 | } |
| 4294 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4295 | uint8_t hs_hash[EVP_MAX_MD_SIZE]; |
| 4296 | size_t hs_hash_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 4297 | if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4298 | return false; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 4299 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4300 | SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 4301 | SHA256_Final(out, &ctx); |
| 4302 | *out_len = SHA256_DIGEST_LENGTH; |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4303 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4304 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4305 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4306 | bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4307 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4308 | // This function should never be called for a resumed session because the |
| 4309 | // handshake hashes that we wish to record are for the original, full |
| 4310 | // handshake. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 4311 | if (ssl->session != NULL) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4312 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4313 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4314 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 4315 | static_assert( |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 4316 | sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE, |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 4317 | "original_handshake_hash is too small"); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 4318 | |
| 4319 | size_t digest_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 4320 | if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash, |
| 4321 | &digest_len)) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4322 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4323 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4324 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 4325 | static_assert(EVP_MAX_MD_SIZE <= 0xff, |
| 4326 | "EVP_MAX_MD_SIZE does not fit in uint8_t"); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 4327 | hs->new_session->original_handshake_hash_len = (uint8_t)digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4328 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4329 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 4330 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 4331 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4332 | bool ssl_is_sct_list_valid(const CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 4333 | // Shallow parse the SCT list for sanity. By the RFC |
| 4334 | // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any |
| 4335 | // of the SCTs may be empty. |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 4336 | CBS copy = *contents; |
| 4337 | CBS sct_list; |
| 4338 | if (!CBS_get_u16_length_prefixed(©, &sct_list) || |
| 4339 | CBS_len(©) != 0 || |
| 4340 | CBS_len(&sct_list) == 0) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4341 | return false; |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | while (CBS_len(&sct_list) > 0) { |
| 4345 | CBS sct; |
| 4346 | if (!CBS_get_u16_length_prefixed(&sct_list, &sct) || |
| 4347 | CBS_len(&sct) == 0) { |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4348 | return false; |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | |
David Benjamin | 861abcc | 2018-07-14 17:40:26 -0400 | [diff] [blame] | 4352 | return true; |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 4353 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 4354 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 4355 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 4356 | |
| 4357 | using namespace bssl; |
| 4358 | |
| 4359 | int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello, |
| 4360 | uint16_t extension_type, |
| 4361 | const uint8_t **out_data, |
| 4362 | size_t *out_len) { |
| 4363 | CBS cbs; |
| 4364 | if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) { |
| 4365 | return 0; |
| 4366 | } |
| 4367 | |
| 4368 | *out_data = CBS_data(&cbs); |
| 4369 | *out_len = CBS_len(&cbs); |
| 4370 | return 1; |
| 4371 | } |