blob: e50a69021342d81b003717881d0ecda6cef926df [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * 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).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * 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.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * 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 :-).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Steven Valdez4d71a9a2017-08-14 15:08:34 -040039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * 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.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * 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
Steven Valdez4d71a9a2017-08-14 15:08:34 -040065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
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 *
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400113 * Portions of the attached software ("Contribution") are developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700114 * 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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin880b14e2014-08-25 22:35:07 -0400151#include <assert.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400155#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/cipher.h>
Daniel McArdle00e434d2021-02-18 11:47:18 -0500157#include <openssl/curve25519.h>
158#include <openssl/digest.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400166#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/rand.h>
168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700174BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400175
David Benjamin861abcc2018-07-14 17:40:26 -0400176bool ssl_client_cipher_list_contains_cipher(
177 const SSL_CLIENT_HELLO *client_hello, uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400178 CBS cipher_suites;
179 CBS_init(&cipher_suites, client_hello->cipher_suites,
180 client_hello->cipher_suites_len);
181
182 while (CBS_len(&cipher_suites) > 0) {
183 uint16_t got_id;
184 if (!CBS_get_u16(&cipher_suites, &got_id)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400185 return false;
David Benjamin1deb41b2016-08-09 19:36:38 -0400186 }
187
188 if (got_id == id) {
David Benjamin861abcc2018-07-14 17:40:26 -0400189 return true;
David Benjamin1deb41b2016-08-09 19:36:38 -0400190 }
191 }
192
David Benjamin861abcc2018-07-14 17:40:26 -0400193 return false;
David Benjamin1deb41b2016-08-09 19:36:38 -0400194}
195
David Benjamin861abcc2018-07-14 17:40:26 -0400196static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
197 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500198 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500199 assert(!ssl->s3->have_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400200 CBS supported_versions, versions;
David Benjamin731058e2016-12-03 23:15:13 -0500201 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
202 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400203 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
204 CBS_len(&supported_versions) != 0 ||
205 CBS_len(&versions) == 0) {
206 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
207 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin861abcc2018-07-14 17:40:26 -0400208 return false;
Steven Valdezfdd10992016-09-15 16:27:05 -0400209 }
David Benjamin2dc02042016-09-19 19:57:37 -0400210 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400211 // Convert the ClientHello version to an equivalent supported_versions
212 // extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400213 static const uint8_t kTLSVersions[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400214 0x03, 0x03, // TLS 1.2
215 0x03, 0x02, // TLS 1.1
216 0x03, 0x01, // TLS 1
Steven Valdez8f36c512017-06-20 10:55:02 -0400217 };
218
219 static const uint8_t kDTLSVersions[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400220 0xfe, 0xfd, // DTLS 1.2
221 0xfe, 0xff, // DTLS 1.0
Steven Valdez8f36c512017-06-20 10:55:02 -0400222 };
223
224 size_t versions_len = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400225 if (SSL_is_dtls(ssl)) {
226 if (client_hello->version <= DTLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400227 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400228 } else if (client_hello->version <= DTLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400229 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400230 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400231 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
232 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400233 } else {
234 if (client_hello->version >= TLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400235 versions_len = 6;
David Benjamin9bb15f52018-06-26 00:07:40 -0400236 } else if (client_hello->version >= TLS1_1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400237 versions_len = 4;
David Benjamin9bb15f52018-06-26 00:07:40 -0400238 } else if (client_hello->version >= TLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400239 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400240 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400241 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
242 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400243 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400244 }
245
Steven Valdez8f36c512017-06-20 10:55:02 -0400246 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjamin861abcc2018-07-14 17:40:26 -0400247 return false;
David Benjamin25fe85b2016-08-09 20:00:32 -0400248 }
249
David Benjaminc11ea9422017-08-29 16:33:21 -0400250 // At this point, the connection's version is known and |ssl->version| is
251 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400252 ssl->s3->have_version = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400253 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
David Benjamin25fe85b2016-08-09 20:00:32 -0400254
David Benjaminc11ea9422017-08-29 16:33:21 -0400255 // Handle FALLBACK_SCSV.
Steven Valdez8f36c512017-06-20 10:55:02 -0400256 if (ssl_client_cipher_list_contains_cipher(client_hello,
257 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
David Benjamind1e3ce12017-10-06 18:31:15 -0400258 ssl_protocol_version(ssl) < hs->max_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400259 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
260 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
David Benjamin861abcc2018-07-14 17:40:26 -0400261 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400262 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400263
David Benjamin861abcc2018-07-14 17:40:26 -0400264 return true;
David Benjamin25fe85b2016-08-09 20:00:32 -0400265}
266
David Benjaminee910bf2017-07-25 22:36:00 -0400267static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
268 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin2578b292016-12-03 23:19:55 -0500269 CBS cipher_suites;
270 CBS_init(&cipher_suites, client_hello->cipher_suites,
271 client_hello->cipher_suites_len);
272
David Benjaminee910bf2017-07-25 22:36:00 -0400273 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
274 if (!sk) {
David Benjaminee910bf2017-07-25 22:36:00 -0400275 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500276 }
277
278 while (CBS_len(&cipher_suites) > 0) {
279 uint16_t cipher_suite;
280
281 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
282 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjaminee910bf2017-07-25 22:36:00 -0400283 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500284 }
285
286 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
David Benjaminee910bf2017-07-25 22:36:00 -0400287 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjaminee910bf2017-07-25 22:36:00 -0400288 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500289 }
290 }
291
292 return sk;
David Benjamin2578b292016-12-03 23:19:55 -0500293}
294
David Benjaminc11ea9422017-08-29 16:33:21 -0400295// 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 Benjamin2578b292016-12-03 23:19:55 -0500299static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
300 uint32_t *out_mask_k,
301 uint32_t *out_mask_a) {
David Benjamin2578b292016-12-03 23:19:55 -0500302 uint32_t mask_k = 0;
303 uint32_t mask_a = 0;
304
Christopher Patton9cde8482018-07-17 11:36:36 -0700305 if (ssl_has_certificate(hs)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400306 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 Benjamin2578b292016-12-03 23:19:55 -0500308 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500309 }
310 }
311
David Benjaminc11ea9422017-08-29 16:33:21 -0400312 // Check for a shared group to consider ECDHE ciphers.
David Benjamin2578b292016-12-03 23:19:55 -0500313 uint16_t unused;
314 if (tls1_get_shared_group(hs, &unused)) {
315 mask_k |= SSL_kECDHE;
316 }
317
David Benjaminc11ea9422017-08-29 16:33:21 -0400318 // PSK requires a server callback.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700319 if (hs->config->psk_server_callback != NULL) {
David Benjamin2578b292016-12-03 23:19:55 -0500320 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
David Benjaminf6cc8dd2020-02-09 17:45:04 -0500328static const SSL_CIPHER *choose_cipher(
David Benjamin2578b292016-12-03 23:19:55 -0500329 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
David Benjamin48b276d2018-04-12 17:37:32 -0400330 const SSLCipherPreferenceList *server_pref) {
David Benjamin2578b292016-12-03 23:19:55 -0500331 SSL *const ssl = hs->ssl;
Steven Valdezacddb8c2018-04-11 12:58:27 -0400332 const STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjaminc11ea9422017-08-29 16:33:21 -0400333 // 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
David Benjamin48b276d2018-04-12 17:37:32 -0400335 // comment about |in_group_flags| in the |SSLCipherPreferenceList|
David Benjaminc11ea9422017-08-29 16:33:21 -0400336 // struct.
David Benjamin48b276d2018-04-12 17:37:32 -0400337 const bool *in_group_flags;
David Benjaminc11ea9422017-08-29 16:33:21 -0400338 // group_min contains the minimal index so far found in a group, or -1 if no
339 // such value exists yet.
David Benjamin2578b292016-12-03 23:19:55 -0500340 int group_min = -1;
341
David Benjaminee910bf2017-07-25 22:36:00 -0400342 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
David Benjaminccbb1652017-03-22 18:24:32 -0400343 ssl_parse_client_cipher_list(client_hello);
David Benjaminee910bf2017-07-25 22:36:00 -0400344 if (!client_pref) {
345 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500346 }
347
348 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamin48b276d2018-04-12 17:37:32 -0400349 prio = server_pref->ciphers.get();
David Benjamin2578b292016-12-03 23:19:55 -0500350 in_group_flags = server_pref->in_group_flags;
David Benjaminee910bf2017-07-25 22:36:00 -0400351 allow = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500352 } else {
David Benjaminee910bf2017-07-25 22:36:00 -0400353 prio = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500354 in_group_flags = NULL;
David Benjamin48b276d2018-04-12 17:37:32 -0400355 allow = server_pref->ciphers.get();
David Benjamin2578b292016-12-03 23:19:55 -0500356 }
357
David Benjaminccbb1652017-03-22 18:24:32 -0400358 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -0500359 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
360
361 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400362 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500363
David Benjaminccbb1652017-03-22 18:24:32 -0400364 size_t cipher_index;
David Benjaminc11ea9422017-08-29 16:33:21 -0400365 if (// Check if the cipher is supported for the current version.
David Benjamind1e3ce12017-10-06 18:31:15 -0400366 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
367 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400368 // Check the cipher is supported for the server configuration.
David Benjaminccbb1652017-03-22 18:24:32 -0400369 (c->algorithm_mkey & mask_k) &&
370 (c->algorithm_auth & mask_a) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400371 // Check the cipher is in the |allow| list.
David Benjaminccbb1652017-03-22 18:24:32 -0400372 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin48b276d2018-04-12 17:37:32 -0400373 if (in_group_flags != NULL && in_group_flags[i]) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400374 // This element of |prio| is in a group. Update the minimum index found
375 // so far and continue looking.
David Benjamin2578b292016-12-03 23:19:55 -0500376 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 }
David Benjaminee910bf2017-07-25 22:36:00 -0400383 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin2578b292016-12-03 23:19:55 -0500384 }
385 }
386
David Benjamin48b276d2018-04-12 17:37:32 -0400387 if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400388 // We are about to leave a group, but we found a match in it, so that's
389 // our answer.
David Benjaminee910bf2017-07-25 22:36:00 -0400390 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin2578b292016-12-03 23:19:55 -0500391 }
392 }
393
David Benjaminee910bf2017-07-25 22:36:00 -0400394 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500395}
396
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400397static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
398 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700399 hs->state = state12_read_client_hello;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400400 return ssl_hs_ok;
401}
402
David Benjamin6965d252018-11-19 15:49:56 -0600403// is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent
David Benjaminfa81cc62019-01-16 18:11:46 -0600404// from a JDK 11 client with both TLS 1.3 and a prior version enabled.
David Benjamin6965d252018-11-19 15:49:56 -0600405static 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(
David Benjamindfddbc42022-07-18 16:08:06 -0400412 client_hello, TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamin6965d252018-11-19 15:49:56 -0600413 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;
David Benjamin21440762022-03-19 18:39:18 -0400419 static constexpr struct {
David Benjamin6965d252018-11-19 15:49:56 -0600420 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(&copy, &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
David Benjamin18b68362021-06-18 23:13:46 -0400505static bool decrypt_ech(SSL_HANDSHAKE *hs, uint8_t *out_alert,
506 const SSL_CLIENT_HELLO *client_hello) {
507 SSL *const ssl = hs->ssl;
508 CBS body;
509 if (!ssl_client_hello_get_extension(client_hello, &body,
510 TLSEXT_TYPE_encrypted_client_hello)) {
511 return true;
512 }
513 uint8_t type;
514 if (!CBS_get_u8(&body, &type)) {
515 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
516 *out_alert = SSL_AD_DECODE_ERROR;
517 return false;
518 }
519 if (type != ECH_CLIENT_OUTER) {
520 return true;
521 }
522 // This is a ClientHelloOuter ECH extension. Attempt to decrypt it.
523 uint8_t config_id;
524 uint16_t kdf_id, aead_id;
525 CBS enc, payload;
526 if (!CBS_get_u16(&body, &kdf_id) || //
527 !CBS_get_u16(&body, &aead_id) || //
528 !CBS_get_u8(&body, &config_id) ||
529 !CBS_get_u16_length_prefixed(&body, &enc) ||
530 !CBS_get_u16_length_prefixed(&body, &payload) || //
531 CBS_len(&body) != 0) {
532 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
533 *out_alert = SSL_AD_DECODE_ERROR;
534 return false;
535 }
536
537 {
538 MutexReadLock lock(&ssl->ctx->lock);
539 hs->ech_keys = UpRef(ssl->ctx->ech_keys);
540 }
541
542 if (!hs->ech_keys) {
543 ssl->s3->ech_status = ssl_ech_rejected;
544 return true;
545 }
546
547 for (const auto &config : hs->ech_keys->configs) {
548 hs->ech_hpke_ctx.Reset();
549 if (config_id != config->ech_config().config_id ||
550 !config->SetupContext(hs->ech_hpke_ctx.get(), kdf_id, aead_id, enc)) {
551 // Ignore the error and try another ECHConfig.
552 ERR_clear_error();
553 continue;
554 }
David Benjamin18b68362021-06-18 23:13:46 -0400555 bool is_decrypt_error;
David Benjamin44425dd2022-01-27 12:22:42 -0500556 if (!ssl_client_hello_decrypt(hs, out_alert, &is_decrypt_error,
557 &hs->ech_client_hello_buf, client_hello,
558 payload)) {
David Benjamin18b68362021-06-18 23:13:46 -0400559 if (is_decrypt_error) {
560 // Ignore the error and try another ECHConfig.
561 ERR_clear_error();
David Benjamin44425dd2022-01-27 12:22:42 -0500562 // The |out_alert| calling convention currently relies on a default of
563 // |SSL_AD_DECODE_ERROR|. https://crbug.com/boringssl/373 tracks
564 // switching to sum types, which avoids this.
565 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin18b68362021-06-18 23:13:46 -0400566 continue;
567 }
568 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
569 return false;
570 }
David Benjamin18b68362021-06-18 23:13:46 -0400571 hs->ech_config_id = config_id;
572 ssl->s3->ech_status = ssl_ech_accepted;
573 return true;
574 }
575
576 // If we did not accept ECH, proceed with the ClientHelloOuter. Note this
577 // could be key mismatch or ECH GREASE, so we must complete the handshake
578 // as usual, except EncryptedExtensions will contain retry configs.
579 ssl->s3->ech_status = ssl_ech_rejected;
580 return true;
581}
582
David Benjaminef0183c2019-07-20 09:11:05 -0400583static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert,
584 const SSL_CLIENT_HELLO *client_hello) {
585 SSL *const ssl = hs->ssl;
586 CBS sni;
587 if (!ssl_client_hello_get_extension(client_hello, &sni,
588 TLSEXT_TYPE_server_name)) {
589 // No SNI extension to parse.
590 return true;
591 }
592
593 CBS server_name_list, host_name;
594 uint8_t name_type;
595 if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) ||
596 !CBS_get_u8(&server_name_list, &name_type) ||
597 // Although the server_name extension was intended to be extensible to
598 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
599 // different name types will cause an error. Further, RFC 4366 originally
600 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
601 // adding new name types is no longer feasible.
602 //
603 // Act as if the extensibility does not exist to simplify parsing.
604 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
605 CBS_len(&server_name_list) != 0 ||
606 CBS_len(&sni) != 0) {
607 *out_alert = SSL_AD_DECODE_ERROR;
608 return false;
609 }
610
611 if (name_type != TLSEXT_NAMETYPE_host_name ||
612 CBS_len(&host_name) == 0 ||
613 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
614 CBS_contains_zero_byte(&host_name)) {
615 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
616 return false;
617 }
618
619 // Copy the hostname as a string.
620 char *raw = nullptr;
621 if (!CBS_strdup(&host_name, &raw)) {
622 *out_alert = SSL_AD_INTERNAL_ERROR;
623 return false;
624 }
625 ssl->s3->hostname.reset(raw);
626
627 hs->should_ack_sni = true;
628 return true;
629}
630
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400631static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900632 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400633
David Benjamin7934f082017-08-01 16:32:25 -0400634 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400635 if (!ssl->method->get_message(ssl, &msg)) {
636 return ssl_hs_read_message;
David Benjamin7934f082017-08-01 16:32:25 -0400637 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400638
David Benjamin7934f082017-08-01 16:32:25 -0400639 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400640 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 }
Adam Langley95c29f32014-06-20 12:00:00 -0700642
David Benjamin731058e2016-12-03 23:15:13 -0500643 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500644 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
David Benjamin3570d732015-06-29 00:28:17 -0400645 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400646 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400647 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 }
Adam Langley95c29f32014-06-20 12:00:00 -0700649
David Benjaminf9cc26f2020-02-09 16:49:31 -0500650 // ClientHello should be the end of the flight. We check this early to cover
651 // all protocol versions.
652 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
653 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
654 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
655 return ssl_hs_error;
656 }
657
Adam Langleyc9827e02019-04-12 14:46:50 -0700658 if (hs->config->handoff) {
659 return ssl_hs_handoff;
660 }
661
David Benjamin18b68362021-06-18 23:13:46 -0400662 uint8_t alert = SSL_AD_DECODE_ERROR;
663 if (!decrypt_ech(hs, &alert, &client_hello)) {
664 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
665 return ssl_hs_error;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500666 }
667
David Benjamin18b68362021-06-18 23:13:46 -0400668 // ECH may have changed which ClientHello we process. Update |msg| and
669 // |client_hello| in case.
670 if (!hs->GetClientHello(&msg, &client_hello)) {
671 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
672 return ssl_hs_error;
673 }
674
David Benjaminef0183c2019-07-20 09:11:05 -0400675 if (!extract_sni(hs, &alert, &client_hello)) {
676 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
677 return ssl_hs_error;
678 }
679
Daniel McArdle00e434d2021-02-18 11:47:18 -0500680 hs->state = state12_read_client_hello_after_ech;
681 return ssl_hs_ok;
682}
683
684static enum ssl_hs_wait_t do_read_client_hello_after_ech(SSL_HANDSHAKE *hs) {
685 SSL *const ssl = hs->ssl;
686
687 SSLMessage msg_unused;
688 SSL_CLIENT_HELLO client_hello;
689 if (!hs->GetClientHello(&msg_unused, &client_hello)) {
690 return ssl_hs_error;
691 }
692
David Benjaminc11ea9422017-08-29 16:33:21 -0400693 // Run the early callback.
David Benjamin59bae5a2017-02-02 23:51:22 -0500694 if (ssl->ctx->select_certificate_cb != NULL) {
695 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000696 case ssl_select_cert_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400697 return ssl_hs_certificate_selection_pending;
David Benjamine14ff062016-08-09 16:21:24 -0400698
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000699 case ssl_select_cert_error:
David Benjaminc11ea9422017-08-29 16:33:21 -0400700 // Connection rejected.
David Benjamin59bae5a2017-02-02 23:51:22 -0500701 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400702 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400703 return ssl_hs_error;
David Benjamine14ff062016-08-09 16:21:24 -0400704
David Benjamin59bae5a2017-02-02 23:51:22 -0500705 default:
706 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400707 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500708 }
David Benjamine14ff062016-08-09 16:21:24 -0400709
David Benjaminc11ea9422017-08-29 16:33:21 -0400710 // Freeze the version range after the early callback.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700711 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400712 return ssl_hs_error;
David Benjamin68161cb2017-06-20 14:49:43 -0400713 }
714
David Benjamin6965d252018-11-19 15:49:56 -0600715 if (hs->config->jdk11_workaround &&
716 is_probably_jdk11_with_tls13(&client_hello)) {
717 hs->apply_jdk11_workaround = true;
718 }
719
Daniel McArdle00e434d2021-02-18 11:47:18 -0500720 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500721 if (!negotiate_version(hs, &alert, &client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400722 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400723 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500724 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400725
Steven Valdez8f36c512017-06-20 10:55:02 -0400726 hs->client_version = client_hello.version;
David Benjamin59bae5a2017-02-02 23:51:22 -0500727 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
728 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400729 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500730 }
731 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
732 client_hello.random_len);
733
David Benjaminc11ea9422017-08-29 16:33:21 -0400734 // Only null compression is supported. TLS 1.3 further requires the peer
735 // advertise no other compression.
David Benjamin59bae5a2017-02-02 23:51:22 -0500736 if (OPENSSL_memchr(client_hello.compression_methods, 0,
737 client_hello.compression_methods_len) == NULL ||
David Benjamind1e3ce12017-10-06 18:31:15 -0400738 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
David Benjamin59bae5a2017-02-02 23:51:22 -0500739 client_hello.compression_methods_len != 1)) {
740 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
David Benjamind1e3ce12017-10-06 18:31:15 -0400741 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400742 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500743 }
744
David Benjaminc11ea9422017-08-29 16:33:21 -0400745 // TLS extensions.
David Benjamin59bae5a2017-02-02 23:51:22 -0500746 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
747 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400748 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500749 }
750
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700751 hs->state = state12_select_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400752 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500753}
754
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400755static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500756 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400757
David Benjaminc11ea9422017-08-29 16:33:21 -0400758 // Call |cert_cb| to update server certificates if required.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700759 if (hs->config->cert->cert_cb != NULL) {
760 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
David Benjamin59bae5a2017-02-02 23:51:22 -0500761 if (rv == 0) {
762 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400763 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400764 return ssl_hs_error;
David Benjamin25fe85b2016-08-09 20:00:32 -0400765 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500766 if (rv < 0) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400767 return ssl_hs_x509_lookup;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900768 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900769 }
770
David Benjamina232a712017-03-30 15:51:53 -0500771 if (!ssl_on_certificate_selected(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400772 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900773 }
774
David Benjamin103ed082018-05-10 19:55:02 -0400775 if (hs->ocsp_stapling_requested &&
776 ssl->ctx->legacy_ocsp_callback != nullptr) {
777 switch (ssl->ctx->legacy_ocsp_callback(
778 ssl, ssl->ctx->legacy_ocsp_callback_arg)) {
779 case SSL_TLSEXT_ERR_OK:
780 break;
781 case SSL_TLSEXT_ERR_NOACK:
782 hs->ocsp_stapling_requested = false;
783 break;
784 default:
785 OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR);
786 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
787 return ssl_hs_error;
788 }
789 }
790
David Benjamind1e3ce12017-10-06 18:31:15 -0400791 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400792 // Jump to the TLS 1.3 state machine.
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700793 hs->state = state12_tls13;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400794 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500795 }
796
Daniel McArdle00e434d2021-02-18 11:47:18 -0500797 // It should not be possible to negotiate TLS 1.2 with ECH. The
798 // ClientHelloInner decoding function rejects ClientHellos which offer TLS 1.2
799 // or below.
David Benjaminba423c92021-06-15 16:26:58 -0400800 assert(ssl->s3->ech_status != ssl_ech_accepted);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500801
David Benjamin64770122019-05-04 11:00:04 -0500802 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
803
Daniel McArdle00e434d2021-02-18 11:47:18 -0500804 SSLMessage msg_unused;
David Benjamin59bae5a2017-02-02 23:51:22 -0500805 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500806 if (!hs->GetClientHello(&msg_unused, &client_hello)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400807 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500808 }
809
David Benjaminc11ea9422017-08-29 16:33:21 -0400810 // Negotiate the cipher suite. This must be done after |cert_cb| so the
811 // certificate is finalized.
David Benjamin0ce090a2018-07-02 20:24:40 -0400812 SSLCipherPreferenceList *prefs = hs->config->cipher_list
813 ? hs->config->cipher_list.get()
814 : ssl->ctx->cipher_list.get();
David Benjaminf6cc8dd2020-02-09 17:45:04 -0500815 hs->new_cipher = choose_cipher(hs, &client_hello, prefs);
David Benjamin45738dd2017-02-09 20:01:26 -0500816 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500817 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
David Benjamind1e3ce12017-10-06 18:31:15 -0400818 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400819 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500820 }
821
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700822 hs->state = state12_select_parameters;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400823 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500824}
825
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400826static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
827 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
828 if (wait == ssl_hs_ok) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700829 hs->state = state12_finish_server_handshake;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400830 return ssl_hs_ok;
David Benjamin7934f082017-08-01 16:32:25 -0400831 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400832
833 return wait;
834}
835
836static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
837 SSL *const ssl = hs->ssl;
838
839 SSLMessage msg;
840 if (!ssl->method->get_message(ssl, &msg)) {
841 return ssl_hs_read_message;
842 }
843
David Benjamin59bae5a2017-02-02 23:51:22 -0500844 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500845 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400846 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500847 }
David Benjamin34202b92016-11-16 19:07:53 +0900848
Adam Langley47cefed2021-05-26 13:36:40 -0700849 hs->session_id_len = client_hello.session_id_len;
850 // This is checked in |ssl_client_hello_init|.
851 assert(hs->session_id_len <= sizeof(hs->session_id));
852 OPENSSL_memcpy(hs->session_id, client_hello.session_id, hs->session_id_len);
853
David Benjaminc11ea9422017-08-29 16:33:21 -0400854 // Determine whether we are doing session resumption.
David Benjamin37af90f2017-07-29 01:42:16 -0400855 UniquePtr<SSL_SESSION> session;
David Benjaminfd45ee72017-08-31 14:49:09 -0400856 bool tickets_supported = false, renew_ticket = false;
Steven Valdez398085b2017-08-29 13:25:56 -0400857 enum ssl_hs_wait_t wait = ssl_get_prev_session(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700858 hs, &session, &tickets_supported, &renew_ticket, &client_hello);
Steven Valdez398085b2017-08-29 13:25:56 -0400859 if (wait != ssl_hs_ok) {
860 return wait;
David Benjamin34202b92016-11-16 19:07:53 +0900861 }
862
David Benjamind781fc42017-07-12 16:25:57 -0400863 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500864 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400865 // A ClientHello without EMS that attempts to resume a session with EMS
866 // is fatal to the connection.
David Benjamin34202b92016-11-16 19:07:53 +0900867 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind1e3ce12017-10-06 18:31:15 -0400868 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400869 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900870 }
871
David Benjamind781fc42017-07-12 16:25:57 -0400872 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400873 // If the client offers the EMS extension, but the previous session
874 // didn't use it, then negotiate a new session.
David Benjaminfc02b592017-02-17 16:26:01 -0500875 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400876 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900877 }
878 }
879
David Benjamind781fc42017-07-12 16:25:57 -0400880 if (session) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400881 // Use the old session.
David Benjaminc3c88822016-11-14 10:32:04 +0900882 hs->ticket_expected = renew_ticket;
David Benjamin50596f82018-07-02 19:47:27 -0400883 ssl->session = std::move(session);
David Benjamin046bc1f2017-08-31 15:06:42 -0400884 ssl->s3->session_reused = true;
David Benjamin9b2cdb72021-04-01 23:21:53 -0400885 hs->can_release_private_key = true;
David Benjamin34202b92016-11-16 19:07:53 +0900886 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900887 hs->ticket_expected = tickets_supported;
David Benjamin962b3752021-05-10 15:17:18 -0400888 ssl_set_session(ssl, nullptr);
889 if (!ssl_get_new_session(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400890 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900891 }
892
David Benjamin962b3752021-05-10 15:17:18 -0400893 // Assign a session ID if not using session tickets.
894 if (!hs->ticket_expected &&
895 (ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
896 hs->new_session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
897 RAND_bytes(hs->new_session->session_id,
898 hs->new_session->session_id_length);
David Benjamin34202b92016-11-16 19:07:53 +0900899 }
900 }
901
902 if (ssl->ctx->dos_protection_cb != NULL &&
903 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400904 // Connection rejected for DOS reasons.
David Benjamin34202b92016-11-16 19:07:53 +0900905 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400906 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400907 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900908 }
909
David Benjaminf01f42a2016-11-16 19:05:33 +0900910 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500911 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400912
David Benjaminc11ea9422017-08-29 16:33:21 -0400913 // Determine whether to request a client certificate.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700914 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400915 // Only request a certificate if Channel ID isn't negotiated.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700916 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
David Benjamin8acec002021-05-19 13:03:34 -0400917 hs->channel_id_negotiated) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400918 hs->cert_request = false;
David Benjamin5c1ce292015-05-26 16:59:43 -0400919 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400920 // CertificateRequest may only be sent in certificate-based ciphers.
David Benjamin45738dd2017-02-09 20:01:26 -0500921 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400922 hs->cert_request = false;
David Benjamin5c1ce292015-05-26 16:59:43 -0400923 }
Adam Langley37646832016-08-01 16:16:46 -0700924
David Benjaminc3c88822016-11-14 10:32:04 +0900925 if (!hs->cert_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400926 // OpenSSL returns X509_V_OK when no certificates are requested. This is
927 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -0500928 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700929 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 }
931
David Benjaminc11ea9422017-08-29 16:33:21 -0400932 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
933 // deferred. Complete it now.
David Benjamind98107b2017-06-15 22:56:37 -0400934 uint8_t alert = SSL_AD_DECODE_ERROR;
935 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400936 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400937 return ssl_hs_error;
David Benjamin9ef31f02016-10-31 18:01:13 -0400938 }
939
David Benjaminc11ea9422017-08-29 16:33:21 -0400940 // Now that all parameters are known, initialize the handshake hash and hash
941 // the ClientHello.
David Benjamind1e3ce12017-10-06 18:31:15 -0400942 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400943 !ssl_hash_message(hs, msg)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400944 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400945 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400946 }
947
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700948 // Handback includes the whole handshake transcript, so we cannot free the
949 // transcript buffer in the handback case.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700950 if (!hs->cert_request && !hs->handback) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400951 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 }
953
David Benjamin8f94c312017-08-01 17:35:55 -0400954 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400955
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700956 hs->state = state12_send_server_hello;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400957 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800958}
Adam Langley95c29f32014-06-20 12:00:00 -0700959
David Benjamin6965d252018-11-19 15:49:56 -0600960static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) {
David Benjamin006f20a2021-06-22 23:00:49 -0400961 out = out.last(in.size());
David Benjamin6965d252018-11-19 15:49:56 -0600962 OPENSSL_memcpy(out.data(), in.data(), in.size());
963}
964
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400965static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900966 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400967
David Benjaminc11ea9422017-08-29 16:33:21 -0400968 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
969 // known attack while we fix ChannelID itself.
David Benjamin8acec002021-05-19 13:03:34 -0400970 if (hs->channel_id_negotiated &&
David Benjamin45738dd2017-02-09 20:01:26 -0500971 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin8acec002021-05-19 13:03:34 -0400972 hs->channel_id_negotiated = false;
David Benjamin56380462015-10-10 14:59:09 -0400973 }
974
David Benjaminc11ea9422017-08-29 16:33:21 -0400975 // If this is a resumption and the original handshake didn't support
976 // ChannelID then we didn't record the original handshake hashes in the
977 // session and so cannot resume with ChannelIDs.
Steven Valdez87eab492016-06-27 16:34:59 -0400978 if (ssl->session != NULL &&
979 ssl->session->original_handshake_hash_len == 0) {
David Benjamin8acec002021-05-19 13:03:34 -0400980 hs->channel_id_negotiated = false;
David Benjamin56380462015-10-10 14:59:09 -0400981 }
982
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700983 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
984 if (hints && !hs->hints_requested &&
985 hints->server_random_tls12.size() == SSL3_RANDOM_SIZE) {
986 OPENSSL_memcpy(ssl->s3->server_random, hints->server_random_tls12.data(),
987 SSL3_RANDOM_SIZE);
988 } else {
989 struct OPENSSL_timeval now;
990 ssl_get_current_time(ssl, &now);
991 CRYPTO_store_u32_be(ssl->s3->server_random,
992 static_cast<uint32_t>(now.tv_sec));
993 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
994 return ssl_hs_error;
995 }
996 if (hints && hs->hints_requested &&
997 !hints->server_random_tls12.CopyFrom(ssl->s3->server_random)) {
998 return ssl_hs_error;
999 }
David Benjamin56380462015-10-10 14:59:09 -04001000 }
1001
Steven Valdezf1af1292018-08-13 10:54:48 -04001002 // Implement the TLS 1.3 anti-downgrade feature.
1003 if (ssl_supports_version(hs, TLS1_3_VERSION)) {
1004 if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
David Benjamin6965d252018-11-19 15:49:56 -06001005 if (hs->apply_jdk11_workaround) {
1006 // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it
1007 // here. However, the signal is only effective if all TLS 1.2
1008 // ServerHellos produced by the server are marked. Thus we send a
1009 // different non-standard signal for the time being, until JDK 11.0.2 is
1010 // released and clients have updated.
1011 copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom);
1012 } else {
1013 copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom);
1014 }
Steven Valdezf1af1292018-08-13 10:54:48 -04001015 } else {
David Benjamin6965d252018-11-19 15:49:56 -06001016 copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom);
Steven Valdezf1af1292018-08-13 10:54:48 -04001017 }
David Benjamin6df65402017-12-18 18:00:23 -05001018 }
David Benjamin1f61f0d2016-07-10 12:20:35 -04001019
Adam Langley47cefed2021-05-26 13:36:40 -07001020 Span<const uint8_t> session_id;
David Benjamin50596f82018-07-02 19:47:27 -04001021 if (ssl->session != nullptr) {
Adam Langley47cefed2021-05-26 13:36:40 -07001022 // Echo the session ID from the ClientHello to indicate resumption.
1023 session_id = MakeConstSpan(hs->session_id, hs->session_id_len);
1024 } else {
1025 session_id = MakeConstSpan(hs->new_session->session_id,
1026 hs->new_session->session_id_length);
Steven Valdez87eab492016-06-27 16:34:59 -04001027 }
1028
David Benjamin1386aad2017-07-19 23:57:40 -04001029 ScopedCBB cbb;
Adam Langley47cefed2021-05-26 13:36:40 -07001030 CBB body, session_id_bytes;
David Benjamin1386aad2017-07-19 23:57:40 -04001031 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -04001032 !CBB_add_u16(&body, ssl->version) ||
1033 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
Adam Langley47cefed2021-05-26 13:36:40 -07001034 !CBB_add_u8_length_prefixed(&body, &session_id_bytes) ||
1035 !CBB_add_bytes(&session_id_bytes, session_id.data(), session_id.size()) ||
David Benjamin3743aaf2020-09-21 13:55:16 -04001036 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -04001037 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001038 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001039 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -04001040 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001041 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -04001042 }
1043
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001044 if (ssl->session != nullptr) {
1045 // No additional hints to generate in resumption.
1046 if (hs->hints_requested) {
1047 return ssl_hs_hints_ready;
1048 }
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001049 hs->state = state12_send_server_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001050 } else {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001051 hs->state = state12_send_server_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001052 }
1053 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001054}
Adam Langley95c29f32014-06-20 12:00:00 -07001055
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001056static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001057 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001058 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -04001059
David Benjamin44148742017-06-17 13:20:59 -04001060 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Christopher Patton9cde8482018-07-17 11:36:36 -07001061 if (!ssl_has_certificate(hs)) {
David Benjamin44148742017-06-17 13:20:59 -04001062 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001063 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001064 }
David Benjamin32a66d52016-07-13 22:03:11 -04001065
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001066 if (!ssl_output_cert_chain(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001067 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001068 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001069
David Benjamin44148742017-06-17 13:20:59 -04001070 if (hs->certificate_status_expected) {
1071 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -04001072 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -04001073 SSL3_MT_CERTIFICATE_STATUS) ||
1074 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1075 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001076 !CBB_add_bytes(
1077 &ocsp_response,
1078 CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
1079 CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
David Benjamind781fc42017-07-12 16:25:57 -04001080 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -04001081 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001082 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001083 }
David Benjamin0d1730d2017-06-15 23:24:25 -04001084 }
David Benjamin75836432016-06-17 18:48:29 -04001085 }
1086
David Benjaminc11ea9422017-08-29 16:33:21 -04001087 // Assemble ServerKeyExchange parameters if needed.
David Benjamin44148742017-06-17 13:20:59 -04001088 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1089 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1090 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001091 ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001092 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
1093 // the client and server randoms for the signing transcript.
David Benjamin44148742017-06-17 13:20:59 -04001094 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -04001095 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
1096 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1097 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001098 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001099 }
1100
David Benjaminc11ea9422017-08-29 16:33:21 -04001101 // PSK ciphers begin with an identity hint.
Adam Langleyfcf25832014-12-18 17:42:32 -08001102 if (alg_a & SSL_aPSK) {
David Benjamin0ce090a2018-07-02 20:24:40 -04001103 size_t len = hs->config->psk_identity_hint == nullptr
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001104 ? 0
David Benjamin0ce090a2018-07-02 20:24:40 -04001105 : strlen(hs->config->psk_identity_hint.get());
David Benjamind781fc42017-07-12 16:25:57 -04001106 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04001107 !CBB_add_bytes(&child,
1108 (const uint8_t *)hs->config->psk_identity_hint.get(),
David Benjamin2a0b3912015-12-18 01:01:21 -05001109 len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001110 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001111 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 }
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001114 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001115 // Determine the group to use.
Steven Valdezce902a92016-05-17 11:47:53 -04001116 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001117 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001118 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001119 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001120 return ssl_hs_error;
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001121 }
David Benjamin45738dd2017-02-09 20:01:26 -05001122 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langley7b935932018-11-12 13:53:42 -08001124 hs->key_shares[0] = SSLKeyShare::Create(group_id);
1125 if (!hs->key_shares[0] ||
David Benjamind781fc42017-07-12 16:25:57 -04001126 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
1127 !CBB_add_u16(cbb.get(), group_id) ||
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001128 !CBB_add_u8_length_prefixed(cbb.get(), &child)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001129 return ssl_hs_error;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001130 }
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001131
1132 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
1133 bool hint_ok = false;
1134 if (hints && !hs->hints_requested &&
1135 hints->ecdhe_group_id == group_id &&
1136 !hints->ecdhe_public_key.empty() &&
1137 !hints->ecdhe_private_key.empty()) {
1138 CBS cbs = MakeConstSpan(hints->ecdhe_private_key);
1139 hint_ok = hs->key_shares[0]->DeserializePrivateKey(&cbs);
1140 }
1141 if (hint_ok) {
1142 // Reuse the ECDH key from handshake hints.
1143 if (!CBB_add_bytes(&child, hints->ecdhe_public_key.data(),
1144 hints->ecdhe_public_key.size())) {
1145 return ssl_hs_error;
1146 }
1147 } else {
1148 // Generate a key, and emit the public half.
David Benjamin08b1f382023-02-28 17:22:23 -05001149 if (!hs->key_shares[0]->Generate(&child)) {
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001150 return ssl_hs_error;
1151 }
1152 // If generating hints, save the ECDHE key.
1153 if (hints && hs->hints_requested) {
1154 bssl::ScopedCBB private_key_cbb;
1155 if (!hints->ecdhe_public_key.CopyFrom(
1156 MakeConstSpan(CBB_data(&child), CBB_len(&child))) ||
1157 !CBB_init(private_key_cbb.get(), 32) ||
1158 !hs->key_shares[0]->SerializePrivateKey(private_key_cbb.get()) ||
1159 !CBBFinishArray(private_key_cbb.get(),
1160 &hints->ecdhe_private_key)) {
1161 return ssl_hs_error;
1162 }
1163 hints->ecdhe_group_id = group_id;
1164 }
1165 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001166 } else {
1167 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 }
Adam Langley95c29f32014-06-20 12:00:00 -07001169
David Benjamin879efc32017-09-21 11:20:53 -04001170 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001171 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001172 }
David Benjaminc42acee2016-06-17 17:47:58 -04001173 }
1174
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001175 hs->state = state12_send_server_key_exchange;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001176 return ssl_hs_ok;
David Benjamin44148742017-06-17 13:20:59 -04001177}
1178
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001179static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin44148742017-06-17 13:20:59 -04001180 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001181
David Benjamin879efc32017-09-21 11:20:53 -04001182 if (hs->server_params.size() == 0) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001183 hs->state = state12_send_server_hello_done;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001184 return ssl_hs_ok;
1185 }
1186
David Benjamin1386aad2017-07-19 23:57:40 -04001187 ScopedCBB cbb;
1188 CBB body, child;
1189 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001190 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001191 // |hs->server_params| contains a prefix for signing.
David Benjamin879efc32017-09-21 11:20:53 -04001192 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
1193 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
1194 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001195 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -05001196 }
Adam Langley95c29f32014-06-20 12:00:00 -07001197
David Benjaminc11ea9422017-08-29 16:33:21 -04001198 // Add a signature.
David Benjamin45738dd2017-02-09 20:01:26 -05001199 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Christopher Patton9cde8482018-07-17 11:36:36 -07001200 if (!ssl_has_private_key(hs)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001201 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001202 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 }
Adam Langley95c29f32014-06-20 12:00:00 -07001204
David Benjaminc11ea9422017-08-29 16:33:21 -04001205 // Determine the signature algorithm.
David Benjaminea9a0d52016-07-08 15:52:59 -07001206 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001207 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Adam Langleye0afc852018-07-09 16:59:33 -07001208 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001209 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001210 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001211 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001212 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001213 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001214 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001215 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001216 }
David Benjaminc42acee2016-06-17 17:47:58 -04001217 }
1218
David Benjaminc11ea9422017-08-29 16:33:21 -04001219 // Add space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001220 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -04001221 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001222 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001223 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001224 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001225 }
1226
David Benjamin2a0b3912015-12-18 01:01:21 -05001227 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001228 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
David Benjamin75a1f232017-10-11 17:19:19 -04001229 signature_algorithm, hs->server_params)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001230 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001231 if (!CBB_did_write(&child, sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001232 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -05001233 }
1234 break;
1235 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001236 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -05001237 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001238 return ssl_hs_private_key_operation;
David Benjamin2a0b3912015-12-18 01:01:21 -05001239 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001240 }
1241
David Benjamin9b2cdb72021-04-01 23:21:53 -04001242 hs->can_release_private_key = true;
David Benjamin1386aad2017-07-19 23:57:40 -04001243 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001244 return ssl_hs_error;
David Benjamin1f9f9c42015-08-28 16:17:59 -04001245 }
David Benjaminc42acee2016-06-17 17:47:58 -04001246
David Benjamin879efc32017-09-21 11:20:53 -04001247 hs->server_params.Reset();
David Benjaminc42acee2016-06-17 17:47:58 -04001248
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001249 hs->state = state12_send_server_hello_done;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001250 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001251}
Adam Langley95c29f32014-06-20 12:00:00 -07001252
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001253static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001254 SSL *const ssl = hs->ssl;
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001255 if (hs->hints_requested) {
1256 return ssl_hs_hints_ready;
1257 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001258
David Benjamin1386aad2017-07-19 23:57:40 -04001259 ScopedCBB cbb;
1260 CBB body;
David Benjamin75836432016-06-17 18:48:29 -04001261
David Benjamin0d1730d2017-06-15 23:24:25 -04001262 if (hs->cert_request) {
1263 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -04001264 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -04001265 SSL3_MT_CERTIFICATE_REQUEST) ||
1266 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1267 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
David Benjamin9bb15f52018-06-26 00:07:40 -04001268 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001269 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
David Benjamin0d1730d2017-06-15 23:24:25 -04001270 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
David Benjaminebad5082020-02-03 19:32:19 -05001271 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001272 !ssl_add_client_CA_list(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001273 !ssl_add_message_cbb(ssl, cbb.get())) {
1274 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001275 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001276 }
1277 }
1278
David Benjamin1386aad2017-07-19 23:57:40 -04001279 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1280 SSL3_MT_SERVER_HELLO_DONE) ||
1281 !ssl_add_message_cbb(ssl, cbb.get())) {
1282 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001283 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001284 }
1285
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001286 hs->state = state12_read_client_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001287 return ssl_hs_flush;
Adam Langleyfcf25832014-12-18 17:42:32 -08001288}
Adam Langley95c29f32014-06-20 12:00:00 -07001289
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001290static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001291 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001292
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001293 if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
1294 return ssl_hs_handback;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001295 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001296 if (!hs->cert_request) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001297 hs->state = state12_verify_client_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001298 return ssl_hs_ok;
1299 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001300
David Benjamin7934f082017-08-01 16:32:25 -04001301 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001302 if (!ssl->method->get_message(ssl, &msg)) {
1303 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -04001304 }
1305
David Benjamin9bb15f52018-06-26 00:07:40 -04001306 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001307 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001308 }
1309
David Benjamin7934f082017-08-01 16:32:25 -04001310 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001311 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -05001312 }
1313
David Benjamin7934f082017-08-01 16:32:25 -04001314 CBS certificate_msg = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001315 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminbfdd1a92018-06-29 16:26:38 -04001316 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001317 hs->config->retain_only_sha256_of_client_certs
David Benjamine664a532017-07-20 20:19:36 -04001318 ? hs->new_session->peer_sha256
David Benjaminbfdd1a92018-06-29 16:26:38 -04001319 : nullptr,
David Benjamine664a532017-07-20 20:19:36 -04001320 &certificate_msg, ssl->ctx->pool)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001321 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001322 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001323 }
1324
Adam Langley68e71242016-12-12 11:06:16 -08001325 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001326 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001327 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001328 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001329 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001330 }
1331
David Benjaminbfdd1a92018-06-29 16:26:38 -04001332 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001333 // No client certificate so the handshake buffer may be discarded.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001334 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001335
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001336 if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001337 // Fail for TLS only if we required a certificate
David Benjamin9f1dc822016-06-07 17:03:46 -04001338 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001339 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001340 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001341 }
Adam Langley37646832016-08-01 16:16:46 -07001342
David Benjaminc11ea9422017-08-29 16:33:21 -04001343 // OpenSSL returns X509_V_OK when no certificates are received. This is
1344 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -05001345 hs->new_session->verify_result = X509_V_OK;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001346 } else if (hs->config->retain_only_sha256_of_client_certs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001347 // The hash will have been filled in.
Anton Bikineev50e7ea52022-01-23 22:35:48 +01001348 hs->new_session->peer_sha256_valid = true;
Adam Langley364f7a62016-12-12 10:51:00 -08001349 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001350
David Benjamin8f94c312017-08-01 17:35:55 -04001351 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001352 hs->state = state12_verify_client_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001353 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001354}
1355
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001356static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001357 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001358 switch (ssl_verify_peer_cert(hs)) {
1359 case ssl_verify_ok:
1360 break;
1361 case ssl_verify_invalid:
1362 return ssl_hs_error;
1363 case ssl_verify_retry:
1364 return ssl_hs_certificate_verify;
1365 }
1366 }
1367
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001368 hs->state = state12_read_client_key_exchange;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001369 return ssl_hs_ok;
1370}
1371
1372static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001373 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001374 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001375 if (!ssl->method->get_message(ssl, &msg)) {
1376 return ssl_hs_read_message;
David Benjamin44148742017-06-17 13:20:59 -04001377 }
David Benjamin276b7e82017-01-21 14:13:39 -05001378
David Benjamin7934f082017-08-01 16:32:25 -04001379 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001380 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 }
Adam Langley95c29f32014-06-20 12:00:00 -07001382
David Benjamin7934f082017-08-01 16:32:25 -04001383 CBS client_key_exchange = msg.body;
David Benjamin44148742017-06-17 13:20:59 -04001384 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1385 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001386
David Benjaminc11ea9422017-08-29 16:33:21 -04001387 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 if (alg_a & SSL_aPSK) {
1389 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001390
David Benjaminc11ea9422017-08-29 16:33:21 -04001391 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1392 // then this is the only field in the message.
Adam Langleyfcf25832014-12-18 17:42:32 -08001393 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1394 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001395 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001396 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001397 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001398 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001399
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1401 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001402 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind1e3ce12017-10-06 18:31:15 -04001403 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin499742c2017-07-22 12:45:38 -04001404 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001405 }
David Benjaminbfdd1a92018-06-29 16:26:38 -04001406 char *raw = nullptr;
1407 if (!CBS_strdup(&psk_identity, &raw)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001408 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001409 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001410 }
David Benjaminbfdd1a92018-06-29 16:26:38 -04001411 hs->new_session->psk_identity.reset(raw);
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001413
David Benjamin499742c2017-07-22 12:45:38 -04001414 // Depending on the key exchange method, compute |premaster_secret|.
1415 Array<uint8_t> premaster_secret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001417 CBS encrypted_premaster_secret;
David Benjamin9bb15f52018-06-26 00:07:40 -04001418 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1419 &encrypted_premaster_secret) ||
1420 CBS_len(&client_key_exchange) != 0) {
1421 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1422 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1423 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001424 }
1425
David Benjaminc11ea9422017-08-29 16:33:21 -04001426 // Allocate a buffer large enough for an RSA decryption.
David Benjamin499742c2017-07-22 12:45:38 -04001427 Array<uint8_t> decrypt_buf;
1428 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1429 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001430 }
David Benjamin35c02302014-07-13 04:14:59 -04001431
David Benjaminc11ea9422017-08-29 16:33:21 -04001432 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1433 // timing-sensitive code below.
David Benjamin3f5b43d2015-12-01 19:31:24 -05001434 size_t decrypt_len;
David Benjamin499742c2017-07-22 12:45:38 -04001435 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1436 decrypt_buf.size(),
David Benjamin75a1f232017-10-11 17:19:19 -04001437 encrypted_premaster_secret)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001438 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001439 break;
1440 case ssl_private_key_failure:
David Benjamin499742c2017-07-22 12:45:38 -04001441 return ssl_hs_error;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001442 case ssl_private_key_retry:
David Benjamin499742c2017-07-22 12:45:38 -04001443 return ssl_hs_private_key_operation;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001444 }
1445
David Benjamin499742c2017-07-22 12:45:38 -04001446 if (decrypt_len != decrypt_buf.size()) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001447 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -04001448 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001449 return ssl_hs_error;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001450 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001451
Adam Langleya6a049a2018-12-06 17:15:58 -08001452 CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len);
1453
David Benjaminc11ea9422017-08-29 16:33:21 -04001454 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1455 // section 7.4.7.1.
David Benjamin499742c2017-07-22 12:45:38 -04001456 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1457 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1458 return ssl_hs_error;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001459 }
1460
David Benjaminc11ea9422017-08-29 16:33:21 -04001461 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1462 // publicly invalid.
David Benjamin499742c2017-07-22 12:45:38 -04001463 if (decrypt_len < 11 + premaster_secret.size()) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001464 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -04001465 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001466 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 }
David Benjamin35c02302014-07-13 04:14:59 -04001468
David Benjaminc11ea9422017-08-29 16:33:21 -04001469 // Check the padding. See RFC 3447, section 7.2.2.
David Benjamin499742c2017-07-22 12:45:38 -04001470 size_t padding_len = decrypt_len - premaster_secret.size();
David Benjamin3f5b43d2015-12-01 19:31:24 -05001471 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1472 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001473 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001474 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1475 }
1476 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1477
David Benjaminc11ea9422017-08-29 16:33:21 -04001478 // The premaster secret must begin with |client_version|. This too must be
1479 // checked in constant time (http://eprint.iacr.org/2003/052/).
David Benjamin3f5b43d2015-12-01 19:31:24 -05001480 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001481 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001482 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001483 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001484
David Benjaminc11ea9422017-08-29 16:33:21 -04001485 // Select, in constant time, either the decrypted premaster or the random
1486 // premaster based on |good|.
David Benjamin499742c2017-07-22 12:45:38 -04001487 for (size_t i = 0; i < premaster_secret.size(); i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001488 premaster_secret[i] = constant_time_select_8(
1489 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1490 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001491 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001492 // Parse the ClientKeyExchange.
David Benjamin08b1f382023-02-28 17:22:23 -05001493 CBS ciphertext;
1494 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ciphertext) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001495 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001496 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001497 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001498 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001499 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001500
David Benjamin08b1f382023-02-28 17:22:23 -05001501 // Decapsulate the premaster secret.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001502 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin08b1f382023-02-28 17:22:23 -05001503 if (!hs->key_shares[0]->Decap(&premaster_secret, &alert, ciphertext)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001504 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin499742c2017-07-22 12:45:38 -04001505 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001506 }
Adam Langley95c29f32014-06-20 12:00:00 -07001507
David Benjaminc11ea9422017-08-29 16:33:21 -04001508 // The key exchange state may now be discarded.
Adam Langley7b935932018-11-12 13:53:42 -08001509 hs->key_shares[0].reset();
1510 hs->key_shares[1].reset();
David Benjamin44148742017-06-17 13:20:59 -04001511 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001512 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001513 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001514 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001515 }
Adam Langley95c29f32014-06-20 12:00:00 -07001516
David Benjaminc11ea9422017-08-29 16:33:21 -04001517 // For a PSK cipher suite, the actual pre-master secret is combined with the
1518 // pre-shared key.
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 if (alg_a & SSL_aPSK) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001520 if (hs->config->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001521 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001522 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001523 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001524 }
1525
David Benjaminc11ea9422017-08-29 16:33:21 -04001526 // Look up the key for the identity.
David Benjamin44148742017-06-17 13:20:59 -04001527 uint8_t psk[PSK_MAX_PSK_LEN];
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001528 unsigned psk_len = hs->config->psk_server_callback(
David Benjaminbfdd1a92018-06-29 16:26:38 -04001529 ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
David Benjamin44148742017-06-17 13:20:59 -04001530 if (psk_len > PSK_MAX_PSK_LEN) {
1531 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001532 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001533 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001534 } else if (psk_len == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001535 // PSK related to the given identity not found.
David Benjamin44148742017-06-17 13:20:59 -04001536 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamind1e3ce12017-10-06 18:31:15 -04001537 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
David Benjamin499742c2017-07-22 12:45:38 -04001538 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001539 }
1540
1541 if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001542 // In plain PSK, other_secret is a block of 0s with the same length as the
1543 // pre-shared key.
David Benjamin499742c2017-07-22 12:45:38 -04001544 if (!premaster_secret.Init(psk_len)) {
1545 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001546 }
David Benjamin499742c2017-07-22 12:45:38 -04001547 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
David Benjamin44148742017-06-17 13:20:59 -04001548 }
1549
David Benjamin1386aad2017-07-19 23:57:40 -04001550 ScopedCBB new_premaster;
1551 CBB child;
David Benjamin1386aad2017-07-19 23:57:40 -04001552 if (!CBB_init(new_premaster.get(),
David Benjamin499742c2017-07-22 12:45:38 -04001553 2 + psk_len + 2 + premaster_secret.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001554 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
David Benjamin499742c2017-07-22 12:45:38 -04001555 !CBB_add_bytes(&child, premaster_secret.data(),
1556 premaster_secret.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001557 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin75a1f232017-10-11 17:19:19 -04001559 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
David Benjamin499742c2017-07-22 12:45:38 -04001560 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001561 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 }
David Benjamin14c83e72014-07-13 04:54:57 -04001563
David Benjamin7934f082017-08-01 16:32:25 -04001564 if (!ssl_hash_message(hs, msg)) {
David Benjamin499742c2017-07-22 12:45:38 -04001565 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001566 }
1567
David Benjaminc11ea9422017-08-29 16:33:21 -04001568 // Compute the master secret.
David Benjamin5351c8b2020-11-19 00:25:29 -05001569 hs->new_session->secret_length = tls1_generate_master_secret(
1570 hs, hs->new_session->secret, premaster_secret);
1571 if (hs->new_session->secret_length == 0) {
David Benjamin499742c2017-07-22 12:45:38 -04001572 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 }
David Benjaminfc02b592017-02-17 16:26:01 -05001574 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin5351c8b2020-11-19 00:25:29 -05001575 CONSTTIME_DECLASSIFY(hs->new_session->secret, hs->new_session->secret_length);
David Benjamin9b2cdb72021-04-01 23:21:53 -04001576 hs->can_release_private_key = true;
David Benjamin14c83e72014-07-13 04:54:57 -04001577
David Benjamin8f94c312017-08-01 17:35:55 -04001578 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001579 hs->state = state12_read_client_certificate_verify;
David Benjamin499742c2017-07-22 12:45:38 -04001580 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001581}
Adam Langley95c29f32014-06-20 12:00:00 -07001582
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001583static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001584 SSL *const ssl = hs->ssl;
David Benjamin6553b372014-07-22 14:11:30 -04001585
David Benjaminc11ea9422017-08-29 16:33:21 -04001586 // Only RSA and ECDSA client certificates are supported, so a
1587 // CertificateVerify is required if and only if there's a client certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001588 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001589 hs->transcript.FreeBuffer();
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001590 hs->state = state12_read_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001591 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001592 }
Adam Langley95c29f32014-06-20 12:00:00 -07001593
David Benjamin7934f082017-08-01 16:32:25 -04001594 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001595 if (!ssl->method->get_message(ssl, &msg)) {
1596 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 }
David Benjaminef865502014-08-24 02:48:34 -04001598
David Benjamin7934f082017-08-01 16:32:25 -04001599 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001600 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001601 }
1602
David Benjamincd8f3d32020-06-15 11:16:06 -04001603 // The peer certificate must be valid for signing.
1604 const CRYPTO_BUFFER *leaf =
1605 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1606 CBS leaf_cbs;
1607 CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1608 if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) {
1609 return ssl_hs_error;
1610 }
1611
David Benjamin7934f082017-08-01 16:32:25 -04001612 CBS certificate_verify = msg.body, signature;
David Benjamin6897dbe2014-07-12 20:18:28 -04001613
David Benjaminc11ea9422017-08-29 16:33:21 -04001614 // Determine the signature algorithm.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001615 uint16_t signature_algorithm = 0;
David Benjamind1e3ce12017-10-06 18:31:15 -04001616 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001617 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001618 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001619 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001620 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001621 }
David Benjamin8d606e32017-06-15 22:43:04 -04001622 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminebad5082020-02-03 19:32:19 -05001623 if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001624 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001625 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001626 }
David Benjamin45738dd2017-02-09 20:01:26 -05001627 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001628 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001629 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001630 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001631 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001632 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001633 }
David Benjamin854dd652014-08-26 00:32:30 -04001634
David Benjaminc11ea9422017-08-29 16:33:21 -04001635 // Parse and verify the signature.
Adam Langleyfcf25832014-12-18 17:42:32 -08001636 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1637 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001638 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001639 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001640 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 }
Adam Langley95c29f32014-06-20 12:00:00 -07001642
David Benjamin4dfd5af2019-07-19 17:34:37 -04001643 if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1644 hs->peer_pubkey.get(), hs->transcript.buffer())) {
David Benjamin3570d732015-06-29 00:28:17 -04001645 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001646 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001647 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 }
1649
David Benjaminc11ea9422017-08-29 16:33:21 -04001650 // The handshake buffer is no longer necessary, and we may hash the current
1651 // message.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001652 hs->transcript.FreeBuffer();
David Benjamin7934f082017-08-01 16:32:25 -04001653 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001654 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001655 }
1656
David Benjamin8f94c312017-08-01 17:35:55 -04001657 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001658 hs->state = state12_read_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001659 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001660}
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001662static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001663 if (hs->handback && hs->ssl->session != NULL) {
1664 return ssl_hs_handback;
1665 }
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001666 hs->state = state12_process_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001667 return ssl_hs_read_change_cipher_spec;
1668}
1669
1670static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
David Benjamine58f8a62017-09-21 19:07:15 -04001671 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001672 return ssl_hs_error;
1673 }
1674
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001675 hs->state = state12_read_next_proto;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001676 return ssl_hs_ok;
1677}
1678
1679static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001680 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001681
1682 if (!hs->next_proto_neg_seen) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001683 hs->state = state12_read_channel_id;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001684 return ssl_hs_ok;
1685 }
1686
David Benjamin7934f082017-08-01 16:32:25 -04001687 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001688 if (!ssl->method->get_message(ssl, &msg)) {
1689 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001690 }
Adam Langley95c29f32014-06-20 12:00:00 -07001691
David Benjamin7934f082017-08-01 16:32:25 -04001692 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1693 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001694 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001695 }
1696
David Benjamin7934f082017-08-01 16:32:25 -04001697 CBS next_protocol = msg.body, selected_protocol, padding;
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1699 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001700 CBS_len(&next_protocol) != 0) {
1701 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001702 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001703 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001704 }
1705
David Benjamin8e7bbba2017-10-13 17:18:35 -04001706 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001707 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001708 }
Adam Langley95c29f32014-06-20 12:00:00 -07001709
David Benjamin8f94c312017-08-01 17:35:55 -04001710 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001711 hs->state = state12_read_channel_id;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001712 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001713}
Adam Langley95c29f32014-06-20 12:00:00 -07001714
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001715static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001716 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001717
David Benjamin8acec002021-05-19 13:03:34 -04001718 if (!hs->channel_id_negotiated) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001719 hs->state = state12_read_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001720 return ssl_hs_ok;
1721 }
1722
David Benjamin7934f082017-08-01 16:32:25 -04001723 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001724 if (!ssl->method->get_message(ssl, &msg)) {
1725 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001727
David Benjamin7934f082017-08-01 16:32:25 -04001728 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1729 !tls1_verify_channel_id(hs, msg) ||
1730 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001731 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001733
David Benjamin8f94c312017-08-01 17:35:55 -04001734 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001735 hs->state = state12_read_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001736 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001737}
David Benjamin9f1dc822016-06-07 17:03:46 -04001738
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001739static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1740 SSL *const ssl = hs->ssl;
1741 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1742 if (wait != ssl_hs_ok) {
1743 return wait;
1744 }
1745
1746 if (ssl->session != NULL) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001747 hs->state = state12_finish_server_handshake;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001748 } else {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001749 hs->state = state12_send_server_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001750 }
1751
David Benjaminc11ea9422017-08-29 16:33:21 -04001752 // If this is a full handshake with ChannelID then record the handshake
1753 // hashes in |hs->new_session| in case we need them to verify a
1754 // ChannelID signature on a resumption of this session in the future.
David Benjamin46853762018-07-03 14:01:26 -04001755 if (ssl->session == NULL && ssl->s3->channel_id_valid &&
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001756 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1757 return ssl_hs_error;
1758 }
1759
1760 return ssl_hs_ok;
1761}
1762
1763static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001764 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001765
1766 if (hs->ticket_expected) {
1767 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001768 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001769 if (ssl->session == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001770 // Fix the timeout to measure from the ticket issuance time.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001771 ssl_session_rebase_time(ssl, hs->new_session.get());
1772 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001773 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001774 // We are renewing an existing session. Duplicate the session to adjust
1775 // the timeout.
David Benjamin50596f82018-07-02 19:47:27 -04001776 session_copy =
1777 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001778 if (!session_copy) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001779 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001780 }
1781
David Benjamin1386aad2017-07-19 23:57:40 -04001782 ssl_session_rebase_time(ssl, session_copy.get());
1783 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001784 }
1785
David Benjamin1386aad2017-07-19 23:57:40 -04001786 ScopedCBB cbb;
1787 CBB body, ticket;
1788 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1789 SSL3_MT_NEW_SESSION_TICKET) ||
1790 !CBB_add_u32(&body, session->timeout) ||
1791 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001792 !ssl_encrypt_ticket(hs, &ticket, session) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001793 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001794 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001795 }
David Benjamin123db572016-11-03 16:59:25 -04001796 }
1797
David Benjaminb5f55c32017-06-15 23:15:15 -04001798 if (!ssl->method->add_change_cipher_spec(ssl) ||
David Benjamine58f8a62017-09-21 19:07:15 -04001799 !tls1_change_cipher_state(hs, evp_aead_seal) ||
David Benjamin00f48c82017-10-06 18:43:53 -04001800 !ssl_send_finished(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001801 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001802 }
1803
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001804 if (ssl->session != NULL) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001805 hs->state = state12_read_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001806 } else {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001807 hs->state = state12_finish_server_handshake;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001808 }
1809 return ssl_hs_flush;
David Benjamin9f1dc822016-06-07 17:03:46 -04001810}
David Benjamin86e95b82017-07-18 16:34:25 -04001811
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001812static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1813 SSL *const ssl = hs->ssl;
1814
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001815 if (hs->handback) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001816 return ssl_hs_handback;
1817 }
1818
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001819 ssl->method->on_handshake_complete(ssl);
1820
David Benjaminc11ea9422017-08-29 16:33:21 -04001821 // If we aren't retaining peer certificates then we can discard it now.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001822 if (hs->new_session != NULL &&
1823 hs->config->retain_only_sha256_of_client_certs) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001824 hs->new_session->certs.reset();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001825 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1826 }
1827
David Benjamina10017c2021-06-16 16:00:13 -04001828 bool has_new_session = hs->new_session != nullptr;
1829 if (has_new_session) {
David Benjamin10a76ac2021-06-16 11:33:37 -04001830 assert(ssl->session == nullptr);
David Benjamin8e7bbba2017-10-13 17:18:35 -04001831 ssl->s3->established_session = std::move(hs->new_session);
David Benjamina3a71e92018-06-29 13:24:45 -04001832 ssl->s3->established_session->not_resumable = false;
David Benjamina10017c2021-06-16 16:00:13 -04001833 } else {
1834 assert(ssl->session != nullptr);
1835 ssl->s3->established_session = UpRef(ssl->session);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001836 }
1837
David Benjaminfd45ee72017-08-31 14:49:09 -04001838 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001839 ssl->s3->initial_handshake_complete = true;
David Benjamina10017c2021-06-16 16:00:13 -04001840 if (has_new_session) {
1841 ssl_update_cache(ssl);
1842 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001843
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001844 hs->state = state12_done;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001845 return ssl_hs_ok;
1846}
1847
1848enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001849 while (hs->state != state12_done) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001850 enum ssl_hs_wait_t ret = ssl_hs_error;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001851 enum tls12_server_hs_state_t state =
1852 static_cast<enum tls12_server_hs_state_t>(hs->state);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001853 switch (state) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001854 case state12_start_accept:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001855 ret = do_start_accept(hs);
1856 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001857 case state12_read_client_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001858 ret = do_read_client_hello(hs);
1859 break;
Daniel McArdle00e434d2021-02-18 11:47:18 -05001860 case state12_read_client_hello_after_ech:
1861 ret = do_read_client_hello_after_ech(hs);
1862 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001863 case state12_select_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001864 ret = do_select_certificate(hs);
1865 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001866 case state12_tls13:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001867 ret = do_tls13(hs);
1868 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001869 case state12_select_parameters:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001870 ret = do_select_parameters(hs);
1871 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001872 case state12_send_server_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001873 ret = do_send_server_hello(hs);
1874 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001875 case state12_send_server_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001876 ret = do_send_server_certificate(hs);
1877 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001878 case state12_send_server_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001879 ret = do_send_server_key_exchange(hs);
1880 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001881 case state12_send_server_hello_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001882 ret = do_send_server_hello_done(hs);
1883 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001884 case state12_read_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001885 ret = do_read_client_certificate(hs);
1886 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001887 case state12_verify_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001888 ret = do_verify_client_certificate(hs);
1889 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001890 case state12_read_client_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001891 ret = do_read_client_key_exchange(hs);
1892 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001893 case state12_read_client_certificate_verify:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001894 ret = do_read_client_certificate_verify(hs);
1895 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001896 case state12_read_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001897 ret = do_read_change_cipher_spec(hs);
1898 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001899 case state12_process_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001900 ret = do_process_change_cipher_spec(hs);
1901 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001902 case state12_read_next_proto:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001903 ret = do_read_next_proto(hs);
1904 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001905 case state12_read_channel_id:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001906 ret = do_read_channel_id(hs);
1907 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001908 case state12_read_client_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001909 ret = do_read_client_finished(hs);
1910 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001911 case state12_send_server_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001912 ret = do_send_server_finished(hs);
1913 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001914 case state12_finish_server_handshake:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001915 ret = do_finish_server_handshake(hs);
1916 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001917 case state12_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001918 ret = ssl_hs_ok;
1919 break;
1920 }
1921
1922 if (hs->state != state) {
1923 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1924 }
1925
1926 if (ret != ssl_hs_ok) {
1927 return ret;
1928 }
1929 }
1930
1931 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1932 return ssl_hs_ok;
1933}
1934
1935const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001936 enum tls12_server_hs_state_t state =
1937 static_cast<enum tls12_server_hs_state_t>(hs->state);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001938 switch (state) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001939 case state12_start_accept:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001940 return "TLS server start_accept";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001941 case state12_read_client_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001942 return "TLS server read_client_hello";
Daniel McArdle00e434d2021-02-18 11:47:18 -05001943 case state12_read_client_hello_after_ech:
1944 return "TLS server read_client_hello_after_ech";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001945 case state12_select_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001946 return "TLS server select_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001947 case state12_tls13:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001948 return tls13_server_handshake_state(hs);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001949 case state12_select_parameters:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001950 return "TLS server select_parameters";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001951 case state12_send_server_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001952 return "TLS server send_server_hello";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001953 case state12_send_server_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001954 return "TLS server send_server_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001955 case state12_send_server_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001956 return "TLS server send_server_key_exchange";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001957 case state12_send_server_hello_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001958 return "TLS server send_server_hello_done";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001959 case state12_read_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001960 return "TLS server read_client_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001961 case state12_verify_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001962 return "TLS server verify_client_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001963 case state12_read_client_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001964 return "TLS server read_client_key_exchange";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001965 case state12_read_client_certificate_verify:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001966 return "TLS server read_client_certificate_verify";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001967 case state12_read_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001968 return "TLS server read_change_cipher_spec";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001969 case state12_process_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001970 return "TLS server process_change_cipher_spec";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001971 case state12_read_next_proto:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001972 return "TLS server read_next_proto";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001973 case state12_read_channel_id:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001974 return "TLS server read_channel_id";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001975 case state12_read_client_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001976 return "TLS server read_client_finished";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001977 case state12_send_server_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001978 return "TLS server send_server_finished";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001979 case state12_finish_server_handshake:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001980 return "TLS server finish_server_handshake";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001981 case state12_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001982 return "TLS server done";
1983 }
1984
1985 return "TLS server unknown";
1986}
1987
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07001988BSSL_NAMESPACE_END