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