blob: efbb7bd0fb4064c42ded04197c08e29c50409b4f [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * 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).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * 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.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * 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 :-).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Steven Valdez4d71a9a2017-08-14 15:08:34 -040039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * 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.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * 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
Steven Valdez4d71a9a2017-08-14 15:08:34 -040065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
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 *
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400113 * Portions of the attached software ("Contribution") are developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700114 * 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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin880b14e2014-08-25 22:35:07 -0400151#include <assert.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/cipher.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700158#include <openssl/ec.h>
159#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400160#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700161#include <openssl/evp.h>
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400165#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/rand.h>
167#include <openssl/x509.h>
168
David Benjamin2ee94aa2015-04-07 22:38:30 -0400169#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800170#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172
David Benjamin86e95b82017-07-18 16:34:25 -0400173namespace bssl {
174
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400175enum 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 Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin731058e2016-12-03 23:15:13 -0500199int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
200 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400201 CBS cipher_suites;
202 CBS_init(&cipher_suites, client_hello->cipher_suites,
203 client_hello->cipher_suites_len);
204
205 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 }
214 }
215
216 return 0;
217}
218
David Benjaminf04c2e92016-12-06 13:35:25 -0500219static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500220 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500221 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500222 assert(!ssl->s3->have_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400223 CBS supported_versions, versions;
David Benjamin731058e2016-12-03 23:15:13 -0500224 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
225 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -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 Benjamin2dc02042016-09-19 19:57:37 -0400233 } else {
Steven Valdez8f36c512017-06-20 10:55:02 -0400234 /* Convert the ClientHello version to an equivalent supported_versions
235 * extension. */
236 static const uint8_t kTLSVersions[] = {
237 0x03, 0x03, /* TLS 1.2 */
238 0x03, 0x02, /* TLS 1.1 */
239 0x03, 0x01, /* TLS 1 */
240 0x03, 0x00, /* SSL 3 */
241 };
242
243 static const uint8_t kDTLSVersions[] = {
244 0xfe, 0xfd, /* DTLS 1.2 */
245 0xfe, 0xff, /* DTLS 1.0 */
246 };
247
248 size_t versions_len = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400249 if (SSL_is_dtls(ssl)) {
250 if (client_hello->version <= DTLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400251 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400252 } else if (client_hello->version <= DTLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400253 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400254 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400255 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
256 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400257 } else {
258 if (client_hello->version >= TLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400259 versions_len = 8;
Steven Valdezfdd10992016-09-15 16:27:05 -0400260 } else if (client_hello->version >= TLS1_1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400261 versions_len = 6;
Steven Valdezfdd10992016-09-15 16:27:05 -0400262 } else if (client_hello->version >= TLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400263 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400264 } else if (client_hello->version >= SSL3_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400265 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400266 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400267 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
268 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400269 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400270 }
271
Steven Valdez8f36c512017-06-20 10:55:02 -0400272 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400273 return 0;
274 }
275
David Benjamin25fe85b2016-08-09 20:00:32 -0400276 /* 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 = 1;
279
Steven Valdez8f36c512017-06-20 10:55:02 -0400280 /* Handle FALLBACK_SCSV. */
281 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 Valdezfdd10992016-09-15 16:27:05 -0400288
Steven Valdez8f36c512017-06-20 10:55:02 -0400289 return 1;
David Benjamin25fe85b2016-08-09 20:00:32 -0400290}
291
David Benjaminee910bf2017-07-25 22:36:00 -0400292static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
293 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin2578b292016-12-03 23:19:55 -0500294 CBS cipher_suites;
295 CBS_init(&cipher_suites, client_hello->cipher_suites,
296 client_hello->cipher_suites_len);
297
David Benjaminee910bf2017-07-25 22:36:00 -0400298 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
299 if (!sk) {
David Benjamin2578b292016-12-03 23:19:55 -0500300 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminee910bf2017-07-25 22:36:00 -0400301 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -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);
David Benjaminee910bf2017-07-25 22:36:00 -0400309 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500310 }
311
312 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
David Benjaminee910bf2017-07-25 22:36:00 -0400313 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500314 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminee910bf2017-07-25 22:36:00 -0400315 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500316 }
317 }
318
319 return sk;
David Benjamin2578b292016-12-03 23:19:55 -0500320}
321
322/* 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. */
326static 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 Benjamin2578b292016-12-03 23:19:55 -0500330 uint32_t mask_k = 0;
331 uint32_t mask_a = 0;
332
Adam Langley3a2b47a2017-01-24 13:59:42 -0800333 if (ssl_has_certificate(ssl)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400334 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 Benjamin2578b292016-12-03 23:19:55 -0500336 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500337 }
338 }
339
David Benjamin2578b292016-12-03 23:19:55 -0500340 /* Check for a shared group to consider ECDHE ciphers. */
341 uint16_t unused;
342 if (tls1_get_shared_group(hs, &unused)) {
343 mask_k |= SSL_kECDHE;
344 }
345
David Benjamin2578b292016-12-03 23:19:55 -0500346 /* PSK requires a server callback. */
347 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;
David Benjaminccbb1652017-03-22 18:24:32 -0400360 STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjamin2578b292016-12-03 23:19:55 -0500361 /* 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. */
365 const uint8_t *in_group_flags;
366 /* group_min contains the minimal index so far found in a group, or -1 if no
367 * such value exists yet. */
368 int group_min = -1;
369
David Benjaminee910bf2017-07-25 22:36:00 -0400370 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
David Benjaminccbb1652017-03-22 18:24:32 -0400371 ssl_parse_client_cipher_list(client_hello);
David Benjaminee910bf2017-07-25 22:36:00 -0400372 if (!client_pref) {
373 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500374 }
375
376 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjaminccbb1652017-03-22 18:24:32 -0400377 prio = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500378 in_group_flags = server_pref->in_group_flags;
David Benjaminee910bf2017-07-25 22:36:00 -0400379 allow = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500380 } else {
David Benjaminee910bf2017-07-25 22:36:00 -0400381 prio = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500382 in_group_flags = NULL;
David Benjaminccbb1652017-03-22 18:24:32 -0400383 allow = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500384 }
385
David Benjaminccbb1652017-03-22 18:24:32 -0400386 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -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++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400390 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500391
David Benjaminccbb1652017-03-22 18:24:32 -0400392 size_t cipher_index;
393 if (/* Check if the cipher is supported for the current version. */
394 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
395 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
396 /* Check the cipher is supported for the server configuration. */
397 (c->algorithm_mkey & mask_k) &&
398 (c->algorithm_auth & mask_a) &&
399 /* Check the cipher is in the |allow| list. */
400 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500401 if (in_group_flags != NULL && in_group_flags[i] == 1) {
402 /* This element of |prio| is in a group. Update the minimum index found
403 * so far and continue looking. */
404 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 }
David Benjaminee910bf2017-07-25 22:36:00 -0400411 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin2578b292016-12-03 23:19:55 -0500412 }
413 }
414
415 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
416 /* We are about to leave a group, but we found a match in it, so that's
417 * our answer. */
David Benjaminee910bf2017-07-25 22:36:00 -0400418 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin2578b292016-12-03 23:19:55 -0500419 }
420 }
421
David Benjaminee910bf2017-07-25 22:36:00 -0400422 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500423}
424
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400425static 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 Benjaminc3c88822016-11-14 10:32:04 +0900432 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400433
David Benjamin7934f082017-08-01 16:32:25 -0400434 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400435 if (!ssl->method->get_message(ssl, &msg)) {
436 return ssl_hs_read_message;
David Benjamin7934f082017-08-01 16:32:25 -0400437 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400438
David Benjamin7934f082017-08-01 16:32:25 -0400439 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400440 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 }
Adam Langley95c29f32014-06-20 12:00:00 -0700442
David Benjamin731058e2016-12-03 23:15:13 -0500443 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400444 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin3570d732015-06-29 00:28:17 -0400445 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin59bae5a2017-02-02 23:51:22 -0500446 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400447 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 }
Adam Langley95c29f32014-06-20 12:00:00 -0700449
David Benjamin59bae5a2017-02-02 23:51:22 -0500450 /* Run the early callback. */
451 if (ssl->ctx->select_certificate_cb != NULL) {
452 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000453 case ssl_select_cert_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400454 return ssl_hs_certificate_selection_pending;
David Benjamine14ff062016-08-09 16:21:24 -0400455
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000456 case ssl_select_cert_error:
David Benjamin59bae5a2017-02-02 23:51:22 -0500457 /* Connection rejected. */
458 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
459 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400460 return ssl_hs_error;
David Benjamine14ff062016-08-09 16:21:24 -0400461
David Benjamin59bae5a2017-02-02 23:51:22 -0500462 default:
463 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400464 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500465 }
David Benjamine14ff062016-08-09 16:21:24 -0400466
David Benjamin68161cb2017-06-20 14:49:43 -0400467 /* Freeze the version range after the early callback. */
468 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400469 return ssl_hs_error;
David Benjamin68161cb2017-06-20 14:49:43 -0400470 }
471
Adam Langley04540a72017-02-09 10:11:50 -0800472 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500473 if (!negotiate_version(hs, &alert, &client_hello)) {
474 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400475 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500476 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400477
Steven Valdez8f36c512017-06-20 10:55:02 -0400478 hs->client_version = client_hello.version;
David Benjamin59bae5a2017-02-02 23:51:22 -0500479 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
480 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400481 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500482 }
483 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
484 client_hello.random_len);
485
486 /* Only null compression is supported. TLS 1.3 further requires the peer
487 * advertise no other compression. */
488 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);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400494 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500495 }
496
497 /* TLS extensions. */
498 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
499 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400500 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500501 }
502
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400503 hs->state = state_select_certificate;
504 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500505}
506
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400507static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500508 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400509
David Benjamin7934f082017-08-01 16:32:25 -0400510 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400511 if (!ssl->method->get_message(ssl, &msg)) {
512 return ssl_hs_read_message;
David Benjamin7934f082017-08-01 16:32:25 -0400513 }
514
David Benjamin59bae5a2017-02-02 23:51:22 -0500515 /* Call |cert_cb| to update server certificates if required. */
516 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);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400521 return ssl_hs_error;
David Benjamin25fe85b2016-08-09 20:00:32 -0400522 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500523 if (rv < 0) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400524 return ssl_hs_x509_lookup;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900525 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900526 }
527
David Benjamina232a712017-03-30 15:51:53 -0500528 if (!ssl_on_certificate_selected(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400529 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900530 }
531
David Benjamin59bae5a2017-02-02 23:51:22 -0500532 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
533 /* Jump to the TLS 1.3 state machine. */
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400534 hs->state = state_tls13;
535 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500536 }
537
538 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400539 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400540 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500541 }
542
543 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
544 * certificate is finalized. */
David Benjamin45738dd2017-02-09 20:01:26 -0500545 hs->new_cipher =
David Benjamin59bae5a2017-02-02 23:51:22 -0500546 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjamin45738dd2017-02-09 20:01:26 -0500547 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500548 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
549 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400550 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500551 }
552
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400553 hs->state = state_select_parameters;
554 return ssl_hs_ok;
David Benjamin59bae5a2017-02-02 23:51:22 -0500555}
556
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400557static 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;
David Benjamin7934f082017-08-01 16:32:25 -0400562 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400563
564 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
David Benjamin59bae5a2017-02-02 23:51:22 -0500575 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400576 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400577 return ssl_hs_error;
David Benjamin59bae5a2017-02-02 23:51:22 -0500578 }
David Benjamin34202b92016-11-16 19:07:53 +0900579
580 /* Determine whether we are doing session resumption. */
David Benjamin37af90f2017-07-29 01:42:16 -0400581 UniquePtr<SSL_SESSION> session;
David Benjamin34202b92016-11-16 19:07:53 +0900582 int tickets_supported = 0, renew_ticket = 0;
Steven Valdez398085b2017-08-29 13:25:56 -0400583 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;
David Benjamin34202b92016-11-16 19:07:53 +0900587 }
588
David Benjamind781fc42017-07-12 16:25:57 -0400589 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500590 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900591 /* A ClientHello without EMS that attempts to resume a session with EMS
592 * is fatal to the connection. */
David Benjamin34202b92016-11-16 19:07:53 +0900593 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400594 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400595 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900596 }
597
David Benjamind781fc42017-07-12 16:25:57 -0400598 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjamin34202b92016-11-16 19:07:53 +0900599 /* If the client offers the EMS extension, but the previous session
600 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500601 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400602 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900603 }
604 }
605
David Benjamind781fc42017-07-12 16:25:57 -0400606 if (session) {
David Benjamin34202b92016-11-16 19:07:53 +0900607 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900608 hs->ticket_expected = renew_ticket;
David Benjamind781fc42017-07-12 16:25:57 -0400609 ssl->session = session.release();
David Benjamin34202b92016-11-16 19:07:53 +0900610 ssl->s3->session_reused = 1;
611 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900612 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900613 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900614 if (!ssl_get_new_session(hs, 1 /* server */)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400615 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900616 }
617
618 /* Clear the session ID if we want the session to be single-use. */
619 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500620 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900621 }
622 }
623
624 if (ssl->ctx->dos_protection_cb != NULL &&
625 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
626 /* Connection rejected for DOS reasons. */
David Benjamin34202b92016-11-16 19:07:53 +0900627 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400628 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400629 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900630 }
631
David Benjaminf01f42a2016-11-16 19:05:33 +0900632 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500633 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400634
David Benjamin4eb95cc2016-11-16 17:08:23 +0900635 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900636 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500637 OPENSSL_free(hs->new_session->tlsext_hostname);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400638 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
David Benjamin45738dd2017-02-09 20:01:26 -0500639 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamind98107b2017-06-15 22:56:37 -0400640 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400641 return ssl_hs_error;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900642 }
643 }
644
David Benjamin5c1ce292015-05-26 16:59:43 -0400645 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900646 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400647 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500648 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
649 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900650 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400651 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400652 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500653 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900654 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400655 }
Adam Langley37646832016-08-01 16:16:46 -0700656
David Benjaminc3c88822016-11-14 10:32:04 +0900657 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700658 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400659 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500660 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700661 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 }
663
David Benjaminf01f42a2016-11-16 19:05:33 +0900664 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
665 * deferred. Complete it now. */
David Benjamind98107b2017-06-15 22:56:37 -0400666 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);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400669 return ssl_hs_error;
David Benjamin9ef31f02016-10-31 18:01:13 -0400670 }
671
David Benjaminf71036e2017-01-21 14:49:39 -0500672 /* Now that all parameters are known, initialize the handshake hash and hash
673 * the ClientHello. */
David Benjaminca9e8f52017-08-09 15:02:34 -0400674 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400675 !ssl_hash_message(hs, msg)) {
David Benjamind98107b2017-06-15 22:56:37 -0400676 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400677 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400678 }
679
Steven Valdez2b8415e2016-06-30 13:27:23 -0400680 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +0900681 if (!hs->cert_request) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400682 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800683 }
684
David Benjamin8f94c312017-08-01 17:35:55 -0400685 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400686
687 hs->state = state_send_server_hello;
688 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800689}
Adam Langley95c29f32014-06-20 12:00:00 -0700690
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400691static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900692 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400693
694 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
695 * known attack while we fix ChannelID itself. */
696 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -0500697 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400698 ssl->s3->tlsext_channel_id_valid = 0;
699 }
700
701 /* 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. */
Steven Valdez87eab492016-06-27 16:34:59 -0400704 if (ssl->session != NULL &&
705 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400706 ssl->s3->tlsext_channel_id_valid = 0;
707 }
708
David Benjaminad8f5e12017-02-20 17:00:20 -0500709 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -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;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400715 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400716 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -0400717 }
718
David Benjamin55108632016-08-11 22:01:18 -0400719 /* 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 Benjamin1f61f0d2016-07-10 12:20:35 -0400721
David Benjamin31b0c9b2017-07-20 14:49:15 -0400722 const SSL_SESSION *session = hs->new_session.get();
Steven Valdez87eab492016-06-27 16:34:59 -0400723 if (ssl->session != NULL) {
724 session = ssl->session;
725 }
726
David Benjamin1386aad2017-07-19 23:57:40 -0400727 ScopedCBB cbb;
728 CBB body, session_id;
729 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -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) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400733 !CBB_add_bytes(&session_id, session->session_id,
734 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500735 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -0400736 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500737 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400738 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -0400739 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400740 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -0400741 }
742
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400743 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 Langleyfcf25832014-12-18 17:42:32 -0800749}
Adam Langley95c29f32014-06-20 12:00:00 -0700750
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400751static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900752 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400753 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -0400754
David Benjamin44148742017-06-17 13:20:59 -0400755 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);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400758 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400759 }
David Benjamin32a66d52016-07-13 22:03:11 -0400760
David Benjamin44148742017-06-17 13:20:59 -0400761 if (!ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400762 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400763 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400764
David Benjamin44148742017-06-17 13:20:59 -0400765 if (hs->certificate_status_expected) {
766 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -0400767 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -0400768 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)) ||
David Benjamind781fc42017-07-12 16:25:57 -0400774 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -0400775 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400776 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400777 }
David Benjamin0d1730d2017-06-15 23:24:25 -0400778 }
David Benjamin75836432016-06-17 18:48:29 -0400779 }
780
David Benjamin44148742017-06-17 13:20:59 -0400781 /* Assemble ServerKeyExchange parameters if needed. */
782 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)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100786
David Benjamin44148742017-06-17 13:20:59 -0400787 /* Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
788 * the client and server randoms for the signing transcript. */
789 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -0400790 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)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400793 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400794 }
795
David Benjamin2a0b3912015-12-18 01:01:21 -0500796 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800797 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500798 size_t len =
799 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
David Benjamind781fc42017-07-12 16:25:57 -0400800 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin2a0b3912015-12-18 01:01:21 -0500801 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
802 len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400803 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700807 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400808 /* Determine the group to use. */
809 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900810 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -0400811 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -0500812 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400813 return ssl_hs_error;
David Benjamind781fc42017-07-12 16:25:57 -0400814 }
David Benjamin45738dd2017-02-09 20:01:26 -0500815 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700816
David Benjamin4298d772015-12-19 00:18:25 -0500817 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc642aca2017-07-19 23:28:43 -0400818 hs->key_share = SSLKeyShare::Create(group_id);
819 if (!hs->key_share ||
David Benjamind781fc42017-07-12 16:25:57 -0400820 !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) ||
David Benjaminc642aca2017-07-19 23:28:43 -0400823 !hs->key_share->Offer(&child)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400824 return ssl_hs_error;
Matt Braithwaite053931e2016-05-25 12:06:05 -0700825 }
David Benjamin2a0b3912015-12-18 01:01:21 -0500826 } else {
827 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 }
Adam Langley95c29f32014-06-20 12:00:00 -0700829
David Benjamind781fc42017-07-12 16:25:57 -0400830 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400831 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400832 }
David Benjaminc42acee2016-06-17 17:47:58 -0400833 }
834
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400835 hs->state = state_send_server_key_exchange;
836 return ssl_hs_ok;
David Benjamin44148742017-06-17 13:20:59 -0400837}
838
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400839static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin44148742017-06-17 13:20:59 -0400840 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400841
842 if (hs->server_params_len == 0) {
843 hs->state = state_send_server_hello_done;
844 return ssl_hs_ok;
845 }
846
David Benjamin1386aad2017-07-19 23:57:40 -0400847 ScopedCBB cbb;
848 CBB body, child;
849 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -0400850 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin44148742017-06-17 13:20:59 -0400851 /* |hs->server_params| contains a prefix for signing. */
852 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)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400855 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
David Benjamin2a0b3912015-12-18 01:01:21 -0500858 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -0500859 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500860 if (!ssl_has_private_key(ssl)) {
861 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400862 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 }
Adam Langley95c29f32014-06-20 12:00:00 -0700864
David Benjaminc42acee2016-06-17 17:47:58 -0400865 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -0700866 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900867 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400868 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -0700869 }
David Benjaminc42acee2016-06-17 17:47:58 -0400870 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400871 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -0400872 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
873 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400874 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400875 }
David Benjaminc42acee2016-06-17 17:47:58 -0400876 }
877
878 /* Add space for the signature. */
David Benjamin31b0c9b2017-07-20 14:49:15 -0400879 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -0400880 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -0400881 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -0400882 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400883 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400884 }
885
David Benjamin2a0b3912015-12-18 01:01:21 -0500886 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -0400887 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
888 signature_algorithm, hs->server_params,
889 hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500890 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -0500891 if (!CBB_did_write(&child, sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400892 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500893 }
894 break;
895 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400896 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500897 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400898 return ssl_hs_private_key_operation;
David Benjamin2a0b3912015-12-18 01:01:21 -0500899 }
David Benjamin1f9f9c42015-08-28 16:17:59 -0400900 }
901
David Benjamin1386aad2017-07-19 23:57:40 -0400902 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400903 return ssl_hs_error;
David Benjamin1f9f9c42015-08-28 16:17:59 -0400904 }
David Benjaminc42acee2016-06-17 17:47:58 -0400905
David Benjaminc3c88822016-11-14 10:32:04 +0900906 OPENSSL_free(hs->server_params);
907 hs->server_params = NULL;
908 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -0400909
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400910 hs->state = state_send_server_hello_done;
911 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800912}
Adam Langley95c29f32014-06-20 12:00:00 -0700913
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400914static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900915 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400916
David Benjamin1386aad2017-07-19 23:57:40 -0400917 ScopedCBB cbb;
918 CBB body;
David Benjamin75836432016-06-17 18:48:29 -0400919
David Benjamin0d1730d2017-06-15 23:24:25 -0400920 if (hs->cert_request) {
921 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -0400922 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -0400923 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) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400932 !ssl_add_message_cbb(ssl, cbb.get())) {
933 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400934 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -0400935 }
936 }
937
David Benjamin1386aad2017-07-19 23:57:40 -0400938 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);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400942 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -0400943 }
944
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400945 hs->state = state_read_client_certificate;
946 return ssl_hs_flush;
Adam Langleyfcf25832014-12-18 17:42:32 -0800947}
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400949static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900950 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400951
952 if (!hs->cert_request) {
953 hs->state = state_verify_client_certificate;
954 return ssl_hs_ok;
955 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400956
David Benjamin7934f082017-08-01 16:32:25 -0400957 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400958 if (!ssl->method->get_message(ssl, &msg)) {
959 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400960 }
961
David Benjamin7934f082017-08-01 16:32:25 -0400962 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400963 if (ssl->version == SSL3_VERSION &&
David Benjamin7934f082017-08-01 16:32:25 -0400964 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -0700965 /* In SSL 3.0, the Certificate message is omitted to signal no
966 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -0400967 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400968 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -0400969 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400970 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400971 }
972
Adam Langley37646832016-08-01 16:16:46 -0700973 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400974 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500975 hs->new_session->verify_result = X509_V_OK;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400976 hs->state = state_verify_client_certificate;
977 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400978 }
979
David Benjamin9f1dc822016-06-07 17:03:46 -0400980 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -0400981 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400982 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400983 }
984
David Benjamin7934f082017-08-01 16:32:25 -0400985 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400986 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500987 }
988
David Benjamin7934f082017-08-01 16:32:25 -0400989 CBS certificate_msg = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800990 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400991 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 Benjamin5c900c82016-07-13 23:03:26 -0400997 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400998 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400999 }
David Benjamine664a532017-07-20 20:19:36 -04001000 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1001 hs->new_session->certs = chain.release();
David Benjamin9f1dc822016-06-07 17:03:46 -04001002
Adam Langley68e71242016-12-12 11:06:16 -08001003 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001004 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001005 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001006 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001007 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001008 }
1009
David Benjamin45738dd2017-02-09 20:01:26 -05001010 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001011 /* No client certificate so the handshake buffer may be discarded. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001012 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001013
David Benjamine455e512016-08-01 20:11:13 -04001014 /* In SSL 3.0, sending no certificate is signaled by omitting the
1015 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001016 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001017 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001018 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001019 return ssl_hs_error;
David Benjamine455e512016-08-01 20:11:13 -04001020 }
1021
1022 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001023 /* Fail for TLS only if we required a certificate */
1024 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001025 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001026 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001027 }
Adam Langley37646832016-08-01 16:16:46 -07001028
1029 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001030 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001031 hs->new_session->verify_result = X509_V_OK;
David Benjamin8f94c312017-08-01 17:35:55 -04001032 } else if (ssl->retain_only_sha256_of_client_certs) {
1033 /* The hash will have been filled in. */
David Benjamin45738dd2017-02-09 20:01:26 -05001034 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001035 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001036
David Benjamin8f94c312017-08-01 17:35:55 -04001037 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001038 hs->state = state_verify_client_certificate;
1039 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001040}
1041
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001042static 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 Benjaminc3c88822016-11-14 10:32:04 +09001059 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001060
1061 ssl_hs_wait_t ret = ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 uint8_t *premaster_secret = NULL;
1063 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001064 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001065
David Benjamin7934f082017-08-01 16:32:25 -04001066 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001067 if (!ssl->method->get_message(ssl, &msg)) {
1068 return ssl_hs_read_message;
David Benjamin44148742017-06-17 13:20:59 -04001069 }
David Benjamin276b7e82017-01-21 14:13:39 -05001070
David Benjamin7934f082017-08-01 16:32:25 -04001071 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001072 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 }
Adam Langley95c29f32014-06-20 12:00:00 -07001074
David Benjamin7934f082017-08-01 16:32:25 -04001075 CBS client_key_exchange = msg.body;
David Benjamin44148742017-06-17 13:20:59 -04001076 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1077 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001078
David Benjamin44148742017-06-17 13:20:59 -04001079 /* If using a PSK key exchange, parse the PSK identity. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 if (alg_a & SSL_aPSK) {
1081 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1084 * then this is the only field in the message. */
1085 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1086 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001087 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001088 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1089 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001091
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1093 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001094 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001095 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1096 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001097 }
David Benjamin35c02302014-07-13 04:14:59 -04001098
David Benjamin45738dd2017-02-09 20:01:26 -05001099 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001100 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001101 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1102 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001105
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 /* Depending on the key exchange method, compute |premaster_secret| and
1107 * |premaster_secret_len|. */
1108 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001109 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
Adam Langleyfcf25832014-12-18 17:42:32 -08001122 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001123 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamind781fc42017-07-12 16:25:57 -04001124 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001125 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001126 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 goto err;
1128 }
David Benjamin35c02302014-07-13 04:14:59 -04001129
David Benjamin44148742017-06-17 13:20:59 -04001130 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1131 * timing-sensitive code below. */
David Benjamin3f5b43d2015-12-01 19:31:24 -05001132 size_t decrypt_len;
David Benjamin44148742017-06-17 13:20:59 -04001133 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1134 CBS_data(&encrypted_premaster_secret),
1135 CBS_len(&encrypted_premaster_secret))) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001136 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001137 break;
1138 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001139 goto err;
1140 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001141 ret = ssl_hs_private_key_operation;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001142 goto err;
1143 }
1144
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001145 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001146 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001147 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1148 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001149 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001150
David Benjamin3f5b43d2015-12-01 19:31:24 -05001151 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1152 * section 7.4.7.1. */
1153 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001154 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001156 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 goto err;
1158 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001159 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001160 goto err;
1161 }
1162
David Benjamin3f5b43d2015-12-01 19:31:24 -05001163 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1164 * publicly invalid. */
1165 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001166 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001167 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1168 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001169 }
David Benjamin35c02302014-07-13 04:14:59 -04001170
David Benjamin3f5b43d2015-12-01 19:31:24 -05001171 /* Check the padding. See RFC 3447, section 7.2.2. */
1172 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 Benjamin54091232016-09-05 12:47:25 -04001175 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001176 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1177 }
1178 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1179
1180 /* The premaster secret must begin with |client_version|. This too must be
1181 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1182 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001183 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001184 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001185 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001186
1187 /* Select, in constant time, either the decrypted premaster or the random
1188 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001189 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001190 premaster_secret[i] = constant_time_select_8(
1191 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1192 }
1193
1194 OPENSSL_free(decrypt_buf);
1195 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001196 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001197 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001198 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001199 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001200 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001201 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001202 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1203 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001204 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001205
David Benjamin4298d772015-12-19 00:18:25 -05001206 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001207 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001208 if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
1209 CBS_data(&peer_key), CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001210 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1211 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 }
Adam Langley95c29f32014-06-20 12:00:00 -07001213
David Benjamin4298d772015-12-19 00:18:25 -05001214 /* The key exchange state may now be discarded. */
David Benjaminc642aca2017-07-19 23:28:43 -04001215 hs->key_share.reset();
David Benjamin44148742017-06-17 13:20:59 -04001216 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001217 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001218 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1219 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001220 }
Adam Langley95c29f32014-06-20 12:00:00 -07001221
Adam Langleyfcf25832014-12-18 17:42:32 -08001222 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1223 * pre-shared key. */
1224 if (alg_a & SSL_aPSK) {
David Benjamin44148742017-06-17 13:20:59 -04001225 if (ssl->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001226 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin44148742017-06-17 13:20:59 -04001227 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1228 goto err;
1229 }
1230
1231 /* Look up the key for the identity. */
1232 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) {
1240 /* PSK related to the given identity not found */
1241 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) {
1247 /* In plain PSK, other_secret is a block of 0s with the same length as the
1248 * pre-shared key. */
1249 premaster_secret_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001250 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
David Benjamin44148742017-06-17 13:20:59 -04001251 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
David Benjamin1386aad2017-07-19 23:57:40 -04001258 ScopedCBB new_premaster;
1259 CBB child;
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 uint8_t *new_data;
1261 size_t new_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001262 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 Langleyfcf25832014-12-18 17:42:32 -08001265 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001266 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001267 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001268 !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001269 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001270 goto err;
1271 }
David Benjamin14c83e72014-07-13 04:54:57 -04001272
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1274 OPENSSL_free(premaster_secret);
1275 premaster_secret = new_data;
1276 premaster_secret_len = new_len;
1277 }
David Benjamin14c83e72014-07-13 04:54:57 -04001278
David Benjamin7934f082017-08-01 16:32:25 -04001279 if (!ssl_hash_message(hs, msg)) {
David Benjamin44148742017-06-17 13:20:59 -04001280 goto err;
1281 }
1282
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001284 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 Langleyfcf25832014-12-18 17:42:32 -08001287 goto err;
1288 }
David Benjaminfc02b592017-02-17 16:26:01 -05001289 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001290
David Benjamin8f94c312017-08-01 17:35:55 -04001291 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001292 hs->state = state_read_client_certificate_verify;
1293 ret = ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001294
Adam Langley95c29f32014-06-20 12:00:00 -07001295err:
David Benjamin4298d772015-12-19 00:18:25 -05001296 if (premaster_secret != NULL) {
1297 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001298 OPENSSL_free(premaster_secret);
1299 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001300 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001301
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001302 return ret;
1303
Adam Langleyfcf25832014-12-18 17:42:32 -08001304}
Adam Langley95c29f32014-06-20 12:00:00 -07001305
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001306static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001307 SSL *const ssl = hs->ssl;
David Benjamin6553b372014-07-22 14:11:30 -04001308
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 /* Only RSA and ECDSA client certificates are supported, so a
1310 * CertificateVerify is required if and only if there's a client certificate.
1311 * */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001312 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001313 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001314 hs->state = state_read_change_cipher_spec;
1315 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 }
Adam Langley95c29f32014-06-20 12:00:00 -07001317
David Benjamin7934f082017-08-01 16:32:25 -04001318 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001319 if (!ssl->method->get_message(ssl, &msg)) {
1320 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001321 }
David Benjaminef865502014-08-24 02:48:34 -04001322
David Benjamin7934f082017-08-01 16:32:25 -04001323 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001324 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001325 }
1326
David Benjamin7934f082017-08-01 16:32:25 -04001327 CBS certificate_verify = msg.body, signature;
David Benjamin6897dbe2014-07-12 20:18:28 -04001328
David Benjamin7934f082017-08-01 16:32:25 -04001329 /* Determine the signature algorithm. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001330 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001331 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001332 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001333 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001334 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001335 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001336 }
David Benjamin8d606e32017-06-15 22:43:04 -04001337 uint8_t alert = SSL_AD_DECODE_ERROR;
1338 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001339 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001340 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001341 }
David Benjamin45738dd2017-02-09 20:01:26 -05001342 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001343 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001344 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001345 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001346 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001347 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001348 }
David Benjamin854dd652014-08-26 00:32:30 -04001349
Adam Langleyfcf25832014-12-18 17:42:32 -08001350 /* Parse and verify the signature. */
1351 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1352 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001353 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001354 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001355 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 }
Adam Langley95c29f32014-06-20 12:00:00 -07001357
Steven Valdez2b8415e2016-06-30 13:27:23 -04001358 int sig_ok;
1359 /* The SSL3 construction for CertificateVerify does not decompose into a
1360 * single final digest and signature, and must be special-cased. */
1361 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001362 uint8_t digest[EVP_MAX_MD_SIZE];
1363 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001364 if (!hs->transcript.GetSSL3CertVerifyHash(
1365 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001366 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001367 }
1368
David Benjamin31b0c9b2017-07-20 14:49:15 -04001369 UniquePtr<EVP_PKEY_CTX> pctx(
1370 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
David Benjamin1386aad2017-07-19 23:57:40 -04001371 sig_ok = pctx &&
1372 EVP_PKEY_verify_init(pctx.get()) &&
1373 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1374 CBS_len(&signature), digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001375 } else {
1376 sig_ok = ssl_public_key_verify(
1377 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin6dc8bf62017-07-19 16:38:21 -04001378 hs->peer_pubkey.get(), hs->transcript.buffer_data(),
1379 hs->transcript.buffer_len());
Adam Langleyfcf25832014-12-18 17:42:32 -08001380 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001381
David Benjaminbf82aed2016-03-01 22:57:40 -05001382#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1383 sig_ok = 1;
1384 ERR_clear_error();
1385#endif
1386 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001387 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001388 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001389 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001390 }
1391
Steven Valdez2b8415e2016-06-30 13:27:23 -04001392 /* The handshake buffer is no longer necessary, and we may hash the current
1393 * message.*/
David Benjamin6dc8bf62017-07-19 16:38:21 -04001394 hs->transcript.FreeBuffer();
David Benjamin7934f082017-08-01 16:32:25 -04001395 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001396 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001397 }
1398
David Benjamin8f94c312017-08-01 17:35:55 -04001399 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001400 hs->state = state_read_change_cipher_spec;
1401 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001402}
Adam Langley95c29f32014-06-20 12:00:00 -07001403
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001404static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
1405 hs->state = state_process_change_cipher_spec;
1406 return ssl_hs_read_change_cipher_spec;
1407}
1408
1409static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1410 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
1411 return ssl_hs_error;
1412 }
1413
1414 hs->state = state_read_next_proto;
1415 return ssl_hs_ok;
1416}
1417
1418static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001419 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001420
1421 if (!hs->next_proto_neg_seen) {
1422 hs->state = state_read_channel_id;
1423 return ssl_hs_ok;
1424 }
1425
David Benjamin7934f082017-08-01 16:32:25 -04001426 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001427 if (!ssl->method->get_message(ssl, &msg)) {
1428 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 }
Adam Langley95c29f32014-06-20 12:00:00 -07001430
David Benjamin7934f082017-08-01 16:32:25 -04001431 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1432 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001433 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001434 }
1435
David Benjamin7934f082017-08-01 16:32:25 -04001436 CBS next_protocol = msg.body, selected_protocol, padding;
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1438 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001439 CBS_len(&next_protocol) != 0) {
1440 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1441 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001442 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001443 }
1444
1445 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001446 &ssl->s3->next_proto_negotiated_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001447 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001448 }
Adam Langley95c29f32014-06-20 12:00:00 -07001449
David Benjamin8f94c312017-08-01 17:35:55 -04001450 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001451 hs->state = state_read_channel_id;
1452 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001453}
Adam Langley95c29f32014-06-20 12:00:00 -07001454
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001455static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001456 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001457
1458 if (!ssl->s3->tlsext_channel_id_valid) {
1459 hs->state = state_read_client_finished;
1460 return ssl_hs_ok;
1461 }
1462
David Benjamin7934f082017-08-01 16:32:25 -04001463 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001464 if (!ssl->method->get_message(ssl, &msg)) {
1465 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001466 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001467
David Benjamin7934f082017-08-01 16:32:25 -04001468 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1469 !tls1_verify_channel_id(hs, msg) ||
1470 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001471 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001473
David Benjamin8f94c312017-08-01 17:35:55 -04001474 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001475 hs->state = state_read_client_finished;
1476 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001477}
David Benjamin9f1dc822016-06-07 17:03:46 -04001478
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001479static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1480 SSL *const ssl = hs->ssl;
1481 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1482 if (wait != ssl_hs_ok) {
1483 return wait;
1484 }
1485
1486 if (ssl->session != NULL) {
1487 hs->state = state_finish_server_handshake;
1488 } else {
1489 hs->state = state_send_server_finished;
1490 }
1491
1492 /* If this is a full handshake with ChannelID then record the handshake
1493 * hashes in |hs->new_session| in case we need them to verify a
1494 * ChannelID signature on a resumption of this session in the future. */
1495 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
1496 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1497 return ssl_hs_error;
1498 }
1499
1500 return ssl_hs_ok;
1501}
1502
1503static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001504 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001505
1506 if (hs->ticket_expected) {
1507 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001508 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001509 if (ssl->session == NULL) {
1510 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001511 ssl_session_rebase_time(ssl, hs->new_session.get());
1512 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001513 } else {
1514 /* We are renewing an existing session. Duplicate the session to adjust
1515 * the timeout. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001516 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001517 if (!session_copy) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001518 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001519 }
1520
David Benjamin1386aad2017-07-19 23:57:40 -04001521 ssl_session_rebase_time(ssl, session_copy.get());
1522 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001523 }
1524
David Benjamin1386aad2017-07-19 23:57:40 -04001525 ScopedCBB cbb;
1526 CBB body, ticket;
1527 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1528 SSL3_MT_NEW_SESSION_TICKET) ||
1529 !CBB_add_u32(&body, session->timeout) ||
1530 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1531 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1532 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001533 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001534 }
David Benjamin123db572016-11-03 16:59:25 -04001535 }
1536
David Benjaminb5f55c32017-06-15 23:15:15 -04001537 if (!ssl->method->add_change_cipher_spec(ssl) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001538 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE) ||
1539 !ssl3_send_finished(hs)) {
1540 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001541 }
1542
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001543 if (ssl->session != NULL) {
1544 hs->state = state_read_change_cipher_spec;
1545 } else {
1546 hs->state = state_finish_server_handshake;
1547 }
1548 return ssl_hs_flush;
David Benjamin9f1dc822016-06-07 17:03:46 -04001549}
David Benjamin86e95b82017-07-18 16:34:25 -04001550
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001551static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1552 SSL *const ssl = hs->ssl;
1553
1554 ssl->method->on_handshake_complete(ssl);
1555
1556 /* If we aren't retaining peer certificates then we can discard it
1557 * now. */
1558 if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
1559 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1560 hs->new_session->certs = NULL;
1561 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1562 }
1563
1564 SSL_SESSION_free(ssl->s3->established_session);
1565 if (ssl->session != NULL) {
1566 SSL_SESSION_up_ref(ssl->session);
1567 ssl->s3->established_session = ssl->session;
1568 } else {
1569 ssl->s3->established_session = hs->new_session.release();
1570 ssl->s3->established_session->not_resumable = 0;
1571 }
1572
1573 ssl->s3->initial_handshake_complete = 1;
1574 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1575
1576 hs->state = state_done;
1577 return ssl_hs_ok;
1578}
1579
1580enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
1581 while (hs->state != state_done) {
1582 enum ssl_hs_wait_t ret = ssl_hs_error;
1583 enum ssl_server_hs_state_t state =
1584 static_cast<enum ssl_server_hs_state_t>(hs->state);
1585 switch (state) {
1586 case state_start_accept:
1587 ret = do_start_accept(hs);
1588 break;
1589 case state_read_client_hello:
1590 ret = do_read_client_hello(hs);
1591 break;
1592 case state_select_certificate:
1593 ret = do_select_certificate(hs);
1594 break;
1595 case state_tls13:
1596 ret = do_tls13(hs);
1597 break;
1598 case state_select_parameters:
1599 ret = do_select_parameters(hs);
1600 break;
1601 case state_send_server_hello:
1602 ret = do_send_server_hello(hs);
1603 break;
1604 case state_send_server_certificate:
1605 ret = do_send_server_certificate(hs);
1606 break;
1607 case state_send_server_key_exchange:
1608 ret = do_send_server_key_exchange(hs);
1609 break;
1610 case state_send_server_hello_done:
1611 ret = do_send_server_hello_done(hs);
1612 break;
1613 case state_read_client_certificate:
1614 ret = do_read_client_certificate(hs);
1615 break;
1616 case state_verify_client_certificate:
1617 ret = do_verify_client_certificate(hs);
1618 break;
1619 case state_read_client_key_exchange:
1620 ret = do_read_client_key_exchange(hs);
1621 break;
1622 case state_read_client_certificate_verify:
1623 ret = do_read_client_certificate_verify(hs);
1624 break;
1625 case state_read_change_cipher_spec:
1626 ret = do_read_change_cipher_spec(hs);
1627 break;
1628 case state_process_change_cipher_spec:
1629 ret = do_process_change_cipher_spec(hs);
1630 break;
1631 case state_read_next_proto:
1632 ret = do_read_next_proto(hs);
1633 break;
1634 case state_read_channel_id:
1635 ret = do_read_channel_id(hs);
1636 break;
1637 case state_read_client_finished:
1638 ret = do_read_client_finished(hs);
1639 break;
1640 case state_send_server_finished:
1641 ret = do_send_server_finished(hs);
1642 break;
1643 case state_finish_server_handshake:
1644 ret = do_finish_server_handshake(hs);
1645 break;
1646 case state_done:
1647 ret = ssl_hs_ok;
1648 break;
1649 }
1650
1651 if (hs->state != state) {
1652 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1653 }
1654
1655 if (ret != ssl_hs_ok) {
1656 return ret;
1657 }
1658 }
1659
1660 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1661 return ssl_hs_ok;
1662}
1663
1664const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
1665 enum ssl_server_hs_state_t state =
1666 static_cast<enum ssl_server_hs_state_t>(hs->state);
1667 switch (state) {
1668 case state_start_accept:
1669 return "TLS server start_accept";
1670 case state_read_client_hello:
1671 return "TLS server read_client_hello";
1672 case state_select_certificate:
1673 return "TLS server select_certificate";
1674 case state_tls13:
1675 return tls13_server_handshake_state(hs);
1676 case state_select_parameters:
1677 return "TLS server select_parameters";
1678 case state_send_server_hello:
1679 return "TLS server send_server_hello";
1680 case state_send_server_certificate:
1681 return "TLS server send_server_certificate";
1682 case state_send_server_key_exchange:
1683 return "TLS server send_server_key_exchange";
1684 case state_send_server_hello_done:
1685 return "TLS server send_server_hello_done";
1686 case state_read_client_certificate:
1687 return "TLS server read_client_certificate";
1688 case state_verify_client_certificate:
1689 return "TLS server verify_client_certificate";
1690 case state_read_client_key_exchange:
1691 return "TLS server read_client_key_exchange";
1692 case state_read_client_certificate_verify:
1693 return "TLS server read_client_certificate_verify";
1694 case state_read_change_cipher_spec:
1695 return "TLS server read_change_cipher_spec";
1696 case state_process_change_cipher_spec:
1697 return "TLS server process_change_cipher_spec";
1698 case state_read_next_proto:
1699 return "TLS server read_next_proto";
1700 case state_read_channel_id:
1701 return "TLS server read_channel_id";
1702 case state_read_client_finished:
1703 return "TLS server read_client_finished";
1704 case state_send_server_finished:
1705 return "TLS server send_server_finished";
1706 case state_finish_server_handshake:
1707 return "TLS server finish_server_handshake";
1708 case state_done:
1709 return "TLS server done";
1710 }
1711
1712 return "TLS server unknown";
1713}
1714
1715}