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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 7 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 14 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 21 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 :-). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 39 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 51 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 65 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 116 | #include <utility> |
| 117 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 118 | #include <openssl/bytestring.h> |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 119 | #include <openssl/chacha.h> |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 120 | #include <openssl/digest.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 121 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | #include <openssl/evp.h> |
| 123 | #include <openssl/hmac.h> |
| 124 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 125 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | #include <openssl/rand.h> |
| 127 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 128 | #include "internal.h" |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 129 | #include "../crypto/internal.h" |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 130 | |
| 131 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 132 | namespace bssl { |
| 133 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 134 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 135 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 136 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 137 | uint16_t u1 = *((const uint16_t *)p1); |
| 138 | uint16_t u2 = *((const uint16_t *)p2); |
| 139 | if (u1 < u2) { |
| 140 | return -1; |
| 141 | } else if (u1 > u2) { |
| 142 | return 1; |
| 143 | } else { |
| 144 | return 0; |
| 145 | } |
| 146 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 147 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 148 | // Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 149 | // more than one extension of the same type in a ClientHello or ServerHello. |
| 150 | // This function does an initial scan over the extensions block to filter those |
| 151 | // out. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 152 | static int tls1_check_duplicate_extensions(const CBS *cbs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 153 | // First pass: count the extensions. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 154 | size_t num_extensions = 0; |
| 155 | CBS extensions = *cbs; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 156 | while (CBS_len(&extensions) > 0) { |
| 157 | uint16_t type; |
| 158 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 159 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 160 | if (!CBS_get_u16(&extensions, &type) || |
| 161 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 162 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 163 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | num_extensions++; |
| 166 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 167 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 168 | if (num_extensions == 0) { |
| 169 | return 1; |
| 170 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 171 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 172 | Array<uint16_t> extension_types; |
| 173 | if (!extension_types.Init(num_extensions)) { |
| 174 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 175 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 176 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 177 | // Second pass: gather the extension types. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 178 | extensions = *cbs; |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 179 | for (size_t i = 0; i < extension_types.size(); i++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 184 | // This should not happen. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 185 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 189 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 190 | // Sort the extensions and make sure there are no duplicates. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 191 | qsort(extension_types.data(), extension_types.size(), sizeof(uint16_t), |
| 192 | compare_uint16_t); |
| 193 | for (size_t i = 1; i < num_extensions; i++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 194 | if (extension_types[i - 1] == extension_types[i]) { |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 195 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 198 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 199 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 200 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 201 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 202 | int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out, |
| 203 | const SSLMessage &msg) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 204 | OPENSSL_memset(out, 0, sizeof(*out)); |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 205 | out->ssl = ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 206 | out->client_hello = CBS_data(&msg.body); |
| 207 | out->client_hello_len = CBS_len(&msg.body); |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 208 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 209 | CBS client_hello, random, session_id; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 210 | CBS_init(&client_hello, out->client_hello, out->client_hello_len); |
| 211 | if (!CBS_get_u16(&client_hello, &out->version) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 212 | !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) || |
| 213 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 214 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 215 | return 0; |
| 216 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 218 | out->random = CBS_data(&random); |
| 219 | out->random_len = CBS_len(&random); |
| 220 | out->session_id = CBS_data(&session_id); |
| 221 | out->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 223 | // Skip past DTLS cookie |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 224 | if (SSL_is_dtls(out->ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 225 | CBS cookie; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 226 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 227 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 232 | CBS cipher_suites, compression_methods; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 233 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 234 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 || |
| 235 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 236 | CBS_len(&compression_methods) < 1) { |
| 237 | return 0; |
| 238 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 239 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 240 | out->cipher_suites = CBS_data(&cipher_suites); |
| 241 | out->cipher_suites_len = CBS_len(&cipher_suites); |
| 242 | out->compression_methods = CBS_data(&compression_methods); |
| 243 | out->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 245 | // If the ClientHello ends here then it's valid, but doesn't have any |
| 246 | // extensions. (E.g. SSLv3.) |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 247 | if (CBS_len(&client_hello) == 0) { |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 248 | out->extensions = NULL; |
| 249 | out->extensions_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 250 | return 1; |
| 251 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 253 | // Extract extensions and check it is valid. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 254 | CBS extensions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 255 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 256 | !tls1_check_duplicate_extensions(&extensions) || |
| 257 | CBS_len(&client_hello) != 0) { |
| 258 | return 0; |
| 259 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 260 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 261 | out->extensions = CBS_data(&extensions); |
| 262 | out->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 264 | return 1; |
| 265 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 266 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 267 | int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello, |
| 268 | CBS *out, uint16_t extension_type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | CBS extensions; |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 270 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 271 | while (CBS_len(&extensions) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 272 | // Decode the next extension. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 273 | uint16_t type; |
| 274 | CBS extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 275 | if (!CBS_get_u16(&extensions, &type) || |
| 276 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 277 | return 0; |
| 278 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 279 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 280 | if (type == extension_type) { |
David Benjamin | cec7344 | 2016-08-02 17:41:33 -0400 | [diff] [blame] | 281 | *out = extension; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 282 | return 1; |
| 283 | } |
| 284 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 286 | return 0; |
| 287 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 289 | static const uint16_t kDefaultGroups[] = { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 290 | SSL_CURVE_X25519, |
| 291 | SSL_CURVE_SECP256R1, |
| 292 | SSL_CURVE_SECP384R1, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 293 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 295 | Span<const uint16_t> tls1_get_grouplist(const SSL *ssl) { |
| 296 | if (ssl->supported_group_list != nullptr) { |
| 297 | return MakeConstSpan(ssl->supported_group_list, |
| 298 | ssl->supported_group_list_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 299 | } |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 300 | return Span<const uint16_t>(kDefaultGroups); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 301 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 302 | |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 303 | int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) { |
| 304 | SSL *const ssl = hs->ssl; |
David Benjamin | f04976b | 2016-10-07 00:37:55 -0400 | [diff] [blame] | 305 | assert(ssl->server); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 306 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 307 | // Clients are not required to send a supported_groups extension. In this |
| 308 | // case, the server is free to pick any group it likes. See RFC 4492, |
| 309 | // section 4, paragraph 3. |
| 310 | // |
| 311 | // However, in the interests of compatibility, we will skip ECDH if the |
| 312 | // client didn't send an extension because we can't be sure that they'll |
| 313 | // support our favoured group. Thus we do not special-case an emtpy |
| 314 | // |peer_supported_group_list|. |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 315 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 316 | Span<const uint16_t> groups = tls1_get_grouplist(ssl); |
| 317 | Span<const uint16_t> pref, supp; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 318 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 319 | pref = groups; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 320 | supp = hs->peer_supported_group_list; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 321 | } else { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 322 | pref = hs->peer_supported_group_list; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 323 | supp = groups; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 324 | } |
| 325 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 326 | for (uint16_t pref_group : pref) { |
| 327 | for (uint16_t supp_group : supp) { |
| 328 | if (pref_group == supp_group) { |
| 329 | *out_group_id = pref_group; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 330 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 335 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 336 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 338 | 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] | 339 | const int *curves, size_t ncurves) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 340 | uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 341 | if (group_ids == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 345 | for (size_t i = 0; i < ncurves; i++) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 346 | if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) { |
| 347 | OPENSSL_free(group_ids); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 348 | return 0; |
| 349 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 352 | OPENSSL_free(*out_group_ids); |
| 353 | *out_group_ids = group_ids; |
| 354 | *out_group_ids_len = ncurves; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 355 | |
| 356 | return 1; |
| 357 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 358 | |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 359 | int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len, |
| 360 | const char *curves) { |
| 361 | uint16_t *group_ids = NULL; |
| 362 | size_t ncurves = 0; |
| 363 | |
| 364 | const char *col; |
| 365 | const char *ptr = curves; |
| 366 | |
| 367 | do { |
| 368 | col = strchr(ptr, ':'); |
| 369 | |
| 370 | uint16_t group_id; |
| 371 | if (!ssl_name_to_group_id(&group_id, ptr, |
| 372 | col ? (size_t)(col - ptr) : strlen(ptr))) { |
| 373 | goto err; |
| 374 | } |
| 375 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 376 | uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc( |
| 377 | group_ids, (ncurves + 1) * sizeof(uint16_t)); |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 378 | if (new_group_ids == NULL) { |
| 379 | goto err; |
| 380 | } |
| 381 | group_ids = new_group_ids; |
| 382 | |
| 383 | group_ids[ncurves] = group_id; |
| 384 | ncurves++; |
| 385 | |
| 386 | if (col) { |
| 387 | ptr = col + 1; |
| 388 | } |
| 389 | } while (col); |
| 390 | |
| 391 | OPENSSL_free(*out_group_ids); |
| 392 | *out_group_ids = group_ids; |
| 393 | *out_group_ids_len = ncurves; |
| 394 | |
| 395 | return 1; |
| 396 | |
| 397 | err: |
| 398 | OPENSSL_free(group_ids); |
| 399 | return 0; |
| 400 | } |
| 401 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 402 | int tls1_check_group_id(const SSL *ssl, uint16_t group_id) { |
| 403 | for (uint16_t supported : tls1_get_grouplist(ssl)) { |
| 404 | if (supported == group_id) { |
David Benjamin | 9d0b4bc | 2016-10-07 00:34:08 -0400 | [diff] [blame] | 405 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 406 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 407 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 408 | |
David Benjamin | 9d0b4bc | 2016-10-07 00:34:08 -0400 | [diff] [blame] | 409 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 411 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 412 | // kVerifySignatureAlgorithms is the default list of accepted signature |
| 413 | // algorithms for verifying. |
| 414 | // |
| 415 | // For now, RSA-PSS signature algorithms are not enabled on Android's system |
| 416 | // BoringSSL. Once the change in Chrome has stuck and the values are finalized, |
| 417 | // restore them. |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 418 | static const uint16_t kVerifySignatureAlgorithms[] = { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 419 | // List our preferred algorithms first. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 420 | SSL_SIGN_ED25519, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 421 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame^] | 422 | SSL_SIGN_RSA_PSS_RSAE_SHA256, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 423 | SSL_SIGN_RSA_PKCS1_SHA256, |
| 424 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 425 | // Larger hashes are acceptable. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 426 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame^] | 427 | SSL_SIGN_RSA_PSS_RSAE_SHA384, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 428 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 429 | |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame^] | 430 | SSL_SIGN_RSA_PSS_RSAE_SHA512, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 431 | SSL_SIGN_RSA_PKCS1_SHA512, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 432 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 433 | // For now, SHA-1 is still accepted but least preferable. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 434 | SSL_SIGN_RSA_PKCS1_SHA1, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 435 | |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 436 | }; |
| 437 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 438 | // kSignSignatureAlgorithms is the default list of supported signature |
| 439 | // algorithms for signing. |
| 440 | // |
| 441 | // For now, RSA-PSS signature algorithms are not enabled on Android's system |
| 442 | // BoringSSL. Once the change in Chrome has stuck and the values are finalized, |
| 443 | // restore them. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 444 | static const uint16_t kSignSignatureAlgorithms[] = { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 445 | // List our preferred algorithms first. |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 446 | SSL_SIGN_ED25519, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 447 | SSL_SIGN_ECDSA_SECP256R1_SHA256, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame^] | 448 | SSL_SIGN_RSA_PSS_RSAE_SHA256, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 449 | SSL_SIGN_RSA_PKCS1_SHA256, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 450 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 451 | // If needed, sign larger hashes. |
| 452 | // |
| 453 | // TODO(davidben): Determine which of these may be pruned. |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 454 | SSL_SIGN_ECDSA_SECP384R1_SHA384, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame^] | 455 | SSL_SIGN_RSA_PSS_RSAE_SHA384, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 456 | SSL_SIGN_RSA_PKCS1_SHA384, |
| 457 | |
| 458 | SSL_SIGN_ECDSA_SECP521R1_SHA512, |
David Benjamin | 6879e19 | 2018-04-13 16:01:02 -0400 | [diff] [blame^] | 459 | SSL_SIGN_RSA_PSS_RSAE_SHA512, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 460 | SSL_SIGN_RSA_PKCS1_SHA512, |
| 461 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 462 | // If the peer supports nothing else, sign with SHA-1. |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 463 | SSL_SIGN_ECDSA_SHA1, |
David Benjamin | 3a322f5 | 2016-10-26 12:45:35 -0400 | [diff] [blame] | 464 | SSL_SIGN_RSA_PKCS1_SHA1, |
Steven Valdez | eff1e8d | 2016-07-06 14:24:47 -0400 | [diff] [blame] | 465 | }; |
| 466 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 467 | bool tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) { |
| 468 | bool use_default = ssl->ctx->num_verify_sigalgs == 0; |
| 469 | Span<const uint16_t> sigalgs = kVerifySignatureAlgorithms; |
| 470 | if (!use_default) { |
| 471 | sigalgs = MakeConstSpan(ssl->ctx->verify_sigalgs, |
| 472 | ssl->ctx->num_verify_sigalgs); |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 473 | } |
| 474 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 475 | for (uint16_t sigalg : sigalgs) { |
| 476 | if (use_default && |
| 477 | sigalg == SSL_SIGN_ED25519 && |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 478 | !ssl->ctx->ed25519_enabled) { |
| 479 | continue; |
| 480 | } |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 481 | if (!CBB_add_u16(out, sigalg)) { |
| 482 | return false; |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 486 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 487 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 488 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 489 | bool tls12_check_peer_sigalg(const SSL *ssl, uint8_t *out_alert, |
| 490 | uint16_t sigalg) { |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 491 | const uint16_t *sigalgs = kVerifySignatureAlgorithms; |
| 492 | size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms); |
| 493 | if (ssl->ctx->num_verify_sigalgs != 0) { |
| 494 | sigalgs = ssl->ctx->verify_sigalgs; |
| 495 | num_sigalgs = ssl->ctx->num_verify_sigalgs; |
| 496 | } |
| 497 | |
| 498 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 499 | if (sigalgs == kVerifySignatureAlgorithms && |
| 500 | sigalgs[i] == SSL_SIGN_ED25519 && |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 501 | !ssl->ctx->ed25519_enabled) { |
| 502 | continue; |
| 503 | } |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 504 | if (sigalg == sigalgs[i]) { |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 505 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 509 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 510 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 511 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 512 | } |
| 513 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 514 | // tls_extension represents a TLS extension that is handled internally. The |
| 515 | // |init| function is called for each handshake, before any other functions of |
| 516 | // the extension. Then the add and parse callbacks are called as needed. |
| 517 | // |
| 518 | // The parse callbacks receive a |CBS| that contains the contents of the |
| 519 | // extension (i.e. not including the type and length bytes). If an extension is |
| 520 | // not received then the parse callbacks will be called with a NULL CBS so that |
| 521 | // they can do any processing needed to handle the absence of an extension. |
| 522 | // |
| 523 | // The add callbacks receive a |CBB| to which the extension can be appended but |
| 524 | // the function is responsible for appending the type and length bytes too. |
| 525 | // |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 526 | // All callbacks return true for success and false for error. If a parse |
| 527 | // function returns zero then a fatal alert with value |*out_alert| will be |
| 528 | // sent. If |*out_alert| isn't set, then a |decode_error| alert will be sent. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 529 | struct tls_extension { |
| 530 | uint16_t value; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 531 | void (*init)(SSL_HANDSHAKE *hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 532 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 533 | bool (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out); |
| 534 | bool (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 535 | CBS *contents); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 536 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 537 | bool (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 538 | CBS *contents); |
| 539 | bool (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 540 | }; |
| 541 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 542 | static bool forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 543 | CBS *contents) { |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 544 | if (contents != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 545 | // Servers MUST NOT send this extension. |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 546 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 547 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 548 | return false; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 549 | } |
| 550 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 551 | return true; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 552 | } |
| 553 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 554 | static bool ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 555 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 556 | // This extension from the client is handled elsewhere. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 557 | return true; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 558 | } |
| 559 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 560 | static bool dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 561 | return true; |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 562 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 563 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 564 | // Server name indication (SNI). |
| 565 | // |
| 566 | // https://tools.ietf.org/html/rfc6066#section-3. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 567 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 568 | static bool ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 569 | SSL *const ssl = hs->ssl; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 570 | if (ssl->tlsext_hostname == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 571 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | CBB contents, server_name_list, name; |
| 575 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 576 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 577 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 578 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 579 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 580 | !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 581 | strlen(ssl->tlsext_hostname)) || |
| 582 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 583 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 584 | } |
| 585 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 586 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 587 | } |
| 588 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 589 | static bool ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 590 | CBS *contents) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 591 | // The server may acknowledge SNI with an empty extension. We check the syntax |
| 592 | // but otherwise ignore this signal. |
| 593 | return contents == NULL || CBS_len(contents) == 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 594 | } |
| 595 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 596 | static bool ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 597 | CBS *contents) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 598 | SSL *const ssl = hs->ssl; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 599 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 600 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 601 | } |
| 602 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 603 | CBS server_name_list, host_name; |
| 604 | uint8_t name_type; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 605 | if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 606 | !CBS_get_u8(&server_name_list, &name_type) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 607 | // Although the server_name extension was intended to be extensible to |
| 608 | // new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 609 | // different name types will cause an error. Further, RFC 4366 originally |
| 610 | // defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 611 | // adding new name types is no longer feasible. |
| 612 | // |
| 613 | // Act as if the extensibility does not exist to simplify parsing. |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 614 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 615 | CBS_len(&server_name_list) != 0 || |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 616 | CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 617 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 618 | } |
| 619 | |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 620 | if (name_type != TLSEXT_NAMETYPE_host_name || |
| 621 | CBS_len(&host_name) == 0 || |
| 622 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 623 | CBS_contains_zero_byte(&host_name)) { |
| 624 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 625 | return false; |
David Benjamin | 9b611e2 | 2016-03-03 08:48:30 -0500 | [diff] [blame] | 626 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 627 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 628 | // Copy the hostname as a string. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 629 | char *raw = nullptr; |
| 630 | if (!CBS_strdup(&host_name, &raw)) { |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 631 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 632 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 633 | } |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 634 | ssl->s3->hostname.reset(raw); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 635 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 636 | hs->should_ack_sni = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 637 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 638 | } |
| 639 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 640 | static bool ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 641 | if (hs->ssl->s3->session_reused || |
| 642 | !hs->should_ack_sni) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 643 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 647 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 648 | return false; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 649 | } |
| 650 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 651 | return true; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 655 | // Renegotiation indication. |
| 656 | // |
| 657 | // https://tools.ietf.org/html/rfc5746 |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 658 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 659 | static bool ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 660 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 661 | // Renegotiation indication is not necessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 662 | if (hs->min_version >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 663 | return true; |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 664 | } |
| 665 | |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 666 | assert(ssl->s3->initial_handshake_complete == |
| 667 | (ssl->s3->previous_client_finished_len != 0)); |
| 668 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 669 | CBB contents, prev_finished; |
| 670 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 671 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 672 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 673 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 674 | ssl->s3->previous_client_finished_len) || |
| 675 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 676 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 677 | } |
| 678 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 679 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 680 | } |
| 681 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 682 | static bool ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 683 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 684 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 685 | if (contents != NULL && ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
Steven Valdez | 246eeee | 2017-03-26 12:49:17 -0500 | [diff] [blame] | 686 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 687 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 688 | } |
| 689 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 690 | // Servers may not switch between omitting the extension and supporting it. |
| 691 | // See RFC 5746, sections 3.5 and 4.2. |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 692 | if (ssl->s3->initial_handshake_complete && |
| 693 | (contents != NULL) != ssl->s3->send_connection_binding) { |
| 694 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 695 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 696 | return false; |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 697 | } |
| 698 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 699 | if (contents == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 700 | // Strictly speaking, if we want to avoid an attack we should *always* see |
| 701 | // RI even on initial ServerHello because the client doesn't see any |
| 702 | // renegotiation during an attack. However this would mean we could not |
| 703 | // connect to any server which doesn't support RI. |
| 704 | // |
| 705 | // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 706 | // practical terms every client sets it so it's just assumed here. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 707 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 711 | ssl->s3->previous_server_finished_len; |
| 712 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 713 | // Check for logic errors |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 714 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 715 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 716 | assert(ssl->s3->initial_handshake_complete == |
| 717 | (ssl->s3->previous_client_finished_len != 0)); |
| 718 | assert(ssl->s3->initial_handshake_complete == |
| 719 | (ssl->s3->previous_server_finished_len != 0)); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 720 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 721 | // Parse out the extension contents. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 722 | CBS renegotiated_connection; |
| 723 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 724 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 725 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 726 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 727 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 728 | } |
| 729 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 730 | // Check that the extension matches. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 731 | if (CBS_len(&renegotiated_connection) != expected_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 732 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 733 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 734 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | const uint8_t *d = CBS_data(&renegotiated_connection); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 738 | bool ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 739 | ssl->s3->previous_client_finished_len) == 0; |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 740 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 741 | ok = true; |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 742 | #endif |
| 743 | if (!ok) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 744 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 745 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 746 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 747 | } |
| 748 | d += ssl->s3->previous_client_finished_len; |
| 749 | |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 750 | ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 751 | ssl->s3->previous_server_finished_len) == 0; |
| 752 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 753 | ok = true; |
David Benjamin | 9343b0b | 2017-07-01 00:31:27 -0400 | [diff] [blame] | 754 | #endif |
| 755 | if (!ok) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 756 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 10e1060 | 2017-07-25 13:33:21 -0700 | [diff] [blame] | 757 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 758 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 759 | } |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 760 | ssl->s3->send_connection_binding = true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 761 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 762 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 763 | } |
| 764 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 765 | static bool ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 766 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 767 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 768 | // Renegotiation isn't supported as a server so this function should never be |
| 769 | // called after the initial handshake. |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 770 | assert(!ssl->s3->initial_handshake_complete); |
| 771 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 772 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 773 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 774 | } |
| 775 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 776 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 777 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | CBS renegotiated_connection; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 781 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 782 | CBS_len(contents) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 783 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 784 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 785 | } |
| 786 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 787 | // Check that the extension matches. We do not support renegotiation as a |
| 788 | // server, so this must be empty. |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 789 | if (CBS_len(&renegotiated_connection) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 790 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 791 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 792 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 793 | } |
| 794 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 795 | ssl->s3->send_connection_binding = true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 796 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 797 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 798 | } |
| 799 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 800 | static bool ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 801 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 802 | // Renegotiation isn't supported as a server so this function should never be |
| 803 | // called after the initial handshake. |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 804 | assert(!ssl->s3->initial_handshake_complete); |
| 805 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 806 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 807 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 808 | } |
| 809 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 810 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
David Benjamin | 52bf690 | 2016-10-08 12:05:03 -0400 | [diff] [blame] | 811 | !CBB_add_u16(out, 1 /* length */) || |
| 812 | !CBB_add_u8(out, 0 /* empty renegotiation info */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 813 | return false; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 814 | } |
| 815 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 816 | return true; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 819 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 820 | // Extended Master Secret. |
| 821 | // |
| 822 | // https://tools.ietf.org/html/rfc7627 |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 823 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 824 | static bool ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 825 | // Extended master secret is not necessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 826 | if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 827 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 831 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 832 | return false; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 833 | } |
| 834 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 835 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 836 | } |
| 837 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 838 | static bool ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 839 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 840 | SSL *const ssl = hs->ssl; |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 841 | |
| 842 | if (contents != NULL) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 843 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 844 | ssl->version == SSL3_VERSION || |
| 845 | CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 846 | return false; |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 847 | } |
| 848 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 849 | hs->extended_master_secret = true; |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 850 | } |
| 851 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 852 | // Whether EMS is negotiated may not change on renegotiation. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 853 | if (ssl->s3->established_session != nullptr && |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 854 | hs->extended_master_secret != |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 855 | !!ssl->s3->established_session->extended_master_secret) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 856 | OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH); |
| 857 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 858 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 859 | } |
| 860 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 861 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 862 | } |
| 863 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 864 | static bool ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 865 | CBS *contents) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 866 | uint16_t version = ssl_protocol_version(hs->ssl); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 867 | if (version >= TLS1_3_VERSION || |
| 868 | version == SSL3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 869 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 873 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 877 | return false; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 878 | } |
| 879 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 880 | hs->extended_master_secret = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 881 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 882 | } |
| 883 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 884 | static bool ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 885 | if (!hs->extended_master_secret) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 886 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 890 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 891 | return false; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 892 | } |
| 893 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 894 | return true; |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 897 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 898 | // Session tickets. |
| 899 | // |
| 900 | // https://tools.ietf.org/html/rfc5077 |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 901 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 902 | static bool ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 903 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 904 | // TLS 1.3 uses a different ticket extension. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 905 | if (hs->min_version >= TLS1_3_VERSION || |
David Benjamin | 7c7d831 | 2016-08-20 13:39:03 -0400 | [diff] [blame] | 906 | SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 907 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | const uint8_t *ticket_data = NULL; |
| 911 | int ticket_len = 0; |
| 912 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 913 | // Renegotiation does not participate in session resumption. However, still |
| 914 | // advertise the extension to avoid potentially breaking servers which carry |
| 915 | // over the state from the previous handshake, such as OpenSSL servers |
| 916 | // without upstream's 3c3f0259238594d77264a78944d409f2127642c4. |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 917 | if (!ssl->s3->initial_handshake_complete && |
| 918 | ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 919 | ssl->session->tlsext_tick != NULL && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 920 | // Don't send TLS 1.3 session tickets in the ticket extension. |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 921 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 922 | ticket_data = ssl->session->tlsext_tick; |
| 923 | ticket_len = ssl->session->tlsext_ticklen; |
| 924 | } |
| 925 | |
| 926 | CBB ticket; |
| 927 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 928 | !CBB_add_u16_length_prefixed(out, &ticket) || |
| 929 | !CBB_add_bytes(&ticket, ticket_data, ticket_len) || |
| 930 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 931 | return false; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 932 | } |
| 933 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 934 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 935 | } |
| 936 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 937 | static bool ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 938 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 939 | SSL *const ssl = hs->ssl; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 940 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 941 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 942 | } |
| 943 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 944 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 945 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 946 | } |
| 947 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 948 | // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 949 | // this function should never be called, even if the server tries to send the |
| 950 | // extension. |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 951 | assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 952 | |
| 953 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 954 | return false; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 955 | } |
| 956 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 957 | hs->ticket_expected = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 958 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 959 | } |
| 960 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 961 | static bool ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 962 | if (!hs->ticket_expected) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 963 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 964 | } |
| 965 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 966 | // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 967 | assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0); |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 968 | |
| 969 | if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 970 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 971 | return false; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 972 | } |
| 973 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 974 | return true; |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 978 | // Signature Algorithms. |
| 979 | // |
| 980 | // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 981 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 982 | static bool ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 983 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 984 | if (hs->max_version < TLS1_2_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 985 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 986 | } |
| 987 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 988 | CBB contents, sigalgs_cbb; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 989 | if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) || |
| 990 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 991 | !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) || |
| 992 | !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) || |
| 993 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 994 | return false; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 995 | } |
| 996 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 997 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 998 | } |
| 999 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1000 | static bool ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1001 | CBS *contents) { |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 1002 | hs->peer_sigalgs.Reset(); |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1003 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1004 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | CBS supported_signature_algorithms; |
| 1008 | if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1009 | CBS_len(contents) != 0 || |
| 1010 | CBS_len(&supported_signature_algorithms) == 0 || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1011 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1012 | return false; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1015 | return true; |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 1018 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1019 | // OCSP Stapling. |
| 1020 | // |
| 1021 | // https://tools.ietf.org/html/rfc6066#section-8 |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1022 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1023 | static bool ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1024 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1025 | if (!ssl->ocsp_stapling_enabled) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1026 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | CBB contents; |
| 1030 | if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1031 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1032 | !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1033 | !CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1034 | !CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1035 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1036 | return false; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1039 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1042 | static bool ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1043 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1044 | SSL *const ssl = hs->ssl; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1045 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1046 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1049 | // TLS 1.3 OCSP responses are included in the Certificate extensions. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1050 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1051 | return false; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1054 | // OCSP stapling is forbidden on non-certificate ciphers. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1055 | if (CBS_len(contents) != 0 || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1056 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1057 | return false; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1058 | } |
| 1059 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1060 | // Note this does not check for resumption in TLS 1.2. Sending |
| 1061 | // status_request here does not make sense, but OpenSSL does so and the |
| 1062 | // specification does not say anything. Tolerate it but ignore it. |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1063 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1064 | hs->certificate_status_expected = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1065 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1068 | static bool ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1069 | CBS *contents) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1070 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1071 | return true; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | uint8_t status_type; |
| 1075 | if (!CBS_get_u8(contents, &status_type)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1076 | return false; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1079 | // We cannot decide whether OCSP stapling will occur yet because the correct |
| 1080 | // SSL_CTX might not have been selected. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1081 | hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1082 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1083 | return true; |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1086 | static bool ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1087 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1088 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1089 | !hs->ocsp_stapling_requested || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1090 | ssl->cert->ocsp_response == NULL || |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 1091 | ssl->s3->session_reused || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1092 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1093 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1096 | hs->certificate_status_expected = true; |
David Benjamin | 942f4ed | 2016-07-16 19:03:49 +0300 | [diff] [blame] | 1097 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1098 | return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1099 | CBB_add_u16(out, 0 /* length */); |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1103 | // Next protocol negotiation. |
| 1104 | // |
| 1105 | // https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1106 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1107 | static bool ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1108 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1109 | if (ssl->s3->initial_handshake_complete || |
| 1110 | ssl->ctx->next_proto_select_cb == NULL || |
David Benjamin | 9d125dc | 2016-12-07 21:32:37 -0500 | [diff] [blame] | 1111 | SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1112 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1116 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1117 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1120 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1123 | static bool ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1124 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1125 | SSL *const ssl = hs->ssl; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1126 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1127 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1130 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1131 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1134 | // If any of these are false then we should never have sent the NPN |
| 1135 | // extension in the ClientHello and thus this function should never have been |
| 1136 | // called. |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1137 | assert(!ssl->s3->initial_handshake_complete); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1138 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1139 | assert(ssl->ctx->next_proto_select_cb != NULL); |
| 1140 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1141 | if (!ssl->s3->alpn_selected.empty()) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1142 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1143 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1144 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1145 | return false; |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1146 | } |
| 1147 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1148 | const uint8_t *const orig_contents = CBS_data(contents); |
| 1149 | const size_t orig_len = CBS_len(contents); |
| 1150 | |
| 1151 | while (CBS_len(contents) != 0) { |
| 1152 | CBS proto; |
| 1153 | if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1154 | CBS_len(&proto) == 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1155 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | uint8_t *selected; |
| 1160 | uint8_t selected_len; |
| 1161 | if (ssl->ctx->next_proto_select_cb( |
| 1162 | ssl, &selected, &selected_len, orig_contents, orig_len, |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1163 | ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK || |
| 1164 | !ssl->s3->next_proto_negotiated.CopyFrom( |
| 1165 | MakeConstSpan(selected, selected_len))) { |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1166 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1167 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1170 | hs->next_proto_neg_seen = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1171 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1174 | static bool ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1175 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1176 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1177 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1178 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1179 | } |
| 1180 | |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1181 | if (contents != NULL && CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1182 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | if (contents == NULL || |
| 1186 | ssl->s3->initial_handshake_complete || |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1187 | ssl->ctx->next_protos_advertised_cb == NULL || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1188 | SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1189 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1192 | hs->next_proto_neg_seen = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1193 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1196 | static bool ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1197 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1198 | // |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1199 | // parsed. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1200 | if (!hs->next_proto_neg_seen) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1201 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | const uint8_t *npa; |
| 1205 | unsigned npa_len; |
| 1206 | |
| 1207 | if (ssl->ctx->next_protos_advertised_cb( |
| 1208 | ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1209 | SSL_TLSEXT_ERR_OK) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1210 | hs->next_proto_neg_seen = false; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1211 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | CBB contents; |
| 1215 | if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1216 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1217 | !CBB_add_bytes(&contents, npa, npa_len) || |
| 1218 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1219 | return false; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1222 | return true; |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1226 | // Signed certificate timestamps. |
| 1227 | // |
| 1228 | // https://tools.ietf.org/html/rfc6962#section-3.3.1 |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1229 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1230 | static bool ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1231 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1232 | if (!ssl->signed_cert_timestamps_enabled) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1233 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1237 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1238 | return false; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1241 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1244 | static bool ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1245 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1246 | SSL *const ssl = hs->ssl; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1247 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1248 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1251 | // TLS 1.3 SCTs are included in the Certificate extensions. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1252 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1253 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1254 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1255 | } |
| 1256 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1257 | // If this is false then we should never have sent the SCT extension in the |
| 1258 | // ClientHello and thus this function should never have been called. |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1259 | assert(ssl->signed_cert_timestamps_enabled); |
| 1260 | |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 1261 | if (!ssl_is_sct_list_valid(contents)) { |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1262 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1263 | return false; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1266 | // Session resumption uses the original session information. The extension |
| 1267 | // should not be sent on resumption, but RFC 6962 did not make it a |
| 1268 | // requirement, so tolerate this. |
| 1269 | // |
| 1270 | // TODO(davidben): Enforce this anyway. |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1271 | if (!ssl->s3->session_reused) { |
| 1272 | CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list); |
| 1273 | hs->new_session->signed_cert_timestamp_list = |
| 1274 | CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool); |
| 1275 | if (hs->new_session->signed_cert_timestamp_list == nullptr) { |
| 1276 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1277 | return false; |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1278 | } |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1281 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1284 | static bool ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1285 | CBS *contents) { |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1286 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1287 | return true; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1291 | return false; |
David Benjamin | 53210cb | 2016-11-16 09:01:48 +0900 | [diff] [blame] | 1292 | } |
| 1293 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1294 | hs->scts_requested = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1295 | return true; |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1298 | static bool ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1299 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1300 | // The extension shouldn't be sent when resuming sessions. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1301 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1302 | ssl->s3->session_reused || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1303 | ssl->cert->signed_cert_timestamp_list == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1304 | return true; |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | CBB contents; |
| 1308 | return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1309 | CBB_add_u16_length_prefixed(out, &contents) && |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1310 | CBB_add_bytes( |
| 1311 | &contents, |
| 1312 | CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list), |
| 1313 | CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) && |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1314 | CBB_flush(out); |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1318 | // Application-level Protocol Negotiation. |
| 1319 | // |
| 1320 | // https://tools.ietf.org/html/rfc7301 |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1321 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1322 | static bool ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1323 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1324 | if (ssl->alpn_client_proto_list == NULL || |
| 1325 | ssl->s3->initial_handshake_complete) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1326 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | CBB contents, proto_list; |
| 1330 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1331 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1332 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1333 | !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1334 | ssl->alpn_client_proto_list_len) || |
| 1335 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1336 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1339 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1342 | static bool ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1343 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1344 | SSL *const ssl = hs->ssl; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1345 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1346 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | assert(!ssl->s3->initial_handshake_complete); |
| 1350 | assert(ssl->alpn_client_proto_list != NULL); |
| 1351 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1352 | if (hs->next_proto_neg_seen) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1353 | // NPN and ALPN may not be negotiated in the same connection. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1354 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1355 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1356 | return false; |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 1357 | } |
| 1358 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1359 | // The extension data consists of a ProtocolNameList which must have |
| 1360 | // exactly one ProtocolName. Each of these is length-prefixed. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1361 | CBS protocol_name_list, protocol_name; |
| 1362 | if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1363 | CBS_len(contents) != 0 || |
| 1364 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1365 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1366 | CBS_len(&protocol_name) == 0 || |
| 1367 | CBS_len(&protocol_name_list) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1368 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1371 | if (!ssl_is_alpn_protocol_allowed(ssl, protocol_name)) { |
| 1372 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL); |
| 1373 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1374 | return false; |
David Benjamin | 3e51757d | 2016-08-11 11:52:23 -0400 | [diff] [blame] | 1375 | } |
| 1376 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1377 | if (!ssl->s3->alpn_selected.CopyFrom(protocol_name)) { |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1378 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1379 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1382 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1385 | bool ssl_is_alpn_protocol_allowed(const SSL *ssl, |
| 1386 | Span<const uint8_t> protocol) { |
| 1387 | if (ssl->alpn_client_proto_list == nullptr) { |
| 1388 | return false; |
| 1389 | } |
| 1390 | |
| 1391 | if (ssl->ctx->allow_unknown_alpn_protos) { |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
| 1395 | // Check that the protocol name is one of the ones we advertised. |
| 1396 | CBS client_protocol_name_list, client_protocol_name; |
| 1397 | CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list, |
| 1398 | ssl->alpn_client_proto_list_len); |
| 1399 | while (CBS_len(&client_protocol_name_list) > 0) { |
| 1400 | if (!CBS_get_u8_length_prefixed(&client_protocol_name_list, |
| 1401 | &client_protocol_name)) { |
| 1402 | return false; |
| 1403 | } |
| 1404 | |
| 1405 | if (client_protocol_name == protocol) { |
| 1406 | return true; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | return false; |
| 1411 | } |
| 1412 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1413 | bool ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1414 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1415 | SSL *const ssl = hs->ssl; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1416 | CBS contents; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1417 | if (ssl->ctx->alpn_select_cb == NULL || |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 1418 | !ssl_client_hello_get_extension( |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1419 | client_hello, &contents, |
| 1420 | TLSEXT_TYPE_application_layer_protocol_negotiation)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1421 | // Ignore ALPN if not configured or no extension was supplied. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1422 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1425 | // ALPN takes precedence over NPN. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1426 | hs->next_proto_neg_seen = false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1427 | |
| 1428 | CBS protocol_name_list; |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1429 | if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) || |
| 1430 | CBS_len(&contents) != 0 || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1431 | CBS_len(&protocol_name_list) < 2) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1432 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1433 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1434 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1437 | // Validate the protocol list. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1438 | CBS protocol_name_list_copy = protocol_name_list; |
| 1439 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1440 | CBS protocol_name; |
| 1441 | |
| 1442 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1443 | // Empty protocol names are forbidden. |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1444 | CBS_len(&protocol_name) == 0) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1445 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 1446 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1447 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | const uint8_t *selected; |
| 1452 | uint8_t selected_len; |
| 1453 | if (ssl->ctx->alpn_select_cb( |
| 1454 | ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1455 | CBS_len(&protocol_name_list), |
| 1456 | ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1457 | if (!ssl->s3->alpn_selected.CopyFrom( |
| 1458 | MakeConstSpan(selected, selected_len))) { |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1459 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1460 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1461 | } |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1464 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1467 | static bool ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1468 | SSL *const ssl = hs->ssl; |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1469 | if (ssl->s3->alpn_selected.empty()) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1470 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | CBB contents, proto_list, proto; |
| 1474 | if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1475 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1476 | !CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1477 | !CBB_add_u8_length_prefixed(&proto_list, &proto) || |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1478 | !CBB_add_bytes(&proto, ssl->s3->alpn_selected.data(), |
| 1479 | ssl->s3->alpn_selected.size()) || |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1480 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1481 | return false; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1484 | return true; |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1488 | // Channel ID. |
| 1489 | // |
| 1490 | // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1491 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1492 | static void ext_channel_id_init(SSL_HANDSHAKE *hs) { |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1493 | hs->ssl->s3->tlsext_channel_id_valid = false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1496 | static bool ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1497 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1498 | if (!ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1499 | SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1500 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1504 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1505 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1508 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1511 | static bool ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1512 | uint8_t *out_alert, |
| 1513 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1514 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1515 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1516 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1519 | assert(!SSL_is_dtls(ssl)); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1520 | assert(ssl->tlsext_channel_id_enabled); |
| 1521 | |
| 1522 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1523 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1526 | ssl->s3->tlsext_channel_id_valid = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1527 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1530 | static bool ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1531 | uint8_t *out_alert, |
| 1532 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1533 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1534 | if (contents == NULL || |
| 1535 | !ssl->tlsext_channel_id_enabled || |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1536 | SSL_is_dtls(ssl)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1537 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | if (CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1541 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1544 | ssl->s3->tlsext_channel_id_valid = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1545 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1548 | static bool ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1549 | SSL *const ssl = hs->ssl; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1550 | if (!ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1551 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1555 | !CBB_add_u16(out, 0 /* length */)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1556 | return false; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1559 | return true; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1562 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1563 | // Secure Real-time Transport Protocol (SRTP) extension. |
| 1564 | // |
| 1565 | // https://tools.ietf.org/html/rfc5764 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1566 | |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1567 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1568 | static void ext_srtp_init(SSL_HANDSHAKE *hs) { |
| 1569 | hs->ssl->srtp_profile = NULL; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1572 | static bool ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1573 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1574 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1575 | if (profiles == NULL || |
| 1576 | sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1577 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | CBB contents, profile_ids; |
| 1581 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1582 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1583 | !CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1584 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1587 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
| 1588 | if (!CBB_add_u16(&profile_ids, profile->id)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1589 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1594 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1595 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1598 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1601 | static bool ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1602 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1603 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1604 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1605 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1608 | // The extension consists of a u16-prefixed profile ID list containing a |
| 1609 | // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1610 | // |
| 1611 | // See https://tools.ietf.org/html/rfc5764#section-4.1.1 |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1612 | CBS profile_ids, srtp_mki; |
| 1613 | uint16_t profile_id; |
| 1614 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1615 | !CBS_get_u16(&profile_ids, &profile_id) || |
| 1616 | CBS_len(&profile_ids) != 0 || |
| 1617 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1618 | CBS_len(contents) != 0) { |
| 1619 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1620 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | if (CBS_len(&srtp_mki) != 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1624 | // Must be no MKI, since we never offer one. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1625 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1626 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1627 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1631 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1632 | // Check to see if the server gave us something we support (and presumably |
| 1633 | // offered). |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1634 | for (const SRTP_PROTECTION_PROFILE *profile : profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1635 | if (profile->id == profile_id) { |
| 1636 | ssl->srtp_profile = profile; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1637 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1642 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1643 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1646 | static bool ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1647 | CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1648 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1649 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1650 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | CBS profile_ids, srtp_mki; |
| 1654 | if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1655 | CBS_len(&profile_ids) < 2 || |
| 1656 | !CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1657 | CBS_len(contents) != 0) { |
| 1658 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1659 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1660 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1661 | // Discard the MKI value for now. |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1662 | |
| 1663 | const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1664 | SSL_get_srtp_profiles(ssl); |
| 1665 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1666 | // Pick the server's most preferred profile. |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 1667 | for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) { |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1668 | CBS profile_ids_tmp; |
| 1669 | CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1670 | |
| 1671 | while (CBS_len(&profile_ids_tmp) > 0) { |
| 1672 | uint16_t profile_id; |
| 1673 | if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1674 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | if (server_profile->id == profile_id) { |
| 1678 | ssl->srtp_profile = server_profile; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1679 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1680 | } |
| 1681 | } |
| 1682 | } |
| 1683 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1684 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1687 | static bool ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1688 | SSL *const ssl = hs->ssl; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1689 | if (ssl->srtp_profile == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1690 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | CBB contents, profile_ids; |
| 1694 | if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1695 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1696 | !CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1697 | !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1698 | !CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1699 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1700 | return false; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1703 | return true; |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1706 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1707 | // EC point formats. |
| 1708 | // |
| 1709 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1710 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1711 | static bool ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1712 | CBB contents, formats; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1713 | if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1714 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1715 | !CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1716 | !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1717 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1718 | return false; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1721 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1724 | static bool ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1725 | // The point format extension is unneccessary in TLS 1.3. |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1726 | if (hs->min_version >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1727 | return true; |
David Benjamin | 70aba26 | 2016-11-01 12:08:15 -0400 | [diff] [blame] | 1728 | } |
| 1729 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1730 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1733 | static bool ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 1734 | CBS *contents) { |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1735 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1736 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1739 | if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1740 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1741 | } |
| 1742 | |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1743 | CBS ec_point_format_list; |
| 1744 | if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1745 | CBS_len(contents) != 0) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1746 | return false; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1749 | // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1750 | // point format. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1751 | if (OPENSSL_memchr(CBS_data(&ec_point_format_list), |
| 1752 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1753 | CBS_len(&ec_point_format_list)) == NULL) { |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 1754 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1755 | return false; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1758 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1761 | static bool ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1762 | CBS *contents) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1763 | if (ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1764 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1765 | } |
| 1766 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1767 | return ext_ec_point_parse_serverhello(hs, out_alert, contents); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1770 | static bool ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1771 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1772 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1773 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 1774 | } |
| 1775 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1776 | const uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1777 | const uint32_t alg_a = hs->new_cipher->algorithm_auth; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1778 | const bool using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1779 | |
| 1780 | if (!using_ecc) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1781 | return true; |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1784 | return ext_ec_point_add_extension(hs, out); |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1787 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1788 | // Pre Shared Key |
| 1789 | // |
| 1790 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6 |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1791 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1792 | static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) { |
| 1793 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1794 | if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL || |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1795 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1799 | size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session)); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1800 | return 15 + ssl->session->tlsext_ticklen + binder_len; |
| 1801 | } |
| 1802 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1803 | static bool ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1804 | SSL *const ssl = hs->ssl; |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1805 | hs->needs_psk_binder = false; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1806 | if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL || |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1807 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1808 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1809 | } |
| 1810 | |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1811 | // Per draft-ietf-tls-tls13-21 section 4.1.4, skip offering the session if the |
| 1812 | // selected cipher in HelloRetryRequest does not match. This avoids performing |
| 1813 | // the transcript hash transformation for multiple hashes. |
| 1814 | if (hs->received_hello_retry_request && |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1815 | ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) { |
| 1816 | return true; |
| 1817 | } |
| 1818 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1819 | struct OPENSSL_timeval now; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1820 | ssl_get_current_time(ssl, &now); |
| 1821 | uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time); |
| 1822 | uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add; |
| 1823 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1824 | // Fill in a placeholder zero binder of the appropriate length. It will be |
| 1825 | // computed and filled in later after length prefixes are computed. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1826 | uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0}; |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1827 | size_t binder_len = EVP_MD_size(ssl_session_get_digest(ssl->session)); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1828 | |
| 1829 | CBB contents, identity, ticket, binders, binder; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1830 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1831 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1832 | !CBB_add_u16_length_prefixed(&contents, &identity) || |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1833 | !CBB_add_u16_length_prefixed(&identity, &ticket) || |
| 1834 | !CBB_add_bytes(&ticket, ssl->session->tlsext_tick, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1835 | ssl->session->tlsext_ticklen) || |
| 1836 | !CBB_add_u32(&identity, obfuscated_ticket_age) || |
| 1837 | !CBB_add_u16_length_prefixed(&contents, &binders) || |
| 1838 | !CBB_add_u8_length_prefixed(&binders, &binder) || |
| 1839 | !CBB_add_bytes(&binder, zero_binder, binder_len)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1840 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1841 | } |
| 1842 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1843 | hs->needs_psk_binder = true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1844 | return CBB_flush(out); |
| 1845 | } |
| 1846 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1847 | bool ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs, |
| 1848 | uint8_t *out_alert, |
| 1849 | CBS *contents) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1850 | uint16_t psk_id; |
| 1851 | if (!CBS_get_u16(contents, &psk_id) || |
| 1852 | CBS_len(contents) != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1853 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1854 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1855 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1856 | } |
| 1857 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1858 | // We only advertise one PSK identity, so the only legal index is zero. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1859 | if (psk_id != 0) { |
David Benjamin | 7f78df4 | 2016-10-05 22:33:19 -0400 | [diff] [blame] | 1860 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1861 | *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1862 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1863 | } |
| 1864 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1865 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1866 | } |
| 1867 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1868 | bool ssl_ext_pre_shared_key_parse_clienthello( |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1869 | SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders, |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1870 | uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1871 | // We only process the first PSK identity since we don't support pure PSK. |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1872 | CBS identities, binders; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1873 | if (!CBS_get_u16_length_prefixed(contents, &identities) || |
David Benjamin | 707af29 | 2017-03-10 17:47:18 -0500 | [diff] [blame] | 1874 | !CBS_get_u16_length_prefixed(&identities, out_ticket) || |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 1875 | !CBS_get_u32(&identities, out_obfuscated_ticket_age) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1876 | !CBS_get_u16_length_prefixed(contents, &binders) || |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1877 | CBS_len(&binders) == 0 || |
Steven Valdez | af3b8a9 | 2016-11-01 12:49:22 -0400 | [diff] [blame] | 1878 | CBS_len(contents) != 0) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1879 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1880 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1881 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1882 | } |
| 1883 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1884 | *out_binders = binders; |
| 1885 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1886 | // Check the syntax of the remaining identities, but do not process them. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1887 | size_t num_identities = 1; |
| 1888 | while (CBS_len(&identities) != 0) { |
| 1889 | CBS unused_ticket; |
| 1890 | uint32_t unused_obfuscated_ticket_age; |
| 1891 | if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) || |
| 1892 | !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) { |
| 1893 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1894 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1895 | return false; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | num_identities++; |
| 1899 | } |
| 1900 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1901 | // Check the syntax of the binders. The value will be checked later if |
| 1902 | // resuming. |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1903 | size_t num_binders = 0; |
| 1904 | while (CBS_len(&binders) != 0) { |
| 1905 | CBS binder; |
| 1906 | if (!CBS_get_u8_length_prefixed(&binders, &binder)) { |
| 1907 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1908 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1909 | return false; |
David Benjamin | aedf303 | 2016-12-01 16:47:56 -0500 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | num_binders++; |
| 1913 | } |
| 1914 | |
| 1915 | if (num_identities != num_binders) { |
| 1916 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH); |
| 1917 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1918 | return false; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 1919 | } |
| 1920 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1921 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1922 | } |
| 1923 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1924 | bool ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8baf963 | 2016-11-17 17:11:16 +0900 | [diff] [blame] | 1925 | if (!hs->ssl->s3->session_reused) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1926 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | CBB contents; |
| 1930 | if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) || |
| 1931 | !CBB_add_u16_length_prefixed(out, &contents) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1932 | // We only consider the first identity for resumption |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1933 | !CBB_add_u16(&contents, 0) || |
| 1934 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1935 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1936 | } |
| 1937 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1938 | return true; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1942 | // Pre-Shared Key Exchange Modes |
| 1943 | // |
| 1944 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7 |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 1945 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1946 | static bool ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs, |
| 1947 | CBB *out) { |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 1948 | if (hs->max_version < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1949 | return true; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | CBB contents, ke_modes; |
| 1953 | if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) || |
| 1954 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1955 | !CBB_add_u8_length_prefixed(&contents, &ke_modes) || |
| 1956 | !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1957 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | return CBB_flush(out); |
| 1961 | } |
| 1962 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1963 | static bool ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs, |
| 1964 | uint8_t *out_alert, |
| 1965 | CBS *contents) { |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 1966 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1967 | return true; |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 1968 | } |
| 1969 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1970 | CBS ke_modes; |
| 1971 | if (!CBS_get_u8_length_prefixed(contents, &ke_modes) || |
| 1972 | CBS_len(&ke_modes) == 0 || |
| 1973 | CBS_len(contents) != 0) { |
| 1974 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1975 | return false; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1976 | } |
| 1977 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1978 | // We only support tickets with PSK_DHE_KE. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1979 | hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE, |
| 1980 | CBS_len(&ke_modes)) != NULL; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1981 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1982 | return true; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1986 | // Early Data Indication |
| 1987 | // |
| 1988 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8 |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 1989 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 1990 | static bool ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1991 | SSL *const ssl = hs->ssl; |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1992 | if (!ssl->cert->enable_early_data || |
| 1993 | // Session must be 0-RTT capable. |
| 1994 | ssl->session == NULL || |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1995 | ssl_session_protocol_version(ssl->session) < TLS1_3_VERSION || |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1996 | ssl->session->ticket_max_early_data == 0 || |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1997 | // The second ClientHello never offers early data. |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1998 | hs->received_hello_retry_request || |
David Benjamin | dd6c2e8 | 2017-10-17 15:48:46 -0400 | [diff] [blame] | 1999 | // In case ALPN preferences changed since this session was established, |
| 2000 | // avoid reporting a confusing value in |SSL_get0_alpn_selected|. |
| 2001 | (ssl->session->early_alpn_len != 0 && |
| 2002 | !ssl_is_alpn_protocol_allowed( |
| 2003 | ssl, MakeConstSpan(ssl->session->early_alpn, |
| 2004 | ssl->session->early_alpn_len)))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2005 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2006 | } |
| 2007 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2008 | hs->early_data_offered = true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2009 | |
| 2010 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2011 | !CBB_add_u16(out, 0) || |
| 2012 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2013 | return false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2014 | } |
| 2015 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2016 | return true; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2017 | } |
| 2018 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2019 | static bool ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2020 | uint8_t *out_alert, CBS *contents) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2021 | SSL *const ssl = hs->ssl; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2022 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2023 | return true; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | if (CBS_len(contents) != 0) { |
| 2027 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2028 | return false; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2029 | } |
| 2030 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2031 | if (!ssl->s3->session_reused) { |
| 2032 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2033 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2034 | return false; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2035 | } |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2036 | |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 2037 | ssl->s3->early_data_accepted = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2038 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2039 | } |
| 2040 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2041 | static bool ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2042 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2043 | SSL *const ssl = hs->ssl; |
| 2044 | if (contents == NULL || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 2045 | ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2046 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | if (CBS_len(contents) != 0) { |
| 2050 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2051 | return false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2052 | } |
| 2053 | |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 2054 | hs->early_data_offered = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2055 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2056 | } |
| 2057 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2058 | static bool ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 2059 | if (!hs->ssl->s3->early_data_accepted) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2060 | return true; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) || |
| 2064 | !CBB_add_u16(out, 0) || |
| 2065 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2066 | return false; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2067 | } |
| 2068 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2069 | return true; |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2073 | // Key Share |
| 2074 | // |
| 2075 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5 |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2076 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2077 | static bool ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2078 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2079 | if (hs->max_version < TLS1_3_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2080 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | CBB contents, kse_bytes; |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 2084 | if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2085 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2086 | !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2087 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2088 | } |
| 2089 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2090 | uint16_t group_id = hs->retry_group; |
| 2091 | if (hs->received_hello_retry_request) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2092 | // We received a HelloRetryRequest without a new curve, so there is no new |
| 2093 | // share to append. Leave |hs->key_share| as-is. |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2094 | if (group_id == 0 && |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2095 | !CBB_add_bytes(&kse_bytes, hs->key_share_bytes.data(), |
| 2096 | hs->key_share_bytes.size())) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2097 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2098 | } |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2099 | hs->key_share_bytes.Reset(); |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2100 | if (group_id == 0) { |
| 2101 | return CBB_flush(out); |
| 2102 | } |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2103 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2104 | // Add a fake group. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2105 | if (ssl->ctx->grease_enabled && |
| 2106 | (!CBB_add_u16(&kse_bytes, |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2107 | ssl_get_grease_value(hs, ssl_grease_group)) || |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2108 | !CBB_add_u16(&kse_bytes, 1 /* length */) || |
| 2109 | !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2110 | return false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2111 | } |
| 2112 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2113 | // Predict the most preferred group. |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2114 | Span<const uint16_t> groups = tls1_get_grouplist(ssl); |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2115 | if (groups.empty()) { |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2116 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2117 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2118 | } |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2119 | |
| 2120 | group_id = groups[0]; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2121 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2122 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2123 | hs->key_share = SSLKeyShare::Create(group_id); |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2124 | CBB key_exchange; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2125 | if (!hs->key_share || |
| 2126 | !CBB_add_u16(&kse_bytes, group_id) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2127 | !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) || |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2128 | !hs->key_share->Offer(&key_exchange) || |
David Benjamin | c8b6b4f | 2016-09-08 23:47:48 -0400 | [diff] [blame] | 2129 | !CBB_flush(&kse_bytes)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2130 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2131 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2132 | |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2133 | // Save the contents of the extension to repeat it in the second ClientHello. |
| 2134 | if (!hs->received_hello_retry_request && |
| 2135 | !hs->key_share_bytes.CopyFrom( |
| 2136 | MakeConstSpan(CBB_data(&kse_bytes), CBB_len(&kse_bytes)))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2137 | return false; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 2138 | } |
| 2139 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2140 | return CBB_flush(out); |
| 2141 | } |
| 2142 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2143 | bool ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2144 | Array<uint8_t> *out_secret, |
| 2145 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2146 | CBS peer_key; |
David Benjamin | 5c4e857 | 2016-08-19 17:44:53 -0400 | [diff] [blame] | 2147 | uint16_t group_id; |
| 2148 | if (!CBS_get_u16(contents, &group_id) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2149 | !CBS_get_u16_length_prefixed(contents, &peer_key) || |
| 2150 | CBS_len(contents) != 0) { |
David Benjamin | ac4d534 | 2017-11-17 01:42:04 +0800 | [diff] [blame] | 2151 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2152 | *out_alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2153 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2154 | } |
| 2155 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2156 | if (hs->key_share->GroupID() != group_id) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2157 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2158 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2159 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2160 | } |
| 2161 | |
David Benjamin | 6b3ab72 | 2017-09-21 02:37:58 -0400 | [diff] [blame] | 2162 | if (!hs->key_share->Finish(out_secret, out_alert, peer_key)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2163 | *out_alert = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2164 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2165 | } |
| 2166 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2167 | hs->new_session->group_id = group_id; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2168 | hs->key_share.reset(); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2169 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2170 | } |
| 2171 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2172 | bool ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, bool *out_found, |
| 2173 | Array<uint8_t> *out_secret, |
| 2174 | uint8_t *out_alert, CBS *contents) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2175 | uint16_t group_id; |
| 2176 | CBS key_shares; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 2177 | if (!tls1_get_shared_group(hs, &group_id)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2178 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 2179 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2180 | return false; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | if (!CBS_get_u16_length_prefixed(contents, &key_shares) || |
David Benjamin | a70de14 | 2016-08-02 16:52:57 -0400 | [diff] [blame] | 2184 | CBS_len(contents) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2185 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2186 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2187 | } |
| 2188 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2189 | // Find the corresponding key share. |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2190 | CBS peer_key; |
Steven Valdez | 619c8ce | 2017-10-16 13:12:33 -0400 | [diff] [blame] | 2191 | CBS_init(&peer_key, NULL, 0); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2192 | while (CBS_len(&key_shares) > 0) { |
| 2193 | uint16_t id; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2194 | CBS peer_key_tmp; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2195 | if (!CBS_get_u16(&key_shares, &id) || |
Steven Valdez | 619c8ce | 2017-10-16 13:12:33 -0400 | [diff] [blame] | 2196 | !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp) || |
| 2197 | CBS_len(&peer_key_tmp) == 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2198 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2199 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2200 | } |
| 2201 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2202 | if (id == group_id) { |
Steven Valdez | 619c8ce | 2017-10-16 13:12:33 -0400 | [diff] [blame] | 2203 | if (CBS_len(&peer_key) != 0) { |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2204 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE); |
| 2205 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2206 | return false; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2207 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2208 | |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2209 | peer_key = peer_key_tmp; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2210 | // Continue parsing the structure to keep peers honest. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2211 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2212 | } |
| 2213 | |
Steven Valdez | 619c8ce | 2017-10-16 13:12:33 -0400 | [diff] [blame] | 2214 | if (CBS_len(&peer_key) == 0) { |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2215 | *out_found = false; |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2216 | out_secret->Reset(); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2217 | return true; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2218 | } |
| 2219 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2220 | // Compute the DH secret. |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2221 | Array<uint8_t> secret; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 2222 | ScopedCBB public_key; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 2223 | UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id); |
David Benjamin | 6b3ab72 | 2017-09-21 02:37:58 -0400 | [diff] [blame] | 2224 | if (!key_share || |
| 2225 | !CBB_init(public_key.get(), 32) || |
| 2226 | !key_share->Accept(public_key.get(), &secret, out_alert, peer_key) || |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2227 | !CBBFinishArray(public_key.get(), &hs->ecdh_public_key)) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 2228 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2229 | return false; |
David Benjamin | 7e1f984 | 2016-09-20 19:24:40 -0400 | [diff] [blame] | 2230 | } |
| 2231 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 2232 | *out_secret = std::move(secret); |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 2233 | *out_found = true; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2234 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2235 | } |
| 2236 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2237 | bool ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2238 | uint16_t group_id; |
| 2239 | CBB kse_bytes, public_key; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 2240 | if (!tls1_get_shared_group(hs, &group_id) || |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 2241 | !CBB_add_u16(out, TLSEXT_TYPE_key_share) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2242 | !CBB_add_u16_length_prefixed(out, &kse_bytes) || |
| 2243 | !CBB_add_u16(&kse_bytes, group_id) || |
| 2244 | !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) || |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2245 | !CBB_add_bytes(&public_key, hs->ecdh_public_key.data(), |
| 2246 | hs->ecdh_public_key.size()) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2247 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2248 | return false; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2249 | } |
| 2250 | |
David Benjamin | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 2251 | hs->ecdh_public_key.Reset(); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 2252 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2253 | hs->new_session->group_id = group_id; |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2254 | return true; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2258 | // Supported Versions |
| 2259 | // |
| 2260 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1 |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2261 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2262 | static bool ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2263 | SSL *const ssl = hs->ssl; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 2264 | if (hs->max_version <= TLS1_2_VERSION) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2265 | return true; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | CBB contents, versions; |
| 2269 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || |
| 2270 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2271 | !CBB_add_u8_length_prefixed(&contents, &versions)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2272 | return false; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2273 | } |
| 2274 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2275 | // Add a fake version. See draft-davidben-tls-grease-01. |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2276 | if (ssl->ctx->grease_enabled && |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2277 | !CBB_add_u16(&versions, ssl_get_grease_value(hs, ssl_grease_version))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2278 | return false; |
David Benjamin | d9791bf | 2016-09-27 16:39:52 -0400 | [diff] [blame] | 2279 | } |
| 2280 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 2281 | if (!ssl_add_supported_versions(hs, &versions) || |
| 2282 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2283 | return false; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2284 | } |
| 2285 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2286 | return true; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2290 | // Cookie |
| 2291 | // |
| 2292 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2 |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2293 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2294 | static bool ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | b949355 | 2017-09-27 19:02:51 -0400 | [diff] [blame] | 2295 | if (hs->cookie.empty()) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2296 | return true; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | CBB contents, cookie; |
| 2300 | if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) || |
| 2301 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2302 | !CBB_add_u16_length_prefixed(&contents, &cookie) || |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2303 | !CBB_add_bytes(&cookie, hs->cookie.data(), hs->cookie.size()) || |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2304 | !CBB_flush(out)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2305 | return false; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2306 | } |
| 2307 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2308 | // The cookie is no longer needed in memory. |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2309 | hs->cookie.Reset(); |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2310 | return true; |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2314 | // Dummy PQ Padding extension |
| 2315 | // |
| 2316 | // Dummy post-quantum padding invovles the client (and later server) sending |
| 2317 | // useless, random-looking bytes in an extension in their ClientHello or |
| 2318 | // ServerHello. These extensions are sized to simulate a post-quantum |
| 2319 | // key-exchange and so enable measurement of the latency impact of the |
| 2320 | // additional bandwidth. |
| 2321 | |
Adam Langley | 4702db6 | 2018-02-26 13:51:55 -0800 | [diff] [blame] | 2322 | static bool ext_dummy_pq_padding_add(CBB *out, size_t len) { |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2323 | CBB contents; |
| 2324 | uint8_t *buffer; |
| 2325 | if (!CBB_add_u16(out, TLSEXT_TYPE_dummy_pq_padding) || |
| 2326 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2327 | !CBB_add_space(&contents, &buffer, len)) { |
| 2328 | return false; |
| 2329 | } |
| 2330 | |
| 2331 | // The length is used as the nonce so that different length extensions have |
| 2332 | // different contents. There's no reason this has to be the case, it just |
| 2333 | // makes things a little more obvious in a packet dump. |
| 2334 | uint8_t nonce[12] = {0}; |
| 2335 | memcpy(nonce, &len, sizeof(len)); |
| 2336 | |
| 2337 | memset(buffer, 0, len); |
| 2338 | static const uint8_t kZeroKey[32] = {0}; |
| 2339 | CRYPTO_chacha_20(buffer, buffer, len, kZeroKey, nonce, 0); |
| 2340 | |
| 2341 | return CBB_flush(out); |
| 2342 | } |
| 2343 | |
Adam Langley | 4702db6 | 2018-02-26 13:51:55 -0800 | [diff] [blame] | 2344 | static bool ext_dummy_pq_padding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2345 | const size_t len = hs->ssl->dummy_pq_padding_len; |
| 2346 | if (len == 0) { |
| 2347 | return true; |
| 2348 | } |
| 2349 | |
| 2350 | return ext_dummy_pq_padding_add(out, len); |
| 2351 | } |
| 2352 | |
Adam Langley | 8df8e64 | 2018-02-28 13:35:39 -0800 | [diff] [blame] | 2353 | static bool ext_dummy_pq_padding_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2354 | uint8_t *out_alert, |
| 2355 | CBS *contents) { |
| 2356 | if (contents == nullptr) { |
| 2357 | return true; |
| 2358 | } |
| 2359 | |
| 2360 | if (CBS_len(contents) != hs->ssl->dummy_pq_padding_len) { |
| 2361 | return false; |
| 2362 | } |
| 2363 | |
| 2364 | hs->ssl->did_dummy_pq_padding = true; |
| 2365 | return true; |
| 2366 | } |
| 2367 | |
Adam Langley | 4702db6 | 2018-02-26 13:51:55 -0800 | [diff] [blame] | 2368 | static bool ext_dummy_pq_padding_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2369 | uint8_t *out_alert, |
| 2370 | CBS *contents) { |
| 2371 | if (contents != nullptr && |
| 2372 | 0 < CBS_len(contents) && CBS_len(contents) < (1 << 12)) { |
| 2373 | hs->dummy_pq_padding_len = CBS_len(contents); |
| 2374 | } |
| 2375 | |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | static bool ext_dummy_pq_padding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2380 | if (!hs->dummy_pq_padding_len) { |
| 2381 | return true; |
| 2382 | } |
| 2383 | |
| 2384 | return ext_dummy_pq_padding_add(out, hs->dummy_pq_padding_len); |
| 2385 | } |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2386 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2387 | // Negotiated Groups |
| 2388 | // |
| 2389 | // https://tools.ietf.org/html/rfc4492#section-5.1.2 |
| 2390 | // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4 |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2391 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2392 | static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2393 | SSL *const ssl = hs->ssl; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2394 | CBB contents, groups_bytes; |
| 2395 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) || |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2396 | !CBB_add_u16_length_prefixed(out, &contents) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2397 | !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2398 | return false; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2399 | } |
| 2400 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2401 | // Add a fake group. See draft-davidben-tls-grease-01. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2402 | if (ssl->ctx->grease_enabled && |
| 2403 | !CBB_add_u16(&groups_bytes, |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2404 | ssl_get_grease_value(hs, ssl_grease_group))) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2405 | return false; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2406 | } |
| 2407 | |
David Benjamin | cf0ce67 | 2017-09-21 02:25:59 -0400 | [diff] [blame] | 2408 | for (uint16_t group : tls1_get_grouplist(ssl)) { |
| 2409 | if (!CBB_add_u16(&groups_bytes, group)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2410 | return false; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | return CBB_flush(out); |
| 2415 | } |
| 2416 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2417 | static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2418 | uint8_t *out_alert, |
| 2419 | CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2420 | // This extension is not expected to be echoed by servers in TLS 1.2, but some |
| 2421 | // BigIP servers send it nonetheless, so do not enforce this. |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2422 | return true; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2425 | static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) { |
| 2426 | CBS copy = *cbs; |
| 2427 | if ((CBS_len(©) & 1) != 0) { |
| 2428 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2429 | return false; |
| 2430 | } |
| 2431 | |
| 2432 | Array<uint16_t> ret; |
| 2433 | if (!ret.Init(CBS_len(©) / 2)) { |
| 2434 | return false; |
| 2435 | } |
| 2436 | for (size_t i = 0; i < ret.size(); i++) { |
| 2437 | if (!CBS_get_u16(©, &ret[i])) { |
| 2438 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2439 | return false; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | assert(CBS_len(©) == 0); |
| 2444 | *out = std::move(ret); |
| 2445 | return 1; |
| 2446 | } |
| 2447 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2448 | static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs, |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2449 | uint8_t *out_alert, |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2450 | CBS *contents) { |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2451 | if (contents == NULL) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2452 | return true; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2455 | CBS supported_group_list; |
| 2456 | if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 2457 | CBS_len(&supported_group_list) == 0 || |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 2458 | CBS_len(contents) != 0 || |
| 2459 | !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) { |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2460 | return false; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2463 | return true; |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2466 | // Token Binding |
| 2467 | // |
| 2468 | // https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10 |
| 2469 | |
| 2470 | // The Token Binding version number currently matches the draft number of |
| 2471 | // draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100. |
| 2472 | // Since there are no wire changes to the protocol from draft 13 through the |
| 2473 | // current draft (16), this implementation supports all versions in that range. |
| 2474 | static uint16_t kTokenBindingMaxVersion = 16; |
| 2475 | static uint16_t kTokenBindingMinVersion = 13; |
| 2476 | |
| 2477 | static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2478 | SSL *const ssl = hs->ssl; |
| 2479 | if (ssl->token_binding_params == nullptr || SSL_is_dtls(ssl)) { |
| 2480 | return true; |
| 2481 | } |
| 2482 | |
| 2483 | CBB contents, params; |
| 2484 | if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) || |
| 2485 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2486 | !CBB_add_u16(&contents, kTokenBindingMaxVersion) || |
| 2487 | !CBB_add_u8_length_prefixed(&contents, ¶ms) || |
| 2488 | !CBB_add_bytes(¶ms, ssl->token_binding_params, |
| 2489 | ssl->token_binding_params_len) || |
| 2490 | !CBB_flush(out)) { |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
| 2494 | return true; |
| 2495 | } |
| 2496 | |
| 2497 | static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2498 | uint8_t *out_alert, |
| 2499 | CBS *contents) { |
| 2500 | SSL *const ssl = hs->ssl; |
| 2501 | if (contents == nullptr) { |
| 2502 | return true; |
| 2503 | } |
| 2504 | |
| 2505 | CBS params_list; |
| 2506 | uint16_t version; |
| 2507 | uint8_t param; |
| 2508 | if (!CBS_get_u16(contents, &version) || |
| 2509 | !CBS_get_u8_length_prefixed(contents, ¶ms_list) || |
| 2510 | !CBS_get_u8(¶ms_list, ¶m) || |
| 2511 | CBS_len(¶ms_list) > 0 || |
| 2512 | CBS_len(contents) > 0) { |
| 2513 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2514 | return false; |
| 2515 | } |
| 2516 | |
| 2517 | // The server-negotiated version must be less than or equal to our version. |
| 2518 | if (version > kTokenBindingMaxVersion) { |
| 2519 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2520 | return false; |
| 2521 | } |
| 2522 | |
| 2523 | // If the server-selected version is less than what we support, then Token |
| 2524 | // Binding wasn't negotiated (but the extension was parsed successfully). |
| 2525 | if (version < kTokenBindingMinVersion) { |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | for (size_t i = 0; i < ssl->token_binding_params_len; ++i) { |
| 2530 | if (param == ssl->token_binding_params[i]) { |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2531 | ssl->s3->negotiated_token_binding_param = param; |
| 2532 | ssl->s3->token_binding_negotiated = true; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2533 | return true; |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 2538 | return false; |
| 2539 | } |
| 2540 | |
| 2541 | // select_tb_param looks for the first token binding param in |
| 2542 | // |ssl->token_binding_params| that is also in |params| and puts it in |
| 2543 | // |ssl->negotiated_token_binding_param|. It returns true if a token binding |
| 2544 | // param is found, and false otherwise. |
| 2545 | static bool select_tb_param(SSL *ssl, Span<const uint8_t> peer_params) { |
| 2546 | for (size_t i = 0; i < ssl->token_binding_params_len; ++i) { |
| 2547 | uint8_t tb_param = ssl->token_binding_params[i]; |
| 2548 | for (uint8_t peer_param : peer_params) { |
| 2549 | if (tb_param == peer_param) { |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2550 | ssl->s3->negotiated_token_binding_param = tb_param; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2551 | return true; |
| 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | return false; |
| 2556 | } |
| 2557 | |
| 2558 | static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2559 | uint8_t *out_alert, |
| 2560 | CBS *contents) { |
| 2561 | SSL *const ssl = hs->ssl; |
| 2562 | if (contents == nullptr || ssl->token_binding_params == nullptr) { |
| 2563 | return true; |
| 2564 | } |
| 2565 | |
| 2566 | CBS params; |
| 2567 | uint16_t version; |
| 2568 | if (!CBS_get_u16(contents, &version) || |
| 2569 | !CBS_get_u8_length_prefixed(contents, ¶ms) || |
| 2570 | CBS_len(¶ms) == 0 || |
| 2571 | CBS_len(contents) > 0) { |
| 2572 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2573 | return false; |
| 2574 | } |
| 2575 | |
| 2576 | // If the client-selected version is less than what we support, then Token |
| 2577 | // Binding wasn't negotiated (but the extension was parsed successfully). |
| 2578 | if (version < kTokenBindingMinVersion) { |
| 2579 | return true; |
| 2580 | } |
| 2581 | |
| 2582 | // If the client-selected version is higher than we support, use our max |
| 2583 | // version. Otherwise, use the client's version. |
| 2584 | hs->negotiated_token_binding_version = |
| 2585 | std::min(version, kTokenBindingMaxVersion); |
| 2586 | if (!select_tb_param(ssl, params)) { |
| 2587 | return true; |
| 2588 | } |
| 2589 | |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2590 | ssl->s3->token_binding_negotiated = true; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2591 | return true; |
| 2592 | } |
| 2593 | |
| 2594 | static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) { |
| 2595 | SSL *const ssl = hs->ssl; |
| 2596 | |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2597 | if (!ssl->s3->token_binding_negotiated) { |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2598 | return true; |
| 2599 | } |
| 2600 | |
| 2601 | CBB contents, params; |
| 2602 | if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) || |
| 2603 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2604 | !CBB_add_u16(&contents, hs->negotiated_token_binding_version) || |
| 2605 | !CBB_add_u8_length_prefixed(&contents, ¶ms) || |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2606 | !CBB_add_u8(¶ms, ssl->s3->negotiated_token_binding_param) || |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2607 | !CBB_flush(out)) { |
| 2608 | return false; |
| 2609 | } |
| 2610 | |
| 2611 | return true; |
| 2612 | } |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2613 | |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2614 | // QUIC Transport Parameters |
| 2615 | |
| 2616 | static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs, |
| 2617 | CBB *out) { |
| 2618 | SSL *const ssl = hs->ssl; |
| 2619 | if (!ssl->quic_transport_params || hs->max_version <= TLS1_2_VERSION) { |
| 2620 | return true; |
| 2621 | } |
| 2622 | |
| 2623 | CBB contents; |
| 2624 | if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) || |
| 2625 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2626 | !CBB_add_bytes(&contents, ssl->quic_transport_params, |
| 2627 | ssl->quic_transport_params_len) || |
| 2628 | !CBB_flush(out)) { |
| 2629 | return false; |
| 2630 | } |
| 2631 | return true; |
| 2632 | } |
| 2633 | |
| 2634 | static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs, |
| 2635 | uint8_t *out_alert, |
| 2636 | CBS *contents) { |
| 2637 | SSL *const ssl = hs->ssl; |
| 2638 | if (contents == nullptr) { |
| 2639 | return true; |
| 2640 | } |
| 2641 | // QUIC requires TLS 1.3. |
| 2642 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2643 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2644 | return false; |
| 2645 | } |
| 2646 | |
| 2647 | return ssl->s3->peer_quic_transport_params.CopyFrom(*contents); |
| 2648 | } |
| 2649 | |
| 2650 | static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs, |
| 2651 | uint8_t *out_alert, |
| 2652 | CBS *contents) { |
| 2653 | SSL *const ssl = hs->ssl; |
| 2654 | if (!contents || !ssl->quic_transport_params) { |
| 2655 | return true; |
| 2656 | } |
| 2657 | // Ignore the extension before TLS 1.3. |
| 2658 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
| 2662 | return ssl->s3->peer_quic_transport_params.CopyFrom(*contents); |
| 2663 | } |
| 2664 | |
| 2665 | static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs, |
| 2666 | CBB *out) { |
| 2667 | SSL *const ssl = hs->ssl; |
| 2668 | if (!ssl->quic_transport_params) { |
| 2669 | return true; |
| 2670 | } |
| 2671 | |
| 2672 | CBB contents; |
| 2673 | if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) || |
| 2674 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 2675 | !CBB_add_bytes(&contents, ssl->quic_transport_params, |
| 2676 | ssl->quic_transport_params_len) || |
| 2677 | !CBB_flush(out)) { |
| 2678 | return false; |
| 2679 | } |
| 2680 | |
| 2681 | return true; |
| 2682 | } |
| 2683 | |
| 2684 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2685 | // kExtensions contains all the supported extensions. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2686 | static const struct tls_extension kExtensions[] = { |
| 2687 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 2688 | TLSEXT_TYPE_renegotiate, |
| 2689 | NULL, |
| 2690 | ext_ri_add_clienthello, |
| 2691 | ext_ri_parse_serverhello, |
| 2692 | ext_ri_parse_clienthello, |
| 2693 | ext_ri_add_serverhello, |
| 2694 | }, |
| 2695 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2696 | TLSEXT_TYPE_server_name, |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2697 | NULL, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2698 | ext_sni_add_clienthello, |
| 2699 | ext_sni_parse_serverhello, |
| 2700 | ext_sni_parse_clienthello, |
| 2701 | ext_sni_add_serverhello, |
| 2702 | }, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2703 | { |
| 2704 | TLSEXT_TYPE_extended_master_secret, |
David Benjamin | 163c956 | 2016-08-29 23:14:17 -0400 | [diff] [blame] | 2705 | NULL, |
Adam Langley | 0a05671 | 2015-07-01 15:03:33 -0700 | [diff] [blame] | 2706 | ext_ems_add_clienthello, |
| 2707 | ext_ems_parse_serverhello, |
| 2708 | ext_ems_parse_clienthello, |
| 2709 | ext_ems_add_serverhello, |
| 2710 | }, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2711 | { |
| 2712 | TLSEXT_TYPE_session_ticket, |
| 2713 | NULL, |
| 2714 | ext_ticket_add_clienthello, |
| 2715 | ext_ticket_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2716 | // Ticket extension client parsing is handled in ssl_session.c |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2717 | ignore_parse_clienthello, |
Adam Langley | 9b05bc5 | 2015-07-01 15:25:33 -0700 | [diff] [blame] | 2718 | ext_ticket_add_serverhello, |
| 2719 | }, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2720 | { |
| 2721 | TLSEXT_TYPE_signature_algorithms, |
| 2722 | NULL, |
| 2723 | ext_sigalgs_add_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2724 | forbid_parse_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2725 | ext_sigalgs_parse_clienthello, |
Steven Valdez | 6b8509a | 2016-07-12 13:38:32 -0400 | [diff] [blame] | 2726 | dont_add_serverhello, |
Adam Langley | 2e857bd | 2015-07-01 16:09:19 -0700 | [diff] [blame] | 2727 | }, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2728 | { |
| 2729 | TLSEXT_TYPE_status_request, |
David Benjamin | c253864 | 2017-01-13 16:32:05 -0500 | [diff] [blame] | 2730 | NULL, |
Adam Langley | bb0bd04 | 2015-07-01 16:21:03 -0700 | [diff] [blame] | 2731 | ext_ocsp_add_clienthello, |
| 2732 | ext_ocsp_parse_serverhello, |
| 2733 | ext_ocsp_parse_clienthello, |
| 2734 | ext_ocsp_add_serverhello, |
| 2735 | }, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2736 | { |
| 2737 | TLSEXT_TYPE_next_proto_neg, |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 2738 | NULL, |
Adam Langley | 97dfcbf | 2015-07-01 18:35:20 -0700 | [diff] [blame] | 2739 | ext_npn_add_clienthello, |
| 2740 | ext_npn_parse_serverhello, |
| 2741 | ext_npn_parse_clienthello, |
| 2742 | ext_npn_add_serverhello, |
| 2743 | }, |
Adam Langley | ab8d87d | 2015-07-10 12:21:39 -0700 | [diff] [blame] | 2744 | { |
| 2745 | TLSEXT_TYPE_certificate_timestamp, |
| 2746 | NULL, |
| 2747 | ext_sct_add_clienthello, |
| 2748 | ext_sct_parse_serverhello, |
| 2749 | ext_sct_parse_clienthello, |
| 2750 | ext_sct_add_serverhello, |
| 2751 | }, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2752 | { |
| 2753 | TLSEXT_TYPE_application_layer_protocol_negotiation, |
David Benjamin | 35598ae | 2016-11-16 15:38:27 +0900 | [diff] [blame] | 2754 | NULL, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2755 | ext_alpn_add_clienthello, |
| 2756 | ext_alpn_parse_serverhello, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2757 | // ALPN is negotiated late in |ssl_negotiate_alpn|. |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 2758 | ignore_parse_clienthello, |
Adam Langley | f18e453 | 2015-07-10 13:39:53 -0700 | [diff] [blame] | 2759 | ext_alpn_add_serverhello, |
| 2760 | }, |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2761 | { |
| 2762 | TLSEXT_TYPE_channel_id, |
| 2763 | ext_channel_id_init, |
| 2764 | ext_channel_id_add_clienthello, |
| 2765 | ext_channel_id_parse_serverhello, |
| 2766 | ext_channel_id_parse_clienthello, |
| 2767 | ext_channel_id_add_serverhello, |
| 2768 | }, |
Adam Langley | 391250d | 2015-07-15 19:06:07 -0700 | [diff] [blame] | 2769 | { |
| 2770 | TLSEXT_TYPE_srtp, |
| 2771 | ext_srtp_init, |
| 2772 | ext_srtp_add_clienthello, |
| 2773 | ext_srtp_parse_serverhello, |
| 2774 | ext_srtp_parse_clienthello, |
| 2775 | ext_srtp_add_serverhello, |
| 2776 | }, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2777 | { |
| 2778 | TLSEXT_TYPE_ec_point_formats, |
David Benjamin | fc05994 | 2015-07-30 23:01:59 -0400 | [diff] [blame] | 2779 | NULL, |
Adam Langley | bdd5d66 | 2015-07-20 16:19:08 -0700 | [diff] [blame] | 2780 | ext_ec_point_add_clienthello, |
| 2781 | ext_ec_point_parse_serverhello, |
| 2782 | ext_ec_point_parse_clienthello, |
| 2783 | ext_ec_point_add_serverhello, |
| 2784 | }, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2785 | { |
Steven Valdez | 7e5dd25 | 2018-01-22 15:20:31 -0500 | [diff] [blame] | 2786 | TLSEXT_TYPE_key_share, |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 2787 | NULL, |
| 2788 | ext_key_share_add_clienthello, |
| 2789 | forbid_parse_serverhello, |
| 2790 | ignore_parse_clienthello, |
| 2791 | dont_add_serverhello, |
| 2792 | }, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2793 | { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2794 | TLSEXT_TYPE_psk_key_exchange_modes, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2795 | NULL, |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2796 | ext_psk_key_exchange_modes_add_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2797 | forbid_parse_serverhello, |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 2798 | ext_psk_key_exchange_modes_parse_clienthello, |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 2799 | dont_add_serverhello, |
| 2800 | }, |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2801 | { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2802 | TLSEXT_TYPE_early_data, |
| 2803 | NULL, |
| 2804 | ext_early_data_add_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2805 | ext_early_data_parse_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2806 | ext_early_data_parse_clienthello, |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 2807 | ext_early_data_add_serverhello, |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 2808 | }, |
| 2809 | { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 2810 | TLSEXT_TYPE_supported_versions, |
| 2811 | NULL, |
| 2812 | ext_supported_versions_add_clienthello, |
| 2813 | forbid_parse_serverhello, |
| 2814 | ignore_parse_clienthello, |
| 2815 | dont_add_serverhello, |
| 2816 | }, |
David Benjamin | 3baa6e1 | 2016-10-07 21:10:38 -0400 | [diff] [blame] | 2817 | { |
| 2818 | TLSEXT_TYPE_cookie, |
| 2819 | NULL, |
| 2820 | ext_cookie_add_clienthello, |
| 2821 | forbid_parse_serverhello, |
| 2822 | ignore_parse_clienthello, |
| 2823 | dont_add_serverhello, |
| 2824 | }, |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2825 | { |
| 2826 | TLSEXT_TYPE_dummy_pq_padding, |
| 2827 | NULL, |
| 2828 | ext_dummy_pq_padding_add_clienthello, |
Adam Langley | 8df8e64 | 2018-02-28 13:35:39 -0800 | [diff] [blame] | 2829 | ext_dummy_pq_padding_parse_serverhello, |
Adam Langley | 4702db6 | 2018-02-26 13:51:55 -0800 | [diff] [blame] | 2830 | ext_dummy_pq_padding_parse_clienthello, |
| 2831 | ext_dummy_pq_padding_add_serverhello, |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2832 | }, |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 2833 | { |
| 2834 | TLSEXT_TYPE_quic_transport_parameters, |
| 2835 | NULL, |
| 2836 | ext_quic_transport_params_add_clienthello, |
| 2837 | ext_quic_transport_params_parse_serverhello, |
| 2838 | ext_quic_transport_params_parse_clienthello, |
| 2839 | ext_quic_transport_params_add_serverhello, |
| 2840 | }, |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2841 | // The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2842 | // intolerant to the last extension being zero-length. See |
| 2843 | // https://crbug.com/363583. |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2844 | { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2845 | TLSEXT_TYPE_supported_groups, |
David Benjamin | 43612b6 | 2016-10-07 00:41:50 -0400 | [diff] [blame] | 2846 | NULL, |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 2847 | ext_supported_groups_add_clienthello, |
| 2848 | ext_supported_groups_parse_serverhello, |
| 2849 | ext_supported_groups_parse_clienthello, |
David Benjamin | 3164093 | 2017-10-11 13:22:39 -0400 | [diff] [blame] | 2850 | dont_add_serverhello, |
Adam Langley | 273d49c | 2015-07-20 16:38:52 -0700 | [diff] [blame] | 2851 | }, |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2852 | { |
| 2853 | TLSEXT_TYPE_token_binding, |
| 2854 | NULL, |
| 2855 | ext_token_binding_add_clienthello, |
| 2856 | ext_token_binding_parse_serverhello, |
| 2857 | ext_token_binding_parse_clienthello, |
| 2858 | ext_token_binding_add_serverhello, |
| 2859 | }, |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2860 | }; |
| 2861 | |
| 2862 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2863 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 2864 | static_assert(kNumExtensions <= |
| 2865 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8, |
| 2866 | "too many extensions for sent bitset"); |
| 2867 | static_assert(kNumExtensions <= |
| 2868 | sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8, |
| 2869 | "too many extensions for received bitset"); |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 2870 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2871 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2872 | uint16_t value) { |
| 2873 | unsigned i; |
| 2874 | for (i = 0; i < kNumExtensions; i++) { |
| 2875 | if (kExtensions[i].value == value) { |
| 2876 | *out_index = i; |
| 2877 | return &kExtensions[i]; |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | return NULL; |
| 2882 | } |
| 2883 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2884 | int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) { |
| 2885 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2886 | // Don't add extensions for SSLv3 unless doing secure renegotiation. |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 2887 | if (hs->client_version == SSL3_VERSION && |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2888 | !ssl->s3->send_connection_binding) { |
| 2889 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2890 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2891 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2892 | CBB extensions; |
| 2893 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2894 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2895 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2896 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2897 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2898 | hs->extensions.sent = 0; |
| 2899 | hs->custom_extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2900 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2901 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2902 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2903 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2904 | } |
| 2905 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2906 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2907 | uint16_t grease_ext1 = 0; |
| 2908 | if (ssl->ctx->grease_enabled) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2909 | // Add a fake empty extension. See draft-davidben-tls-grease-01. |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2910 | grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1); |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2911 | if (!CBB_add_u16(&extensions, grease_ext1) || |
| 2912 | !CBB_add_u16(&extensions, 0 /* zero length */)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2913 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2914 | return 0; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 2918 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2919 | const size_t len_before = CBB_len(&extensions); |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2920 | if (!kExtensions[i].add_clienthello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2921 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 2922 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2923 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2924 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2925 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2926 | if (CBB_len(&extensions) != len_before) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2927 | hs->extensions.sent |= (1u << i); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2928 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2929 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2930 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 2931 | if (!custom_ext_add_clienthello(hs, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2932 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2933 | return 0; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2936 | if (ssl->ctx->grease_enabled) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2937 | // Add a fake non-empty extension. See draft-davidben-tls-grease-01. |
David Benjamin | a7bc944 | 2018-01-18 10:08:53 -0500 | [diff] [blame] | 2938 | uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2); |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2939 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2940 | // The two fake extensions must not have the same value. GREASE values are |
| 2941 | // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different |
| 2942 | // one. |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2943 | if (grease_ext1 == grease_ext2) { |
| 2944 | grease_ext2 ^= 0x1010; |
| 2945 | } |
| 2946 | |
| 2947 | if (!CBB_add_u16(&extensions, grease_ext2) || |
| 2948 | !CBB_add_u16(&extensions, 1 /* one byte length */) || |
| 2949 | !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2950 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2951 | return 0; |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2955 | if (!SSL_is_dtls(ssl)) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2956 | size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2957 | header_len += 2 + CBB_len(&extensions) + psk_extension_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2958 | if (header_len > 0xff && header_len < 0x200) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2959 | // Add padding to workaround bugs in F5 terminators. See RFC 7685. |
| 2960 | // |
| 2961 | // NB: because this code works out the length of all existing extensions |
| 2962 | // it MUST always appear last. |
David Benjamin | 0a96859 | 2015-07-21 22:06:19 -0400 | [diff] [blame] | 2963 | size_t padding_len = 0x200 - header_len; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2964 | // Extensions take at least four bytes to encode. Always include at least |
| 2965 | // one byte of data if including the extension. WebSphere Application |
| 2966 | // Server 7.0 is intolerant to the last extension being zero-length. See |
| 2967 | // https://crbug.com/363583. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2968 | if (padding_len >= 4 + 1) { |
| 2969 | padding_len -= 4; |
| 2970 | } else { |
| 2971 | padding_len = 1; |
| 2972 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2973 | |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2974 | uint8_t *padding_bytes; |
| 2975 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2976 | !CBB_add_u16(&extensions, padding_len) || |
| 2977 | !CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2978 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2979 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2980 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2981 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2982 | OPENSSL_memset(padding_bytes, 0, padding_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2983 | } |
| 2984 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2985 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2986 | // The PSK extension must be last, including after the padding. |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 2987 | if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 2988 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2989 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 2990 | } |
| 2991 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2992 | // Discard empty extensions blocks. |
David Benjamin | a01deee | 2015-12-08 18:56:31 -0500 | [diff] [blame] | 2993 | if (CBB_len(&extensions) == 0) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2994 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 2997 | return CBB_flush(out); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2998 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2999 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3000 | int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) { |
| 3001 | SSL *const ssl = hs->ssl; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3002 | CBB extensions; |
| 3003 | if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3004 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3005 | } |
| 3006 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3007 | for (unsigned i = 0; i < kNumExtensions; i++) { |
| 3008 | if (!(hs->extensions.received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3009 | // Don't send extensions that were not received. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3010 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3011 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3012 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3013 | if (!kExtensions[i].add_serverhello(hs, &extensions)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3014 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3015 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3016 | goto err; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3017 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3018 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3019 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 3020 | if (!custom_ext_add_serverhello(hs, &extensions)) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 3021 | goto err; |
| 3022 | } |
| 3023 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3024 | // Discard empty extensions blocks before TLS 1.3. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3025 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION && |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3026 | CBB_len(&extensions) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3027 | CBB_discard_child(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3030 | return CBB_flush(out); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3031 | |
| 3032 | err: |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3033 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 3034 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3035 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3036 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 3037 | static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3038 | const SSL_CLIENT_HELLO *client_hello, |
| 3039 | int *out_alert) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3040 | SSL *const ssl = hs->ssl; |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3041 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3042 | if (kExtensions[i].init != NULL) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3043 | kExtensions[i].init(hs); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3047 | hs->extensions.received = 0; |
| 3048 | hs->custom_extensions.received = 0; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3049 | CBS extensions; |
| 3050 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 3051 | while (CBS_len(&extensions) != 0) { |
| 3052 | uint16_t type; |
| 3053 | CBS extension; |
| 3054 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3055 | // Decode the next extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3056 | if (!CBS_get_u16(&extensions, &type) || |
| 3057 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3058 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3059 | return 0; |
| 3060 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3061 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3062 | // RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 3063 | // ambiguous. Ignore all but the renegotiation_info extension. |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3064 | if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
| 3065 | continue; |
| 3066 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3067 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3068 | unsigned ext_index; |
| 3069 | const struct tls_extension *const ext = |
| 3070 | tls_extension_find(&ext_index, type); |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3071 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3072 | if (ext == NULL) { |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 3073 | if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3074 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3075 | return 0; |
| 3076 | } |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3077 | continue; |
| 3078 | } |
| 3079 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3080 | hs->extensions.received |= (1u << ext_index); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3081 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3082 | if (!ext->parse_clienthello(hs, &alert, &extension)) { |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3083 | *out_alert = alert; |
| 3084 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3085 | ERR_add_error_dataf("extension %u", (unsigned)type); |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 3086 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3087 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3088 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 3089 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3090 | for (size_t i = 0; i < kNumExtensions; i++) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3091 | if (hs->extensions.received & (1u << i)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3092 | continue; |
| 3093 | } |
| 3094 | |
| 3095 | CBS *contents = NULL, fake_contents; |
| 3096 | static const uint8_t kFakeRenegotiateExtension[] = {0}; |
| 3097 | if (kExtensions[i].value == TLSEXT_TYPE_renegotiate && |
| 3098 | ssl_client_cipher_list_contains_cipher(client_hello, |
| 3099 | SSL3_CK_SCSV & 0xffff)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3100 | // The renegotiation SCSV was received so pretend that we received a |
| 3101 | // renegotiation extension. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3102 | CBS_init(&fake_contents, kFakeRenegotiateExtension, |
| 3103 | sizeof(kFakeRenegotiateExtension)); |
| 3104 | contents = &fake_contents; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3105 | hs->extensions.received |= (1u << i); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3106 | } |
| 3107 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3108 | // Extension wasn't observed so call the callback with a NULL |
| 3109 | // parameter. |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3110 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3111 | if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) { |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3112 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3113 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3114 | *out_alert = alert; |
| 3115 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3116 | } |
| 3117 | } |
| 3118 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3119 | return 1; |
| 3120 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3121 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 3122 | int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs, |
| 3123 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3124 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 3125 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3126 | if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3127 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3128 | return 0; |
| 3129 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3130 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3131 | if (ssl_check_clienthello_tlsext(hs) <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 3132 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3133 | return 0; |
| 3134 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3135 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3136 | return 1; |
| 3137 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3138 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3139 | static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs, |
| 3140 | int *out_alert) { |
| 3141 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3142 | // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3143 | if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3144 | return 1; |
| 3145 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3146 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3147 | // Decode the extensions block and check it is valid. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3148 | CBS extensions; |
| 3149 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 3150 | !tls1_check_duplicate_extensions(&extensions)) { |
| 3151 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3152 | return 0; |
| 3153 | } |
| 3154 | |
| 3155 | uint32_t received = 0; |
| 3156 | while (CBS_len(&extensions) != 0) { |
| 3157 | uint16_t type; |
| 3158 | CBS extension; |
| 3159 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3160 | // Decode the next extension. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3161 | if (!CBS_get_u16(&extensions, &type) || |
| 3162 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3163 | *out_alert = SSL_AD_DECODE_ERROR; |
| 3164 | return 0; |
| 3165 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3166 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3167 | unsigned ext_index; |
| 3168 | const struct tls_extension *const ext = |
| 3169 | tls_extension_find(&ext_index, type); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3170 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3171 | if (ext == NULL) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3172 | hs->received_custom_extension = true; |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 3173 | if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3174 | return 0; |
| 3175 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3176 | continue; |
| 3177 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3178 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3179 | static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8, |
| 3180 | "too many bits"); |
David Benjamin | 5db7c9b | 2017-01-24 16:17:03 -0500 | [diff] [blame] | 3181 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3182 | if (!(hs->extensions.sent & (1u << ext_index)) && |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 3183 | type != TLSEXT_TYPE_renegotiate) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3184 | // If the extension was never sent then it is illegal, except for the |
| 3185 | // renegotiation extension which, in SSL 3.0, is signaled via SCSV. |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3186 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 3187 | ERR_add_error_dataf("extension :%u", (unsigned)type); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 3188 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3189 | return 0; |
| 3190 | } |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3191 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3192 | received |= (1u << ext_index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 3193 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3194 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3195 | if (!ext->parse_serverhello(hs, &alert, &extension)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3196 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3197 | ERR_add_error_dataf("extension %u", (unsigned)type); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 3198 | *out_alert = alert; |
| 3199 | return 0; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3200 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3201 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3202 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 3203 | for (size_t i = 0; i < kNumExtensions; i++) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3204 | if (!(received & (1u << i))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3205 | // Extension wasn't observed so call the callback with a NULL |
| 3206 | // parameter. |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3207 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3208 | if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) { |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 3209 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 3210 | ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 3211 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3212 | return 0; |
| 3213 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3214 | } |
| 3215 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3216 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3217 | return 1; |
| 3218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3219 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3220 | static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) { |
| 3221 | SSL *const ssl = hs->ssl; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 3222 | |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 3223 | if (ssl->s3->token_binding_negotiated && |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 3224 | !(SSL_get_secure_renegotiation_support(ssl) && |
| 3225 | SSL_get_extms_support(ssl))) { |
| 3226 | OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI); |
| 3227 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 3228 | return -1; |
| 3229 | } |
| 3230 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3231 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 3232 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 3233 | |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 3234 | if (ssl->ctx->tlsext_servername_callback != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 3235 | ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
David Benjamin | 78f8aab | 2016-03-10 16:33:58 -0500 | [diff] [blame] | 3236 | ssl->ctx->tlsext_servername_arg); |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 3237 | } else if (ssl->session_ctx->tlsext_servername_callback != 0) { |
| 3238 | ret = ssl->session_ctx->tlsext_servername_callback( |
| 3239 | ssl, &al, ssl->session_ctx->tlsext_servername_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3240 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3241 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3242 | switch (ret) { |
| 3243 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3244 | ssl_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3245 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3246 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3247 | case SSL_TLSEXT_ERR_NOACK: |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3248 | hs->should_ack_sni = false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3249 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3250 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3251 | default: |
| 3252 | return 1; |
| 3253 | } |
| 3254 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 3255 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3256 | int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) { |
| 3257 | SSL *const ssl = hs->ssl; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 3258 | int alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 3259 | if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3260 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3261 | return 0; |
| 3262 | } |
| 3263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3264 | return 1; |
| 3265 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3266 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3267 | static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx( |
| 3268 | uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx, |
| 3269 | HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) { |
| 3270 | size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3271 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3272 | // Check the MAC at the end of the ticket. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3273 | uint8_t mac[EVP_MAX_MD_SIZE]; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3274 | size_t mac_len = HMAC_size(hmac_ctx); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3275 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3276 | // The ticket must be large enough for key name, IV, data, and MAC. |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3277 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3278 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3279 | HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len); |
| 3280 | HMAC_Final(hmac_ctx, mac, NULL); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3281 | int mac_ok = |
| 3282 | CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0; |
| 3283 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3284 | mac_ok = 1; |
| 3285 | #endif |
| 3286 | if (!mac_ok) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3287 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3288 | } |
| 3289 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3290 | // Decrypt the session data. |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3291 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 3292 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 3293 | mac_len; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3294 | UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len)); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3295 | if (!plaintext) { |
| 3296 | return ssl_ticket_aead_error; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3297 | } |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3298 | size_t plaintext_len; |
| 3299 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3300 | OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3301 | plaintext_len = ciphertext_len; |
| 3302 | #else |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3303 | if (ciphertext_len >= INT_MAX) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3304 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3305 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3306 | int len1, len2; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3307 | if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3308 | (int)ciphertext_len) || |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3309 | !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3310 | ERR_clear_error(); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3311 | return ssl_ticket_aead_ignore_ticket; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3312 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3313 | plaintext_len = (size_t)(len1) + len2; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 3314 | #endif |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3315 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3316 | *out = plaintext.release(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3317 | *out_len = plaintext_len; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3318 | return ssl_ticket_aead_success; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3319 | } |
| 3320 | |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3321 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3322 | SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket, |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3323 | const uint8_t *ticket, size_t ticket_len) { |
| 3324 | assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3325 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3326 | ScopedHMAC_CTX hmac_ctx; |
| 3327 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 3328 | int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb( |
| 3329 | ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(), |
| 3330 | hmac_ctx.get(), 0 /* decrypt */); |
| 3331 | if (cb_ret < 0) { |
| 3332 | return ssl_ticket_aead_error; |
| 3333 | } else if (cb_ret == 0) { |
| 3334 | return ssl_ticket_aead_ignore_ticket; |
| 3335 | } else if (cb_ret == 2) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3336 | *out_renew_ticket = true; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3337 | } else { |
| 3338 | assert(cb_ret == 1); |
| 3339 | } |
| 3340 | return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(), |
| 3341 | hmac_ctx.get(), ticket, ticket_len); |
| 3342 | } |
| 3343 | |
| 3344 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys( |
| 3345 | SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket, |
| 3346 | size_t ticket_len) { |
| 3347 | assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH); |
| 3348 | SSL_CTX *ctx = ssl->session_ctx; |
| 3349 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3350 | // Rotate the ticket key if necessary. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3351 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 3352 | return ssl_ticket_aead_error; |
| 3353 | } |
| 3354 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3355 | // Pick the matching ticket key and decrypt. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3356 | ScopedEVP_CIPHER_CTX cipher_ctx; |
| 3357 | ScopedHMAC_CTX hmac_ctx; |
| 3358 | { |
| 3359 | MutexReadLock lock(&ctx->lock); |
| 3360 | const tlsext_ticket_key *key; |
| 3361 | if (ctx->tlsext_ticket_key_current && |
| 3362 | !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket, |
| 3363 | SSL_TICKET_KEY_NAME_LEN)) { |
| 3364 | key = ctx->tlsext_ticket_key_current; |
| 3365 | } else if (ctx->tlsext_ticket_key_prev && |
| 3366 | !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket, |
| 3367 | SSL_TICKET_KEY_NAME_LEN)) { |
| 3368 | key = ctx->tlsext_ticket_key_prev; |
| 3369 | } else { |
| 3370 | return ssl_ticket_aead_ignore_ticket; |
| 3371 | } |
| 3372 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 3373 | if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key), |
| 3374 | tlsext_tick_md(), NULL) || |
| 3375 | !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL, |
| 3376 | key->aes_key, iv)) { |
| 3377 | return ssl_ticket_aead_error; |
| 3378 | } |
| 3379 | } |
| 3380 | return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(), |
| 3381 | hmac_ctx.get(), ticket, ticket_len); |
| 3382 | } |
| 3383 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3384 | static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3385 | SSL *ssl, uint8_t **out, size_t *out_len, bool *out_renew_ticket, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3386 | const uint8_t *ticket, size_t ticket_len) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3387 | uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3388 | if (plaintext == NULL) { |
| 3389 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 3390 | return ssl_ticket_aead_error; |
| 3391 | } |
| 3392 | |
| 3393 | size_t plaintext_len; |
| 3394 | const enum ssl_ticket_aead_result_t result = |
| 3395 | ssl->session_ctx->ticket_aead_method->open( |
| 3396 | ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len); |
| 3397 | |
| 3398 | if (result == ssl_ticket_aead_success) { |
| 3399 | *out = plaintext; |
| 3400 | plaintext = NULL; |
| 3401 | *out_len = plaintext_len; |
| 3402 | } |
| 3403 | |
| 3404 | OPENSSL_free(plaintext); |
| 3405 | return result; |
| 3406 | } |
| 3407 | |
| 3408 | enum ssl_ticket_aead_result_t ssl_process_ticket( |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3409 | SSL *ssl, UniquePtr<SSL_SESSION> *out_session, bool *out_renew_ticket, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3410 | const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id, |
| 3411 | size_t session_id_len) { |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 3412 | *out_renew_ticket = false; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3413 | out_session->reset(); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3414 | |
| 3415 | if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) || |
| 3416 | session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3417 | return ssl_ticket_aead_ignore_ticket; |
| 3418 | } |
| 3419 | |
| 3420 | uint8_t *plaintext = NULL; |
| 3421 | size_t plaintext_len; |
| 3422 | enum ssl_ticket_aead_result_t result; |
| 3423 | if (ssl->session_ctx->ticket_aead_method != NULL) { |
| 3424 | result = ssl_decrypt_ticket_with_method( |
| 3425 | ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len); |
| 3426 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3427 | // Ensure there is room for the key name and the largest IV |
| 3428 | // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, |
| 3429 | // but the maximum IV length should be well under the minimum size for the |
| 3430 | // session material and HMAC. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 3431 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 3432 | return ssl_ticket_aead_ignore_ticket; |
| 3433 | } |
| 3434 | if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) { |
| 3435 | result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len, |
| 3436 | out_renew_ticket, ticket, ticket_len); |
| 3437 | } else { |
| 3438 | result = ssl_decrypt_ticket_with_ticket_keys( |
| 3439 | ssl, &plaintext, &plaintext_len, ticket, ticket_len); |
| 3440 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | if (result != ssl_ticket_aead_success) { |
| 3444 | return result; |
| 3445 | } |
| 3446 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3447 | // Decode the session. |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3448 | UniquePtr<SSL_SESSION> session( |
| 3449 | SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx)); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3450 | OPENSSL_free(plaintext); |
| 3451 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3452 | if (!session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3453 | ERR_clear_error(); // Don't leave an error on the queue. |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3454 | return ssl_ticket_aead_ignore_ticket; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3455 | } |
| 3456 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3457 | // Copy the client's session ID into the new session, to denote the ticket has |
| 3458 | // been accepted. |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 3459 | OPENSSL_memcpy(session->session_id, session_id, session_id_len); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 3460 | session->session_id_length = session_id_len; |
| 3461 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 3462 | *out_session = std::move(session); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 3463 | return ssl_ticket_aead_success; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3465 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3466 | bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3467 | // Extension ignored for inappropriate versions |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3468 | if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) { |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3469 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3470 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 3471 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3472 | return parse_u16_array(in_sigalgs, &hs->peer_sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3473 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3474 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3475 | bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) { |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3476 | switch (EVP_PKEY_id(pkey)) { |
| 3477 | case EVP_PKEY_RSA: |
| 3478 | *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3479 | return true; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3480 | case EVP_PKEY_EC: |
| 3481 | *out = SSL_SIGN_ECDSA_SHA1; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3482 | return true; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3483 | default: |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3484 | return false; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3485 | } |
| 3486 | } |
| 3487 | |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3488 | bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 3489 | SSL *const ssl = hs->ssl; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 3490 | CERT *cert = ssl->cert; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 3491 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3492 | // Before TLS 1.2, the signature algorithm isn't negotiated as part of the |
| 3493 | // handshake. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3494 | if (ssl_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 3495 | if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) { |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 3496 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3497 | return false; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3498 | } |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3499 | return true; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3500 | } |
| 3501 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3502 | Span<const uint16_t> sigalgs = kSignSignatureAlgorithms; |
| 3503 | if (cert->sigalgs != nullptr) { |
| 3504 | sigalgs = MakeConstSpan(cert->sigalgs, cert->num_sigalgs); |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 3505 | } |
| 3506 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3507 | Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3508 | if (peer_sigalgs.empty() && ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3509 | // If the client didn't specify any signature_algorithms extension then |
| 3510 | // we can assume that it supports SHA1. See |
| 3511 | // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3512 | static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1, |
| 3513 | SSL_SIGN_ECDSA_SHA1}; |
| 3514 | peer_sigalgs = kDefaultPeerAlgorithms; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3517 | for (uint16_t sigalg : sigalgs) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3518 | // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be |
| 3519 | // negotiated. |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3520 | if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 || |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3521 | !ssl_private_key_supports_signature_algorithm(hs, sigalg)) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3522 | continue; |
| 3523 | } |
| 3524 | |
David Benjamin | b1cf48e | 2017-09-21 11:37:46 -0400 | [diff] [blame] | 3525 | for (uint16_t peer_sigalg : peer_sigalgs) { |
| 3526 | if (sigalg == peer_sigalg) { |
David Benjamin | 1fb125c | 2016-07-08 18:52:12 -0700 | [diff] [blame] | 3527 | *out = sigalg; |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3528 | return true; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 3529 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3530 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3531 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3532 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 3533 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
David Benjamin | 610cdbb | 2018-01-22 19:08:38 -0500 | [diff] [blame] | 3534 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3535 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3536 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3537 | int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3538 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3539 | // A Channel ID handshake message is structured to contain multiple |
| 3540 | // extensions, but the only one that can be present is Channel ID. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 3541 | uint16_t extension_type; |
| 3542 | CBS channel_id = msg.body, extension; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3543 | if (!CBS_get_u16(&channel_id, &extension_type) || |
| 3544 | !CBS_get_u16_length_prefixed(&channel_id, &extension) || |
| 3545 | CBS_len(&channel_id) != 0 || |
| 3546 | extension_type != TLSEXT_TYPE_channel_id || |
| 3547 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 3548 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3549 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3550 | return 0; |
| 3551 | } |
| 3552 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3553 | UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3554 | if (!p256) { |
| 3555 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
| 3556 | return 0; |
| 3557 | } |
| 3558 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3559 | UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new()); |
| 3560 | UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3561 | if (!sig || !x || !y) { |
| 3562 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | const uint8_t *p = CBS_data(&extension); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3566 | if (BN_bin2bn(p + 0, 32, x.get()) == NULL || |
| 3567 | BN_bin2bn(p + 32, 32, y.get()) == NULL || |
| 3568 | BN_bin2bn(p + 64, 32, sig->r) == NULL || |
| 3569 | BN_bin2bn(p + 96, 32, sig->s) == NULL) { |
| 3570 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3573 | UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 3574 | UniquePtr<EC_POINT> point(EC_POINT_new(p256.get())); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3575 | if (!key || !point || |
| 3576 | !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(), |
| 3577 | y.get(), nullptr) || |
| 3578 | !EC_KEY_set_group(key.get(), p256.get()) || |
| 3579 | !EC_KEY_set_public_key(key.get(), point.get())) { |
| 3580 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3584 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3585 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3586 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3589 | int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get()); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3590 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 3591 | sig_ok = 1; |
David Benjamin | d90b803 | 2017-12-18 16:47:51 -0500 | [diff] [blame] | 3592 | ERR_clear_error(); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3593 | #endif |
| 3594 | if (!sig_ok) { |
| 3595 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3596 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 3597 | ssl->s3->tlsext_channel_id_valid = false; |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3598 | return 0; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3599 | } |
| 3600 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 3601 | OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64); |
David Benjamin | 81678aa | 2017-07-12 22:43:42 -0400 | [diff] [blame] | 3602 | return 1; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3603 | } |
| 3604 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3605 | bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3606 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3607 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 3608 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3609 | if (!tls1_channel_id_hash(hs, digest, &digest_len)) { |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3610 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3614 | if (ec_key == nullptr) { |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3615 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3616 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3619 | UniquePtr<BIGNUM> x(BN_new()), y(BN_new()); |
| 3620 | if (!x || !y || |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3621 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 3622 | EC_KEY_get0_public_key(ec_key), |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3623 | x.get(), y.get(), nullptr)) { |
| 3624 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3625 | } |
| 3626 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3627 | UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key)); |
| 3628 | if (!sig) { |
| 3629 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | CBB child; |
| 3633 | if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) || |
| 3634 | !CBB_add_u16_length_prefixed(cbb, &child) || |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3635 | !BN_bn2cbb_padded(&child, 32, x.get()) || |
| 3636 | !BN_bn2cbb_padded(&child, 32, y.get()) || |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3637 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 3638 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
| 3639 | !CBB_flush(cbb)) { |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3640 | return false; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3641 | } |
| 3642 | |
David Benjamin | f1db1a3 | 2017-10-27 01:12:34 -0400 | [diff] [blame] | 3643 | return true; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3644 | } |
| 3645 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3646 | int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) { |
| 3647 | SSL *const ssl = hs->ssl; |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 3648 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 3649 | Array<uint8_t> msg; |
| 3650 | if (!tls13_get_cert_verify_signature_input(hs, &msg, |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3651 | ssl_cert_verify_channel_id)) { |
| 3652 | return 0; |
| 3653 | } |
David Benjamin | 75a1f23 | 2017-10-11 17:19:19 -0400 | [diff] [blame] | 3654 | SHA256(msg.data(), msg.size(), out); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3655 | *out_len = SHA256_DIGEST_LENGTH; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3656 | return 1; |
| 3657 | } |
| 3658 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3659 | SHA256_CTX ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3660 | |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3661 | SHA256_Init(&ctx); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3662 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3663 | SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3664 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3665 | if (ssl->session != NULL) { |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3666 | static const char kResumptionMagic[] = "Resumption"; |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3667 | SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3668 | if (ssl->session->original_handshake_hash_len == 0) { |
| 3669 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3670 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3671 | } |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3672 | SHA256_Update(&ctx, ssl->session->original_handshake_hash, |
| 3673 | ssl->session->original_handshake_hash_len); |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3674 | } |
| 3675 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3676 | uint8_t hs_hash[EVP_MAX_MD_SIZE]; |
| 3677 | size_t hs_hash_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 3678 | if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) { |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3679 | return 0; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 3680 | } |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3681 | SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len); |
Nick Harper | 9559401 | 2016-10-20 14:07:13 -0700 | [diff] [blame] | 3682 | SHA256_Final(out, &ctx); |
| 3683 | *out_len = SHA256_DIGEST_LENGTH; |
| 3684 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3685 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3686 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3687 | // tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 3688 | // hashes in |hs->new_session| so that Channel ID resumptions can sign that |
| 3689 | // data. |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3690 | int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) { |
| 3691 | SSL *const ssl = hs->ssl; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3692 | // This function should never be called for a resumed session because the |
| 3693 | // handshake hashes that we wish to record are for the original, full |
| 3694 | // handshake. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 3695 | if (ssl->session != NULL) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 3696 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3697 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3698 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3699 | static_assert( |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 3700 | sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE, |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3701 | "original_handshake_hash is too small"); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 3702 | |
| 3703 | size_t digest_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 3704 | if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash, |
| 3705 | &digest_len)) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 3706 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3707 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3708 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 3709 | static_assert(EVP_MAX_MD_SIZE <= 0xff, |
| 3710 | "EVP_MAX_MD_SIZE does not fit in uint8_t"); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 3711 | hs->new_session->original_handshake_hash_len = (uint8_t)digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3712 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3713 | return 1; |
| 3714 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3715 | |
| 3716 | int ssl_do_channel_id_callback(SSL *ssl) { |
| 3717 | if (ssl->tlsext_channel_id_private != NULL || |
| 3718 | ssl->ctx->channel_id_cb == NULL) { |
| 3719 | return 1; |
| 3720 | } |
| 3721 | |
| 3722 | EVP_PKEY *key = NULL; |
| 3723 | ssl->ctx->channel_id_cb(ssl, &key); |
| 3724 | if (key == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3725 | // The caller should try again later. |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 3726 | return 1; |
| 3727 | } |
| 3728 | |
| 3729 | int ret = SSL_set1_tls_channel_id(ssl, key); |
| 3730 | EVP_PKEY_free(key); |
| 3731 | return ret; |
| 3732 | } |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3733 | |
| 3734 | int ssl_is_sct_list_valid(const CBS *contents) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 3735 | // Shallow parse the SCT list for sanity. By the RFC |
| 3736 | // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any |
| 3737 | // of the SCTs may be empty. |
Adam Langley | cfa08c3 | 2016-11-17 13:21:27 -0800 | [diff] [blame] | 3738 | CBS copy = *contents; |
| 3739 | CBS sct_list; |
| 3740 | if (!CBS_get_u16_length_prefixed(©, &sct_list) || |
| 3741 | CBS_len(©) != 0 || |
| 3742 | CBS_len(&sct_list) == 0) { |
| 3743 | return 0; |
| 3744 | } |
| 3745 | |
| 3746 | while (CBS_len(&sct_list) > 0) { |
| 3747 | CBS sct; |
| 3748 | if (!CBS_get_u16_length_prefixed(&sct_list, &sct) || |
| 3749 | CBS_len(&sct) == 0) { |
| 3750 | return 0; |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | return 1; |
| 3755 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 3756 | |
| 3757 | } // namespace bssl |
| 3758 | |
| 3759 | using namespace bssl; |
| 3760 | |
| 3761 | int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello, |
| 3762 | uint16_t extension_type, |
| 3763 | const uint8_t **out_data, |
| 3764 | size_t *out_len) { |
| 3765 | CBS cbs; |
| 3766 | if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) { |
| 3767 | return 0; |
| 3768 | } |
| 3769 | |
| 3770 | *out_data = CBS_data(&cbs); |
| 3771 | *out_len = CBS_len(&cbs); |
| 3772 | return 1; |
| 3773 | } |
| 3774 | |
| 3775 | void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) { |
| 3776 | ctx->ed25519_enabled = !!enabled; |
| 3777 | } |
| 3778 | |
| 3779 | int SSL_extension_supported(unsigned extension_value) { |
| 3780 | uint32_t index; |
| 3781 | return extension_value == TLSEXT_TYPE_padding || |
| 3782 | tls_extension_find(&index, extension_value) != NULL; |
| 3783 | } |