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 | a27a6a4 | 2017-09-05 08:39:28 -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 | a27a6a4 | 2017-09-05 08:39:28 -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 | a27a6a4 | 2017-09-05 08:39:28 -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 | a27a6a4 | 2017-09-05 08:39:28 -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 | a27a6a4 | 2017-09-05 08:39:28 -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 | a27a6a4 | 2017-09-05 08:39:28 -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 | a27a6a4 | 2017-09-05 08:39:28 -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 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 113 | * Portions of the attached software ("Contribution") are developed by |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 149 | #include <openssl/ssl.h> |
| 150 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | #include <assert.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 152 | #include <string.h> |
| 153 | |
| 154 | #include <openssl/bn.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 155 | #include <openssl/bytestring.h> |
| 156 | #include <openssl/cipher.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | #include <openssl/ec.h> |
| 158 | #include <openssl/ecdsa.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 159 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 160 | #include <openssl/evp.h> |
| 161 | #include <openssl/hmac.h> |
| 162 | #include <openssl/md5.h> |
| 163 | #include <openssl/mem.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 164 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 165 | #include <openssl/rand.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 166 | #include <openssl/x509.h> |
| 167 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 168 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | #include "../crypto/internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 170 | |
| 171 | |
Robert Sloan | 726e9d1 | 2018-09-11 11:45:04 -0700 | [diff] [blame] | 172 | BSSL_NAMESPACE_BEGIN |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 173 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 174 | bool ssl_client_cipher_list_contains_cipher( |
| 175 | const SSL_CLIENT_HELLO *client_hello, uint16_t id) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 176 | CBS cipher_suites; |
| 177 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 178 | client_hello->cipher_suites_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 179 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 180 | while (CBS_len(&cipher_suites) > 0) { |
| 181 | uint16_t got_id; |
| 182 | if (!CBS_get_u16(&cipher_suites, &got_id)) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 183 | return false; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (got_id == id) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 187 | return true; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 188 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 191 | return false; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 194 | static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 195 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 196 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 197 | assert(!ssl->s3->have_version); |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 198 | CBS supported_versions, versions; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 199 | if (ssl_client_hello_get_extension(client_hello, &supported_versions, |
| 200 | TLSEXT_TYPE_supported_versions)) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 201 | if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || |
| 202 | CBS_len(&supported_versions) != 0 || |
| 203 | CBS_len(&versions) == 0) { |
| 204 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 205 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 206 | return false; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 207 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 208 | } else { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 209 | // Convert the ClientHello version to an equivalent supported_versions |
| 210 | // extension. |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 211 | static const uint8_t kTLSVersions[] = { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 212 | 0x03, 0x03, // TLS 1.2 |
| 213 | 0x03, 0x02, // TLS 1.1 |
| 214 | 0x03, 0x01, // TLS 1 |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static const uint8_t kDTLSVersions[] = { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 218 | 0xfe, 0xfd, // DTLS 1.2 |
| 219 | 0xfe, 0xff, // DTLS 1.0 |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | size_t versions_len = 0; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 223 | if (SSL_is_dtls(ssl)) { |
| 224 | if (client_hello->version <= DTLS1_2_VERSION) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 225 | versions_len = 4; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 226 | } else if (client_hello->version <= DTLS1_VERSION) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 227 | versions_len = 2; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 228 | } |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 229 | CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len, |
| 230 | versions_len); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 231 | } else { |
| 232 | if (client_hello->version >= TLS1_2_VERSION) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 233 | versions_len = 6; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 234 | } else if (client_hello->version >= TLS1_1_VERSION) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 235 | versions_len = 4; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 236 | } else if (client_hello->version >= TLS1_VERSION) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 237 | versions_len = 2; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 238 | } |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 239 | CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len, |
| 240 | versions_len); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 241 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 244 | if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 245 | return false; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 248 | // At this point, the connection's version is known and |ssl->version| is |
| 249 | // fixed. Begin enforcing the record-layer version. |
| 250 | ssl->s3->have_version = true; |
Robert Sloan | db4251a | 2017-09-18 09:38:15 -0700 | [diff] [blame] | 251 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 252 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 253 | // Handle FALLBACK_SCSV. |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 254 | if (ssl_client_cipher_list_contains_cipher(client_hello, |
| 255 | SSL3_CK_FALLBACK_SCSV & 0xffff) && |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 256 | ssl_protocol_version(ssl) < hs->max_version) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 257 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
| 258 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 259 | return false; |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 260 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 261 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 262 | return true; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 265 | static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list( |
| 266 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 267 | CBS cipher_suites; |
| 268 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 269 | client_hello->cipher_suites_len); |
| 270 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 271 | UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null()); |
| 272 | if (!sk) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 273 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 274 | return nullptr; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | while (CBS_len(&cipher_suites) > 0) { |
| 278 | uint16_t cipher_suite; |
| 279 | |
| 280 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 281 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 282 | return nullptr; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 286 | if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 287 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 288 | return nullptr; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | return sk; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 293 | } |
| 294 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 295 | // ssl_get_compatible_server_ciphers determines the key exchange and |
| 296 | // authentication cipher suite masks compatible with the server configuration |
| 297 | // and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key |
| 298 | // exchange mask and |*out_mask_a| to the authentication mask. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 299 | static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs, |
| 300 | uint32_t *out_mask_k, |
| 301 | uint32_t *out_mask_a) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 302 | uint32_t mask_k = 0; |
| 303 | uint32_t mask_a = 0; |
| 304 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 305 | if (ssl_has_certificate(hs)) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 306 | mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get()); |
| 307 | if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 308 | mask_k |= SSL_kRSA; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 312 | // Check for a shared group to consider ECDHE ciphers. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 313 | uint16_t unused; |
| 314 | if (tls1_get_shared_group(hs, &unused)) { |
| 315 | mask_k |= SSL_kECDHE; |
| 316 | } |
| 317 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 318 | // PSK requires a server callback. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 319 | if (hs->config->psk_server_callback != NULL) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 320 | mask_k |= SSL_kPSK; |
| 321 | mask_a |= SSL_aPSK; |
| 322 | } |
| 323 | |
| 324 | *out_mask_k = mask_k; |
| 325 | *out_mask_a = mask_a; |
| 326 | } |
| 327 | |
Tobias Thierer | 43be7d2 | 2020-03-02 19:23:34 +0000 | [diff] [blame] | 328 | static const SSL_CIPHER *choose_cipher( |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 329 | SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello, |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 330 | const SSLCipherPreferenceList *server_pref) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 331 | SSL *const ssl = hs->ssl; |
Robert Sloan | 15c0b35 | 2018-04-16 08:36:46 -0700 | [diff] [blame] | 332 | const STACK_OF(SSL_CIPHER) *prio, *allow; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 333 | // in_group_flags will either be NULL, or will point to an array of bytes |
| 334 | // which indicate equal-preference groups in the |prio| stack. See the |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 335 | // comment about |in_group_flags| in the |SSLCipherPreferenceList| |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 336 | // struct. |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 337 | const bool *in_group_flags; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 338 | // group_min contains the minimal index so far found in a group, or -1 if no |
| 339 | // such value exists yet. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 340 | int group_min = -1; |
| 341 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 342 | UniquePtr<STACK_OF(SSL_CIPHER)> client_pref = |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 343 | ssl_parse_client_cipher_list(client_hello); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 344 | if (!client_pref) { |
| 345 | return nullptr; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 349 | prio = server_pref->ciphers.get(); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 350 | in_group_flags = server_pref->in_group_flags; |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 351 | allow = client_pref.get(); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 352 | } else { |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 353 | prio = client_pref.get(); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 354 | in_group_flags = NULL; |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 355 | allow = server_pref->ciphers.get(); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 358 | uint32_t mask_k, mask_a; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 359 | ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a); |
| 360 | |
| 361 | for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) { |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 362 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 363 | |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 364 | size_t cipher_index; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 365 | if (// Check if the cipher is supported for the current version. |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 366 | SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) && |
| 367 | ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 368 | // Check the cipher is supported for the server configuration. |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 369 | (c->algorithm_mkey & mask_k) && |
| 370 | (c->algorithm_auth & mask_a) && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 371 | // Check the cipher is in the |allow| list. |
Robert Sloan | 6f79a50 | 2017-04-03 09:16:40 -0700 | [diff] [blame] | 372 | sk_SSL_CIPHER_find(allow, &cipher_index, c)) { |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 373 | if (in_group_flags != NULL && in_group_flags[i]) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 374 | // This element of |prio| is in a group. Update the minimum index found |
| 375 | // so far and continue looking. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 376 | if (group_min == -1 || (size_t)group_min > cipher_index) { |
| 377 | group_min = cipher_index; |
| 378 | } |
| 379 | } else { |
| 380 | if (group_min != -1 && (size_t)group_min < cipher_index) { |
| 381 | cipher_index = group_min; |
| 382 | } |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 383 | return sk_SSL_CIPHER_value(allow, cipher_index); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 387 | if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 388 | // We are about to leave a group, but we found a match in it, so that's |
| 389 | // our answer. |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 390 | return sk_SSL_CIPHER_value(allow, group_min); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 394 | return nullptr; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 397 | static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) { |
| 398 | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 399 | hs->state = state12_read_client_hello; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 400 | return ssl_hs_ok; |
| 401 | } |
| 402 | |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 403 | // is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 404 | // from a JDK 11 client with both TLS 1.3 and a prior version enabled. |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 405 | static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) { |
| 406 | // JDK 11 ClientHellos contain a number of unusual properties which should |
| 407 | // limit false positives. |
| 408 | |
| 409 | // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern |
| 410 | // clients implement ChaCha20-Poly1305. |
| 411 | if (ssl_client_cipher_list_contains_cipher( |
| 412 | client_hello, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | // JDK 11 always sends extensions in a particular order. |
| 417 | constexpr uint16_t kMaxFragmentLength = 0x0001; |
| 418 | constexpr uint16_t kStatusRequestV2 = 0x0011; |
| 419 | static CONSTEXPR_ARRAY struct { |
| 420 | uint16_t id; |
| 421 | bool required; |
| 422 | } kJavaExtensions[] = { |
| 423 | {TLSEXT_TYPE_server_name, false}, |
| 424 | {kMaxFragmentLength, false}, |
| 425 | {TLSEXT_TYPE_status_request, false}, |
| 426 | {TLSEXT_TYPE_supported_groups, true}, |
| 427 | {TLSEXT_TYPE_ec_point_formats, false}, |
| 428 | {TLSEXT_TYPE_signature_algorithms, true}, |
| 429 | // Java always sends signature_algorithms_cert. |
| 430 | {TLSEXT_TYPE_signature_algorithms_cert, true}, |
| 431 | {TLSEXT_TYPE_application_layer_protocol_negotiation, false}, |
| 432 | {kStatusRequestV2, false}, |
| 433 | {TLSEXT_TYPE_extended_master_secret, false}, |
| 434 | {TLSEXT_TYPE_supported_versions, true}, |
| 435 | {TLSEXT_TYPE_cookie, false}, |
| 436 | {TLSEXT_TYPE_psk_key_exchange_modes, true}, |
| 437 | {TLSEXT_TYPE_key_share, true}, |
| 438 | {TLSEXT_TYPE_renegotiate, false}, |
| 439 | {TLSEXT_TYPE_pre_shared_key, false}, |
| 440 | }; |
| 441 | Span<const uint8_t> sigalgs, sigalgs_cert; |
| 442 | bool has_status_request = false, has_status_request_v2 = false; |
| 443 | CBS extensions, supported_groups; |
| 444 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 445 | for (const auto &java_extension : kJavaExtensions) { |
| 446 | CBS copy = extensions; |
| 447 | uint16_t id; |
| 448 | if (CBS_get_u16(©, &id) && id == java_extension.id) { |
| 449 | // The next extension is the one we expected. |
| 450 | extensions = copy; |
| 451 | CBS body; |
| 452 | if (!CBS_get_u16_length_prefixed(&extensions, &body)) { |
| 453 | return false; |
| 454 | } |
| 455 | switch (id) { |
| 456 | case TLSEXT_TYPE_status_request: |
| 457 | has_status_request = true; |
| 458 | break; |
| 459 | case kStatusRequestV2: |
| 460 | has_status_request_v2 = true; |
| 461 | break; |
| 462 | case TLSEXT_TYPE_signature_algorithms: |
| 463 | sigalgs = body; |
| 464 | break; |
| 465 | case TLSEXT_TYPE_signature_algorithms_cert: |
| 466 | sigalgs_cert = body; |
| 467 | break; |
| 468 | case TLSEXT_TYPE_supported_groups: |
| 469 | supported_groups = body; |
| 470 | break; |
| 471 | } |
| 472 | } else if (java_extension.required) { |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | if (CBS_len(&extensions) != 0) { |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | // JDK 11 never advertises X25519. It is not offered by default, and |
| 481 | // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern |
| 482 | // clients implement X25519. |
| 483 | while (CBS_len(&supported_groups) > 0) { |
| 484 | uint16_t group; |
| 485 | if (!CBS_get_u16(&supported_groups, &group) || |
| 486 | group == SSL_CURVE_X25519) { |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (// JDK 11 always sends the same contents in signature_algorithms and |
| 492 | // signature_algorithms_cert. This is unusual: signature_algorithms_cert, |
| 493 | // if omitted, is treated as if it were signature_algorithms. |
| 494 | sigalgs != sigalgs_cert || |
| 495 | // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it |
| 496 | // sends status_request. This is unusual: status_request_v2 is not widely |
| 497 | // implemented. |
| 498 | has_status_request != has_status_request_v2) { |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | return true; |
| 503 | } |
| 504 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 505 | static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 506 | const SSL_CLIENT_HELLO *client_hello) { |
| 507 | SSL *const ssl = hs->ssl; |
| 508 | CBS sni; |
| 509 | if (!ssl_client_hello_get_extension(client_hello, &sni, |
| 510 | TLSEXT_TYPE_server_name)) { |
| 511 | // No SNI extension to parse. |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | CBS server_name_list, host_name; |
| 516 | uint8_t name_type; |
| 517 | if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) || |
| 518 | !CBS_get_u8(&server_name_list, &name_type) || |
| 519 | // Although the server_name extension was intended to be extensible to |
| 520 | // new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 521 | // different name types will cause an error. Further, RFC 4366 originally |
| 522 | // defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 523 | // adding new name types is no longer feasible. |
| 524 | // |
| 525 | // Act as if the extensibility does not exist to simplify parsing. |
| 526 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 527 | CBS_len(&server_name_list) != 0 || |
| 528 | CBS_len(&sni) != 0) { |
| 529 | *out_alert = SSL_AD_DECODE_ERROR; |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | if (name_type != TLSEXT_NAMETYPE_host_name || |
| 534 | CBS_len(&host_name) == 0 || |
| 535 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 536 | CBS_contains_zero_byte(&host_name)) { |
| 537 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | // Copy the hostname as a string. |
| 542 | char *raw = nullptr; |
| 543 | if (!CBS_strdup(&host_name, &raw)) { |
| 544 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 545 | return false; |
| 546 | } |
| 547 | ssl->s3->hostname.reset(raw); |
| 548 | |
| 549 | hs->should_ack_sni = true; |
| 550 | return true; |
| 551 | } |
| 552 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 553 | static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 554 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 555 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 556 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 557 | if (!ssl->method->get_message(ssl, &msg)) { |
| 558 | return ssl_hs_read_message; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 559 | } |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 560 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 561 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 562 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 563 | } |
| 564 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 565 | SSL_CLIENT_HELLO client_hello; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 566 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 567 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 568 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 569 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Tobias Thierer | 43be7d2 | 2020-03-02 19:23:34 +0000 | [diff] [blame] | 572 | // ClientHello should be the end of the flight. We check this early to cover |
| 573 | // all protocol versions. |
| 574 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 575 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 576 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 577 | return ssl_hs_error; |
| 578 | } |
| 579 | |
Robert Sloan | f63bd1f | 2019-04-16 09:26:20 -0700 | [diff] [blame] | 580 | if (hs->config->handoff) { |
| 581 | return ssl_hs_handoff; |
| 582 | } |
| 583 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 584 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 585 | if (!extract_sni(hs, &alert, &client_hello)) { |
| 586 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 587 | return ssl_hs_error; |
| 588 | } |
| 589 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 590 | // Run the early callback. |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 591 | if (ssl->ctx->select_certificate_cb != NULL) { |
| 592 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 593 | case ssl_select_cert_retry: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 594 | return ssl_hs_certificate_selection_pending; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 595 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 596 | case ssl_select_cert_error: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 597 | // Connection rejected. |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 598 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 599 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 600 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 601 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 602 | default: |
| 603 | /* fallthrough */; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 604 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 605 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 606 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 607 | // Freeze the version range after the early callback. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 608 | if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 609 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 612 | if (hs->config->jdk11_workaround && |
| 613 | is_probably_jdk11_with_tls13(&client_hello)) { |
| 614 | hs->apply_jdk11_workaround = true; |
| 615 | } |
| 616 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 617 | if (!negotiate_version(hs, &alert, &client_hello)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 618 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 619 | return ssl_hs_error; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 620 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 621 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 622 | hs->client_version = client_hello.version; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 623 | if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
| 624 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 625 | return ssl_hs_error; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 626 | } |
| 627 | OPENSSL_memcpy(ssl->s3->client_random, client_hello.random, |
| 628 | client_hello.random_len); |
| 629 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 630 | // Only null compression is supported. TLS 1.3 further requires the peer |
| 631 | // advertise no other compression. |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 632 | if (OPENSSL_memchr(client_hello.compression_methods, 0, |
| 633 | client_hello.compression_methods_len) == NULL || |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 634 | (ssl_protocol_version(ssl) >= TLS1_3_VERSION && |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 635 | client_hello.compression_methods_len != 1)) { |
| 636 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 637 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 638 | return ssl_hs_error; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 641 | // TLS extensions. |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 642 | if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) { |
| 643 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 644 | return ssl_hs_error; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Pete Bentley | 1748611 | 2021-01-20 11:51:47 +0000 | [diff] [blame^] | 647 | if (hs->ech_present && hs->ech_is_inner_present) { |
| 648 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 649 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 650 | return ssl_hs_error; |
| 651 | } |
| 652 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 653 | hs->state = state12_select_certificate; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 654 | return ssl_hs_ok; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 657 | static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 658 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 659 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 660 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 661 | if (!ssl->method->get_message(ssl, &msg)) { |
| 662 | return ssl_hs_read_message; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 665 | // Call |cert_cb| to update server certificates if required. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 666 | if (hs->config->cert->cert_cb != NULL) { |
| 667 | int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 668 | if (rv == 0) { |
| 669 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 670 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 671 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 672 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 673 | if (rv < 0) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 674 | return ssl_hs_x509_lookup; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 675 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 678 | if (!ssl_on_certificate_selected(hs)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 679 | return ssl_hs_error; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 680 | } |
| 681 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 682 | if (hs->ocsp_stapling_requested && |
| 683 | ssl->ctx->legacy_ocsp_callback != nullptr) { |
| 684 | switch (ssl->ctx->legacy_ocsp_callback( |
| 685 | ssl, ssl->ctx->legacy_ocsp_callback_arg)) { |
| 686 | case SSL_TLSEXT_ERR_OK: |
| 687 | break; |
| 688 | case SSL_TLSEXT_ERR_NOACK: |
| 689 | hs->ocsp_stapling_requested = false; |
| 690 | break; |
| 691 | default: |
| 692 | OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR); |
| 693 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 694 | return ssl_hs_error; |
| 695 | } |
| 696 | } |
| 697 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 698 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 699 | // Jump to the TLS 1.3 state machine. |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 700 | hs->state = state12_tls13; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 701 | return ssl_hs_ok; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 704 | ssl->s3->early_data_reason = ssl_early_data_protocol_version; |
| 705 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 706 | SSL_CLIENT_HELLO client_hello; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 707 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 708 | return ssl_hs_error; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 711 | // Negotiate the cipher suite. This must be done after |cert_cb| so the |
| 712 | // certificate is finalized. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 713 | SSLCipherPreferenceList *prefs = hs->config->cipher_list |
| 714 | ? hs->config->cipher_list.get() |
| 715 | : ssl->ctx->cipher_list.get(); |
Tobias Thierer | 43be7d2 | 2020-03-02 19:23:34 +0000 | [diff] [blame] | 716 | hs->new_cipher = choose_cipher(hs, &client_hello, prefs); |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 717 | if (hs->new_cipher == NULL) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 718 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 719 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 720 | return ssl_hs_error; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 723 | hs->state = state12_select_parameters; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 724 | return ssl_hs_ok; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 727 | static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) { |
| 728 | enum ssl_hs_wait_t wait = tls13_server_handshake(hs); |
| 729 | if (wait == ssl_hs_ok) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 730 | hs->state = state12_finish_server_handshake; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 731 | return ssl_hs_ok; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 732 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 733 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 734 | return wait; |
| 735 | } |
| 736 | |
| 737 | static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { |
| 738 | SSL *const ssl = hs->ssl; |
| 739 | |
| 740 | SSLMessage msg; |
| 741 | if (!ssl->method->get_message(ssl, &msg)) { |
| 742 | return ssl_hs_read_message; |
| 743 | } |
| 744 | |
| 745 | SSL_CLIENT_HELLO client_hello; |
| 746 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { |
| 747 | return ssl_hs_error; |
| 748 | } |
| 749 | |
| 750 | // Determine whether we are doing session resumption. |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 751 | UniquePtr<SSL_SESSION> session; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 752 | bool tickets_supported = false, renew_ticket = false; |
| 753 | enum ssl_hs_wait_t wait = ssl_get_prev_session( |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 754 | hs, &session, &tickets_supported, &renew_ticket, &client_hello); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 755 | if (wait != ssl_hs_ok) { |
| 756 | return wait; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 757 | } |
| 758 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 759 | if (session) { |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 760 | if (session->extended_master_secret && !hs->extended_master_secret) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 761 | // A ClientHello without EMS that attempts to resume a session with EMS |
| 762 | // is fatal to the connection. |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 763 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 764 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 765 | return ssl_hs_error; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 766 | } |
| 767 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 768 | if (!ssl_session_is_resumable(hs, session.get()) || |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 769 | // If the client offers the EMS extension, but the previous session |
| 770 | // didn't use it, then negotiate a new session. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 771 | hs->extended_master_secret != session->extended_master_secret) { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 772 | session.reset(); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 776 | if (session) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 777 | // Use the old session. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 778 | hs->ticket_expected = renew_ticket; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 779 | ssl->session = std::move(session); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 780 | ssl->s3->session_reused = true; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 781 | } else { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 782 | hs->ticket_expected = tickets_supported; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 783 | ssl_set_session(ssl, NULL); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 784 | if (!ssl_get_new_session(hs, 1 /* server */)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 785 | return ssl_hs_error; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 786 | } |
| 787 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 788 | // Clear the session ID if we want the session to be single-use. |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 789 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 790 | hs->new_session->session_id_length = 0; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | if (ssl->ctx->dos_protection_cb != NULL && |
| 795 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 796 | // Connection rejected for DOS reasons. |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 797 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 798 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 799 | return ssl_hs_error; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | if (ssl->session == NULL) { |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 803 | hs->new_session->cipher = hs->new_cipher; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 804 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 805 | // Determine whether to request a client certificate. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 806 | hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 807 | // Only request a certificate if Channel ID isn't negotiated. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 808 | if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 809 | ssl->s3->channel_id_valid) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 810 | hs->cert_request = false; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 811 | } |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 812 | // CertificateRequest may only be sent in certificate-based ciphers. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 813 | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 814 | hs->cert_request = false; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 815 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 816 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 817 | if (!hs->cert_request) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 818 | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 819 | // classed by them as a bug, but it's assumed by at least NGINX. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 820 | hs->new_session->verify_result = X509_V_OK; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 821 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 824 | // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 825 | // deferred. Complete it now. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 826 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 827 | if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 828 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 829 | return ssl_hs_error; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 830 | } |
| 831 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 832 | // Now that all parameters are known, initialize the handshake hash and hash |
| 833 | // the ClientHello. |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 834 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 835 | !ssl_hash_message(hs, msg)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 836 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 837 | return ssl_hs_error; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 840 | // Handback includes the whole handshake transcript, so we cannot free the |
| 841 | // transcript buffer in the handback case. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 842 | if (!hs->cert_request && !hs->handback) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 843 | hs->transcript.FreeBuffer(); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 846 | ssl->method->next_message(ssl); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 847 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 848 | hs->state = state12_send_server_hello; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 849 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 852 | static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) { |
| 853 | out = out.subspan(out.size() - in.size()); |
| 854 | assert(out.size() == in.size()); |
| 855 | OPENSSL_memcpy(out.data(), in.data(), in.size()); |
| 856 | } |
| 857 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 858 | static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 859 | SSL *const ssl = hs->ssl; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 860 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 861 | // We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 862 | // known attack while we fix ChannelID itself. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 863 | if (ssl->s3->channel_id_valid && |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 864 | (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 865 | ssl->s3->channel_id_valid = false; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 868 | // If this is a resumption and the original handshake didn't support |
| 869 | // ChannelID then we didn't record the original handshake hashes in the |
| 870 | // session and so cannot resume with ChannelIDs. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 871 | if (ssl->session != NULL && |
| 872 | ssl->session->original_handshake_hash_len == 0) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 873 | ssl->s3->channel_id_valid = false; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 876 | struct OPENSSL_timeval now; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 877 | ssl_get_current_time(ssl, &now); |
| 878 | ssl->s3->server_random[0] = now.tv_sec >> 24; |
| 879 | ssl->s3->server_random[1] = now.tv_sec >> 16; |
| 880 | ssl->s3->server_random[2] = now.tv_sec >> 8; |
| 881 | ssl->s3->server_random[3] = now.tv_sec; |
| 882 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 883 | return ssl_hs_error; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 886 | // Implement the TLS 1.3 anti-downgrade feature. |
| 887 | if (ssl_supports_version(hs, TLS1_3_VERSION)) { |
| 888 | if (ssl_protocol_version(ssl) == TLS1_2_VERSION) { |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 889 | if (hs->apply_jdk11_workaround) { |
| 890 | // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it |
| 891 | // here. However, the signal is only effective if all TLS 1.2 |
| 892 | // ServerHellos produced by the server are marked. Thus we send a |
| 893 | // different non-standard signal for the time being, until JDK 11.0.2 is |
| 894 | // released and clients have updated. |
| 895 | copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom); |
| 896 | } else { |
| 897 | copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom); |
| 898 | } |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 899 | } else { |
Robert Sloan | c9abfe4 | 2018-11-26 12:19:07 -0800 | [diff] [blame] | 900 | copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom); |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 901 | } |
Robert Sloan | 0da4395 | 2018-01-03 15:13:14 -0800 | [diff] [blame] | 902 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 903 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 904 | const SSL_SESSION *session = hs->new_session.get(); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 905 | if (ssl->session != nullptr) { |
| 906 | session = ssl->session.get(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 907 | } |
| 908 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 909 | ScopedCBB cbb; |
| 910 | CBB body, session_id; |
| 911 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 912 | !CBB_add_u16(&body, ssl->version) || |
| 913 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 914 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 915 | !CBB_add_bytes(&session_id, session->session_id, |
| 916 | session->session_id_length) || |
Pete Bentley | f23caaf | 2020-09-22 18:02:11 +0100 | [diff] [blame] | 917 | !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 918 | !CBB_add_u8(&body, 0 /* no compression */) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 919 | !ssl_add_serverhello_tlsext(hs, &body) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 920 | !ssl_add_message_cbb(ssl, cbb.get())) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 921 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 922 | return ssl_hs_error; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 925 | if (ssl->session != NULL) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 926 | hs->state = state12_send_server_finished; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 927 | } else { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 928 | hs->state = state12_send_server_certificate; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 929 | } |
| 930 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 931 | } |
| 932 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 933 | static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 934 | SSL *const ssl = hs->ssl; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 935 | ScopedCBB cbb; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 936 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 937 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 938 | if (!ssl_has_certificate(hs)) { |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 939 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 940 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 941 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 942 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 943 | if (!ssl_output_cert_chain(hs)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 944 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | if (hs->certificate_status_expected) { |
| 948 | CBB body, ocsp_response; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 949 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 950 | SSL3_MT_CERTIFICATE_STATUS) || |
| 951 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 952 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 953 | !CBB_add_bytes( |
| 954 | &ocsp_response, |
| 955 | CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()), |
| 956 | CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) || |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 957 | !ssl_add_message_cbb(ssl, cbb.get())) { |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 958 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 959 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 964 | // Assemble ServerKeyExchange parameters if needed. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 965 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 966 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
| 967 | if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 968 | ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 969 | // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend |
| 970 | // the client and server randoms for the signing transcript. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 971 | CBB child; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 972 | if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) || |
| 973 | !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 974 | !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 975 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 976 | } |
| 977 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 978 | // PSK ciphers begin with an identity hint. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 979 | if (alg_a & SSL_aPSK) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 980 | size_t len = hs->config->psk_identity_hint == nullptr |
| 981 | ? 0 |
| 982 | : strlen(hs->config->psk_identity_hint.get()); |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 983 | if (!CBB_add_u16_length_prefixed(cbb.get(), &child) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 984 | !CBB_add_bytes(&child, |
| 985 | (const uint8_t *)hs->config->psk_identity_hint.get(), |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 986 | len)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 987 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 988 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 991 | if (alg_k & SSL_kECDHE) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 992 | // Determine the group to use. |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 993 | uint16_t group_id; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 994 | if (!tls1_get_shared_group(hs, &group_id)) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 995 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 996 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 997 | return ssl_hs_error; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 998 | } |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 999 | hs->new_session->group_id = group_id; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1000 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1001 | // Set up ECDH, generate a key, and emit the public half. |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1002 | hs->key_shares[0] = SSLKeyShare::Create(group_id); |
| 1003 | if (!hs->key_shares[0] || |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 1004 | !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) || |
| 1005 | !CBB_add_u16(cbb.get(), group_id) || |
| 1006 | !CBB_add_u8_length_prefixed(cbb.get(), &child) || |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1007 | !hs->key_shares[0]->Offer(&child)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1008 | return ssl_hs_error; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1009 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1010 | } else { |
| 1011 | assert(alg_k & SSL_kPSK); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1014 | if (!CBBFinishArray(cbb.get(), &hs->server_params)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1015 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1016 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1019 | hs->state = state12_send_server_key_exchange; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1020 | return ssl_hs_ok; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1023 | static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) { |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1024 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1025 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1026 | if (hs->server_params.size() == 0) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1027 | hs->state = state12_send_server_hello_done; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1028 | return ssl_hs_ok; |
| 1029 | } |
| 1030 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1031 | ScopedCBB cbb; |
| 1032 | CBB body, child; |
| 1033 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1034 | SSL3_MT_SERVER_KEY_EXCHANGE) || |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1035 | // |hs->server_params| contains a prefix for signing. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1036 | hs->server_params.size() < 2 * SSL3_RANDOM_SIZE || |
| 1037 | !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE, |
| 1038 | hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1039 | return ssl_hs_error; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1040 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1041 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1042 | // Add a signature. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 1043 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1044 | if (!ssl_has_private_key(hs)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1045 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1046 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1047 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1048 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1049 | // Determine the signature algorithm. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1050 | uint16_t signature_algorithm; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1051 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1052 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1053 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1054 | } |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1055 | if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1056 | if (!CBB_add_u16(&body, signature_algorithm)) { |
| 1057 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1058 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1059 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1063 | // Add space for the signature. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1064 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1065 | uint8_t *ptr; |
| 1066 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 1067 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1068 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1071 | size_t sig_len; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1072 | switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len, |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1073 | signature_algorithm, hs->server_params)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1074 | case ssl_private_key_success: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1075 | if (!CBB_did_write(&child, sig_len)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1076 | return ssl_hs_error; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1077 | } |
| 1078 | break; |
| 1079 | case ssl_private_key_failure: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1080 | return ssl_hs_error; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1081 | case ssl_private_key_retry: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1082 | return ssl_hs_private_key_operation; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1083 | } |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1086 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1087 | return ssl_hs_error; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1088 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1089 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1090 | hs->server_params.Reset(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1091 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1092 | hs->state = state12_send_server_hello_done; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1093 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1096 | static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1097 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1098 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1099 | ScopedCBB cbb; |
| 1100 | CBB body; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1101 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1102 | if (hs->cert_request) { |
| 1103 | CBB cert_types, sigalgs_cbb; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1104 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1105 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 1106 | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
| 1107 | !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1108 | !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) || |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1109 | (ssl_protocol_version(ssl) >= TLS1_2_VERSION && |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1110 | (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) || |
Tobias Thierer | 43be7d2 | 2020-03-02 19:23:34 +0000 | [diff] [blame] | 1111 | !tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1112 | !ssl_add_client_CA_list(hs, &body) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1113 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 1114 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1115 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1116 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1117 | } |
| 1118 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1119 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1120 | SSL3_MT_SERVER_HELLO_DONE) || |
| 1121 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 1122 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1123 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1124 | } |
| 1125 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1126 | hs->state = state12_read_client_certificate; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1127 | return ssl_hs_flush; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1130 | static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1131 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1132 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1133 | if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) { |
| 1134 | return ssl_hs_handback; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1135 | } |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1136 | if (!hs->cert_request) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1137 | hs->state = state12_verify_client_certificate; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1138 | return ssl_hs_ok; |
| 1139 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1140 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1141 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1142 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1143 | return ssl_hs_read_message; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1144 | } |
| 1145 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1146 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1147 | return ssl_hs_error; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1148 | } |
| 1149 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1150 | if (!ssl_hash_message(hs, msg)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1151 | return ssl_hs_error; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1154 | CBS certificate_msg = msg.body; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 1155 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1156 | if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey, |
| 1157 | hs->config->retain_only_sha256_of_client_certs |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 1158 | ? hs->new_session->peer_sha256 |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1159 | : nullptr, |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 1160 | &certificate_msg, ssl->ctx->pool)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1161 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1162 | return ssl_hs_error; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1163 | } |
| 1164 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1165 | if (CBS_len(&certificate_msg) != 0 || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1166 | !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1167 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1168 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1169 | return ssl_hs_error; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1172 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1173 | // No client certificate so the handshake buffer may be discarded. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1174 | hs->transcript.FreeBuffer(); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1175 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1176 | if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1177 | // Fail for TLS only if we required a certificate |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1178 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1179 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1180 | return ssl_hs_error; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1181 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1182 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1183 | // OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1184 | // classed by them as a bug, but it's assumed by at least NGINX. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 1185 | hs->new_session->verify_result = X509_V_OK; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1186 | } else if (hs->config->retain_only_sha256_of_client_certs) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1187 | // The hash will have been filled in. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 1188 | hs->new_session->peer_sha256_valid = 1; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1189 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1190 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1191 | ssl->method->next_message(ssl); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1192 | hs->state = state12_verify_client_certificate; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1193 | return ssl_hs_ok; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1194 | } |
| 1195 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1196 | static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1197 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1198 | switch (ssl_verify_peer_cert(hs)) { |
| 1199 | case ssl_verify_ok: |
| 1200 | break; |
| 1201 | case ssl_verify_invalid: |
| 1202 | return ssl_hs_error; |
| 1203 | case ssl_verify_retry: |
| 1204 | return ssl_hs_certificate_verify; |
| 1205 | } |
| 1206 | } |
| 1207 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1208 | hs->state = state12_read_client_key_exchange; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1209 | return ssl_hs_ok; |
| 1210 | } |
| 1211 | |
| 1212 | static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1213 | SSL *const ssl = hs->ssl; |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1214 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1215 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1216 | return ssl_hs_read_message; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1217 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1218 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1219 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1220 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1223 | CBS client_key_exchange = msg.body; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1224 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1225 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1226 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1227 | // If using a PSK key exchange, parse the PSK identity. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1228 | if (alg_a & SSL_aPSK) { |
| 1229 | CBS psk_identity; |
| 1230 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1231 | // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1232 | // then this is the only field in the message. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1233 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1234 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1235 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1236 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1237 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1241 | CBS_contains_zero_byte(&psk_identity)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1242 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1243 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1244 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1245 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1246 | char *raw = nullptr; |
| 1247 | if (!CBS_strdup(&psk_identity, &raw)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1248 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1249 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1250 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1251 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1252 | hs->new_session->psk_identity.reset(raw); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1255 | // Depending on the key exchange method, compute |premaster_secret|. |
| 1256 | Array<uint8_t> premaster_secret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1257 | if (alg_k & SSL_kRSA) { |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1258 | CBS encrypted_premaster_secret; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1259 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1260 | &encrypted_premaster_secret) || |
| 1261 | CBS_len(&client_key_exchange) != 0) { |
| 1262 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1263 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1264 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1267 | // Allocate a buffer large enough for an RSA decryption. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1268 | Array<uint8_t> decrypt_buf; |
| 1269 | if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) { |
| 1270 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1273 | // Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1274 | // timing-sensitive code below. |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1275 | size_t decrypt_len; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1276 | switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len, |
| 1277 | decrypt_buf.size(), |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1278 | encrypted_premaster_secret)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1279 | case ssl_private_key_success: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1280 | break; |
| 1281 | case ssl_private_key_failure: |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1282 | return ssl_hs_error; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1283 | case ssl_private_key_retry: |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1284 | return ssl_hs_private_key_operation; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1287 | if (decrypt_len != decrypt_buf.size()) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1288 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1289 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1290 | return ssl_hs_error; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1291 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1292 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 1293 | CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len); |
| 1294 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1295 | // Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1296 | // section 7.4.7.1. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1297 | if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) || |
| 1298 | !RAND_bytes(premaster_secret.data(), premaster_secret.size())) { |
| 1299 | return ssl_hs_error; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1302 | // The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1303 | // publicly invalid. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1304 | if (decrypt_len < 11 + premaster_secret.size()) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1305 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1306 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1307 | return ssl_hs_error; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1310 | // Check the padding. See RFC 3447, section 7.2.2. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1311 | size_t padding_len = decrypt_len - premaster_secret.size(); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1312 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1313 | constant_time_eq_int_8(decrypt_buf[1], 2); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1314 | for (size_t i = 2; i < padding_len - 1; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1315 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1316 | } |
| 1317 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1318 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1319 | // The premaster secret must begin with |client_version|. This too must be |
| 1320 | // checked in constant time (http://eprint.iacr.org/2003/052/). |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1321 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1322 | (unsigned)(hs->client_version >> 8)); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1323 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1324 | (unsigned)(hs->client_version & 0xff)); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1325 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1326 | // Select, in constant time, either the decrypted premaster or the random |
| 1327 | // premaster based on |good|. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1328 | for (size_t i = 0; i < premaster_secret.size(); i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1329 | premaster_secret[i] = constant_time_select_8( |
| 1330 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1331 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 1332 | } else if (alg_k & SSL_kECDHE) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1333 | // Parse the ClientKeyExchange. |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1334 | CBS peer_key; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 1335 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1336 | CBS_len(&client_key_exchange) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1337 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1338 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1339 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1342 | // Compute the premaster. |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 1343 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1344 | if (!hs->key_shares[0]->Finish(&premaster_secret, &alert, peer_key)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1345 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1346 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1347 | } |
| 1348 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1349 | // The key exchange state may now be discarded. |
Robert Sloan | 11c28bd | 2018-12-17 12:09:20 -0800 | [diff] [blame] | 1350 | hs->key_shares[0].reset(); |
| 1351 | hs->key_shares[1].reset(); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1352 | } else if (!(alg_k & SSL_kPSK)) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 1353 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1354 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1355 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1358 | // For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1359 | // pre-shared key. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1360 | if (alg_a & SSL_aPSK) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1361 | if (hs->config->psk_server_callback == NULL) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 1362 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1363 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1364 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1367 | // Look up the key for the identity. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1368 | uint8_t psk[PSK_MAX_PSK_LEN]; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1369 | unsigned psk_len = hs->config->psk_server_callback( |
| 1370 | ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk)); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1371 | if (psk_len > PSK_MAX_PSK_LEN) { |
| 1372 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1373 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1374 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1375 | } else if (psk_len == 0) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1376 | // PSK related to the given identity not found. |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1377 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1378 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1379 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | if (alg_k & SSL_kPSK) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1383 | // In plain PSK, other_secret is a block of 0s with the same length as the |
| 1384 | // pre-shared key. |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1385 | if (!premaster_secret.Init(psk_len)) { |
| 1386 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1387 | } |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1388 | OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size()); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1391 | ScopedCBB new_premaster; |
| 1392 | CBB child; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1393 | if (!CBB_init(new_premaster.get(), |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1394 | 2 + psk_len + 2 + premaster_secret.size()) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1395 | !CBB_add_u16_length_prefixed(new_premaster.get(), &child) || |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1396 | !CBB_add_bytes(&child, premaster_secret.data(), |
| 1397 | premaster_secret.size()) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1398 | !CBB_add_u16_length_prefixed(new_premaster.get(), &child) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1399 | !CBB_add_bytes(&child, psk, psk_len) || |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1400 | !CBBFinishArray(new_premaster.get(), &premaster_secret)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1401 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1402 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1403 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1406 | if (!ssl_hash_message(hs, msg)) { |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1407 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1410 | // Compute the master secret. |
Pete Bentley | 1748611 | 2021-01-20 11:51:47 +0000 | [diff] [blame^] | 1411 | hs->new_session->secret_length = tls1_generate_master_secret( |
| 1412 | hs, hs->new_session->secret, premaster_secret); |
| 1413 | if (hs->new_session->secret_length == 0) { |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1414 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1415 | } |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 1416 | hs->new_session->extended_master_secret = hs->extended_master_secret; |
Pete Bentley | 1748611 | 2021-01-20 11:51:47 +0000 | [diff] [blame^] | 1417 | CONSTTIME_DECLASSIFY(hs->new_session->secret, hs->new_session->secret_length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1418 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1419 | ssl->method->next_message(ssl); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1420 | hs->state = state12_read_client_certificate_verify; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1421 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1424 | static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1425 | SSL *const ssl = hs->ssl; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1426 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1427 | // Only RSA and ECDSA client certificates are supported, so a |
| 1428 | // CertificateVerify is required if and only if there's a client certificate. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1429 | if (!hs->peer_pubkey) { |
| 1430 | hs->transcript.FreeBuffer(); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1431 | hs->state = state12_read_change_cipher_spec; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1432 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1435 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1436 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1437 | return ssl_hs_read_message; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1440 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1441 | return ssl_hs_error; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Pete Bentley | f23caaf | 2020-09-22 18:02:11 +0100 | [diff] [blame] | 1444 | // The peer certificate must be valid for signing. |
| 1445 | const CRYPTO_BUFFER *leaf = |
| 1446 | sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0); |
| 1447 | CBS leaf_cbs; |
| 1448 | CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs); |
| 1449 | if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) { |
| 1450 | return ssl_hs_error; |
| 1451 | } |
| 1452 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1453 | CBS certificate_verify = msg.body, signature; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1454 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1455 | // Determine the signature algorithm. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1456 | uint16_t signature_algorithm = 0; |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1457 | if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1458 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1459 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1460 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1461 | return ssl_hs_error; |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1462 | } |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1463 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Tobias Thierer | 43be7d2 | 2020-03-02 19:23:34 +0000 | [diff] [blame] | 1464 | if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) { |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1465 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1466 | return ssl_hs_error; |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1467 | } |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 1468 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
Robert Sloan | 2424d84 | 2017-05-01 07:46:28 -0700 | [diff] [blame] | 1469 | } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1470 | hs->peer_pubkey.get())) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1471 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1472 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1473 | return ssl_hs_error; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1474 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1475 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1476 | // Parse and verify the signature. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1477 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1478 | CBS_len(&certificate_verify) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1479 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1480 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1481 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame] | 1484 | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
| 1485 | hs->peer_pubkey.get(), hs->transcript.buffer())) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1486 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1487 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1488 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1491 | // The handshake buffer is no longer necessary, and we may hash the current |
| 1492 | // message. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1493 | hs->transcript.FreeBuffer(); |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1494 | if (!ssl_hash_message(hs, msg)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1495 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1496 | } |
| 1497 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1498 | ssl->method->next_message(ssl); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1499 | hs->state = state12_read_change_cipher_spec; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1500 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1503 | static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1504 | if (hs->handback && hs->ssl->session != NULL) { |
| 1505 | return ssl_hs_handback; |
| 1506 | } |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1507 | hs->state = state12_process_change_cipher_spec; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1508 | return ssl_hs_read_change_cipher_spec; |
| 1509 | } |
| 1510 | |
| 1511 | static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) { |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 1512 | if (!tls1_change_cipher_state(hs, evp_aead_open)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1513 | return ssl_hs_error; |
| 1514 | } |
| 1515 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1516 | hs->state = state12_read_next_proto; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1517 | return ssl_hs_ok; |
| 1518 | } |
| 1519 | |
| 1520 | static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1521 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1522 | |
| 1523 | if (!hs->next_proto_neg_seen) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1524 | hs->state = state12_read_channel_id; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1525 | return ssl_hs_ok; |
| 1526 | } |
| 1527 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1528 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1529 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1530 | return ssl_hs_read_message; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1531 | } |
| 1532 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1533 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) || |
| 1534 | !ssl_hash_message(hs, msg)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1535 | return ssl_hs_error; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1538 | CBS next_protocol = msg.body, selected_protocol, padding; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1539 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1540 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1541 | CBS_len(&next_protocol) != 0) { |
| 1542 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1543 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1544 | return ssl_hs_error; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1545 | } |
| 1546 | |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1547 | if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1548 | return ssl_hs_error; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1551 | ssl->method->next_message(ssl); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1552 | hs->state = state12_read_channel_id; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1553 | return ssl_hs_ok; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1554 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1555 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1556 | static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1557 | SSL *const ssl = hs->ssl; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1558 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1559 | if (!ssl->s3->channel_id_valid) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1560 | hs->state = state12_read_client_finished; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1561 | return ssl_hs_ok; |
| 1562 | } |
| 1563 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1564 | SSLMessage msg; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1565 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1566 | return ssl_hs_read_message; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1567 | } |
| 1568 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1569 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || |
| 1570 | !tls1_verify_channel_id(hs, msg) || |
| 1571 | !ssl_hash_message(hs, msg)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1572 | return ssl_hs_error; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1573 | } |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1574 | |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 1575 | ssl->method->next_message(ssl); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1576 | hs->state = state12_read_client_finished; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1577 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1580 | static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { |
| 1581 | SSL *const ssl = hs->ssl; |
| 1582 | enum ssl_hs_wait_t wait = ssl_get_finished(hs); |
| 1583 | if (wait != ssl_hs_ok) { |
| 1584 | return wait; |
| 1585 | } |
| 1586 | |
| 1587 | if (ssl->session != NULL) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1588 | hs->state = state12_finish_server_handshake; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1589 | } else { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1590 | hs->state = state12_send_server_finished; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | // If this is a full handshake with ChannelID then record the handshake |
| 1594 | // hashes in |hs->new_session| in case we need them to verify a |
| 1595 | // ChannelID signature on a resumption of this session in the future. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1596 | if (ssl->session == NULL && ssl->s3->channel_id_valid && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1597 | !tls1_record_handshake_hashes_for_channel_id(hs)) { |
| 1598 | return ssl_hs_error; |
| 1599 | } |
| 1600 | |
| 1601 | return ssl_hs_ok; |
| 1602 | } |
| 1603 | |
| 1604 | static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1605 | SSL *const ssl = hs->ssl; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1606 | |
| 1607 | if (hs->ticket_expected) { |
| 1608 | const SSL_SESSION *session; |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1609 | UniquePtr<SSL_SESSION> session_copy; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1610 | if (ssl->session == NULL) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1611 | // Fix the timeout to measure from the ticket issuance time. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1612 | ssl_session_rebase_time(ssl, hs->new_session.get()); |
| 1613 | session = hs->new_session.get(); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1614 | } else { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1615 | // We are renewing an existing session. Duplicate the session to adjust |
| 1616 | // the timeout. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1617 | session_copy = |
| 1618 | SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1619 | if (!session_copy) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1620 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1623 | ssl_session_rebase_time(ssl, session_copy.get()); |
| 1624 | session = session_copy.get(); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1625 | } |
| 1626 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1627 | ScopedCBB cbb; |
| 1628 | CBB body, ticket; |
| 1629 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1630 | SSL3_MT_NEW_SESSION_TICKET) || |
| 1631 | !CBB_add_u32(&body, session->timeout) || |
| 1632 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1633 | !ssl_encrypt_ticket(hs, &ticket, session) || |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1634 | !ssl_add_message_cbb(ssl, cbb.get())) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1635 | return ssl_hs_error; |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1636 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1637 | } |
| 1638 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 1639 | if (!ssl->method->add_change_cipher_spec(ssl) || |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 1640 | !tls1_change_cipher_state(hs, evp_aead_seal) || |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 1641 | !ssl_send_finished(hs)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1642 | return ssl_hs_error; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1643 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1644 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1645 | if (ssl->session != NULL) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1646 | hs->state = state12_read_change_cipher_spec; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1647 | } else { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1648 | hs->state = state12_finish_server_handshake; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1649 | } |
| 1650 | return ssl_hs_flush; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1651 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1652 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1653 | static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) { |
| 1654 | SSL *const ssl = hs->ssl; |
| 1655 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1656 | if (hs->handback) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1657 | return ssl_hs_handback; |
| 1658 | } |
| 1659 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1660 | ssl->method->on_handshake_complete(ssl); |
| 1661 | |
| 1662 | // If we aren't retaining peer certificates then we can discard it now. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1663 | if (hs->new_session != NULL && |
| 1664 | hs->config->retain_only_sha256_of_client_certs) { |
| 1665 | hs->new_session->certs.reset(); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1666 | ssl->ctx->x509_method->session_clear(hs->new_session.get()); |
| 1667 | } |
| 1668 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1669 | if (ssl->session != NULL) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1670 | ssl->s3->established_session = UpRef(ssl->session); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1671 | } else { |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1672 | ssl->s3->established_session = std::move(hs->new_session); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1673 | ssl->s3->established_session->not_resumable = false; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | hs->handshake_finalized = true; |
| 1677 | ssl->s3->initial_handshake_complete = true; |
| 1678 | ssl_update_cache(hs, SSL_SESS_CACHE_SERVER); |
| 1679 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1680 | hs->state = state12_done; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1681 | return ssl_hs_ok; |
| 1682 | } |
| 1683 | |
| 1684 | enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1685 | while (hs->state != state12_done) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1686 | enum ssl_hs_wait_t ret = ssl_hs_error; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1687 | enum tls12_server_hs_state_t state = |
| 1688 | static_cast<enum tls12_server_hs_state_t>(hs->state); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1689 | switch (state) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1690 | case state12_start_accept: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1691 | ret = do_start_accept(hs); |
| 1692 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1693 | case state12_read_client_hello: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1694 | ret = do_read_client_hello(hs); |
| 1695 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1696 | case state12_select_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1697 | ret = do_select_certificate(hs); |
| 1698 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1699 | case state12_tls13: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1700 | ret = do_tls13(hs); |
| 1701 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1702 | case state12_select_parameters: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1703 | ret = do_select_parameters(hs); |
| 1704 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1705 | case state12_send_server_hello: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1706 | ret = do_send_server_hello(hs); |
| 1707 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1708 | case state12_send_server_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1709 | ret = do_send_server_certificate(hs); |
| 1710 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1711 | case state12_send_server_key_exchange: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1712 | ret = do_send_server_key_exchange(hs); |
| 1713 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1714 | case state12_send_server_hello_done: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1715 | ret = do_send_server_hello_done(hs); |
| 1716 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1717 | case state12_read_client_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1718 | ret = do_read_client_certificate(hs); |
| 1719 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1720 | case state12_verify_client_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1721 | ret = do_verify_client_certificate(hs); |
| 1722 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1723 | case state12_read_client_key_exchange: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1724 | ret = do_read_client_key_exchange(hs); |
| 1725 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1726 | case state12_read_client_certificate_verify: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1727 | ret = do_read_client_certificate_verify(hs); |
| 1728 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1729 | case state12_read_change_cipher_spec: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1730 | ret = do_read_change_cipher_spec(hs); |
| 1731 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1732 | case state12_process_change_cipher_spec: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1733 | ret = do_process_change_cipher_spec(hs); |
| 1734 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1735 | case state12_read_next_proto: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1736 | ret = do_read_next_proto(hs); |
| 1737 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1738 | case state12_read_channel_id: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1739 | ret = do_read_channel_id(hs); |
| 1740 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1741 | case state12_read_client_finished: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1742 | ret = do_read_client_finished(hs); |
| 1743 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1744 | case state12_send_server_finished: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1745 | ret = do_send_server_finished(hs); |
| 1746 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1747 | case state12_finish_server_handshake: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1748 | ret = do_finish_server_handshake(hs); |
| 1749 | break; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1750 | case state12_done: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1751 | ret = ssl_hs_ok; |
| 1752 | break; |
| 1753 | } |
| 1754 | |
| 1755 | if (hs->state != state) { |
| 1756 | ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 1757 | } |
| 1758 | |
| 1759 | if (ret != ssl_hs_ok) { |
| 1760 | return ret; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1); |
| 1765 | return ssl_hs_ok; |
| 1766 | } |
| 1767 | |
| 1768 | const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1769 | enum tls12_server_hs_state_t state = |
| 1770 | static_cast<enum tls12_server_hs_state_t>(hs->state); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1771 | switch (state) { |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1772 | case state12_start_accept: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1773 | return "TLS server start_accept"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1774 | case state12_read_client_hello: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1775 | return "TLS server read_client_hello"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1776 | case state12_select_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1777 | return "TLS server select_certificate"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1778 | case state12_tls13: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1779 | return tls13_server_handshake_state(hs); |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1780 | case state12_select_parameters: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1781 | return "TLS server select_parameters"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1782 | case state12_send_server_hello: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1783 | return "TLS server send_server_hello"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1784 | case state12_send_server_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1785 | return "TLS server send_server_certificate"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1786 | case state12_send_server_key_exchange: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1787 | return "TLS server send_server_key_exchange"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1788 | case state12_send_server_hello_done: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1789 | return "TLS server send_server_hello_done"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1790 | case state12_read_client_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1791 | return "TLS server read_client_certificate"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1792 | case state12_verify_client_certificate: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1793 | return "TLS server verify_client_certificate"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1794 | case state12_read_client_key_exchange: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1795 | return "TLS server read_client_key_exchange"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1796 | case state12_read_client_certificate_verify: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1797 | return "TLS server read_client_certificate_verify"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1798 | case state12_read_change_cipher_spec: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1799 | return "TLS server read_change_cipher_spec"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1800 | case state12_process_change_cipher_spec: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1801 | return "TLS server process_change_cipher_spec"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1802 | case state12_read_next_proto: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1803 | return "TLS server read_next_proto"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1804 | case state12_read_channel_id: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1805 | return "TLS server read_channel_id"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1806 | case state12_read_client_finished: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1807 | return "TLS server read_client_finished"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1808 | case state12_send_server_finished: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1809 | return "TLS server send_server_finished"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1810 | case state12_finish_server_handshake: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1811 | return "TLS server finish_server_handshake"; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame] | 1812 | case state12_done: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1813 | return "TLS server done"; |
| 1814 | } |
| 1815 | |
| 1816 | return "TLS server unknown"; |
| 1817 | } |
| 1818 | |
Robert Sloan | 726e9d1 | 2018-09-11 11:45:04 -0700 | [diff] [blame] | 1819 | BSSL_NAMESPACE_END |