blob: 7c84ef8324d6cf864f4bd488d3261ba2dc3ec08b [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 Benjamin90f0f052024-02-12 14:48:31 -0500306 uint16_t unused;
307 bool sign_ok = tls1_choose_signature_algorithm(hs, &unused);
308 ERR_clear_error();
309
310 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get(), sign_ok);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400311 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin2578b292016-12-03 23:19:55 -0500312 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500313 }
314 }
315
David Benjaminc11ea9422017-08-29 16:33:21 -0400316 // Check for a shared group to consider ECDHE ciphers.
David Benjamin2578b292016-12-03 23:19:55 -0500317 uint16_t unused;
318 if (tls1_get_shared_group(hs, &unused)) {
319 mask_k |= SSL_kECDHE;
320 }
321
David Benjaminc11ea9422017-08-29 16:33:21 -0400322 // PSK requires a server callback.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700323 if (hs->config->psk_server_callback != NULL) {
David Benjamin2578b292016-12-03 23:19:55 -0500324 mask_k |= SSL_kPSK;
325 mask_a |= SSL_aPSK;
326 }
327
328 *out_mask_k = mask_k;
329 *out_mask_a = mask_a;
330}
331
David Benjaminf6cc8dd2020-02-09 17:45:04 -0500332static const SSL_CIPHER *choose_cipher(
David Benjamin2578b292016-12-03 23:19:55 -0500333 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
David Benjamin48b276d2018-04-12 17:37:32 -0400334 const SSLCipherPreferenceList *server_pref) {
David Benjamin2578b292016-12-03 23:19:55 -0500335 SSL *const ssl = hs->ssl;
Steven Valdezacddb8c2018-04-11 12:58:27 -0400336 const STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjaminc11ea9422017-08-29 16:33:21 -0400337 // in_group_flags will either be NULL, or will point to an array of bytes
338 // which indicate equal-preference groups in the |prio| stack. See the
David Benjamin48b276d2018-04-12 17:37:32 -0400339 // comment about |in_group_flags| in the |SSLCipherPreferenceList|
David Benjaminc11ea9422017-08-29 16:33:21 -0400340 // struct.
David Benjamin48b276d2018-04-12 17:37:32 -0400341 const bool *in_group_flags;
David Benjaminc11ea9422017-08-29 16:33:21 -0400342 // group_min contains the minimal index so far found in a group, or -1 if no
343 // such value exists yet.
David Benjamin2578b292016-12-03 23:19:55 -0500344 int group_min = -1;
345
David Benjaminee910bf2017-07-25 22:36:00 -0400346 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
David Benjaminccbb1652017-03-22 18:24:32 -0400347 ssl_parse_client_cipher_list(client_hello);
David Benjaminee910bf2017-07-25 22:36:00 -0400348 if (!client_pref) {
349 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500350 }
351
352 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjamin48b276d2018-04-12 17:37:32 -0400353 prio = server_pref->ciphers.get();
David Benjamin2578b292016-12-03 23:19:55 -0500354 in_group_flags = server_pref->in_group_flags;
David Benjaminee910bf2017-07-25 22:36:00 -0400355 allow = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500356 } else {
David Benjaminee910bf2017-07-25 22:36:00 -0400357 prio = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500358 in_group_flags = NULL;
David Benjamin48b276d2018-04-12 17:37:32 -0400359 allow = server_pref->ciphers.get();
David Benjamin2578b292016-12-03 23:19:55 -0500360 }
361
David Benjaminccbb1652017-03-22 18:24:32 -0400362 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -0500363 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
364
365 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400366 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500367
David Benjaminccbb1652017-03-22 18:24:32 -0400368 size_t cipher_index;
David Benjaminc11ea9422017-08-29 16:33:21 -0400369 if (// Check if the cipher is supported for the current version.
David Benjamind1e3ce12017-10-06 18:31:15 -0400370 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
371 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400372 // Check the cipher is supported for the server configuration.
David Benjaminccbb1652017-03-22 18:24:32 -0400373 (c->algorithm_mkey & mask_k) &&
374 (c->algorithm_auth & mask_a) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400375 // Check the cipher is in the |allow| list.
David Benjaminccbb1652017-03-22 18:24:32 -0400376 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin48b276d2018-04-12 17:37:32 -0400377 if (in_group_flags != NULL && in_group_flags[i]) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400378 // This element of |prio| is in a group. Update the minimum index found
379 // so far and continue looking.
David Benjamin2578b292016-12-03 23:19:55 -0500380 if (group_min == -1 || (size_t)group_min > cipher_index) {
381 group_min = cipher_index;
382 }
383 } else {
384 if (group_min != -1 && (size_t)group_min < cipher_index) {
385 cipher_index = group_min;
386 }
David Benjaminee910bf2017-07-25 22:36:00 -0400387 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin2578b292016-12-03 23:19:55 -0500388 }
389 }
390
David Benjamin48b276d2018-04-12 17:37:32 -0400391 if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400392 // We are about to leave a group, but we found a match in it, so that's
393 // our answer.
David Benjaminee910bf2017-07-25 22:36:00 -0400394 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin2578b292016-12-03 23:19:55 -0500395 }
396 }
397
David Benjaminee910bf2017-07-25 22:36:00 -0400398 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500399}
400
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400401static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
402 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700403 hs->state = state12_read_client_hello;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400404 return ssl_hs_ok;
405}
406
David Benjamin6965d252018-11-19 15:49:56 -0600407// is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent
David Benjaminfa81cc62019-01-16 18:11:46 -0600408// from a JDK 11 client with both TLS 1.3 and a prior version enabled.
David Benjamin6965d252018-11-19 15:49:56 -0600409static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) {
410 // JDK 11 ClientHellos contain a number of unusual properties which should
411 // limit false positives.
412
413 // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern
414 // clients implement ChaCha20-Poly1305.
415 if (ssl_client_cipher_list_contains_cipher(
David Benjamindfddbc42022-07-18 16:08:06 -0400416 client_hello, TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamin6965d252018-11-19 15:49:56 -0600417 return false;
418 }
419
420 // JDK 11 always sends extensions in a particular order.
421 constexpr uint16_t kMaxFragmentLength = 0x0001;
422 constexpr uint16_t kStatusRequestV2 = 0x0011;
David Benjamin21440762022-03-19 18:39:18 -0400423 static constexpr struct {
David Benjamin6965d252018-11-19 15:49:56 -0600424 uint16_t id;
425 bool required;
426 } kJavaExtensions[] = {
427 {TLSEXT_TYPE_server_name, false},
428 {kMaxFragmentLength, false},
429 {TLSEXT_TYPE_status_request, false},
430 {TLSEXT_TYPE_supported_groups, true},
431 {TLSEXT_TYPE_ec_point_formats, false},
432 {TLSEXT_TYPE_signature_algorithms, true},
433 // Java always sends signature_algorithms_cert.
434 {TLSEXT_TYPE_signature_algorithms_cert, true},
435 {TLSEXT_TYPE_application_layer_protocol_negotiation, false},
436 {kStatusRequestV2, false},
437 {TLSEXT_TYPE_extended_master_secret, false},
438 {TLSEXT_TYPE_supported_versions, true},
439 {TLSEXT_TYPE_cookie, false},
440 {TLSEXT_TYPE_psk_key_exchange_modes, true},
441 {TLSEXT_TYPE_key_share, true},
442 {TLSEXT_TYPE_renegotiate, false},
443 {TLSEXT_TYPE_pre_shared_key, false},
444 };
445 Span<const uint8_t> sigalgs, sigalgs_cert;
446 bool has_status_request = false, has_status_request_v2 = false;
447 CBS extensions, supported_groups;
448 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
449 for (const auto &java_extension : kJavaExtensions) {
450 CBS copy = extensions;
451 uint16_t id;
452 if (CBS_get_u16(&copy, &id) && id == java_extension.id) {
453 // The next extension is the one we expected.
454 extensions = copy;
455 CBS body;
456 if (!CBS_get_u16_length_prefixed(&extensions, &body)) {
457 return false;
458 }
459 switch (id) {
460 case TLSEXT_TYPE_status_request:
461 has_status_request = true;
462 break;
463 case kStatusRequestV2:
464 has_status_request_v2 = true;
465 break;
466 case TLSEXT_TYPE_signature_algorithms:
467 sigalgs = body;
468 break;
469 case TLSEXT_TYPE_signature_algorithms_cert:
470 sigalgs_cert = body;
471 break;
472 case TLSEXT_TYPE_supported_groups:
473 supported_groups = body;
474 break;
475 }
476 } else if (java_extension.required) {
477 return false;
478 }
479 }
480 if (CBS_len(&extensions) != 0) {
481 return false;
482 }
483
484 // JDK 11 never advertises X25519. It is not offered by default, and
485 // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern
486 // clients implement X25519.
487 while (CBS_len(&supported_groups) > 0) {
488 uint16_t group;
489 if (!CBS_get_u16(&supported_groups, &group) ||
David Benjamin335523a2023-05-26 21:55:56 -0400490 group == SSL_GROUP_X25519) {
David Benjamin6965d252018-11-19 15:49:56 -0600491 return false;
492 }
493 }
494
495 if (// JDK 11 always sends the same contents in signature_algorithms and
496 // signature_algorithms_cert. This is unusual: signature_algorithms_cert,
497 // if omitted, is treated as if it were signature_algorithms.
498 sigalgs != sigalgs_cert ||
499 // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it
500 // sends status_request. This is unusual: status_request_v2 is not widely
501 // implemented.
502 has_status_request != has_status_request_v2) {
503 return false;
504 }
505
506 return true;
507}
508
David Benjamin18b68362021-06-18 23:13:46 -0400509static bool decrypt_ech(SSL_HANDSHAKE *hs, uint8_t *out_alert,
510 const SSL_CLIENT_HELLO *client_hello) {
511 SSL *const ssl = hs->ssl;
512 CBS body;
513 if (!ssl_client_hello_get_extension(client_hello, &body,
514 TLSEXT_TYPE_encrypted_client_hello)) {
515 return true;
516 }
517 uint8_t type;
518 if (!CBS_get_u8(&body, &type)) {
519 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
520 *out_alert = SSL_AD_DECODE_ERROR;
521 return false;
522 }
523 if (type != ECH_CLIENT_OUTER) {
524 return true;
525 }
526 // This is a ClientHelloOuter ECH extension. Attempt to decrypt it.
527 uint8_t config_id;
528 uint16_t kdf_id, aead_id;
529 CBS enc, payload;
530 if (!CBS_get_u16(&body, &kdf_id) || //
531 !CBS_get_u16(&body, &aead_id) || //
532 !CBS_get_u8(&body, &config_id) ||
533 !CBS_get_u16_length_prefixed(&body, &enc) ||
534 !CBS_get_u16_length_prefixed(&body, &payload) || //
535 CBS_len(&body) != 0) {
536 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
537 *out_alert = SSL_AD_DECODE_ERROR;
538 return false;
539 }
540
541 {
542 MutexReadLock lock(&ssl->ctx->lock);
543 hs->ech_keys = UpRef(ssl->ctx->ech_keys);
544 }
545
546 if (!hs->ech_keys) {
547 ssl->s3->ech_status = ssl_ech_rejected;
548 return true;
549 }
550
551 for (const auto &config : hs->ech_keys->configs) {
552 hs->ech_hpke_ctx.Reset();
553 if (config_id != config->ech_config().config_id ||
554 !config->SetupContext(hs->ech_hpke_ctx.get(), kdf_id, aead_id, enc)) {
555 // Ignore the error and try another ECHConfig.
556 ERR_clear_error();
557 continue;
558 }
David Benjamin18b68362021-06-18 23:13:46 -0400559 bool is_decrypt_error;
David Benjamin44425dd2022-01-27 12:22:42 -0500560 if (!ssl_client_hello_decrypt(hs, out_alert, &is_decrypt_error,
561 &hs->ech_client_hello_buf, client_hello,
562 payload)) {
David Benjamin18b68362021-06-18 23:13:46 -0400563 if (is_decrypt_error) {
564 // Ignore the error and try another ECHConfig.
565 ERR_clear_error();
David Benjamin44425dd2022-01-27 12:22:42 -0500566 // The |out_alert| calling convention currently relies on a default of
567 // |SSL_AD_DECODE_ERROR|. https://crbug.com/boringssl/373 tracks
568 // switching to sum types, which avoids this.
569 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin18b68362021-06-18 23:13:46 -0400570 continue;
571 }
572 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
573 return false;
574 }
David Benjamin18b68362021-06-18 23:13:46 -0400575 hs->ech_config_id = config_id;
576 ssl->s3->ech_status = ssl_ech_accepted;
577 return true;
578 }
579
580 // If we did not accept ECH, proceed with the ClientHelloOuter. Note this
581 // could be key mismatch or ECH GREASE, so we must complete the handshake
582 // as usual, except EncryptedExtensions will contain retry configs.
583 ssl->s3->ech_status = ssl_ech_rejected;
584 return true;
585}
586
David Benjaminef0183c2019-07-20 09:11:05 -0400587static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert,
588 const SSL_CLIENT_HELLO *client_hello) {
589 SSL *const ssl = hs->ssl;
590 CBS sni;
591 if (!ssl_client_hello_get_extension(client_hello, &sni,
592 TLSEXT_TYPE_server_name)) {
593 // No SNI extension to parse.
594 return true;
595 }
596
597 CBS server_name_list, host_name;
598 uint8_t name_type;
599 if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) ||
600 !CBS_get_u8(&server_name_list, &name_type) ||
601 // Although the server_name extension was intended to be extensible to
602 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
603 // different name types will cause an error. Further, RFC 4366 originally
604 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
605 // adding new name types is no longer feasible.
606 //
607 // Act as if the extensibility does not exist to simplify parsing.
608 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
609 CBS_len(&server_name_list) != 0 ||
610 CBS_len(&sni) != 0) {
611 *out_alert = SSL_AD_DECODE_ERROR;
612 return false;
613 }
614
615 if (name_type != TLSEXT_NAMETYPE_host_name ||
616 CBS_len(&host_name) == 0 ||
617 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
618 CBS_contains_zero_byte(&host_name)) {
619 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
620 return false;
621 }
622
623 // Copy the hostname as a string.
624 char *raw = nullptr;
625 if (!CBS_strdup(&host_name, &raw)) {
626 *out_alert = SSL_AD_INTERNAL_ERROR;
627 return false;
628 }
629 ssl->s3->hostname.reset(raw);
630
631 hs->should_ack_sni = true;
632 return true;
633}
634
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400635static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900636 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400637
David Benjamin7934f082017-08-01 16:32:25 -0400638 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400639 if (!ssl->method->get_message(ssl, &msg)) {
640 return ssl_hs_read_message;
David Benjamin7934f082017-08-01 16:32:25 -0400641 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400642
David Benjamin7934f082017-08-01 16:32:25 -0400643 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400644 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 }
Adam Langley95c29f32014-06-20 12:00:00 -0700646
David Benjamin731058e2016-12-03 23:15:13 -0500647 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500648 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
David Benjamin3570d732015-06-29 00:28:17 -0400649 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400650 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400651 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 }
Adam Langley95c29f32014-06-20 12:00:00 -0700653
David Benjaminf9cc26f2020-02-09 16:49:31 -0500654 // ClientHello should be the end of the flight. We check this early to cover
655 // all protocol versions.
656 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
657 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
658 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
659 return ssl_hs_error;
660 }
661
Adam Langleyc9827e02019-04-12 14:46:50 -0700662 if (hs->config->handoff) {
663 return ssl_hs_handoff;
664 }
665
David Benjamin18b68362021-06-18 23:13:46 -0400666 uint8_t alert = SSL_AD_DECODE_ERROR;
667 if (!decrypt_ech(hs, &alert, &client_hello)) {
668 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
669 return ssl_hs_error;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500670 }
671
David Benjamin18b68362021-06-18 23:13:46 -0400672 // ECH may have changed which ClientHello we process. Update |msg| and
673 // |client_hello| in case.
674 if (!hs->GetClientHello(&msg, &client_hello)) {
675 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
676 return ssl_hs_error;
677 }
678
David Benjaminef0183c2019-07-20 09:11:05 -0400679 if (!extract_sni(hs, &alert, &client_hello)) {
680 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
681 return ssl_hs_error;
682 }
683
Daniel McArdle00e434d2021-02-18 11:47:18 -0500684 hs->state = state12_read_client_hello_after_ech;
685 return ssl_hs_ok;
686}
687
688static enum ssl_hs_wait_t do_read_client_hello_after_ech(SSL_HANDSHAKE *hs) {
689 SSL *const ssl = hs->ssl;
690
691 SSLMessage msg_unused;
692 SSL_CLIENT_HELLO client_hello;
693 if (!hs->GetClientHello(&msg_unused, &client_hello)) {
694 return ssl_hs_error;
695 }
696
David Benjaminc11ea9422017-08-29 16:33:21 -0400697 // Run the early callback.
David Benjamin59bae5a2017-02-02 23:51:22 -0500698 if (ssl->ctx->select_certificate_cb != NULL) {
699 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000700 case ssl_select_cert_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400701 return ssl_hs_certificate_selection_pending;
David Benjamine14ff062016-08-09 16:21:24 -0400702
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000703 case ssl_select_cert_error:
David Benjaminc11ea9422017-08-29 16:33:21 -0400704 // Connection rejected.
David Benjamin59bae5a2017-02-02 23:51:22 -0500705 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400706 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400707 return ssl_hs_error;
David Benjamine14ff062016-08-09 16:21:24 -0400708
David Benjamin59bae5a2017-02-02 23:51:22 -0500709 default:
710 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400711 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500712 }
David Benjamine14ff062016-08-09 16:21:24 -0400713
David Benjaminc11ea9422017-08-29 16:33:21 -0400714 // Freeze the version range after the early callback.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700715 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400716 return ssl_hs_error;
David Benjamin68161cb2017-06-20 14:49:43 -0400717 }
718
David Benjamin6965d252018-11-19 15:49:56 -0600719 if (hs->config->jdk11_workaround &&
720 is_probably_jdk11_with_tls13(&client_hello)) {
721 hs->apply_jdk11_workaround = true;
722 }
723
Daniel McArdle00e434d2021-02-18 11:47:18 -0500724 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500725 if (!negotiate_version(hs, &alert, &client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400726 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400727 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500728 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400729
Steven Valdez8f36c512017-06-20 10:55:02 -0400730 hs->client_version = client_hello.version;
David Benjamin59bae5a2017-02-02 23:51:22 -0500731 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
732 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400733 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500734 }
735 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
736 client_hello.random_len);
737
David Benjaminc11ea9422017-08-29 16:33:21 -0400738 // Only null compression is supported. TLS 1.3 further requires the peer
739 // advertise no other compression.
David Benjamin59bae5a2017-02-02 23:51:22 -0500740 if (OPENSSL_memchr(client_hello.compression_methods, 0,
741 client_hello.compression_methods_len) == NULL ||
David Benjamind1e3ce12017-10-06 18:31:15 -0400742 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
David Benjamin59bae5a2017-02-02 23:51:22 -0500743 client_hello.compression_methods_len != 1)) {
744 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
David Benjamind1e3ce12017-10-06 18:31:15 -0400745 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400746 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500747 }
748
David Benjaminc11ea9422017-08-29 16:33:21 -0400749 // TLS extensions.
David Benjamin59bae5a2017-02-02 23:51:22 -0500750 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
751 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400752 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500753 }
754
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700755 hs->state = state12_select_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400756 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500757}
758
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400759static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500760 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400761
David Benjaminc11ea9422017-08-29 16:33:21 -0400762 // Call |cert_cb| to update server certificates if required.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700763 if (hs->config->cert->cert_cb != NULL) {
764 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
David Benjamin59bae5a2017-02-02 23:51:22 -0500765 if (rv == 0) {
766 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400767 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400768 return ssl_hs_error;
David Benjamin25fe85b2016-08-09 20:00:32 -0400769 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500770 if (rv < 0) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400771 return ssl_hs_x509_lookup;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900772 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900773 }
774
David Benjamina232a712017-03-30 15:51:53 -0500775 if (!ssl_on_certificate_selected(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400776 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900777 }
778
David Benjamin103ed082018-05-10 19:55:02 -0400779 if (hs->ocsp_stapling_requested &&
780 ssl->ctx->legacy_ocsp_callback != nullptr) {
781 switch (ssl->ctx->legacy_ocsp_callback(
782 ssl, ssl->ctx->legacy_ocsp_callback_arg)) {
783 case SSL_TLSEXT_ERR_OK:
784 break;
785 case SSL_TLSEXT_ERR_NOACK:
786 hs->ocsp_stapling_requested = false;
787 break;
788 default:
789 OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR);
790 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
791 return ssl_hs_error;
792 }
793 }
794
David Benjamind1e3ce12017-10-06 18:31:15 -0400795 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400796 // Jump to the TLS 1.3 state machine.
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700797 hs->state = state12_tls13;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400798 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500799 }
800
Daniel McArdle00e434d2021-02-18 11:47:18 -0500801 // It should not be possible to negotiate TLS 1.2 with ECH. The
802 // ClientHelloInner decoding function rejects ClientHellos which offer TLS 1.2
803 // or below.
David Benjaminba423c92021-06-15 16:26:58 -0400804 assert(ssl->s3->ech_status != ssl_ech_accepted);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500805
David Benjamin64770122019-05-04 11:00:04 -0500806 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
807
Daniel McArdle00e434d2021-02-18 11:47:18 -0500808 SSLMessage msg_unused;
David Benjamin59bae5a2017-02-02 23:51:22 -0500809 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500810 if (!hs->GetClientHello(&msg_unused, &client_hello)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400811 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500812 }
813
David Benjaminc11ea9422017-08-29 16:33:21 -0400814 // Negotiate the cipher suite. This must be done after |cert_cb| so the
815 // certificate is finalized.
David Benjamin0ce090a2018-07-02 20:24:40 -0400816 SSLCipherPreferenceList *prefs = hs->config->cipher_list
817 ? hs->config->cipher_list.get()
818 : ssl->ctx->cipher_list.get();
David Benjaminf6cc8dd2020-02-09 17:45:04 -0500819 hs->new_cipher = choose_cipher(hs, &client_hello, prefs);
David Benjamin45738dd2017-02-09 20:01:26 -0500820 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500821 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
David Benjamind1e3ce12017-10-06 18:31:15 -0400822 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400823 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500824 }
825
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700826 hs->state = state12_select_parameters;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400827 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500828}
829
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400830static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
831 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
832 if (wait == ssl_hs_ok) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700833 hs->state = state12_finish_server_handshake;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400834 return ssl_hs_ok;
David Benjamin7934f082017-08-01 16:32:25 -0400835 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400836
837 return wait;
838}
839
840static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
841 SSL *const ssl = hs->ssl;
842
843 SSLMessage msg;
844 if (!ssl->method->get_message(ssl, &msg)) {
845 return ssl_hs_read_message;
846 }
847
David Benjamin59bae5a2017-02-02 23:51:22 -0500848 SSL_CLIENT_HELLO client_hello;
Daniel McArdle00e434d2021-02-18 11:47:18 -0500849 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400850 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500851 }
David Benjamin34202b92016-11-16 19:07:53 +0900852
Adam Langley47cefed2021-05-26 13:36:40 -0700853 hs->session_id_len = client_hello.session_id_len;
854 // This is checked in |ssl_client_hello_init|.
855 assert(hs->session_id_len <= sizeof(hs->session_id));
856 OPENSSL_memcpy(hs->session_id, client_hello.session_id, hs->session_id_len);
857
David Benjaminc11ea9422017-08-29 16:33:21 -0400858 // Determine whether we are doing session resumption.
David Benjamin37af90f2017-07-29 01:42:16 -0400859 UniquePtr<SSL_SESSION> session;
David Benjaminfd45ee72017-08-31 14:49:09 -0400860 bool tickets_supported = false, renew_ticket = false;
Steven Valdez398085b2017-08-29 13:25:56 -0400861 enum ssl_hs_wait_t wait = ssl_get_prev_session(
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700862 hs, &session, &tickets_supported, &renew_ticket, &client_hello);
Steven Valdez398085b2017-08-29 13:25:56 -0400863 if (wait != ssl_hs_ok) {
864 return wait;
David Benjamin34202b92016-11-16 19:07:53 +0900865 }
866
David Benjamind781fc42017-07-12 16:25:57 -0400867 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500868 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400869 // A ClientHello without EMS that attempts to resume a session with EMS
870 // is fatal to the connection.
David Benjamin34202b92016-11-16 19:07:53 +0900871 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind1e3ce12017-10-06 18:31:15 -0400872 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400873 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900874 }
875
David Benjamind781fc42017-07-12 16:25:57 -0400876 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400877 // If the client offers the EMS extension, but the previous session
878 // didn't use it, then negotiate a new session.
David Benjaminfc02b592017-02-17 16:26:01 -0500879 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400880 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900881 }
882 }
883
David Benjamind781fc42017-07-12 16:25:57 -0400884 if (session) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400885 // Use the old session.
David Benjaminc3c88822016-11-14 10:32:04 +0900886 hs->ticket_expected = renew_ticket;
David Benjamin50596f82018-07-02 19:47:27 -0400887 ssl->session = std::move(session);
David Benjamin046bc1f2017-08-31 15:06:42 -0400888 ssl->s3->session_reused = true;
David Benjamin9b2cdb72021-04-01 23:21:53 -0400889 hs->can_release_private_key = true;
David Benjamin34202b92016-11-16 19:07:53 +0900890 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900891 hs->ticket_expected = tickets_supported;
David Benjamin962b3752021-05-10 15:17:18 -0400892 ssl_set_session(ssl, nullptr);
893 if (!ssl_get_new_session(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400894 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900895 }
896
David Benjamin962b3752021-05-10 15:17:18 -0400897 // Assign a session ID if not using session tickets.
898 if (!hs->ticket_expected &&
899 (ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
900 hs->new_session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
901 RAND_bytes(hs->new_session->session_id,
902 hs->new_session->session_id_length);
David Benjamin34202b92016-11-16 19:07:53 +0900903 }
904 }
905
906 if (ssl->ctx->dos_protection_cb != NULL &&
907 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400908 // Connection rejected for DOS reasons.
David Benjamin34202b92016-11-16 19:07:53 +0900909 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400910 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400911 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900912 }
913
David Benjaminf01f42a2016-11-16 19:05:33 +0900914 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500915 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400916
David Benjaminc11ea9422017-08-29 16:33:21 -0400917 // Determine whether to request a client certificate.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700918 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400919 // Only request a certificate if Channel ID isn't negotiated.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700920 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
David Benjamin8acec002021-05-19 13:03:34 -0400921 hs->channel_id_negotiated) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400922 hs->cert_request = false;
David Benjamin5c1ce292015-05-26 16:59:43 -0400923 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400924 // CertificateRequest may only be sent in certificate-based ciphers.
David Benjamin45738dd2017-02-09 20:01:26 -0500925 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400926 hs->cert_request = false;
David Benjamin5c1ce292015-05-26 16:59:43 -0400927 }
Adam Langley37646832016-08-01 16:16:46 -0700928
David Benjaminc3c88822016-11-14 10:32:04 +0900929 if (!hs->cert_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400930 // OpenSSL returns X509_V_OK when no certificates are requested. This is
931 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -0500932 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700933 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 }
935
David Benjaminc11ea9422017-08-29 16:33:21 -0400936 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
937 // deferred. Complete it now.
David Benjamind98107b2017-06-15 22:56:37 -0400938 uint8_t alert = SSL_AD_DECODE_ERROR;
939 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400940 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400941 return ssl_hs_error;
David Benjamin9ef31f02016-10-31 18:01:13 -0400942 }
943
David Benjaminc11ea9422017-08-29 16:33:21 -0400944 // Now that all parameters are known, initialize the handshake hash and hash
945 // the ClientHello.
David Benjamind1e3ce12017-10-06 18:31:15 -0400946 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400947 !ssl_hash_message(hs, msg)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400948 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400949 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400950 }
951
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700952 // Handback includes the whole handshake transcript, so we cannot free the
953 // transcript buffer in the handback case.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700954 if (!hs->cert_request && !hs->handback) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400955 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 }
957
David Benjamin8f94c312017-08-01 17:35:55 -0400958 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400959
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700960 hs->state = state12_send_server_hello;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400961 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800962}
Adam Langley95c29f32014-06-20 12:00:00 -0700963
David Benjamin6965d252018-11-19 15:49:56 -0600964static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) {
David Benjamin006f20a2021-06-22 23:00:49 -0400965 out = out.last(in.size());
David Benjamin6965d252018-11-19 15:49:56 -0600966 OPENSSL_memcpy(out.data(), in.data(), in.size());
967}
968
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400969static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900970 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400971
David Benjaminc11ea9422017-08-29 16:33:21 -0400972 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
973 // known attack while we fix ChannelID itself.
David Benjamin8acec002021-05-19 13:03:34 -0400974 if (hs->channel_id_negotiated &&
David Benjamin45738dd2017-02-09 20:01:26 -0500975 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin8acec002021-05-19 13:03:34 -0400976 hs->channel_id_negotiated = false;
David Benjamin56380462015-10-10 14:59:09 -0400977 }
978
David Benjaminc11ea9422017-08-29 16:33:21 -0400979 // If this is a resumption and the original handshake didn't support
980 // ChannelID then we didn't record the original handshake hashes in the
981 // session and so cannot resume with ChannelIDs.
Steven Valdez87eab492016-06-27 16:34:59 -0400982 if (ssl->session != NULL &&
983 ssl->session->original_handshake_hash_len == 0) {
David Benjamin8acec002021-05-19 13:03:34 -0400984 hs->channel_id_negotiated = false;
David Benjamin56380462015-10-10 14:59:09 -0400985 }
986
David Benjamin4a6c8fd2022-07-21 14:05:41 -0700987 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
988 if (hints && !hs->hints_requested &&
989 hints->server_random_tls12.size() == SSL3_RANDOM_SIZE) {
990 OPENSSL_memcpy(ssl->s3->server_random, hints->server_random_tls12.data(),
991 SSL3_RANDOM_SIZE);
992 } else {
993 struct OPENSSL_timeval now;
994 ssl_get_current_time(ssl, &now);
995 CRYPTO_store_u32_be(ssl->s3->server_random,
996 static_cast<uint32_t>(now.tv_sec));
997 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
998 return ssl_hs_error;
999 }
1000 if (hints && hs->hints_requested &&
1001 !hints->server_random_tls12.CopyFrom(ssl->s3->server_random)) {
1002 return ssl_hs_error;
1003 }
David Benjamin56380462015-10-10 14:59:09 -04001004 }
1005
Steven Valdezf1af1292018-08-13 10:54:48 -04001006 // Implement the TLS 1.3 anti-downgrade feature.
1007 if (ssl_supports_version(hs, TLS1_3_VERSION)) {
1008 if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
David Benjamin6965d252018-11-19 15:49:56 -06001009 if (hs->apply_jdk11_workaround) {
1010 // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it
1011 // here. However, the signal is only effective if all TLS 1.2
1012 // ServerHellos produced by the server are marked. Thus we send a
1013 // different non-standard signal for the time being, until JDK 11.0.2 is
1014 // released and clients have updated.
1015 copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom);
1016 } else {
1017 copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom);
1018 }
Steven Valdezf1af1292018-08-13 10:54:48 -04001019 } else {
David Benjamin6965d252018-11-19 15:49:56 -06001020 copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom);
Steven Valdezf1af1292018-08-13 10:54:48 -04001021 }
David Benjamin6df65402017-12-18 18:00:23 -05001022 }
David Benjamin1f61f0d2016-07-10 12:20:35 -04001023
Adam Langley47cefed2021-05-26 13:36:40 -07001024 Span<const uint8_t> session_id;
David Benjamin50596f82018-07-02 19:47:27 -04001025 if (ssl->session != nullptr) {
Adam Langley47cefed2021-05-26 13:36:40 -07001026 // Echo the session ID from the ClientHello to indicate resumption.
1027 session_id = MakeConstSpan(hs->session_id, hs->session_id_len);
1028 } else {
1029 session_id = MakeConstSpan(hs->new_session->session_id,
1030 hs->new_session->session_id_length);
Steven Valdez87eab492016-06-27 16:34:59 -04001031 }
1032
David Benjamin1386aad2017-07-19 23:57:40 -04001033 ScopedCBB cbb;
Adam Langley47cefed2021-05-26 13:36:40 -07001034 CBB body, session_id_bytes;
David Benjamin1386aad2017-07-19 23:57:40 -04001035 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -04001036 !CBB_add_u16(&body, ssl->version) ||
1037 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
Adam Langley47cefed2021-05-26 13:36:40 -07001038 !CBB_add_u8_length_prefixed(&body, &session_id_bytes) ||
1039 !CBB_add_bytes(&session_id_bytes, session_id.data(), session_id.size()) ||
David Benjamin3743aaf2020-09-21 13:55:16 -04001040 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -04001041 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001042 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001043 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -04001044 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001045 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -04001046 }
1047
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001048 if (ssl->session != nullptr) {
1049 // No additional hints to generate in resumption.
1050 if (hs->hints_requested) {
1051 return ssl_hs_hints_ready;
1052 }
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001053 hs->state = state12_send_server_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001054 } else {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001055 hs->state = state12_send_server_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001056 }
1057 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001058}
Adam Langley95c29f32014-06-20 12:00:00 -07001059
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001060static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001061 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001062 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -04001063
David Benjamin44148742017-06-17 13:20:59 -04001064 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Christopher Patton9cde8482018-07-17 11:36:36 -07001065 if (!ssl_has_certificate(hs)) {
David Benjamin44148742017-06-17 13:20:59 -04001066 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001067 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001068 }
David Benjamin32a66d52016-07-13 22:03:11 -04001069
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001070 if (!ssl_output_cert_chain(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001071 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001072 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001073
David Benjamin44148742017-06-17 13:20:59 -04001074 if (hs->certificate_status_expected) {
1075 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -04001076 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -04001077 SSL3_MT_CERTIFICATE_STATUS) ||
1078 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1079 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001080 !CBB_add_bytes(
1081 &ocsp_response,
1082 CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
1083 CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
David Benjamind781fc42017-07-12 16:25:57 -04001084 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -04001085 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001086 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001087 }
David Benjamin0d1730d2017-06-15 23:24:25 -04001088 }
David Benjamin75836432016-06-17 18:48:29 -04001089 }
1090
David Benjaminc11ea9422017-08-29 16:33:21 -04001091 // Assemble ServerKeyExchange parameters if needed.
David Benjamin44148742017-06-17 13:20:59 -04001092 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1093 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1094 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001095 ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001096 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
1097 // the client and server randoms for the signing transcript.
David Benjamin44148742017-06-17 13:20:59 -04001098 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -04001099 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
1100 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1101 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001102 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001103 }
1104
David Benjaminc11ea9422017-08-29 16:33:21 -04001105 // PSK ciphers begin with an identity hint.
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 if (alg_a & SSL_aPSK) {
David Benjamin0ce090a2018-07-02 20:24:40 -04001107 size_t len = hs->config->psk_identity_hint == nullptr
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001108 ? 0
David Benjamin0ce090a2018-07-02 20:24:40 -04001109 : strlen(hs->config->psk_identity_hint.get());
David Benjamind781fc42017-07-12 16:25:57 -04001110 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin0ce090a2018-07-02 20:24:40 -04001111 !CBB_add_bytes(&child,
1112 (const uint8_t *)hs->config->psk_identity_hint.get(),
David Benjamin2a0b3912015-12-18 01:01:21 -05001113 len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001114 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001116 }
Adam Langley95c29f32014-06-20 12:00:00 -07001117
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001118 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001119 // Determine the group to use.
Steven Valdezce902a92016-05-17 11:47:53 -04001120 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001121 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001122 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001123 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001124 return ssl_hs_error;
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001125 }
David Benjamin45738dd2017-02-09 20:01:26 -05001126 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langley7b935932018-11-12 13:53:42 -08001128 hs->key_shares[0] = SSLKeyShare::Create(group_id);
1129 if (!hs->key_shares[0] ||
David Benjamind781fc42017-07-12 16:25:57 -04001130 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
1131 !CBB_add_u16(cbb.get(), group_id) ||
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001132 !CBB_add_u8_length_prefixed(cbb.get(), &child)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001133 return ssl_hs_error;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001134 }
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001135
1136 SSL_HANDSHAKE_HINTS *const hints = hs->hints.get();
1137 bool hint_ok = false;
1138 if (hints && !hs->hints_requested &&
1139 hints->ecdhe_group_id == group_id &&
1140 !hints->ecdhe_public_key.empty() &&
1141 !hints->ecdhe_private_key.empty()) {
1142 CBS cbs = MakeConstSpan(hints->ecdhe_private_key);
1143 hint_ok = hs->key_shares[0]->DeserializePrivateKey(&cbs);
1144 }
1145 if (hint_ok) {
1146 // Reuse the ECDH key from handshake hints.
1147 if (!CBB_add_bytes(&child, hints->ecdhe_public_key.data(),
1148 hints->ecdhe_public_key.size())) {
1149 return ssl_hs_error;
1150 }
1151 } else {
1152 // Generate a key, and emit the public half.
David Benjamin08b1f382023-02-28 17:22:23 -05001153 if (!hs->key_shares[0]->Generate(&child)) {
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001154 return ssl_hs_error;
1155 }
1156 // If generating hints, save the ECDHE key.
1157 if (hints && hs->hints_requested) {
1158 bssl::ScopedCBB private_key_cbb;
1159 if (!hints->ecdhe_public_key.CopyFrom(
1160 MakeConstSpan(CBB_data(&child), CBB_len(&child))) ||
1161 !CBB_init(private_key_cbb.get(), 32) ||
1162 !hs->key_shares[0]->SerializePrivateKey(private_key_cbb.get()) ||
1163 !CBBFinishArray(private_key_cbb.get(),
1164 &hints->ecdhe_private_key)) {
1165 return ssl_hs_error;
1166 }
1167 hints->ecdhe_group_id = group_id;
1168 }
1169 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001170 } else {
1171 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001172 }
Adam Langley95c29f32014-06-20 12:00:00 -07001173
David Benjamin879efc32017-09-21 11:20:53 -04001174 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001175 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001176 }
David Benjaminc42acee2016-06-17 17:47:58 -04001177 }
1178
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001179 hs->state = state12_send_server_key_exchange;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001180 return ssl_hs_ok;
David Benjamin44148742017-06-17 13:20:59 -04001181}
1182
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001183static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin44148742017-06-17 13:20:59 -04001184 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001185
David Benjamin879efc32017-09-21 11:20:53 -04001186 if (hs->server_params.size() == 0) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001187 hs->state = state12_send_server_hello_done;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001188 return ssl_hs_ok;
1189 }
1190
David Benjamin1386aad2017-07-19 23:57:40 -04001191 ScopedCBB cbb;
1192 CBB body, child;
1193 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001194 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -04001195 // |hs->server_params| contains a prefix for signing.
David Benjamin879efc32017-09-21 11:20:53 -04001196 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
1197 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
1198 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001199 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -05001200 }
Adam Langley95c29f32014-06-20 12:00:00 -07001201
David Benjaminc11ea9422017-08-29 16:33:21 -04001202 // Add a signature.
David Benjamin45738dd2017-02-09 20:01:26 -05001203 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Christopher Patton9cde8482018-07-17 11:36:36 -07001204 if (!ssl_has_private_key(hs)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001205 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001206 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001207 }
Adam Langley95c29f32014-06-20 12:00:00 -07001208
David Benjaminc11ea9422017-08-29 16:33:21 -04001209 // Determine the signature algorithm.
David Benjaminea9a0d52016-07-08 15:52:59 -07001210 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001211 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Adam Langleye0afc852018-07-09 16:59:33 -07001212 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001213 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001214 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001215 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001216 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001217 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001218 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001219 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001220 }
David Benjaminc42acee2016-06-17 17:47:58 -04001221 }
1222
David Benjaminc11ea9422017-08-29 16:33:21 -04001223 // Add space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001224 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -04001225 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001226 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001227 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001228 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -04001229 }
1230
David Benjamin2a0b3912015-12-18 01:01:21 -05001231 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001232 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
David Benjamin75a1f232017-10-11 17:19:19 -04001233 signature_algorithm, hs->server_params)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001234 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001235 if (!CBB_did_write(&child, sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001236 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -05001237 }
1238 break;
1239 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001240 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -05001241 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001242 return ssl_hs_private_key_operation;
David Benjamin2a0b3912015-12-18 01:01:21 -05001243 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001244 }
1245
David Benjamin9b2cdb72021-04-01 23:21:53 -04001246 hs->can_release_private_key = true;
David Benjamin1386aad2017-07-19 23:57:40 -04001247 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001248 return ssl_hs_error;
David Benjamin1f9f9c42015-08-28 16:17:59 -04001249 }
David Benjaminc42acee2016-06-17 17:47:58 -04001250
David Benjamin879efc32017-09-21 11:20:53 -04001251 hs->server_params.Reset();
David Benjaminc42acee2016-06-17 17:47:58 -04001252
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001253 hs->state = state12_send_server_hello_done;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001254 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001255}
Adam Langley95c29f32014-06-20 12:00:00 -07001256
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001257static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001258 SSL *const ssl = hs->ssl;
David Benjamin4a6c8fd2022-07-21 14:05:41 -07001259 if (hs->hints_requested) {
1260 return ssl_hs_hints_ready;
1261 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001262
David Benjamin1386aad2017-07-19 23:57:40 -04001263 ScopedCBB cbb;
1264 CBB body;
David Benjamin75836432016-06-17 18:48:29 -04001265
David Benjamin0d1730d2017-06-15 23:24:25 -04001266 if (hs->cert_request) {
1267 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -04001268 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -04001269 SSL3_MT_CERTIFICATE_REQUEST) ||
1270 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1271 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
David Benjamin9bb15f52018-06-26 00:07:40 -04001272 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
David Benjamind1e3ce12017-10-06 18:31:15 -04001273 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
David Benjamin0d1730d2017-06-15 23:24:25 -04001274 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
David Benjaminebad5082020-02-03 19:32:19 -05001275 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001276 !ssl_add_client_CA_list(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001277 !ssl_add_message_cbb(ssl, cbb.get())) {
1278 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001279 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001280 }
1281 }
1282
David Benjamin1386aad2017-07-19 23:57:40 -04001283 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1284 SSL3_MT_SERVER_HELLO_DONE) ||
1285 !ssl_add_message_cbb(ssl, cbb.get())) {
1286 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001287 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001288 }
1289
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001290 hs->state = state12_read_client_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001291 return ssl_hs_flush;
Adam Langleyfcf25832014-12-18 17:42:32 -08001292}
Adam Langley95c29f32014-06-20 12:00:00 -07001293
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001294static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001295 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001296
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001297 if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
1298 return ssl_hs_handback;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001299 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001300 if (!hs->cert_request) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001301 hs->state = state12_verify_client_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001302 return ssl_hs_ok;
1303 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001304
David Benjamin7934f082017-08-01 16:32:25 -04001305 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001306 if (!ssl->method->get_message(ssl, &msg)) {
1307 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -04001308 }
1309
David Benjamin9bb15f52018-06-26 00:07:40 -04001310 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001311 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001312 }
1313
David Benjamin7934f082017-08-01 16:32:25 -04001314 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001315 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -05001316 }
1317
David Benjamin7934f082017-08-01 16:32:25 -04001318 CBS certificate_msg = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001319 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminbfdd1a92018-06-29 16:26:38 -04001320 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001321 hs->config->retain_only_sha256_of_client_certs
David Benjamine664a532017-07-20 20:19:36 -04001322 ? hs->new_session->peer_sha256
David Benjaminbfdd1a92018-06-29 16:26:38 -04001323 : nullptr,
David Benjamine664a532017-07-20 20:19:36 -04001324 &certificate_msg, ssl->ctx->pool)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001325 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001326 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001327 }
1328
Adam Langley68e71242016-12-12 11:06:16 -08001329 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001330 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001331 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001332 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001333 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001334 }
1335
David Benjaminbfdd1a92018-06-29 16:26:38 -04001336 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001337 // No client certificate so the handshake buffer may be discarded.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001338 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001339
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001340 if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001341 // Fail for TLS only if we required a certificate
David Benjamin9f1dc822016-06-07 17:03:46 -04001342 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001343 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001344 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001345 }
Adam Langley37646832016-08-01 16:16:46 -07001346
David Benjaminc11ea9422017-08-29 16:33:21 -04001347 // OpenSSL returns X509_V_OK when no certificates are received. This is
1348 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -05001349 hs->new_session->verify_result = X509_V_OK;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001350 } else if (hs->config->retain_only_sha256_of_client_certs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001351 // The hash will have been filled in.
Anton Bikineev50e7ea52022-01-23 22:35:48 +01001352 hs->new_session->peer_sha256_valid = true;
Adam Langley364f7a62016-12-12 10:51:00 -08001353 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001354
David Benjamin8f94c312017-08-01 17:35:55 -04001355 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001356 hs->state = state12_verify_client_certificate;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001357 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001358}
1359
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001360static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001361 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001362 switch (ssl_verify_peer_cert(hs)) {
1363 case ssl_verify_ok:
1364 break;
1365 case ssl_verify_invalid:
1366 return ssl_hs_error;
1367 case ssl_verify_retry:
1368 return ssl_hs_certificate_verify;
1369 }
1370 }
1371
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001372 hs->state = state12_read_client_key_exchange;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001373 return ssl_hs_ok;
1374}
1375
1376static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001377 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001378 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001379 if (!ssl->method->get_message(ssl, &msg)) {
1380 return ssl_hs_read_message;
David Benjamin44148742017-06-17 13:20:59 -04001381 }
David Benjamin276b7e82017-01-21 14:13:39 -05001382
David Benjamin7934f082017-08-01 16:32:25 -04001383 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001384 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001385 }
Adam Langley95c29f32014-06-20 12:00:00 -07001386
David Benjamin7934f082017-08-01 16:32:25 -04001387 CBS client_key_exchange = msg.body;
David Benjamin44148742017-06-17 13:20:59 -04001388 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1389 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001390
David Benjaminc11ea9422017-08-29 16:33:21 -04001391 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyfcf25832014-12-18 17:42:32 -08001392 if (alg_a & SSL_aPSK) {
1393 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001394
David Benjaminc11ea9422017-08-29 16:33:21 -04001395 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1396 // then this is the only field in the message.
Adam Langleyfcf25832014-12-18 17:42:32 -08001397 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1398 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001399 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001400 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001401 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001402 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001403
Adam Langleyfcf25832014-12-18 17:42:32 -08001404 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1405 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001406 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind1e3ce12017-10-06 18:31:15 -04001407 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin499742c2017-07-22 12:45:38 -04001408 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 }
David Benjaminbfdd1a92018-06-29 16:26:38 -04001410 char *raw = nullptr;
1411 if (!CBS_strdup(&psk_identity, &raw)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001412 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001413 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 }
David Benjaminbfdd1a92018-06-29 16:26:38 -04001415 hs->new_session->psk_identity.reset(raw);
Adam Langleyfcf25832014-12-18 17:42:32 -08001416 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001417
David Benjamin499742c2017-07-22 12:45:38 -04001418 // Depending on the key exchange method, compute |premaster_secret|.
1419 Array<uint8_t> premaster_secret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001421 CBS encrypted_premaster_secret;
David Benjamin9bb15f52018-06-26 00:07:40 -04001422 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1423 &encrypted_premaster_secret) ||
1424 CBS_len(&client_key_exchange) != 0) {
1425 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1426 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1427 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001428 }
1429
David Benjaminc11ea9422017-08-29 16:33:21 -04001430 // Allocate a buffer large enough for an RSA decryption.
David Benjamin499742c2017-07-22 12:45:38 -04001431 Array<uint8_t> decrypt_buf;
1432 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1433 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001434 }
David Benjamin35c02302014-07-13 04:14:59 -04001435
David Benjaminc11ea9422017-08-29 16:33:21 -04001436 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1437 // timing-sensitive code below.
David Benjamin3f5b43d2015-12-01 19:31:24 -05001438 size_t decrypt_len;
David Benjamin499742c2017-07-22 12:45:38 -04001439 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1440 decrypt_buf.size(),
David Benjamin75a1f232017-10-11 17:19:19 -04001441 encrypted_premaster_secret)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001442 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001443 break;
1444 case ssl_private_key_failure:
David Benjamin499742c2017-07-22 12:45:38 -04001445 return ssl_hs_error;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001446 case ssl_private_key_retry:
David Benjamin499742c2017-07-22 12:45:38 -04001447 return ssl_hs_private_key_operation;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001448 }
1449
David Benjamin499742c2017-07-22 12:45:38 -04001450 if (decrypt_len != decrypt_buf.size()) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001451 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -04001452 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001453 return ssl_hs_error;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001454 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001455
Adam Langleya6a049a2018-12-06 17:15:58 -08001456 CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len);
1457
David Benjaminc11ea9422017-08-29 16:33:21 -04001458 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1459 // section 7.4.7.1.
David Benjamin499742c2017-07-22 12:45:38 -04001460 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1461 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1462 return ssl_hs_error;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001463 }
1464
David Benjaminc11ea9422017-08-29 16:33:21 -04001465 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1466 // publicly invalid.
David Benjamin499742c2017-07-22 12:45:38 -04001467 if (decrypt_len < 11 + premaster_secret.size()) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001468 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind1e3ce12017-10-06 18:31:15 -04001469 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001470 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001471 }
David Benjamin35c02302014-07-13 04:14:59 -04001472
David Benjaminc11ea9422017-08-29 16:33:21 -04001473 // Check the padding. See RFC 3447, section 7.2.2.
David Benjamin499742c2017-07-22 12:45:38 -04001474 size_t padding_len = decrypt_len - premaster_secret.size();
David Benjamin3f5b43d2015-12-01 19:31:24 -05001475 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1476 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001477 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001478 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1479 }
1480 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1481
David Benjaminc11ea9422017-08-29 16:33:21 -04001482 // The premaster secret must begin with |client_version|. This too must be
1483 // checked in constant time (http://eprint.iacr.org/2003/052/).
David Benjamin3f5b43d2015-12-01 19:31:24 -05001484 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001485 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001486 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001487 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001488
David Benjaminc11ea9422017-08-29 16:33:21 -04001489 // Select, in constant time, either the decrypted premaster or the random
1490 // premaster based on |good|.
David Benjamin499742c2017-07-22 12:45:38 -04001491 for (size_t i = 0; i < premaster_secret.size(); i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001492 premaster_secret[i] = constant_time_select_8(
1493 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1494 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001495 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001496 // Parse the ClientKeyExchange.
David Benjamin08b1f382023-02-28 17:22:23 -05001497 CBS ciphertext;
1498 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ciphertext) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001499 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001500 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001501 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001502 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001504
David Benjamin08b1f382023-02-28 17:22:23 -05001505 // Decapsulate the premaster secret.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001506 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin08b1f382023-02-28 17:22:23 -05001507 if (!hs->key_shares[0]->Decap(&premaster_secret, &alert, ciphertext)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001508 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin499742c2017-07-22 12:45:38 -04001509 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001510 }
Adam Langley95c29f32014-06-20 12:00:00 -07001511
David Benjaminc11ea9422017-08-29 16:33:21 -04001512 // The key exchange state may now be discarded.
Adam Langley7b935932018-11-12 13:53:42 -08001513 hs->key_shares[0].reset();
1514 hs->key_shares[1].reset();
David Benjamin44148742017-06-17 13:20:59 -04001515 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001516 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001517 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001518 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 }
Adam Langley95c29f32014-06-20 12:00:00 -07001520
David Benjaminc11ea9422017-08-29 16:33:21 -04001521 // For a PSK cipher suite, the actual pre-master secret is combined with the
1522 // pre-shared key.
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 if (alg_a & SSL_aPSK) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001524 if (hs->config->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001525 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001526 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001527 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001528 }
1529
David Benjaminc11ea9422017-08-29 16:33:21 -04001530 // Look up the key for the identity.
David Benjamin44148742017-06-17 13:20:59 -04001531 uint8_t psk[PSK_MAX_PSK_LEN];
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001532 unsigned psk_len = hs->config->psk_server_callback(
David Benjaminbfdd1a92018-06-29 16:26:38 -04001533 ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
David Benjamin44148742017-06-17 13:20:59 -04001534 if (psk_len > PSK_MAX_PSK_LEN) {
1535 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001536 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001537 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001538 } else if (psk_len == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001539 // PSK related to the given identity not found.
David Benjamin44148742017-06-17 13:20:59 -04001540 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamind1e3ce12017-10-06 18:31:15 -04001541 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
David Benjamin499742c2017-07-22 12:45:38 -04001542 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001543 }
1544
1545 if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001546 // In plain PSK, other_secret is a block of 0s with the same length as the
1547 // pre-shared key.
David Benjamin499742c2017-07-22 12:45:38 -04001548 if (!premaster_secret.Init(psk_len)) {
1549 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001550 }
David Benjamin499742c2017-07-22 12:45:38 -04001551 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
David Benjamin44148742017-06-17 13:20:59 -04001552 }
1553
David Benjamin1386aad2017-07-19 23:57:40 -04001554 ScopedCBB new_premaster;
1555 CBB child;
David Benjamin1386aad2017-07-19 23:57:40 -04001556 if (!CBB_init(new_premaster.get(),
David Benjamin499742c2017-07-22 12:45:38 -04001557 2 + psk_len + 2 + premaster_secret.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001558 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
David Benjamin499742c2017-07-22 12:45:38 -04001559 !CBB_add_bytes(&child, premaster_secret.data(),
1560 premaster_secret.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001561 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin75a1f232017-10-11 17:19:19 -04001563 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
David Benjamin499742c2017-07-22 12:45:38 -04001564 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001565 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 }
David Benjamin14c83e72014-07-13 04:54:57 -04001567
David Benjamin7934f082017-08-01 16:32:25 -04001568 if (!ssl_hash_message(hs, msg)) {
David Benjamin499742c2017-07-22 12:45:38 -04001569 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001570 }
1571
David Benjaminc11ea9422017-08-29 16:33:21 -04001572 // Compute the master secret.
David Benjamin5351c8b2020-11-19 00:25:29 -05001573 hs->new_session->secret_length = tls1_generate_master_secret(
1574 hs, hs->new_session->secret, premaster_secret);
1575 if (hs->new_session->secret_length == 0) {
David Benjamin499742c2017-07-22 12:45:38 -04001576 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 }
David Benjaminfc02b592017-02-17 16:26:01 -05001578 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin5351c8b2020-11-19 00:25:29 -05001579 CONSTTIME_DECLASSIFY(hs->new_session->secret, hs->new_session->secret_length);
David Benjamin9b2cdb72021-04-01 23:21:53 -04001580 hs->can_release_private_key = true;
David Benjamin14c83e72014-07-13 04:54:57 -04001581
David Benjamin8f94c312017-08-01 17:35:55 -04001582 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001583 hs->state = state12_read_client_certificate_verify;
David Benjamin499742c2017-07-22 12:45:38 -04001584 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001585}
Adam Langley95c29f32014-06-20 12:00:00 -07001586
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001587static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001588 SSL *const ssl = hs->ssl;
David Benjamin6553b372014-07-22 14:11:30 -04001589
David Benjaminc11ea9422017-08-29 16:33:21 -04001590 // Only RSA and ECDSA client certificates are supported, so a
1591 // CertificateVerify is required if and only if there's a client certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001592 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001593 hs->transcript.FreeBuffer();
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001594 hs->state = state12_read_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001595 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001596 }
Adam Langley95c29f32014-06-20 12:00:00 -07001597
David Benjamin7934f082017-08-01 16:32:25 -04001598 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001599 if (!ssl->method->get_message(ssl, &msg)) {
1600 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 }
David Benjaminef865502014-08-24 02:48:34 -04001602
David Benjamin7934f082017-08-01 16:32:25 -04001603 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001604 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001605 }
1606
David Benjamincd8f3d32020-06-15 11:16:06 -04001607 // The peer certificate must be valid for signing.
1608 const CRYPTO_BUFFER *leaf =
1609 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1610 CBS leaf_cbs;
1611 CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1612 if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) {
1613 return ssl_hs_error;
1614 }
1615
David Benjamin7934f082017-08-01 16:32:25 -04001616 CBS certificate_verify = msg.body, signature;
David Benjamin6897dbe2014-07-12 20:18:28 -04001617
David Benjaminc11ea9422017-08-29 16:33:21 -04001618 // Determine the signature algorithm.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001619 uint16_t signature_algorithm = 0;
David Benjamind1e3ce12017-10-06 18:31:15 -04001620 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001621 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001622 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001623 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001624 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001625 }
David Benjamin8d606e32017-06-15 22:43:04 -04001626 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminebad5082020-02-03 19:32:19 -05001627 if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001628 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001629 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001630 }
David Benjamin45738dd2017-02-09 20:01:26 -05001631 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001632 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001633 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001634 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001635 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001636 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 }
David Benjamin854dd652014-08-26 00:32:30 -04001638
David Benjaminc11ea9422017-08-29 16:33:21 -04001639 // Parse and verify the signature.
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1641 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001642 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001643 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001644 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 }
Adam Langley95c29f32014-06-20 12:00:00 -07001646
David Benjamin4dfd5af2019-07-19 17:34:37 -04001647 if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1648 hs->peer_pubkey.get(), hs->transcript.buffer())) {
David Benjamin3570d732015-06-29 00:28:17 -04001649 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001650 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001651 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 }
1653
David Benjaminc11ea9422017-08-29 16:33:21 -04001654 // The handshake buffer is no longer necessary, and we may hash the current
1655 // message.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001656 hs->transcript.FreeBuffer();
David Benjamin7934f082017-08-01 16:32:25 -04001657 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001658 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001659 }
1660
David Benjamin8f94c312017-08-01 17:35:55 -04001661 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001662 hs->state = state12_read_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001663 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001664}
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001666static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
Matthew Braithwaite3e2b3ee2018-05-10 15:46:42 -07001667 if (hs->handback && hs->ssl->session != NULL) {
1668 return ssl_hs_handback;
1669 }
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001670 hs->state = state12_process_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001671 return ssl_hs_read_change_cipher_spec;
1672}
1673
1674static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
David Benjamine58f8a62017-09-21 19:07:15 -04001675 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001676 return ssl_hs_error;
1677 }
1678
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001679 hs->state = state12_read_next_proto;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001680 return ssl_hs_ok;
1681}
1682
1683static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001684 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001685
1686 if (!hs->next_proto_neg_seen) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001687 hs->state = state12_read_channel_id;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001688 return ssl_hs_ok;
1689 }
1690
David Benjamin7934f082017-08-01 16:32:25 -04001691 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001692 if (!ssl->method->get_message(ssl, &msg)) {
1693 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 }
Adam Langley95c29f32014-06-20 12:00:00 -07001695
David Benjamin7934f082017-08-01 16:32:25 -04001696 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1697 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001698 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001699 }
1700
David Benjamin7934f082017-08-01 16:32:25 -04001701 CBS next_protocol = msg.body, selected_protocol, padding;
Adam Langleyfcf25832014-12-18 17:42:32 -08001702 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1703 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001704 CBS_len(&next_protocol) != 0) {
1705 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001706 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001707 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001708 }
1709
David Benjamin8e7bbba2017-10-13 17:18:35 -04001710 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001711 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 }
Adam Langley95c29f32014-06-20 12:00:00 -07001713
David Benjamin8f94c312017-08-01 17:35:55 -04001714 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001715 hs->state = state12_read_channel_id;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001716 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001717}
Adam Langley95c29f32014-06-20 12:00:00 -07001718
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001719static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001720 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001721
David Benjamin8acec002021-05-19 13:03:34 -04001722 if (!hs->channel_id_negotiated) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001723 hs->state = state12_read_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001724 return ssl_hs_ok;
1725 }
1726
David Benjamin7934f082017-08-01 16:32:25 -04001727 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001728 if (!ssl->method->get_message(ssl, &msg)) {
1729 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001731
David Benjamin7934f082017-08-01 16:32:25 -04001732 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1733 !tls1_verify_channel_id(hs, msg) ||
1734 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001735 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001736 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001737
David Benjamin8f94c312017-08-01 17:35:55 -04001738 ssl->method->next_message(ssl);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001739 hs->state = state12_read_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001740 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001741}
David Benjamin9f1dc822016-06-07 17:03:46 -04001742
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001743static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1744 SSL *const ssl = hs->ssl;
1745 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1746 if (wait != ssl_hs_ok) {
1747 return wait;
1748 }
1749
1750 if (ssl->session != NULL) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001751 hs->state = state12_finish_server_handshake;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001752 } else {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001753 hs->state = state12_send_server_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001754 }
1755
David Benjaminc11ea9422017-08-29 16:33:21 -04001756 // If this is a full handshake with ChannelID then record the handshake
1757 // hashes in |hs->new_session| in case we need them to verify a
1758 // ChannelID signature on a resumption of this session in the future.
David Benjamin46853762018-07-03 14:01:26 -04001759 if (ssl->session == NULL && ssl->s3->channel_id_valid &&
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001760 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1761 return ssl_hs_error;
1762 }
1763
1764 return ssl_hs_ok;
1765}
1766
1767static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001768 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001769
1770 if (hs->ticket_expected) {
1771 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001772 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001773 if (ssl->session == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001774 // Fix the timeout to measure from the ticket issuance time.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001775 ssl_session_rebase_time(ssl, hs->new_session.get());
1776 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001777 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001778 // We are renewing an existing session. Duplicate the session to adjust
1779 // the timeout.
David Benjamin50596f82018-07-02 19:47:27 -04001780 session_copy =
1781 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001782 if (!session_copy) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001783 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001784 }
1785
David Benjamin1386aad2017-07-19 23:57:40 -04001786 ssl_session_rebase_time(ssl, session_copy.get());
1787 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001788 }
1789
David Benjamin1386aad2017-07-19 23:57:40 -04001790 ScopedCBB cbb;
1791 CBB body, ticket;
1792 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1793 SSL3_MT_NEW_SESSION_TICKET) ||
1794 !CBB_add_u32(&body, session->timeout) ||
1795 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001796 !ssl_encrypt_ticket(hs, &ticket, session) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001797 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001798 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001799 }
David Benjamin123db572016-11-03 16:59:25 -04001800 }
1801
David Benjaminb5f55c32017-06-15 23:15:15 -04001802 if (!ssl->method->add_change_cipher_spec(ssl) ||
David Benjamine58f8a62017-09-21 19:07:15 -04001803 !tls1_change_cipher_state(hs, evp_aead_seal) ||
David Benjamin00f48c82017-10-06 18:43:53 -04001804 !ssl_send_finished(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001805 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001806 }
1807
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001808 if (ssl->session != NULL) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001809 hs->state = state12_read_change_cipher_spec;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001810 } else {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001811 hs->state = state12_finish_server_handshake;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001812 }
1813 return ssl_hs_flush;
David Benjamin9f1dc822016-06-07 17:03:46 -04001814}
David Benjamin86e95b82017-07-18 16:34:25 -04001815
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001816static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1817 SSL *const ssl = hs->ssl;
1818
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001819 if (hs->handback) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001820 return ssl_hs_handback;
1821 }
1822
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001823 ssl->method->on_handshake_complete(ssl);
1824
David Benjaminc11ea9422017-08-29 16:33:21 -04001825 // If we aren't retaining peer certificates then we can discard it now.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001826 if (hs->new_session != NULL &&
1827 hs->config->retain_only_sha256_of_client_certs) {
David Benjaminbfdd1a92018-06-29 16:26:38 -04001828 hs->new_session->certs.reset();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001829 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1830 }
1831
David Benjamina10017c2021-06-16 16:00:13 -04001832 bool has_new_session = hs->new_session != nullptr;
1833 if (has_new_session) {
David Benjamin10a76ac2021-06-16 11:33:37 -04001834 assert(ssl->session == nullptr);
David Benjamin8e7bbba2017-10-13 17:18:35 -04001835 ssl->s3->established_session = std::move(hs->new_session);
David Benjamina3a71e92018-06-29 13:24:45 -04001836 ssl->s3->established_session->not_resumable = false;
David Benjamina10017c2021-06-16 16:00:13 -04001837 } else {
1838 assert(ssl->session != nullptr);
1839 ssl->s3->established_session = UpRef(ssl->session);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001840 }
1841
David Benjaminfd45ee72017-08-31 14:49:09 -04001842 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001843 ssl->s3->initial_handshake_complete = true;
David Benjamina10017c2021-06-16 16:00:13 -04001844 if (has_new_session) {
1845 ssl_update_cache(ssl);
1846 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001847
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001848 hs->state = state12_done;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001849 return ssl_hs_ok;
1850}
1851
1852enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001853 while (hs->state != state12_done) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001854 enum ssl_hs_wait_t ret = ssl_hs_error;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001855 enum tls12_server_hs_state_t state =
1856 static_cast<enum tls12_server_hs_state_t>(hs->state);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001857 switch (state) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001858 case state12_start_accept:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001859 ret = do_start_accept(hs);
1860 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001861 case state12_read_client_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001862 ret = do_read_client_hello(hs);
1863 break;
Daniel McArdle00e434d2021-02-18 11:47:18 -05001864 case state12_read_client_hello_after_ech:
1865 ret = do_read_client_hello_after_ech(hs);
1866 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001867 case state12_select_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001868 ret = do_select_certificate(hs);
1869 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001870 case state12_tls13:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001871 ret = do_tls13(hs);
1872 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001873 case state12_select_parameters:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001874 ret = do_select_parameters(hs);
1875 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001876 case state12_send_server_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001877 ret = do_send_server_hello(hs);
1878 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001879 case state12_send_server_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001880 ret = do_send_server_certificate(hs);
1881 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001882 case state12_send_server_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001883 ret = do_send_server_key_exchange(hs);
1884 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001885 case state12_send_server_hello_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001886 ret = do_send_server_hello_done(hs);
1887 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001888 case state12_read_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001889 ret = do_read_client_certificate(hs);
1890 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001891 case state12_verify_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001892 ret = do_verify_client_certificate(hs);
1893 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001894 case state12_read_client_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001895 ret = do_read_client_key_exchange(hs);
1896 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001897 case state12_read_client_certificate_verify:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001898 ret = do_read_client_certificate_verify(hs);
1899 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001900 case state12_read_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001901 ret = do_read_change_cipher_spec(hs);
1902 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001903 case state12_process_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001904 ret = do_process_change_cipher_spec(hs);
1905 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001906 case state12_read_next_proto:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001907 ret = do_read_next_proto(hs);
1908 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001909 case state12_read_channel_id:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001910 ret = do_read_channel_id(hs);
1911 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001912 case state12_read_client_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001913 ret = do_read_client_finished(hs);
1914 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001915 case state12_send_server_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001916 ret = do_send_server_finished(hs);
1917 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001918 case state12_finish_server_handshake:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001919 ret = do_finish_server_handshake(hs);
1920 break;
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001921 case state12_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001922 ret = ssl_hs_ok;
1923 break;
1924 }
1925
1926 if (hs->state != state) {
1927 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1928 }
1929
1930 if (ret != ssl_hs_ok) {
1931 return ret;
1932 }
1933 }
1934
1935 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1936 return ssl_hs_ok;
1937}
1938
1939const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001940 enum tls12_server_hs_state_t state =
1941 static_cast<enum tls12_server_hs_state_t>(hs->state);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001942 switch (state) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001943 case state12_start_accept:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001944 return "TLS server start_accept";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001945 case state12_read_client_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001946 return "TLS server read_client_hello";
Daniel McArdle00e434d2021-02-18 11:47:18 -05001947 case state12_read_client_hello_after_ech:
1948 return "TLS server read_client_hello_after_ech";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001949 case state12_select_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001950 return "TLS server select_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001951 case state12_tls13:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001952 return tls13_server_handshake_state(hs);
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001953 case state12_select_parameters:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001954 return "TLS server select_parameters";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001955 case state12_send_server_hello:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001956 return "TLS server send_server_hello";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001957 case state12_send_server_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001958 return "TLS server send_server_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001959 case state12_send_server_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001960 return "TLS server send_server_key_exchange";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001961 case state12_send_server_hello_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001962 return "TLS server send_server_hello_done";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001963 case state12_read_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001964 return "TLS server read_client_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001965 case state12_verify_client_certificate:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001966 return "TLS server verify_client_certificate";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001967 case state12_read_client_key_exchange:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001968 return "TLS server read_client_key_exchange";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001969 case state12_read_client_certificate_verify:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001970 return "TLS server read_client_certificate_verify";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001971 case state12_read_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001972 return "TLS server read_change_cipher_spec";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001973 case state12_process_change_cipher_spec:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001974 return "TLS server process_change_cipher_spec";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001975 case state12_read_next_proto:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001976 return "TLS server read_next_proto";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001977 case state12_read_channel_id:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001978 return "TLS server read_channel_id";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001979 case state12_read_client_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001980 return "TLS server read_client_finished";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001981 case state12_send_server_finished:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001982 return "TLS server send_server_finished";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001983 case state12_finish_server_handshake:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001984 return "TLS server finish_server_handshake";
Matthew Braithwaite56986f92018-03-22 11:48:33 -07001985 case state12_done:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001986 return "TLS server done";
1987 }
1988
1989 return "TLS server unknown";
1990}
1991
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -07001992BSSL_NAMESPACE_END