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