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 | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 130 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs); |
| 131 | static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs); |
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; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 600 | void (*init)(SSL_HANDSHAKE *hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 601 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 602 | int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out); |
| 603 | int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 604 | CBS *contents); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 605 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 606 | int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 607 | CBS *contents); |
| 608 | int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 609 | }; |
| 610 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 611 | static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 612 | CBS *contents) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 613 | if (contents != NULL) { |
| 614 | /* Servers MUST NOT send this extension. */ |
| 615 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 616 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | return 1; |
| 621 | } |
| 622 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 623 | static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 624 | CBS *contents) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 625 | /* This extension from the client is handled elsewhere. */ |
| 626 | return 1; |
| 627 | } |
| 628 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 629 | static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 630 | return 1; |
| 631 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 632 | |
| 633 | /* Server name indication (SNI). |
| 634 | * |
| 635 | * https://tools.ietf.org/html/rfc6066#section-3. */ |
| 636 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 637 | static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 638 | SSL *const ssl = hs->ssl; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 639 | if (ssl->tlsext_hostname == NULL) { |
| 640 | return 1; |
| 641 | } |
| 642 | |
| 643 | CBB contents, server_name_list, name; |
| 644 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 645 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 646 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 647 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 648 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 649 | !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 650 | strlen(ssl->tlsext_hostname)) || |
| 651 | !CBB_flush(out)) { |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | return 1; |
| 656 | } |
| 657 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 658 | static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 659 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 660 | SSL *const ssl = hs->ssl; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 661 | if (contents == NULL) { |
| 662 | return 1; |
| 663 | } |
| 664 | |
| 665 | if (CBS_len(contents) != 0) { |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | assert(ssl->tlsext_hostname != NULL); |
| 670 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 671 | if (ssl->session == NULL) { |
| 672 | assert(ssl->s3->new_session->tlsext_hostname == NULL); |
| 673 | ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname); |
| 674 | if (!ssl->s3->new_session->tlsext_hostname) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 675 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 676 | return 0; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return 1; |
| 681 | } |
| 682 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 683 | static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 684 | CBS *contents) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 685 | if (contents == NULL) { |
| 686 | return 1; |
| 687 | } |
| 688 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 689 | CBS server_name_list, host_name; |
| 690 | uint8_t name_type; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 691 | if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 692 | !CBS_get_u8(&server_name_list, &name_type) || |
| 693 | /* Although the server_name extension was intended to be extensible to |
| 694 | * new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 695 | * different name types will cause an error. Further, RFC 4366 originally |
| 696 | * defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 697 | * adding new name types is no longer feasible. |
| 698 | * |
| 699 | * Act as if the extensibility does not exist to simplify parsing. */ |
| 700 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 701 | CBS_len(&server_name_list) != 0 || |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 702 | CBS_len(contents) != 0) { |
| 703 | return 0; |
| 704 | } |
| 705 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 706 | if (name_type != TLSEXT_NAMETYPE_host_name || |
| 707 | CBS_len(&host_name) == 0 || |
| 708 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 709 | CBS_contains_zero_byte(&host_name)) { |
| 710 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 711 | return 0; |
| 712 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 713 | |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 714 | /* Copy the hostname as a string. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 715 | if (!CBS_strdup(&host_name, &hs->hostname)) { |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 716 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 717 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 718 | } |
| 719 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 720 | hs->should_ack_sni = 1; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 721 | return 1; |
| 722 | } |
| 723 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 724 | static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 725 | if (hs->ssl->s3->session_reused || |
| 726 | !hs->should_ack_sni) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 727 | return 1; |
| 728 | } |
| 729 | |
| 730 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 731 | !CBB_add_u16(out, 0 /* length */)) { |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | return 1; |
| 736 | } |
| 737 | |
| 738 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 739 | /* Renegotiation indication. |
| 740 | * |
| 741 | * https://tools.ietf.org/html/rfc5746 */ |
| 742 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 743 | static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 744 | SSL *const ssl = hs->ssl; |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 745 | uint16_t min_version, max_version; |
| 746 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /* Renegotiation indication is not necessary in TLS 1.3. */ |
| 751 | if (min_version >= TLS1_3_VERSION) { |
| 752 | return 1; |
| 753 | } |
| 754 | |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 755 | assert(ssl->s3->initial_handshake_complete == |
| 756 | (ssl->s3->previous_client_finished_len != 0)); |
| 757 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 758 | CBB contents, prev_finished; |
| 759 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 760 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 761 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 762 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 763 | ssl->s3->previous_client_finished_len) || |
| 764 | !CBB_flush(out)) { |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | return 1; |
| 769 | } |
| 770 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 771 | static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 772 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 773 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 774 | if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 775 | return 0; |
| 776 | } |
| 777 | |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 778 | /* Servers may not switch between omitting the extension and supporting it. |
| 779 | * See RFC 5746, sections 3.5 and 4.2. */ |
| 780 | if (ssl->s3->initial_handshake_complete && |
| 781 | (contents != NULL) != ssl->s3->send_connection_binding) { |
| 782 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 783 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 784 | return 0; |
| 785 | } |
| 786 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 787 | if (contents == NULL) { |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 788 | /* 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] | 789 | * RI even on initial ServerHello because the client doesn't see any |
| 790 | * renegotiation during an attack. However this would mean we could not |
| 791 | * connect to any server which doesn't support RI. |
| 792 | * |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 793 | * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 794 | * practical terms every client sets it so it's just assumed here. */ |
| 795 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 799 | ssl->s3->previous_server_finished_len; |
| 800 | |
| 801 | /* Check for logic errors */ |
| 802 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 803 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 804 | assert(ssl->s3->initial_handshake_complete == |
| 805 | (ssl->s3->previous_client_finished_len != 0)); |
| 806 | assert(ssl->s3->initial_handshake_complete == |
| 807 | (ssl->s3->previous_server_finished_len != 0)); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 808 | |
| 809 | /* Parse out the extension contents. */ |
| 810 | CBS renegotiated_connection; |
| 811 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 812 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 813 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 814 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | /* Check that the extension matches. */ |
| 819 | if (CBS_len(&renegotiated_connection) != expected_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 | |
| 825 | const uint8_t *d = CBS_data(&renegotiated_connection); |
| 826 | if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 827 | ssl->s3->previous_client_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_HANDSHAKE_FAILURE; |
| 830 | return 0; |
| 831 | } |
| 832 | d += ssl->s3->previous_client_finished_len; |
| 833 | |
| 834 | if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 835 | ssl->s3->previous_server_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 836 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 837 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 838 | return 0; |
| 839 | } |
| 840 | ssl->s3->send_connection_binding = 1; |
| 841 | |
| 842 | return 1; |
| 843 | } |
| 844 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 845 | static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 846 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 847 | SSL *const ssl = hs->ssl; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 848 | /* Renegotiation isn't supported as a server so this function should never be |
| 849 | * called after the initial handshake. */ |
| 850 | assert(!ssl->s3->initial_handshake_complete); |
| 851 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 852 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 853 | return 1; |
| 854 | } |
| 855 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 856 | if (contents == NULL) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 857 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | CBS renegotiated_connection; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 861 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 862 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 863 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 867 | /* Check that the extension matches. We do not support renegotiation as a |
| 868 | * server, so this must be empty. */ |
| 869 | if (CBS_len(&renegotiated_connection) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 870 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 871 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | ssl->s3->send_connection_binding = 1; |
| 876 | |
| 877 | return 1; |
| 878 | } |
| 879 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 880 | static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 881 | SSL *const ssl = hs->ssl; |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 882 | /* Renegotiation isn't supported as a server so this function should never be |
| 883 | * called after the initial handshake. */ |
| 884 | assert(!ssl->s3->initial_handshake_complete); |
| 885 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 886 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 887 | return 1; |
| 888 | } |
| 889 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 890 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 891 | !CBB_add_u16(out, 1 /* length */) || |
| 892 | !CBB_add_u8(out, 0 /* empty renegotiation info */)) { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | return 1; |
| 897 | } |
| 898 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 899 | |
| 900 | /* Extended Master Secret. |
| 901 | * |
David Benjamin | 43946d4 | 2016-02-01 08:42:19 -0500 | [diff] [blame] | 902 | * https://tools.ietf.org/html/rfc7627 */ |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 903 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 904 | static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 905 | uint16_t min_version, max_version; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 906 | if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) { |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /* Extended master secret is not necessary in TLS 1.3. */ |
| 911 | if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 912 | return 1; |
| 913 | } |
| 914 | |
| 915 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 916 | !CBB_add_u16(out, 0 /* length */)) { |
| 917 | return 0; |
| 918 | } |
| 919 | |
| 920 | return 1; |
| 921 | } |
| 922 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 923 | static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 924 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 925 | SSL *const ssl = hs->ssl; |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 926 | /* Whether EMS is negotiated may not change on renegotation. */ |
| 927 | if (ssl->s3->initial_handshake_complete) { |
| 928 | if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) { |
| 929 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH); |
| 930 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | return 1; |
| 935 | } |
| 936 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 937 | if (contents == NULL) { |
| 938 | return 1; |
| 939 | } |
| 940 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 941 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 942 | ssl->version == SSL3_VERSION) { |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | if (CBS_len(contents) != 0) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | ssl->s3->tmp.extended_master_secret = 1; |
| 951 | return 1; |
| 952 | } |
| 953 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 954 | static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 955 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 956 | SSL *const ssl = hs->ssl; |
| 957 | uint16_t version = ssl3_protocol_version(ssl); |
| 958 | if (version >= TLS1_3_VERSION || |
| 959 | version == SSL3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 960 | return 1; |
| 961 | } |
| 962 | |
| 963 | if (contents == NULL) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 964 | return 1; |
| 965 | } |
| 966 | |
| 967 | if (CBS_len(contents) != 0) { |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | ssl->s3->tmp.extended_master_secret = 1; |
| 972 | return 1; |
| 973 | } |
| 974 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 975 | static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 976 | if (!hs->ssl->s3->tmp.extended_master_secret) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 977 | return 1; |
| 978 | } |
| 979 | |
| 980 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 981 | !CBB_add_u16(out, 0 /* length */)) { |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | return 1; |
| 986 | } |
| 987 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 988 | |
| 989 | /* Session tickets. |
| 990 | * |
| 991 | * https://tools.ietf.org/html/rfc5077 */ |
| 992 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 993 | static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 994 | SSL *const ssl = hs->ssl; |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 995 | uint16_t min_version, max_version; |
| 996 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | /* TLS 1.3 uses a different ticket extension. */ |
| 1001 | if (min_version >= TLS1_3_VERSION || |
| 1002 | SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1003 | return 1; |
| 1004 | } |
| 1005 | |
| 1006 | const uint8_t *ticket_data = NULL; |
| 1007 | int ticket_len = 0; |
| 1008 | |
| 1009 | /* Renegotiation does not participate in session resumption. However, still |
| 1010 | * advertise the extension to avoid potentially breaking servers which carry |
| 1011 | * over the state from the previous handshake, such as OpenSSL servers |
| 1012 | * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */ |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1013 | uint16_t session_version; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1014 | if (!ssl->s3->initial_handshake_complete && |
| 1015 | ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1016 | ssl->session->tlsext_tick != NULL && |
| 1017 | /* Don't send TLS 1.3 session tickets in the ticket extension. */ |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1018 | ssl->method->version_from_wire(&session_version, |
| 1019 | ssl->session->ssl_version) && |
| 1020 | session_version < TLS1_3_VERSION) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1021 | ticket_data = ssl->session->tlsext_tick; |
| 1022 | ticket_len = ssl->session->tlsext_ticklen; |
| 1023 | } |
| 1024 | |
| 1025 | CBB ticket; |
| 1026 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1027 | !CBB_add_u16_length_prefixed(out, &ticket) || |
| 1028 | !CBB_add_bytes(&ticket, ticket_data, ticket_len) || |
| 1029 | !CBB_flush(out)) { |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | return 1; |
| 1034 | } |
| 1035 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1036 | static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1037 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1038 | SSL *const ssl = hs->ssl; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1039 | if (contents == NULL) { |
| 1040 | return 1; |
| 1041 | } |
| 1042 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1043 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1047 | /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 1048 | * this function should never be called, even if the server tries to send the |
| 1049 | * extension. */ |
| 1050 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1051 | |
| 1052 | if (CBS_len(contents) != 0) { |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1056 | hs->ticket_expected = 1; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1057 | return 1; |
| 1058 | } |
| 1059 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1060 | static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1061 | if (!hs->ticket_expected) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1062 | return 1; |
| 1063 | } |
| 1064 | |
David Benjamin | 78476f6 | 2016-11-12 11:20:55 +0900 | [diff] [blame] | 1065 | /* If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1066 | assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0); |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1069 | !CBB_add_u16(out, 0 /* length */)) { |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | return 1; |
| 1074 | } |
| 1075 | |
| 1076 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1077 | /* Signature Algorithms. |
| 1078 | * |
| 1079 | * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
| 1080 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1081 | static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1082 | SSL *const ssl = hs->ssl; |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1083 | uint16_t min_version, max_version; |
| 1084 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | if (max_version < TLS1_2_VERSION) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1089 | return 1; |
| 1090 | } |
| 1091 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1092 | const uint16_t *sigalgs; |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 1093 | const size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs); |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1094 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1095 | CBB contents, sigalgs_cbb; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1096 | if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) || |
| 1097 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1098 | !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1099 | return 0; |
| 1100 | } |
| 1101 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1102 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 1103 | if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | if (!CBB_flush(out)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | return 1; |
| 1113 | } |
| 1114 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1115 | static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1116 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1117 | SSL *const ssl = hs->ssl; |
| 1118 | OPENSSL_free(hs->peer_sigalgs); |
| 1119 | hs->peer_sigalgs = NULL; |
| 1120 | hs->num_peer_sigalgs = 0; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1121 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1122 | if (contents == NULL) { |
| 1123 | return 1; |
| 1124 | } |
| 1125 | |
| 1126 | CBS supported_signature_algorithms; |
| 1127 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1128 | CBS_len(contents) != 0 || |
| 1129 | CBS_len(&supported_signature_algorithms) == 0 || |
| 1130 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | return 1; |
| 1135 | } |
| 1136 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1137 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1138 | /* OCSP Stapling. |
| 1139 | * |
| 1140 | * https://tools.ietf.org/html/rfc6066#section-8 */ |
| 1141 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1142 | static void ext_ocsp_init(SSL_HANDSHAKE *hs) { |
| 1143 | hs->ssl->tlsext_status_type = -1; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1146 | static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1147 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1148 | if (!ssl->ocsp_stapling_enabled) { |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
| 1152 | CBB contents; |
| 1153 | if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1154 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1155 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1156 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1157 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1158 | !CBB_flush(out)) { |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1162 | ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1163 | return 1; |
| 1164 | } |
| 1165 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1166 | static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1167 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1168 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1169 | if (contents == NULL) { |
| 1170 | return 1; |
| 1171 | } |
| 1172 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1173 | /* TLS 1.3 OCSP responses are included in the Certificate extensions. */ |
| 1174 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1178 | /* OCSP stapling is forbidden on non-certificate ciphers. */ |
| 1179 | if (CBS_len(contents) != 0 || |
| 1180 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1181 | return 0; |
| 1182 | } |
| 1183 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1184 | /* Note this does not check for resumption in TLS 1.2. Sending |
| 1185 | * status_request here does not make sense, but OpenSSL does so and the |
| 1186 | * specification does not say anything. Tolerate it but ignore it. */ |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1187 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1188 | hs->certificate_status_expected = 1; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1189 | return 1; |
| 1190 | } |
| 1191 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1192 | static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1193 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1194 | if (contents == NULL) { |
| 1195 | return 1; |
| 1196 | } |
| 1197 | |
| 1198 | uint8_t status_type; |
| 1199 | if (!CBS_get_u8(contents, &status_type)) { |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* We cannot decide whether OCSP stapling will occur yet because the correct |
| 1204 | * SSL_CTX might not have been selected. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1205 | hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1206 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1207 | return 1; |
| 1208 | } |
| 1209 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1210 | static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1211 | SSL *const ssl = hs->ssl; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1212 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1213 | !hs->ocsp_stapling_requested || |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1214 | ssl->ctx->ocsp_response_length == 0 || |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1215 | ssl->s3->session_reused || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1216 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1217 | return 1; |
| 1218 | } |
| 1219 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1220 | hs->certificate_status_expected = 1; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1221 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1222 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1223 | CBB_add_u16(out, 0 /* length */); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1227 | /* Next protocol negotiation. |
| 1228 | * |
| 1229 | * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */ |
| 1230 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1231 | static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1232 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1233 | if (ssl->s3->initial_handshake_complete || |
| 1234 | ssl->ctx->next_proto_select_cb == NULL || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1235 | (ssl->options & SSL_OP_DISABLE_NPN) || SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1240 | !CBB_add_u16(out, 0 /* length */)) { |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | return 1; |
| 1245 | } |
| 1246 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1247 | static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1248 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1249 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1250 | if (contents == NULL) { |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1254 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1258 | /* If any of these are false then we should never have sent the NPN |
| 1259 | * extension in the ClientHello and thus this function should never have been |
| 1260 | * called. */ |
| 1261 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1262 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1263 | assert(ssl->ctx->next_proto_select_cb != NULL); |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 1264 | assert(!(ssl->options & SSL_OP_DISABLE_NPN)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1265 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1266 | if (ssl->s3->alpn_selected != NULL) { |
| 1267 | /* NPN and ALPN may not be negotiated in the same connection. */ |
| 1268 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1269 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1273 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1274 | const size_t orig_len = CBS_len(contents); |
| 1275 | |
| 1276 | while (CBS_len(contents) != 0) { |
| 1277 | CBS proto; |
| 1278 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1279 | CBS_len(&proto) == 0) { |
| 1280 | return 0; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | uint8_t *selected; |
| 1285 | uint8_t selected_len; |
| 1286 | if (ssl->ctx->next_proto_select_cb( |
| 1287 | ssl, &selected, &selected_len, orig_contents, orig_len, |
| 1288 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1289 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1293 | OPENSSL_free(ssl->s3->next_proto_negotiated); |
| 1294 | ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 1295 | if (ssl->s3->next_proto_negotiated == NULL) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1296 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1300 | ssl->s3->next_proto_negotiated_len = selected_len; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1301 | hs->next_proto_neg_seen = 1; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1302 | |
| 1303 | return 1; |
| 1304 | } |
| 1305 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1306 | static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1307 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1308 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1309 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1310 | return 1; |
| 1311 | } |
| 1312 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1313 | if (contents != NULL && CBS_len(contents) != 0) { |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | if (contents == NULL || |
| 1318 | ssl->s3->initial_handshake_complete || |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1319 | ssl->ctx->next_protos_advertised_cb == NULL || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1320 | SSL_is_dtls(ssl)) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1321 | return 1; |
| 1322 | } |
| 1323 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1324 | hs->next_proto_neg_seen = 1; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1325 | return 1; |
| 1326 | } |
| 1327 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1328 | static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1329 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1330 | /* |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1331 | * parsed. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1332 | if (!hs->next_proto_neg_seen) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1333 | return 1; |
| 1334 | } |
| 1335 | |
| 1336 | const uint8_t *npa; |
| 1337 | unsigned npa_len; |
| 1338 | |
| 1339 | if (ssl->ctx->next_protos_advertised_cb( |
| 1340 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1341 | SSL_TLSEXT_ERR_OK) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1342 | hs->next_proto_neg_seen = 0; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1343 | return 1; |
| 1344 | } |
| 1345 | |
| 1346 | CBB contents; |
| 1347 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1348 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1349 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1350 | !CBB_flush(out)) { |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | return 1; |
| 1355 | } |
| 1356 | |
| 1357 | |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1358 | /* Signed certificate timestamps. |
| 1359 | * |
| 1360 | * https://tools.ietf.org/html/rfc6962#section-3.3.1 */ |
| 1361 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1362 | static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1363 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1364 | if (!ssl->signed_cert_timestamps_enabled) { |
| 1365 | return 1; |
| 1366 | } |
| 1367 | |
| 1368 | if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1369 | !CBB_add_u16(out, 0 /* length */)) { |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | return 1; |
| 1374 | } |
| 1375 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1376 | static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1377 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1378 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1379 | if (contents == NULL) { |
| 1380 | return 1; |
| 1381 | } |
| 1382 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1383 | /* TLS 1.3 SCTs are included in the Certificate extensions. */ |
| 1384 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1385 | *out_alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1389 | /* If this is false then we should never have sent the SCT extension in the |
| 1390 | * ClientHello and thus this function should never have been called. */ |
| 1391 | assert(ssl->signed_cert_timestamps_enabled); |
| 1392 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1393 | if (!ssl_is_sct_list_valid(contents)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1394 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
David Benjamin | daa8850 | 2016-10-04 16:32:16 -0400 | [diff] [blame] | 1398 | /* Session resumption uses the original session information. The extension |
| 1399 | * should not be sent on resumption, but RFC 6962 did not make it a |
| 1400 | * requirement, so tolerate this. |
| 1401 | * |
| 1402 | * TODO(davidben): Enforce this anyway. */ |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1403 | if (!ssl->s3->session_reused && |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1404 | !CBS_stow( |
| 1405 | contents, |
| 1406 | &ssl->s3->new_session->tlsext_signed_cert_timestamp_list, |
| 1407 | &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1408 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1415 | static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1416 | CBS *contents) { |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1417 | if (contents == NULL) { |
| 1418 | return 1; |
| 1419 | } |
| 1420 | |
| 1421 | if (CBS_len(contents) != 0) { |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1425 | hs->scts_requested = 1; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1426 | return 1; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1429 | static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1430 | SSL *const ssl = hs->ssl; |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 1431 | /* The extension shouldn't be sent when resuming sessions. */ |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1432 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1433 | ssl->s3->session_reused || |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 1434 | ssl->ctx->signed_cert_timestamp_list_length == 0) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1435 | return 1; |
| 1436 | } |
| 1437 | |
| 1438 | CBB contents; |
| 1439 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1440 | CBB_add_u16_length_prefixed(out, &contents) && |
| 1441 | CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list, |
| 1442 | ssl->ctx->signed_cert_timestamp_list_length) && |
| 1443 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1447 | /* Application-level Protocol Negotiation. |
| 1448 | * |
| 1449 | * https://tools.ietf.org/html/rfc7301 */ |
| 1450 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1451 | static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1452 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1453 | if (ssl->alpn_client_proto_list == NULL || |
| 1454 | ssl->s3->initial_handshake_complete) { |
| 1455 | return 1; |
| 1456 | } |
| 1457 | |
| 1458 | CBB contents, proto_list; |
| 1459 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1460 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1461 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1462 | !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1463 | ssl->alpn_client_proto_list_len) || |
| 1464 | !CBB_flush(out)) { |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | return 1; |
| 1469 | } |
| 1470 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1471 | static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1472 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1473 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1474 | if (contents == NULL) { |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
| 1478 | assert(!ssl->s3->initial_handshake_complete); |
| 1479 | assert(ssl->alpn_client_proto_list != NULL); |
| 1480 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1481 | if (hs->next_proto_neg_seen) { |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1482 | /* NPN and ALPN may not be negotiated in the same connection. */ |
| 1483 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1484 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1488 | /* The extension data consists of a ProtocolNameList which must have |
| 1489 | * exactly one ProtocolName. Each of these is length-prefixed. */ |
| 1490 | CBS protocol_name_list, protocol_name; |
| 1491 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1492 | CBS_len(contents) != 0 || |
| 1493 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1494 | /* Empty protocol names are forbidden. */ |
| 1495 | CBS_len(&protocol_name) == 0 || |
| 1496 | CBS_len(&protocol_name_list) != 0) { |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
David Benjamin | 3e51757d | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1500 | /* Check that the protcol name is one of the ones we advertised. */ |
| 1501 | int protocol_ok = 0; |
| 1502 | CBS client_protocol_name_list, client_protocol_name; |
| 1503 | CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list, |
| 1504 | ssl->alpn_client_proto_list_len); |
| 1505 | while (CBS_len(&client_protocol_name_list) > 0) { |
| 1506 | if (!CBS_get_u8_length_prefixed(&client_protocol_name_list, |
| 1507 | &client_protocol_name)) { |
| 1508 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) && |
| 1513 | memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name), |
| 1514 | CBS_len(&protocol_name)) == 0) { |
| 1515 | protocol_ok = 1; |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | if (!protocol_ok) { |
| 1521 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1522 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1526 | if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected, |
| 1527 | &ssl->s3->alpn_selected_len)) { |
| 1528 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | return 1; |
| 1533 | } |
| 1534 | |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1535 | int ssl_negotiate_alpn(SSL *ssl, uint8_t *out_alert, |
| 1536 | const struct ssl_early_callback_ctx *client_hello) { |
| 1537 | CBS contents; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1538 | if (ssl->ctx->alpn_select_cb == NULL || |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1539 | !ssl_early_callback_get_extension( |
| 1540 | client_hello, &contents, |
| 1541 | TLSEXT_TYPE_application_layer_protocol_negotiation)) { |
| 1542 | /* Ignore ALPN if not configured or no extension was supplied. */ |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1543 | return 1; |
| 1544 | } |
| 1545 | |
| 1546 | /* ALPN takes precedence over NPN. */ |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 1547 | ssl->s3->hs->next_proto_neg_seen = 0; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1548 | |
| 1549 | CBS protocol_name_list; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1550 | if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) || |
| 1551 | CBS_len(&contents) != 0 || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1552 | CBS_len(&protocol_name_list) < 2) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1553 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1554 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | /* Validate the protocol list. */ |
| 1559 | CBS protocol_name_list_copy = protocol_name_list; |
| 1560 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1561 | CBS protocol_name; |
| 1562 | |
| 1563 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
| 1564 | /* Empty protocol names are forbidden. */ |
| 1565 | CBS_len(&protocol_name) == 0) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1566 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1567 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1568 | return 0; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | const uint8_t *selected; |
| 1573 | uint8_t selected_len; |
| 1574 | if (ssl->ctx->alpn_select_cb( |
| 1575 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1576 | CBS_len(&protocol_name_list), |
| 1577 | ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
| 1578 | OPENSSL_free(ssl->s3->alpn_selected); |
| 1579 | ssl->s3->alpn_selected = BUF_memdup(selected, selected_len); |
| 1580 | if (ssl->s3->alpn_selected == NULL) { |
| 1581 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1582 | return 0; |
| 1583 | } |
| 1584 | ssl->s3->alpn_selected_len = selected_len; |
| 1585 | } |
| 1586 | |
| 1587 | return 1; |
| 1588 | } |
| 1589 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1590 | static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1591 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1592 | if (ssl->s3->alpn_selected == NULL) { |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | CBB contents, proto_list, proto; |
| 1597 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1598 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1599 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1600 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1601 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected, |
| 1602 | ssl->s3->alpn_selected_len) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1603 | !CBB_flush(out)) { |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | return 1; |
| 1608 | } |
| 1609 | |
| 1610 | |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1611 | /* Channel ID. |
| 1612 | * |
| 1613 | * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */ |
| 1614 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1615 | static void ext_channel_id_init(SSL_HANDSHAKE *hs) { |
| 1616 | hs->ssl->s3->tlsext_channel_id_valid = 0; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1619 | static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1620 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1621 | if (!ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1622 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1623 | return 1; |
| 1624 | } |
| 1625 | |
| 1626 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1627 | !CBB_add_u16(out, 0 /* length */)) { |
| 1628 | return 0; |
| 1629 | } |
| 1630 | |
| 1631 | return 1; |
| 1632 | } |
| 1633 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1634 | static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1635 | uint8_t *out_alert, CBS *contents) { |
| 1636 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1637 | if (contents == NULL) { |
| 1638 | return 1; |
| 1639 | } |
| 1640 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1641 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1642 | assert(ssl->tlsext_channel_id_enabled); |
| 1643 | |
| 1644 | if (CBS_len(contents) != 0) { |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | ssl->s3->tlsext_channel_id_valid = 1; |
| 1649 | return 1; |
| 1650 | } |
| 1651 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1652 | static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1653 | uint8_t *out_alert, CBS *contents) { |
| 1654 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1655 | if (contents == NULL || |
| 1656 | !ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1657 | SSL_is_dtls(ssl)) { |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1658 | return 1; |
| 1659 | } |
| 1660 | |
| 1661 | if (CBS_len(contents) != 0) { |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | ssl->s3->tlsext_channel_id_valid = 1; |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1669 | static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1670 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1671 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 1672 | return 1; |
| 1673 | } |
| 1674 | |
| 1675 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1676 | !CBB_add_u16(out, 0 /* length */)) { |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | return 1; |
| 1681 | } |
| 1682 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* Secure Real-time Transport Protocol (SRTP) extension. |
| 1685 | * |
| 1686 | * https://tools.ietf.org/html/rfc5764 */ |
| 1687 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1688 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1689 | static void ext_srtp_init(SSL_HANDSHAKE *hs) { |
| 1690 | hs->ssl->srtp_profile = NULL; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1693 | static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1694 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1695 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1696 | if (profiles == NULL) { |
| 1697 | return 1; |
| 1698 | } |
| 1699 | const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles); |
| 1700 | if (num_profiles == 0) { |
| 1701 | return 1; |
| 1702 | } |
| 1703 | |
| 1704 | CBB contents, profile_ids; |
| 1705 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1706 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1707 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1711 | for (size_t i = 0; i < num_profiles; i++) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1712 | if (!CBB_add_u16(&profile_ids, |
| 1713 | sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) { |
| 1714 | return 0; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1719 | !CBB_flush(out)) { |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | return 1; |
| 1724 | } |
| 1725 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1726 | static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1727 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1728 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1729 | if (contents == NULL) { |
| 1730 | return 1; |
| 1731 | } |
| 1732 | |
| 1733 | /* The extension consists of a u16-prefixed profile ID list containing a |
| 1734 | * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1735 | * |
| 1736 | * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */ |
| 1737 | CBS profile_ids, srtp_mki; |
| 1738 | uint16_t profile_id; |
| 1739 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1740 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1741 | CBS_len(&profile_ids) != 0 || |
| 1742 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1743 | CBS_len(contents) != 0) { |
| 1744 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | if (CBS_len(&srtp_mki) != 0) { |
| 1749 | /* Must be no MKI, since we never offer one. */ |
| 1750 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1751 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
| 1755 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1756 | |
| 1757 | /* Check to see if the server gave us something we support (and presumably |
| 1758 | * offered). */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1759 | 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] | 1760 | const SRTP_PROTECTION_PROFILE *profile = |
| 1761 | sk_SRTP_PROTECTION_PROFILE_value(profiles, i); |
| 1762 | |
| 1763 | if (profile->id == profile_id) { |
| 1764 | ssl->srtp_profile = profile; |
| 1765 | return 1; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1770 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1774 | static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1775 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1776 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1777 | if (contents == NULL) { |
| 1778 | return 1; |
| 1779 | } |
| 1780 | |
| 1781 | CBS profile_ids, srtp_mki; |
| 1782 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1783 | CBS_len(&profile_ids) < 2 || |
| 1784 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1785 | CBS_len(contents) != 0) { |
| 1786 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | /* Discard the MKI value for now. */ |
| 1790 | |
| 1791 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1792 | SSL_get_srtp_profiles(ssl); |
| 1793 | |
| 1794 | /* Pick the server's most preferred profile. */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1795 | 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] | 1796 | const SRTP_PROTECTION_PROFILE *server_profile = |
| 1797 | sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i); |
| 1798 | |
| 1799 | CBS profile_ids_tmp; |
| 1800 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1801 | |
| 1802 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1803 | uint16_t profile_id; |
| 1804 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | if (server_profile->id == profile_id) { |
| 1809 | ssl->srtp_profile = server_profile; |
| 1810 | return 1; |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | return 1; |
| 1816 | } |
| 1817 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1818 | static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1819 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1820 | if (ssl->srtp_profile == NULL) { |
| 1821 | return 1; |
| 1822 | } |
| 1823 | |
| 1824 | CBB contents, profile_ids; |
| 1825 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1826 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1827 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1828 | !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1829 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1830 | !CBB_flush(out)) { |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
| 1834 | return 1; |
| 1835 | } |
| 1836 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1837 | |
| 1838 | /* EC point formats. |
| 1839 | * |
| 1840 | * https://tools.ietf.org/html/rfc4492#section-5.1.2 */ |
| 1841 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1842 | static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1843 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1844 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1845 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1846 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1847 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1848 | !CBB_flush(out)) { |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | return 1; |
| 1853 | } |
| 1854 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1855 | static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 70aba26 | 2016-11-01 12:08:15 -0400 | [diff] [blame] | 1856 | uint16_t min_version, max_version; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1857 | if (!ssl_get_version_range(hs->ssl, &min_version, &max_version)) { |
David Benjamin | 70aba26 | 2016-11-01 12:08:15 -0400 | [diff] [blame] | 1858 | return 0; |
| 1859 | } |
| 1860 | |
| 1861 | /* The point format extension is unneccessary in TLS 1.3. */ |
| 1862 | if (min_version >= TLS1_3_VERSION) { |
| 1863 | return 1; |
| 1864 | } |
| 1865 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1866 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1869 | static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1870 | CBS *contents) { |
| 1871 | if (contents == NULL) { |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1875 | if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1876 | return 0; |
| 1877 | } |
| 1878 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1879 | CBS ec_point_format_list; |
| 1880 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1881 | CBS_len(contents) != 0) { |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1885 | /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1886 | * point format. */ |
| 1887 | if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1888 | CBS_len(&ec_point_format_list)) == NULL) { |
| 1889 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | return 1; |
| 1894 | } |
| 1895 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1896 | static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1897 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1898 | if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1899 | return 1; |
| 1900 | } |
| 1901 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1902 | return ext_ec_point_parse_serverhello(hs, out_alert, contents); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1905 | static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1906 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1907 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1908 | return 1; |
| 1909 | } |
| 1910 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1911 | const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1912 | const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1913 | const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1914 | |
| 1915 | if (!using_ecc) { |
| 1916 | return 1; |
| 1917 | } |
| 1918 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1919 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1922 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1923 | /* Pre Shared Key |
| 1924 | * |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1925 | * 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] | 1926 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1927 | static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) { |
| 1928 | SSL *const ssl = hs->ssl; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1929 | uint16_t min_version, max_version; |
| 1930 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | uint16_t session_version; |
| 1935 | if (max_version < TLS1_3_VERSION || ssl->session == NULL || |
| 1936 | !ssl->method->version_from_wire(&session_version, |
| 1937 | ssl->session->ssl_version) || |
| 1938 | session_version < TLS1_3_VERSION) { |
| 1939 | return 0; |
| 1940 | } |
| 1941 | |
| 1942 | const EVP_MD *digest = |
| 1943 | ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf); |
| 1944 | size_t binder_len = EVP_MD_size(digest); |
| 1945 | return 15 + ssl->session->tlsext_ticklen + binder_len; |
| 1946 | } |
| 1947 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1948 | static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 1949 | SSL *const ssl = hs->ssl; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1950 | uint16_t min_version, max_version; |
| 1951 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1955 | uint16_t session_version; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1956 | if (max_version < TLS1_3_VERSION || ssl->session == NULL || |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 1957 | !ssl->method->version_from_wire(&session_version, |
| 1958 | ssl->session->ssl_version) || |
| 1959 | session_version < TLS1_3_VERSION) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1960 | return 1; |
| 1961 | } |
| 1962 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1963 | struct timeval now; |
| 1964 | ssl_get_current_time(ssl, &now); |
| 1965 | uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time); |
| 1966 | uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add; |
| 1967 | |
| 1968 | /* Fill in a placeholder zero binder of the appropriate length. It will be |
| 1969 | * computed and filled in later after length prefixes are computed. */ |
| 1970 | uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0}; |
| 1971 | const EVP_MD *digest = |
| 1972 | ssl_get_handshake_digest(ssl->session->cipher->algorithm_prf); |
| 1973 | size_t binder_len = EVP_MD_size(digest); |
| 1974 | |
| 1975 | CBB contents, identity, ticket, binders, binder; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1976 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1977 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1978 | !CBB_add_u16_length_prefixed(&contents, &identity) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1979 | !CBB_add_u16_length_prefixed(&identity, &ticket) || |
| 1980 | !CBB_add_bytes(&ticket, ssl->session->tlsext_tick, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1981 | ssl->session->tlsext_ticklen) || |
| 1982 | !CBB_add_u32(&identity, obfuscated_ticket_age) || |
| 1983 | !CBB_add_u16_length_prefixed(&contents, &binders) || |
| 1984 | !CBB_add_u8_length_prefixed(&binders, &binder) || |
| 1985 | !CBB_add_bytes(&binder, zero_binder, binder_len)) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1986 | return 0; |
| 1987 | } |
| 1988 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 1989 | hs->needs_psk_binder = 1; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1990 | return CBB_flush(out); |
| 1991 | } |
| 1992 | |
| 1993 | int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1994 | CBS *contents) { |
| 1995 | uint16_t psk_id; |
| 1996 | if (!CBS_get_u16(contents, &psk_id) || |
| 1997 | CBS_len(contents) != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1998 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1999 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
David Benjamin | eab773a | 2016-11-09 18:32:35 -0500 | [diff] [blame] | 2003 | /* 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] | 2004 | if (psk_id != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 2005 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2006 | *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 2007 | return 0; |
| 2008 | } |
| 2009 | |
| 2010 | return 1; |
| 2011 | } |
| 2012 | |
| 2013 | int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl, |
| 2014 | SSL_SESSION **out_session, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2015 | CBS *out_binders, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2016 | uint8_t *out_alert, |
| 2017 | CBS *contents) { |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 2018 | /* 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] | 2019 | uint32_t obfuscated_ticket_age; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2020 | CBS identities, ticket, binders; |
| 2021 | if (!CBS_get_u16_length_prefixed(contents, &identities) || |
| 2022 | !CBS_get_u16_length_prefixed(&identities, &ticket) || |
| 2023 | !CBS_get_u32(&identities, &obfuscated_ticket_age) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2024 | !CBS_get_u16_length_prefixed(contents, &binders) || |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2025 | CBS_len(&binders) == 0 || |
Steven Valdez | af3b8a9 | 2016-11-01 12:49:22 -0400 | [diff] [blame] | 2026 | CBS_len(contents) != 0) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2027 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2028 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2032 | *out_binders = binders; |
| 2033 | |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 2034 | /* Check the syntax of the remaining identities, but do not process them. */ |
| 2035 | size_t num_identities = 1; |
| 2036 | while (CBS_len(&identities) != 0) { |
| 2037 | CBS unused_ticket; |
| 2038 | uint32_t unused_obfuscated_ticket_age; |
| 2039 | if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) || |
| 2040 | !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) { |
| 2041 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2042 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2043 | return 0; |
| 2044 | } |
| 2045 | |
| 2046 | num_identities++; |
| 2047 | } |
| 2048 | |
| 2049 | /* Check the syntax of the binders. The value will be checked later if |
| 2050 | * resuming. */ |
| 2051 | size_t num_binders = 0; |
| 2052 | while (CBS_len(&binders) != 0) { |
| 2053 | CBS binder; |
| 2054 | if (!CBS_get_u8_length_prefixed(&binders, &binder)) { |
| 2055 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2056 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | num_binders++; |
| 2061 | } |
| 2062 | |
| 2063 | if (num_identities != num_binders) { |
| 2064 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH); |
| 2065 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2066 | return 0; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2069 | /* TODO(svaldez): Check that the ticket_age is valid when attempting to use |
| 2070 | * the PSK for 0-RTT. http://crbug.com/boringssl/113 */ |
| 2071 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2072 | /* TLS 1.3 session tickets are renewed separately as part of the |
| 2073 | * NewSessionTicket. */ |
David Benjamin | e7f60a2 | 2016-11-16 18:54:25 +0900 | [diff] [blame] | 2074 | int unused_renew; |
| 2075 | if (!tls_process_ticket(ssl, out_session, &unused_renew, CBS_data(&ticket), |
| 2076 | CBS_len(&ticket), NULL, 0)) { |
| 2077 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
| 2081 | return 1; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) { |
| 2085 | if (!ssl->s3->session_reused) { |
| 2086 | return 1; |
| 2087 | } |
| 2088 | |
| 2089 | CBB contents; |
| 2090 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 2091 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2092 | /* We only consider the first identity for resumption */ |
| 2093 | !CBB_add_u16(&contents, 0) || |
| 2094 | !CBB_flush(out)) { |
| 2095 | return 0; |
| 2096 | } |
| 2097 | |
| 2098 | return 1; |
| 2099 | } |
| 2100 | |
| 2101 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2102 | /* Pre-Shared Key Exchange Modes |
| 2103 | * |
| 2104 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 */ |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2105 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2106 | static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs, |
| 2107 | CBB *out) { |
| 2108 | SSL *const ssl = hs->ssl; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2109 | uint16_t min_version, max_version; |
| 2110 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | if (max_version < TLS1_3_VERSION) { |
| 2115 | return 1; |
| 2116 | } |
| 2117 | |
| 2118 | CBB contents, ke_modes; |
| 2119 | if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) || |
| 2120 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2121 | !CBB_add_u8_length_prefixed(&contents, &ke_modes) || |
| 2122 | !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) { |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | return CBB_flush(out); |
| 2127 | } |
| 2128 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2129 | static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2130 | uint8_t *out_alert, |
| 2131 | CBS *contents) { |
| 2132 | if (contents == NULL) { |
| 2133 | return 1; |
| 2134 | } |
| 2135 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2136 | CBS ke_modes; |
| 2137 | if (!CBS_get_u8_length_prefixed(contents, &ke_modes) || |
| 2138 | CBS_len(&ke_modes) == 0 || |
| 2139 | CBS_len(contents) != 0) { |
| 2140 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | /* We only support tickets with PSK_DHE_KE. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2145 | hs->accept_psk_mode = |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2146 | memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, CBS_len(&ke_modes)) != NULL; |
| 2147 | |
| 2148 | return 1; |
| 2149 | } |
| 2150 | |
| 2151 | |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2152 | /* Early Data Indication |
| 2153 | * |
| 2154 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 */ |
| 2155 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2156 | static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2157 | /* TODO(svaldez): Support 0RTT. */ |
| 2158 | return 1; |
| 2159 | } |
| 2160 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2161 | static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2162 | uint8_t *out_alert, CBS *contents) { |
| 2163 | SSL *const ssl = hs->ssl; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2164 | if (contents == NULL) { |
| 2165 | return 1; |
| 2166 | } |
| 2167 | |
| 2168 | if (CBS_len(contents) != 0) { |
| 2169 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | /* Since we don't currently accept 0-RTT, we have to skip past any early data |
| 2174 | * the client might have sent. */ |
| 2175 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 2176 | ssl->s3->skip_early_data = 1; |
| 2177 | } |
| 2178 | return 1; |
| 2179 | } |
| 2180 | |
| 2181 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2182 | /* Key Share |
| 2183 | * |
David Benjamin | a128a55 | 2016-10-13 14:26:33 -0400 | [diff] [blame] | 2184 | * 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] | 2185 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2186 | static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2187 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2188 | uint16_t min_version, max_version; |
| 2189 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
David Benjamin | 53a2dfc | 2016-10-07 21:04:12 -0400 | [diff] [blame] | 2193 | if (max_version < TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2194 | return 1; |
| 2195 | } |
| 2196 | |
| 2197 | CBB contents, kse_bytes; |
| 2198 | if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2199 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2200 | !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) { |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2204 | uint16_t group_id = hs->retry_group; |
| 2205 | if (hs->received_hello_retry_request) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2206 | /* We received a HelloRetryRequest without a new curve, so there is no new |
| 2207 | * share to append. Leave |ecdh_ctx| as-is. */ |
| 2208 | if (group_id == 0 && |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2209 | !CBB_add_bytes(&kse_bytes, hs->key_share_bytes, |
| 2210 | hs->key_share_bytes_len)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2211 | return 0; |
| 2212 | } |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2213 | OPENSSL_free(hs->key_share_bytes); |
| 2214 | hs->key_share_bytes = NULL; |
| 2215 | hs->key_share_bytes_len = 0; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2216 | if (group_id == 0) { |
| 2217 | return CBB_flush(out); |
| 2218 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2219 | } else { |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2220 | /* Add a fake group. See draft-davidben-tls-grease-01. */ |
| 2221 | if (ssl->ctx->grease_enabled && |
| 2222 | (!CBB_add_u16(&kse_bytes, |
| 2223 | ssl_get_grease_value(ssl, ssl_grease_group)) || |
| 2224 | !CBB_add_u16(&kse_bytes, 1 /* length */) || |
| 2225 | !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) { |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2229 | /* Predict the most preferred group. */ |
| 2230 | const uint16_t *groups; |
| 2231 | size_t groups_len; |
David Benjamin | f04976b | 2016-10-07 00:37:55 -0400 | [diff] [blame] | 2232 | tls1_get_grouplist(ssl, &groups, &groups_len); |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2233 | if (groups_len == 0) { |
| 2234 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED); |
| 2235 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2236 | } |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2237 | |
| 2238 | group_id = groups[0]; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2239 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2240 | |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2241 | CBB key_exchange; |
| 2242 | if (!CBB_add_u16(&kse_bytes, group_id) || |
| 2243 | !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2244 | !SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) || |
| 2245 | !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &key_exchange) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2246 | !CBB_flush(&kse_bytes)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2247 | return 0; |
| 2248 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2249 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2250 | if (!hs->received_hello_retry_request) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2251 | /* Save the contents of the extension to repeat it in the second |
| 2252 | * ClientHello. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2253 | hs->key_share_bytes_len = CBB_len(&kse_bytes); |
| 2254 | hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes)); |
| 2255 | if (hs->key_share_bytes == NULL) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
| 2258 | } |
| 2259 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2260 | return CBB_flush(out); |
| 2261 | } |
| 2262 | |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2263 | int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret, |
| 2264 | size_t *out_secret_len, |
| 2265 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2266 | CBS peer_key; |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2267 | uint16_t group_id; |
| 2268 | if (!CBS_get_u16(contents, &group_id) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2269 | !CBS_get_u16_length_prefixed(contents, &peer_key) || |
| 2270 | CBS_len(contents) != 0) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2271 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2275 | 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] | 2276 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2277 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2281 | if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, out_secret, out_secret_len, |
| 2282 | out_alert, CBS_data(&peer_key), |
| 2283 | CBS_len(&peer_key))) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2284 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2288 | ssl->s3->new_session->key_exchange_info = group_id; |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2289 | SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2290 | return 1; |
| 2291 | } |
| 2292 | |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2293 | int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found, |
| 2294 | uint8_t **out_secret, |
| 2295 | size_t *out_secret_len, |
| 2296 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2297 | uint16_t group_id; |
| 2298 | CBS key_shares; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2299 | if (!tls1_get_shared_group(ssl, &group_id)) { |
| 2300 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 2301 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
| 2305 | if (!CBS_get_u16_length_prefixed(contents, &key_shares) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2306 | CBS_len(contents) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2307 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2308 | return 0; |
| 2309 | } |
| 2310 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2311 | /* Find the corresponding key share. */ |
| 2312 | int found = 0; |
| 2313 | CBS peer_key; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2314 | while (CBS_len(&key_shares) > 0) { |
| 2315 | uint16_t id; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2316 | CBS peer_key_tmp; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2317 | if (!CBS_get_u16(&key_shares, &id) || |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2318 | !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) { |
| 2319 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2320 | return 0; |
| 2321 | } |
| 2322 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2323 | if (id == group_id) { |
| 2324 | if (found) { |
| 2325 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE); |
| 2326 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2327 | return 0; |
| 2328 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2329 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2330 | found = 1; |
| 2331 | peer_key = peer_key_tmp; |
| 2332 | /* Continue parsing the structure to keep peers honest. */ |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2333 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2334 | } |
| 2335 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2336 | if (!found) { |
| 2337 | *out_found = 0; |
| 2338 | *out_secret = NULL; |
| 2339 | *out_secret_len = 0; |
| 2340 | return 1; |
| 2341 | } |
| 2342 | |
| 2343 | /* Compute the DH secret. */ |
| 2344 | uint8_t *secret = NULL; |
| 2345 | size_t secret_len; |
| 2346 | SSL_ECDH_CTX group; |
| 2347 | memset(&group, 0, sizeof(SSL_ECDH_CTX)); |
| 2348 | CBB public_key; |
| 2349 | if (!CBB_init(&public_key, 32) || |
| 2350 | !SSL_ECDH_CTX_init(&group, group_id) || |
| 2351 | !SSL_ECDH_CTX_accept(&group, &public_key, &secret, &secret_len, |
| 2352 | out_alert, CBS_data(&peer_key), |
| 2353 | CBS_len(&peer_key)) || |
| 2354 | !CBB_finish(&public_key, &ssl->s3->hs->public_key, |
| 2355 | &ssl->s3->hs->public_key_len)) { |
| 2356 | OPENSSL_free(secret); |
| 2357 | SSL_ECDH_CTX_cleanup(&group); |
| 2358 | CBB_cleanup(&public_key); |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2359 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | SSL_ECDH_CTX_cleanup(&group); |
| 2364 | |
| 2365 | *out_secret = secret; |
| 2366 | *out_secret_len = secret_len; |
| 2367 | *out_found = 1; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2368 | return 1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2369 | } |
| 2370 | |
Steven Valdez | 7259f2f | 2016-08-02 16:55:05 -0400 | [diff] [blame] | 2371 | int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2372 | uint16_t group_id; |
| 2373 | CBB kse_bytes, public_key; |
| 2374 | if (!tls1_get_shared_group(ssl, &group_id) || |
| 2375 | !CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
| 2376 | !CBB_add_u16_length_prefixed(out, &kse_bytes) || |
| 2377 | !CBB_add_u16(&kse_bytes, group_id) || |
| 2378 | !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) || |
| 2379 | !CBB_add_bytes(&public_key, ssl->s3->hs->public_key, |
| 2380 | ssl->s3->hs->public_key_len) || |
| 2381 | !CBB_flush(out)) { |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 2385 | OPENSSL_free(ssl->s3->hs->public_key); |
| 2386 | ssl->s3->hs->public_key = NULL; |
| 2387 | ssl->s3->hs->public_key_len = 0; |
| 2388 | |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2389 | ssl->s3->new_session->key_exchange_info = group_id; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2390 | return 1; |
| 2391 | } |
| 2392 | |
| 2393 | |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2394 | /* Supported Versions |
| 2395 | * |
| 2396 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 */ |
| 2397 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2398 | static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2399 | SSL *const ssl = hs->ssl; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2400 | uint16_t min_version, max_version; |
| 2401 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 2402 | return 0; |
| 2403 | } |
| 2404 | |
| 2405 | if (max_version <= TLS1_2_VERSION) { |
| 2406 | return 1; |
| 2407 | } |
| 2408 | |
| 2409 | CBB contents, versions; |
| 2410 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 2411 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2412 | !CBB_add_u8_length_prefixed(&contents, &versions)) { |
| 2413 | return 0; |
| 2414 | } |
| 2415 | |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2416 | /* Add a fake version. See draft-davidben-tls-grease-01. */ |
| 2417 | if (ssl->ctx->grease_enabled && |
| 2418 | !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) { |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2422 | for (uint16_t version = max_version; version >= min_version; version--) { |
| 2423 | if (!CBB_add_u16(&versions, ssl->method->version_to_wire(version))) { |
| 2424 | return 0; |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | if (!CBB_flush(out)) { |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | return 1; |
| 2433 | } |
| 2434 | |
| 2435 | |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2436 | /* Cookie |
| 2437 | * |
| 2438 | * https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 */ |
| 2439 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2440 | static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2441 | if (hs->cookie == NULL) { |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2442 | return 1; |
| 2443 | } |
| 2444 | |
| 2445 | CBB contents, cookie; |
| 2446 | if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) || |
| 2447 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2448 | !CBB_add_u16_length_prefixed(&contents, &cookie) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2449 | !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) || |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2450 | !CBB_flush(out)) { |
| 2451 | return 0; |
| 2452 | } |
| 2453 | |
| 2454 | /* The cookie is no longer needed in memory. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2455 | OPENSSL_free(hs->cookie); |
| 2456 | hs->cookie = NULL; |
| 2457 | hs->cookie_len = 0; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2458 | return 1; |
| 2459 | } |
| 2460 | |
| 2461 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2462 | /* Negotiated Groups |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2463 | * |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2464 | * https://tools.ietf.org/html/rfc4492#section-5.1.2 |
David Benjamin | a128a55 | 2016-10-13 14:26:33 -0400 | [diff] [blame] | 2465 | * 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] | 2466 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2467 | static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2468 | SSL *const ssl = hs->ssl; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2469 | CBB contents, groups_bytes; |
| 2470 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2471 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2472 | !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2473 | return 0; |
| 2474 | } |
| 2475 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2476 | /* Add a fake group. See draft-davidben-tls-grease-01. */ |
| 2477 | if (ssl->ctx->grease_enabled && |
| 2478 | !CBB_add_u16(&groups_bytes, |
| 2479 | ssl_get_grease_value(ssl, ssl_grease_group))) { |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2483 | const uint16_t *groups; |
| 2484 | size_t groups_len; |
David Benjamin | f04976b | 2016-10-07 00:37:55 -0400 | [diff] [blame] | 2485 | tls1_get_grouplist(ssl, &groups, &groups_len); |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2486 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2487 | for (size_t i = 0; i < groups_len; i++) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2488 | if (!CBB_add_u16(&groups_bytes, groups[i])) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2489 | return 0; |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | return CBB_flush(out); |
| 2494 | } |
| 2495 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2496 | static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2497 | uint8_t *out_alert, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2498 | CBS *contents) { |
David Benjamin | 4ac2dc4 | 2016-08-12 15:50:48 -0400 | [diff] [blame] | 2499 | /* This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2500 | * BigIP servers send it nonetheless, so do not enforce this. */ |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2501 | return 1; |
| 2502 | } |
| 2503 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2504 | static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2505 | uint8_t *out_alert, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2506 | CBS *contents) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2507 | if (contents == NULL) { |
| 2508 | return 1; |
| 2509 | } |
| 2510 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2511 | CBS supported_group_list; |
| 2512 | if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2513 | CBS_len(&supported_group_list) == 0 || |
| 2514 | (CBS_len(&supported_group_list) & 1) != 0 || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2515 | CBS_len(contents) != 0) { |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2519 | hs->peer_supported_group_list = |
| 2520 | OPENSSL_malloc(CBS_len(&supported_group_list)); |
| 2521 | if (hs->peer_supported_group_list == NULL) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2522 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2526 | const size_t num_groups = CBS_len(&supported_group_list) / 2; |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2527 | for (size_t i = 0; i < num_groups; i++) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2528 | if (!CBS_get_u16(&supported_group_list, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2529 | &hs->peer_supported_group_list[i])) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2530 | goto err; |
| 2531 | } |
| 2532 | } |
| 2533 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2534 | assert(CBS_len(&supported_group_list) == 0); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2535 | hs->peer_supported_group_list_len = num_groups; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2536 | |
| 2537 | return 1; |
| 2538 | |
| 2539 | err: |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2540 | OPENSSL_free(hs->peer_supported_group_list); |
| 2541 | hs->peer_supported_group_list = NULL; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2542 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2546 | static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2547 | /* Servers don't echo this extension. */ |
| 2548 | return 1; |
| 2549 | } |
| 2550 | |
| 2551 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2552 | /* kExtensions contains all the supported extensions. */ |
| 2553 | static const struct tls_extension kExtensions[] = { |
| 2554 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 2555 | TLSEXT_TYPE_renegotiate, |
| 2556 | NULL, |
| 2557 | ext_ri_add_clienthello, |
| 2558 | ext_ri_parse_serverhello, |
| 2559 | ext_ri_parse_clienthello, |
| 2560 | ext_ri_add_serverhello, |
| 2561 | }, |
| 2562 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2563 | TLSEXT_TYPE_server_name, |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2564 | NULL, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2565 | ext_sni_add_clienthello, |
| 2566 | ext_sni_parse_serverhello, |
| 2567 | ext_sni_parse_clienthello, |
| 2568 | ext_sni_add_serverhello, |
| 2569 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2570 | { |
| 2571 | TLSEXT_TYPE_extended_master_secret, |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 2572 | NULL, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2573 | ext_ems_add_clienthello, |
| 2574 | ext_ems_parse_serverhello, |
| 2575 | ext_ems_parse_clienthello, |
| 2576 | ext_ems_add_serverhello, |
| 2577 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2578 | { |
| 2579 | TLSEXT_TYPE_session_ticket, |
| 2580 | NULL, |
| 2581 | ext_ticket_add_clienthello, |
| 2582 | ext_ticket_parse_serverhello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2583 | /* Ticket extension client parsing is handled in ssl_session.c */ |
| 2584 | ignore_parse_clienthello, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2585 | ext_ticket_add_serverhello, |
| 2586 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2587 | { |
| 2588 | TLSEXT_TYPE_signature_algorithms, |
| 2589 | NULL, |
| 2590 | ext_sigalgs_add_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2591 | forbid_parse_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2592 | ext_sigalgs_parse_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2593 | dont_add_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2594 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2595 | { |
| 2596 | TLSEXT_TYPE_status_request, |
| 2597 | ext_ocsp_init, |
| 2598 | ext_ocsp_add_clienthello, |
| 2599 | ext_ocsp_parse_serverhello, |
| 2600 | ext_ocsp_parse_clienthello, |
| 2601 | ext_ocsp_add_serverhello, |
| 2602 | }, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2603 | { |
| 2604 | TLSEXT_TYPE_next_proto_neg, |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 2605 | NULL, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2606 | ext_npn_add_clienthello, |
| 2607 | ext_npn_parse_serverhello, |
| 2608 | ext_npn_parse_clienthello, |
| 2609 | ext_npn_add_serverhello, |
| 2610 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 2611 | { |
| 2612 | TLSEXT_TYPE_certificate_timestamp, |
| 2613 | NULL, |
| 2614 | ext_sct_add_clienthello, |
| 2615 | ext_sct_parse_serverhello, |
| 2616 | ext_sct_parse_clienthello, |
| 2617 | ext_sct_add_serverhello, |
| 2618 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2619 | { |
| 2620 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
David Benjamin | 35598ae | 2016-11-16 15:38:27 +0900 | [diff] [blame] | 2621 | NULL, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2622 | ext_alpn_add_clienthello, |
| 2623 | ext_alpn_parse_serverhello, |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 2624 | /* ALPN is negotiated late in |ssl_negotiate_alpn|. */ |
| 2625 | ignore_parse_clienthello, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2626 | ext_alpn_add_serverhello, |
| 2627 | }, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2628 | { |
| 2629 | TLSEXT_TYPE_channel_id, |
| 2630 | ext_channel_id_init, |
| 2631 | ext_channel_id_add_clienthello, |
| 2632 | ext_channel_id_parse_serverhello, |
| 2633 | ext_channel_id_parse_clienthello, |
| 2634 | ext_channel_id_add_serverhello, |
| 2635 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 2636 | { |
| 2637 | TLSEXT_TYPE_srtp, |
| 2638 | ext_srtp_init, |
| 2639 | ext_srtp_add_clienthello, |
| 2640 | ext_srtp_parse_serverhello, |
| 2641 | ext_srtp_parse_clienthello, |
| 2642 | ext_srtp_add_serverhello, |
| 2643 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2644 | { |
| 2645 | TLSEXT_TYPE_ec_point_formats, |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2646 | NULL, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2647 | ext_ec_point_add_clienthello, |
| 2648 | ext_ec_point_parse_serverhello, |
| 2649 | ext_ec_point_parse_clienthello, |
| 2650 | ext_ec_point_add_serverhello, |
| 2651 | }, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2652 | { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2653 | TLSEXT_TYPE_key_share, |
| 2654 | NULL, |
| 2655 | ext_key_share_add_clienthello, |
| 2656 | forbid_parse_serverhello, |
| 2657 | ignore_parse_clienthello, |
| 2658 | dont_add_serverhello, |
| 2659 | }, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2660 | { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2661 | TLSEXT_TYPE_psk_key_exchange_modes, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2662 | NULL, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2663 | ext_psk_key_exchange_modes_add_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2664 | forbid_parse_serverhello, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2665 | ext_psk_key_exchange_modes_parse_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2666 | dont_add_serverhello, |
| 2667 | }, |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2668 | { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2669 | TLSEXT_TYPE_early_data, |
| 2670 | NULL, |
| 2671 | ext_early_data_add_clienthello, |
| 2672 | forbid_parse_serverhello, |
| 2673 | ext_early_data_parse_clienthello, |
| 2674 | dont_add_serverhello, |
| 2675 | }, |
| 2676 | { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2677 | TLSEXT_TYPE_supported_versions, |
| 2678 | NULL, |
| 2679 | ext_supported_versions_add_clienthello, |
| 2680 | forbid_parse_serverhello, |
| 2681 | ignore_parse_clienthello, |
| 2682 | dont_add_serverhello, |
| 2683 | }, |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2684 | { |
| 2685 | TLSEXT_TYPE_cookie, |
| 2686 | NULL, |
| 2687 | ext_cookie_add_clienthello, |
| 2688 | forbid_parse_serverhello, |
| 2689 | ignore_parse_clienthello, |
| 2690 | dont_add_serverhello, |
| 2691 | }, |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2692 | /* The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2693 | * intolerant to the last extension being zero-length. See |
| 2694 | * https://crbug.com/363583. */ |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2695 | { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2696 | TLSEXT_TYPE_supported_groups, |
David Benjamin | 43612b6 | 2016-10-07 00:41:50 -0400 | [diff] [blame] | 2697 | NULL, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2698 | ext_supported_groups_add_clienthello, |
| 2699 | ext_supported_groups_parse_serverhello, |
| 2700 | ext_supported_groups_parse_clienthello, |
| 2701 | ext_supported_groups_add_serverhello, |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2702 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2703 | }; |
| 2704 | |
| 2705 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2706 | |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2707 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
David Benjamin | f5d2cd0 | 2016-10-06 19:39:20 -0400 | [diff] [blame] | 2708 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8, |
David Benjamin | 7ca4b42 | 2015-07-13 16:43:47 -0400 | [diff] [blame] | 2709 | too_many_extensions_for_sent_bitset); |
David Benjamin | f5d2cd0 | 2016-10-06 19:39:20 -0400 | [diff] [blame] | 2710 | OPENSSL_COMPILE_ASSERT( |
| 2711 | kNumExtensions <= sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8, |
| 2712 | too_many_extensions_for_received_bitset); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2713 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2714 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2715 | uint16_t value) { |
| 2716 | unsigned i; |
| 2717 | for (i = 0; i < kNumExtensions; i++) { |
| 2718 | if (kExtensions[i].value == value) { |
| 2719 | *out_index = i; |
| 2720 | return &kExtensions[i]; |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | return NULL; |
| 2725 | } |
| 2726 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2727 | int SSL_extension_supported(unsigned extension_value) { |
| 2728 | uint32_t index; |
| 2729 | return extension_value == TLSEXT_TYPE_padding || |
| 2730 | tls_extension_find(&index, extension_value) != NULL; |
| 2731 | } |
| 2732 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2733 | int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) { |
| 2734 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2735 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2736 | if (ssl->client_version == SSL3_VERSION && |
| 2737 | !ssl->s3->send_connection_binding) { |
| 2738 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2739 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2740 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2741 | CBB extensions; |
| 2742 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2743 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2744 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2745 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2746 | hs->extensions.sent = 0; |
| 2747 | hs->custom_extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2748 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2749 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2750 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2751 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2752 | } |
| 2753 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2754 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2755 | uint16_t grease_ext1 = 0; |
| 2756 | if (ssl->ctx->grease_enabled) { |
| 2757 | /* Add a fake empty extension. See draft-davidben-tls-grease-01. */ |
| 2758 | grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1); |
| 2759 | if (!CBB_add_u16(&extensions, grease_ext1) || |
| 2760 | !CBB_add_u16(&extensions, 0 /* zero length */)) { |
| 2761 | goto err; |
| 2762 | } |
| 2763 | } |
| 2764 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2765 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2766 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2767 | if (!kExtensions[i].add_clienthello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2768 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2769 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2770 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2771 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2772 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2773 | if (CBB_len(&extensions) != len_before) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2774 | hs->extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2775 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2776 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2777 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2778 | if (!custom_ext_add_clienthello(ssl, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2779 | goto err; |
| 2780 | } |
| 2781 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2782 | if (ssl->ctx->grease_enabled) { |
| 2783 | /* Add a fake non-empty extension. See draft-davidben-tls-grease-01. */ |
| 2784 | uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2); |
| 2785 | |
| 2786 | /* The two fake extensions must not have the same value. GREASE values are |
| 2787 | * of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different |
| 2788 | * one. */ |
| 2789 | if (grease_ext1 == grease_ext2) { |
| 2790 | grease_ext2 ^= 0x1010; |
| 2791 | } |
| 2792 | |
| 2793 | if (!CBB_add_u16(&extensions, grease_ext2) || |
| 2794 | !CBB_add_u16(&extensions, 1 /* one byte length */) || |
| 2795 | !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) { |
| 2796 | goto err; |
| 2797 | } |
| 2798 | } |
| 2799 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2800 | if (!SSL_is_dtls(ssl)) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2801 | size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2802 | header_len += 2 + CBB_len(&extensions) + psk_extension_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2803 | if (header_len > 0xff && header_len < 0x200) { |
Adam Langley | 10a1a9d | 2015-10-21 14:49:23 -0700 | [diff] [blame] | 2804 | /* Add padding to workaround bugs in F5 terminators. See RFC 7685. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2805 | * |
| 2806 | * NB: because this code works out the length of all existing extensions |
| 2807 | * it MUST always appear last. */ |
David Benjamin | 0a96859 | 2015-07-21 22:06:19 -0400 | [diff] [blame] | 2808 | size_t padding_len = 0x200 - header_len; |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2809 | /* Extensions take at least four bytes to encode. Always include at least |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2810 | * one byte of data if including the extension. WebSphere Application |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2811 | * Server 7.0 is intolerant to the last extension being zero-length. See |
| 2812 | * https://crbug.com/363583. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2813 | if (padding_len >= 4 + 1) { |
| 2814 | padding_len -= 4; |
| 2815 | } else { |
| 2816 | padding_len = 1; |
| 2817 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2818 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2819 | uint8_t *padding_bytes; |
| 2820 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2821 | !CBB_add_u16(&extensions, padding_len) || |
| 2822 | !CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
| 2823 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2824 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2825 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2826 | memset(padding_bytes, 0, padding_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2827 | } |
| 2828 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2829 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2830 | /* The PSK extension must be last, including after the padding. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2831 | if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2832 | goto err; |
| 2833 | } |
| 2834 | |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2835 | /* Discard empty extensions blocks. */ |
| 2836 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2837 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2840 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2841 | |
| 2842 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2843 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2844 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2845 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2846 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2847 | int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) { |
| 2848 | SSL *const ssl = hs->ssl; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2849 | CBB extensions; |
| 2850 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2851 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2854 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 2855 | if (!(hs->extensions.received & (1u << i))) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2856 | /* Don't send extensions that were not received. */ |
| 2857 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2858 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2859 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2860 | if (!kExtensions[i].add_serverhello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2861 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2862 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2863 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2864 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2865 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2866 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2867 | if (!custom_ext_add_serverhello(ssl, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2868 | goto err; |
| 2869 | } |
| 2870 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2871 | /* Discard empty extensions blocks before TLS 1.3. */ |
| 2872 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION && |
| 2873 | CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2874 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2877 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2878 | |
| 2879 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2880 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2881 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2882 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2883 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2884 | static int ssl_scan_clienthello_tlsext( |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2885 | SSL_HANDSHAKE *hs, const struct ssl_early_callback_ctx *client_hello, |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2886 | int *out_alert) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2887 | SSL *const ssl = hs->ssl; |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2888 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2889 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2890 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2894 | hs->extensions.received = 0; |
| 2895 | hs->custom_extensions.received = 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2896 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2897 | CBS extensions; |
| 2898 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 2899 | while (CBS_len(&extensions) != 0) { |
| 2900 | uint16_t type; |
| 2901 | CBS extension; |
| 2902 | |
| 2903 | /* Decode the next extension. */ |
| 2904 | if (!CBS_get_u16(&extensions, &type) || |
| 2905 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2906 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2907 | return 0; |
| 2908 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2909 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2910 | /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 2911 | * ambiguous. Ignore all but the renegotiation_info extension. */ |
| 2912 | if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
| 2913 | continue; |
| 2914 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2915 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2916 | unsigned ext_index; |
| 2917 | const struct tls_extension *const ext = |
| 2918 | tls_extension_find(&ext_index, type); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2919 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2920 | if (ext == NULL) { |
| 2921 | if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2922 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2923 | return 0; |
| 2924 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2925 | continue; |
| 2926 | } |
| 2927 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2928 | hs->extensions.received |= (1u << ext_index); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2929 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2930 | if (!ext->parse_clienthello(hs, &alert, &extension)) { |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2931 | *out_alert = alert; |
| 2932 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2933 | ERR_add_error_dataf("extension %u", (unsigned)type); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2934 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2935 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2936 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2937 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2938 | for (size_t i = 0; i < kNumExtensions; i++) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2939 | if (hs->extensions.received & (1u << i)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2940 | continue; |
| 2941 | } |
| 2942 | |
| 2943 | CBS *contents = NULL, fake_contents; |
| 2944 | static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 2945 | if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 2946 | ssl_client_cipher_list_contains_cipher(client_hello, |
| 2947 | SSL3_CK_SCSV & 0xffff)) { |
| 2948 | /* The renegotiation SCSV was received so pretend that we received a |
| 2949 | * renegotiation extension. */ |
| 2950 | CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 2951 | sizeof(kFakeRenegotiateExtension)); |
| 2952 | contents = &fake_contents; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2953 | hs->extensions.received |= (1u << i); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | /* Extension wasn't observed so call the callback with a NULL |
| 2957 | * parameter. */ |
| 2958 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2959 | if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2960 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2961 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 2962 | *out_alert = alert; |
| 2963 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2967 | return 1; |
| 2968 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2969 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 2970 | int ssl_parse_clienthello_tlsext( |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2971 | SSL_HANDSHAKE *hs, const struct ssl_early_callback_ctx *client_hello) { |
| 2972 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2973 | int alert = -1; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2974 | if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2975 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2976 | return 0; |
| 2977 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2978 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2979 | if (ssl_check_clienthello_tlsext(hs) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2980 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2981 | return 0; |
| 2982 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2983 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2984 | return 1; |
| 2985 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2986 | |
Matt Braithwaite | e564a5b | 2015-09-30 15:24:05 -0700 | [diff] [blame] | 2987 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits); |
| 2988 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 2989 | static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs, |
| 2990 | int *out_alert) { |
| 2991 | SSL *const ssl = hs->ssl; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2992 | /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */ |
| 2993 | if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2994 | return 1; |
| 2995 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2996 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2997 | /* Decode the extensions block and check it is valid. */ |
| 2998 | CBS extensions; |
| 2999 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 3000 | !tls1_check_duplicate_extensions(&extensions)) { |
| 3001 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | uint32_t received = 0; |
| 3006 | while (CBS_len(&extensions) != 0) { |
| 3007 | uint16_t type; |
| 3008 | CBS extension; |
| 3009 | |
| 3010 | /* Decode the next extension. */ |
| 3011 | if (!CBS_get_u16(&extensions, &type) || |
| 3012 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3013 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3014 | return 0; |
| 3015 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3016 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3017 | unsigned ext_index; |
| 3018 | const struct tls_extension *const ext = |
| 3019 | tls_extension_find(&ext_index, type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3020 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3021 | if (ext == NULL) { |
| 3022 | if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3023 | return 0; |
| 3024 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3025 | continue; |
| 3026 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3027 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3028 | if (!(hs->extensions.sent & (1u << ext_index)) && |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3029 | type != TLSEXT_TYPE_renegotiate) { |
| 3030 | /* If the extension was never sent then it is illegal, except for the |
| 3031 | * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */ |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3032 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 3033 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 3034 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3035 | return 0; |
| 3036 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3037 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3038 | received |= (1u << ext_index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 3039 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3040 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3041 | if (!ext->parse_serverhello(hs, &alert, &extension)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3042 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3043 | ERR_add_error_dataf("extension %u", (unsigned)type); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3044 | *out_alert = alert; |
| 3045 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3046 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3047 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3048 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 3049 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3050 | if (!(received & (1u << i))) { |
| 3051 | /* Extension wasn't observed so call the callback with a NULL |
| 3052 | * parameter. */ |
| 3053 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3054 | if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3055 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3056 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3057 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3058 | return 0; |
| 3059 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3060 | } |
| 3061 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3062 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3063 | return 1; |
| 3064 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3065 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3066 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) { |
| 3067 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3068 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 3069 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 3070 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 3071 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3072 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 3073 | ssl->ctx->tlsext_servername_arg); |
| 3074 | } else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3075 | ret = ssl->initial_ctx->tlsext_servername_callback( |
| 3076 | ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3077 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3078 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3079 | switch (ret) { |
| 3080 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3081 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3082 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3083 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3084 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3085 | ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3086 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3087 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3088 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3089 | hs->should_ack_sni = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3090 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3091 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3092 | default: |
| 3093 | return 1; |
| 3094 | } |
| 3095 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 3096 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3097 | static int ssl_check_serverhello_tlsext(SSL_HANDSHAKE *hs) { |
| 3098 | SSL *const ssl = hs->ssl; |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 3099 | int ret = SSL_TLSEXT_ERR_OK; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3100 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3101 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 3102 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3103 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 3104 | ssl->ctx->tlsext_servername_arg); |
| 3105 | } else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3106 | ret = ssl->initial_ctx->tlsext_servername_callback( |
| 3107 | ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3108 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3109 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3110 | switch (ret) { |
| 3111 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3112 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3113 | return -1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 3114 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3115 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3116 | ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3117 | return 1; |
| 3118 | |
| 3119 | default: |
| 3120 | return 1; |
| 3121 | } |
| 3122 | } |
| 3123 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3124 | int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) { |
| 3125 | SSL *const ssl = hs->ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3126 | int alert = -1; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3127 | if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3128 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3129 | return 0; |
| 3130 | } |
| 3131 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame^] | 3132 | if (ssl_check_serverhello_tlsext(hs) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 3133 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3134 | return 0; |
| 3135 | } |
| 3136 | |
| 3137 | return 1; |
| 3138 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3139 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3140 | int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session, |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 3141 | int *out_renew_ticket, const uint8_t *ticket, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3142 | size_t ticket_len, const uint8_t *session_id, |
| 3143 | size_t session_id_len) { |
| 3144 | int ret = 1; /* Most errors are non-fatal. */ |
| 3145 | SSL_CTX *ssl_ctx = ssl->initial_ctx; |
| 3146 | uint8_t *plaintext = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3147 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3148 | HMAC_CTX hmac_ctx; |
| 3149 | HMAC_CTX_init(&hmac_ctx); |
| 3150 | EVP_CIPHER_CTX cipher_ctx; |
| 3151 | EVP_CIPHER_CTX_init(&cipher_ctx); |
| 3152 | |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 3153 | *out_renew_ticket = 0; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3154 | *out_session = NULL; |
| 3155 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 3156 | if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
| 3157 | goto done; |
| 3158 | } |
| 3159 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3160 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3161 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3162 | } |
| 3163 | |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 3164 | /* Ensure there is room for the key name and the largest IV |
| 3165 | * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but |
| 3166 | * the maximum IV length should be well under the minimum size for the |
| 3167 | * session material and HMAC. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3168 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 3169 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3170 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3171 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3172 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3173 | if (ssl_ctx->tlsext_ticket_key_cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3174 | int cb_ret = ssl_ctx->tlsext_ticket_key_cb( |
| 3175 | ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx, |
| 3176 | &hmac_ctx, 0 /* decrypt */); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3177 | if (cb_ret < 0) { |
| 3178 | ret = 0; |
| 3179 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3180 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3181 | if (cb_ret == 0) { |
| 3182 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3183 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3184 | if (cb_ret == 2) { |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 3185 | *out_renew_ticket = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3186 | } |
| 3187 | } else { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3188 | /* Check the key name matches. */ |
| 3189 | if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name, |
| 3190 | SSL_TICKET_KEY_NAME_LEN) != 0) { |
| 3191 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3192 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3193 | if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key, |
| 3194 | sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3195 | NULL) || |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3196 | !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL, |
| 3197 | ssl_ctx->tlsext_tick_aes_key, iv)) { |
| 3198 | ret = 0; |
| 3199 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3200 | } |
| 3201 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3202 | size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3203 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3204 | /* Check the MAC at the end of the ticket. */ |
| 3205 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 3206 | size_t mac_len = HMAC_size(&hmac_ctx); |
| 3207 | 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] | 3208 | /* 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] | 3209 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3210 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3211 | HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len); |
| 3212 | HMAC_Final(&hmac_ctx, mac, NULL); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3213 | int mac_ok = |
| 3214 | CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0; |
| 3215 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3216 | mac_ok = 1; |
| 3217 | #endif |
| 3218 | if (!mac_ok) { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3219 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3220 | } |
| 3221 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3222 | /* Decrypt the session data. */ |
| 3223 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 3224 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 3225 | mac_len; |
| 3226 | plaintext = OPENSSL_malloc(ciphertext_len); |
| 3227 | if (plaintext == NULL) { |
| 3228 | ret = 0; |
| 3229 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3230 | } |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3231 | size_t plaintext_len; |
| 3232 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3233 | memcpy(plaintext, ciphertext, ciphertext_len); |
| 3234 | plaintext_len = ciphertext_len; |
| 3235 | #else |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3236 | if (ciphertext_len >= INT_MAX) { |
| 3237 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3238 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3239 | int len1, len2; |
| 3240 | if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext, |
| 3241 | (int)ciphertext_len) || |
| 3242 | !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) { |
| 3243 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 3244 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3245 | } |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3246 | plaintext_len = (size_t)(len1 + len2); |
| 3247 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3248 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3249 | /* Decode the session. */ |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3250 | SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, plaintext_len); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3251 | if (session == NULL) { |
| 3252 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 3253 | goto done; |
| 3254 | } |
| 3255 | |
| 3256 | /* Copy the client's session ID into the new session, to denote the ticket has |
| 3257 | * been accepted. */ |
| 3258 | memcpy(session->session_id, session_id, session_id_len); |
| 3259 | session->session_id_length = session_id_len; |
| 3260 | |
| 3261 | *out_session = session; |
| 3262 | |
| 3263 | done: |
| 3264 | OPENSSL_free(plaintext); |
| 3265 | HMAC_CTX_cleanup(&hmac_ctx); |
| 3266 | EVP_CIPHER_CTX_cleanup(&cipher_ctx); |
| 3267 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3268 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3269 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3270 | int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3271 | /* Extension ignored for inappropriate versions */ |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 3272 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3273 | return 1; |
| 3274 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 3275 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3276 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 3277 | OPENSSL_free(hs->peer_sigalgs); |
| 3278 | hs->peer_sigalgs = NULL; |
| 3279 | hs->num_peer_sigalgs = 0; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3280 | |
| 3281 | size_t num_sigalgs = CBS_len(in_sigalgs); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3282 | if (num_sigalgs % 2 != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3283 | return 0; |
| 3284 | } |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3285 | num_sigalgs /= 2; |
| 3286 | |
| 3287 | /* supported_signature_algorithms in the certificate request is |
| 3288 | * allowed to be empty. */ |
| 3289 | if (num_sigalgs == 0) { |
| 3290 | return 1; |
| 3291 | } |
| 3292 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 3293 | /* This multiplication doesn't overflow because sizeof(uint16_t) is two |
| 3294 | * and we just divided |num_sigalgs| by two. */ |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3295 | hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t)); |
| 3296 | if (hs->peer_sigalgs == NULL) { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3297 | return 0; |
| 3298 | } |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3299 | hs->num_peer_sigalgs = num_sigalgs; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3300 | |
| 3301 | CBS sigalgs; |
| 3302 | CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs)); |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3303 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 3304 | if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) { |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3305 | return 0; |
| 3306 | } |
| 3307 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3308 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3309 | return 1; |
| 3310 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3311 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3312 | int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 3313 | CERT *cert = ssl->cert; |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3314 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3315 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3316 | /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 3317 | * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */ |
| 3318 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 3319 | int type = ssl_private_key_type(ssl); |
| 3320 | if (type == NID_rsaEncryption) { |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3321 | *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 3322 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3323 | } |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 3324 | if (ssl_is_ecdsa_key_type(type)) { |
| 3325 | *out = SSL_SIGN_ECDSA_SHA1; |
| 3326 | return 1; |
| 3327 | } |
| 3328 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3329 | return 0; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3330 | } |
| 3331 | |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 3332 | const uint16_t *sigalgs = cert->sigalgs; |
| 3333 | size_t num_sigalgs = cert->num_sigalgs; |
| 3334 | if (sigalgs == NULL) { |
| 3335 | sigalgs = kSignSignatureAlgorithms; |
| 3336 | num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3337 | } |
| 3338 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3339 | const uint16_t *peer_sigalgs = hs->peer_sigalgs; |
| 3340 | size_t num_peer_sigalgs = hs->num_peer_sigalgs; |
| 3341 | if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3342 | /* If the client didn't specify any signature_algorithms extension then |
| 3343 | * we can assume that it supports SHA1. See |
| 3344 | * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
| 3345 | static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 3346 | SSL_SIGN_ECDSA_SHA1}; |
| 3347 | peer_sigalgs = kDefaultPeerAlgorithms; |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3348 | num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms); |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3351 | for (size_t i = 0; i < num_sigalgs; i++) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3352 | uint16_t sigalg = sigalgs[i]; |
| 3353 | /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be |
| 3354 | * negotiated. */ |
| 3355 | if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 || |
| 3356 | !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) { |
| 3357 | continue; |
| 3358 | } |
| 3359 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 3360 | for (size_t j = 0; j < num_peer_sigalgs; j++) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3361 | if (sigalg == peer_sigalgs[j]) { |
| 3362 | *out = sigalg; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3363 | return 1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3364 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3365 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3366 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3367 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3368 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 3369 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3370 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3371 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3372 | int tls1_verify_channel_id(SSL *ssl) { |
| 3373 | int ret = 0; |
| 3374 | uint16_t extension_type; |
| 3375 | CBS extension, channel_id; |
| 3376 | |
| 3377 | /* A Channel ID handshake message is structured to contain multiple |
| 3378 | * extensions, but the only one that can be present is Channel ID. */ |
| 3379 | CBS_init(&channel_id, ssl->init_msg, ssl->init_num); |
| 3380 | if (!CBS_get_u16(&channel_id, &extension_type) || |
| 3381 | !CBS_get_u16_length_prefixed(&channel_id, &extension) || |
| 3382 | CBS_len(&channel_id) != 0 || |
| 3383 | extension_type != TLSEXT_TYPE_channel_id || |
| 3384 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 3385 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 3386 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 3387 | return 0; |
| 3388 | } |
| 3389 | |
| 3390 | EC_GROUP *p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 3391 | if (!p256) { |
| 3392 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | EC_KEY *key = NULL; |
| 3397 | EC_POINT *point = NULL; |
| 3398 | BIGNUM x, y; |
| 3399 | ECDSA_SIG sig; |
| 3400 | BN_init(&x); |
| 3401 | BN_init(&y); |
| 3402 | sig.r = BN_new(); |
| 3403 | sig.s = BN_new(); |
| 3404 | if (sig.r == NULL || sig.s == NULL) { |
| 3405 | goto err; |
| 3406 | } |
| 3407 | |
| 3408 | const uint8_t *p = CBS_data(&extension); |
| 3409 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 3410 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 3411 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 3412 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 3413 | goto err; |
| 3414 | } |
| 3415 | |
| 3416 | point = EC_POINT_new(p256); |
| 3417 | if (point == NULL || |
| 3418 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
| 3419 | goto err; |
| 3420 | } |
| 3421 | |
| 3422 | key = EC_KEY_new(); |
| 3423 | if (key == NULL || |
| 3424 | !EC_KEY_set_group(key, p256) || |
| 3425 | !EC_KEY_set_public_key(key, point)) { |
| 3426 | goto err; |
| 3427 | } |
| 3428 | |
| 3429 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3430 | size_t digest_len; |
| 3431 | if (!tls1_channel_id_hash(ssl, digest, &digest_len)) { |
| 3432 | goto err; |
| 3433 | } |
| 3434 | |
| 3435 | int sig_ok = ECDSA_do_verify(digest, digest_len, &sig, key); |
| 3436 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3437 | sig_ok = 1; |
| 3438 | #endif |
| 3439 | if (!sig_ok) { |
| 3440 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 3441 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 3442 | ssl->s3->tlsext_channel_id_valid = 0; |
| 3443 | goto err; |
| 3444 | } |
| 3445 | |
| 3446 | memcpy(ssl->s3->tlsext_channel_id, p, 64); |
| 3447 | ret = 1; |
| 3448 | |
| 3449 | err: |
| 3450 | BN_free(&x); |
| 3451 | BN_free(&y); |
| 3452 | BN_free(sig.r); |
| 3453 | BN_free(sig.s); |
| 3454 | EC_KEY_free(key); |
| 3455 | EC_POINT_free(point); |
| 3456 | EC_GROUP_free(p256); |
| 3457 | return ret; |
| 3458 | } |
| 3459 | |
| 3460 | int tls1_write_channel_id(SSL *ssl, CBB *cbb) { |
| 3461 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3462 | size_t digest_len; |
| 3463 | if (!tls1_channel_id_hash(ssl, digest, &digest_len)) { |
| 3464 | return 0; |
| 3465 | } |
| 3466 | |
| 3467 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
| 3468 | if (ec_key == NULL) { |
| 3469 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 3470 | return 0; |
| 3471 | } |
| 3472 | |
| 3473 | int ret = 0; |
| 3474 | BIGNUM *x = BN_new(); |
| 3475 | BIGNUM *y = BN_new(); |
| 3476 | ECDSA_SIG *sig = NULL; |
| 3477 | if (x == NULL || y == NULL || |
| 3478 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 3479 | EC_KEY_get0_public_key(ec_key), |
| 3480 | x, y, NULL)) { |
| 3481 | goto err; |
| 3482 | } |
| 3483 | |
| 3484 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
| 3485 | if (sig == NULL) { |
| 3486 | goto err; |
| 3487 | } |
| 3488 | |
| 3489 | CBB child; |
| 3490 | if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) || |
| 3491 | !CBB_add_u16_length_prefixed(cbb, &child) || |
| 3492 | !BN_bn2cbb_padded(&child, 32, x) || |
| 3493 | !BN_bn2cbb_padded(&child, 32, y) || |
| 3494 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 3495 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
| 3496 | !CBB_flush(cbb)) { |
| 3497 | goto err; |
| 3498 | } |
| 3499 | |
| 3500 | ret = 1; |
| 3501 | |
| 3502 | err: |
| 3503 | BN_free(x); |
| 3504 | BN_free(y); |
| 3505 | ECDSA_SIG_free(sig); |
| 3506 | return ret; |
| 3507 | } |
| 3508 | |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3509 | 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] | 3510 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 3511 | uint8_t *msg; |
| 3512 | size_t msg_len; |
| 3513 | if (!tls13_get_cert_verify_signature_input(ssl, &msg, &msg_len, |
| 3514 | ssl_cert_verify_channel_id)) { |
| 3515 | return 0; |
| 3516 | } |
| 3517 | SHA256(msg, msg_len, out); |
| 3518 | *out_len = SHA256_DIGEST_LENGTH; |
| 3519 | OPENSSL_free(msg); |
| 3520 | return 1; |
| 3521 | } |
| 3522 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3523 | SHA256_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3524 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3525 | SHA256_Init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3526 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3527 | SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3528 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3529 | if (ssl->session != NULL) { |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3530 | static const char kResumptionMagic[] = "Resumption"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3531 | SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3532 | if (ssl->session->original_handshake_hash_len == 0) { |
| 3533 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3534 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3535 | } |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3536 | SHA256_Update(&ctx, ssl->session->original_handshake_hash, |
| 3537 | ssl->session->original_handshake_hash_len); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | uint8_t handshake_hash[EVP_MAX_MD_SIZE]; |
| 3541 | int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash, |
| 3542 | sizeof(handshake_hash)); |
| 3543 | if (handshake_hash_len < 0) { |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3544 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3545 | } |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3546 | SHA256_Update(&ctx, handshake_hash, (size_t)handshake_hash_len); |
| 3547 | SHA256_Final(out, &ctx); |
| 3548 | *out_len = SHA256_DIGEST_LENGTH; |
| 3549 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3550 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3551 | |
| 3552 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3553 | * 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] | 3554 | * data. */ |
| 3555 | int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3556 | int digest_len; |
| 3557 | /* This function should never be called for a resumed session because the |
| 3558 | * handshake hashes that we wish to record are for the original, full |
| 3559 | * handshake. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3560 | if (ssl->session != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3561 | return -1; |
| 3562 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3563 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3564 | digest_len = |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3565 | tls1_handshake_digest( |
| 3566 | ssl, ssl->s3->new_session->original_handshake_hash, |
| 3567 | sizeof(ssl->s3->new_session->original_handshake_hash)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3568 | if (digest_len < 0) { |
| 3569 | return -1; |
| 3570 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3571 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3572 | ssl->s3->new_session->original_handshake_hash_len = digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3574 | return 1; |
| 3575 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3576 | |
| 3577 | int ssl_do_channel_id_callback(SSL *ssl) { |
| 3578 | if (ssl->tlsext_channel_id_private != NULL || |
| 3579 | ssl->ctx->channel_id_cb == NULL) { |
| 3580 | return 1; |
| 3581 | } |
| 3582 | |
| 3583 | EVP_PKEY *key = NULL; |
| 3584 | ssl->ctx->channel_id_cb(ssl, &key); |
| 3585 | if (key == NULL) { |
| 3586 | /* The caller should try again later. */ |
| 3587 | return 1; |
| 3588 | } |
| 3589 | |
| 3590 | int ret = SSL_set1_tls_channel_id(ssl, key); |
| 3591 | EVP_PKEY_free(key); |
| 3592 | return ret; |
| 3593 | } |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3594 | |
| 3595 | int ssl_is_sct_list_valid(const CBS *contents) { |
| 3596 | /* Shallow parse the SCT list for sanity. By the RFC |
| 3597 | * (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any |
| 3598 | * of the SCTs may be empty. */ |
| 3599 | CBS copy = *contents; |
| 3600 | CBS sct_list; |
| 3601 | if (!CBS_get_u16_length_prefixed(©, &sct_list) || |
| 3602 | CBS_len(©) != 0 || |
| 3603 | CBS_len(&sct_list) == 0) { |
| 3604 | return 0; |
| 3605 | } |
| 3606 | |
| 3607 | while (CBS_len(&sct_list) > 0) { |
| 3608 | CBS sct; |
| 3609 | if (!CBS_get_u16_length_prefixed(&sct_list, &sct) || |
| 3610 | CBS_len(&sct) == 0) { |
| 3611 | return 0; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | return 1; |
| 3616 | } |