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