blob: 9f9e4833f8c604a92d5d6b3bbf55045f89063969 [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 */
149
David Benjamin9e4e01e2015-09-15 01:48:04 -0400150#include <openssl/ssl.h>
151
David Benjamin81ea0bf2014-11-23 04:20:17 -0500152#include <assert.h>
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400153#include <limits.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400154#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamin31b0c9b2017-07-20 14:49:15 -0400156#include <utility>
157
David Benjaminabbbee12016-10-31 19:20:42 -0400158#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400159#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400161#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400162#include <openssl/ec_key.h>
163#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400164#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400167#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400168#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin17cf2cb2016-12-13 01:07:13 -0500170#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400171#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin86e95b82017-07-18 16:34:25 -0400174namespace bssl {
175
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400176enum ssl_client_hs_state_t {
177 state_start_connect = 0,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400178 state_enter_early_data,
179 state_read_hello_verify_request,
180 state_read_server_hello,
181 state_tls13,
182 state_read_server_certificate,
183 state_read_certificate_status,
184 state_verify_server_certificate,
Jesse Selover1c337e52018-08-10 13:28:47 -0400185 state_reverify_server_certificate,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400186 state_read_server_key_exchange,
187 state_read_certificate_request,
188 state_read_server_hello_done,
189 state_send_client_certificate,
190 state_send_client_key_exchange,
191 state_send_client_certificate_verify,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400192 state_send_client_finished,
193 state_finish_flight,
194 state_read_session_ticket,
195 state_process_change_cipher_spec,
196 state_read_server_finished,
197 state_finish_client_handshake,
198 state_done,
199};
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjaminc11ea9422017-08-29 16:33:21 -0400201// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
202// disabled algorithms.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700203static void ssl_get_client_disabled(SSL_HANDSHAKE *hs, uint32_t *out_mask_a,
David Benjamin2fe6e222017-01-25 02:18:43 -0500204 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500205 *out_mask_a = 0;
206 *out_mask_k = 0;
207
David Benjaminc11ea9422017-08-29 16:33:21 -0400208 // PSK requires a client callback.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700209 if (hs->config->psk_client_callback == NULL) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500210 *out_mask_a |= SSL_aPSK;
211 *out_mask_k |= SSL_kPSK;
212 }
213}
214
David Benjamin68161cb2017-06-20 14:49:43 -0400215static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
216 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500217 uint32_t mask_a, mask_k;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700218 ssl_get_client_disabled(hs, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjamine8d53502015-10-10 14:13:23 -0400220 CBB child;
221 if (!CBB_add_u16_length_prefixed(out, &child)) {
222 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800223 }
224
David Benjaminc11ea9422017-08-29 16:33:21 -0400225 // Add a fake cipher suite. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -0400226 if (ssl->ctx->grease_enabled &&
David Benjamina7bc9442018-01-18 10:08:53 -0500227 !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
David Benjamin65ac9972016-09-02 21:35:25 -0400228 return 0;
229 }
230
David Benjaminc11ea9422017-08-29 16:33:21 -0400231 // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
232 // hardware support.
David Benjamin68161cb2017-06-20 14:49:43 -0400233 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400234 if (!EVP_has_aes_hardware() &&
235 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
236 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500237 }
David Benjaminabbbee12016-10-31 19:20:42 -0400238 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
239 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
240 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400241 }
David Benjaminabbbee12016-10-31 19:20:42 -0400242 if (EVP_has_aes_hardware() &&
243 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400244 return 0;
245 }
246 }
247
David Benjamin68161cb2017-06-20 14:49:43 -0400248 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400249 int any_enabled = 0;
David Benjaminee910bf2017-07-25 22:36:00 -0400250 for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400251 // Skip disabled ciphers
David Benjamin2fe6e222017-01-25 02:18:43 -0500252 if ((cipher->algorithm_mkey & mask_k) ||
253 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400254 continue;
255 }
David Benjamin68161cb2017-06-20 14:49:43 -0400256 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
257 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400258 continue;
259 }
260 any_enabled = 1;
261 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
262 return 0;
263 }
264 }
265
David Benjaminc11ea9422017-08-29 16:33:21 -0400266 // If all ciphers were disabled, return the error to the caller.
David Benjamin68161cb2017-06-20 14:49:43 -0400267 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400268 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
269 return 0;
270 }
David Benjamine8d53502015-10-10 14:13:23 -0400271 }
272
David Benjamine0ff7672016-09-19 18:40:03 -0400273 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200274 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
275 return 0;
276 }
David Benjamine8d53502015-10-10 14:13:23 -0400277 }
278
279 return CBB_flush(out);
280}
281
David Benjamin8c880a22016-12-03 02:20:34 -0500282int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
283 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400284 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -0400285 CBB body;
286 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
287 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400288 }
289
Steven Valdez5440fe02016-07-18 12:40:30 -0400290 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500291 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400292 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez9986f6b2017-12-01 12:24:01 -0500293 !CBB_add_u8_length_prefixed(&body, &child)) {
294 return 0;
295 }
296
297 // Do not send a session ID on renegotiation.
298 if (!ssl->s3->initial_handshake_complete &&
Steven Valdezc5c31ab2017-11-11 20:34:06 -0500299 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400300 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400301 }
302
David Benjamince079fd2016-08-02 16:22:34 -0400303 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400304 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400305 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400306 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400307 }
308 }
309
310 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400311 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400312 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400313 !CBB_add_u8(&body, 1 /* one compression method */) ||
314 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500315 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400316 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400317 }
318
David Benjamin879efc32017-09-21 11:20:53 -0400319 Array<uint8_t> msg;
320 if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
David Benjamind781fc42017-07-12 16:25:57 -0400321 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400322 }
323
David Benjaminc11ea9422017-08-29 16:33:21 -0400324 // Now that the length prefixes have been computed, fill in the placeholder
325 // PSK binder.
David Benjamin8c880a22016-12-03 02:20:34 -0500326 if (hs->needs_psk_binder &&
David Benjamin879efc32017-09-21 11:20:53 -0400327 !tls13_write_psk_binder(hs, msg.data(), msg.size())) {
David Benjamind781fc42017-07-12 16:25:57 -0400328 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400329 }
330
David Benjamin879efc32017-09-21 11:20:53 -0400331 return ssl->method->add_message(ssl, std::move(msg));
Steven Valdez5440fe02016-07-18 12:40:30 -0400332}
333
David Benjamina0bc29a2018-03-13 16:51:58 -0400334static bool parse_supported_versions(SSL_HANDSHAKE *hs, uint16_t *version,
335 const CBS *in) {
336 // If the outer version is not TLS 1.2, or there is no extensions block, use
337 // the outer version.
338 if (*version != TLS1_2_VERSION || CBS_len(in) == 0) {
339 return true;
340 }
341
David Benjaminb853f312017-07-14 18:40:34 -0400342 SSL *const ssl = hs->ssl;
David Benjamina0bc29a2018-03-13 16:51:58 -0400343 CBS copy = *in, extensions;
344 if (!CBS_get_u16_length_prefixed(&copy, &extensions) ||
345 CBS_len(&copy) != 0) {
David Benjaminb853f312017-07-14 18:40:34 -0400346 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400347 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamina0bc29a2018-03-13 16:51:58 -0400348 return false;
David Benjaminb853f312017-07-14 18:40:34 -0400349 }
350
David Benjamin74795b32017-08-31 15:13:12 -0400351 bool have_supported_versions;
David Benjaminb853f312017-07-14 18:40:34 -0400352 CBS supported_versions;
353 const SSL_EXTENSION_TYPE ext_types[] = {
354 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
355 &supported_versions},
356 };
357
358 uint8_t alert = SSL_AD_DECODE_ERROR;
359 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
360 OPENSSL_ARRAY_SIZE(ext_types),
361 1 /* ignore unknown */)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400362 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamina0bc29a2018-03-13 16:51:58 -0400363 return false;
David Benjaminb853f312017-07-14 18:40:34 -0400364 }
365
David Benjamina0bc29a2018-03-13 16:51:58 -0400366 // Override the outer version with the extension, if present.
David Benjaminb853f312017-07-14 18:40:34 -0400367 if (have_supported_versions &&
David Benjamina0bc29a2018-03-13 16:51:58 -0400368 (!CBS_get_u16(&supported_versions, version) ||
David Benjaminb853f312017-07-14 18:40:34 -0400369 CBS_len(&supported_versions) != 0)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400370 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamina0bc29a2018-03-13 16:51:58 -0400371 return false;
David Benjaminb853f312017-07-14 18:40:34 -0400372 }
373
David Benjamina0bc29a2018-03-13 16:51:58 -0400374 return true;
David Benjaminb853f312017-07-14 18:40:34 -0400375}
376
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400377static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400378 SSL *const ssl = hs->ssl;
379
Steven Valdez398085b2017-08-29 13:25:56 -0400380 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamind0beda02017-09-06 16:46:50 -0400381 // |session_reused| must be reset in case this is a renegotiation.
382 ssl->s3->session_reused = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400383
David Benjaminc11ea9422017-08-29 16:33:21 -0400384 // Freeze the version range.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700385 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400386 return ssl_hs_error;
387 }
388
David Benjaminc11ea9422017-08-29 16:33:21 -0400389 // Always advertise the ClientHello version from the original maximum version,
390 // even on renegotiation. The static RSA key exchange uses this field, and
391 // some servers fail when it changes across handshakes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400392 if (SSL_is_dtls(hs->ssl)) {
393 hs->client_version =
394 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
395 } else {
396 hs->client_version =
397 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
398 }
399
David Benjaminc11ea9422017-08-29 16:33:21 -0400400 // If the configured session has expired or was created at a disabled
401 // version, drop it.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400402 if (ssl->session != NULL) {
403 if (ssl->session->is_server ||
404 !ssl_supports_version(hs, ssl->session->ssl_version) ||
405 (ssl->session->session_id_length == 0 &&
David Benjaminbfdd1a92018-06-29 16:26:38 -0400406 ssl->session->ticket.empty()) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400407 ssl->session->not_resumable ||
David Benjamin50596f82018-07-02 19:47:27 -0400408 !ssl_session_is_time_valid(ssl, ssl->session.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400409 ssl_set_session(ssl, NULL);
410 }
411 }
412
Steven Valdez398085b2017-08-29 13:25:56 -0400413 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400414 return ssl_hs_error;
415 }
416
David Benjaminc11ea9422017-08-29 16:33:21 -0400417 // Initialize a random session ID for the experimental TLS 1.3 variant
418 // requiring a session id.
Steven Valdezc5c31ab2017-11-11 20:34:06 -0500419 if (ssl->session != nullptr &&
420 !ssl->s3->initial_handshake_complete &&
421 ssl->session->session_id_length > 0) {
422 hs->session_id_len = ssl->session->session_id_length;
423 OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
424 hs->session_id_len);
Steven Valdez64cc1212017-12-04 11:15:37 -0500425 } else if (hs->max_version >= TLS1_3_VERSION) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400426 hs->session_id_len = sizeof(hs->session_id);
427 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
428 return ssl_hs_error;
429 }
430 }
431
432 if (!ssl_write_client_hello(hs)) {
433 return ssl_hs_error;
434 }
435
436 hs->state = state_enter_early_data;
437 return ssl_hs_flush;
438}
439
440static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
441 SSL *const ssl = hs->ssl;
442
Steven Valdez398085b2017-08-29 13:25:56 -0400443 if (SSL_is_dtls(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400444 hs->state = state_read_hello_verify_request;
445 return ssl_hs_ok;
446 }
447
448 if (!hs->early_data_offered) {
449 hs->state = state_read_server_hello;
450 return ssl_hs_ok;
451 }
452
Steven Valdez964b2372017-11-07 17:09:52 -0500453 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
Steven Valdez7e5dd252018-01-22 15:20:31 -0500454 if (!ssl->method->add_change_cipher_spec(ssl)) {
Steven Valdez964b2372017-11-07 17:09:52 -0500455 return ssl_hs_error;
456 }
457
Steven Valdezcd8470f2017-10-11 12:29:36 -0400458 if (!tls13_init_early_key_schedule(hs, ssl->session->master_key,
459 ssl->session->master_key_length) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400460 !tls13_derive_early_secrets(hs) ||
461 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
462 hs->hash_len)) {
463 return ssl_hs_error;
464 }
465
David Benjaminc11ea9422017-08-29 16:33:21 -0400466 // Stash the early data session, so connection properties may be queried out
467 // of it.
David Benjaminfd45ee72017-08-31 14:49:09 -0400468 hs->in_early_data = true;
David Benjamin2908dd12018-06-29 17:46:42 -0400469 hs->early_session = UpRef(ssl->session);
David Benjaminfd45ee72017-08-31 14:49:09 -0400470 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400471
472 hs->state = state_read_server_hello;
473 return ssl_hs_early_return;
474}
475
476static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
477 SSL *const ssl = hs->ssl;
478
479 assert(SSL_is_dtls(ssl));
480
481 SSLMessage msg;
482 if (!ssl->method->get_message(ssl, &msg)) {
483 return ssl_hs_read_message;
484 }
485
486 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400487 hs->state = state_read_server_hello;
488 return ssl_hs_ok;
489 }
490
491 CBS hello_verify_request = msg.body, cookie;
492 uint16_t server_version;
493 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
494 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
495 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
496 CBS_len(&hello_verify_request) != 0) {
497 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400498 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400499 return ssl_hs_error;
500 }
501
502 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
503 ssl->d1->cookie_len = CBS_len(&cookie);
504
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400505 ssl->method->next_message(ssl);
Steven Valdez398085b2017-08-29 13:25:56 -0400506
David Benjaminc11ea9422017-08-29 16:33:21 -0400507 // DTLS resets the handshake buffer after HelloVerifyRequest.
Steven Valdez398085b2017-08-29 13:25:56 -0400508 if (!hs->transcript.Init()) {
509 return ssl_hs_error;
510 }
511
512 if (!ssl_write_client_hello(hs)) {
513 return ssl_hs_error;
514 }
515
516 hs->state = state_read_server_hello;
517 return ssl_hs_flush;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400518}
519
520static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900521 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400522 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400523 if (!ssl->method->get_message(ssl, &msg)) {
524 return ssl_hs_read_server_hello;
Adam Langley1bea1732014-12-17 19:06:57 -0800525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamina0bc29a2018-03-13 16:51:58 -0400527 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
528 return ssl_hs_error;
529 }
530
531 CBS server_hello = msg.body, server_random, session_id;
532 uint16_t server_version, cipher_suite;
533 uint8_t compression_method;
534 if (!CBS_get_u16(&server_hello, &server_version) ||
535 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
536 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
537 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
538 !CBS_get_u16(&server_hello, &cipher_suite) ||
539 !CBS_get_u8(&server_hello, &compression_method)) {
540 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
541 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
542 return ssl_hs_error;
543 }
544
545 // Use the supported_versions extension if applicable.
546 if (!parse_supported_versions(hs, &server_version, &server_hello)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400547 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400548 }
549
Steven Valdez8f36c512017-06-20 10:55:02 -0400550 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400551 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind1e3ce12017-10-06 18:31:15 -0400552 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400553 return ssl_hs_error;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400554 }
555
David Benjamin0d56f882015-12-19 17:05:56 -0500556 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
557 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400558 ssl->version = server_version;
David Benjaminc11ea9422017-08-29 16:33:21 -0400559 // At this point, the connection's version is known and ssl->version is
560 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400561 ssl->s3->have_version = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400562 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400563 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400564 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind1e3ce12017-10-06 18:31:15 -0400565 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400566 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
David Benjamind1e3ce12017-10-06 18:31:15 -0400569 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400570 hs->state = state_tls13;
571 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400572 }
573
David Benjamin73d42e62017-09-21 03:05:35 -0400574 // Clear some TLS 1.3 state that no longer needs to be retained.
575 hs->key_share.reset();
576 hs->key_share_bytes.Reset();
577
578 // A TLS 1.2 server would not know to skip the early data we offered. Report
579 // an error code sooner. The caller may use this error code to implement the
David Benjamina130ce02018-08-14 22:26:39 -0500580 // fallback described in RFC 8446 appendix D.3.
Steven Valdez2d850622017-01-11 11:34:52 -0500581 if (hs->early_data_offered) {
582 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind1e3ce12017-10-06 18:31:15 -0400583 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400584 return ssl_hs_error;
Steven Valdez2d850622017-01-11 11:34:52 -0500585 }
586
David Benjaminc11ea9422017-08-29 16:33:21 -0400587 // Copy over the server random.
David Benjamind3bca042017-04-08 11:05:04 -0400588 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
589 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700590
Steven Valdezf1af1292018-08-13 10:54:48 -0400591 // Enforce the TLS 1.3 anti-downgrade feature.
David Benjamin6df65402017-12-18 18:00:23 -0500592 if (!ssl->s3->initial_handshake_complete &&
Steven Valdezf1af1292018-08-13 10:54:48 -0400593 ssl_supports_version(hs, TLS1_3_VERSION)) {
594 static_assert(
595 sizeof(kTLS12DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
596 "downgrade signals have different size");
597 auto suffix =
598 MakeConstSpan(ssl->s3->server_random, sizeof(ssl->s3->server_random))
599 .subspan(SSL3_RANDOM_SIZE - sizeof(kTLS13DowngradeRandom));
600 if (suffix == kTLS12DowngradeRandom || suffix == kTLS13DowngradeRandom) {
601 ssl->s3->tls13_downgrade = true;
602 if (!ssl->ctx->ignore_tls13_downgrade) {
603 OPENSSL_PUT_ERROR(SSL, SSL_R_TLS13_DOWNGRADE);
604 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
605 return ssl_hs_error;
606 }
607 }
David Benjamin6df65402017-12-18 18:00:23 -0500608 }
David Benjamin1f61f0d2016-07-10 12:20:35 -0400609
David Benjamin0d56f882015-12-19 17:05:56 -0500610 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400611 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500612 CBS_mem_equal(&session_id, ssl->session->session_id,
613 ssl->session->session_id_length)) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400614 ssl->s3->session_reused = true;
Adam Langley1bea1732014-12-17 19:06:57 -0800615 } else {
David Benjamin0ab3f0c2018-01-26 16:33:12 -0500616 // The server may also have echoed back the TLS 1.3 compatibility mode
617 // session ID. As we know this is not a session the server knows about, any
618 // server resuming it is in error. Reject the first connection
619 // deterministicly, rather than installing an invalid session into the
620 // session cache. https://crbug.com/796910
621 if (hs->session_id_len != 0 &&
622 CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len)) {
623 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
624 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
625 return ssl_hs_error;
626 }
627
David Benjaminc11ea9422017-08-29 16:33:21 -0400628 // The session wasn't resumed. Create a fresh SSL_SESSION to
629 // fill out.
David Benjamin4d0be242016-09-01 01:10:07 -0400630 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900631 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400632 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400633 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800634 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400635 // Note: session_id could be empty.
David Benjamin45738dd2017-02-09 20:01:26 -0500636 hs->new_session->session_id_length = CBS_len(&session_id);
637 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
638 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800639 }
David Benjamina03d95d2014-07-12 19:49:07 -0400640
David Benjaminca9e8f52017-08-09 15:02:34 -0400641 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
642 if (cipher == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400643 // unknown cipher
David Benjamin3570d732015-06-29 00:28:17 -0400644 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400645 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400646 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800647 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400648
David Benjaminc11ea9422017-08-29 16:33:21 -0400649 // The cipher must be allowed in the selected version and enabled.
David Benjamin2fe6e222017-01-25 02:18:43 -0500650 uint32_t mask_a, mask_k;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700651 ssl_get_client_disabled(hs, &mask_a, &mask_k);
David Benjaminca9e8f52017-08-09 15:02:34 -0400652 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
David Benjamind1e3ce12017-10-06 18:31:15 -0400653 SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
654 SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
David Benjaminca9e8f52017-08-09 15:02:34 -0400655 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400656 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400657 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400658 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800659 }
Adam Langley95c29f32014-06-20 12:00:00 -0700660
Steven Valdez87eab492016-06-27 16:34:59 -0400661 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500662 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400664 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400665 return ssl_hs_error;
David Benjaminece3de92015-03-16 18:02:20 -0400666 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400667 if (ssl->session->cipher != cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400668 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind1e3ce12017-10-06 18:31:15 -0400669 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400670 return ssl_hs_error;
Steven Valdez803c77a2016-09-06 14:13:43 -0400671 }
David Benjamin50596f82018-07-02 19:47:27 -0400672 if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400673 // This is actually a client application bug.
Steven Valdez4aa154e2016-07-29 14:32:55 -0400674 OPENSSL_PUT_ERROR(SSL,
675 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind1e3ce12017-10-06 18:31:15 -0400676 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400677 return ssl_hs_error;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400678 }
David Benjamin0623bce2015-12-25 15:40:14 -0500679 } else {
David Benjaminca9e8f52017-08-09 15:02:34 -0400680 hs->new_session->cipher = cipher;
Adam Langley1bea1732014-12-17 19:06:57 -0800681 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400682 hs->new_cipher = cipher;
David Benjaminbdf5e722014-11-11 00:52:15 -0500683
David Benjaminc11ea9422017-08-29 16:33:21 -0400684 // Now that the cipher is known, initialize the handshake hash and hash the
685 // ServerHello.
David Benjamind1e3ce12017-10-06 18:31:15 -0400686 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400687 !ssl_hash_message(hs, msg)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400688 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400689 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400690 }
691
David Benjaminc11ea9422017-08-29 16:33:21 -0400692 // If doing a full handshake, the server may request a client certificate
693 // which requires hashing the handshake transcript. Otherwise, the handshake
694 // buffer may be released.
David Benjamin3d458dc2016-09-12 22:40:27 +0000695 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500696 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400697 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -0800698 }
David Benjamina03d95d2014-07-12 19:49:07 -0400699
David Benjaminc11ea9422017-08-29 16:33:21 -0400700 // Only the NULL compression algorithm is supported.
Adam Langley1bea1732014-12-17 19:06:57 -0800701 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400702 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind1e3ce12017-10-06 18:31:15 -0400703 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400704 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800705 }
Adam Langley95c29f32014-06-20 12:00:00 -0700706
David Benjaminc11ea9422017-08-29 16:33:21 -0400707 // TLS extensions
David Benjamin8c880a22016-12-03 02:20:34 -0500708 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400709 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400710 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800711 }
Adam Langley95c29f32014-06-20 12:00:00 -0700712
David Benjaminc11ea9422017-08-29 16:33:21 -0400713 // There should be nothing left over in the record.
Adam Langley1bea1732014-12-17 19:06:57 -0800714 if (CBS_len(&server_hello) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400715 // wrong packet length
David Benjamin639846e2016-09-09 11:41:18 -0400716 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400717 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400718 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800719 }
Adam Langley95c29f32014-06-20 12:00:00 -0700720
Steven Valdez87eab492016-06-27 16:34:59 -0400721 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -0500722 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -0500723 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400724 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700725 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400726 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700727 }
David Benjamind1e3ce12017-10-06 18:31:15 -0400728 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400729 return ssl_hs_error;
Adam Langleyba5934b2015-06-02 10:50:35 -0700730 }
731
David Benjamin9f0e7cb2018-04-12 15:36:30 -0400732 if (ssl->s3->token_binding_negotiated &&
Nick Harper36fcc4c2017-09-21 15:02:22 -0700733 (!hs->extended_master_secret || !ssl->s3->send_connection_binding)) {
734 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
735 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
736 return ssl_hs_error;
737 }
738
David Benjamin8f94c312017-08-01 17:35:55 -0400739 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400740
741 if (ssl->session != NULL) {
Jesse Selover1c337e52018-08-10 13:28:47 -0400742 if (ssl->ctx->reverify_on_resume &&
743 ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
744 hs->state = state_reverify_server_certificate;
745 } else {
746 hs->state = state_read_session_ticket;
747 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400748 return ssl_hs_ok;
749 }
750
751 hs->state = state_read_server_certificate;
752 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800753}
Adam Langley95c29f32014-06-20 12:00:00 -0700754
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400755static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
756 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
757 if (wait == ssl_hs_ok) {
758 hs->state = state_finish_client_handshake;
759 return ssl_hs_ok;
760 }
761
762 return wait;
763}
764
765static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900766 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400767
768 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
769 hs->state = state_read_certificate_status;
770 return ssl_hs_ok;
771 }
772
David Benjamin7934f082017-08-01 16:32:25 -0400773 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400774 if (!ssl->method->get_message(ssl, &msg)) {
775 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800776 }
Adam Langley95c29f32014-06-20 12:00:00 -0700777
David Benjamin7934f082017-08-01 16:32:25 -0400778 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
779 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400780 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -0500781 }
782
David Benjamin7934f082017-08-01 16:32:25 -0400783 CBS body = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800784 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminbfdd1a92018-06-29 16:26:38 -0400785 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
786 NULL, &body, ssl->ctx->pool)) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400787 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400788 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800789 }
Adam Langley95c29f32014-06-20 12:00:00 -0700790
David Benjaminbfdd1a92018-06-29 16:26:38 -0400791 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -0400792 CBS_len(&body) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400793 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -0400794 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400795 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400796 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800797 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400798
Adam Langley05672202016-12-13 12:05:49 -0800799 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -0400800 hs, hs->peer_pubkey.get(),
David Benjaminbfdd1a92018-06-29 16:26:38 -0400801 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0))) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400802 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400803 return ssl_hs_error;
David Benjamin436bf822015-06-07 19:50:37 -0400804 }
805
David Benjamin8f94c312017-08-01 17:35:55 -0400806 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400807
808 hs->state = state_read_certificate_status;
809 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800810}
811
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400812static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900813 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400814
815 if (!hs->certificate_status_expected) {
816 hs->state = state_verify_server_certificate;
817 return ssl_hs_ok;
818 }
819
David Benjamin7934f082017-08-01 16:32:25 -0400820 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400821 if (!ssl->method->get_message(ssl, &msg)) {
822 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400823 }
824
David Benjamin7934f082017-08-01 16:32:25 -0400825 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400826 // A server may send status_request in ServerHello and then change its mind
827 // about sending CertificateStatus.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400828 hs->state = state_verify_server_certificate;
829 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400830 }
831
David Benjamin7934f082017-08-01 16:32:25 -0400832 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400833 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500834 }
835
David Benjamin7934f082017-08-01 16:32:25 -0400836 CBS certificate_status = msg.body, ocsp_response;
David Benjamin8d606e32017-06-15 22:43:04 -0400837 uint8_t status_type;
David Benjamin9f1dc822016-06-07 17:03:46 -0400838 if (!CBS_get_u8(&certificate_status, &status_type) ||
839 status_type != TLSEXT_STATUSTYPE_ocsp ||
840 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
841 CBS_len(&ocsp_response) == 0 ||
842 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400843 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400844 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400845 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400846 }
847
David Benjaminbfdd1a92018-06-29 16:26:38 -0400848 hs->new_session->ocsp_response.reset(
849 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool));
David Benjamin8fc2dc02017-08-22 15:07:51 -0700850 if (hs->new_session->ocsp_response == nullptr) {
David Benjamind1e3ce12017-10-06 18:31:15 -0400851 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400852 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400853 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400854
David Benjamin8f94c312017-08-01 17:35:55 -0400855 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400856
857 hs->state = state_verify_server_certificate;
858 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400859}
860
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400861static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
862 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
863 hs->state = state_read_server_key_exchange;
864 return ssl_hs_ok;
865 }
866
867 switch (ssl_verify_peer_cert(hs)) {
868 case ssl_verify_ok:
869 break;
870 case ssl_verify_invalid:
871 return ssl_hs_error;
872 case ssl_verify_retry:
873 hs->state = state_verify_server_certificate;
874 return ssl_hs_certificate_verify;
875 }
876
877 hs->state = state_read_server_key_exchange;
878 return ssl_hs_ok;
879}
880
Jesse Selover1c337e52018-08-10 13:28:47 -0400881static enum ssl_hs_wait_t do_reverify_server_certificate(SSL_HANDSHAKE *hs) {
882 assert(hs->ssl->ctx->reverify_on_resume);
883
884 switch (ssl_reverify_peer_cert(hs)) {
885 case ssl_verify_ok:
886 break;
887 case ssl_verify_invalid:
888 return ssl_hs_error;
889 case ssl_verify_retry:
890 hs->state = state_reverify_server_certificate;
891 return ssl_hs_certificate_verify;
892 }
893
894 hs->state = state_read_session_ticket;
895 return ssl_hs_ok;
896}
897
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400898static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900899 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400900 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400901 if (!ssl->method->get_message(ssl, &msg)) {
902 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800903 }
904
David Benjamin7934f082017-08-01 16:32:25 -0400905 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400906 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
David Benjamin45738dd2017-02-09 20:01:26 -0500907 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400908 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamind1e3ce12017-10-06 18:31:15 -0400909 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400910 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800911 }
912
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400913 hs->state = state_read_certificate_request;
914 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800915 }
916
David Benjamin7934f082017-08-01 16:32:25 -0400917 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400918 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500919 }
920
David Benjamin45738dd2017-02-09 20:01:26 -0500921 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
922 uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin7934f082017-08-01 16:32:25 -0400923 CBS server_key_exchange = msg.body;
Adam Langley1bea1732014-12-17 19:06:57 -0800924 if (alg_a & SSL_aPSK) {
925 CBS psk_identity_hint;
926
David Benjaminc11ea9422017-08-29 16:33:21 -0400927 // Each of the PSK key exchanges begins with a psk_identity_hint.
Adam Langley1bea1732014-12-17 19:06:57 -0800928 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
929 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400930 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400931 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400932 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800933 }
934
David Benjamind1e3ce12017-10-06 18:31:15 -0400935 // Store the PSK identity hint for the ClientKeyExchange. Assume that the
936 // maximum length of a PSK identity hint can be as long as the maximum
937 // length of a PSK identity. Also do not allow NULL characters; identities
938 // are saved as C strings.
David Benjaminc11ea9422017-08-29 16:33:21 -0400939 //
940 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
941 // a specific identity.
Adam Langley1bea1732014-12-17 19:06:57 -0800942 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
943 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400944 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind1e3ce12017-10-06 18:31:15 -0400945 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400946 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800947 }
948
David Benjaminc11ea9422017-08-29 16:33:21 -0400949 // Save non-empty identity hints as a C string. Empty identity hints we
950 // treat as missing. Plain PSK makes it possible to send either no hint
951 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
952 // empty hint. Having different capabilities is odd, so we interpret empty
953 // and missing as identical.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400954 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -0400955 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -0400956 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -0400957 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind1e3ce12017-10-06 18:31:15 -0400958 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400959 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800960 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400961 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -0800962 }
963
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700964 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400965 // Parse the server parameters.
Steven Valdezce902a92016-05-17 11:47:53 -0400966 uint8_t group_type;
967 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -0500968 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -0400969 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
970 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -0700971 !CBS_get_u16(&server_key_exchange, &group_id) ||
972 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -0400973 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -0400974 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400975 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800976 }
David Benjamin45738dd2017-02-09 20:01:26 -0500977 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -0800978
David Benjaminc11ea9422017-08-29 16:33:21 -0400979 // Ensure the group is consistent with preferences.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700980 if (!tls1_check_group_id(hs, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -0500981 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamind1e3ce12017-10-06 18:31:15 -0400982 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400983 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800984 }
David Benjamin4298d772015-12-19 00:18:25 -0500985
David Benjaminc11ea9422017-08-29 16:33:21 -0400986 // Initialize ECDH and save the peer public key for later.
David Benjaminc642aca2017-07-19 23:28:43 -0400987 hs->key_share = SSLKeyShare::Create(group_id);
988 if (!hs->key_share ||
David Benjamin08f5c762017-09-21 02:43:05 -0400989 !hs->peer_key.CopyFrom(point)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400990 return ssl_hs_error;
David Benjaminc1cc8582015-12-17 01:22:15 -0500991 }
Adam Langley1bea1732014-12-17 19:06:57 -0800992 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -0400993 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamind1e3ce12017-10-06 18:31:15 -0400994 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400995 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800996 }
997
David Benjaminc11ea9422017-08-29 16:33:21 -0400998 // At this point, |server_key_exchange| contains the signature, if any, while
999 // |msg.body| contains the entire message. From that, derive a CBS containing
1000 // just the parameter.
David Benjaminb35d6842015-12-29 17:08:08 -05001001 CBS parameter;
David Benjamin7934f082017-08-01 16:32:25 -04001002 CBS_init(&parameter, CBS_data(&msg.body),
1003 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
Adam Langley1bea1732014-12-17 19:06:57 -08001004
David Benjaminc11ea9422017-08-29 16:33:21 -04001005 // ServerKeyExchange should be signed by the server's public key.
David Benjamin45738dd2017-02-09 20:01:26 -05001006 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001007 uint16_t signature_algorithm = 0;
David Benjamind1e3ce12017-10-06 18:31:15 -04001008 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001009 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001010 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001011 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001012 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001013 }
David Benjamin8d606e32017-06-15 22:43:04 -04001014 uint8_t alert = SSL_AD_DECODE_ERROR;
1015 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001016 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001017 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001018 }
David Benjamin45738dd2017-02-09 20:01:26 -05001019 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001020 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001021 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001022 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001023 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001024 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001025 }
1026
David Benjaminc11ea9422017-08-29 16:33:21 -04001027 // The last field in |server_key_exchange| is the signature.
David Benjaminf4958e72015-06-07 11:00:53 -04001028 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001029 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1030 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001031 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001032 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001033 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001034 }
1035
David Benjamin1386aad2017-07-19 23:57:40 -04001036 ScopedCBB transcript;
David Benjamin75a1f232017-10-11 17:19:19 -04001037 Array<uint8_t> transcript_data;
David Benjamin1386aad2017-07-19 23:57:40 -04001038 if (!CBB_init(transcript.get(),
1039 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1040 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1041 SSL3_RANDOM_SIZE) ||
1042 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1043 SSL3_RANDOM_SIZE) ||
1044 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1045 CBS_len(&parameter)) ||
David Benjamin75a1f232017-10-11 17:19:19 -04001046 !CBBFinishArray(transcript.get(), &transcript_data)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001047 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind1e3ce12017-10-06 18:31:15 -04001048 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001049 return ssl_hs_error;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001050 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001051
David Benjamin75a1f232017-10-11 17:19:19 -04001052 bool sig_ok = ssl_public_key_verify(ssl, signature, signature_algorithm,
1053 hs->peer_pubkey.get(), transcript_data);
David Benjaminbf82aed2016-03-01 22:57:40 -05001054#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin75a1f232017-10-11 17:19:19 -04001055 sig_ok = true;
David Benjaminbf82aed2016-03-01 22:57:40 -05001056 ERR_clear_error();
1057#endif
1058 if (!sig_ok) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001059 // bad signature
David Benjamin3570d732015-06-29 00:28:17 -04001060 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001061 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001062 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001063 }
1064 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001065 // PSK ciphers are the only supported certificate-less ciphers.
David Benjaminf4958e72015-06-07 11:00:53 -04001066 assert(alg_a == SSL_aPSK);
1067
Adam Langley1bea1732014-12-17 19:06:57 -08001068 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001069 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamind1e3ce12017-10-06 18:31:15 -04001070 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001071 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001072 }
1073 }
David Benjamin8f94c312017-08-01 17:35:55 -04001074
1075 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001076 hs->state = state_read_certificate_request;
1077 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001078}
Adam Langley95c29f32014-06-20 12:00:00 -07001079
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001080static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001081 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001082
1083 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1084 hs->state = state_read_server_hello_done;
1085 return ssl_hs_ok;
1086 }
1087
David Benjamin7934f082017-08-01 16:32:25 -04001088 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001089 if (!ssl->method->get_message(ssl, &msg)) {
1090 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001091 }
David Benjamin688d8df2014-11-02 23:06:42 -05001092
David Benjamin7934f082017-08-01 16:32:25 -04001093 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001094 // If we get here we don't need the handshake buffer as we won't be doing
1095 // client auth.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001096 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001097 hs->state = state_read_server_hello_done;
1098 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001099 }
David Benjamined439582014-07-14 19:13:02 -04001100
David Benjamin7934f082017-08-01 16:32:25 -04001101 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1102 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001103 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001104 }
Adam Langley95c29f32014-06-20 12:00:00 -07001105
David Benjaminc11ea9422017-08-29 16:33:21 -04001106 // Get the certificate types.
David Benjamin7934f082017-08-01 16:32:25 -04001107 CBS body = msg.body, certificate_types;
1108 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001109 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001110 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001111 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001112 }
David Benjamined439582014-07-14 19:13:02 -04001113
David Benjamin08f5c762017-09-21 02:43:05 -04001114 if (!hs->certificate_types.CopyFrom(certificate_types)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001115 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001116 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001117 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001118
David Benjamind1e3ce12017-10-06 18:31:15 -04001119 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001120 CBS supported_signature_algorithms;
David Benjamin7934f082017-08-01 16:32:25 -04001121 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001122 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001123 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001124 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001125 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001126 }
Adam Langley1bea1732014-12-17 19:06:57 -08001127 }
David Benjamined439582014-07-14 19:13:02 -04001128
Adam Langleyc68e5b92017-02-08 13:33:15 -08001129 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001130 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -04001131 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001132 if (!ca_names) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001133 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001134 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001135 }
Adam Langley95c29f32014-06-20 12:00:00 -07001136
David Benjamin7934f082017-08-01 16:32:25 -04001137 if (CBS_len(&body) != 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001138 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001139 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001140 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001141 }
1142
David Benjaminfd45ee72017-08-31 14:49:09 -04001143 hs->cert_request = true;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001144 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001145 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001146
David Benjamin8f94c312017-08-01 17:35:55 -04001147 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001148 hs->state = state_read_server_hello_done;
1149 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001150}
Adam Langley95c29f32014-06-20 12:00:00 -07001151
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001152static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001153 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001154 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001155 if (!ssl->method->get_message(ssl, &msg)) {
1156 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001157 }
David Benjamin46062682014-07-14 19:14:32 -04001158
David Benjamin7934f082017-08-01 16:32:25 -04001159 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1160 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001161 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001162 }
1163
David Benjaminc11ea9422017-08-29 16:33:21 -04001164 // ServerHelloDone is empty.
David Benjamin7934f082017-08-01 16:32:25 -04001165 if (CBS_len(&msg.body) != 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001166 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001167 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001168 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001169 }
David Benjamin46062682014-07-14 19:14:32 -04001170
David Benjamin8f94c312017-08-01 17:35:55 -04001171 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001172 hs->state = state_send_client_certificate;
1173 return ssl_hs_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001174}
Adam Langley1258b6a2014-06-20 12:00:00 -07001175
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001176static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001177 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001178
David Benjaminc11ea9422017-08-29 16:33:21 -04001179 // The peer didn't request a certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001180 if (!hs->cert_request) {
1181 hs->state = state_send_client_key_exchange;
1182 return ssl_hs_ok;
1183 }
1184
David Benjaminc11ea9422017-08-29 16:33:21 -04001185 // Call cert_cb to update the certificate.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001186 if (hs->config->cert->cert_cb != NULL) {
1187 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001188 if (rv == 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001189 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001190 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1191 return ssl_hs_error;
1192 }
1193 if (rv < 0) {
1194 hs->state = state_send_client_certificate;
1195 return ssl_hs_x509_lookup;
David Benjamin9f1dc822016-06-07 17:03:46 -04001196 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001197 }
1198
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001199 if (!ssl_has_certificate(hs->config)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001200 // Without a client certificate, the handshake buffer may be released.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001201 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001202 }
1203
David Benjamina232a712017-03-30 15:51:53 -05001204 if (!ssl_on_certificate_selected(hs) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001205 !ssl_output_cert_chain(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001206 return ssl_hs_error;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001207 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001208
1209
1210 hs->state = state_send_client_key_exchange;
1211 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001212}
1213
David Benjamina3d76d02017-07-14 19:36:07 -04001214static_assert(sizeof(size_t) >= sizeof(unsigned),
1215 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001216
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001217static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001218 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001219 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001220 CBB body;
1221 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001222 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001223 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001224 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001225
David Benjamin499742c2017-07-22 12:45:38 -04001226 Array<uint8_t> pms;
David Benjamin45738dd2017-02-09 20:01:26 -05001227 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1228 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001229
David Benjaminc11ea9422017-08-29 16:33:21 -04001230 // If using a PSK key exchange, prepare the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001231 unsigned psk_len = 0;
1232 uint8_t psk[PSK_MAX_PSK_LEN];
1233 if (alg_a & SSL_aPSK) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001234 if (hs->config->psk_client_callback == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001235 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
David Benjamin499742c2017-07-22 12:45:38 -04001236 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001237 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001238
David Benjaminae0eaaa2015-12-17 02:01:25 -05001239 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001240 OPENSSL_memset(identity, 0, sizeof(identity));
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001241 psk_len = hs->config->psk_client_callback(
1242 ssl, hs->peer_psk_identity_hint.get(), identity, sizeof(identity), psk,
1243 sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001244 if (psk_len == 0) {
1245 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamind1e3ce12017-10-06 18:31:15 -04001246 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001247 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001248 }
1249 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001250
David Benjaminbfdd1a92018-06-29 16:26:38 -04001251 hs->new_session->psk_identity.reset(BUF_strdup(identity));
1252 if (hs->new_session->psk_identity == nullptr) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001253 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001254 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001255 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001256
David Benjaminc11ea9422017-08-29 16:33:21 -04001257 // Write out psk_identity.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001258 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001259 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001260 !CBB_add_bytes(&child, (const uint8_t *)identity,
1261 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001262 !CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001263 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001264 }
1265 }
David Benjamin758d1272015-11-20 17:47:25 -05001266
David Benjamin499742c2017-07-22 12:45:38 -04001267 // Depending on the key exchange method, compute |pms|.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001268 if (alg_k & SSL_kRSA) {
David Benjamin499742c2017-07-22 12:45:38 -04001269 if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
1270 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001271 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001272
David Benjamin31b0c9b2017-07-20 14:49:15 -04001273 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001274 if (rsa == NULL) {
1275 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001276 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001277 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001278
David Benjaminf04c2e92016-12-06 13:35:25 -05001279 pms[0] = hs->client_version >> 8;
1280 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001281 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
David Benjamin499742c2017-07-22 12:45:38 -04001282 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001283 }
1284
David Benjamin9bb15f52018-06-26 00:07:40 -04001285 CBB enc_pms;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001286 uint8_t *ptr;
David Benjamin9bb15f52018-06-26 00:07:40 -04001287 size_t enc_pms_len;
1288 if (!CBB_add_u16_length_prefixed(&body, &enc_pms) ||
1289 !CBB_reserve(&enc_pms, &ptr, RSA_size(rsa)) ||
David Benjamin499742c2017-07-22 12:45:38 -04001290 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
1291 pms.size(), RSA_PKCS1_PADDING) ||
David Benjamin9bb15f52018-06-26 00:07:40 -04001292 !CBB_did_write(&enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001293 !CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001294 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001295 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001296 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001297 // Generate a keypair and serialize the public half.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001298 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001299 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamin499742c2017-07-22 12:45:38 -04001300 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001301 }
1302
David Benjaminc11ea9422017-08-29 16:33:21 -04001303 // Compute the premaster.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001304 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin499742c2017-07-22 12:45:38 -04001305 if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001306 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin499742c2017-07-22 12:45:38 -04001307 return ssl_hs_error;
David Benjamin4298d772015-12-19 00:18:25 -05001308 }
David Benjamin75836432016-06-17 18:48:29 -04001309 if (!CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001310 return ssl_hs_error;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001311 }
David Benjamin4298d772015-12-19 00:18:25 -05001312
David Benjaminc11ea9422017-08-29 16:33:21 -04001313 // The key exchange state may now be discarded.
David Benjaminc642aca2017-07-19 23:28:43 -04001314 hs->key_share.reset();
David Benjamin499742c2017-07-22 12:45:38 -04001315 hs->peer_key.Reset();
David Benjaminae0eaaa2015-12-17 02:01:25 -05001316 } else if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001317 // For plain PSK, other_secret is a block of 0s with the same length as
1318 // the pre-shared key.
David Benjamin499742c2017-07-22 12:45:38 -04001319 if (!pms.Init(psk_len)) {
1320 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001321 }
David Benjamin499742c2017-07-22 12:45:38 -04001322 OPENSSL_memset(pms.data(), 0, pms.size());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001323 } else {
David Benjamind1e3ce12017-10-06 18:31:15 -04001324 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001325 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001326 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001327 }
1328
David Benjaminc11ea9422017-08-29 16:33:21 -04001329 // For a PSK cipher suite, other_secret is combined with the pre-shared
1330 // key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001331 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001332 ScopedCBB pms_cbb;
1333 CBB child;
David Benjamin499742c2017-07-22 12:45:38 -04001334 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001335 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjamin499742c2017-07-22 12:45:38 -04001336 !CBB_add_bytes(&child, pms.data(), pms.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001337 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001338 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin75a1f232017-10-11 17:19:19 -04001339 !CBBFinishArray(pms_cbb.get(), &pms)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001340 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001341 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001342 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001343 }
1344
David Benjaminc11ea9422017-08-29 16:33:21 -04001345 // The message must be added to the finished hash before calculating the
1346 // master secret.
David Benjamind781fc42017-07-12 16:25:57 -04001347 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin499742c2017-07-22 12:45:38 -04001348 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001349 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001350
David Benjamin2637f3c2017-10-27 00:56:55 -04001351 hs->new_session->master_key_length =
1352 tls1_generate_master_secret(hs, hs->new_session->master_key, pms);
David Benjamin45738dd2017-02-09 20:01:26 -05001353 if (hs->new_session->master_key_length == 0) {
David Benjamin499742c2017-07-22 12:45:38 -04001354 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001355 }
David Benjaminfc02b592017-02-17 16:26:01 -05001356 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001357
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001358 hs->state = state_send_client_certificate_verify;
1359 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001360}
Adam Langley1258b6a2014-06-20 12:00:00 -07001361
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001362static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001363 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001364
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001365 if (!hs->cert_request || !ssl_has_certificate(hs->config)) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001366 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001367 return ssl_hs_ok;
1368 }
1369
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001370 assert(ssl_has_private_key(hs->config));
David Benjamin86e95b82017-07-18 16:34:25 -04001371 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001372 CBB body, child;
1373 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001374 SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001375 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001376 }
1377
David Benjaminea9a0d52016-07-08 15:52:59 -07001378 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001379 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Adam Langleye0afc852018-07-09 16:59:33 -07001380 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001381 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001382 }
David Benjamind1e3ce12017-10-06 18:31:15 -04001383 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001384 // Write out the digest type in TLS 1.2.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001385 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001386 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001387 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001388 }
1389 }
David Benjaminfc825122015-12-18 01:57:43 -05001390
David Benjaminc11ea9422017-08-29 16:33:21 -04001391 // Set aside space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001392 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001393 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001394 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001395 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001396 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001397 }
1398
Steven Valdez2b8415e2016-06-30 13:27:23 -04001399 size_t sig_len = max_sig_len;
David Benjamin9bb15f52018-06-26 00:07:40 -04001400 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1401 signature_algorithm,
1402 hs->transcript.buffer())) {
1403 case ssl_private_key_success:
1404 break;
1405 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001406 return ssl_hs_error;
David Benjamin9bb15f52018-06-26 00:07:40 -04001407 case ssl_private_key_retry:
1408 hs->state = state_send_client_certificate_verify;
1409 return ssl_hs_private_key_operation;
David Benjaminfc825122015-12-18 01:57:43 -05001410 }
1411
David Benjaminfc825122015-12-18 01:57:43 -05001412 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001413 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001414 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001415 }
1416
David Benjaminc11ea9422017-08-29 16:33:21 -04001417 // The handshake buffer is no longer necessary.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001418 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001419
David Benjamin4cbb9312017-08-31 14:52:31 -04001420 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001421 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001422}
1423
David Benjamin4cbb9312017-08-31 14:52:31 -04001424static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001425 SSL *const ssl = hs->ssl;
David Benjamin4cbb9312017-08-31 14:52:31 -04001426 // Resolve Channel ID first, before any non-idempotent operations.
David Benjamin46853762018-07-03 14:01:26 -04001427 if (ssl->s3->channel_id_valid) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -07001428 if (!ssl_do_channel_id_callback(hs)) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001429 return ssl_hs_error;
1430 }
1431
David Benjamin46853762018-07-03 14:01:26 -04001432 if (hs->config->channel_id_private == NULL) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001433 hs->state = state_send_client_finished;
1434 return ssl_hs_channel_id_lookup;
1435 }
1436 }
David Benjamine97b9622015-10-10 14:59:09 -04001437
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001438 if (!ssl->method->add_change_cipher_spec(ssl) ||
David Benjamine58f8a62017-09-21 19:07:15 -04001439 !tls1_change_cipher_state(hs, evp_aead_seal)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001440 return ssl_hs_error;
David Benjamine97b9622015-10-10 14:59:09 -04001441 }
1442
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001443 if (hs->next_proto_neg_seen) {
1444 static const uint8_t kZero[32] = {0};
David Benjamin8e7bbba2017-10-13 17:18:35 -04001445 size_t padding_len =
1446 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001447
1448 ScopedCBB cbb;
1449 CBB body, child;
1450 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1451 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin8e7bbba2017-10-13 17:18:35 -04001452 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
1453 ssl->s3->next_proto_negotiated.size()) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001454 !CBB_add_u8_length_prefixed(&body, &child) ||
1455 !CBB_add_bytes(&child, kZero, padding_len) ||
1456 !ssl_add_message_cbb(ssl, cbb.get())) {
1457 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1458 return ssl_hs_error;
1459 }
1460 }
1461
David Benjamin46853762018-07-03 14:01:26 -04001462 if (ssl->s3->channel_id_valid) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001463 ScopedCBB cbb;
1464 CBB body;
1465 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1466 !tls1_write_channel_id(hs, &body) ||
1467 !ssl_add_message_cbb(ssl, cbb.get())) {
1468 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1469 return ssl_hs_error;
1470 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001471 }
1472
David Benjamin00f48c82017-10-06 18:43:53 -04001473 if (!ssl_send_finished(hs)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001474 return ssl_hs_error;
1475 }
1476
1477 hs->state = state_finish_flight;
1478 return ssl_hs_flush;
1479}
1480
David Benjamin00f48c82017-10-06 18:43:53 -04001481static bool can_false_start(const SSL_HANDSHAKE *hs) {
1482 SSL *const ssl = hs->ssl;
1483
Steven Valdezf1af1292018-08-13 10:54:48 -04001484 // False Start bypasses the Finished check's downgrade protection. This can
1485 // enable attacks where we send data under weaker settings than supported
1486 // (e.g. the Logjam attack). Thus we require TLS 1.2 with an ECDHE+AEAD
1487 // cipher, our strongest settings before TLS 1.3.
1488 //
1489 // Now that TLS 1.3 exists, we would like to avoid similar attacks between
1490 // TLS 1.2 and TLS 1.3, but there are too many TLS 1.2 deployments to
1491 // sacrifice False Start on them. TLS 1.3's downgrade signal fixes this, but
1492 // |SSL_CTX_set_ignore_tls13_downgrade| can disable it due to compatibility
1493 // issues.
1494 //
1495 // |SSL_CTX_set_ignore_tls13_downgrade| normally still retains Finished-based
1496 // downgrade protection, but False Start bypasses that. Thus, we disable False
1497 // Start based on the TLS 1.3 downgrade signal, even if otherwise unenforced.
David Benjamin494e4d02017-12-01 13:05:12 -05001498 if (SSL_is_dtls(ssl) ||
1499 SSL_version(ssl) != TLS1_2_VERSION ||
1500 hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
Steven Valdezf1af1292018-08-13 10:54:48 -04001501 hs->new_cipher->algorithm_mac != SSL_AEAD ||
1502 ssl->s3->tls13_downgrade) {
David Benjamin494e4d02017-12-01 13:05:12 -05001503 return false;
1504 }
1505
1506 // Additionally require ALPN or NPN by default.
1507 //
1508 // TODO(davidben): Can this constraint be relaxed globally now that cipher
Steven Valdezf1af1292018-08-13 10:54:48 -04001509 // suite requirements have been tightened?
David Benjamin494e4d02017-12-01 13:05:12 -05001510 if (!ssl->ctx->false_start_allowed_without_alpn &&
1511 ssl->s3->alpn_selected.empty() &&
1512 ssl->s3->next_proto_negotiated.empty()) {
1513 return false;
1514 }
1515
1516 return true;
David Benjamin00f48c82017-10-06 18:43:53 -04001517}
1518
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001519static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001520 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001521 if (ssl->session != NULL) {
1522 hs->state = state_finish_client_handshake;
1523 return ssl_hs_ok;
1524 }
1525
David Benjaminc11ea9422017-08-29 16:33:21 -04001526 // This is a full handshake. If it involves ChannelID, then record the
1527 // handshake hashes at this point in the session so that any resumption of
1528 // this session with ChannelID can sign those hashes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001529 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1530 return ssl_hs_error;
1531 }
1532
1533 hs->state = state_read_session_ticket;
1534
1535 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
David Benjamin00f48c82017-10-06 18:43:53 -04001536 can_false_start(hs) &&
David Benjaminc11ea9422017-08-29 16:33:21 -04001537 // No False Start on renegotiation (would complicate the state machine).
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001538 !ssl->s3->initial_handshake_complete) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001539 hs->in_false_start = true;
1540 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001541 return ssl_hs_early_return;
1542 }
1543
1544 return ssl_hs_ok;
1545}
1546
1547static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1548 SSL *const ssl = hs->ssl;
1549
1550 if (!hs->ticket_expected) {
1551 hs->state = state_process_change_cipher_spec;
1552 return ssl_hs_read_change_cipher_spec;
1553 }
1554
David Benjamin7934f082017-08-01 16:32:25 -04001555 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001556 if (!ssl->method->get_message(ssl, &msg)) {
1557 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001558 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001559
David Benjamin7934f082017-08-01 16:32:25 -04001560 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1561 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001562 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001563 }
1564
David Benjamin7934f082017-08-01 16:32:25 -04001565 CBS new_session_ticket = msg.body, ticket;
David Benjaminbfdd1a92018-06-29 16:26:38 -04001566 uint32_t ticket_lifetime_hint;
1567 if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001568 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1569 CBS_len(&new_session_ticket) != 0) {
David Benjamind1e3ce12017-10-06 18:31:15 -04001570 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001571 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001572 return ssl_hs_error;
David Benjamin454aa4c2016-03-10 08:43:27 -05001573 }
1574
David Benjamin9f1dc822016-06-07 17:03:46 -04001575 if (CBS_len(&ticket) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001576 // RFC 5077 allows a server to change its mind and send no ticket after
1577 // negotiating the extension. The value of |ticket_expected| is checked in
1578 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
David Benjaminfd45ee72017-08-31 14:49:09 -04001579 hs->ticket_expected = false;
David Benjamin8f94c312017-08-01 17:35:55 -04001580 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001581 hs->state = state_process_change_cipher_spec;
1582 return ssl_hs_read_change_cipher_spec;
David Benjamin9f1dc822016-06-07 17:03:46 -04001583 }
1584
David Benjamin31b0c9b2017-07-20 14:49:15 -04001585 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001586 UniquePtr<SSL_SESSION> renewed_session;
1587 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001588 // The server is sending a new ticket for an existing session. Sessions are
1589 // immutable once established, so duplicate all but the ticket of the
1590 // existing session.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001591 renewed_session =
David Benjamin50596f82018-07-02 19:47:27 -04001592 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001593 if (!renewed_session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001594 // This should never happen.
David Benjamin9f1dc822016-06-07 17:03:46 -04001595 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001596 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001597 }
David Benjamin1386aad2017-07-19 23:57:40 -04001598 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001599 }
1600
David Benjaminbfdd1a92018-06-29 16:26:38 -04001601 // |ticket_lifetime_hint| is measured from when the ticket was issued.
David Benjamin17b30832017-01-28 14:00:32 -05001602 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001603
David Benjaminbfdd1a92018-06-29 16:26:38 -04001604 if (!session->ticket.CopyFrom(ticket)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001605 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001606 }
David Benjaminbfdd1a92018-06-29 16:26:38 -04001607 session->ticket_lifetime_hint = ticket_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001608
David Benjaminc11ea9422017-08-29 16:33:21 -04001609 // Generate a session ID for this session based on the session ticket. We use
1610 // the session ID mechanism for detecting ticket resumption. This also fits in
1611 // with assumptions elsewhere in OpenSSL.
Steven Valdez87eab492016-06-27 16:34:59 -04001612 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1613 session->session_id, &session->session_id_length,
1614 EVP_sha256(), NULL)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001615 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001616 }
1617
David Benjamin1386aad2017-07-19 23:57:40 -04001618 if (renewed_session) {
David Benjamina3a71e92018-06-29 13:24:45 -04001619 session->not_resumable = false;
David Benjamin50596f82018-07-02 19:47:27 -04001620 ssl->session = std::move(renewed_session);
Steven Valdez87eab492016-06-27 16:34:59 -04001621 }
1622
David Benjamin8f94c312017-08-01 17:35:55 -04001623 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001624 hs->state = state_process_change_cipher_spec;
1625 return ssl_hs_read_change_cipher_spec;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001626}
David Benjamin86e95b82017-07-18 16:34:25 -04001627
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001628static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
David Benjamine58f8a62017-09-21 19:07:15 -04001629 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001630 return ssl_hs_error;
1631 }
1632
1633 hs->state = state_read_server_finished;
1634 return ssl_hs_ok;
1635}
1636
1637static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1638 SSL *const ssl = hs->ssl;
1639 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1640 if (wait != ssl_hs_ok) {
1641 return wait;
1642 }
1643
1644 if (ssl->session != NULL) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001645 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001646 return ssl_hs_ok;
1647 }
1648
1649 hs->state = state_finish_client_handshake;
1650 return ssl_hs_ok;
1651}
1652
1653static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1654 SSL *const ssl = hs->ssl;
1655
1656 ssl->method->on_handshake_complete(ssl);
1657
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001658 if (ssl->session != NULL) {
David Benjamin2908dd12018-06-29 17:46:42 -04001659 ssl->s3->established_session = UpRef(ssl->session);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001660 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001661 // We make a copy of the session in order to maintain the immutability
1662 // of the new established_session due to False Start. The caller may
1663 // have taken a reference to the temporary session.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001664 ssl->s3->established_session =
David Benjamin8e7bbba2017-10-13 17:18:35 -04001665 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
1666 if (!ssl->s3->established_session) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001667 return ssl_hs_error;
1668 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001669 // Renegotiations do not participate in session resumption.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001670 if (!ssl->s3->initial_handshake_complete) {
David Benjamina3a71e92018-06-29 13:24:45 -04001671 ssl->s3->established_session->not_resumable = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001672 }
1673
1674 hs->new_session.reset();
1675 }
1676
David Benjaminfd45ee72017-08-31 14:49:09 -04001677 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001678 ssl->s3->initial_handshake_complete = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001679 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
1680
1681 hs->state = state_done;
1682 return ssl_hs_ok;
1683}
1684
1685enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1686 while (hs->state != state_done) {
1687 enum ssl_hs_wait_t ret = ssl_hs_error;
1688 enum ssl_client_hs_state_t state =
1689 static_cast<enum ssl_client_hs_state_t>(hs->state);
1690 switch (state) {
1691 case state_start_connect:
1692 ret = do_start_connect(hs);
1693 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001694 case state_enter_early_data:
1695 ret = do_enter_early_data(hs);
1696 break;
1697 case state_read_hello_verify_request:
1698 ret = do_read_hello_verify_request(hs);
1699 break;
1700 case state_read_server_hello:
1701 ret = do_read_server_hello(hs);
1702 break;
1703 case state_tls13:
1704 ret = do_tls13(hs);
1705 break;
1706 case state_read_server_certificate:
1707 ret = do_read_server_certificate(hs);
1708 break;
1709 case state_read_certificate_status:
1710 ret = do_read_certificate_status(hs);
1711 break;
1712 case state_verify_server_certificate:
1713 ret = do_verify_server_certificate(hs);
1714 break;
Jesse Selover1c337e52018-08-10 13:28:47 -04001715 case state_reverify_server_certificate:
1716 ret = do_reverify_server_certificate(hs);
1717 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001718 case state_read_server_key_exchange:
1719 ret = do_read_server_key_exchange(hs);
1720 break;
1721 case state_read_certificate_request:
1722 ret = do_read_certificate_request(hs);
1723 break;
1724 case state_read_server_hello_done:
1725 ret = do_read_server_hello_done(hs);
1726 break;
1727 case state_send_client_certificate:
1728 ret = do_send_client_certificate(hs);
1729 break;
1730 case state_send_client_key_exchange:
1731 ret = do_send_client_key_exchange(hs);
1732 break;
1733 case state_send_client_certificate_verify:
1734 ret = do_send_client_certificate_verify(hs);
1735 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001736 case state_send_client_finished:
1737 ret = do_send_client_finished(hs);
1738 break;
1739 case state_finish_flight:
1740 ret = do_finish_flight(hs);
1741 break;
1742 case state_read_session_ticket:
1743 ret = do_read_session_ticket(hs);
1744 break;
1745 case state_process_change_cipher_spec:
1746 ret = do_process_change_cipher_spec(hs);
1747 break;
1748 case state_read_server_finished:
1749 ret = do_read_server_finished(hs);
1750 break;
1751 case state_finish_client_handshake:
1752 ret = do_finish_client_handshake(hs);
1753 break;
1754 case state_done:
1755 ret = ssl_hs_ok;
1756 break;
1757 }
1758
1759 if (hs->state != state) {
1760 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1761 }
1762
1763 if (ret != ssl_hs_ok) {
1764 return ret;
1765 }
1766 }
1767
1768 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1769 return ssl_hs_ok;
1770}
1771
1772const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1773 enum ssl_client_hs_state_t state =
1774 static_cast<enum ssl_client_hs_state_t>(hs->state);
1775 switch (state) {
1776 case state_start_connect:
1777 return "TLS client start_connect";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001778 case state_enter_early_data:
1779 return "TLS client enter_early_data";
1780 case state_read_hello_verify_request:
1781 return "TLS client read_hello_verify_request";
1782 case state_read_server_hello:
1783 return "TLS client read_server_hello";
1784 case state_tls13:
1785 return tls13_client_handshake_state(hs);
1786 case state_read_server_certificate:
1787 return "TLS client read_server_certificate";
1788 case state_read_certificate_status:
1789 return "TLS client read_certificate_status";
1790 case state_verify_server_certificate:
1791 return "TLS client verify_server_certificate";
Jesse Selover1c337e52018-08-10 13:28:47 -04001792 case state_reverify_server_certificate:
1793 return "TLS client reverify_server_certificate";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001794 case state_read_server_key_exchange:
1795 return "TLS client read_server_key_exchange";
1796 case state_read_certificate_request:
1797 return "TLS client read_certificate_request";
1798 case state_read_server_hello_done:
1799 return "TLS client read_server_hello_done";
1800 case state_send_client_certificate:
1801 return "TLS client send_client_certificate";
1802 case state_send_client_key_exchange:
1803 return "TLS client send_client_key_exchange";
1804 case state_send_client_certificate_verify:
1805 return "TLS client send_client_certificate_verify";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001806 case state_send_client_finished:
1807 return "TLS client send_client_finished";
1808 case state_finish_flight:
1809 return "TLS client finish_flight";
1810 case state_read_session_ticket:
1811 return "TLS client read_session_ticket";
1812 case state_process_change_cipher_spec:
1813 return "TLS client process_change_cipher_spec";
1814 case state_read_server_finished:
1815 return "TLS client read_server_finished";
1816 case state_finish_client_handshake:
1817 return "TLS client finish_client_handshake";
1818 case state_done:
1819 return "TLS client done";
1820 }
1821
1822 return "TLS client unknown";
1823}
1824
1825}