blob: c4f3b75e5bdfff53bf4c1772ebce474124646231 [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>
155#include <openssl/buf.h>
156#include <openssl/bytestring.h>
157#include <openssl/cipher.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800158#include <openssl/ec.h>
159#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700160#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161#include <openssl/evp.h>
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/mem.h>
David Benjamin4969cc92016-04-22 15:02:23 -0400165#include <openssl/nid.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800166#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167#include <openssl/x509.h>
168
Adam Langleye9ada862015-05-11 17:20:37 -0700169#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170#include "../crypto/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171
172
Robert Sloan726e9d12018-09-11 11:45:04 -0700173BSSL_NAMESPACE_BEGIN
Robert Sloanb6d070c2017-07-24 08:40:01 -0700174
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100175bool ssl_client_cipher_list_contains_cipher(
176 const SSL_CLIENT_HELLO *client_hello, uint16_t id) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400177 CBS cipher_suites;
178 CBS_init(&cipher_suites, client_hello->cipher_suites,
179 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180
David Benjaminc895d6b2016-08-11 13:26:41 -0400181 while (CBS_len(&cipher_suites) > 0) {
182 uint16_t got_id;
183 if (!CBS_get_u16(&cipher_suites, &got_id)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100184 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -0400185 }
186
187 if (got_id == id) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100188 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -0400189 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 }
191
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100192 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193}
194
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100195static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
196 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500197 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800198 assert(!ssl->s3->have_version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700199 CBS supported_versions, versions;
David Benjamin1b249672016-12-06 18:25:50 -0500200 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
201 TLSEXT_TYPE_supported_versions)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400202 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
203 CBS_len(&supported_versions) != 0 ||
204 CBS_len(&versions) == 0) {
205 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
206 *out_alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100207 return false;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400208 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400209 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -0700210 // Convert the ClientHello version to an equivalent supported_versions
211 // extension.
Robert Sloanf6200e72017-07-10 08:09:18 -0700212 static const uint8_t kTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700213 0x03, 0x03, // TLS 1.2
214 0x03, 0x02, // TLS 1.1
215 0x03, 0x01, // TLS 1
Robert Sloanf6200e72017-07-10 08:09:18 -0700216 };
217
218 static const uint8_t kDTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700219 0xfe, 0xfd, // DTLS 1.2
220 0xfe, 0xff, // DTLS 1.0
Robert Sloanf6200e72017-07-10 08:09:18 -0700221 };
222
223 size_t versions_len = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400224 if (SSL_is_dtls(ssl)) {
225 if (client_hello->version <= DTLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700226 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400227 } else if (client_hello->version <= DTLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700228 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400229 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700230 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
231 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400232 } else {
233 if (client_hello->version >= TLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700234 versions_len = 6;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100235 } else if (client_hello->version >= TLS1_1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700236 versions_len = 4;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100237 } else if (client_hello->version >= TLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700238 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400239 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700240 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
241 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400242 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 }
244
Robert Sloanf6200e72017-07-10 08:09:18 -0700245 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100246 return false;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800247 }
248
Robert Sloana27a6a42017-09-05 08:39:28 -0700249 // At this point, the connection's version is known and |ssl->version| is
250 // fixed. Begin enforcing the record-layer version.
251 ssl->s3->have_version = true;
Robert Sloandb4251a2017-09-18 09:38:15 -0700252 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253
Robert Sloana27a6a42017-09-05 08:39:28 -0700254 // Handle FALLBACK_SCSV.
Robert Sloanf6200e72017-07-10 08:09:18 -0700255 if (ssl_client_cipher_list_contains_cipher(client_hello,
256 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Robert Sloan921ef2c2017-10-17 09:02:20 -0700257 ssl_protocol_version(ssl) < hs->max_version) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700258 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
259 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100260 return false;
Robert Sloanf6200e72017-07-10 08:09:18 -0700261 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400262
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100263 return true;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264}
265
Robert Sloanfe7cd212017-08-07 09:03:39 -0700266static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
267 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500268 CBS cipher_suites;
269 CBS_init(&cipher_suites, client_hello->cipher_suites,
270 client_hello->cipher_suites_len);
271
Robert Sloanfe7cd212017-08-07 09:03:39 -0700272 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
273 if (!sk) {
David Benjamin1b249672016-12-06 18:25:50 -0500274 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700275 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500276 }
277
278 while (CBS_len(&cipher_suites) > 0) {
279 uint16_t cipher_suite;
280
281 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
282 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700283 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500284 }
285
286 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700287 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500288 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700289 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500290 }
291 }
292
293 return sk;
David Benjamin1b249672016-12-06 18:25:50 -0500294}
295
Robert Sloana27a6a42017-09-05 08:39:28 -0700296// ssl_get_compatible_server_ciphers determines the key exchange and
297// authentication cipher suite masks compatible with the server configuration
298// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
299// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin1b249672016-12-06 18:25:50 -0500300static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
301 uint32_t *out_mask_k,
302 uint32_t *out_mask_a) {
David Benjamin1b249672016-12-06 18:25:50 -0500303 uint32_t mask_k = 0;
304 uint32_t mask_a = 0;
305
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100306 if (ssl_has_certificate(hs->config)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700307 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
308 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin1b249672016-12-06 18:25:50 -0500309 mask_k |= SSL_kRSA;
David Benjamin1b249672016-12-06 18:25:50 -0500310 }
311 }
312
Robert Sloana27a6a42017-09-05 08:39:28 -0700313 // Check for a shared group to consider ECDHE ciphers.
David Benjamin1b249672016-12-06 18:25:50 -0500314 uint16_t unused;
315 if (tls1_get_shared_group(hs, &unused)) {
316 mask_k |= SSL_kECDHE;
317 }
318
Robert Sloana27a6a42017-09-05 08:39:28 -0700319 // PSK requires a server callback.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100320 if (hs->config->psk_server_callback != NULL) {
David Benjamin1b249672016-12-06 18:25:50 -0500321 mask_k |= SSL_kPSK;
322 mask_a |= SSL_aPSK;
323 }
324
325 *out_mask_k = mask_k;
326 *out_mask_a = mask_a;
327}
328
329static const SSL_CIPHER *ssl3_choose_cipher(
330 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
Robert Sloanc6ebb282018-04-30 10:10:26 -0700331 const SSLCipherPreferenceList *server_pref) {
David Benjamin1b249672016-12-06 18:25:50 -0500332 SSL *const ssl = hs->ssl;
Robert Sloan15c0b352018-04-16 08:36:46 -0700333 const STACK_OF(SSL_CIPHER) *prio, *allow;
Robert Sloana27a6a42017-09-05 08:39:28 -0700334 // in_group_flags will either be NULL, or will point to an array of bytes
335 // which indicate equal-preference groups in the |prio| stack. See the
Robert Sloanc6ebb282018-04-30 10:10:26 -0700336 // comment about |in_group_flags| in the |SSLCipherPreferenceList|
Robert Sloana27a6a42017-09-05 08:39:28 -0700337 // struct.
Robert Sloanc6ebb282018-04-30 10:10:26 -0700338 const bool *in_group_flags;
Robert Sloana27a6a42017-09-05 08:39:28 -0700339 // group_min contains the minimal index so far found in a group, or -1 if no
340 // such value exists yet.
David Benjamin1b249672016-12-06 18:25:50 -0500341 int group_min = -1;
342
Robert Sloanfe7cd212017-08-07 09:03:39 -0700343 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
Robert Sloan6f79a502017-04-03 09:16:40 -0700344 ssl_parse_client_cipher_list(client_hello);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700345 if (!client_pref) {
346 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500347 }
348
349 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Robert Sloanc6ebb282018-04-30 10:10:26 -0700350 prio = server_pref->ciphers.get();
David Benjamin1b249672016-12-06 18:25:50 -0500351 in_group_flags = server_pref->in_group_flags;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700352 allow = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500353 } else {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700354 prio = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500355 in_group_flags = NULL;
Robert Sloanc6ebb282018-04-30 10:10:26 -0700356 allow = server_pref->ciphers.get();
David Benjamin1b249672016-12-06 18:25:50 -0500357 }
358
Robert Sloan6f79a502017-04-03 09:16:40 -0700359 uint32_t mask_k, mask_a;
David Benjamin1b249672016-12-06 18:25:50 -0500360 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
361
362 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700363 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin1b249672016-12-06 18:25:50 -0500364
Robert Sloan6f79a502017-04-03 09:16:40 -0700365 size_t cipher_index;
Robert Sloana27a6a42017-09-05 08:39:28 -0700366 if (// Check if the cipher is supported for the current version.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700367 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
368 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700369 // Check the cipher is supported for the server configuration.
Robert Sloan6f79a502017-04-03 09:16:40 -0700370 (c->algorithm_mkey & mask_k) &&
371 (c->algorithm_auth & mask_a) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700372 // Check the cipher is in the |allow| list.
Robert Sloan6f79a502017-04-03 09:16:40 -0700373 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
Robert Sloanc6ebb282018-04-30 10:10:26 -0700374 if (in_group_flags != NULL && in_group_flags[i]) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700375 // This element of |prio| is in a group. Update the minimum index found
376 // so far and continue looking.
David Benjamin1b249672016-12-06 18:25:50 -0500377 if (group_min == -1 || (size_t)group_min > cipher_index) {
378 group_min = cipher_index;
379 }
380 } else {
381 if (group_min != -1 && (size_t)group_min < cipher_index) {
382 cipher_index = group_min;
383 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700384 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin1b249672016-12-06 18:25:50 -0500385 }
386 }
387
Robert Sloanc6ebb282018-04-30 10:10:26 -0700388 if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700389 // We are about to leave a group, but we found a match in it, so that's
390 // our answer.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700391 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin1b249672016-12-06 18:25:50 -0500392 }
393 }
394
Robert Sloanfe7cd212017-08-07 09:03:39 -0700395 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500396}
397
Robert Sloana27a6a42017-09-05 08:39:28 -0700398static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
399 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
Robert Sloandc2f6092018-04-10 10:22:33 -0700400 hs->state = state12_read_client_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700401 return ssl_hs_ok;
402}
403
Robert Sloanc9abfe42018-11-26 12:19:07 -0800404// is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent
405// from a JDK 11 client (11.0.1 or earlier) with both TLS 1.3 and a prior
406// version enabled.
407static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) {
408 // JDK 11 ClientHellos contain a number of unusual properties which should
409 // limit false positives.
410
411 // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern
412 // clients implement ChaCha20-Poly1305.
413 if (ssl_client_cipher_list_contains_cipher(
414 client_hello, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
415 return false;
416 }
417
418 // JDK 11 always sends extensions in a particular order.
419 constexpr uint16_t kMaxFragmentLength = 0x0001;
420 constexpr uint16_t kStatusRequestV2 = 0x0011;
421 static CONSTEXPR_ARRAY struct {
422 uint16_t id;
423 bool required;
424 } kJavaExtensions[] = {
425 {TLSEXT_TYPE_server_name, false},
426 {kMaxFragmentLength, false},
427 {TLSEXT_TYPE_status_request, false},
428 {TLSEXT_TYPE_supported_groups, true},
429 {TLSEXT_TYPE_ec_point_formats, false},
430 {TLSEXT_TYPE_signature_algorithms, true},
431 // Java always sends signature_algorithms_cert.
432 {TLSEXT_TYPE_signature_algorithms_cert, true},
433 {TLSEXT_TYPE_application_layer_protocol_negotiation, false},
434 {kStatusRequestV2, false},
435 {TLSEXT_TYPE_extended_master_secret, false},
436 {TLSEXT_TYPE_supported_versions, true},
437 {TLSEXT_TYPE_cookie, false},
438 {TLSEXT_TYPE_psk_key_exchange_modes, true},
439 {TLSEXT_TYPE_key_share, true},
440 {TLSEXT_TYPE_renegotiate, false},
441 {TLSEXT_TYPE_pre_shared_key, false},
442 };
443 Span<const uint8_t> sigalgs, sigalgs_cert;
444 bool has_status_request = false, has_status_request_v2 = false;
445 CBS extensions, supported_groups;
446 CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
447 for (const auto &java_extension : kJavaExtensions) {
448 CBS copy = extensions;
449 uint16_t id;
450 if (CBS_get_u16(&copy, &id) && id == java_extension.id) {
451 // The next extension is the one we expected.
452 extensions = copy;
453 CBS body;
454 if (!CBS_get_u16_length_prefixed(&extensions, &body)) {
455 return false;
456 }
457 switch (id) {
458 case TLSEXT_TYPE_status_request:
459 has_status_request = true;
460 break;
461 case kStatusRequestV2:
462 has_status_request_v2 = true;
463 break;
464 case TLSEXT_TYPE_signature_algorithms:
465 sigalgs = body;
466 break;
467 case TLSEXT_TYPE_signature_algorithms_cert:
468 sigalgs_cert = body;
469 break;
470 case TLSEXT_TYPE_supported_groups:
471 supported_groups = body;
472 break;
473 }
474 } else if (java_extension.required) {
475 return false;
476 }
477 }
478 if (CBS_len(&extensions) != 0) {
479 return false;
480 }
481
482 // JDK 11 never advertises X25519. It is not offered by default, and
483 // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern
484 // clients implement X25519.
485 while (CBS_len(&supported_groups) > 0) {
486 uint16_t group;
487 if (!CBS_get_u16(&supported_groups, &group) ||
488 group == SSL_CURVE_X25519) {
489 return false;
490 }
491 }
492
493 if (// JDK 11 always sends the same contents in signature_algorithms and
494 // signature_algorithms_cert. This is unusual: signature_algorithms_cert,
495 // if omitted, is treated as if it were signature_algorithms.
496 sigalgs != sigalgs_cert ||
497 // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it
498 // sends status_request. This is unusual: status_request_v2 is not widely
499 // implemented.
500 has_status_request != has_status_request_v2) {
501 return false;
502 }
503
504 return true;
505}
506
Robert Sloana27a6a42017-09-05 08:39:28 -0700507static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500508 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700509
Robert Sloan84377092017-08-14 09:33:19 -0700510 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700511 if (!ssl->method->get_message(ssl, &msg)) {
512 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700513 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700514
Robert Sloan84377092017-08-14 09:33:19 -0700515 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700516 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400517 }
518
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100519 if (hs->config->handoff) {
Robert Sloan8542c082018-02-05 09:07:34 -0800520 return ssl_hs_handoff;
521 }
522
David Benjamin1b249672016-12-06 18:25:50 -0500523 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700524 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400525 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700526 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700527 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400528 }
529
Robert Sloana27a6a42017-09-05 08:39:28 -0700530 // Run the early callback.
Robert Sloan5d625782017-02-13 09:55:39 -0800531 if (ssl->ctx->select_certificate_cb != NULL) {
532 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700533 case ssl_select_cert_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700534 return ssl_hs_certificate_selection_pending;
David Benjaminc895d6b2016-08-11 13:26:41 -0400535
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700536 case ssl_select_cert_error:
Robert Sloana27a6a42017-09-05 08:39:28 -0700537 // Connection rejected.
Robert Sloan5d625782017-02-13 09:55:39 -0800538 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700539 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700540 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400541
Robert Sloan5d625782017-02-13 09:55:39 -0800542 default:
543 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400544 }
Robert Sloan5d625782017-02-13 09:55:39 -0800545 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800546
Robert Sloana27a6a42017-09-05 08:39:28 -0700547 // Freeze the version range after the early callback.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100548 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700549 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700550 }
551
Robert Sloanc9abfe42018-11-26 12:19:07 -0800552 if (hs->config->jdk11_workaround &&
553 is_probably_jdk11_with_tls13(&client_hello)) {
554 hs->apply_jdk11_workaround = true;
555 }
556
Robert Sloan5d625782017-02-13 09:55:39 -0800557 uint8_t alert = SSL_AD_DECODE_ERROR;
558 if (!negotiate_version(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700559 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700560 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800561 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800562
Robert Sloanf6200e72017-07-10 08:09:18 -0700563 hs->client_version = client_hello.version;
Robert Sloan5d625782017-02-13 09:55:39 -0800564 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
565 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700566 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800567 }
568 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
569 client_hello.random_len);
570
Robert Sloana27a6a42017-09-05 08:39:28 -0700571 // Only null compression is supported. TLS 1.3 further requires the peer
572 // advertise no other compression.
Robert Sloan5d625782017-02-13 09:55:39 -0800573 if (OPENSSL_memchr(client_hello.compression_methods, 0,
574 client_hello.compression_methods_len) == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700575 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
Robert Sloan5d625782017-02-13 09:55:39 -0800576 client_hello.compression_methods_len != 1)) {
577 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700578 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700579 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800580 }
581
Robert Sloana27a6a42017-09-05 08:39:28 -0700582 // TLS extensions.
Robert Sloan5d625782017-02-13 09:55:39 -0800583 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
584 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Robert Sloana27a6a42017-09-05 08:39:28 -0700585 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800586 }
587
Robert Sloandc2f6092018-04-10 10:22:33 -0700588 hs->state = state12_select_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700589 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800590}
591
Robert Sloana27a6a42017-09-05 08:39:28 -0700592static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800593 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700594
Robert Sloan84377092017-08-14 09:33:19 -0700595 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700596 if (!ssl->method->get_message(ssl, &msg)) {
597 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700598 }
599
Robert Sloana27a6a42017-09-05 08:39:28 -0700600 // Call |cert_cb| to update server certificates if required.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100601 if (hs->config->cert->cert_cb != NULL) {
602 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
Robert Sloan5d625782017-02-13 09:55:39 -0800603 if (rv == 0) {
604 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700605 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700606 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400607 }
Robert Sloan5d625782017-02-13 09:55:39 -0800608 if (rv < 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700609 return ssl_hs_x509_lookup;
David Benjaminc895d6b2016-08-11 13:26:41 -0400610 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400611 }
612
Robert Sloan572a4e22017-04-17 10:52:19 -0700613 if (!ssl_on_certificate_selected(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700614 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500615 }
616
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100617 if (hs->ocsp_stapling_requested &&
618 ssl->ctx->legacy_ocsp_callback != nullptr) {
619 switch (ssl->ctx->legacy_ocsp_callback(
620 ssl, ssl->ctx->legacy_ocsp_callback_arg)) {
621 case SSL_TLSEXT_ERR_OK:
622 break;
623 case SSL_TLSEXT_ERR_NOACK:
624 hs->ocsp_stapling_requested = false;
625 break;
626 default:
627 OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR);
628 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
629 return ssl_hs_error;
630 }
631 }
632
Robert Sloan921ef2c2017-10-17 09:02:20 -0700633 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700634 // Jump to the TLS 1.3 state machine.
Robert Sloandc2f6092018-04-10 10:22:33 -0700635 hs->state = state12_tls13;
Robert Sloana27a6a42017-09-05 08:39:28 -0700636 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800637 }
638
639 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700640 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700641 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800642 }
643
Robert Sloana27a6a42017-09-05 08:39:28 -0700644 // Negotiate the cipher suite. This must be done after |cert_cb| so the
645 // certificate is finalized.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100646 SSLCipherPreferenceList *prefs = hs->config->cipher_list
647 ? hs->config->cipher_list.get()
648 : ssl->ctx->cipher_list.get();
649 hs->new_cipher = ssl3_choose_cipher(hs, &client_hello, prefs);
Robert Sloana94fe052017-02-21 08:49:28 -0800650 if (hs->new_cipher == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -0800651 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700652 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700653 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800654 }
655
Robert Sloandc2f6092018-04-10 10:22:33 -0700656 hs->state = state12_select_parameters;
Robert Sloana27a6a42017-09-05 08:39:28 -0700657 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800658}
659
Robert Sloana27a6a42017-09-05 08:39:28 -0700660static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
661 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
662 if (wait == ssl_hs_ok) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700663 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -0700664 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800665 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500666
Robert Sloana27a6a42017-09-05 08:39:28 -0700667 return wait;
668}
669
670static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
671 SSL *const ssl = hs->ssl;
672
673 SSLMessage msg;
674 if (!ssl->method->get_message(ssl, &msg)) {
675 return ssl_hs_read_message;
676 }
677
678 SSL_CLIENT_HELLO client_hello;
679 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
680 return ssl_hs_error;
681 }
682
683 // Determine whether we are doing session resumption.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700684 UniquePtr<SSL_SESSION> session;
Robert Sloana27a6a42017-09-05 08:39:28 -0700685 bool tickets_supported = false, renew_ticket = false;
686 enum ssl_hs_wait_t wait = ssl_get_prev_session(
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100687 hs, &session, &tickets_supported, &renew_ticket, &client_hello);
Robert Sloana27a6a42017-09-05 08:39:28 -0700688 if (wait != ssl_hs_ok) {
689 return wait;
Steven Valdez909b19f2016-11-21 15:35:44 -0500690 }
691
Robert Sloana12bf462017-07-17 07:08:26 -0700692 if (session) {
Robert Sloana94fe052017-02-21 08:49:28 -0800693 if (session->extended_master_secret && !hs->extended_master_secret) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700694 // A ClientHello without EMS that attempts to resume a session with EMS
695 // is fatal to the connection.
Steven Valdez909b19f2016-11-21 15:35:44 -0500696 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700697 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700698 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500699 }
700
Robert Sloana12bf462017-07-17 07:08:26 -0700701 if (!ssl_session_is_resumable(hs, session.get()) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700702 // If the client offers the EMS extension, but the previous session
703 // didn't use it, then negotiate a new session.
Robert Sloana94fe052017-02-21 08:49:28 -0800704 hs->extended_master_secret != session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -0700705 session.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500706 }
707 }
708
Robert Sloana12bf462017-07-17 07:08:26 -0700709 if (session) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700710 // Use the old session.
David Benjamin1b249672016-12-06 18:25:50 -0500711 hs->ticket_expected = renew_ticket;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100712 ssl->session = std::move(session);
Robert Sloana27a6a42017-09-05 08:39:28 -0700713 ssl->s3->session_reused = true;
Steven Valdez909b19f2016-11-21 15:35:44 -0500714 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500715 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500716 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500717 if (!ssl_get_new_session(hs, 1 /* server */)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700718 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500719 }
720
Robert Sloana27a6a42017-09-05 08:39:28 -0700721 // Clear the session ID if we want the session to be single-use.
Steven Valdez909b19f2016-11-21 15:35:44 -0500722 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
Robert Sloana94fe052017-02-21 08:49:28 -0800723 hs->new_session->session_id_length = 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500724 }
725 }
726
727 if (ssl->ctx->dos_protection_cb != NULL &&
728 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700729 // Connection rejected for DOS reasons.
Steven Valdez909b19f2016-11-21 15:35:44 -0500730 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700731 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700732 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500733 }
734
735 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800736 hs->new_session->cipher = hs->new_cipher;
Steven Valdez909b19f2016-11-21 15:35:44 -0500737
Robert Sloana27a6a42017-09-05 08:39:28 -0700738 // Determine whether to request a client certificate.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100739 hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700740 // Only request a certificate if Channel ID isn't negotiated.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100741 if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
742 ssl->s3->channel_id_valid) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700743 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700744 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700745 // CertificateRequest may only be sent in certificate-based ciphers.
Robert Sloana94fe052017-02-21 08:49:28 -0800746 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700747 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700748 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400749
David Benjamin1b249672016-12-06 18:25:50 -0500750 if (!hs->cert_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700751 // OpenSSL returns X509_V_OK when no certificates are requested. This is
752 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800753 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400754 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800755 }
756
Robert Sloana27a6a42017-09-05 08:39:28 -0700757 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
758 // deferred. Complete it now.
Robert Sloane56da3e2017-06-26 08:26:42 -0700759 uint8_t alert = SSL_AD_DECODE_ERROR;
760 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700761 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700762 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500763 }
764
Robert Sloana27a6a42017-09-05 08:39:28 -0700765 // Now that all parameters are known, initialize the handshake hash and hash
766 // the ClientHello.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700767 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
Robert Sloan84377092017-08-14 09:33:19 -0700768 !ssl_hash_message(hs, msg)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700769 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700770 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000771 }
772
Robert Sloandc2f6092018-04-10 10:22:33 -0700773 // Handback includes the whole handshake transcript, so we cannot free the
774 // transcript buffer in the handback case.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100775 if (!hs->cert_request && !hs->handback) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700776 hs->transcript.FreeBuffer();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800777 }
778
Robert Sloan84377092017-08-14 09:33:19 -0700779 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700780
Robert Sloandc2f6092018-04-10 10:22:33 -0700781 hs->state = state12_send_server_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700782 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800783}
784
Robert Sloanc9abfe42018-11-26 12:19:07 -0800785static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) {
786 out = out.subspan(out.size() - in.size());
787 assert(out.size() == in.size());
788 OPENSSL_memcpy(out.data(), in.data(), in.size());
789}
790
Robert Sloana27a6a42017-09-05 08:39:28 -0700791static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500792 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -0800793
Robert Sloana27a6a42017-09-05 08:39:28 -0700794 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
795 // known attack while we fix ChannelID itself.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100796 if (ssl->s3->channel_id_valid &&
Robert Sloana94fe052017-02-21 08:49:28 -0800797 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100798 ssl->s3->channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800799 }
800
Robert Sloana27a6a42017-09-05 08:39:28 -0700801 // If this is a resumption and the original handshake didn't support
802 // ChannelID then we didn't record the original handshake hashes in the
803 // session and so cannot resume with ChannelIDs.
David Benjaminc895d6b2016-08-11 13:26:41 -0400804 if (ssl->session != NULL &&
805 ssl->session->original_handshake_hash_len == 0) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100806 ssl->s3->channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800807 }
808
Robert Sloan7d422bc2017-03-06 10:04:29 -0800809 struct OPENSSL_timeval now;
David Benjaminc895d6b2016-08-11 13:26:41 -0400810 ssl_get_current_time(ssl, &now);
811 ssl->s3->server_random[0] = now.tv_sec >> 24;
812 ssl->s3->server_random[1] = now.tv_sec >> 16;
813 ssl->s3->server_random[2] = now.tv_sec >> 8;
814 ssl->s3->server_random[3] = now.tv_sec;
815 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700816 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800817 }
818
Robert Sloand9e572d2018-08-27 12:27:00 -0700819 // Implement the TLS 1.3 anti-downgrade feature.
820 if (ssl_supports_version(hs, TLS1_3_VERSION)) {
821 if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
Robert Sloanc9abfe42018-11-26 12:19:07 -0800822 if (hs->apply_jdk11_workaround) {
823 // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it
824 // here. However, the signal is only effective if all TLS 1.2
825 // ServerHellos produced by the server are marked. Thus we send a
826 // different non-standard signal for the time being, until JDK 11.0.2 is
827 // released and clients have updated.
828 copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom);
829 } else {
830 copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom);
831 }
Robert Sloand9e572d2018-08-27 12:27:00 -0700832 } else {
Robert Sloanc9abfe42018-11-26 12:19:07 -0800833 copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom);
Robert Sloand9e572d2018-08-27 12:27:00 -0700834 }
Robert Sloan0da43952018-01-03 15:13:14 -0800835 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400836
Robert Sloanb6d070c2017-07-24 08:40:01 -0700837 const SSL_SESSION *session = hs->new_session.get();
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100838 if (ssl->session != nullptr) {
839 session = ssl->session.get();
David Benjaminc895d6b2016-08-11 13:26:41 -0400840 }
841
Robert Sloanb6d070c2017-07-24 08:40:01 -0700842 ScopedCBB cbb;
843 CBB body, session_id;
844 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400845 !CBB_add_u16(&body, ssl->version) ||
846 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
847 !CBB_add_u8_length_prefixed(&body, &session_id) ||
848 !CBB_add_bytes(&session_id, session->session_id,
849 session->session_id_length) ||
Robert Sloana94fe052017-02-21 08:49:28 -0800850 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400851 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -0500852 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700853 !ssl_add_message_cbb(ssl, cbb.get())) {
Kenny Roote99801b2015-11-06 15:31:15 -0800854 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700855 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800856 }
857
Robert Sloana27a6a42017-09-05 08:39:28 -0700858 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700859 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -0700860 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -0700861 hs->state = state12_send_server_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700862 }
863 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800864}
865
Robert Sloana27a6a42017-09-05 08:39:28 -0700866static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500867 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700868 ScopedCBB cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000869
Robert Sloane56da3e2017-06-26 08:26:42 -0700870 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100871 if (!ssl_has_certificate(hs->config)) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700872 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloana27a6a42017-09-05 08:39:28 -0700873 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700874 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800875
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100876 if (!ssl_output_cert_chain(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700877 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700878 }
879
880 if (hs->certificate_status_expected) {
881 CBB body, ocsp_response;
Robert Sloana12bf462017-07-17 07:08:26 -0700882 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700883 SSL3_MT_CERTIFICATE_STATUS) ||
884 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
885 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100886 !CBB_add_bytes(
887 &ocsp_response,
888 CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
889 CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700890 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700891 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700892 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700893 }
894 }
895 }
896
Robert Sloana27a6a42017-09-05 08:39:28 -0700897 // Assemble ServerKeyExchange parameters if needed.
Robert Sloane56da3e2017-06-26 08:26:42 -0700898 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
899 uint32_t alg_a = hs->new_cipher->algorithm_auth;
900 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100901 ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700902 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
903 // the client and server randoms for the signing transcript.
Robert Sloane56da3e2017-06-26 08:26:42 -0700904 CBB child;
Robert Sloana12bf462017-07-17 07:08:26 -0700905 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
906 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
907 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700908 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400909 }
910
Robert Sloana27a6a42017-09-05 08:39:28 -0700911 // PSK ciphers begin with an identity hint.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800912 if (alg_a & SSL_aPSK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100913 size_t len = hs->config->psk_identity_hint == nullptr
914 ? 0
915 : strlen(hs->config->psk_identity_hint.get());
Robert Sloana12bf462017-07-17 07:08:26 -0700916 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100917 !CBB_add_bytes(&child,
918 (const uint8_t *)hs->config->psk_identity_hint.get(),
Adam Langley4139edb2016-01-13 15:00:54 -0800919 len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700920 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800921 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800922 }
923
Robert Sloan572a4e22017-04-17 10:52:19 -0700924 if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700925 // Determine the group to use.
David Benjamind316cba2016-06-02 16:17:39 -0400926 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -0500927 if (!tls1_get_shared_group(hs, &group_id)) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700928 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700929 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700930 return ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700931 }
Robert Sloana94fe052017-02-21 08:49:28 -0800932 hs->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -0800933
Robert Sloana27a6a42017-09-05 08:39:28 -0700934 // Set up ECDH, generate a key, and emit the public half.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700935 hs->key_share = SSLKeyShare::Create(group_id);
936 if (!hs->key_share ||
Robert Sloana12bf462017-07-17 07:08:26 -0700937 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
938 !CBB_add_u16(cbb.get(), group_id) ||
939 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700940 !hs->key_share->Offer(&child)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700941 return ssl_hs_error;
David Benjamind316cba2016-06-02 16:17:39 -0400942 }
Adam Langley4139edb2016-01-13 15:00:54 -0800943 } else {
944 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800945 }
946
Robert Sloan4562e9d2017-10-02 10:26:51 -0700947 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700948 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400949 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400950 }
951
Robert Sloandc2f6092018-04-10 10:22:33 -0700952 hs->state = state12_send_server_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -0700953 return ssl_hs_ok;
Robert Sloane56da3e2017-06-26 08:26:42 -0700954}
955
Robert Sloana27a6a42017-09-05 08:39:28 -0700956static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700957 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700958
Robert Sloan4562e9d2017-10-02 10:26:51 -0700959 if (hs->server_params.size() == 0) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700960 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -0700961 return ssl_hs_ok;
962 }
963
Robert Sloanb6d070c2017-07-24 08:40:01 -0700964 ScopedCBB cbb;
965 CBB body, child;
966 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -0400967 SSL3_MT_SERVER_KEY_EXCHANGE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700968 // |hs->server_params| contains a prefix for signing.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700969 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
970 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
971 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700972 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800973 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800974
Robert Sloana27a6a42017-09-05 08:39:28 -0700975 // Add a signature.
Robert Sloana94fe052017-02-21 08:49:28 -0800976 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100977 if (!ssl_has_private_key(hs->config)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700978 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700979 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800980 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800981
Robert Sloana27a6a42017-09-05 08:39:28 -0700982 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -0400983 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -0500984 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100985 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700986 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400987 }
Robert Sloan921ef2c2017-10-17 09:02:20 -0700988 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400989 if (!CBB_add_u16(&body, signature_algorithm)) {
990 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700991 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700992 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400993 }
994 }
995
Robert Sloana27a6a42017-09-05 08:39:28 -0700996 // Add space for the signature.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700997 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -0400998 uint8_t *ptr;
999 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1000 !CBB_reserve(&child, &ptr, max_sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001001 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001002 }
1003
Adam Langley4139edb2016-01-13 15:00:54 -08001004 size_t sig_len;
Robert Sloane56da3e2017-06-26 08:26:42 -07001005 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
Robert Sloan921ef2c2017-10-17 09:02:20 -07001006 signature_algorithm, hs->server_params)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001007 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001008 if (!CBB_did_write(&child, sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001009 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001010 }
1011 break;
1012 case ssl_private_key_failure:
Robert Sloana27a6a42017-09-05 08:39:28 -07001013 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001014 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -07001015 return ssl_hs_private_key_operation;
Adam Langley4139edb2016-01-13 15:00:54 -08001016 }
Adam Langley1e4884f2015-09-24 10:57:52 -07001017 }
1018
Robert Sloanb6d070c2017-07-24 08:40:01 -07001019 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001020 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +00001021 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001022
Robert Sloan4562e9d2017-10-02 10:26:51 -07001023 hs->server_params.Reset();
David Benjaminc895d6b2016-08-11 13:26:41 -04001024
Robert Sloandc2f6092018-04-10 10:22:33 -07001025 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001026 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001027}
1028
Robert Sloana27a6a42017-09-05 08:39:28 -07001029static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001030 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001031
Robert Sloanb6d070c2017-07-24 08:40:01 -07001032 ScopedCBB cbb;
1033 CBB body;
David Benjaminc895d6b2016-08-11 13:26:41 -04001034
Robert Sloane56da3e2017-06-26 08:26:42 -07001035 if (hs->cert_request) {
1036 CBB cert_types, sigalgs_cbb;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001037 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -07001038 SSL3_MT_CERTIFICATE_REQUEST) ||
1039 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1040 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001041 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001042 // TLS 1.2 has no way to specify different signature algorithms for
1043 // certificates and the online signature, so emit the more restrictive
1044 // certificate list.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001045 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -07001046 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
Robert Sloan5cbb5c82018-04-24 11:35:46 -07001047 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */))) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001048 !ssl_add_client_CA_list(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001049 !ssl_add_message_cbb(ssl, cbb.get())) {
1050 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001051 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001052 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001053 }
1054
Robert Sloanb6d070c2017-07-24 08:40:01 -07001055 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1056 SSL3_MT_SERVER_HELLO_DONE) ||
1057 !ssl_add_message_cbb(ssl, cbb.get())) {
1058 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001059 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001060 }
1061
Robert Sloandc2f6092018-04-10 10:22:33 -07001062 hs->state = state12_read_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001063 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001064}
1065
Robert Sloana27a6a42017-09-05 08:39:28 -07001066static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001067 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001068
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001069 if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
1070 return ssl_hs_handback;
Robert Sloandc2f6092018-04-10 10:22:33 -07001071 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001072 if (!hs->cert_request) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001073 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001074 return ssl_hs_ok;
1075 }
David Benjamin6e899c72016-06-09 18:02:18 -04001076
Robert Sloan84377092017-08-14 09:33:19 -07001077 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001078 if (!ssl->method->get_message(ssl, &msg)) {
1079 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001080 }
1081
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001082 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001083 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001084 }
1085
Robert Sloan84377092017-08-14 09:33:19 -07001086 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001087 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001088 }
1089
Robert Sloan84377092017-08-14 09:33:19 -07001090 CBS certificate_msg = msg.body;
Robert Sloan5d625782017-02-13 09:55:39 -08001091 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001092 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
1093 hs->config->retain_only_sha256_of_client_certs
Robert Sloanfe7cd212017-08-07 09:03:39 -07001094 ? hs->new_session->peer_sha256
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001095 : nullptr,
Robert Sloanfe7cd212017-08-07 09:03:39 -07001096 &certificate_msg, ssl->ctx->pool)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001097 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001098 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001099 }
1100
Steven Valdeze7531f02016-12-14 13:29:57 -05001101 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001102 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin6e899c72016-06-09 18:02:18 -04001103 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001104 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001105 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001106 }
1107
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001108 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001109 // No client certificate so the handshake buffer may be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001110 hs->transcript.FreeBuffer();
David Benjamin6e899c72016-06-09 18:02:18 -04001111
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001112 if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001113 // Fail for TLS only if we required a certificate
David Benjamin6e899c72016-06-09 18:02:18 -04001114 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001115 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001116 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001117 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001118
Robert Sloana27a6a42017-09-05 08:39:28 -07001119 // OpenSSL returns X509_V_OK when no certificates are received. This is
1120 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -08001121 hs->new_session->verify_result = X509_V_OK;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001122 } else if (hs->config->retain_only_sha256_of_client_certs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001123 // The hash will have been filled in.
Robert Sloana94fe052017-02-21 08:49:28 -08001124 hs->new_session->peer_sha256_valid = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001125 }
David Benjamin6e899c72016-06-09 18:02:18 -04001126
Robert Sloan84377092017-08-14 09:33:19 -07001127 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001128 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001129 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001130}
1131
Robert Sloana27a6a42017-09-05 08:39:28 -07001132static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001133 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001134 switch (ssl_verify_peer_cert(hs)) {
1135 case ssl_verify_ok:
1136 break;
1137 case ssl_verify_invalid:
1138 return ssl_hs_error;
1139 case ssl_verify_retry:
1140 return ssl_hs_certificate_verify;
1141 }
1142 }
1143
Robert Sloandc2f6092018-04-10 10:22:33 -07001144 hs->state = state12_read_client_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -07001145 return ssl_hs_ok;
1146}
1147
1148static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001149 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -07001150 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001151 if (!ssl->method->get_message(ssl, &msg)) {
1152 return ssl_hs_read_message;
Robert Sloane56da3e2017-06-26 08:26:42 -07001153 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001154
Robert Sloan84377092017-08-14 09:33:19 -07001155 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001156 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001157 }
1158
Robert Sloan84377092017-08-14 09:33:19 -07001159 CBS client_key_exchange = msg.body;
Robert Sloane56da3e2017-06-26 08:26:42 -07001160 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1161 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001162
Robert Sloana27a6a42017-09-05 08:39:28 -07001163 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001164 if (alg_a & SSL_aPSK) {
1165 CBS psk_identity;
1166
Robert Sloana27a6a42017-09-05 08:39:28 -07001167 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1168 // then this is the only field in the message.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001169 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1170 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001171 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001172 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001173 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001174 }
1175
1176 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1177 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001178 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001179 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001180 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001181 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001182 char *raw = nullptr;
1183 if (!CBS_strdup(&psk_identity, &raw)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001184 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001185 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001186 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001187 }
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001188 hs->new_session->psk_identity.reset(raw);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001189 }
1190
Robert Sloan4562e9d2017-10-02 10:26:51 -07001191 // Depending on the key exchange method, compute |premaster_secret|.
1192 Array<uint8_t> premaster_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001193 if (alg_k & SSL_kRSA) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001194 CBS encrypted_premaster_secret;
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001195 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1196 &encrypted_premaster_secret) ||
1197 CBS_len(&client_key_exchange) != 0) {
1198 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1199 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1200 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001201 }
1202
Robert Sloana27a6a42017-09-05 08:39:28 -07001203 // Allocate a buffer large enough for an RSA decryption.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001204 Array<uint8_t> decrypt_buf;
1205 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1206 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001207 }
1208
Robert Sloana27a6a42017-09-05 08:39:28 -07001209 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1210 // timing-sensitive code below.
Adam Langley4139edb2016-01-13 15:00:54 -08001211 size_t decrypt_len;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001212 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1213 decrypt_buf.size(),
Robert Sloan921ef2c2017-10-17 09:02:20 -07001214 encrypted_premaster_secret)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001215 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001216 break;
1217 case ssl_private_key_failure:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001218 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001219 case ssl_private_key_retry:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001220 return ssl_hs_private_key_operation;
Kenny Roote99801b2015-11-06 15:31:15 -08001221 }
1222
Robert Sloan4562e9d2017-10-02 10:26:51 -07001223 if (decrypt_len != decrypt_buf.size()) {
David Benjamin4969cc92016-04-22 15:02:23 -04001224 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001225 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001226 return ssl_hs_error;
David Benjamin4969cc92016-04-22 15:02:23 -04001227 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001228
Robert Sloana27a6a42017-09-05 08:39:28 -07001229 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1230 // section 7.4.7.1.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001231 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1232 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1233 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001234 }
1235
Robert Sloana27a6a42017-09-05 08:39:28 -07001236 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1237 // publicly invalid.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001238 if (decrypt_len < 11 + premaster_secret.size()) {
Adam Langley4139edb2016-01-13 15:00:54 -08001239 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001240 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001241 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001242 }
1243
Robert Sloana27a6a42017-09-05 08:39:28 -07001244 // Check the padding. See RFC 3447, section 7.2.2.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001245 size_t padding_len = decrypt_len - premaster_secret.size();
Adam Langley4139edb2016-01-13 15:00:54 -08001246 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1247 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001248 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001249 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1250 }
1251 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1252
Robert Sloana27a6a42017-09-05 08:39:28 -07001253 // The premaster secret must begin with |client_version|. This too must be
1254 // checked in constant time (http://eprint.iacr.org/2003/052/).
Adam Langley4139edb2016-01-13 15:00:54 -08001255 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001256 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001257 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001258 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001259
Robert Sloana27a6a42017-09-05 08:39:28 -07001260 // Select, in constant time, either the decrypted premaster or the random
1261 // premaster based on |good|.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001262 for (size_t i = 0; i < premaster_secret.size(); i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001263 premaster_secret[i] = constant_time_select_8(
1264 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1265 }
Robert Sloan572a4e22017-04-17 10:52:19 -07001266 } else if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001267 // Parse the ClientKeyExchange.
Adam Langley4139edb2016-01-13 15:00:54 -08001268 CBS peer_key;
Robert Sloan572a4e22017-04-17 10:52:19 -07001269 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001270 CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001271 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001272 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001273 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001274 }
1275
Robert Sloana27a6a42017-09-05 08:39:28 -07001276 // Compute the premaster.
Robert Sloan5d625782017-02-13 09:55:39 -08001277 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001278 if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001279 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001280 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001281 }
1282
Robert Sloana27a6a42017-09-05 08:39:28 -07001283 // The key exchange state may now be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001284 hs->key_share.reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001285 } else if (!(alg_k & SSL_kPSK)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001286 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001287 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001288 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001289 }
1290
Robert Sloana27a6a42017-09-05 08:39:28 -07001291 // For a PSK cipher suite, the actual pre-master secret is combined with the
1292 // pre-shared key.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001293 if (alg_a & SSL_aPSK) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001294 if (hs->config->psk_server_callback == NULL) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001295 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001296 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001297 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001298 }
1299
Robert Sloana27a6a42017-09-05 08:39:28 -07001300 // Look up the key for the identity.
Robert Sloane56da3e2017-06-26 08:26:42 -07001301 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001302 unsigned psk_len = hs->config->psk_server_callback(
1303 ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
Robert Sloane56da3e2017-06-26 08:26:42 -07001304 if (psk_len > PSK_MAX_PSK_LEN) {
1305 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001306 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001307 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001308 } else if (psk_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001309 // PSK related to the given identity not found.
Robert Sloane56da3e2017-06-26 08:26:42 -07001310 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001311 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001312 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001313 }
1314
1315 if (alg_k & SSL_kPSK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001316 // In plain PSK, other_secret is a block of 0s with the same length as the
1317 // pre-shared key.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001318 if (!premaster_secret.Init(psk_len)) {
1319 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001320 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07001321 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
Robert Sloane56da3e2017-06-26 08:26:42 -07001322 }
1323
Robert Sloanb6d070c2017-07-24 08:40:01 -07001324 ScopedCBB new_premaster;
1325 CBB child;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001326 if (!CBB_init(new_premaster.get(),
Robert Sloan4562e9d2017-10-02 10:26:51 -07001327 2 + psk_len + 2 + premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001328 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07001329 !CBB_add_bytes(&child, premaster_secret.data(),
1330 premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001331 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001332 !CBB_add_bytes(&child, psk, psk_len) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001333 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001334 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001335 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001336 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001337 }
1338
Robert Sloan84377092017-08-14 09:33:19 -07001339 if (!ssl_hash_message(hs, msg)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001340 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001341 }
1342
Robert Sloana27a6a42017-09-05 08:39:28 -07001343 // Compute the master secret.
Robert Sloana94fe052017-02-21 08:49:28 -08001344 hs->new_session->master_key_length = tls1_generate_master_secret(
Robert Sloanb1b54b82017-11-06 13:50:02 -08001345 hs, hs->new_session->master_key, premaster_secret);
Robert Sloana94fe052017-02-21 08:49:28 -08001346 if (hs->new_session->master_key_length == 0) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001347 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001348 }
Robert Sloana94fe052017-02-21 08:49:28 -08001349 hs->new_session->extended_master_secret = hs->extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001350
Robert Sloan84377092017-08-14 09:33:19 -07001351 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001352 hs->state = state12_read_client_certificate_verify;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001353 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001354}
1355
Robert Sloana27a6a42017-09-05 08:39:28 -07001356static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001357 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001358
Robert Sloana27a6a42017-09-05 08:39:28 -07001359 // Only RSA and ECDSA client certificates are supported, so a
1360 // CertificateVerify is required if and only if there's a client certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001361 if (!hs->peer_pubkey) {
1362 hs->transcript.FreeBuffer();
Robert Sloandc2f6092018-04-10 10:22:33 -07001363 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001364 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001365 }
1366
Robert Sloan84377092017-08-14 09:33:19 -07001367 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001368 if (!ssl->method->get_message(ssl, &msg)) {
1369 return ssl_hs_read_message;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001370 }
1371
Robert Sloan84377092017-08-14 09:33:19 -07001372 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001373 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001374 }
1375
Robert Sloan84377092017-08-14 09:33:19 -07001376 CBS certificate_verify = msg.body, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001377
Robert Sloana27a6a42017-09-05 08:39:28 -07001378 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001379 uint16_t signature_algorithm = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001380 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001381 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001382 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001383 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001384 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001385 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001386 uint8_t alert = SSL_AD_DECODE_ERROR;
1387 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001388 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001389 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001390 }
Robert Sloana94fe052017-02-21 08:49:28 -08001391 hs->new_session->peer_signature_algorithm = signature_algorithm;
Robert Sloan2424d842017-05-01 07:46:28 -07001392 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001393 hs->peer_pubkey.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001394 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001395 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001396 return ssl_hs_error;
Adam Langleye9ada862015-05-11 17:20:37 -07001397 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001398
Robert Sloana27a6a42017-09-05 08:39:28 -07001399 // Parse and verify the signature.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001400 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1401 CBS_len(&certificate_verify) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001402 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001403 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001404 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001405 }
1406
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001407 bool sig_ok =
1408 ssl_public_key_verify(ssl, signature, signature_algorithm,
1409 hs->peer_pubkey.get(), hs->transcript.buffer());
David Benjamin4969cc92016-04-22 15:02:23 -04001410#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -07001411 sig_ok = true;
David Benjamin4969cc92016-04-22 15:02:23 -04001412 ERR_clear_error();
1413#endif
1414 if (!sig_ok) {
Kenny Rootb8494592015-09-25 02:29:14 +00001415 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001416 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001417 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001418 }
1419
Robert Sloana27a6a42017-09-05 08:39:28 -07001420 // The handshake buffer is no longer necessary, and we may hash the current
1421 // message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001422 hs->transcript.FreeBuffer();
Robert Sloan84377092017-08-14 09:33:19 -07001423 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001424 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001425 }
1426
Robert Sloan84377092017-08-14 09:33:19 -07001427 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001428 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001429 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001430}
1431
Robert Sloana27a6a42017-09-05 08:39:28 -07001432static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001433 if (hs->handback && hs->ssl->session != NULL) {
1434 return ssl_hs_handback;
1435 }
Robert Sloandc2f6092018-04-10 10:22:33 -07001436 hs->state = state12_process_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001437 return ssl_hs_read_change_cipher_spec;
1438}
1439
1440static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001441 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001442 return ssl_hs_error;
1443 }
1444
Robert Sloandc2f6092018-04-10 10:22:33 -07001445 hs->state = state12_read_next_proto;
Robert Sloana27a6a42017-09-05 08:39:28 -07001446 return ssl_hs_ok;
1447}
1448
1449static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001450 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001451
1452 if (!hs->next_proto_neg_seen) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001453 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001454 return ssl_hs_ok;
1455 }
1456
Robert Sloan84377092017-08-14 09:33:19 -07001457 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001458 if (!ssl->method->get_message(ssl, &msg)) {
1459 return ssl_hs_read_message;
David Benjaminc895d6b2016-08-11 13:26:41 -04001460 }
1461
Robert Sloan84377092017-08-14 09:33:19 -07001462 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1463 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001464 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001465 }
1466
Robert Sloan84377092017-08-14 09:33:19 -07001467 CBS next_protocol = msg.body, selected_protocol, padding;
David Benjamin6e899c72016-06-09 18:02:18 -04001468 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1469 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001470 CBS_len(&next_protocol) != 0) {
1471 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001472 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001473 return ssl_hs_error;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001474 }
1475
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001476 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001477 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001478 }
1479
Robert Sloan84377092017-08-14 09:33:19 -07001480 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001481 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001482 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001483}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001484
Robert Sloana27a6a42017-09-05 08:39:28 -07001485static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001486 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001487
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001488 if (!ssl->s3->channel_id_valid) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001489 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001490 return ssl_hs_ok;
1491 }
1492
Robert Sloan84377092017-08-14 09:33:19 -07001493 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001494 if (!ssl->method->get_message(ssl, &msg)) {
1495 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001496 }
1497
Robert Sloan84377092017-08-14 09:33:19 -07001498 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1499 !tls1_verify_channel_id(hs, msg) ||
1500 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001501 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001502 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001503
Robert Sloan84377092017-08-14 09:33:19 -07001504 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001505 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001506 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001507}
1508
Robert Sloana27a6a42017-09-05 08:39:28 -07001509static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1510 SSL *const ssl = hs->ssl;
1511 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1512 if (wait != ssl_hs_ok) {
1513 return wait;
1514 }
1515
1516 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001517 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001518 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001519 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001520 }
1521
1522 // If this is a full handshake with ChannelID then record the handshake
1523 // hashes in |hs->new_session| in case we need them to verify a
1524 // ChannelID signature on a resumption of this session in the future.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001525 if (ssl->session == NULL && ssl->s3->channel_id_valid &&
Robert Sloana27a6a42017-09-05 08:39:28 -07001526 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1527 return ssl_hs_error;
1528 }
1529
1530 return ssl_hs_ok;
1531}
1532
1533static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001534 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001535
1536 if (hs->ticket_expected) {
1537 const SSL_SESSION *session;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001538 UniquePtr<SSL_SESSION> session_copy;
Robert Sloane56da3e2017-06-26 08:26:42 -07001539 if (ssl->session == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001540 // Fix the timeout to measure from the ticket issuance time.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001541 ssl_session_rebase_time(ssl, hs->new_session.get());
1542 session = hs->new_session.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001543 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001544 // We are renewing an existing session. Duplicate the session to adjust
1545 // the timeout.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001546 session_copy =
1547 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001548 if (!session_copy) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001549 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001550 }
1551
Robert Sloanb6d070c2017-07-24 08:40:01 -07001552 ssl_session_rebase_time(ssl, session_copy.get());
1553 session = session_copy.get();
Steven Valdez909b19f2016-11-21 15:35:44 -05001554 }
1555
Robert Sloanb6d070c2017-07-24 08:40:01 -07001556 ScopedCBB cbb;
1557 CBB body, ticket;
1558 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1559 SSL3_MT_NEW_SESSION_TICKET) ||
1560 !CBB_add_u32(&body, session->timeout) ||
1561 !CBB_add_u16_length_prefixed(&body, &ticket) ||
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001562 !ssl_encrypt_ticket(hs, &ticket, session) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001563 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001564 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001565 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001566 }
1567
Robert Sloane56da3e2017-06-26 08:26:42 -07001568 if (!ssl->method->add_change_cipher_spec(ssl) ||
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001569 !tls1_change_cipher_state(hs, evp_aead_seal) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001570 !ssl_send_finished(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001571 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001572 }
Adam Langleye9ada862015-05-11 17:20:37 -07001573
Robert Sloana27a6a42017-09-05 08:39:28 -07001574 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001575 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001576 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001577 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001578 }
1579 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001580}
Robert Sloanb6d070c2017-07-24 08:40:01 -07001581
Robert Sloana27a6a42017-09-05 08:39:28 -07001582static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1583 SSL *const ssl = hs->ssl;
1584
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001585 if (hs->handback) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001586 return ssl_hs_handback;
1587 }
1588
Robert Sloana27a6a42017-09-05 08:39:28 -07001589 ssl->method->on_handshake_complete(ssl);
1590
1591 // If we aren't retaining peer certificates then we can discard it now.
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001592 if (hs->new_session != NULL &&
1593 hs->config->retain_only_sha256_of_client_certs) {
1594 hs->new_session->certs.reset();
Robert Sloana27a6a42017-09-05 08:39:28 -07001595 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1596 }
1597
Robert Sloana27a6a42017-09-05 08:39:28 -07001598 if (ssl->session != NULL) {
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001599 ssl->s3->established_session = UpRef(ssl->session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001600 } else {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001601 ssl->s3->established_session = std::move(hs->new_session);
Adam Vartanianbfcf3a72018-08-10 14:55:24 +01001602 ssl->s3->established_session->not_resumable = false;
Robert Sloana27a6a42017-09-05 08:39:28 -07001603 }
1604
1605 hs->handshake_finalized = true;
1606 ssl->s3->initial_handshake_complete = true;
1607 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1608
Robert Sloandc2f6092018-04-10 10:22:33 -07001609 hs->state = state12_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001610 return ssl_hs_ok;
1611}
1612
1613enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001614 while (hs->state != state12_done) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001615 enum ssl_hs_wait_t ret = ssl_hs_error;
Robert Sloandc2f6092018-04-10 10:22:33 -07001616 enum tls12_server_hs_state_t state =
1617 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001618 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001619 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001620 ret = do_start_accept(hs);
1621 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001622 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001623 ret = do_read_client_hello(hs);
1624 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001625 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001626 ret = do_select_certificate(hs);
1627 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001628 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001629 ret = do_tls13(hs);
1630 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001631 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001632 ret = do_select_parameters(hs);
1633 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001634 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001635 ret = do_send_server_hello(hs);
1636 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001637 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001638 ret = do_send_server_certificate(hs);
1639 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001640 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001641 ret = do_send_server_key_exchange(hs);
1642 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001643 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001644 ret = do_send_server_hello_done(hs);
1645 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001646 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001647 ret = do_read_client_certificate(hs);
1648 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001649 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001650 ret = do_verify_client_certificate(hs);
1651 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001652 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001653 ret = do_read_client_key_exchange(hs);
1654 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001655 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001656 ret = do_read_client_certificate_verify(hs);
1657 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001658 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001659 ret = do_read_change_cipher_spec(hs);
1660 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001661 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001662 ret = do_process_change_cipher_spec(hs);
1663 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001664 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001665 ret = do_read_next_proto(hs);
1666 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001667 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001668 ret = do_read_channel_id(hs);
1669 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001670 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001671 ret = do_read_client_finished(hs);
1672 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001673 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001674 ret = do_send_server_finished(hs);
1675 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001676 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001677 ret = do_finish_server_handshake(hs);
1678 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001679 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001680 ret = ssl_hs_ok;
1681 break;
1682 }
1683
1684 if (hs->state != state) {
1685 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1686 }
1687
1688 if (ret != ssl_hs_ok) {
1689 return ret;
1690 }
1691 }
1692
1693 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1694 return ssl_hs_ok;
1695}
1696
1697const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001698 enum tls12_server_hs_state_t state =
1699 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001700 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001701 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001702 return "TLS server start_accept";
Robert Sloandc2f6092018-04-10 10:22:33 -07001703 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001704 return "TLS server read_client_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001705 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001706 return "TLS server select_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001707 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001708 return tls13_server_handshake_state(hs);
Robert Sloandc2f6092018-04-10 10:22:33 -07001709 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001710 return "TLS server select_parameters";
Robert Sloandc2f6092018-04-10 10:22:33 -07001711 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001712 return "TLS server send_server_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001713 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001714 return "TLS server send_server_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001715 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001716 return "TLS server send_server_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001717 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001718 return "TLS server send_server_hello_done";
Robert Sloandc2f6092018-04-10 10:22:33 -07001719 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001720 return "TLS server read_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001721 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001722 return "TLS server verify_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001723 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001724 return "TLS server read_client_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001725 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001726 return "TLS server read_client_certificate_verify";
Robert Sloandc2f6092018-04-10 10:22:33 -07001727 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001728 return "TLS server read_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001729 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001730 return "TLS server process_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001731 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001732 return "TLS server read_next_proto";
Robert Sloandc2f6092018-04-10 10:22:33 -07001733 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001734 return "TLS server read_channel_id";
Robert Sloandc2f6092018-04-10 10:22:33 -07001735 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001736 return "TLS server read_client_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001737 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001738 return "TLS server send_server_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001739 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001740 return "TLS server finish_server_handshake";
Robert Sloandc2f6092018-04-10 10:22:33 -07001741 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001742 return "TLS server done";
1743 }
1744
1745 return "TLS server unknown";
1746}
1747
Robert Sloan726e9d12018-09-11 11:45:04 -07001748BSSL_NAMESPACE_END