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