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" |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 127 | |
| 128 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 129 | static int ssl_check_clienthello_tlsext(SSL *ssl); |
| 130 | static int ssl_check_serverhello_tlsext(SSL *ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 132 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 133 | uint16_t u1 = *((const uint16_t *)p1); |
| 134 | uint16_t u2 = *((const uint16_t *)p2); |
| 135 | if (u1 < u2) { |
| 136 | return -1; |
| 137 | } else if (u1 > u2) { |
| 138 | return 1; |
| 139 | } else { |
| 140 | return 0; |
| 141 | } |
| 142 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 143 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 144 | /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 145 | * more than one extension of the same type in a ClientHello or ServerHello. |
| 146 | * 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] | 147 | * out. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 148 | static int tls1_check_duplicate_extensions(const CBS *cbs) { |
| 149 | CBS extensions = *cbs; |
| 150 | size_t num_extensions = 0, i = 0; |
| 151 | uint16_t *extension_types = NULL; |
| 152 | int ret = 0; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 153 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 154 | /* First pass: count the extensions. */ |
| 155 | while (CBS_len(&extensions) > 0) { |
| 156 | uint16_t type; |
| 157 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 158 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 159 | if (!CBS_get_u16(&extensions, &type) || |
| 160 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 161 | goto done; |
| 162 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 163 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 164 | num_extensions++; |
| 165 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 166 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 167 | if (num_extensions == 0) { |
| 168 | return 1; |
| 169 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 170 | |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 171 | extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 172 | if (extension_types == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 173 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 174 | goto done; |
| 175 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 176 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 177 | /* Second pass: gather the extension types. */ |
| 178 | extensions = *cbs; |
| 179 | for (i = 0; i < num_extensions; i++) { |
| 180 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 181 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 182 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 183 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 184 | /* This should not happen. */ |
| 185 | goto done; |
| 186 | } |
| 187 | } |
| 188 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 189 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 190 | /* Sort the extensions and make sure there are no duplicates. */ |
| 191 | qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t); |
| 192 | for (i = 1; i < num_extensions; i++) { |
| 193 | if (extension_types[i - 1] == extension_types[i]) { |
| 194 | goto done; |
| 195 | } |
| 196 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 197 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 198 | ret = 1; |
| 199 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 200 | done: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 201 | OPENSSL_free(extension_types); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 202 | return ret; |
| 203 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 204 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 205 | char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) { |
| 206 | CBS client_hello, session_id, cipher_suites, compression_methods, extensions; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 207 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 208 | CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 210 | if (/* Skip client version. */ |
| 211 | !CBS_skip(&client_hello, 2) || |
| 212 | /* Skip client nonce. */ |
| 213 | !CBS_skip(&client_hello, 32) || |
| 214 | /* Extract session_id. */ |
| 215 | !CBS_get_u8_length_prefixed(&client_hello, &session_id)) { |
| 216 | return 0; |
| 217 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 219 | ctx->session_id = CBS_data(&session_id); |
| 220 | ctx->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 222 | /* Skip past DTLS cookie */ |
| 223 | if (SSL_IS_DTLS(ctx->ssl)) { |
| 224 | CBS cookie; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 225 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 226 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) { |
| 227 | return 0; |
| 228 | } |
| 229 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 231 | /* Extract cipher_suites. */ |
| 232 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 233 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) { |
| 234 | return 0; |
| 235 | } |
| 236 | ctx->cipher_suites = CBS_data(&cipher_suites); |
| 237 | ctx->cipher_suites_len = CBS_len(&cipher_suites); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 239 | /* Extract compression_methods. */ |
| 240 | if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 241 | CBS_len(&compression_methods) < 1) { |
| 242 | return 0; |
| 243 | } |
| 244 | ctx->compression_methods = CBS_data(&compression_methods); |
| 245 | ctx->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 246 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 247 | /* If the ClientHello ends here then it's valid, but doesn't have any |
| 248 | * extensions. (E.g. SSLv3.) */ |
| 249 | if (CBS_len(&client_hello) == 0) { |
| 250 | ctx->extensions = NULL; |
| 251 | ctx->extensions_len = 0; |
| 252 | return 1; |
| 253 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 255 | /* Extract extensions and check it is valid. */ |
| 256 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 257 | !tls1_check_duplicate_extensions(&extensions) || |
| 258 | CBS_len(&client_hello) != 0) { |
| 259 | return 0; |
| 260 | } |
| 261 | ctx->extensions = CBS_data(&extensions); |
| 262 | ctx->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 264 | return 1; |
| 265 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 266 | |
David Benjamin | d4c2bce | 2015-10-17 12:28:18 -0400 | [diff] [blame] | 267 | int SSL_early_callback_ctx_extension_get( |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | const struct ssl_early_callback_ctx *ctx, uint16_t extension_type, |
| 269 | const uint8_t **out_data, size_t *out_len) { |
| 270 | CBS extensions; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 271 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 272 | CBS_init(&extensions, ctx->extensions, ctx->extensions_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | while (CBS_len(&extensions) != 0) { |
| 275 | uint16_t type; |
| 276 | CBS extension; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 277 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 278 | /* Decode the next extension. */ |
| 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) { |
| 285 | *out_data = CBS_data(&extension); |
| 286 | *out_len = CBS_len(&extension); |
| 287 | return 1; |
| 288 | } |
| 289 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 291 | return 0; |
| 292 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 294 | static const uint16_t eccurves_default[] = { |
David Benjamin | fc6e5a7 | 2016-01-19 15:36:43 -0500 | [diff] [blame] | 295 | SSL_CURVE_X25519, |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 296 | SSL_CURVE_SECP256R1, |
| 297 | SSL_CURVE_SECP384R1, |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 298 | #if defined(BORINGSSL_ANDROID_SYSTEM) |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 299 | SSL_CURVE_SECP521R1, |
Adam Langley | d98dc13 | 2015-09-23 16:41:33 -0700 | [diff] [blame] | 300 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 301 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 303 | /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the |
| 304 | * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the |
| 305 | * peer's curve list. Otherwise, return the preferred list. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 306 | static void tls1_get_curvelist(SSL *ssl, int get_peer_curves, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | const uint16_t **out_curve_ids, |
| 308 | size_t *out_curve_ids_len) { |
| 309 | if (get_peer_curves) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 310 | /* Only clients send a curve list, so this function is only called |
| 311 | * on the server. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 312 | assert(ssl->server); |
| 313 | *out_curve_ids = ssl->s3->tmp.peer_ellipticcurvelist; |
| 314 | *out_curve_ids_len = ssl->s3->tmp.peer_ellipticcurvelist_length; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 315 | return; |
| 316 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 318 | *out_curve_ids = ssl->tlsext_ellipticcurvelist; |
| 319 | *out_curve_ids_len = ssl->tlsext_ellipticcurvelist_length; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | if (!*out_curve_ids) { |
| 321 | *out_curve_ids = eccurves_default; |
| 322 | *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]); |
| 323 | } |
| 324 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 325 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 326 | int tls1_get_shared_curve(SSL *ssl, uint16_t *out_curve_id) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 327 | const uint16_t *curves, *peer_curves, *pref, *supp; |
| 328 | size_t curves_len, peer_curves_len, pref_len, supp_len, i, j; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 329 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 330 | /* Can't do anything on client side */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 331 | if (ssl->server == 0) { |
| 332 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 333 | } |
| 334 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 335 | tls1_get_curvelist(ssl, 0 /* local curves */, &curves, &curves_len); |
| 336 | tls1_get_curvelist(ssl, 1 /* peer curves */, &peer_curves, &peer_curves_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 337 | |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 338 | if (peer_curves_len == 0) { |
| 339 | /* Clients are not required to send a supported_curves extension. In this |
| 340 | * case, the server is free to pick any curve it likes. See RFC 4492, |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 341 | * section 4, paragraph 3. |
| 342 | * |
| 343 | * However, in the interests of compatibility, we will skip ECDH if the |
| 344 | * client didn't send an extension because we can't be sure that they'll |
| 345 | * support our favoured curve. */ |
| 346 | return 0; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 347 | } |
| 348 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 349 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 350 | pref = curves; |
| 351 | pref_len = curves_len; |
| 352 | supp = peer_curves; |
| 353 | supp_len = peer_curves_len; |
| 354 | } else { |
| 355 | pref = peer_curves; |
| 356 | pref_len = peer_curves_len; |
| 357 | supp = curves; |
| 358 | supp_len = curves_len; |
| 359 | } |
| 360 | |
| 361 | for (i = 0; i < pref_len; i++) { |
| 362 | for (j = 0; j < supp_len; j++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 363 | if (pref[i] == supp[j]) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 364 | *out_curve_id = pref[i]; |
| 365 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 370 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 373 | int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 374 | const int *curves, size_t ncurves) { |
| 375 | uint16_t *curve_ids; |
| 376 | size_t i; |
| 377 | |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 378 | curve_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 379 | if (curve_ids == NULL) { |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | for (i = 0; i < ncurves; i++) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 384 | if (!ssl_nid_to_curve_id(&curve_ids[i], curves[i])) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | OPENSSL_free(curve_ids); |
| 386 | return 0; |
| 387 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 388 | } |
| 389 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 390 | OPENSSL_free(*out_curve_ids); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 391 | *out_curve_ids = curve_ids; |
| 392 | *out_curve_ids_len = ncurves; |
| 393 | |
| 394 | return 1; |
| 395 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 396 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 397 | /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the |
| 398 | * TLS curve ID and point format, respectively, for |ec|. It returns one on |
| 399 | * success and zero on failure. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 400 | static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, |
| 401 | uint8_t *out_comp_id, EC_KEY *ec) { |
| 402 | int nid; |
| 403 | uint16_t id; |
| 404 | const EC_GROUP *grp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 406 | if (ec == NULL) { |
| 407 | return 0; |
| 408 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | grp = EC_KEY_get0_group(ec); |
| 411 | if (grp == NULL) { |
| 412 | return 0; |
| 413 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 414 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 415 | /* Determine curve ID */ |
| 416 | nid = EC_GROUP_get_curve_name(grp); |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 417 | if (!ssl_nid_to_curve_id(&id, nid)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 418 | return 0; |
| 419 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 420 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 421 | /* Set the named curve ID. Arbitrary explicit curves are not supported. */ |
| 422 | *out_curve_id = id; |
| 423 | |
| 424 | if (out_comp_id) { |
| 425 | if (EC_KEY_get0_public_key(ec) == NULL) { |
| 426 | return 0; |
| 427 | } |
| 428 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { |
| 429 | *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 430 | } else { |
| 431 | *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return 1; |
| 436 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 437 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 438 | /* tls1_check_curve_id returns one if |curve_id| is consistent with both our |
| 439 | * and the peer's curve preferences. Note: if called as the client, only our |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 440 | * preferences are checked; the peer (the server) does not send preferences. */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 441 | int tls1_check_curve_id(SSL *ssl, uint16_t curve_id) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 442 | const uint16_t *curves; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 443 | size_t curves_len, i, get_peer_curves; |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 444 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 445 | /* Check against our list, then the peer's list. */ |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 446 | for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 447 | if (get_peer_curves && !ssl->server) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 448 | /* Servers do not present a preference list so, if we are a client, only |
| 449 | * check our list. */ |
| 450 | continue; |
| 451 | } |
| 452 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 453 | tls1_get_curvelist(ssl, get_peer_curves, &curves, &curves_len); |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 454 | if (get_peer_curves && curves_len == 0) { |
| 455 | /* Clients are not required to send a supported_curves extension. In this |
| 456 | * case, the server is free to pick any curve it likes. See RFC 4492, |
| 457 | * section 4, paragraph 3. */ |
| 458 | continue; |
| 459 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | for (i = 0; i < curves_len; i++) { |
| 461 | if (curves[i] == curve_id) { |
| 462 | break; |
| 463 | } |
| 464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 466 | if (i == curves_len) { |
| 467 | return 0; |
| 468 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 469 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 470 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | return 1; |
| 472 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 473 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 474 | int tls1_check_ec_cert(SSL *ssl, X509 *x) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | int ret = 0; |
| 476 | EVP_PKEY *pkey = X509_get_pubkey(x); |
| 477 | uint16_t curve_id; |
| 478 | uint8_t comp_id; |
| 479 | |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 480 | if (!pkey) { |
| 481 | goto done; |
| 482 | } |
| 483 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 484 | if (ec_key == NULL || |
| 485 | !tls1_curve_params_from_ec_key(&curve_id, &comp_id, ec_key) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 486 | !tls1_check_curve_id(ssl, curve_id) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 487 | comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 488 | goto done; |
| 489 | } |
| 490 | |
| 491 | ret = 1; |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 492 | |
| 493 | done: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 494 | EVP_PKEY_free(pkey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 495 | return ret; |
| 496 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 497 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | /* List of supported signature algorithms and hashes. Should make this |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 499 | * customisable at some point, for now include everything we support. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 505 | #define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 507 | static const uint8_t tls12_sigalgs[] = { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 508 | tlsext_sigalg(TLSEXT_hash_sha512) |
| 509 | tlsext_sigalg(TLSEXT_hash_sha384) |
| 510 | tlsext_sigalg(TLSEXT_hash_sha256) |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 511 | tlsext_sigalg(TLSEXT_hash_sha1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | }; |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 513 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 514 | size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) { |
David Benjamin | 3dd9016 | 2015-08-09 11:26:40 -0400 | [diff] [blame] | 515 | *psigs = tls12_sigalgs; |
| 516 | return sizeof(tls12_sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 517 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 519 | int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert, |
| 520 | uint8_t hash, uint8_t signature, EVP_PKEY *pkey) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 521 | const uint8_t *sent_sigs; |
| 522 | size_t sent_sigslen, i; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 523 | int sigalg = tls12_get_sigid(pkey->type); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 524 | |
| 525 | /* Should never happen */ |
| 526 | if (sigalg == -1) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 527 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 528 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 529 | return 0; |
| 530 | } |
| 531 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 532 | /* Check key type is consistent with signature */ |
| 533 | if (sigalg != signature) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 534 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 535 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 536 | return 0; |
| 537 | } |
| 538 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 539 | /* Check signature matches a type we sent */ |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 540 | sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 541 | for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { |
| 542 | if (hash == sent_sigs[0] && signature == sent_sigs[1]) { |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | |
David Benjamin | 788be4a | 2015-10-30 17:50:57 -0400 | [diff] [blame] | 547 | if (i == sent_sigslen) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 548 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 549 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | *out_md = tls12_get_hash(hash); |
| 554 | if (*out_md == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 555 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 556 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | return 1; |
| 561 | } |
| 562 | |
| 563 | /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 564 | * supported or doesn't appear in supported signature algorithms. Unlike |
| 565 | * ssl_cipher_get_disabled this applies to a specific session and not global |
| 566 | * settings. */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 567 | void ssl_set_client_disabled(SSL *ssl) { |
| 568 | CERT *c = ssl->cert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 569 | const uint8_t *sigalgs; |
| 570 | size_t i, sigalgslen; |
| 571 | int have_rsa = 0, have_ecdsa = 0; |
| 572 | c->mask_a = 0; |
| 573 | c->mask_k = 0; |
| 574 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 575 | /* Now go through all signature algorithms seeing if we support any for RSA, |
| 576 | * DSA, ECDSA. Do this for all versions not just TLS 1.2. */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 577 | sigalgslen = tls12_get_psigalgs(ssl, &sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 578 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { |
| 579 | switch (sigalgs[1]) { |
| 580 | case TLSEXT_signature_rsa: |
| 581 | have_rsa = 1; |
| 582 | break; |
| 583 | |
| 584 | case TLSEXT_signature_ecdsa: |
| 585 | have_ecdsa = 1; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* Disable auth if we don't include any appropriate signature algorithms. */ |
| 591 | if (!have_rsa) { |
| 592 | c->mask_a |= SSL_aRSA; |
| 593 | } |
| 594 | if (!have_ecdsa) { |
| 595 | c->mask_a |= SSL_aECDSA; |
| 596 | } |
| 597 | |
| 598 | /* with PSK there must be client callback set */ |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 599 | if (!ssl->psk_client_callback) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 600 | c->mask_a |= SSL_aPSK; |
| 601 | c->mask_k |= SSL_kPSK; |
| 602 | } |
| 603 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 604 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 605 | /* tls_extension represents a TLS extension that is handled internally. The |
| 606 | * |init| function is called for each handshake, before any other functions of |
| 607 | * the extension. Then the add and parse callbacks are called as needed. |
| 608 | * |
| 609 | * The parse callbacks receive a |CBS| that contains the contents of the |
| 610 | * extension (i.e. not including the type and length bytes). If an extension is |
| 611 | * not received then the parse callbacks will be called with a NULL CBS so that |
| 612 | * they can do any processing needed to handle the absence of an extension. |
| 613 | * |
| 614 | * The add callbacks receive a |CBB| to which the extension can be appended but |
| 615 | * the function is responsible for appending the type and length bytes too. |
| 616 | * |
| 617 | * All callbacks return one for success and zero for error. If a parse function |
| 618 | * returns zero then a fatal alert with value |*out_alert| will be sent. If |
| 619 | * |*out_alert| isn't set, then a |decode_error| alert will be sent. */ |
| 620 | struct tls_extension { |
| 621 | uint16_t value; |
| 622 | void (*init)(SSL *ssl); |
| 623 | |
| 624 | int (*add_clienthello)(SSL *ssl, CBB *out); |
| 625 | int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 626 | |
| 627 | int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 628 | int (*add_serverhello)(SSL *ssl, CBB *out); |
| 629 | }; |
| 630 | |
| 631 | |
| 632 | /* Server name indication (SNI). |
| 633 | * |
| 634 | * https://tools.ietf.org/html/rfc6066#section-3. */ |
| 635 | |
| 636 | static void ext_sni_init(SSL *ssl) { |
| 637 | ssl->s3->tmp.should_ack_sni = 0; |
| 638 | } |
| 639 | |
| 640 | static int ext_sni_add_clienthello(SSL *ssl, CBB *out) { |
| 641 | if (ssl->tlsext_hostname == NULL) { |
| 642 | return 1; |
| 643 | } |
| 644 | |
| 645 | CBB contents, server_name_list, name; |
| 646 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 647 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 648 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 649 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 650 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 651 | !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 652 | strlen(ssl->tlsext_hostname)) || |
| 653 | !CBB_flush(out)) { |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | return 1; |
| 658 | } |
| 659 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 660 | static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 661 | CBS *contents) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 662 | if (contents == NULL) { |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | if (CBS_len(contents) != 0) { |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | assert(ssl->tlsext_hostname != NULL); |
| 671 | |
| 672 | if (!ssl->hit) { |
| 673 | assert(ssl->session->tlsext_hostname == NULL); |
| 674 | ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname); |
| 675 | if (!ssl->session->tlsext_hostname) { |
| 676 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 677 | return 0; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | return 1; |
| 682 | } |
| 683 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 684 | static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 685 | CBS *contents) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 686 | if (contents == NULL) { |
| 687 | return 1; |
| 688 | } |
| 689 | |
| 690 | /* The servername extension is treated as follows: |
| 691 | * |
| 692 | * - Only the hostname type is supported with a maximum length of 255. |
| 693 | * - The servername is rejected if too long or if it contains zeros, in |
| 694 | * which case an fatal alert is generated. |
| 695 | * - The servername field is maintained together with the session cache. |
| 696 | * - When a session is resumed, the servername callback is invoked in order |
| 697 | * to allow the application to position itself to the right context. |
| 698 | * - The servername is acknowledged if it is new for a session or when |
| 699 | * it is identical to a previously used for the same session. |
| 700 | * Applications can control the behaviour. They can at any time |
| 701 | * set a 'desirable' servername for a new SSL object. This can be the |
| 702 | * case for example with HTTPS when a Host: header field is received and |
| 703 | * a renegotiation is requested. In this case, a possible servername |
| 704 | * presented in the new client hello is only acknowledged if it matches |
| 705 | * the value of the Host: field. |
| 706 | * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 707 | * if they provide for changing an explicit servername context for the |
| 708 | * session, |
| 709 | * i.e. when the session has been established with a servername extension. |
| 710 | */ |
| 711 | |
| 712 | CBS server_name_list; |
| 713 | char have_seen_host_name = 0; |
| 714 | |
| 715 | if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
| 716 | CBS_len(&server_name_list) == 0 || |
| 717 | CBS_len(contents) != 0) { |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | /* Decode each ServerName in the extension. */ |
| 722 | while (CBS_len(&server_name_list) > 0) { |
| 723 | uint8_t name_type; |
| 724 | CBS host_name; |
| 725 | |
| 726 | if (!CBS_get_u8(&server_name_list, &name_type) || |
| 727 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) { |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* Only host_name is supported. */ |
| 732 | if (name_type != TLSEXT_NAMETYPE_host_name) { |
| 733 | continue; |
| 734 | } |
| 735 | |
| 736 | if (have_seen_host_name) { |
| 737 | /* The ServerNameList MUST NOT contain more than one name of the same |
| 738 | * name_type. */ |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | have_seen_host_name = 1; |
| 743 | |
| 744 | if (CBS_len(&host_name) == 0 || |
| 745 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 746 | CBS_contains_zero_byte(&host_name)) { |
| 747 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | if (!ssl->hit) { |
| 752 | assert(ssl->session->tlsext_hostname == NULL); |
| 753 | if (ssl->session->tlsext_hostname) { |
| 754 | /* This should be impossible. */ |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* Copy the hostname as a string. */ |
| 759 | if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) { |
| 760 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | ssl->s3->tmp.should_ack_sni = 1; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return 1; |
| 769 | } |
| 770 | |
| 771 | static int ext_sni_add_serverhello(SSL *ssl, CBB *out) { |
| 772 | if (ssl->hit || |
| 773 | !ssl->s3->tmp.should_ack_sni || |
| 774 | ssl->session->tlsext_hostname == NULL) { |
| 775 | return 1; |
| 776 | } |
| 777 | |
| 778 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 779 | !CBB_add_u16(out, 0 /* length */)) { |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | return 1; |
| 784 | } |
| 785 | |
| 786 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 787 | /* Renegotiation indication. |
| 788 | * |
| 789 | * https://tools.ietf.org/html/rfc5746 */ |
| 790 | |
| 791 | static int ext_ri_add_clienthello(SSL *ssl, CBB *out) { |
| 792 | CBB contents, prev_finished; |
| 793 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 794 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 795 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 796 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 797 | ssl->s3->previous_client_finished_len) || |
| 798 | !CBB_flush(out)) { |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | return 1; |
| 803 | } |
| 804 | |
| 805 | static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 806 | CBS *contents) { |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 807 | /* Servers may not switch between omitting the extension and supporting it. |
| 808 | * See RFC 5746, sections 3.5 and 4.2. */ |
| 809 | if (ssl->s3->initial_handshake_complete && |
| 810 | (contents != NULL) != ssl->s3->send_connection_binding) { |
| 811 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 812 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 813 | return 0; |
| 814 | } |
| 815 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 816 | if (contents == NULL) { |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 817 | /* 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] | 818 | * RI even on initial ServerHello because the client doesn't see any |
| 819 | * renegotiation during an attack. However this would mean we could not |
| 820 | * connect to any server which doesn't support RI. |
| 821 | * |
David Benjamin | e9cddb8 | 2015-11-23 14:36:40 -0500 | [diff] [blame] | 822 | * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 823 | * practical terms every client sets it so it's just assumed here. */ |
| 824 | return 1; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 828 | ssl->s3->previous_server_finished_len; |
| 829 | |
| 830 | /* Check for logic errors */ |
| 831 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 832 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
| 833 | |
| 834 | /* Parse out the extension contents. */ |
| 835 | CBS renegotiated_connection; |
| 836 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 837 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 838 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 839 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /* Check that the extension matches. */ |
| 844 | if (CBS_len(&renegotiated_connection) != expected_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 845 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 846 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | const uint8_t *d = CBS_data(&renegotiated_connection); |
| 851 | if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 852 | ssl->s3->previous_client_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 853 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 854 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 855 | return 0; |
| 856 | } |
| 857 | d += ssl->s3->previous_client_finished_len; |
| 858 | |
| 859 | if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 860 | ssl->s3->previous_server_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 861 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 862 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 863 | return 0; |
| 864 | } |
| 865 | ssl->s3->send_connection_binding = 1; |
| 866 | |
| 867 | return 1; |
| 868 | } |
| 869 | |
| 870 | static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 871 | CBS *contents) { |
| 872 | /* Renegotiation isn't supported as a server so this function should never be |
| 873 | * called after the initial handshake. */ |
| 874 | assert(!ssl->s3->initial_handshake_complete); |
| 875 | |
| 876 | CBS fake_contents; |
| 877 | static const uint8_t kFakeExtension[] = {0}; |
| 878 | |
| 879 | if (contents == NULL) { |
| 880 | if (ssl->s3->send_connection_binding) { |
| 881 | /* The renegotiation SCSV was received so pretend that we received a |
| 882 | * renegotiation extension. */ |
| 883 | CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension)); |
| 884 | contents = &fake_contents; |
| 885 | /* We require that the renegotiation extension is at index zero of |
| 886 | * kExtensions. */ |
| 887 | ssl->s3->tmp.extensions.received |= (1u << 0); |
| 888 | } else { |
| 889 | return 1; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | CBS renegotiated_connection; |
| 894 | |
| 895 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 896 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 897 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /* Check that the extension matches */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 902 | if (!CBS_mem_equal(&renegotiated_connection, |
| 903 | ssl->s3->previous_client_finished, |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 904 | ssl->s3->previous_client_finished_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 905 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 906 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | ssl->s3->send_connection_binding = 1; |
| 911 | |
| 912 | return 1; |
| 913 | } |
| 914 | |
| 915 | static int ext_ri_add_serverhello(SSL *ssl, CBB *out) { |
| 916 | CBB contents, prev_finished; |
| 917 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 918 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 919 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 920 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 921 | ssl->s3->previous_client_finished_len) || |
| 922 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished, |
| 923 | ssl->s3->previous_server_finished_len) || |
| 924 | !CBB_flush(out)) { |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | return 1; |
| 929 | } |
| 930 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 931 | |
| 932 | /* Extended Master Secret. |
| 933 | * |
David Benjamin | 43946d4 | 2016-02-01 08:42:19 -0500 | [diff] [blame] | 934 | * https://tools.ietf.org/html/rfc7627 */ |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 935 | |
| 936 | static void ext_ems_init(SSL *ssl) { |
| 937 | ssl->s3->tmp.extended_master_secret = 0; |
| 938 | } |
| 939 | |
| 940 | static int ext_ems_add_clienthello(SSL *ssl, CBB *out) { |
| 941 | if (ssl->version == SSL3_VERSION) { |
| 942 | return 1; |
| 943 | } |
| 944 | |
| 945 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 946 | !CBB_add_u16(out, 0 /* length */)) { |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | return 1; |
| 951 | } |
| 952 | |
| 953 | static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 954 | CBS *contents) { |
| 955 | if (contents == NULL) { |
| 956 | return 1; |
| 957 | } |
| 958 | |
| 959 | if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) { |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | ssl->s3->tmp.extended_master_secret = 1; |
| 964 | return 1; |
| 965 | } |
| 966 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 967 | static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 968 | CBS *contents) { |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 969 | if (ssl->version == SSL3_VERSION || contents == NULL) { |
| 970 | return 1; |
| 971 | } |
| 972 | |
| 973 | if (CBS_len(contents) != 0) { |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | ssl->s3->tmp.extended_master_secret = 1; |
| 978 | return 1; |
| 979 | } |
| 980 | |
| 981 | static int ext_ems_add_serverhello(SSL *ssl, CBB *out) { |
| 982 | if (!ssl->s3->tmp.extended_master_secret) { |
| 983 | return 1; |
| 984 | } |
| 985 | |
| 986 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 987 | !CBB_add_u16(out, 0 /* length */)) { |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | return 1; |
| 992 | } |
| 993 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 994 | |
| 995 | /* Session tickets. |
| 996 | * |
| 997 | * https://tools.ietf.org/html/rfc5077 */ |
| 998 | |
| 999 | static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) { |
| 1000 | if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
| 1001 | return 1; |
| 1002 | } |
| 1003 | |
| 1004 | const uint8_t *ticket_data = NULL; |
| 1005 | int ticket_len = 0; |
| 1006 | |
| 1007 | /* Renegotiation does not participate in session resumption. However, still |
| 1008 | * advertise the extension to avoid potentially breaking servers which carry |
| 1009 | * over the state from the previous handshake, such as OpenSSL servers |
| 1010 | * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */ |
| 1011 | if (!ssl->s3->initial_handshake_complete && |
| 1012 | ssl->session != NULL && |
| 1013 | ssl->session->tlsext_tick != NULL) { |
| 1014 | ticket_data = ssl->session->tlsext_tick; |
| 1015 | ticket_len = ssl->session->tlsext_ticklen; |
| 1016 | } |
| 1017 | |
| 1018 | CBB ticket; |
| 1019 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1020 | !CBB_add_u16_length_prefixed(out, &ticket) || |
| 1021 | !CBB_add_bytes(&ticket, ticket_data, ticket_len) || |
| 1022 | !CBB_flush(out)) { |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | return 1; |
| 1027 | } |
| 1028 | |
| 1029 | static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1030 | CBS *contents) { |
| 1031 | ssl->tlsext_ticket_expected = 0; |
| 1032 | |
| 1033 | if (contents == NULL) { |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
| 1037 | /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 1038 | * this function should never be called, even if the server tries to send the |
| 1039 | * extension. */ |
| 1040 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1041 | |
| 1042 | if (CBS_len(contents) != 0) { |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | ssl->tlsext_ticket_expected = 1; |
| 1047 | return 1; |
| 1048 | } |
| 1049 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1050 | static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1051 | CBS *contents) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1052 | /* This function isn't used because the ticket extension from the client is |
David Benjamin | c7ce977 | 2015-10-09 19:32:41 -0400 | [diff] [blame] | 1053 | * handled in ssl_session.c. */ |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1054 | return 1; |
| 1055 | } |
| 1056 | |
| 1057 | static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) { |
| 1058 | if (!ssl->tlsext_ticket_expected) { |
| 1059 | return 1; |
| 1060 | } |
| 1061 | |
| 1062 | /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be |
| 1063 | * true. */ |
| 1064 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1065 | |
| 1066 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1067 | !CBB_add_u16(out, 0 /* length */)) { |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | return 1; |
| 1072 | } |
| 1073 | |
| 1074 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1075 | /* Signature Algorithms. |
| 1076 | * |
| 1077 | * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
| 1078 | |
| 1079 | static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) { |
| 1080 | if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) { |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |
| 1084 | const uint8_t *sigalgs_data; |
| 1085 | const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data); |
| 1086 | |
| 1087 | CBB contents, sigalgs; |
| 1088 | if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) || |
| 1089 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1090 | !CBB_add_u16_length_prefixed(&contents, &sigalgs) || |
| 1091 | !CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) || |
| 1092 | !CBB_flush(out)) { |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | return 1; |
| 1097 | } |
| 1098 | |
| 1099 | static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1100 | CBS *contents) { |
| 1101 | if (contents != NULL) { |
| 1102 | /* Servers MUST NOT send this extension. */ |
| 1103 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1104 | OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER); |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | return 1; |
| 1109 | } |
| 1110 | |
| 1111 | static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1112 | CBS *contents) { |
| 1113 | OPENSSL_free(ssl->cert->peer_sigalgs); |
| 1114 | ssl->cert->peer_sigalgs = NULL; |
| 1115 | ssl->cert->peer_sigalgslen = 0; |
| 1116 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1117 | if (contents == NULL) { |
| 1118 | return 1; |
| 1119 | } |
| 1120 | |
| 1121 | CBS supported_signature_algorithms; |
| 1122 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1123 | CBS_len(contents) != 0 || |
| 1124 | CBS_len(&supported_signature_algorithms) == 0 || |
| 1125 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | return 1; |
| 1130 | } |
| 1131 | |
| 1132 | static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) { |
| 1133 | /* Servers MUST NOT send this extension. */ |
| 1134 | return 1; |
| 1135 | } |
| 1136 | |
| 1137 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1138 | /* OCSP Stapling. |
| 1139 | * |
| 1140 | * https://tools.ietf.org/html/rfc6066#section-8 */ |
| 1141 | |
| 1142 | static void ext_ocsp_init(SSL *ssl) { |
| 1143 | ssl->s3->tmp.certificate_status_expected = 0; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1144 | ssl->tlsext_status_type = -1; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) { |
| 1148 | if (!ssl->ocsp_stapling_enabled) { |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
| 1152 | CBB contents; |
| 1153 | if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1154 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1155 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1156 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1157 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1158 | !CBB_flush(out)) { |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1162 | ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1163 | return 1; |
| 1164 | } |
| 1165 | |
| 1166 | static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1167 | CBS *contents) { |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1168 | if (contents == NULL) { |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
| 1172 | if (CBS_len(contents) != 0) { |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | ssl->s3->tmp.certificate_status_expected = 1; |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1181 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1182 | if (contents == NULL) { |
| 1183 | return 1; |
| 1184 | } |
| 1185 | |
| 1186 | uint8_t status_type; |
| 1187 | if (!CBS_get_u8(contents, &status_type)) { |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | /* We cannot decide whether OCSP stapling will occur yet because the correct |
| 1192 | * SSL_CTX might not have been selected. */ |
| 1193 | ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
| 1194 | |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1195 | return 1; |
| 1196 | } |
| 1197 | |
| 1198 | static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) { |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 1199 | /* The extension shouldn't be sent when resuming sessions. */ |
| 1200 | if (ssl->hit || |
| 1201 | !ssl->s3->tmp.ocsp_stapling_requested || |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1202 | ssl->ctx->ocsp_response_length == 0) { |
| 1203 | return 1; |
| 1204 | } |
| 1205 | |
| 1206 | ssl->s3->tmp.certificate_status_expected = 1; |
| 1207 | |
| 1208 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
| 1209 | CBB_add_u16(out, 0 /* length */); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1213 | /* Next protocol negotiation. |
| 1214 | * |
| 1215 | * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */ |
| 1216 | |
| 1217 | static void ext_npn_init(SSL *ssl) { |
| 1218 | ssl->s3->next_proto_neg_seen = 0; |
| 1219 | } |
| 1220 | |
| 1221 | static int ext_npn_add_clienthello(SSL *ssl, CBB *out) { |
| 1222 | if (ssl->s3->initial_handshake_complete || |
| 1223 | ssl->ctx->next_proto_select_cb == NULL || |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 1224 | (ssl->options & SSL_OP_DISABLE_NPN) || |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1225 | SSL_IS_DTLS(ssl)) { |
| 1226 | return 1; |
| 1227 | } |
| 1228 | |
| 1229 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1230 | !CBB_add_u16(out, 0 /* length */)) { |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | return 1; |
| 1235 | } |
| 1236 | |
| 1237 | static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1238 | CBS *contents) { |
| 1239 | if (contents == NULL) { |
| 1240 | return 1; |
| 1241 | } |
| 1242 | |
| 1243 | /* If any of these are false then we should never have sent the NPN |
| 1244 | * extension in the ClientHello and thus this function should never have been |
| 1245 | * called. */ |
| 1246 | assert(!ssl->s3->initial_handshake_complete); |
| 1247 | assert(!SSL_IS_DTLS(ssl)); |
| 1248 | assert(ssl->ctx->next_proto_select_cb != NULL); |
David Benjamin | 091c4b9 | 2015-10-26 13:33:21 -0400 | [diff] [blame] | 1249 | assert(!(ssl->options & SSL_OP_DISABLE_NPN)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1250 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1251 | if (ssl->s3->alpn_selected != NULL) { |
| 1252 | /* NPN and ALPN may not be negotiated in the same connection. */ |
| 1253 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1254 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1258 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1259 | const size_t orig_len = CBS_len(contents); |
| 1260 | |
| 1261 | while (CBS_len(contents) != 0) { |
| 1262 | CBS proto; |
| 1263 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1264 | CBS_len(&proto) == 0) { |
| 1265 | return 0; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | uint8_t *selected; |
| 1270 | uint8_t selected_len; |
| 1271 | if (ssl->ctx->next_proto_select_cb( |
| 1272 | ssl, &selected, &selected_len, orig_contents, orig_len, |
| 1273 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1274 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1278 | OPENSSL_free(ssl->s3->next_proto_negotiated); |
| 1279 | ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 1280 | if (ssl->s3->next_proto_negotiated == NULL) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1281 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1285 | ssl->s3->next_proto_negotiated_len = selected_len; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1286 | ssl->s3->next_proto_neg_seen = 1; |
| 1287 | |
| 1288 | return 1; |
| 1289 | } |
| 1290 | |
| 1291 | static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1292 | CBS *contents) { |
| 1293 | if (contents != NULL && CBS_len(contents) != 0) { |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | if (contents == NULL || |
| 1298 | ssl->s3->initial_handshake_complete || |
| 1299 | /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen |
| 1300 | * afterwards, parsing the ALPN extension will clear |
| 1301 | * |next_proto_neg_seen|. */ |
| 1302 | ssl->s3->alpn_selected != NULL || |
| 1303 | ssl->ctx->next_protos_advertised_cb == NULL || |
| 1304 | SSL_IS_DTLS(ssl)) { |
| 1305 | return 1; |
| 1306 | } |
| 1307 | |
| 1308 | ssl->s3->next_proto_neg_seen = 1; |
| 1309 | return 1; |
| 1310 | } |
| 1311 | |
| 1312 | static int ext_npn_add_serverhello(SSL *ssl, CBB *out) { |
| 1313 | /* |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1314 | * parsed. */ |
| 1315 | if (!ssl->s3->next_proto_neg_seen) { |
| 1316 | return 1; |
| 1317 | } |
| 1318 | |
| 1319 | const uint8_t *npa; |
| 1320 | unsigned npa_len; |
| 1321 | |
| 1322 | if (ssl->ctx->next_protos_advertised_cb( |
| 1323 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1324 | SSL_TLSEXT_ERR_OK) { |
| 1325 | ssl->s3->next_proto_neg_seen = 0; |
| 1326 | return 1; |
| 1327 | } |
| 1328 | |
| 1329 | CBB contents; |
| 1330 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1331 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1332 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1333 | !CBB_flush(out)) { |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
| 1340 | |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1341 | /* Signed certificate timestamps. |
| 1342 | * |
| 1343 | * https://tools.ietf.org/html/rfc6962#section-3.3.1 */ |
| 1344 | |
| 1345 | static int ext_sct_add_clienthello(SSL *ssl, CBB *out) { |
| 1346 | if (!ssl->signed_cert_timestamps_enabled) { |
| 1347 | return 1; |
| 1348 | } |
| 1349 | |
| 1350 | if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1351 | !CBB_add_u16(out, 0 /* length */)) { |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | return 1; |
| 1356 | } |
| 1357 | |
| 1358 | static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1359 | CBS *contents) { |
| 1360 | if (contents == NULL) { |
| 1361 | return 1; |
| 1362 | } |
| 1363 | |
| 1364 | /* If this is false then we should never have sent the SCT extension in the |
| 1365 | * ClientHello and thus this function should never have been called. */ |
| 1366 | assert(ssl->signed_cert_timestamps_enabled); |
| 1367 | |
| 1368 | if (CBS_len(contents) == 0) { |
| 1369 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | /* Session resumption uses the original session information. */ |
| 1374 | if (!ssl->hit && |
| 1375 | !CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list, |
| 1376 | &ssl->session->tlsext_signed_cert_timestamp_list_length)) { |
| 1377 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | return 1; |
| 1382 | } |
| 1383 | |
| 1384 | static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1385 | CBS *contents) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1386 | return contents == NULL || CBS_len(contents) == 0; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | static int ext_sct_add_serverhello(SSL *ssl, CBB *out) { |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 1390 | /* The extension shouldn't be sent when resuming sessions. */ |
| 1391 | if (ssl->hit || |
| 1392 | ssl->ctx->signed_cert_timestamp_list_length == 0) { |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1393 | return 1; |
| 1394 | } |
| 1395 | |
| 1396 | CBB contents; |
| 1397 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1398 | CBB_add_u16_length_prefixed(out, &contents) && |
| 1399 | CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list, |
| 1400 | ssl->ctx->signed_cert_timestamp_list_length) && |
| 1401 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1405 | /* Application-level Protocol Negotiation. |
| 1406 | * |
| 1407 | * https://tools.ietf.org/html/rfc7301 */ |
| 1408 | |
| 1409 | static void ext_alpn_init(SSL *ssl) { |
| 1410 | OPENSSL_free(ssl->s3->alpn_selected); |
| 1411 | ssl->s3->alpn_selected = NULL; |
| 1412 | } |
| 1413 | |
| 1414 | static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) { |
| 1415 | if (ssl->alpn_client_proto_list == NULL || |
| 1416 | ssl->s3->initial_handshake_complete) { |
| 1417 | return 1; |
| 1418 | } |
| 1419 | |
| 1420 | CBB contents, proto_list; |
| 1421 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1422 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1423 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1424 | !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1425 | ssl->alpn_client_proto_list_len) || |
| 1426 | !CBB_flush(out)) { |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1434 | CBS *contents) { |
| 1435 | if (contents == NULL) { |
| 1436 | return 1; |
| 1437 | } |
| 1438 | |
| 1439 | assert(!ssl->s3->initial_handshake_complete); |
| 1440 | assert(ssl->alpn_client_proto_list != NULL); |
| 1441 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1442 | if (ssl->s3->next_proto_neg_seen) { |
| 1443 | /* NPN and ALPN may not be negotiated in the same connection. */ |
| 1444 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1445 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1449 | /* The extension data consists of a ProtocolNameList which must have |
| 1450 | * exactly one ProtocolName. Each of these is length-prefixed. */ |
| 1451 | CBS protocol_name_list, protocol_name; |
| 1452 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1453 | CBS_len(contents) != 0 || |
| 1454 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1455 | /* Empty protocol names are forbidden. */ |
| 1456 | CBS_len(&protocol_name) == 0 || |
| 1457 | CBS_len(&protocol_name_list) != 0) { |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected, |
| 1462 | &ssl->s3->alpn_selected_len)) { |
| 1463 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | return 1; |
| 1468 | } |
| 1469 | |
| 1470 | static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1471 | CBS *contents) { |
| 1472 | if (contents == NULL) { |
| 1473 | return 1; |
| 1474 | } |
| 1475 | |
| 1476 | if (ssl->ctx->alpn_select_cb == NULL || |
| 1477 | ssl->s3->initial_handshake_complete) { |
| 1478 | return 1; |
| 1479 | } |
| 1480 | |
| 1481 | /* ALPN takes precedence over NPN. */ |
| 1482 | ssl->s3->next_proto_neg_seen = 0; |
| 1483 | |
| 1484 | CBS protocol_name_list; |
| 1485 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1486 | CBS_len(contents) != 0 || |
| 1487 | CBS_len(&protocol_name_list) < 2) { |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | /* Validate the protocol list. */ |
| 1492 | CBS protocol_name_list_copy = protocol_name_list; |
| 1493 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1494 | CBS protocol_name; |
| 1495 | |
| 1496 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
| 1497 | /* Empty protocol names are forbidden. */ |
| 1498 | CBS_len(&protocol_name) == 0) { |
| 1499 | return 0; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | const uint8_t *selected; |
| 1504 | uint8_t selected_len; |
| 1505 | if (ssl->ctx->alpn_select_cb( |
| 1506 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1507 | CBS_len(&protocol_name_list), |
| 1508 | ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
| 1509 | OPENSSL_free(ssl->s3->alpn_selected); |
| 1510 | ssl->s3->alpn_selected = BUF_memdup(selected, selected_len); |
| 1511 | if (ssl->s3->alpn_selected == NULL) { |
| 1512 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1513 | return 0; |
| 1514 | } |
| 1515 | ssl->s3->alpn_selected_len = selected_len; |
| 1516 | } |
| 1517 | |
| 1518 | return 1; |
| 1519 | } |
| 1520 | |
| 1521 | static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) { |
| 1522 | if (ssl->s3->alpn_selected == NULL) { |
| 1523 | return 1; |
| 1524 | } |
| 1525 | |
| 1526 | CBB contents, proto_list, proto; |
| 1527 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1528 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1529 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1530 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1531 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected, |
| 1532 | ssl->s3->alpn_selected_len) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1533 | !CBB_flush(out)) { |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
| 1537 | return 1; |
| 1538 | } |
| 1539 | |
| 1540 | |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1541 | /* Channel ID. |
| 1542 | * |
| 1543 | * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */ |
| 1544 | |
| 1545 | static void ext_channel_id_init(SSL *ssl) { |
| 1546 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1547 | } |
| 1548 | |
| 1549 | static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) { |
| 1550 | if (!ssl->tlsext_channel_id_enabled || |
| 1551 | SSL_IS_DTLS(ssl)) { |
| 1552 | return 1; |
| 1553 | } |
| 1554 | |
| 1555 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1556 | !CBB_add_u16(out, 0 /* length */)) { |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | return 1; |
| 1561 | } |
| 1562 | |
| 1563 | static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1564 | CBS *contents) { |
| 1565 | if (contents == NULL) { |
| 1566 | return 1; |
| 1567 | } |
| 1568 | |
| 1569 | assert(!SSL_IS_DTLS(ssl)); |
| 1570 | assert(ssl->tlsext_channel_id_enabled); |
| 1571 | |
| 1572 | if (CBS_len(contents) != 0) { |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | ssl->s3->tlsext_channel_id_valid = 1; |
| 1577 | return 1; |
| 1578 | } |
| 1579 | |
| 1580 | static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1581 | CBS *contents) { |
| 1582 | if (contents == NULL || |
| 1583 | !ssl->tlsext_channel_id_enabled || |
| 1584 | SSL_IS_DTLS(ssl)) { |
| 1585 | return 1; |
| 1586 | } |
| 1587 | |
| 1588 | if (CBS_len(contents) != 0) { |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | ssl->s3->tlsext_channel_id_valid = 1; |
| 1593 | return 1; |
| 1594 | } |
| 1595 | |
| 1596 | static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) { |
| 1597 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 1598 | return 1; |
| 1599 | } |
| 1600 | |
| 1601 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1602 | !CBB_add_u16(out, 0 /* length */)) { |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | return 1; |
| 1607 | } |
| 1608 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1609 | |
| 1610 | /* Secure Real-time Transport Protocol (SRTP) extension. |
| 1611 | * |
| 1612 | * https://tools.ietf.org/html/rfc5764 */ |
| 1613 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1614 | |
| 1615 | static void ext_srtp_init(SSL *ssl) { |
| 1616 | ssl->srtp_profile = NULL; |
| 1617 | } |
| 1618 | |
| 1619 | static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) { |
| 1620 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1621 | if (profiles == NULL) { |
| 1622 | return 1; |
| 1623 | } |
| 1624 | const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles); |
| 1625 | if (num_profiles == 0) { |
| 1626 | return 1; |
| 1627 | } |
| 1628 | |
| 1629 | CBB contents, profile_ids; |
| 1630 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1631 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1632 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | size_t i; |
| 1637 | for (i = 0; i < num_profiles; i++) { |
| 1638 | if (!CBB_add_u16(&profile_ids, |
| 1639 | sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) { |
| 1640 | return 0; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1645 | !CBB_flush(out)) { |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | return 1; |
| 1650 | } |
| 1651 | |
| 1652 | static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1653 | CBS *contents) { |
| 1654 | if (contents == NULL) { |
| 1655 | return 1; |
| 1656 | } |
| 1657 | |
| 1658 | /* The extension consists of a u16-prefixed profile ID list containing a |
| 1659 | * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1660 | * |
| 1661 | * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */ |
| 1662 | CBS profile_ids, srtp_mki; |
| 1663 | uint16_t profile_id; |
| 1664 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1665 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1666 | CBS_len(&profile_ids) != 0 || |
| 1667 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1668 | CBS_len(contents) != 0) { |
| 1669 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | if (CBS_len(&srtp_mki) != 0) { |
| 1674 | /* Must be no MKI, since we never offer one. */ |
| 1675 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1676 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1681 | |
| 1682 | /* Check to see if the server gave us something we support (and presumably |
| 1683 | * offered). */ |
| 1684 | size_t i; |
| 1685 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) { |
| 1686 | const SRTP_PROTECTION_PROFILE *profile = |
| 1687 | sk_SRTP_PROTECTION_PROFILE_value(profiles, i); |
| 1688 | |
| 1689 | if (profile->id == profile_id) { |
| 1690 | ssl->srtp_profile = profile; |
| 1691 | return 1; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1696 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1697 | return 0; |
| 1698 | } |
| 1699 | |
| 1700 | static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1701 | CBS *contents) { |
| 1702 | if (contents == NULL) { |
| 1703 | return 1; |
| 1704 | } |
| 1705 | |
| 1706 | CBS profile_ids, srtp_mki; |
| 1707 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1708 | CBS_len(&profile_ids) < 2 || |
| 1709 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1710 | CBS_len(contents) != 0) { |
| 1711 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1712 | return 0; |
| 1713 | } |
| 1714 | /* Discard the MKI value for now. */ |
| 1715 | |
| 1716 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1717 | SSL_get_srtp_profiles(ssl); |
| 1718 | |
| 1719 | /* Pick the server's most preferred profile. */ |
| 1720 | size_t i; |
| 1721 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) { |
| 1722 | const SRTP_PROTECTION_PROFILE *server_profile = |
| 1723 | sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i); |
| 1724 | |
| 1725 | CBS profile_ids_tmp; |
| 1726 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1727 | |
| 1728 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1729 | uint16_t profile_id; |
| 1730 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | if (server_profile->id == profile_id) { |
| 1735 | ssl->srtp_profile = server_profile; |
| 1736 | return 1; |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | return 1; |
| 1742 | } |
| 1743 | |
| 1744 | static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) { |
| 1745 | if (ssl->srtp_profile == NULL) { |
| 1746 | return 1; |
| 1747 | } |
| 1748 | |
| 1749 | CBB contents, profile_ids; |
| 1750 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1751 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1752 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1753 | !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1754 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1755 | !CBB_flush(out)) { |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | return 1; |
| 1760 | } |
| 1761 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1762 | |
| 1763 | /* EC point formats. |
| 1764 | * |
| 1765 | * https://tools.ietf.org/html/rfc4492#section-5.1.2 */ |
| 1766 | |
| 1767 | static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) { |
| 1768 | if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) { |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl); |
| 1773 | |
| 1774 | size_t i; |
| 1775 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { |
| 1776 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i); |
| 1777 | |
| 1778 | const uint32_t alg_k = cipher->algorithm_mkey; |
| 1779 | const uint32_t alg_a = cipher->algorithm_auth; |
| 1780 | if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) { |
| 1781 | return 1; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1788 | static int ext_ec_point_add_extension(SSL *ssl, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1789 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1790 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1791 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1792 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1793 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1794 | !CBB_flush(out)) { |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | return 1; |
| 1799 | } |
| 1800 | |
| 1801 | static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) { |
| 1802 | if (!ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 1803 | return 1; |
| 1804 | } |
| 1805 | |
| 1806 | return ext_ec_point_add_extension(ssl, out); |
| 1807 | } |
| 1808 | |
| 1809 | static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1810 | CBS *contents) { |
| 1811 | if (contents == NULL) { |
| 1812 | return 1; |
| 1813 | } |
| 1814 | |
| 1815 | CBS ec_point_format_list; |
| 1816 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1817 | CBS_len(contents) != 0) { |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1821 | /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1822 | * point format. */ |
| 1823 | if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1824 | CBS_len(&ec_point_format_list)) == NULL) { |
| 1825 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1826 | return 0; |
| 1827 | } |
| 1828 | |
| 1829 | return 1; |
| 1830 | } |
| 1831 | |
| 1832 | static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1833 | CBS *contents) { |
| 1834 | return ext_ec_point_parse_serverhello(ssl, out_alert, contents); |
| 1835 | } |
| 1836 | |
| 1837 | static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) { |
| 1838 | const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1839 | const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1840 | const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1841 | |
| 1842 | if (!using_ecc) { |
| 1843 | return 1; |
| 1844 | } |
| 1845 | |
| 1846 | return ext_ec_point_add_extension(ssl, out); |
| 1847 | } |
| 1848 | |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 1849 | |
| 1850 | /* EC supported curves. |
| 1851 | * |
| 1852 | * https://tools.ietf.org/html/rfc4492#section-5.1.2 */ |
| 1853 | |
| 1854 | static void ext_ec_curves_init(SSL *ssl) { |
| 1855 | OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist); |
| 1856 | ssl->s3->tmp.peer_ellipticcurvelist = NULL; |
| 1857 | ssl->s3->tmp.peer_ellipticcurvelist_length = 0; |
| 1858 | } |
| 1859 | |
| 1860 | static int ext_ec_curves_add_clienthello(SSL *ssl, CBB *out) { |
| 1861 | if (!ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 1862 | return 1; |
| 1863 | } |
| 1864 | |
| 1865 | CBB contents, curves_bytes; |
| 1866 | if (!CBB_add_u16(out, TLSEXT_TYPE_elliptic_curves) || |
| 1867 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1868 | !CBB_add_u16_length_prefixed(&contents, &curves_bytes)) { |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | const uint16_t *curves; |
| 1873 | size_t curves_len; |
| 1874 | tls1_get_curvelist(ssl, 0, &curves, &curves_len); |
| 1875 | |
| 1876 | size_t i; |
| 1877 | for (i = 0; i < curves_len; i++) { |
| 1878 | if (!CBB_add_u16(&curves_bytes, curves[i])) { |
| 1879 | return 0; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | return CBB_flush(out); |
| 1884 | } |
| 1885 | |
| 1886 | static int ext_ec_curves_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1887 | CBS *contents) { |
| 1888 | /* This extension is not expected to be echoed by servers and is ignored. */ |
| 1889 | return 1; |
| 1890 | } |
| 1891 | |
| 1892 | static int ext_ec_curves_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1893 | CBS *contents) { |
| 1894 | if (contents == NULL) { |
| 1895 | return 1; |
| 1896 | } |
| 1897 | |
| 1898 | CBS elliptic_curve_list; |
| 1899 | if (!CBS_get_u16_length_prefixed(contents, &elliptic_curve_list) || |
| 1900 | CBS_len(&elliptic_curve_list) == 0 || |
| 1901 | (CBS_len(&elliptic_curve_list) & 1) != 0 || |
| 1902 | CBS_len(contents) != 0) { |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 1906 | ssl->s3->tmp.peer_ellipticcurvelist = OPENSSL_malloc(CBS_len(&elliptic_curve_list)); |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 1907 | if (ssl->s3->tmp.peer_ellipticcurvelist == NULL) { |
| 1908 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
| 1912 | const size_t num_curves = CBS_len(&elliptic_curve_list) / 2; |
| 1913 | size_t i; |
| 1914 | for (i = 0; i < num_curves; i++) { |
| 1915 | if (!CBS_get_u16(&elliptic_curve_list, |
| 1916 | &ssl->s3->tmp.peer_ellipticcurvelist[i])) { |
| 1917 | goto err; |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | assert(CBS_len(&elliptic_curve_list) == 0); |
| 1922 | ssl->s3->tmp.peer_ellipticcurvelist_length = num_curves; |
| 1923 | |
| 1924 | return 1; |
| 1925 | |
| 1926 | err: |
| 1927 | OPENSSL_free(ssl->s3->tmp.peer_ellipticcurvelist); |
| 1928 | ssl->s3->tmp.peer_ellipticcurvelist = NULL; |
| 1929 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | static int ext_ec_curves_add_serverhello(SSL *ssl, CBB *out) { |
| 1934 | /* Servers don't echo this extension. */ |
| 1935 | return 1; |
| 1936 | } |
| 1937 | |
| 1938 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1939 | /* kExtensions contains all the supported extensions. */ |
| 1940 | static const struct tls_extension kExtensions[] = { |
| 1941 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 1942 | /* The renegotiation extension must always be at index zero because the |
| 1943 | * |received| and |sent| bitsets need to be tweaked when the "extension" is |
| 1944 | * sent as an SCSV. */ |
| 1945 | TLSEXT_TYPE_renegotiate, |
| 1946 | NULL, |
| 1947 | ext_ri_add_clienthello, |
| 1948 | ext_ri_parse_serverhello, |
| 1949 | ext_ri_parse_clienthello, |
| 1950 | ext_ri_add_serverhello, |
| 1951 | }, |
| 1952 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1953 | TLSEXT_TYPE_server_name, |
| 1954 | ext_sni_init, |
| 1955 | ext_sni_add_clienthello, |
| 1956 | ext_sni_parse_serverhello, |
| 1957 | ext_sni_parse_clienthello, |
| 1958 | ext_sni_add_serverhello, |
| 1959 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 1960 | { |
| 1961 | TLSEXT_TYPE_extended_master_secret, |
| 1962 | ext_ems_init, |
| 1963 | ext_ems_add_clienthello, |
| 1964 | ext_ems_parse_serverhello, |
| 1965 | ext_ems_parse_clienthello, |
| 1966 | ext_ems_add_serverhello, |
| 1967 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 1968 | { |
| 1969 | TLSEXT_TYPE_session_ticket, |
| 1970 | NULL, |
| 1971 | ext_ticket_add_clienthello, |
| 1972 | ext_ticket_parse_serverhello, |
| 1973 | ext_ticket_parse_clienthello, |
| 1974 | ext_ticket_add_serverhello, |
| 1975 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1976 | { |
| 1977 | TLSEXT_TYPE_signature_algorithms, |
| 1978 | NULL, |
| 1979 | ext_sigalgs_add_clienthello, |
| 1980 | ext_sigalgs_parse_serverhello, |
| 1981 | ext_sigalgs_parse_clienthello, |
| 1982 | ext_sigalgs_add_serverhello, |
| 1983 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1984 | { |
| 1985 | TLSEXT_TYPE_status_request, |
| 1986 | ext_ocsp_init, |
| 1987 | ext_ocsp_add_clienthello, |
| 1988 | ext_ocsp_parse_serverhello, |
| 1989 | ext_ocsp_parse_clienthello, |
| 1990 | ext_ocsp_add_serverhello, |
| 1991 | }, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1992 | { |
| 1993 | TLSEXT_TYPE_next_proto_neg, |
| 1994 | ext_npn_init, |
| 1995 | ext_npn_add_clienthello, |
| 1996 | ext_npn_parse_serverhello, |
| 1997 | ext_npn_parse_clienthello, |
| 1998 | ext_npn_add_serverhello, |
| 1999 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 2000 | { |
| 2001 | TLSEXT_TYPE_certificate_timestamp, |
| 2002 | NULL, |
| 2003 | ext_sct_add_clienthello, |
| 2004 | ext_sct_parse_serverhello, |
| 2005 | ext_sct_parse_clienthello, |
| 2006 | ext_sct_add_serverhello, |
| 2007 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2008 | { |
| 2009 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
| 2010 | ext_alpn_init, |
| 2011 | ext_alpn_add_clienthello, |
| 2012 | ext_alpn_parse_serverhello, |
| 2013 | ext_alpn_parse_clienthello, |
| 2014 | ext_alpn_add_serverhello, |
| 2015 | }, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2016 | { |
| 2017 | TLSEXT_TYPE_channel_id, |
| 2018 | ext_channel_id_init, |
| 2019 | ext_channel_id_add_clienthello, |
| 2020 | ext_channel_id_parse_serverhello, |
| 2021 | ext_channel_id_parse_clienthello, |
| 2022 | ext_channel_id_add_serverhello, |
| 2023 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 2024 | { |
| 2025 | TLSEXT_TYPE_srtp, |
| 2026 | ext_srtp_init, |
| 2027 | ext_srtp_add_clienthello, |
| 2028 | ext_srtp_parse_serverhello, |
| 2029 | ext_srtp_parse_clienthello, |
| 2030 | ext_srtp_add_serverhello, |
| 2031 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2032 | { |
| 2033 | TLSEXT_TYPE_ec_point_formats, |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2034 | NULL, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2035 | ext_ec_point_add_clienthello, |
| 2036 | ext_ec_point_parse_serverhello, |
| 2037 | ext_ec_point_parse_clienthello, |
| 2038 | ext_ec_point_add_serverhello, |
| 2039 | }, |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2040 | /* The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2041 | * intolerant to the last extension being zero-length. See |
| 2042 | * https://crbug.com/363583. */ |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2043 | { |
| 2044 | TLSEXT_TYPE_elliptic_curves, |
| 2045 | ext_ec_curves_init, |
| 2046 | ext_ec_curves_add_clienthello, |
| 2047 | ext_ec_curves_parse_serverhello, |
| 2048 | ext_ec_curves_parse_clienthello, |
| 2049 | ext_ec_curves_add_serverhello, |
| 2050 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2051 | }; |
| 2052 | |
| 2053 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2054 | |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2055 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 2056 | sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8, |
David Benjamin | 7ca4b42 | 2015-07-13 16:43:47 -0400 | [diff] [blame] | 2057 | too_many_extensions_for_sent_bitset); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2058 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 2059 | sizeof(((SSL *)NULL)->s3->tmp.extensions.received) * |
| 2060 | 8, |
David Benjamin | 7ca4b42 | 2015-07-13 16:43:47 -0400 | [diff] [blame] | 2061 | too_many_extensions_for_received_bitset); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2062 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2063 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2064 | uint16_t value) { |
| 2065 | unsigned i; |
| 2066 | for (i = 0; i < kNumExtensions; i++) { |
| 2067 | if (kExtensions[i].value == value) { |
| 2068 | *out_index = i; |
| 2069 | return &kExtensions[i]; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | return NULL; |
| 2074 | } |
| 2075 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2076 | int SSL_extension_supported(unsigned extension_value) { |
| 2077 | uint32_t index; |
| 2078 | return extension_value == TLSEXT_TYPE_padding || |
| 2079 | tls_extension_find(&index, extension_value) != NULL; |
| 2080 | } |
| 2081 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2082 | 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] | 2083 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2084 | if (ssl->client_version == SSL3_VERSION && |
| 2085 | !ssl->s3->send_connection_binding) { |
| 2086 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2087 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2088 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2089 | CBB extensions; |
| 2090 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2091 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2092 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2093 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2094 | ssl->s3->tmp.extensions.sent = 0; |
| 2095 | ssl->s3->tmp.custom_extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2096 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2097 | size_t i; |
| 2098 | for (i = 0; i < kNumExtensions; i++) { |
| 2099 | if (kExtensions[i].init != NULL) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2100 | kExtensions[i].init(ssl); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2101 | } |
| 2102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2103 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2104 | for (i = 0; i < kNumExtensions; i++) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2105 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2106 | if (!kExtensions[i].add_clienthello(ssl, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2107 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 2108 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2109 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2110 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2111 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2112 | if (CBB_len(&extensions) != len_before) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2113 | ssl->s3->tmp.extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2114 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2115 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2116 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2117 | if (!custom_ext_add_clienthello(ssl, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2118 | goto err; |
| 2119 | } |
| 2120 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2121 | if (!SSL_IS_DTLS(ssl)) { |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2122 | header_len += 2 + CBB_len(&extensions); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2123 | if (header_len > 0xff && header_len < 0x200) { |
Adam Langley | 10a1a9d | 2015-10-21 14:49:23 -0700 | [diff] [blame] | 2124 | /* Add padding to workaround bugs in F5 terminators. See RFC 7685. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2125 | * |
| 2126 | * NB: because this code works out the length of all existing extensions |
| 2127 | * it MUST always appear last. */ |
David Benjamin | 0a96859 | 2015-07-21 22:06:19 -0400 | [diff] [blame] | 2128 | size_t padding_len = 0x200 - header_len; |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2129 | /* Extensions take at least four bytes to encode. Always include at least |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2130 | * one byte of data if including the extension. WebSphere Application |
David Benjamin | 1e4ae00 | 2016-03-25 18:56:10 -0400 | [diff] [blame] | 2131 | * Server 7.0 is intolerant to the last extension being zero-length. See |
| 2132 | * https://crbug.com/363583. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2133 | if (padding_len >= 4 + 1) { |
| 2134 | padding_len -= 4; |
| 2135 | } else { |
| 2136 | padding_len = 1; |
| 2137 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2138 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2139 | uint8_t *padding_bytes; |
| 2140 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2141 | !CBB_add_u16(&extensions, padding_len) || |
| 2142 | !CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
| 2143 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2144 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2145 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2146 | memset(padding_bytes, 0, padding_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2147 | } |
| 2148 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2149 | |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2150 | /* Discard empty extensions blocks. */ |
| 2151 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2152 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2155 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2156 | |
| 2157 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2158 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2159 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2160 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2161 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2162 | int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2163 | CBB extensions; |
| 2164 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2165 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | unsigned i; |
| 2169 | for (i = 0; i < kNumExtensions; i++) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2170 | if (!(ssl->s3->tmp.extensions.received & (1u << i))) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2171 | /* Don't send extensions that were not received. */ |
| 2172 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2174 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2175 | if (!kExtensions[i].add_serverhello(ssl, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2176 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 2177 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2178 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2179 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2181 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2182 | if (!custom_ext_add_serverhello(ssl, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2183 | goto err; |
| 2184 | } |
| 2185 | |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2186 | /* Discard empty extensions blocks. */ |
| 2187 | if (CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2188 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2191 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2192 | |
| 2193 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2194 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2195 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2197 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2198 | static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2199 | size_t i; |
| 2200 | for (i = 0; i < kNumExtensions; i++) { |
| 2201 | if (kExtensions[i].init != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2202 | kExtensions[i].init(ssl); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2206 | ssl->s3->tmp.extensions.received = 0; |
| 2207 | ssl->s3->tmp.custom_extensions.received = 0; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 2208 | /* The renegotiation extension must always be at index zero because the |
| 2209 | * |received| and |sent| bitsets need to be tweaked when the "extension" is |
| 2210 | * sent as an SCSV. */ |
| 2211 | assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2212 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2213 | /* There may be no extensions. */ |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2214 | if (CBS_len(cbs) != 0) { |
| 2215 | /* Decode the extensions block and check it is valid. */ |
| 2216 | CBS extensions; |
| 2217 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2218 | !tls1_check_duplicate_extensions(&extensions)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2219 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2220 | return 0; |
| 2221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2222 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2223 | while (CBS_len(&extensions) != 0) { |
| 2224 | uint16_t type; |
| 2225 | CBS extension; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2226 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2227 | /* Decode the next extension. */ |
| 2228 | if (!CBS_get_u16(&extensions, &type) || |
| 2229 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 2230 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
David Benjamin | c7ce977 | 2015-10-09 19:32:41 -0400 | [diff] [blame] | 2234 | /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 2235 | * ambiguous. Ignore all but the renegotiation_info extension. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2236 | if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
David Benjamin | c7ce977 | 2015-10-09 19:32:41 -0400 | [diff] [blame] | 2237 | continue; |
| 2238 | } |
| 2239 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2240 | unsigned ext_index; |
| 2241 | const struct tls_extension *const ext = |
| 2242 | tls_extension_find(&ext_index, type); |
| 2243 | |
| 2244 | if (ext == NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2245 | if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2246 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2247 | return 0; |
| 2248 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2249 | continue; |
| 2250 | } |
| 2251 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2252 | ssl->s3->tmp.extensions.received |= (1u << ext_index); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2253 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2254 | if (!ext->parse_clienthello(ssl, &alert, &extension)) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2255 | *out_alert = alert; |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2256 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2257 | ERR_add_error_dataf("extension: %u", (unsigned)type); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2258 | return 0; |
| 2259 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2260 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2261 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2262 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2263 | for (i = 0; i < kNumExtensions; i++) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2264 | if (!(ssl->s3->tmp.extensions.received & (1u << i))) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2265 | /* Extension wasn't observed so call the callback with a NULL |
| 2266 | * parameter. */ |
| 2267 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2268 | if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2269 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 2270 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2271 | *out_alert = alert; |
| 2272 | return 0; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
| 2276 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2277 | return 1; |
| 2278 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2279 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2280 | int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2281 | int alert = -1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2282 | if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) { |
| 2283 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2284 | return 0; |
| 2285 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2286 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2287 | if (ssl_check_clienthello_tlsext(ssl) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2288 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2289 | return 0; |
| 2290 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2291 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2292 | return 1; |
| 2293 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2294 | |
Matt Braithwaite | e564a5b | 2015-09-30 15:24:05 -0700 | [diff] [blame] | 2295 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits); |
| 2296 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2297 | static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2298 | uint32_t received = 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2299 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2300 | if (CBS_len(cbs) != 0) { |
| 2301 | /* Decode the extensions block and check it is valid. */ |
| 2302 | CBS extensions; |
| 2303 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2304 | !tls1_check_duplicate_extensions(&extensions)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2305 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2306 | return 0; |
| 2307 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2308 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2309 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2310 | while (CBS_len(&extensions) != 0) { |
| 2311 | uint16_t type; |
| 2312 | CBS extension; |
| 2313 | |
| 2314 | /* Decode the next extension. */ |
| 2315 | if (!CBS_get_u16(&extensions, &type) || |
| 2316 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 2317 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2318 | return 0; |
| 2319 | } |
| 2320 | |
| 2321 | unsigned ext_index; |
| 2322 | const struct tls_extension *const ext = |
| 2323 | tls_extension_find(&ext_index, type); |
| 2324 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2325 | if (ext == NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2326 | if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2327 | return 0; |
| 2328 | } |
| 2329 | continue; |
| 2330 | } |
| 2331 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2332 | if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2333 | /* If the extension was never sent then it is illegal. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2334 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2335 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2336 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2337 | return 0; |
| 2338 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2339 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2340 | received |= (1u << ext_index); |
| 2341 | |
| 2342 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2343 | if (!ext->parse_serverhello(ssl, &alert, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2344 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2345 | ERR_add_error_dataf("extension: %u", (unsigned)type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2346 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2349 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2350 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2351 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2352 | size_t i; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2353 | for (i = 0; i < kNumExtensions; i++) { |
| 2354 | if (!(received & (1u << i))) { |
| 2355 | /* Extension wasn't observed so call the callback with a NULL |
| 2356 | * parameter. */ |
| 2357 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2358 | if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2359 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 2360 | ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2361 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2362 | return 0; |
| 2363 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2364 | } |
| 2365 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2366 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2367 | return 1; |
| 2368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2369 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2370 | static int ssl_check_clienthello_tlsext(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2371 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2372 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2373 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2374 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2375 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2376 | ssl->ctx->tlsext_servername_arg); |
| 2377 | } else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2378 | ret = ssl->initial_ctx->tlsext_servername_callback( |
| 2379 | ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2380 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2381 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2382 | switch (ret) { |
| 2383 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2384 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2385 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2386 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2387 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2388 | ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2389 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2390 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2391 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2392 | ssl->s3->tmp.should_ack_sni = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2393 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2394 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2395 | default: |
| 2396 | return 1; |
| 2397 | } |
| 2398 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2399 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2400 | static int ssl_check_serverhello_tlsext(SSL *ssl) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2401 | int ret = SSL_TLSEXT_ERR_OK; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2402 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2403 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2404 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2405 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 2406 | ssl->ctx->tlsext_servername_arg); |
| 2407 | } else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2408 | ret = ssl->initial_ctx->tlsext_servername_callback( |
| 2409 | ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2410 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2411 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2412 | switch (ret) { |
| 2413 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2414 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2415 | return -1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2416 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2417 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2418 | ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2419 | return 1; |
| 2420 | |
| 2421 | default: |
| 2422 | return 1; |
| 2423 | } |
| 2424 | } |
| 2425 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2426 | int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2427 | int alert = -1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2428 | if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) { |
| 2429 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2430 | return 0; |
| 2431 | } |
| 2432 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2433 | if (ssl_check_serverhello_tlsext(ssl) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2434 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2435 | return 0; |
| 2436 | } |
| 2437 | |
| 2438 | return 1; |
| 2439 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2440 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2441 | int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session, |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 2442 | int *out_renew_ticket, const uint8_t *ticket, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2443 | size_t ticket_len, const uint8_t *session_id, |
| 2444 | size_t session_id_len) { |
| 2445 | int ret = 1; /* Most errors are non-fatal. */ |
| 2446 | SSL_CTX *ssl_ctx = ssl->initial_ctx; |
| 2447 | uint8_t *plaintext = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2448 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2449 | HMAC_CTX hmac_ctx; |
| 2450 | HMAC_CTX_init(&hmac_ctx); |
| 2451 | EVP_CIPHER_CTX cipher_ctx; |
| 2452 | EVP_CIPHER_CTX_init(&cipher_ctx); |
| 2453 | |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 2454 | *out_renew_ticket = 0; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2455 | *out_session = NULL; |
| 2456 | |
| 2457 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 2458 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2459 | } |
| 2460 | |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 2461 | /* Ensure there is room for the key name and the largest IV |
| 2462 | * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but |
| 2463 | * the maximum IV length should be well under the minimum size for the |
| 2464 | * session material and HMAC. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2465 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 2466 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2467 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2468 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2469 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2470 | if (ssl_ctx->tlsext_ticket_key_cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2471 | int cb_ret = ssl_ctx->tlsext_ticket_key_cb( |
| 2472 | ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx, |
| 2473 | &hmac_ctx, 0 /* decrypt */); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2474 | if (cb_ret < 0) { |
| 2475 | ret = 0; |
| 2476 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2477 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2478 | if (cb_ret == 0) { |
| 2479 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2480 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2481 | if (cb_ret == 2) { |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 2482 | *out_renew_ticket = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2483 | } |
| 2484 | } else { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2485 | /* Check the key name matches. */ |
| 2486 | if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name, |
| 2487 | SSL_TICKET_KEY_NAME_LEN) != 0) { |
| 2488 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2489 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2490 | if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key, |
| 2491 | sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2492 | NULL) || |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2493 | !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL, |
| 2494 | ssl_ctx->tlsext_tick_aes_key, iv)) { |
| 2495 | ret = 0; |
| 2496 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2497 | } |
| 2498 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2499 | size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2500 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2501 | /* Check the MAC at the end of the ticket. */ |
| 2502 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 2503 | size_t mac_len = HMAC_size(&hmac_ctx); |
| 2504 | 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] | 2505 | /* 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] | 2506 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2507 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2508 | HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len); |
| 2509 | HMAC_Final(&hmac_ctx, mac, NULL); |
| 2510 | if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) { |
| 2511 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2512 | } |
| 2513 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2514 | /* Decrypt the session data. */ |
| 2515 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 2516 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 2517 | mac_len; |
| 2518 | plaintext = OPENSSL_malloc(ciphertext_len); |
| 2519 | if (plaintext == NULL) { |
| 2520 | ret = 0; |
| 2521 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2522 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2523 | if (ciphertext_len >= INT_MAX) { |
| 2524 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2525 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2526 | int len1, len2; |
| 2527 | if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext, |
| 2528 | (int)ciphertext_len) || |
| 2529 | !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) { |
| 2530 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2531 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2532 | } |
| 2533 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 2534 | /* Decode the session. */ |
| 2535 | SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2); |
| 2536 | if (session == NULL) { |
| 2537 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2538 | goto done; |
| 2539 | } |
| 2540 | |
| 2541 | /* Copy the client's session ID into the new session, to denote the ticket has |
| 2542 | * been accepted. */ |
| 2543 | memcpy(session->session_id, session_id, session_id_len); |
| 2544 | session->session_id_length = session_id_len; |
| 2545 | |
| 2546 | *out_session = session; |
| 2547 | |
| 2548 | done: |
| 2549 | OPENSSL_free(plaintext); |
| 2550 | HMAC_CTX_cleanup(&hmac_ctx); |
| 2551 | EVP_CIPHER_CTX_cleanup(&cipher_ctx); |
| 2552 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2553 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2554 | |
| 2555 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2556 | typedef struct { |
| 2557 | int nid; |
| 2558 | int id; |
| 2559 | } tls12_lookup; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2560 | |
David Benjamin | 72f7e21 | 2016-01-29 15:28:58 -0500 | [diff] [blame] | 2561 | static const tls12_lookup tls12_md[] = { |
| 2562 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2563 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2564 | {NID_sha384, TLSEXT_hash_sha384}, |
| 2565 | {NID_sha512, TLSEXT_hash_sha512}, |
| 2566 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2567 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2568 | static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2569 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}}; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2570 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2571 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) { |
| 2572 | size_t i; |
| 2573 | for (i = 0; i < tlen; i++) { |
| 2574 | if (table[i].nid == nid) { |
| 2575 | return table[i].id; |
| 2576 | } |
| 2577 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2578 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2579 | return -1; |
| 2580 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2581 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 2582 | int tls12_get_sigid(int pkey_type) { |
| 2583 | return tls12_find_id(pkey_type, tls12_sig, |
| 2584 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
| 2585 | } |
| 2586 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 2587 | int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) { |
| 2588 | int md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2589 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2590 | int sig_id = tls12_get_sigid(ssl_private_key_type(ssl)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2591 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 2592 | return md_id != -1 && |
| 2593 | sig_id != -1 && |
| 2594 | CBB_add_u8(out, (uint8_t)md_id) && |
| 2595 | CBB_add_u8(out, (uint8_t)sig_id); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2596 | } |
| 2597 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2598 | const EVP_MD *tls12_get_hash(uint8_t hash_alg) { |
| 2599 | switch (hash_alg) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2600 | case TLSEXT_hash_sha1: |
| 2601 | return EVP_sha1(); |
| 2602 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2603 | case TLSEXT_hash_sha256: |
| 2604 | return EVP_sha256(); |
| 2605 | |
| 2606 | case TLSEXT_hash_sha384: |
| 2607 | return EVP_sha384(); |
| 2608 | |
| 2609 | case TLSEXT_hash_sha512: |
| 2610 | return EVP_sha512(); |
| 2611 | |
| 2612 | default: |
| 2613 | return NULL; |
| 2614 | } |
| 2615 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2616 | |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2617 | /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature |
| 2618 | * algorithm |sig_alg|. It returns -1 if the type is unknown. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2619 | static int tls12_get_pkey_type(uint8_t sig_alg) { |
| 2620 | switch (sig_alg) { |
| 2621 | case TLSEXT_signature_rsa: |
| 2622 | return EVP_PKEY_RSA; |
| 2623 | |
| 2624 | case TLSEXT_signature_ecdsa: |
| 2625 | return EVP_PKEY_EC; |
| 2626 | |
| 2627 | default: |
| 2628 | return -1; |
| 2629 | } |
| 2630 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2631 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2632 | OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2, |
| 2633 | sizeof_tls_sigalgs_is_not_two); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2634 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2635 | int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2636 | /* Extension ignored for inappropriate versions */ |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 2637 | if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2638 | return 1; |
| 2639 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2640 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2641 | CERT *const cert = ssl->cert; |
| 2642 | OPENSSL_free(cert->peer_sigalgs); |
| 2643 | cert->peer_sigalgs = NULL; |
| 2644 | cert->peer_sigalgslen = 0; |
| 2645 | |
| 2646 | size_t num_sigalgs = CBS_len(in_sigalgs); |
| 2647 | |
| 2648 | if (num_sigalgs % 2 != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2649 | return 0; |
| 2650 | } |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2651 | num_sigalgs /= 2; |
| 2652 | |
| 2653 | /* supported_signature_algorithms in the certificate request is |
| 2654 | * allowed to be empty. */ |
| 2655 | if (num_sigalgs == 0) { |
| 2656 | return 1; |
| 2657 | } |
| 2658 | |
| 2659 | /* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two |
| 2660 | * (statically asserted above) and we just divided |num_sigalgs| by two. */ |
| 2661 | cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS)); |
| 2662 | if (cert->peer_sigalgs == NULL) { |
| 2663 | return 0; |
| 2664 | } |
| 2665 | cert->peer_sigalgslen = num_sigalgs; |
| 2666 | |
| 2667 | CBS sigalgs; |
| 2668 | CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs)); |
| 2669 | |
| 2670 | size_t i; |
| 2671 | for (i = 0; i < num_sigalgs; i++) { |
| 2672 | TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i]; |
| 2673 | if (!CBS_get_u8(&sigalgs, &sigalg->rhash) || |
| 2674 | !CBS_get_u8(&sigalgs, &sigalg->rsign)) { |
| 2675 | return 0; |
| 2676 | } |
| 2677 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2678 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2679 | return 1; |
| 2680 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2681 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2682 | const EVP_MD *tls1_choose_signing_digest(SSL *ssl) { |
| 2683 | CERT *cert = ssl->cert; |
| 2684 | int type = ssl_private_key_type(ssl); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2685 | size_t i, j; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2686 | |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2687 | static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512, |
David Benjamin | 72f7e21 | 2016-01-29 15:28:58 -0500 | [diff] [blame] | 2688 | NID_sha1}; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 2689 | |
| 2690 | const int *digest_nids = kDefaultDigestList; |
| 2691 | size_t num_digest_nids = |
| 2692 | sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]); |
| 2693 | if (cert->digest_nids != NULL) { |
| 2694 | digest_nids = cert->digest_nids; |
| 2695 | num_digest_nids = cert->num_digest_nids; |
| 2696 | } |
| 2697 | |
| 2698 | for (i = 0; i < num_digest_nids; i++) { |
| 2699 | const int digest_nid = digest_nids[i]; |
| 2700 | for (j = 0; j < cert->peer_sigalgslen; j++) { |
| 2701 | const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash); |
| 2702 | if (md == NULL || |
| 2703 | digest_nid != EVP_MD_type(md) || |
| 2704 | tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) { |
| 2705 | continue; |
| 2706 | } |
| 2707 | |
| 2708 | return md; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2709 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2710 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2711 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2712 | /* If no suitable digest may be found, default to SHA-1. */ |
| 2713 | return EVP_sha1(); |
| 2714 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2715 | |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 2716 | int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) { |
| 2717 | int ret = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2718 | EVP_MD_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2719 | |
| 2720 | EVP_MD_CTX_init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 2721 | if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) { |
| 2722 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2723 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2724 | |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 2725 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 2726 | EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
| 2727 | |
| 2728 | if (ssl->hit) { |
| 2729 | static const char kResumptionMagic[] = "Resumption"; |
| 2730 | EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
| 2731 | if (ssl->session->original_handshake_hash_len == 0) { |
| 2732 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2733 | goto err; |
| 2734 | } |
| 2735 | EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash, |
| 2736 | ssl->session->original_handshake_hash_len); |
| 2737 | } |
| 2738 | |
| 2739 | uint8_t handshake_hash[EVP_MAX_MD_SIZE]; |
| 2740 | int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash, |
| 2741 | sizeof(handshake_hash)); |
| 2742 | if (handshake_hash_len < 0) { |
| 2743 | goto err; |
| 2744 | } |
| 2745 | EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len); |
| 2746 | unsigned len_u; |
| 2747 | EVP_DigestFinal_ex(&ctx, out, &len_u); |
| 2748 | *out_len = len_u; |
| 2749 | |
| 2750 | ret = 1; |
| 2751 | |
| 2752 | err: |
| 2753 | EVP_MD_CTX_cleanup(&ctx); |
| 2754 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2755 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2756 | |
| 2757 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2758 | * hashes in |ssl->session| so that Channel ID resumptions can sign that |
| 2759 | * data. */ |
| 2760 | int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2761 | int digest_len; |
| 2762 | /* This function should never be called for a resumed session because the |
| 2763 | * handshake hashes that we wish to record are for the original, full |
| 2764 | * handshake. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2765 | if (ssl->hit) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2766 | return -1; |
| 2767 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2768 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2769 | digest_len = |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2770 | tls1_handshake_digest(ssl, ssl->session->original_handshake_hash, |
| 2771 | sizeof(ssl->session->original_handshake_hash)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2772 | if (digest_len < 0) { |
| 2773 | return -1; |
| 2774 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2775 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2776 | ssl->session->original_handshake_hash_len = digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2777 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2778 | return 1; |
| 2779 | } |