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