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