blob: 26227d3417773da14e364531203218d4edaf70d9 [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>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157#include <openssl/ec.h>
158#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700159#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160#include <openssl/evp.h>
161#include <openssl/hmac.h>
162#include <openssl/md5.h>
163#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400164#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800165#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800166#include <openssl/x509.h>
167
Adam Langleye9ada862015-05-11 17:20:37 -0700168#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800169#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170
171
Robert Sloan726e9d12018-09-11 11:45:04 -0700172BSSL_NAMESPACE_BEGIN
Robert Sloanb6d070c2017-07-24 08:40:01 -0700173
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100174bool ssl_client_cipher_list_contains_cipher(
175 const SSL_CLIENT_HELLO *client_hello, uint16_t id) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400176 CBS cipher_suites;
177 CBS_init(&cipher_suites, client_hello->cipher_suites,
178 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179
David Benjaminc895d6b2016-08-11 13:26:41 -0400180 while (CBS_len(&cipher_suites) > 0) {
181 uint16_t got_id;
182 if (!CBS_get_u16(&cipher_suites, &got_id)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100183 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400184 }
185
186 if (got_id == id) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100187 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400188 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800189 }
190
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100191 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192}
193
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100194static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
195 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500196 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800197 assert(!ssl->s3->have_version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700198 CBS supported_versions, versions;
David Benjamin1b249672016-12-06 18:25:50 -0500199 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
200 TLSEXT_TYPE_supported_versions)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400201 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
202 CBS_len(&supported_versions) != 0 ||
203 CBS_len(&versions) == 0) {
204 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
205 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100206 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400207 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400208 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -0700209 // Convert the ClientHello version to an equivalent supported_versions
210 // extension.
Robert Sloanf6200e72017-07-10 08:09:18 -0700211 static const uint8_t kTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700212 0x03, 0x03, // TLS 1.2
213 0x03, 0x02, // TLS 1.1
214 0x03, 0x01, // TLS 1
Robert Sloanf6200e72017-07-10 08:09:18 -0700215 };
216
217 static const uint8_t kDTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700218 0xfe, 0xfd, // DTLS 1.2
219 0xfe, 0xff, // DTLS 1.0
Robert Sloanf6200e72017-07-10 08:09:18 -0700220 };
221
222 size_t versions_len = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400223 if (SSL_is_dtls(ssl)) {
224 if (client_hello->version <= DTLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700225 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400226 } else if (client_hello->version <= DTLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700227 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400228 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700229 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
230 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400231 } else {
232 if (client_hello->version >= TLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700233 versions_len = 6;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100234 } else if (client_hello->version >= TLS1_1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700235 versions_len = 4;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100236 } else if (client_hello->version >= TLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700237 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400238 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700239 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
240 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400241 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242 }
243
Robert Sloanf6200e72017-07-10 08:09:18 -0700244 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100245 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 }
247
Robert Sloana27a6a42017-09-05 08:39:28 -0700248 // At this point, the connection's version is known and |ssl->version| is
249 // fixed. Begin enforcing the record-layer version.
250 ssl->s3->have_version = true;
Robert Sloandb4251a2017-09-18 09:38:15 -0700251 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252
Robert Sloana27a6a42017-09-05 08:39:28 -0700253 // Handle FALLBACK_SCSV.
Robert Sloanf6200e72017-07-10 08:09:18 -0700254 if (ssl_client_cipher_list_contains_cipher(client_hello,
255 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Robert Sloan921ef2c2017-10-17 09:02:20 -0700256 ssl_protocol_version(ssl) < hs->max_version) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700257 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
258 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100259 return false;
Robert Sloanf6200e72017-07-10 08:09:18 -0700260 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400261
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100262 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800263}
264
Robert Sloanfe7cd212017-08-07 09:03:39 -0700265static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
266 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500267 CBS cipher_suites;
268 CBS_init(&cipher_suites, client_hello->cipher_suites,
269 client_hello->cipher_suites_len);
270
Robert Sloanfe7cd212017-08-07 09:03:39 -0700271 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
272 if (!sk) {
David Benjamin1b249672016-12-06 18:25:50 -0500273 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700274 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500275 }
276
277 while (CBS_len(&cipher_suites) > 0) {
278 uint16_t cipher_suite;
279
280 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
281 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700282 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500283 }
284
285 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700286 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500287 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700288 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500289 }
290 }
291
292 return sk;
David Benjamin1b249672016-12-06 18:25:50 -0500293}
294
Robert Sloana27a6a42017-09-05 08:39:28 -0700295// ssl_get_compatible_server_ciphers determines the key exchange and
296// authentication cipher suite masks compatible with the server configuration
297// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
298// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin1b249672016-12-06 18:25:50 -0500299static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
300 uint32_t *out_mask_k,
301 uint32_t *out_mask_a) {
David Benjamin1b249672016-12-06 18:25:50 -0500302 uint32_t mask_k = 0;
303 uint32_t mask_a = 0;
304
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800305 if (ssl_has_certificate(hs)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700306 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
307 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin1b249672016-12-06 18:25:50 -0500308 mask_k |= SSL_kRSA;
David Benjamin1b249672016-12-06 18:25:50 -0500309 }
310 }
311
Robert Sloana27a6a42017-09-05 08:39:28 -0700312 // Check for a shared group to consider ECDHE ciphers.
David Benjamin1b249672016-12-06 18:25:50 -0500313 uint16_t unused;
314 if (tls1_get_shared_group(hs, &unused)) {
315 mask_k |= SSL_kECDHE;
316 }
317
Robert Sloana27a6a42017-09-05 08:39:28 -0700318 // PSK requires a server callback.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100319 if (hs->config->psk_server_callback != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -0500320 mask_k |= SSL_kPSK;
321 mask_a |= SSL_aPSK;
322 }
323
324 *out_mask_k = mask_k;
325 *out_mask_a = mask_a;
326}
327
Tobias Thierer43be7d22020-03-02 19:23:34 +0000328static const SSL_CIPHER *choose_cipher(
David Benjamin1b249672016-12-06 18:25:50 -0500329 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
Robert Sloanc6ebb282018-04-30 10:10:26 -0700330 const SSLCipherPreferenceList *server_pref) {
David Benjamin1b249672016-12-06 18:25:50 -0500331 SSL *const ssl = hs->ssl;
Robert Sloan15c0b352018-04-16 08:36:46 -0700332 const STACK_OF(SSL_CIPHER) *prio, *allow;
Robert Sloana27a6a42017-09-05 08:39:28 -0700333 // in_group_flags will either be NULL, or will point to an array of bytes
334 // which indicate equal-preference groups in the |prio| stack. See the
Robert Sloanc6ebb282018-04-30 10:10:26 -0700335 // comment about |in_group_flags| in the |SSLCipherPreferenceList|
Robert Sloana27a6a42017-09-05 08:39:28 -0700336 // struct.
Robert Sloanc6ebb282018-04-30 10:10:26 -0700337 const bool *in_group_flags;
Robert Sloana27a6a42017-09-05 08:39:28 -0700338 // group_min contains the minimal index so far found in a group, or -1 if no
339 // such value exists yet.
David Benjamin1b249672016-12-06 18:25:50 -0500340 int group_min = -1;
341
Robert Sloanfe7cd212017-08-07 09:03:39 -0700342 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
Robert Sloan6f79a502017-04-03 09:16:40 -0700343 ssl_parse_client_cipher_list(client_hello);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700344 if (!client_pref) {
345 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500346 }
347
348 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Robert Sloanc6ebb282018-04-30 10:10:26 -0700349 prio = server_pref->ciphers.get();
David Benjamin1b249672016-12-06 18:25:50 -0500350 in_group_flags = server_pref->in_group_flags;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700351 allow = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500352 } else {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700353 prio = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500354 in_group_flags = NULL;
Robert Sloanc6ebb282018-04-30 10:10:26 -0700355 allow = server_pref->ciphers.get();
David Benjamin1b249672016-12-06 18:25:50 -0500356 }
357
Robert Sloan6f79a502017-04-03 09:16:40 -0700358 uint32_t mask_k, mask_a;
David Benjamin1b249672016-12-06 18:25:50 -0500359 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
360
361 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700362 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin1b249672016-12-06 18:25:50 -0500363
Robert Sloan6f79a502017-04-03 09:16:40 -0700364 size_t cipher_index;
Robert Sloana27a6a42017-09-05 08:39:28 -0700365 if (// Check if the cipher is supported for the current version.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700366 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
367 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700368 // Check the cipher is supported for the server configuration.
Robert Sloan6f79a502017-04-03 09:16:40 -0700369 (c->algorithm_mkey & mask_k) &&
370 (c->algorithm_auth & mask_a) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700371 // Check the cipher is in the |allow| list.
Robert Sloan6f79a502017-04-03 09:16:40 -0700372 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
Robert Sloanc6ebb282018-04-30 10:10:26 -0700373 if (in_group_flags != NULL && in_group_flags[i]) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700374 // This element of |prio| is in a group. Update the minimum index found
375 // so far and continue looking.
David Benjamin1b249672016-12-06 18:25:50 -0500376 if (group_min == -1 || (size_t)group_min > cipher_index) {
377 group_min = cipher_index;
378 }
379 } else {
380 if (group_min != -1 && (size_t)group_min < cipher_index) {
381 cipher_index = group_min;
382 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700383 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin1b249672016-12-06 18:25:50 -0500384 }
385 }
386
Robert Sloanc6ebb282018-04-30 10:10:26 -0700387 if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700388 // We are about to leave a group, but we found a match in it, so that's
389 // our answer.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700390 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin1b249672016-12-06 18:25:50 -0500391 }
392 }
393
Robert Sloanfe7cd212017-08-07 09:03:39 -0700394 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500395}
396
Robert Sloana27a6a42017-09-05 08:39:28 -0700397static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
398 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
Robert Sloandc2f6092018-04-10 10:22:33 -0700399 hs->state = state12_read_client_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700400 return ssl_hs_ok;
401}
402
Robert Sloanc9abfe42018-11-26 12:19:07 -0800403// is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800404// from a JDK 11 client with both TLS 1.3 and a prior version enabled.
Robert Sloanc9abfe42018-11-26 12:19:07 -0800405static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) {
406 // JDK 11 ClientHellos contain a number of unusual properties which should
407 // limit false positives.
408
409 // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern
410 // clients implement ChaCha20-Poly1305.
411 if (ssl_client_cipher_list_contains_cipher(
412 client_hello, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
413 return false;
414 }
415
416 // JDK 11 always sends extensions in a particular order.
417 constexpr uint16_t kMaxFragmentLength = 0x0001;
418 constexpr uint16_t kStatusRequestV2 = 0x0011;
419 static CONSTEXPR_ARRAY struct {
420 uint16_t id;
421 bool required;
422 } kJavaExtensions[] = {
423 {TLSEXT_TYPE_server_name, false},
424 {kMaxFragmentLength, false},
425 {TLSEXT_TYPE_status_request, false},
426 {TLSEXT_TYPE_supported_groups, true},
427 {TLSEXT_TYPE_ec_point_formats, false},
428 {TLSEXT_TYPE_signature_algorithms, true},
429 // Java always sends signature_algorithms_cert.
430 {TLSEXT_TYPE_signature_algorithms_cert, true},
431 {TLSEXT_TYPE_application_layer_protocol_negotiation, false},
432 {kStatusRequestV2, false},
433 {TLSEXT_TYPE_extended_master_secret, false},
434 {TLSEXT_TYPE_supported_versions, true},
435 {TLSEXT_TYPE_cookie, false},
436 {TLSEXT_TYPE_psk_key_exchange_modes, true},
437 {TLSEXT_TYPE_key_share, true},
438 {TLSEXT_TYPE_renegotiate, false},
439 {TLSEXT_TYPE_pre_shared_key, false},
440 };
441 Span<const uint8_t> sigalgs, sigalgs_cert;
442 bool has_status_request = false, has_status_request_v2 = false;
443 CBS extensions, supported_groups;
444 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
445 for (const auto &java_extension : kJavaExtensions) {
446 CBS copy = extensions;
447 uint16_t id;
448 if (CBS_get_u16(&copy, &id) && id == java_extension.id) {
449 // The next extension is the one we expected.
450 extensions = copy;
451 CBS body;
452 if (!CBS_get_u16_length_prefixed(&extensions, &body)) {
453 return false;
454 }
455 switch (id) {
456 case TLSEXT_TYPE_status_request:
457 has_status_request = true;
458 break;
459 case kStatusRequestV2:
460 has_status_request_v2 = true;
461 break;
462 case TLSEXT_TYPE_signature_algorithms:
463 sigalgs = body;
464 break;
465 case TLSEXT_TYPE_signature_algorithms_cert:
466 sigalgs_cert = body;
467 break;
468 case TLSEXT_TYPE_supported_groups:
469 supported_groups = body;
470 break;
471 }
472 } else if (java_extension.required) {
473 return false;
474 }
475 }
476 if (CBS_len(&extensions) != 0) {
477 return false;
478 }
479
480 // JDK 11 never advertises X25519. It is not offered by default, and
481 // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern
482 // clients implement X25519.
483 while (CBS_len(&supported_groups) > 0) {
484 uint16_t group;
485 if (!CBS_get_u16(&supported_groups, &group) ||
486 group == SSL_CURVE_X25519) {
487 return false;
488 }
489 }
490
491 if (// JDK 11 always sends the same contents in signature_algorithms and
492 // signature_algorithms_cert. This is unusual: signature_algorithms_cert,
493 // if omitted, is treated as if it were signature_algorithms.
494 sigalgs != sigalgs_cert ||
495 // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it
496 // sends status_request. This is unusual: status_request_v2 is not widely
497 // implemented.
498 has_status_request != has_status_request_v2) {
499 return false;
500 }
501
502 return true;
503}
504
Pete Bentley0c61efe2019-08-13 09:32:23 +0100505static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert,
506 const SSL_CLIENT_HELLO *client_hello) {
507 SSL *const ssl = hs->ssl;
508 CBS sni;
509 if (!ssl_client_hello_get_extension(client_hello, &sni,
510 TLSEXT_TYPE_server_name)) {
511 // No SNI extension to parse.
512 return true;
513 }
514
515 CBS server_name_list, host_name;
516 uint8_t name_type;
517 if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) ||
518 !CBS_get_u8(&server_name_list, &name_type) ||
519 // Although the server_name extension was intended to be extensible to
520 // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
521 // different name types will cause an error. Further, RFC 4366 originally
522 // defined syntax inextensibly. RFC 6066 corrected this mistake, but
523 // adding new name types is no longer feasible.
524 //
525 // Act as if the extensibility does not exist to simplify parsing.
526 !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
527 CBS_len(&server_name_list) != 0 ||
528 CBS_len(&sni) != 0) {
529 *out_alert = SSL_AD_DECODE_ERROR;
530 return false;
531 }
532
533 if (name_type != TLSEXT_NAMETYPE_host_name ||
534 CBS_len(&host_name) == 0 ||
535 CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
536 CBS_contains_zero_byte(&host_name)) {
537 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
538 return false;
539 }
540
541 // Copy the hostname as a string.
542 char *raw = nullptr;
543 if (!CBS_strdup(&host_name, &raw)) {
544 *out_alert = SSL_AD_INTERNAL_ERROR;
545 return false;
546 }
547 ssl->s3->hostname.reset(raw);
548
549 hs->should_ack_sni = true;
550 return true;
551}
552
Robert Sloana27a6a42017-09-05 08:39:28 -0700553static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500554 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700555
Robert Sloan84377092017-08-14 09:33:19 -0700556 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700557 if (!ssl->method->get_message(ssl, &msg)) {
558 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700559 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700560
Robert Sloan84377092017-08-14 09:33:19 -0700561 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700562 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400563 }
564
David Benjamin1b249672016-12-06 18:25:50 -0500565 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700566 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400567 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700568 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700569 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400570 }
571
Tobias Thierer43be7d22020-03-02 19:23:34 +0000572 // ClientHello should be the end of the flight. We check this early to cover
573 // all protocol versions.
574 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
575 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
576 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
577 return ssl_hs_error;
578 }
579
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700580 if (hs->config->handoff) {
581 return ssl_hs_handoff;
582 }
583
Pete Bentley0c61efe2019-08-13 09:32:23 +0100584 uint8_t alert = SSL_AD_DECODE_ERROR;
585 if (!extract_sni(hs, &alert, &client_hello)) {
586 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
587 return ssl_hs_error;
588 }
589
Robert Sloana27a6a42017-09-05 08:39:28 -0700590 // Run the early callback.
Robert Sloan5d625782017-02-13 09:55:39 -0800591 if (ssl->ctx->select_certificate_cb != NULL) {
592 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700593 case ssl_select_cert_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700594 return ssl_hs_certificate_selection_pending;
David Benjaminc895d6b2016-08-11 13:26:41 -0400595
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700596 case ssl_select_cert_error:
Robert Sloana27a6a42017-09-05 08:39:28 -0700597 // Connection rejected.
Robert Sloan5d625782017-02-13 09:55:39 -0800598 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700599 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700600 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400601
Robert Sloan5d625782017-02-13 09:55:39 -0800602 default:
603 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400604 }
Robert Sloan5d625782017-02-13 09:55:39 -0800605 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800606
Robert Sloana27a6a42017-09-05 08:39:28 -0700607 // Freeze the version range after the early callback.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100608 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700609 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700610 }
611
Robert Sloanc9abfe42018-11-26 12:19:07 -0800612 if (hs->config->jdk11_workaround &&
613 is_probably_jdk11_with_tls13(&client_hello)) {
614 hs->apply_jdk11_workaround = true;
615 }
616
Robert Sloan5d625782017-02-13 09:55:39 -0800617 if (!negotiate_version(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700618 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700619 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800620 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800621
Robert Sloanf6200e72017-07-10 08:09:18 -0700622 hs->client_version = client_hello.version;
Robert Sloan5d625782017-02-13 09:55:39 -0800623 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
624 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700625 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800626 }
627 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
628 client_hello.random_len);
629
Robert Sloana27a6a42017-09-05 08:39:28 -0700630 // Only null compression is supported. TLS 1.3 further requires the peer
631 // advertise no other compression.
Robert Sloan5d625782017-02-13 09:55:39 -0800632 if (OPENSSL_memchr(client_hello.compression_methods, 0,
633 client_hello.compression_methods_len) == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700634 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
Robert Sloan5d625782017-02-13 09:55:39 -0800635 client_hello.compression_methods_len != 1)) {
636 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700637 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700638 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800639 }
640
Robert Sloana27a6a42017-09-05 08:39:28 -0700641 // TLS extensions.
Robert Sloan5d625782017-02-13 09:55:39 -0800642 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
643 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Robert Sloana27a6a42017-09-05 08:39:28 -0700644 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800645 }
646
Robert Sloandc2f6092018-04-10 10:22:33 -0700647 hs->state = state12_select_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700648 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800649}
650
Robert Sloana27a6a42017-09-05 08:39:28 -0700651static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800652 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700653
Robert Sloan84377092017-08-14 09:33:19 -0700654 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700655 if (!ssl->method->get_message(ssl, &msg)) {
656 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700657 }
658
Robert Sloana27a6a42017-09-05 08:39:28 -0700659 // Call |cert_cb| to update server certificates if required.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100660 if (hs->config->cert->cert_cb != NULL) {
661 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
Robert Sloan5d625782017-02-13 09:55:39 -0800662 if (rv == 0) {
663 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700664 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700665 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400666 }
Robert Sloan5d625782017-02-13 09:55:39 -0800667 if (rv < 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700668 return ssl_hs_x509_lookup;
David Benjaminc895d6b2016-08-11 13:26:41 -0400669 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400670 }
671
Robert Sloan572a4e22017-04-17 10:52:19 -0700672 if (!ssl_on_certificate_selected(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700673 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500674 }
675
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100676 if (hs->ocsp_stapling_requested &&
677 ssl->ctx->legacy_ocsp_callback != nullptr) {
678 switch (ssl->ctx->legacy_ocsp_callback(
679 ssl, ssl->ctx->legacy_ocsp_callback_arg)) {
680 case SSL_TLSEXT_ERR_OK:
681 break;
682 case SSL_TLSEXT_ERR_NOACK:
683 hs->ocsp_stapling_requested = false;
684 break;
685 default:
686 OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR);
687 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
688 return ssl_hs_error;
689 }
690 }
691
Robert Sloan921ef2c2017-10-17 09:02:20 -0700692 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700693 // Jump to the TLS 1.3 state machine.
Robert Sloandc2f6092018-04-10 10:22:33 -0700694 hs->state = state12_tls13;
Robert Sloana27a6a42017-09-05 08:39:28 -0700695 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800696 }
697
Pete Bentley0c61efe2019-08-13 09:32:23 +0100698 ssl->s3->early_data_reason = ssl_early_data_protocol_version;
699
Robert Sloan5d625782017-02-13 09:55:39 -0800700 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700701 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700702 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800703 }
704
Robert Sloana27a6a42017-09-05 08:39:28 -0700705 // Negotiate the cipher suite. This must be done after |cert_cb| so the
706 // certificate is finalized.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100707 SSLCipherPreferenceList *prefs = hs->config->cipher_list
708 ? hs->config->cipher_list.get()
709 : ssl->ctx->cipher_list.get();
Tobias Thierer43be7d22020-03-02 19:23:34 +0000710 hs->new_cipher = choose_cipher(hs, &client_hello, prefs);
Robert Sloana94fe052017-02-21 08:49:28 -0800711 if (hs->new_cipher == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -0800712 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700713 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700714 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800715 }
716
Robert Sloandc2f6092018-04-10 10:22:33 -0700717 hs->state = state12_select_parameters;
Robert Sloana27a6a42017-09-05 08:39:28 -0700718 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800719}
720
Robert Sloana27a6a42017-09-05 08:39:28 -0700721static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
722 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
723 if (wait == ssl_hs_ok) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700724 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -0700725 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800726 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500727
Robert Sloana27a6a42017-09-05 08:39:28 -0700728 return wait;
729}
730
731static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
732 SSL *const ssl = hs->ssl;
733
734 SSLMessage msg;
735 if (!ssl->method->get_message(ssl, &msg)) {
736 return ssl_hs_read_message;
737 }
738
739 SSL_CLIENT_HELLO client_hello;
740 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
741 return ssl_hs_error;
742 }
743
744 // Determine whether we are doing session resumption.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700745 UniquePtr<SSL_SESSION> session;
Robert Sloana27a6a42017-09-05 08:39:28 -0700746 bool tickets_supported = false, renew_ticket = false;
747 enum ssl_hs_wait_t wait = ssl_get_prev_session(
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100748 hs, &session, &tickets_supported, &renew_ticket, &client_hello);
Robert Sloana27a6a42017-09-05 08:39:28 -0700749 if (wait != ssl_hs_ok) {
750 return wait;
Steven Valdez909b19f2016-11-21 15:35:44 -0500751 }
752
Robert Sloana12bf462017-07-17 07:08:26 -0700753 if (session) {
Robert Sloana94fe052017-02-21 08:49:28 -0800754 if (session->extended_master_secret && !hs->extended_master_secret) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700755 // A ClientHello without EMS that attempts to resume a session with EMS
756 // is fatal to the connection.
Steven Valdez909b19f2016-11-21 15:35:44 -0500757 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700758 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700759 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500760 }
761
Robert Sloana12bf462017-07-17 07:08:26 -0700762 if (!ssl_session_is_resumable(hs, session.get()) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700763 // If the client offers the EMS extension, but the previous session
764 // didn't use it, then negotiate a new session.
Robert Sloana94fe052017-02-21 08:49:28 -0800765 hs->extended_master_secret != session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -0700766 session.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500767 }
768 }
769
Robert Sloana12bf462017-07-17 07:08:26 -0700770 if (session) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700771 // Use the old session.
David Benjamin1b249672016-12-06 18:25:50 -0500772 hs->ticket_expected = renew_ticket;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100773 ssl->session = std::move(session);
Robert Sloana27a6a42017-09-05 08:39:28 -0700774 ssl->s3->session_reused = true;
Steven Valdez909b19f2016-11-21 15:35:44 -0500775 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500776 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500777 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500778 if (!ssl_get_new_session(hs, 1 /* server */)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700779 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500780 }
781
Robert Sloana27a6a42017-09-05 08:39:28 -0700782 // Clear the session ID if we want the session to be single-use.
Steven Valdez909b19f2016-11-21 15:35:44 -0500783 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
Robert Sloana94fe052017-02-21 08:49:28 -0800784 hs->new_session->session_id_length = 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500785 }
786 }
787
788 if (ssl->ctx->dos_protection_cb != NULL &&
789 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700790 // Connection rejected for DOS reasons.
Steven Valdez909b19f2016-11-21 15:35:44 -0500791 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700792 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700793 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500794 }
795
796 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800797 hs->new_session->cipher = hs->new_cipher;
Steven Valdez909b19f2016-11-21 15:35:44 -0500798
Robert Sloana27a6a42017-09-05 08:39:28 -0700799 // Determine whether to request a client certificate.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100800 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700801 // Only request a certificate if Channel ID isn't negotiated.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100802 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
803 ssl->s3->channel_id_valid) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700804 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700805 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700806 // CertificateRequest may only be sent in certificate-based ciphers.
Robert Sloana94fe052017-02-21 08:49:28 -0800807 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700808 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700809 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400810
David Benjamin1b249672016-12-06 18:25:50 -0500811 if (!hs->cert_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700812 // OpenSSL returns X509_V_OK when no certificates are requested. This is
813 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800814 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400815 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800816 }
817
Robert Sloana27a6a42017-09-05 08:39:28 -0700818 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
819 // deferred. Complete it now.
Robert Sloane56da3e2017-06-26 08:26:42 -0700820 uint8_t alert = SSL_AD_DECODE_ERROR;
821 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700822 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700823 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500824 }
825
Robert Sloana27a6a42017-09-05 08:39:28 -0700826 // Now that all parameters are known, initialize the handshake hash and hash
827 // the ClientHello.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700828 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
Robert Sloan84377092017-08-14 09:33:19 -0700829 !ssl_hash_message(hs, msg)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700830 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700831 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000832 }
833
Robert Sloandc2f6092018-04-10 10:22:33 -0700834 // Handback includes the whole handshake transcript, so we cannot free the
835 // transcript buffer in the handback case.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100836 if (!hs->cert_request && !hs->handback) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700837 hs->transcript.FreeBuffer();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800838 }
839
Robert Sloan84377092017-08-14 09:33:19 -0700840 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700841
Robert Sloandc2f6092018-04-10 10:22:33 -0700842 hs->state = state12_send_server_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700843 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800844}
845
Robert Sloanc9abfe42018-11-26 12:19:07 -0800846static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) {
847 out = out.subspan(out.size() - in.size());
848 assert(out.size() == in.size());
849 OPENSSL_memcpy(out.data(), in.data(), in.size());
850}
851
Robert Sloana27a6a42017-09-05 08:39:28 -0700852static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500853 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -0800854
Robert Sloana27a6a42017-09-05 08:39:28 -0700855 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
856 // known attack while we fix ChannelID itself.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100857 if (ssl->s3->channel_id_valid &&
Robert Sloana94fe052017-02-21 08:49:28 -0800858 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100859 ssl->s3->channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800860 }
861
Robert Sloana27a6a42017-09-05 08:39:28 -0700862 // If this is a resumption and the original handshake didn't support
863 // ChannelID then we didn't record the original handshake hashes in the
864 // session and so cannot resume with ChannelIDs.
David Benjaminc895d6b2016-08-11 13:26:41 -0400865 if (ssl->session != NULL &&
866 ssl->session->original_handshake_hash_len == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100867 ssl->s3->channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800868 }
869
Robert Sloan7d422bc2017-03-06 10:04:29 -0800870 struct OPENSSL_timeval now;
David Benjaminc895d6b2016-08-11 13:26:41 -0400871 ssl_get_current_time(ssl, &now);
872 ssl->s3->server_random[0] = now.tv_sec >> 24;
873 ssl->s3->server_random[1] = now.tv_sec >> 16;
874 ssl->s3->server_random[2] = now.tv_sec >> 8;
875 ssl->s3->server_random[3] = now.tv_sec;
876 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700877 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800878 }
879
Robert Sloand9e572d2018-08-27 12:27:00 -0700880 // Implement the TLS 1.3 anti-downgrade feature.
881 if (ssl_supports_version(hs, TLS1_3_VERSION)) {
882 if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
Robert Sloanc9abfe42018-11-26 12:19:07 -0800883 if (hs->apply_jdk11_workaround) {
884 // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it
885 // here. However, the signal is only effective if all TLS 1.2
886 // ServerHellos produced by the server are marked. Thus we send a
887 // different non-standard signal for the time being, until JDK 11.0.2 is
888 // released and clients have updated.
889 copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom);
890 } else {
891 copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom);
892 }
Robert Sloand9e572d2018-08-27 12:27:00 -0700893 } else {
Robert Sloanc9abfe42018-11-26 12:19:07 -0800894 copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom);
Robert Sloand9e572d2018-08-27 12:27:00 -0700895 }
Robert Sloan0da43952018-01-03 15:13:14 -0800896 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400897
Robert Sloanb6d070c2017-07-24 08:40:01 -0700898 const SSL_SESSION *session = hs->new_session.get();
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100899 if (ssl->session != nullptr) {
900 session = ssl->session.get();
David Benjaminc895d6b2016-08-11 13:26:41 -0400901 }
902
Robert Sloanb6d070c2017-07-24 08:40:01 -0700903 ScopedCBB cbb;
904 CBB body, session_id;
905 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400906 !CBB_add_u16(&body, ssl->version) ||
907 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
908 !CBB_add_u8_length_prefixed(&body, &session_id) ||
909 !CBB_add_bytes(&session_id, session->session_id,
910 session->session_id_length) ||
Pete Bentleyf23caaf2020-09-22 18:02:11 +0100911 !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400912 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -0500913 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700914 !ssl_add_message_cbb(ssl, cbb.get())) {
Kenny Roote99801b2015-11-06 15:31:15 -0800915 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700916 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800917 }
918
Robert Sloana27a6a42017-09-05 08:39:28 -0700919 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700920 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -0700921 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -0700922 hs->state = state12_send_server_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700923 }
924 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800925}
926
Robert Sloana27a6a42017-09-05 08:39:28 -0700927static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500928 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700929 ScopedCBB cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000930
Robert Sloane56da3e2017-06-26 08:26:42 -0700931 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800932 if (!ssl_has_certificate(hs)) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700933 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloana27a6a42017-09-05 08:39:28 -0700934 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700935 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800936
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100937 if (!ssl_output_cert_chain(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700938 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700939 }
940
941 if (hs->certificate_status_expected) {
942 CBB body, ocsp_response;
Robert Sloana12bf462017-07-17 07:08:26 -0700943 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700944 SSL3_MT_CERTIFICATE_STATUS) ||
945 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
946 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100947 !CBB_add_bytes(
948 &ocsp_response,
949 CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
950 CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700951 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700952 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700953 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700954 }
955 }
956 }
957
Robert Sloana27a6a42017-09-05 08:39:28 -0700958 // Assemble ServerKeyExchange parameters if needed.
Robert Sloane56da3e2017-06-26 08:26:42 -0700959 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
960 uint32_t alg_a = hs->new_cipher->algorithm_auth;
961 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100962 ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700963 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
964 // the client and server randoms for the signing transcript.
Robert Sloane56da3e2017-06-26 08:26:42 -0700965 CBB child;
Robert Sloana12bf462017-07-17 07:08:26 -0700966 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
967 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
968 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700969 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400970 }
971
Robert Sloana27a6a42017-09-05 08:39:28 -0700972 // PSK ciphers begin with an identity hint.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800973 if (alg_a & SSL_aPSK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100974 size_t len = hs->config->psk_identity_hint == nullptr
975 ? 0
976 : strlen(hs->config->psk_identity_hint.get());
Robert Sloana12bf462017-07-17 07:08:26 -0700977 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100978 !CBB_add_bytes(&child,
979 (const uint8_t *)hs->config->psk_identity_hint.get(),
Adam Langley4139edb2016-01-13 15:00:54 -0800980 len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700981 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800982 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800983 }
984
Robert Sloan572a4e22017-04-17 10:52:19 -0700985 if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700986 // Determine the group to use.
David Benjamind316cba2016-06-02 16:17:39 -0400987 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -0500988 if (!tls1_get_shared_group(hs, &group_id)) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700989 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700990 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700991 return ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700992 }
Robert Sloana94fe052017-02-21 08:49:28 -0800993 hs->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -0800994
Robert Sloana27a6a42017-09-05 08:39:28 -0700995 // Set up ECDH, generate a key, and emit the public half.
Robert Sloan11c28bd2018-12-17 12:09:20 -0800996 hs->key_shares[0] = SSLKeyShare::Create(group_id);
997 if (!hs->key_shares[0] ||
Robert Sloana12bf462017-07-17 07:08:26 -0700998 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
999 !CBB_add_u16(cbb.get(), group_id) ||
1000 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
Robert Sloan11c28bd2018-12-17 12:09:20 -08001001 !hs->key_shares[0]->Offer(&child)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001002 return ssl_hs_error;
David Benjamind316cba2016-06-02 16:17:39 -04001003 }
Adam Langley4139edb2016-01-13 15:00:54 -08001004 } else {
1005 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001006 }
1007
Robert Sloan4562e9d2017-10-02 10:26:51 -07001008 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001009 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001010 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001011 }
1012
Robert Sloandc2f6092018-04-10 10:22:33 -07001013 hs->state = state12_send_server_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -07001014 return ssl_hs_ok;
Robert Sloane56da3e2017-06-26 08:26:42 -07001015}
1016
Robert Sloana27a6a42017-09-05 08:39:28 -07001017static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001018 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001019
Robert Sloan4562e9d2017-10-02 10:26:51 -07001020 if (hs->server_params.size() == 0) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001021 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001022 return ssl_hs_ok;
1023 }
1024
Robert Sloanb6d070c2017-07-24 08:40:01 -07001025 ScopedCBB cbb;
1026 CBB body, child;
1027 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -04001028 SSL3_MT_SERVER_KEY_EXCHANGE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001029 // |hs->server_params| contains a prefix for signing.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001030 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
1031 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
1032 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001033 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001034 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001035
Robert Sloana27a6a42017-09-05 08:39:28 -07001036 // Add a signature.
Robert Sloana94fe052017-02-21 08:49:28 -08001037 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001038 if (!ssl_has_private_key(hs)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001039 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001040 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001041 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001042
Robert Sloana27a6a42017-09-05 08:39:28 -07001043 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001044 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -05001045 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001046 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001047 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001048 }
Robert Sloan921ef2c2017-10-17 09:02:20 -07001049 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001050 if (!CBB_add_u16(&body, signature_algorithm)) {
1051 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001052 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001053 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001054 }
1055 }
1056
Robert Sloana27a6a42017-09-05 08:39:28 -07001057 // Add space for the signature.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001058 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -04001059 uint8_t *ptr;
1060 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1061 !CBB_reserve(&child, &ptr, max_sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001062 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001063 }
1064
Adam Langley4139edb2016-01-13 15:00:54 -08001065 size_t sig_len;
Robert Sloane56da3e2017-06-26 08:26:42 -07001066 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
Robert Sloan921ef2c2017-10-17 09:02:20 -07001067 signature_algorithm, hs->server_params)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001068 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001069 if (!CBB_did_write(&child, sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001070 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001071 }
1072 break;
1073 case ssl_private_key_failure:
Robert Sloana27a6a42017-09-05 08:39:28 -07001074 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001075 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -07001076 return ssl_hs_private_key_operation;
Adam Langley4139edb2016-01-13 15:00:54 -08001077 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001078 }
1079
Robert Sloanb6d070c2017-07-24 08:40:01 -07001080 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001081 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +00001082 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001083
Robert Sloan4562e9d2017-10-02 10:26:51 -07001084 hs->server_params.Reset();
David Benjaminc895d6b2016-08-11 13:26:41 -04001085
Robert Sloandc2f6092018-04-10 10:22:33 -07001086 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001087 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001088}
1089
Robert Sloana27a6a42017-09-05 08:39:28 -07001090static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001091 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001092
Robert Sloanb6d070c2017-07-24 08:40:01 -07001093 ScopedCBB cbb;
1094 CBB body;
David Benjaminc895d6b2016-08-11 13:26:41 -04001095
Robert Sloane56da3e2017-06-26 08:26:42 -07001096 if (hs->cert_request) {
1097 CBB cert_types, sigalgs_cbb;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001098 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -07001099 SSL3_MT_CERTIFICATE_REQUEST) ||
1100 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1101 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001102 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001103 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -07001104 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
Tobias Thierer43be7d22020-03-02 19:23:34 +00001105 !tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001106 !ssl_add_client_CA_list(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001107 !ssl_add_message_cbb(ssl, cbb.get())) {
1108 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001109 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001110 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001111 }
1112
Robert Sloanb6d070c2017-07-24 08:40:01 -07001113 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1114 SSL3_MT_SERVER_HELLO_DONE) ||
1115 !ssl_add_message_cbb(ssl, cbb.get())) {
1116 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001117 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001118 }
1119
Robert Sloandc2f6092018-04-10 10:22:33 -07001120 hs->state = state12_read_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001121 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001122}
1123
Robert Sloana27a6a42017-09-05 08:39:28 -07001124static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001125 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001126
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001127 if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
1128 return ssl_hs_handback;
Robert Sloandc2f6092018-04-10 10:22:33 -07001129 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001130 if (!hs->cert_request) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001131 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001132 return ssl_hs_ok;
1133 }
David Benjamin6e899c72016-06-09 18:02:18 -04001134
Robert Sloan84377092017-08-14 09:33:19 -07001135 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001136 if (!ssl->method->get_message(ssl, &msg)) {
1137 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001138 }
1139
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001140 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001141 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001142 }
1143
Robert Sloan84377092017-08-14 09:33:19 -07001144 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001145 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001146 }
1147
Robert Sloan84377092017-08-14 09:33:19 -07001148 CBS certificate_msg = msg.body;
Robert Sloan5d625782017-02-13 09:55:39 -08001149 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001150 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
1151 hs->config->retain_only_sha256_of_client_certs
Robert Sloanfe7cd212017-08-07 09:03:39 -07001152 ? hs->new_session->peer_sha256
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001153 : nullptr,
Robert Sloanfe7cd212017-08-07 09:03:39 -07001154 &certificate_msg, ssl->ctx->pool)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001155 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001156 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001157 }
1158
Steven Valdeze7531f02016-12-14 13:29:57 -05001159 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001160 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin6e899c72016-06-09 18:02:18 -04001161 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001162 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001163 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001164 }
1165
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001166 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001167 // No client certificate so the handshake buffer may be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001168 hs->transcript.FreeBuffer();
David Benjamin6e899c72016-06-09 18:02:18 -04001169
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001170 if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001171 // Fail for TLS only if we required a certificate
David Benjamin6e899c72016-06-09 18:02:18 -04001172 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001173 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001174 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001175 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001176
Robert Sloana27a6a42017-09-05 08:39:28 -07001177 // OpenSSL returns X509_V_OK when no certificates are received. This is
1178 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -08001179 hs->new_session->verify_result = X509_V_OK;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001180 } else if (hs->config->retain_only_sha256_of_client_certs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001181 // The hash will have been filled in.
Robert Sloana94fe052017-02-21 08:49:28 -08001182 hs->new_session->peer_sha256_valid = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001183 }
David Benjamin6e899c72016-06-09 18:02:18 -04001184
Robert Sloan84377092017-08-14 09:33:19 -07001185 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001186 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001187 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001188}
1189
Robert Sloana27a6a42017-09-05 08:39:28 -07001190static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001191 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001192 switch (ssl_verify_peer_cert(hs)) {
1193 case ssl_verify_ok:
1194 break;
1195 case ssl_verify_invalid:
1196 return ssl_hs_error;
1197 case ssl_verify_retry:
1198 return ssl_hs_certificate_verify;
1199 }
1200 }
1201
Robert Sloandc2f6092018-04-10 10:22:33 -07001202 hs->state = state12_read_client_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -07001203 return ssl_hs_ok;
1204}
1205
1206static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001207 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -07001208 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001209 if (!ssl->method->get_message(ssl, &msg)) {
1210 return ssl_hs_read_message;
Robert Sloane56da3e2017-06-26 08:26:42 -07001211 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001212
Robert Sloan84377092017-08-14 09:33:19 -07001213 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001214 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001215 }
1216
Robert Sloan84377092017-08-14 09:33:19 -07001217 CBS client_key_exchange = msg.body;
Robert Sloane56da3e2017-06-26 08:26:42 -07001218 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1219 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001220
Robert Sloana27a6a42017-09-05 08:39:28 -07001221 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001222 if (alg_a & SSL_aPSK) {
1223 CBS psk_identity;
1224
Robert Sloana27a6a42017-09-05 08:39:28 -07001225 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1226 // then this is the only field in the message.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001227 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1228 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001230 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001231 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001232 }
1233
1234 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1235 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001236 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001237 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001238 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001239 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001240 char *raw = nullptr;
1241 if (!CBS_strdup(&psk_identity, &raw)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001242 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001243 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001244 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001245 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001246 hs->new_session->psk_identity.reset(raw);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001247 }
1248
Robert Sloan4562e9d2017-10-02 10:26:51 -07001249 // Depending on the key exchange method, compute |premaster_secret|.
1250 Array<uint8_t> premaster_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001251 if (alg_k & SSL_kRSA) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001252 CBS encrypted_premaster_secret;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001253 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1254 &encrypted_premaster_secret) ||
1255 CBS_len(&client_key_exchange) != 0) {
1256 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1257 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1258 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001259 }
1260
Robert Sloana27a6a42017-09-05 08:39:28 -07001261 // Allocate a buffer large enough for an RSA decryption.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001262 Array<uint8_t> decrypt_buf;
1263 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1264 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001265 }
1266
Robert Sloana27a6a42017-09-05 08:39:28 -07001267 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1268 // timing-sensitive code below.
Adam Langley4139edb2016-01-13 15:00:54 -08001269 size_t decrypt_len;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001270 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1271 decrypt_buf.size(),
Robert Sloan921ef2c2017-10-17 09:02:20 -07001272 encrypted_premaster_secret)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001273 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001274 break;
1275 case ssl_private_key_failure:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001276 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001277 case ssl_private_key_retry:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001278 return ssl_hs_private_key_operation;
Kenny Roote99801b2015-11-06 15:31:15 -08001279 }
1280
Robert Sloan4562e9d2017-10-02 10:26:51 -07001281 if (decrypt_len != decrypt_buf.size()) {
David Benjamin4969cc92016-04-22 15:02:23 -04001282 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001283 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001284 return ssl_hs_error;
David Benjamin4969cc92016-04-22 15:02:23 -04001285 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001286
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001287 CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len);
1288
Robert Sloana27a6a42017-09-05 08:39:28 -07001289 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1290 // section 7.4.7.1.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001291 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1292 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1293 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001294 }
1295
Robert Sloana27a6a42017-09-05 08:39:28 -07001296 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1297 // publicly invalid.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001298 if (decrypt_len < 11 + premaster_secret.size()) {
Adam Langley4139edb2016-01-13 15:00:54 -08001299 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001300 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001301 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001302 }
1303
Robert Sloana27a6a42017-09-05 08:39:28 -07001304 // Check the padding. See RFC 3447, section 7.2.2.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001305 size_t padding_len = decrypt_len - premaster_secret.size();
Adam Langley4139edb2016-01-13 15:00:54 -08001306 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1307 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001308 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001309 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1310 }
1311 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1312
Robert Sloana27a6a42017-09-05 08:39:28 -07001313 // The premaster secret must begin with |client_version|. This too must be
1314 // checked in constant time (http://eprint.iacr.org/2003/052/).
Adam Langley4139edb2016-01-13 15:00:54 -08001315 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001316 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001317 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001318 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001319
Robert Sloana27a6a42017-09-05 08:39:28 -07001320 // Select, in constant time, either the decrypted premaster or the random
1321 // premaster based on |good|.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001322 for (size_t i = 0; i < premaster_secret.size(); i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001323 premaster_secret[i] = constant_time_select_8(
1324 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1325 }
Robert Sloan572a4e22017-04-17 10:52:19 -07001326 } else if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001327 // Parse the ClientKeyExchange.
Adam Langley4139edb2016-01-13 15:00:54 -08001328 CBS peer_key;
Robert Sloan572a4e22017-04-17 10:52:19 -07001329 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001330 CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001331 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001332 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001333 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001334 }
1335
Robert Sloana27a6a42017-09-05 08:39:28 -07001336 // Compute the premaster.
Robert Sloan5d625782017-02-13 09:55:39 -08001337 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan11c28bd2018-12-17 12:09:20 -08001338 if (!hs->key_shares[0]->Finish(&premaster_secret, &alert, peer_key)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001339 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001340 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001341 }
1342
Robert Sloana27a6a42017-09-05 08:39:28 -07001343 // The key exchange state may now be discarded.
Robert Sloan11c28bd2018-12-17 12:09:20 -08001344 hs->key_shares[0].reset();
1345 hs->key_shares[1].reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001346 } else if (!(alg_k & SSL_kPSK)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001347 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001348 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001349 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001350 }
1351
Robert Sloana27a6a42017-09-05 08:39:28 -07001352 // For a PSK cipher suite, the actual pre-master secret is combined with the
1353 // pre-shared key.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001354 if (alg_a & SSL_aPSK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001355 if (hs->config->psk_server_callback == NULL) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001356 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001357 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001358 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001359 }
1360
Robert Sloana27a6a42017-09-05 08:39:28 -07001361 // Look up the key for the identity.
Robert Sloane56da3e2017-06-26 08:26:42 -07001362 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001363 unsigned psk_len = hs->config->psk_server_callback(
1364 ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
Robert Sloane56da3e2017-06-26 08:26:42 -07001365 if (psk_len > PSK_MAX_PSK_LEN) {
1366 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001367 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001368 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001369 } else if (psk_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001370 // PSK related to the given identity not found.
Robert Sloane56da3e2017-06-26 08:26:42 -07001371 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001372 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001373 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001374 }
1375
1376 if (alg_k & SSL_kPSK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001377 // In plain PSK, other_secret is a block of 0s with the same length as the
1378 // pre-shared key.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001379 if (!premaster_secret.Init(psk_len)) {
1380 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001381 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07001382 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
Robert Sloane56da3e2017-06-26 08:26:42 -07001383 }
1384
Robert Sloanb6d070c2017-07-24 08:40:01 -07001385 ScopedCBB new_premaster;
1386 CBB child;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001387 if (!CBB_init(new_premaster.get(),
Robert Sloan4562e9d2017-10-02 10:26:51 -07001388 2 + psk_len + 2 + premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001389 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07001390 !CBB_add_bytes(&child, premaster_secret.data(),
1391 premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001392 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001393 !CBB_add_bytes(&child, psk, psk_len) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001394 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001395 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001396 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001397 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001398 }
1399
Robert Sloan84377092017-08-14 09:33:19 -07001400 if (!ssl_hash_message(hs, msg)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001401 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001402 }
1403
Robert Sloana27a6a42017-09-05 08:39:28 -07001404 // Compute the master secret.
Robert Sloana94fe052017-02-21 08:49:28 -08001405 hs->new_session->master_key_length = tls1_generate_master_secret(
Robert Sloanb1b54b82017-11-06 13:50:02 -08001406 hs, hs->new_session->master_key, premaster_secret);
Robert Sloana94fe052017-02-21 08:49:28 -08001407 if (hs->new_session->master_key_length == 0) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001408 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001409 }
Robert Sloana94fe052017-02-21 08:49:28 -08001410 hs->new_session->extended_master_secret = hs->extended_master_secret;
Robert Sloan4c22c5f2019-03-01 15:53:37 -08001411 CONSTTIME_DECLASSIFY(hs->new_session->master_key,
1412 hs->new_session->master_key_length);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001413
Robert Sloan84377092017-08-14 09:33:19 -07001414 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001415 hs->state = state12_read_client_certificate_verify;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001416 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001417}
1418
Robert Sloana27a6a42017-09-05 08:39:28 -07001419static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001420 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001421
Robert Sloana27a6a42017-09-05 08:39:28 -07001422 // Only RSA and ECDSA client certificates are supported, so a
1423 // CertificateVerify is required if and only if there's a client certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001424 if (!hs->peer_pubkey) {
1425 hs->transcript.FreeBuffer();
Robert Sloandc2f6092018-04-10 10:22:33 -07001426 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001427 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001428 }
1429
Robert Sloan84377092017-08-14 09:33:19 -07001430 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001431 if (!ssl->method->get_message(ssl, &msg)) {
1432 return ssl_hs_read_message;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001433 }
1434
Robert Sloan84377092017-08-14 09:33:19 -07001435 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001436 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001437 }
1438
Pete Bentleyf23caaf2020-09-22 18:02:11 +01001439 // The peer certificate must be valid for signing.
1440 const CRYPTO_BUFFER *leaf =
1441 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1442 CBS leaf_cbs;
1443 CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1444 if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) {
1445 return ssl_hs_error;
1446 }
1447
Robert Sloan84377092017-08-14 09:33:19 -07001448 CBS certificate_verify = msg.body, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001449
Robert Sloana27a6a42017-09-05 08:39:28 -07001450 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001451 uint16_t signature_algorithm = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001452 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001453 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001454 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001455 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001456 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001457 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001458 uint8_t alert = SSL_AD_DECODE_ERROR;
Tobias Thierer43be7d22020-03-02 19:23:34 +00001459 if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001460 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001461 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001462 }
Robert Sloana94fe052017-02-21 08:49:28 -08001463 hs->new_session->peer_signature_algorithm = signature_algorithm;
Robert Sloan2424d842017-05-01 07:46:28 -07001464 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001465 hs->peer_pubkey.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001466 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001467 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001468 return ssl_hs_error;
Adam Langleye9ada862015-05-11 17:20:37 -07001469 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001470
Robert Sloana27a6a42017-09-05 08:39:28 -07001471 // Parse and verify the signature.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001472 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1473 CBS_len(&certificate_verify) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001474 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001475 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001476 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001477 }
1478
Pete Bentley0c61efe2019-08-13 09:32:23 +01001479 if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1480 hs->peer_pubkey.get(), hs->transcript.buffer())) {
Kenny Rootb8494592015-09-25 02:29:14 +00001481 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001482 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001483 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001484 }
1485
Robert Sloana27a6a42017-09-05 08:39:28 -07001486 // The handshake buffer is no longer necessary, and we may hash the current
1487 // message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001488 hs->transcript.FreeBuffer();
Robert Sloan84377092017-08-14 09:33:19 -07001489 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001490 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001491 }
1492
Robert Sloan84377092017-08-14 09:33:19 -07001493 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001494 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001495 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001496}
1497
Robert Sloana27a6a42017-09-05 08:39:28 -07001498static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001499 if (hs->handback && hs->ssl->session != NULL) {
1500 return ssl_hs_handback;
1501 }
Robert Sloandc2f6092018-04-10 10:22:33 -07001502 hs->state = state12_process_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001503 return ssl_hs_read_change_cipher_spec;
1504}
1505
1506static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001507 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001508 return ssl_hs_error;
1509 }
1510
Robert Sloandc2f6092018-04-10 10:22:33 -07001511 hs->state = state12_read_next_proto;
Robert Sloana27a6a42017-09-05 08:39:28 -07001512 return ssl_hs_ok;
1513}
1514
1515static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001516 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001517
1518 if (!hs->next_proto_neg_seen) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001519 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001520 return ssl_hs_ok;
1521 }
1522
Robert Sloan84377092017-08-14 09:33:19 -07001523 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001524 if (!ssl->method->get_message(ssl, &msg)) {
1525 return ssl_hs_read_message;
David Benjaminc895d6b2016-08-11 13:26:41 -04001526 }
1527
Robert Sloan84377092017-08-14 09:33:19 -07001528 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1529 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001530 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001531 }
1532
Robert Sloan84377092017-08-14 09:33:19 -07001533 CBS next_protocol = msg.body, selected_protocol, padding;
David Benjamin6e899c72016-06-09 18:02:18 -04001534 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1535 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001536 CBS_len(&next_protocol) != 0) {
1537 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001538 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001539 return ssl_hs_error;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001540 }
1541
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001542 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001543 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001544 }
1545
Robert Sloan84377092017-08-14 09:33:19 -07001546 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001547 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001548 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001549}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001550
Robert Sloana27a6a42017-09-05 08:39:28 -07001551static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001552 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001553
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001554 if (!ssl->s3->channel_id_valid) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001555 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001556 return ssl_hs_ok;
1557 }
1558
Robert Sloan84377092017-08-14 09:33:19 -07001559 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001560 if (!ssl->method->get_message(ssl, &msg)) {
1561 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001562 }
1563
Robert Sloan84377092017-08-14 09:33:19 -07001564 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1565 !tls1_verify_channel_id(hs, msg) ||
1566 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001567 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001568 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001569
Robert Sloan84377092017-08-14 09:33:19 -07001570 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001571 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001572 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001573}
1574
Robert Sloana27a6a42017-09-05 08:39:28 -07001575static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1576 SSL *const ssl = hs->ssl;
1577 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1578 if (wait != ssl_hs_ok) {
1579 return wait;
1580 }
1581
1582 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001583 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001584 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001585 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001586 }
1587
1588 // If this is a full handshake with ChannelID then record the handshake
1589 // hashes in |hs->new_session| in case we need them to verify a
1590 // ChannelID signature on a resumption of this session in the future.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001591 if (ssl->session == NULL && ssl->s3->channel_id_valid &&
Robert Sloana27a6a42017-09-05 08:39:28 -07001592 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1593 return ssl_hs_error;
1594 }
1595
1596 return ssl_hs_ok;
1597}
1598
1599static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001600 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001601
1602 if (hs->ticket_expected) {
1603 const SSL_SESSION *session;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001604 UniquePtr<SSL_SESSION> session_copy;
Robert Sloane56da3e2017-06-26 08:26:42 -07001605 if (ssl->session == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001606 // Fix the timeout to measure from the ticket issuance time.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001607 ssl_session_rebase_time(ssl, hs->new_session.get());
1608 session = hs->new_session.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001609 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001610 // We are renewing an existing session. Duplicate the session to adjust
1611 // the timeout.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001612 session_copy =
1613 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001614 if (!session_copy) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001615 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001616 }
1617
Robert Sloanb6d070c2017-07-24 08:40:01 -07001618 ssl_session_rebase_time(ssl, session_copy.get());
1619 session = session_copy.get();
Steven Valdez909b19f2016-11-21 15:35:44 -05001620 }
1621
Robert Sloanb6d070c2017-07-24 08:40:01 -07001622 ScopedCBB cbb;
1623 CBB body, ticket;
1624 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1625 SSL3_MT_NEW_SESSION_TICKET) ||
1626 !CBB_add_u32(&body, session->timeout) ||
1627 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001628 !ssl_encrypt_ticket(hs, &ticket, session) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001629 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001630 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001631 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001632 }
1633
Robert Sloane56da3e2017-06-26 08:26:42 -07001634 if (!ssl->method->add_change_cipher_spec(ssl) ||
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001635 !tls1_change_cipher_state(hs, evp_aead_seal) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001636 !ssl_send_finished(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001637 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001638 }
Adam Langleye9ada862015-05-11 17:20:37 -07001639
Robert Sloana27a6a42017-09-05 08:39:28 -07001640 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001641 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001642 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001643 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001644 }
1645 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001646}
Robert Sloanb6d070c2017-07-24 08:40:01 -07001647
Robert Sloana27a6a42017-09-05 08:39:28 -07001648static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1649 SSL *const ssl = hs->ssl;
1650
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001651 if (hs->handback) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001652 return ssl_hs_handback;
1653 }
1654
Robert Sloana27a6a42017-09-05 08:39:28 -07001655 ssl->method->on_handshake_complete(ssl);
1656
1657 // If we aren't retaining peer certificates then we can discard it now.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001658 if (hs->new_session != NULL &&
1659 hs->config->retain_only_sha256_of_client_certs) {
1660 hs->new_session->certs.reset();
Robert Sloana27a6a42017-09-05 08:39:28 -07001661 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1662 }
1663
Robert Sloana27a6a42017-09-05 08:39:28 -07001664 if (ssl->session != NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001665 ssl->s3->established_session = UpRef(ssl->session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001666 } else {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001667 ssl->s3->established_session = std::move(hs->new_session);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001668 ssl->s3->established_session->not_resumable = false;
Robert Sloana27a6a42017-09-05 08:39:28 -07001669 }
1670
1671 hs->handshake_finalized = true;
1672 ssl->s3->initial_handshake_complete = true;
1673 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1674
Robert Sloandc2f6092018-04-10 10:22:33 -07001675 hs->state = state12_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001676 return ssl_hs_ok;
1677}
1678
1679enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001680 while (hs->state != state12_done) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001681 enum ssl_hs_wait_t ret = ssl_hs_error;
Robert Sloandc2f6092018-04-10 10:22:33 -07001682 enum tls12_server_hs_state_t state =
1683 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001684 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001685 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001686 ret = do_start_accept(hs);
1687 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001688 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001689 ret = do_read_client_hello(hs);
1690 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001691 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001692 ret = do_select_certificate(hs);
1693 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001694 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001695 ret = do_tls13(hs);
1696 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001697 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001698 ret = do_select_parameters(hs);
1699 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001700 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001701 ret = do_send_server_hello(hs);
1702 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001703 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001704 ret = do_send_server_certificate(hs);
1705 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001706 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001707 ret = do_send_server_key_exchange(hs);
1708 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001709 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001710 ret = do_send_server_hello_done(hs);
1711 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001712 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001713 ret = do_read_client_certificate(hs);
1714 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001715 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001716 ret = do_verify_client_certificate(hs);
1717 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001718 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001719 ret = do_read_client_key_exchange(hs);
1720 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001721 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001722 ret = do_read_client_certificate_verify(hs);
1723 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001724 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001725 ret = do_read_change_cipher_spec(hs);
1726 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001727 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001728 ret = do_process_change_cipher_spec(hs);
1729 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001730 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001731 ret = do_read_next_proto(hs);
1732 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001733 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001734 ret = do_read_channel_id(hs);
1735 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001736 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001737 ret = do_read_client_finished(hs);
1738 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001739 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001740 ret = do_send_server_finished(hs);
1741 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001742 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001743 ret = do_finish_server_handshake(hs);
1744 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001745 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001746 ret = ssl_hs_ok;
1747 break;
1748 }
1749
1750 if (hs->state != state) {
1751 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1752 }
1753
1754 if (ret != ssl_hs_ok) {
1755 return ret;
1756 }
1757 }
1758
1759 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1760 return ssl_hs_ok;
1761}
1762
1763const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001764 enum tls12_server_hs_state_t state =
1765 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001766 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001767 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001768 return "TLS server start_accept";
Robert Sloandc2f6092018-04-10 10:22:33 -07001769 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001770 return "TLS server read_client_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001771 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001772 return "TLS server select_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001773 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001774 return tls13_server_handshake_state(hs);
Robert Sloandc2f6092018-04-10 10:22:33 -07001775 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001776 return "TLS server select_parameters";
Robert Sloandc2f6092018-04-10 10:22:33 -07001777 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001778 return "TLS server send_server_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001779 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001780 return "TLS server send_server_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001781 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001782 return "TLS server send_server_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001783 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001784 return "TLS server send_server_hello_done";
Robert Sloandc2f6092018-04-10 10:22:33 -07001785 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001786 return "TLS server read_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001787 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001788 return "TLS server verify_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001789 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001790 return "TLS server read_client_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001791 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001792 return "TLS server read_client_certificate_verify";
Robert Sloandc2f6092018-04-10 10:22:33 -07001793 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001794 return "TLS server read_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001795 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001796 return "TLS server process_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001797 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001798 return "TLS server read_next_proto";
Robert Sloandc2f6092018-04-10 10:22:33 -07001799 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001800 return "TLS server read_channel_id";
Robert Sloandc2f6092018-04-10 10:22:33 -07001801 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001802 return "TLS server read_client_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001803 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001804 return "TLS server send_server_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001805 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001806 return "TLS server finish_server_handshake";
Robert Sloandc2f6092018-04-10 10:22:33 -07001807 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001808 return "TLS server done";
1809 }
1810
1811 return "TLS server unknown";
1812}
1813
Robert Sloan726e9d12018-09-11 11:45:04 -07001814BSSL_NAMESPACE_END