blob: a779b8ada5281f46164b5958b8ca05e1fddf7b5c [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 Valdezdbe01582017-07-14 10:39:28 -0400319 ssl->tls13_variant == tls13_experiment &&
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
Steven Valdeza833c352016-11-01 13:39:36 -0400341 uint8_t *msg = NULL;
342 size_t len;
David Benjamind781fc42017-07-12 16:25:57 -0400343 if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) {
344 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400345 }
346
David Benjaminc11ea9422017-08-29 16:33:21 -0400347 // Now that the length prefixes have been computed, fill in the placeholder
348 // PSK binder.
David Benjamin8c880a22016-12-03 02:20:34 -0500349 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500350 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400351 OPENSSL_free(msg);
David Benjamind781fc42017-07-12 16:25:57 -0400352 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400353 }
354
David Benjamindaf207a2017-01-03 18:37:41 -0500355 return ssl->method->add_message(ssl, msg, len);
Steven Valdez5440fe02016-07-18 12:40:30 -0400356}
357
David Benjamin7934f082017-08-01 16:32:25 -0400358static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out,
359 const SSLMessage &msg) {
David Benjaminb853f312017-07-14 18:40:34 -0400360 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400361 if (msg.type != SSL3_MT_SERVER_HELLO &&
362 msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
David Benjaminb853f312017-07-14 18:40:34 -0400363 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
364 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
365 return 0;
366 }
367
David Benjamin7934f082017-08-01 16:32:25 -0400368 CBS server_hello = msg.body;
David Benjaminb853f312017-07-14 18:40:34 -0400369 if (!CBS_get_u16(&server_hello, out)) {
370 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
371 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
372 return 0;
373 }
374
David Benjaminc11ea9422017-08-29 16:33:21 -0400375 // The server version may also be in the supported_versions extension if
376 // applicable.
David Benjamin7934f082017-08-01 16:32:25 -0400377 if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) {
David Benjaminb853f312017-07-14 18:40:34 -0400378 return 1;
379 }
380
381 uint8_t sid_length;
382 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
383 !CBS_get_u8(&server_hello, &sid_length) ||
384 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
385 1 /* compression_method */)) {
386 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
388 return 0;
389 }
390
David Benjaminc11ea9422017-08-29 16:33:21 -0400391 // The extensions block may not be present.
David Benjaminb853f312017-07-14 18:40:34 -0400392 if (CBS_len(&server_hello) == 0) {
393 return 1;
394 }
395
396 CBS extensions;
397 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
398 CBS_len(&server_hello) != 0) {
399 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
400 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
401 return 0;
402 }
403
David Benjamin74795b32017-08-31 15:13:12 -0400404 bool have_supported_versions;
David Benjaminb853f312017-07-14 18:40:34 -0400405 CBS supported_versions;
406 const SSL_EXTENSION_TYPE ext_types[] = {
407 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
408 &supported_versions},
409 };
410
411 uint8_t alert = SSL_AD_DECODE_ERROR;
412 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
413 OPENSSL_ARRAY_SIZE(ext_types),
414 1 /* ignore unknown */)) {
415 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
416 return 0;
417 }
418
419 if (have_supported_versions &&
420 (!CBS_get_u16(&supported_versions, out) ||
421 CBS_len(&supported_versions) != 0)) {
422 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
423 return 0;
424 }
425
426 return 1;
427}
428
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400429static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400430 SSL *const ssl = hs->ssl;
431
Steven Valdez398085b2017-08-29 13:25:56 -0400432 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
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
David Benjaminc11ea9422017-08-29 16:33:21 -0400439 // Always advertise the ClientHello version from the original maximum version,
440 // even on renegotiation. The static RSA key exchange uses this field, and
441 // some servers fail when it changes across handshakes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400442 if (SSL_is_dtls(hs->ssl)) {
443 hs->client_version =
444 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
445 } else {
446 hs->client_version =
447 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
448 }
449
David Benjaminc11ea9422017-08-29 16:33:21 -0400450 // If the configured session has expired or was created at a disabled
451 // version, drop it.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400452 if (ssl->session != NULL) {
453 if (ssl->session->is_server ||
454 !ssl_supports_version(hs, ssl->session->ssl_version) ||
455 (ssl->session->session_id_length == 0 &&
456 ssl->session->tlsext_ticklen == 0) ||
457 ssl->session->not_resumable ||
458 !ssl_session_is_time_valid(ssl, ssl->session)) {
459 ssl_set_session(ssl, NULL);
460 }
461 }
462
Steven Valdez398085b2017-08-29 13:25:56 -0400463 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400464 return ssl_hs_error;
465 }
466
David Benjaminc11ea9422017-08-29 16:33:21 -0400467 // Initialize a random session ID for the experimental TLS 1.3 variant
468 // requiring a session id.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400469 if (ssl->tls13_variant == tls13_experiment) {
470 hs->session_id_len = sizeof(hs->session_id);
471 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
472 return ssl_hs_error;
473 }
474 }
475
476 if (!ssl_write_client_hello(hs)) {
477 return ssl_hs_error;
478 }
479
480 hs->state = state_enter_early_data;
481 return ssl_hs_flush;
482}
483
484static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
485 SSL *const ssl = hs->ssl;
486
Steven Valdez398085b2017-08-29 13:25:56 -0400487 if (SSL_is_dtls(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400488 hs->state = state_read_hello_verify_request;
489 return ssl_hs_ok;
490 }
491
492 if (!hs->early_data_offered) {
493 hs->state = state_read_server_hello;
494 return ssl_hs_ok;
495 }
496
497 if (!tls13_init_early_key_schedule(hs) ||
498 !tls13_advance_key_schedule(hs, ssl->session->master_key,
499 ssl->session->master_key_length) ||
500 !tls13_derive_early_secrets(hs) ||
501 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
502 hs->hash_len)) {
503 return ssl_hs_error;
504 }
505
David Benjaminc11ea9422017-08-29 16:33:21 -0400506 // Stash the early data session, so connection properties may be queried out
507 // of it.
David Benjaminfd45ee72017-08-31 14:49:09 -0400508 hs->in_early_data = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400509 SSL_SESSION_up_ref(ssl->session);
510 hs->early_session.reset(ssl->session);
David Benjaminfd45ee72017-08-31 14:49:09 -0400511 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400512
513 hs->state = state_read_server_hello;
514 return ssl_hs_early_return;
515}
516
517static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
518 SSL *const ssl = hs->ssl;
519
520 assert(SSL_is_dtls(ssl));
521
522 SSLMessage msg;
523 if (!ssl->method->get_message(ssl, &msg)) {
524 return ssl_hs_read_message;
525 }
526
527 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400528 hs->state = state_read_server_hello;
529 return ssl_hs_ok;
530 }
531
532 CBS hello_verify_request = msg.body, cookie;
533 uint16_t server_version;
534 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
535 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
536 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
537 CBS_len(&hello_verify_request) != 0) {
538 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
539 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
540 return ssl_hs_error;
541 }
542
543 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
544 ssl->d1->cookie_len = CBS_len(&cookie);
545
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400546 ssl->method->next_message(ssl);
Steven Valdez398085b2017-08-29 13:25:56 -0400547
David Benjaminc11ea9422017-08-29 16:33:21 -0400548 // DTLS resets the handshake buffer after HelloVerifyRequest.
Steven Valdez398085b2017-08-29 13:25:56 -0400549 if (!hs->transcript.Init()) {
550 return ssl_hs_error;
551 }
552
553 if (!ssl_write_client_hello(hs)) {
554 return ssl_hs_error;
555 }
556
557 hs->state = state_read_server_hello;
558 return ssl_hs_flush;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400559}
560
561static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900562 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400563 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400564 if (!ssl->method->get_message(ssl, &msg)) {
565 return ssl_hs_read_server_hello;
Adam Langley1bea1732014-12-17 19:06:57 -0800566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
David Benjaminb853f312017-07-14 18:40:34 -0400568 uint16_t server_version;
David Benjamin7934f082017-08-01 16:32:25 -0400569 if (!parse_server_version(hs, &server_version, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400570 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400571 }
572
Steven Valdez8f36c512017-06-20 10:55:02 -0400573 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400574 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400575 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400576 return ssl_hs_error;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400577 }
578
David Benjamin0d56f882015-12-19 17:05:56 -0500579 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
580 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400581 ssl->version = server_version;
David Benjaminc11ea9422017-08-29 16:33:21 -0400582 // At this point, the connection's version is known and ssl->version is
583 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400584 ssl->s3->have_version = true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400585 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400586 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400587 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400588 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800589 }
Adam Langley95c29f32014-06-20 12:00:00 -0700590
Steven Valdez143e8b32016-07-11 13:19:03 -0400591 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400592 hs->state = state_tls13;
593 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400594 }
595
Steven Valdez2d850622017-01-11 11:34:52 -0500596 if (hs->early_data_offered) {
597 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400598 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400599 return ssl_hs_error;
Steven Valdez2d850622017-01-11 11:34:52 -0500600 }
601
David Benjamin6e4fc332016-11-17 16:43:08 +0900602 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400603
David Benjamin7934f082017-08-01 16:32:25 -0400604 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400605 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400606 }
607
David Benjamin7934f082017-08-01 16:32:25 -0400608 CBS server_hello = msg.body, server_random, session_id;
David Benjaminb853f312017-07-14 18:40:34 -0400609 uint16_t cipher_suite;
610 uint8_t compression_method;
David Benjaminb853f312017-07-14 18:40:34 -0400611 if (!CBS_skip(&server_hello, 2 /* version */) ||
612 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400613 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
614 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
615 !CBS_get_u16(&server_hello, &cipher_suite) ||
616 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400617 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400618 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400619 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400620 }
621
David Benjaminc11ea9422017-08-29 16:33:21 -0400622 // Copy over the server random.
David Benjamind3bca042017-04-08 11:05:04 -0400623 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
624 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700625
David Benjaminc11ea9422017-08-29 16:33:21 -0400626 // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
627 // 1.3 is finalized and we are not implementing a draft version.
David Benjamin1f61f0d2016-07-10 12:20:35 -0400628
David Benjamin0d56f882015-12-19 17:05:56 -0500629 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400630 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500631 CBS_mem_equal(&session_id, ssl->session->session_id,
632 ssl->session->session_id_length)) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400633 ssl->s3->session_reused = true;
Adam Langley1bea1732014-12-17 19:06:57 -0800634 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400635 // The session wasn't resumed. Create a fresh SSL_SESSION to
636 // fill out.
David Benjamin4d0be242016-09-01 01:10:07 -0400637 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900638 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400639 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400640 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800641 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400642 // Note: session_id could be empty.
David Benjamin45738dd2017-02-09 20:01:26 -0500643 hs->new_session->session_id_length = CBS_len(&session_id);
644 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
645 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800646 }
David Benjamina03d95d2014-07-12 19:49:07 -0400647
David Benjaminca9e8f52017-08-09 15:02:34 -0400648 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
649 if (cipher == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400650 // unknown cipher
David Benjamin3570d732015-06-29 00:28:17 -0400651 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400652 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400653 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800654 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400655
David Benjaminc11ea9422017-08-29 16:33:21 -0400656 // The cipher must be allowed in the selected version and enabled.
David Benjamin2fe6e222017-01-25 02:18:43 -0500657 uint32_t mask_a, mask_k;
658 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
David Benjaminca9e8f52017-08-09 15:02:34 -0400659 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
660 SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
661 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) ||
662 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_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 }
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Steven Valdez87eab492016-06-27 16:34:59 -0400668 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500669 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400670 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400671 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400672 return ssl_hs_error;
David Benjaminece3de92015-03-16 18:02:20 -0400673 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400674 if (ssl->session->cipher != cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400675 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_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;
Steven Valdez803c77a2016-09-06 14:13:43 -0400678 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400679 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400680 // This is actually a client application bug.
Steven Valdez4aa154e2016-07-29 14:32:55 -0400681 OPENSSL_PUT_ERROR(SSL,
682 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
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;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400685 }
David Benjamin0623bce2015-12-25 15:40:14 -0500686 } else {
David Benjaminca9e8f52017-08-09 15:02:34 -0400687 hs->new_session->cipher = cipher;
Adam Langley1bea1732014-12-17 19:06:57 -0800688 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400689 hs->new_cipher = cipher;
David Benjaminbdf5e722014-11-11 00:52:15 -0500690
David Benjaminc11ea9422017-08-29 16:33:21 -0400691 // Now that the cipher is known, initialize the handshake hash and hash the
692 // ServerHello.
David Benjaminca9e8f52017-08-09 15:02:34 -0400693 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400694 !ssl_hash_message(hs, msg)) {
David Benjamind3bca042017-04-08 11:05:04 -0400695 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400696 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400697 }
698
David Benjaminc11ea9422017-08-29 16:33:21 -0400699 // If doing a full handshake, the server may request a client certificate
700 // which requires hashing the handshake transcript. Otherwise, the handshake
701 // buffer may be released.
David Benjamin3d458dc2016-09-12 22:40:27 +0000702 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500703 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400704 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -0800705 }
David Benjamina03d95d2014-07-12 19:49:07 -0400706
David Benjaminc11ea9422017-08-29 16:33:21 -0400707 // Only the NULL compression algorithm is supported.
Adam Langley1bea1732014-12-17 19:06:57 -0800708 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400709 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -0400710 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400711 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800712 }
Adam Langley95c29f32014-06-20 12:00:00 -0700713
David Benjaminc11ea9422017-08-29 16:33:21 -0400714 // TLS extensions
David Benjamin8c880a22016-12-03 02:20:34 -0500715 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400716 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400717 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800718 }
Adam Langley95c29f32014-06-20 12:00:00 -0700719
David Benjaminc11ea9422017-08-29 16:33:21 -0400720 // There should be nothing left over in the record.
Adam Langley1bea1732014-12-17 19:06:57 -0800721 if (CBS_len(&server_hello) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400722 // wrong packet length
David Benjamin639846e2016-09-09 11:41:18 -0400723 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400724 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400725 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800726 }
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Steven Valdez87eab492016-06-27 16:34:59 -0400728 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -0500729 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -0500730 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400731 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700732 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400733 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700734 }
David Benjamind3bca042017-04-08 11:05:04 -0400735 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400736 return ssl_hs_error;
Adam Langleyba5934b2015-06-02 10:50:35 -0700737 }
738
David Benjamin8f94c312017-08-01 17:35:55 -0400739 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400740
741 if (ssl->session != NULL) {
742 hs->state = state_read_session_ticket;
743 return ssl_hs_ok;
744 }
745
746 hs->state = state_read_server_certificate;
747 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800748}
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400750static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
751 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
752 if (wait == ssl_hs_ok) {
753 hs->state = state_finish_client_handshake;
754 return ssl_hs_ok;
755 }
756
757 return wait;
758}
759
760static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900761 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400762
763 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
764 hs->state = state_read_certificate_status;
765 return ssl_hs_ok;
766 }
767
David Benjamin7934f082017-08-01 16:32:25 -0400768 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400769 if (!ssl->method->get_message(ssl, &msg)) {
770 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800771 }
Adam Langley95c29f32014-06-20 12:00:00 -0700772
David Benjamin7934f082017-08-01 16:32:25 -0400773 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
774 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400775 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -0500776 }
777
David Benjamin7934f082017-08-01 16:32:25 -0400778 CBS body = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800779 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400780 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
David Benjamin7934f082017-08-01 16:32:25 -0400781 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
David Benjamine664a532017-07-20 20:19:36 -0400782 ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400783 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400784 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800785 }
David Benjamine664a532017-07-20 20:19:36 -0400786 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
787 hs->new_session->certs = chain.release();
Adam Langley95c29f32014-06-20 12:00:00 -0700788
David Benjamin45738dd2017-02-09 20:01:26 -0500789 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -0400790 CBS_len(&body) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400791 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -0400792 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -0400793 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400794 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800795 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400796
Adam Langley05672202016-12-13 12:05:49 -0800797 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -0400798 hs, hs->peer_pubkey.get(),
David Benjamin45738dd2017-02-09 20:01:26 -0500799 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -0400800 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400801 return ssl_hs_error;
David Benjamin436bf822015-06-07 19:50:37 -0400802 }
803
David Benjamin8f94c312017-08-01 17:35:55 -0400804 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400805
806 hs->state = state_read_certificate_status;
807 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800808}
809
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400810static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900811 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400812
813 if (!hs->certificate_status_expected) {
814 hs->state = state_verify_server_certificate;
815 return ssl_hs_ok;
816 }
817
David Benjamin7934f082017-08-01 16:32:25 -0400818 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400819 if (!ssl->method->get_message(ssl, &msg)) {
820 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400821 }
822
David Benjamin7934f082017-08-01 16:32:25 -0400823 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400824 // A server may send status_request in ServerHello and then change its mind
825 // about sending CertificateStatus.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400826 hs->state = state_verify_server_certificate;
827 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400828 }
829
David Benjamin7934f082017-08-01 16:32:25 -0400830 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400831 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500832 }
833
David Benjamin7934f082017-08-01 16:32:25 -0400834 CBS certificate_status = msg.body, ocsp_response;
David Benjamin8d606e32017-06-15 22:43:04 -0400835 uint8_t status_type;
David Benjamin9f1dc822016-06-07 17:03:46 -0400836 if (!CBS_get_u8(&certificate_status, &status_type) ||
837 status_type != TLSEXT_STATUSTYPE_ocsp ||
838 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
839 CBS_len(&ocsp_response) == 0 ||
840 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400841 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400842 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400843 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400844 }
845
David Benjamin8fc2dc02017-08-22 15:07:51 -0700846 CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
847 hs->new_session->ocsp_response =
848 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
849 if (hs->new_session->ocsp_response == nullptr) {
David Benjamin8d606e32017-06-15 22:43:04 -0400850 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400851 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400852 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400853
David Benjamin8f94c312017-08-01 17:35:55 -0400854 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400855
856 hs->state = state_verify_server_certificate;
857 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400858}
859
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400860static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
861 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
862 hs->state = state_read_server_key_exchange;
863 return ssl_hs_ok;
864 }
865
866 switch (ssl_verify_peer_cert(hs)) {
867 case ssl_verify_ok:
868 break;
869 case ssl_verify_invalid:
870 return ssl_hs_error;
871 case ssl_verify_retry:
872 hs->state = state_verify_server_certificate;
873 return ssl_hs_certificate_verify;
874 }
875
876 hs->state = state_read_server_key_exchange;
877 return ssl_hs_ok;
878}
879
880static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900881 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400882 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400883 if (!ssl->method->get_message(ssl, &msg)) {
884 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800885 }
886
David Benjamin7934f082017-08-01 16:32:25 -0400887 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400888 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
David Benjamin45738dd2017-02-09 20:01:26 -0500889 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400890 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -0500891 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400892 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800893 }
894
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400895 hs->state = state_read_certificate_request;
896 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800897 }
898
David Benjamin7934f082017-08-01 16:32:25 -0400899 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400900 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500901 }
902
David Benjamin45738dd2017-02-09 20:01:26 -0500903 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
904 uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin7934f082017-08-01 16:32:25 -0400905 CBS server_key_exchange = msg.body;
Adam Langley1bea1732014-12-17 19:06:57 -0800906 if (alg_a & SSL_aPSK) {
907 CBS psk_identity_hint;
908
David Benjaminc11ea9422017-08-29 16:33:21 -0400909 // Each of the PSK key exchanges begins with a psk_identity_hint.
Adam Langley1bea1732014-12-17 19:06:57 -0800910 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
911 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400912 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400913 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400914 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800915 }
916
David Benjaminc11ea9422017-08-29 16:33:21 -0400917 // Store PSK identity hint for later use, hint is used in
918 // ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
919 // identity hint can be as long as the maximum length of a PSK identity.
920 // Also do not allow NULL characters; identities are saved as C strings.
921 //
922 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
923 // a specific identity.
Adam Langley1bea1732014-12-17 19:06:57 -0800924 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
925 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400926 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -0400927 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400928 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800929 }
930
David Benjaminc11ea9422017-08-29 16:33:21 -0400931 // Save non-empty identity hints as a C string. Empty identity hints we
932 // treat as missing. Plain PSK makes it possible to send either no hint
933 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
934 // empty hint. Having different capabilities is odd, so we interpret empty
935 // and missing as identical.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400936 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -0400937 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -0400938 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -0400939 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -0400940 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400941 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800942 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400943 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -0800944 }
945
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700946 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400947 // Parse the server parameters.
Steven Valdezce902a92016-05-17 11:47:53 -0400948 uint8_t group_type;
949 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -0500950 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -0400951 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
952 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -0700953 !CBS_get_u16(&server_key_exchange, &group_id) ||
954 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -0400955 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400956 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400957 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800958 }
David Benjamin45738dd2017-02-09 20:01:26 -0500959 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -0800960
David Benjaminc11ea9422017-08-29 16:33:21 -0400961 // Ensure the group is consistent with preferences.
Steven Valdezce902a92016-05-17 11:47:53 -0400962 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -0500963 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -0400964 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400965 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800966 }
David Benjamin4298d772015-12-19 00:18:25 -0500967
David Benjaminc11ea9422017-08-29 16:33:21 -0400968 // Initialize ECDH and save the peer public key for later.
David Benjaminc642aca2017-07-19 23:28:43 -0400969 hs->key_share = SSLKeyShare::Create(group_id);
970 if (!hs->key_share ||
David Benjaminc3c88822016-11-14 10:32:04 +0900971 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400972 return ssl_hs_error;
David Benjaminc1cc8582015-12-17 01:22:15 -0500973 }
Adam Langley1bea1732014-12-17 19:06:57 -0800974 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -0400975 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -0400976 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400977 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800978 }
979
David Benjaminc11ea9422017-08-29 16:33:21 -0400980 // At this point, |server_key_exchange| contains the signature, if any, while
981 // |msg.body| contains the entire message. From that, derive a CBS containing
982 // just the parameter.
David Benjaminb35d6842015-12-29 17:08:08 -0500983 CBS parameter;
David Benjamin7934f082017-08-01 16:32:25 -0400984 CBS_init(&parameter, CBS_data(&msg.body),
985 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
Adam Langley1bea1732014-12-17 19:06:57 -0800986
David Benjaminc11ea9422017-08-29 16:33:21 -0400987 // ServerKeyExchange should be signed by the server's public key.
David Benjamin45738dd2017-02-09 20:01:26 -0500988 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400989 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -0500990 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -0400991 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -0500992 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400993 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400994 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800995 }
David Benjamin8d606e32017-06-15 22:43:04 -0400996 uint8_t alert = SSL_AD_DECODE_ERROR;
997 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
998 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400999 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001000 }
David Benjamin45738dd2017-02-09 20:01:26 -05001001 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001002 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001003 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001004 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001005 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001006 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001007 }
1008
David Benjaminc11ea9422017-08-29 16:33:21 -04001009 // The last field in |server_key_exchange| is the signature.
David Benjaminf4958e72015-06-07 11:00:53 -04001010 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001011 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1012 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001013 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001014 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001015 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001016 }
1017
David Benjamin1386aad2017-07-19 23:57:40 -04001018 ScopedCBB transcript;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001019 uint8_t *transcript_data;
1020 size_t transcript_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001021 if (!CBB_init(transcript.get(),
1022 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1023 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1024 SSL3_RANDOM_SIZE) ||
1025 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1026 SSL3_RANDOM_SIZE) ||
1027 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1028 CBS_len(&parameter)) ||
1029 !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001030 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001031 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001032 return ssl_hs_error;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001033 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001034
1035 int sig_ok = ssl_public_key_verify(
1036 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001037 hs->peer_pubkey.get(), transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001038 OPENSSL_free(transcript_data);
1039
David Benjaminbf82aed2016-03-01 22:57:40 -05001040#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1041 sig_ok = 1;
1042 ERR_clear_error();
1043#endif
1044 if (!sig_ok) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001045 // bad signature
David Benjamin3570d732015-06-29 00:28:17 -04001046 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001047 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001048 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001049 }
1050 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001051 // PSK ciphers are the only supported certificate-less ciphers.
David Benjaminf4958e72015-06-07 11:00:53 -04001052 assert(alg_a == SSL_aPSK);
1053
Adam Langley1bea1732014-12-17 19:06:57 -08001054 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001055 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001056 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001057 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001058 }
1059 }
David Benjamin8f94c312017-08-01 17:35:55 -04001060
1061 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001062 hs->state = state_read_certificate_request;
1063 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001064}
Adam Langley95c29f32014-06-20 12:00:00 -07001065
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001066static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001067 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001068
1069 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1070 hs->state = state_read_server_hello_done;
1071 return ssl_hs_ok;
1072 }
1073
David Benjamin7934f082017-08-01 16:32:25 -04001074 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001075 if (!ssl->method->get_message(ssl, &msg)) {
1076 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001077 }
David Benjamin688d8df2014-11-02 23:06:42 -05001078
David Benjamin7934f082017-08-01 16:32:25 -04001079 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001080 // If we get here we don't need the handshake buffer as we won't be doing
1081 // client auth.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001082 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001083 hs->state = state_read_server_hello_done;
1084 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001085 }
David Benjamined439582014-07-14 19:13:02 -04001086
David Benjamin7934f082017-08-01 16:32:25 -04001087 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1088 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001089 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001090 }
Adam Langley95c29f32014-06-20 12:00:00 -07001091
David Benjaminc11ea9422017-08-29 16:33:21 -04001092 // Get the certificate types.
David Benjamin7934f082017-08-01 16:32:25 -04001093 CBS body = msg.body, certificate_types;
1094 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001095 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001096 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001097 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001098 }
David Benjamined439582014-07-14 19:13:02 -04001099
David Benjaminc3c88822016-11-14 10:32:04 +09001100 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1101 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001102 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001103 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001104 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001105
David Benjamina1e9cab2015-12-30 00:08:49 -05001106 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001107 CBS supported_signature_algorithms;
David Benjamin7934f082017-08-01 16:32:25 -04001108 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001109 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001110 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001111 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001112 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001113 }
Adam Langley1bea1732014-12-17 19:06:57 -08001114 }
David Benjamined439582014-07-14 19:13:02 -04001115
Adam Langleyc68e5b92017-02-08 13:33:15 -08001116 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001117 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -04001118 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001119 if (!ca_names) {
David Benjamine0332e82016-07-13 22:40:36 -04001120 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001121 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
David Benjamin7934f082017-08-01 16:32:25 -04001124 if (CBS_len(&body) != 0) {
David Benjamin639846e2016-09-09 11:41:18 -04001125 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001126 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001127 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001128 }
1129
David Benjaminfd45ee72017-08-31 14:49:09 -04001130 hs->cert_request = true;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001131 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001132 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001133
David Benjamin8f94c312017-08-01 17:35:55 -04001134 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001135 hs->state = state_read_server_hello_done;
1136 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001137}
Adam Langley95c29f32014-06-20 12:00:00 -07001138
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001139static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001140 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001141 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001142 if (!ssl->method->get_message(ssl, &msg)) {
1143 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001144 }
David Benjamin46062682014-07-14 19:14:32 -04001145
David Benjamin7934f082017-08-01 16:32:25 -04001146 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1147 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001148 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001149 }
1150
David Benjaminc11ea9422017-08-29 16:33:21 -04001151 // ServerHelloDone is empty.
David Benjamin7934f082017-08-01 16:32:25 -04001152 if (CBS_len(&msg.body) != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001153 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001154 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001155 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001156 }
David Benjamin46062682014-07-14 19:14:32 -04001157
David Benjamin8f94c312017-08-01 17:35:55 -04001158 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001159 hs->state = state_send_client_certificate;
1160 return ssl_hs_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001161}
Adam Langley1258b6a2014-06-20 12:00:00 -07001162
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001163static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001164 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001165
David Benjaminc11ea9422017-08-29 16:33:21 -04001166 // The peer didn't request a certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001167 if (!hs->cert_request) {
1168 hs->state = state_send_client_key_exchange;
1169 return ssl_hs_ok;
1170 }
1171
David Benjaminc11ea9422017-08-29 16:33:21 -04001172 // Call cert_cb to update the certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001173 if (ssl->cert->cert_cb != NULL) {
1174 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1175 if (rv == 0) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001176 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001177 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1178 return ssl_hs_error;
1179 }
1180 if (rv < 0) {
1181 hs->state = state_send_client_certificate;
1182 return ssl_hs_x509_lookup;
David Benjamin9f1dc822016-06-07 17:03:46 -04001183 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001184 }
1185
David Benjamin5edfc8c2016-12-10 15:46:58 -05001186 if (!ssl_has_certificate(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001187 // Without a client certificate, the handshake buffer may be released.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001188 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001189
David Benjaminc11ea9422017-08-29 16:33:21 -04001190 // In SSL 3.0, the Certificate message is replaced with a warning alert.
David Benjamin5edfc8c2016-12-10 15:46:58 -05001191 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001192 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1193 SSL_AD_NO_CERTIFICATE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001194 return ssl_hs_error;
David Benjamindaf207a2017-01-03 18:37:41 -05001195 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001196 hs->state = state_send_client_key_exchange;
1197 return ssl_hs_ok;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001198 }
1199 }
1200
David Benjamina232a712017-03-30 15:51:53 -05001201 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001202 !ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001203 return ssl_hs_error;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001204 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001205
1206
1207 hs->state = state_send_client_key_exchange;
1208 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001209}
1210
David Benjamina3d76d02017-07-14 19:36:07 -04001211static_assert(sizeof(size_t) >= sizeof(unsigned),
1212 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001213
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001214static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001215 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001216 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001217 CBB body;
1218 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001219 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001220 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001221 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001222
David Benjamind781fc42017-07-12 16:25:57 -04001223 uint8_t *pms = NULL;
1224 size_t pms_len = 0;
David Benjamin45738dd2017-02-09 20:01:26 -05001225 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1226 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001227
David Benjaminc11ea9422017-08-29 16:33:21 -04001228 // If using a PSK key exchange, prepare the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001229 unsigned psk_len = 0;
1230 uint8_t psk[PSK_MAX_PSK_LEN];
1231 if (alg_a & SSL_aPSK) {
1232 if (ssl->psk_client_callback == NULL) {
1233 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1234 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001235 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001236
David Benjaminae0eaaa2015-12-17 02:01:25 -05001237 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001238 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001239 psk_len =
David Benjamin31b0c9b2017-07-20 14:49:15 -04001240 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
1241 identity, sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001242 if (psk_len == 0) {
1243 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1244 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1245 goto err;
1246 }
1247 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001248
David Benjamin45738dd2017-02-09 20:01:26 -05001249 OPENSSL_free(hs->new_session->psk_identity);
1250 hs->new_session->psk_identity = BUF_strdup(identity);
1251 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001252 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1253 goto err;
1254 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001255
David Benjaminc11ea9422017-08-29 16:33:21 -04001256 // Write out psk_identity.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001257 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001258 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001259 !CBB_add_bytes(&child, (const uint8_t *)identity,
1260 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001261 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001262 goto err;
1263 }
1264 }
David Benjamin758d1272015-11-20 17:47:25 -05001265
David Benjaminc11ea9422017-08-29 16:33:21 -04001266 // Depending on the key exchange method, compute |pms| and |pms_len|.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001267 if (alg_k & SSL_kRSA) {
1268 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001269 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001270 if (pms == NULL) {
1271 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1272 goto err;
1273 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001274
David Benjamin31b0c9b2017-07-20 14:49:15 -04001275 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001276 if (rsa == NULL) {
1277 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001278 goto err;
1279 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001280
David Benjaminf04c2e92016-12-06 13:35:25 -05001281 pms[0] = hs->client_version >> 8;
1282 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001283 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1284 goto err;
1285 }
1286
David Benjamin75836432016-06-17 18:48:29 -04001287 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001288 size_t enc_pms_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001289 // In TLS, there is a length prefix.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001290 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001291 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001292 goto err;
1293 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001294 enc_pms = &child;
1295 }
Adam Langley1bea1732014-12-17 19:06:57 -08001296
David Benjaminae0eaaa2015-12-17 02:01:25 -05001297 uint8_t *ptr;
1298 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1299 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1300 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001301 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001302 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001303 goto err;
1304 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001305 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001306 // Generate a keypair and serialize the public half.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001307 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001308 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001309 goto err;
1310 }
1311
David Benjaminc11ea9422017-08-29 16:33:21 -04001312 // Compute the premaster.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001313 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001314 if (!hs->key_share->Accept(&child, &pms, &pms_len, &alert, hs->peer_key,
1315 hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001316 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1317 goto err;
1318 }
David Benjamin75836432016-06-17 18:48:29 -04001319 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001320 goto err;
1321 }
David Benjamin4298d772015-12-19 00:18:25 -05001322
David Benjaminc11ea9422017-08-29 16:33:21 -04001323 // The key exchange state may now be discarded.
David Benjaminc642aca2017-07-19 23:28:43 -04001324 hs->key_share.reset();
David Benjaminc3c88822016-11-14 10:32:04 +09001325 OPENSSL_free(hs->peer_key);
1326 hs->peer_key = NULL;
1327 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001328 } else if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001329 // For plain PSK, other_secret is a block of 0s with the same length as
1330 // the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001331 pms_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001332 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001333 if (pms == NULL) {
1334 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1335 goto err;
1336 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001337 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001338 } else {
1339 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1340 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1341 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001342 }
1343
David Benjaminc11ea9422017-08-29 16:33:21 -04001344 // For a PSK cipher suite, other_secret is combined with the pre-shared
1345 // key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001346 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001347 ScopedCBB pms_cbb;
1348 CBB child;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001349 uint8_t *new_pms;
1350 size_t new_pms_len;
1351
David Benjamin1386aad2017-07-19 23:57:40 -04001352 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms_len) ||
1353 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001354 !CBB_add_bytes(&child, pms, pms_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001355 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001356 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001357 !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001358 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1359 goto err;
1360 }
1361 OPENSSL_cleanse(pms, pms_len);
1362 OPENSSL_free(pms);
1363 pms = new_pms;
1364 pms_len = new_pms_len;
1365 }
1366
David Benjaminc11ea9422017-08-29 16:33:21 -04001367 // The message must be added to the finished hash before calculating the
1368 // master secret.
David Benjamind781fc42017-07-12 16:25:57 -04001369 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001370 goto err;
1371 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001372
David Benjamin45738dd2017-02-09 20:01:26 -05001373 hs->new_session->master_key_length = tls1_generate_master_secret(
1374 hs, hs->new_session->master_key, pms, pms_len);
1375 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001376 goto err;
1377 }
David Benjaminfc02b592017-02-17 16:26:01 -05001378 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001379 OPENSSL_cleanse(pms, pms_len);
1380 OPENSSL_free(pms);
1381
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001382 hs->state = state_send_client_certificate_verify;
1383 return ssl_hs_ok;
Adam Langley1258b6a2014-06-20 12:00:00 -07001384
1385err:
David Benjaminae0eaaa2015-12-17 02:01:25 -05001386 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001387 OPENSSL_cleanse(pms, pms_len);
1388 OPENSSL_free(pms);
1389 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001390 return ssl_hs_error;
1391
Adam Langley1bea1732014-12-17 19:06:57 -08001392}
Adam Langley1258b6a2014-06-20 12:00:00 -07001393
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001394static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001395 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001396
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001397 if (!hs->cert_request || !ssl_has_certificate(ssl)) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001398 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001399 return ssl_hs_ok;
1400 }
1401
1402 assert(ssl_has_private_key(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -04001403 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001404 CBB body, child;
1405 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001406 SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001407 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001408 }
1409
David Benjaminea9a0d52016-07-08 15:52:59 -07001410 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001411 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001412 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001413 }
David Benjamin429fdc02016-06-17 17:33:30 -04001414 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001415 // Write out the digest type in TLS 1.2.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001416 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001417 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001418 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001419 }
1420 }
David Benjaminfc825122015-12-18 01:57:43 -05001421
David Benjaminc11ea9422017-08-29 16:33:21 -04001422 // Set aside space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001423 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001424 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001425 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001426 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001427 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001428 }
1429
Steven Valdez2b8415e2016-06-30 13:27:23 -04001430 size_t sig_len = max_sig_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001431 // The SSL3 construction for CertificateVerify does not decompose into a
1432 // single final digest and signature, and must be special-cased.
David Benjamin44148742017-06-17 13:20:59 -04001433 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1434 if (ssl->cert->key_method != NULL) {
1435 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001436 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001437 }
1438
David Benjamin44148742017-06-17 13:20:59 -04001439 uint8_t digest[EVP_MAX_MD_SIZE];
1440 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001441 if (!hs->transcript.GetSSL3CertVerifyHash(
1442 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001443 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001444 }
David Benjaminfc825122015-12-18 01:57:43 -05001445
David Benjamin1386aad2017-07-19 23:57:40 -04001446 UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
1447 if (!pctx ||
1448 !EVP_PKEY_sign_init(pctx.get()) ||
1449 !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001450 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001451 }
1452 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001453 switch (ssl_private_key_sign(
1454 hs, ptr, &sig_len, max_sig_len, signature_algorithm,
1455 hs->transcript.buffer_data(), hs->transcript.buffer_len())) {
David Benjamin44148742017-06-17 13:20:59 -04001456 case ssl_private_key_success:
1457 break;
1458 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001459 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001460 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001461 hs->state = state_send_client_certificate_verify;
1462 return ssl_hs_private_key_operation;
David Benjamin44148742017-06-17 13:20:59 -04001463 }
David Benjaminfc825122015-12-18 01:57:43 -05001464 }
1465
David Benjaminfc825122015-12-18 01:57:43 -05001466 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001467 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001468 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001469 }
1470
David Benjaminc11ea9422017-08-29 16:33:21 -04001471 // The handshake buffer is no longer necessary.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001472 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001473
David Benjamin4cbb9312017-08-31 14:52:31 -04001474 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001475 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001476}
1477
David Benjamin4cbb9312017-08-31 14:52:31 -04001478static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001479 SSL *const ssl = hs->ssl;
David Benjamin4cbb9312017-08-31 14:52:31 -04001480 // Resolve Channel ID first, before any non-idempotent operations.
1481 if (ssl->s3->tlsext_channel_id_valid) {
1482 if (!ssl_do_channel_id_callback(ssl)) {
1483 return ssl_hs_error;
1484 }
1485
1486 if (ssl->tlsext_channel_id_private == NULL) {
1487 hs->state = state_send_client_finished;
1488 return ssl_hs_channel_id_lookup;
1489 }
1490 }
David Benjamine97b9622015-10-10 14:59:09 -04001491
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001492 if (!ssl->method->add_change_cipher_spec(ssl) ||
1493 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
1494 return ssl_hs_error;
David Benjamine97b9622015-10-10 14:59:09 -04001495 }
1496
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001497 if (hs->next_proto_neg_seen) {
1498 static const uint8_t kZero[32] = {0};
1499 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
1500
1501 ScopedCBB cbb;
1502 CBB body, child;
1503 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1504 !CBB_add_u8_length_prefixed(&body, &child) ||
1505 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1506 ssl->s3->next_proto_negotiated_len) ||
1507 !CBB_add_u8_length_prefixed(&body, &child) ||
1508 !CBB_add_bytes(&child, kZero, padding_len) ||
1509 !ssl_add_message_cbb(ssl, cbb.get())) {
1510 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1511 return ssl_hs_error;
1512 }
1513 }
1514
David Benjamin4cbb9312017-08-31 14:52:31 -04001515 if (ssl->s3->tlsext_channel_id_valid) {
1516 ScopedCBB cbb;
1517 CBB body;
1518 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1519 !tls1_write_channel_id(hs, &body) ||
1520 !ssl_add_message_cbb(ssl, cbb.get())) {
1521 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1522 return ssl_hs_error;
1523 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001524 }
1525
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001526 if (!ssl3_send_finished(hs)) {
1527 return ssl_hs_error;
1528 }
1529
1530 hs->state = state_finish_flight;
1531 return ssl_hs_flush;
1532}
1533
1534static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001535 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001536 if (ssl->session != NULL) {
1537 hs->state = state_finish_client_handshake;
1538 return ssl_hs_ok;
1539 }
1540
David Benjaminc11ea9422017-08-29 16:33:21 -04001541 // This is a full handshake. If it involves ChannelID, then record the
1542 // handshake hashes at this point in the session so that any resumption of
1543 // this session with ChannelID can sign those hashes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001544 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1545 return ssl_hs_error;
1546 }
1547
1548 hs->state = state_read_session_ticket;
1549
1550 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1551 ssl3_can_false_start(ssl) &&
David Benjaminc11ea9422017-08-29 16:33:21 -04001552 // No False Start on renegotiation (would complicate the state machine).
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001553 !ssl->s3->initial_handshake_complete) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001554 hs->in_false_start = true;
1555 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001556 return ssl_hs_early_return;
1557 }
1558
1559 return ssl_hs_ok;
1560}
1561
1562static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1563 SSL *const ssl = hs->ssl;
1564
1565 if (!hs->ticket_expected) {
1566 hs->state = state_process_change_cipher_spec;
1567 return ssl_hs_read_change_cipher_spec;
1568 }
1569
David Benjamin7934f082017-08-01 16:32:25 -04001570 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001571 if (!ssl->method->get_message(ssl, &msg)) {
1572 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001573 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001574
David Benjamin7934f082017-08-01 16:32:25 -04001575 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1576 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001577 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001578 }
1579
David Benjamin7934f082017-08-01 16:32:25 -04001580 CBS new_session_ticket = msg.body, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001581 uint32_t tlsext_tick_lifetime_hint;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001582 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001583 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1584 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001585 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001586 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001587 return ssl_hs_error;
David Benjamin454aa4c2016-03-10 08:43:27 -05001588 }
1589
David Benjamin9f1dc822016-06-07 17:03:46 -04001590 if (CBS_len(&ticket) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001591 // RFC 5077 allows a server to change its mind and send no ticket after
1592 // negotiating the extension. The value of |ticket_expected| is checked in
1593 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
David Benjaminfd45ee72017-08-31 14:49:09 -04001594 hs->ticket_expected = false;
David Benjamin8f94c312017-08-01 17:35:55 -04001595 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001596 hs->state = state_process_change_cipher_spec;
1597 return ssl_hs_read_change_cipher_spec;
David Benjamin9f1dc822016-06-07 17:03:46 -04001598 }
1599
David Benjamin31b0c9b2017-07-20 14:49:15 -04001600 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001601 UniquePtr<SSL_SESSION> renewed_session;
1602 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001603 // The server is sending a new ticket for an existing session. Sessions are
1604 // immutable once established, so duplicate all but the ticket of the
1605 // existing session.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001606 renewed_session =
1607 SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001608 if (!renewed_session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001609 // This should never happen.
David Benjamin9f1dc822016-06-07 17:03:46 -04001610 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001611 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001612 }
David Benjamin1386aad2017-07-19 23:57:40 -04001613 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001614 }
1615
David Benjaminc11ea9422017-08-29 16:33:21 -04001616 // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued.
David Benjamin17b30832017-01-28 14:00:32 -05001617 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001618
Steven Valdez87eab492016-06-27 16:34:59 -04001619 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001620 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001621 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001622 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001623 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001624
David Benjaminc11ea9422017-08-29 16:33:21 -04001625 // Generate a session ID for this session based on the session ticket. We use
1626 // the session ID mechanism for detecting ticket resumption. This also fits in
1627 // with assumptions elsewhere in OpenSSL.
Steven Valdez87eab492016-06-27 16:34:59 -04001628 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1629 session->session_id, &session->session_id_length,
1630 EVP_sha256(), NULL)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001631 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001632 }
1633
David Benjamin1386aad2017-07-19 23:57:40 -04001634 if (renewed_session) {
Steven Valdez87eab492016-06-27 16:34:59 -04001635 session->not_resumable = 0;
1636 SSL_SESSION_free(ssl->session);
David Benjamin1386aad2017-07-19 23:57:40 -04001637 ssl->session = renewed_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -04001638 }
1639
David Benjamin8f94c312017-08-01 17:35:55 -04001640 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001641 hs->state = state_process_change_cipher_spec;
1642 return ssl_hs_read_change_cipher_spec;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001643}
David Benjamin86e95b82017-07-18 16:34:25 -04001644
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001645static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1646 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
1647 return ssl_hs_error;
1648 }
1649
1650 hs->state = state_read_server_finished;
1651 return ssl_hs_ok;
1652}
1653
1654static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1655 SSL *const ssl = hs->ssl;
1656 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1657 if (wait != ssl_hs_ok) {
1658 return wait;
1659 }
1660
1661 if (ssl->session != NULL) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001662 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001663 return ssl_hs_ok;
1664 }
1665
1666 hs->state = state_finish_client_handshake;
1667 return ssl_hs_ok;
1668}
1669
1670static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1671 SSL *const ssl = hs->ssl;
1672
1673 ssl->method->on_handshake_complete(ssl);
1674
1675 SSL_SESSION_free(ssl->s3->established_session);
1676 if (ssl->session != NULL) {
1677 SSL_SESSION_up_ref(ssl->session);
1678 ssl->s3->established_session = ssl->session;
1679 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001680 // We make a copy of the session in order to maintain the immutability
1681 // of the new established_session due to False Start. The caller may
1682 // have taken a reference to the temporary session.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001683 ssl->s3->established_session =
1684 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL)
1685 .release();
1686 if (ssl->s3->established_session == NULL) {
1687 return ssl_hs_error;
1688 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001689 // Renegotiations do not participate in session resumption.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001690 if (!ssl->s3->initial_handshake_complete) {
1691 ssl->s3->established_session->not_resumable = 0;
1692 }
1693
1694 hs->new_session.reset();
1695 }
1696
David Benjaminfd45ee72017-08-31 14:49:09 -04001697 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001698 ssl->s3->initial_handshake_complete = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001699 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
1700
1701 hs->state = state_done;
1702 return ssl_hs_ok;
1703}
1704
1705enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1706 while (hs->state != state_done) {
1707 enum ssl_hs_wait_t ret = ssl_hs_error;
1708 enum ssl_client_hs_state_t state =
1709 static_cast<enum ssl_client_hs_state_t>(hs->state);
1710 switch (state) {
1711 case state_start_connect:
1712 ret = do_start_connect(hs);
1713 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001714 case state_enter_early_data:
1715 ret = do_enter_early_data(hs);
1716 break;
1717 case state_read_hello_verify_request:
1718 ret = do_read_hello_verify_request(hs);
1719 break;
1720 case state_read_server_hello:
1721 ret = do_read_server_hello(hs);
1722 break;
1723 case state_tls13:
1724 ret = do_tls13(hs);
1725 break;
1726 case state_read_server_certificate:
1727 ret = do_read_server_certificate(hs);
1728 break;
1729 case state_read_certificate_status:
1730 ret = do_read_certificate_status(hs);
1731 break;
1732 case state_verify_server_certificate:
1733 ret = do_verify_server_certificate(hs);
1734 break;
1735 case state_read_server_key_exchange:
1736 ret = do_read_server_key_exchange(hs);
1737 break;
1738 case state_read_certificate_request:
1739 ret = do_read_certificate_request(hs);
1740 break;
1741 case state_read_server_hello_done:
1742 ret = do_read_server_hello_done(hs);
1743 break;
1744 case state_send_client_certificate:
1745 ret = do_send_client_certificate(hs);
1746 break;
1747 case state_send_client_key_exchange:
1748 ret = do_send_client_key_exchange(hs);
1749 break;
1750 case state_send_client_certificate_verify:
1751 ret = do_send_client_certificate_verify(hs);
1752 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001753 case state_send_client_finished:
1754 ret = do_send_client_finished(hs);
1755 break;
1756 case state_finish_flight:
1757 ret = do_finish_flight(hs);
1758 break;
1759 case state_read_session_ticket:
1760 ret = do_read_session_ticket(hs);
1761 break;
1762 case state_process_change_cipher_spec:
1763 ret = do_process_change_cipher_spec(hs);
1764 break;
1765 case state_read_server_finished:
1766 ret = do_read_server_finished(hs);
1767 break;
1768 case state_finish_client_handshake:
1769 ret = do_finish_client_handshake(hs);
1770 break;
1771 case state_done:
1772 ret = ssl_hs_ok;
1773 break;
1774 }
1775
1776 if (hs->state != state) {
1777 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1778 }
1779
1780 if (ret != ssl_hs_ok) {
1781 return ret;
1782 }
1783 }
1784
1785 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1786 return ssl_hs_ok;
1787}
1788
1789const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1790 enum ssl_client_hs_state_t state =
1791 static_cast<enum ssl_client_hs_state_t>(hs->state);
1792 switch (state) {
1793 case state_start_connect:
1794 return "TLS client start_connect";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001795 case state_enter_early_data:
1796 return "TLS client enter_early_data";
1797 case state_read_hello_verify_request:
1798 return "TLS client read_hello_verify_request";
1799 case state_read_server_hello:
1800 return "TLS client read_server_hello";
1801 case state_tls13:
1802 return tls13_client_handshake_state(hs);
1803 case state_read_server_certificate:
1804 return "TLS client read_server_certificate";
1805 case state_read_certificate_status:
1806 return "TLS client read_certificate_status";
1807 case state_verify_server_certificate:
1808 return "TLS client verify_server_certificate";
1809 case state_read_server_key_exchange:
1810 return "TLS client read_server_key_exchange";
1811 case state_read_certificate_request:
1812 return "TLS client read_certificate_request";
1813 case state_read_server_hello_done:
1814 return "TLS client read_server_hello_done";
1815 case state_send_client_certificate:
1816 return "TLS client send_client_certificate";
1817 case state_send_client_key_exchange:
1818 return "TLS client send_client_key_exchange";
1819 case state_send_client_certificate_verify:
1820 return "TLS client send_client_certificate_verify";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001821 case state_send_client_finished:
1822 return "TLS client send_client_finished";
1823 case state_finish_flight:
1824 return "TLS client finish_flight";
1825 case state_read_session_ticket:
1826 return "TLS client read_session_ticket";
1827 case state_process_change_cipher_spec:
1828 return "TLS client process_change_cipher_spec";
1829 case state_read_server_finished:
1830 return "TLS client read_server_finished";
1831 case state_finish_client_handshake:
1832 return "TLS client finish_client_handshake";
1833 case state_done:
1834 return "TLS client done";
1835 }
1836
1837 return "TLS client unknown";
1838}
1839
1840}