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