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