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