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