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