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