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