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