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