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