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