Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-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 |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
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 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 116 | #include <utility> |
| 117 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 118 | #include <openssl/bytestring.h> |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 119 | #include <openssl/digest.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 120 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | #include <openssl/evp.h> |
| 122 | #include <openssl/hmac.h> |
| 123 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 124 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | #include <openssl/rand.h> |
| 126 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 127 | #include "internal.h" |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 128 | #include "../crypto/internal.h" |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 129 | |
| 130 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 131 | namespace bssl { |
| 132 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 133 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 135 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 136 | uint16_t u1 = *((const uint16_t *)p1); |
| 137 | uint16_t u2 = *((const uint16_t *)p2); |
| 138 | if (u1 < u2) { |
| 139 | return -1; |
| 140 | } else if (u1 > u2) { |
| 141 | return 1; |
| 142 | } else { |
| 143 | return 0; |
| 144 | } |
| 145 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 146 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 147 | // Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 148 | // more than one extension of the same type in a ClientHello or ServerHello. |
| 149 | // This function does an initial scan over the extensions block to filter those |
| 150 | // out. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 151 | static int tls1_check_duplicate_extensions(const CBS *cbs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 152 | // First pass: count the extensions. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 153 | size_t num_extensions = 0; |
| 154 | CBS extensions = *cbs; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 155 | while (CBS_len(&extensions) > 0) { |
| 156 | uint16_t type; |
| 157 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 158 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 159 | if (!CBS_get_u16(&extensions, &type) || |
| 160 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 161 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 162 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 163 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 164 | num_extensions++; |
| 165 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 166 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 167 | if (num_extensions == 0) { |
| 168 | return 1; |
| 169 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 170 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 171 | Array<uint16_t> extension_types; |
| 172 | if (!extension_types.Init(num_extensions)) { |
| 173 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 174 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 175 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 176 | // Second pass: gather the extension types. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 177 | extensions = *cbs; |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 178 | for (size_t i = 0; i < extension_types.size(); i++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 179 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 180 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 181 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 182 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 183 | // This should not happen. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 184 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 188 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 189 | // Sort the extensions and make sure there are no duplicates. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 190 | qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t), |
| 191 | compare_uint16_t); |
| 192 | for (size_t i = 1; i < num_extensions; i++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | if (extension_types[i - 1] == extension_types[i]) { |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 194 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 197 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 198 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 200 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 201 | int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, |
| 202 | const SSLMessage &msg) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 203 | OPENSSL_memset(out, 0, sizeof(*out)); |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 204 | out->ssl = ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 205 | out->client_hello = CBS_data(&msg.body); |
| 206 | out->client_hello_len = CBS_len(&msg.body); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 207 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 208 | CBS client_hello, random, session_id; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 209 | CBS_init(&client_hello, out->client_hello, out->client_hello_len); |
| 210 | if (!CBS_get_u16(&client_hello, &out->version) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 211 | !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) || |
| 212 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 213 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 214 | return 0; |
| 215 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 217 | out->random = CBS_data(&random); |
| 218 | out->random_len = CBS_len(&random); |
| 219 | out->session_id = CBS_data(&session_id); |
| 220 | out->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 222 | // Skip past DTLS cookie |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 223 | if (SSL_is_dtls(out->ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 224 | CBS cookie; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 225 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 226 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 231 | CBS cipher_suites, compression_methods; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 232 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 233 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 || |
| 234 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 235 | CBS_len(&compression_methods) < 1) { |
| 236 | return 0; |
| 237 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 238 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 239 | out->cipher_suites = CBS_data(&cipher_suites); |
| 240 | out->cipher_suites_len = CBS_len(&cipher_suites); |
| 241 | out->compression_methods = CBS_data(&compression_methods); |
| 242 | out->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 244 | // If the ClientHello ends here then it's valid, but doesn't have any |
| 245 | // extensions. (E.g. SSLv3.) |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 246 | if (CBS_len(&client_hello) == 0) { |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 247 | out->extensions = NULL; |
| 248 | out->extensions_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 249 | return 1; |
| 250 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 252 | // Extract extensions and check it is valid. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 253 | CBS extensions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 254 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 255 | !tls1_check_duplicate_extensions(&extensions) || |
| 256 | CBS_len(&client_hello) != 0) { |
| 257 | return 0; |
| 258 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 259 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 260 | out->extensions = CBS_data(&extensions); |
| 261 | out->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 263 | return 1; |
| 264 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 266 | int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
| 267 | CBS *out, uint16_t extension_type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | CBS extensions; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 269 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 270 | while (CBS_len(&extensions) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 271 | // Decode the next extension. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 272 | uint16_t type; |
| 273 | CBS extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | if (!CBS_get_u16(&extensions, &type) || |
| 275 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 276 | return 0; |
| 277 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 279 | if (type == extension_type) { |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 280 | *out = extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 281 | return 1; |
| 282 | } |
| 283 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 285 | return 0; |
| 286 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 288 | static const uint16_t kDefaultGroups[] = { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 289 | SSL_CURVE_X25519, |
| 290 | SSL_CURVE_SECP256R1, |
| 291 | SSL_CURVE_SECP384R1, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 292 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 294 | Span<const uint16_t> tls1_get_grouplist(const SSL *ssl) { |
| 295 | if (ssl->supported_group_list != nullptr) { |
| 296 | return MakeConstSpan(ssl->supported_group_list, |
| 297 | ssl->supported_group_list_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 298 | } |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 299 | return Span<const uint16_t>(kDefaultGroups); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 300 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 301 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 302 | int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) { |
| 303 | SSL *const ssl = hs->ssl; |
David Benjamin | f04976b | 2016-10-07 00:37:55 -0400 | [diff] [blame] | 304 | assert(ssl->server); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 305 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 306 | // Clients are not required to send a supported_groups extension. In this |
| 307 | // case, the server is free to pick any group it likes. See RFC 4492, |
| 308 | // section 4, paragraph 3. |
| 309 | // |
| 310 | // However, in the interests of compatibility, we will skip ECDH if the |
| 311 | // client didn't send an extension because we can't be sure that they'll |
| 312 | // support our favoured group. Thus we do not special-case an emtpy |
| 313 | // |peer_supported_group_list|. |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 314 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 315 | Span<const uint16_t> groups = tls1_get_grouplist(ssl); |
| 316 | Span<const uint16_t> pref, supp; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 317 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 318 | pref = groups; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 319 | supp = hs->peer_supported_group_list; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 320 | } else { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 321 | pref = hs->peer_supported_group_list; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 322 | supp = groups; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 323 | } |
| 324 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 325 | for (uint16_t pref_group : pref) { |
| 326 | for (uint16_t supp_group : supp) { |
| 327 | if (pref_group == supp_group) { |
| 328 | *out_group_id = pref_group; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 329 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 334 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 335 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 337 | int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 338 | const int *curves, size_t ncurves) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 339 | uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 340 | if (group_ids == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 344 | for (size_t i = 0; i < ncurves; i++) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 345 | if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) { |
| 346 | OPENSSL_free(group_ids); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 347 | return 0; |
| 348 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 351 | OPENSSL_free(*out_group_ids); |
| 352 | *out_group_ids = group_ids; |
| 353 | *out_group_ids_len = ncurves; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 354 | |
| 355 | return 1; |
| 356 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 358 | int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len, |
| 359 | const char *curves) { |
| 360 | uint16_t *group_ids = NULL; |
| 361 | size_t ncurves = 0; |
| 362 | |
| 363 | const char *col; |
| 364 | const char *ptr = curves; |
| 365 | |
| 366 | do { |
| 367 | col = strchr(ptr, ':'); |
| 368 | |
| 369 | uint16_t group_id; |
| 370 | if (!ssl_name_to_group_id(&group_id, ptr, |
| 371 | col ? (size_t)(col - ptr) : strlen(ptr))) { |
| 372 | goto err; |
| 373 | } |
| 374 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 375 | uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc( |
| 376 | group_ids, (ncurves + 1) * sizeof(uint16_t)); |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 377 | if (new_group_ids == NULL) { |
| 378 | goto err; |
| 379 | } |
| 380 | group_ids = new_group_ids; |
| 381 | |
| 382 | group_ids[ncurves] = group_id; |
| 383 | ncurves++; |
| 384 | |
| 385 | if (col) { |
| 386 | ptr = col + 1; |
| 387 | } |
| 388 | } while (col); |
| 389 | |
| 390 | OPENSSL_free(*out_group_ids); |
| 391 | *out_group_ids = group_ids; |
| 392 | *out_group_ids_len = ncurves; |
| 393 | |
| 394 | return 1; |
| 395 | |
| 396 | err: |
| 397 | OPENSSL_free(group_ids); |
| 398 | return 0; |
| 399 | } |
| 400 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 401 | int tls1_check_group_id(const SSL *ssl, uint16_t group_id) { |
| 402 | for (uint16_t supported : tls1_get_grouplist(ssl)) { |
| 403 | if (supported == group_id) { |
David Benjamin | 9d0b4bc | 2016-10-07 00:34:08 -0400 | [diff] [blame] | 404 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 405 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 406 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 407 | |
David Benjamin | 9d0b4bc | 2016-10-07 00:34:08 -0400 | [diff] [blame] | 408 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 409 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 410 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 411 | // kVerifySignatureAlgorithms is the default list of accepted signature |
| 412 | // algorithms for verifying. |
| 413 | // |
| 414 | // For now, RSA-PSS signature algorithms are not enabled on Android's system |
| 415 | // BoringSSL. Once the change in Chrome has stuck and the values are finalized, |
| 416 | // restore them. |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 417 | static const uint16_t kVerifySignatureAlgorithms[] = { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 418 | // List our preferred algorithms first. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 419 | SSL_SIGN_ED25519, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 420 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 421 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 422 | SSL_SIGN_RSA_PSS_SHA256, |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 423 | #endif |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 424 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 425 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 426 | // Larger hashes are acceptable. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 427 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 428 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
| 429 | SSL_SIGN_RSA_PSS_SHA384, |
| 430 | #endif |
| 431 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 432 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 433 | // TODO(davidben): Remove this. |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 434 | #if defined(BORINGSSL_ANDROID_SYSTEM) |
| 435 | SSL_SIGN_ECDSA_SECP521R1_SHA512, |
| 436 | #endif |
David Benjamin | 57e929f | 2016-08-30 00:30:38 -0400 | [diff] [blame] | 437 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 438 | SSL_SIGN_RSA_PSS_SHA512, |
David Benjamin | 57e929f | 2016-08-30 00:30:38 -0400 | [diff] [blame] | 439 | #endif |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 440 | SSL_SIGN_RSA_PKCS1_SHA512, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 441 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 442 | // For now, SHA-1 is still accepted but least preferable. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 443 | SSL_SIGN_RSA_PKCS1_SHA1, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 444 | |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 445 | }; |
| 446 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 447 | // kSignSignatureAlgorithms is the default list of supported signature |
| 448 | // algorithms for signing. |
| 449 | // |
| 450 | // For now, RSA-PSS signature algorithms are not enabled on Android's system |
| 451 | // BoringSSL. Once the change in Chrome has stuck and the values are finalized, |
| 452 | // restore them. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 453 | static const uint16_t kSignSignatureAlgorithms[] = { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 454 | // List our preferred algorithms first. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 455 | SSL_SIGN_ED25519, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 456 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
David Benjamin | 57e929f | 2016-08-30 00:30:38 -0400 | [diff] [blame] | 457 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 458 | SSL_SIGN_RSA_PSS_SHA256, |
David Benjamin | 57e929f | 2016-08-30 00:30:38 -0400 | [diff] [blame] | 459 | #endif |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 460 | SSL_SIGN_RSA_PKCS1_SHA256, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 461 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 462 | // If needed, sign larger hashes. |
| 463 | // |
| 464 | // TODO(davidben): Determine which of these may be pruned. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 465 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 466 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
| 467 | SSL_SIGN_RSA_PSS_SHA384, |
| 468 | #endif |
| 469 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 470 | |
| 471 | SSL_SIGN_ECDSA_SECP521R1_SHA512, |
| 472 | #if !defined(BORINGSSL_ANDROID_SYSTEM) |
| 473 | SSL_SIGN_RSA_PSS_SHA512, |
| 474 | #endif |
| 475 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 476 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 477 | // If the peer supports nothing else, sign with SHA-1. |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 478 | SSL_SIGN_ECDSA_SHA1, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 479 | SSL_SIGN_RSA_PKCS1_SHA1, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 480 | }; |
| 481 | |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 482 | int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) { |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 483 | const uint16_t *sigalgs = kVerifySignatureAlgorithms; |
| 484 | size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms); |
| 485 | if (ssl->ctx->num_verify_sigalgs != 0) { |
| 486 | sigalgs = ssl->ctx->verify_sigalgs; |
| 487 | num_sigalgs = ssl->ctx->num_verify_sigalgs; |
| 488 | } |
| 489 | |
| 490 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 491 | if (sigalgs == kVerifySignatureAlgorithms && |
| 492 | sigalgs[i] == SSL_SIGN_ED25519 && |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 493 | !ssl->ctx->ed25519_enabled) { |
| 494 | continue; |
| 495 | } |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 496 | if (!CBB_add_u16(out, sigalgs[i])) { |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 504 | int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) { |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 505 | const uint16_t *sigalgs = kVerifySignatureAlgorithms; |
| 506 | size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms); |
| 507 | if (ssl->ctx->num_verify_sigalgs != 0) { |
| 508 | sigalgs = ssl->ctx->verify_sigalgs; |
| 509 | num_sigalgs = ssl->ctx->num_verify_sigalgs; |
| 510 | } |
| 511 | |
| 512 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 513 | if (sigalgs == kVerifySignatureAlgorithms && |
| 514 | sigalgs[i] == SSL_SIGN_ED25519 && |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 515 | !ssl->ctx->ed25519_enabled) { |
| 516 | continue; |
| 517 | } |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 518 | if (sigalg == sigalgs[i]) { |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 519 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 523 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 524 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 525 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 526 | } |
| 527 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 528 | // tls_extension represents a TLS extension that is handled internally. The |
| 529 | // |init| function is called for each handshake, before any other functions of |
| 530 | // the extension. Then the add and parse callbacks are called as needed. |
| 531 | // |
| 532 | // The parse callbacks receive a |CBS| that contains the contents of the |
| 533 | // extension (i.e. not including the type and length bytes). If an extension is |
| 534 | // not received then the parse callbacks will be called with a NULL CBS so that |
| 535 | // they can do any processing needed to handle the absence of an extension. |
| 536 | // |
| 537 | // The add callbacks receive a |CBB| to which the extension can be appended but |
| 538 | // the function is responsible for appending the type and length bytes too. |
| 539 | // |
| 540 | // All callbacks return one for success and zero for error. If a parse function |
| 541 | // returns zero then a fatal alert with value |*out_alert| will be sent. If |
| 542 | // |*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] | 543 | struct tls_extension { |
| 544 | uint16_t value; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 545 | void (*init)(SSL_HANDSHAKE *hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 546 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 547 | int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out); |
| 548 | int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 549 | CBS *contents); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 550 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 551 | int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 552 | CBS *contents); |
| 553 | int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 556 | static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 557 | CBS *contents) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 558 | if (contents != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 559 | // Servers MUST NOT send this extension. |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 560 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 561 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | return 1; |
| 566 | } |
| 567 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 568 | static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 569 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 570 | // This extension from the client is handled elsewhere. |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 571 | return 1; |
| 572 | } |
| 573 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 574 | static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 575 | return 1; |
| 576 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 577 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 578 | // Server name indication (SNI). |
| 579 | // |
| 580 | // https://tools.ietf.org/html/rfc6066#section-3. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 581 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 582 | static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 583 | SSL *const ssl = hs->ssl; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 584 | if (ssl->tlsext_hostname == NULL) { |
| 585 | return 1; |
| 586 | } |
| 587 | |
| 588 | CBB contents, server_name_list, name; |
| 589 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 590 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 591 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 592 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 593 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 594 | !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 595 | strlen(ssl->tlsext_hostname)) || |
| 596 | !CBB_flush(out)) { |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | return 1; |
| 601 | } |
| 602 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 603 | static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 604 | CBS *contents) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 605 | // The server may acknowledge SNI with an empty extension. We check the syntax |
| 606 | // but otherwise ignore this signal. |
| 607 | return contents == NULL || CBS_len(contents) == 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 608 | } |
| 609 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 610 | static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 611 | CBS *contents) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 612 | SSL *const ssl = hs->ssl; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 613 | if (contents == NULL) { |
| 614 | return 1; |
| 615 | } |
| 616 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 617 | CBS server_name_list, host_name; |
| 618 | uint8_t name_type; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 619 | if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 620 | !CBS_get_u8(&server_name_list, &name_type) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 621 | // Although the server_name extension was intended to be extensible to |
| 622 | // new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 623 | // different name types will cause an error. Further, RFC 4366 originally |
| 624 | // defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 625 | // adding new name types is no longer feasible. |
| 626 | // |
| 627 | // Act as if the extensibility does not exist to simplify parsing. |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 628 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 629 | CBS_len(&server_name_list) != 0 || |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 630 | CBS_len(contents) != 0) { |
| 631 | return 0; |
| 632 | } |
| 633 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 634 | if (name_type != TLSEXT_NAMETYPE_host_name || |
| 635 | CBS_len(&host_name) == 0 || |
| 636 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 637 | CBS_contains_zero_byte(&host_name)) { |
| 638 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 639 | return 0; |
| 640 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 641 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 642 | // Copy the hostname as a string. |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 643 | if (!CBS_strdup(&host_name, &ssl->s3->hostname)) { |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 644 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 645 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 646 | } |
| 647 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 648 | hs->should_ack_sni = true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 649 | return 1; |
| 650 | } |
| 651 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 652 | static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 653 | if (hs->ssl->s3->session_reused || |
| 654 | !hs->should_ack_sni) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 655 | return 1; |
| 656 | } |
| 657 | |
| 658 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 659 | !CBB_add_u16(out, 0 /* length */)) { |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 667 | // Renegotiation indication. |
| 668 | // |
| 669 | // https://tools.ietf.org/html/rfc5746 |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 670 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 671 | static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 672 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 673 | // Renegotiation indication is not necessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 674 | if (hs->min_version >= TLS1_3_VERSION) { |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 675 | return 1; |
| 676 | } |
| 677 | |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 678 | assert(ssl->s3->initial_handshake_complete == |
| 679 | (ssl->s3->previous_client_finished_len != 0)); |
| 680 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 681 | CBB contents, prev_finished; |
| 682 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 683 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 684 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 685 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 686 | ssl->s3->previous_client_finished_len) || |
| 687 | !CBB_flush(out)) { |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | return 1; |
| 692 | } |
| 693 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 694 | static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 695 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 696 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 697 | if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 246eeee | 2017-03-26 12:49:17 -0500 | [diff] [blame] | 698 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 702 | // Servers may not switch between omitting the extension and supporting it. |
| 703 | // See RFC 5746, sections 3.5 and 4.2. |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 704 | if (ssl->s3->initial_handshake_complete && |
| 705 | (contents != NULL) != ssl->s3->send_connection_binding) { |
| 706 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 707 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 708 | return 0; |
| 709 | } |
| 710 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 711 | if (contents == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 712 | // Strictly speaking, if we want to avoid an attack we should *always* see |
| 713 | // RI even on initial ServerHello because the client doesn't see any |
| 714 | // renegotiation during an attack. However this would mean we could not |
| 715 | // connect to any server which doesn't support RI. |
| 716 | // |
| 717 | // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 718 | // practical terms every client sets it so it's just assumed here. |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 719 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 723 | ssl->s3->previous_server_finished_len; |
| 724 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 725 | // Check for logic errors |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 726 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 727 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 728 | assert(ssl->s3->initial_handshake_complete == |
| 729 | (ssl->s3->previous_client_finished_len != 0)); |
| 730 | assert(ssl->s3->initial_handshake_complete == |
| 731 | (ssl->s3->previous_server_finished_len != 0)); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 732 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 733 | // Parse out the extension contents. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 734 | CBS renegotiated_connection; |
| 735 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 736 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 737 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 738 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 739 | return 0; |
| 740 | } |
| 741 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 742 | // Check that the extension matches. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 743 | if (CBS_len(&renegotiated_connection) != expected_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 744 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 745 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | const uint8_t *d = CBS_data(&renegotiated_connection); |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 750 | int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 751 | ssl->s3->previous_client_finished_len) == 0; |
| 752 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 753 | ok = 1; |
| 754 | #endif |
| 755 | if (!ok) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 756 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 757 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 758 | return 0; |
| 759 | } |
| 760 | d += ssl->s3->previous_client_finished_len; |
| 761 | |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 762 | ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 763 | ssl->s3->previous_server_finished_len) == 0; |
| 764 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 765 | ok = 1; |
| 766 | #endif |
| 767 | if (!ok) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 768 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 10e1060 | 2017-07-25 13:33:21 -0700 | [diff] [blame] | 769 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 770 | return 0; |
| 771 | } |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 772 | ssl->s3->send_connection_binding = true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 773 | |
| 774 | return 1; |
| 775 | } |
| 776 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 777 | static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 778 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 779 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 780 | // Renegotiation isn't supported as a server so this function should never be |
| 781 | // called after the initial handshake. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 782 | assert(!ssl->s3->initial_handshake_complete); |
| 783 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 784 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 785 | return 1; |
| 786 | } |
| 787 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 788 | if (contents == NULL) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 789 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | CBS renegotiated_connection; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 793 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 794 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 795 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 799 | // Check that the extension matches. We do not support renegotiation as a |
| 800 | // server, so this must be empty. |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 801 | if (CBS_len(&renegotiated_connection) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 802 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 803 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 804 | return 0; |
| 805 | } |
| 806 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 807 | ssl->s3->send_connection_binding = true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 808 | |
| 809 | return 1; |
| 810 | } |
| 811 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 812 | static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 813 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 814 | // Renegotiation isn't supported as a server so this function should never be |
| 815 | // called after the initial handshake. |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 816 | assert(!ssl->s3->initial_handshake_complete); |
| 817 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 818 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 819 | return 1; |
| 820 | } |
| 821 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 822 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 823 | !CBB_add_u16(out, 1 /* length */) || |
| 824 | !CBB_add_u8(out, 0 /* empty renegotiation info */)) { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | return 1; |
| 829 | } |
| 830 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 831 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 832 | // Extended Master Secret. |
| 833 | // |
| 834 | // https://tools.ietf.org/html/rfc7627 |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 835 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 836 | static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 837 | // Extended master secret is not necessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 838 | if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 839 | return 1; |
| 840 | } |
| 841 | |
| 842 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 843 | !CBB_add_u16(out, 0 /* length */)) { |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | return 1; |
| 848 | } |
| 849 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 850 | static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 851 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 852 | SSL *const ssl = hs->ssl; |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 853 | |
| 854 | if (contents != NULL) { |
| 855 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 856 | ssl->version == SSL3_VERSION || |
| 857 | CBS_len(contents) != 0) { |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 861 | hs->extended_master_secret = true; |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 862 | } |
| 863 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 864 | // Whether EMS is negotiated may not change on renegotiation. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 865 | if (ssl->s3->established_session != NULL && |
| 866 | hs->extended_master_secret != |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 867 | !!ssl->s3->established_session->extended_master_secret) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 868 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH); |
| 869 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 873 | return 1; |
| 874 | } |
| 875 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 876 | static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 877 | CBS *contents) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 878 | uint16_t version = ssl3_protocol_version(hs->ssl); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 879 | if (version >= TLS1_3_VERSION || |
| 880 | version == SSL3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | if (contents == NULL) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 885 | return 1; |
| 886 | } |
| 887 | |
| 888 | if (CBS_len(contents) != 0) { |
| 889 | return 0; |
| 890 | } |
| 891 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 892 | hs->extended_master_secret = true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 893 | return 1; |
| 894 | } |
| 895 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 896 | static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 897 | if (!hs->extended_master_secret) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 902 | !CBB_add_u16(out, 0 /* length */)) { |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | return 1; |
| 907 | } |
| 908 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 909 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 910 | // Session tickets. |
| 911 | // |
| 912 | // https://tools.ietf.org/html/rfc5077 |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 913 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 914 | static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 915 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 916 | // TLS 1.3 uses a different ticket extension. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 917 | if (hs->min_version >= TLS1_3_VERSION || |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 918 | SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | const uint8_t *ticket_data = NULL; |
| 923 | int ticket_len = 0; |
| 924 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 925 | // Renegotiation does not participate in session resumption. However, still |
| 926 | // advertise the extension to avoid potentially breaking servers which carry |
| 927 | // over the state from the previous handshake, such as OpenSSL servers |
| 928 | // without upstream's 3c3f0259238594d77264a78944d409f2127642c4. |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 929 | if (!ssl->s3->initial_handshake_complete && |
| 930 | ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 931 | ssl->session->tlsext_tick != NULL && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 932 | // Don't send TLS 1.3 session tickets in the ticket extension. |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 933 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 934 | ticket_data = ssl->session->tlsext_tick; |
| 935 | ticket_len = ssl->session->tlsext_ticklen; |
| 936 | } |
| 937 | |
| 938 | CBB ticket; |
| 939 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 940 | !CBB_add_u16_length_prefixed(out, &ticket) || |
| 941 | !CBB_add_bytes(&ticket, ticket_data, ticket_len) || |
| 942 | !CBB_flush(out)) { |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | return 1; |
| 947 | } |
| 948 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 949 | static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 950 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 951 | SSL *const ssl = hs->ssl; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 952 | if (contents == NULL) { |
| 953 | return 1; |
| 954 | } |
| 955 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 956 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 957 | return 0; |
| 958 | } |
| 959 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 960 | // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 961 | // this function should never be called, even if the server tries to send the |
| 962 | // extension. |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 963 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 964 | |
| 965 | if (CBS_len(contents) != 0) { |
| 966 | return 0; |
| 967 | } |
| 968 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 969 | hs->ticket_expected = true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 970 | return 1; |
| 971 | } |
| 972 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 973 | static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 974 | if (!hs->ticket_expected) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 975 | return 1; |
| 976 | } |
| 977 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 978 | // 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] | 979 | assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0); |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 980 | |
| 981 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 982 | !CBB_add_u16(out, 0 /* length */)) { |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 990 | // Signature Algorithms. |
| 991 | // |
| 992 | // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 993 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 994 | static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 995 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 996 | if (hs->max_version < TLS1_2_VERSION) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 997 | return 1; |
| 998 | } |
| 999 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1000 | CBB contents, sigalgs_cbb; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1001 | if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) || |
| 1002 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 1003 | !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) || |
| 1004 | !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) || |
| 1005 | !CBB_flush(out)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | return 1; |
| 1010 | } |
| 1011 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1012 | static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1013 | CBS *contents) { |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 1014 | hs->peer_sigalgs.Reset(); |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1015 | if (contents == NULL) { |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
| 1019 | CBS supported_signature_algorithms; |
| 1020 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1021 | CBS_len(contents) != 0 || |
| 1022 | CBS_len(&supported_signature_algorithms) == 0 || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1023 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | return 1; |
| 1028 | } |
| 1029 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1030 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1031 | // OCSP Stapling. |
| 1032 | // |
| 1033 | // https://tools.ietf.org/html/rfc6066#section-8 |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1034 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1035 | static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1036 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1037 | if (!ssl->ocsp_stapling_enabled) { |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | CBB contents; |
| 1042 | if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1043 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1044 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1045 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1046 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1047 | !CBB_flush(out)) { |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | return 1; |
| 1052 | } |
| 1053 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1054 | static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1055 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1056 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1057 | if (contents == NULL) { |
| 1058 | return 1; |
| 1059 | } |
| 1060 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1061 | // TLS 1.3 OCSP responses are included in the Certificate extensions. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1062 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1063 | return 0; |
| 1064 | } |
| 1065 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1066 | // OCSP stapling is forbidden on non-certificate ciphers. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1067 | if (CBS_len(contents) != 0 || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1068 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1072 | // Note this does not check for resumption in TLS 1.2. Sending |
| 1073 | // status_request here does not make sense, but OpenSSL does so and the |
| 1074 | // specification does not say anything. Tolerate it but ignore it. |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1075 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1076 | hs->certificate_status_expected = true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1077 | return 1; |
| 1078 | } |
| 1079 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1080 | static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1081 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1082 | if (contents == NULL) { |
| 1083 | return 1; |
| 1084 | } |
| 1085 | |
| 1086 | uint8_t status_type; |
| 1087 | if (!CBS_get_u8(contents, &status_type)) { |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1091 | // We cannot decide whether OCSP stapling will occur yet because the correct |
| 1092 | // SSL_CTX might not have been selected. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1093 | hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1094 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1095 | return 1; |
| 1096 | } |
| 1097 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1098 | static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1099 | SSL *const ssl = hs->ssl; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1100 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1101 | !hs->ocsp_stapling_requested || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1102 | ssl->cert->ocsp_response == NULL || |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1103 | ssl->s3->session_reused || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1104 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1105 | return 1; |
| 1106 | } |
| 1107 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1108 | hs->certificate_status_expected = true; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1109 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1110 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1111 | CBB_add_u16(out, 0 /* length */); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1115 | // Next protocol negotiation. |
| 1116 | // |
| 1117 | // 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] | 1118 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1119 | static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1120 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1121 | if (ssl->s3->initial_handshake_complete || |
| 1122 | ssl->ctx->next_proto_select_cb == NULL || |
David Benjamin | 9d125dc | 2016-12-07 21:32:37 -0500 | [diff] [blame] | 1123 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1124 | return 1; |
| 1125 | } |
| 1126 | |
| 1127 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1128 | !CBB_add_u16(out, 0 /* length */)) { |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | return 1; |
| 1133 | } |
| 1134 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1135 | static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1136 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1137 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1138 | if (contents == NULL) { |
| 1139 | return 1; |
| 1140 | } |
| 1141 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1142 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1146 | // If any of these are false then we should never have sent the NPN |
| 1147 | // extension in the ClientHello and thus this function should never have been |
| 1148 | // called. |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1149 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1150 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1151 | assert(ssl->ctx->next_proto_select_cb != NULL); |
| 1152 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1153 | if (ssl->s3->alpn_selected != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1154 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1155 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1156 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1160 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1161 | const size_t orig_len = CBS_len(contents); |
| 1162 | |
| 1163 | while (CBS_len(contents) != 0) { |
| 1164 | CBS proto; |
| 1165 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1166 | CBS_len(&proto) == 0) { |
| 1167 | return 0; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | uint8_t *selected; |
| 1172 | uint8_t selected_len; |
| 1173 | if (ssl->ctx->next_proto_select_cb( |
| 1174 | ssl, &selected, &selected_len, orig_contents, orig_len, |
| 1175 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1176 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1180 | OPENSSL_free(ssl->s3->next_proto_negotiated); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 1181 | ssl->s3->next_proto_negotiated = |
| 1182 | (uint8_t *)BUF_memdup(selected, selected_len); |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1183 | if (ssl->s3->next_proto_negotiated == NULL) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1184 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1188 | ssl->s3->next_proto_negotiated_len = selected_len; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1189 | hs->next_proto_neg_seen = true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1190 | |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1194 | static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1195 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1196 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1197 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1198 | return 1; |
| 1199 | } |
| 1200 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1201 | if (contents != NULL && CBS_len(contents) != 0) { |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | if (contents == NULL || |
| 1206 | ssl->s3->initial_handshake_complete || |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1207 | ssl->ctx->next_protos_advertised_cb == NULL || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1208 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1209 | return 1; |
| 1210 | } |
| 1211 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1212 | hs->next_proto_neg_seen = true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1213 | return 1; |
| 1214 | } |
| 1215 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1216 | static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1217 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1218 | // |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1219 | // parsed. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1220 | if (!hs->next_proto_neg_seen) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1221 | return 1; |
| 1222 | } |
| 1223 | |
| 1224 | const uint8_t *npa; |
| 1225 | unsigned npa_len; |
| 1226 | |
| 1227 | if (ssl->ctx->next_protos_advertised_cb( |
| 1228 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1229 | SSL_TLSEXT_ERR_OK) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1230 | hs->next_proto_neg_seen = false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1231 | return 1; |
| 1232 | } |
| 1233 | |
| 1234 | CBB contents; |
| 1235 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1236 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1237 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1238 | !CBB_flush(out)) { |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | return 1; |
| 1243 | } |
| 1244 | |
| 1245 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1246 | // Signed certificate timestamps. |
| 1247 | // |
| 1248 | // https://tools.ietf.org/html/rfc6962#section-3.3.1 |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1249 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1250 | static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1251 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1252 | if (!ssl->signed_cert_timestamps_enabled) { |
| 1253 | return 1; |
| 1254 | } |
| 1255 | |
| 1256 | if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1257 | !CBB_add_u16(out, 0 /* length */)) { |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | return 1; |
| 1262 | } |
| 1263 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1264 | static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1265 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1266 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1267 | if (contents == NULL) { |
| 1268 | return 1; |
| 1269 | } |
| 1270 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1271 | // TLS 1.3 SCTs are included in the Certificate extensions. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1272 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1273 | *out_alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1274 | return 0; |
| 1275 | } |
| 1276 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1277 | // If this is false then we should never have sent the SCT extension in the |
| 1278 | // ClientHello and thus this function should never have been called. |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1279 | assert(ssl->signed_cert_timestamps_enabled); |
| 1280 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1281 | if (!ssl_is_sct_list_valid(contents)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1282 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1286 | // Session resumption uses the original session information. The extension |
| 1287 | // should not be sent on resumption, but RFC 6962 did not make it a |
| 1288 | // requirement, so tolerate this. |
| 1289 | // |
| 1290 | // TODO(davidben): Enforce this anyway. |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1291 | if (!ssl->s3->session_reused) { |
| 1292 | CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list); |
| 1293 | hs->new_session->signed_cert_timestamp_list = |
| 1294 | CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool); |
| 1295 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
| 1296 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1297 | return 0; |
| 1298 | } |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | return 1; |
| 1302 | } |
| 1303 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1304 | static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1305 | CBS *contents) { |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1306 | if (contents == NULL) { |
| 1307 | return 1; |
| 1308 | } |
| 1309 | |
| 1310 | if (CBS_len(contents) != 0) { |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1314 | hs->scts_requested = true; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1315 | return 1; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1318 | static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1319 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1320 | // The extension shouldn't be sent when resuming sessions. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1321 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1322 | ssl->s3->session_reused || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1323 | ssl->cert->signed_cert_timestamp_list == NULL) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1324 | return 1; |
| 1325 | } |
| 1326 | |
| 1327 | CBB contents; |
| 1328 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1329 | CBB_add_u16_length_prefixed(out, &contents) && |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1330 | CBB_add_bytes( |
| 1331 | &contents, |
| 1332 | CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list), |
| 1333 | CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) && |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1334 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1338 | // Application-level Protocol Negotiation. |
| 1339 | // |
| 1340 | // https://tools.ietf.org/html/rfc7301 |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1341 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1342 | static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1343 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1344 | if (ssl->alpn_client_proto_list == NULL || |
| 1345 | ssl->s3->initial_handshake_complete) { |
| 1346 | return 1; |
| 1347 | } |
| 1348 | |
| 1349 | CBB contents, proto_list; |
| 1350 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1351 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1352 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1353 | !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1354 | ssl->alpn_client_proto_list_len) || |
| 1355 | !CBB_flush(out)) { |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | return 1; |
| 1360 | } |
| 1361 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1362 | static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1363 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1364 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1365 | if (contents == NULL) { |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
| 1369 | assert(!ssl->s3->initial_handshake_complete); |
| 1370 | assert(ssl->alpn_client_proto_list != NULL); |
| 1371 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1372 | if (hs->next_proto_neg_seen) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1373 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1374 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1375 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1379 | // The extension data consists of a ProtocolNameList which must have |
| 1380 | // exactly one ProtocolName. Each of these is length-prefixed. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1381 | CBS protocol_name_list, protocol_name; |
| 1382 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1383 | CBS_len(contents) != 0 || |
| 1384 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1385 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1386 | CBS_len(&protocol_name) == 0 || |
| 1387 | CBS_len(&protocol_name_list) != 0) { |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 1391 | if (!ssl->ctx->allow_unknown_alpn_protos) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1392 | // Check that the protocol name is one of the ones we advertised. |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 1393 | int protocol_ok = 0; |
| 1394 | CBS client_protocol_name_list, client_protocol_name; |
| 1395 | CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list, |
| 1396 | ssl->alpn_client_proto_list_len); |
| 1397 | while (CBS_len(&client_protocol_name_list) > 0) { |
| 1398 | if (!CBS_get_u8_length_prefixed(&client_protocol_name_list, |
| 1399 | &client_protocol_name)) { |
| 1400 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) && |
| 1405 | OPENSSL_memcmp(CBS_data(&client_protocol_name), |
| 1406 | CBS_data(&protocol_name), |
| 1407 | CBS_len(&protocol_name)) == 0) { |
| 1408 | protocol_ok = 1; |
| 1409 | break; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | if (!protocol_ok) { |
| 1414 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1415 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3e51757 | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1416 | return 0; |
| 1417 | } |
David Benjamin | 3e51757 | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1418 | } |
| 1419 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1420 | if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected, |
| 1421 | &ssl->s3->alpn_selected_len)) { |
| 1422 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | return 1; |
| 1427 | } |
| 1428 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1429 | int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 1430 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1431 | SSL *const ssl = hs->ssl; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1432 | CBS contents; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1433 | if (ssl->ctx->alpn_select_cb == NULL || |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 1434 | !ssl_client_hello_get_extension( |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1435 | client_hello, &contents, |
| 1436 | TLSEXT_TYPE_application_layer_protocol_negotiation)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1437 | // Ignore ALPN if not configured or no extension was supplied. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1438 | return 1; |
| 1439 | } |
| 1440 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1441 | // ALPN takes precedence over NPN. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1442 | hs->next_proto_neg_seen = false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1443 | |
| 1444 | CBS protocol_name_list; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1445 | if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) || |
| 1446 | CBS_len(&contents) != 0 || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1447 | CBS_len(&protocol_name_list) < 2) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1448 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1449 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1450 | return 0; |
| 1451 | } |
| 1452 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1453 | // Validate the protocol list. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1454 | CBS protocol_name_list_copy = protocol_name_list; |
| 1455 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1456 | CBS protocol_name; |
| 1457 | |
| 1458 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1459 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1460 | CBS_len(&protocol_name) == 0) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1461 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1462 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | const uint8_t *selected; |
| 1468 | uint8_t selected_len; |
| 1469 | if (ssl->ctx->alpn_select_cb( |
| 1470 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1471 | CBS_len(&protocol_name_list), |
| 1472 | ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
| 1473 | OPENSSL_free(ssl->s3->alpn_selected); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 1474 | ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len); |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1475 | if (ssl->s3->alpn_selected == NULL) { |
| 1476 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1477 | return 0; |
| 1478 | } |
| 1479 | ssl->s3->alpn_selected_len = selected_len; |
| 1480 | } |
| 1481 | |
| 1482 | return 1; |
| 1483 | } |
| 1484 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1485 | static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1486 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1487 | if (ssl->s3->alpn_selected == NULL) { |
| 1488 | return 1; |
| 1489 | } |
| 1490 | |
| 1491 | CBB contents, proto_list, proto; |
| 1492 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1493 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1494 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1495 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1496 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected, |
| 1497 | ssl->s3->alpn_selected_len) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1498 | !CBB_flush(out)) { |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | return 1; |
| 1503 | } |
| 1504 | |
| 1505 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1506 | // Channel ID. |
| 1507 | // |
| 1508 | // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1509 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1510 | static void ext_channel_id_init(SSL_HANDSHAKE *hs) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1511 | hs->ssl->s3->tlsext_channel_id_valid = false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1514 | static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1515 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1516 | if (!ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1517 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1518 | return 1; |
| 1519 | } |
| 1520 | |
| 1521 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1522 | !CBB_add_u16(out, 0 /* length */)) { |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | return 1; |
| 1527 | } |
| 1528 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1529 | static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1530 | uint8_t *out_alert, CBS *contents) { |
| 1531 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1532 | if (contents == NULL) { |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1536 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1537 | assert(ssl->tlsext_channel_id_enabled); |
| 1538 | |
| 1539 | if (CBS_len(contents) != 0) { |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1543 | ssl->s3->tlsext_channel_id_valid = true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1544 | return 1; |
| 1545 | } |
| 1546 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1547 | static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1548 | uint8_t *out_alert, CBS *contents) { |
| 1549 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1550 | if (contents == NULL || |
| 1551 | !ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1552 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1553 | return 1; |
| 1554 | } |
| 1555 | |
| 1556 | if (CBS_len(contents) != 0) { |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1560 | ssl->s3->tlsext_channel_id_valid = true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1561 | return 1; |
| 1562 | } |
| 1563 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1564 | static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1565 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1566 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1571 | !CBB_add_u16(out, 0 /* length */)) { |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | return 1; |
| 1576 | } |
| 1577 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1578 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1579 | // Secure Real-time Transport Protocol (SRTP) extension. |
| 1580 | // |
| 1581 | // https://tools.ietf.org/html/rfc5764 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1582 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1583 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1584 | static void ext_srtp_init(SSL_HANDSHAKE *hs) { |
| 1585 | hs->ssl->srtp_profile = NULL; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1588 | static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1589 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1590 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1591 | if (profiles == NULL || |
| 1592 | sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | CBB contents, profile_ids; |
| 1597 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1598 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1599 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1603 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
| 1604 | if (!CBB_add_u16(&profile_ids, profile->id)) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1605 | return 0; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1610 | !CBB_flush(out)) { |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | return 1; |
| 1615 | } |
| 1616 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1617 | static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1618 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1619 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1620 | if (contents == NULL) { |
| 1621 | return 1; |
| 1622 | } |
| 1623 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1624 | // The extension consists of a u16-prefixed profile ID list containing a |
| 1625 | // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1626 | // |
| 1627 | // See https://tools.ietf.org/html/rfc5764#section-4.1.1 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1628 | CBS profile_ids, srtp_mki; |
| 1629 | uint16_t profile_id; |
| 1630 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1631 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1632 | CBS_len(&profile_ids) != 0 || |
| 1633 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1634 | CBS_len(contents) != 0) { |
| 1635 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | if (CBS_len(&srtp_mki) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1640 | // Must be no MKI, since we never offer one. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1641 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1642 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1647 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1648 | // Check to see if the server gave us something we support (and presumably |
| 1649 | // offered). |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1650 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1651 | if (profile->id == profile_id) { |
| 1652 | ssl->srtp_profile = profile; |
| 1653 | return 1; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1658 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1662 | static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1663 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1664 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1665 | if (contents == NULL) { |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
| 1669 | CBS profile_ids, srtp_mki; |
| 1670 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1671 | CBS_len(&profile_ids) < 2 || |
| 1672 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1673 | CBS_len(contents) != 0) { |
| 1674 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1675 | return 0; |
| 1676 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1677 | // Discard the MKI value for now. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1678 | |
| 1679 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1680 | SSL_get_srtp_profiles(ssl); |
| 1681 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1682 | // Pick the server's most preferred profile. |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1683 | for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1684 | CBS profile_ids_tmp; |
| 1685 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1686 | |
| 1687 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1688 | uint16_t profile_id; |
| 1689 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | if (server_profile->id == profile_id) { |
| 1694 | ssl->srtp_profile = server_profile; |
| 1695 | return 1; |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | return 1; |
| 1701 | } |
| 1702 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1703 | static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1704 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1705 | if (ssl->srtp_profile == NULL) { |
| 1706 | return 1; |
| 1707 | } |
| 1708 | |
| 1709 | CBB contents, profile_ids; |
| 1710 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1711 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1712 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1713 | !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1714 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1715 | !CBB_flush(out)) { |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
| 1719 | return 1; |
| 1720 | } |
| 1721 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1722 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1723 | // EC point formats. |
| 1724 | // |
| 1725 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1726 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1727 | static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1728 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1729 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1730 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1731 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1732 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1733 | !CBB_flush(out)) { |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | return 1; |
| 1738 | } |
| 1739 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1740 | static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1741 | // The point format extension is unneccessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1742 | if (hs->min_version >= TLS1_3_VERSION) { |
David Benjamin | 70aba26 | 2016-11-01 12:08:15 -0400 | [diff] [blame] | 1743 | return 1; |
| 1744 | } |
| 1745 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1746 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1749 | static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1750 | CBS *contents) { |
| 1751 | if (contents == NULL) { |
| 1752 | return 1; |
| 1753 | } |
| 1754 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1755 | if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1756 | return 0; |
| 1757 | } |
| 1758 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1759 | CBS ec_point_format_list; |
| 1760 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1761 | CBS_len(contents) != 0) { |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1765 | // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1766 | // point format. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1767 | if (OPENSSL_memchr(CBS_data(&ec_point_format_list), |
| 1768 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1769 | CBS_len(&ec_point_format_list)) == NULL) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1770 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | return 1; |
| 1775 | } |
| 1776 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1777 | static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1778 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1779 | if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1780 | return 1; |
| 1781 | } |
| 1782 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1783 | return ext_ec_point_parse_serverhello(hs, out_alert, contents); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1786 | static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1787 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1788 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1789 | return 1; |
| 1790 | } |
| 1791 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1792 | const uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1793 | const uint32_t alg_a = hs->new_cipher->algorithm_auth; |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1794 | const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1795 | |
| 1796 | if (!using_ecc) { |
| 1797 | return 1; |
| 1798 | } |
| 1799 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1800 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1803 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1804 | // Pre Shared Key |
| 1805 | // |
| 1806 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1807 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1808 | static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) { |
| 1809 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1810 | if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL || |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1811 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1812 | return 0; |
| 1813 | } |
| 1814 | |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1815 | size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session)); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1816 | return 15 + ssl->session->tlsext_ticklen + binder_len; |
| 1817 | } |
| 1818 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1819 | static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1820 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1821 | if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL || |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1822 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1823 | return 1; |
| 1824 | } |
| 1825 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1826 | struct OPENSSL_timeval now; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1827 | ssl_get_current_time(ssl, &now); |
| 1828 | uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time); |
| 1829 | uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add; |
| 1830 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1831 | // Fill in a placeholder zero binder of the appropriate length. It will be |
| 1832 | // computed and filled in later after length prefixes are computed. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1833 | uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0}; |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1834 | size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session)); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1835 | |
| 1836 | CBB contents, identity, ticket, binders, binder; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1837 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1838 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1839 | !CBB_add_u16_length_prefixed(&contents, &identity) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1840 | !CBB_add_u16_length_prefixed(&identity, &ticket) || |
| 1841 | !CBB_add_bytes(&ticket, ssl->session->tlsext_tick, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1842 | ssl->session->tlsext_ticklen) || |
| 1843 | !CBB_add_u32(&identity, obfuscated_ticket_age) || |
| 1844 | !CBB_add_u16_length_prefixed(&contents, &binders) || |
| 1845 | !CBB_add_u8_length_prefixed(&binders, &binder) || |
| 1846 | !CBB_add_bytes(&binder, zero_binder, binder_len)) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1847 | return 0; |
| 1848 | } |
| 1849 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1850 | hs->needs_psk_binder = true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1851 | return CBB_flush(out); |
| 1852 | } |
| 1853 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 1854 | int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1855 | uint8_t *out_alert, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1856 | CBS *contents) { |
| 1857 | uint16_t psk_id; |
| 1858 | if (!CBS_get_u16(contents, &psk_id) || |
| 1859 | CBS_len(contents) != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1860 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1861 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1865 | // 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] | 1866 | if (psk_id != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1867 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1868 | *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1875 | int ssl_ext_pre_shared_key_parse_clienthello( |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1876 | SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1877 | uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1878 | // 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] | 1879 | CBS identities, binders; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1880 | if (!CBS_get_u16_length_prefixed(contents, &identities) || |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1881 | !CBS_get_u16_length_prefixed(&identities, out_ticket) || |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1882 | !CBS_get_u32(&identities, out_obfuscated_ticket_age) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1883 | !CBS_get_u16_length_prefixed(contents, &binders) || |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1884 | CBS_len(&binders) == 0 || |
Steven Valdez | af3b8a9 | 2016-11-01 12:49:22 -0400 | [diff] [blame] | 1885 | CBS_len(contents) != 0) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1886 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1887 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1891 | *out_binders = binders; |
| 1892 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1893 | // Check the syntax of the remaining identities, but do not process them. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1894 | size_t num_identities = 1; |
| 1895 | while (CBS_len(&identities) != 0) { |
| 1896 | CBS unused_ticket; |
| 1897 | uint32_t unused_obfuscated_ticket_age; |
| 1898 | if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) || |
| 1899 | !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) { |
| 1900 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1901 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | num_identities++; |
| 1906 | } |
| 1907 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1908 | // Check the syntax of the binders. The value will be checked later if |
| 1909 | // resuming. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1910 | size_t num_binders = 0; |
| 1911 | while (CBS_len(&binders) != 0) { |
| 1912 | CBS binder; |
| 1913 | if (!CBS_get_u8_length_prefixed(&binders, &binder)) { |
| 1914 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1915 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | num_binders++; |
| 1920 | } |
| 1921 | |
| 1922 | if (num_identities != num_binders) { |
| 1923 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH); |
| 1924 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1925 | return 0; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
David Benjamin | e7f60a2 | 2016-11-16 18:54:25 +0900 | [diff] [blame] | 1928 | return 1; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1929 | } |
| 1930 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 1931 | int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1932 | if (!hs->ssl->s3->session_reused) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1933 | return 1; |
| 1934 | } |
| 1935 | |
| 1936 | CBB contents; |
| 1937 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1938 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1939 | // We only consider the first identity for resumption |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1940 | !CBB_add_u16(&contents, 0) || |
| 1941 | !CBB_flush(out)) { |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | return 1; |
| 1946 | } |
| 1947 | |
| 1948 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1949 | // Pre-Shared Key Exchange Modes |
| 1950 | // |
| 1951 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 1952 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1953 | static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs, |
| 1954 | CBB *out) { |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1955 | if (hs->max_version < TLS1_3_VERSION) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1956 | return 1; |
| 1957 | } |
| 1958 | |
| 1959 | CBB contents, ke_modes; |
| 1960 | if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) || |
| 1961 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1962 | !CBB_add_u8_length_prefixed(&contents, &ke_modes) || |
| 1963 | !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) { |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | return CBB_flush(out); |
| 1968 | } |
| 1969 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1970 | static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 1971 | uint8_t *out_alert, |
| 1972 | CBS *contents) { |
| 1973 | if (contents == NULL) { |
| 1974 | return 1; |
| 1975 | } |
| 1976 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1977 | CBS ke_modes; |
| 1978 | if (!CBS_get_u8_length_prefixed(contents, &ke_modes) || |
| 1979 | CBS_len(&ke_modes) == 0 || |
| 1980 | CBS_len(contents) != 0) { |
| 1981 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1985 | // We only support tickets with PSK_DHE_KE. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1986 | hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, |
| 1987 | CBS_len(&ke_modes)) != NULL; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1988 | |
| 1989 | return 1; |
| 1990 | } |
| 1991 | |
| 1992 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1993 | // Early Data Indication |
| 1994 | // |
| 1995 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 1996 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1997 | static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1998 | SSL *const ssl = hs->ssl; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1999 | if (ssl->session == NULL || |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 2000 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION || |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2001 | ssl->session->ticket_max_early_data == 0 || |
| 2002 | hs->received_hello_retry_request || |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 2003 | !ssl->cert->enable_early_data) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2004 | return 1; |
| 2005 | } |
| 2006 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2007 | hs->early_data_offered = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2008 | |
| 2009 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2010 | !CBB_add_u16(out, 0) || |
| 2011 | !CBB_flush(out)) { |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2015 | return 1; |
| 2016 | } |
| 2017 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2018 | static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2019 | uint8_t *out_alert, CBS *contents) { |
| 2020 | SSL *const ssl = hs->ssl; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2021 | if (contents == NULL) { |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
| 2025 | if (CBS_len(contents) != 0) { |
| 2026 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2027 | return 0; |
| 2028 | } |
| 2029 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2030 | if (!ssl->s3->session_reused) { |
| 2031 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2032 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2033 | return 0; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2034 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2035 | |
| 2036 | ssl->early_data_accepted = 1; |
| 2037 | return 1; |
| 2038 | } |
| 2039 | |
| 2040 | static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2041 | uint8_t *out_alert, CBS *contents) { |
| 2042 | SSL *const ssl = hs->ssl; |
| 2043 | if (contents == NULL || |
| 2044 | ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2045 | return 1; |
| 2046 | } |
| 2047 | |
| 2048 | if (CBS_len(contents) != 0) { |
| 2049 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2050 | return 0; |
| 2051 | } |
| 2052 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2053 | hs->early_data_offered = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2054 | return 1; |
| 2055 | } |
| 2056 | |
| 2057 | static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2058 | if (!hs->ssl->early_data_accepted) { |
| 2059 | return 1; |
| 2060 | } |
| 2061 | |
| 2062 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2063 | !CBB_add_u16(out, 0) || |
| 2064 | !CBB_flush(out)) { |
| 2065 | return 0; |
| 2066 | } |
| 2067 | |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2068 | return 1; |
| 2069 | } |
| 2070 | |
| 2071 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2072 | // Key Share |
| 2073 | // |
| 2074 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2075 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2076 | static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2077 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2078 | if (hs->max_version < TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2079 | return 1; |
| 2080 | } |
| 2081 | |
| 2082 | CBB contents, kse_bytes; |
| 2083 | if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2084 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2085 | !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) { |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2089 | uint16_t group_id = hs->retry_group; |
| 2090 | if (hs->received_hello_retry_request) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2091 | // We received a HelloRetryRequest without a new curve, so there is no new |
| 2092 | // share to append. Leave |hs->key_share| as-is. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2093 | if (group_id == 0 && |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2094 | !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(), |
| 2095 | hs->key_share_bytes.size())) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2096 | return 0; |
| 2097 | } |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2098 | hs->key_share_bytes.Reset(); |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2099 | if (group_id == 0) { |
| 2100 | return CBB_flush(out); |
| 2101 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2102 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2103 | // Add a fake group. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2104 | if (ssl->ctx->grease_enabled && |
| 2105 | (!CBB_add_u16(&kse_bytes, |
| 2106 | ssl_get_grease_value(ssl, ssl_grease_group)) || |
| 2107 | !CBB_add_u16(&kse_bytes, 1 /* length */) || |
| 2108 | !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) { |
| 2109 | return 0; |
| 2110 | } |
| 2111 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2112 | // Predict the most preferred group. |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2113 | Span<const uint16_t> groups = tls1_get_grouplist(ssl); |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2114 | if (groups.empty()) { |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2115 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED); |
| 2116 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2117 | } |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2118 | |
| 2119 | group_id = groups[0]; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2120 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2121 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2122 | hs->key_share = SSLKeyShare::Create(group_id); |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2123 | CBB key_exchange; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2124 | if (!hs->key_share || |
| 2125 | !CBB_add_u16(&kse_bytes, group_id) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2126 | !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) || |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2127 | !hs->key_share->Offer(&key_exchange) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2128 | !CBB_flush(&kse_bytes)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2129 | return 0; |
| 2130 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2131 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2132 | // Save the contents of the extension to repeat it in the second ClientHello. |
| 2133 | if (!hs->received_hello_retry_request && |
| 2134 | !hs->key_share_bytes.CopyFrom( |
| 2135 | MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) { |
| 2136 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2137 | } |
| 2138 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2139 | return CBB_flush(out); |
| 2140 | } |
| 2141 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2142 | int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2143 | Array<uint8_t> *out_secret, |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2144 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2145 | CBS peer_key; |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2146 | uint16_t group_id; |
| 2147 | if (!CBS_get_u16(contents, &group_id) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2148 | !CBS_get_u16_length_prefixed(contents, &peer_key) || |
| 2149 | CBS_len(contents) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2150 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2154 | if (hs->key_share->GroupID() != group_id) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2155 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2156 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 2157 | return 0; |
| 2158 | } |
| 2159 | |
David Benjamin | 6b3ab72 | 2017-09-21 02:37:58 -0400 | [diff] [blame] | 2160 | if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2161 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2162 | return 0; |
| 2163 | } |
| 2164 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2165 | hs->new_session->group_id = group_id; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2166 | hs->key_share.reset(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2167 | return 1; |
| 2168 | } |
| 2169 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2170 | int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2171 | Array<uint8_t> *out_secret, |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2172 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2173 | uint16_t group_id; |
| 2174 | CBS key_shares; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 2175 | if (!tls1_get_shared_group(hs, &group_id)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2176 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 2177 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 2178 | return 0; |
| 2179 | } |
| 2180 | |
| 2181 | if (!CBS_get_u16_length_prefixed(contents, &key_shares) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2182 | CBS_len(contents) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2183 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2184 | return 0; |
| 2185 | } |
| 2186 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2187 | // Find the corresponding key share. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2188 | bool found = false; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2189 | CBS peer_key; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2190 | while (CBS_len(&key_shares) > 0) { |
| 2191 | uint16_t id; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2192 | CBS peer_key_tmp; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2193 | if (!CBS_get_u16(&key_shares, &id) || |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2194 | !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) { |
| 2195 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2196 | return 0; |
| 2197 | } |
| 2198 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2199 | if (id == group_id) { |
| 2200 | if (found) { |
| 2201 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE); |
| 2202 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2203 | return 0; |
| 2204 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2205 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2206 | found = true; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2207 | peer_key = peer_key_tmp; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2208 | // Continue parsing the structure to keep peers honest. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2209 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2210 | } |
| 2211 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2212 | if (!found) { |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2213 | *out_found = false; |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2214 | out_secret->Reset(); |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2215 | return 1; |
| 2216 | } |
| 2217 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2218 | // Compute the DH secret. |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2219 | Array<uint8_t> secret; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 2220 | ScopedCBB public_key; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2221 | UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id); |
David Benjamin | 6b3ab72 | 2017-09-21 02:37:58 -0400 | [diff] [blame] | 2222 | if (!key_share || |
| 2223 | !CBB_init(public_key.get(), 32) || |
| 2224 | !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) || |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2225 | !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2226 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2227 | return 0; |
| 2228 | } |
| 2229 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2230 | *out_secret = std::move(secret); |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2231 | *out_found = true; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2232 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2233 | } |
| 2234 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 2235 | int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2236 | uint16_t group_id; |
| 2237 | CBB kse_bytes, public_key; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 2238 | if (!tls1_get_shared_group(hs, &group_id) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2239 | !CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2240 | !CBB_add_u16_length_prefixed(out, &kse_bytes) || |
| 2241 | !CBB_add_u16(&kse_bytes, group_id) || |
| 2242 | !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) || |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2243 | !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(), |
| 2244 | hs->ecdh_public_key.size()) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2245 | !CBB_flush(out)) { |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2249 | hs->ecdh_public_key.Reset(); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 2250 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2251 | hs->new_session->group_id = group_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2252 | return 1; |
| 2253 | } |
| 2254 | |
| 2255 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2256 | // Supported Versions |
| 2257 | // |
| 2258 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2259 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2260 | static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2261 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2262 | if (hs->max_version <= TLS1_2_VERSION) { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2263 | return 1; |
| 2264 | } |
| 2265 | |
| 2266 | CBB contents, versions; |
| 2267 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 2268 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2269 | !CBB_add_u8_length_prefixed(&contents, &versions)) { |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2273 | // Add a fake version. See draft-davidben-tls-grease-01. |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2274 | if (ssl->ctx->grease_enabled && |
| 2275 | !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) { |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 2279 | if (!ssl_add_supported_versions(hs, &versions) || |
| 2280 | !CBB_flush(out)) { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | return 1; |
| 2285 | } |
| 2286 | |
| 2287 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2288 | // Cookie |
| 2289 | // |
| 2290 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2291 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2292 | static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2293 | if (hs->cookie.empty()) { |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2294 | return 1; |
| 2295 | } |
| 2296 | |
| 2297 | CBB contents, cookie; |
| 2298 | if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) || |
| 2299 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2300 | !CBB_add_u16_length_prefixed(&contents, &cookie) || |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2301 | !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) || |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2302 | !CBB_flush(out)) { |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2306 | // The cookie is no longer needed in memory. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2307 | hs->cookie.Reset(); |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2308 | return 1; |
| 2309 | } |
| 2310 | |
| 2311 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2312 | // Negotiated Groups |
| 2313 | // |
| 2314 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
| 2315 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2316 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2317 | static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2318 | SSL *const ssl = hs->ssl; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2319 | CBB contents, groups_bytes; |
| 2320 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2321 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2322 | !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2323 | return 0; |
| 2324 | } |
| 2325 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2326 | // Add a fake group. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2327 | if (ssl->ctx->grease_enabled && |
| 2328 | !CBB_add_u16(&groups_bytes, |
| 2329 | ssl_get_grease_value(ssl, ssl_grease_group))) { |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2333 | for (uint16_t group : tls1_get_grouplist(ssl)) { |
| 2334 | if (!CBB_add_u16(&groups_bytes, group)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2335 | return 0; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | return CBB_flush(out); |
| 2340 | } |
| 2341 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2342 | static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2343 | uint8_t *out_alert, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2344 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2345 | // This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2346 | // BigIP servers send it nonetheless, so do not enforce this. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2347 | return 1; |
| 2348 | } |
| 2349 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2350 | static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) { |
| 2351 | CBS copy = *cbs; |
| 2352 | if ((CBS_len(©) & 1) != 0) { |
| 2353 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2354 | return false; |
| 2355 | } |
| 2356 | |
| 2357 | Array<uint16_t> ret; |
| 2358 | if (!ret.Init(CBS_len(©) / 2)) { |
| 2359 | return false; |
| 2360 | } |
| 2361 | for (size_t i = 0; i < ret.size(); i++) { |
| 2362 | if (!CBS_get_u16(©, &ret[i])) { |
| 2363 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2364 | return false; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | assert(CBS_len(©) == 0); |
| 2369 | *out = std::move(ret); |
| 2370 | return 1; |
| 2371 | } |
| 2372 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2373 | static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2374 | uint8_t *out_alert, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2375 | CBS *contents) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2376 | if (contents == NULL) { |
| 2377 | return 1; |
| 2378 | } |
| 2379 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2380 | CBS supported_group_list; |
| 2381 | if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2382 | CBS_len(&supported_group_list) == 0 || |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2383 | CBS_len(contents) != 0 || |
| 2384 | !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2385 | return 0; |
| 2386 | } |
| 2387 | |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2388 | return 1; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2391 | static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2392 | // Servers don't echo this extension. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2393 | return 1; |
| 2394 | } |
| 2395 | |
| 2396 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2397 | // kExtensions contains all the supported extensions. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2398 | static const struct tls_extension kExtensions[] = { |
| 2399 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 2400 | TLSEXT_TYPE_renegotiate, |
| 2401 | NULL, |
| 2402 | ext_ri_add_clienthello, |
| 2403 | ext_ri_parse_serverhello, |
| 2404 | ext_ri_parse_clienthello, |
| 2405 | ext_ri_add_serverhello, |
| 2406 | }, |
| 2407 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2408 | TLSEXT_TYPE_server_name, |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2409 | NULL, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2410 | ext_sni_add_clienthello, |
| 2411 | ext_sni_parse_serverhello, |
| 2412 | ext_sni_parse_clienthello, |
| 2413 | ext_sni_add_serverhello, |
| 2414 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2415 | { |
| 2416 | TLSEXT_TYPE_extended_master_secret, |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 2417 | NULL, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2418 | ext_ems_add_clienthello, |
| 2419 | ext_ems_parse_serverhello, |
| 2420 | ext_ems_parse_clienthello, |
| 2421 | ext_ems_add_serverhello, |
| 2422 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2423 | { |
| 2424 | TLSEXT_TYPE_session_ticket, |
| 2425 | NULL, |
| 2426 | ext_ticket_add_clienthello, |
| 2427 | ext_ticket_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2428 | // Ticket extension client parsing is handled in ssl_session.c |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2429 | ignore_parse_clienthello, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2430 | ext_ticket_add_serverhello, |
| 2431 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2432 | { |
| 2433 | TLSEXT_TYPE_signature_algorithms, |
| 2434 | NULL, |
| 2435 | ext_sigalgs_add_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2436 | forbid_parse_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2437 | ext_sigalgs_parse_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2438 | dont_add_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2439 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2440 | { |
| 2441 | TLSEXT_TYPE_status_request, |
David Benjamin | c253864 | 2017-01-13 16:32:05 -0500 | [diff] [blame] | 2442 | NULL, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2443 | ext_ocsp_add_clienthello, |
| 2444 | ext_ocsp_parse_serverhello, |
| 2445 | ext_ocsp_parse_clienthello, |
| 2446 | ext_ocsp_add_serverhello, |
| 2447 | }, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2448 | { |
| 2449 | TLSEXT_TYPE_next_proto_neg, |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 2450 | NULL, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2451 | ext_npn_add_clienthello, |
| 2452 | ext_npn_parse_serverhello, |
| 2453 | ext_npn_parse_clienthello, |
| 2454 | ext_npn_add_serverhello, |
| 2455 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 2456 | { |
| 2457 | TLSEXT_TYPE_certificate_timestamp, |
| 2458 | NULL, |
| 2459 | ext_sct_add_clienthello, |
| 2460 | ext_sct_parse_serverhello, |
| 2461 | ext_sct_parse_clienthello, |
| 2462 | ext_sct_add_serverhello, |
| 2463 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2464 | { |
| 2465 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
David Benjamin | 35598ae | 2016-11-16 15:38:27 +0900 | [diff] [blame] | 2466 | NULL, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2467 | ext_alpn_add_clienthello, |
| 2468 | ext_alpn_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2469 | // ALPN is negotiated late in |ssl_negotiate_alpn|. |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 2470 | ignore_parse_clienthello, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2471 | ext_alpn_add_serverhello, |
| 2472 | }, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2473 | { |
| 2474 | TLSEXT_TYPE_channel_id, |
| 2475 | ext_channel_id_init, |
| 2476 | ext_channel_id_add_clienthello, |
| 2477 | ext_channel_id_parse_serverhello, |
| 2478 | ext_channel_id_parse_clienthello, |
| 2479 | ext_channel_id_add_serverhello, |
| 2480 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 2481 | { |
| 2482 | TLSEXT_TYPE_srtp, |
| 2483 | ext_srtp_init, |
| 2484 | ext_srtp_add_clienthello, |
| 2485 | ext_srtp_parse_serverhello, |
| 2486 | ext_srtp_parse_clienthello, |
| 2487 | ext_srtp_add_serverhello, |
| 2488 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2489 | { |
| 2490 | TLSEXT_TYPE_ec_point_formats, |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2491 | NULL, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2492 | ext_ec_point_add_clienthello, |
| 2493 | ext_ec_point_parse_serverhello, |
| 2494 | ext_ec_point_parse_clienthello, |
| 2495 | ext_ec_point_add_serverhello, |
| 2496 | }, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2497 | { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2498 | TLSEXT_TYPE_key_share, |
| 2499 | NULL, |
| 2500 | ext_key_share_add_clienthello, |
| 2501 | forbid_parse_serverhello, |
| 2502 | ignore_parse_clienthello, |
| 2503 | dont_add_serverhello, |
| 2504 | }, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2505 | { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2506 | TLSEXT_TYPE_psk_key_exchange_modes, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2507 | NULL, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2508 | ext_psk_key_exchange_modes_add_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2509 | forbid_parse_serverhello, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2510 | ext_psk_key_exchange_modes_parse_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2511 | dont_add_serverhello, |
| 2512 | }, |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2513 | { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2514 | TLSEXT_TYPE_early_data, |
| 2515 | NULL, |
| 2516 | ext_early_data_add_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2517 | ext_early_data_parse_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2518 | ext_early_data_parse_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2519 | ext_early_data_add_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2520 | }, |
| 2521 | { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2522 | TLSEXT_TYPE_supported_versions, |
| 2523 | NULL, |
| 2524 | ext_supported_versions_add_clienthello, |
| 2525 | forbid_parse_serverhello, |
| 2526 | ignore_parse_clienthello, |
| 2527 | dont_add_serverhello, |
| 2528 | }, |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2529 | { |
| 2530 | TLSEXT_TYPE_cookie, |
| 2531 | NULL, |
| 2532 | ext_cookie_add_clienthello, |
| 2533 | forbid_parse_serverhello, |
| 2534 | ignore_parse_clienthello, |
| 2535 | dont_add_serverhello, |
| 2536 | }, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2537 | // The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2538 | // intolerant to the last extension being zero-length. See |
| 2539 | // https://crbug.com/363583. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2540 | { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2541 | TLSEXT_TYPE_supported_groups, |
David Benjamin | 43612b6 | 2016-10-07 00:41:50 -0400 | [diff] [blame] | 2542 | NULL, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2543 | ext_supported_groups_add_clienthello, |
| 2544 | ext_supported_groups_parse_serverhello, |
| 2545 | ext_supported_groups_parse_clienthello, |
| 2546 | ext_supported_groups_add_serverhello, |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2547 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2548 | }; |
| 2549 | |
| 2550 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2551 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 2552 | static_assert(kNumExtensions <= |
| 2553 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8, |
| 2554 | "too many extensions for sent bitset"); |
| 2555 | static_assert(kNumExtensions <= |
| 2556 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8, |
| 2557 | "too many extensions for received bitset"); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2558 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2559 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2560 | uint16_t value) { |
| 2561 | unsigned i; |
| 2562 | for (i = 0; i < kNumExtensions; i++) { |
| 2563 | if (kExtensions[i].value == value) { |
| 2564 | *out_index = i; |
| 2565 | return &kExtensions[i]; |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | return NULL; |
| 2570 | } |
| 2571 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2572 | int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) { |
| 2573 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2574 | // Don't add extensions for SSLv3 unless doing secure renegotiation. |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 2575 | if (hs->client_version == SSL3_VERSION && |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2576 | !ssl->s3->send_connection_binding) { |
| 2577 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2578 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2579 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2580 | CBB extensions; |
| 2581 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2582 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2583 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2584 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2585 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2586 | hs->extensions.sent = 0; |
| 2587 | hs->custom_extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2588 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2589 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2590 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2591 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2592 | } |
| 2593 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2594 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2595 | uint16_t grease_ext1 = 0; |
| 2596 | if (ssl->ctx->grease_enabled) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2597 | // Add a fake empty extension. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2598 | grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1); |
| 2599 | if (!CBB_add_u16(&extensions, grease_ext1) || |
| 2600 | !CBB_add_u16(&extensions, 0 /* zero length */)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2601 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2602 | return 0; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2606 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2607 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2608 | if (!kExtensions[i].add_clienthello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2609 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2610 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2611 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2613 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2614 | if (CBB_len(&extensions) != len_before) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2615 | hs->extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2616 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2617 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2618 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2619 | if (!custom_ext_add_clienthello(hs, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2620 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2621 | return 0; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2624 | if (ssl->ctx->grease_enabled) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2625 | // Add a fake non-empty extension. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2626 | uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2); |
| 2627 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2628 | // The two fake extensions must not have the same value. GREASE values are |
| 2629 | // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different |
| 2630 | // one. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2631 | if (grease_ext1 == grease_ext2) { |
| 2632 | grease_ext2 ^= 0x1010; |
| 2633 | } |
| 2634 | |
| 2635 | if (!CBB_add_u16(&extensions, grease_ext2) || |
| 2636 | !CBB_add_u16(&extensions, 1 /* one byte length */) || |
| 2637 | !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2638 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2639 | return 0; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2643 | if (!SSL_is_dtls(ssl)) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2644 | size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2645 | header_len += 2 + CBB_len(&extensions) + psk_extension_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2646 | if (header_len > 0xff && header_len < 0x200) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2647 | // Add padding to workaround bugs in F5 terminators. See RFC 7685. |
| 2648 | // |
| 2649 | // NB: because this code works out the length of all existing extensions |
| 2650 | // it MUST always appear last. |
David Benjamin | 0a96859 | 2015-07-21 22:06:19 -0400 | [diff] [blame] | 2651 | size_t padding_len = 0x200 - header_len; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2652 | // Extensions take at least four bytes to encode. Always include at least |
| 2653 | // one byte of data if including the extension. WebSphere Application |
| 2654 | // Server 7.0 is intolerant to the last extension being zero-length. See |
| 2655 | // https://crbug.com/363583. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2656 | if (padding_len >= 4 + 1) { |
| 2657 | padding_len -= 4; |
| 2658 | } else { |
| 2659 | padding_len = 1; |
| 2660 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2661 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2662 | uint8_t *padding_bytes; |
| 2663 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2664 | !CBB_add_u16(&extensions, padding_len) || |
| 2665 | !CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2666 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2667 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2668 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2669 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2670 | OPENSSL_memset(padding_bytes, 0, padding_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2671 | } |
| 2672 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2673 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2674 | // The PSK extension must be last, including after the padding. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2675 | if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2676 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2677 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2678 | } |
| 2679 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2680 | // Discard empty extensions blocks. |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2681 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2682 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2685 | return CBB_flush(out); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2686 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2687 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2688 | int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) { |
| 2689 | SSL *const ssl = hs->ssl; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2690 | CBB extensions; |
| 2691 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2692 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2693 | } |
| 2694 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2695 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 2696 | if (!(hs->extensions.received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2697 | // Don't send extensions that were not received. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2698 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2699 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2700 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2701 | if (!kExtensions[i].add_serverhello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2702 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2703 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2704 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2705 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2706 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2707 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2708 | if (!custom_ext_add_serverhello(hs, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2709 | goto err; |
| 2710 | } |
| 2711 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2712 | // Discard empty extensions blocks before TLS 1.3. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2713 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION && |
| 2714 | CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2715 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2718 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2719 | |
| 2720 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2721 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2722 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2723 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2724 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2725 | static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 2726 | const SSL_CLIENT_HELLO *client_hello, |
| 2727 | int *out_alert) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2728 | SSL *const ssl = hs->ssl; |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2729 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2730 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2731 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2732 | } |
| 2733 | } |
| 2734 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2735 | hs->extensions.received = 0; |
| 2736 | hs->custom_extensions.received = 0; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2737 | CBS extensions; |
| 2738 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 2739 | while (CBS_len(&extensions) != 0) { |
| 2740 | uint16_t type; |
| 2741 | CBS extension; |
| 2742 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2743 | // Decode the next extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2744 | if (!CBS_get_u16(&extensions, &type) || |
| 2745 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2746 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2747 | return 0; |
| 2748 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2749 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2750 | // RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 2751 | // ambiguous. Ignore all but the renegotiation_info extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2752 | if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
| 2753 | continue; |
| 2754 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2755 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2756 | unsigned ext_index; |
| 2757 | const struct tls_extension *const ext = |
| 2758 | tls_extension_find(&ext_index, type); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2759 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2760 | if (ext == NULL) { |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2761 | if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2762 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2763 | return 0; |
| 2764 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2765 | continue; |
| 2766 | } |
| 2767 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2768 | hs->extensions.received |= (1u << ext_index); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2769 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2770 | if (!ext->parse_clienthello(hs, &alert, &extension)) { |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2771 | *out_alert = alert; |
| 2772 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2773 | ERR_add_error_dataf("extension %u", (unsigned)type); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2774 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2775 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2776 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2777 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2778 | for (size_t i = 0; i < kNumExtensions; i++) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2779 | if (hs->extensions.received & (1u << i)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2780 | continue; |
| 2781 | } |
| 2782 | |
| 2783 | CBS *contents = NULL, fake_contents; |
| 2784 | static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 2785 | if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 2786 | ssl_client_cipher_list_contains_cipher(client_hello, |
| 2787 | SSL3_CK_SCSV & 0xffff)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2788 | // The renegotiation SCSV was received so pretend that we received a |
| 2789 | // renegotiation extension. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2790 | CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 2791 | sizeof(kFakeRenegotiateExtension)); |
| 2792 | contents = &fake_contents; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2793 | hs->extensions.received |= (1u << i); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2794 | } |
| 2795 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2796 | // Extension wasn't observed so call the callback with a NULL |
| 2797 | // parameter. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2798 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2799 | if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2800 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2801 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2802 | *out_alert = alert; |
| 2803 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2807 | return 1; |
| 2808 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2809 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2810 | int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 2811 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2812 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 2813 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2814 | if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2815 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2818 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2819 | if (ssl_check_clienthello_tlsext(hs) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2820 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2821 | return 0; |
| 2822 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2823 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2824 | return 1; |
| 2825 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2826 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2827 | static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs, |
| 2828 | int *out_alert) { |
| 2829 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2830 | // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2831 | if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2832 | return 1; |
| 2833 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2834 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2835 | // Decode the extensions block and check it is valid. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2836 | CBS extensions; |
| 2837 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2838 | !tls1_check_duplicate_extensions(&extensions)) { |
| 2839 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2840 | return 0; |
| 2841 | } |
| 2842 | |
| 2843 | uint32_t received = 0; |
| 2844 | while (CBS_len(&extensions) != 0) { |
| 2845 | uint16_t type; |
| 2846 | CBS extension; |
| 2847 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2848 | // Decode the next extension. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2849 | if (!CBS_get_u16(&extensions, &type) || |
| 2850 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2851 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2852 | return 0; |
| 2853 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2854 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2855 | unsigned ext_index; |
| 2856 | const struct tls_extension *const ext = |
| 2857 | tls_extension_find(&ext_index, type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2858 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2859 | if (ext == NULL) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2860 | hs->received_custom_extension = true; |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2861 | if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2862 | return 0; |
| 2863 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2864 | continue; |
| 2865 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2866 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 2867 | static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8, |
| 2868 | "too many bits"); |
David Benjamin | 5db7c9b | 2017-01-24 16:17:03 -0500 | [diff] [blame] | 2869 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2870 | if (!(hs->extensions.sent & (1u << ext_index)) && |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2871 | type != TLSEXT_TYPE_renegotiate) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2872 | // If the extension was never sent then it is illegal, except for the |
| 2873 | // renegotiation extension which, in SSL 3.0, is signaled via SCSV. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2874 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2875 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 2876 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2877 | return 0; |
| 2878 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2879 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2880 | received |= (1u << ext_index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2881 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2882 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2883 | if (!ext->parse_serverhello(hs, &alert, &extension)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2884 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2885 | ERR_add_error_dataf("extension %u", (unsigned)type); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2886 | *out_alert = alert; |
| 2887 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2888 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2889 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2890 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2891 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2892 | if (!(received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2893 | // Extension wasn't observed so call the callback with a NULL |
| 2894 | // parameter. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2895 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2896 | if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2897 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2898 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2899 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2900 | return 0; |
| 2901 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2902 | } |
| 2903 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2904 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2905 | return 1; |
| 2906 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2907 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2908 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) { |
| 2909 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2910 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2911 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2912 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2913 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2914 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2915 | ssl->ctx->tlsext_servername_arg); |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 2916 | } else if (ssl->session_ctx->tlsext_servername_callback != 0) { |
| 2917 | ret = ssl->session_ctx->tlsext_servername_callback( |
| 2918 | ssl, &al, ssl->session_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2919 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2920 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2921 | switch (ret) { |
| 2922 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2923 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2924 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2925 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2926 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2927 | hs->should_ack_sni = false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2928 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2929 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2930 | default: |
| 2931 | return 1; |
| 2932 | } |
| 2933 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2934 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2935 | int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) { |
| 2936 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 2937 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2938 | if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2939 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2940 | return 0; |
| 2941 | } |
| 2942 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2943 | return 1; |
| 2944 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2945 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2946 | static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx( |
| 2947 | uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx, |
| 2948 | HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) { |
| 2949 | size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2950 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2951 | // Check the MAC at the end of the ticket. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2952 | uint8_t mac[EVP_MAX_MD_SIZE]; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2953 | size_t mac_len = HMAC_size(hmac_ctx); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2954 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2955 | // 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] | 2956 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2957 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2958 | HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len); |
| 2959 | HMAC_Final(hmac_ctx, mac, NULL); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2960 | int mac_ok = |
| 2961 | CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0; |
| 2962 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 2963 | mac_ok = 1; |
| 2964 | #endif |
| 2965 | if (!mac_ok) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2966 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2967 | } |
| 2968 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2969 | // Decrypt the session data. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2970 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 2971 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 2972 | mac_len; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2973 | UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len)); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2974 | if (!plaintext) { |
| 2975 | return ssl_ticket_aead_error; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2976 | } |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2977 | size_t plaintext_len; |
| 2978 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2979 | OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2980 | plaintext_len = ciphertext_len; |
| 2981 | #else |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2982 | if (ciphertext_len >= INT_MAX) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2983 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2984 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2985 | int len1, len2; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2986 | if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2987 | (int)ciphertext_len) || |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2988 | !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2989 | ERR_clear_error(); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2990 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2991 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2992 | plaintext_len = (size_t)(len1) + len2; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2993 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2994 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2995 | *out = plaintext.release(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2996 | *out_len = plaintext_len; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2997 | return ssl_ticket_aead_success; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2998 | } |
| 2999 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3000 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3001 | SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket, |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3002 | const uint8_t *ticket, size_t ticket_len) { |
| 3003 | assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3004 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3005 | ScopedHMAC_CTX hmac_ctx; |
| 3006 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 3007 | int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb( |
| 3008 | ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(), |
| 3009 | hmac_ctx.get(), 0 /* decrypt */); |
| 3010 | if (cb_ret < 0) { |
| 3011 | return ssl_ticket_aead_error; |
| 3012 | } else if (cb_ret == 0) { |
| 3013 | return ssl_ticket_aead_ignore_ticket; |
| 3014 | } else if (cb_ret == 2) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3015 | *out_renew_ticket = true; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3016 | } else { |
| 3017 | assert(cb_ret == 1); |
| 3018 | } |
| 3019 | return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(), |
| 3020 | hmac_ctx.get(), ticket, ticket_len); |
| 3021 | } |
| 3022 | |
| 3023 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys( |
| 3024 | SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket, |
| 3025 | size_t ticket_len) { |
| 3026 | assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3027 | SSL_CTX *ctx = ssl->session_ctx; |
| 3028 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3029 | // Rotate the ticket key if necessary. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3030 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 3031 | return ssl_ticket_aead_error; |
| 3032 | } |
| 3033 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3034 | // Pick the matching ticket key and decrypt. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3035 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3036 | ScopedHMAC_CTX hmac_ctx; |
| 3037 | { |
| 3038 | MutexReadLock lock(&ctx->lock); |
| 3039 | const tlsext_ticket_key *key; |
| 3040 | if (ctx->tlsext_ticket_key_current && |
| 3041 | !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket, |
| 3042 | SSL_TICKET_KEY_NAME_LEN)) { |
| 3043 | key = ctx->tlsext_ticket_key_current; |
| 3044 | } else if (ctx->tlsext_ticket_key_prev && |
| 3045 | !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket, |
| 3046 | SSL_TICKET_KEY_NAME_LEN)) { |
| 3047 | key = ctx->tlsext_ticket_key_prev; |
| 3048 | } else { |
| 3049 | return ssl_ticket_aead_ignore_ticket; |
| 3050 | } |
| 3051 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 3052 | if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key), |
| 3053 | tlsext_tick_md(), NULL) || |
| 3054 | !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL, |
| 3055 | key->aes_key, iv)) { |
| 3056 | return ssl_ticket_aead_error; |
| 3057 | } |
| 3058 | } |
| 3059 | return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(), |
| 3060 | hmac_ctx.get(), ticket, ticket_len); |
| 3061 | } |
| 3062 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3063 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3064 | SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3065 | const uint8_t *ticket, size_t ticket_len) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3066 | uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3067 | if (plaintext == NULL) { |
| 3068 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 3069 | return ssl_ticket_aead_error; |
| 3070 | } |
| 3071 | |
| 3072 | size_t plaintext_len; |
| 3073 | const enum ssl_ticket_aead_result_t result = |
| 3074 | ssl->session_ctx->ticket_aead_method->open( |
| 3075 | ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len); |
| 3076 | |
| 3077 | if (result == ssl_ticket_aead_success) { |
| 3078 | *out = plaintext; |
| 3079 | plaintext = NULL; |
| 3080 | *out_len = plaintext_len; |
| 3081 | } |
| 3082 | |
| 3083 | OPENSSL_free(plaintext); |
| 3084 | return result; |
| 3085 | } |
| 3086 | |
| 3087 | enum ssl_ticket_aead_result_t ssl_process_ticket( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3088 | SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3089 | const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id, |
| 3090 | size_t session_id_len) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3091 | *out_renew_ticket = false; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3092 | out_session->reset(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3093 | |
| 3094 | if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) || |
| 3095 | session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3096 | return ssl_ticket_aead_ignore_ticket; |
| 3097 | } |
| 3098 | |
| 3099 | uint8_t *plaintext = NULL; |
| 3100 | size_t plaintext_len; |
| 3101 | enum ssl_ticket_aead_result_t result; |
| 3102 | if (ssl->session_ctx->ticket_aead_method != NULL) { |
| 3103 | result = ssl_decrypt_ticket_with_method( |
| 3104 | ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len); |
| 3105 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3106 | // Ensure there is room for the key name and the largest IV |
| 3107 | // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, |
| 3108 | // but the maximum IV length should be well under the minimum size for the |
| 3109 | // session material and HMAC. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3110 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 3111 | return ssl_ticket_aead_ignore_ticket; |
| 3112 | } |
| 3113 | if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) { |
| 3114 | result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len, |
| 3115 | out_renew_ticket, ticket, ticket_len); |
| 3116 | } else { |
| 3117 | result = ssl_decrypt_ticket_with_ticket_keys( |
| 3118 | ssl, &plaintext, &plaintext_len, ticket, ticket_len); |
| 3119 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | if (result != ssl_ticket_aead_success) { |
| 3123 | return result; |
| 3124 | } |
| 3125 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3126 | // Decode the session. |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3127 | UniquePtr<SSL_SESSION> session( |
| 3128 | SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx)); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3129 | OPENSSL_free(plaintext); |
| 3130 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3131 | if (!session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3132 | ERR_clear_error(); // Don't leave an error on the queue. |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3133 | return ssl_ticket_aead_ignore_ticket; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3134 | } |
| 3135 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3136 | // Copy the client's session ID into the new session, to denote the ticket has |
| 3137 | // been accepted. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 3138 | OPENSSL_memcpy(session->session_id, session_id, session_id_len); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3139 | session->session_id_length = session_id_len; |
| 3140 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3141 | *out_session = std::move(session); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3142 | return ssl_ticket_aead_success; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3143 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3144 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3145 | int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3146 | // Extension ignored for inappropriate versions |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3147 | if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3148 | return 1; |
| 3149 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 3150 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3151 | return parse_u16_array(in_sigalgs, &hs->peer_sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3152 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3153 | |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3154 | int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) { |
| 3155 | switch (EVP_PKEY_id(pkey)) { |
| 3156 | case EVP_PKEY_RSA: |
| 3157 | *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 3158 | return 1; |
| 3159 | case EVP_PKEY_EC: |
| 3160 | *out = SSL_SIGN_ECDSA_SHA1; |
| 3161 | return 1; |
| 3162 | default: |
| 3163 | return 0; |
| 3164 | } |
| 3165 | } |
| 3166 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3167 | int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) { |
| 3168 | SSL *const ssl = hs->ssl; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 3169 | CERT *cert = ssl->cert; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3170 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3171 | // Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 3172 | // handshake. |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3173 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 3174 | if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) { |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3175 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3176 | return 0; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3177 | } |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3178 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3179 | } |
| 3180 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3181 | Span<const uint16_t> sigalgs = kSignSignatureAlgorithms; |
| 3182 | if (cert->sigalgs != nullptr) { |
| 3183 | sigalgs = MakeConstSpan(cert->sigalgs, cert->num_sigalgs); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3184 | } |
| 3185 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3186 | Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs; |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 3187 | if (peer_sigalgs.empty() && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3188 | // If the client didn't specify any signature_algorithms extension then |
| 3189 | // we can assume that it supports SHA1. See |
| 3190 | // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3191 | static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 3192 | SSL_SIGN_ECDSA_SHA1}; |
| 3193 | peer_sigalgs = kDefaultPeerAlgorithms; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3196 | for (uint16_t sigalg : sigalgs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3197 | // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be |
| 3198 | // negotiated. |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3199 | if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 || |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3200 | !ssl_private_key_supports_signature_algorithm(hs, sigalg)) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3201 | continue; |
| 3202 | } |
| 3203 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3204 | for (uint16_t peer_sigalg : peer_sigalgs) { |
| 3205 | if (sigalg == peer_sigalg) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3206 | *out = sigalg; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3207 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3208 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3209 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3211 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3212 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3213 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3214 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3215 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3216 | int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3217 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3218 | // A Channel ID handshake message is structured to contain multiple |
| 3219 | // extensions, but the only one that can be present is Channel ID. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3220 | uint16_t extension_type; |
| 3221 | CBS channel_id = msg.body, extension; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3222 | if (!CBS_get_u16(&channel_id, &extension_type) || |
| 3223 | !CBS_get_u16_length_prefixed(&channel_id, &extension) || |
| 3224 | CBS_len(&channel_id) != 0 || |
| 3225 | extension_type != TLSEXT_TYPE_channel_id || |
| 3226 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 3227 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3228 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 3229 | return 0; |
| 3230 | } |
| 3231 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3232 | 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] | 3233 | if (!p256) { |
| 3234 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
| 3235 | return 0; |
| 3236 | } |
| 3237 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3238 | UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new()); |
| 3239 | UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3240 | if (!sig || !x || !y) { |
| 3241 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | const uint8_t *p = CBS_data(&extension); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3245 | if (BN_bin2bn(p + 0, 32, x.get()) == NULL || |
| 3246 | BN_bin2bn(p + 32, 32, y.get()) == NULL || |
| 3247 | BN_bin2bn(p + 64, 32, sig->r) == NULL || |
| 3248 | BN_bin2bn(p + 96, 32, sig->s) == NULL) { |
| 3249 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3252 | UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 3253 | UniquePtr<EC_POINT> point(EC_POINT_new(p256.get())); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3254 | if (!key || !point || |
| 3255 | !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(), |
| 3256 | y.get(), nullptr) || |
| 3257 | !EC_KEY_set_group(key.get(), p256.get()) || |
| 3258 | !EC_KEY_set_public_key(key.get(), point.get())) { |
| 3259 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3263 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3264 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3265 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3268 | int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get()); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3269 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3270 | sig_ok = 1; |
| 3271 | #endif |
| 3272 | if (!sig_ok) { |
| 3273 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 3274 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 3275 | ssl->s3->tlsext_channel_id_valid = false; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3276 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 3279 | OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3280 | return 1; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3281 | } |
| 3282 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3283 | int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) { |
| 3284 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3285 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3286 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3287 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3288 | return 0; |
| 3289 | } |
| 3290 | |
| 3291 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
| 3292 | if (ec_key == NULL) { |
| 3293 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
| 3297 | int ret = 0; |
| 3298 | BIGNUM *x = BN_new(); |
| 3299 | BIGNUM *y = BN_new(); |
| 3300 | ECDSA_SIG *sig = NULL; |
| 3301 | if (x == NULL || y == NULL || |
| 3302 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 3303 | EC_KEY_get0_public_key(ec_key), |
| 3304 | x, y, NULL)) { |
| 3305 | goto err; |
| 3306 | } |
| 3307 | |
| 3308 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
| 3309 | if (sig == NULL) { |
| 3310 | goto err; |
| 3311 | } |
| 3312 | |
| 3313 | CBB child; |
| 3314 | if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) || |
| 3315 | !CBB_add_u16_length_prefixed(cbb, &child) || |
| 3316 | !BN_bn2cbb_padded(&child, 32, x) || |
| 3317 | !BN_bn2cbb_padded(&child, 32, y) || |
| 3318 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 3319 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
| 3320 | !CBB_flush(cbb)) { |
| 3321 | goto err; |
| 3322 | } |
| 3323 | |
| 3324 | ret = 1; |
| 3325 | |
| 3326 | err: |
| 3327 | BN_free(x); |
| 3328 | BN_free(y); |
| 3329 | ECDSA_SIG_free(sig); |
| 3330 | return ret; |
| 3331 | } |
| 3332 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3333 | int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) { |
| 3334 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3335 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 3336 | uint8_t *msg; |
| 3337 | size_t msg_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3338 | if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3339 | ssl_cert_verify_channel_id)) { |
| 3340 | return 0; |
| 3341 | } |
| 3342 | SHA256(msg, msg_len, out); |
| 3343 | *out_len = SHA256_DIGEST_LENGTH; |
| 3344 | OPENSSL_free(msg); |
| 3345 | return 1; |
| 3346 | } |
| 3347 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3348 | SHA256_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3349 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3350 | SHA256_Init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3351 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3352 | SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3353 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3354 | if (ssl->session != NULL) { |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3355 | static const char kResumptionMagic[] = "Resumption"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3356 | SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3357 | if (ssl->session->original_handshake_hash_len == 0) { |
| 3358 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3359 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3360 | } |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3361 | SHA256_Update(&ctx, ssl->session->original_handshake_hash, |
| 3362 | ssl->session->original_handshake_hash_len); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3363 | } |
| 3364 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3365 | uint8_t hs_hash[EVP_MAX_MD_SIZE]; |
| 3366 | size_t hs_hash_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 3367 | if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) { |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3368 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3369 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3370 | SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3371 | SHA256_Final(out, &ctx); |
| 3372 | *out_len = SHA256_DIGEST_LENGTH; |
| 3373 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3374 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3375 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3376 | // tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 3377 | // hashes in |hs->new_session| so that Channel ID resumptions can sign that |
| 3378 | // data. |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3379 | int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) { |
| 3380 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3381 | // This function should never be called for a resumed session because the |
| 3382 | // handshake hashes that we wish to record are for the original, full |
| 3383 | // handshake. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3384 | if (ssl->session != NULL) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 3385 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3386 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3387 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3388 | static_assert( |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 3389 | sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE, |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3390 | "original_handshake_hash is too small"); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3391 | |
| 3392 | size_t digest_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 3393 | if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash, |
| 3394 | &digest_len)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 3395 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3396 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3397 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3398 | static_assert(EVP_MAX_MD_SIZE <= 0xff, |
| 3399 | "EVP_MAX_MD_SIZE does not fit in uint8_t"); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 3400 | hs->new_session->original_handshake_hash_len = (uint8_t)digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3401 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3402 | return 1; |
| 3403 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3404 | |
| 3405 | int ssl_do_channel_id_callback(SSL *ssl) { |
| 3406 | if (ssl->tlsext_channel_id_private != NULL || |
| 3407 | ssl->ctx->channel_id_cb == NULL) { |
| 3408 | return 1; |
| 3409 | } |
| 3410 | |
| 3411 | EVP_PKEY *key = NULL; |
| 3412 | ssl->ctx->channel_id_cb(ssl, &key); |
| 3413 | if (key == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3414 | // The caller should try again later. |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3415 | return 1; |
| 3416 | } |
| 3417 | |
| 3418 | int ret = SSL_set1_tls_channel_id(ssl, key); |
| 3419 | EVP_PKEY_free(key); |
| 3420 | return ret; |
| 3421 | } |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3422 | |
| 3423 | int ssl_is_sct_list_valid(const CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3424 | // Shallow parse the SCT list for sanity. By the RFC |
| 3425 | // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any |
| 3426 | // of the SCTs may be empty. |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3427 | CBS copy = *contents; |
| 3428 | CBS sct_list; |
| 3429 | if (!CBS_get_u16_length_prefixed(©, &sct_list) || |
| 3430 | CBS_len(©) != 0 || |
| 3431 | CBS_len(&sct_list) == 0) { |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | while (CBS_len(&sct_list) > 0) { |
| 3436 | CBS sct; |
| 3437 | if (!CBS_get_u16_length_prefixed(&sct_list, &sct) || |
| 3438 | CBS_len(&sct) == 0) { |
| 3439 | return 0; |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | return 1; |
| 3444 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3445 | |
| 3446 | } // namespace bssl |
| 3447 | |
| 3448 | using namespace bssl; |
| 3449 | |
| 3450 | int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello, |
| 3451 | uint16_t extension_type, |
| 3452 | const uint8_t **out_data, |
| 3453 | size_t *out_len) { |
| 3454 | CBS cbs; |
| 3455 | if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) { |
| 3456 | return 0; |
| 3457 | } |
| 3458 | |
| 3459 | *out_data = CBS_data(&cbs); |
| 3460 | *out_len = CBS_len(&cbs); |
| 3461 | return 1; |
| 3462 | } |
| 3463 | |
| 3464 | void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) { |
| 3465 | ctx->ed25519_enabled = !!enabled; |
| 3466 | } |
| 3467 | |
| 3468 | int SSL_extension_supported(unsigned extension_value) { |
| 3469 | uint32_t index; |
| 3470 | return extension_value == TLSEXT_TYPE_padding || |
| 3471 | tls_extension_find(&index, extension_value) != NULL; |
| 3472 | } |