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