blob: bcbd7e2ec0d5de0894e8e9e042711a2a28ba7acd [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
Robert Sloana27a6a42017-09-05 08:39:28 -0700175enum ssl_server_hs_state_t {
176 state_start_accept = 0,
177 state_read_client_hello,
178 state_select_certificate,
179 state_tls13,
180 state_select_parameters,
181 state_send_server_hello,
182 state_send_server_certificate,
183 state_send_server_key_exchange,
184 state_send_server_hello_done,
185 state_read_client_certificate,
186 state_verify_client_certificate,
187 state_read_client_key_exchange,
188 state_read_client_certificate_verify,
189 state_read_change_cipher_spec,
190 state_process_change_cipher_spec,
191 state_read_next_proto,
192 state_read_channel_id,
193 state_read_client_finished,
194 state_send_server_finished,
195 state_finish_server_handshake,
196 state_done,
197};
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198
David Benjamin1b249672016-12-06 18:25:50 -0500199int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
200 uint16_t id) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400201 CBS cipher_suites;
202 CBS_init(&cipher_suites, client_hello->cipher_suites,
203 client_hello->cipher_suites_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204
David Benjaminc895d6b2016-08-11 13:26:41 -0400205 while (CBS_len(&cipher_suites) > 0) {
206 uint16_t got_id;
207 if (!CBS_get_u16(&cipher_suites, &got_id)) {
208 return 0;
209 }
210
211 if (got_id == id) {
212 return 1;
213 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800214 }
215
David Benjaminc895d6b2016-08-11 13:26:41 -0400216 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217}
218
David Benjamin1b249672016-12-06 18:25:50 -0500219static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
220 const SSL_CLIENT_HELLO *client_hello) {
221 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800222 assert(!ssl->s3->have_version);
Robert Sloanf6200e72017-07-10 08:09:18 -0700223 CBS supported_versions, versions;
David Benjamin1b249672016-12-06 18:25:50 -0500224 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
225 TLSEXT_TYPE_supported_versions)) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400226 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
227 CBS_len(&supported_versions) != 0 ||
228 CBS_len(&versions) == 0) {
229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
230 *out_alert = SSL_AD_DECODE_ERROR;
231 return 0;
232 }
David Benjamin7c0d06c2016-08-11 13:26:41 -0400233 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -0700234 // Convert the ClientHello version to an equivalent supported_versions
235 // extension.
Robert Sloanf6200e72017-07-10 08:09:18 -0700236 static const uint8_t kTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700237 0x03, 0x03, // TLS 1.2
238 0x03, 0x02, // TLS 1.1
239 0x03, 0x01, // TLS 1
240 0x03, 0x00, // SSL 3
Robert Sloanf6200e72017-07-10 08:09:18 -0700241 };
242
243 static const uint8_t kDTLSVersions[] = {
Robert Sloana27a6a42017-09-05 08:39:28 -0700244 0xfe, 0xfd, // DTLS 1.2
245 0xfe, 0xff, // DTLS 1.0
Robert Sloanf6200e72017-07-10 08:09:18 -0700246 };
247
248 size_t versions_len = 0;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400249 if (SSL_is_dtls(ssl)) {
250 if (client_hello->version <= DTLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700251 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400252 } else if (client_hello->version <= DTLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700253 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400254 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700255 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
256 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400257 } else {
258 if (client_hello->version >= TLS1_2_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700259 versions_len = 8;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400260 } else if (client_hello->version >= TLS1_1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700261 versions_len = 6;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400262 } else if (client_hello->version >= TLS1_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700263 versions_len = 4;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400264 } else if (client_hello->version >= SSL3_VERSION) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700265 versions_len = 2;
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400266 }
Robert Sloanf6200e72017-07-10 08:09:18 -0700267 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
268 versions_len);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400269 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270 }
271
Robert Sloanf6200e72017-07-10 08:09:18 -0700272 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400273 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274 }
275
Robert Sloana27a6a42017-09-05 08:39:28 -0700276 // At this point, the connection's version is known and |ssl->version| is
277 // fixed. Begin enforcing the record-layer version.
278 ssl->s3->have_version = true;
Robert Sloandb4251a2017-09-18 09:38:15 -0700279 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280
Robert Sloana27a6a42017-09-05 08:39:28 -0700281 // Handle FALLBACK_SCSV.
Robert Sloanf6200e72017-07-10 08:09:18 -0700282 if (ssl_client_cipher_list_contains_cipher(client_hello,
283 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Robert Sloan921ef2c2017-10-17 09:02:20 -0700284 ssl_protocol_version(ssl) < hs->max_version) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700285 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
286 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
287 return 0;
288 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400289
Robert Sloanf6200e72017-07-10 08:09:18 -0700290 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291}
292
Robert Sloanfe7cd212017-08-07 09:03:39 -0700293static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
294 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500295 CBS cipher_suites;
296 CBS_init(&cipher_suites, client_hello->cipher_suites,
297 client_hello->cipher_suites_len);
298
Robert Sloanfe7cd212017-08-07 09:03:39 -0700299 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
300 if (!sk) {
David Benjamin1b249672016-12-06 18:25:50 -0500301 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700302 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500303 }
304
305 while (CBS_len(&cipher_suites) > 0) {
306 uint16_t cipher_suite;
307
308 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
309 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700310 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500311 }
312
313 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700314 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500315 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700316 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500317 }
318 }
319
320 return sk;
David Benjamin1b249672016-12-06 18:25:50 -0500321}
322
Robert Sloana27a6a42017-09-05 08:39:28 -0700323// ssl_get_compatible_server_ciphers determines the key exchange and
324// authentication cipher suite masks compatible with the server configuration
325// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
326// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin1b249672016-12-06 18:25:50 -0500327static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
328 uint32_t *out_mask_k,
329 uint32_t *out_mask_a) {
330 SSL *const ssl = hs->ssl;
David Benjamin1b249672016-12-06 18:25:50 -0500331 uint32_t mask_k = 0;
332 uint32_t mask_a = 0;
333
Robert Sloan4d1ac502017-02-06 08:36:14 -0800334 if (ssl_has_certificate(ssl)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700335 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
336 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin1b249672016-12-06 18:25:50 -0500337 mask_k |= SSL_kRSA;
David Benjamin1b249672016-12-06 18:25:50 -0500338 }
339 }
340
Robert Sloana27a6a42017-09-05 08:39:28 -0700341 // Check for a shared group to consider ECDHE ciphers.
David Benjamin1b249672016-12-06 18:25:50 -0500342 uint16_t unused;
343 if (tls1_get_shared_group(hs, &unused)) {
344 mask_k |= SSL_kECDHE;
345 }
346
Robert Sloana27a6a42017-09-05 08:39:28 -0700347 // PSK requires a server callback.
David Benjamin1b249672016-12-06 18:25:50 -0500348 if (ssl->psk_server_callback != NULL) {
349 mask_k |= SSL_kPSK;
350 mask_a |= SSL_aPSK;
351 }
352
353 *out_mask_k = mask_k;
354 *out_mask_a = mask_a;
355}
356
357static const SSL_CIPHER *ssl3_choose_cipher(
358 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
359 const struct ssl_cipher_preference_list_st *server_pref) {
360 SSL *const ssl = hs->ssl;
Robert Sloan6f79a502017-04-03 09:16:40 -0700361 STACK_OF(SSL_CIPHER) *prio, *allow;
Robert Sloana27a6a42017-09-05 08:39:28 -0700362 // in_group_flags will either be NULL, or will point to an array of bytes
363 // which indicate equal-preference groups in the |prio| stack. See the
364 // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
365 // struct.
David Benjamin1b249672016-12-06 18:25:50 -0500366 const uint8_t *in_group_flags;
Robert Sloana27a6a42017-09-05 08:39:28 -0700367 // group_min contains the minimal index so far found in a group, or -1 if no
368 // such value exists yet.
David Benjamin1b249672016-12-06 18:25:50 -0500369 int group_min = -1;
370
Robert Sloanfe7cd212017-08-07 09:03:39 -0700371 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
Robert Sloan6f79a502017-04-03 09:16:40 -0700372 ssl_parse_client_cipher_list(client_hello);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700373 if (!client_pref) {
374 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500375 }
376
377 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700378 prio = server_pref->ciphers;
David Benjamin1b249672016-12-06 18:25:50 -0500379 in_group_flags = server_pref->in_group_flags;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700380 allow = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500381 } else {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700382 prio = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500383 in_group_flags = NULL;
Robert Sloan6f79a502017-04-03 09:16:40 -0700384 allow = server_pref->ciphers;
David Benjamin1b249672016-12-06 18:25:50 -0500385 }
386
Robert Sloan6f79a502017-04-03 09:16:40 -0700387 uint32_t mask_k, mask_a;
David Benjamin1b249672016-12-06 18:25:50 -0500388 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
389
390 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700391 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin1b249672016-12-06 18:25:50 -0500392
Robert Sloan6f79a502017-04-03 09:16:40 -0700393 size_t cipher_index;
Robert Sloana27a6a42017-09-05 08:39:28 -0700394 if (// Check if the cipher is supported for the current version.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700395 SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
396 ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700397 // Check the cipher is supported for the server configuration.
Robert Sloan6f79a502017-04-03 09:16:40 -0700398 (c->algorithm_mkey & mask_k) &&
399 (c->algorithm_auth & mask_a) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700400 // Check the cipher is in the |allow| list.
Robert Sloan6f79a502017-04-03 09:16:40 -0700401 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500402 if (in_group_flags != NULL && in_group_flags[i] == 1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700403 // This element of |prio| is in a group. Update the minimum index found
404 // so far and continue looking.
David Benjamin1b249672016-12-06 18:25:50 -0500405 if (group_min == -1 || (size_t)group_min > cipher_index) {
406 group_min = cipher_index;
407 }
408 } else {
409 if (group_min != -1 && (size_t)group_min < cipher_index) {
410 cipher_index = group_min;
411 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700412 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin1b249672016-12-06 18:25:50 -0500413 }
414 }
415
416 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700417 // We are about to leave a group, but we found a match in it, so that's
418 // our answer.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700419 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin1b249672016-12-06 18:25:50 -0500420 }
421 }
422
Robert Sloanfe7cd212017-08-07 09:03:39 -0700423 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500424}
425
Robert Sloana27a6a42017-09-05 08:39:28 -0700426static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
427 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
428 hs->state = state_read_client_hello;
429 return ssl_hs_ok;
430}
431
432static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500433 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700434
Robert Sloan84377092017-08-14 09:33:19 -0700435 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700436 if (!ssl->method->get_message(ssl, &msg)) {
437 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700438 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700439
Robert Sloan84377092017-08-14 09:33:19 -0700440 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700441 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400442 }
443
David Benjamin1b249672016-12-06 18:25:50 -0500444 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700445 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400446 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700447 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700448 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400449 }
450
Robert Sloana27a6a42017-09-05 08:39:28 -0700451 // Run the early callback.
Robert Sloan5d625782017-02-13 09:55:39 -0800452 if (ssl->ctx->select_certificate_cb != NULL) {
453 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700454 case ssl_select_cert_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700455 return ssl_hs_certificate_selection_pending;
David Benjaminc895d6b2016-08-11 13:26:41 -0400456
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700457 case ssl_select_cert_error:
Robert Sloana27a6a42017-09-05 08:39:28 -0700458 // Connection rejected.
Robert Sloan5d625782017-02-13 09:55:39 -0800459 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700460 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700461 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400462
Robert Sloan5d625782017-02-13 09:55:39 -0800463 default:
464 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400465 }
Robert Sloan5d625782017-02-13 09:55:39 -0800466 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800467
Robert Sloana27a6a42017-09-05 08:39:28 -0700468 // Freeze the version range after the early callback.
Robert Sloane56da3e2017-06-26 08:26:42 -0700469 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700470 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700471 }
472
Robert Sloan5d625782017-02-13 09:55:39 -0800473 uint8_t alert = SSL_AD_DECODE_ERROR;
474 if (!negotiate_version(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700475 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700476 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800477 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800478
Robert Sloanf6200e72017-07-10 08:09:18 -0700479 hs->client_version = client_hello.version;
Robert Sloan5d625782017-02-13 09:55:39 -0800480 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
481 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700482 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800483 }
484 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
485 client_hello.random_len);
486
Robert Sloana27a6a42017-09-05 08:39:28 -0700487 // Only null compression is supported. TLS 1.3 further requires the peer
488 // advertise no other compression.
Robert Sloan5d625782017-02-13 09:55:39 -0800489 if (OPENSSL_memchr(client_hello.compression_methods, 0,
490 client_hello.compression_methods_len) == NULL ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700491 (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
Robert Sloan5d625782017-02-13 09:55:39 -0800492 client_hello.compression_methods_len != 1)) {
493 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700494 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700495 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800496 }
497
Robert Sloana27a6a42017-09-05 08:39:28 -0700498 // TLS extensions.
Robert Sloan5d625782017-02-13 09:55:39 -0800499 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
500 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Robert Sloana27a6a42017-09-05 08:39:28 -0700501 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800502 }
503
Robert Sloana27a6a42017-09-05 08:39:28 -0700504 hs->state = state_select_certificate;
505 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800506}
507
Robert Sloana27a6a42017-09-05 08:39:28 -0700508static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800509 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700510
Robert Sloan84377092017-08-14 09:33:19 -0700511 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700512 if (!ssl->method->get_message(ssl, &msg)) {
513 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700514 }
515
Robert Sloana27a6a42017-09-05 08:39:28 -0700516 // Call |cert_cb| to update server certificates if required.
Robert Sloan5d625782017-02-13 09:55:39 -0800517 if (ssl->cert->cert_cb != NULL) {
518 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
519 if (rv == 0) {
520 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700521 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700522 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400523 }
Robert Sloan5d625782017-02-13 09:55:39 -0800524 if (rv < 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700525 return ssl_hs_x509_lookup;
David Benjaminc895d6b2016-08-11 13:26:41 -0400526 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400527 }
528
Robert Sloan572a4e22017-04-17 10:52:19 -0700529 if (!ssl_on_certificate_selected(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700530 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500531 }
532
Robert Sloan921ef2c2017-10-17 09:02:20 -0700533 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700534 // Jump to the TLS 1.3 state machine.
535 hs->state = state_tls13;
536 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800537 }
538
539 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700540 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700541 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800542 }
543
Robert Sloana27a6a42017-09-05 08:39:28 -0700544 // Negotiate the cipher suite. This must be done after |cert_cb| so the
545 // certificate is finalized.
Robert Sloana94fe052017-02-21 08:49:28 -0800546 hs->new_cipher =
Robert Sloan5d625782017-02-13 09:55:39 -0800547 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
Robert Sloana94fe052017-02-21 08:49:28 -0800548 if (hs->new_cipher == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -0800549 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700550 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700551 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800552 }
553
Robert Sloana27a6a42017-09-05 08:39:28 -0700554 hs->state = state_select_parameters;
555 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800556}
557
Robert Sloana27a6a42017-09-05 08:39:28 -0700558static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
559 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
560 if (wait == ssl_hs_ok) {
561 hs->state = state_finish_server_handshake;
562 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800563 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500564
Robert Sloana27a6a42017-09-05 08:39:28 -0700565 return wait;
566}
567
568static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
569 SSL *const ssl = hs->ssl;
570
571 SSLMessage msg;
572 if (!ssl->method->get_message(ssl, &msg)) {
573 return ssl_hs_read_message;
574 }
575
576 SSL_CLIENT_HELLO client_hello;
577 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
578 return ssl_hs_error;
579 }
580
581 // Determine whether we are doing session resumption.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700582 UniquePtr<SSL_SESSION> session;
Robert Sloana27a6a42017-09-05 08:39:28 -0700583 bool tickets_supported = false, renew_ticket = false;
584 enum ssl_hs_wait_t wait = ssl_get_prev_session(
585 ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
586 if (wait != ssl_hs_ok) {
587 return wait;
Steven Valdez909b19f2016-11-21 15:35:44 -0500588 }
589
Robert Sloana12bf462017-07-17 07:08:26 -0700590 if (session) {
Robert Sloana94fe052017-02-21 08:49:28 -0800591 if (session->extended_master_secret && !hs->extended_master_secret) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700592 // A ClientHello without EMS that attempts to resume a session with EMS
593 // is fatal to the connection.
Steven Valdez909b19f2016-11-21 15:35:44 -0500594 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700595 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700596 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500597 }
598
Robert Sloana12bf462017-07-17 07:08:26 -0700599 if (!ssl_session_is_resumable(hs, session.get()) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700600 // If the client offers the EMS extension, but the previous session
601 // didn't use it, then negotiate a new session.
Robert Sloana94fe052017-02-21 08:49:28 -0800602 hs->extended_master_secret != session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -0700603 session.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500604 }
605 }
606
Robert Sloana12bf462017-07-17 07:08:26 -0700607 if (session) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700608 // Use the old session.
David Benjamin1b249672016-12-06 18:25:50 -0500609 hs->ticket_expected = renew_ticket;
Robert Sloana12bf462017-07-17 07:08:26 -0700610 ssl->session = session.release();
Robert Sloana27a6a42017-09-05 08:39:28 -0700611 ssl->s3->session_reused = true;
Steven Valdez909b19f2016-11-21 15:35:44 -0500612 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500613 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500614 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500615 if (!ssl_get_new_session(hs, 1 /* server */)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700616 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500617 }
618
Robert Sloana27a6a42017-09-05 08:39:28 -0700619 // Clear the session ID if we want the session to be single-use.
Steven Valdez909b19f2016-11-21 15:35:44 -0500620 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
Robert Sloana94fe052017-02-21 08:49:28 -0800621 hs->new_session->session_id_length = 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500622 }
623 }
624
625 if (ssl->ctx->dos_protection_cb != NULL &&
626 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700627 // Connection rejected for DOS reasons.
Steven Valdez909b19f2016-11-21 15:35:44 -0500628 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700629 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700630 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500631 }
632
633 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800634 hs->new_session->cipher = hs->new_cipher;
Steven Valdez909b19f2016-11-21 15:35:44 -0500635
Robert Sloana27a6a42017-09-05 08:39:28 -0700636 // Determine whether to request a client certificate.
David Benjamin1b249672016-12-06 18:25:50 -0500637 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700638 // Only request a certificate if Channel ID isn't negotiated.
Adam Langley4139edb2016-01-13 15:00:54 -0800639 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
640 ssl->s3->tlsext_channel_id_valid) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700641 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700642 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700643 // CertificateRequest may only be sent in certificate-based ciphers.
Robert Sloana94fe052017-02-21 08:49:28 -0800644 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700645 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700646 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400647
David Benjamin1b249672016-12-06 18:25:50 -0500648 if (!hs->cert_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700649 // OpenSSL returns X509_V_OK when no certificates are requested. This is
650 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800651 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400652 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800653 }
654
Robert Sloana27a6a42017-09-05 08:39:28 -0700655 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
656 // deferred. Complete it now.
Robert Sloane56da3e2017-06-26 08:26:42 -0700657 uint8_t alert = SSL_AD_DECODE_ERROR;
658 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700659 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700660 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500661 }
662
Robert Sloana27a6a42017-09-05 08:39:28 -0700663 // Now that all parameters are known, initialize the handshake hash and hash
664 // the ClientHello.
Robert Sloan921ef2c2017-10-17 09:02:20 -0700665 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
Robert Sloan84377092017-08-14 09:33:19 -0700666 !ssl_hash_message(hs, msg)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700667 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700668 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000669 }
670
Robert Sloana27a6a42017-09-05 08:39:28 -0700671 // Release the handshake buffer if client authentication isn't required.
David Benjamin1b249672016-12-06 18:25:50 -0500672 if (!hs->cert_request) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700673 hs->transcript.FreeBuffer();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800674 }
675
Robert Sloan84377092017-08-14 09:33:19 -0700676 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700677
678 hs->state = state_send_server_hello;
679 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800680}
681
Robert Sloana27a6a42017-09-05 08:39:28 -0700682static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500683 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -0800684
Robert Sloana27a6a42017-09-05 08:39:28 -0700685 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
686 // known attack while we fix ChannelID itself.
Kenny Roote99801b2015-11-06 15:31:15 -0800687 if (ssl->s3->tlsext_channel_id_valid &&
Robert Sloana94fe052017-02-21 08:49:28 -0800688 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700689 ssl->s3->tlsext_channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800690 }
691
Robert Sloana27a6a42017-09-05 08:39:28 -0700692 // If this is a resumption and the original handshake didn't support
693 // ChannelID then we didn't record the original handshake hashes in the
694 // session and so cannot resume with ChannelIDs.
David Benjaminc895d6b2016-08-11 13:26:41 -0400695 if (ssl->session != NULL &&
696 ssl->session->original_handshake_hash_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700697 ssl->s3->tlsext_channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800698 }
699
Robert Sloan7d422bc2017-03-06 10:04:29 -0800700 struct OPENSSL_timeval now;
David Benjaminc895d6b2016-08-11 13:26:41 -0400701 ssl_get_current_time(ssl, &now);
702 ssl->s3->server_random[0] = now.tv_sec >> 24;
703 ssl->s3->server_random[1] = now.tv_sec >> 16;
704 ssl->s3->server_random[2] = now.tv_sec >> 8;
705 ssl->s3->server_random[3] = now.tv_sec;
706 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700707 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800708 }
709
Robert Sloan0da43952018-01-03 15:13:14 -0800710 // Implement the TLS 1.3 anti-downgrade feature, but with a different value.
711 //
712 // For draft TLS 1.3 versions, it is not safe to deploy this feature. However,
713 // some TLS terminators are non-compliant and copy the origin server's value,
714 // so we wish to measure eventual compatibility impact.
715 if (hs->max_version >= TLS1_3_VERSION) {
716 OPENSSL_memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE -
717 sizeof(kDraftDowngradeRandom),
718 kDraftDowngradeRandom, sizeof(kDraftDowngradeRandom));
719 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400720
Robert Sloanb6d070c2017-07-24 08:40:01 -0700721 const SSL_SESSION *session = hs->new_session.get();
David Benjaminc895d6b2016-08-11 13:26:41 -0400722 if (ssl->session != NULL) {
723 session = ssl->session;
724 }
725
Robert Sloanb6d070c2017-07-24 08:40:01 -0700726 ScopedCBB cbb;
727 CBB body, session_id;
728 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400729 !CBB_add_u16(&body, ssl->version) ||
730 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
731 !CBB_add_u8_length_prefixed(&body, &session_id) ||
732 !CBB_add_bytes(&session_id, session->session_id,
733 session->session_id_length) ||
Robert Sloana94fe052017-02-21 08:49:28 -0800734 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400735 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -0500736 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700737 !ssl_add_message_cbb(ssl, cbb.get())) {
Kenny Roote99801b2015-11-06 15:31:15 -0800738 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700739 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800740 }
741
Robert Sloana27a6a42017-09-05 08:39:28 -0700742 if (ssl->session != NULL) {
743 hs->state = state_send_server_finished;
744 } else {
745 hs->state = state_send_server_certificate;
746 }
747 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800748}
749
Robert Sloana27a6a42017-09-05 08:39:28 -0700750static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500751 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700752 ScopedCBB cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000753
Robert Sloane56da3e2017-06-26 08:26:42 -0700754 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
755 if (!ssl_has_certificate(ssl)) {
756 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloana27a6a42017-09-05 08:39:28 -0700757 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700758 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800759
Robert Sloan921ef2c2017-10-17 09:02:20 -0700760 if (!ssl_output_cert_chain(ssl)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700761 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700762 }
763
764 if (hs->certificate_status_expected) {
765 CBB body, ocsp_response;
Robert Sloana12bf462017-07-17 07:08:26 -0700766 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700767 SSL3_MT_CERTIFICATE_STATUS) ||
768 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
769 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
770 !CBB_add_bytes(&ocsp_response,
771 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
772 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700773 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700774 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700775 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700776 }
777 }
778 }
779
Robert Sloana27a6a42017-09-05 08:39:28 -0700780 // Assemble ServerKeyExchange parameters if needed.
Robert Sloane56da3e2017-06-26 08:26:42 -0700781 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
782 uint32_t alg_a = hs->new_cipher->algorithm_auth;
783 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
784 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
785
Robert Sloana27a6a42017-09-05 08:39:28 -0700786 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
787 // the client and server randoms for the signing transcript.
Robert Sloane56da3e2017-06-26 08:26:42 -0700788 CBB child;
Robert Sloana12bf462017-07-17 07:08:26 -0700789 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
790 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
791 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700792 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400793 }
794
Robert Sloana27a6a42017-09-05 08:39:28 -0700795 // PSK ciphers begin with an identity hint.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800796 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -0800797 size_t len =
798 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
Robert Sloana12bf462017-07-17 07:08:26 -0700799 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
Adam Langley4139edb2016-01-13 15:00:54 -0800800 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
801 len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700802 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800803 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800804 }
805
Robert Sloan572a4e22017-04-17 10:52:19 -0700806 if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700807 // Determine the group to use.
David Benjamind316cba2016-06-02 16:17:39 -0400808 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -0500809 if (!tls1_get_shared_group(hs, &group_id)) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700810 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700811 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700812 return ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700813 }
Robert Sloana94fe052017-02-21 08:49:28 -0800814 hs->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -0800815
Robert Sloana27a6a42017-09-05 08:39:28 -0700816 // Set up ECDH, generate a key, and emit the public half.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700817 hs->key_share = SSLKeyShare::Create(group_id);
818 if (!hs->key_share ||
Robert Sloana12bf462017-07-17 07:08:26 -0700819 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
820 !CBB_add_u16(cbb.get(), group_id) ||
821 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700822 !hs->key_share->Offer(&child)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700823 return ssl_hs_error;
David Benjamind316cba2016-06-02 16:17:39 -0400824 }
Adam Langley4139edb2016-01-13 15:00:54 -0800825 } else {
826 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800827 }
828
Robert Sloan4562e9d2017-10-02 10:26:51 -0700829 if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700830 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400831 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400832 }
833
Robert Sloana27a6a42017-09-05 08:39:28 -0700834 hs->state = state_send_server_key_exchange;
835 return ssl_hs_ok;
Robert Sloane56da3e2017-06-26 08:26:42 -0700836}
837
Robert Sloana27a6a42017-09-05 08:39:28 -0700838static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700839 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700840
Robert Sloan4562e9d2017-10-02 10:26:51 -0700841 if (hs->server_params.size() == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700842 hs->state = state_send_server_hello_done;
843 return ssl_hs_ok;
844 }
845
Robert Sloanb6d070c2017-07-24 08:40:01 -0700846 ScopedCBB cbb;
847 CBB body, child;
848 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -0400849 SSL3_MT_SERVER_KEY_EXCHANGE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700850 // |hs->server_params| contains a prefix for signing.
Robert Sloan4562e9d2017-10-02 10:26:51 -0700851 hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
852 !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
853 hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700854 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800855 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800856
Robert Sloana27a6a42017-09-05 08:39:28 -0700857 // Add a signature.
Robert Sloana94fe052017-02-21 08:49:28 -0800858 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800859 if (!ssl_has_private_key(ssl)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700860 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700861 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800862 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800863
Robert Sloana27a6a42017-09-05 08:39:28 -0700864 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -0400865 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -0500866 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700867 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400868 }
Robert Sloan921ef2c2017-10-17 09:02:20 -0700869 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400870 if (!CBB_add_u16(&body, signature_algorithm)) {
871 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700872 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700873 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400874 }
875 }
876
Robert Sloana27a6a42017-09-05 08:39:28 -0700877 // Add space for the signature.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700878 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -0400879 uint8_t *ptr;
880 if (!CBB_add_u16_length_prefixed(&body, &child) ||
881 !CBB_reserve(&child, &ptr, max_sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700882 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400883 }
884
Adam Langley4139edb2016-01-13 15:00:54 -0800885 size_t sig_len;
Robert Sloane56da3e2017-06-26 08:26:42 -0700886 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
Robert Sloan921ef2c2017-10-17 09:02:20 -0700887 signature_algorithm, hs->server_params)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800888 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -0800889 if (!CBB_did_write(&child, sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700890 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800891 }
892 break;
893 case ssl_private_key_failure:
Robert Sloana27a6a42017-09-05 08:39:28 -0700894 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800895 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700896 return ssl_hs_private_key_operation;
Adam Langley4139edb2016-01-13 15:00:54 -0800897 }
Adam Langley1e4884f2015-09-24 10:57:52 -0700898 }
899
Robert Sloanb6d070c2017-07-24 08:40:01 -0700900 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700901 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000902 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400903
Robert Sloan4562e9d2017-10-02 10:26:51 -0700904 hs->server_params.Reset();
David Benjaminc895d6b2016-08-11 13:26:41 -0400905
Robert Sloana27a6a42017-09-05 08:39:28 -0700906 hs->state = state_send_server_hello_done;
907 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800908}
909
Robert Sloana27a6a42017-09-05 08:39:28 -0700910static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500911 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700912
Robert Sloanb6d070c2017-07-24 08:40:01 -0700913 ScopedCBB cbb;
914 CBB body;
David Benjaminc895d6b2016-08-11 13:26:41 -0400915
Robert Sloane56da3e2017-06-26 08:26:42 -0700916 if (hs->cert_request) {
917 CBB cert_types, sigalgs_cbb;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700918 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700919 SSL3_MT_CERTIFICATE_REQUEST) ||
920 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
921 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700922 (ssl_protocol_version(ssl) >= TLS1_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -0700923 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -0700924 (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
Robert Sloane56da3e2017-06-26 08:26:42 -0700925 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
926 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
927 !ssl_add_client_CA_list(ssl, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700928 !ssl_add_message_cbb(ssl, cbb.get())) {
929 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700930 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400931 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400932 }
933
Robert Sloanb6d070c2017-07-24 08:40:01 -0700934 if (!ssl->method->init_message(ssl, cbb.get(), &body,
935 SSL3_MT_SERVER_HELLO_DONE) ||
936 !ssl_add_message_cbb(ssl, cbb.get())) {
937 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700938 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400939 }
940
Robert Sloana27a6a42017-09-05 08:39:28 -0700941 hs->state = state_read_client_certificate;
942 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800943}
944
Robert Sloana27a6a42017-09-05 08:39:28 -0700945static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500946 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700947
948 if (!hs->cert_request) {
949 hs->state = state_verify_client_certificate;
950 return ssl_hs_ok;
951 }
David Benjamin6e899c72016-06-09 18:02:18 -0400952
Robert Sloan84377092017-08-14 09:33:19 -0700953 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700954 if (!ssl->method->get_message(ssl, &msg)) {
955 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -0400956 }
957
Robert Sloan84377092017-08-14 09:33:19 -0700958 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin6e899c72016-06-09 18:02:18 -0400959 if (ssl->version == SSL3_VERSION &&
Robert Sloan84377092017-08-14 09:33:19 -0700960 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700961 // In SSL 3.0, the Certificate message is omitted to signal no
962 // certificate.
David Benjaminc895d6b2016-08-11 13:26:41 -0400963 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -0400964 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700965 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700966 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400967 }
968
Robert Sloana27a6a42017-09-05 08:39:28 -0700969 // OpenSSL returns X509_V_OK when no certificates are received. This is
970 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800971 hs->new_session->verify_result = X509_V_OK;
Robert Sloana27a6a42017-09-05 08:39:28 -0700972 hs->state = state_verify_client_certificate;
973 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -0400974 }
975
David Benjamin6e899c72016-06-09 18:02:18 -0400976 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700977 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700978 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400979 }
980
Robert Sloan84377092017-08-14 09:33:19 -0700981 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700982 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800983 }
984
Robert Sloan84377092017-08-14 09:33:19 -0700985 CBS certificate_msg = msg.body;
Robert Sloan5d625782017-02-13 09:55:39 -0800986 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700987 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
988 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
989 ssl->retain_only_sha256_of_client_certs
990 ? hs->new_session->peer_sha256
991 : NULL,
992 &certificate_msg, ssl->ctx->pool)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700993 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700994 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400995 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700996 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
997 hs->new_session->certs = chain.release();
David Benjamin6e899c72016-06-09 18:02:18 -0400998
Steven Valdeze7531f02016-12-14 13:29:57 -0500999 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001000 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin6e899c72016-06-09 18:02:18 -04001001 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001002 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001003 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001004 }
1005
Robert Sloana94fe052017-02-21 08:49:28 -08001006 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001007 // No client certificate so the handshake buffer may be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001008 hs->transcript.FreeBuffer();
David Benjamin6e899c72016-06-09 18:02:18 -04001009
Robert Sloana27a6a42017-09-05 08:39:28 -07001010 // In SSL 3.0, sending no certificate is signaled by omitting the
1011 // Certificate message.
David Benjamin6e899c72016-06-09 18:02:18 -04001012 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -04001013 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001014 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001015 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001016 }
1017
1018 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001019 // Fail for TLS only if we required a certificate
David Benjamin6e899c72016-06-09 18:02:18 -04001020 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001021 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001022 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001023 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001024
Robert Sloana27a6a42017-09-05 08:39:28 -07001025 // OpenSSL returns X509_V_OK when no certificates are received. This is
1026 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -08001027 hs->new_session->verify_result = X509_V_OK;
Robert Sloan84377092017-08-14 09:33:19 -07001028 } else if (ssl->retain_only_sha256_of_client_certs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001029 // The hash will have been filled in.
Robert Sloana94fe052017-02-21 08:49:28 -08001030 hs->new_session->peer_sha256_valid = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001031 }
David Benjamin6e899c72016-06-09 18:02:18 -04001032
Robert Sloan84377092017-08-14 09:33:19 -07001033 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001034 hs->state = state_verify_client_certificate;
1035 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001036}
1037
Robert Sloana27a6a42017-09-05 08:39:28 -07001038static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
1039 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
1040 switch (ssl_verify_peer_cert(hs)) {
1041 case ssl_verify_ok:
1042 break;
1043 case ssl_verify_invalid:
1044 return ssl_hs_error;
1045 case ssl_verify_retry:
1046 return ssl_hs_certificate_verify;
1047 }
1048 }
1049
1050 hs->state = state_read_client_key_exchange;
1051 return ssl_hs_ok;
1052}
1053
1054static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001055 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -07001056 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001057 if (!ssl->method->get_message(ssl, &msg)) {
1058 return ssl_hs_read_message;
Robert Sloane56da3e2017-06-26 08:26:42 -07001059 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001060
Robert Sloan84377092017-08-14 09:33:19 -07001061 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001062 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001063 }
1064
Robert Sloan84377092017-08-14 09:33:19 -07001065 CBS client_key_exchange = msg.body;
Robert Sloane56da3e2017-06-26 08:26:42 -07001066 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1067 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001068
Robert Sloana27a6a42017-09-05 08:39:28 -07001069 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001070 if (alg_a & SSL_aPSK) {
1071 CBS psk_identity;
1072
Robert Sloana27a6a42017-09-05 08:39:28 -07001073 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1074 // then this is the only field in the message.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001075 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1076 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001077 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001078 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001079 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001080 }
1081
1082 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1083 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001084 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001085 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001086 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001087 }
1088
Robert Sloana94fe052017-02-21 08:49:28 -08001089 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001090 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001091 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001092 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001093 }
1094 }
1095
Robert Sloan4562e9d2017-10-02 10:26:51 -07001096 // Depending on the key exchange method, compute |premaster_secret|.
1097 Array<uint8_t> premaster_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001098 if (alg_k & SSL_kRSA) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001099 CBS encrypted_premaster_secret;
1100 if (ssl->version > SSL3_VERSION) {
1101 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1102 &encrypted_premaster_secret) ||
1103 CBS_len(&client_key_exchange) != 0) {
1104 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001105 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001106 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001107 }
1108 } else {
1109 encrypted_premaster_secret = client_key_exchange;
1110 }
1111
Robert Sloana27a6a42017-09-05 08:39:28 -07001112 // Allocate a buffer large enough for an RSA decryption.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001113 Array<uint8_t> decrypt_buf;
1114 if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
1115 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001116 }
1117
Robert Sloana27a6a42017-09-05 08:39:28 -07001118 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1119 // timing-sensitive code below.
Adam Langley4139edb2016-01-13 15:00:54 -08001120 size_t decrypt_len;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001121 switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
1122 decrypt_buf.size(),
Robert Sloan921ef2c2017-10-17 09:02:20 -07001123 encrypted_premaster_secret)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001124 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001125 break;
1126 case ssl_private_key_failure:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001127 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -08001128 case ssl_private_key_retry:
Robert Sloan4562e9d2017-10-02 10:26:51 -07001129 return ssl_hs_private_key_operation;
Kenny Roote99801b2015-11-06 15:31:15 -08001130 }
1131
Robert Sloan4562e9d2017-10-02 10:26:51 -07001132 if (decrypt_len != decrypt_buf.size()) {
David Benjamin4969cc92016-04-22 15:02:23 -04001133 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001134 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001135 return ssl_hs_error;
David Benjamin4969cc92016-04-22 15:02:23 -04001136 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001137
Robert Sloana27a6a42017-09-05 08:39:28 -07001138 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1139 // section 7.4.7.1.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001140 if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
1141 !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
1142 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001143 }
1144
Robert Sloana27a6a42017-09-05 08:39:28 -07001145 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1146 // publicly invalid.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001147 if (decrypt_len < 11 + premaster_secret.size()) {
Adam Langley4139edb2016-01-13 15:00:54 -08001148 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001149 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001150 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -08001151 }
1152
Robert Sloana27a6a42017-09-05 08:39:28 -07001153 // Check the padding. See RFC 3447, section 7.2.2.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001154 size_t padding_len = decrypt_len - premaster_secret.size();
Adam Langley4139edb2016-01-13 15:00:54 -08001155 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1156 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001157 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001158 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1159 }
1160 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1161
Robert Sloana27a6a42017-09-05 08:39:28 -07001162 // The premaster secret must begin with |client_version|. This too must be
1163 // checked in constant time (http://eprint.iacr.org/2003/052/).
Adam Langley4139edb2016-01-13 15:00:54 -08001164 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001165 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001166 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001167 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001168
Robert Sloana27a6a42017-09-05 08:39:28 -07001169 // Select, in constant time, either the decrypted premaster or the random
1170 // premaster based on |good|.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001171 for (size_t i = 0; i < premaster_secret.size(); i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001172 premaster_secret[i] = constant_time_select_8(
1173 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1174 }
Robert Sloan572a4e22017-04-17 10:52:19 -07001175 } else if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001176 // Parse the ClientKeyExchange.
Adam Langley4139edb2016-01-13 15:00:54 -08001177 CBS peer_key;
Robert Sloan572a4e22017-04-17 10:52:19 -07001178 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001179 CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001180 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001181 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001182 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001183 }
1184
Robert Sloana27a6a42017-09-05 08:39:28 -07001185 // Compute the premaster.
Robert Sloan5d625782017-02-13 09:55:39 -08001186 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001187 if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001188 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001189 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001190 }
1191
Robert Sloana27a6a42017-09-05 08:39:28 -07001192 // The key exchange state may now be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001193 hs->key_share.reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001194 } else if (!(alg_k & SSL_kPSK)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001195 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001196 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001197 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001198 }
1199
Robert Sloana27a6a42017-09-05 08:39:28 -07001200 // For a PSK cipher suite, the actual pre-master secret is combined with the
1201 // pre-shared key.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001202 if (alg_a & SSL_aPSK) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001203 if (ssl->psk_server_callback == NULL) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001204 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001205 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001206 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001207 }
1208
Robert Sloana27a6a42017-09-05 08:39:28 -07001209 // Look up the key for the identity.
Robert Sloane56da3e2017-06-26 08:26:42 -07001210 uint8_t psk[PSK_MAX_PSK_LEN];
1211 unsigned psk_len = ssl->psk_server_callback(
1212 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1213 if (psk_len > PSK_MAX_PSK_LEN) {
1214 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001215 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001216 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001217 } else if (psk_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001218 // PSK related to the given identity not found.
Robert Sloane56da3e2017-06-26 08:26:42 -07001219 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001220 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001221 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001222 }
1223
1224 if (alg_k & SSL_kPSK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001225 // In plain PSK, other_secret is a block of 0s with the same length as the
1226 // pre-shared key.
Robert Sloan4562e9d2017-10-02 10:26:51 -07001227 if (!premaster_secret.Init(psk_len)) {
1228 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001229 }
Robert Sloan4562e9d2017-10-02 10:26:51 -07001230 OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
Robert Sloane56da3e2017-06-26 08:26:42 -07001231 }
1232
Robert Sloanb6d070c2017-07-24 08:40:01 -07001233 ScopedCBB new_premaster;
1234 CBB child;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001235 if (!CBB_init(new_premaster.get(),
Robert Sloan4562e9d2017-10-02 10:26:51 -07001236 2 + psk_len + 2 + premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001237 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Robert Sloan4562e9d2017-10-02 10:26:51 -07001238 !CBB_add_bytes(&child, premaster_secret.data(),
1239 premaster_secret.size()) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001240 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001241 !CBB_add_bytes(&child, psk, psk_len) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001242 !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001243 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan4562e9d2017-10-02 10:26:51 -07001244 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001245 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001246 }
1247
Robert Sloan84377092017-08-14 09:33:19 -07001248 if (!ssl_hash_message(hs, msg)) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001249 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001250 }
1251
Robert Sloana27a6a42017-09-05 08:39:28 -07001252 // Compute the master secret.
Robert Sloana94fe052017-02-21 08:49:28 -08001253 hs->new_session->master_key_length = tls1_generate_master_secret(
Robert Sloanb1b54b82017-11-06 13:50:02 -08001254 hs, hs->new_session->master_key, premaster_secret);
Robert Sloana94fe052017-02-21 08:49:28 -08001255 if (hs->new_session->master_key_length == 0) {
Robert Sloan4562e9d2017-10-02 10:26:51 -07001256 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001257 }
Robert Sloana94fe052017-02-21 08:49:28 -08001258 hs->new_session->extended_master_secret = hs->extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001259
Robert Sloan84377092017-08-14 09:33:19 -07001260 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001261 hs->state = state_read_client_certificate_verify;
Robert Sloan4562e9d2017-10-02 10:26:51 -07001262 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001263}
1264
Robert Sloana27a6a42017-09-05 08:39:28 -07001265static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001266 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001267
Robert Sloana27a6a42017-09-05 08:39:28 -07001268 // Only RSA and ECDSA client certificates are supported, so a
1269 // CertificateVerify is required if and only if there's a client certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001270 if (!hs->peer_pubkey) {
1271 hs->transcript.FreeBuffer();
Robert Sloana27a6a42017-09-05 08:39:28 -07001272 hs->state = state_read_change_cipher_spec;
1273 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001274 }
1275
Robert Sloan84377092017-08-14 09:33:19 -07001276 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001277 if (!ssl->method->get_message(ssl, &msg)) {
1278 return ssl_hs_read_message;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001279 }
1280
Robert Sloan84377092017-08-14 09:33:19 -07001281 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001282 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001283 }
1284
Robert Sloan84377092017-08-14 09:33:19 -07001285 CBS certificate_verify = msg.body, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001286
Robert Sloana27a6a42017-09-05 08:39:28 -07001287 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001288 uint16_t signature_algorithm = 0;
Robert Sloan921ef2c2017-10-17 09:02:20 -07001289 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001290 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001291 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001292 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001293 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001294 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001295 uint8_t alert = SSL_AD_DECODE_ERROR;
1296 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001297 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001298 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001299 }
Robert Sloana94fe052017-02-21 08:49:28 -08001300 hs->new_session->peer_signature_algorithm = signature_algorithm;
Robert Sloan2424d842017-05-01 07:46:28 -07001301 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001302 hs->peer_pubkey.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001303 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001304 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001305 return ssl_hs_error;
Adam Langleye9ada862015-05-11 17:20:37 -07001306 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001307
Robert Sloana27a6a42017-09-05 08:39:28 -07001308 // Parse and verify the signature.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001309 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1310 CBS_len(&certificate_verify) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001311 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001312 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001313 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001314 }
1315
Robert Sloan921ef2c2017-10-17 09:02:20 -07001316 bool sig_ok;
Robert Sloana27a6a42017-09-05 08:39:28 -07001317 // The SSL3 construction for CertificateVerify does not decompose into a
1318 // single final digest and signature, and must be special-cased.
Robert Sloan921ef2c2017-10-17 09:02:20 -07001319 if (ssl_protocol_version(ssl) == SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001320 uint8_t digest[EVP_MAX_MD_SIZE];
1321 size_t digest_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001322 if (!hs->transcript.GetSSL3CertVerifyHash(
1323 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001324 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001325 }
1326
Robert Sloanb6d070c2017-07-24 08:40:01 -07001327 UniquePtr<EVP_PKEY_CTX> pctx(
1328 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
1329 sig_ok = pctx &&
1330 EVP_PKEY_verify_init(pctx.get()) &&
1331 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1332 CBS_len(&signature), digest, digest_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001333 } else {
Robert Sloan921ef2c2017-10-17 09:02:20 -07001334 sig_ok =
1335 ssl_public_key_verify(ssl, signature, signature_algorithm,
1336 hs->peer_pubkey.get(), hs->transcript.buffer());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001337 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001338
David Benjamin4969cc92016-04-22 15:02:23 -04001339#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -07001340 sig_ok = true;
David Benjamin4969cc92016-04-22 15:02:23 -04001341 ERR_clear_error();
1342#endif
1343 if (!sig_ok) {
Kenny Rootb8494592015-09-25 02:29:14 +00001344 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001345 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001346 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001347 }
1348
Robert Sloana27a6a42017-09-05 08:39:28 -07001349 // The handshake buffer is no longer necessary, and we may hash the current
1350 // message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001351 hs->transcript.FreeBuffer();
Robert Sloan84377092017-08-14 09:33:19 -07001352 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001353 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001354 }
1355
Robert Sloan84377092017-08-14 09:33:19 -07001356 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001357 hs->state = state_read_change_cipher_spec;
1358 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001359}
1360
Robert Sloana27a6a42017-09-05 08:39:28 -07001361static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
1362 hs->state = state_process_change_cipher_spec;
1363 return ssl_hs_read_change_cipher_spec;
1364}
1365
1366static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001367 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001368 return ssl_hs_error;
1369 }
1370
1371 hs->state = state_read_next_proto;
1372 return ssl_hs_ok;
1373}
1374
1375static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001376 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001377
1378 if (!hs->next_proto_neg_seen) {
1379 hs->state = state_read_channel_id;
1380 return ssl_hs_ok;
1381 }
1382
Robert Sloan84377092017-08-14 09:33:19 -07001383 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001384 if (!ssl->method->get_message(ssl, &msg)) {
1385 return ssl_hs_read_message;
David Benjaminc895d6b2016-08-11 13:26:41 -04001386 }
1387
Robert Sloan84377092017-08-14 09:33:19 -07001388 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1389 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001390 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001391 }
1392
Robert Sloan84377092017-08-14 09:33:19 -07001393 CBS next_protocol = msg.body, selected_protocol, padding;
David Benjamin6e899c72016-06-09 18:02:18 -04001394 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1395 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001396 CBS_len(&next_protocol) != 0) {
1397 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -07001398 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001399 return ssl_hs_error;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001400 }
1401
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001402 if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001403 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001404 }
1405
Robert Sloan84377092017-08-14 09:33:19 -07001406 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001407 hs->state = state_read_channel_id;
1408 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001409}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001410
Robert Sloana27a6a42017-09-05 08:39:28 -07001411static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001412 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001413
1414 if (!ssl->s3->tlsext_channel_id_valid) {
1415 hs->state = state_read_client_finished;
1416 return ssl_hs_ok;
1417 }
1418
Robert Sloan84377092017-08-14 09:33:19 -07001419 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001420 if (!ssl->method->get_message(ssl, &msg)) {
1421 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001422 }
1423
Robert Sloan84377092017-08-14 09:33:19 -07001424 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1425 !tls1_verify_channel_id(hs, msg) ||
1426 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001427 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001428 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001429
Robert Sloan84377092017-08-14 09:33:19 -07001430 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001431 hs->state = state_read_client_finished;
1432 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001433}
1434
Robert Sloana27a6a42017-09-05 08:39:28 -07001435static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1436 SSL *const ssl = hs->ssl;
1437 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1438 if (wait != ssl_hs_ok) {
1439 return wait;
1440 }
1441
1442 if (ssl->session != NULL) {
1443 hs->state = state_finish_server_handshake;
1444 } else {
1445 hs->state = state_send_server_finished;
1446 }
1447
1448 // If this is a full handshake with ChannelID then record the handshake
1449 // hashes in |hs->new_session| in case we need them to verify a
1450 // ChannelID signature on a resumption of this session in the future.
1451 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
1452 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1453 return ssl_hs_error;
1454 }
1455
1456 return ssl_hs_ok;
1457}
1458
1459static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001460 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001461
1462 if (hs->ticket_expected) {
1463 const SSL_SESSION *session;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001464 UniquePtr<SSL_SESSION> session_copy;
Robert Sloane56da3e2017-06-26 08:26:42 -07001465 if (ssl->session == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001466 // Fix the timeout to measure from the ticket issuance time.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001467 ssl_session_rebase_time(ssl, hs->new_session.get());
1468 session = hs->new_session.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001469 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001470 // We are renewing an existing session. Duplicate the session to adjust
1471 // the timeout.
Robert Sloane56da3e2017-06-26 08:26:42 -07001472 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001473 if (!session_copy) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001474 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001475 }
1476
Robert Sloanb6d070c2017-07-24 08:40:01 -07001477 ssl_session_rebase_time(ssl, session_copy.get());
1478 session = session_copy.get();
Steven Valdez909b19f2016-11-21 15:35:44 -05001479 }
1480
Robert Sloanb6d070c2017-07-24 08:40:01 -07001481 ScopedCBB cbb;
1482 CBB body, ticket;
1483 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1484 SSL3_MT_NEW_SESSION_TICKET) ||
1485 !CBB_add_u32(&body, session->timeout) ||
1486 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1487 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1488 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001489 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001490 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001491 }
1492
Robert Sloane56da3e2017-06-26 08:26:42 -07001493 if (!ssl->method->add_change_cipher_spec(ssl) ||
Robert Sloan2e9e66a2017-09-25 09:08:29 -07001494 !tls1_change_cipher_state(hs, evp_aead_seal) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -07001495 !ssl_send_finished(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001496 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001497 }
Adam Langleye9ada862015-05-11 17:20:37 -07001498
Robert Sloana27a6a42017-09-05 08:39:28 -07001499 if (ssl->session != NULL) {
1500 hs->state = state_read_change_cipher_spec;
1501 } else {
1502 hs->state = state_finish_server_handshake;
1503 }
1504 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001505}
Robert Sloanb6d070c2017-07-24 08:40:01 -07001506
Robert Sloana27a6a42017-09-05 08:39:28 -07001507static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1508 SSL *const ssl = hs->ssl;
1509
1510 ssl->method->on_handshake_complete(ssl);
1511
1512 // If we aren't retaining peer certificates then we can discard it now.
1513 if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
1514 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1515 hs->new_session->certs = NULL;
1516 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1517 }
1518
Robert Sloana27a6a42017-09-05 08:39:28 -07001519 if (ssl->session != NULL) {
1520 SSL_SESSION_up_ref(ssl->session);
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001521 ssl->s3->established_session.reset(ssl->session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001522 } else {
Robert Sloan29c1d2c2017-10-30 14:10:28 -07001523 ssl->s3->established_session = std::move(hs->new_session);
Robert Sloana27a6a42017-09-05 08:39:28 -07001524 ssl->s3->established_session->not_resumable = 0;
1525 }
1526
1527 hs->handshake_finalized = true;
1528 ssl->s3->initial_handshake_complete = true;
1529 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1530
1531 hs->state = state_done;
1532 return ssl_hs_ok;
1533}
1534
1535enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
1536 while (hs->state != state_done) {
1537 enum ssl_hs_wait_t ret = ssl_hs_error;
1538 enum ssl_server_hs_state_t state =
1539 static_cast<enum ssl_server_hs_state_t>(hs->state);
1540 switch (state) {
1541 case state_start_accept:
1542 ret = do_start_accept(hs);
1543 break;
1544 case state_read_client_hello:
1545 ret = do_read_client_hello(hs);
1546 break;
1547 case state_select_certificate:
1548 ret = do_select_certificate(hs);
1549 break;
1550 case state_tls13:
1551 ret = do_tls13(hs);
1552 break;
1553 case state_select_parameters:
1554 ret = do_select_parameters(hs);
1555 break;
1556 case state_send_server_hello:
1557 ret = do_send_server_hello(hs);
1558 break;
1559 case state_send_server_certificate:
1560 ret = do_send_server_certificate(hs);
1561 break;
1562 case state_send_server_key_exchange:
1563 ret = do_send_server_key_exchange(hs);
1564 break;
1565 case state_send_server_hello_done:
1566 ret = do_send_server_hello_done(hs);
1567 break;
1568 case state_read_client_certificate:
1569 ret = do_read_client_certificate(hs);
1570 break;
1571 case state_verify_client_certificate:
1572 ret = do_verify_client_certificate(hs);
1573 break;
1574 case state_read_client_key_exchange:
1575 ret = do_read_client_key_exchange(hs);
1576 break;
1577 case state_read_client_certificate_verify:
1578 ret = do_read_client_certificate_verify(hs);
1579 break;
1580 case state_read_change_cipher_spec:
1581 ret = do_read_change_cipher_spec(hs);
1582 break;
1583 case state_process_change_cipher_spec:
1584 ret = do_process_change_cipher_spec(hs);
1585 break;
1586 case state_read_next_proto:
1587 ret = do_read_next_proto(hs);
1588 break;
1589 case state_read_channel_id:
1590 ret = do_read_channel_id(hs);
1591 break;
1592 case state_read_client_finished:
1593 ret = do_read_client_finished(hs);
1594 break;
1595 case state_send_server_finished:
1596 ret = do_send_server_finished(hs);
1597 break;
1598 case state_finish_server_handshake:
1599 ret = do_finish_server_handshake(hs);
1600 break;
1601 case state_done:
1602 ret = ssl_hs_ok;
1603 break;
1604 }
1605
1606 if (hs->state != state) {
1607 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1608 }
1609
1610 if (ret != ssl_hs_ok) {
1611 return ret;
1612 }
1613 }
1614
1615 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1616 return ssl_hs_ok;
1617}
1618
1619const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
1620 enum ssl_server_hs_state_t state =
1621 static_cast<enum ssl_server_hs_state_t>(hs->state);
1622 switch (state) {
1623 case state_start_accept:
1624 return "TLS server start_accept";
1625 case state_read_client_hello:
1626 return "TLS server read_client_hello";
1627 case state_select_certificate:
1628 return "TLS server select_certificate";
1629 case state_tls13:
1630 return tls13_server_handshake_state(hs);
1631 case state_select_parameters:
1632 return "TLS server select_parameters";
1633 case state_send_server_hello:
1634 return "TLS server send_server_hello";
1635 case state_send_server_certificate:
1636 return "TLS server send_server_certificate";
1637 case state_send_server_key_exchange:
1638 return "TLS server send_server_key_exchange";
1639 case state_send_server_hello_done:
1640 return "TLS server send_server_hello_done";
1641 case state_read_client_certificate:
1642 return "TLS server read_client_certificate";
1643 case state_verify_client_certificate:
1644 return "TLS server verify_client_certificate";
1645 case state_read_client_key_exchange:
1646 return "TLS server read_client_key_exchange";
1647 case state_read_client_certificate_verify:
1648 return "TLS server read_client_certificate_verify";
1649 case state_read_change_cipher_spec:
1650 return "TLS server read_change_cipher_spec";
1651 case state_process_change_cipher_spec:
1652 return "TLS server process_change_cipher_spec";
1653 case state_read_next_proto:
1654 return "TLS server read_next_proto";
1655 case state_read_channel_id:
1656 return "TLS server read_channel_id";
1657 case state_read_client_finished:
1658 return "TLS server read_client_finished";
1659 case state_send_server_finished:
1660 return "TLS server send_server_finished";
1661 case state_finish_server_handshake:
1662 return "TLS server finish_server_handshake";
1663 case state_done:
1664 return "TLS server done";
1665 }
1666
1667 return "TLS server unknown";
1668}
1669
1670}