blob: b2d538481d9faafd293fd6c7ac3f1e05adcb31d4 [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,
185 state_read_server_key_exchange,
186 state_read_certificate_request,
187 state_read_server_hello_done,
188 state_send_client_certificate,
189 state_send_client_key_exchange,
190 state_send_client_certificate_verify,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400191 state_send_client_finished,
192 state_finish_flight,
193 state_read_session_ticket,
194 state_process_change_cipher_spec,
195 state_read_server_finished,
196 state_finish_client_handshake,
197 state_done,
198};
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjaminc11ea9422017-08-29 16:33:21 -0400200// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
201// disabled algorithms.
David Benjamin2fe6e222017-01-25 02:18:43 -0500202static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
203 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500204 *out_mask_a = 0;
205 *out_mask_k = 0;
206
David Benjaminc11ea9422017-08-29 16:33:21 -0400207 // PSK requires a client callback.
David Benjamin2fe6e222017-01-25 02:18:43 -0500208 if (ssl->psk_client_callback == NULL) {
209 *out_mask_a |= SSL_aPSK;
210 *out_mask_k |= SSL_kPSK;
211 }
212}
213
David Benjamin68161cb2017-06-20 14:49:43 -0400214static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
215 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500216 uint32_t mask_a, mask_k;
217 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamine8d53502015-10-10 14:13:23 -0400219 CBB child;
220 if (!CBB_add_u16_length_prefixed(out, &child)) {
221 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800222 }
223
David Benjaminc11ea9422017-08-29 16:33:21 -0400224 // Add a fake cipher suite. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -0400225 if (ssl->ctx->grease_enabled &&
226 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
227 return 0;
228 }
229
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
231 // hardware support.
David Benjamin68161cb2017-06-20 14:49:43 -0400232 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400233 if (!EVP_has_aes_hardware() &&
234 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
235 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500236 }
David Benjaminabbbee12016-10-31 19:20:42 -0400237 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
238 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
239 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400240 }
David Benjaminabbbee12016-10-31 19:20:42 -0400241 if (EVP_has_aes_hardware() &&
242 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400243 return 0;
244 }
245 }
246
David Benjamin68161cb2017-06-20 14:49:43 -0400247 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400248 int any_enabled = 0;
David Benjaminee910bf2017-07-25 22:36:00 -0400249 for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400250 // Skip disabled ciphers
David Benjamin2fe6e222017-01-25 02:18:43 -0500251 if ((cipher->algorithm_mkey & mask_k) ||
252 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400253 continue;
254 }
David Benjamin68161cb2017-06-20 14:49:43 -0400255 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
256 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400257 continue;
258 }
259 any_enabled = 1;
260 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
261 return 0;
262 }
263 }
264
David Benjaminc11ea9422017-08-29 16:33:21 -0400265 // If all ciphers were disabled, return the error to the caller.
David Benjamin68161cb2017-06-20 14:49:43 -0400266 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400267 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
268 return 0;
269 }
David Benjamine8d53502015-10-10 14:13:23 -0400270 }
271
David Benjaminc11ea9422017-08-29 16:33:21 -0400272 // For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
273 // added.
David Benjamin68161cb2017-06-20 14:49:43 -0400274 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400275 !ssl->s3->initial_handshake_complete) {
276 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
277 return 0;
278 }
David Benjamine8d53502015-10-10 14:13:23 -0400279 }
280
David Benjamine0ff7672016-09-19 18:40:03 -0400281 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200282 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
283 return 0;
284 }
David Benjamine8d53502015-10-10 14:13:23 -0400285 }
286
287 return CBB_flush(out);
288}
289
David Benjamin8c880a22016-12-03 02:20:34 -0500290int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
291 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400292 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -0400293 CBB body;
294 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
295 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400296 }
297
David Benjaminc11ea9422017-08-29 16:33:21 -0400298 // Renegotiations do not participate in session resumption.
Steven Valdez520e1222017-06-13 12:45:25 -0400299 int has_session_id = ssl->session != NULL &&
300 !ssl->s3->initial_handshake_complete &&
301 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400302
303 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500304 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400305 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400306 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400307 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400308 }
309
Steven Valdez520e1222017-06-13 12:45:25 -0400310 if (has_session_id) {
311 if (!CBB_add_bytes(&child, ssl->session->session_id,
312 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400313 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400314 }
315 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400316 // In TLS 1.3 experimental encodings, send a fake placeholder session ID
317 // when we do not otherwise have one to send.
Steven Valdez520e1222017-06-13 12:45:25 -0400318 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezc7d4d212017-09-11 13:53:08 -0400319 ssl_is_resumption_variant(ssl->tls13_variant) &&
Steven Valdez520e1222017-06-13 12:45:25 -0400320 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400321 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400322 }
323 }
324
David Benjamince079fd2016-08-02 16:22:34 -0400325 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400326 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400327 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400328 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400329 }
330 }
331
332 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400333 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400334 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400335 !CBB_add_u8(&body, 1 /* one compression method */) ||
336 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500337 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400338 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400339 }
340
David Benjamin879efc32017-09-21 11:20:53 -0400341 Array<uint8_t> msg;
342 if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
David Benjamind781fc42017-07-12 16:25:57 -0400343 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400344 }
345
David Benjaminc11ea9422017-08-29 16:33:21 -0400346 // Now that the length prefixes have been computed, fill in the placeholder
347 // PSK binder.
David Benjamin8c880a22016-12-03 02:20:34 -0500348 if (hs->needs_psk_binder &&
David Benjamin879efc32017-09-21 11:20:53 -0400349 !tls13_write_psk_binder(hs, msg.data(), msg.size())) {
David Benjamind781fc42017-07-12 16:25:57 -0400350 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400351 }
352
David Benjamin879efc32017-09-21 11:20:53 -0400353 return ssl->method->add_message(ssl, std::move(msg));
Steven Valdez5440fe02016-07-18 12:40:30 -0400354}
355
David Benjamin7934f082017-08-01 16:32:25 -0400356static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out,
357 const SSLMessage &msg) {
David Benjaminb853f312017-07-14 18:40:34 -0400358 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400359 if (msg.type != SSL3_MT_SERVER_HELLO &&
360 msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
David Benjaminb853f312017-07-14 18:40:34 -0400361 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
362 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
363 return 0;
364 }
365
David Benjamin7934f082017-08-01 16:32:25 -0400366 CBS server_hello = msg.body;
David Benjaminb853f312017-07-14 18:40:34 -0400367 if (!CBS_get_u16(&server_hello, out)) {
368 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
369 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
370 return 0;
371 }
372
David Benjaminc11ea9422017-08-29 16:33:21 -0400373 // The server version may also be in the supported_versions extension if
374 // applicable.
David Benjamin7934f082017-08-01 16:32:25 -0400375 if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) {
David Benjaminb853f312017-07-14 18:40:34 -0400376 return 1;
377 }
378
379 uint8_t sid_length;
380 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
381 !CBS_get_u8(&server_hello, &sid_length) ||
382 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
383 1 /* compression_method */)) {
384 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
385 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
386 return 0;
387 }
388
David Benjaminc11ea9422017-08-29 16:33:21 -0400389 // The extensions block may not be present.
David Benjaminb853f312017-07-14 18:40:34 -0400390 if (CBS_len(&server_hello) == 0) {
391 return 1;
392 }
393
394 CBS extensions;
395 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
396 CBS_len(&server_hello) != 0) {
397 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
398 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
399 return 0;
400 }
401
David Benjamin74795b32017-08-31 15:13:12 -0400402 bool have_supported_versions;
David Benjaminb853f312017-07-14 18:40:34 -0400403 CBS supported_versions;
404 const SSL_EXTENSION_TYPE ext_types[] = {
405 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
406 &supported_versions},
407 };
408
409 uint8_t alert = SSL_AD_DECODE_ERROR;
410 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
411 OPENSSL_ARRAY_SIZE(ext_types),
412 1 /* ignore unknown */)) {
413 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
414 return 0;
415 }
416
417 if (have_supported_versions &&
418 (!CBS_get_u16(&supported_versions, out) ||
419 CBS_len(&supported_versions) != 0)) {
420 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
421 return 0;
422 }
423
424 return 1;
425}
426
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400427static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400428 SSL *const ssl = hs->ssl;
429
Steven Valdez398085b2017-08-29 13:25:56 -0400430 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamind0beda02017-09-06 16:46:50 -0400431 // |session_reused| must be reset in case this is a renegotiation.
432 ssl->s3->session_reused = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400433
David Benjaminc11ea9422017-08-29 16:33:21 -0400434 // Freeze the version range.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400435 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
436 return ssl_hs_error;
437 }
438
Steven Valdezc7d4d212017-09-11 13:53:08 -0400439 // SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer
440 // version.
441 if (hs->max_version == SSL3_VERSION) {
442 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION);
443 }
444
David Benjaminc11ea9422017-08-29 16:33:21 -0400445 // Always advertise the ClientHello version from the original maximum version,
446 // even on renegotiation. The static RSA key exchange uses this field, and
447 // some servers fail when it changes across handshakes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400448 if (SSL_is_dtls(hs->ssl)) {
449 hs->client_version =
450 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
451 } else {
452 hs->client_version =
453 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
454 }
455
David Benjaminc11ea9422017-08-29 16:33:21 -0400456 // If the configured session has expired or was created at a disabled
457 // version, drop it.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400458 if (ssl->session != NULL) {
459 if (ssl->session->is_server ||
460 !ssl_supports_version(hs, ssl->session->ssl_version) ||
461 (ssl->session->session_id_length == 0 &&
462 ssl->session->tlsext_ticklen == 0) ||
463 ssl->session->not_resumable ||
464 !ssl_session_is_time_valid(ssl, ssl->session)) {
465 ssl_set_session(ssl, NULL);
466 }
467 }
468
Steven Valdez398085b2017-08-29 13:25:56 -0400469 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400470 return ssl_hs_error;
471 }
472
David Benjaminc11ea9422017-08-29 16:33:21 -0400473 // Initialize a random session ID for the experimental TLS 1.3 variant
474 // requiring a session id.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400475 if (ssl_is_resumption_variant(ssl->tls13_variant)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400476 hs->session_id_len = sizeof(hs->session_id);
477 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
478 return ssl_hs_error;
479 }
480 }
481
482 if (!ssl_write_client_hello(hs)) {
483 return ssl_hs_error;
484 }
485
486 hs->state = state_enter_early_data;
487 return ssl_hs_flush;
488}
489
490static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
491 SSL *const ssl = hs->ssl;
492
Steven Valdez398085b2017-08-29 13:25:56 -0400493 if (SSL_is_dtls(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400494 hs->state = state_read_hello_verify_request;
495 return ssl_hs_ok;
496 }
497
498 if (!hs->early_data_offered) {
499 hs->state = state_read_server_hello;
500 return ssl_hs_ok;
501 }
502
503 if (!tls13_init_early_key_schedule(hs) ||
504 !tls13_advance_key_schedule(hs, ssl->session->master_key,
505 ssl->session->master_key_length) ||
506 !tls13_derive_early_secrets(hs) ||
507 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
508 hs->hash_len)) {
509 return ssl_hs_error;
510 }
511
David Benjaminc11ea9422017-08-29 16:33:21 -0400512 // Stash the early data session, so connection properties may be queried out
513 // of it.
David Benjaminfd45ee72017-08-31 14:49:09 -0400514 hs->in_early_data = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400515 SSL_SESSION_up_ref(ssl->session);
516 hs->early_session.reset(ssl->session);
David Benjaminfd45ee72017-08-31 14:49:09 -0400517 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400518
519 hs->state = state_read_server_hello;
520 return ssl_hs_early_return;
521}
522
523static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
524 SSL *const ssl = hs->ssl;
525
526 assert(SSL_is_dtls(ssl));
527
528 SSLMessage msg;
529 if (!ssl->method->get_message(ssl, &msg)) {
530 return ssl_hs_read_message;
531 }
532
533 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400534 hs->state = state_read_server_hello;
535 return ssl_hs_ok;
536 }
537
538 CBS hello_verify_request = msg.body, cookie;
539 uint16_t server_version;
540 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
541 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
542 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
543 CBS_len(&hello_verify_request) != 0) {
544 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
545 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
546 return ssl_hs_error;
547 }
548
549 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
550 ssl->d1->cookie_len = CBS_len(&cookie);
551
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400552 ssl->method->next_message(ssl);
Steven Valdez398085b2017-08-29 13:25:56 -0400553
David Benjaminc11ea9422017-08-29 16:33:21 -0400554 // DTLS resets the handshake buffer after HelloVerifyRequest.
Steven Valdez398085b2017-08-29 13:25:56 -0400555 if (!hs->transcript.Init()) {
556 return ssl_hs_error;
557 }
558
559 if (!ssl_write_client_hello(hs)) {
560 return ssl_hs_error;
561 }
562
563 hs->state = state_read_server_hello;
564 return ssl_hs_flush;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400565}
566
567static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900568 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400569 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400570 if (!ssl->method->get_message(ssl, &msg)) {
571 return ssl_hs_read_server_hello;
Adam Langley1bea1732014-12-17 19:06:57 -0800572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
David Benjaminb853f312017-07-14 18:40:34 -0400574 uint16_t server_version;
David Benjamin7934f082017-08-01 16:32:25 -0400575 if (!parse_server_version(hs, &server_version, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400576 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400577 }
578
Steven Valdez8f36c512017-06-20 10:55:02 -0400579 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400580 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400581 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400582 return ssl_hs_error;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400583 }
584
David Benjamin0d56f882015-12-19 17:05:56 -0500585 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
586 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400587 ssl->version = server_version;
David Benjaminc11ea9422017-08-29 16:33:21 -0400588 // At this point, the connection's version is known and ssl->version is
589 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400590 ssl->s3->have_version = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400591 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400592 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400593 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400594 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400595 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800596 }
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Steven Valdez143e8b32016-07-11 13:19:03 -0400598 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400599 hs->state = state_tls13;
600 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400601 }
602
David Benjamin73d42e62017-09-21 03:05:35 -0400603 // Clear some TLS 1.3 state that no longer needs to be retained.
604 hs->key_share.reset();
605 hs->key_share_bytes.Reset();
606
607 // A TLS 1.2 server would not know to skip the early data we offered. Report
608 // an error code sooner. The caller may use this error code to implement the
609 // fallback described in draft-ietf-tls-tls13-18 appendix C.3.
Steven Valdez2d850622017-01-11 11:34:52 -0500610 if (hs->early_data_offered) {
611 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400612 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400613 return ssl_hs_error;
Steven Valdez2d850622017-01-11 11:34:52 -0500614 }
615
David Benjamin7934f082017-08-01 16:32:25 -0400616 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400617 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400618 }
619
David Benjamin7934f082017-08-01 16:32:25 -0400620 CBS server_hello = msg.body, server_random, session_id;
David Benjaminb853f312017-07-14 18:40:34 -0400621 uint16_t cipher_suite;
622 uint8_t compression_method;
David Benjaminb853f312017-07-14 18:40:34 -0400623 if (!CBS_skip(&server_hello, 2 /* version */) ||
624 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400625 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
626 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
627 !CBS_get_u16(&server_hello, &cipher_suite) ||
628 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400629 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400630 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400631 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400632 }
633
David Benjaminc11ea9422017-08-29 16:33:21 -0400634 // Copy over the server random.
David Benjamind3bca042017-04-08 11:05:04 -0400635 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
636 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700637
David Benjaminc11ea9422017-08-29 16:33:21 -0400638 // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
639 // 1.3 is finalized and we are not implementing a draft version.
David Benjamin1f61f0d2016-07-10 12:20:35 -0400640
David Benjamin0d56f882015-12-19 17:05:56 -0500641 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400642 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500643 CBS_mem_equal(&session_id, ssl->session->session_id,
644 ssl->session->session_id_length)) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400645 ssl->s3->session_reused = true;
Adam Langley1bea1732014-12-17 19:06:57 -0800646 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400647 // The session wasn't resumed. Create a fresh SSL_SESSION to
648 // fill out.
David Benjamin4d0be242016-09-01 01:10:07 -0400649 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900650 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400651 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400652 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800653 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400654 // Note: session_id could be empty.
David Benjamin45738dd2017-02-09 20:01:26 -0500655 hs->new_session->session_id_length = CBS_len(&session_id);
656 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
657 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800658 }
David Benjamina03d95d2014-07-12 19:49:07 -0400659
David Benjaminca9e8f52017-08-09 15:02:34 -0400660 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
661 if (cipher == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400662 // unknown cipher
David Benjamin3570d732015-06-29 00:28:17 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400664 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400665 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800666 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400667
David Benjaminc11ea9422017-08-29 16:33:21 -0400668 // The cipher must be allowed in the selected version and enabled.
David Benjamin2fe6e222017-01-25 02:18:43 -0500669 uint32_t mask_a, mask_k;
670 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
David Benjaminca9e8f52017-08-09 15:02:34 -0400671 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
672 SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
673 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) ||
674 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400675 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400676 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400677 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800678 }
Adam Langley95c29f32014-06-20 12:00:00 -0700679
Steven Valdez87eab492016-06-27 16:34:59 -0400680 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500681 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400682 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400683 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400684 return ssl_hs_error;
David Benjaminece3de92015-03-16 18:02:20 -0400685 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400686 if (ssl->session->cipher != cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400687 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400688 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400689 return ssl_hs_error;
Steven Valdez803c77a2016-09-06 14:13:43 -0400690 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400691 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400692 // This is actually a client application bug.
Steven Valdez4aa154e2016-07-29 14:32:55 -0400693 OPENSSL_PUT_ERROR(SSL,
694 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400695 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400696 return ssl_hs_error;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400697 }
David Benjamin0623bce2015-12-25 15:40:14 -0500698 } else {
David Benjaminca9e8f52017-08-09 15:02:34 -0400699 hs->new_session->cipher = cipher;
Adam Langley1bea1732014-12-17 19:06:57 -0800700 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400701 hs->new_cipher = cipher;
David Benjaminbdf5e722014-11-11 00:52:15 -0500702
David Benjaminc11ea9422017-08-29 16:33:21 -0400703 // Now that the cipher is known, initialize the handshake hash and hash the
704 // ServerHello.
David Benjaminca9e8f52017-08-09 15:02:34 -0400705 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400706 !ssl_hash_message(hs, msg)) {
David Benjamind3bca042017-04-08 11:05:04 -0400707 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400708 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400709 }
710
David Benjaminc11ea9422017-08-29 16:33:21 -0400711 // If doing a full handshake, the server may request a client certificate
712 // which requires hashing the handshake transcript. Otherwise, the handshake
713 // buffer may be released.
David Benjamin3d458dc2016-09-12 22:40:27 +0000714 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500715 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400716 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -0800717 }
David Benjamina03d95d2014-07-12 19:49:07 -0400718
David Benjaminc11ea9422017-08-29 16:33:21 -0400719 // Only the NULL compression algorithm is supported.
Adam Langley1bea1732014-12-17 19:06:57 -0800720 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400721 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -0400722 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400723 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800724 }
Adam Langley95c29f32014-06-20 12:00:00 -0700725
David Benjaminc11ea9422017-08-29 16:33:21 -0400726 // TLS extensions
David Benjamin8c880a22016-12-03 02:20:34 -0500727 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400728 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400729 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800730 }
Adam Langley95c29f32014-06-20 12:00:00 -0700731
David Benjaminc11ea9422017-08-29 16:33:21 -0400732 // There should be nothing left over in the record.
Adam Langley1bea1732014-12-17 19:06:57 -0800733 if (CBS_len(&server_hello) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400734 // wrong packet length
David Benjamin639846e2016-09-09 11:41:18 -0400735 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400736 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400737 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800738 }
Adam Langley95c29f32014-06-20 12:00:00 -0700739
Steven Valdez87eab492016-06-27 16:34:59 -0400740 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -0500741 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -0500742 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400743 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700744 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400745 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700746 }
David Benjamind3bca042017-04-08 11:05:04 -0400747 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400748 return ssl_hs_error;
Adam Langleyba5934b2015-06-02 10:50:35 -0700749 }
750
David Benjamin8f94c312017-08-01 17:35:55 -0400751 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400752
753 if (ssl->session != NULL) {
754 hs->state = state_read_session_ticket;
755 return ssl_hs_ok;
756 }
757
758 hs->state = state_read_server_certificate;
759 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800760}
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400762static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
763 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
764 if (wait == ssl_hs_ok) {
765 hs->state = state_finish_client_handshake;
766 return ssl_hs_ok;
767 }
768
769 return wait;
770}
771
772static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900773 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400774
775 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
776 hs->state = state_read_certificate_status;
777 return ssl_hs_ok;
778 }
779
David Benjamin7934f082017-08-01 16:32:25 -0400780 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400781 if (!ssl->method->get_message(ssl, &msg)) {
782 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800783 }
Adam Langley95c29f32014-06-20 12:00:00 -0700784
David Benjamin7934f082017-08-01 16:32:25 -0400785 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
786 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400787 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -0500788 }
789
David Benjamin7934f082017-08-01 16:32:25 -0400790 CBS body = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800791 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400792 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
David Benjamin7934f082017-08-01 16:32:25 -0400793 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
David Benjamine664a532017-07-20 20:19:36 -0400794 ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400795 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400796 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800797 }
David Benjamine664a532017-07-20 20:19:36 -0400798 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
799 hs->new_session->certs = chain.release();
Adam Langley95c29f32014-06-20 12:00:00 -0700800
David Benjamin45738dd2017-02-09 20:01:26 -0500801 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -0400802 CBS_len(&body) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400803 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -0400804 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -0400805 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400806 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800807 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400808
Adam Langley05672202016-12-13 12:05:49 -0800809 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -0400810 hs, hs->peer_pubkey.get(),
David Benjamin45738dd2017-02-09 20:01:26 -0500811 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -0400812 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400813 return ssl_hs_error;
David Benjamin436bf822015-06-07 19:50:37 -0400814 }
815
David Benjamin8f94c312017-08-01 17:35:55 -0400816 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400817
818 hs->state = state_read_certificate_status;
819 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800820}
821
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400822static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900823 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400824
825 if (!hs->certificate_status_expected) {
826 hs->state = state_verify_server_certificate;
827 return ssl_hs_ok;
828 }
829
David Benjamin7934f082017-08-01 16:32:25 -0400830 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400831 if (!ssl->method->get_message(ssl, &msg)) {
832 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400833 }
834
David Benjamin7934f082017-08-01 16:32:25 -0400835 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400836 // A server may send status_request in ServerHello and then change its mind
837 // about sending CertificateStatus.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400838 hs->state = state_verify_server_certificate;
839 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400840 }
841
David Benjamin7934f082017-08-01 16:32:25 -0400842 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400843 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500844 }
845
David Benjamin7934f082017-08-01 16:32:25 -0400846 CBS certificate_status = msg.body, ocsp_response;
David Benjamin8d606e32017-06-15 22:43:04 -0400847 uint8_t status_type;
David Benjamin9f1dc822016-06-07 17:03:46 -0400848 if (!CBS_get_u8(&certificate_status, &status_type) ||
849 status_type != TLSEXT_STATUSTYPE_ocsp ||
850 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
851 CBS_len(&ocsp_response) == 0 ||
852 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400853 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400854 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400855 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400856 }
857
David Benjamin8fc2dc02017-08-22 15:07:51 -0700858 CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
859 hs->new_session->ocsp_response =
860 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
861 if (hs->new_session->ocsp_response == nullptr) {
David Benjamin8d606e32017-06-15 22:43:04 -0400862 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400863 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400864 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400865
David Benjamin8f94c312017-08-01 17:35:55 -0400866 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400867
868 hs->state = state_verify_server_certificate;
869 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400870}
871
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400872static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
873 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
874 hs->state = state_read_server_key_exchange;
875 return ssl_hs_ok;
876 }
877
878 switch (ssl_verify_peer_cert(hs)) {
879 case ssl_verify_ok:
880 break;
881 case ssl_verify_invalid:
882 return ssl_hs_error;
883 case ssl_verify_retry:
884 hs->state = state_verify_server_certificate;
885 return ssl_hs_certificate_verify;
886 }
887
888 hs->state = state_read_server_key_exchange;
889 return ssl_hs_ok;
890}
891
892static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900893 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400894 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400895 if (!ssl->method->get_message(ssl, &msg)) {
896 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800897 }
898
David Benjamin7934f082017-08-01 16:32:25 -0400899 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400900 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
David Benjamin45738dd2017-02-09 20:01:26 -0500901 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400902 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -0500903 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400904 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800905 }
906
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400907 hs->state = state_read_certificate_request;
908 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800909 }
910
David Benjamin7934f082017-08-01 16:32:25 -0400911 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400912 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500913 }
914
David Benjamin45738dd2017-02-09 20:01:26 -0500915 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
916 uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin7934f082017-08-01 16:32:25 -0400917 CBS server_key_exchange = msg.body;
Adam Langley1bea1732014-12-17 19:06:57 -0800918 if (alg_a & SSL_aPSK) {
919 CBS psk_identity_hint;
920
David Benjaminc11ea9422017-08-29 16:33:21 -0400921 // Each of the PSK key exchanges begins with a psk_identity_hint.
Adam Langley1bea1732014-12-17 19:06:57 -0800922 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
923 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400924 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400925 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400926 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800927 }
928
David Benjaminc11ea9422017-08-29 16:33:21 -0400929 // Store PSK identity hint for later use, hint is used in
930 // ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
931 // identity hint can be as long as the maximum length of a PSK identity.
932 // Also do not allow NULL characters; identities are saved as C strings.
933 //
934 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
935 // a specific identity.
Adam Langley1bea1732014-12-17 19:06:57 -0800936 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
937 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400938 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -0400939 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400940 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800941 }
942
David Benjaminc11ea9422017-08-29 16:33:21 -0400943 // Save non-empty identity hints as a C string. Empty identity hints we
944 // treat as missing. Plain PSK makes it possible to send either no hint
945 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
946 // empty hint. Having different capabilities is odd, so we interpret empty
947 // and missing as identical.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400948 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -0400949 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -0400950 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -0400951 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -0400952 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400953 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800954 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400955 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -0800956 }
957
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700958 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400959 // Parse the server parameters.
Steven Valdezce902a92016-05-17 11:47:53 -0400960 uint8_t group_type;
961 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -0500962 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -0400963 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
964 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -0700965 !CBS_get_u16(&server_key_exchange, &group_id) ||
966 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -0400967 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400968 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400969 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800970 }
David Benjamin45738dd2017-02-09 20:01:26 -0500971 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -0800972
David Benjaminc11ea9422017-08-29 16:33:21 -0400973 // Ensure the group is consistent with preferences.
Steven Valdezce902a92016-05-17 11:47:53 -0400974 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -0500975 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -0400976 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400977 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800978 }
David Benjamin4298d772015-12-19 00:18:25 -0500979
David Benjaminc11ea9422017-08-29 16:33:21 -0400980 // Initialize ECDH and save the peer public key for later.
David Benjaminc642aca2017-07-19 23:28:43 -0400981 hs->key_share = SSLKeyShare::Create(group_id);
982 if (!hs->key_share ||
David Benjamin08f5c762017-09-21 02:43:05 -0400983 !hs->peer_key.CopyFrom(point)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400984 return ssl_hs_error;
David Benjaminc1cc8582015-12-17 01:22:15 -0500985 }
Adam Langley1bea1732014-12-17 19:06:57 -0800986 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -0400987 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -0400988 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400989 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800990 }
991
David Benjaminc11ea9422017-08-29 16:33:21 -0400992 // At this point, |server_key_exchange| contains the signature, if any, while
993 // |msg.body| contains the entire message. From that, derive a CBS containing
994 // just the parameter.
David Benjaminb35d6842015-12-29 17:08:08 -0500995 CBS parameter;
David Benjamin7934f082017-08-01 16:32:25 -0400996 CBS_init(&parameter, CBS_data(&msg.body),
997 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
Adam Langley1bea1732014-12-17 19:06:57 -0800998
David Benjaminc11ea9422017-08-29 16:33:21 -0400999 // ServerKeyExchange should be signed by the server's public key.
David Benjamin45738dd2017-02-09 20:01:26 -05001000 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001001 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001002 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001003 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001004 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001005 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001006 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001007 }
David Benjamin8d606e32017-06-15 22:43:04 -04001008 uint8_t alert = SSL_AD_DECODE_ERROR;
1009 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1010 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001011 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001012 }
David Benjamin45738dd2017-02-09 20:01:26 -05001013 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001014 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001015 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001016 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001017 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001018 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001019 }
1020
David Benjaminc11ea9422017-08-29 16:33:21 -04001021 // The last field in |server_key_exchange| is the signature.
David Benjaminf4958e72015-06-07 11:00:53 -04001022 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001023 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1024 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001025 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001026 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001027 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001028 }
1029
David Benjamin1386aad2017-07-19 23:57:40 -04001030 ScopedCBB transcript;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001031 uint8_t *transcript_data;
1032 size_t transcript_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001033 if (!CBB_init(transcript.get(),
1034 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1035 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1036 SSL3_RANDOM_SIZE) ||
1037 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1038 SSL3_RANDOM_SIZE) ||
1039 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1040 CBS_len(&parameter)) ||
1041 !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001042 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001043 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001044 return ssl_hs_error;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001045 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001046
1047 int sig_ok = ssl_public_key_verify(
1048 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001049 hs->peer_pubkey.get(), transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001050 OPENSSL_free(transcript_data);
1051
David Benjaminbf82aed2016-03-01 22:57:40 -05001052#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1053 sig_ok = 1;
1054 ERR_clear_error();
1055#endif
1056 if (!sig_ok) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001057 // bad signature
David Benjamin3570d732015-06-29 00:28:17 -04001058 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001059 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001060 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001061 }
1062 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001063 // PSK ciphers are the only supported certificate-less ciphers.
David Benjaminf4958e72015-06-07 11:00:53 -04001064 assert(alg_a == SSL_aPSK);
1065
Adam Langley1bea1732014-12-17 19:06:57 -08001066 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001067 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001068 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001069 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001070 }
1071 }
David Benjamin8f94c312017-08-01 17:35:55 -04001072
1073 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001074 hs->state = state_read_certificate_request;
1075 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001076}
Adam Langley95c29f32014-06-20 12:00:00 -07001077
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001078static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001079 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001080
1081 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1082 hs->state = state_read_server_hello_done;
1083 return ssl_hs_ok;
1084 }
1085
David Benjamin7934f082017-08-01 16:32:25 -04001086 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001087 if (!ssl->method->get_message(ssl, &msg)) {
1088 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001089 }
David Benjamin688d8df2014-11-02 23:06:42 -05001090
David Benjamin7934f082017-08-01 16:32:25 -04001091 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001092 // If we get here we don't need the handshake buffer as we won't be doing
1093 // client auth.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001094 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001095 hs->state = state_read_server_hello_done;
1096 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001097 }
David Benjamined439582014-07-14 19:13:02 -04001098
David Benjamin7934f082017-08-01 16:32:25 -04001099 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1100 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001101 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
David Benjaminc11ea9422017-08-29 16:33:21 -04001104 // Get the certificate types.
David Benjamin7934f082017-08-01 16:32:25 -04001105 CBS body = msg.body, certificate_types;
1106 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001107 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001108 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001109 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001110 }
David Benjamined439582014-07-14 19:13:02 -04001111
David Benjamin08f5c762017-09-21 02:43:05 -04001112 if (!hs->certificate_types.CopyFrom(certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001113 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001114 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001115 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001116
David Benjamina1e9cab2015-12-30 00:08:49 -05001117 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001118 CBS supported_signature_algorithms;
David Benjamin7934f082017-08-01 16:32:25 -04001119 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001120 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001121 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001122 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001123 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001124 }
Adam Langley1bea1732014-12-17 19:06:57 -08001125 }
David Benjamined439582014-07-14 19:13:02 -04001126
Adam Langleyc68e5b92017-02-08 13:33:15 -08001127 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001128 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -04001129 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001130 if (!ca_names) {
David Benjamine0332e82016-07-13 22:40:36 -04001131 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001132 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001133 }
Adam Langley95c29f32014-06-20 12:00:00 -07001134
David Benjamin7934f082017-08-01 16:32:25 -04001135 if (CBS_len(&body) != 0) {
David Benjamin639846e2016-09-09 11:41:18 -04001136 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001137 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001138 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001139 }
1140
David Benjaminfd45ee72017-08-31 14:49:09 -04001141 hs->cert_request = true;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001142 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001143 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001144
David Benjamin8f94c312017-08-01 17:35:55 -04001145 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001146 hs->state = state_read_server_hello_done;
1147 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001148}
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001150static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001151 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001152 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001153 if (!ssl->method->get_message(ssl, &msg)) {
1154 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001155 }
David Benjamin46062682014-07-14 19:14:32 -04001156
David Benjamin7934f082017-08-01 16:32:25 -04001157 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1158 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001159 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001160 }
1161
David Benjaminc11ea9422017-08-29 16:33:21 -04001162 // ServerHelloDone is empty.
David Benjamin7934f082017-08-01 16:32:25 -04001163 if (CBS_len(&msg.body) != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001164 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001165 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001166 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001167 }
David Benjamin46062682014-07-14 19:14:32 -04001168
David Benjamin8f94c312017-08-01 17:35:55 -04001169 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001170 hs->state = state_send_client_certificate;
1171 return ssl_hs_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001172}
Adam Langley1258b6a2014-06-20 12:00:00 -07001173
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001174static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001175 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001176
David Benjaminc11ea9422017-08-29 16:33:21 -04001177 // The peer didn't request a certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001178 if (!hs->cert_request) {
1179 hs->state = state_send_client_key_exchange;
1180 return ssl_hs_ok;
1181 }
1182
David Benjaminc11ea9422017-08-29 16:33:21 -04001183 // Call cert_cb to update the certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001184 if (ssl->cert->cert_cb != NULL) {
1185 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1186 if (rv == 0) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001187 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001188 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1189 return ssl_hs_error;
1190 }
1191 if (rv < 0) {
1192 hs->state = state_send_client_certificate;
1193 return ssl_hs_x509_lookup;
David Benjamin9f1dc822016-06-07 17:03:46 -04001194 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001195 }
1196
David Benjamin5edfc8c2016-12-10 15:46:58 -05001197 if (!ssl_has_certificate(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001198 // Without a client certificate, the handshake buffer may be released.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001199 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001200
David Benjaminc11ea9422017-08-29 16:33:21 -04001201 // In SSL 3.0, the Certificate message is replaced with a warning alert.
David Benjamin5edfc8c2016-12-10 15:46:58 -05001202 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001203 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1204 SSL_AD_NO_CERTIFICATE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001205 return ssl_hs_error;
David Benjamindaf207a2017-01-03 18:37:41 -05001206 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001207 hs->state = state_send_client_key_exchange;
1208 return ssl_hs_ok;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001209 }
1210 }
1211
David Benjamina232a712017-03-30 15:51:53 -05001212 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001213 !ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001214 return ssl_hs_error;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001215 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001216
1217
1218 hs->state = state_send_client_key_exchange;
1219 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001220}
1221
David Benjamina3d76d02017-07-14 19:36:07 -04001222static_assert(sizeof(size_t) >= sizeof(unsigned),
1223 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001224
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001225static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001226 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001227 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001228 CBB body;
1229 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001230 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001231 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001232 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001233
David Benjamin499742c2017-07-22 12:45:38 -04001234 Array<uint8_t> pms;
David Benjamin45738dd2017-02-09 20:01:26 -05001235 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1236 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001237
David Benjaminc11ea9422017-08-29 16:33:21 -04001238 // If using a PSK key exchange, prepare the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001239 unsigned psk_len = 0;
1240 uint8_t psk[PSK_MAX_PSK_LEN];
1241 if (alg_a & SSL_aPSK) {
1242 if (ssl->psk_client_callback == NULL) {
1243 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
David Benjamin499742c2017-07-22 12:45:38 -04001244 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001245 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001246
David Benjaminae0eaaa2015-12-17 02:01:25 -05001247 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001248 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001249 psk_len =
David Benjamin31b0c9b2017-07-20 14:49:15 -04001250 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
1251 identity, sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001252 if (psk_len == 0) {
1253 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1254 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001255 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001256 }
1257 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001258
David Benjamin45738dd2017-02-09 20:01:26 -05001259 OPENSSL_free(hs->new_session->psk_identity);
1260 hs->new_session->psk_identity = BUF_strdup(identity);
1261 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001262 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001263 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001264 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001265
David Benjaminc11ea9422017-08-29 16:33:21 -04001266 // Write out psk_identity.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001267 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001268 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001269 !CBB_add_bytes(&child, (const uint8_t *)identity,
1270 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001271 !CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001272 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001273 }
1274 }
David Benjamin758d1272015-11-20 17:47:25 -05001275
David Benjamin499742c2017-07-22 12:45:38 -04001276 // Depending on the key exchange method, compute |pms|.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001277 if (alg_k & SSL_kRSA) {
David Benjamin499742c2017-07-22 12:45:38 -04001278 if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
1279 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001280 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001281
David Benjamin31b0c9b2017-07-20 14:49:15 -04001282 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001283 if (rsa == NULL) {
1284 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001285 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001286 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001287
David Benjaminf04c2e92016-12-06 13:35:25 -05001288 pms[0] = hs->client_version >> 8;
1289 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001290 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
David Benjamin499742c2017-07-22 12:45:38 -04001291 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001292 }
1293
David Benjamin75836432016-06-17 18:48:29 -04001294 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001295 size_t enc_pms_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001296 // In TLS, there is a length prefix.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001297 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001298 if (!CBB_add_u16_length_prefixed(&body, &child)) {
David Benjamin499742c2017-07-22 12:45:38 -04001299 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001300 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001301 enc_pms = &child;
1302 }
Adam Langley1bea1732014-12-17 19:06:57 -08001303
David Benjaminae0eaaa2015-12-17 02:01:25 -05001304 uint8_t *ptr;
1305 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
David Benjamin499742c2017-07-22 12:45:38 -04001306 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
1307 pms.size(), RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001308 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001309 !CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001310 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001311 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001312 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001313 // Generate a keypair and serialize the public half.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001314 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001315 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamin499742c2017-07-22 12:45:38 -04001316 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001317 }
1318
David Benjaminc11ea9422017-08-29 16:33:21 -04001319 // Compute the premaster.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001320 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin499742c2017-07-22 12:45:38 -04001321 if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
David Benjamin4298d772015-12-19 00:18:25 -05001322 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin499742c2017-07-22 12:45:38 -04001323 return ssl_hs_error;
David Benjamin4298d772015-12-19 00:18:25 -05001324 }
David Benjamin75836432016-06-17 18:48:29 -04001325 if (!CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001326 return ssl_hs_error;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001327 }
David Benjamin4298d772015-12-19 00:18:25 -05001328
David Benjaminc11ea9422017-08-29 16:33:21 -04001329 // The key exchange state may now be discarded.
David Benjaminc642aca2017-07-19 23:28:43 -04001330 hs->key_share.reset();
David Benjamin499742c2017-07-22 12:45:38 -04001331 hs->peer_key.Reset();
David Benjaminae0eaaa2015-12-17 02:01:25 -05001332 } else if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001333 // For plain PSK, other_secret is a block of 0s with the same length as
1334 // the pre-shared key.
David Benjamin499742c2017-07-22 12:45:38 -04001335 if (!pms.Init(psk_len)) {
1336 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001337 }
David Benjamin499742c2017-07-22 12:45:38 -04001338 OPENSSL_memset(pms.data(), 0, pms.size());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001339 } else {
1340 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1341 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001342 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001343 }
1344
David Benjaminc11ea9422017-08-29 16:33:21 -04001345 // For a PSK cipher suite, other_secret is combined with the pre-shared
1346 // key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001347 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001348 ScopedCBB pms_cbb;
1349 CBB child;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001350 uint8_t *new_pms;
1351 size_t new_pms_len;
1352
David Benjamin499742c2017-07-22 12:45:38 -04001353 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001354 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjamin499742c2017-07-22 12:45:38 -04001355 !CBB_add_bytes(&child, pms.data(), pms.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001356 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001357 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001358 !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001359 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001360 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001361 }
David Benjamin499742c2017-07-22 12:45:38 -04001362 pms.Reset(new_pms, new_pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001363 }
1364
David Benjaminc11ea9422017-08-29 16:33:21 -04001365 // The message must be added to the finished hash before calculating the
1366 // master secret.
David Benjamind781fc42017-07-12 16:25:57 -04001367 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin499742c2017-07-22 12:45:38 -04001368 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001369 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001370
David Benjamin45738dd2017-02-09 20:01:26 -05001371 hs->new_session->master_key_length = tls1_generate_master_secret(
David Benjamin499742c2017-07-22 12:45:38 -04001372 hs, hs->new_session->master_key, pms.data(), pms.size());
David Benjamin45738dd2017-02-09 20:01:26 -05001373 if (hs->new_session->master_key_length == 0) {
David Benjamin499742c2017-07-22 12:45:38 -04001374 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001375 }
David Benjaminfc02b592017-02-17 16:26:01 -05001376 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001377
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001378 hs->state = state_send_client_certificate_verify;
1379 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001380}
Adam Langley1258b6a2014-06-20 12:00:00 -07001381
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001382static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001383 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001384
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001385 if (!hs->cert_request || !ssl_has_certificate(ssl)) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001386 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001387 return ssl_hs_ok;
1388 }
1389
1390 assert(ssl_has_private_key(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -04001391 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001392 CBB body, child;
1393 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001394 SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001395 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001396 }
1397
David Benjaminea9a0d52016-07-08 15:52:59 -07001398 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001399 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001400 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001401 }
David Benjamin429fdc02016-06-17 17:33:30 -04001402 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001403 // Write out the digest type in TLS 1.2.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001404 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001405 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001406 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001407 }
1408 }
David Benjaminfc825122015-12-18 01:57:43 -05001409
David Benjaminc11ea9422017-08-29 16:33:21 -04001410 // Set aside space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001411 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001412 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001413 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001414 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001415 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001416 }
1417
Steven Valdez2b8415e2016-06-30 13:27:23 -04001418 size_t sig_len = max_sig_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001419 // The SSL3 construction for CertificateVerify does not decompose into a
1420 // single final digest and signature, and must be special-cased.
David Benjamin44148742017-06-17 13:20:59 -04001421 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1422 if (ssl->cert->key_method != NULL) {
1423 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001424 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001425 }
1426
David Benjamin44148742017-06-17 13:20:59 -04001427 uint8_t digest[EVP_MAX_MD_SIZE];
1428 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001429 if (!hs->transcript.GetSSL3CertVerifyHash(
1430 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001431 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001432 }
David Benjaminfc825122015-12-18 01:57:43 -05001433
David Benjamin1386aad2017-07-19 23:57:40 -04001434 UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
1435 if (!pctx ||
1436 !EVP_PKEY_sign_init(pctx.get()) ||
1437 !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001438 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001439 }
1440 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001441 switch (ssl_private_key_sign(
1442 hs, ptr, &sig_len, max_sig_len, signature_algorithm,
1443 hs->transcript.buffer_data(), hs->transcript.buffer_len())) {
David Benjamin44148742017-06-17 13:20:59 -04001444 case ssl_private_key_success:
1445 break;
1446 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001447 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001448 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001449 hs->state = state_send_client_certificate_verify;
1450 return ssl_hs_private_key_operation;
David Benjamin44148742017-06-17 13:20:59 -04001451 }
David Benjaminfc825122015-12-18 01:57:43 -05001452 }
1453
David Benjaminfc825122015-12-18 01:57:43 -05001454 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001455 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001456 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001457 }
1458
David Benjaminc11ea9422017-08-29 16:33:21 -04001459 // The handshake buffer is no longer necessary.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001460 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001461
David Benjamin4cbb9312017-08-31 14:52:31 -04001462 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001463 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001464}
1465
David Benjamin4cbb9312017-08-31 14:52:31 -04001466static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001467 SSL *const ssl = hs->ssl;
David Benjamin4cbb9312017-08-31 14:52:31 -04001468 // Resolve Channel ID first, before any non-idempotent operations.
1469 if (ssl->s3->tlsext_channel_id_valid) {
1470 if (!ssl_do_channel_id_callback(ssl)) {
1471 return ssl_hs_error;
1472 }
1473
1474 if (ssl->tlsext_channel_id_private == NULL) {
1475 hs->state = state_send_client_finished;
1476 return ssl_hs_channel_id_lookup;
1477 }
1478 }
David Benjamine97b9622015-10-10 14:59:09 -04001479
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001480 if (!ssl->method->add_change_cipher_spec(ssl) ||
David Benjamine58f8a62017-09-21 19:07:15 -04001481 !tls1_change_cipher_state(hs, evp_aead_seal)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001482 return ssl_hs_error;
David Benjamine97b9622015-10-10 14:59:09 -04001483 }
1484
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001485 if (hs->next_proto_neg_seen) {
1486 static const uint8_t kZero[32] = {0};
1487 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
1488
1489 ScopedCBB cbb;
1490 CBB body, child;
1491 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1492 !CBB_add_u8_length_prefixed(&body, &child) ||
1493 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1494 ssl->s3->next_proto_negotiated_len) ||
1495 !CBB_add_u8_length_prefixed(&body, &child) ||
1496 !CBB_add_bytes(&child, kZero, padding_len) ||
1497 !ssl_add_message_cbb(ssl, cbb.get())) {
1498 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1499 return ssl_hs_error;
1500 }
1501 }
1502
David Benjamin4cbb9312017-08-31 14:52:31 -04001503 if (ssl->s3->tlsext_channel_id_valid) {
1504 ScopedCBB cbb;
1505 CBB body;
1506 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1507 !tls1_write_channel_id(hs, &body) ||
1508 !ssl_add_message_cbb(ssl, cbb.get())) {
1509 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1510 return ssl_hs_error;
1511 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001512 }
1513
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001514 if (!ssl3_send_finished(hs)) {
1515 return ssl_hs_error;
1516 }
1517
1518 hs->state = state_finish_flight;
1519 return ssl_hs_flush;
1520}
1521
1522static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001523 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001524 if (ssl->session != NULL) {
1525 hs->state = state_finish_client_handshake;
1526 return ssl_hs_ok;
1527 }
1528
David Benjaminc11ea9422017-08-29 16:33:21 -04001529 // This is a full handshake. If it involves ChannelID, then record the
1530 // handshake hashes at this point in the session so that any resumption of
1531 // this session with ChannelID can sign those hashes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001532 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1533 return ssl_hs_error;
1534 }
1535
1536 hs->state = state_read_session_ticket;
1537
1538 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1539 ssl3_can_false_start(ssl) &&
David Benjaminc11ea9422017-08-29 16:33:21 -04001540 // No False Start on renegotiation (would complicate the state machine).
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001541 !ssl->s3->initial_handshake_complete) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001542 hs->in_false_start = true;
1543 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001544 return ssl_hs_early_return;
1545 }
1546
1547 return ssl_hs_ok;
1548}
1549
1550static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1551 SSL *const ssl = hs->ssl;
1552
1553 if (!hs->ticket_expected) {
1554 hs->state = state_process_change_cipher_spec;
1555 return ssl_hs_read_change_cipher_spec;
1556 }
1557
David Benjamin7934f082017-08-01 16:32:25 -04001558 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001559 if (!ssl->method->get_message(ssl, &msg)) {
1560 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001561 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001562
David Benjamin7934f082017-08-01 16:32:25 -04001563 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1564 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001565 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001566 }
1567
David Benjamin7934f082017-08-01 16:32:25 -04001568 CBS new_session_ticket = msg.body, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001569 uint32_t tlsext_tick_lifetime_hint;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001570 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001571 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1572 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001573 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001574 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001575 return ssl_hs_error;
David Benjamin454aa4c2016-03-10 08:43:27 -05001576 }
1577
David Benjamin9f1dc822016-06-07 17:03:46 -04001578 if (CBS_len(&ticket) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001579 // RFC 5077 allows a server to change its mind and send no ticket after
1580 // negotiating the extension. The value of |ticket_expected| is checked in
1581 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
David Benjaminfd45ee72017-08-31 14:49:09 -04001582 hs->ticket_expected = false;
David Benjamin8f94c312017-08-01 17:35:55 -04001583 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001584 hs->state = state_process_change_cipher_spec;
1585 return ssl_hs_read_change_cipher_spec;
David Benjamin9f1dc822016-06-07 17:03:46 -04001586 }
1587
David Benjamin31b0c9b2017-07-20 14:49:15 -04001588 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001589 UniquePtr<SSL_SESSION> renewed_session;
1590 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001591 // The server is sending a new ticket for an existing session. Sessions are
1592 // immutable once established, so duplicate all but the ticket of the
1593 // existing session.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001594 renewed_session =
1595 SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001596 if (!renewed_session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001597 // This should never happen.
David Benjamin9f1dc822016-06-07 17:03:46 -04001598 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001599 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001600 }
David Benjamin1386aad2017-07-19 23:57:40 -04001601 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001602 }
1603
David Benjaminc11ea9422017-08-29 16:33:21 -04001604 // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued.
David Benjamin17b30832017-01-28 14:00:32 -05001605 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001606
Steven Valdez87eab492016-06-27 16:34:59 -04001607 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001608 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001609 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001610 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001611 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001612
David Benjaminc11ea9422017-08-29 16:33:21 -04001613 // Generate a session ID for this session based on the session ticket. We use
1614 // the session ID mechanism for detecting ticket resumption. This also fits in
1615 // with assumptions elsewhere in OpenSSL.
Steven Valdez87eab492016-06-27 16:34:59 -04001616 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1617 session->session_id, &session->session_id_length,
1618 EVP_sha256(), NULL)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001619 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001620 }
1621
David Benjamin1386aad2017-07-19 23:57:40 -04001622 if (renewed_session) {
Steven Valdez87eab492016-06-27 16:34:59 -04001623 session->not_resumable = 0;
1624 SSL_SESSION_free(ssl->session);
David Benjamin1386aad2017-07-19 23:57:40 -04001625 ssl->session = renewed_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -04001626 }
1627
David Benjamin8f94c312017-08-01 17:35:55 -04001628 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001629 hs->state = state_process_change_cipher_spec;
1630 return ssl_hs_read_change_cipher_spec;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001631}
David Benjamin86e95b82017-07-18 16:34:25 -04001632
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001633static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
David Benjamine58f8a62017-09-21 19:07:15 -04001634 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001635 return ssl_hs_error;
1636 }
1637
1638 hs->state = state_read_server_finished;
1639 return ssl_hs_ok;
1640}
1641
1642static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1643 SSL *const ssl = hs->ssl;
1644 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1645 if (wait != ssl_hs_ok) {
1646 return wait;
1647 }
1648
1649 if (ssl->session != NULL) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001650 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001651 return ssl_hs_ok;
1652 }
1653
1654 hs->state = state_finish_client_handshake;
1655 return ssl_hs_ok;
1656}
1657
1658static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1659 SSL *const ssl = hs->ssl;
1660
1661 ssl->method->on_handshake_complete(ssl);
1662
1663 SSL_SESSION_free(ssl->s3->established_session);
1664 if (ssl->session != NULL) {
1665 SSL_SESSION_up_ref(ssl->session);
1666 ssl->s3->established_session = ssl->session;
1667 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001668 // We make a copy of the session in order to maintain the immutability
1669 // of the new established_session due to False Start. The caller may
1670 // have taken a reference to the temporary session.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001671 ssl->s3->established_session =
1672 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL)
1673 .release();
1674 if (ssl->s3->established_session == NULL) {
1675 return ssl_hs_error;
1676 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001677 // Renegotiations do not participate in session resumption.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001678 if (!ssl->s3->initial_handshake_complete) {
1679 ssl->s3->established_session->not_resumable = 0;
1680 }
1681
1682 hs->new_session.reset();
1683 }
1684
David Benjaminfd45ee72017-08-31 14:49:09 -04001685 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001686 ssl->s3->initial_handshake_complete = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001687 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
1688
1689 hs->state = state_done;
1690 return ssl_hs_ok;
1691}
1692
1693enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1694 while (hs->state != state_done) {
1695 enum ssl_hs_wait_t ret = ssl_hs_error;
1696 enum ssl_client_hs_state_t state =
1697 static_cast<enum ssl_client_hs_state_t>(hs->state);
1698 switch (state) {
1699 case state_start_connect:
1700 ret = do_start_connect(hs);
1701 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001702 case state_enter_early_data:
1703 ret = do_enter_early_data(hs);
1704 break;
1705 case state_read_hello_verify_request:
1706 ret = do_read_hello_verify_request(hs);
1707 break;
1708 case state_read_server_hello:
1709 ret = do_read_server_hello(hs);
1710 break;
1711 case state_tls13:
1712 ret = do_tls13(hs);
1713 break;
1714 case state_read_server_certificate:
1715 ret = do_read_server_certificate(hs);
1716 break;
1717 case state_read_certificate_status:
1718 ret = do_read_certificate_status(hs);
1719 break;
1720 case state_verify_server_certificate:
1721 ret = do_verify_server_certificate(hs);
1722 break;
1723 case state_read_server_key_exchange:
1724 ret = do_read_server_key_exchange(hs);
1725 break;
1726 case state_read_certificate_request:
1727 ret = do_read_certificate_request(hs);
1728 break;
1729 case state_read_server_hello_done:
1730 ret = do_read_server_hello_done(hs);
1731 break;
1732 case state_send_client_certificate:
1733 ret = do_send_client_certificate(hs);
1734 break;
1735 case state_send_client_key_exchange:
1736 ret = do_send_client_key_exchange(hs);
1737 break;
1738 case state_send_client_certificate_verify:
1739 ret = do_send_client_certificate_verify(hs);
1740 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001741 case state_send_client_finished:
1742 ret = do_send_client_finished(hs);
1743 break;
1744 case state_finish_flight:
1745 ret = do_finish_flight(hs);
1746 break;
1747 case state_read_session_ticket:
1748 ret = do_read_session_ticket(hs);
1749 break;
1750 case state_process_change_cipher_spec:
1751 ret = do_process_change_cipher_spec(hs);
1752 break;
1753 case state_read_server_finished:
1754 ret = do_read_server_finished(hs);
1755 break;
1756 case state_finish_client_handshake:
1757 ret = do_finish_client_handshake(hs);
1758 break;
1759 case state_done:
1760 ret = ssl_hs_ok;
1761 break;
1762 }
1763
1764 if (hs->state != state) {
1765 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1766 }
1767
1768 if (ret != ssl_hs_ok) {
1769 return ret;
1770 }
1771 }
1772
1773 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1774 return ssl_hs_ok;
1775}
1776
1777const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1778 enum ssl_client_hs_state_t state =
1779 static_cast<enum ssl_client_hs_state_t>(hs->state);
1780 switch (state) {
1781 case state_start_connect:
1782 return "TLS client start_connect";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001783 case state_enter_early_data:
1784 return "TLS client enter_early_data";
1785 case state_read_hello_verify_request:
1786 return "TLS client read_hello_verify_request";
1787 case state_read_server_hello:
1788 return "TLS client read_server_hello";
1789 case state_tls13:
1790 return tls13_client_handshake_state(hs);
1791 case state_read_server_certificate:
1792 return "TLS client read_server_certificate";
1793 case state_read_certificate_status:
1794 return "TLS client read_certificate_status";
1795 case state_verify_server_certificate:
1796 return "TLS client verify_server_certificate";
1797 case state_read_server_key_exchange:
1798 return "TLS client read_server_key_exchange";
1799 case state_read_certificate_request:
1800 return "TLS client read_certificate_request";
1801 case state_read_server_hello_done:
1802 return "TLS client read_server_hello_done";
1803 case state_send_client_certificate:
1804 return "TLS client send_client_certificate";
1805 case state_send_client_key_exchange:
1806 return "TLS client send_client_key_exchange";
1807 case state_send_client_certificate_verify:
1808 return "TLS client send_client_certificate_verify";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001809 case state_send_client_finished:
1810 return "TLS client send_client_finished";
1811 case state_finish_flight:
1812 return "TLS client finish_flight";
1813 case state_read_session_ticket:
1814 return "TLS client read_session_ticket";
1815 case state_process_change_cipher_spec:
1816 return "TLS client process_change_cipher_spec";
1817 case state_read_server_finished:
1818 return "TLS client read_server_finished";
1819 case state_finish_client_handshake:
1820 return "TLS client finish_client_handshake";
1821 case state_done:
1822 return "TLS client done";
1823 }
1824
1825 return "TLS client unknown";
1826}
1827
1828}