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