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