blob: c8a23a165a6207e6da5ca81a3e642b3c2a36383b [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
Robert Sloana27a6a42017-09-05 08:39:28 -07007 *
Adam Langleyd9e397b2015-01-22 14:27:53 -08008 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Robert Sloana27a6a42017-09-05 08:39:28 -070014 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080015 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
Robert Sloana27a6a42017-09-05 08:39:28 -070021 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080022 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
Robert Sloana27a6a42017-09-05 08:39:28 -070036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langleyd9e397b2015-01-22 14:27:53 -080037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Robert Sloana27a6a42017-09-05 08:39:28 -070039 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080040 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
Robert Sloana27a6a42017-09-05 08:39:28 -070051 *
Adam Langleyd9e397b2015-01-22 14:27:53 -080052 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
Robert Sloana27a6a42017-09-05 08:39:28 -070065 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080066 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
Robert Sloana27a6a42017-09-05 08:39:28 -0700113 * Portions of the attached software ("Contribution") are developed by
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
Kenny Rootb8494592015-09-25 02:29:14 +0000149#include <openssl/ssl.h>
150
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151#include <assert.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152#include <string.h>
153
154#include <openssl/bn.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800155#include <openssl/bytestring.h>
156#include <openssl/cipher.h>
Pete Bentley00a7c402021-07-23 17:57:12 +0100157#include <openssl/curve25519.h>
158#include <openssl/digest.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700161#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400166#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168#include <openssl/x509.h>
169
Adam Langleye9ada862015-05-11 17:20:37 -0700170#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172
173
Robert Sloan726e9d12018-09-11 11:45:04 -0700174BSSL_NAMESPACE_BEGIN
Robert Sloanb6d070c2017-07-24 08:40:01 -0700175
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100176bool ssl_client_cipher_list_contains_cipher(
177 const SSL_CLIENT_HELLO *client_hello, uint16_t id) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400178 CBS cipher_suites;
179 CBS_init(&cipher_suites, client_hello->cipher_suites,
180 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800181
David Benjaminc895d6b2016-08-11 13:26:41 -0400182 while (CBS_len(&cipher_suites) > 0) {
183 uint16_t got_id;
184 if (!CBS_get_u16(&cipher_suites, &got_id)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100185 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400186 }
187
188 if (got_id == id) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100189 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400190 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800191 }
192
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100193 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194}
195
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100196static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
197 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500198 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800199 assert(!ssl->s3->have_version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700200 CBS supported_versions, versions;
David Benjamin1b249672016-12-06 18:25:50 -0500201 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
202 TLSEXT_TYPE_supported_versions)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -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;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100208 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400209 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400210 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -0700211 // Convert the ClientHello version to an equivalent supported_versions
212 // extension.
Robert Sloanf6200e72017-07-10 08:09:18 -0700213 static const uint8_t kTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700214 0x03, 0x03, // TLS 1.2
215 0x03, 0x02, // TLS 1.1
216 0x03, 0x01, // TLS 1
Robert Sloanf6200e72017-07-10 08:09:18 -0700217 };
218
219 static const uint8_t kDTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700220 0xfe, 0xfd, // DTLS 1.2
221 0xfe, 0xff, // DTLS 1.0
Robert Sloanf6200e72017-07-10 08:09:18 -0700222 };
223
224 size_t versions_len = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400225 if (SSL_is_dtls(ssl)) {
226 if (client_hello->version <= DTLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700227 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400228 } else if (client_hello->version <= DTLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700229 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400230 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700231 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
232 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400233 } else {
234 if (client_hello->version >= TLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700235 versions_len = 6;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100236 } else if (client_hello->version >= TLS1_1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700237 versions_len = 4;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100238 } else if (client_hello->version >= TLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700239 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400240 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700241 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
242 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400243 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244 }
245
Robert Sloanf6200e72017-07-10 08:09:18 -0700246 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100247 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248 }
249
Robert Sloana27a6a42017-09-05 08:39:28 -0700250 // At this point, the connection's version is known and |ssl->version| is
251 // fixed. Begin enforcing the record-layer version.
252 ssl->s3->have_version = true;
Robert Sloandb4251a2017-09-18 09:38:15 -0700253 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254
Robert Sloana27a6a42017-09-05 08:39:28 -0700255 // Handle FALLBACK_SCSV.
Robert Sloanf6200e72017-07-10 08:09:18 -0700256 if (ssl_client_cipher_list_contains_cipher(client_hello,
257 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Robert Sloan921ef2c2017-10-17 09:02:20 -0700258 ssl_protocol_version(ssl) < hs->max_version) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700259 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
260 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100261 return false;
Robert Sloanf6200e72017-07-10 08:09:18 -0700262 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400263
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100264 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265}
266
Robert Sloanfe7cd212017-08-07 09:03:39 -0700267static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
268 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500269 CBS cipher_suites;
270 CBS_init(&cipher_suites, client_hello->cipher_suites,
271 client_hello->cipher_suites_len);
272
Robert Sloanfe7cd212017-08-07 09:03:39 -0700273 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
274 if (!sk) {
David Benjamin1b249672016-12-06 18:25:50 -0500275 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700276 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500277 }
278
279 while (CBS_len(&cipher_suites) > 0) {
280 uint16_t cipher_suite;
281
282 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
283 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700284 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500285 }
286
287 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700288 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500289 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700290 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500291 }
292 }
293
294 return sk;
David Benjamin1b249672016-12-06 18:25:50 -0500295}
296
Robert Sloana27a6a42017-09-05 08:39:28 -0700297// ssl_get_compatible_server_ciphers determines the key exchange and
298// authentication cipher suite masks compatible with the server configuration
299// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
300// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin1b249672016-12-06 18:25:50 -0500301static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
302 uint32_t *out_mask_k,
303 uint32_t *out_mask_a) {
David Benjamin1b249672016-12-06 18:25:50 -0500304 uint32_t mask_k = 0;
305 uint32_t mask_a = 0;
306
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800307 if (ssl_has_certificate(hs)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700308 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
309 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin1b249672016-12-06 18:25:50 -0500310 mask_k |= SSL_kRSA;
David Benjamin1b249672016-12-06 18:25:50 -0500311 }
312 }
313
Robert Sloana27a6a42017-09-05 08:39:28 -0700314 // Check for a shared group to consider ECDHE ciphers.
David Benjamin1b249672016-12-06 18:25:50 -0500315 uint16_t unused;
316 if (tls1_get_shared_group(hs, &unused)) {
317 mask_k |= SSL_kECDHE;
318 }
319
Robert Sloana27a6a42017-09-05 08:39:28 -0700320 // PSK requires a server callback.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100321 if (hs->config->psk_server_callback != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -0500322 mask_k |= SSL_kPSK;
323 mask_a |= SSL_aPSK;
324 }
325
326 *out_mask_k = mask_k;
327 *out_mask_a = mask_a;
328}
329
Tobias Thierer43be7d22020-03-02 19:23:34 +0000330static const SSL_CIPHER *choose_cipher(
David Benjamin1b249672016-12-06 18:25:50 -0500331 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
Robert Sloanc6ebb282018-04-30 10:10:26 -0700332 const SSLCipherPreferenceList *server_pref) {
David Benjamin1b249672016-12-06 18:25:50 -0500333 SSL *const ssl = hs->ssl;
Robert Sloan15c0b352018-04-16 08:36:46 -0700334 const STACK_OF(SSL_CIPHER) *prio, *allow;
Robert Sloana27a6a42017-09-05 08:39:28 -0700335 // in_group_flags will either be NULL, or will point to an array of bytes
336 // which indicate equal-preference groups in the |prio| stack. See the
Robert Sloanc6ebb282018-04-30 10:10:26 -0700337 // comment about |in_group_flags| in the |SSLCipherPreferenceList|
Robert Sloana27a6a42017-09-05 08:39:28 -0700338 // struct.
Robert Sloanc6ebb282018-04-30 10:10:26 -0700339 const bool *in_group_flags;
Robert Sloana27a6a42017-09-05 08:39:28 -0700340 // group_min contains the minimal index so far found in a group, or -1 if no
341 // such value exists yet.
David Benjamin1b249672016-12-06 18:25:50 -0500342 int group_min = -1;
343
Robert Sloanfe7cd212017-08-07 09:03:39 -0700344 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
Robert Sloan6f79a502017-04-03 09:16:40 -0700345 ssl_parse_client_cipher_list(client_hello);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700346 if (!client_pref) {
347 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500348 }
349
350 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Robert Sloanc6ebb282018-04-30 10:10:26 -0700351 prio = server_pref->ciphers.get();
David Benjamin1b249672016-12-06 18:25:50 -0500352 in_group_flags = server_pref->in_group_flags;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700353 allow = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500354 } else {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700355 prio = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500356 in_group_flags = NULL;
Robert Sloanc6ebb282018-04-30 10:10:26 -0700357 allow = server_pref->ciphers.get();
David Benjamin1b249672016-12-06 18:25:50 -0500358 }
359
Robert Sloan6f79a502017-04-03 09:16:40 -0700360 uint32_t mask_k, mask_a;
David Benjamin1b249672016-12-06 18:25:50 -0500361 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
362
363 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700364 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin1b249672016-12-06 18:25:50 -0500365
Robert Sloan6f79a502017-04-03 09:16:40 -0700366 size_t cipher_index;
Robert Sloana27a6a42017-09-05 08:39:28 -0700367 if (// Check if the cipher is supported for the current version.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700368 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
369 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700370 // Check the cipher is supported for the server configuration.
Robert Sloan6f79a502017-04-03 09:16:40 -0700371 (c->algorithm_mkey & mask_k) &&
372 (c->algorithm_auth & mask_a) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700373 // Check the cipher is in the |allow| list.
Robert Sloan6f79a502017-04-03 09:16:40 -0700374 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
Robert Sloanc6ebb282018-04-30 10:10:26 -0700375 if (in_group_flags != NULL && in_group_flags[i]) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700376 // This element of |prio| is in a group. Update the minimum index found
377 // so far and continue looking.
David Benjamin1b249672016-12-06 18:25:50 -0500378 if (group_min == -1 || (size_t)group_min > cipher_index) {
379 group_min = cipher_index;
380 }
381 } else {
382 if (group_min != -1 && (size_t)group_min < cipher_index) {
383 cipher_index = group_min;
384 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700385 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin1b249672016-12-06 18:25:50 -0500386 }
387 }
388
Robert Sloanc6ebb282018-04-30 10:10:26 -0700389 if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700390 // We are about to leave a group, but we found a match in it, so that's
391 // our answer.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700392 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin1b249672016-12-06 18:25:50 -0500393 }
394 }
395
Robert Sloanfe7cd212017-08-07 09:03:39 -0700396 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500397}
398
Robert Sloana27a6a42017-09-05 08:39:28 -0700399static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
400 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
Robert Sloandc2f6092018-04-10 10:22:33 -0700401 hs->state = state12_read_client_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700402 return ssl_hs_ok;
403}
404
Robert Sloanc9abfe42018-11-26 12:19:07 -0800405// is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800406// from a JDK 11 client with both TLS 1.3 and a prior version enabled.
Robert Sloanc9abfe42018-11-26 12:19:07 -0800407static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) {
408 // JDK 11 ClientHellos contain a number of unusual properties which should
409 // limit false positives.
410
411 // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern
412 // clients implement ChaCha20-Poly1305.
413 if (ssl_client_cipher_list_contains_cipher(
414 client_hello, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
415 return false;
416 }
417
418 // JDK 11 always sends extensions in a particular order.
419 constexpr uint16_t kMaxFragmentLength = 0x0001;
420 constexpr uint16_t kStatusRequestV2 = 0x0011;
421 static CONSTEXPR_ARRAY struct {
422 uint16_t id;
423 bool required;
424 } kJavaExtensions[] = {
425 {TLSEXT_TYPE_server_name, false},
426 {kMaxFragmentLength, false},
427 {TLSEXT_TYPE_status_request, false},
428 {TLSEXT_TYPE_supported_groups, true},
429 {TLSEXT_TYPE_ec_point_formats, false},
430 {TLSEXT_TYPE_signature_algorithms, true},
431 // Java always sends signature_algorithms_cert.
432 {TLSEXT_TYPE_signature_algorithms_cert, true},
433 {TLSEXT_TYPE_application_layer_protocol_negotiation, false},
434 {kStatusRequestV2, false},
435 {TLSEXT_TYPE_extended_master_secret, false},
436 {TLSEXT_TYPE_supported_versions, true},
437 {TLSEXT_TYPE_cookie, false},
438 {TLSEXT_TYPE_psk_key_exchange_modes, true},
439 {TLSEXT_TYPE_key_share, true},
440 {TLSEXT_TYPE_renegotiate, false},
441 {TLSEXT_TYPE_pre_shared_key, false},
442 };
443 Span<const uint8_t> sigalgs, sigalgs_cert;
444 bool has_status_request = false, has_status_request_v2 = false;
445 CBS extensions, supported_groups;
446 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
447 for (const auto &java_extension : kJavaExtensions) {
448 CBS copy = extensions;
449 uint16_t id;
450 if (CBS_get_u16(&copy, &id) && id == java_extension.id) {
451 // The next extension is the one we expected.
452 extensions = copy;
453 CBS body;
454 if (!CBS_get_u16_length_prefixed(&extensions, &body)) {
455 return false;
456 }
457 switch (id) {
458 case TLSEXT_TYPE_status_request:
459 has_status_request = true;
460 break;
461 case kStatusRequestV2:
462 has_status_request_v2 = true;
463 break;
464 case TLSEXT_TYPE_signature_algorithms:
465 sigalgs = body;
466 break;
467 case TLSEXT_TYPE_signature_algorithms_cert:
468 sigalgs_cert = body;
469 break;
470 case TLSEXT_TYPE_supported_groups:
471 supported_groups = body;
472 break;
473 }
474 } else if (java_extension.required) {
475 return false;
476 }
477 }
478 if (CBS_len(&extensions) != 0) {
479 return false;
480 }
481
482 // JDK 11 never advertises X25519. It is not offered by default, and
483 // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern
484 // clients implement X25519.
485 while (CBS_len(&supported_groups) > 0) {
486 uint16_t group;
487 if (!CBS_get_u16(&supported_groups, &group) ||
488 group == SSL_CURVE_X25519) {
489 return false;
490 }
491 }
492
493 if (// JDK 11 always sends the same contents in signature_algorithms and
494 // signature_algorithms_cert. This is unusual: signature_algorithms_cert,
495 // if omitted, is treated as if it were signature_algorithms.
496 sigalgs != sigalgs_cert ||
497 // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it
498 // sends status_request. This is unusual: status_request_v2 is not widely
499 // implemented.
500 has_status_request != has_status_request_v2) {
501 return false;
502 }
503
504 return true;
505}
506
Pete Bentley0c61efe2019-08-13 09:32:23 +0100507static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert,
508 const SSL_CLIENT_HELLO *client_hello) {
509 SSL *const ssl = hs->ssl;
510 CBS sni;
511 if (!ssl_client_hello_get_extension(client_hello, &sni,
512 TLSEXT_TYPE_server_name)) {
513 // No SNI extension to parse.
514 return true;
515 }
516
517 CBS server_name_list, host_name;
518 uint8_t name_type;
519 if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) ||
520 !CBS_get_u8(&server_name_list, &name_type) ||
521 // Although the server_name extension was intended to be extensible to
522 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
523 // different name types will cause an error. Further, RFC 4366 originally
524 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
525 // adding new name types is no longer feasible.
526 //
527 // Act as if the extensibility does not exist to simplify parsing.
528 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
529 CBS_len(&server_name_list) != 0 ||
530 CBS_len(&sni) != 0) {
531 *out_alert = SSL_AD_DECODE_ERROR;
532 return false;
533 }
534
535 if (name_type != TLSEXT_NAMETYPE_host_name ||
536 CBS_len(&host_name) == 0 ||
537 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
538 CBS_contains_zero_byte(&host_name)) {
539 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
540 return false;
541 }
542
543 // Copy the hostname as a string.
544 char *raw = nullptr;
545 if (!CBS_strdup(&host_name, &raw)) {
546 *out_alert = SSL_AD_INTERNAL_ERROR;
547 return false;
548 }
549 ssl->s3->hostname.reset(raw);
550
551 hs->should_ack_sni = true;
552 return true;
553}
554
Robert Sloana27a6a42017-09-05 08:39:28 -0700555static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500556 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700557
Robert Sloan84377092017-08-14 09:33:19 -0700558 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700559 if (!ssl->method->get_message(ssl, &msg)) {
560 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700561 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700562
Robert Sloan84377092017-08-14 09:33:19 -0700563 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700564 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400565 }
566
David Benjamin1b249672016-12-06 18:25:50 -0500567 SSL_CLIENT_HELLO client_hello;
Pete Bentley00a7c402021-07-23 17:57:12 +0100568 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400569 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700570 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700571 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400572 }
573
Tobias Thierer43be7d22020-03-02 19:23:34 +0000574 // ClientHello should be the end of the flight. We check this early to cover
575 // all protocol versions.
576 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
577 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
578 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
579 return ssl_hs_error;
580 }
581
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700582 if (hs->config->handoff) {
583 return ssl_hs_handoff;
584 }
585
Michael Eastwood220a6542021-09-30 20:13:43 +0000586 // If the ClientHello contains an encrypted_client_hello extension (and no
587 // ech_is_inner extension), act as a client-facing server and attempt to
588 // decrypt the ClientHelloInner.
589 CBS ech_body;
590 if (ssl_client_hello_get_extension(&client_hello, &ech_body,
591 TLSEXT_TYPE_encrypted_client_hello)) {
592 CBS unused;
593 if (ssl_client_hello_get_extension(&client_hello, &unused,
594 TLSEXT_TYPE_ech_is_inner)) {
595 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
596 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
597 return ssl_hs_error;
598 }
599
600 // Parse a ClientECH out of the extension body.
601 uint8_t config_id;
602 uint16_t kdf_id, aead_id;
603 CBS enc, payload;
604 if (!CBS_get_u16(&ech_body, &kdf_id) || //
605 !CBS_get_u16(&ech_body, &aead_id) ||
606 !CBS_get_u8(&ech_body, &config_id) ||
607 !CBS_get_u16_length_prefixed(&ech_body, &enc) ||
608 !CBS_get_u16_length_prefixed(&ech_body, &payload) ||
609 CBS_len(&ech_body) != 0) {
610 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
611 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
612 return ssl_hs_error;
613 }
614
615 {
616 MutexReadLock lock(&ssl->ctx->lock);
617 hs->ech_keys = UpRef(ssl->ctx->ech_keys);
618 }
619
620 if (hs->ech_keys) {
621 for (const auto &config : hs->ech_keys->configs) {
622 hs->ech_hpke_ctx.Reset();
623 if (config_id != config->ech_config().config_id ||
624 !config->SetupContext(hs->ech_hpke_ctx.get(), kdf_id, aead_id,
625 enc)) {
626 // Ignore the error and try another ECHConfig.
627 ERR_clear_error();
628 continue;
629 }
630 Array<uint8_t> encoded_client_hello_inner;
631 bool is_decrypt_error;
632 if (!ssl_client_hello_decrypt(hs->ech_hpke_ctx.get(),
633 &encoded_client_hello_inner,
634 &is_decrypt_error, &client_hello, kdf_id,
635 aead_id, config_id, enc, payload)) {
636 if (is_decrypt_error) {
637 // Ignore the error and try another ECHConfig.
638 ERR_clear_error();
639 continue;
640 }
641 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
642 return ssl_hs_error;
643 }
644
645 // Recover the ClientHelloInner from the EncodedClientHelloInner.
646 uint8_t alert = SSL_AD_DECODE_ERROR;
647 bssl::Array<uint8_t> client_hello_inner;
648 if (!ssl_decode_client_hello_inner(ssl, &alert, &client_hello_inner,
649 encoded_client_hello_inner,
650 &client_hello)) {
651 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
652 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
653 return ssl_hs_error;
654 }
655 hs->ech_client_hello_buf = std::move(client_hello_inner);
656
657 // Load the ClientHelloInner into |client_hello|.
658 if (!hs->GetClientHello(&msg, &client_hello)) {
659 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
660 return ssl_hs_error;
661 }
662
663 hs->ech_config_id = config_id;
664 ssl->s3->ech_status = ssl_ech_accepted;
665 break;
666 }
667 }
668
669 // If we did not accept ECH, proceed with the ClientHelloOuter. Note this
670 // could be key mismatch or ECH GREASE, so we most complete the handshake
671 // as usual, except EncryptedExtensions will contain retry configs.
672 if (ssl->s3->ech_status != ssl_ech_accepted) {
673 ssl->s3->ech_status = ssl_ech_rejected;
674 }
675 }
676
Pete Bentley8bb65ff2021-09-23 10:49:18 +0100677 uint8_t alert = SSL_AD_DECODE_ERROR;
Pete Bentley0c61efe2019-08-13 09:32:23 +0100678 if (!extract_sni(hs, &alert, &client_hello)) {
679 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
680 return ssl_hs_error;
681 }
682
Pete Bentley00a7c402021-07-23 17:57:12 +0100683 hs->state = state12_read_client_hello_after_ech;
684 return ssl_hs_ok;
685}
686
687static enum ssl_hs_wait_t do_read_client_hello_after_ech(SSL_HANDSHAKE *hs) {
688 SSL *const ssl = hs->ssl;
689
690 SSLMessage msg_unused;
691 SSL_CLIENT_HELLO client_hello;
692 if (!hs->GetClientHello(&msg_unused, &client_hello)) {
693 return ssl_hs_error;
694 }
695
Robert Sloana27a6a42017-09-05 08:39:28 -0700696 // Run the early callback.
Robert Sloan5d625782017-02-13 09:55:39 -0800697 if (ssl->ctx->select_certificate_cb != NULL) {
698 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700699 case ssl_select_cert_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700700 return ssl_hs_certificate_selection_pending;
David Benjaminc895d6b2016-08-11 13:26:41 -0400701
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700702 case ssl_select_cert_error:
Robert Sloana27a6a42017-09-05 08:39:28 -0700703 // Connection rejected.
Robert Sloan5d625782017-02-13 09:55:39 -0800704 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700705 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700706 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400707
Robert Sloan5d625782017-02-13 09:55:39 -0800708 default:
709 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400710 }
Robert Sloan5d625782017-02-13 09:55:39 -0800711 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800712
Robert Sloana27a6a42017-09-05 08:39:28 -0700713 // Freeze the version range after the early callback.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100714 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700715 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700716 }
717
Robert Sloanc9abfe42018-11-26 12:19:07 -0800718 if (hs->config->jdk11_workaround &&
719 is_probably_jdk11_with_tls13(&client_hello)) {
720 hs->apply_jdk11_workaround = true;
721 }
722
Pete Bentley00a7c402021-07-23 17:57:12 +0100723 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan5d625782017-02-13 09:55:39 -0800724 if (!negotiate_version(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700725 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700726 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800727 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800728
Robert Sloanf6200e72017-07-10 08:09:18 -0700729 hs->client_version = client_hello.version;
Robert Sloan5d625782017-02-13 09:55:39 -0800730 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
731 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700732 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800733 }
734 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
735 client_hello.random_len);
736
Robert Sloana27a6a42017-09-05 08:39:28 -0700737 // Only null compression is supported. TLS 1.3 further requires the peer
738 // advertise no other compression.
Robert Sloan5d625782017-02-13 09:55:39 -0800739 if (OPENSSL_memchr(client_hello.compression_methods, 0,
740 client_hello.compression_methods_len) == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700741 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
Robert Sloan5d625782017-02-13 09:55:39 -0800742 client_hello.compression_methods_len != 1)) {
743 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700744 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700745 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800746 }
747
Robert Sloana27a6a42017-09-05 08:39:28 -0700748 // TLS extensions.
Robert Sloan5d625782017-02-13 09:55:39 -0800749 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
750 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Robert Sloana27a6a42017-09-05 08:39:28 -0700751 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800752 }
753
Michael Eastwood220a6542021-09-30 20:13:43 +0000754 if (hs->ech_present && hs->ech_is_inner_present) {
755 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
756 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
757 return ssl_hs_error;
758 }
759
Robert Sloandc2f6092018-04-10 10:22:33 -0700760 hs->state = state12_select_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700761 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800762}
763
Robert Sloana27a6a42017-09-05 08:39:28 -0700764static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800765 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700766
Robert Sloana27a6a42017-09-05 08:39:28 -0700767 // Call |cert_cb| to update server certificates if required.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100768 if (hs->config->cert->cert_cb != NULL) {
769 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
Robert Sloan5d625782017-02-13 09:55:39 -0800770 if (rv == 0) {
771 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700772 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700773 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400774 }
Robert Sloan5d625782017-02-13 09:55:39 -0800775 if (rv < 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700776 return ssl_hs_x509_lookup;
David Benjaminc895d6b2016-08-11 13:26:41 -0400777 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400778 }
779
Robert Sloan572a4e22017-04-17 10:52:19 -0700780 if (!ssl_on_certificate_selected(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700781 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500782 }
783
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100784 if (hs->ocsp_stapling_requested &&
785 ssl->ctx->legacy_ocsp_callback != nullptr) {
786 switch (ssl->ctx->legacy_ocsp_callback(
787 ssl, ssl->ctx->legacy_ocsp_callback_arg)) {
788 case SSL_TLSEXT_ERR_OK:
789 break;
790 case SSL_TLSEXT_ERR_NOACK:
791 hs->ocsp_stapling_requested = false;
792 break;
793 default:
794 OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR);
795 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
796 return ssl_hs_error;
797 }
798 }
799
Robert Sloan921ef2c2017-10-17 09:02:20 -0700800 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700801 // Jump to the TLS 1.3 state machine.
Robert Sloandc2f6092018-04-10 10:22:33 -0700802 hs->state = state12_tls13;
Robert Sloana27a6a42017-09-05 08:39:28 -0700803 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800804 }
805
Pete Bentley00a7c402021-07-23 17:57:12 +0100806 // It should not be possible to negotiate TLS 1.2 with ECH. The
807 // ClientHelloInner decoding function rejects ClientHellos which offer TLS 1.2
808 // or below.
809 assert(ssl->s3->ech_status != ssl_ech_accepted);
810
811 // TODO(davidben): Also compute hints for TLS 1.2. When doing so, update the
812 // check in bssl_shim.cc to test this.
813 if (hs->hints_requested) {
814 return ssl_hs_hints_ready;
815 }
816
Pete Bentley0c61efe2019-08-13 09:32:23 +0100817 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
818
Pete Bentley00a7c402021-07-23 17:57:12 +0100819 SSLMessage msg_unused;
Robert Sloan5d625782017-02-13 09:55:39 -0800820 SSL_CLIENT_HELLO client_hello;
Pete Bentley00a7c402021-07-23 17:57:12 +0100821 if (!hs->GetClientHello(&msg_unused, &client_hello)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700822 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800823 }
824
Robert Sloana27a6a42017-09-05 08:39:28 -0700825 // Negotiate the cipher suite. This must be done after |cert_cb| so the
826 // certificate is finalized.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100827 SSLCipherPreferenceList *prefs = hs->config->cipher_list
828 ? hs->config->cipher_list.get()
829 : ssl->ctx->cipher_list.get();
Tobias Thierer43be7d22020-03-02 19:23:34 +0000830 hs->new_cipher = choose_cipher(hs, &client_hello, prefs);
Robert Sloana94fe052017-02-21 08:49:28 -0800831 if (hs->new_cipher == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -0800832 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700833 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700834 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800835 }
836
Robert Sloandc2f6092018-04-10 10:22:33 -0700837 hs->state = state12_select_parameters;
Robert Sloana27a6a42017-09-05 08:39:28 -0700838 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800839}
840
Robert Sloana27a6a42017-09-05 08:39:28 -0700841static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
842 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
843 if (wait == ssl_hs_ok) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700844 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -0700845 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800846 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500847
Robert Sloana27a6a42017-09-05 08:39:28 -0700848 return wait;
849}
850
851static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
852 SSL *const ssl = hs->ssl;
853
854 SSLMessage msg;
855 if (!ssl->method->get_message(ssl, &msg)) {
856 return ssl_hs_read_message;
857 }
858
859 SSL_CLIENT_HELLO client_hello;
Pete Bentley00a7c402021-07-23 17:57:12 +0100860 if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700861 return ssl_hs_error;
862 }
863
Pete Bentley00a7c402021-07-23 17:57:12 +0100864 hs->session_id_len = client_hello.session_id_len;
865 // This is checked in |ssl_client_hello_init|.
866 assert(hs->session_id_len <= sizeof(hs->session_id));
867 OPENSSL_memcpy(hs->session_id, client_hello.session_id, hs->session_id_len);
868
Robert Sloana27a6a42017-09-05 08:39:28 -0700869 // Determine whether we are doing session resumption.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700870 UniquePtr<SSL_SESSION> session;
Robert Sloana27a6a42017-09-05 08:39:28 -0700871 bool tickets_supported = false, renew_ticket = false;
872 enum ssl_hs_wait_t wait = ssl_get_prev_session(
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100873 hs, &session, &tickets_supported, &renew_ticket, &client_hello);
Robert Sloana27a6a42017-09-05 08:39:28 -0700874 if (wait != ssl_hs_ok) {
875 return wait;
Steven Valdez909b19f2016-11-21 15:35:44 -0500876 }
877
Robert Sloana12bf462017-07-17 07:08:26 -0700878 if (session) {
Robert Sloana94fe052017-02-21 08:49:28 -0800879 if (session->extended_master_secret && !hs->extended_master_secret) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700880 // A ClientHello without EMS that attempts to resume a session with EMS
881 // is fatal to the connection.
Steven Valdez909b19f2016-11-21 15:35:44 -0500882 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700883 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700884 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500885 }
886
Robert Sloana12bf462017-07-17 07:08:26 -0700887 if (!ssl_session_is_resumable(hs, session.get()) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700888 // If the client offers the EMS extension, but the previous session
889 // didn't use it, then negotiate a new session.
Robert Sloana94fe052017-02-21 08:49:28 -0800890 hs->extended_master_secret != session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -0700891 session.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500892 }
893 }
894
Robert Sloana12bf462017-07-17 07:08:26 -0700895 if (session) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700896 // Use the old session.
David Benjamin1b249672016-12-06 18:25:50 -0500897 hs->ticket_expected = renew_ticket;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100898 ssl->session = std::move(session);
Robert Sloana27a6a42017-09-05 08:39:28 -0700899 ssl->s3->session_reused = true;
Pete Bentley00a7c402021-07-23 17:57:12 +0100900 hs->can_release_private_key = true;
Steven Valdez909b19f2016-11-21 15:35:44 -0500901 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500902 hs->ticket_expected = tickets_supported;
Pete Bentley00a7c402021-07-23 17:57:12 +0100903 ssl_set_session(ssl, nullptr);
904 if (!ssl_get_new_session(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700905 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500906 }
907
Pete Bentley00a7c402021-07-23 17:57:12 +0100908 // Assign a session ID if not using session tickets.
909 if (!hs->ticket_expected &&
910 (ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
911 hs->new_session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
912 RAND_bytes(hs->new_session->session_id,
913 hs->new_session->session_id_length);
Steven Valdez909b19f2016-11-21 15:35:44 -0500914 }
915 }
916
917 if (ssl->ctx->dos_protection_cb != NULL &&
918 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700919 // Connection rejected for DOS reasons.
Steven Valdez909b19f2016-11-21 15:35:44 -0500920 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700921 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700922 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500923 }
924
925 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800926 hs->new_session->cipher = hs->new_cipher;
Steven Valdez909b19f2016-11-21 15:35:44 -0500927
Robert Sloana27a6a42017-09-05 08:39:28 -0700928 // Determine whether to request a client certificate.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100929 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700930 // Only request a certificate if Channel ID isn't negotiated.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100931 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
Pete Bentley00a7c402021-07-23 17:57:12 +0100932 hs->channel_id_negotiated) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700933 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700934 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700935 // CertificateRequest may only be sent in certificate-based ciphers.
Robert Sloana94fe052017-02-21 08:49:28 -0800936 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700937 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700938 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400939
David Benjamin1b249672016-12-06 18:25:50 -0500940 if (!hs->cert_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700941 // OpenSSL returns X509_V_OK when no certificates are requested. This is
942 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800943 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400944 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800945 }
946
Robert Sloana27a6a42017-09-05 08:39:28 -0700947 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
948 // deferred. Complete it now.
Robert Sloane56da3e2017-06-26 08:26:42 -0700949 uint8_t alert = SSL_AD_DECODE_ERROR;
950 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700951 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700952 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500953 }
954
Robert Sloana27a6a42017-09-05 08:39:28 -0700955 // Now that all parameters are known, initialize the handshake hash and hash
956 // the ClientHello.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700957 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
Robert Sloan84377092017-08-14 09:33:19 -0700958 !ssl_hash_message(hs, msg)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700959 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700960 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000961 }
962
Robert Sloandc2f6092018-04-10 10:22:33 -0700963 // Handback includes the whole handshake transcript, so we cannot free the
964 // transcript buffer in the handback case.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100965 if (!hs->cert_request && !hs->handback) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700966 hs->transcript.FreeBuffer();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800967 }
968
Robert Sloan84377092017-08-14 09:33:19 -0700969 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700970
Robert Sloandc2f6092018-04-10 10:22:33 -0700971 hs->state = state12_send_server_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700972 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800973}
974
Robert Sloanc9abfe42018-11-26 12:19:07 -0800975static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) {
Michael Eastwood220a6542021-09-30 20:13:43 +0000976 out = out.subspan(out.size() - in.size());
977 assert(out.size() == in.size());
Robert Sloanc9abfe42018-11-26 12:19:07 -0800978 OPENSSL_memcpy(out.data(), in.data(), in.size());
979}
980
Robert Sloana27a6a42017-09-05 08:39:28 -0700981static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500982 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -0800983
Robert Sloana27a6a42017-09-05 08:39:28 -0700984 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
985 // known attack while we fix ChannelID itself.
Pete Bentley00a7c402021-07-23 17:57:12 +0100986 if (hs->channel_id_negotiated &&
Robert Sloana94fe052017-02-21 08:49:28 -0800987 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Pete Bentley00a7c402021-07-23 17:57:12 +0100988 hs->channel_id_negotiated = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800989 }
990
Robert Sloana27a6a42017-09-05 08:39:28 -0700991 // If this is a resumption and the original handshake didn't support
992 // ChannelID then we didn't record the original handshake hashes in the
993 // session and so cannot resume with ChannelIDs.
David Benjaminc895d6b2016-08-11 13:26:41 -0400994 if (ssl->session != NULL &&
995 ssl->session->original_handshake_hash_len == 0) {
Pete Bentley00a7c402021-07-23 17:57:12 +0100996 hs->channel_id_negotiated = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800997 }
998
Robert Sloan7d422bc2017-03-06 10:04:29 -0800999 struct OPENSSL_timeval now;
David Benjaminc895d6b2016-08-11 13:26:41 -04001000 ssl_get_current_time(ssl, &now);
1001 ssl->s3->server_random[0] = now.tv_sec >> 24;
1002 ssl->s3->server_random[1] = now.tv_sec >> 16;
1003 ssl->s3->server_random[2] = now.tv_sec >> 8;
1004 ssl->s3->server_random[3] = now.tv_sec;
1005 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001006 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001007 }
1008
Robert Sloand9e572d2018-08-27 12:27:00 -07001009 // Implement the TLS 1.3 anti-downgrade feature.
1010 if (ssl_supports_version(hs, TLS1_3_VERSION)) {
1011 if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
Robert Sloanc9abfe42018-11-26 12:19:07 -08001012 if (hs->apply_jdk11_workaround) {
1013 // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it
1014 // here. However, the signal is only effective if all TLS 1.2
1015 // ServerHellos produced by the server are marked. Thus we send a
1016 // different non-standard signal for the time being, until JDK 11.0.2 is
1017 // released and clients have updated.
1018 copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom);
1019 } else {
1020 copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom);
1021 }
Robert Sloand9e572d2018-08-27 12:27:00 -07001022 } else {
Robert Sloanc9abfe42018-11-26 12:19:07 -08001023 copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom);
Robert Sloand9e572d2018-08-27 12:27:00 -07001024 }
Robert Sloan0da43952018-01-03 15:13:14 -08001025 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001026
Pete Bentley00a7c402021-07-23 17:57:12 +01001027 Span<const uint8_t> session_id;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001028 if (ssl->session != nullptr) {
Pete Bentley00a7c402021-07-23 17:57:12 +01001029 // Echo the session ID from the ClientHello to indicate resumption.
1030 session_id = MakeConstSpan(hs->session_id, hs->session_id_len);
1031 } else {
1032 session_id = MakeConstSpan(hs->new_session->session_id,
1033 hs->new_session->session_id_length);
David Benjaminc895d6b2016-08-11 13:26:41 -04001034 }
1035
Robert Sloanb6d070c2017-07-24 08:40:01 -07001036 ScopedCBB cbb;
Pete Bentley00a7c402021-07-23 17:57:12 +01001037 CBB body, session_id_bytes;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001038 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001039 !CBB_add_u16(&body, ssl->version) ||
1040 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
Pete Bentley00a7c402021-07-23 17:57:12 +01001041 !CBB_add_u8_length_prefixed(&body, &session_id_bytes) ||
1042 !CBB_add_bytes(&session_id_bytes, session_id.data(), session_id.size()) ||
Pete Bentleyf23caaf2020-09-22 18:02:11 +01001043 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001044 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -05001045 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001046 !ssl_add_message_cbb(ssl, cbb.get())) {
Kenny Roote99801b2015-11-06 15:31:15 -08001047 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001048 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001049 }
1050
Robert Sloana27a6a42017-09-05 08:39:28 -07001051 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001052 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001053 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001054 hs->state = state12_send_server_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001055 }
1056 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001057}
1058
Robert Sloana27a6a42017-09-05 08:39:28 -07001059static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001060 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001061 ScopedCBB cbb;
Kenny Rootb8494592015-09-25 02:29:14 +00001062
Robert Sloane56da3e2017-06-26 08:26:42 -07001063 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001064 if (!ssl_has_certificate(hs)) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001065 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloana27a6a42017-09-05 08:39:28 -07001066 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001067 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001068
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001069 if (!ssl_output_cert_chain(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001070 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001071 }
1072
1073 if (hs->certificate_status_expected) {
1074 CBB body, ocsp_response;
Robert Sloana12bf462017-07-17 07:08:26 -07001075 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -07001076 SSL3_MT_CERTIFICATE_STATUS) ||
1077 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1078 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001079 !CBB_add_bytes(
1080 &ocsp_response,
1081 CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
1082 CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
Robert Sloana12bf462017-07-17 07:08:26 -07001083 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001084 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001085 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001086 }
1087 }
1088 }
1089
Robert Sloana27a6a42017-09-05 08:39:28 -07001090 // Assemble ServerKeyExchange parameters if needed.
Robert Sloane56da3e2017-06-26 08:26:42 -07001091 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1092 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1093 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001094 ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001095 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
1096 // the client and server randoms for the signing transcript.
Robert Sloane56da3e2017-06-26 08:26:42 -07001097 CBB child;
Robert Sloana12bf462017-07-17 07:08:26 -07001098 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
1099 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1100 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001101 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001102 }
1103
Robert Sloana27a6a42017-09-05 08:39:28 -07001104 // PSK ciphers begin with an identity hint.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001105 if (alg_a & SSL_aPSK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001106 size_t len = hs->config->psk_identity_hint == nullptr
1107 ? 0
1108 : strlen(hs->config->psk_identity_hint.get());
Robert Sloana12bf462017-07-17 07:08:26 -07001109 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001110 !CBB_add_bytes(&child,
1111 (const uint8_t *)hs->config->psk_identity_hint.get(),
Adam Langley4139edb2016-01-13 15:00:54 -08001112 len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001113 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001114 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001115 }
1116
Robert Sloan572a4e22017-04-17 10:52:19 -07001117 if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001118 // Determine the group to use.
David Benjamind316cba2016-06-02 16:17:39 -04001119 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -05001120 if (!tls1_get_shared_group(hs, &group_id)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001121 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001122 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001123 return ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -07001124 }
Robert Sloana94fe052017-02-21 08:49:28 -08001125 hs->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001126
Robert Sloana27a6a42017-09-05 08:39:28 -07001127 // Set up ECDH, generate a key, and emit the public half.
Robert Sloan11c28bd2018-12-17 12:09:20 -08001128 hs->key_shares[0] = SSLKeyShare::Create(group_id);
1129 if (!hs->key_shares[0] ||
Robert Sloana12bf462017-07-17 07:08:26 -07001130 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
1131 !CBB_add_u16(cbb.get(), group_id) ||
1132 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
Robert Sloan11c28bd2018-12-17 12:09:20 -08001133 !hs->key_shares[0]->Offer(&child)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001134 return ssl_hs_error;
David Benjamind316cba2016-06-02 16:17:39 -04001135 }
Adam Langley4139edb2016-01-13 15:00:54 -08001136 } else {
1137 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001138 }
1139
Robert Sloan4562e9d2017-10-02 10:26:51 -07001140 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001141 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001142 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001143 }
1144
Robert Sloandc2f6092018-04-10 10:22:33 -07001145 hs->state = state12_send_server_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -07001146 return ssl_hs_ok;
Robert Sloane56da3e2017-06-26 08:26:42 -07001147}
1148
Robert Sloana27a6a42017-09-05 08:39:28 -07001149static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001150 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001151
Robert Sloan4562e9d2017-10-02 10:26:51 -07001152 if (hs->server_params.size() == 0) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001153 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001154 return ssl_hs_ok;
1155 }
1156
Robert Sloanb6d070c2017-07-24 08:40:01 -07001157 ScopedCBB cbb;
1158 CBB body, child;
1159 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -04001160 SSL3_MT_SERVER_KEY_EXCHANGE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001161 // |hs->server_params| contains a prefix for signing.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001162 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
1163 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
1164 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001165 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001166 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001167
Robert Sloana27a6a42017-09-05 08:39:28 -07001168 // Add a signature.
Robert Sloana94fe052017-02-21 08:49:28 -08001169 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001170 if (!ssl_has_private_key(hs)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001171 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001172 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001173 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001174
Robert Sloana27a6a42017-09-05 08:39:28 -07001175 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001176 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -05001177 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001178 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001179 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001180 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001181 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001182 if (!CBB_add_u16(&body, signature_algorithm)) {
1183 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001184 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001185 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001186 }
1187 }
1188
Robert Sloana27a6a42017-09-05 08:39:28 -07001189 // Add space for the signature.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001190 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -04001191 uint8_t *ptr;
1192 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1193 !CBB_reserve(&child, &ptr, max_sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001194 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001195 }
1196
Adam Langley4139edb2016-01-13 15:00:54 -08001197 size_t sig_len;
Robert Sloane56da3e2017-06-26 08:26:42 -07001198 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
Robert Sloan921ef2c2017-10-17 09:02:20 -07001199 signature_algorithm, hs->server_params)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001200 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001201 if (!CBB_did_write(&child, sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001202 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001203 }
1204 break;
1205 case ssl_private_key_failure:
Robert Sloana27a6a42017-09-05 08:39:28 -07001206 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001207 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -07001208 return ssl_hs_private_key_operation;
Adam Langley4139edb2016-01-13 15:00:54 -08001209 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001210 }
1211
Pete Bentley00a7c402021-07-23 17:57:12 +01001212 hs->can_release_private_key = true;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001213 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001214 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +00001215 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001216
Robert Sloan4562e9d2017-10-02 10:26:51 -07001217 hs->server_params.Reset();
David Benjaminc895d6b2016-08-11 13:26:41 -04001218
Robert Sloandc2f6092018-04-10 10:22:33 -07001219 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001220 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001221}
1222
Robert Sloana27a6a42017-09-05 08:39:28 -07001223static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001224 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001225
Robert Sloanb6d070c2017-07-24 08:40:01 -07001226 ScopedCBB cbb;
1227 CBB body;
David Benjaminc895d6b2016-08-11 13:26:41 -04001228
Robert Sloane56da3e2017-06-26 08:26:42 -07001229 if (hs->cert_request) {
1230 CBB cert_types, sigalgs_cbb;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001231 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -07001232 SSL3_MT_CERTIFICATE_REQUEST) ||
1233 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1234 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001235 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001236 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -07001237 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
Tobias Thierer43be7d22020-03-02 19:23:34 +00001238 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001239 !ssl_add_client_CA_list(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001240 !ssl_add_message_cbb(ssl, cbb.get())) {
1241 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001242 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001243 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001244 }
1245
Robert Sloanb6d070c2017-07-24 08:40:01 -07001246 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1247 SSL3_MT_SERVER_HELLO_DONE) ||
1248 !ssl_add_message_cbb(ssl, cbb.get())) {
1249 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001250 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001251 }
1252
Robert Sloandc2f6092018-04-10 10:22:33 -07001253 hs->state = state12_read_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001254 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001255}
1256
Robert Sloana27a6a42017-09-05 08:39:28 -07001257static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001258 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001259
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001260 if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
1261 return ssl_hs_handback;
Robert Sloandc2f6092018-04-10 10:22:33 -07001262 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001263 if (!hs->cert_request) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001264 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001265 return ssl_hs_ok;
1266 }
David Benjamin6e899c72016-06-09 18:02:18 -04001267
Robert Sloan84377092017-08-14 09:33:19 -07001268 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001269 if (!ssl->method->get_message(ssl, &msg)) {
1270 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001271 }
1272
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001273 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001274 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001275 }
1276
Robert Sloan84377092017-08-14 09:33:19 -07001277 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001278 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001279 }
1280
Robert Sloan84377092017-08-14 09:33:19 -07001281 CBS certificate_msg = msg.body;
Robert Sloan5d625782017-02-13 09:55:39 -08001282 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001283 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
1284 hs->config->retain_only_sha256_of_client_certs
Robert Sloanfe7cd212017-08-07 09:03:39 -07001285 ? hs->new_session->peer_sha256
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001286 : nullptr,
Robert Sloanfe7cd212017-08-07 09:03:39 -07001287 &certificate_msg, ssl->ctx->pool)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001288 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001289 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001290 }
1291
Steven Valdeze7531f02016-12-14 13:29:57 -05001292 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001293 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin6e899c72016-06-09 18:02:18 -04001294 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001295 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001296 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001297 }
1298
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001299 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001300 // No client certificate so the handshake buffer may be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001301 hs->transcript.FreeBuffer();
David Benjamin6e899c72016-06-09 18:02:18 -04001302
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001303 if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001304 // Fail for TLS only if we required a certificate
David Benjamin6e899c72016-06-09 18:02:18 -04001305 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001306 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001307 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001308 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001309
Robert Sloana27a6a42017-09-05 08:39:28 -07001310 // OpenSSL returns X509_V_OK when no certificates are received. This is
1311 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -08001312 hs->new_session->verify_result = X509_V_OK;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001313 } else if (hs->config->retain_only_sha256_of_client_certs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001314 // The hash will have been filled in.
Robert Sloana94fe052017-02-21 08:49:28 -08001315 hs->new_session->peer_sha256_valid = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001316 }
David Benjamin6e899c72016-06-09 18:02:18 -04001317
Robert Sloan84377092017-08-14 09:33:19 -07001318 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001319 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001320 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001321}
1322
Robert Sloana27a6a42017-09-05 08:39:28 -07001323static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001324 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001325 switch (ssl_verify_peer_cert(hs)) {
1326 case ssl_verify_ok:
1327 break;
1328 case ssl_verify_invalid:
1329 return ssl_hs_error;
1330 case ssl_verify_retry:
1331 return ssl_hs_certificate_verify;
1332 }
1333 }
1334
Robert Sloandc2f6092018-04-10 10:22:33 -07001335 hs->state = state12_read_client_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -07001336 return ssl_hs_ok;
1337}
1338
1339static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001340 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -07001341 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001342 if (!ssl->method->get_message(ssl, &msg)) {
1343 return ssl_hs_read_message;
Robert Sloane56da3e2017-06-26 08:26:42 -07001344 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001345
Robert Sloan84377092017-08-14 09:33:19 -07001346 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001347 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001348 }
1349
Robert Sloan84377092017-08-14 09:33:19 -07001350 CBS client_key_exchange = msg.body;
Robert Sloane56da3e2017-06-26 08:26:42 -07001351 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1352 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001353
Robert Sloana27a6a42017-09-05 08:39:28 -07001354 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001355 if (alg_a & SSL_aPSK) {
1356 CBS psk_identity;
1357
Robert Sloana27a6a42017-09-05 08:39:28 -07001358 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1359 // then this is the only field in the message.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001360 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1361 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001362 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001363 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001364 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001365 }
1366
1367 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1368 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001369 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001370 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001371 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001372 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001373 char *raw = nullptr;
1374 if (!CBS_strdup(&psk_identity, &raw)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001375 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001376 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001377 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001378 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001379 hs->new_session->psk_identity.reset(raw);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001380 }
1381
Robert Sloan4562e9d2017-10-02 10:26:51 -07001382 // Depending on the key exchange method, compute |premaster_secret|.
1383 Array<uint8_t> premaster_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001384 if (alg_k & SSL_kRSA) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001385 CBS encrypted_premaster_secret;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001386 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1387 &encrypted_premaster_secret) ||
1388 CBS_len(&client_key_exchange) != 0) {
1389 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1390 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1391 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001392 }
1393
Robert Sloana27a6a42017-09-05 08:39:28 -07001394 // Allocate a buffer large enough for an RSA decryption.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001395 Array<uint8_t> decrypt_buf;
1396 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1397 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001398 }
1399
Robert Sloana27a6a42017-09-05 08:39:28 -07001400 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1401 // timing-sensitive code below.
Adam Langley4139edb2016-01-13 15:00:54 -08001402 size_t decrypt_len;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001403 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1404 decrypt_buf.size(),
Robert Sloan921ef2c2017-10-17 09:02:20 -07001405 encrypted_premaster_secret)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001406 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001407 break;
1408 case ssl_private_key_failure:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001409 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001410 case ssl_private_key_retry:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001411 return ssl_hs_private_key_operation;
Kenny Roote99801b2015-11-06 15:31:15 -08001412 }
1413
Robert Sloan4562e9d2017-10-02 10:26:51 -07001414 if (decrypt_len != decrypt_buf.size()) {
David Benjamin4969cc92016-04-22 15:02:23 -04001415 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001416 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001417 return ssl_hs_error;
David Benjamin4969cc92016-04-22 15:02:23 -04001418 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001419
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001420 CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len);
1421
Robert Sloana27a6a42017-09-05 08:39:28 -07001422 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1423 // section 7.4.7.1.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001424 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1425 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1426 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001427 }
1428
Robert Sloana27a6a42017-09-05 08:39:28 -07001429 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1430 // publicly invalid.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001431 if (decrypt_len < 11 + premaster_secret.size()) {
Adam Langley4139edb2016-01-13 15:00:54 -08001432 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001433 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001434 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001435 }
1436
Robert Sloana27a6a42017-09-05 08:39:28 -07001437 // Check the padding. See RFC 3447, section 7.2.2.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001438 size_t padding_len = decrypt_len - premaster_secret.size();
Adam Langley4139edb2016-01-13 15:00:54 -08001439 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1440 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001441 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001442 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1443 }
1444 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1445
Robert Sloana27a6a42017-09-05 08:39:28 -07001446 // The premaster secret must begin with |client_version|. This too must be
1447 // checked in constant time (http://eprint.iacr.org/2003/052/).
Adam Langley4139edb2016-01-13 15:00:54 -08001448 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001449 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001450 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001451 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001452
Robert Sloana27a6a42017-09-05 08:39:28 -07001453 // Select, in constant time, either the decrypted premaster or the random
1454 // premaster based on |good|.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001455 for (size_t i = 0; i < premaster_secret.size(); i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001456 premaster_secret[i] = constant_time_select_8(
1457 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1458 }
Robert Sloan572a4e22017-04-17 10:52:19 -07001459 } else if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001460 // Parse the ClientKeyExchange.
Adam Langley4139edb2016-01-13 15:00:54 -08001461 CBS peer_key;
Robert Sloan572a4e22017-04-17 10:52:19 -07001462 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001463 CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001464 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001465 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001466 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001467 }
1468
Robert Sloana27a6a42017-09-05 08:39:28 -07001469 // Compute the premaster.
Robert Sloan5d625782017-02-13 09:55:39 -08001470 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan11c28bd2018-12-17 12:09:20 -08001471 if (!hs->key_shares[0]->Finish(&premaster_secret, &alert, peer_key)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001472 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001473 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001474 }
1475
Robert Sloana27a6a42017-09-05 08:39:28 -07001476 // The key exchange state may now be discarded.
Robert Sloan11c28bd2018-12-17 12:09:20 -08001477 hs->key_shares[0].reset();
1478 hs->key_shares[1].reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001479 } else if (!(alg_k & SSL_kPSK)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001480 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001481 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001482 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001483 }
1484
Robert Sloana27a6a42017-09-05 08:39:28 -07001485 // For a PSK cipher suite, the actual pre-master secret is combined with the
1486 // pre-shared key.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001487 if (alg_a & SSL_aPSK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001488 if (hs->config->psk_server_callback == NULL) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001489 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001490 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001491 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001492 }
1493
Robert Sloana27a6a42017-09-05 08:39:28 -07001494 // Look up the key for the identity.
Robert Sloane56da3e2017-06-26 08:26:42 -07001495 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001496 unsigned psk_len = hs->config->psk_server_callback(
1497 ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
Robert Sloane56da3e2017-06-26 08:26:42 -07001498 if (psk_len > PSK_MAX_PSK_LEN) {
1499 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001500 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001501 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001502 } else if (psk_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001503 // PSK related to the given identity not found.
Robert Sloane56da3e2017-06-26 08:26:42 -07001504 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001505 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001506 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001507 }
1508
1509 if (alg_k & SSL_kPSK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001510 // In plain PSK, other_secret is a block of 0s with the same length as the
1511 // pre-shared key.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001512 if (!premaster_secret.Init(psk_len)) {
1513 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001514 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07001515 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
Robert Sloane56da3e2017-06-26 08:26:42 -07001516 }
1517
Robert Sloanb6d070c2017-07-24 08:40:01 -07001518 ScopedCBB new_premaster;
1519 CBB child;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001520 if (!CBB_init(new_premaster.get(),
Robert Sloan4562e9d2017-10-02 10:26:51 -07001521 2 + psk_len + 2 + premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001522 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07001523 !CBB_add_bytes(&child, premaster_secret.data(),
1524 premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001525 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001526 !CBB_add_bytes(&child, psk, psk_len) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001527 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001528 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001529 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001530 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001531 }
1532
Robert Sloan84377092017-08-14 09:33:19 -07001533 if (!ssl_hash_message(hs, msg)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001534 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001535 }
1536
Robert Sloana27a6a42017-09-05 08:39:28 -07001537 // Compute the master secret.
Pete Bentley17486112021-01-20 11:51:47 +00001538 hs->new_session->secret_length = tls1_generate_master_secret(
1539 hs, hs->new_session->secret, premaster_secret);
1540 if (hs->new_session->secret_length == 0) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001541 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001542 }
Robert Sloana94fe052017-02-21 08:49:28 -08001543 hs->new_session->extended_master_secret = hs->extended_master_secret;
Pete Bentley17486112021-01-20 11:51:47 +00001544 CONSTTIME_DECLASSIFY(hs->new_session->secret, hs->new_session->secret_length);
Pete Bentley00a7c402021-07-23 17:57:12 +01001545 hs->can_release_private_key = true;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001546
Robert Sloan84377092017-08-14 09:33:19 -07001547 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001548 hs->state = state12_read_client_certificate_verify;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001549 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001550}
1551
Robert Sloana27a6a42017-09-05 08:39:28 -07001552static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001553 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001554
Robert Sloana27a6a42017-09-05 08:39:28 -07001555 // Only RSA and ECDSA client certificates are supported, so a
1556 // CertificateVerify is required if and only if there's a client certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001557 if (!hs->peer_pubkey) {
1558 hs->transcript.FreeBuffer();
Robert Sloandc2f6092018-04-10 10:22:33 -07001559 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001560 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001561 }
1562
Robert Sloan84377092017-08-14 09:33:19 -07001563 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001564 if (!ssl->method->get_message(ssl, &msg)) {
1565 return ssl_hs_read_message;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001566 }
1567
Robert Sloan84377092017-08-14 09:33:19 -07001568 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001569 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001570 }
1571
Pete Bentleyf23caaf2020-09-22 18:02:11 +01001572 // The peer certificate must be valid for signing.
1573 const CRYPTO_BUFFER *leaf =
1574 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1575 CBS leaf_cbs;
1576 CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1577 if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) {
1578 return ssl_hs_error;
1579 }
1580
Robert Sloan84377092017-08-14 09:33:19 -07001581 CBS certificate_verify = msg.body, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001582
Robert Sloana27a6a42017-09-05 08:39:28 -07001583 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001584 uint16_t signature_algorithm = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001585 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001586 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001587 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001588 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001589 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001590 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001591 uint8_t alert = SSL_AD_DECODE_ERROR;
Tobias Thierer43be7d22020-03-02 19:23:34 +00001592 if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001593 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001594 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001595 }
Robert Sloana94fe052017-02-21 08:49:28 -08001596 hs->new_session->peer_signature_algorithm = signature_algorithm;
Robert Sloan2424d842017-05-01 07:46:28 -07001597 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001598 hs->peer_pubkey.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001599 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001600 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001601 return ssl_hs_error;
Adam Langleye9ada862015-05-11 17:20:37 -07001602 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001603
Robert Sloana27a6a42017-09-05 08:39:28 -07001604 // Parse and verify the signature.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001605 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1606 CBS_len(&certificate_verify) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001607 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001608 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001609 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001610 }
1611
Pete Bentley0c61efe2019-08-13 09:32:23 +01001612 if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1613 hs->peer_pubkey.get(), hs->transcript.buffer())) {
Kenny Rootb8494592015-09-25 02:29:14 +00001614 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001615 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001616 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001617 }
1618
Robert Sloana27a6a42017-09-05 08:39:28 -07001619 // The handshake buffer is no longer necessary, and we may hash the current
1620 // message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001621 hs->transcript.FreeBuffer();
Robert Sloan84377092017-08-14 09:33:19 -07001622 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001623 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001624 }
1625
Robert Sloan84377092017-08-14 09:33:19 -07001626 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001627 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001628 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001629}
1630
Robert Sloana27a6a42017-09-05 08:39:28 -07001631static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001632 if (hs->handback && hs->ssl->session != NULL) {
1633 return ssl_hs_handback;
1634 }
Robert Sloandc2f6092018-04-10 10:22:33 -07001635 hs->state = state12_process_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001636 return ssl_hs_read_change_cipher_spec;
1637}
1638
1639static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001640 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001641 return ssl_hs_error;
1642 }
1643
Robert Sloandc2f6092018-04-10 10:22:33 -07001644 hs->state = state12_read_next_proto;
Robert Sloana27a6a42017-09-05 08:39:28 -07001645 return ssl_hs_ok;
1646}
1647
1648static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001649 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001650
1651 if (!hs->next_proto_neg_seen) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001652 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001653 return ssl_hs_ok;
1654 }
1655
Robert Sloan84377092017-08-14 09:33:19 -07001656 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001657 if (!ssl->method->get_message(ssl, &msg)) {
1658 return ssl_hs_read_message;
David Benjaminc895d6b2016-08-11 13:26:41 -04001659 }
1660
Robert Sloan84377092017-08-14 09:33:19 -07001661 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1662 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001663 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001664 }
1665
Robert Sloan84377092017-08-14 09:33:19 -07001666 CBS next_protocol = msg.body, selected_protocol, padding;
David Benjamin6e899c72016-06-09 18:02:18 -04001667 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1668 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001669 CBS_len(&next_protocol) != 0) {
1670 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001671 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001672 return ssl_hs_error;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001673 }
1674
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001675 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001676 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001677 }
1678
Robert Sloan84377092017-08-14 09:33:19 -07001679 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001680 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001681 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001682}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001683
Robert Sloana27a6a42017-09-05 08:39:28 -07001684static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001685 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001686
Pete Bentley00a7c402021-07-23 17:57:12 +01001687 if (!hs->channel_id_negotiated) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001688 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001689 return ssl_hs_ok;
1690 }
1691
Robert Sloan84377092017-08-14 09:33:19 -07001692 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001693 if (!ssl->method->get_message(ssl, &msg)) {
1694 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001695 }
1696
Robert Sloan84377092017-08-14 09:33:19 -07001697 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1698 !tls1_verify_channel_id(hs, msg) ||
1699 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001700 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001701 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001702
Robert Sloan84377092017-08-14 09:33:19 -07001703 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001704 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001705 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001706}
1707
Robert Sloana27a6a42017-09-05 08:39:28 -07001708static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1709 SSL *const ssl = hs->ssl;
1710 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1711 if (wait != ssl_hs_ok) {
1712 return wait;
1713 }
1714
1715 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001716 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001717 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001718 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001719 }
1720
1721 // If this is a full handshake with ChannelID then record the handshake
1722 // hashes in |hs->new_session| in case we need them to verify a
1723 // ChannelID signature on a resumption of this session in the future.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001724 if (ssl->session == NULL && ssl->s3->channel_id_valid &&
Robert Sloana27a6a42017-09-05 08:39:28 -07001725 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1726 return ssl_hs_error;
1727 }
1728
1729 return ssl_hs_ok;
1730}
1731
1732static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001733 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001734
1735 if (hs->ticket_expected) {
1736 const SSL_SESSION *session;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001737 UniquePtr<SSL_SESSION> session_copy;
Robert Sloane56da3e2017-06-26 08:26:42 -07001738 if (ssl->session == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001739 // Fix the timeout to measure from the ticket issuance time.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001740 ssl_session_rebase_time(ssl, hs->new_session.get());
1741 session = hs->new_session.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001742 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001743 // We are renewing an existing session. Duplicate the session to adjust
1744 // the timeout.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001745 session_copy =
1746 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001747 if (!session_copy) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001748 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001749 }
1750
Robert Sloanb6d070c2017-07-24 08:40:01 -07001751 ssl_session_rebase_time(ssl, session_copy.get());
1752 session = session_copy.get();
Steven Valdez909b19f2016-11-21 15:35:44 -05001753 }
1754
Robert Sloanb6d070c2017-07-24 08:40:01 -07001755 ScopedCBB cbb;
1756 CBB body, ticket;
1757 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1758 SSL3_MT_NEW_SESSION_TICKET) ||
1759 !CBB_add_u32(&body, session->timeout) ||
1760 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001761 !ssl_encrypt_ticket(hs, &ticket, session) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001762 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001763 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001764 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001765 }
1766
Robert Sloane56da3e2017-06-26 08:26:42 -07001767 if (!ssl->method->add_change_cipher_spec(ssl) ||
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001768 !tls1_change_cipher_state(hs, evp_aead_seal) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001769 !ssl_send_finished(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001770 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001771 }
Adam Langleye9ada862015-05-11 17:20:37 -07001772
Robert Sloana27a6a42017-09-05 08:39:28 -07001773 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001774 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001775 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001776 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001777 }
1778 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001779}
Robert Sloanb6d070c2017-07-24 08:40:01 -07001780
Robert Sloana27a6a42017-09-05 08:39:28 -07001781static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1782 SSL *const ssl = hs->ssl;
1783
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001784 if (hs->handback) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001785 return ssl_hs_handback;
1786 }
1787
Robert Sloana27a6a42017-09-05 08:39:28 -07001788 ssl->method->on_handshake_complete(ssl);
1789
1790 // If we aren't retaining peer certificates then we can discard it now.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001791 if (hs->new_session != NULL &&
1792 hs->config->retain_only_sha256_of_client_certs) {
1793 hs->new_session->certs.reset();
Robert Sloana27a6a42017-09-05 08:39:28 -07001794 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1795 }
1796
Pete Bentley00a7c402021-07-23 17:57:12 +01001797 bool has_new_session = hs->new_session != nullptr;
1798 if (has_new_session) {
1799 assert(ssl->session == nullptr);
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001800 ssl->s3->established_session = std::move(hs->new_session);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001801 ssl->s3->established_session->not_resumable = false;
Pete Bentley00a7c402021-07-23 17:57:12 +01001802 } else {
1803 assert(ssl->session != nullptr);
1804 ssl->s3->established_session = UpRef(ssl->session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001805 }
1806
1807 hs->handshake_finalized = true;
1808 ssl->s3->initial_handshake_complete = true;
Pete Bentley00a7c402021-07-23 17:57:12 +01001809 if (has_new_session) {
1810 ssl_update_cache(ssl);
1811 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001812
Robert Sloandc2f6092018-04-10 10:22:33 -07001813 hs->state = state12_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001814 return ssl_hs_ok;
1815}
1816
1817enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001818 while (hs->state != state12_done) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001819 enum ssl_hs_wait_t ret = ssl_hs_error;
Robert Sloandc2f6092018-04-10 10:22:33 -07001820 enum tls12_server_hs_state_t state =
1821 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001822 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001823 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001824 ret = do_start_accept(hs);
1825 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001826 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001827 ret = do_read_client_hello(hs);
1828 break;
Pete Bentley00a7c402021-07-23 17:57:12 +01001829 case state12_read_client_hello_after_ech:
1830 ret = do_read_client_hello_after_ech(hs);
1831 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001832 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001833 ret = do_select_certificate(hs);
1834 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001835 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001836 ret = do_tls13(hs);
1837 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001838 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001839 ret = do_select_parameters(hs);
1840 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001841 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001842 ret = do_send_server_hello(hs);
1843 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001844 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001845 ret = do_send_server_certificate(hs);
1846 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001847 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001848 ret = do_send_server_key_exchange(hs);
1849 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001850 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001851 ret = do_send_server_hello_done(hs);
1852 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001853 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001854 ret = do_read_client_certificate(hs);
1855 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001856 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001857 ret = do_verify_client_certificate(hs);
1858 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001859 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001860 ret = do_read_client_key_exchange(hs);
1861 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001862 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001863 ret = do_read_client_certificate_verify(hs);
1864 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001865 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001866 ret = do_read_change_cipher_spec(hs);
1867 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001868 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001869 ret = do_process_change_cipher_spec(hs);
1870 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001871 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001872 ret = do_read_next_proto(hs);
1873 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001874 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001875 ret = do_read_channel_id(hs);
1876 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001877 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001878 ret = do_read_client_finished(hs);
1879 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001880 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001881 ret = do_send_server_finished(hs);
1882 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001883 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001884 ret = do_finish_server_handshake(hs);
1885 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001886 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001887 ret = ssl_hs_ok;
1888 break;
1889 }
1890
1891 if (hs->state != state) {
1892 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1893 }
1894
1895 if (ret != ssl_hs_ok) {
1896 return ret;
1897 }
1898 }
1899
1900 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1901 return ssl_hs_ok;
1902}
1903
1904const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001905 enum tls12_server_hs_state_t state =
1906 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001907 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001908 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001909 return "TLS server start_accept";
Robert Sloandc2f6092018-04-10 10:22:33 -07001910 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001911 return "TLS server read_client_hello";
Pete Bentley00a7c402021-07-23 17:57:12 +01001912 case state12_read_client_hello_after_ech:
1913 return "TLS server read_client_hello_after_ech";
Robert Sloandc2f6092018-04-10 10:22:33 -07001914 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001915 return "TLS server select_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001916 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001917 return tls13_server_handshake_state(hs);
Robert Sloandc2f6092018-04-10 10:22:33 -07001918 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001919 return "TLS server select_parameters";
Robert Sloandc2f6092018-04-10 10:22:33 -07001920 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001921 return "TLS server send_server_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001922 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001923 return "TLS server send_server_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001924 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001925 return "TLS server send_server_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001926 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001927 return "TLS server send_server_hello_done";
Robert Sloandc2f6092018-04-10 10:22:33 -07001928 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001929 return "TLS server read_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001930 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001931 return "TLS server verify_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001932 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001933 return "TLS server read_client_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001934 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001935 return "TLS server read_client_certificate_verify";
Robert Sloandc2f6092018-04-10 10:22:33 -07001936 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001937 return "TLS server read_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001938 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001939 return "TLS server process_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001940 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001941 return "TLS server read_next_proto";
Robert Sloandc2f6092018-04-10 10:22:33 -07001942 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001943 return "TLS server read_channel_id";
Robert Sloandc2f6092018-04-10 10:22:33 -07001944 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001945 return "TLS server read_client_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001946 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001947 return "TLS server send_server_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001948 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001949 return "TLS server finish_server_handshake";
Robert Sloandc2f6092018-04-10 10:22:33 -07001950 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001951 return "TLS server done";
1952 }
1953
1954 return "TLS server unknown";
1955}
1956
Robert Sloan726e9d12018-09-11 11:45:04 -07001957BSSL_NAMESPACE_END