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