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