blob: f196db0c8004ca70f2f10550886abb75e37a9d6d [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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279
Robert Sloana27a6a42017-09-05 08:39:28 -0700280 // Handle FALLBACK_SCSV.
Robert Sloanf6200e72017-07-10 08:09:18 -0700281 if (ssl_client_cipher_list_contains_cipher(client_hello,
282 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
283 ssl3_protocol_version(ssl) < hs->max_version) {
284 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
285 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
286 return 0;
287 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400288
Robert Sloanf6200e72017-07-10 08:09:18 -0700289 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800290}
291
Robert Sloanfe7cd212017-08-07 09:03:39 -0700292static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
293 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin1b249672016-12-06 18:25:50 -0500294 CBS cipher_suites;
295 CBS_init(&cipher_suites, client_hello->cipher_suites,
296 client_hello->cipher_suites_len);
297
Robert Sloanfe7cd212017-08-07 09:03:39 -0700298 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
299 if (!sk) {
David Benjamin1b249672016-12-06 18:25:50 -0500300 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700301 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500302 }
303
304 while (CBS_len(&cipher_suites) > 0) {
305 uint16_t cipher_suite;
306
307 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
308 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700309 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500310 }
311
312 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700313 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500314 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700315 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500316 }
317 }
318
319 return sk;
David Benjamin1b249672016-12-06 18:25:50 -0500320}
321
Robert Sloana27a6a42017-09-05 08:39:28 -0700322// ssl_get_compatible_server_ciphers determines the key exchange and
323// authentication cipher suite masks compatible with the server configuration
324// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
325// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin1b249672016-12-06 18:25:50 -0500326static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
327 uint32_t *out_mask_k,
328 uint32_t *out_mask_a) {
329 SSL *const ssl = hs->ssl;
David Benjamin1b249672016-12-06 18:25:50 -0500330 uint32_t mask_k = 0;
331 uint32_t mask_a = 0;
332
Robert Sloan4d1ac502017-02-06 08:36:14 -0800333 if (ssl_has_certificate(ssl)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700334 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
335 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin1b249672016-12-06 18:25:50 -0500336 mask_k |= SSL_kRSA;
David Benjamin1b249672016-12-06 18:25:50 -0500337 }
338 }
339
Robert Sloana27a6a42017-09-05 08:39:28 -0700340 // Check for a shared group to consider ECDHE ciphers.
David Benjamin1b249672016-12-06 18:25:50 -0500341 uint16_t unused;
342 if (tls1_get_shared_group(hs, &unused)) {
343 mask_k |= SSL_kECDHE;
344 }
345
Robert Sloana27a6a42017-09-05 08:39:28 -0700346 // PSK requires a server callback.
David Benjamin1b249672016-12-06 18:25:50 -0500347 if (ssl->psk_server_callback != NULL) {
348 mask_k |= SSL_kPSK;
349 mask_a |= SSL_aPSK;
350 }
351
352 *out_mask_k = mask_k;
353 *out_mask_a = mask_a;
354}
355
356static const SSL_CIPHER *ssl3_choose_cipher(
357 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
358 const struct ssl_cipher_preference_list_st *server_pref) {
359 SSL *const ssl = hs->ssl;
Robert Sloan6f79a502017-04-03 09:16:40 -0700360 STACK_OF(SSL_CIPHER) *prio, *allow;
Robert Sloana27a6a42017-09-05 08:39:28 -0700361 // in_group_flags will either be NULL, or will point to an array of bytes
362 // which indicate equal-preference groups in the |prio| stack. See the
363 // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
364 // struct.
David Benjamin1b249672016-12-06 18:25:50 -0500365 const uint8_t *in_group_flags;
Robert Sloana27a6a42017-09-05 08:39:28 -0700366 // group_min contains the minimal index so far found in a group, or -1 if no
367 // such value exists yet.
David Benjamin1b249672016-12-06 18:25:50 -0500368 int group_min = -1;
369
Robert Sloanfe7cd212017-08-07 09:03:39 -0700370 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
Robert Sloan6f79a502017-04-03 09:16:40 -0700371 ssl_parse_client_cipher_list(client_hello);
Robert Sloanfe7cd212017-08-07 09:03:39 -0700372 if (!client_pref) {
373 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500374 }
375
376 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700377 prio = server_pref->ciphers;
David Benjamin1b249672016-12-06 18:25:50 -0500378 in_group_flags = server_pref->in_group_flags;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700379 allow = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500380 } else {
Robert Sloanfe7cd212017-08-07 09:03:39 -0700381 prio = client_pref.get();
David Benjamin1b249672016-12-06 18:25:50 -0500382 in_group_flags = NULL;
Robert Sloan6f79a502017-04-03 09:16:40 -0700383 allow = server_pref->ciphers;
David Benjamin1b249672016-12-06 18:25:50 -0500384 }
385
Robert Sloan6f79a502017-04-03 09:16:40 -0700386 uint32_t mask_k, mask_a;
David Benjamin1b249672016-12-06 18:25:50 -0500387 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
388
389 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
Robert Sloan6f79a502017-04-03 09:16:40 -0700390 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin1b249672016-12-06 18:25:50 -0500391
Robert Sloan6f79a502017-04-03 09:16:40 -0700392 size_t cipher_index;
Robert Sloana27a6a42017-09-05 08:39:28 -0700393 if (// Check if the cipher is supported for the current version.
Robert Sloan6f79a502017-04-03 09:16:40 -0700394 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
395 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700396 // Check the cipher is supported for the server configuration.
Robert Sloan6f79a502017-04-03 09:16:40 -0700397 (c->algorithm_mkey & mask_k) &&
398 (c->algorithm_auth & mask_a) &&
Robert Sloana27a6a42017-09-05 08:39:28 -0700399 // Check the cipher is in the |allow| list.
Robert Sloan6f79a502017-04-03 09:16:40 -0700400 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin1b249672016-12-06 18:25:50 -0500401 if (in_group_flags != NULL && in_group_flags[i] == 1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700402 // This element of |prio| is in a group. Update the minimum index found
403 // so far and continue looking.
David Benjamin1b249672016-12-06 18:25:50 -0500404 if (group_min == -1 || (size_t)group_min > cipher_index) {
405 group_min = cipher_index;
406 }
407 } else {
408 if (group_min != -1 && (size_t)group_min < cipher_index) {
409 cipher_index = group_min;
410 }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700411 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin1b249672016-12-06 18:25:50 -0500412 }
413 }
414
415 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700416 // We are about to leave a group, but we found a match in it, so that's
417 // our answer.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700418 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin1b249672016-12-06 18:25:50 -0500419 }
420 }
421
Robert Sloanfe7cd212017-08-07 09:03:39 -0700422 return nullptr;
David Benjamin1b249672016-12-06 18:25:50 -0500423}
424
Robert Sloana27a6a42017-09-05 08:39:28 -0700425static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
426 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
427 hs->state = state_read_client_hello;
428 return ssl_hs_ok;
429}
430
431static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500432 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700433
Robert Sloan84377092017-08-14 09:33:19 -0700434 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700435 if (!ssl->method->get_message(ssl, &msg)) {
436 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700437 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700438
Robert Sloan84377092017-08-14 09:33:19 -0700439 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700440 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400441 }
442
David Benjamin1b249672016-12-06 18:25:50 -0500443 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700444 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400445 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan5d625782017-02-13 09:55:39 -0800446 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700447 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400448 }
449
Robert Sloana27a6a42017-09-05 08:39:28 -0700450 // Run the early callback.
Robert Sloan5d625782017-02-13 09:55:39 -0800451 if (ssl->ctx->select_certificate_cb != NULL) {
452 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700453 case ssl_select_cert_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700454 return ssl_hs_certificate_selection_pending;
David Benjaminc895d6b2016-08-11 13:26:41 -0400455
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700456 case ssl_select_cert_error:
Robert Sloana27a6a42017-09-05 08:39:28 -0700457 // Connection rejected.
Robert Sloan5d625782017-02-13 09:55:39 -0800458 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
459 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700460 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400461
Robert Sloan5d625782017-02-13 09:55:39 -0800462 default:
463 /* fallthrough */;
David Benjaminc895d6b2016-08-11 13:26:41 -0400464 }
Robert Sloan5d625782017-02-13 09:55:39 -0800465 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800466
Robert Sloana27a6a42017-09-05 08:39:28 -0700467 // Freeze the version range after the early callback.
Robert Sloane56da3e2017-06-26 08:26:42 -0700468 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700469 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700470 }
471
Robert Sloan5d625782017-02-13 09:55:39 -0800472 uint8_t alert = SSL_AD_DECODE_ERROR;
473 if (!negotiate_version(hs, &alert, &client_hello)) {
474 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700475 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800476 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477
Robert Sloanf6200e72017-07-10 08:09:18 -0700478 hs->client_version = client_hello.version;
Robert Sloan5d625782017-02-13 09:55:39 -0800479 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
480 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700481 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800482 }
483 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
484 client_hello.random_len);
485
Robert Sloana27a6a42017-09-05 08:39:28 -0700486 // Only null compression is supported. TLS 1.3 further requires the peer
487 // advertise no other compression.
Robert Sloan5d625782017-02-13 09:55:39 -0800488 if (OPENSSL_memchr(client_hello.compression_methods, 0,
489 client_hello.compression_methods_len) == NULL ||
490 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
491 client_hello.compression_methods_len != 1)) {
492 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
493 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700494 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800495 }
496
Robert Sloana27a6a42017-09-05 08:39:28 -0700497 // TLS extensions.
Robert Sloan5d625782017-02-13 09:55:39 -0800498 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
499 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Robert Sloana27a6a42017-09-05 08:39:28 -0700500 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800501 }
502
Robert Sloana27a6a42017-09-05 08:39:28 -0700503 hs->state = state_select_certificate;
504 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800505}
506
Robert Sloana27a6a42017-09-05 08:39:28 -0700507static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
Robert Sloan5d625782017-02-13 09:55:39 -0800508 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700509
Robert Sloan84377092017-08-14 09:33:19 -0700510 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700511 if (!ssl->method->get_message(ssl, &msg)) {
512 return ssl_hs_read_message;
Robert Sloan84377092017-08-14 09:33:19 -0700513 }
514
Robert Sloana27a6a42017-09-05 08:39:28 -0700515 // Call |cert_cb| to update server certificates if required.
Robert Sloan5d625782017-02-13 09:55:39 -0800516 if (ssl->cert->cert_cb != NULL) {
517 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
518 if (rv == 0) {
519 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
520 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700521 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400522 }
Robert Sloan5d625782017-02-13 09:55:39 -0800523 if (rv < 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700524 return ssl_hs_x509_lookup;
David Benjaminc895d6b2016-08-11 13:26:41 -0400525 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400526 }
527
Robert Sloan572a4e22017-04-17 10:52:19 -0700528 if (!ssl_on_certificate_selected(hs)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700529 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500530 }
531
Robert Sloan5d625782017-02-13 09:55:39 -0800532 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700533 // Jump to the TLS 1.3 state machine.
534 hs->state = state_tls13;
535 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800536 }
537
538 SSL_CLIENT_HELLO client_hello;
Robert Sloan84377092017-08-14 09:33:19 -0700539 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700540 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800541 }
542
Robert Sloana27a6a42017-09-05 08:39:28 -0700543 // Negotiate the cipher suite. This must be done after |cert_cb| so the
544 // certificate is finalized.
Robert Sloana94fe052017-02-21 08:49:28 -0800545 hs->new_cipher =
Robert Sloan5d625782017-02-13 09:55:39 -0800546 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
Robert Sloana94fe052017-02-21 08:49:28 -0800547 if (hs->new_cipher == NULL) {
Robert Sloan5d625782017-02-13 09:55:39 -0800548 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
549 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700550 return ssl_hs_error;
Robert Sloan5d625782017-02-13 09:55:39 -0800551 }
552
Robert Sloana27a6a42017-09-05 08:39:28 -0700553 hs->state = state_select_parameters;
554 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800555}
556
Robert Sloana27a6a42017-09-05 08:39:28 -0700557static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
558 enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
559 if (wait == ssl_hs_ok) {
560 hs->state = state_finish_server_handshake;
561 return ssl_hs_ok;
Robert Sloan5d625782017-02-13 09:55:39 -0800562 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500563
Robert Sloana27a6a42017-09-05 08:39:28 -0700564 return wait;
565}
566
567static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
568 SSL *const ssl = hs->ssl;
569
570 SSLMessage msg;
571 if (!ssl->method->get_message(ssl, &msg)) {
572 return ssl_hs_read_message;
573 }
574
575 SSL_CLIENT_HELLO client_hello;
576 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
577 return ssl_hs_error;
578 }
579
580 // Determine whether we are doing session resumption.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700581 UniquePtr<SSL_SESSION> session;
Robert Sloana27a6a42017-09-05 08:39:28 -0700582 bool tickets_supported = false, renew_ticket = false;
583 enum ssl_hs_wait_t wait = ssl_get_prev_session(
584 ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
585 if (wait != ssl_hs_ok) {
586 return wait;
Steven Valdez909b19f2016-11-21 15:35:44 -0500587 }
588
Robert Sloana12bf462017-07-17 07:08:26 -0700589 if (session) {
Robert Sloana94fe052017-02-21 08:49:28 -0800590 if (session->extended_master_secret && !hs->extended_master_secret) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700591 // A ClientHello without EMS that attempts to resume a session with EMS
592 // is fatal to the connection.
Steven Valdez909b19f2016-11-21 15:35:44 -0500593 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Robert Sloane56da3e2017-06-26 08:26:42 -0700594 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700595 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500596 }
597
Robert Sloana12bf462017-07-17 07:08:26 -0700598 if (!ssl_session_is_resumable(hs, session.get()) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700599 // If the client offers the EMS extension, but the previous session
600 // didn't use it, then negotiate a new session.
Robert Sloana94fe052017-02-21 08:49:28 -0800601 hs->extended_master_secret != session->extended_master_secret) {
Robert Sloana12bf462017-07-17 07:08:26 -0700602 session.reset();
Steven Valdez909b19f2016-11-21 15:35:44 -0500603 }
604 }
605
Robert Sloana12bf462017-07-17 07:08:26 -0700606 if (session) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700607 // Use the old session.
David Benjamin1b249672016-12-06 18:25:50 -0500608 hs->ticket_expected = renew_ticket;
Robert Sloana12bf462017-07-17 07:08:26 -0700609 ssl->session = session.release();
Robert Sloana27a6a42017-09-05 08:39:28 -0700610 ssl->s3->session_reused = true;
Steven Valdez909b19f2016-11-21 15:35:44 -0500611 } else {
David Benjamin1b249672016-12-06 18:25:50 -0500612 hs->ticket_expected = tickets_supported;
Steven Valdez909b19f2016-11-21 15:35:44 -0500613 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500614 if (!ssl_get_new_session(hs, 1 /* server */)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700615 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500616 }
617
Robert Sloana27a6a42017-09-05 08:39:28 -0700618 // Clear the session ID if we want the session to be single-use.
Steven Valdez909b19f2016-11-21 15:35:44 -0500619 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
Robert Sloana94fe052017-02-21 08:49:28 -0800620 hs->new_session->session_id_length = 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500621 }
622 }
623
624 if (ssl->ctx->dos_protection_cb != NULL &&
625 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700626 // Connection rejected for DOS reasons.
Steven Valdez909b19f2016-11-21 15:35:44 -0500627 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Robert Sloane56da3e2017-06-26 08:26:42 -0700628 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700629 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500630 }
631
632 if (ssl->session == NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800633 hs->new_session->cipher = hs->new_cipher;
Steven Valdez909b19f2016-11-21 15:35:44 -0500634
Robert Sloana27a6a42017-09-05 08:39:28 -0700635 // On new sessions, stash the SNI value in the session.
David Benjamin1b249672016-12-06 18:25:50 -0500636 if (hs->hostname != NULL) {
Robert Sloana94fe052017-02-21 08:49:28 -0800637 OPENSSL_free(hs->new_session->tlsext_hostname);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700638 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
Robert Sloana94fe052017-02-21 08:49:28 -0800639 if (hs->new_session->tlsext_hostname == NULL) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700640 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700641 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500642 }
643 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700644
Robert Sloana27a6a42017-09-05 08:39:28 -0700645 // Determine whether to request a client certificate.
David Benjamin1b249672016-12-06 18:25:50 -0500646 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
Robert Sloana27a6a42017-09-05 08:39:28 -0700647 // Only request a certificate if Channel ID isn't negotiated.
Adam Langley4139edb2016-01-13 15:00:54 -0800648 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
649 ssl->s3->tlsext_channel_id_valid) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700650 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700651 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700652 // CertificateRequest may only be sent in certificate-based ciphers.
Robert Sloana94fe052017-02-21 08:49:28 -0800653 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700654 hs->cert_request = false;
Adam Langleyf4e42722015-06-04 17:45:09 -0700655 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400656
David Benjamin1b249672016-12-06 18:25:50 -0500657 if (!hs->cert_request) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700658 // OpenSSL returns X509_V_OK when no certificates are requested. This is
659 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800660 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400661 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800662 }
663
Robert Sloana27a6a42017-09-05 08:39:28 -0700664 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
665 // deferred. Complete it now.
Robert Sloane56da3e2017-06-26 08:26:42 -0700666 uint8_t alert = SSL_AD_DECODE_ERROR;
667 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
668 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700669 return ssl_hs_error;
Steven Valdez909b19f2016-11-21 15:35:44 -0500670 }
671
Robert Sloana27a6a42017-09-05 08:39:28 -0700672 // Now that all parameters are known, initialize the handshake hash and hash
673 // the ClientHello.
Robert Sloan84377092017-08-14 09:33:19 -0700674 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
675 !ssl_hash_message(hs, msg)) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700676 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700677 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000678 }
679
Robert Sloana27a6a42017-09-05 08:39:28 -0700680 // Release the handshake buffer if client authentication isn't required.
David Benjamin1b249672016-12-06 18:25:50 -0500681 if (!hs->cert_request) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700682 hs->transcript.FreeBuffer();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800683 }
684
Robert Sloan84377092017-08-14 09:33:19 -0700685 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -0700686
687 hs->state = state_send_server_hello;
688 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800689}
690
Robert Sloana27a6a42017-09-05 08:39:28 -0700691static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500692 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -0800693
Robert Sloana27a6a42017-09-05 08:39:28 -0700694 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
695 // known attack while we fix ChannelID itself.
Kenny Roote99801b2015-11-06 15:31:15 -0800696 if (ssl->s3->tlsext_channel_id_valid &&
Robert Sloana94fe052017-02-21 08:49:28 -0800697 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700698 ssl->s3->tlsext_channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800699 }
700
Robert Sloana27a6a42017-09-05 08:39:28 -0700701 // If this is a resumption and the original handshake didn't support
702 // ChannelID then we didn't record the original handshake hashes in the
703 // session and so cannot resume with ChannelIDs.
David Benjaminc895d6b2016-08-11 13:26:41 -0400704 if (ssl->session != NULL &&
705 ssl->session->original_handshake_hash_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700706 ssl->s3->tlsext_channel_id_valid = false;
Kenny Roote99801b2015-11-06 15:31:15 -0800707 }
708
Robert Sloan7d422bc2017-03-06 10:04:29 -0800709 struct OPENSSL_timeval now;
David Benjaminc895d6b2016-08-11 13:26:41 -0400710 ssl_get_current_time(ssl, &now);
711 ssl->s3->server_random[0] = now.tv_sec >> 24;
712 ssl->s3->server_random[1] = now.tv_sec >> 16;
713 ssl->s3->server_random[2] = now.tv_sec >> 8;
714 ssl->s3->server_random[3] = now.tv_sec;
715 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700716 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800717 }
718
Robert Sloana27a6a42017-09-05 08:39:28 -0700719 // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
720 // 1.3 is finalized and we are not implementing a draft version.
David Benjaminc895d6b2016-08-11 13:26:41 -0400721
Robert Sloanb6d070c2017-07-24 08:40:01 -0700722 const SSL_SESSION *session = hs->new_session.get();
David Benjaminc895d6b2016-08-11 13:26:41 -0400723 if (ssl->session != NULL) {
724 session = ssl->session;
725 }
726
Robert Sloanb6d070c2017-07-24 08:40:01 -0700727 ScopedCBB cbb;
728 CBB body, session_id;
729 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400730 !CBB_add_u16(&body, ssl->version) ||
731 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
732 !CBB_add_u8_length_prefixed(&body, &session_id) ||
733 !CBB_add_bytes(&session_id, session->session_id,
734 session->session_id_length) ||
Robert Sloana94fe052017-02-21 08:49:28 -0800735 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400736 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -0500737 !ssl_add_serverhello_tlsext(hs, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700738 !ssl_add_message_cbb(ssl, cbb.get())) {
Kenny Roote99801b2015-11-06 15:31:15 -0800739 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700740 return ssl_hs_error;
Kenny Roote99801b2015-11-06 15:31:15 -0800741 }
742
Robert Sloana27a6a42017-09-05 08:39:28 -0700743 if (ssl->session != NULL) {
744 hs->state = state_send_server_finished;
745 } else {
746 hs->state = state_send_server_certificate;
747 }
748 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800749}
750
Robert Sloana27a6a42017-09-05 08:39:28 -0700751static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500752 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700753 ScopedCBB cbb;
Kenny Rootb8494592015-09-25 02:29:14 +0000754
Robert Sloane56da3e2017-06-26 08:26:42 -0700755 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
756 if (!ssl_has_certificate(ssl)) {
757 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Robert Sloana27a6a42017-09-05 08:39:28 -0700758 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700759 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800760
Robert Sloane56da3e2017-06-26 08:26:42 -0700761 if (!ssl3_output_cert_chain(ssl)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700762 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700763 }
764
765 if (hs->certificate_status_expected) {
766 CBB body, ocsp_response;
Robert Sloana12bf462017-07-17 07:08:26 -0700767 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700768 SSL3_MT_CERTIFICATE_STATUS) ||
769 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
770 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
771 !CBB_add_bytes(&ocsp_response,
772 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
773 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700774 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700775 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700776 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -0700777 }
778 }
779 }
780
Robert Sloana27a6a42017-09-05 08:39:28 -0700781 // Assemble ServerKeyExchange parameters if needed.
Robert Sloane56da3e2017-06-26 08:26:42 -0700782 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
783 uint32_t alg_a = hs->new_cipher->algorithm_auth;
784 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
785 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
786
Robert Sloana27a6a42017-09-05 08:39:28 -0700787 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
788 // the client and server randoms for the signing transcript.
Robert Sloane56da3e2017-06-26 08:26:42 -0700789 CBB child;
Robert Sloana12bf462017-07-17 07:08:26 -0700790 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
791 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
792 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700793 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400794 }
795
Robert Sloana27a6a42017-09-05 08:39:28 -0700796 // PSK ciphers begin with an identity hint.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800797 if (alg_a & SSL_aPSK) {
Adam Langley4139edb2016-01-13 15:00:54 -0800798 size_t len =
799 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
Robert Sloana12bf462017-07-17 07:08:26 -0700800 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
Adam Langley4139edb2016-01-13 15:00:54 -0800801 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
802 len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700803 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800804 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800805 }
806
Robert Sloan572a4e22017-04-17 10:52:19 -0700807 if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700808 // Determine the group to use.
David Benjamind316cba2016-06-02 16:17:39 -0400809 uint16_t group_id;
David Benjamin1b249672016-12-06 18:25:50 -0500810 if (!tls1_get_shared_group(hs, &group_id)) {
Robert Sloanf6200e72017-07-10 08:09:18 -0700811 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -0800812 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700813 return ssl_hs_error;
Robert Sloana12bf462017-07-17 07:08:26 -0700814 }
Robert Sloana94fe052017-02-21 08:49:28 -0800815 hs->new_session->group_id = group_id;
Adam Langley4139edb2016-01-13 15:00:54 -0800816
Robert Sloana27a6a42017-09-05 08:39:28 -0700817 // Set up ECDH, generate a key, and emit the public half.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700818 hs->key_share = SSLKeyShare::Create(group_id);
819 if (!hs->key_share ||
Robert Sloana12bf462017-07-17 07:08:26 -0700820 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
821 !CBB_add_u16(cbb.get(), group_id) ||
822 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700823 !hs->key_share->Offer(&child)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700824 return ssl_hs_error;
David Benjamind316cba2016-06-02 16:17:39 -0400825 }
Adam Langley4139edb2016-01-13 15:00:54 -0800826 } else {
827 assert(alg_k & SSL_kPSK);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800828 }
829
Robert Sloana12bf462017-07-17 07:08:26 -0700830 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700831 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400832 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400833 }
834
Robert Sloana27a6a42017-09-05 08:39:28 -0700835 hs->state = state_send_server_key_exchange;
836 return ssl_hs_ok;
Robert Sloane56da3e2017-06-26 08:26:42 -0700837}
838
Robert Sloana27a6a42017-09-05 08:39:28 -0700839static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
Robert Sloane56da3e2017-06-26 08:26:42 -0700840 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700841
842 if (hs->server_params_len == 0) {
843 hs->state = state_send_server_hello_done;
844 return ssl_hs_ok;
845 }
846
Robert Sloanb6d070c2017-07-24 08:40:01 -0700847 ScopedCBB cbb;
848 CBB body, child;
849 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -0400850 SSL3_MT_SERVER_KEY_EXCHANGE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700851 // |hs->server_params| contains a prefix for signing.
Robert Sloane56da3e2017-06-26 08:26:42 -0700852 hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
853 !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
854 hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700855 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800856 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800857
Robert Sloana27a6a42017-09-05 08:39:28 -0700858 // Add a signature.
Robert Sloana94fe052017-02-21 08:49:28 -0800859 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800860 if (!ssl_has_private_key(ssl)) {
861 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700862 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800863 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800864
Robert Sloana27a6a42017-09-05 08:39:28 -0700865 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -0400866 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -0500867 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700868 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400869 }
870 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
871 if (!CBB_add_u16(&body, signature_algorithm)) {
872 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
873 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700874 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400875 }
876 }
877
Robert Sloana27a6a42017-09-05 08:39:28 -0700878 // Add space for the signature.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700879 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -0400880 uint8_t *ptr;
881 if (!CBB_add_u16_length_prefixed(&body, &child) ||
882 !CBB_reserve(&child, &ptr, max_sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700883 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400884 }
885
Adam Langley4139edb2016-01-13 15:00:54 -0800886 size_t sig_len;
Robert Sloane56da3e2017-06-26 08:26:42 -0700887 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
888 signature_algorithm, hs->server_params,
889 hs->server_params_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -0800890 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -0800891 if (!CBB_did_write(&child, sig_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700892 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800893 }
894 break;
895 case ssl_private_key_failure:
Robert Sloana27a6a42017-09-05 08:39:28 -0700896 return ssl_hs_error;
Adam Langley4139edb2016-01-13 15:00:54 -0800897 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -0700898 return ssl_hs_private_key_operation;
Adam Langley4139edb2016-01-13 15:00:54 -0800899 }
Adam Langley1e4884f2015-09-24 10:57:52 -0700900 }
901
Robert Sloanb6d070c2017-07-24 08:40:01 -0700902 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700903 return ssl_hs_error;
Kenny Rootb8494592015-09-25 02:29:14 +0000904 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400905
David Benjamin1b249672016-12-06 18:25:50 -0500906 OPENSSL_free(hs->server_params);
907 hs->server_params = NULL;
908 hs->server_params_len = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400909
Robert Sloana27a6a42017-09-05 08:39:28 -0700910 hs->state = state_send_server_hello_done;
911 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800912}
913
Robert Sloana27a6a42017-09-05 08:39:28 -0700914static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500915 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700916
Robert Sloanb6d070c2017-07-24 08:40:01 -0700917 ScopedCBB cbb;
918 CBB body;
David Benjaminc895d6b2016-08-11 13:26:41 -0400919
Robert Sloane56da3e2017-06-26 08:26:42 -0700920 if (hs->cert_request) {
921 CBB cert_types, sigalgs_cbb;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700922 if (!ssl->method->init_message(ssl, cbb.get(), &body,
Robert Sloane56da3e2017-06-26 08:26:42 -0700923 SSL3_MT_CERTIFICATE_REQUEST) ||
924 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
925 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
926 (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
927 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
928 (ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
929 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
930 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
931 !ssl_add_client_CA_list(ssl, &body) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700932 !ssl_add_message_cbb(ssl, cbb.get())) {
933 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700934 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400935 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400936 }
937
Robert Sloanb6d070c2017-07-24 08:40:01 -0700938 if (!ssl->method->init_message(ssl, cbb.get(), &body,
939 SSL3_MT_SERVER_HELLO_DONE) ||
940 !ssl_add_message_cbb(ssl, cbb.get())) {
941 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -0700942 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -0400943 }
944
Robert Sloana27a6a42017-09-05 08:39:28 -0700945 hs->state = state_read_client_certificate;
946 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800947}
948
Robert Sloana27a6a42017-09-05 08:39:28 -0700949static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500950 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -0700951
952 if (!hs->cert_request) {
953 hs->state = state_verify_client_certificate;
954 return ssl_hs_ok;
955 }
David Benjamin6e899c72016-06-09 18:02:18 -0400956
Robert Sloan84377092017-08-14 09:33:19 -0700957 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -0700958 if (!ssl->method->get_message(ssl, &msg)) {
959 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -0400960 }
961
Robert Sloan84377092017-08-14 09:33:19 -0700962 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin6e899c72016-06-09 18:02:18 -0400963 if (ssl->version == SSL3_VERSION &&
Robert Sloan84377092017-08-14 09:33:19 -0700964 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700965 // In SSL 3.0, the Certificate message is omitted to signal no
966 // certificate.
David Benjaminc895d6b2016-08-11 13:26:41 -0400967 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin6e899c72016-06-09 18:02:18 -0400968 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -0400969 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700970 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400971 }
972
Robert Sloana27a6a42017-09-05 08:39:28 -0700973 // OpenSSL returns X509_V_OK when no certificates are received. This is
974 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800975 hs->new_session->verify_result = X509_V_OK;
Robert Sloana27a6a42017-09-05 08:39:28 -0700976 hs->state = state_verify_client_certificate;
977 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -0400978 }
979
David Benjamin6e899c72016-06-09 18:02:18 -0400980 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -0400981 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Robert Sloana27a6a42017-09-05 08:39:28 -0700982 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400983 }
984
Robert Sloan84377092017-08-14 09:33:19 -0700985 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700986 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800987 }
988
Robert Sloan84377092017-08-14 09:33:19 -0700989 CBS certificate_msg = msg.body;
Robert Sloan5d625782017-02-13 09:55:39 -0800990 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700991 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
992 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
993 ssl->retain_only_sha256_of_client_certs
994 ? hs->new_session->peer_sha256
995 : NULL,
996 &certificate_msg, ssl->ctx->pool)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400997 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -0700998 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -0400999 }
Robert Sloanfe7cd212017-08-07 09:03:39 -07001000 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1001 hs->new_session->certs = chain.release();
David Benjamin6e899c72016-06-09 18:02:18 -04001002
Steven Valdeze7531f02016-12-14 13:29:57 -05001003 if (CBS_len(&certificate_msg) != 0 ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001004 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin6e899c72016-06-09 18:02:18 -04001005 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001006 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001007 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001008 }
1009
Robert Sloana94fe052017-02-21 08:49:28 -08001010 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001011 // No client certificate so the handshake buffer may be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001012 hs->transcript.FreeBuffer();
David Benjamin6e899c72016-06-09 18:02:18 -04001013
Robert Sloana27a6a42017-09-05 08:39:28 -07001014 // In SSL 3.0, sending no certificate is signaled by omitting the
1015 // Certificate message.
David Benjamin6e899c72016-06-09 18:02:18 -04001016 if (ssl->version == SSL3_VERSION) {
David Benjamin6e899c72016-06-09 18:02:18 -04001017 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjaminc895d6b2016-08-11 13:26:41 -04001018 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001019 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001020 }
1021
1022 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001023 // Fail for TLS only if we required a certificate
David Benjamin6e899c72016-06-09 18:02:18 -04001024 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjaminc895d6b2016-08-11 13:26:41 -04001025 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001026 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001027 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001028
Robert Sloana27a6a42017-09-05 08:39:28 -07001029 // OpenSSL returns X509_V_OK when no certificates are received. This is
1030 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -08001031 hs->new_session->verify_result = X509_V_OK;
Robert Sloan84377092017-08-14 09:33:19 -07001032 } else if (ssl->retain_only_sha256_of_client_certs) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001033 // The hash will have been filled in.
Robert Sloana94fe052017-02-21 08:49:28 -08001034 hs->new_session->peer_sha256_valid = 1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001035 }
David Benjamin6e899c72016-06-09 18:02:18 -04001036
Robert Sloan84377092017-08-14 09:33:19 -07001037 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001038 hs->state = state_verify_client_certificate;
1039 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001040}
1041
Robert Sloana27a6a42017-09-05 08:39:28 -07001042static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
1043 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
1044 switch (ssl_verify_peer_cert(hs)) {
1045 case ssl_verify_ok:
1046 break;
1047 case ssl_verify_invalid:
1048 return ssl_hs_error;
1049 case ssl_verify_retry:
1050 return ssl_hs_certificate_verify;
1051 }
1052 }
1053
1054 hs->state = state_read_client_key_exchange;
1055 return ssl_hs_ok;
1056}
1057
1058static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001059 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001060
1061 ssl_hs_wait_t ret = ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001062 uint8_t *premaster_secret = NULL;
1063 size_t premaster_secret_len = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001064 uint8_t *decrypt_buf = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001065
Robert Sloan84377092017-08-14 09:33:19 -07001066 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001067 if (!ssl->method->get_message(ssl, &msg)) {
1068 return ssl_hs_read_message;
Robert Sloane56da3e2017-06-26 08:26:42 -07001069 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001070
Robert Sloan84377092017-08-14 09:33:19 -07001071 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001072 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001073 }
1074
Robert Sloan84377092017-08-14 09:33:19 -07001075 CBS client_key_exchange = msg.body;
Robert Sloane56da3e2017-06-26 08:26:42 -07001076 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1077 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001078
Robert Sloana27a6a42017-09-05 08:39:28 -07001079 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001080 if (alg_a & SSL_aPSK) {
1081 CBS psk_identity;
1082
Robert Sloana27a6a42017-09-05 08:39:28 -07001083 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1084 // then this is the only field in the message.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001085 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1086 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001087 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloane56da3e2017-06-26 08:26:42 -07001088 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1089 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001090 }
1091
1092 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1093 CBS_contains_zero_byte(&psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001094 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Robert Sloane56da3e2017-06-26 08:26:42 -07001095 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1096 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001097 }
1098
Robert Sloana94fe052017-02-21 08:49:28 -08001099 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001100 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloane56da3e2017-06-26 08:26:42 -07001101 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1102 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001103 }
1104 }
1105
Robert Sloana27a6a42017-09-05 08:39:28 -07001106 // Depending on the key exchange method, compute |premaster_secret| and
1107 // |premaster_secret_len|.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001108 if (alg_k & SSL_kRSA) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001109 CBS encrypted_premaster_secret;
1110 if (ssl->version > SSL3_VERSION) {
1111 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1112 &encrypted_premaster_secret) ||
1113 CBS_len(&client_key_exchange) != 0) {
1114 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1115 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1116 goto err;
1117 }
1118 } else {
1119 encrypted_premaster_secret = client_key_exchange;
1120 }
1121
Robert Sloana27a6a42017-09-05 08:39:28 -07001122 // Allocate a buffer large enough for an RSA decryption.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001123 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
Robert Sloana12bf462017-07-17 07:08:26 -07001124 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001125 if (decrypt_buf == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001126 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001127 goto err;
1128 }
1129
Robert Sloana27a6a42017-09-05 08:39:28 -07001130 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1131 // timing-sensitive code below.
Adam Langley4139edb2016-01-13 15:00:54 -08001132 size_t decrypt_len;
Robert Sloane56da3e2017-06-26 08:26:42 -07001133 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1134 CBS_data(&encrypted_premaster_secret),
1135 CBS_len(&encrypted_premaster_secret))) {
Kenny Roote99801b2015-11-06 15:31:15 -08001136 case ssl_private_key_success:
Kenny Roote99801b2015-11-06 15:31:15 -08001137 break;
1138 case ssl_private_key_failure:
Kenny Roote99801b2015-11-06 15:31:15 -08001139 goto err;
1140 case ssl_private_key_retry:
Robert Sloana27a6a42017-09-05 08:39:28 -07001141 ret = ssl_hs_private_key_operation;
Kenny Roote99801b2015-11-06 15:31:15 -08001142 goto err;
1143 }
1144
David Benjamin4969cc92016-04-22 15:02:23 -04001145 if (decrypt_len != rsa_size) {
David Benjamin4969cc92016-04-22 15:02:23 -04001146 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloane56da3e2017-06-26 08:26:42 -07001147 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1148 goto err;
David Benjamin4969cc92016-04-22 15:02:23 -04001149 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001150
Robert Sloana27a6a42017-09-05 08:39:28 -07001151 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1152 // section 7.4.7.1.
Adam Langley4139edb2016-01-13 15:00:54 -08001153 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
Robert Sloana12bf462017-07-17 07:08:26 -07001154 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001155 if (premaster_secret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +00001156 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001157 goto err;
1158 }
Adam Langley4139edb2016-01-13 15:00:54 -08001159 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
1160 goto err;
1161 }
1162
Robert Sloana27a6a42017-09-05 08:39:28 -07001163 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1164 // publicly invalid.
Adam Langley4139edb2016-01-13 15:00:54 -08001165 if (decrypt_len < 11 + premaster_secret_len) {
Adam Langley4139edb2016-01-13 15:00:54 -08001166 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
Robert Sloane56da3e2017-06-26 08:26:42 -07001167 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1168 goto err;
Adam Langley4139edb2016-01-13 15:00:54 -08001169 }
1170
Robert Sloana27a6a42017-09-05 08:39:28 -07001171 // Check the padding. See RFC 3447, section 7.2.2.
Adam Langley4139edb2016-01-13 15:00:54 -08001172 size_t padding_len = decrypt_len - premaster_secret_len;
1173 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1174 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001175 for (size_t i = 2; i < padding_len - 1; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001176 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1177 }
1178 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1179
Robert Sloana27a6a42017-09-05 08:39:28 -07001180 // The premaster secret must begin with |client_version|. This too must be
1181 // checked in constant time (http://eprint.iacr.org/2003/052/).
Adam Langley4139edb2016-01-13 15:00:54 -08001182 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin1b249672016-12-06 18:25:50 -05001183 (unsigned)(hs->client_version >> 8));
Adam Langley4139edb2016-01-13 15:00:54 -08001184 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin1b249672016-12-06 18:25:50 -05001185 (unsigned)(hs->client_version & 0xff));
Adam Langley4139edb2016-01-13 15:00:54 -08001186
Robert Sloana27a6a42017-09-05 08:39:28 -07001187 // Select, in constant time, either the decrypted premaster or the random
1188 // premaster based on |good|.
David Benjamin7c0d06c2016-08-11 13:26:41 -04001189 for (size_t i = 0; i < premaster_secret_len; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -08001190 premaster_secret[i] = constant_time_select_8(
1191 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1192 }
1193
Adam Langleyd9e397b2015-01-22 14:27:53 -08001194 OPENSSL_free(decrypt_buf);
1195 decrypt_buf = NULL;
Robert Sloan572a4e22017-04-17 10:52:19 -07001196 } else if (alg_k & SSL_kECDHE) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001197 // Parse the ClientKeyExchange.
Adam Langley4139edb2016-01-13 15:00:54 -08001198 CBS peer_key;
Robert Sloan572a4e22017-04-17 10:52:19 -07001199 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
David Benjamind316cba2016-06-02 16:17:39 -04001200 CBS_len(&client_key_exchange) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001201 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloane56da3e2017-06-26 08:26:42 -07001202 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1203 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001204 }
1205
Robert Sloana27a6a42017-09-05 08:39:28 -07001206 // Compute the premaster.
Robert Sloan5d625782017-02-13 09:55:39 -08001207 uint8_t alert = SSL_AD_DECODE_ERROR;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001208 if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
1209 CBS_data(&peer_key), CBS_len(&peer_key))) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001210 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1211 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001212 }
1213
Robert Sloana27a6a42017-09-05 08:39:28 -07001214 // The key exchange state may now be discarded.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001215 hs->key_share.reset();
Robert Sloane56da3e2017-06-26 08:26:42 -07001216 } else if (!(alg_k & SSL_kPSK)) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001217 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloane56da3e2017-06-26 08:26:42 -07001218 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1219 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001220 }
1221
Robert Sloana27a6a42017-09-05 08:39:28 -07001222 // For a PSK cipher suite, the actual pre-master secret is combined with the
1223 // pre-shared key.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001224 if (alg_a & SSL_aPSK) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001225 if (ssl->psk_server_callback == NULL) {
Robert Sloanf6200e72017-07-10 08:09:18 -07001226 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloane56da3e2017-06-26 08:26:42 -07001227 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1228 goto err;
1229 }
1230
Robert Sloana27a6a42017-09-05 08:39:28 -07001231 // Look up the key for the identity.
Robert Sloane56da3e2017-06-26 08:26:42 -07001232 uint8_t psk[PSK_MAX_PSK_LEN];
1233 unsigned psk_len = ssl->psk_server_callback(
1234 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1235 if (psk_len > PSK_MAX_PSK_LEN) {
1236 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1237 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1238 goto err;
1239 } else if (psk_len == 0) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001240 // PSK related to the given identity not found.
Robert Sloane56da3e2017-06-26 08:26:42 -07001241 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1242 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1243 goto err;
1244 }
1245
1246 if (alg_k & SSL_kPSK) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001247 // In plain PSK, other_secret is a block of 0s with the same length as the
1248 // pre-shared key.
Robert Sloane56da3e2017-06-26 08:26:42 -07001249 premaster_secret_len = psk_len;
Robert Sloana12bf462017-07-17 07:08:26 -07001250 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Robert Sloane56da3e2017-06-26 08:26:42 -07001251 if (premaster_secret == NULL) {
1252 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1253 goto err;
1254 }
1255 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
1256 }
1257
Robert Sloanb6d070c2017-07-24 08:40:01 -07001258 ScopedCBB new_premaster;
1259 CBB child;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001260 uint8_t *new_data;
1261 size_t new_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001262 if (!CBB_init(new_premaster.get(),
1263 2 + psk_len + 2 + premaster_secret_len) ||
1264 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001265 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001266 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -08001267 !CBB_add_bytes(&child, psk, psk_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -07001268 !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001269 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001270 goto err;
1271 }
1272
1273 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1274 OPENSSL_free(premaster_secret);
1275 premaster_secret = new_data;
1276 premaster_secret_len = new_len;
1277 }
1278
Robert Sloan84377092017-08-14 09:33:19 -07001279 if (!ssl_hash_message(hs, msg)) {
Robert Sloane56da3e2017-06-26 08:26:42 -07001280 goto err;
1281 }
1282
Robert Sloana27a6a42017-09-05 08:39:28 -07001283 // Compute the master secret.
Robert Sloana94fe052017-02-21 08:49:28 -08001284 hs->new_session->master_key_length = tls1_generate_master_secret(
1285 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1286 if (hs->new_session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001287 goto err;
1288 }
Robert Sloana94fe052017-02-21 08:49:28 -08001289 hs->new_session->extended_master_secret = hs->extended_master_secret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001290
Robert Sloan84377092017-08-14 09:33:19 -07001291 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001292 hs->state = state_read_client_certificate_verify;
1293 ret = ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001294
Adam Langleyd9e397b2015-01-22 14:27:53 -08001295err:
Adam Langley4139edb2016-01-13 15:00:54 -08001296 if (premaster_secret != NULL) {
1297 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001298 OPENSSL_free(premaster_secret);
1299 }
Adam Langleye9ada862015-05-11 17:20:37 -07001300 OPENSSL_free(decrypt_buf);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001301
Robert Sloana27a6a42017-09-05 08:39:28 -07001302 return ret;
1303
Adam Langleyd9e397b2015-01-22 14:27:53 -08001304}
1305
Robert Sloana27a6a42017-09-05 08:39:28 -07001306static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001307 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001308
Robert Sloana27a6a42017-09-05 08:39:28 -07001309 // Only RSA and ECDSA client certificates are supported, so a
1310 // CertificateVerify is required if and only if there's a client certificate.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001311 if (!hs->peer_pubkey) {
1312 hs->transcript.FreeBuffer();
Robert Sloana27a6a42017-09-05 08:39:28 -07001313 hs->state = state_read_change_cipher_spec;
1314 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001315 }
1316
Robert Sloan84377092017-08-14 09:33:19 -07001317 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001318 if (!ssl->method->get_message(ssl, &msg)) {
1319 return ssl_hs_read_message;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001320 }
1321
Robert Sloan84377092017-08-14 09:33:19 -07001322 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001323 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001324 }
1325
Robert Sloan84377092017-08-14 09:33:19 -07001326 CBS certificate_verify = msg.body, signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001327
Robert Sloana27a6a42017-09-05 08:39:28 -07001328 // Determine the signature algorithm.
David Benjaminc895d6b2016-08-11 13:26:41 -04001329 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001330 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001331 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001332 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloane56da3e2017-06-26 08:26:42 -07001333 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001334 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001335 }
Robert Sloane56da3e2017-06-26 08:26:42 -07001336 uint8_t alert = SSL_AD_DECODE_ERROR;
1337 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1338 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana27a6a42017-09-05 08:39:28 -07001339 return ssl_hs_error;
Adam Langleyfad63272015-11-12 12:15:39 -08001340 }
Robert Sloana94fe052017-02-21 08:49:28 -08001341 hs->new_session->peer_signature_algorithm = signature_algorithm;
Robert Sloan2424d842017-05-01 07:46:28 -07001342 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001343 hs->peer_pubkey.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001344 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Robert Sloane56da3e2017-06-26 08:26:42 -07001345 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Robert Sloana27a6a42017-09-05 08:39:28 -07001346 return ssl_hs_error;
Adam Langleye9ada862015-05-11 17:20:37 -07001347 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001348
Robert Sloana27a6a42017-09-05 08:39:28 -07001349 // Parse and verify the signature.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001350 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1351 CBS_len(&certificate_verify) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +00001352 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloane56da3e2017-06-26 08:26:42 -07001353 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001354 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001355 }
1356
David Benjaminc895d6b2016-08-11 13:26:41 -04001357 int sig_ok;
Robert Sloana27a6a42017-09-05 08:39:28 -07001358 // The SSL3 construction for CertificateVerify does not decompose into a
1359 // single final digest and signature, and must be special-cased.
David Benjaminc895d6b2016-08-11 13:26:41 -04001360 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001361 uint8_t digest[EVP_MAX_MD_SIZE];
1362 size_t digest_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001363 if (!hs->transcript.GetSSL3CertVerifyHash(
1364 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001365 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001366 }
1367
Robert Sloanb6d070c2017-07-24 08:40:01 -07001368 UniquePtr<EVP_PKEY_CTX> pctx(
1369 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
1370 sig_ok = pctx &&
1371 EVP_PKEY_verify_init(pctx.get()) &&
1372 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1373 CBS_len(&signature), digest, digest_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001374 } else {
1375 sig_ok = ssl_public_key_verify(
1376 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Robert Sloanb6d070c2017-07-24 08:40:01 -07001377 hs->peer_pubkey.get(), hs->transcript.buffer_data(),
1378 hs->transcript.buffer_len());
Adam Langleyd9e397b2015-01-22 14:27:53 -08001379 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001380
David Benjamin4969cc92016-04-22 15:02:23 -04001381#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1382 sig_ok = 1;
1383 ERR_clear_error();
1384#endif
1385 if (!sig_ok) {
Kenny Rootb8494592015-09-25 02:29:14 +00001386 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloane56da3e2017-06-26 08:26:42 -07001387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001388 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001389 }
1390
Robert Sloana27a6a42017-09-05 08:39:28 -07001391 // The handshake buffer is no longer necessary, and we may hash the current
1392 // message.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001393 hs->transcript.FreeBuffer();
Robert Sloan84377092017-08-14 09:33:19 -07001394 if (!ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001395 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001396 }
1397
Robert Sloan84377092017-08-14 09:33:19 -07001398 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001399 hs->state = state_read_change_cipher_spec;
1400 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001401}
1402
Robert Sloana27a6a42017-09-05 08:39:28 -07001403static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
1404 hs->state = state_process_change_cipher_spec;
1405 return ssl_hs_read_change_cipher_spec;
1406}
1407
1408static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1409 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
1410 return ssl_hs_error;
1411 }
1412
1413 hs->state = state_read_next_proto;
1414 return ssl_hs_ok;
1415}
1416
1417static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001418 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001419
1420 if (!hs->next_proto_neg_seen) {
1421 hs->state = state_read_channel_id;
1422 return ssl_hs_ok;
1423 }
1424
Robert Sloan84377092017-08-14 09:33:19 -07001425 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001426 if (!ssl->method->get_message(ssl, &msg)) {
1427 return ssl_hs_read_message;
David Benjaminc895d6b2016-08-11 13:26:41 -04001428 }
1429
Robert Sloan84377092017-08-14 09:33:19 -07001430 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1431 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001432 return ssl_hs_error;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001433 }
1434
Robert Sloan84377092017-08-14 09:33:19 -07001435 CBS next_protocol = msg.body, selected_protocol, padding;
David Benjamin6e899c72016-06-09 18:02:18 -04001436 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1437 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin7c0d06c2016-08-11 13:26:41 -04001438 CBS_len(&next_protocol) != 0) {
1439 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1440 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana27a6a42017-09-05 08:39:28 -07001441 return ssl_hs_error;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001442 }
1443
1444 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin6e899c72016-06-09 18:02:18 -04001445 &ssl->s3->next_proto_negotiated_len)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001446 return ssl_hs_error;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001447 }
1448
Robert Sloan84377092017-08-14 09:33:19 -07001449 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001450 hs->state = state_read_channel_id;
1451 return ssl_hs_ok;
David Benjamin6e899c72016-06-09 18:02:18 -04001452}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001453
Robert Sloana27a6a42017-09-05 08:39:28 -07001454static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001455 SSL *const ssl = hs->ssl;
Robert Sloana27a6a42017-09-05 08:39:28 -07001456
1457 if (!ssl->s3->tlsext_channel_id_valid) {
1458 hs->state = state_read_client_finished;
1459 return ssl_hs_ok;
1460 }
1461
Robert Sloan84377092017-08-14 09:33:19 -07001462 SSLMessage msg;
Robert Sloana27a6a42017-09-05 08:39:28 -07001463 if (!ssl->method->get_message(ssl, &msg)) {
1464 return ssl_hs_read_message;
David Benjamin6e899c72016-06-09 18:02:18 -04001465 }
1466
Robert Sloan84377092017-08-14 09:33:19 -07001467 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1468 !tls1_verify_channel_id(hs, msg) ||
1469 !ssl_hash_message(hs, msg)) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001470 return ssl_hs_error;
David Benjamin6e899c72016-06-09 18:02:18 -04001471 }
Robert Sloana27a6a42017-09-05 08:39:28 -07001472
Robert Sloan84377092017-08-14 09:33:19 -07001473 ssl->method->next_message(ssl);
Robert Sloana27a6a42017-09-05 08:39:28 -07001474 hs->state = state_read_client_finished;
1475 return ssl_hs_ok;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001476}
1477
Robert Sloana27a6a42017-09-05 08:39:28 -07001478static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1479 SSL *const ssl = hs->ssl;
1480 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1481 if (wait != ssl_hs_ok) {
1482 return wait;
1483 }
1484
1485 if (ssl->session != NULL) {
1486 hs->state = state_finish_server_handshake;
1487 } else {
1488 hs->state = state_send_server_finished;
1489 }
1490
1491 // If this is a full handshake with ChannelID then record the handshake
1492 // hashes in |hs->new_session| in case we need them to verify a
1493 // ChannelID signature on a resumption of this session in the future.
1494 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
1495 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1496 return ssl_hs_error;
1497 }
1498
1499 return ssl_hs_ok;
1500}
1501
1502static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -05001503 SSL *const ssl = hs->ssl;
Robert Sloane56da3e2017-06-26 08:26:42 -07001504
1505 if (hs->ticket_expected) {
1506 const SSL_SESSION *session;
Robert Sloanb6d070c2017-07-24 08:40:01 -07001507 UniquePtr<SSL_SESSION> session_copy;
Robert Sloane56da3e2017-06-26 08:26:42 -07001508 if (ssl->session == NULL) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001509 // Fix the timeout to measure from the ticket issuance time.
Robert Sloanb6d070c2017-07-24 08:40:01 -07001510 ssl_session_rebase_time(ssl, hs->new_session.get());
1511 session = hs->new_session.get();
Robert Sloane56da3e2017-06-26 08:26:42 -07001512 } else {
Robert Sloana27a6a42017-09-05 08:39:28 -07001513 // We are renewing an existing session. Duplicate the session to adjust
1514 // the timeout.
Robert Sloane56da3e2017-06-26 08:26:42 -07001515 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Robert Sloanb6d070c2017-07-24 08:40:01 -07001516 if (!session_copy) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001517 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001518 }
1519
Robert Sloanb6d070c2017-07-24 08:40:01 -07001520 ssl_session_rebase_time(ssl, session_copy.get());
1521 session = session_copy.get();
Steven Valdez909b19f2016-11-21 15:35:44 -05001522 }
1523
Robert Sloanb6d070c2017-07-24 08:40:01 -07001524 ScopedCBB cbb;
1525 CBB body, ticket;
1526 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1527 SSL3_MT_NEW_SESSION_TICKET) ||
1528 !CBB_add_u32(&body, session->timeout) ||
1529 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1530 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1531 !ssl_add_message_cbb(ssl, cbb.get())) {
Robert Sloana27a6a42017-09-05 08:39:28 -07001532 return ssl_hs_error;
Robert Sloane56da3e2017-06-26 08:26:42 -07001533 }
Steven Valdez909b19f2016-11-21 15:35:44 -05001534 }
1535
Robert Sloane56da3e2017-06-26 08:26:42 -07001536 if (!ssl->method->add_change_cipher_spec(ssl) ||
Robert Sloana27a6a42017-09-05 08:39:28 -07001537 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE) ||
1538 !ssl3_send_finished(hs)) {
1539 return ssl_hs_error;
David Benjaminc895d6b2016-08-11 13:26:41 -04001540 }
Adam Langleye9ada862015-05-11 17:20:37 -07001541
Robert Sloana27a6a42017-09-05 08:39:28 -07001542 if (ssl->session != NULL) {
1543 hs->state = state_read_change_cipher_spec;
1544 } else {
1545 hs->state = state_finish_server_handshake;
1546 }
1547 return ssl_hs_flush;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001548}
Robert Sloanb6d070c2017-07-24 08:40:01 -07001549
Robert Sloana27a6a42017-09-05 08:39:28 -07001550static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1551 SSL *const ssl = hs->ssl;
1552
1553 ssl->method->on_handshake_complete(ssl);
1554
1555 // If we aren't retaining peer certificates then we can discard it now.
1556 if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
1557 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1558 hs->new_session->certs = NULL;
1559 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1560 }
1561
1562 SSL_SESSION_free(ssl->s3->established_session);
1563 if (ssl->session != NULL) {
1564 SSL_SESSION_up_ref(ssl->session);
1565 ssl->s3->established_session = ssl->session;
1566 } else {
1567 ssl->s3->established_session = hs->new_session.release();
1568 ssl->s3->established_session->not_resumable = 0;
1569 }
1570
1571 hs->handshake_finalized = true;
1572 ssl->s3->initial_handshake_complete = true;
1573 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1574
1575 hs->state = state_done;
1576 return ssl_hs_ok;
1577}
1578
1579enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
1580 while (hs->state != state_done) {
1581 enum ssl_hs_wait_t ret = ssl_hs_error;
1582 enum ssl_server_hs_state_t state =
1583 static_cast<enum ssl_server_hs_state_t>(hs->state);
1584 switch (state) {
1585 case state_start_accept:
1586 ret = do_start_accept(hs);
1587 break;
1588 case state_read_client_hello:
1589 ret = do_read_client_hello(hs);
1590 break;
1591 case state_select_certificate:
1592 ret = do_select_certificate(hs);
1593 break;
1594 case state_tls13:
1595 ret = do_tls13(hs);
1596 break;
1597 case state_select_parameters:
1598 ret = do_select_parameters(hs);
1599 break;
1600 case state_send_server_hello:
1601 ret = do_send_server_hello(hs);
1602 break;
1603 case state_send_server_certificate:
1604 ret = do_send_server_certificate(hs);
1605 break;
1606 case state_send_server_key_exchange:
1607 ret = do_send_server_key_exchange(hs);
1608 break;
1609 case state_send_server_hello_done:
1610 ret = do_send_server_hello_done(hs);
1611 break;
1612 case state_read_client_certificate:
1613 ret = do_read_client_certificate(hs);
1614 break;
1615 case state_verify_client_certificate:
1616 ret = do_verify_client_certificate(hs);
1617 break;
1618 case state_read_client_key_exchange:
1619 ret = do_read_client_key_exchange(hs);
1620 break;
1621 case state_read_client_certificate_verify:
1622 ret = do_read_client_certificate_verify(hs);
1623 break;
1624 case state_read_change_cipher_spec:
1625 ret = do_read_change_cipher_spec(hs);
1626 break;
1627 case state_process_change_cipher_spec:
1628 ret = do_process_change_cipher_spec(hs);
1629 break;
1630 case state_read_next_proto:
1631 ret = do_read_next_proto(hs);
1632 break;
1633 case state_read_channel_id:
1634 ret = do_read_channel_id(hs);
1635 break;
1636 case state_read_client_finished:
1637 ret = do_read_client_finished(hs);
1638 break;
1639 case state_send_server_finished:
1640 ret = do_send_server_finished(hs);
1641 break;
1642 case state_finish_server_handshake:
1643 ret = do_finish_server_handshake(hs);
1644 break;
1645 case state_done:
1646 ret = ssl_hs_ok;
1647 break;
1648 }
1649
1650 if (hs->state != state) {
1651 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1652 }
1653
1654 if (ret != ssl_hs_ok) {
1655 return ret;
1656 }
1657 }
1658
1659 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1660 return ssl_hs_ok;
1661}
1662
1663const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
1664 enum ssl_server_hs_state_t state =
1665 static_cast<enum ssl_server_hs_state_t>(hs->state);
1666 switch (state) {
1667 case state_start_accept:
1668 return "TLS server start_accept";
1669 case state_read_client_hello:
1670 return "TLS server read_client_hello";
1671 case state_select_certificate:
1672 return "TLS server select_certificate";
1673 case state_tls13:
1674 return tls13_server_handshake_state(hs);
1675 case state_select_parameters:
1676 return "TLS server select_parameters";
1677 case state_send_server_hello:
1678 return "TLS server send_server_hello";
1679 case state_send_server_certificate:
1680 return "TLS server send_server_certificate";
1681 case state_send_server_key_exchange:
1682 return "TLS server send_server_key_exchange";
1683 case state_send_server_hello_done:
1684 return "TLS server send_server_hello_done";
1685 case state_read_client_certificate:
1686 return "TLS server read_client_certificate";
1687 case state_verify_client_certificate:
1688 return "TLS server verify_client_certificate";
1689 case state_read_client_key_exchange:
1690 return "TLS server read_client_key_exchange";
1691 case state_read_client_certificate_verify:
1692 return "TLS server read_client_certificate_verify";
1693 case state_read_change_cipher_spec:
1694 return "TLS server read_change_cipher_spec";
1695 case state_process_change_cipher_spec:
1696 return "TLS server process_change_cipher_spec";
1697 case state_read_next_proto:
1698 return "TLS server read_next_proto";
1699 case state_read_channel_id:
1700 return "TLS server read_channel_id";
1701 case state_read_client_finished:
1702 return "TLS server read_client_finished";
1703 case state_send_server_finished:
1704 return "TLS server send_server_finished";
1705 case state_finish_server_handshake:
1706 return "TLS server finish_server_handshake";
1707 case state_done:
1708 return "TLS server done";
1709 }
1710
1711 return "TLS server unknown";
1712}
1713
1714}