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