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