blob: b1df9f97cce357b1676560d388bfb49e53f8c205 [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;
David Benjamin37af90f2017-07-29 01:42:16 -0400583 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
584 &client_hello)) {
David Benjamin34202b92016-11-16 19:07:53 +0900585 case ssl_session_success:
586 break;
587 case ssl_session_error:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400588 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900589 case ssl_session_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400590 return ssl_hs_pending_session;
Adam Langley4c341d02017-03-08 19:33:21 -0800591 case ssl_session_ticket_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400592 return ssl_hs_pending_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900593 }
594
David Benjamind781fc42017-07-12 16:25:57 -0400595 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500596 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900597 /* A ClientHello without EMS that attempts to resume a session with EMS
598 * is fatal to the connection. */
David Benjamin34202b92016-11-16 19:07:53 +0900599 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400600 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400601 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900602 }
603
David Benjamind781fc42017-07-12 16:25:57 -0400604 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjamin34202b92016-11-16 19:07:53 +0900605 /* If the client offers the EMS extension, but the previous session
606 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500607 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400608 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900609 }
610 }
611
David Benjamind781fc42017-07-12 16:25:57 -0400612 if (session) {
David Benjamin34202b92016-11-16 19:07:53 +0900613 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900614 hs->ticket_expected = renew_ticket;
David Benjamind781fc42017-07-12 16:25:57 -0400615 ssl->session = session.release();
David Benjamin34202b92016-11-16 19:07:53 +0900616 ssl->s3->session_reused = 1;
617 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900618 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900619 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900620 if (!ssl_get_new_session(hs, 1 /* server */)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400621 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900622 }
623
624 /* Clear the session ID if we want the session to be single-use. */
625 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500626 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900627 }
628 }
629
630 if (ssl->ctx->dos_protection_cb != NULL &&
631 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
632 /* Connection rejected for DOS reasons. */
David Benjamin34202b92016-11-16 19:07:53 +0900633 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400634 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400635 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900636 }
637
David Benjaminf01f42a2016-11-16 19:05:33 +0900638 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500639 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400640
David Benjamin4eb95cc2016-11-16 17:08:23 +0900641 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900642 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500643 OPENSSL_free(hs->new_session->tlsext_hostname);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400644 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
David Benjamin45738dd2017-02-09 20:01:26 -0500645 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamind98107b2017-06-15 22:56:37 -0400646 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400647 return ssl_hs_error;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900648 }
649 }
650
David Benjamin5c1ce292015-05-26 16:59:43 -0400651 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900652 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400653 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500654 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
655 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900656 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400657 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400658 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500659 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900660 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400661 }
Adam Langley37646832016-08-01 16:16:46 -0700662
David Benjaminc3c88822016-11-14 10:32:04 +0900663 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700664 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400665 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500666 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700667 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 }
669
David Benjaminf01f42a2016-11-16 19:05:33 +0900670 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
671 * deferred. Complete it now. */
David Benjamind98107b2017-06-15 22:56:37 -0400672 uint8_t alert = SSL_AD_DECODE_ERROR;
673 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
674 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400675 return ssl_hs_error;
David Benjamin9ef31f02016-10-31 18:01:13 -0400676 }
677
David Benjaminf71036e2017-01-21 14:49:39 -0500678 /* Now that all parameters are known, initialize the handshake hash and hash
679 * the ClientHello. */
David Benjaminca9e8f52017-08-09 15:02:34 -0400680 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400681 !ssl_hash_message(hs, msg)) {
David Benjamind98107b2017-06-15 22:56:37 -0400682 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400683 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400684 }
685
Steven Valdez2b8415e2016-06-30 13:27:23 -0400686 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +0900687 if (!hs->cert_request) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400688 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 }
690
David Benjamin8f94c312017-08-01 17:35:55 -0400691 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400692
693 hs->state = state_send_server_hello;
694 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800695}
Adam Langley95c29f32014-06-20 12:00:00 -0700696
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400697static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900698 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400699
700 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
701 * known attack while we fix ChannelID itself. */
702 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -0500703 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400704 ssl->s3->tlsext_channel_id_valid = 0;
705 }
706
707 /* If this is a resumption and the original handshake didn't support
708 * ChannelID then we didn't record the original handshake hashes in the
709 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -0400710 if (ssl->session != NULL &&
711 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400712 ssl->s3->tlsext_channel_id_valid = 0;
713 }
714
David Benjaminad8f5e12017-02-20 17:00:20 -0500715 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -0400716 ssl_get_current_time(ssl, &now);
717 ssl->s3->server_random[0] = now.tv_sec >> 24;
718 ssl->s3->server_random[1] = now.tv_sec >> 16;
719 ssl->s3->server_random[2] = now.tv_sec >> 8;
720 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400721 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400722 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -0400723 }
724
David Benjamin55108632016-08-11 22:01:18 -0400725 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
726 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400727
David Benjamin31b0c9b2017-07-20 14:49:15 -0400728 const SSL_SESSION *session = hs->new_session.get();
Steven Valdez87eab492016-06-27 16:34:59 -0400729 if (ssl->session != NULL) {
730 session = ssl->session;
731 }
732
David Benjamin1386aad2017-07-19 23:57:40 -0400733 ScopedCBB cbb;
734 CBB body, session_id;
735 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -0400736 !CBB_add_u16(&body, ssl->version) ||
737 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
738 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400739 !CBB_add_bytes(&session_id, session->session_id,
740 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500741 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -0400742 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500743 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400744 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -0400745 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400746 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -0400747 }
748
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400749 if (ssl->session != NULL) {
750 hs->state = state_send_server_finished;
751 } else {
752 hs->state = state_send_server_certificate;
753 }
754 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400757static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900758 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400759 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -0400760
David Benjamin44148742017-06-17 13:20:59 -0400761 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
762 if (!ssl_has_certificate(ssl)) {
763 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400764 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400765 }
David Benjamin32a66d52016-07-13 22:03:11 -0400766
David Benjamin44148742017-06-17 13:20:59 -0400767 if (!ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400768 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400769 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400770
David Benjamin44148742017-06-17 13:20:59 -0400771 if (hs->certificate_status_expected) {
772 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -0400773 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -0400774 SSL3_MT_CERTIFICATE_STATUS) ||
775 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
776 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
777 !CBB_add_bytes(&ocsp_response,
778 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
779 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamind781fc42017-07-12 16:25:57 -0400780 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -0400781 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400782 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400783 }
David Benjamin0d1730d2017-06-15 23:24:25 -0400784 }
David Benjamin75836432016-06-17 18:48:29 -0400785 }
786
David Benjamin44148742017-06-17 13:20:59 -0400787 /* Assemble ServerKeyExchange parameters if needed. */
788 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
789 uint32_t alg_a = hs->new_cipher->algorithm_auth;
790 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
791 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100792
David Benjamin44148742017-06-17 13:20:59 -0400793 /* Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
794 * the client and server randoms for the signing transcript. */
795 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -0400796 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
797 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
798 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400799 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400800 }
801
David Benjamin2a0b3912015-12-18 01:01:21 -0500802 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500804 size_t len =
805 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
David Benjamind781fc42017-07-12 16:25:57 -0400806 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin2a0b3912015-12-18 01:01:21 -0500807 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
808 len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400809 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700813 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -0400814 /* Determine the group to use. */
815 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900816 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -0400817 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -0500818 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400819 return ssl_hs_error;
David Benjamind781fc42017-07-12 16:25:57 -0400820 }
David Benjamin45738dd2017-02-09 20:01:26 -0500821 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700822
David Benjamin4298d772015-12-19 00:18:25 -0500823 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc642aca2017-07-19 23:28:43 -0400824 hs->key_share = SSLKeyShare::Create(group_id);
825 if (!hs->key_share ||
David Benjamind781fc42017-07-12 16:25:57 -0400826 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
827 !CBB_add_u16(cbb.get(), group_id) ||
828 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc642aca2017-07-19 23:28:43 -0400829 !hs->key_share->Offer(&child)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400830 return ssl_hs_error;
Matt Braithwaite053931e2016-05-25 12:06:05 -0700831 }
David Benjamin2a0b3912015-12-18 01:01:21 -0500832 } else {
833 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
David Benjamind781fc42017-07-12 16:25:57 -0400836 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400837 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400838 }
David Benjaminc42acee2016-06-17 17:47:58 -0400839 }
840
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400841 hs->state = state_send_server_key_exchange;
842 return ssl_hs_ok;
David Benjamin44148742017-06-17 13:20:59 -0400843}
844
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400845static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin44148742017-06-17 13:20:59 -0400846 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400847
848 if (hs->server_params_len == 0) {
849 hs->state = state_send_server_hello_done;
850 return ssl_hs_ok;
851 }
852
David Benjamin1386aad2017-07-19 23:57:40 -0400853 ScopedCBB cbb;
854 CBB body, child;
855 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -0400856 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin44148742017-06-17 13:20:59 -0400857 /* |hs->server_params| contains a prefix for signing. */
858 hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
859 !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
860 hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400861 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjamin2a0b3912015-12-18 01:01:21 -0500864 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -0500865 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500866 if (!ssl_has_private_key(ssl)) {
867 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400868 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870
David Benjaminc42acee2016-06-17 17:47:58 -0400871 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -0700872 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900873 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400874 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -0700875 }
David Benjaminc42acee2016-06-17 17:47:58 -0400876 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400877 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -0400878 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
879 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400880 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400881 }
David Benjaminc42acee2016-06-17 17:47:58 -0400882 }
883
884 /* Add space for the signature. */
David Benjamin31b0c9b2017-07-20 14:49:15 -0400885 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -0400886 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -0400887 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -0400888 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400889 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400890 }
891
David Benjamin2a0b3912015-12-18 01:01:21 -0500892 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -0400893 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
894 signature_algorithm, hs->server_params,
895 hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500896 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -0500897 if (!CBB_did_write(&child, sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400898 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500899 }
900 break;
901 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400902 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500903 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400904 return ssl_hs_private_key_operation;
David Benjamin2a0b3912015-12-18 01:01:21 -0500905 }
David Benjamin1f9f9c42015-08-28 16:17:59 -0400906 }
907
David Benjamin1386aad2017-07-19 23:57:40 -0400908 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400909 return ssl_hs_error;
David Benjamin1f9f9c42015-08-28 16:17:59 -0400910 }
David Benjaminc42acee2016-06-17 17:47:58 -0400911
David Benjaminc3c88822016-11-14 10:32:04 +0900912 OPENSSL_free(hs->server_params);
913 hs->server_params = NULL;
914 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -0400915
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400916 hs->state = state_send_server_hello_done;
917 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800918}
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400920static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900921 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400922
David Benjamin1386aad2017-07-19 23:57:40 -0400923 ScopedCBB cbb;
924 CBB body;
David Benjamin75836432016-06-17 18:48:29 -0400925
David Benjamin0d1730d2017-06-15 23:24:25 -0400926 if (hs->cert_request) {
927 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -0400928 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -0400929 SSL3_MT_CERTIFICATE_REQUEST) ||
930 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
931 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
932 (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
933 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
934 (ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
935 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
936 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
937 !ssl_add_client_CA_list(ssl, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400938 !ssl_add_message_cbb(ssl, cbb.get())) {
939 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400940 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -0400941 }
942 }
943
David Benjamin1386aad2017-07-19 23:57:40 -0400944 if (!ssl->method->init_message(ssl, cbb.get(), &body,
945 SSL3_MT_SERVER_HELLO_DONE) ||
946 !ssl_add_message_cbb(ssl, cbb.get())) {
947 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400948 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -0400949 }
950
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400951 hs->state = state_read_client_certificate;
952 return ssl_hs_flush;
Adam Langleyfcf25832014-12-18 17:42:32 -0800953}
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400955static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900956 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400957
958 if (!hs->cert_request) {
959 hs->state = state_verify_client_certificate;
960 return ssl_hs_ok;
961 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400962
David Benjamin7934f082017-08-01 16:32:25 -0400963 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400964 if (!ssl->method->get_message(ssl, &msg)) {
965 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400966 }
967
David Benjamin7934f082017-08-01 16:32:25 -0400968 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400969 if (ssl->version == SSL3_VERSION &&
David Benjamin7934f082017-08-01 16:32:25 -0400970 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -0700971 /* In SSL 3.0, the Certificate message is omitted to signal no
972 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -0400973 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400974 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -0400975 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400976 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400977 }
978
Adam Langley37646832016-08-01 16:16:46 -0700979 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400980 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500981 hs->new_session->verify_result = X509_V_OK;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400982 hs->state = state_verify_client_certificate;
983 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400984 }
985
David Benjamin9f1dc822016-06-07 17:03:46 -0400986 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -0400987 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400988 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400989 }
990
David Benjamin7934f082017-08-01 16:32:25 -0400991 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400992 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500993 }
994
David Benjamin7934f082017-08-01 16:32:25 -0400995 CBS certificate_msg = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800996 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400997 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
998 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
999 ssl->retain_only_sha256_of_client_certs
1000 ? hs->new_session->peer_sha256
1001 : NULL,
1002 &certificate_msg, ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001003 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001004 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001005 }
David Benjamine664a532017-07-20 20:19:36 -04001006 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1007 hs->new_session->certs = chain.release();
David Benjamin9f1dc822016-06-07 17:03:46 -04001008
Adam Langley68e71242016-12-12 11:06:16 -08001009 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001010 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001012 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001013 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001014 }
1015
David Benjamin45738dd2017-02-09 20:01:26 -05001016 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001017 /* No client certificate so the handshake buffer may be discarded. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001018 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001019
David Benjamine455e512016-08-01 20:11:13 -04001020 /* In SSL 3.0, sending no certificate is signaled by omitting the
1021 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001022 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001023 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001024 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001025 return ssl_hs_error;
David Benjamine455e512016-08-01 20:11:13 -04001026 }
1027
1028 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001029 /* Fail for TLS only if we required a certificate */
1030 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001031 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001032 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001033 }
Adam Langley37646832016-08-01 16:16:46 -07001034
1035 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001036 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001037 hs->new_session->verify_result = X509_V_OK;
David Benjamin8f94c312017-08-01 17:35:55 -04001038 } else if (ssl->retain_only_sha256_of_client_certs) {
1039 /* The hash will have been filled in. */
David Benjamin45738dd2017-02-09 20:01:26 -05001040 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001041 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001042
David Benjamin8f94c312017-08-01 17:35:55 -04001043 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001044 hs->state = state_verify_client_certificate;
1045 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001046}
1047
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001048static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
1049 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
1050 switch (ssl_verify_peer_cert(hs)) {
1051 case ssl_verify_ok:
1052 break;
1053 case ssl_verify_invalid:
1054 return ssl_hs_error;
1055 case ssl_verify_retry:
1056 return ssl_hs_certificate_verify;
1057 }
1058 }
1059
1060 hs->state = state_read_client_key_exchange;
1061 return ssl_hs_ok;
1062}
1063
1064static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001065 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001066
1067 ssl_hs_wait_t ret = ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001068 uint8_t *premaster_secret = NULL;
1069 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001071
David Benjamin7934f082017-08-01 16:32:25 -04001072 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001073 if (!ssl->method->get_message(ssl, &msg)) {
1074 return ssl_hs_read_message;
David Benjamin44148742017-06-17 13:20:59 -04001075 }
David Benjamin276b7e82017-01-21 14:13:39 -05001076
David Benjamin7934f082017-08-01 16:32:25 -04001077 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001078 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 }
Adam Langley95c29f32014-06-20 12:00:00 -07001080
David Benjamin7934f082017-08-01 16:32:25 -04001081 CBS client_key_exchange = msg.body;
David Benjamin44148742017-06-17 13:20:59 -04001082 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1083 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001084
David Benjamin44148742017-06-17 13:20:59 -04001085 /* If using a PSK key exchange, parse the PSK identity. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 if (alg_a & SSL_aPSK) {
1087 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1090 * then this is the only field in the message. */
1091 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1092 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001093 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001094 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1095 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001097
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1099 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001100 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001101 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1102 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 }
David Benjamin35c02302014-07-13 04:14:59 -04001104
David Benjamin45738dd2017-02-09 20:01:26 -05001105 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001106 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001107 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1108 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 /* Depending on the key exchange method, compute |premaster_secret| and
1113 * |premaster_secret_len|. */
1114 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001115 CBS encrypted_premaster_secret;
1116 if (ssl->version > SSL3_VERSION) {
1117 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1118 &encrypted_premaster_secret) ||
1119 CBS_len(&client_key_exchange) != 0) {
1120 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1121 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1122 goto err;
1123 }
1124 } else {
1125 encrypted_premaster_secret = client_key_exchange;
1126 }
1127
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001129 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamind781fc42017-07-12 16:25:57 -04001130 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001131 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001132 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 goto err;
1134 }
David Benjamin35c02302014-07-13 04:14:59 -04001135
David Benjamin44148742017-06-17 13:20:59 -04001136 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1137 * timing-sensitive code below. */
David Benjamin3f5b43d2015-12-01 19:31:24 -05001138 size_t decrypt_len;
David Benjamin44148742017-06-17 13:20:59 -04001139 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1140 CBS_data(&encrypted_premaster_secret),
1141 CBS_len(&encrypted_premaster_secret))) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001142 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001143 break;
1144 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001145 goto err;
1146 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001147 ret = ssl_hs_private_key_operation;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001148 goto err;
1149 }
1150
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001151 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001152 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001153 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1154 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001155 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001156
David Benjamin3f5b43d2015-12-01 19:31:24 -05001157 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1158 * section 7.4.7.1. */
1159 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001160 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001162 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 goto err;
1164 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001165 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001166 goto err;
1167 }
1168
David Benjamin3f5b43d2015-12-01 19:31:24 -05001169 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1170 * publicly invalid. */
1171 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001172 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001173 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1174 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 }
David Benjamin35c02302014-07-13 04:14:59 -04001176
David Benjamin3f5b43d2015-12-01 19:31:24 -05001177 /* Check the padding. See RFC 3447, section 7.2.2. */
1178 size_t padding_len = decrypt_len - premaster_secret_len;
1179 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1180 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001181 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001182 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1183 }
1184 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1185
1186 /* The premaster secret must begin with |client_version|. This too must be
1187 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1188 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001189 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001190 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001191 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001192
1193 /* Select, in constant time, either the decrypted premaster or the random
1194 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001195 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001196 premaster_secret[i] = constant_time_select_8(
1197 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1198 }
1199
1200 OPENSSL_free(decrypt_buf);
1201 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001202 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001203 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001204 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001205 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001206 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001207 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001208 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1209 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001211
David Benjamin4298d772015-12-19 00:18:25 -05001212 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001213 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001214 if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
1215 CBS_data(&peer_key), CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001216 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1217 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 }
Adam Langley95c29f32014-06-20 12:00:00 -07001219
David Benjamin4298d772015-12-19 00:18:25 -05001220 /* The key exchange state may now be discarded. */
David Benjaminc642aca2017-07-19 23:28:43 -04001221 hs->key_share.reset();
David Benjamin44148742017-06-17 13:20:59 -04001222 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001223 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001224 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1225 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001226 }
Adam Langley95c29f32014-06-20 12:00:00 -07001227
Adam Langleyfcf25832014-12-18 17:42:32 -08001228 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1229 * pre-shared key. */
1230 if (alg_a & SSL_aPSK) {
David Benjamin44148742017-06-17 13:20:59 -04001231 if (ssl->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001232 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin44148742017-06-17 13:20:59 -04001233 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1234 goto err;
1235 }
1236
1237 /* Look up the key for the identity. */
1238 uint8_t psk[PSK_MAX_PSK_LEN];
1239 unsigned psk_len = ssl->psk_server_callback(
1240 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1241 if (psk_len > PSK_MAX_PSK_LEN) {
1242 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1243 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1244 goto err;
1245 } else if (psk_len == 0) {
1246 /* PSK related to the given identity not found */
1247 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1248 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1249 goto err;
1250 }
1251
1252 if (alg_k & SSL_kPSK) {
1253 /* In plain PSK, other_secret is a block of 0s with the same length as the
1254 * pre-shared key. */
1255 premaster_secret_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001256 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
David Benjamin44148742017-06-17 13:20:59 -04001257 if (premaster_secret == NULL) {
1258 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1259 goto err;
1260 }
1261 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
1262 }
1263
David Benjamin1386aad2017-07-19 23:57:40 -04001264 ScopedCBB new_premaster;
1265 CBB child;
Adam Langleyfcf25832014-12-18 17:42:32 -08001266 uint8_t *new_data;
1267 size_t new_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001268 if (!CBB_init(new_premaster.get(),
1269 2 + psk_len + 2 + premaster_secret_len) ||
1270 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001272 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001274 !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001275 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001276 goto err;
1277 }
David Benjamin14c83e72014-07-13 04:54:57 -04001278
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1280 OPENSSL_free(premaster_secret);
1281 premaster_secret = new_data;
1282 premaster_secret_len = new_len;
1283 }
David Benjamin14c83e72014-07-13 04:54:57 -04001284
David Benjamin7934f082017-08-01 16:32:25 -04001285 if (!ssl_hash_message(hs, msg)) {
David Benjamin44148742017-06-17 13:20:59 -04001286 goto err;
1287 }
1288
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001290 hs->new_session->master_key_length = tls1_generate_master_secret(
1291 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1292 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001293 goto err;
1294 }
David Benjaminfc02b592017-02-17 16:26:01 -05001295 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001296
David Benjamin8f94c312017-08-01 17:35:55 -04001297 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001298 hs->state = state_read_client_certificate_verify;
1299 ret = ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001300
Adam Langley95c29f32014-06-20 12:00:00 -07001301err:
David Benjamin4298d772015-12-19 00:18:25 -05001302 if (premaster_secret != NULL) {
1303 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001304 OPENSSL_free(premaster_secret);
1305 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001306 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001307
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001308 return ret;
1309
Adam Langleyfcf25832014-12-18 17:42:32 -08001310}
Adam Langley95c29f32014-06-20 12:00:00 -07001311
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001312static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001313 SSL *const ssl = hs->ssl;
David Benjamin6553b372014-07-22 14:11:30 -04001314
Adam Langleyfcf25832014-12-18 17:42:32 -08001315 /* Only RSA and ECDSA client certificates are supported, so a
1316 * CertificateVerify is required if and only if there's a client certificate.
1317 * */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001318 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001319 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001320 hs->state = state_read_change_cipher_spec;
1321 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 }
Adam Langley95c29f32014-06-20 12:00:00 -07001323
David Benjamin7934f082017-08-01 16:32:25 -04001324 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001325 if (!ssl->method->get_message(ssl, &msg)) {
1326 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 }
David Benjaminef865502014-08-24 02:48:34 -04001328
David Benjamin7934f082017-08-01 16:32:25 -04001329 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001330 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001331 }
1332
David Benjamin7934f082017-08-01 16:32:25 -04001333 CBS certificate_verify = msg.body, signature;
David Benjamin6897dbe2014-07-12 20:18:28 -04001334
David Benjamin7934f082017-08-01 16:32:25 -04001335 /* Determine the signature algorithm. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001336 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001337 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001338 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001339 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001340 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001341 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001342 }
David Benjamin8d606e32017-06-15 22:43:04 -04001343 uint8_t alert = SSL_AD_DECODE_ERROR;
1344 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001345 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001346 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001347 }
David Benjamin45738dd2017-02-09 20:01:26 -05001348 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001349 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001350 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001351 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001352 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001353 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001354 }
David Benjamin854dd652014-08-26 00:32:30 -04001355
Adam Langleyfcf25832014-12-18 17:42:32 -08001356 /* Parse and verify the signature. */
1357 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1358 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001359 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001360 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001361 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001362 }
Adam Langley95c29f32014-06-20 12:00:00 -07001363
Steven Valdez2b8415e2016-06-30 13:27:23 -04001364 int sig_ok;
1365 /* The SSL3 construction for CertificateVerify does not decompose into a
1366 * single final digest and signature, and must be special-cased. */
1367 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001368 uint8_t digest[EVP_MAX_MD_SIZE];
1369 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001370 if (!hs->transcript.GetSSL3CertVerifyHash(
1371 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001372 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001373 }
1374
David Benjamin31b0c9b2017-07-20 14:49:15 -04001375 UniquePtr<EVP_PKEY_CTX> pctx(
1376 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
David Benjamin1386aad2017-07-19 23:57:40 -04001377 sig_ok = pctx &&
1378 EVP_PKEY_verify_init(pctx.get()) &&
1379 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1380 CBS_len(&signature), digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001381 } else {
1382 sig_ok = ssl_public_key_verify(
1383 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin6dc8bf62017-07-19 16:38:21 -04001384 hs->peer_pubkey.get(), hs->transcript.buffer_data(),
1385 hs->transcript.buffer_len());
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001387
David Benjaminbf82aed2016-03-01 22:57:40 -05001388#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1389 sig_ok = 1;
1390 ERR_clear_error();
1391#endif
1392 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001393 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001394 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001395 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001396 }
1397
Steven Valdez2b8415e2016-06-30 13:27:23 -04001398 /* The handshake buffer is no longer necessary, and we may hash the current
1399 * message.*/
David Benjamin6dc8bf62017-07-19 16:38:21 -04001400 hs->transcript.FreeBuffer();
David Benjamin7934f082017-08-01 16:32:25 -04001401 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001402 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001403 }
1404
David Benjamin8f94c312017-08-01 17:35:55 -04001405 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001406 hs->state = state_read_change_cipher_spec;
1407 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001408}
Adam Langley95c29f32014-06-20 12:00:00 -07001409
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001410static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
1411 hs->state = state_process_change_cipher_spec;
1412 return ssl_hs_read_change_cipher_spec;
1413}
1414
1415static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1416 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
1417 return ssl_hs_error;
1418 }
1419
1420 hs->state = state_read_next_proto;
1421 return ssl_hs_ok;
1422}
1423
1424static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001425 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001426
1427 if (!hs->next_proto_neg_seen) {
1428 hs->state = state_read_channel_id;
1429 return ssl_hs_ok;
1430 }
1431
David Benjamin7934f082017-08-01 16:32:25 -04001432 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001433 if (!ssl->method->get_message(ssl, &msg)) {
1434 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 }
Adam Langley95c29f32014-06-20 12:00:00 -07001436
David Benjamin7934f082017-08-01 16:32:25 -04001437 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1438 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001439 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001440 }
1441
David Benjamin7934f082017-08-01 16:32:25 -04001442 CBS next_protocol = msg.body, selected_protocol, padding;
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1444 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001445 CBS_len(&next_protocol) != 0) {
1446 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1447 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001448 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001449 }
1450
1451 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001452 &ssl->s3->next_proto_negotiated_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001453 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001454 }
Adam Langley95c29f32014-06-20 12:00:00 -07001455
David Benjamin8f94c312017-08-01 17:35:55 -04001456 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001457 hs->state = state_read_channel_id;
1458 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001459}
Adam Langley95c29f32014-06-20 12:00:00 -07001460
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001461static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001462 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001463
1464 if (!ssl->s3->tlsext_channel_id_valid) {
1465 hs->state = state_read_client_finished;
1466 return ssl_hs_ok;
1467 }
1468
David Benjamin7934f082017-08-01 16:32:25 -04001469 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001470 if (!ssl->method->get_message(ssl, &msg)) {
1471 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001473
David Benjamin7934f082017-08-01 16:32:25 -04001474 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1475 !tls1_verify_channel_id(hs, msg) ||
1476 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001477 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001479
David Benjamin8f94c312017-08-01 17:35:55 -04001480 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001481 hs->state = state_read_client_finished;
1482 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001483}
David Benjamin9f1dc822016-06-07 17:03:46 -04001484
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001485static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1486 SSL *const ssl = hs->ssl;
1487 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1488 if (wait != ssl_hs_ok) {
1489 return wait;
1490 }
1491
1492 if (ssl->session != NULL) {
1493 hs->state = state_finish_server_handshake;
1494 } else {
1495 hs->state = state_send_server_finished;
1496 }
1497
1498 /* If this is a full handshake with ChannelID then record the handshake
1499 * hashes in |hs->new_session| in case we need them to verify a
1500 * ChannelID signature on a resumption of this session in the future. */
1501 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
1502 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1503 return ssl_hs_error;
1504 }
1505
1506 return ssl_hs_ok;
1507}
1508
1509static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001510 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001511
1512 if (hs->ticket_expected) {
1513 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001514 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001515 if (ssl->session == NULL) {
1516 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001517 ssl_session_rebase_time(ssl, hs->new_session.get());
1518 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001519 } else {
1520 /* We are renewing an existing session. Duplicate the session to adjust
1521 * the timeout. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001522 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001523 if (!session_copy) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001524 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001525 }
1526
David Benjamin1386aad2017-07-19 23:57:40 -04001527 ssl_session_rebase_time(ssl, session_copy.get());
1528 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001529 }
1530
David Benjamin1386aad2017-07-19 23:57:40 -04001531 ScopedCBB cbb;
1532 CBB body, ticket;
1533 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1534 SSL3_MT_NEW_SESSION_TICKET) ||
1535 !CBB_add_u32(&body, session->timeout) ||
1536 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1537 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1538 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001539 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001540 }
David Benjamin123db572016-11-03 16:59:25 -04001541 }
1542
David Benjaminb5f55c32017-06-15 23:15:15 -04001543 if (!ssl->method->add_change_cipher_spec(ssl) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001544 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE) ||
1545 !ssl3_send_finished(hs)) {
1546 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001547 }
1548
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001549 if (ssl->session != NULL) {
1550 hs->state = state_read_change_cipher_spec;
1551 } else {
1552 hs->state = state_finish_server_handshake;
1553 }
1554 return ssl_hs_flush;
David Benjamin9f1dc822016-06-07 17:03:46 -04001555}
David Benjamin86e95b82017-07-18 16:34:25 -04001556
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001557static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1558 SSL *const ssl = hs->ssl;
1559
1560 ssl->method->on_handshake_complete(ssl);
1561
1562 /* If we aren't retaining peer certificates then we can discard it
1563 * now. */
1564 if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
1565 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1566 hs->new_session->certs = NULL;
1567 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1568 }
1569
1570 SSL_SESSION_free(ssl->s3->established_session);
1571 if (ssl->session != NULL) {
1572 SSL_SESSION_up_ref(ssl->session);
1573 ssl->s3->established_session = ssl->session;
1574 } else {
1575 ssl->s3->established_session = hs->new_session.release();
1576 ssl->s3->established_session->not_resumable = 0;
1577 }
1578
1579 ssl->s3->initial_handshake_complete = 1;
1580 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1581
1582 hs->state = state_done;
1583 return ssl_hs_ok;
1584}
1585
1586enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
1587 while (hs->state != state_done) {
1588 enum ssl_hs_wait_t ret = ssl_hs_error;
1589 enum ssl_server_hs_state_t state =
1590 static_cast<enum ssl_server_hs_state_t>(hs->state);
1591 switch (state) {
1592 case state_start_accept:
1593 ret = do_start_accept(hs);
1594 break;
1595 case state_read_client_hello:
1596 ret = do_read_client_hello(hs);
1597 break;
1598 case state_select_certificate:
1599 ret = do_select_certificate(hs);
1600 break;
1601 case state_tls13:
1602 ret = do_tls13(hs);
1603 break;
1604 case state_select_parameters:
1605 ret = do_select_parameters(hs);
1606 break;
1607 case state_send_server_hello:
1608 ret = do_send_server_hello(hs);
1609 break;
1610 case state_send_server_certificate:
1611 ret = do_send_server_certificate(hs);
1612 break;
1613 case state_send_server_key_exchange:
1614 ret = do_send_server_key_exchange(hs);
1615 break;
1616 case state_send_server_hello_done:
1617 ret = do_send_server_hello_done(hs);
1618 break;
1619 case state_read_client_certificate:
1620 ret = do_read_client_certificate(hs);
1621 break;
1622 case state_verify_client_certificate:
1623 ret = do_verify_client_certificate(hs);
1624 break;
1625 case state_read_client_key_exchange:
1626 ret = do_read_client_key_exchange(hs);
1627 break;
1628 case state_read_client_certificate_verify:
1629 ret = do_read_client_certificate_verify(hs);
1630 break;
1631 case state_read_change_cipher_spec:
1632 ret = do_read_change_cipher_spec(hs);
1633 break;
1634 case state_process_change_cipher_spec:
1635 ret = do_process_change_cipher_spec(hs);
1636 break;
1637 case state_read_next_proto:
1638 ret = do_read_next_proto(hs);
1639 break;
1640 case state_read_channel_id:
1641 ret = do_read_channel_id(hs);
1642 break;
1643 case state_read_client_finished:
1644 ret = do_read_client_finished(hs);
1645 break;
1646 case state_send_server_finished:
1647 ret = do_send_server_finished(hs);
1648 break;
1649 case state_finish_server_handshake:
1650 ret = do_finish_server_handshake(hs);
1651 break;
1652 case state_done:
1653 ret = ssl_hs_ok;
1654 break;
1655 }
1656
1657 if (hs->state != state) {
1658 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1659 }
1660
1661 if (ret != ssl_hs_ok) {
1662 return ret;
1663 }
1664 }
1665
1666 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1667 return ssl_hs_ok;
1668}
1669
1670const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
1671 enum ssl_server_hs_state_t state =
1672 static_cast<enum ssl_server_hs_state_t>(hs->state);
1673 switch (state) {
1674 case state_start_accept:
1675 return "TLS server start_accept";
1676 case state_read_client_hello:
1677 return "TLS server read_client_hello";
1678 case state_select_certificate:
1679 return "TLS server select_certificate";
1680 case state_tls13:
1681 return tls13_server_handshake_state(hs);
1682 case state_select_parameters:
1683 return "TLS server select_parameters";
1684 case state_send_server_hello:
1685 return "TLS server send_server_hello";
1686 case state_send_server_certificate:
1687 return "TLS server send_server_certificate";
1688 case state_send_server_key_exchange:
1689 return "TLS server send_server_key_exchange";
1690 case state_send_server_hello_done:
1691 return "TLS server send_server_hello_done";
1692 case state_read_client_certificate:
1693 return "TLS server read_client_certificate";
1694 case state_verify_client_certificate:
1695 return "TLS server verify_client_certificate";
1696 case state_read_client_key_exchange:
1697 return "TLS server read_client_key_exchange";
1698 case state_read_client_certificate_verify:
1699 return "TLS server read_client_certificate_verify";
1700 case state_read_change_cipher_spec:
1701 return "TLS server read_change_cipher_spec";
1702 case state_process_change_cipher_spec:
1703 return "TLS server process_change_cipher_spec";
1704 case state_read_next_proto:
1705 return "TLS server read_next_proto";
1706 case state_read_channel_id:
1707 return "TLS server read_channel_id";
1708 case state_read_client_finished:
1709 return "TLS server read_client_finished";
1710 case state_send_server_finished:
1711 return "TLS server send_server_finished";
1712 case state_finish_server_handshake:
1713 return "TLS server finish_server_handshake";
1714 case state_done:
1715 return "TLS server done";
1716 }
1717
1718 return "TLS server unknown";
1719}
1720
1721}