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