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 | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 130 | static int ssl_check_clienthello_tlsext(SSL *ssl); |
| 131 | static int ssl_check_serverhello_tlsext(SSL *ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 133 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 134 | uint16_t u1 = *((const uint16_t *)p1); |
| 135 | uint16_t u2 = *((const uint16_t *)p2); |
| 136 | if (u1 < u2) { |
| 137 | return -1; |
| 138 | } else if (u1 > u2) { |
| 139 | return 1; |
| 140 | } else { |
| 141 | return 0; |
| 142 | } |
| 143 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 144 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 145 | /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 146 | * more than one extension of the same type in a ClientHello or ServerHello. |
| 147 | * This function does an initial scan over the extensions block to filter those |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 148 | * out. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 149 | static int tls1_check_duplicate_extensions(const CBS *cbs) { |
| 150 | CBS extensions = *cbs; |
| 151 | size_t num_extensions = 0, i = 0; |
| 152 | uint16_t *extension_types = NULL; |
| 153 | int ret = 0; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 154 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 155 | /* First pass: count the extensions. */ |
| 156 | while (CBS_len(&extensions) > 0) { |
| 157 | uint16_t type; |
| 158 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 159 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 160 | if (!CBS_get_u16(&extensions, &type) || |
| 161 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 162 | goto done; |
| 163 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | num_extensions++; |
| 166 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 167 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 168 | if (num_extensions == 0) { |
| 169 | return 1; |
| 170 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 171 | |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 172 | extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 173 | if (extension_types == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 174 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 175 | goto done; |
| 176 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 177 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 178 | /* Second pass: gather the extension types. */ |
| 179 | extensions = *cbs; |
| 180 | for (i = 0; i < num_extensions; i++) { |
| 181 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 182 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 183 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 184 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 185 | /* This should not happen. */ |
| 186 | goto done; |
| 187 | } |
| 188 | } |
| 189 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 190 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 191 | /* Sort the extensions and make sure there are no duplicates. */ |
| 192 | qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t); |
| 193 | for (i = 1; i < num_extensions; i++) { |
| 194 | if (extension_types[i - 1] == extension_types[i]) { |
| 195 | goto done; |
| 196 | } |
| 197 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | ret = 1; |
| 200 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 201 | done: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 202 | OPENSSL_free(extension_types); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 203 | return ret; |
| 204 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 205 | |
David Benjamin | d7573dc | 2016-07-20 19:05:22 +0200 | [diff] [blame] | 206 | int ssl_early_callback_init(SSL *ssl, struct ssl_early_callback_ctx *ctx, |
| 207 | const uint8_t *in, size_t in_len) { |
| 208 | memset(ctx, 0, sizeof(*ctx)); |
| 209 | ctx->ssl = ssl; |
| 210 | ctx->client_hello = in; |
| 211 | ctx->client_hello_len = in_len; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 212 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 213 | CBS client_hello, random, session_id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 214 | CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 215 | if (!CBS_get_u16(&client_hello, &ctx->version) || |
| 216 | !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) || |
| 217 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 218 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 219 | return 0; |
| 220 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 222 | ctx->random = CBS_data(&random); |
| 223 | ctx->random_len = CBS_len(&random); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 224 | ctx->session_id = CBS_data(&session_id); |
| 225 | ctx->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 227 | /* Skip past DTLS cookie */ |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 228 | if (SSL_is_dtls(ctx->ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 229 | CBS cookie; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 230 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 231 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 235 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 236 | CBS cipher_suites, compression_methods; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 237 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 238 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 || |
| 239 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 240 | CBS_len(&compression_methods) < 1) { |
| 241 | return 0; |
| 242 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 243 | |
| 244 | ctx->cipher_suites = CBS_data(&cipher_suites); |
| 245 | ctx->cipher_suites_len = CBS_len(&cipher_suites); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 246 | ctx->compression_methods = CBS_data(&compression_methods); |
| 247 | ctx->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 249 | /* If the ClientHello ends here then it's valid, but doesn't have any |
| 250 | * extensions. (E.g. SSLv3.) */ |
| 251 | if (CBS_len(&client_hello) == 0) { |
| 252 | ctx->extensions = NULL; |
| 253 | ctx->extensions_len = 0; |
| 254 | return 1; |
| 255 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | /* Extract extensions and check it is valid. */ |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 258 | CBS extensions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 259 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 260 | !tls1_check_duplicate_extensions(&extensions) || |
| 261 | CBS_len(&client_hello) != 0) { |
| 262 | return 0; |
| 263 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 264 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 265 | ctx->extensions = CBS_data(&extensions); |
| 266 | ctx->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | return 1; |
| 269 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 271 | int ssl_early_callback_get_extension(const struct ssl_early_callback_ctx *ctx, |
| 272 | CBS *out, uint16_t extension_type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 273 | CBS extensions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | CBS_init(&extensions, ctx->extensions, ctx->extensions_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 275 | while (CBS_len(&extensions) != 0) { |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 276 | /* Decode the next extension. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 277 | uint16_t type; |
| 278 | CBS extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 279 | if (!CBS_get_u16(&extensions, &type) || |
| 280 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 281 | return 0; |
| 282 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | if (type == extension_type) { |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 285 | *out = extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 286 | return 1; |
| 287 | } |
| 288 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 292 | |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 293 | int SSL_early_callback_ctx_extension_get( |
| 294 | const struct ssl_early_callback_ctx *ctx, uint16_t extension_type, |
| 295 | const uint8_t **out_data, size_t *out_len) { |
| 296 | CBS cbs; |
| 297 | if (!ssl_early_callback_get_extension(ctx, &cbs, extension_type)) { |
| 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 | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 310 | #if defined(BORINGSSL_ANDROID_SYSTEM) |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 311 | SSL_CURVE_SECP521R1, |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 312 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 313 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 315 | void tls1_get_grouplist(SSL *ssl, int get_peer_groups, |
| 316 | const uint16_t **out_group_ids, |
| 317 | size_t *out_group_ids_len) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 318 | if (get_peer_groups) { |
| 319 | /* Only clients send a supported group list, so this function is only |
| 320 | * called on the server. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 321 | assert(ssl->server); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 322 | *out_group_ids = ssl->s3->tmp.peer_supported_group_list; |
| 323 | *out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 324 | return; |
| 325 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 327 | *out_group_ids = ssl->supported_group_list; |
| 328 | *out_group_ids_len = ssl->supported_group_list_len; |
| 329 | if (!*out_group_ids) { |
| 330 | *out_group_ids = kDefaultGroups; |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 331 | *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 334 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 335 | int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) { |
| 336 | const uint16_t *groups, *peer_groups, *pref, *supp; |
| 337 | size_t groups_len, peer_groups_len, pref_len, supp_len, i, j; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 338 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 339 | /* Can't do anything on client side */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 340 | if (ssl->server == 0) { |
| 341 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 344 | tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len); |
| 345 | tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 346 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 347 | if (peer_groups_len == 0) { |
| 348 | /* Clients are not required to send a supported_groups extension. In this |
| 349 | * case, the server is free to pick any group it likes. See RFC 4492, |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 350 | * section 4, paragraph 3. |
| 351 | * |
| 352 | * However, in the interests of compatibility, we will skip ECDH if the |
| 353 | * client didn't send an extension because we can't be sure that they'll |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 354 | * support our favoured group. */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 355 | return 0; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 356 | } |
| 357 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 358 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 359 | pref = groups; |
| 360 | pref_len = groups_len; |
| 361 | supp = peer_groups; |
| 362 | supp_len = peer_groups_len; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 363 | } else { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 364 | pref = peer_groups; |
| 365 | pref_len = peer_groups_len; |
| 366 | supp = groups; |
| 367 | supp_len = groups_len; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | for (i = 0; i < pref_len; i++) { |
| 371 | for (j = 0; j < supp_len; j++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 372 | if (pref[i] == supp[j]) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 373 | *out_group_id = pref[i]; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 374 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 379 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 382 | 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] | 383 | const int *curves, size_t ncurves) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 384 | uint16_t *group_ids; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | size_t i; |
| 386 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 387 | group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
| 388 | if (group_ids == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | for (i = 0; i < ncurves; i++) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 393 | if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) { |
| 394 | OPENSSL_free(group_ids); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 395 | return 0; |
| 396 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 399 | OPENSSL_free(*out_group_ids); |
| 400 | *out_group_ids = group_ids; |
| 401 | *out_group_ids_len = ncurves; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 402 | |
| 403 | return 1; |
| 404 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 406 | /* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the |
| 407 | * TLS group ID and point format, respectively, for |ec|. It returns one on |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 408 | * success and zero on failure. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 409 | static int tls1_curve_params_from_ec_key(uint16_t *out_group_id, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | uint8_t *out_comp_id, EC_KEY *ec) { |
| 411 | int nid; |
| 412 | uint16_t id; |
| 413 | const EC_GROUP *grp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 415 | if (ec == NULL) { |
| 416 | return 0; |
| 417 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 419 | grp = EC_KEY_get0_group(ec); |
| 420 | if (grp == NULL) { |
| 421 | return 0; |
| 422 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 423 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 424 | /* Determine group ID */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 425 | nid = EC_GROUP_get_curve_name(grp); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 426 | if (!ssl_nid_to_group_id(&id, nid)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 427 | return 0; |
| 428 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 429 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 430 | /* Set the named group ID. Arbitrary explicit groups are not supported. */ |
| 431 | *out_group_id = id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 432 | |
| 433 | if (out_comp_id) { |
| 434 | if (EC_KEY_get0_public_key(ec) == NULL) { |
| 435 | return 0; |
| 436 | } |
| 437 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { |
| 438 | *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 439 | } else { |
| 440 | *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return 1; |
| 445 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 446 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 447 | /* tls1_check_group_id returns one if |group_id| is consistent with both our |
| 448 | * and the peer's group preferences. Note: if called as the client, only our |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 449 | * preferences are checked; the peer (the server) does not send preferences. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 450 | int tls1_check_group_id(SSL *ssl, uint16_t group_id) { |
| 451 | const uint16_t *groups; |
| 452 | size_t groups_len, i, get_peer_groups; |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 453 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | /* Check against our list, then the peer's list. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 455 | for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) { |
| 456 | if (get_peer_groups && !ssl->server) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 457 | /* Servers do not present a preference list so, if we are a client, only |
| 458 | * check our list. */ |
| 459 | continue; |
| 460 | } |
| 461 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 462 | tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len); |
| 463 | if (get_peer_groups && groups_len == 0) { |
| 464 | /* Clients are not required to send a supported_groups extension. In this |
| 465 | * case, the server is free to pick any group it likes. See RFC 4492, |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 466 | * section 4, paragraph 3. */ |
| 467 | continue; |
| 468 | } |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 469 | for (i = 0; i < groups_len; i++) { |
| 470 | if (groups[i] == group_id) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 475 | if (i == groups_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 476 | return 0; |
| 477 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 478 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 479 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 480 | return 1; |
| 481 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 482 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 483 | int tls1_check_ec_cert(SSL *ssl, X509 *x) { |
David Benjamin | 75ea5bb | 2016-07-08 17:43:29 -0700 | [diff] [blame] | 484 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 485 | /* In TLS 1.3, the ECDSA curve is negotiated via signature algorithms. */ |
| 486 | return 1; |
| 487 | } |
| 488 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 489 | EVP_PKEY *pkey = X509_get_pubkey(x); |
David Benjamin | 75ea5bb | 2016-07-08 17:43:29 -0700 | [diff] [blame] | 490 | if (pkey == NULL) { |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | int ret = 0; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 495 | uint16_t group_id; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | uint8_t comp_id; |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 497 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 498 | if (ec_key == NULL || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 499 | !tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) || |
| 500 | !tls1_check_group_id(ssl, group_id) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 501 | comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 502 | goto done; |
| 503 | } |
| 504 | |
| 505 | ret = 1; |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 506 | |
| 507 | done: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 508 | EVP_PKEY_free(pkey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 509 | return ret; |
| 510 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 511 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | /* List of supported signature algorithms and hashes. Should make this |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 513 | * customisable at some point, for now include everything we support. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 515 | static const uint16_t kDefaultSignatureAlgorithms[] = { |
| 516 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 517 | SSL_SIGN_ECDSA_SECP521R1_SHA512, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 519 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 520 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 522 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 523 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 525 | SSL_SIGN_RSA_PKCS1_SHA1, |
| 526 | SSL_SIGN_ECDSA_SHA1, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 527 | }; |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 528 | |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 529 | static const uint16_t kDefaultTLS13SignatureAlgorithms[] = { |
| 530 | SSL_SIGN_RSA_PSS_SHA512, |
| 531 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 532 | SSL_SIGN_ECDSA_SECP521R1_SHA512, |
| 533 | |
| 534 | SSL_SIGN_RSA_PSS_SHA384, |
| 535 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 536 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
| 537 | |
| 538 | SSL_SIGN_RSA_PSS_SHA256, |
| 539 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 540 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
| 541 | |
| 542 | SSL_SIGN_RSA_PKCS1_SHA1, |
| 543 | SSL_SIGN_ECDSA_SHA1, |
| 544 | }; |
| 545 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 546 | size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) { |
David Benjamin | 0e95015 | 2016-08-13 21:08:56 -0400 | [diff] [blame] | 547 | uint16_t min_version, max_version; |
| 548 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 549 | assert(0); /* This should never happen. */ |
| 550 | |
| 551 | /* Return an empty list. */ |
| 552 | ERR_clear_error(); |
| 553 | *psigs = NULL; |
| 554 | return 0; |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 555 | } |
| 556 | |
David Benjamin | 0e95015 | 2016-08-13 21:08:56 -0400 | [diff] [blame] | 557 | /* TODO(davidben): Once TLS 1.3 has finalized, probably just advertise the |
| 558 | * same algorithm list regardless, as long as no fallback is needed. Note this |
| 559 | * may require care due to lingering NSS servers affected by |
| 560 | * https://bugzilla.mozilla.org/show_bug.cgi?id=1119983 */ |
| 561 | if (max_version >= TLS1_3_VERSION) { |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 562 | *psigs = kDefaultTLS13SignatureAlgorithms; |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 563 | return OPENSSL_ARRAY_SIZE(kDefaultTLS13SignatureAlgorithms); |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 564 | } |
| 565 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 566 | *psigs = kDefaultSignatureAlgorithms; |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 567 | return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 568 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | |
David Benjamin | 887c300 | 2016-07-08 16:15:32 -0700 | [diff] [blame] | 570 | int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 571 | const uint16_t *sent_sigs; |
| 572 | size_t sent_sigslen, i; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 574 | /* Check signature matches a type we sent */ |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 575 | sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs); |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 576 | for (i = 0; i < sent_sigslen; i++) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 577 | if (sigalg == sent_sigs[i]) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
David Benjamin | 788be4a | 2015-10-30 17:50:57 -0400 | [diff] [blame] | 582 | if (i == sent_sigslen) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 583 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 584 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 585 | return 0; |
| 586 | } |
| 587 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 592 | * supported or doesn't appear in supported signature algorithms. Unlike |
| 593 | * ssl_cipher_get_disabled this applies to a specific session and not global |
| 594 | * settings. */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 595 | void ssl_set_client_disabled(SSL *ssl) { |
| 596 | CERT *c = ssl->cert; |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 597 | const uint16_t *sigalgs; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 598 | size_t i, sigalgslen; |
| 599 | int have_rsa = 0, have_ecdsa = 0; |
| 600 | c->mask_a = 0; |
| 601 | c->mask_k = 0; |
| 602 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 603 | /* Now go through all signature algorithms seeing if we support any for RSA, |
| 604 | * DSA, ECDSA. Do this for all versions not just TLS 1.2. */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 605 | sigalgslen = tls12_get_psigalgs(ssl, &sigalgs); |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 606 | for (i = 0; i < sigalgslen; i++) { |
| 607 | switch (sigalgs[i]) { |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 608 | case SSL_SIGN_RSA_PSS_SHA512: |
| 609 | case SSL_SIGN_RSA_PSS_SHA384: |
| 610 | case SSL_SIGN_RSA_PSS_SHA256: |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 611 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 612 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 613 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 614 | case SSL_SIGN_RSA_PKCS1_SHA1: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | have_rsa = 1; |
| 616 | break; |
| 617 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 618 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 619 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 620 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 621 | case SSL_SIGN_ECDSA_SHA1: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 622 | have_ecdsa = 1; |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* Disable auth if we don't include any appropriate signature algorithms. */ |
| 628 | if (!have_rsa) { |
| 629 | c->mask_a |= SSL_aRSA; |
| 630 | } |
| 631 | if (!have_ecdsa) { |
| 632 | c->mask_a |= SSL_aECDSA; |
| 633 | } |
| 634 | |
| 635 | /* with PSK there must be client callback set */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 636 | if (!ssl->psk_client_callback) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 637 | c->mask_a |= SSL_aPSK; |
| 638 | c->mask_k |= SSL_kPSK; |
| 639 | } |
| 640 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 641 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 642 | /* tls_extension represents a TLS extension that is handled internally. The |
| 643 | * |init| function is called for each handshake, before any other functions of |
| 644 | * the extension. Then the add and parse callbacks are called as needed. |
| 645 | * |
| 646 | * The parse callbacks receive a |CBS| that contains the contents of the |
| 647 | * extension (i.e. not including the type and length bytes). If an extension is |
| 648 | * not received then the parse callbacks will be called with a NULL CBS so that |
| 649 | * they can do any processing needed to handle the absence of an extension. |
| 650 | * |
| 651 | * The add callbacks receive a |CBB| to which the extension can be appended but |
| 652 | * the function is responsible for appending the type and length bytes too. |
| 653 | * |
| 654 | * All callbacks return one for success and zero for error. If a parse function |
| 655 | * returns zero then a fatal alert with value |*out_alert| will be sent. If |
| 656 | * |*out_alert| isn't set, then a |decode_error| alert will be sent. */ |
| 657 | struct tls_extension { |
| 658 | uint16_t value; |
| 659 | void (*init)(SSL *ssl); |
| 660 | |
| 661 | int (*add_clienthello)(SSL *ssl, CBB *out); |
| 662 | int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 663 | |
| 664 | int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 665 | int (*add_serverhello)(SSL *ssl, CBB *out); |
| 666 | }; |
| 667 | |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 668 | static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) { |
| 669 | if (contents != NULL) { |
| 670 | /* Servers MUST NOT send this extension. */ |
| 671 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 672 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) { |
| 680 | /* This extension from the client is handled elsewhere. */ |
| 681 | return 1; |
| 682 | } |
| 683 | |
| 684 | static int dont_add_serverhello(SSL *ssl, CBB *out) { |
| 685 | return 1; |
| 686 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 687 | |
| 688 | /* Server name indication (SNI). |
| 689 | * |
| 690 | * https://tools.ietf.org/html/rfc6066#section-3. */ |
| 691 | |
| 692 | static void ext_sni_init(SSL *ssl) { |
| 693 | ssl->s3->tmp.should_ack_sni = 0; |
| 694 | } |
| 695 | |
| 696 | static int ext_sni_add_clienthello(SSL *ssl, CBB *out) { |
| 697 | if (ssl->tlsext_hostname == NULL) { |
| 698 | return 1; |
| 699 | } |
| 700 | |
| 701 | CBB contents, server_name_list, name; |
| 702 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 703 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 704 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 705 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 706 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 707 | !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 708 | strlen(ssl->tlsext_hostname)) || |
| 709 | !CBB_flush(out)) { |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | return 1; |
| 714 | } |
| 715 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 716 | static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 717 | CBS *contents) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 718 | if (contents == NULL) { |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | if (CBS_len(contents) != 0) { |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | assert(ssl->tlsext_hostname != NULL); |
| 727 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 728 | if (ssl->session == NULL) { |
| 729 | assert(ssl->s3->new_session->tlsext_hostname == NULL); |
| 730 | ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname); |
| 731 | if (!ssl->s3->new_session->tlsext_hostname) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 732 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 733 | return 0; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | return 1; |
| 738 | } |
| 739 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 740 | static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 741 | CBS *contents) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 742 | if (contents == NULL) { |
| 743 | return 1; |
| 744 | } |
| 745 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 746 | CBS server_name_list, host_name; |
| 747 | uint8_t name_type; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 748 | if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 749 | !CBS_get_u8(&server_name_list, &name_type) || |
| 750 | /* Although the server_name extension was intended to be extensible to |
| 751 | * new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 752 | * different name types will cause an error. Further, RFC 4366 originally |
| 753 | * defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 754 | * adding new name types is no longer feasible. |
| 755 | * |
| 756 | * Act as if the extensibility does not exist to simplify parsing. */ |
| 757 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 758 | CBS_len(&server_name_list) != 0 || |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 759 | CBS_len(contents) != 0) { |
| 760 | return 0; |
| 761 | } |
| 762 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 763 | if (name_type != TLSEXT_NAMETYPE_host_name || |
| 764 | CBS_len(&host_name) == 0 || |
| 765 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 766 | CBS_contains_zero_byte(&host_name)) { |
| 767 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 768 | return 0; |
| 769 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 770 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 771 | /* TODO(davidben): SNI should be resolved before resumption. We have the |
| 772 | * early callback as a replacement, but we should fix the current callback |
| 773 | * and avoid the need for |SSL_CTX_set_session_id_context|. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 774 | if (ssl->session == NULL) { |
| 775 | assert(ssl->s3->new_session->tlsext_hostname == NULL); |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 776 | |
| 777 | /* Copy the hostname as a string. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 778 | if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) { |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 779 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 783 | ssl->s3->tmp.should_ack_sni = 1; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | return 1; |
| 787 | } |
| 788 | |
| 789 | static int ext_sni_add_serverhello(SSL *ssl, CBB *out) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 790 | if (ssl->session != NULL || |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 791 | !ssl->s3->tmp.should_ack_sni || |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 792 | ssl->s3->new_session->tlsext_hostname == NULL) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 797 | !CBB_add_u16(out, 0 /* length */)) { |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 805 | /* Renegotiation indication. |
| 806 | * |
| 807 | * https://tools.ietf.org/html/rfc5746 */ |
| 808 | |
| 809 | static int ext_ri_add_clienthello(SSL *ssl, CBB *out) { |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame^] | 810 | uint16_t min_version, max_version; |
| 811 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /* Renegotiation indication is not necessary in TLS 1.3. */ |
| 816 | if (min_version >= TLS1_3_VERSION) { |
| 817 | return 1; |
| 818 | } |
| 819 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 820 | CBB contents, prev_finished; |
| 821 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 822 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 823 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 824 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 825 | ssl->s3->previous_client_finished_len) || |
| 826 | !CBB_flush(out)) { |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 834 | CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 835 | if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 836 | return 0; |
| 837 | } |
| 838 | |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 839 | /* Servers may not switch between omitting the extension and supporting it. |
| 840 | * See RFC 5746, sections 3.5 and 4.2. */ |
| 841 | if (ssl->s3->initial_handshake_complete && |
| 842 | (contents != NULL) != ssl->s3->send_connection_binding) { |
| 843 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 844 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 845 | return 0; |
| 846 | } |
| 847 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 848 | if (contents == NULL) { |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 849 | /* 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] | 850 | * RI even on initial ServerHello because the client doesn't see any |
| 851 | * renegotiation during an attack. However this would mean we could not |
| 852 | * connect to any server which doesn't support RI. |
| 853 | * |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 854 | * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 855 | * practical terms every client sets it so it's just assumed here. */ |
| 856 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 860 | ssl->s3->previous_server_finished_len; |
| 861 | |
| 862 | /* Check for logic errors */ |
| 863 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 864 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
| 865 | |
| 866 | /* Parse out the extension contents. */ |
| 867 | CBS renegotiated_connection; |
| 868 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 869 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 870 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 871 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* Check that the extension matches. */ |
| 876 | if (CBS_len(&renegotiated_connection) != expected_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 877 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 878 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | const uint8_t *d = CBS_data(&renegotiated_connection); |
| 883 | if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 884 | ssl->s3->previous_client_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 885 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 886 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 887 | return 0; |
| 888 | } |
| 889 | d += ssl->s3->previous_client_finished_len; |
| 890 | |
| 891 | if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 892 | ssl->s3->previous_server_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 893 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 894 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 895 | return 0; |
| 896 | } |
| 897 | ssl->s3->send_connection_binding = 1; |
| 898 | |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 903 | CBS *contents) { |
| 904 | /* Renegotiation isn't supported as a server so this function should never be |
| 905 | * called after the initial handshake. */ |
| 906 | assert(!ssl->s3->initial_handshake_complete); |
| 907 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 908 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 909 | return 1; |
| 910 | } |
| 911 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 912 | if (contents == NULL) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 913 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | CBS renegotiated_connection; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 917 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 918 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 919 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /* Check that the extension matches */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 924 | if (!CBS_mem_equal(&renegotiated_connection, |
| 925 | ssl->s3->previous_client_finished, |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 926 | ssl->s3->previous_client_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 927 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 928 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | ssl->s3->send_connection_binding = 1; |
| 933 | |
| 934 | return 1; |
| 935 | } |
| 936 | |
| 937 | static int ext_ri_add_serverhello(SSL *ssl, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 938 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 939 | return 1; |
| 940 | } |
| 941 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 942 | CBB contents, prev_finished; |
| 943 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 944 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 945 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 946 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 947 | ssl->s3->previous_client_finished_len) || |
| 948 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished, |
| 949 | ssl->s3->previous_server_finished_len) || |
| 950 | !CBB_flush(out)) { |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | return 1; |
| 955 | } |
| 956 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 957 | |
| 958 | /* Extended Master Secret. |
| 959 | * |
David Benjamin | 43946d4 | 2016-02-01 08:42:19 -0500 | [diff] [blame] | 960 | * https://tools.ietf.org/html/rfc7627 */ |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 961 | |
| 962 | static void ext_ems_init(SSL *ssl) { |
| 963 | ssl->s3->tmp.extended_master_secret = 0; |
| 964 | } |
| 965 | |
| 966 | static int ext_ems_add_clienthello(SSL *ssl, CBB *out) { |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame^] | 967 | uint16_t min_version, max_version; |
| 968 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /* Extended master secret is not necessary in TLS 1.3. */ |
| 973 | if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) { |
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 | |
| 985 | static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 986 | CBS *contents) { |
| 987 | if (contents == NULL) { |
| 988 | return 1; |
| 989 | } |
| 990 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 991 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 992 | ssl->version == SSL3_VERSION) { |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | if (CBS_len(contents) != 0) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | ssl->s3->tmp.extended_master_secret = 1; |
| 1001 | return 1; |
| 1002 | } |
| 1003 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1004 | static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1005 | CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1006 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1007 | ssl->version == SSL3_VERSION) { |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
| 1011 | if (contents == NULL) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 1012 | return 1; |
| 1013 | } |
| 1014 | |
| 1015 | if (CBS_len(contents) != 0) { |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | ssl->s3->tmp.extended_master_secret = 1; |
| 1020 | return 1; |
| 1021 | } |
| 1022 | |
| 1023 | static int ext_ems_add_serverhello(SSL *ssl, CBB *out) { |
| 1024 | if (!ssl->s3->tmp.extended_master_secret) { |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
| 1028 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 1029 | !CBB_add_u16(out, 0 /* length */)) { |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | return 1; |
| 1034 | } |
| 1035 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1036 | |
| 1037 | /* Session tickets. |
| 1038 | * |
| 1039 | * https://tools.ietf.org/html/rfc5077 */ |
| 1040 | |
| 1041 | static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) { |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame^] | 1042 | uint16_t min_version, max_version; |
| 1043 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | /* TLS 1.3 uses a different ticket extension. */ |
| 1048 | if (min_version >= TLS1_3_VERSION || |
| 1049 | SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | const uint8_t *ticket_data = NULL; |
| 1054 | int ticket_len = 0; |
| 1055 | |
| 1056 | /* Renegotiation does not participate in session resumption. However, still |
| 1057 | * advertise the extension to avoid potentially breaking servers which carry |
| 1058 | * over the state from the previous handshake, such as OpenSSL servers |
| 1059 | * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */ |
| 1060 | if (!ssl->s3->initial_handshake_complete && |
| 1061 | ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1062 | ssl->session->tlsext_tick != NULL && |
| 1063 | /* Don't send TLS 1.3 session tickets in the ticket extension. */ |
| 1064 | ssl->method->version_from_wire(ssl->session->ssl_version) < |
| 1065 | TLS1_3_VERSION) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1066 | ticket_data = ssl->session->tlsext_tick; |
| 1067 | ticket_len = ssl->session->tlsext_ticklen; |
| 1068 | } |
| 1069 | |
| 1070 | CBB ticket; |
| 1071 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1072 | !CBB_add_u16_length_prefixed(out, &ticket) || |
| 1073 | !CBB_add_bytes(&ticket, ticket_data, ticket_len) || |
| 1074 | !CBB_flush(out)) { |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | return 1; |
| 1079 | } |
| 1080 | |
| 1081 | static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1082 | CBS *contents) { |
| 1083 | ssl->tlsext_ticket_expected = 0; |
| 1084 | |
| 1085 | if (contents == NULL) { |
| 1086 | return 1; |
| 1087 | } |
| 1088 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1089 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1093 | /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 1094 | * this function should never be called, even if the server tries to send the |
| 1095 | * extension. */ |
| 1096 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1097 | |
| 1098 | if (CBS_len(contents) != 0) { |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | ssl->tlsext_ticket_expected = 1; |
| 1103 | return 1; |
| 1104 | } |
| 1105 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1106 | static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) { |
| 1107 | if (!ssl->tlsext_ticket_expected) { |
| 1108 | return 1; |
| 1109 | } |
| 1110 | |
| 1111 | /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be |
| 1112 | * true. */ |
| 1113 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1114 | |
| 1115 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1116 | !CBB_add_u16(out, 0 /* length */)) { |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | return 1; |
| 1121 | } |
| 1122 | |
| 1123 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1124 | /* Signature Algorithms. |
| 1125 | * |
| 1126 | * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
| 1127 | |
| 1128 | static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) { |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 1129 | if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1130 | return 1; |
| 1131 | } |
| 1132 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1133 | const uint16_t *sigalgs_data; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1134 | const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data); |
| 1135 | |
| 1136 | CBB contents, sigalgs; |
| 1137 | if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) || |
| 1138 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1139 | !CBB_add_u16_length_prefixed(&contents, &sigalgs)) { |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | size_t i; |
| 1144 | for (i = 0; i < sigalgs_len; i++) { |
| 1145 | if (!CBB_add_u16(&sigalgs, sigalgs_data[i])) { |
| 1146 | return 0; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | if (!CBB_flush(out)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | return 1; |
| 1155 | } |
| 1156 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1157 | static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1158 | CBS *contents) { |
| 1159 | OPENSSL_free(ssl->cert->peer_sigalgs); |
| 1160 | ssl->cert->peer_sigalgs = NULL; |
| 1161 | ssl->cert->peer_sigalgslen = 0; |
| 1162 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1163 | if (contents == NULL) { |
| 1164 | return 1; |
| 1165 | } |
| 1166 | |
| 1167 | CBS supported_signature_algorithms; |
| 1168 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1169 | CBS_len(contents) != 0 || |
| 1170 | CBS_len(&supported_signature_algorithms) == 0 || |
| 1171 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | return 1; |
| 1176 | } |
| 1177 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1178 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1179 | /* OCSP Stapling. |
| 1180 | * |
| 1181 | * https://tools.ietf.org/html/rfc6066#section-8 */ |
| 1182 | |
| 1183 | static void ext_ocsp_init(SSL *ssl) { |
| 1184 | ssl->s3->tmp.certificate_status_expected = 0; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1185 | ssl->tlsext_status_type = -1; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) { |
| 1189 | if (!ssl->ocsp_stapling_enabled) { |
| 1190 | return 1; |
| 1191 | } |
| 1192 | |
| 1193 | CBB contents; |
| 1194 | if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1195 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1196 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1197 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1198 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1199 | !CBB_flush(out)) { |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1203 | ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1204 | return 1; |
| 1205 | } |
| 1206 | |
| 1207 | static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1208 | CBS *contents) { |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1209 | if (contents == NULL) { |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1213 | /* OCSP stapling is forbidden on a non-certificate cipher. */ |
| 1214 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1215 | return 0; |
| 1216 | } |
| 1217 | |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1218 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 1219 | if (CBS_len(contents) != 0) { |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | ssl->s3->tmp.certificate_status_expected = 1; |
| 1224 | return 1; |
| 1225 | } |
| 1226 | |
| 1227 | uint8_t status_type; |
| 1228 | CBS ocsp_response; |
| 1229 | if (!CBS_get_u8(contents, &status_type) || |
| 1230 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1231 | !CBS_get_u24_length_prefixed(contents, &ocsp_response) || |
| 1232 | CBS_len(&ocsp_response) == 0 || |
| 1233 | CBS_len(contents) != 0) { |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1237 | if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response, |
| 1238 | &ssl->s3->new_session->ocsp_response_length)) { |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1239 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1243 | return 1; |
| 1244 | } |
| 1245 | |
| 1246 | static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1247 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1248 | if (contents == NULL) { |
| 1249 | return 1; |
| 1250 | } |
| 1251 | |
| 1252 | uint8_t status_type; |
| 1253 | if (!CBS_get_u8(contents, &status_type)) { |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | /* We cannot decide whether OCSP stapling will occur yet because the correct |
| 1258 | * SSL_CTX might not have been selected. */ |
| 1259 | ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
| 1260 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1261 | return 1; |
| 1262 | } |
| 1263 | |
| 1264 | static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) { |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1265 | if (!ssl->s3->tmp.ocsp_stapling_requested || |
| 1266 | ssl->ctx->ocsp_response_length == 0 || |
| 1267 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1268 | return 1; |
| 1269 | } |
| 1270 | |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1271 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 1272 | /* The extension shouldn't be sent when resuming sessions. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1273 | if (ssl->session != NULL) { |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1274 | return 1; |
| 1275 | } |
| 1276 | |
| 1277 | ssl->s3->tmp.certificate_status_expected = 1; |
| 1278 | |
| 1279 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
| 1280 | CBB_add_u16(out, 0 /* length */); |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1283 | CBB body, ocsp_response; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1284 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1285 | CBB_add_u16_length_prefixed(out, &body) && |
| 1286 | CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) && |
| 1287 | CBB_add_u24_length_prefixed(&body, &ocsp_response) && |
| 1288 | CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 1289 | ssl->ctx->ocsp_response_length) && |
| 1290 | CBB_flush(out); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1294 | /* Next protocol negotiation. |
| 1295 | * |
| 1296 | * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */ |
| 1297 | |
| 1298 | static void ext_npn_init(SSL *ssl) { |
| 1299 | ssl->s3->next_proto_neg_seen = 0; |
| 1300 | } |
| 1301 | |
| 1302 | static int ext_npn_add_clienthello(SSL *ssl, CBB *out) { |
| 1303 | if (ssl->s3->initial_handshake_complete || |
| 1304 | ssl->ctx->next_proto_select_cb == NULL || |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 1305 | (ssl->options & SSL_OP_DISABLE_NPN) || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1306 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1307 | return 1; |
| 1308 | } |
| 1309 | |
| 1310 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1311 | !CBB_add_u16(out, 0 /* length */)) { |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | return 1; |
| 1316 | } |
| 1317 | |
| 1318 | static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1319 | CBS *contents) { |
| 1320 | if (contents == NULL) { |
| 1321 | return 1; |
| 1322 | } |
| 1323 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1324 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1328 | /* If any of these are false then we should never have sent the NPN |
| 1329 | * extension in the ClientHello and thus this function should never have been |
| 1330 | * called. */ |
| 1331 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1332 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1333 | assert(ssl->ctx->next_proto_select_cb != NULL); |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 1334 | assert(!(ssl->options & SSL_OP_DISABLE_NPN)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1335 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1336 | if (ssl->s3->alpn_selected != NULL) { |
| 1337 | /* NPN and ALPN may not be negotiated in the same connection. */ |
| 1338 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1339 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1343 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1344 | const size_t orig_len = CBS_len(contents); |
| 1345 | |
| 1346 | while (CBS_len(contents) != 0) { |
| 1347 | CBS proto; |
| 1348 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1349 | CBS_len(&proto) == 0) { |
| 1350 | return 0; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | uint8_t *selected; |
| 1355 | uint8_t selected_len; |
| 1356 | if (ssl->ctx->next_proto_select_cb( |
| 1357 | ssl, &selected, &selected_len, orig_contents, orig_len, |
| 1358 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1359 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1363 | OPENSSL_free(ssl->s3->next_proto_negotiated); |
| 1364 | ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 1365 | if (ssl->s3->next_proto_negotiated == NULL) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1366 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1370 | ssl->s3->next_proto_negotiated_len = selected_len; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1371 | ssl->s3->next_proto_neg_seen = 1; |
| 1372 | |
| 1373 | return 1; |
| 1374 | } |
| 1375 | |
| 1376 | static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1377 | CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1378 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1379 | return 1; |
| 1380 | } |
| 1381 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1382 | if (contents != NULL && CBS_len(contents) != 0) { |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | if (contents == NULL || |
| 1387 | ssl->s3->initial_handshake_complete || |
| 1388 | /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen |
| 1389 | * afterwards, parsing the ALPN extension will clear |
| 1390 | * |next_proto_neg_seen|. */ |
| 1391 | ssl->s3->alpn_selected != NULL || |
| 1392 | ssl->ctx->next_protos_advertised_cb == NULL || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1393 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1394 | return 1; |
| 1395 | } |
| 1396 | |
| 1397 | ssl->s3->next_proto_neg_seen = 1; |
| 1398 | return 1; |
| 1399 | } |
| 1400 | |
| 1401 | static int ext_npn_add_serverhello(SSL *ssl, CBB *out) { |
| 1402 | /* |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1403 | * parsed. */ |
| 1404 | if (!ssl->s3->next_proto_neg_seen) { |
| 1405 | return 1; |
| 1406 | } |
| 1407 | |
| 1408 | const uint8_t *npa; |
| 1409 | unsigned npa_len; |
| 1410 | |
| 1411 | if (ssl->ctx->next_protos_advertised_cb( |
| 1412 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1413 | SSL_TLSEXT_ERR_OK) { |
| 1414 | ssl->s3->next_proto_neg_seen = 0; |
| 1415 | return 1; |
| 1416 | } |
| 1417 | |
| 1418 | CBB contents; |
| 1419 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1420 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1421 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1422 | !CBB_flush(out)) { |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | return 1; |
| 1427 | } |
| 1428 | |
| 1429 | |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1430 | /* Signed certificate timestamps. |
| 1431 | * |
| 1432 | * https://tools.ietf.org/html/rfc6962#section-3.3.1 */ |
| 1433 | |
| 1434 | static int ext_sct_add_clienthello(SSL *ssl, CBB *out) { |
| 1435 | if (!ssl->signed_cert_timestamps_enabled) { |
| 1436 | return 1; |
| 1437 | } |
| 1438 | |
| 1439 | if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1440 | !CBB_add_u16(out, 0 /* length */)) { |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
| 1447 | static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1448 | CBS *contents) { |
| 1449 | if (contents == NULL) { |
| 1450 | return 1; |
| 1451 | } |
| 1452 | |
| 1453 | /* If this is false then we should never have sent the SCT extension in the |
| 1454 | * ClientHello and thus this function should never have been called. */ |
| 1455 | assert(ssl->signed_cert_timestamps_enabled); |
| 1456 | |
| 1457 | if (CBS_len(contents) == 0) { |
| 1458 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* Session resumption uses the original session information. */ |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1463 | if (!ssl->s3->session_reused && |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1464 | !CBS_stow( |
| 1465 | contents, |
| 1466 | &ssl->s3->new_session->tlsext_signed_cert_timestamp_list, |
| 1467 | &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1468 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | return 1; |
| 1473 | } |
| 1474 | |
| 1475 | static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1476 | CBS *contents) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1477 | return contents == NULL || CBS_len(contents) == 0; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | static int ext_sct_add_serverhello(SSL *ssl, CBB *out) { |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 1481 | /* The extension shouldn't be sent when resuming sessions. */ |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1482 | if (ssl->s3->session_reused || |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 1483 | ssl->ctx->signed_cert_timestamp_list_length == 0) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1484 | return 1; |
| 1485 | } |
| 1486 | |
| 1487 | CBB contents; |
| 1488 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1489 | CBB_add_u16_length_prefixed(out, &contents) && |
| 1490 | CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list, |
| 1491 | ssl->ctx->signed_cert_timestamp_list_length) && |
| 1492 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1496 | /* Application-level Protocol Negotiation. |
| 1497 | * |
| 1498 | * https://tools.ietf.org/html/rfc7301 */ |
| 1499 | |
| 1500 | static void ext_alpn_init(SSL *ssl) { |
| 1501 | OPENSSL_free(ssl->s3->alpn_selected); |
| 1502 | ssl->s3->alpn_selected = NULL; |
| 1503 | } |
| 1504 | |
| 1505 | static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) { |
| 1506 | if (ssl->alpn_client_proto_list == NULL || |
| 1507 | ssl->s3->initial_handshake_complete) { |
| 1508 | return 1; |
| 1509 | } |
| 1510 | |
| 1511 | CBB contents, proto_list; |
| 1512 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1513 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1514 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1515 | !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1516 | ssl->alpn_client_proto_list_len) || |
| 1517 | !CBB_flush(out)) { |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
| 1521 | return 1; |
| 1522 | } |
| 1523 | |
| 1524 | static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1525 | CBS *contents) { |
| 1526 | if (contents == NULL) { |
| 1527 | return 1; |
| 1528 | } |
| 1529 | |
| 1530 | assert(!ssl->s3->initial_handshake_complete); |
| 1531 | assert(ssl->alpn_client_proto_list != NULL); |
| 1532 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1533 | if (ssl->s3->next_proto_neg_seen) { |
| 1534 | /* NPN and ALPN may not be negotiated in the same connection. */ |
| 1535 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1536 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1540 | /* The extension data consists of a ProtocolNameList which must have |
| 1541 | * exactly one ProtocolName. Each of these is length-prefixed. */ |
| 1542 | CBS protocol_name_list, protocol_name; |
| 1543 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1544 | CBS_len(contents) != 0 || |
| 1545 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1546 | /* Empty protocol names are forbidden. */ |
| 1547 | CBS_len(&protocol_name) == 0 || |
| 1548 | CBS_len(&protocol_name_list) != 0) { |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
David Benjamin | 3e51757 | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1552 | /* Check that the protcol name is one of the ones we advertised. */ |
| 1553 | int protocol_ok = 0; |
| 1554 | CBS client_protocol_name_list, client_protocol_name; |
| 1555 | CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list, |
| 1556 | ssl->alpn_client_proto_list_len); |
| 1557 | while (CBS_len(&client_protocol_name_list) > 0) { |
| 1558 | if (!CBS_get_u8_length_prefixed(&client_protocol_name_list, |
| 1559 | &client_protocol_name)) { |
| 1560 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) && |
| 1565 | memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name), |
| 1566 | CBS_len(&protocol_name)) == 0) { |
| 1567 | protocol_ok = 1; |
| 1568 | break; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | if (!protocol_ok) { |
| 1573 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1574 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1578 | if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected, |
| 1579 | &ssl->s3->alpn_selected_len)) { |
| 1580 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1581 | return 0; |
| 1582 | } |
| 1583 | |
| 1584 | return 1; |
| 1585 | } |
| 1586 | |
| 1587 | static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1588 | CBS *contents) { |
| 1589 | if (contents == NULL) { |
| 1590 | return 1; |
| 1591 | } |
| 1592 | |
| 1593 | if (ssl->ctx->alpn_select_cb == NULL || |
| 1594 | ssl->s3->initial_handshake_complete) { |
| 1595 | return 1; |
| 1596 | } |
| 1597 | |
| 1598 | /* ALPN takes precedence over NPN. */ |
| 1599 | ssl->s3->next_proto_neg_seen = 0; |
| 1600 | |
| 1601 | CBS protocol_name_list; |
| 1602 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1603 | CBS_len(contents) != 0 || |
| 1604 | CBS_len(&protocol_name_list) < 2) { |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | /* Validate the protocol list. */ |
| 1609 | CBS protocol_name_list_copy = protocol_name_list; |
| 1610 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1611 | CBS protocol_name; |
| 1612 | |
| 1613 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
| 1614 | /* Empty protocol names are forbidden. */ |
| 1615 | CBS_len(&protocol_name) == 0) { |
| 1616 | return 0; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | const uint8_t *selected; |
| 1621 | uint8_t selected_len; |
| 1622 | if (ssl->ctx->alpn_select_cb( |
| 1623 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1624 | CBS_len(&protocol_name_list), |
| 1625 | ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
| 1626 | OPENSSL_free(ssl->s3->alpn_selected); |
| 1627 | ssl->s3->alpn_selected = BUF_memdup(selected, selected_len); |
| 1628 | if (ssl->s3->alpn_selected == NULL) { |
| 1629 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1630 | return 0; |
| 1631 | } |
| 1632 | ssl->s3->alpn_selected_len = selected_len; |
| 1633 | } |
| 1634 | |
| 1635 | return 1; |
| 1636 | } |
| 1637 | |
| 1638 | static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) { |
| 1639 | if (ssl->s3->alpn_selected == NULL) { |
| 1640 | return 1; |
| 1641 | } |
| 1642 | |
| 1643 | CBB contents, proto_list, proto; |
| 1644 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1645 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1646 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1647 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1648 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected, |
| 1649 | ssl->s3->alpn_selected_len) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1650 | !CBB_flush(out)) { |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | return 1; |
| 1655 | } |
| 1656 | |
| 1657 | |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1658 | /* Channel ID. |
| 1659 | * |
| 1660 | * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */ |
| 1661 | |
| 1662 | static void ext_channel_id_init(SSL *ssl) { |
| 1663 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1664 | } |
| 1665 | |
| 1666 | static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) { |
| 1667 | if (!ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1668 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1669 | return 1; |
| 1670 | } |
| 1671 | |
| 1672 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1673 | !CBB_add_u16(out, 0 /* length */)) { |
| 1674 | return 0; |
| 1675 | } |
| 1676 | |
| 1677 | return 1; |
| 1678 | } |
| 1679 | |
| 1680 | static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1681 | CBS *contents) { |
| 1682 | if (contents == NULL) { |
| 1683 | return 1; |
| 1684 | } |
| 1685 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1686 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1690 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1691 | assert(ssl->tlsext_channel_id_enabled); |
| 1692 | |
| 1693 | if (CBS_len(contents) != 0) { |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | ssl->s3->tlsext_channel_id_valid = 1; |
| 1698 | return 1; |
| 1699 | } |
| 1700 | |
| 1701 | static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1702 | CBS *contents) { |
| 1703 | if (contents == NULL || |
| 1704 | !ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1705 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1706 | return 1; |
| 1707 | } |
| 1708 | |
| 1709 | if (CBS_len(contents) != 0) { |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | ssl->s3->tlsext_channel_id_valid = 1; |
| 1714 | return 1; |
| 1715 | } |
| 1716 | |
| 1717 | static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1718 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1719 | return 1; |
| 1720 | } |
| 1721 | |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1722 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 1723 | return 1; |
| 1724 | } |
| 1725 | |
| 1726 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1727 | !CBB_add_u16(out, 0 /* length */)) { |
| 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | return 1; |
| 1732 | } |
| 1733 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1734 | |
| 1735 | /* Secure Real-time Transport Protocol (SRTP) extension. |
| 1736 | * |
| 1737 | * https://tools.ietf.org/html/rfc5764 */ |
| 1738 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1739 | |
| 1740 | static void ext_srtp_init(SSL *ssl) { |
| 1741 | ssl->srtp_profile = NULL; |
| 1742 | } |
| 1743 | |
| 1744 | static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) { |
| 1745 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1746 | if (profiles == NULL) { |
| 1747 | return 1; |
| 1748 | } |
| 1749 | const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles); |
| 1750 | if (num_profiles == 0) { |
| 1751 | return 1; |
| 1752 | } |
| 1753 | |
| 1754 | CBB contents, profile_ids; |
| 1755 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1756 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1757 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | size_t i; |
| 1762 | for (i = 0; i < num_profiles; i++) { |
| 1763 | if (!CBB_add_u16(&profile_ids, |
| 1764 | sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) { |
| 1765 | return 0; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1770 | !CBB_flush(out)) { |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | return 1; |
| 1775 | } |
| 1776 | |
| 1777 | static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1778 | CBS *contents) { |
| 1779 | if (contents == NULL) { |
| 1780 | return 1; |
| 1781 | } |
| 1782 | |
| 1783 | /* The extension consists of a u16-prefixed profile ID list containing a |
| 1784 | * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1785 | * |
| 1786 | * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */ |
| 1787 | CBS profile_ids, srtp_mki; |
| 1788 | uint16_t profile_id; |
| 1789 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1790 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1791 | CBS_len(&profile_ids) != 0 || |
| 1792 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1793 | CBS_len(contents) != 0) { |
| 1794 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | if (CBS_len(&srtp_mki) != 0) { |
| 1799 | /* Must be no MKI, since we never offer one. */ |
| 1800 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1801 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1806 | |
| 1807 | /* Check to see if the server gave us something we support (and presumably |
| 1808 | * offered). */ |
| 1809 | size_t i; |
| 1810 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) { |
| 1811 | const SRTP_PROTECTION_PROFILE *profile = |
| 1812 | sk_SRTP_PROTECTION_PROFILE_value(profiles, i); |
| 1813 | |
| 1814 | if (profile->id == profile_id) { |
| 1815 | ssl->srtp_profile = profile; |
| 1816 | return 1; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1821 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1826 | CBS *contents) { |
| 1827 | if (contents == NULL) { |
| 1828 | return 1; |
| 1829 | } |
| 1830 | |
| 1831 | CBS profile_ids, srtp_mki; |
| 1832 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1833 | CBS_len(&profile_ids) < 2 || |
| 1834 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1835 | CBS_len(contents) != 0) { |
| 1836 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1837 | return 0; |
| 1838 | } |
| 1839 | /* Discard the MKI value for now. */ |
| 1840 | |
| 1841 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1842 | SSL_get_srtp_profiles(ssl); |
| 1843 | |
| 1844 | /* Pick the server's most preferred profile. */ |
| 1845 | size_t i; |
| 1846 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) { |
| 1847 | const SRTP_PROTECTION_PROFILE *server_profile = |
| 1848 | sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i); |
| 1849 | |
| 1850 | CBS profile_ids_tmp; |
| 1851 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1852 | |
| 1853 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1854 | uint16_t profile_id; |
| 1855 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | if (server_profile->id == profile_id) { |
| 1860 | ssl->srtp_profile = server_profile; |
| 1861 | return 1; |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | return 1; |
| 1867 | } |
| 1868 | |
| 1869 | static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) { |
| 1870 | if (ssl->srtp_profile == NULL) { |
| 1871 | return 1; |
| 1872 | } |
| 1873 | |
| 1874 | CBB contents, profile_ids; |
| 1875 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1876 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1877 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1878 | !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1879 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1880 | !CBB_flush(out)) { |
| 1881 | return 0; |
| 1882 | } |
| 1883 | |
| 1884 | return 1; |
| 1885 | } |
| 1886 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1887 | |
| 1888 | /* EC point formats. |
| 1889 | * |
| 1890 | * https://tools.ietf.org/html/rfc4492#section-5.1.2 */ |
| 1891 | |
| 1892 | static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1893 | if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) { |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl); |
| 1898 | |
| 1899 | size_t i; |
| 1900 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { |
| 1901 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i); |
| 1902 | |
| 1903 | const uint32_t alg_k = cipher->algorithm_mkey; |
| 1904 | const uint32_t alg_a = cipher->algorithm_auth; |
| 1905 | if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) { |
| 1906 | return 1; |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1913 | static int ext_ec_point_add_extension(SSL *ssl, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1914 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1915 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1916 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1917 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1918 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1919 | !CBB_flush(out)) { |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
| 1923 | return 1; |
| 1924 | } |
| 1925 | |
| 1926 | static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) { |
| 1927 | if (!ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 1928 | return 1; |
| 1929 | } |
| 1930 | |
| 1931 | return ext_ec_point_add_extension(ssl, out); |
| 1932 | } |
| 1933 | |
| 1934 | static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1935 | CBS *contents) { |
| 1936 | if (contents == NULL) { |
| 1937 | return 1; |
| 1938 | } |
| 1939 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1940 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1944 | CBS ec_point_format_list; |
| 1945 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1946 | CBS_len(contents) != 0) { |
| 1947 | return 0; |
| 1948 | } |
| 1949 | |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1950 | /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1951 | * point format. */ |
| 1952 | if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1953 | CBS_len(&ec_point_format_list)) == NULL) { |
| 1954 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | return 1; |
| 1959 | } |
| 1960 | |
| 1961 | static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1962 | CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1963 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1964 | return 1; |
| 1965 | } |
| 1966 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1967 | return ext_ec_point_parse_serverhello(ssl, out_alert, contents); |
| 1968 | } |
| 1969 | |
| 1970 | static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1971 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1972 | return 1; |
| 1973 | } |
| 1974 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1975 | const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1976 | const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1977 | const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1978 | |
| 1979 | if (!using_ecc) { |
| 1980 | return 1; |
| 1981 | } |
| 1982 | |
| 1983 | return ext_ec_point_add_extension(ssl, out); |
| 1984 | } |
| 1985 | |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 1986 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1987 | /* Draft Version Extension */ |
| 1988 | |
| 1989 | static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) { |
| 1990 | uint16_t min_version, max_version; |
| 1991 | if (!ssl_get_version_range(ssl, &min_version, &max_version) || |
Steven Valdez | 3a28755 | 2016-07-19 05:10:50 -0400 | [diff] [blame] | 1992 | max_version < TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1993 | return 1; |
| 1994 | } |
| 1995 | |
| 1996 | CBB contents; |
| 1997 | if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) || |
| 1998 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1999 | !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) { |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | return CBB_flush(out); |
| 2004 | } |
| 2005 | |
| 2006 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2007 | /* Pre Shared Key |
| 2008 | * |
| 2009 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */ |
| 2010 | |
| 2011 | static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) { |
| 2012 | uint16_t min_version, max_version; |
| 2013 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 2014 | return 0; |
| 2015 | } |
| 2016 | |
| 2017 | if (max_version < TLS1_3_VERSION || ssl->session == NULL || |
| 2018 | ssl->method->version_from_wire(ssl->session->ssl_version) < |
| 2019 | TLS1_3_VERSION) { |
| 2020 | return 1; |
| 2021 | } |
| 2022 | |
| 2023 | CBB contents, identities, identity; |
| 2024 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 2025 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2026 | !CBB_add_u16_length_prefixed(&contents, &identities) || |
| 2027 | !CBB_add_u16_length_prefixed(&identities, &identity) || |
| 2028 | !CBB_add_bytes(&identity, ssl->session->tlsext_tick, |
| 2029 | ssl->session->tlsext_ticklen)) { |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
| 2033 | return CBB_flush(out); |
| 2034 | } |
| 2035 | |
| 2036 | int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 2037 | CBS *contents) { |
| 2038 | uint16_t psk_id; |
| 2039 | if (!CBS_get_u16(contents, &psk_id) || |
| 2040 | CBS_len(contents) != 0) { |
| 2041 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | if (psk_id != 0) { |
| 2046 | *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 2047 | return 0; |
| 2048 | } |
| 2049 | |
| 2050 | return 1; |
| 2051 | } |
| 2052 | |
| 2053 | int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl, |
| 2054 | SSL_SESSION **out_session, |
| 2055 | uint8_t *out_alert, |
| 2056 | CBS *contents) { |
| 2057 | CBS identities, identity; |
| 2058 | if (!CBS_get_u16_length_prefixed(contents, &identities) || |
| 2059 | !CBS_get_u16_length_prefixed(&identities, &identity) || |
| 2060 | CBS_len(contents) != 0) { |
| 2061 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2062 | return 0; |
| 2063 | } |
| 2064 | |
| 2065 | /* TLS 1.3 session tickets are renewed separately as part of the |
| 2066 | * NewSessionTicket. */ |
| 2067 | int renew; |
| 2068 | return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity), |
| 2069 | CBS_len(&identity), NULL, 0); |
| 2070 | } |
| 2071 | |
| 2072 | int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) { |
| 2073 | if (!ssl->s3->session_reused) { |
| 2074 | return 1; |
| 2075 | } |
| 2076 | |
| 2077 | CBB contents; |
| 2078 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 2079 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2080 | /* We only consider the first identity for resumption */ |
| 2081 | !CBB_add_u16(&contents, 0) || |
| 2082 | !CBB_flush(out)) { |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | return 1; |
| 2087 | } |
| 2088 | |
| 2089 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2090 | /* Key Share |
| 2091 | * |
| 2092 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */ |
| 2093 | |
| 2094 | static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) { |
| 2095 | uint16_t min_version, max_version; |
| 2096 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 2101 | return 1; |
| 2102 | } |
| 2103 | |
| 2104 | CBB contents, kse_bytes; |
| 2105 | if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2106 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2107 | !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) { |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
| 2111 | const uint16_t *groups; |
| 2112 | size_t groups_len; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2113 | if (ssl->s3->hs->retry_group) { |
| 2114 | /* Append the new key share to the old list. */ |
| 2115 | if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes, |
| 2116 | ssl->s3->hs->key_share_bytes_len)) { |
| 2117 | return 0; |
| 2118 | } |
| 2119 | OPENSSL_free(ssl->s3->hs->key_share_bytes); |
| 2120 | ssl->s3->hs->key_share_bytes = NULL; |
| 2121 | |
| 2122 | groups = &ssl->s3->hs->retry_group; |
| 2123 | groups_len = 1; |
| 2124 | } else { |
| 2125 | tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len); |
| 2126 | /* Only send the top two preferred key shares. */ |
| 2127 | if (groups_len > 2) { |
| 2128 | groups_len = 2; |
| 2129 | } |
| 2130 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2131 | |
| 2132 | ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX)); |
| 2133 | if (ssl->s3->hs->groups == NULL) { |
| 2134 | return 0; |
| 2135 | } |
| 2136 | memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX)); |
| 2137 | ssl->s3->hs->groups_len = groups_len; |
| 2138 | |
| 2139 | for (size_t i = 0; i < groups_len; i++) { |
| 2140 | if (!CBB_add_u16(&kse_bytes, groups[i])) { |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | CBB key_exchange; |
| 2145 | if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) || |
| 2146 | !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) || |
| 2147 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) || |
| 2148 | !CBB_flush(&kse_bytes)) { |
| 2149 | return 0; |
| 2150 | } |
| 2151 | } |
| 2152 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2153 | if (!ssl->s3->hs->retry_group) { |
| 2154 | /* Save the contents of the extension to repeat it in the second |
| 2155 | * ClientHello. */ |
| 2156 | ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes); |
| 2157 | ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), |
| 2158 | CBB_len(&kse_bytes)); |
| 2159 | if (ssl->s3->hs->key_share_bytes == NULL) { |
| 2160 | return 0; |
| 2161 | } |
| 2162 | } |
| 2163 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2164 | return CBB_flush(out); |
| 2165 | } |
| 2166 | |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2167 | int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret, |
| 2168 | size_t *out_secret_len, |
| 2169 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2170 | CBS peer_key; |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2171 | uint16_t group_id; |
| 2172 | if (!CBS_get_u16(contents, &group_id) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2173 | !CBS_get_u16_length_prefixed(contents, &peer_key) || |
| 2174 | CBS_len(contents) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2175 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | SSL_ECDH_CTX *group_ctx = NULL; |
| 2180 | for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) { |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2181 | if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2182 | group_ctx = &ssl->s3->hs->groups[i]; |
| 2183 | break; |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | if (group_ctx == NULL) { |
| 2188 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2189 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
| 2193 | if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert, |
| 2194 | CBS_data(&peer_key), CBS_len(&peer_key))) { |
| 2195 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2199 | ssl->s3->new_session->key_exchange_info = group_id; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2200 | ssl_handshake_clear_groups(ssl->s3->hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2201 | return 1; |
| 2202 | } |
| 2203 | |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2204 | int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found, |
| 2205 | uint8_t **out_secret, |
| 2206 | size_t *out_secret_len, |
| 2207 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2208 | uint16_t group_id; |
| 2209 | CBS key_shares; |
| 2210 | if (!tls1_get_shared_group(ssl, &group_id) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2211 | !CBS_get_u16_length_prefixed(contents, &key_shares) || |
| 2212 | CBS_len(contents) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2213 | return 0; |
| 2214 | } |
| 2215 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2216 | *out_found = 0; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2217 | while (CBS_len(&key_shares) > 0) { |
| 2218 | uint16_t id; |
| 2219 | CBS peer_key; |
| 2220 | if (!CBS_get_u16(&key_shares, &id) || |
| 2221 | !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) { |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2225 | if (id != group_id || *out_found) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2226 | continue; |
| 2227 | } |
| 2228 | |
| 2229 | SSL_ECDH_CTX group; |
| 2230 | memset(&group, 0, sizeof(SSL_ECDH_CTX)); |
| 2231 | CBB public_key; |
| 2232 | if (!CBB_init(&public_key, 0) || |
| 2233 | !SSL_ECDH_CTX_init(&group, group_id) || |
| 2234 | !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len, |
| 2235 | out_alert, CBS_data(&peer_key), |
| 2236 | CBS_len(&peer_key)) || |
| 2237 | !CBB_finish(&public_key, &ssl->s3->hs->public_key, |
| 2238 | &ssl->s3->hs->public_key_len)) { |
| 2239 | SSL_ECDH_CTX_cleanup(&group); |
| 2240 | CBB_cleanup(&public_key); |
| 2241 | return 0; |
| 2242 | } |
| 2243 | SSL_ECDH_CTX_cleanup(&group); |
| 2244 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2245 | *out_found = 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2246 | } |
| 2247 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2248 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2249 | } |
| 2250 | |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2251 | int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2252 | if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) { |
| 2253 | return 1; |
| 2254 | } |
| 2255 | |
| 2256 | uint16_t group_id; |
| 2257 | CBB kse_bytes, public_key; |
| 2258 | if (!tls1_get_shared_group(ssl, &group_id) || |
| 2259 | !CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2260 | !CBB_add_u16_length_prefixed(out, &kse_bytes) || |
| 2261 | !CBB_add_u16(&kse_bytes, group_id) || |
| 2262 | !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) || |
| 2263 | !CBB_add_bytes(&public_key, ssl->s3->hs->public_key, |
| 2264 | ssl->s3->hs->public_key_len) || |
| 2265 | !CBB_flush(out)) { |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2269 | ssl->s3->new_session->key_exchange_info = group_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2270 | return 1; |
| 2271 | } |
| 2272 | |
| 2273 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2274 | /* Negotiated Groups |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2275 | * |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2276 | * https://tools.ietf.org/html/rfc4492#section-5.1.2 |
| 2277 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */ |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2278 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2279 | static void ext_supported_groups_init(SSL *ssl) { |
| 2280 | OPENSSL_free(ssl->s3->tmp.peer_supported_group_list); |
| 2281 | ssl->s3->tmp.peer_supported_group_list = NULL; |
| 2282 | ssl->s3->tmp.peer_supported_group_list_len = 0; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2285 | static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2286 | if (!ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 2287 | return 1; |
| 2288 | } |
| 2289 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2290 | CBB contents, groups_bytes; |
| 2291 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2292 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2293 | !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
| 2296 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2297 | const uint16_t *groups; |
| 2298 | size_t groups_len; |
| 2299 | tls1_get_grouplist(ssl, 0, &groups, &groups_len); |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2300 | |
| 2301 | size_t i; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2302 | for (i = 0; i < groups_len; i++) { |
| 2303 | if (!CBB_add_u16(&groups_bytes, groups[i])) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2304 | return 0; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | return CBB_flush(out); |
| 2309 | } |
| 2310 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2311 | static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 2312 | CBS *contents) { |
David Benjamin | 4ac2dc4 | 2016-08-12 15:50:48 -0400 | [diff] [blame] | 2313 | /* This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2314 | * BigIP servers send it nonetheless, so do not enforce this. */ |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2315 | return 1; |
| 2316 | } |
| 2317 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2318 | static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 2319 | CBS *contents) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2320 | if (contents == NULL) { |
| 2321 | return 1; |
| 2322 | } |
| 2323 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2324 | CBS supported_group_list; |
| 2325 | if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2326 | CBS_len(&supported_group_list) == 0 || |
| 2327 | (CBS_len(&supported_group_list) & 1) != 0 || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2328 | CBS_len(contents) != 0) { |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2332 | ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc( |
| 2333 | CBS_len(&supported_group_list)); |
| 2334 | if (ssl->s3->tmp.peer_supported_group_list == NULL) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2335 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2339 | const size_t num_groups = CBS_len(&supported_group_list) / 2; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2340 | size_t i; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2341 | for (i = 0; i < num_groups; i++) { |
| 2342 | if (!CBS_get_u16(&supported_group_list, |
| 2343 | &ssl->s3->tmp.peer_supported_group_list[i])) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2344 | goto err; |
| 2345 | } |
| 2346 | } |
| 2347 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2348 | assert(CBS_len(&supported_group_list) == 0); |
| 2349 | ssl->s3->tmp.peer_supported_group_list_len = num_groups; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2350 | |
| 2351 | return 1; |
| 2352 | |
| 2353 | err: |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2354 | OPENSSL_free(ssl->s3->tmp.peer_supported_group_list); |
| 2355 | ssl->s3->tmp.peer_supported_group_list = NULL; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2356 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2357 | return 0; |
| 2358 | } |
| 2359 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2360 | static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2361 | /* Servers don't echo this extension. */ |
| 2362 | return 1; |
| 2363 | } |
| 2364 | |
| 2365 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2366 | /* kExtensions contains all the supported extensions. */ |
| 2367 | static const struct tls_extension kExtensions[] = { |
| 2368 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 2369 | TLSEXT_TYPE_renegotiate, |
| 2370 | NULL, |
| 2371 | ext_ri_add_clienthello, |
| 2372 | ext_ri_parse_serverhello, |
| 2373 | ext_ri_parse_clienthello, |
| 2374 | ext_ri_add_serverhello, |
| 2375 | }, |
| 2376 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2377 | TLSEXT_TYPE_server_name, |
| 2378 | ext_sni_init, |
| 2379 | ext_sni_add_clienthello, |
| 2380 | ext_sni_parse_serverhello, |
| 2381 | ext_sni_parse_clienthello, |
| 2382 | ext_sni_add_serverhello, |
| 2383 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2384 | { |
| 2385 | TLSEXT_TYPE_extended_master_secret, |
| 2386 | ext_ems_init, |
| 2387 | ext_ems_add_clienthello, |
| 2388 | ext_ems_parse_serverhello, |
| 2389 | ext_ems_parse_clienthello, |
| 2390 | ext_ems_add_serverhello, |
| 2391 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2392 | { |
| 2393 | TLSEXT_TYPE_session_ticket, |
| 2394 | NULL, |
| 2395 | ext_ticket_add_clienthello, |
| 2396 | ext_ticket_parse_serverhello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2397 | /* Ticket extension client parsing is handled in ssl_session.c */ |
| 2398 | ignore_parse_clienthello, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2399 | ext_ticket_add_serverhello, |
| 2400 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2401 | { |
| 2402 | TLSEXT_TYPE_signature_algorithms, |
| 2403 | NULL, |
| 2404 | ext_sigalgs_add_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2405 | forbid_parse_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2406 | ext_sigalgs_parse_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2407 | dont_add_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2408 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2409 | { |
| 2410 | TLSEXT_TYPE_status_request, |
| 2411 | ext_ocsp_init, |
| 2412 | ext_ocsp_add_clienthello, |
| 2413 | ext_ocsp_parse_serverhello, |
| 2414 | ext_ocsp_parse_clienthello, |
| 2415 | ext_ocsp_add_serverhello, |
| 2416 | }, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2417 | { |
| 2418 | TLSEXT_TYPE_next_proto_neg, |
| 2419 | ext_npn_init, |
| 2420 | ext_npn_add_clienthello, |
| 2421 | ext_npn_parse_serverhello, |
| 2422 | ext_npn_parse_clienthello, |
| 2423 | ext_npn_add_serverhello, |
| 2424 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 2425 | { |
| 2426 | TLSEXT_TYPE_certificate_timestamp, |
| 2427 | NULL, |
| 2428 | ext_sct_add_clienthello, |
| 2429 | ext_sct_parse_serverhello, |
| 2430 | ext_sct_parse_clienthello, |
| 2431 | ext_sct_add_serverhello, |
| 2432 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2433 | { |
| 2434 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
| 2435 | ext_alpn_init, |
| 2436 | ext_alpn_add_clienthello, |
| 2437 | ext_alpn_parse_serverhello, |
| 2438 | ext_alpn_parse_clienthello, |
| 2439 | ext_alpn_add_serverhello, |
| 2440 | }, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2441 | { |
| 2442 | TLSEXT_TYPE_channel_id, |
| 2443 | ext_channel_id_init, |
| 2444 | ext_channel_id_add_clienthello, |
| 2445 | ext_channel_id_parse_serverhello, |
| 2446 | ext_channel_id_parse_clienthello, |
| 2447 | ext_channel_id_add_serverhello, |
| 2448 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 2449 | { |
| 2450 | TLSEXT_TYPE_srtp, |
| 2451 | ext_srtp_init, |
| 2452 | ext_srtp_add_clienthello, |
| 2453 | ext_srtp_parse_serverhello, |
| 2454 | ext_srtp_parse_clienthello, |
| 2455 | ext_srtp_add_serverhello, |
| 2456 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2457 | { |
| 2458 | TLSEXT_TYPE_ec_point_formats, |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2459 | NULL, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2460 | ext_ec_point_add_clienthello, |
| 2461 | ext_ec_point_parse_serverhello, |
| 2462 | ext_ec_point_parse_clienthello, |
| 2463 | ext_ec_point_add_serverhello, |
| 2464 | }, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2465 | { |
| 2466 | TLSEXT_TYPE_draft_version, |
| 2467 | NULL, |
| 2468 | ext_draft_version_add_clienthello, |
| 2469 | forbid_parse_serverhello, |
| 2470 | ignore_parse_clienthello, |
| 2471 | dont_add_serverhello, |
| 2472 | }, |
| 2473 | { |
| 2474 | TLSEXT_TYPE_key_share, |
| 2475 | NULL, |
| 2476 | ext_key_share_add_clienthello, |
| 2477 | forbid_parse_serverhello, |
| 2478 | ignore_parse_clienthello, |
| 2479 | dont_add_serverhello, |
| 2480 | }, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2481 | { |
| 2482 | TLSEXT_TYPE_pre_shared_key, |
| 2483 | NULL, |
| 2484 | ext_pre_shared_key_add_clienthello, |
| 2485 | forbid_parse_serverhello, |
| 2486 | ignore_parse_clienthello, |
| 2487 | dont_add_serverhello, |
| 2488 | }, |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2489 | /* The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2490 | * intolerant to the last extension being zero-length. See |
| 2491 | * https://crbug.com/363583. */ |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2492 | { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2493 | TLSEXT_TYPE_supported_groups, |
| 2494 | ext_supported_groups_init, |
| 2495 | ext_supported_groups_add_clienthello, |
| 2496 | ext_supported_groups_parse_serverhello, |
| 2497 | ext_supported_groups_parse_clienthello, |
| 2498 | ext_supported_groups_add_serverhello, |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2499 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2500 | }; |
| 2501 | |
| 2502 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2503 | |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2504 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 2505 | sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8, |
David Benjamin | 7ca4b42 | 2015-07-13 16:43:47 -0400 | [diff] [blame] | 2506 | too_many_extensions_for_sent_bitset); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2507 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 2508 | sizeof(((SSL *)NULL)->s3->tmp.extensions.received) * |
| 2509 | 8, |
David Benjamin | 7ca4b42 | 2015-07-13 16:43:47 -0400 | [diff] [blame] | 2510 | too_many_extensions_for_received_bitset); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2511 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2512 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2513 | uint16_t value) { |
| 2514 | unsigned i; |
| 2515 | for (i = 0; i < kNumExtensions; i++) { |
| 2516 | if (kExtensions[i].value == value) { |
| 2517 | *out_index = i; |
| 2518 | return &kExtensions[i]; |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | return NULL; |
| 2523 | } |
| 2524 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2525 | int SSL_extension_supported(unsigned extension_value) { |
| 2526 | uint32_t index; |
| 2527 | return extension_value == TLSEXT_TYPE_padding || |
| 2528 | tls_extension_find(&index, extension_value) != NULL; |
| 2529 | } |
| 2530 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2531 | int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2532 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2533 | if (ssl->client_version == SSL3_VERSION && |
| 2534 | !ssl->s3->send_connection_binding) { |
| 2535 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2536 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2537 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2538 | CBB extensions; |
| 2539 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2540 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2541 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2542 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2543 | ssl->s3->tmp.extensions.sent = 0; |
| 2544 | ssl->s3->tmp.custom_extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2545 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2546 | size_t i; |
| 2547 | for (i = 0; i < kNumExtensions; i++) { |
| 2548 | if (kExtensions[i].init != NULL) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2549 | kExtensions[i].init(ssl); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2550 | } |
| 2551 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2552 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2553 | for (i = 0; i < kNumExtensions; i++) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2554 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2555 | if (!kExtensions[i].add_clienthello(ssl, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2556 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 2557 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2558 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2559 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2560 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2561 | if (CBB_len(&extensions) != len_before) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2562 | ssl->s3->tmp.extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2563 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2564 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2565 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2566 | if (!custom_ext_add_clienthello(ssl, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2567 | goto err; |
| 2568 | } |
| 2569 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2570 | if (!SSL_is_dtls(ssl)) { |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2571 | header_len += 2 + CBB_len(&extensions); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2572 | if (header_len > 0xff && header_len < 0x200) { |
Adam Langley | 10a1a9d | 2015-10-21 14:49:23 -0700 | [diff] [blame] | 2573 | /* Add padding to workaround bugs in F5 terminators. See RFC 7685. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2574 | * |
| 2575 | * NB: because this code works out the length of all existing extensions |
| 2576 | * it MUST always appear last. */ |
David Benjamin | 0a96859 | 2015-07-21 22:06:19 -0400 | [diff] [blame] | 2577 | size_t padding_len = 0x200 - header_len; |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2578 | /* Extensions take at least four bytes to encode. Always include at least |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2579 | * one byte of data if including the extension. WebSphere Application |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2580 | * Server 7.0 is intolerant to the last extension being zero-length. See |
| 2581 | * https://crbug.com/363583. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2582 | if (padding_len >= 4 + 1) { |
| 2583 | padding_len -= 4; |
| 2584 | } else { |
| 2585 | padding_len = 1; |
| 2586 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2587 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2588 | uint8_t *padding_bytes; |
| 2589 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2590 | !CBB_add_u16(&extensions, padding_len) || |
| 2591 | !CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
| 2592 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2593 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2594 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2595 | memset(padding_bytes, 0, padding_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2596 | } |
| 2597 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2598 | |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2599 | /* Discard empty extensions blocks. */ |
| 2600 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2601 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2604 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2605 | |
| 2606 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2607 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2608 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2609 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2610 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2611 | int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2612 | CBB extensions; |
| 2613 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2614 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | unsigned i; |
| 2618 | for (i = 0; i < kNumExtensions; i++) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2619 | if (!(ssl->s3->tmp.extensions.received & (1u << i))) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2620 | /* Don't send extensions that were not received. */ |
| 2621 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2622 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2623 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2624 | if (!kExtensions[i].add_serverhello(ssl, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2625 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 2626 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2627 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2628 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2629 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2630 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2631 | if (!custom_ext_add_serverhello(ssl, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2632 | goto err; |
| 2633 | } |
| 2634 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2635 | /* Discard empty extensions blocks before TLS 1.3. */ |
| 2636 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION && |
| 2637 | CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2638 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2639 | } |
| 2640 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2641 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2642 | |
| 2643 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2644 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2645 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2646 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2647 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2648 | static int ssl_scan_clienthello_tlsext( |
| 2649 | SSL *ssl, const struct ssl_early_callback_ctx *client_hello, |
| 2650 | int *out_alert) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2651 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2652 | if (kExtensions[i].init != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2653 | kExtensions[i].init(ssl); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2654 | } |
| 2655 | } |
| 2656 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2657 | ssl->s3->tmp.extensions.received = 0; |
| 2658 | ssl->s3->tmp.custom_extensions.received = 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2659 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2660 | CBS extensions; |
| 2661 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 2662 | while (CBS_len(&extensions) != 0) { |
| 2663 | uint16_t type; |
| 2664 | CBS extension; |
| 2665 | |
| 2666 | /* Decode the next extension. */ |
| 2667 | if (!CBS_get_u16(&extensions, &type) || |
| 2668 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2669 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2670 | return 0; |
| 2671 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2672 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2673 | /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 2674 | * ambiguous. Ignore all but the renegotiation_info extension. */ |
| 2675 | if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
| 2676 | continue; |
| 2677 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2678 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2679 | unsigned ext_index; |
| 2680 | const struct tls_extension *const ext = |
| 2681 | tls_extension_find(&ext_index, type); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2682 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2683 | if (ext == NULL) { |
| 2684 | if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2685 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2686 | return 0; |
| 2687 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2688 | continue; |
| 2689 | } |
| 2690 | |
| 2691 | ssl->s3->tmp.extensions.received |= (1u << ext_index); |
| 2692 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2693 | if (!ext->parse_clienthello(ssl, &alert, &extension)) { |
| 2694 | *out_alert = alert; |
| 2695 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2696 | ERR_add_error_dataf("extension: %u", (unsigned)type); |
| 2697 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2698 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2699 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2700 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2701 | for (size_t i = 0; i < kNumExtensions; i++) { |
| 2702 | if (ssl->s3->tmp.extensions.received & (1u << i)) { |
| 2703 | continue; |
| 2704 | } |
| 2705 | |
| 2706 | CBS *contents = NULL, fake_contents; |
| 2707 | static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 2708 | if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 2709 | ssl_client_cipher_list_contains_cipher(client_hello, |
| 2710 | SSL3_CK_SCSV & 0xffff)) { |
| 2711 | /* The renegotiation SCSV was received so pretend that we received a |
| 2712 | * renegotiation extension. */ |
| 2713 | CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 2714 | sizeof(kFakeRenegotiateExtension)); |
| 2715 | contents = &fake_contents; |
| 2716 | ssl->s3->tmp.extensions.received |= (1u << i); |
| 2717 | } |
| 2718 | |
| 2719 | /* Extension wasn't observed so call the callback with a NULL |
| 2720 | * parameter. */ |
| 2721 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2722 | if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) { |
| 2723 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 2724 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2725 | *out_alert = alert; |
| 2726 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2727 | } |
| 2728 | } |
| 2729 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2730 | return 1; |
| 2731 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2732 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2733 | int ssl_parse_clienthello_tlsext( |
| 2734 | SSL *ssl, const struct ssl_early_callback_ctx *client_hello) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2735 | int alert = -1; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2736 | if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2737 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2738 | return 0; |
| 2739 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2740 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2741 | if (ssl_check_clienthello_tlsext(ssl) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2742 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2743 | return 0; |
| 2744 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2745 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2746 | return 1; |
| 2747 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2748 | |
Matt Braithwaite | e564a5b | 2015-09-30 15:24:05 -0700 | [diff] [blame] | 2749 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits); |
| 2750 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2751 | static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2752 | /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */ |
| 2753 | if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2754 | return 1; |
| 2755 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2756 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2757 | /* Decode the extensions block and check it is valid. */ |
| 2758 | CBS extensions; |
| 2759 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2760 | !tls1_check_duplicate_extensions(&extensions)) { |
| 2761 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
| 2765 | uint32_t received = 0; |
| 2766 | while (CBS_len(&extensions) != 0) { |
| 2767 | uint16_t type; |
| 2768 | CBS extension; |
| 2769 | |
| 2770 | /* Decode the next extension. */ |
| 2771 | if (!CBS_get_u16(&extensions, &type) || |
| 2772 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2773 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2774 | return 0; |
| 2775 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2776 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2777 | unsigned ext_index; |
| 2778 | const struct tls_extension *const ext = |
| 2779 | tls_extension_find(&ext_index, type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2780 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2781 | if (ext == NULL) { |
| 2782 | if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2783 | return 0; |
| 2784 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2785 | continue; |
| 2786 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2787 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2788 | if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) && |
| 2789 | type != TLSEXT_TYPE_renegotiate) { |
| 2790 | /* If the extension was never sent then it is illegal, except for the |
| 2791 | * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */ |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2792 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2793 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 2794 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2795 | return 0; |
| 2796 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2797 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2798 | received |= (1u << ext_index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2799 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2800 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2801 | if (!ext->parse_serverhello(ssl, &alert, &extension)) { |
| 2802 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2803 | ERR_add_error_dataf("extension: %u", (unsigned)type); |
| 2804 | *out_alert = alert; |
| 2805 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2806 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2807 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2808 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2809 | size_t i; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2810 | for (i = 0; i < kNumExtensions; i++) { |
| 2811 | if (!(received & (1u << i))) { |
| 2812 | /* Extension wasn't observed so call the callback with a NULL |
| 2813 | * parameter. */ |
| 2814 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2815 | if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2816 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 2817 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2818 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2819 | return 0; |
| 2820 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2821 | } |
| 2822 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2823 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2824 | return 1; |
| 2825 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2826 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2827 | static int ssl_check_clienthello_tlsext(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2828 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2829 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2830 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2831 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2832 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2833 | ssl->ctx->tlsext_servername_arg); |
| 2834 | } else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2835 | ret = ssl->initial_ctx->tlsext_servername_callback( |
| 2836 | ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2837 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2838 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2839 | switch (ret) { |
| 2840 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2841 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2842 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2843 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2844 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2845 | ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2846 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2847 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2848 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2849 | ssl->s3->tmp.should_ack_sni = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2850 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2851 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2852 | default: |
| 2853 | return 1; |
| 2854 | } |
| 2855 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2856 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2857 | static int ssl_check_serverhello_tlsext(SSL *ssl) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2858 | int ret = SSL_TLSEXT_ERR_OK; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2859 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2860 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2861 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2862 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2863 | ssl->ctx->tlsext_servername_arg); |
| 2864 | } else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2865 | ret = ssl->initial_ctx->tlsext_servername_callback( |
| 2866 | ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2867 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2868 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2869 | switch (ret) { |
| 2870 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2871 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2872 | return -1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2873 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2874 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2875 | ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2876 | return 1; |
| 2877 | |
| 2878 | default: |
| 2879 | return 1; |
| 2880 | } |
| 2881 | } |
| 2882 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2883 | int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2884 | int alert = -1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2885 | if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) { |
| 2886 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2887 | return 0; |
| 2888 | } |
| 2889 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2890 | if (ssl_check_serverhello_tlsext(ssl) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2891 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2892 | return 0; |
| 2893 | } |
| 2894 | |
| 2895 | return 1; |
| 2896 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2897 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2898 | int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session, |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 2899 | int *out_renew_ticket, const uint8_t *ticket, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2900 | size_t ticket_len, const uint8_t *session_id, |
| 2901 | size_t session_id_len) { |
| 2902 | int ret = 1; /* Most errors are non-fatal. */ |
| 2903 | SSL_CTX *ssl_ctx = ssl->initial_ctx; |
| 2904 | uint8_t *plaintext = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2905 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2906 | HMAC_CTX hmac_ctx; |
| 2907 | HMAC_CTX_init(&hmac_ctx); |
| 2908 | EVP_CIPHER_CTX cipher_ctx; |
| 2909 | EVP_CIPHER_CTX_init(&cipher_ctx); |
| 2910 | |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 2911 | *out_renew_ticket = 0; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2912 | *out_session = NULL; |
| 2913 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2914 | if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
| 2915 | goto done; |
| 2916 | } |
| 2917 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2918 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 2919 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2920 | } |
| 2921 | |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 2922 | /* Ensure there is room for the key name and the largest IV |
| 2923 | * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but |
| 2924 | * the maximum IV length should be well under the minimum size for the |
| 2925 | * session material and HMAC. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2926 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 2927 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2928 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2929 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2930 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2931 | if (ssl_ctx->tlsext_ticket_key_cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2932 | int cb_ret = ssl_ctx->tlsext_ticket_key_cb( |
| 2933 | ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx, |
| 2934 | &hmac_ctx, 0 /* decrypt */); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2935 | if (cb_ret < 0) { |
| 2936 | ret = 0; |
| 2937 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2938 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2939 | if (cb_ret == 0) { |
| 2940 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2941 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2942 | if (cb_ret == 2) { |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 2943 | *out_renew_ticket = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2944 | } |
| 2945 | } else { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2946 | /* Check the key name matches. */ |
| 2947 | if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name, |
| 2948 | SSL_TICKET_KEY_NAME_LEN) != 0) { |
| 2949 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2950 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2951 | if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key, |
| 2952 | sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2953 | NULL) || |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2954 | !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL, |
| 2955 | ssl_ctx->tlsext_tick_aes_key, iv)) { |
| 2956 | ret = 0; |
| 2957 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2958 | } |
| 2959 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2960 | size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2961 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2962 | /* Check the MAC at the end of the ticket. */ |
| 2963 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 2964 | size_t mac_len = HMAC_size(&hmac_ctx); |
| 2965 | 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] | 2966 | /* 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] | 2967 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2968 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2969 | HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len); |
| 2970 | HMAC_Final(&hmac_ctx, mac, NULL); |
| 2971 | if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) { |
| 2972 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2973 | } |
| 2974 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2975 | /* Decrypt the session data. */ |
| 2976 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 2977 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 2978 | mac_len; |
| 2979 | plaintext = OPENSSL_malloc(ciphertext_len); |
| 2980 | if (plaintext == NULL) { |
| 2981 | ret = 0; |
| 2982 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2983 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2984 | if (ciphertext_len >= INT_MAX) { |
| 2985 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2986 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2987 | int len1, len2; |
| 2988 | if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext, |
| 2989 | (int)ciphertext_len) || |
| 2990 | !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) { |
| 2991 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2992 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2993 | } |
| 2994 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2995 | /* Decode the session. */ |
| 2996 | SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2); |
| 2997 | if (session == NULL) { |
| 2998 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2999 | goto done; |
| 3000 | } |
| 3001 | |
| 3002 | /* Copy the client's session ID into the new session, to denote the ticket has |
| 3003 | * been accepted. */ |
| 3004 | memcpy(session->session_id, session_id, session_id_len); |
| 3005 | session->session_id_length = session_id_len; |
| 3006 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3007 | if (!ssl_session_is_context_valid(ssl, session) || |
| 3008 | !ssl_session_is_time_valid(ssl, session)) { |
| 3009 | SSL_SESSION_free(session); |
| 3010 | session = NULL; |
| 3011 | } |
| 3012 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3013 | *out_session = session; |
| 3014 | |
| 3015 | done: |
| 3016 | OPENSSL_free(plaintext); |
| 3017 | HMAC_CTX_cleanup(&hmac_ctx); |
| 3018 | EVP_CIPHER_CTX_cleanup(&cipher_ctx); |
| 3019 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3020 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3021 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3022 | int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3023 | /* Extension ignored for inappropriate versions */ |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 3024 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3025 | return 1; |
| 3026 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 3027 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3028 | CERT *const cert = ssl->cert; |
| 3029 | OPENSSL_free(cert->peer_sigalgs); |
| 3030 | cert->peer_sigalgs = NULL; |
| 3031 | cert->peer_sigalgslen = 0; |
| 3032 | |
| 3033 | size_t num_sigalgs = CBS_len(in_sigalgs); |
| 3034 | |
| 3035 | if (num_sigalgs % 2 != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3036 | return 0; |
| 3037 | } |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3038 | num_sigalgs /= 2; |
| 3039 | |
| 3040 | /* supported_signature_algorithms in the certificate request is |
| 3041 | * allowed to be empty. */ |
| 3042 | if (num_sigalgs == 0) { |
| 3043 | return 1; |
| 3044 | } |
| 3045 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 3046 | /* This multiplication doesn't overflow because sizeof(uint16_t) is two |
| 3047 | * and we just divided |num_sigalgs| by two. */ |
| 3048 | cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t)); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3049 | if (cert->peer_sigalgs == NULL) { |
| 3050 | return 0; |
| 3051 | } |
| 3052 | cert->peer_sigalgslen = num_sigalgs; |
| 3053 | |
| 3054 | CBS sigalgs; |
| 3055 | CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs)); |
| 3056 | |
| 3057 | size_t i; |
| 3058 | for (i = 0; i < num_sigalgs; i++) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 3059 | if (!CBS_get_u16(&sigalgs, &cert->peer_sigalgs[i])) { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3060 | return 0; |
| 3061 | } |
| 3062 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3063 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3064 | return 1; |
| 3065 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3066 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3067 | int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 3068 | CERT *cert = ssl->cert; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3069 | size_t i, j; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3070 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3071 | /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 3072 | * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */ |
| 3073 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 3074 | int type = ssl_private_key_type(ssl); |
| 3075 | if (type == NID_rsaEncryption) { |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3076 | *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 3077 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3078 | } |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 3079 | if (ssl_is_ecdsa_key_type(type)) { |
| 3080 | *out = SSL_SIGN_ECDSA_SHA1; |
| 3081 | return 1; |
| 3082 | } |
| 3083 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3084 | return 0; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3085 | } |
| 3086 | |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 3087 | const uint16_t *sigalgs; |
| 3088 | size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs); |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 3089 | if (cert->sigalgs != NULL) { |
| 3090 | sigalgs = cert->sigalgs; |
| 3091 | sigalgs_len = cert->sigalgs_len; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3092 | } |
| 3093 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3094 | const uint16_t *peer_sigalgs = cert->peer_sigalgs; |
| 3095 | size_t peer_sigalgs_len = cert->peer_sigalgslen; |
David Benjamin | 51dd7d6 | 2016-07-08 16:07:01 -0700 | [diff] [blame] | 3096 | if (peer_sigalgs_len == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3097 | /* If the client didn't specify any signature_algorithms extension then |
| 3098 | * we can assume that it supports SHA1. See |
| 3099 | * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
| 3100 | static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 3101 | SSL_SIGN_ECDSA_SHA1}; |
| 3102 | peer_sigalgs = kDefaultPeerAlgorithms; |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 3103 | peer_sigalgs_len = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms); |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
David Benjamin | d246b81 | 2016-07-08 15:07:02 -0700 | [diff] [blame] | 3106 | for (i = 0; i < sigalgs_len; i++) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3107 | uint16_t sigalg = sigalgs[i]; |
| 3108 | /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be |
| 3109 | * negotiated. */ |
| 3110 | if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 || |
| 3111 | !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) { |
| 3112 | continue; |
| 3113 | } |
| 3114 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3115 | for (j = 0; j < peer_sigalgs_len; j++) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3116 | if (sigalg == peer_sigalgs[j]) { |
| 3117 | *out = sigalg; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3118 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3119 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3120 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3121 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3122 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3123 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3124 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3125 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3126 | |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3127 | int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) { |
| 3128 | int ret = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3129 | EVP_MD_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3130 | |
| 3131 | EVP_MD_CTX_init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3132 | if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) { |
| 3133 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3134 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3135 | |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3136 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 3137 | EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
| 3138 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3139 | if (ssl->session != NULL) { |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3140 | static const char kResumptionMagic[] = "Resumption"; |
| 3141 | EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
| 3142 | if (ssl->session->original_handshake_hash_len == 0) { |
| 3143 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3144 | goto err; |
| 3145 | } |
| 3146 | EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash, |
| 3147 | ssl->session->original_handshake_hash_len); |
| 3148 | } |
| 3149 | |
| 3150 | uint8_t handshake_hash[EVP_MAX_MD_SIZE]; |
| 3151 | int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash, |
| 3152 | sizeof(handshake_hash)); |
| 3153 | if (handshake_hash_len < 0) { |
| 3154 | goto err; |
| 3155 | } |
| 3156 | EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len); |
| 3157 | unsigned len_u; |
| 3158 | EVP_DigestFinal_ex(&ctx, out, &len_u); |
| 3159 | *out_len = len_u; |
| 3160 | |
| 3161 | ret = 1; |
| 3162 | |
| 3163 | err: |
| 3164 | EVP_MD_CTX_cleanup(&ctx); |
| 3165 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3166 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3167 | |
| 3168 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3169 | * 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] | 3170 | * data. */ |
| 3171 | int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3172 | int digest_len; |
| 3173 | /* This function should never be called for a resumed session because the |
| 3174 | * handshake hashes that we wish to record are for the original, full |
| 3175 | * handshake. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3176 | if (ssl->session != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3177 | return -1; |
| 3178 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3179 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3180 | digest_len = |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3181 | tls1_handshake_digest( |
| 3182 | ssl, ssl->s3->new_session->original_handshake_hash, |
| 3183 | sizeof(ssl->s3->new_session->original_handshake_hash)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3184 | if (digest_len < 0) { |
| 3185 | return -1; |
| 3186 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3187 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3188 | ssl->s3->new_session->original_handshake_hash_len = digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3189 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3190 | return 1; |
| 3191 | } |