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