blob: 5f2f41fd8270f6798775965cfcce542ca3543449 [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 Sloanb6d070c2017-07-24 08:40:01 -0700173namespace bssl {
174
David Benjamin1b249672016-12-06 18:25:50 -0500175int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
176 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)) {
184 return 0;
185 }
186
187 if (got_id == id) {
188 return 1;
189 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 }
191
David Benjaminc895d6b2016-08-11 13:26:41 -0400192 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193}
194
David Benjamin1b249672016-12-06 18:25:50 -0500195static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
196 const SSL_CLIENT_HELLO *client_hello) {
197 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;
207 return 0;
208 }
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
216 0x03, 0x00, // SSL 3
Robert Sloanf6200e72017-07-10 08:09:18 -0700217 };
218
219 static const uint8_t kDTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700220 0xfe, 0xfd, // DTLS 1.2
221 0xfe, 0xff, // DTLS 1.0
Robert Sloanf6200e72017-07-10 08:09:18 -0700222 };
223
224 size_t versions_len = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400225 if (SSL_is_dtls(ssl)) {
226 if (client_hello->version <= DTLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700227 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400228 } else if (client_hello->version <= DTLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700229 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400230 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700231 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
232 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400233 } else {
234 if (client_hello->version >= TLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700235 versions_len = 8;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400236 } else if (client_hello->version >= TLS1_1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700237 versions_len = 6;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400238 } else if (client_hello->version >= TLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700239 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400240 } else if (client_hello->version >= SSL3_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700241 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400242 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700243 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
244 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400245 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 }
247
Robert Sloanf6200e72017-07-10 08:09:18 -0700248 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400249 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 }
251
Robert Sloana27a6a42017-09-05 08:39:28 -0700252 // At this point, the connection's version is known and |ssl->version| is
253 // fixed. Begin enforcing the record-layer version.
254 ssl->s3->have_version = true;
Robert Sloandb4251a2017-09-18 09:38:15 -0700255 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256
Robert Sloana27a6a42017-09-05 08:39:28 -0700257 // Handle FALLBACK_SCSV.
Robert Sloanf6200e72017-07-10 08:09:18 -0700258 if (ssl_client_cipher_list_contains_cipher(client_hello,
259 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Robert Sloan921ef2c2017-10-17 09:02:20 -0700260 ssl_protocol_version(ssl) < hs->max_version) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700261 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
262 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
263 return 0;
264 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400265
Robert Sloanf6200e72017-07-10 08:09:18 -0700266 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267}
268
Robert Sloanfe7cd212017-08-07 09:03:39 -0700269static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
270 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500271 CBS cipher_suites;
272 CBS_init(&cipher_suites, client_hello->cipher_suites,
273 client_hello->cipher_suites_len);
274
Robert Sloanfe7cd212017-08-07 09:03:39 -0700275 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
276 if (!sk) {
David Benjamin1b249672016-12-06 18:25:50 -0500277 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700278 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500279 }
280
281 while (CBS_len(&cipher_suites) > 0) {
282 uint16_t cipher_suite;
283
284 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
285 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700286 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500287 }
288
289 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700290 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500291 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700292 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500293 }
294 }
295
296 return sk;
David Benjamin1b249672016-12-06 18:25:50 -0500297}
298
Robert Sloana27a6a42017-09-05 08:39:28 -0700299// ssl_get_compatible_server_ciphers determines the key exchange and
300// authentication cipher suite masks compatible with the server configuration
301// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
302// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin1b249672016-12-06 18:25:50 -0500303static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
304 uint32_t *out_mask_k,
305 uint32_t *out_mask_a) {
306 SSL *const ssl = hs->ssl;
David Benjamin1b249672016-12-06 18:25:50 -0500307 uint32_t mask_k = 0;
308 uint32_t mask_a = 0;
309
Robert Sloan4d1ac502017-02-06 08:36:14 -0800310 if (ssl_has_certificate(ssl)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700311 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
312 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin1b249672016-12-06 18:25:50 -0500313 mask_k |= SSL_kRSA;
David Benjamin1b249672016-12-06 18:25:50 -0500314 }
315 }
316
Robert Sloana27a6a42017-09-05 08:39:28 -0700317 // Check for a shared group to consider ECDHE ciphers.
David Benjamin1b249672016-12-06 18:25:50 -0500318 uint16_t unused;
319 if (tls1_get_shared_group(hs, &unused)) {
320 mask_k |= SSL_kECDHE;
321 }
322
Robert Sloana27a6a42017-09-05 08:39:28 -0700323 // PSK requires a server callback.
David Benjamin1b249672016-12-06 18:25:50 -0500324 if (ssl->psk_server_callback != NULL) {
325 mask_k |= SSL_kPSK;
326 mask_a |= SSL_aPSK;
327 }
328
329 *out_mask_k = mask_k;
330 *out_mask_a = mask_a;
331}
332
333static const SSL_CIPHER *ssl3_choose_cipher(
334 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
335 const struct ssl_cipher_preference_list_st *server_pref) {
336 SSL *const ssl = hs->ssl;
Robert Sloan6f79a502017-04-03 09:16:40 -0700337 STACK_OF(SSL_CIPHER) *prio, *allow;
Robert Sloana27a6a42017-09-05 08:39:28 -0700338 // in_group_flags will either be NULL, or will point to an array of bytes
339 // which indicate equal-preference groups in the |prio| stack. See the
340 // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
341 // struct.
David Benjamin1b249672016-12-06 18:25:50 -0500342 const uint8_t *in_group_flags;
Robert Sloana27a6a42017-09-05 08:39:28 -0700343 // group_min contains the minimal index so far found in a group, or -1 if no
344 // such value exists yet.
David Benjamin1b249672016-12-06 18:25:50 -0500345 int group_min = -1;
346
Robert Sloanfe7cd212017-08-07 09:03:39 -0700347 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
Robert Sloan6f79a502017-04-03 09:16:40 -0700348 ssl_parse_client_cipher_list(client_hello);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700349 if (!client_pref) {
350 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500351 }
352
353 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700354 prio = server_pref->ciphers;
David Benjamin1b249672016-12-06 18:25:50 -0500355 in_group_flags = server_pref->in_group_flags;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700356 allow = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500357 } else {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700358 prio = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500359 in_group_flags = NULL;
Robert Sloan6f79a502017-04-03 09:16:40 -0700360 allow = server_pref->ciphers;
David Benjamin1b249672016-12-06 18:25:50 -0500361 }
362
Robert Sloan6f79a502017-04-03 09:16:40 -0700363 uint32_t mask_k, mask_a;
David Benjamin1b249672016-12-06 18:25:50 -0500364 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
365
366 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700367 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin1b249672016-12-06 18:25:50 -0500368
Robert Sloan6f79a502017-04-03 09:16:40 -0700369 size_t cipher_index;
Robert Sloana27a6a42017-09-05 08:39:28 -0700370 if (// Check if the cipher is supported for the current version.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700371 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
372 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700373 // Check the cipher is supported for the server configuration.
Robert Sloan6f79a502017-04-03 09:16:40 -0700374 (c->algorithm_mkey & mask_k) &&
375 (c->algorithm_auth & mask_a) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700376 // Check the cipher is in the |allow| list.
Robert Sloan6f79a502017-04-03 09:16:40 -0700377 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500378 if (in_group_flags != NULL && in_group_flags[i] == 1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700379 // This element of |prio| is in a group. Update the minimum index found
380 // so far and continue looking.
David Benjamin1b249672016-12-06 18:25:50 -0500381 if (group_min == -1 || (size_t)group_min > cipher_index) {
382 group_min = cipher_index;
383 }
384 } else {
385 if (group_min != -1 && (size_t)group_min < cipher_index) {
386 cipher_index = group_min;
387 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700388 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin1b249672016-12-06 18:25:50 -0500389 }
390 }
391
392 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700393 // We are about to leave a group, but we found a match in it, so that's
394 // our answer.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700395 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin1b249672016-12-06 18:25:50 -0500396 }
397 }
398
Robert Sloanfe7cd212017-08-07 09:03:39 -0700399 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500400}
401
Robert Sloana27a6a42017-09-05 08:39:28 -0700402static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
403 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
Robert Sloandc2f6092018-04-10 10:22:33 -0700404 hs->state = state12_read_client_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700405 return ssl_hs_ok;
406}
407
408static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500409 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700410
Robert Sloan84377092017-08-14 09:33:19 -0700411 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700412 if (!ssl->method->get_message(ssl, &msg)) {
413 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700414 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700415
Robert Sloan84377092017-08-14 09:33:19 -0700416 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700417 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400418 }
419
Robert Sloan8542c082018-02-05 09:07:34 -0800420 if (ssl->handoff) {
421 return ssl_hs_handoff;
422 }
423
David Benjamin1b249672016-12-06 18:25:50 -0500424 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700425 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400426 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700427 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700428 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400429 }
430
Robert Sloana27a6a42017-09-05 08:39:28 -0700431 // Run the early callback.
Robert Sloan5d625782017-02-13 09:55:39 -0800432 if (ssl->ctx->select_certificate_cb != NULL) {
433 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700434 case ssl_select_cert_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700435 return ssl_hs_certificate_selection_pending;
David Benjaminc895d6b2016-08-11 13:26:41 -0400436
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700437 case ssl_select_cert_error:
Robert Sloana27a6a42017-09-05 08:39:28 -0700438 // Connection rejected.
Robert Sloan5d625782017-02-13 09:55:39 -0800439 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700440 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700441 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400442
Robert Sloan5d625782017-02-13 09:55:39 -0800443 default:
444 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400445 }
Robert Sloan5d625782017-02-13 09:55:39 -0800446 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800447
Robert Sloana27a6a42017-09-05 08:39:28 -0700448 // Freeze the version range after the early callback.
Robert Sloane56da3e2017-06-26 08:26:42 -0700449 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700450 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700451 }
452
Robert Sloan5d625782017-02-13 09:55:39 -0800453 uint8_t alert = SSL_AD_DECODE_ERROR;
454 if (!negotiate_version(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700455 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700456 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800457 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800458
Robert Sloanf6200e72017-07-10 08:09:18 -0700459 hs->client_version = client_hello.version;
Robert Sloan5d625782017-02-13 09:55:39 -0800460 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
461 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700462 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800463 }
464 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
465 client_hello.random_len);
466
Robert Sloana27a6a42017-09-05 08:39:28 -0700467 // Only null compression is supported. TLS 1.3 further requires the peer
468 // advertise no other compression.
Robert Sloan5d625782017-02-13 09:55:39 -0800469 if (OPENSSL_memchr(client_hello.compression_methods, 0,
470 client_hello.compression_methods_len) == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700471 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
Robert Sloan5d625782017-02-13 09:55:39 -0800472 client_hello.compression_methods_len != 1)) {
473 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700474 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700475 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800476 }
477
Robert Sloana27a6a42017-09-05 08:39:28 -0700478 // TLS extensions.
Robert Sloan5d625782017-02-13 09:55:39 -0800479 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
480 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Robert Sloana27a6a42017-09-05 08:39:28 -0700481 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800482 }
483
Robert Sloandc2f6092018-04-10 10:22:33 -0700484 hs->state = state12_select_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700485 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800486}
487
Robert Sloana27a6a42017-09-05 08:39:28 -0700488static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800489 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700490
Robert Sloan84377092017-08-14 09:33:19 -0700491 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700492 if (!ssl->method->get_message(ssl, &msg)) {
493 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700494 }
495
Robert Sloana27a6a42017-09-05 08:39:28 -0700496 // Call |cert_cb| to update server certificates if required.
Robert Sloan5d625782017-02-13 09:55:39 -0800497 if (ssl->cert->cert_cb != NULL) {
498 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
499 if (rv == 0) {
500 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700501 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700502 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400503 }
Robert Sloan5d625782017-02-13 09:55:39 -0800504 if (rv < 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700505 return ssl_hs_x509_lookup;
David Benjaminc895d6b2016-08-11 13:26:41 -0400506 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400507 }
508
Robert Sloan572a4e22017-04-17 10:52:19 -0700509 if (!ssl_on_certificate_selected(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700510 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500511 }
512
Robert Sloan921ef2c2017-10-17 09:02:20 -0700513 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700514 // Jump to the TLS 1.3 state machine.
Robert Sloandc2f6092018-04-10 10:22:33 -0700515 hs->state = state12_tls13;
Robert Sloana27a6a42017-09-05 08:39:28 -0700516 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800517 }
518
519 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700520 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700521 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800522 }
523
Robert Sloana27a6a42017-09-05 08:39:28 -0700524 // Negotiate the cipher suite. This must be done after |cert_cb| so the
525 // certificate is finalized.
Robert Sloana94fe052017-02-21 08:49:28 -0800526 hs->new_cipher =
Robert Sloan5d625782017-02-13 09:55:39 -0800527 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
Robert Sloana94fe052017-02-21 08:49:28 -0800528 if (hs->new_cipher == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -0800529 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700530 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700531 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800532 }
533
Robert Sloandc2f6092018-04-10 10:22:33 -0700534 hs->state = state12_select_parameters;
Robert Sloana27a6a42017-09-05 08:39:28 -0700535 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800536}
537
Robert Sloana27a6a42017-09-05 08:39:28 -0700538static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
539 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
540 if (wait == ssl_hs_ok) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700541 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -0700542 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800543 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500544
Robert Sloana27a6a42017-09-05 08:39:28 -0700545 return wait;
546}
547
548static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
549 SSL *const ssl = hs->ssl;
550
551 SSLMessage msg;
552 if (!ssl->method->get_message(ssl, &msg)) {
553 return ssl_hs_read_message;
554 }
555
556 SSL_CLIENT_HELLO client_hello;
557 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
558 return ssl_hs_error;
559 }
560
561 // Determine whether we are doing session resumption.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700562 UniquePtr<SSL_SESSION> session;
Robert Sloana27a6a42017-09-05 08:39:28 -0700563 bool tickets_supported = false, renew_ticket = false;
564 enum ssl_hs_wait_t wait = ssl_get_prev_session(
565 ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
566 if (wait != ssl_hs_ok) {
567 return wait;
Steven Valdez909b19f2016-11-21 15:35:44 -0500568 }
569
Robert Sloana12bf462017-07-17 07:08:26 -0700570 if (session) {
Robert Sloana94fe052017-02-21 08:49:28 -0800571 if (session->extended_master_secret && !hs->extended_master_secret) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700572 // A ClientHello without EMS that attempts to resume a session with EMS
573 // is fatal to the connection.
Steven Valdez909b19f2016-11-21 15:35:44 -0500574 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700575 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700576 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500577 }
578
Robert Sloana12bf462017-07-17 07:08:26 -0700579 if (!ssl_session_is_resumable(hs, session.get()) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700580 // If the client offers the EMS extension, but the previous session
581 // didn't use it, then negotiate a new session.
Robert Sloana94fe052017-02-21 08:49:28 -0800582 hs->extended_master_secret != session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -0700583 session.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500584 }
585 }
586
Robert Sloana12bf462017-07-17 07:08:26 -0700587 if (session) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700588 // Use the old session.
David Benjamin1b249672016-12-06 18:25:50 -0500589 hs->ticket_expected = renew_ticket;
Robert Sloana12bf462017-07-17 07:08:26 -0700590 ssl->session = session.release();
Robert Sloana27a6a42017-09-05 08:39:28 -0700591 ssl->s3->session_reused = true;
Steven Valdez909b19f2016-11-21 15:35:44 -0500592 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500593 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500594 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500595 if (!ssl_get_new_session(hs, 1 /* server */)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700596 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500597 }
598
Robert Sloana27a6a42017-09-05 08:39:28 -0700599 // Clear the session ID if we want the session to be single-use.
Steven Valdez909b19f2016-11-21 15:35:44 -0500600 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
Robert Sloana94fe052017-02-21 08:49:28 -0800601 hs->new_session->session_id_length = 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500602 }
603 }
604
605 if (ssl->ctx->dos_protection_cb != NULL &&
606 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700607 // Connection rejected for DOS reasons.
Steven Valdez909b19f2016-11-21 15:35:44 -0500608 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700609 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700610 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500611 }
612
613 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800614 hs->new_session->cipher = hs->new_cipher;
Steven Valdez909b19f2016-11-21 15:35:44 -0500615
Robert Sloana27a6a42017-09-05 08:39:28 -0700616 // Determine whether to request a client certificate.
David Benjamin1b249672016-12-06 18:25:50 -0500617 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700618 // Only request a certificate if Channel ID isn't negotiated.
Adam Langley4139edb2016-01-13 15:00:54 -0800619 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
620 ssl->s3->tlsext_channel_id_valid) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700621 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700622 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700623 // CertificateRequest may only be sent in certificate-based ciphers.
Robert Sloana94fe052017-02-21 08:49:28 -0800624 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700625 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700626 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400627
David Benjamin1b249672016-12-06 18:25:50 -0500628 if (!hs->cert_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700629 // OpenSSL returns X509_V_OK when no certificates are requested. This is
630 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800631 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400632 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800633 }
634
Robert Sloana27a6a42017-09-05 08:39:28 -0700635 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
636 // deferred. Complete it now.
Robert Sloane56da3e2017-06-26 08:26:42 -0700637 uint8_t alert = SSL_AD_DECODE_ERROR;
638 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700639 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700640 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500641 }
642
Robert Sloana27a6a42017-09-05 08:39:28 -0700643 // Now that all parameters are known, initialize the handshake hash and hash
644 // the ClientHello.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700645 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
Robert Sloan84377092017-08-14 09:33:19 -0700646 !ssl_hash_message(hs, msg)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700647 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700648 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000649 }
650
Robert Sloandc2f6092018-04-10 10:22:33 -0700651 // Handback includes the whole handshake transcript, so we cannot free the
652 // transcript buffer in the handback case.
653 if (!hs->cert_request && !hs->ssl->handback) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700654 hs->transcript.FreeBuffer();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800655 }
656
Robert Sloan84377092017-08-14 09:33:19 -0700657 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700658
Robert Sloandc2f6092018-04-10 10:22:33 -0700659 hs->state = state12_send_server_hello;
Robert Sloana27a6a42017-09-05 08:39:28 -0700660 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800661}
662
Robert Sloana27a6a42017-09-05 08:39:28 -0700663static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500664 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -0800665
Robert Sloana27a6a42017-09-05 08:39:28 -0700666 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
667 // known attack while we fix ChannelID itself.
Kenny Roote99801b2015-11-06 15:31:15 -0800668 if (ssl->s3->tlsext_channel_id_valid &&
Robert Sloana94fe052017-02-21 08:49:28 -0800669 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700670 ssl->s3->tlsext_channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800671 }
672
Robert Sloana27a6a42017-09-05 08:39:28 -0700673 // If this is a resumption and the original handshake didn't support
674 // ChannelID then we didn't record the original handshake hashes in the
675 // session and so cannot resume with ChannelIDs.
David Benjaminc895d6b2016-08-11 13:26:41 -0400676 if (ssl->session != NULL &&
677 ssl->session->original_handshake_hash_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700678 ssl->s3->tlsext_channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800679 }
680
Robert Sloan7d422bc2017-03-06 10:04:29 -0800681 struct OPENSSL_timeval now;
David Benjaminc895d6b2016-08-11 13:26:41 -0400682 ssl_get_current_time(ssl, &now);
683 ssl->s3->server_random[0] = now.tv_sec >> 24;
684 ssl->s3->server_random[1] = now.tv_sec >> 16;
685 ssl->s3->server_random[2] = now.tv_sec >> 8;
686 ssl->s3->server_random[3] = now.tv_sec;
687 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700688 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800689 }
690
Robert Sloan0da43952018-01-03 15:13:14 -0800691 // Implement the TLS 1.3 anti-downgrade feature, but with a different value.
692 //
693 // For draft TLS 1.3 versions, it is not safe to deploy this feature. However,
694 // some TLS terminators are non-compliant and copy the origin server's value,
695 // so we wish to measure eventual compatibility impact.
696 if (hs->max_version >= TLS1_3_VERSION) {
697 OPENSSL_memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE -
698 sizeof(kDraftDowngradeRandom),
699 kDraftDowngradeRandom, sizeof(kDraftDowngradeRandom));
700 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400701
Robert Sloanb6d070c2017-07-24 08:40:01 -0700702 const SSL_SESSION *session = hs->new_session.get();
David Benjaminc895d6b2016-08-11 13:26:41 -0400703 if (ssl->session != NULL) {
704 session = ssl->session;
705 }
706
Robert Sloanb6d070c2017-07-24 08:40:01 -0700707 ScopedCBB cbb;
708 CBB body, session_id;
709 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400710 !CBB_add_u16(&body, ssl->version) ||
711 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
712 !CBB_add_u8_length_prefixed(&body, &session_id) ||
713 !CBB_add_bytes(&session_id, session->session_id,
714 session->session_id_length) ||
Robert Sloana94fe052017-02-21 08:49:28 -0800715 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400716 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -0500717 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700718 !ssl_add_message_cbb(ssl, cbb.get())) {
Kenny Roote99801b2015-11-06 15:31:15 -0800719 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700720 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800721 }
722
Robert Sloana27a6a42017-09-05 08:39:28 -0700723 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700724 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -0700725 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -0700726 hs->state = state12_send_server_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700727 }
728 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800729}
730
Robert Sloana27a6a42017-09-05 08:39:28 -0700731static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500732 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700733 ScopedCBB cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000734
Robert Sloane56da3e2017-06-26 08:26:42 -0700735 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
736 if (!ssl_has_certificate(ssl)) {
737 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloana27a6a42017-09-05 08:39:28 -0700738 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700739 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800740
Robert Sloan921ef2c2017-10-17 09:02:20 -0700741 if (!ssl_output_cert_chain(ssl)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700742 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700743 }
744
745 if (hs->certificate_status_expected) {
746 CBB body, ocsp_response;
Robert Sloana12bf462017-07-17 07:08:26 -0700747 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700748 SSL3_MT_CERTIFICATE_STATUS) ||
749 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
750 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
751 !CBB_add_bytes(&ocsp_response,
752 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
753 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700754 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700755 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700756 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700757 }
758 }
759 }
760
Robert Sloana27a6a42017-09-05 08:39:28 -0700761 // Assemble ServerKeyExchange parameters if needed.
Robert Sloane56da3e2017-06-26 08:26:42 -0700762 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
763 uint32_t alg_a = hs->new_cipher->algorithm_auth;
764 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
765 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
766
Robert Sloana27a6a42017-09-05 08:39:28 -0700767 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
768 // the client and server randoms for the signing transcript.
Robert Sloane56da3e2017-06-26 08:26:42 -0700769 CBB child;
Robert Sloana12bf462017-07-17 07:08:26 -0700770 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
771 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
772 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700773 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400774 }
775
Robert Sloana27a6a42017-09-05 08:39:28 -0700776 // PSK ciphers begin with an identity hint.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800777 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -0800778 size_t len =
779 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
Robert Sloana12bf462017-07-17 07:08:26 -0700780 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
Adam Langley4139edb2016-01-13 15:00:54 -0800781 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
782 len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700783 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800784 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800785 }
786
Robert Sloan572a4e22017-04-17 10:52:19 -0700787 if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700788 // Determine the group to use.
David Benjamind316cba2016-06-02 16:17:39 -0400789 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -0500790 if (!tls1_get_shared_group(hs, &group_id)) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700791 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700792 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700793 return ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700794 }
Robert Sloana94fe052017-02-21 08:49:28 -0800795 hs->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -0800796
Robert Sloana27a6a42017-09-05 08:39:28 -0700797 // Set up ECDH, generate a key, and emit the public half.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700798 hs->key_share = SSLKeyShare::Create(group_id);
799 if (!hs->key_share ||
Robert Sloana12bf462017-07-17 07:08:26 -0700800 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
801 !CBB_add_u16(cbb.get(), group_id) ||
802 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700803 !hs->key_share->Offer(&child)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700804 return ssl_hs_error;
David Benjamind316cba2016-06-02 16:17:39 -0400805 }
Adam Langley4139edb2016-01-13 15:00:54 -0800806 } else {
807 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800808 }
809
Robert Sloan4562e9d2017-10-02 10:26:51 -0700810 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700811 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400812 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400813 }
814
Robert Sloandc2f6092018-04-10 10:22:33 -0700815 hs->state = state12_send_server_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -0700816 return ssl_hs_ok;
Robert Sloane56da3e2017-06-26 08:26:42 -0700817}
818
Robert Sloana27a6a42017-09-05 08:39:28 -0700819static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700820 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700821
Robert Sloan4562e9d2017-10-02 10:26:51 -0700822 if (hs->server_params.size() == 0) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700823 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -0700824 return ssl_hs_ok;
825 }
826
Robert Sloanb6d070c2017-07-24 08:40:01 -0700827 ScopedCBB cbb;
828 CBB body, child;
829 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -0400830 SSL3_MT_SERVER_KEY_EXCHANGE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700831 // |hs->server_params| contains a prefix for signing.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700832 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
833 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
834 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700835 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800836 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800837
Robert Sloana27a6a42017-09-05 08:39:28 -0700838 // Add a signature.
Robert Sloana94fe052017-02-21 08:49:28 -0800839 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800840 if (!ssl_has_private_key(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700841 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700842 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800843 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800844
Robert Sloana27a6a42017-09-05 08:39:28 -0700845 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -0400846 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -0500847 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700848 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400849 }
Robert Sloan921ef2c2017-10-17 09:02:20 -0700850 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400851 if (!CBB_add_u16(&body, signature_algorithm)) {
852 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700853 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700854 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400855 }
856 }
857
Robert Sloana27a6a42017-09-05 08:39:28 -0700858 // Add space for the signature.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700859 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -0400860 uint8_t *ptr;
861 if (!CBB_add_u16_length_prefixed(&body, &child) ||
862 !CBB_reserve(&child, &ptr, max_sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700863 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400864 }
865
Adam Langley4139edb2016-01-13 15:00:54 -0800866 size_t sig_len;
Robert Sloane56da3e2017-06-26 08:26:42 -0700867 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700868 signature_algorithm, hs->server_params)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800869 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -0800870 if (!CBB_did_write(&child, sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700871 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800872 }
873 break;
874 case ssl_private_key_failure:
Robert Sloana27a6a42017-09-05 08:39:28 -0700875 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800876 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700877 return ssl_hs_private_key_operation;
Adam Langley4139edb2016-01-13 15:00:54 -0800878 }
Adam Langley1e4884f2015-09-24 10:57:52 -0700879 }
880
Robert Sloanb6d070c2017-07-24 08:40:01 -0700881 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700882 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000883 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400884
Robert Sloan4562e9d2017-10-02 10:26:51 -0700885 hs->server_params.Reset();
David Benjaminc895d6b2016-08-11 13:26:41 -0400886
Robert Sloandc2f6092018-04-10 10:22:33 -0700887 hs->state = state12_send_server_hello_done;
Robert Sloana27a6a42017-09-05 08:39:28 -0700888 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800889}
890
Robert Sloana27a6a42017-09-05 08:39:28 -0700891static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500892 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700893
Robert Sloanb6d070c2017-07-24 08:40:01 -0700894 ScopedCBB cbb;
895 CBB body;
David Benjaminc895d6b2016-08-11 13:26:41 -0400896
Robert Sloane56da3e2017-06-26 08:26:42 -0700897 if (hs->cert_request) {
898 CBB cert_types, sigalgs_cbb;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700899 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700900 SSL3_MT_CERTIFICATE_REQUEST) ||
901 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
902 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700903 (ssl_protocol_version(ssl) >= TLS1_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -0700904 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700905 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -0700906 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
907 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
908 !ssl_add_client_CA_list(ssl, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700909 !ssl_add_message_cbb(ssl, cbb.get())) {
910 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700911 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400912 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400913 }
914
Robert Sloanb6d070c2017-07-24 08:40:01 -0700915 if (!ssl->method->init_message(ssl, cbb.get(), &body,
916 SSL3_MT_SERVER_HELLO_DONE) ||
917 !ssl_add_message_cbb(ssl, cbb.get())) {
918 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700919 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400920 }
921
Robert Sloandc2f6092018-04-10 10:22:33 -0700922 hs->state = state12_read_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700923 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800924}
925
Robert Sloana27a6a42017-09-05 08:39:28 -0700926static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500927 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700928
Robert Sloandc2f6092018-04-10 10:22:33 -0700929 if (ssl->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
930 return ssl_hs_handback;
931 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700932 if (!hs->cert_request) {
Robert Sloandc2f6092018-04-10 10:22:33 -0700933 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700934 return ssl_hs_ok;
935 }
David Benjamin6e899c72016-06-09 18:02:18 -0400936
Robert Sloan84377092017-08-14 09:33:19 -0700937 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700938 if (!ssl->method->get_message(ssl, &msg)) {
939 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -0400940 }
941
Robert Sloan84377092017-08-14 09:33:19 -0700942 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin6e899c72016-06-09 18:02:18 -0400943 if (ssl->version == SSL3_VERSION &&
Robert Sloan84377092017-08-14 09:33:19 -0700944 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700945 // In SSL 3.0, the Certificate message is omitted to signal no
946 // certificate.
David Benjaminc895d6b2016-08-11 13:26:41 -0400947 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -0400948 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700949 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700950 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400951 }
952
Robert Sloana27a6a42017-09-05 08:39:28 -0700953 // OpenSSL returns X509_V_OK when no certificates are received. This is
954 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800955 hs->new_session->verify_result = X509_V_OK;
Robert Sloandc2f6092018-04-10 10:22:33 -0700956 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -0700957 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -0400958 }
959
David Benjamin6e899c72016-06-09 18:02:18 -0400960 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700961 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700962 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400963 }
964
Robert Sloan84377092017-08-14 09:33:19 -0700965 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700966 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800967 }
968
Robert Sloan84377092017-08-14 09:33:19 -0700969 CBS certificate_msg = msg.body;
Robert Sloan5d625782017-02-13 09:55:39 -0800970 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700971 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
972 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
973 ssl->retain_only_sha256_of_client_certs
974 ? hs->new_session->peer_sha256
975 : NULL,
976 &certificate_msg, ssl->ctx->pool)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700977 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700978 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400979 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700980 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
981 hs->new_session->certs = chain.release();
David Benjamin6e899c72016-06-09 18:02:18 -0400982
Steven Valdeze7531f02016-12-14 13:29:57 -0500983 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700984 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin6e899c72016-06-09 18:02:18 -0400985 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700986 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700987 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400988 }
989
Robert Sloana94fe052017-02-21 08:49:28 -0800990 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700991 // No client certificate so the handshake buffer may be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700992 hs->transcript.FreeBuffer();
David Benjamin6e899c72016-06-09 18:02:18 -0400993
Robert Sloana27a6a42017-09-05 08:39:28 -0700994 // In SSL 3.0, sending no certificate is signaled by omitting the
995 // Certificate message.
David Benjamin6e899c72016-06-09 18:02:18 -0400996 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -0400997 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700998 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700999 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001000 }
1001
1002 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001003 // Fail for TLS only if we required a certificate
David Benjamin6e899c72016-06-09 18:02:18 -04001004 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001005 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001006 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001007 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001008
Robert Sloana27a6a42017-09-05 08:39:28 -07001009 // OpenSSL returns X509_V_OK when no certificates are received. This is
1010 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -08001011 hs->new_session->verify_result = X509_V_OK;
Robert Sloan84377092017-08-14 09:33:19 -07001012 } else if (ssl->retain_only_sha256_of_client_certs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001013 // The hash will have been filled in.
Robert Sloana94fe052017-02-21 08:49:28 -08001014 hs->new_session->peer_sha256_valid = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001015 }
David Benjamin6e899c72016-06-09 18:02:18 -04001016
Robert Sloan84377092017-08-14 09:33:19 -07001017 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001018 hs->state = state12_verify_client_certificate;
Robert Sloana27a6a42017-09-05 08:39:28 -07001019 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001020}
1021
Robert Sloana27a6a42017-09-05 08:39:28 -07001022static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
1023 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
1024 switch (ssl_verify_peer_cert(hs)) {
1025 case ssl_verify_ok:
1026 break;
1027 case ssl_verify_invalid:
1028 return ssl_hs_error;
1029 case ssl_verify_retry:
1030 return ssl_hs_certificate_verify;
1031 }
1032 }
1033
Robert Sloandc2f6092018-04-10 10:22:33 -07001034 hs->state = state12_read_client_key_exchange;
Robert Sloana27a6a42017-09-05 08:39:28 -07001035 return ssl_hs_ok;
1036}
1037
1038static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001039 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -07001040 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001041 if (!ssl->method->get_message(ssl, &msg)) {
1042 return ssl_hs_read_message;
Robert Sloane56da3e2017-06-26 08:26:42 -07001043 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001044
Robert Sloan84377092017-08-14 09:33:19 -07001045 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001046 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001047 }
1048
Robert Sloan84377092017-08-14 09:33:19 -07001049 CBS client_key_exchange = msg.body;
Robert Sloane56da3e2017-06-26 08:26:42 -07001050 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1051 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001052
Robert Sloana27a6a42017-09-05 08:39:28 -07001053 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001054 if (alg_a & SSL_aPSK) {
1055 CBS psk_identity;
1056
Robert Sloana27a6a42017-09-05 08:39:28 -07001057 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1058 // then this is the only field in the message.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001059 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1060 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001061 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001062 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001063 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001064 }
1065
1066 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1067 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001068 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001069 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001070 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001071 }
1072
Robert Sloana94fe052017-02-21 08:49:28 -08001073 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001074 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001075 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001076 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001077 }
1078 }
1079
Robert Sloan4562e9d2017-10-02 10:26:51 -07001080 // Depending on the key exchange method, compute |premaster_secret|.
1081 Array<uint8_t> premaster_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001082 if (alg_k & SSL_kRSA) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001083 CBS encrypted_premaster_secret;
1084 if (ssl->version > SSL3_VERSION) {
1085 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1086 &encrypted_premaster_secret) ||
1087 CBS_len(&client_key_exchange) != 0) {
1088 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001089 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001090 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001091 }
1092 } else {
1093 encrypted_premaster_secret = client_key_exchange;
1094 }
1095
Robert Sloana27a6a42017-09-05 08:39:28 -07001096 // Allocate a buffer large enough for an RSA decryption.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001097 Array<uint8_t> decrypt_buf;
1098 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1099 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001100 }
1101
Robert Sloana27a6a42017-09-05 08:39:28 -07001102 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1103 // timing-sensitive code below.
Adam Langley4139edb2016-01-13 15:00:54 -08001104 size_t decrypt_len;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001105 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1106 decrypt_buf.size(),
Robert Sloan921ef2c2017-10-17 09:02:20 -07001107 encrypted_premaster_secret)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001108 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001109 break;
1110 case ssl_private_key_failure:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001111 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001112 case ssl_private_key_retry:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001113 return ssl_hs_private_key_operation;
Kenny Roote99801b2015-11-06 15:31:15 -08001114 }
1115
Robert Sloan4562e9d2017-10-02 10:26:51 -07001116 if (decrypt_len != decrypt_buf.size()) {
David Benjamin4969cc92016-04-22 15:02:23 -04001117 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001118 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001119 return ssl_hs_error;
David Benjamin4969cc92016-04-22 15:02:23 -04001120 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001121
Robert Sloana27a6a42017-09-05 08:39:28 -07001122 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1123 // section 7.4.7.1.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001124 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1125 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1126 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001127 }
1128
Robert Sloana27a6a42017-09-05 08:39:28 -07001129 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1130 // publicly invalid.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001131 if (decrypt_len < 11 + premaster_secret.size()) {
Adam Langley4139edb2016-01-13 15:00:54 -08001132 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001133 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001134 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001135 }
1136
Robert Sloana27a6a42017-09-05 08:39:28 -07001137 // Check the padding. See RFC 3447, section 7.2.2.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001138 size_t padding_len = decrypt_len - premaster_secret.size();
Adam Langley4139edb2016-01-13 15:00:54 -08001139 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1140 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001141 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001142 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1143 }
1144 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1145
Robert Sloana27a6a42017-09-05 08:39:28 -07001146 // The premaster secret must begin with |client_version|. This too must be
1147 // checked in constant time (http://eprint.iacr.org/2003/052/).
Adam Langley4139edb2016-01-13 15:00:54 -08001148 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001149 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001150 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001151 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001152
Robert Sloana27a6a42017-09-05 08:39:28 -07001153 // Select, in constant time, either the decrypted premaster or the random
1154 // premaster based on |good|.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001155 for (size_t i = 0; i < premaster_secret.size(); i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001156 premaster_secret[i] = constant_time_select_8(
1157 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1158 }
Robert Sloan572a4e22017-04-17 10:52:19 -07001159 } else if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001160 // Parse the ClientKeyExchange.
Adam Langley4139edb2016-01-13 15:00:54 -08001161 CBS peer_key;
Robert Sloan572a4e22017-04-17 10:52:19 -07001162 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001163 CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001164 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001165 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001166 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001167 }
1168
Robert Sloana27a6a42017-09-05 08:39:28 -07001169 // Compute the premaster.
Robert Sloan5d625782017-02-13 09:55:39 -08001170 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001171 if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001172 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001173 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001174 }
1175
Robert Sloana27a6a42017-09-05 08:39:28 -07001176 // The key exchange state may now be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001177 hs->key_share.reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001178 } else if (!(alg_k & SSL_kPSK)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001179 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001180 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001181 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001182 }
1183
Robert Sloana27a6a42017-09-05 08:39:28 -07001184 // For a PSK cipher suite, the actual pre-master secret is combined with the
1185 // pre-shared key.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001186 if (alg_a & SSL_aPSK) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001187 if (ssl->psk_server_callback == NULL) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001188 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001189 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001190 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001191 }
1192
Robert Sloana27a6a42017-09-05 08:39:28 -07001193 // Look up the key for the identity.
Robert Sloane56da3e2017-06-26 08:26:42 -07001194 uint8_t psk[PSK_MAX_PSK_LEN];
1195 unsigned psk_len = ssl->psk_server_callback(
1196 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1197 if (psk_len > PSK_MAX_PSK_LEN) {
1198 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001199 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001200 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001201 } else if (psk_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001202 // PSK related to the given identity not found.
Robert Sloane56da3e2017-06-26 08:26:42 -07001203 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001204 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001205 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001206 }
1207
1208 if (alg_k & SSL_kPSK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001209 // In plain PSK, other_secret is a block of 0s with the same length as the
1210 // pre-shared key.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001211 if (!premaster_secret.Init(psk_len)) {
1212 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001213 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07001214 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
Robert Sloane56da3e2017-06-26 08:26:42 -07001215 }
1216
Robert Sloanb6d070c2017-07-24 08:40:01 -07001217 ScopedCBB new_premaster;
1218 CBB child;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001219 if (!CBB_init(new_premaster.get(),
Robert Sloan4562e9d2017-10-02 10:26:51 -07001220 2 + psk_len + 2 + premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001221 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07001222 !CBB_add_bytes(&child, premaster_secret.data(),
1223 premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001224 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001225 !CBB_add_bytes(&child, psk, psk_len) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001226 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001227 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001228 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001229 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001230 }
1231
Robert Sloan84377092017-08-14 09:33:19 -07001232 if (!ssl_hash_message(hs, msg)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001233 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001234 }
1235
Robert Sloana27a6a42017-09-05 08:39:28 -07001236 // Compute the master secret.
Robert Sloana94fe052017-02-21 08:49:28 -08001237 hs->new_session->master_key_length = tls1_generate_master_secret(
Robert Sloanb1b54b82017-11-06 13:50:02 -08001238 hs, hs->new_session->master_key, premaster_secret);
Robert Sloana94fe052017-02-21 08:49:28 -08001239 if (hs->new_session->master_key_length == 0) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001240 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001241 }
Robert Sloana94fe052017-02-21 08:49:28 -08001242 hs->new_session->extended_master_secret = hs->extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001243
Robert Sloan84377092017-08-14 09:33:19 -07001244 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001245 hs->state = state12_read_client_certificate_verify;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001246 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001247}
1248
Robert Sloana27a6a42017-09-05 08:39:28 -07001249static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001250 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001251
Robert Sloana27a6a42017-09-05 08:39:28 -07001252 // Only RSA and ECDSA client certificates are supported, so a
1253 // CertificateVerify is required if and only if there's a client certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001254 if (!hs->peer_pubkey) {
1255 hs->transcript.FreeBuffer();
Robert Sloandc2f6092018-04-10 10:22:33 -07001256 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001257 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001258 }
1259
Robert Sloan84377092017-08-14 09:33:19 -07001260 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001261 if (!ssl->method->get_message(ssl, &msg)) {
1262 return ssl_hs_read_message;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001263 }
1264
Robert Sloan84377092017-08-14 09:33:19 -07001265 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001266 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001267 }
1268
Robert Sloan84377092017-08-14 09:33:19 -07001269 CBS certificate_verify = msg.body, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001270
Robert Sloana27a6a42017-09-05 08:39:28 -07001271 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001272 uint16_t signature_algorithm = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001273 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001274 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001275 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001276 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001277 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001278 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001279 uint8_t alert = SSL_AD_DECODE_ERROR;
1280 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001281 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001282 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001283 }
Robert Sloana94fe052017-02-21 08:49:28 -08001284 hs->new_session->peer_signature_algorithm = signature_algorithm;
Robert Sloan2424d842017-05-01 07:46:28 -07001285 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001286 hs->peer_pubkey.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001287 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001288 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001289 return ssl_hs_error;
Adam Langleye9ada862015-05-11 17:20:37 -07001290 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001291
Robert Sloana27a6a42017-09-05 08:39:28 -07001292 // Parse and verify the signature.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001293 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1294 CBS_len(&certificate_verify) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001295 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001296 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001297 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001298 }
1299
Robert Sloan921ef2c2017-10-17 09:02:20 -07001300 bool sig_ok;
Robert Sloana27a6a42017-09-05 08:39:28 -07001301 // The SSL3 construction for CertificateVerify does not decompose into a
1302 // single final digest and signature, and must be special-cased.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001303 if (ssl_protocol_version(ssl) == SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001304 uint8_t digest[EVP_MAX_MD_SIZE];
1305 size_t digest_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001306 if (!hs->transcript.GetSSL3CertVerifyHash(
1307 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001308 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001309 }
1310
Robert Sloanb6d070c2017-07-24 08:40:01 -07001311 UniquePtr<EVP_PKEY_CTX> pctx(
1312 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
1313 sig_ok = pctx &&
1314 EVP_PKEY_verify_init(pctx.get()) &&
1315 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1316 CBS_len(&signature), digest, digest_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001317 } else {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001318 sig_ok =
1319 ssl_public_key_verify(ssl, signature, signature_algorithm,
1320 hs->peer_pubkey.get(), hs->transcript.buffer());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001321 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001322
David Benjamin4969cc92016-04-22 15:02:23 -04001323#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -07001324 sig_ok = true;
David Benjamin4969cc92016-04-22 15:02:23 -04001325 ERR_clear_error();
1326#endif
1327 if (!sig_ok) {
Kenny Rootb8494592015-09-25 02:29:14 +00001328 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001329 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001330 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001331 }
1332
Robert Sloana27a6a42017-09-05 08:39:28 -07001333 // The handshake buffer is no longer necessary, and we may hash the current
1334 // message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001335 hs->transcript.FreeBuffer();
Robert Sloan84377092017-08-14 09:33:19 -07001336 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001337 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001338 }
1339
Robert Sloan84377092017-08-14 09:33:19 -07001340 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001341 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001342 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001343}
1344
Robert Sloana27a6a42017-09-05 08:39:28 -07001345static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001346 hs->state = state12_process_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001347 return ssl_hs_read_change_cipher_spec;
1348}
1349
1350static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001351 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001352 return ssl_hs_error;
1353 }
1354
Robert Sloandc2f6092018-04-10 10:22:33 -07001355 hs->state = state12_read_next_proto;
Robert Sloana27a6a42017-09-05 08:39:28 -07001356 return ssl_hs_ok;
1357}
1358
1359static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001360 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001361
1362 if (!hs->next_proto_neg_seen) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001363 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001364 return ssl_hs_ok;
1365 }
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;
David Benjaminc895d6b2016-08-11 13:26:41 -04001370 }
1371
Robert Sloan84377092017-08-14 09:33:19 -07001372 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1373 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001374 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001375 }
1376
Robert Sloan84377092017-08-14 09:33:19 -07001377 CBS next_protocol = msg.body, selected_protocol, padding;
David Benjamin6e899c72016-06-09 18:02:18 -04001378 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1379 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001380 CBS_len(&next_protocol) != 0) {
1381 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001382 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001383 return ssl_hs_error;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001384 }
1385
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001386 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001387 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001388 }
1389
Robert Sloan84377092017-08-14 09:33:19 -07001390 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001391 hs->state = state12_read_channel_id;
Robert Sloana27a6a42017-09-05 08:39:28 -07001392 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001393}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001394
Robert Sloana27a6a42017-09-05 08:39:28 -07001395static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001396 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001397
1398 if (!ssl->s3->tlsext_channel_id_valid) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001399 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001400 return ssl_hs_ok;
1401 }
1402
Robert Sloan84377092017-08-14 09:33:19 -07001403 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001404 if (!ssl->method->get_message(ssl, &msg)) {
1405 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001406 }
1407
Robert Sloan84377092017-08-14 09:33:19 -07001408 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1409 !tls1_verify_channel_id(hs, msg) ||
1410 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001411 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001412 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001413
Robert Sloan84377092017-08-14 09:33:19 -07001414 ssl->method->next_message(ssl);
Robert Sloandc2f6092018-04-10 10:22:33 -07001415 hs->state = state12_read_client_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001416 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001417}
1418
Robert Sloana27a6a42017-09-05 08:39:28 -07001419static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1420 SSL *const ssl = hs->ssl;
1421 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1422 if (wait != ssl_hs_ok) {
1423 return wait;
1424 }
1425
1426 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001427 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001428 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001429 hs->state = state12_send_server_finished;
Robert Sloana27a6a42017-09-05 08:39:28 -07001430 }
1431
1432 // If this is a full handshake with ChannelID then record the handshake
1433 // hashes in |hs->new_session| in case we need them to verify a
1434 // ChannelID signature on a resumption of this session in the future.
1435 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
1436 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1437 return ssl_hs_error;
1438 }
1439
1440 return ssl_hs_ok;
1441}
1442
1443static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001444 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001445
1446 if (hs->ticket_expected) {
1447 const SSL_SESSION *session;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001448 UniquePtr<SSL_SESSION> session_copy;
Robert Sloane56da3e2017-06-26 08:26:42 -07001449 if (ssl->session == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001450 // Fix the timeout to measure from the ticket issuance time.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001451 ssl_session_rebase_time(ssl, hs->new_session.get());
1452 session = hs->new_session.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001453 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001454 // We are renewing an existing session. Duplicate the session to adjust
1455 // the timeout.
Robert Sloane56da3e2017-06-26 08:26:42 -07001456 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001457 if (!session_copy) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001458 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001459 }
1460
Robert Sloanb6d070c2017-07-24 08:40:01 -07001461 ssl_session_rebase_time(ssl, session_copy.get());
1462 session = session_copy.get();
Steven Valdez909b19f2016-11-21 15:35:44 -05001463 }
1464
Robert Sloanb6d070c2017-07-24 08:40:01 -07001465 ScopedCBB cbb;
1466 CBB body, ticket;
1467 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1468 SSL3_MT_NEW_SESSION_TICKET) ||
1469 !CBB_add_u32(&body, session->timeout) ||
1470 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1471 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1472 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001473 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001474 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001475 }
1476
Robert Sloane56da3e2017-06-26 08:26:42 -07001477 if (!ssl->method->add_change_cipher_spec(ssl) ||
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001478 !tls1_change_cipher_state(hs, evp_aead_seal) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001479 !ssl_send_finished(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001480 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001481 }
Adam Langleye9ada862015-05-11 17:20:37 -07001482
Robert Sloana27a6a42017-09-05 08:39:28 -07001483 if (ssl->session != NULL) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001484 hs->state = state12_read_change_cipher_spec;
Robert Sloana27a6a42017-09-05 08:39:28 -07001485 } else {
Robert Sloandc2f6092018-04-10 10:22:33 -07001486 hs->state = state12_finish_server_handshake;
Robert Sloana27a6a42017-09-05 08:39:28 -07001487 }
1488 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001489}
Robert Sloanb6d070c2017-07-24 08:40:01 -07001490
Robert Sloana27a6a42017-09-05 08:39:28 -07001491static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1492 SSL *const ssl = hs->ssl;
1493
Robert Sloandc2f6092018-04-10 10:22:33 -07001494 if (ssl->handback) {
1495 return ssl_hs_handback;
1496 }
1497
Robert Sloana27a6a42017-09-05 08:39:28 -07001498 ssl->method->on_handshake_complete(ssl);
1499
1500 // If we aren't retaining peer certificates then we can discard it now.
1501 if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
1502 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1503 hs->new_session->certs = NULL;
1504 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1505 }
1506
Robert Sloana27a6a42017-09-05 08:39:28 -07001507 if (ssl->session != NULL) {
1508 SSL_SESSION_up_ref(ssl->session);
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001509 ssl->s3->established_session.reset(ssl->session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001510 } else {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001511 ssl->s3->established_session = std::move(hs->new_session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001512 ssl->s3->established_session->not_resumable = 0;
1513 }
1514
1515 hs->handshake_finalized = true;
1516 ssl->s3->initial_handshake_complete = true;
1517 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1518
Robert Sloandc2f6092018-04-10 10:22:33 -07001519 hs->state = state12_done;
Robert Sloana27a6a42017-09-05 08:39:28 -07001520 return ssl_hs_ok;
1521}
1522
1523enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001524 while (hs->state != state12_done) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001525 enum ssl_hs_wait_t ret = ssl_hs_error;
Robert Sloandc2f6092018-04-10 10:22:33 -07001526 enum tls12_server_hs_state_t state =
1527 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001528 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001529 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001530 ret = do_start_accept(hs);
1531 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001532 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001533 ret = do_read_client_hello(hs);
1534 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001535 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001536 ret = do_select_certificate(hs);
1537 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001538 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001539 ret = do_tls13(hs);
1540 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001541 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001542 ret = do_select_parameters(hs);
1543 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001544 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001545 ret = do_send_server_hello(hs);
1546 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001547 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001548 ret = do_send_server_certificate(hs);
1549 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001550 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001551 ret = do_send_server_key_exchange(hs);
1552 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001553 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001554 ret = do_send_server_hello_done(hs);
1555 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001556 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001557 ret = do_read_client_certificate(hs);
1558 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001559 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001560 ret = do_verify_client_certificate(hs);
1561 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001562 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001563 ret = do_read_client_key_exchange(hs);
1564 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001565 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001566 ret = do_read_client_certificate_verify(hs);
1567 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001568 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001569 ret = do_read_change_cipher_spec(hs);
1570 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001571 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001572 ret = do_process_change_cipher_spec(hs);
1573 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001574 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001575 ret = do_read_next_proto(hs);
1576 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001577 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001578 ret = do_read_channel_id(hs);
1579 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001580 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001581 ret = do_read_client_finished(hs);
1582 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001583 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001584 ret = do_send_server_finished(hs);
1585 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001586 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001587 ret = do_finish_server_handshake(hs);
1588 break;
Robert Sloandc2f6092018-04-10 10:22:33 -07001589 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001590 ret = ssl_hs_ok;
1591 break;
1592 }
1593
1594 if (hs->state != state) {
1595 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1596 }
1597
1598 if (ret != ssl_hs_ok) {
1599 return ret;
1600 }
1601 }
1602
1603 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1604 return ssl_hs_ok;
1605}
1606
1607const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001608 enum tls12_server_hs_state_t state =
1609 static_cast<enum tls12_server_hs_state_t>(hs->state);
Robert Sloana27a6a42017-09-05 08:39:28 -07001610 switch (state) {
Robert Sloandc2f6092018-04-10 10:22:33 -07001611 case state12_start_accept:
Robert Sloana27a6a42017-09-05 08:39:28 -07001612 return "TLS server start_accept";
Robert Sloandc2f6092018-04-10 10:22:33 -07001613 case state12_read_client_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001614 return "TLS server read_client_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001615 case state12_select_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001616 return "TLS server select_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001617 case state12_tls13:
Robert Sloana27a6a42017-09-05 08:39:28 -07001618 return tls13_server_handshake_state(hs);
Robert Sloandc2f6092018-04-10 10:22:33 -07001619 case state12_select_parameters:
Robert Sloana27a6a42017-09-05 08:39:28 -07001620 return "TLS server select_parameters";
Robert Sloandc2f6092018-04-10 10:22:33 -07001621 case state12_send_server_hello:
Robert Sloana27a6a42017-09-05 08:39:28 -07001622 return "TLS server send_server_hello";
Robert Sloandc2f6092018-04-10 10:22:33 -07001623 case state12_send_server_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001624 return "TLS server send_server_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001625 case state12_send_server_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001626 return "TLS server send_server_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001627 case state12_send_server_hello_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001628 return "TLS server send_server_hello_done";
Robert Sloandc2f6092018-04-10 10:22:33 -07001629 case state12_read_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001630 return "TLS server read_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001631 case state12_verify_client_certificate:
Robert Sloana27a6a42017-09-05 08:39:28 -07001632 return "TLS server verify_client_certificate";
Robert Sloandc2f6092018-04-10 10:22:33 -07001633 case state12_read_client_key_exchange:
Robert Sloana27a6a42017-09-05 08:39:28 -07001634 return "TLS server read_client_key_exchange";
Robert Sloandc2f6092018-04-10 10:22:33 -07001635 case state12_read_client_certificate_verify:
Robert Sloana27a6a42017-09-05 08:39:28 -07001636 return "TLS server read_client_certificate_verify";
Robert Sloandc2f6092018-04-10 10:22:33 -07001637 case state12_read_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001638 return "TLS server read_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001639 case state12_process_change_cipher_spec:
Robert Sloana27a6a42017-09-05 08:39:28 -07001640 return "TLS server process_change_cipher_spec";
Robert Sloandc2f6092018-04-10 10:22:33 -07001641 case state12_read_next_proto:
Robert Sloana27a6a42017-09-05 08:39:28 -07001642 return "TLS server read_next_proto";
Robert Sloandc2f6092018-04-10 10:22:33 -07001643 case state12_read_channel_id:
Robert Sloana27a6a42017-09-05 08:39:28 -07001644 return "TLS server read_channel_id";
Robert Sloandc2f6092018-04-10 10:22:33 -07001645 case state12_read_client_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001646 return "TLS server read_client_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001647 case state12_send_server_finished:
Robert Sloana27a6a42017-09-05 08:39:28 -07001648 return "TLS server send_server_finished";
Robert Sloandc2f6092018-04-10 10:22:33 -07001649 case state12_finish_server_handshake:
Robert Sloana27a6a42017-09-05 08:39:28 -07001650 return "TLS server finish_server_handshake";
Robert Sloandc2f6092018-04-10 10:22:33 -07001651 case state12_done:
Robert Sloana27a6a42017-09-05 08:39:28 -07001652 return "TLS server done";
1653 }
1654
1655 return "TLS server unknown";
1656}
1657
1658}