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. |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -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 | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1014 | OPENSSL_free(hs->peer_sigalgs); |
| 1015 | hs->peer_sigalgs = NULL; |
| 1016 | hs->num_peer_sigalgs = 0; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1017 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1018 | if (contents == NULL) { |
| 1019 | return 1; |
| 1020 | } |
| 1021 | |
| 1022 | CBS supported_signature_algorithms; |
| 1023 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1024 | CBS_len(contents) != 0 || |
| 1025 | CBS_len(&supported_signature_algorithms) == 0 || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1026 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1033 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1034 | // OCSP Stapling. |
| 1035 | // |
| 1036 | // https://tools.ietf.org/html/rfc6066#section-8 |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1037 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1038 | static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1039 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1040 | if (!ssl->ocsp_stapling_enabled) { |
| 1041 | return 1; |
| 1042 | } |
| 1043 | |
| 1044 | CBB contents; |
| 1045 | if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1046 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1047 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1048 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1049 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1050 | !CBB_flush(out)) { |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | return 1; |
| 1055 | } |
| 1056 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1057 | 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] | 1058 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1059 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1060 | if (contents == NULL) { |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1064 | // TLS 1.3 OCSP responses are included in the Certificate extensions. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1065 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1069 | // OCSP stapling is forbidden on non-certificate ciphers. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1070 | if (CBS_len(contents) != 0 || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1071 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1075 | // Note this does not check for resumption in TLS 1.2. Sending |
| 1076 | // status_request here does not make sense, but OpenSSL does so and the |
| 1077 | // specification does not say anything. Tolerate it but ignore it. |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1078 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1079 | hs->certificate_status_expected = true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1080 | return 1; |
| 1081 | } |
| 1082 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1083 | 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] | 1084 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1085 | if (contents == NULL) { |
| 1086 | return 1; |
| 1087 | } |
| 1088 | |
| 1089 | uint8_t status_type; |
| 1090 | if (!CBS_get_u8(contents, &status_type)) { |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1094 | // We cannot decide whether OCSP stapling will occur yet because the correct |
| 1095 | // SSL_CTX might not have been selected. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1096 | hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1097 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1098 | return 1; |
| 1099 | } |
| 1100 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1101 | static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1102 | SSL *const ssl = hs->ssl; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1103 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1104 | !hs->ocsp_stapling_requested || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1105 | ssl->cert->ocsp_response == NULL || |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1106 | ssl->s3->session_reused || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1107 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1108 | return 1; |
| 1109 | } |
| 1110 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1111 | hs->certificate_status_expected = true; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1112 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1113 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1114 | CBB_add_u16(out, 0 /* length */); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1118 | // Next protocol negotiation. |
| 1119 | // |
| 1120 | // 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] | 1121 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1122 | static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1123 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1124 | if (ssl->s3->initial_handshake_complete || |
| 1125 | ssl->ctx->next_proto_select_cb == NULL || |
David Benjamin | 9d125dc | 2016-12-07 21:32:37 -0500 | [diff] [blame] | 1126 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1127 | return 1; |
| 1128 | } |
| 1129 | |
| 1130 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1131 | !CBB_add_u16(out, 0 /* length */)) { |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1138 | 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] | 1139 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1140 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1141 | if (contents == NULL) { |
| 1142 | return 1; |
| 1143 | } |
| 1144 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1145 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1149 | // If any of these are false then we should never have sent the NPN |
| 1150 | // extension in the ClientHello and thus this function should never have been |
| 1151 | // called. |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1152 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1153 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1154 | assert(ssl->ctx->next_proto_select_cb != NULL); |
| 1155 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1156 | if (ssl->s3->alpn_selected != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1157 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1158 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1159 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1163 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1164 | const size_t orig_len = CBS_len(contents); |
| 1165 | |
| 1166 | while (CBS_len(contents) != 0) { |
| 1167 | CBS proto; |
| 1168 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1169 | CBS_len(&proto) == 0) { |
| 1170 | return 0; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | uint8_t *selected; |
| 1175 | uint8_t selected_len; |
| 1176 | if (ssl->ctx->next_proto_select_cb( |
| 1177 | ssl, &selected, &selected_len, orig_contents, orig_len, |
| 1178 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1179 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1183 | OPENSSL_free(ssl->s3->next_proto_negotiated); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 1184 | ssl->s3->next_proto_negotiated = |
| 1185 | (uint8_t *)BUF_memdup(selected, selected_len); |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1186 | if (ssl->s3->next_proto_negotiated == NULL) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1187 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1191 | ssl->s3->next_proto_negotiated_len = selected_len; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1192 | hs->next_proto_neg_seen = true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1193 | |
| 1194 | return 1; |
| 1195 | } |
| 1196 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1197 | 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] | 1198 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1199 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1200 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1204 | if (contents != NULL && CBS_len(contents) != 0) { |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | if (contents == NULL || |
| 1209 | ssl->s3->initial_handshake_complete || |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1210 | ssl->ctx->next_protos_advertised_cb == NULL || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1211 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1212 | return 1; |
| 1213 | } |
| 1214 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1215 | hs->next_proto_neg_seen = true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1216 | return 1; |
| 1217 | } |
| 1218 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1219 | static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1220 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1221 | // |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1222 | // parsed. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1223 | if (!hs->next_proto_neg_seen) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1224 | return 1; |
| 1225 | } |
| 1226 | |
| 1227 | const uint8_t *npa; |
| 1228 | unsigned npa_len; |
| 1229 | |
| 1230 | if (ssl->ctx->next_protos_advertised_cb( |
| 1231 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1232 | SSL_TLSEXT_ERR_OK) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1233 | hs->next_proto_neg_seen = false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1234 | return 1; |
| 1235 | } |
| 1236 | |
| 1237 | CBB contents; |
| 1238 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1239 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1240 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1241 | !CBB_flush(out)) { |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | return 1; |
| 1246 | } |
| 1247 | |
| 1248 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1249 | // Signed certificate timestamps. |
| 1250 | // |
| 1251 | // https://tools.ietf.org/html/rfc6962#section-3.3.1 |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1252 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1253 | static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1254 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1255 | if (!ssl->signed_cert_timestamps_enabled) { |
| 1256 | return 1; |
| 1257 | } |
| 1258 | |
| 1259 | if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1260 | !CBB_add_u16(out, 0 /* length */)) { |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1267 | 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] | 1268 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1269 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1270 | if (contents == NULL) { |
| 1271 | return 1; |
| 1272 | } |
| 1273 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1274 | // TLS 1.3 SCTs are included in the Certificate extensions. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1275 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1276 | *out_alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1277 | return 0; |
| 1278 | } |
| 1279 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1280 | // If this is false then we should never have sent the SCT extension in the |
| 1281 | // ClientHello and thus this function should never have been called. |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1282 | assert(ssl->signed_cert_timestamps_enabled); |
| 1283 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1284 | if (!ssl_is_sct_list_valid(contents)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1285 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1289 | // Session resumption uses the original session information. The extension |
| 1290 | // should not be sent on resumption, but RFC 6962 did not make it a |
| 1291 | // requirement, so tolerate this. |
| 1292 | // |
| 1293 | // TODO(davidben): Enforce this anyway. |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1294 | if (!ssl->s3->session_reused) { |
| 1295 | CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list); |
| 1296 | hs->new_session->signed_cert_timestamp_list = |
| 1297 | CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool); |
| 1298 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
| 1299 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1300 | return 0; |
| 1301 | } |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | return 1; |
| 1305 | } |
| 1306 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1307 | 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] | 1308 | CBS *contents) { |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1309 | if (contents == NULL) { |
| 1310 | return 1; |
| 1311 | } |
| 1312 | |
| 1313 | if (CBS_len(contents) != 0) { |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1317 | hs->scts_requested = true; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1318 | return 1; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1321 | static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1322 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1323 | // The extension shouldn't be sent when resuming sessions. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1324 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1325 | ssl->s3->session_reused || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1326 | ssl->cert->signed_cert_timestamp_list == NULL) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1327 | return 1; |
| 1328 | } |
| 1329 | |
| 1330 | CBB contents; |
| 1331 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1332 | CBB_add_u16_length_prefixed(out, &contents) && |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1333 | CBB_add_bytes( |
| 1334 | &contents, |
| 1335 | CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list), |
| 1336 | CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) && |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1337 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1341 | // Application-level Protocol Negotiation. |
| 1342 | // |
| 1343 | // https://tools.ietf.org/html/rfc7301 |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1344 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1345 | static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1346 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1347 | if (ssl->alpn_client_proto_list == NULL || |
| 1348 | ssl->s3->initial_handshake_complete) { |
| 1349 | return 1; |
| 1350 | } |
| 1351 | |
| 1352 | CBB contents, proto_list; |
| 1353 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1354 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1355 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1356 | !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1357 | ssl->alpn_client_proto_list_len) || |
| 1358 | !CBB_flush(out)) { |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | return 1; |
| 1363 | } |
| 1364 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1365 | 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] | 1366 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1367 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1368 | if (contents == NULL) { |
| 1369 | return 1; |
| 1370 | } |
| 1371 | |
| 1372 | assert(!ssl->s3->initial_handshake_complete); |
| 1373 | assert(ssl->alpn_client_proto_list != NULL); |
| 1374 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1375 | if (hs->next_proto_neg_seen) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1376 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1377 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1378 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1382 | // The extension data consists of a ProtocolNameList which must have |
| 1383 | // exactly one ProtocolName. Each of these is length-prefixed. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1384 | CBS protocol_name_list, protocol_name; |
| 1385 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1386 | CBS_len(contents) != 0 || |
| 1387 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1388 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1389 | CBS_len(&protocol_name) == 0 || |
| 1390 | CBS_len(&protocol_name_list) != 0) { |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 1394 | if (!ssl->ctx->allow_unknown_alpn_protos) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1395 | // Check that the protocol name is one of the ones we advertised. |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 1396 | int protocol_ok = 0; |
| 1397 | CBS client_protocol_name_list, client_protocol_name; |
| 1398 | CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list, |
| 1399 | ssl->alpn_client_proto_list_len); |
| 1400 | while (CBS_len(&client_protocol_name_list) > 0) { |
| 1401 | if (!CBS_get_u8_length_prefixed(&client_protocol_name_list, |
| 1402 | &client_protocol_name)) { |
| 1403 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) && |
| 1408 | OPENSSL_memcmp(CBS_data(&client_protocol_name), |
| 1409 | CBS_data(&protocol_name), |
| 1410 | CBS_len(&protocol_name)) == 0) { |
| 1411 | protocol_ok = 1; |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | if (!protocol_ok) { |
| 1417 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1418 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3e51757 | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
David Benjamin | 3e51757 | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1421 | } |
| 1422 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1423 | if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected, |
| 1424 | &ssl->s3->alpn_selected_len)) { |
| 1425 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1432 | int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 1433 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1434 | SSL *const ssl = hs->ssl; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1435 | CBS contents; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1436 | if (ssl->ctx->alpn_select_cb == NULL || |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 1437 | !ssl_client_hello_get_extension( |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1438 | client_hello, &contents, |
| 1439 | TLSEXT_TYPE_application_layer_protocol_negotiation)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1440 | // Ignore ALPN if not configured or no extension was supplied. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1441 | return 1; |
| 1442 | } |
| 1443 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1444 | // ALPN takes precedence over NPN. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1445 | hs->next_proto_neg_seen = false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1446 | |
| 1447 | CBS protocol_name_list; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1448 | if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) || |
| 1449 | CBS_len(&contents) != 0 || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1450 | CBS_len(&protocol_name_list) < 2) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1451 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1452 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1456 | // Validate the protocol list. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1457 | CBS protocol_name_list_copy = protocol_name_list; |
| 1458 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1459 | CBS protocol_name; |
| 1460 | |
| 1461 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1462 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1463 | CBS_len(&protocol_name) == 0) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1464 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1465 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1466 | return 0; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | const uint8_t *selected; |
| 1471 | uint8_t selected_len; |
| 1472 | if (ssl->ctx->alpn_select_cb( |
| 1473 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1474 | CBS_len(&protocol_name_list), |
| 1475 | ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
| 1476 | OPENSSL_free(ssl->s3->alpn_selected); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 1477 | ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len); |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1478 | if (ssl->s3->alpn_selected == NULL) { |
| 1479 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1480 | return 0; |
| 1481 | } |
| 1482 | ssl->s3->alpn_selected_len = selected_len; |
| 1483 | } |
| 1484 | |
| 1485 | return 1; |
| 1486 | } |
| 1487 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1488 | static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1489 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1490 | if (ssl->s3->alpn_selected == NULL) { |
| 1491 | return 1; |
| 1492 | } |
| 1493 | |
| 1494 | CBB contents, proto_list, proto; |
| 1495 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1496 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1497 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1498 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1499 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected, |
| 1500 | ssl->s3->alpn_selected_len) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1501 | !CBB_flush(out)) { |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | return 1; |
| 1506 | } |
| 1507 | |
| 1508 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1509 | // Channel ID. |
| 1510 | // |
| 1511 | // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1512 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1513 | static void ext_channel_id_init(SSL_HANDSHAKE *hs) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1514 | hs->ssl->s3->tlsext_channel_id_valid = false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1517 | static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1518 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1519 | if (!ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1520 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1521 | return 1; |
| 1522 | } |
| 1523 | |
| 1524 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1525 | !CBB_add_u16(out, 0 /* length */)) { |
| 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | return 1; |
| 1530 | } |
| 1531 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1532 | static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1533 | uint8_t *out_alert, CBS *contents) { |
| 1534 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1535 | if (contents == NULL) { |
| 1536 | return 1; |
| 1537 | } |
| 1538 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1539 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1540 | assert(ssl->tlsext_channel_id_enabled); |
| 1541 | |
| 1542 | if (CBS_len(contents) != 0) { |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1546 | ssl->s3->tlsext_channel_id_valid = true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1547 | return 1; |
| 1548 | } |
| 1549 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1550 | static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1551 | uint8_t *out_alert, CBS *contents) { |
| 1552 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1553 | if (contents == NULL || |
| 1554 | !ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1555 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | if (CBS_len(contents) != 0) { |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1563 | ssl->s3->tlsext_channel_id_valid = true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1564 | return 1; |
| 1565 | } |
| 1566 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1567 | static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1568 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1569 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
| 1573 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1574 | !CBB_add_u16(out, 0 /* length */)) { |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
| 1578 | return 1; |
| 1579 | } |
| 1580 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1581 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1582 | // Secure Real-time Transport Protocol (SRTP) extension. |
| 1583 | // |
| 1584 | // https://tools.ietf.org/html/rfc5764 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1585 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1586 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1587 | static void ext_srtp_init(SSL_HANDSHAKE *hs) { |
| 1588 | hs->ssl->srtp_profile = NULL; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1591 | static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1592 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1593 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1594 | if (profiles == NULL || |
| 1595 | sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1596 | return 1; |
| 1597 | } |
| 1598 | |
| 1599 | CBB contents, profile_ids; |
| 1600 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1601 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1602 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1606 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
| 1607 | if (!CBB_add_u16(&profile_ids, profile->id)) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1608 | return 0; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1613 | !CBB_flush(out)) { |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | return 1; |
| 1618 | } |
| 1619 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1620 | 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] | 1621 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1622 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1623 | if (contents == NULL) { |
| 1624 | return 1; |
| 1625 | } |
| 1626 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1627 | // The extension consists of a u16-prefixed profile ID list containing a |
| 1628 | // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1629 | // |
| 1630 | // See https://tools.ietf.org/html/rfc5764#section-4.1.1 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1631 | CBS profile_ids, srtp_mki; |
| 1632 | uint16_t profile_id; |
| 1633 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1634 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1635 | CBS_len(&profile_ids) != 0 || |
| 1636 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1637 | CBS_len(contents) != 0) { |
| 1638 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | if (CBS_len(&srtp_mki) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1643 | // Must be no MKI, since we never offer one. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1644 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1645 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1650 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1651 | // Check to see if the server gave us something we support (and presumably |
| 1652 | // offered). |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1653 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1654 | if (profile->id == profile_id) { |
| 1655 | ssl->srtp_profile = profile; |
| 1656 | return 1; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1661 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1665 | 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] | 1666 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1667 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1668 | if (contents == NULL) { |
| 1669 | return 1; |
| 1670 | } |
| 1671 | |
| 1672 | CBS profile_ids, srtp_mki; |
| 1673 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1674 | CBS_len(&profile_ids) < 2 || |
| 1675 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1676 | CBS_len(contents) != 0) { |
| 1677 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1678 | return 0; |
| 1679 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1680 | // Discard the MKI value for now. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1681 | |
| 1682 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1683 | SSL_get_srtp_profiles(ssl); |
| 1684 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1685 | // Pick the server's most preferred profile. |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1686 | for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1687 | CBS profile_ids_tmp; |
| 1688 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1689 | |
| 1690 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1691 | uint16_t profile_id; |
| 1692 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | if (server_profile->id == profile_id) { |
| 1697 | ssl->srtp_profile = server_profile; |
| 1698 | return 1; |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | return 1; |
| 1704 | } |
| 1705 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1706 | static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1707 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1708 | if (ssl->srtp_profile == NULL) { |
| 1709 | return 1; |
| 1710 | } |
| 1711 | |
| 1712 | CBB contents, profile_ids; |
| 1713 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1714 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1715 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1716 | !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1717 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1718 | !CBB_flush(out)) { |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | return 1; |
| 1723 | } |
| 1724 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1725 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1726 | // EC point formats. |
| 1727 | // |
| 1728 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1729 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1730 | static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1731 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1732 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1733 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1734 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1735 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1736 | !CBB_flush(out)) { |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | return 1; |
| 1741 | } |
| 1742 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1743 | static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1744 | // The point format extension is unneccessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1745 | if (hs->min_version >= TLS1_3_VERSION) { |
David Benjamin | 70aba26 | 2016-11-01 12:08:15 -0400 | [diff] [blame] | 1746 | return 1; |
| 1747 | } |
| 1748 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1749 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1752 | 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] | 1753 | CBS *contents) { |
| 1754 | if (contents == NULL) { |
| 1755 | return 1; |
| 1756 | } |
| 1757 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1758 | if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1759 | return 0; |
| 1760 | } |
| 1761 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1762 | CBS ec_point_format_list; |
| 1763 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1764 | CBS_len(contents) != 0) { |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1768 | // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1769 | // point format. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1770 | if (OPENSSL_memchr(CBS_data(&ec_point_format_list), |
| 1771 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1772 | CBS_len(&ec_point_format_list)) == NULL) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1773 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | return 1; |
| 1778 | } |
| 1779 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1780 | 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] | 1781 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1782 | if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1783 | return 1; |
| 1784 | } |
| 1785 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1786 | return ext_ec_point_parse_serverhello(hs, out_alert, contents); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1789 | static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1790 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1791 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1792 | return 1; |
| 1793 | } |
| 1794 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1795 | const uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1796 | const uint32_t alg_a = hs->new_cipher->algorithm_auth; |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1797 | const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1798 | |
| 1799 | if (!using_ecc) { |
| 1800 | return 1; |
| 1801 | } |
| 1802 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1803 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1806 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1807 | // Pre Shared Key |
| 1808 | // |
| 1809 | // 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] | 1810 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1811 | static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) { |
| 1812 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1813 | if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 1814 | SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1815 | return 0; |
| 1816 | } |
| 1817 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 1818 | 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] | 1819 | return 15 + ssl->session->tlsext_ticklen + binder_len; |
| 1820 | } |
| 1821 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1822 | static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1823 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1824 | if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 1825 | SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1826 | return 1; |
| 1827 | } |
| 1828 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1829 | struct OPENSSL_timeval now; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1830 | ssl_get_current_time(ssl, &now); |
| 1831 | uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time); |
| 1832 | uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add; |
| 1833 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1834 | // Fill in a placeholder zero binder of the appropriate length. It will be |
| 1835 | // computed and filled in later after length prefixes are computed. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1836 | uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0}; |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 1837 | 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] | 1838 | |
| 1839 | CBB contents, identity, ticket, binders, binder; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1840 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1841 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1842 | !CBB_add_u16_length_prefixed(&contents, &identity) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1843 | !CBB_add_u16_length_prefixed(&identity, &ticket) || |
| 1844 | !CBB_add_bytes(&ticket, ssl->session->tlsext_tick, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1845 | ssl->session->tlsext_ticklen) || |
| 1846 | !CBB_add_u32(&identity, obfuscated_ticket_age) || |
| 1847 | !CBB_add_u16_length_prefixed(&contents, &binders) || |
| 1848 | !CBB_add_u8_length_prefixed(&binders, &binder) || |
| 1849 | !CBB_add_bytes(&binder, zero_binder, binder_len)) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1850 | return 0; |
| 1851 | } |
| 1852 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1853 | hs->needs_psk_binder = true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1854 | return CBB_flush(out); |
| 1855 | } |
| 1856 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 1857 | int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1858 | uint8_t *out_alert, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1859 | CBS *contents) { |
| 1860 | uint16_t psk_id; |
| 1861 | if (!CBS_get_u16(contents, &psk_id) || |
| 1862 | CBS_len(contents) != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1863 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1864 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1868 | // 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] | 1869 | if (psk_id != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1870 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1871 | *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1872 | return 0; |
| 1873 | } |
| 1874 | |
| 1875 | return 1; |
| 1876 | } |
| 1877 | |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1878 | int ssl_ext_pre_shared_key_parse_clienthello( |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1879 | SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1880 | uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1881 | // 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] | 1882 | CBS identities, binders; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1883 | if (!CBS_get_u16_length_prefixed(contents, &identities) || |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1884 | !CBS_get_u16_length_prefixed(&identities, out_ticket) || |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1885 | !CBS_get_u32(&identities, out_obfuscated_ticket_age) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1886 | !CBS_get_u16_length_prefixed(contents, &binders) || |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1887 | CBS_len(&binders) == 0 || |
Steven Valdez | af3b8a9 | 2016-11-01 12:49:22 -0400 | [diff] [blame] | 1888 | CBS_len(contents) != 0) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1889 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1890 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1894 | *out_binders = binders; |
| 1895 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1896 | // Check the syntax of the remaining identities, but do not process them. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1897 | size_t num_identities = 1; |
| 1898 | while (CBS_len(&identities) != 0) { |
| 1899 | CBS unused_ticket; |
| 1900 | uint32_t unused_obfuscated_ticket_age; |
| 1901 | if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) || |
| 1902 | !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) { |
| 1903 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1904 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1905 | return 0; |
| 1906 | } |
| 1907 | |
| 1908 | num_identities++; |
| 1909 | } |
| 1910 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1911 | // Check the syntax of the binders. The value will be checked later if |
| 1912 | // resuming. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1913 | size_t num_binders = 0; |
| 1914 | while (CBS_len(&binders) != 0) { |
| 1915 | CBS binder; |
| 1916 | if (!CBS_get_u8_length_prefixed(&binders, &binder)) { |
| 1917 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1918 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | num_binders++; |
| 1923 | } |
| 1924 | |
| 1925 | if (num_identities != num_binders) { |
| 1926 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH); |
| 1927 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1928 | return 0; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1929 | } |
| 1930 | |
David Benjamin | e7f60a2 | 2016-11-16 18:54:25 +0900 | [diff] [blame] | 1931 | return 1; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1932 | } |
| 1933 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 1934 | int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1935 | if (!hs->ssl->s3->session_reused) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1936 | return 1; |
| 1937 | } |
| 1938 | |
| 1939 | CBB contents; |
| 1940 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1941 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1942 | // We only consider the first identity for resumption |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1943 | !CBB_add_u16(&contents, 0) || |
| 1944 | !CBB_flush(out)) { |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
| 1948 | return 1; |
| 1949 | } |
| 1950 | |
| 1951 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1952 | // Pre-Shared Key Exchange Modes |
| 1953 | // |
| 1954 | // 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] | 1955 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1956 | static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs, |
| 1957 | CBB *out) { |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1958 | if (hs->max_version < TLS1_3_VERSION) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1959 | return 1; |
| 1960 | } |
| 1961 | |
| 1962 | CBB contents, ke_modes; |
| 1963 | if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) || |
| 1964 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1965 | !CBB_add_u8_length_prefixed(&contents, &ke_modes) || |
| 1966 | !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) { |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | return CBB_flush(out); |
| 1971 | } |
| 1972 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1973 | static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 1974 | uint8_t *out_alert, |
| 1975 | CBS *contents) { |
| 1976 | if (contents == NULL) { |
| 1977 | return 1; |
| 1978 | } |
| 1979 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1980 | CBS ke_modes; |
| 1981 | if (!CBS_get_u8_length_prefixed(contents, &ke_modes) || |
| 1982 | CBS_len(&ke_modes) == 0 || |
| 1983 | CBS_len(contents) != 0) { |
| 1984 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1988 | // We only support tickets with PSK_DHE_KE. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1989 | hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, |
| 1990 | CBS_len(&ke_modes)) != NULL; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1991 | |
| 1992 | return 1; |
| 1993 | } |
| 1994 | |
| 1995 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1996 | // Early Data Indication |
| 1997 | // |
| 1998 | // 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] | 1999 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2000 | static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2001 | SSL *const ssl = hs->ssl; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2002 | if (ssl->session == NULL || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 2003 | SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION || |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2004 | ssl->session->ticket_max_early_data == 0 || |
| 2005 | hs->received_hello_retry_request || |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 2006 | !ssl->cert->enable_early_data) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2007 | return 1; |
| 2008 | } |
| 2009 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2010 | hs->early_data_offered = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2011 | |
| 2012 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2013 | !CBB_add_u16(out, 0) || |
| 2014 | !CBB_flush(out)) { |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2018 | return 1; |
| 2019 | } |
| 2020 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2021 | static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2022 | uint8_t *out_alert, CBS *contents) { |
| 2023 | SSL *const ssl = hs->ssl; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2024 | if (contents == NULL) { |
| 2025 | return 1; |
| 2026 | } |
| 2027 | |
| 2028 | if (CBS_len(contents) != 0) { |
| 2029 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2033 | if (!ssl->s3->session_reused) { |
| 2034 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2035 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2036 | return 0; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2037 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2038 | |
| 2039 | ssl->early_data_accepted = 1; |
| 2040 | return 1; |
| 2041 | } |
| 2042 | |
| 2043 | static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2044 | uint8_t *out_alert, CBS *contents) { |
| 2045 | SSL *const ssl = hs->ssl; |
| 2046 | if (contents == NULL || |
| 2047 | ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2048 | return 1; |
| 2049 | } |
| 2050 | |
| 2051 | if (CBS_len(contents) != 0) { |
| 2052 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2056 | hs->early_data_offered = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2057 | return 1; |
| 2058 | } |
| 2059 | |
| 2060 | static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2061 | if (!hs->ssl->early_data_accepted) { |
| 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2066 | !CBB_add_u16(out, 0) || |
| 2067 | !CBB_flush(out)) { |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2071 | return 1; |
| 2072 | } |
| 2073 | |
| 2074 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2075 | // Key Share |
| 2076 | // |
| 2077 | // 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] | 2078 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2079 | static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2080 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2081 | if (hs->max_version < TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2082 | return 1; |
| 2083 | } |
| 2084 | |
| 2085 | CBB contents, kse_bytes; |
| 2086 | if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2087 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2088 | !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) { |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2092 | uint16_t group_id = hs->retry_group; |
| 2093 | if (hs->received_hello_retry_request) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2094 | // We received a HelloRetryRequest without a new curve, so there is no new |
| 2095 | // share to append. Leave |hs->key_share| as-is. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2096 | if (group_id == 0 && |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2097 | !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(), |
| 2098 | hs->key_share_bytes.size())) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2099 | return 0; |
| 2100 | } |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2101 | hs->key_share_bytes.Reset(); |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2102 | if (group_id == 0) { |
| 2103 | return CBB_flush(out); |
| 2104 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2105 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2106 | // Add a fake group. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2107 | if (ssl->ctx->grease_enabled && |
| 2108 | (!CBB_add_u16(&kse_bytes, |
| 2109 | ssl_get_grease_value(ssl, ssl_grease_group)) || |
| 2110 | !CBB_add_u16(&kse_bytes, 1 /* length */) || |
| 2111 | !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) { |
| 2112 | return 0; |
| 2113 | } |
| 2114 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2115 | // Predict the most preferred group. |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2116 | Span<const uint16_t> groups = tls1_get_grouplist(ssl); |
| 2117 | if (groups.size() == 0) { |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2118 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED); |
| 2119 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2120 | } |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2121 | |
| 2122 | group_id = groups[0]; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2123 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2124 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2125 | hs->key_share = SSLKeyShare::Create(group_id); |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2126 | CBB key_exchange; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2127 | if (!hs->key_share || |
| 2128 | !CBB_add_u16(&kse_bytes, group_id) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2129 | !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) || |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2130 | !hs->key_share->Offer(&key_exchange) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2131 | !CBB_flush(&kse_bytes)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2132 | return 0; |
| 2133 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2134 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2135 | // Save the contents of the extension to repeat it in the second ClientHello. |
| 2136 | if (!hs->received_hello_retry_request && |
| 2137 | !hs->key_share_bytes.CopyFrom( |
| 2138 | MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) { |
| 2139 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2140 | } |
| 2141 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2142 | return CBB_flush(out); |
| 2143 | } |
| 2144 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2145 | int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2146 | Array<uint8_t> *out_secret, |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2147 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2148 | CBS peer_key; |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2149 | uint16_t group_id; |
| 2150 | if (!CBS_get_u16(contents, &group_id) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2151 | !CBS_get_u16_length_prefixed(contents, &peer_key) || |
| 2152 | CBS_len(contents) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2153 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2154 | return 0; |
| 2155 | } |
| 2156 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2157 | if (hs->key_share->GroupID() != group_id) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2158 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2159 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
David Benjamin | 6b3ab72 | 2017-09-21 02:37:58 -0400 | [diff] [blame] | 2163 | if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2164 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2168 | hs->new_session->group_id = group_id; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2169 | hs->key_share.reset(); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2170 | return 1; |
| 2171 | } |
| 2172 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2173 | 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] | 2174 | Array<uint8_t> *out_secret, |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2175 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2176 | uint16_t group_id; |
| 2177 | CBS key_shares; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 2178 | if (!tls1_get_shared_group(hs, &group_id)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2179 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 2180 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
| 2184 | if (!CBS_get_u16_length_prefixed(contents, &key_shares) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2185 | CBS_len(contents) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2186 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2187 | return 0; |
| 2188 | } |
| 2189 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2190 | // Find the corresponding key share. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2191 | bool found = false; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2192 | CBS peer_key; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2193 | while (CBS_len(&key_shares) > 0) { |
| 2194 | uint16_t id; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2195 | CBS peer_key_tmp; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2196 | if (!CBS_get_u16(&key_shares, &id) || |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2197 | !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) { |
| 2198 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2199 | return 0; |
| 2200 | } |
| 2201 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2202 | if (id == group_id) { |
| 2203 | if (found) { |
| 2204 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE); |
| 2205 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2206 | return 0; |
| 2207 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2208 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2209 | found = true; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2210 | peer_key = peer_key_tmp; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2211 | // Continue parsing the structure to keep peers honest. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2212 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2213 | } |
| 2214 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2215 | if (!found) { |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2216 | *out_found = false; |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2217 | out_secret->Reset(); |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2218 | return 1; |
| 2219 | } |
| 2220 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2221 | // Compute the DH secret. |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2222 | Array<uint8_t> secret; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 2223 | ScopedCBB public_key; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2224 | UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id); |
David Benjamin | 6b3ab72 | 2017-09-21 02:37:58 -0400 | [diff] [blame] | 2225 | if (!key_share || |
| 2226 | !CBB_init(public_key.get(), 32) || |
| 2227 | !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) || |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 2228 | !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2229 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2230 | return 0; |
| 2231 | } |
| 2232 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2233 | *out_secret = std::move(secret); |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2234 | *out_found = true; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2235 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2236 | } |
| 2237 | |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 2238 | int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2239 | uint16_t group_id; |
| 2240 | CBB kse_bytes, public_key; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 2241 | if (!tls1_get_shared_group(hs, &group_id) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2242 | !CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2243 | !CBB_add_u16_length_prefixed(out, &kse_bytes) || |
| 2244 | !CBB_add_u16(&kse_bytes, group_id) || |
| 2245 | !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) || |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 2246 | !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(), |
| 2247 | hs->ecdh_public_key.size()) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2248 | !CBB_flush(out)) { |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame^] | 2252 | hs->ecdh_public_key.Reset(); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 2253 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2254 | hs->new_session->group_id = group_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2255 | return 1; |
| 2256 | } |
| 2257 | |
| 2258 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2259 | // Supported Versions |
| 2260 | // |
| 2261 | // 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] | 2262 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2263 | static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2264 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2265 | if (hs->max_version <= TLS1_2_VERSION) { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2266 | return 1; |
| 2267 | } |
| 2268 | |
| 2269 | CBB contents, versions; |
| 2270 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 2271 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2272 | !CBB_add_u8_length_prefixed(&contents, &versions)) { |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2276 | // Add a fake version. See draft-davidben-tls-grease-01. |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2277 | if (ssl->ctx->grease_enabled && |
| 2278 | !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) { |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 2282 | if (!ssl_add_supported_versions(hs, &versions) || |
| 2283 | !CBB_flush(out)) { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | return 1; |
| 2288 | } |
| 2289 | |
| 2290 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2291 | // Cookie |
| 2292 | // |
| 2293 | // 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] | 2294 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2295 | static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2296 | if (hs->cookie.size() == 0) { |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2297 | return 1; |
| 2298 | } |
| 2299 | |
| 2300 | CBB contents, cookie; |
| 2301 | if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) || |
| 2302 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2303 | !CBB_add_u16_length_prefixed(&contents, &cookie) || |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2304 | !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) || |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2305 | !CBB_flush(out)) { |
| 2306 | return 0; |
| 2307 | } |
| 2308 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2309 | // The cookie is no longer needed in memory. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2310 | hs->cookie.Reset(); |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2311 | return 1; |
| 2312 | } |
| 2313 | |
| 2314 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2315 | // Negotiated Groups |
| 2316 | // |
| 2317 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
| 2318 | // 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] | 2319 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2320 | static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2321 | SSL *const ssl = hs->ssl; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2322 | CBB contents, groups_bytes; |
| 2323 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2324 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2325 | !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2326 | return 0; |
| 2327 | } |
| 2328 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2329 | // Add a fake group. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2330 | if (ssl->ctx->grease_enabled && |
| 2331 | !CBB_add_u16(&groups_bytes, |
| 2332 | ssl_get_grease_value(ssl, ssl_grease_group))) { |
| 2333 | return 0; |
| 2334 | } |
| 2335 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2336 | for (uint16_t group : tls1_get_grouplist(ssl)) { |
| 2337 | if (!CBB_add_u16(&groups_bytes, group)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | return CBB_flush(out); |
| 2343 | } |
| 2344 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2345 | static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2346 | uint8_t *out_alert, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2347 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2348 | // This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2349 | // BigIP servers send it nonetheless, so do not enforce this. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2350 | return 1; |
| 2351 | } |
| 2352 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2353 | static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2354 | uint8_t *out_alert, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2355 | CBS *contents) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2356 | if (contents == NULL) { |
| 2357 | return 1; |
| 2358 | } |
| 2359 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2360 | CBS supported_group_list; |
| 2361 | if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2362 | CBS_len(&supported_group_list) == 0 || |
| 2363 | (CBS_len(&supported_group_list) & 1) != 0 || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2364 | CBS_len(contents) != 0) { |
| 2365 | return 0; |
| 2366 | } |
| 2367 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2368 | Array<uint16_t> groups; |
| 2369 | if (!groups.Init(CBS_len(&supported_group_list) / 2)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2370 | return 0; |
| 2371 | } |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2372 | for (size_t i = 0; i < groups.size(); i++) { |
| 2373 | if (!CBS_get_u16(&supported_group_list, &groups[i])) { |
| 2374 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2375 | return 0; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2376 | } |
| 2377 | } |
| 2378 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2379 | assert(CBS_len(&supported_group_list) == 0); |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2380 | hs->peer_supported_group_list = std::move(groups); |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2381 | return 1; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2384 | static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2385 | // Servers don't echo this extension. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2386 | return 1; |
| 2387 | } |
| 2388 | |
| 2389 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2390 | // kExtensions contains all the supported extensions. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2391 | static const struct tls_extension kExtensions[] = { |
| 2392 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 2393 | TLSEXT_TYPE_renegotiate, |
| 2394 | NULL, |
| 2395 | ext_ri_add_clienthello, |
| 2396 | ext_ri_parse_serverhello, |
| 2397 | ext_ri_parse_clienthello, |
| 2398 | ext_ri_add_serverhello, |
| 2399 | }, |
| 2400 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2401 | TLSEXT_TYPE_server_name, |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2402 | NULL, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2403 | ext_sni_add_clienthello, |
| 2404 | ext_sni_parse_serverhello, |
| 2405 | ext_sni_parse_clienthello, |
| 2406 | ext_sni_add_serverhello, |
| 2407 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2408 | { |
| 2409 | TLSEXT_TYPE_extended_master_secret, |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 2410 | NULL, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2411 | ext_ems_add_clienthello, |
| 2412 | ext_ems_parse_serverhello, |
| 2413 | ext_ems_parse_clienthello, |
| 2414 | ext_ems_add_serverhello, |
| 2415 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2416 | { |
| 2417 | TLSEXT_TYPE_session_ticket, |
| 2418 | NULL, |
| 2419 | ext_ticket_add_clienthello, |
| 2420 | ext_ticket_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2421 | // Ticket extension client parsing is handled in ssl_session.c |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2422 | ignore_parse_clienthello, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2423 | ext_ticket_add_serverhello, |
| 2424 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2425 | { |
| 2426 | TLSEXT_TYPE_signature_algorithms, |
| 2427 | NULL, |
| 2428 | ext_sigalgs_add_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2429 | forbid_parse_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2430 | ext_sigalgs_parse_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2431 | dont_add_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2432 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2433 | { |
| 2434 | TLSEXT_TYPE_status_request, |
David Benjamin | c253864 | 2017-01-13 16:32:05 -0500 | [diff] [blame] | 2435 | NULL, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2436 | ext_ocsp_add_clienthello, |
| 2437 | ext_ocsp_parse_serverhello, |
| 2438 | ext_ocsp_parse_clienthello, |
| 2439 | ext_ocsp_add_serverhello, |
| 2440 | }, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2441 | { |
| 2442 | TLSEXT_TYPE_next_proto_neg, |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 2443 | NULL, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2444 | ext_npn_add_clienthello, |
| 2445 | ext_npn_parse_serverhello, |
| 2446 | ext_npn_parse_clienthello, |
| 2447 | ext_npn_add_serverhello, |
| 2448 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 2449 | { |
| 2450 | TLSEXT_TYPE_certificate_timestamp, |
| 2451 | NULL, |
| 2452 | ext_sct_add_clienthello, |
| 2453 | ext_sct_parse_serverhello, |
| 2454 | ext_sct_parse_clienthello, |
| 2455 | ext_sct_add_serverhello, |
| 2456 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2457 | { |
| 2458 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
David Benjamin | 35598ae | 2016-11-16 15:38:27 +0900 | [diff] [blame] | 2459 | NULL, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2460 | ext_alpn_add_clienthello, |
| 2461 | ext_alpn_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2462 | // ALPN is negotiated late in |ssl_negotiate_alpn|. |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 2463 | ignore_parse_clienthello, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2464 | ext_alpn_add_serverhello, |
| 2465 | }, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2466 | { |
| 2467 | TLSEXT_TYPE_channel_id, |
| 2468 | ext_channel_id_init, |
| 2469 | ext_channel_id_add_clienthello, |
| 2470 | ext_channel_id_parse_serverhello, |
| 2471 | ext_channel_id_parse_clienthello, |
| 2472 | ext_channel_id_add_serverhello, |
| 2473 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 2474 | { |
| 2475 | TLSEXT_TYPE_srtp, |
| 2476 | ext_srtp_init, |
| 2477 | ext_srtp_add_clienthello, |
| 2478 | ext_srtp_parse_serverhello, |
| 2479 | ext_srtp_parse_clienthello, |
| 2480 | ext_srtp_add_serverhello, |
| 2481 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2482 | { |
| 2483 | TLSEXT_TYPE_ec_point_formats, |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2484 | NULL, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2485 | ext_ec_point_add_clienthello, |
| 2486 | ext_ec_point_parse_serverhello, |
| 2487 | ext_ec_point_parse_clienthello, |
| 2488 | ext_ec_point_add_serverhello, |
| 2489 | }, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2490 | { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2491 | TLSEXT_TYPE_key_share, |
| 2492 | NULL, |
| 2493 | ext_key_share_add_clienthello, |
| 2494 | forbid_parse_serverhello, |
| 2495 | ignore_parse_clienthello, |
| 2496 | dont_add_serverhello, |
| 2497 | }, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2498 | { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2499 | TLSEXT_TYPE_psk_key_exchange_modes, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2500 | NULL, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2501 | ext_psk_key_exchange_modes_add_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2502 | forbid_parse_serverhello, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2503 | ext_psk_key_exchange_modes_parse_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2504 | dont_add_serverhello, |
| 2505 | }, |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2506 | { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2507 | TLSEXT_TYPE_early_data, |
| 2508 | NULL, |
| 2509 | ext_early_data_add_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2510 | ext_early_data_parse_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2511 | ext_early_data_parse_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2512 | ext_early_data_add_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2513 | }, |
| 2514 | { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2515 | TLSEXT_TYPE_supported_versions, |
| 2516 | NULL, |
| 2517 | ext_supported_versions_add_clienthello, |
| 2518 | forbid_parse_serverhello, |
| 2519 | ignore_parse_clienthello, |
| 2520 | dont_add_serverhello, |
| 2521 | }, |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2522 | { |
| 2523 | TLSEXT_TYPE_cookie, |
| 2524 | NULL, |
| 2525 | ext_cookie_add_clienthello, |
| 2526 | forbid_parse_serverhello, |
| 2527 | ignore_parse_clienthello, |
| 2528 | dont_add_serverhello, |
| 2529 | }, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2530 | // The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2531 | // intolerant to the last extension being zero-length. See |
| 2532 | // https://crbug.com/363583. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2533 | { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2534 | TLSEXT_TYPE_supported_groups, |
David Benjamin | 43612b6 | 2016-10-07 00:41:50 -0400 | [diff] [blame] | 2535 | NULL, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2536 | ext_supported_groups_add_clienthello, |
| 2537 | ext_supported_groups_parse_serverhello, |
| 2538 | ext_supported_groups_parse_clienthello, |
| 2539 | ext_supported_groups_add_serverhello, |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2540 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2541 | }; |
| 2542 | |
| 2543 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2544 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 2545 | static_assert(kNumExtensions <= |
| 2546 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8, |
| 2547 | "too many extensions for sent bitset"); |
| 2548 | static_assert(kNumExtensions <= |
| 2549 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8, |
| 2550 | "too many extensions for received bitset"); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2551 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2552 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2553 | uint16_t value) { |
| 2554 | unsigned i; |
| 2555 | for (i = 0; i < kNumExtensions; i++) { |
| 2556 | if (kExtensions[i].value == value) { |
| 2557 | *out_index = i; |
| 2558 | return &kExtensions[i]; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | return NULL; |
| 2563 | } |
| 2564 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2565 | int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) { |
| 2566 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2567 | // Don't add extensions for SSLv3 unless doing secure renegotiation. |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 2568 | if (hs->client_version == SSL3_VERSION && |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2569 | !ssl->s3->send_connection_binding) { |
| 2570 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2571 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2572 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2573 | CBB extensions; |
| 2574 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2575 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2576 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2577 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2578 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2579 | hs->extensions.sent = 0; |
| 2580 | hs->custom_extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2581 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2582 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2583 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2584 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2585 | } |
| 2586 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2587 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2588 | uint16_t grease_ext1 = 0; |
| 2589 | if (ssl->ctx->grease_enabled) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2590 | // Add a fake empty extension. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2591 | grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1); |
| 2592 | if (!CBB_add_u16(&extensions, grease_ext1) || |
| 2593 | !CBB_add_u16(&extensions, 0 /* zero length */)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2594 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2595 | return 0; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2599 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2600 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2601 | if (!kExtensions[i].add_clienthello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2602 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2603 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2604 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2605 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2606 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2607 | if (CBB_len(&extensions) != len_before) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2608 | hs->extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2609 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2610 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2611 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2612 | if (!custom_ext_add_clienthello(hs, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2613 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2614 | return 0; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2617 | if (ssl->ctx->grease_enabled) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2618 | // Add a fake non-empty extension. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2619 | uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2); |
| 2620 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2621 | // The two fake extensions must not have the same value. GREASE values are |
| 2622 | // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different |
| 2623 | // one. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2624 | if (grease_ext1 == grease_ext2) { |
| 2625 | grease_ext2 ^= 0x1010; |
| 2626 | } |
| 2627 | |
| 2628 | if (!CBB_add_u16(&extensions, grease_ext2) || |
| 2629 | !CBB_add_u16(&extensions, 1 /* one byte length */) || |
| 2630 | !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2631 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2632 | return 0; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2636 | if (!SSL_is_dtls(ssl)) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2637 | size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2638 | header_len += 2 + CBB_len(&extensions) + psk_extension_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2639 | if (header_len > 0xff && header_len < 0x200) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2640 | // Add padding to workaround bugs in F5 terminators. See RFC 7685. |
| 2641 | // |
| 2642 | // NB: because this code works out the length of all existing extensions |
| 2643 | // it MUST always appear last. |
David Benjamin | 0a96859 | 2015-07-21 22:06:19 -0400 | [diff] [blame] | 2644 | size_t padding_len = 0x200 - header_len; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2645 | // Extensions take at least four bytes to encode. Always include at least |
| 2646 | // one byte of data if including the extension. WebSphere Application |
| 2647 | // Server 7.0 is intolerant to the last extension being zero-length. See |
| 2648 | // https://crbug.com/363583. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2649 | if (padding_len >= 4 + 1) { |
| 2650 | padding_len -= 4; |
| 2651 | } else { |
| 2652 | padding_len = 1; |
| 2653 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2654 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2655 | uint8_t *padding_bytes; |
| 2656 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2657 | !CBB_add_u16(&extensions, padding_len) || |
| 2658 | !CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2659 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2660 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2661 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2662 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2663 | OPENSSL_memset(padding_bytes, 0, padding_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2664 | } |
| 2665 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2666 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2667 | // The PSK extension must be last, including after the padding. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2668 | if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2669 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2670 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2671 | } |
| 2672 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2673 | // Discard empty extensions blocks. |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2674 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2675 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2678 | return CBB_flush(out); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2679 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2680 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2681 | int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) { |
| 2682 | SSL *const ssl = hs->ssl; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2683 | CBB extensions; |
| 2684 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2685 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2688 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 2689 | if (!(hs->extensions.received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2690 | // Don't send extensions that were not received. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2691 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2692 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2693 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2694 | if (!kExtensions[i].add_serverhello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2695 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2696 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2697 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2698 | } |
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 | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2701 | if (!custom_ext_add_serverhello(hs, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2702 | goto err; |
| 2703 | } |
| 2704 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2705 | // Discard empty extensions blocks before TLS 1.3. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2706 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION && |
| 2707 | CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2708 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2709 | } |
| 2710 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2711 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2712 | |
| 2713 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2714 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2715 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2716 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2717 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2718 | static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 2719 | const SSL_CLIENT_HELLO *client_hello, |
| 2720 | int *out_alert) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2721 | SSL *const ssl = hs->ssl; |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2722 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2723 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2724 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2728 | hs->extensions.received = 0; |
| 2729 | hs->custom_extensions.received = 0; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2730 | CBS extensions; |
| 2731 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 2732 | while (CBS_len(&extensions) != 0) { |
| 2733 | uint16_t type; |
| 2734 | CBS extension; |
| 2735 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2736 | // Decode the next extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2737 | if (!CBS_get_u16(&extensions, &type) || |
| 2738 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2739 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2740 | return 0; |
| 2741 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2742 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2743 | // RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 2744 | // ambiguous. Ignore all but the renegotiation_info extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2745 | if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
| 2746 | continue; |
| 2747 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2748 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2749 | unsigned ext_index; |
| 2750 | const struct tls_extension *const ext = |
| 2751 | tls_extension_find(&ext_index, type); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2752 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2753 | if (ext == NULL) { |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2754 | if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2755 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2756 | return 0; |
| 2757 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2758 | continue; |
| 2759 | } |
| 2760 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2761 | hs->extensions.received |= (1u << ext_index); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2762 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2763 | if (!ext->parse_clienthello(hs, &alert, &extension)) { |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2764 | *out_alert = alert; |
| 2765 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2766 | ERR_add_error_dataf("extension %u", (unsigned)type); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2767 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2768 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2769 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2770 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2771 | for (size_t i = 0; i < kNumExtensions; i++) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2772 | if (hs->extensions.received & (1u << i)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2773 | continue; |
| 2774 | } |
| 2775 | |
| 2776 | CBS *contents = NULL, fake_contents; |
| 2777 | static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 2778 | if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 2779 | ssl_client_cipher_list_contains_cipher(client_hello, |
| 2780 | SSL3_CK_SCSV & 0xffff)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2781 | // The renegotiation SCSV was received so pretend that we received a |
| 2782 | // renegotiation extension. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2783 | CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 2784 | sizeof(kFakeRenegotiateExtension)); |
| 2785 | contents = &fake_contents; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2786 | hs->extensions.received |= (1u << i); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2787 | } |
| 2788 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2789 | // Extension wasn't observed so call the callback with a NULL |
| 2790 | // parameter. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2791 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2792 | if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2793 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2794 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2795 | *out_alert = alert; |
| 2796 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2800 | return 1; |
| 2801 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2802 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2803 | int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 2804 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2805 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 2806 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2807 | if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2808 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2809 | return 0; |
| 2810 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2811 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2812 | if (ssl_check_clienthello_tlsext(hs) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2813 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2814 | return 0; |
| 2815 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2816 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2817 | return 1; |
| 2818 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2819 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2820 | static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs, |
| 2821 | int *out_alert) { |
| 2822 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2823 | // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2824 | if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2825 | return 1; |
| 2826 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2827 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2828 | // Decode the extensions block and check it is valid. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2829 | CBS extensions; |
| 2830 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2831 | !tls1_check_duplicate_extensions(&extensions)) { |
| 2832 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2833 | return 0; |
| 2834 | } |
| 2835 | |
| 2836 | uint32_t received = 0; |
| 2837 | while (CBS_len(&extensions) != 0) { |
| 2838 | uint16_t type; |
| 2839 | CBS extension; |
| 2840 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2841 | // Decode the next extension. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2842 | if (!CBS_get_u16(&extensions, &type) || |
| 2843 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2844 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2845 | return 0; |
| 2846 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2847 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2848 | unsigned ext_index; |
| 2849 | const struct tls_extension *const ext = |
| 2850 | tls_extension_find(&ext_index, type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2851 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2852 | if (ext == NULL) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2853 | hs->received_custom_extension = true; |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2854 | if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2855 | return 0; |
| 2856 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2857 | continue; |
| 2858 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2859 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 2860 | static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8, |
| 2861 | "too many bits"); |
David Benjamin | 5db7c9b | 2017-01-24 16:17:03 -0500 | [diff] [blame] | 2862 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2863 | if (!(hs->extensions.sent & (1u << ext_index)) && |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2864 | type != TLSEXT_TYPE_renegotiate) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2865 | // If the extension was never sent then it is illegal, except for the |
| 2866 | // renegotiation extension which, in SSL 3.0, is signaled via SCSV. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2867 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2868 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 2869 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2870 | return 0; |
| 2871 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2872 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2873 | received |= (1u << ext_index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2874 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2875 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2876 | if (!ext->parse_serverhello(hs, &alert, &extension)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2877 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2878 | ERR_add_error_dataf("extension %u", (unsigned)type); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2879 | *out_alert = alert; |
| 2880 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2881 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2882 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2883 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2884 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2885 | if (!(received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2886 | // Extension wasn't observed so call the callback with a NULL |
| 2887 | // parameter. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2888 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2889 | if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2890 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2891 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2892 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2893 | return 0; |
| 2894 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2895 | } |
| 2896 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2897 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2898 | return 1; |
| 2899 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2900 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2901 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) { |
| 2902 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2903 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2904 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2905 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2906 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2907 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2908 | ssl->ctx->tlsext_servername_arg); |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 2909 | } else if (ssl->session_ctx->tlsext_servername_callback != 0) { |
| 2910 | ret = ssl->session_ctx->tlsext_servername_callback( |
| 2911 | ssl, &al, ssl->session_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2912 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2913 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2914 | switch (ret) { |
| 2915 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2916 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2917 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2918 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2919 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2920 | hs->should_ack_sni = false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2921 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2922 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2923 | default: |
| 2924 | return 1; |
| 2925 | } |
| 2926 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2927 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2928 | int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) { |
| 2929 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 2930 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2931 | if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2932 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2933 | return 0; |
| 2934 | } |
| 2935 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2936 | return 1; |
| 2937 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2938 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2939 | static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx( |
| 2940 | uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx, |
| 2941 | HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) { |
| 2942 | size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2943 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2944 | // Check the MAC at the end of the ticket. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2945 | uint8_t mac[EVP_MAX_MD_SIZE]; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2946 | size_t mac_len = HMAC_size(hmac_ctx); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2947 | 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] | 2948 | // 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] | 2949 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2950 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2951 | HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len); |
| 2952 | HMAC_Final(hmac_ctx, mac, NULL); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2953 | int mac_ok = |
| 2954 | CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0; |
| 2955 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 2956 | mac_ok = 1; |
| 2957 | #endif |
| 2958 | if (!mac_ok) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2959 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2960 | } |
| 2961 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2962 | // Decrypt the session data. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2963 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 2964 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 2965 | mac_len; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 2966 | UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len)); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2967 | if (!plaintext) { |
| 2968 | return ssl_ticket_aead_error; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2969 | } |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2970 | size_t plaintext_len; |
| 2971 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2972 | OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2973 | plaintext_len = ciphertext_len; |
| 2974 | #else |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2975 | if (ciphertext_len >= INT_MAX) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2976 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2977 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2978 | int len1, len2; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2979 | if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2980 | (int)ciphertext_len) || |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2981 | !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2982 | ERR_clear_error(); |
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 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2985 | plaintext_len = (size_t)(len1) + len2; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 2986 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2987 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2988 | *out = plaintext.release(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2989 | *out_len = plaintext_len; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2990 | return ssl_ticket_aead_success; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2991 | } |
| 2992 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 2993 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2994 | 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] | 2995 | const uint8_t *ticket, size_t ticket_len) { |
| 2996 | assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 2997 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 2998 | ScopedHMAC_CTX hmac_ctx; |
| 2999 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 3000 | int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb( |
| 3001 | ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(), |
| 3002 | hmac_ctx.get(), 0 /* decrypt */); |
| 3003 | if (cb_ret < 0) { |
| 3004 | return ssl_ticket_aead_error; |
| 3005 | } else if (cb_ret == 0) { |
| 3006 | return ssl_ticket_aead_ignore_ticket; |
| 3007 | } else if (cb_ret == 2) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3008 | *out_renew_ticket = true; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3009 | } else { |
| 3010 | assert(cb_ret == 1); |
| 3011 | } |
| 3012 | return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(), |
| 3013 | hmac_ctx.get(), ticket, ticket_len); |
| 3014 | } |
| 3015 | |
| 3016 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys( |
| 3017 | SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket, |
| 3018 | size_t ticket_len) { |
| 3019 | assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3020 | SSL_CTX *ctx = ssl->session_ctx; |
| 3021 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3022 | // Rotate the ticket key if necessary. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3023 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 3024 | return ssl_ticket_aead_error; |
| 3025 | } |
| 3026 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3027 | // Pick the matching ticket key and decrypt. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3028 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3029 | ScopedHMAC_CTX hmac_ctx; |
| 3030 | { |
| 3031 | MutexReadLock lock(&ctx->lock); |
| 3032 | const tlsext_ticket_key *key; |
| 3033 | if (ctx->tlsext_ticket_key_current && |
| 3034 | !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket, |
| 3035 | SSL_TICKET_KEY_NAME_LEN)) { |
| 3036 | key = ctx->tlsext_ticket_key_current; |
| 3037 | } else if (ctx->tlsext_ticket_key_prev && |
| 3038 | !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket, |
| 3039 | SSL_TICKET_KEY_NAME_LEN)) { |
| 3040 | key = ctx->tlsext_ticket_key_prev; |
| 3041 | } else { |
| 3042 | return ssl_ticket_aead_ignore_ticket; |
| 3043 | } |
| 3044 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 3045 | if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key), |
| 3046 | tlsext_tick_md(), NULL) || |
| 3047 | !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL, |
| 3048 | key->aes_key, iv)) { |
| 3049 | return ssl_ticket_aead_error; |
| 3050 | } |
| 3051 | } |
| 3052 | return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(), |
| 3053 | hmac_ctx.get(), ticket, ticket_len); |
| 3054 | } |
| 3055 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3056 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3057 | 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] | 3058 | const uint8_t *ticket, size_t ticket_len) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3059 | uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3060 | if (plaintext == NULL) { |
| 3061 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 3062 | return ssl_ticket_aead_error; |
| 3063 | } |
| 3064 | |
| 3065 | size_t plaintext_len; |
| 3066 | const enum ssl_ticket_aead_result_t result = |
| 3067 | ssl->session_ctx->ticket_aead_method->open( |
| 3068 | ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len); |
| 3069 | |
| 3070 | if (result == ssl_ticket_aead_success) { |
| 3071 | *out = plaintext; |
| 3072 | plaintext = NULL; |
| 3073 | *out_len = plaintext_len; |
| 3074 | } |
| 3075 | |
| 3076 | OPENSSL_free(plaintext); |
| 3077 | return result; |
| 3078 | } |
| 3079 | |
| 3080 | enum ssl_ticket_aead_result_t ssl_process_ticket( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3081 | SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3082 | const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id, |
| 3083 | size_t session_id_len) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3084 | *out_renew_ticket = false; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3085 | out_session->reset(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3086 | |
| 3087 | if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) || |
| 3088 | session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3089 | return ssl_ticket_aead_ignore_ticket; |
| 3090 | } |
| 3091 | |
| 3092 | uint8_t *plaintext = NULL; |
| 3093 | size_t plaintext_len; |
| 3094 | enum ssl_ticket_aead_result_t result; |
| 3095 | if (ssl->session_ctx->ticket_aead_method != NULL) { |
| 3096 | result = ssl_decrypt_ticket_with_method( |
| 3097 | ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len); |
| 3098 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3099 | // Ensure there is room for the key name and the largest IV |
| 3100 | // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, |
| 3101 | // but the maximum IV length should be well under the minimum size for the |
| 3102 | // session material and HMAC. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3103 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 3104 | return ssl_ticket_aead_ignore_ticket; |
| 3105 | } |
| 3106 | if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) { |
| 3107 | result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len, |
| 3108 | out_renew_ticket, ticket, ticket_len); |
| 3109 | } else { |
| 3110 | result = ssl_decrypt_ticket_with_ticket_keys( |
| 3111 | ssl, &plaintext, &plaintext_len, ticket, ticket_len); |
| 3112 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | if (result != ssl_ticket_aead_success) { |
| 3116 | return result; |
| 3117 | } |
| 3118 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3119 | // Decode the session. |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3120 | UniquePtr<SSL_SESSION> session( |
| 3121 | SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx)); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3122 | OPENSSL_free(plaintext); |
| 3123 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3124 | if (!session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3125 | ERR_clear_error(); // Don't leave an error on the queue. |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3126 | return ssl_ticket_aead_ignore_ticket; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3127 | } |
| 3128 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3129 | // Copy the client's session ID into the new session, to denote the ticket has |
| 3130 | // been accepted. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 3131 | OPENSSL_memcpy(session->session_id, session_id, session_id_len); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3132 | session->session_id_length = session_id_len; |
| 3133 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3134 | *out_session = std::move(session); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3135 | return ssl_ticket_aead_success; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3136 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3137 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3138 | int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3139 | // Extension ignored for inappropriate versions |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3140 | if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3141 | return 1; |
| 3142 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 3143 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3144 | OPENSSL_free(hs->peer_sigalgs); |
| 3145 | hs->peer_sigalgs = NULL; |
| 3146 | hs->num_peer_sigalgs = 0; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3147 | |
| 3148 | size_t num_sigalgs = CBS_len(in_sigalgs); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3149 | if (num_sigalgs % 2 != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3150 | return 0; |
| 3151 | } |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3152 | num_sigalgs /= 2; |
| 3153 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3154 | // supported_signature_algorithms in the certificate request is |
| 3155 | // allowed to be empty. |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3156 | if (num_sigalgs == 0) { |
| 3157 | return 1; |
| 3158 | } |
| 3159 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3160 | // This multiplication doesn't overflow because sizeof(uint16_t) is two |
| 3161 | // and we just divided |num_sigalgs| by two. |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3162 | hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t)); |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3163 | if (hs->peer_sigalgs == NULL) { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3164 | return 0; |
| 3165 | } |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3166 | hs->num_peer_sigalgs = num_sigalgs; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3167 | |
| 3168 | CBS sigalgs; |
| 3169 | CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs)); |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3170 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 3171 | if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3172 | return 0; |
| 3173 | } |
| 3174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3175 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3176 | return 1; |
| 3177 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3178 | |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3179 | int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) { |
| 3180 | switch (EVP_PKEY_id(pkey)) { |
| 3181 | case EVP_PKEY_RSA: |
| 3182 | *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 3183 | return 1; |
| 3184 | case EVP_PKEY_EC: |
| 3185 | *out = SSL_SIGN_ECDSA_SHA1; |
| 3186 | return 1; |
| 3187 | default: |
| 3188 | return 0; |
| 3189 | } |
| 3190 | } |
| 3191 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3192 | int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) { |
| 3193 | SSL *const ssl = hs->ssl; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 3194 | CERT *cert = ssl->cert; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3195 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3196 | // Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 3197 | // handshake. |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3198 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 3199 | if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) { |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3200 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3201 | return 0; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3202 | } |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3203 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3204 | } |
| 3205 | |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 3206 | const uint16_t *sigalgs = cert->sigalgs; |
| 3207 | size_t num_sigalgs = cert->num_sigalgs; |
| 3208 | if (sigalgs == NULL) { |
| 3209 | sigalgs = kSignSignatureAlgorithms; |
| 3210 | num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3211 | } |
| 3212 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3213 | const uint16_t *peer_sigalgs = hs->peer_sigalgs; |
| 3214 | size_t num_peer_sigalgs = hs->num_peer_sigalgs; |
| 3215 | if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3216 | // If the client didn't specify any signature_algorithms extension then |
| 3217 | // we can assume that it supports SHA1. See |
| 3218 | // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3219 | static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 3220 | SSL_SIGN_ECDSA_SHA1}; |
| 3221 | peer_sigalgs = kDefaultPeerAlgorithms; |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3222 | num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms); |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3225 | for (size_t i = 0; i < num_sigalgs; i++) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3226 | uint16_t sigalg = sigalgs[i]; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3227 | // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be |
| 3228 | // negotiated. |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3229 | if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 || |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 3230 | !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3231 | continue; |
| 3232 | } |
| 3233 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3234 | for (size_t j = 0; j < num_peer_sigalgs; j++) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3235 | if (sigalg == peer_sigalgs[j]) { |
| 3236 | *out = sigalg; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3237 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3238 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3239 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3240 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3241 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3242 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3243 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3245 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3246 | int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3247 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3248 | // A Channel ID handshake message is structured to contain multiple |
| 3249 | // extensions, but the only one that can be present is Channel ID. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3250 | uint16_t extension_type; |
| 3251 | CBS channel_id = msg.body, extension; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3252 | if (!CBS_get_u16(&channel_id, &extension_type) || |
| 3253 | !CBS_get_u16_length_prefixed(&channel_id, &extension) || |
| 3254 | CBS_len(&channel_id) != 0 || |
| 3255 | extension_type != TLSEXT_TYPE_channel_id || |
| 3256 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 3257 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3258 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 3259 | return 0; |
| 3260 | } |
| 3261 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3262 | 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] | 3263 | if (!p256) { |
| 3264 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
| 3265 | return 0; |
| 3266 | } |
| 3267 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3268 | UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new()); |
| 3269 | UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3270 | if (!sig || !x || !y) { |
| 3271 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | const uint8_t *p = CBS_data(&extension); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3275 | if (BN_bin2bn(p + 0, 32, x.get()) == NULL || |
| 3276 | BN_bin2bn(p + 32, 32, y.get()) == NULL || |
| 3277 | BN_bin2bn(p + 64, 32, sig->r) == NULL || |
| 3278 | BN_bin2bn(p + 96, 32, sig->s) == NULL) { |
| 3279 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3280 | } |
| 3281 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3282 | UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 3283 | UniquePtr<EC_POINT> point(EC_POINT_new(p256.get())); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3284 | if (!key || !point || |
| 3285 | !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(), |
| 3286 | y.get(), nullptr) || |
| 3287 | !EC_KEY_set_group(key.get(), p256.get()) || |
| 3288 | !EC_KEY_set_public_key(key.get(), point.get())) { |
| 3289 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3293 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3294 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3295 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3296 | } |
| 3297 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3298 | 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] | 3299 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3300 | sig_ok = 1; |
| 3301 | #endif |
| 3302 | if (!sig_ok) { |
| 3303 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 3304 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 3305 | ssl->s3->tlsext_channel_id_valid = false; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3306 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 3309 | OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3310 | return 1; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3313 | int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) { |
| 3314 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3315 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3316 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3317 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3318 | return 0; |
| 3319 | } |
| 3320 | |
| 3321 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
| 3322 | if (ec_key == NULL) { |
| 3323 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3324 | return 0; |
| 3325 | } |
| 3326 | |
| 3327 | int ret = 0; |
| 3328 | BIGNUM *x = BN_new(); |
| 3329 | BIGNUM *y = BN_new(); |
| 3330 | ECDSA_SIG *sig = NULL; |
| 3331 | if (x == NULL || y == NULL || |
| 3332 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 3333 | EC_KEY_get0_public_key(ec_key), |
| 3334 | x, y, NULL)) { |
| 3335 | goto err; |
| 3336 | } |
| 3337 | |
| 3338 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
| 3339 | if (sig == NULL) { |
| 3340 | goto err; |
| 3341 | } |
| 3342 | |
| 3343 | CBB child; |
| 3344 | if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) || |
| 3345 | !CBB_add_u16_length_prefixed(cbb, &child) || |
| 3346 | !BN_bn2cbb_padded(&child, 32, x) || |
| 3347 | !BN_bn2cbb_padded(&child, 32, y) || |
| 3348 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 3349 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
| 3350 | !CBB_flush(cbb)) { |
| 3351 | goto err; |
| 3352 | } |
| 3353 | |
| 3354 | ret = 1; |
| 3355 | |
| 3356 | err: |
| 3357 | BN_free(x); |
| 3358 | BN_free(y); |
| 3359 | ECDSA_SIG_free(sig); |
| 3360 | return ret; |
| 3361 | } |
| 3362 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3363 | int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) { |
| 3364 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3365 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 3366 | uint8_t *msg; |
| 3367 | size_t msg_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3368 | if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3369 | ssl_cert_verify_channel_id)) { |
| 3370 | return 0; |
| 3371 | } |
| 3372 | SHA256(msg, msg_len, out); |
| 3373 | *out_len = SHA256_DIGEST_LENGTH; |
| 3374 | OPENSSL_free(msg); |
| 3375 | return 1; |
| 3376 | } |
| 3377 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3378 | SHA256_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3379 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3380 | SHA256_Init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3381 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3382 | SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3383 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3384 | if (ssl->session != NULL) { |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3385 | static const char kResumptionMagic[] = "Resumption"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3386 | SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3387 | if (ssl->session->original_handshake_hash_len == 0) { |
| 3388 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3389 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3390 | } |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3391 | SHA256_Update(&ctx, ssl->session->original_handshake_hash, |
| 3392 | ssl->session->original_handshake_hash_len); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3393 | } |
| 3394 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3395 | uint8_t hs_hash[EVP_MAX_MD_SIZE]; |
| 3396 | size_t hs_hash_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 3397 | if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) { |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3398 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3399 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3400 | SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3401 | SHA256_Final(out, &ctx); |
| 3402 | *out_len = SHA256_DIGEST_LENGTH; |
| 3403 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3404 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3405 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3406 | // tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 3407 | // hashes in |hs->new_session| so that Channel ID resumptions can sign that |
| 3408 | // data. |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3409 | int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) { |
| 3410 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3411 | // This function should never be called for a resumed session because the |
| 3412 | // handshake hashes that we wish to record are for the original, full |
| 3413 | // handshake. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3414 | if (ssl->session != NULL) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 3415 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3416 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3417 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3418 | static_assert( |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 3419 | sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE, |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3420 | "original_handshake_hash is too small"); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3421 | |
| 3422 | size_t digest_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 3423 | if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash, |
| 3424 | &digest_len)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 3425 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3426 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3427 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3428 | static_assert(EVP_MAX_MD_SIZE <= 0xff, |
| 3429 | "EVP_MAX_MD_SIZE does not fit in uint8_t"); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 3430 | hs->new_session->original_handshake_hash_len = (uint8_t)digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3431 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3432 | return 1; |
| 3433 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3434 | |
| 3435 | int ssl_do_channel_id_callback(SSL *ssl) { |
| 3436 | if (ssl->tlsext_channel_id_private != NULL || |
| 3437 | ssl->ctx->channel_id_cb == NULL) { |
| 3438 | return 1; |
| 3439 | } |
| 3440 | |
| 3441 | EVP_PKEY *key = NULL; |
| 3442 | ssl->ctx->channel_id_cb(ssl, &key); |
| 3443 | if (key == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3444 | // The caller should try again later. |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3445 | return 1; |
| 3446 | } |
| 3447 | |
| 3448 | int ret = SSL_set1_tls_channel_id(ssl, key); |
| 3449 | EVP_PKEY_free(key); |
| 3450 | return ret; |
| 3451 | } |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3452 | |
| 3453 | int ssl_is_sct_list_valid(const CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3454 | // Shallow parse the SCT list for sanity. By the RFC |
| 3455 | // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any |
| 3456 | // of the SCTs may be empty. |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3457 | CBS copy = *contents; |
| 3458 | CBS sct_list; |
| 3459 | if (!CBS_get_u16_length_prefixed(©, &sct_list) || |
| 3460 | CBS_len(©) != 0 || |
| 3461 | CBS_len(&sct_list) == 0) { |
| 3462 | return 0; |
| 3463 | } |
| 3464 | |
| 3465 | while (CBS_len(&sct_list) > 0) { |
| 3466 | CBS sct; |
| 3467 | if (!CBS_get_u16_length_prefixed(&sct_list, &sct) || |
| 3468 | CBS_len(&sct) == 0) { |
| 3469 | return 0; |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | return 1; |
| 3474 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3475 | |
| 3476 | } // namespace bssl |
| 3477 | |
| 3478 | using namespace bssl; |
| 3479 | |
| 3480 | int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello, |
| 3481 | uint16_t extension_type, |
| 3482 | const uint8_t **out_data, |
| 3483 | size_t *out_len) { |
| 3484 | CBS cbs; |
| 3485 | if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) { |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
| 3489 | *out_data = CBS_data(&cbs); |
| 3490 | *out_len = CBS_len(&cbs); |
| 3491 | return 1; |
| 3492 | } |
| 3493 | |
| 3494 | void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) { |
| 3495 | ctx->ed25519_enabled = !!enabled; |
| 3496 | } |
| 3497 | |
| 3498 | int SSL_extension_supported(unsigned extension_value) { |
| 3499 | uint32_t index; |
| 3500 | return extension_value == TLSEXT_TYPE_padding || |
| 3501 | tls_extension_find(&index, extension_value) != NULL; |
| 3502 | } |