blob: 10e618d1026c71ee94bf5039ba827b3244ba196f [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 {
David Benjaminc11ea9422017-08-29 16:33:21 -0400234 // Convert the ClientHello version to an equivalent supported_versions
235 // extension.
Steven Valdez8f36c512017-06-20 10:55:02 -0400236 static const uint8_t kTLSVersions[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400237 0x03, 0x03, // TLS 1.2
238 0x03, 0x02, // TLS 1.1
239 0x03, 0x01, // TLS 1
240 0x03, 0x00, // SSL 3
Steven Valdez8f36c512017-06-20 10:55:02 -0400241 };
242
243 static const uint8_t kDTLSVersions[] = {
David Benjaminc11ea9422017-08-29 16:33:21 -0400244 0xfe, 0xfd, // DTLS 1.2
245 0xfe, 0xff, // DTLS 1.0
Steven Valdez8f36c512017-06-20 10:55:02 -0400246 };
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 Benjaminc11ea9422017-08-29 16:33:21 -0400276 // At this point, the connection's version is known and |ssl->version| is
277 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400278 ssl->s3->have_version = true;
David Benjamin25fe85b2016-08-09 20:00:32 -0400279
David Benjaminc11ea9422017-08-29 16:33:21 -0400280 // Handle FALLBACK_SCSV.
Steven Valdez8f36c512017-06-20 10:55:02 -0400281 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400322// ssl_get_compatible_server_ciphers determines the key exchange and
323// authentication cipher suite masks compatible with the server configuration
324// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
325// exchange mask and |*out_mask_a| to the authentication mask.
David Benjamin2578b292016-12-03 23:19:55 -0500326static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
327 uint32_t *out_mask_k,
328 uint32_t *out_mask_a) {
329 SSL *const ssl = hs->ssl;
David 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 Benjaminc11ea9422017-08-29 16:33:21 -0400340 // Check for a shared group to consider ECDHE ciphers.
David Benjamin2578b292016-12-03 23:19:55 -0500341 uint16_t unused;
342 if (tls1_get_shared_group(hs, &unused)) {
343 mask_k |= SSL_kECDHE;
344 }
345
David Benjaminc11ea9422017-08-29 16:33:21 -0400346 // PSK requires a server callback.
David Benjamin2578b292016-12-03 23:19:55 -0500347 if (ssl->psk_server_callback != NULL) {
348 mask_k |= SSL_kPSK;
349 mask_a |= SSL_aPSK;
350 }
351
352 *out_mask_k = mask_k;
353 *out_mask_a = mask_a;
354}
355
356static const SSL_CIPHER *ssl3_choose_cipher(
357 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
358 const struct ssl_cipher_preference_list_st *server_pref) {
359 SSL *const ssl = hs->ssl;
David Benjaminccbb1652017-03-22 18:24:32 -0400360 STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjaminc11ea9422017-08-29 16:33:21 -0400361 // in_group_flags will either be NULL, or will point to an array of bytes
362 // which indicate equal-preference groups in the |prio| stack. See the
363 // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
364 // struct.
David Benjamin2578b292016-12-03 23:19:55 -0500365 const uint8_t *in_group_flags;
David Benjaminc11ea9422017-08-29 16:33:21 -0400366 // group_min contains the minimal index so far found in a group, or -1 if no
367 // such value exists yet.
David Benjamin2578b292016-12-03 23:19:55 -0500368 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;
David Benjaminc11ea9422017-08-29 16:33:21 -0400393 if (// Check if the cipher is supported for the current version.
David Benjaminccbb1652017-03-22 18:24:32 -0400394 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
395 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400396 // Check the cipher is supported for the server configuration.
David Benjaminccbb1652017-03-22 18:24:32 -0400397 (c->algorithm_mkey & mask_k) &&
398 (c->algorithm_auth & mask_a) &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400399 // Check the cipher is in the |allow| list.
David Benjaminccbb1652017-03-22 18:24:32 -0400400 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) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400402 // This element of |prio| is in a group. Update the minimum index found
403 // so far and continue looking.
David Benjamin2578b292016-12-03 23:19:55 -0500404 if (group_min == -1 || (size_t)group_min > cipher_index) {
405 group_min = cipher_index;
406 }
407 } else {
408 if (group_min != -1 && (size_t)group_min < cipher_index) {
409 cipher_index = group_min;
410 }
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) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400416 // 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 Benjaminc11ea9422017-08-29 16:33:21 -0400450 // Run the early callback.
David Benjamin59bae5a2017-02-02 23:51:22 -0500451 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 Benjaminc11ea9422017-08-29 16:33:21 -0400457 // Connection rejected.
David Benjamin59bae5a2017-02-02 23:51:22 -0500458 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 Benjaminc11ea9422017-08-29 16:33:21 -0400467 // Freeze the version range after the early callback.
David Benjamin68161cb2017-06-20 14:49:43 -0400468 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400486 // Only null compression is supported. TLS 1.3 further requires the peer
487 // advertise no other compression.
David Benjamin59bae5a2017-02-02 23:51:22 -0500488 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400497 // TLS extensions.
David Benjamin59bae5a2017-02-02 23:51:22 -0500498 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 Benjaminc11ea9422017-08-29 16:33:21 -0400515 // Call |cert_cb| to update server certificates if required.
David Benjamin59bae5a2017-02-02 23:51:22 -0500516 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) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400533 // 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400543 // 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400580 // Determine whether we are doing session resumption.
David Benjamin37af90f2017-07-29 01:42:16 -0400581 UniquePtr<SSL_SESSION> session;
David Benjaminfd45ee72017-08-31 14:49:09 -0400582 bool tickets_supported = false, renew_ticket = false;
Steven Valdez398085b2017-08-29 13:25:56 -0400583 enum ssl_hs_wait_t wait = ssl_get_prev_session(
584 ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
585 if (wait != ssl_hs_ok) {
586 return wait;
David Benjamin34202b92016-11-16 19:07:53 +0900587 }
588
David Benjamind781fc42017-07-12 16:25:57 -0400589 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500590 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400591 // A ClientHello without EMS that attempts to resume a session with EMS
592 // is fatal to the connection.
David Benjamin34202b92016-11-16 19:07:53 +0900593 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400594 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400595 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900596 }
597
David Benjamind781fc42017-07-12 16:25:57 -0400598 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400599 // If the client offers the EMS extension, but the previous session
600 // didn't use it, then negotiate a new session.
David Benjaminfc02b592017-02-17 16:26:01 -0500601 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400602 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900603 }
604 }
605
David Benjamind781fc42017-07-12 16:25:57 -0400606 if (session) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400607 // Use the old session.
David Benjaminc3c88822016-11-14 10:32:04 +0900608 hs->ticket_expected = renew_ticket;
David Benjamind781fc42017-07-12 16:25:57 -0400609 ssl->session = session.release();
David Benjamin046bc1f2017-08-31 15:06:42 -0400610 ssl->s3->session_reused = true;
David Benjamin34202b92016-11-16 19:07:53 +0900611 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900612 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900613 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900614 if (!ssl_get_new_session(hs, 1 /* server */)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400615 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900616 }
617
David Benjaminc11ea9422017-08-29 16:33:21 -0400618 // Clear the session ID if we want the session to be single-use.
David Benjamin34202b92016-11-16 19:07:53 +0900619 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500620 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900621 }
622 }
623
624 if (ssl->ctx->dos_protection_cb != NULL &&
625 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400626 // Connection rejected for DOS reasons.
David Benjamin34202b92016-11-16 19:07:53 +0900627 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400628 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400629 return ssl_hs_error;
David Benjamin34202b92016-11-16 19:07:53 +0900630 }
631
David Benjaminf01f42a2016-11-16 19:05:33 +0900632 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500633 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400634
David Benjaminc11ea9422017-08-29 16:33:21 -0400635 // Determine whether to request a client certificate.
David Benjaminc3c88822016-11-14 10:32:04 +0900636 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjaminc11ea9422017-08-29 16:33:21 -0400637 // Only request a certificate if Channel ID isn't negotiated.
David Benjamin0d56f882015-12-19 17:05:56 -0500638 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
639 ssl->s3->tlsext_channel_id_valid) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400640 hs->cert_request = false;
David Benjamin5c1ce292015-05-26 16:59:43 -0400641 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400642 // CertificateRequest may only be sent in certificate-based ciphers.
David Benjamin45738dd2017-02-09 20:01:26 -0500643 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminfd45ee72017-08-31 14:49:09 -0400644 hs->cert_request = false;
David Benjamin5c1ce292015-05-26 16:59:43 -0400645 }
Adam Langley37646832016-08-01 16:16:46 -0700646
David Benjaminc3c88822016-11-14 10:32:04 +0900647 if (!hs->cert_request) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400648 // OpenSSL returns X509_V_OK when no certificates are requested. This is
649 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -0500650 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700651 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 }
653
David Benjaminc11ea9422017-08-29 16:33:21 -0400654 // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
655 // deferred. Complete it now.
David Benjamind98107b2017-06-15 22:56:37 -0400656 uint8_t alert = SSL_AD_DECODE_ERROR;
657 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
658 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400659 return ssl_hs_error;
David Benjamin9ef31f02016-10-31 18:01:13 -0400660 }
661
David Benjaminc11ea9422017-08-29 16:33:21 -0400662 // Now that all parameters are known, initialize the handshake hash and hash
663 // the ClientHello.
David Benjaminca9e8f52017-08-09 15:02:34 -0400664 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400665 !ssl_hash_message(hs, msg)) {
David Benjamind98107b2017-06-15 22:56:37 -0400666 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400667 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400668 }
669
David Benjaminc11ea9422017-08-29 16:33:21 -0400670 // Release the handshake buffer if client authentication isn't required.
David Benjaminc3c88822016-11-14 10:32:04 +0900671 if (!hs->cert_request) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400672 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 }
674
David Benjamin8f94c312017-08-01 17:35:55 -0400675 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400676
677 hs->state = state_send_server_hello;
678 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800679}
Adam Langley95c29f32014-06-20 12:00:00 -0700680
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400681static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900682 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400683
David Benjaminc11ea9422017-08-29 16:33:21 -0400684 // We only accept ChannelIDs on connections with ECDHE in order to avoid a
685 // known attack while we fix ChannelID itself.
David Benjamin56380462015-10-10 14:59:09 -0400686 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -0500687 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400688 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin56380462015-10-10 14:59:09 -0400689 }
690
David Benjaminc11ea9422017-08-29 16:33:21 -0400691 // If this is a resumption and the original handshake didn't support
692 // ChannelID then we didn't record the original handshake hashes in the
693 // session and so cannot resume with ChannelIDs.
Steven Valdez87eab492016-06-27 16:34:59 -0400694 if (ssl->session != NULL &&
695 ssl->session->original_handshake_hash_len == 0) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400696 ssl->s3->tlsext_channel_id_valid = false;
David Benjamin56380462015-10-10 14:59:09 -0400697 }
698
David Benjaminad8f5e12017-02-20 17:00:20 -0500699 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -0400700 ssl_get_current_time(ssl, &now);
701 ssl->s3->server_random[0] = now.tv_sec >> 24;
702 ssl->s3->server_random[1] = now.tv_sec >> 16;
703 ssl->s3->server_random[2] = now.tv_sec >> 8;
704 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400705 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400706 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -0400707 }
708
David Benjaminc11ea9422017-08-29 16:33:21 -0400709 // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
710 // 1.3 is finalized and we are not implementing a draft version.
David Benjamin1f61f0d2016-07-10 12:20:35 -0400711
David Benjamin31b0c9b2017-07-20 14:49:15 -0400712 const SSL_SESSION *session = hs->new_session.get();
Steven Valdez87eab492016-06-27 16:34:59 -0400713 if (ssl->session != NULL) {
714 session = ssl->session;
715 }
716
David Benjamin1386aad2017-07-19 23:57:40 -0400717 ScopedCBB cbb;
718 CBB body, session_id;
719 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -0400720 !CBB_add_u16(&body, ssl->version) ||
721 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
722 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400723 !CBB_add_bytes(&session_id, session->session_id,
724 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500725 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -0400726 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500727 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400728 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -0400729 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400730 return ssl_hs_error;
David Benjamin56380462015-10-10 14:59:09 -0400731 }
732
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400733 if (ssl->session != NULL) {
734 hs->state = state_send_server_finished;
735 } else {
736 hs->state = state_send_server_certificate;
737 }
738 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800739}
Adam Langley95c29f32014-06-20 12:00:00 -0700740
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400741static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900742 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400743 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -0400744
David Benjamin44148742017-06-17 13:20:59 -0400745 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
746 if (!ssl_has_certificate(ssl)) {
747 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400748 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400749 }
David Benjamin32a66d52016-07-13 22:03:11 -0400750
David Benjamin44148742017-06-17 13:20:59 -0400751 if (!ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400752 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400753 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400754
David Benjamin44148742017-06-17 13:20:59 -0400755 if (hs->certificate_status_expected) {
756 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -0400757 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -0400758 SSL3_MT_CERTIFICATE_STATUS) ||
759 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
760 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
761 !CBB_add_bytes(&ocsp_response,
762 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
763 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamind781fc42017-07-12 16:25:57 -0400764 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -0400765 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400766 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -0400767 }
David Benjamin0d1730d2017-06-15 23:24:25 -0400768 }
David Benjamin75836432016-06-17 18:48:29 -0400769 }
770
David Benjaminc11ea9422017-08-29 16:33:21 -0400771 // Assemble ServerKeyExchange parameters if needed.
David Benjamin44148742017-06-17 13:20:59 -0400772 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
773 uint32_t alg_a = hs->new_cipher->algorithm_auth;
774 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
775 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100776
David Benjaminc11ea9422017-08-29 16:33:21 -0400777 // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
778 // the client and server randoms for the signing transcript.
David Benjamin44148742017-06-17 13:20:59 -0400779 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -0400780 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
781 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
782 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400783 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400784 }
785
David Benjaminc11ea9422017-08-29 16:33:21 -0400786 // PSK ciphers begin with an identity hint.
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500788 size_t len =
789 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
David Benjamind781fc42017-07-12 16:25:57 -0400790 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin2a0b3912015-12-18 01:01:21 -0500791 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
792 len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400793 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 }
Adam Langley95c29f32014-06-20 12:00:00 -0700796
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700797 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400798 // Determine the group to use.
Steven Valdezce902a92016-05-17 11:47:53 -0400799 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900800 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -0400801 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -0500802 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400803 return ssl_hs_error;
David Benjamind781fc42017-07-12 16:25:57 -0400804 }
David Benjamin45738dd2017-02-09 20:01:26 -0500805 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -0700806
David Benjaminc11ea9422017-08-29 16:33:21 -0400807 // Set up ECDH, generate a key, and emit the public half.
David Benjaminc642aca2017-07-19 23:28:43 -0400808 hs->key_share = SSLKeyShare::Create(group_id);
809 if (!hs->key_share ||
David Benjamind781fc42017-07-12 16:25:57 -0400810 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
811 !CBB_add_u16(cbb.get(), group_id) ||
812 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc642aca2017-07-19 23:28:43 -0400813 !hs->key_share->Offer(&child)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400814 return ssl_hs_error;
Matt Braithwaite053931e2016-05-25 12:06:05 -0700815 }
David Benjamin2a0b3912015-12-18 01:01:21 -0500816 } else {
817 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 }
Adam Langley95c29f32014-06-20 12:00:00 -0700819
David Benjamind781fc42017-07-12 16:25:57 -0400820 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400821 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400822 }
David Benjaminc42acee2016-06-17 17:47:58 -0400823 }
824
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400825 hs->state = state_send_server_key_exchange;
826 return ssl_hs_ok;
David Benjamin44148742017-06-17 13:20:59 -0400827}
828
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400829static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjamin44148742017-06-17 13:20:59 -0400830 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400831
832 if (hs->server_params_len == 0) {
833 hs->state = state_send_server_hello_done;
834 return ssl_hs_ok;
835 }
836
David Benjamin1386aad2017-07-19 23:57:40 -0400837 ScopedCBB cbb;
838 CBB body, child;
839 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -0400840 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400841 // |hs->server_params| contains a prefix for signing.
David Benjamin44148742017-06-17 13:20:59 -0400842 hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
843 !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
844 hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400845 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500846 }
Adam Langley95c29f32014-06-20 12:00:00 -0700847
David Benjaminc11ea9422017-08-29 16:33:21 -0400848 // Add a signature.
David Benjamin45738dd2017-02-09 20:01:26 -0500849 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500850 if (!ssl_has_private_key(ssl)) {
851 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400852 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 }
Adam Langley95c29f32014-06-20 12:00:00 -0700854
David Benjaminc11ea9422017-08-29 16:33:21 -0400855 // Determine the signature algorithm.
David Benjaminea9a0d52016-07-08 15:52:59 -0700856 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900857 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400858 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -0700859 }
David Benjaminc42acee2016-06-17 17:47:58 -0400860 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400861 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -0400862 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
863 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400864 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400865 }
David Benjaminc42acee2016-06-17 17:47:58 -0400866 }
867
David Benjaminc11ea9422017-08-29 16:33:21 -0400868 // Add space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400869 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -0400870 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -0400871 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -0400872 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400873 return ssl_hs_error;
David Benjaminc42acee2016-06-17 17:47:58 -0400874 }
875
David Benjamin2a0b3912015-12-18 01:01:21 -0500876 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -0400877 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
878 signature_algorithm, hs->server_params,
879 hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -0500880 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -0500881 if (!CBB_did_write(&child, sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400882 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500883 }
884 break;
885 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400886 return ssl_hs_error;
David Benjamin2a0b3912015-12-18 01:01:21 -0500887 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400888 return ssl_hs_private_key_operation;
David Benjamin2a0b3912015-12-18 01:01:21 -0500889 }
David Benjamin1f9f9c42015-08-28 16:17:59 -0400890 }
891
David Benjamin1386aad2017-07-19 23:57:40 -0400892 if (!ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400893 return ssl_hs_error;
David Benjamin1f9f9c42015-08-28 16:17:59 -0400894 }
David Benjaminc42acee2016-06-17 17:47:58 -0400895
David Benjaminc3c88822016-11-14 10:32:04 +0900896 OPENSSL_free(hs->server_params);
897 hs->server_params = NULL;
898 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -0400899
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400900 hs->state = state_send_server_hello_done;
901 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -0800902}
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400904static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900905 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400906
David Benjamin1386aad2017-07-19 23:57:40 -0400907 ScopedCBB cbb;
908 CBB body;
David Benjamin75836432016-06-17 18:48:29 -0400909
David Benjamin0d1730d2017-06-15 23:24:25 -0400910 if (hs->cert_request) {
911 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -0400912 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -0400913 SSL3_MT_CERTIFICATE_REQUEST) ||
914 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
915 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
916 (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
917 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
918 (ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
919 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
920 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
921 !ssl_add_client_CA_list(ssl, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400922 !ssl_add_message_cbb(ssl, cbb.get())) {
923 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400924 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -0400925 }
926 }
927
David Benjamin1386aad2017-07-19 23:57:40 -0400928 if (!ssl->method->init_message(ssl, cbb.get(), &body,
929 SSL3_MT_SERVER_HELLO_DONE) ||
930 !ssl_add_message_cbb(ssl, cbb.get())) {
931 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400932 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -0400933 }
934
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400935 hs->state = state_read_client_certificate;
936 return ssl_hs_flush;
Adam Langleyfcf25832014-12-18 17:42:32 -0800937}
Adam Langley95c29f32014-06-20 12:00:00 -0700938
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400939static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900940 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400941
942 if (!hs->cert_request) {
943 hs->state = state_verify_client_certificate;
944 return ssl_hs_ok;
945 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400946
David Benjamin7934f082017-08-01 16:32:25 -0400947 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400948 if (!ssl->method->get_message(ssl, &msg)) {
949 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400950 }
951
David Benjamin7934f082017-08-01 16:32:25 -0400952 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400953 if (ssl->version == SSL3_VERSION &&
David Benjamin7934f082017-08-01 16:32:25 -0400954 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400955 // In SSL 3.0, the Certificate message is omitted to signal no
956 // certificate.
David Benjaminda2630c2016-08-01 19:57:43 -0400957 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400958 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -0400959 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400960 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400961 }
962
David Benjaminc11ea9422017-08-29 16:33:21 -0400963 // OpenSSL returns X509_V_OK when no certificates are received. This is
964 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -0500965 hs->new_session->verify_result = X509_V_OK;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400966 hs->state = state_verify_client_certificate;
967 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400968 }
969
David Benjamin9f1dc822016-06-07 17:03:46 -0400970 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -0400971 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400972 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400973 }
974
David Benjamin7934f082017-08-01 16:32:25 -0400975 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400976 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500977 }
978
David Benjamin7934f082017-08-01 16:32:25 -0400979 CBS certificate_msg = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800980 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400981 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
982 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
983 ssl->retain_only_sha256_of_client_certs
984 ? hs->new_session->peer_sha256
985 : NULL,
986 &certificate_msg, ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400987 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400988 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400989 }
David Benjamine664a532017-07-20 20:19:36 -0400990 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
991 hs->new_session->certs = chain.release();
David Benjamin9f1dc822016-06-07 17:03:46 -0400992
Adam Langley68e71242016-12-12 11:06:16 -0800993 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400994 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400995 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -0400996 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400997 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400998 }
999
David Benjamin45738dd2017-02-09 20:01:26 -05001000 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001001 // No client certificate so the handshake buffer may be discarded.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001002 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001003
David Benjaminc11ea9422017-08-29 16:33:21 -04001004 // In SSL 3.0, sending no certificate is signaled by omitting the
1005 // Certificate message.
David Benjamin9f1dc822016-06-07 17:03:46 -04001006 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001007 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001008 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001009 return ssl_hs_error;
David Benjamine455e512016-08-01 20:11:13 -04001010 }
1011
1012 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001013 // Fail for TLS only if we required a certificate
David Benjamin9f1dc822016-06-07 17:03:46 -04001014 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001015 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001016 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001017 }
Adam Langley37646832016-08-01 16:16:46 -07001018
David Benjaminc11ea9422017-08-29 16:33:21 -04001019 // OpenSSL returns X509_V_OK when no certificates are received. This is
1020 // classed by them as a bug, but it's assumed by at least NGINX.
David Benjamin45738dd2017-02-09 20:01:26 -05001021 hs->new_session->verify_result = X509_V_OK;
David Benjamin8f94c312017-08-01 17:35:55 -04001022 } else if (ssl->retain_only_sha256_of_client_certs) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001023 // The hash will have been filled in.
David Benjamin45738dd2017-02-09 20:01:26 -05001024 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001025 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001026
David Benjamin8f94c312017-08-01 17:35:55 -04001027 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001028 hs->state = state_verify_client_certificate;
1029 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001030}
1031
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001032static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
1033 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
1034 switch (ssl_verify_peer_cert(hs)) {
1035 case ssl_verify_ok:
1036 break;
1037 case ssl_verify_invalid:
1038 return ssl_hs_error;
1039 case ssl_verify_retry:
1040 return ssl_hs_certificate_verify;
1041 }
1042 }
1043
1044 hs->state = state_read_client_key_exchange;
1045 return ssl_hs_ok;
1046}
1047
1048static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001049 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001050
1051 ssl_hs_wait_t ret = ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 uint8_t *premaster_secret = NULL;
1053 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001055
David Benjamin7934f082017-08-01 16:32:25 -04001056 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001057 if (!ssl->method->get_message(ssl, &msg)) {
1058 return ssl_hs_read_message;
David Benjamin44148742017-06-17 13:20:59 -04001059 }
David Benjamin276b7e82017-01-21 14:13:39 -05001060
David Benjamin7934f082017-08-01 16:32:25 -04001061 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001062 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
David Benjamin7934f082017-08-01 16:32:25 -04001065 CBS client_key_exchange = msg.body;
David Benjamin44148742017-06-17 13:20:59 -04001066 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1067 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001068
David Benjaminc11ea9422017-08-29 16:33:21 -04001069 // If using a PSK key exchange, parse the PSK identity.
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 if (alg_a & SSL_aPSK) {
1071 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001072
David Benjaminc11ea9422017-08-29 16:33:21 -04001073 // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1074 // then this is the only field in the message.
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1076 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001077 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001078 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1079 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001080 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001081
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1083 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001084 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001085 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1086 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001087 }
David Benjamin35c02302014-07-13 04:14:59 -04001088
David Benjamin45738dd2017-02-09 20:01:26 -05001089 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001090 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001091 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1092 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001093 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001095
David Benjaminc11ea9422017-08-29 16:33:21 -04001096 // Depending on the key exchange method, compute |premaster_secret| and
1097 // |premaster_secret_len|.
Adam Langleyfcf25832014-12-18 17:42:32 -08001098 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001099 CBS encrypted_premaster_secret;
1100 if (ssl->version > SSL3_VERSION) {
1101 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1102 &encrypted_premaster_secret) ||
1103 CBS_len(&client_key_exchange) != 0) {
1104 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1105 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1106 goto err;
1107 }
1108 } else {
1109 encrypted_premaster_secret = client_key_exchange;
1110 }
1111
David Benjaminc11ea9422017-08-29 16:33:21 -04001112 // Allocate a buffer large enough for an RSA decryption.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001113 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamind781fc42017-07-12 16:25:57 -04001114 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001115 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001116 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001117 goto err;
1118 }
David Benjamin35c02302014-07-13 04:14:59 -04001119
David Benjaminc11ea9422017-08-29 16:33:21 -04001120 // Decrypt with no padding. PKCS#1 padding will be removed as part of the
1121 // timing-sensitive code below.
David Benjamin3f5b43d2015-12-01 19:31:24 -05001122 size_t decrypt_len;
David Benjamin44148742017-06-17 13:20:59 -04001123 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1124 CBS_data(&encrypted_premaster_secret),
1125 CBS_len(&encrypted_premaster_secret))) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001126 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001127 break;
1128 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001129 goto err;
1130 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001131 ret = ssl_hs_private_key_operation;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001132 goto err;
1133 }
1134
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001135 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001136 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001137 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1138 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001139 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001140
David Benjaminc11ea9422017-08-29 16:33:21 -04001141 // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1142 // section 7.4.7.1.
David Benjamin3f5b43d2015-12-01 19:31:24 -05001143 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001144 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001146 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 goto err;
1148 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001149 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001150 goto err;
1151 }
1152
David Benjaminc11ea9422017-08-29 16:33:21 -04001153 // The smallest padded premaster is 11 bytes of overhead. Small keys are
1154 // publicly invalid.
David Benjamin3f5b43d2015-12-01 19:31:24 -05001155 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001156 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001157 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1158 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 }
David Benjamin35c02302014-07-13 04:14:59 -04001160
David Benjaminc11ea9422017-08-29 16:33:21 -04001161 // Check the padding. See RFC 3447, section 7.2.2.
David Benjamin3f5b43d2015-12-01 19:31:24 -05001162 size_t padding_len = decrypt_len - premaster_secret_len;
1163 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1164 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001165 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001166 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1167 }
1168 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1169
David Benjaminc11ea9422017-08-29 16:33:21 -04001170 // The premaster secret must begin with |client_version|. This too must be
1171 // checked in constant time (http://eprint.iacr.org/2003/052/).
David Benjamin3f5b43d2015-12-01 19:31:24 -05001172 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001173 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001174 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001175 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001176
David Benjaminc11ea9422017-08-29 16:33:21 -04001177 // Select, in constant time, either the decrypted premaster or the random
1178 // premaster based on |good|.
David Benjamin54091232016-09-05 12:47:25 -04001179 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001180 premaster_secret[i] = constant_time_select_8(
1181 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1182 }
1183
1184 OPENSSL_free(decrypt_buf);
1185 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001186 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001187 // Parse the ClientKeyExchange.
David Benjamin974c7ba2015-12-19 16:58:04 -05001188 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001189 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001190 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001191 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001192 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1193 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001195
David Benjaminc11ea9422017-08-29 16:33:21 -04001196 // Compute the premaster.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001197 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001198 if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
1199 CBS_data(&peer_key), CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001200 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1201 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001202 }
Adam Langley95c29f32014-06-20 12:00:00 -07001203
David Benjaminc11ea9422017-08-29 16:33:21 -04001204 // The key exchange state may now be discarded.
David Benjaminc642aca2017-07-19 23:28:43 -04001205 hs->key_share.reset();
David Benjamin44148742017-06-17 13:20:59 -04001206 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001207 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001208 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1209 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001210 }
Adam Langley95c29f32014-06-20 12:00:00 -07001211
David Benjaminc11ea9422017-08-29 16:33:21 -04001212 // For a PSK cipher suite, the actual pre-master secret is combined with the
1213 // pre-shared key.
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 if (alg_a & SSL_aPSK) {
David Benjamin44148742017-06-17 13:20:59 -04001215 if (ssl->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001216 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin44148742017-06-17 13:20:59 -04001217 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1218 goto err;
1219 }
1220
David Benjaminc11ea9422017-08-29 16:33:21 -04001221 // Look up the key for the identity.
David Benjamin44148742017-06-17 13:20:59 -04001222 uint8_t psk[PSK_MAX_PSK_LEN];
1223 unsigned psk_len = ssl->psk_server_callback(
1224 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1225 if (psk_len > PSK_MAX_PSK_LEN) {
1226 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1227 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1228 goto err;
1229 } else if (psk_len == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001230 // PSK related to the given identity not found.
David Benjamin44148742017-06-17 13:20:59 -04001231 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1232 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1233 goto err;
1234 }
1235
1236 if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001237 // In plain PSK, other_secret is a block of 0s with the same length as the
1238 // pre-shared key.
David Benjamin44148742017-06-17 13:20:59 -04001239 premaster_secret_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001240 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
David Benjamin44148742017-06-17 13:20:59 -04001241 if (premaster_secret == NULL) {
1242 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1243 goto err;
1244 }
1245 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
1246 }
1247
David Benjamin1386aad2017-07-19 23:57:40 -04001248 ScopedCBB new_premaster;
1249 CBB child;
Adam Langleyfcf25832014-12-18 17:42:32 -08001250 uint8_t *new_data;
1251 size_t new_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001252 if (!CBB_init(new_premaster.get(),
1253 2 + psk_len + 2 + premaster_secret_len) ||
1254 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001255 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001256 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001257 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001258 !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001259 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 goto err;
1261 }
David Benjamin14c83e72014-07-13 04:54:57 -04001262
Adam Langleyfcf25832014-12-18 17:42:32 -08001263 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1264 OPENSSL_free(premaster_secret);
1265 premaster_secret = new_data;
1266 premaster_secret_len = new_len;
1267 }
David Benjamin14c83e72014-07-13 04:54:57 -04001268
David Benjamin7934f082017-08-01 16:32:25 -04001269 if (!ssl_hash_message(hs, msg)) {
David Benjamin44148742017-06-17 13:20:59 -04001270 goto err;
1271 }
1272
David Benjaminc11ea9422017-08-29 16:33:21 -04001273 // Compute the master secret.
David Benjamin45738dd2017-02-09 20:01:26 -05001274 hs->new_session->master_key_length = tls1_generate_master_secret(
1275 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1276 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 goto err;
1278 }
David Benjaminfc02b592017-02-17 16:26:01 -05001279 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001280
David Benjamin8f94c312017-08-01 17:35:55 -04001281 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001282 hs->state = state_read_client_certificate_verify;
1283 ret = ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001284
Adam Langley95c29f32014-06-20 12:00:00 -07001285err:
David Benjamin4298d772015-12-19 00:18:25 -05001286 if (premaster_secret != NULL) {
1287 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 OPENSSL_free(premaster_secret);
1289 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001290 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001291
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001292 return ret;
1293
Adam Langleyfcf25832014-12-18 17:42:32 -08001294}
Adam Langley95c29f32014-06-20 12:00:00 -07001295
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001296static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001297 SSL *const ssl = hs->ssl;
David Benjamin6553b372014-07-22 14:11:30 -04001298
David Benjaminc11ea9422017-08-29 16:33:21 -04001299 // Only RSA and ECDSA client certificates are supported, so a
1300 // CertificateVerify is required if and only if there's a client certificate.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001301 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001302 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001303 hs->state = state_read_change_cipher_spec;
1304 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 }
Adam Langley95c29f32014-06-20 12:00:00 -07001306
David Benjamin7934f082017-08-01 16:32:25 -04001307 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001308 if (!ssl->method->get_message(ssl, &msg)) {
1309 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 }
David Benjaminef865502014-08-24 02:48:34 -04001311
David Benjamin7934f082017-08-01 16:32:25 -04001312 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001313 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001314 }
1315
David Benjamin7934f082017-08-01 16:32:25 -04001316 CBS certificate_verify = msg.body, signature;
David Benjamin6897dbe2014-07-12 20:18:28 -04001317
David Benjaminc11ea9422017-08-29 16:33:21 -04001318 // Determine the signature algorithm.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001319 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001320 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001321 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001322 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001323 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001324 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001325 }
David Benjamin8d606e32017-06-15 22:43:04 -04001326 uint8_t alert = SSL_AD_DECODE_ERROR;
1327 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001328 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001329 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001330 }
David Benjamin45738dd2017-02-09 20:01:26 -05001331 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001332 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001333 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001334 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001335 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001336 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 }
David Benjamin854dd652014-08-26 00:32:30 -04001338
David Benjaminc11ea9422017-08-29 16:33:21 -04001339 // Parse and verify the signature.
Adam Langleyfcf25832014-12-18 17:42:32 -08001340 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1341 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001342 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001343 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001344 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001345 }
Adam Langley95c29f32014-06-20 12:00:00 -07001346
Steven Valdez2b8415e2016-06-30 13:27:23 -04001347 int sig_ok;
David Benjaminc11ea9422017-08-29 16:33:21 -04001348 // The SSL3 construction for CertificateVerify does not decompose into a
1349 // single final digest and signature, and must be special-cased.
Steven Valdez2b8415e2016-06-30 13:27:23 -04001350 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001351 uint8_t digest[EVP_MAX_MD_SIZE];
1352 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001353 if (!hs->transcript.GetSSL3CertVerifyHash(
1354 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001355 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001356 }
1357
David Benjamin31b0c9b2017-07-20 14:49:15 -04001358 UniquePtr<EVP_PKEY_CTX> pctx(
1359 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
David Benjamin1386aad2017-07-19 23:57:40 -04001360 sig_ok = pctx &&
1361 EVP_PKEY_verify_init(pctx.get()) &&
1362 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1363 CBS_len(&signature), digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001364 } else {
1365 sig_ok = ssl_public_key_verify(
1366 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin6dc8bf62017-07-19 16:38:21 -04001367 hs->peer_pubkey.get(), hs->transcript.buffer_data(),
1368 hs->transcript.buffer_len());
Adam Langleyfcf25832014-12-18 17:42:32 -08001369 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001370
David Benjaminbf82aed2016-03-01 22:57:40 -05001371#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1372 sig_ok = 1;
1373 ERR_clear_error();
1374#endif
1375 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001376 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001377 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001378 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 }
1380
David Benjaminc11ea9422017-08-29 16:33:21 -04001381 // The handshake buffer is no longer necessary, and we may hash the current
1382 // message.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001383 hs->transcript.FreeBuffer();
David Benjamin7934f082017-08-01 16:32:25 -04001384 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001385 return ssl_hs_error;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001386 }
1387
David Benjamin8f94c312017-08-01 17:35:55 -04001388 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001389 hs->state = state_read_change_cipher_spec;
1390 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001391}
Adam Langley95c29f32014-06-20 12:00:00 -07001392
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001393static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
1394 hs->state = state_process_change_cipher_spec;
1395 return ssl_hs_read_change_cipher_spec;
1396}
1397
1398static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1399 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
1400 return ssl_hs_error;
1401 }
1402
1403 hs->state = state_read_next_proto;
1404 return ssl_hs_ok;
1405}
1406
1407static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001408 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001409
1410 if (!hs->next_proto_neg_seen) {
1411 hs->state = state_read_channel_id;
1412 return ssl_hs_ok;
1413 }
1414
David Benjamin7934f082017-08-01 16:32:25 -04001415 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001416 if (!ssl->method->get_message(ssl, &msg)) {
1417 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001418 }
Adam Langley95c29f32014-06-20 12:00:00 -07001419
David Benjamin7934f082017-08-01 16:32:25 -04001420 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1421 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001422 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001423 }
1424
David Benjamin7934f082017-08-01 16:32:25 -04001425 CBS next_protocol = msg.body, selected_protocol, padding;
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1427 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001428 CBS_len(&next_protocol) != 0) {
1429 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1430 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001431 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001432 }
1433
1434 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001435 &ssl->s3->next_proto_negotiated_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001436 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin8f94c312017-08-01 17:35:55 -04001439 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001440 hs->state = state_read_channel_id;
1441 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001442}
Adam Langley95c29f32014-06-20 12:00:00 -07001443
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001444static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001445 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001446
1447 if (!ssl->s3->tlsext_channel_id_valid) {
1448 hs->state = state_read_client_finished;
1449 return ssl_hs_ok;
1450 }
1451
David Benjamin7934f082017-08-01 16:32:25 -04001452 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001453 if (!ssl->method->get_message(ssl, &msg)) {
1454 return ssl_hs_read_message;
Adam Langleyfcf25832014-12-18 17:42:32 -08001455 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001456
David Benjamin7934f082017-08-01 16:32:25 -04001457 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1458 !tls1_verify_channel_id(hs, msg) ||
1459 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001460 return ssl_hs_error;
Adam Langleyfcf25832014-12-18 17:42:32 -08001461 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001462
David Benjamin8f94c312017-08-01 17:35:55 -04001463 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001464 hs->state = state_read_client_finished;
1465 return ssl_hs_ok;
Adam Langleyfcf25832014-12-18 17:42:32 -08001466}
David Benjamin9f1dc822016-06-07 17:03:46 -04001467
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001468static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
1469 SSL *const ssl = hs->ssl;
1470 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1471 if (wait != ssl_hs_ok) {
1472 return wait;
1473 }
1474
1475 if (ssl->session != NULL) {
1476 hs->state = state_finish_server_handshake;
1477 } else {
1478 hs->state = state_send_server_finished;
1479 }
1480
David Benjaminc11ea9422017-08-29 16:33:21 -04001481 // If this is a full handshake with ChannelID then record the handshake
1482 // hashes in |hs->new_session| in case we need them to verify a
1483 // ChannelID signature on a resumption of this session in the future.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001484 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
1485 !tls1_record_handshake_hashes_for_channel_id(hs)) {
1486 return ssl_hs_error;
1487 }
1488
1489 return ssl_hs_ok;
1490}
1491
1492static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001493 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001494
1495 if (hs->ticket_expected) {
1496 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001497 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001498 if (ssl->session == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001499 // Fix the timeout to measure from the ticket issuance time.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001500 ssl_session_rebase_time(ssl, hs->new_session.get());
1501 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001502 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001503 // We are renewing an existing session. Duplicate the session to adjust
1504 // the timeout.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001505 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001506 if (!session_copy) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001507 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001508 }
1509
David Benjamin1386aad2017-07-19 23:57:40 -04001510 ssl_session_rebase_time(ssl, session_copy.get());
1511 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001512 }
1513
David Benjamin1386aad2017-07-19 23:57:40 -04001514 ScopedCBB cbb;
1515 CBB body, ticket;
1516 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1517 SSL3_MT_NEW_SESSION_TICKET) ||
1518 !CBB_add_u32(&body, session->timeout) ||
1519 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1520 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1521 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001522 return ssl_hs_error;
David Benjaminb5f55c32017-06-15 23:15:15 -04001523 }
David Benjamin123db572016-11-03 16:59:25 -04001524 }
1525
David Benjaminb5f55c32017-06-15 23:15:15 -04001526 if (!ssl->method->add_change_cipher_spec(ssl) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001527 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE) ||
1528 !ssl3_send_finished(hs)) {
1529 return ssl_hs_error;
David Benjamin75836432016-06-17 18:48:29 -04001530 }
1531
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001532 if (ssl->session != NULL) {
1533 hs->state = state_read_change_cipher_spec;
1534 } else {
1535 hs->state = state_finish_server_handshake;
1536 }
1537 return ssl_hs_flush;
David Benjamin9f1dc822016-06-07 17:03:46 -04001538}
David Benjamin86e95b82017-07-18 16:34:25 -04001539
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001540static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
1541 SSL *const ssl = hs->ssl;
1542
1543 ssl->method->on_handshake_complete(ssl);
1544
David Benjaminc11ea9422017-08-29 16:33:21 -04001545 // If we aren't retaining peer certificates then we can discard it now.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001546 if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
1547 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1548 hs->new_session->certs = NULL;
1549 ssl->ctx->x509_method->session_clear(hs->new_session.get());
1550 }
1551
1552 SSL_SESSION_free(ssl->s3->established_session);
1553 if (ssl->session != NULL) {
1554 SSL_SESSION_up_ref(ssl->session);
1555 ssl->s3->established_session = ssl->session;
1556 } else {
1557 ssl->s3->established_session = hs->new_session.release();
1558 ssl->s3->established_session->not_resumable = 0;
1559 }
1560
David Benjaminfd45ee72017-08-31 14:49:09 -04001561 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001562 ssl->s3->initial_handshake_complete = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001563 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
1564
1565 hs->state = state_done;
1566 return ssl_hs_ok;
1567}
1568
1569enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
1570 while (hs->state != state_done) {
1571 enum ssl_hs_wait_t ret = ssl_hs_error;
1572 enum ssl_server_hs_state_t state =
1573 static_cast<enum ssl_server_hs_state_t>(hs->state);
1574 switch (state) {
1575 case state_start_accept:
1576 ret = do_start_accept(hs);
1577 break;
1578 case state_read_client_hello:
1579 ret = do_read_client_hello(hs);
1580 break;
1581 case state_select_certificate:
1582 ret = do_select_certificate(hs);
1583 break;
1584 case state_tls13:
1585 ret = do_tls13(hs);
1586 break;
1587 case state_select_parameters:
1588 ret = do_select_parameters(hs);
1589 break;
1590 case state_send_server_hello:
1591 ret = do_send_server_hello(hs);
1592 break;
1593 case state_send_server_certificate:
1594 ret = do_send_server_certificate(hs);
1595 break;
1596 case state_send_server_key_exchange:
1597 ret = do_send_server_key_exchange(hs);
1598 break;
1599 case state_send_server_hello_done:
1600 ret = do_send_server_hello_done(hs);
1601 break;
1602 case state_read_client_certificate:
1603 ret = do_read_client_certificate(hs);
1604 break;
1605 case state_verify_client_certificate:
1606 ret = do_verify_client_certificate(hs);
1607 break;
1608 case state_read_client_key_exchange:
1609 ret = do_read_client_key_exchange(hs);
1610 break;
1611 case state_read_client_certificate_verify:
1612 ret = do_read_client_certificate_verify(hs);
1613 break;
1614 case state_read_change_cipher_spec:
1615 ret = do_read_change_cipher_spec(hs);
1616 break;
1617 case state_process_change_cipher_spec:
1618 ret = do_process_change_cipher_spec(hs);
1619 break;
1620 case state_read_next_proto:
1621 ret = do_read_next_proto(hs);
1622 break;
1623 case state_read_channel_id:
1624 ret = do_read_channel_id(hs);
1625 break;
1626 case state_read_client_finished:
1627 ret = do_read_client_finished(hs);
1628 break;
1629 case state_send_server_finished:
1630 ret = do_send_server_finished(hs);
1631 break;
1632 case state_finish_server_handshake:
1633 ret = do_finish_server_handshake(hs);
1634 break;
1635 case state_done:
1636 ret = ssl_hs_ok;
1637 break;
1638 }
1639
1640 if (hs->state != state) {
1641 ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
1642 }
1643
1644 if (ret != ssl_hs_ok) {
1645 return ret;
1646 }
1647 }
1648
1649 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1650 return ssl_hs_ok;
1651}
1652
1653const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
1654 enum ssl_server_hs_state_t state =
1655 static_cast<enum ssl_server_hs_state_t>(hs->state);
1656 switch (state) {
1657 case state_start_accept:
1658 return "TLS server start_accept";
1659 case state_read_client_hello:
1660 return "TLS server read_client_hello";
1661 case state_select_certificate:
1662 return "TLS server select_certificate";
1663 case state_tls13:
1664 return tls13_server_handshake_state(hs);
1665 case state_select_parameters:
1666 return "TLS server select_parameters";
1667 case state_send_server_hello:
1668 return "TLS server send_server_hello";
1669 case state_send_server_certificate:
1670 return "TLS server send_server_certificate";
1671 case state_send_server_key_exchange:
1672 return "TLS server send_server_key_exchange";
1673 case state_send_server_hello_done:
1674 return "TLS server send_server_hello_done";
1675 case state_read_client_certificate:
1676 return "TLS server read_client_certificate";
1677 case state_verify_client_certificate:
1678 return "TLS server verify_client_certificate";
1679 case state_read_client_key_exchange:
1680 return "TLS server read_client_key_exchange";
1681 case state_read_client_certificate_verify:
1682 return "TLS server read_client_certificate_verify";
1683 case state_read_change_cipher_spec:
1684 return "TLS server read_change_cipher_spec";
1685 case state_process_change_cipher_spec:
1686 return "TLS server process_change_cipher_spec";
1687 case state_read_next_proto:
1688 return "TLS server read_next_proto";
1689 case state_read_channel_id:
1690 return "TLS server read_channel_id";
1691 case state_read_client_finished:
1692 return "TLS server read_client_finished";
1693 case state_send_server_finished:
1694 return "TLS server send_server_finished";
1695 case state_finish_server_handshake:
1696 return "TLS server finish_server_handshake";
1697 case state_done:
1698 return "TLS server done";
1699 }
1700
1701 return "TLS server unknown";
1702}
1703
1704}