blob: 18dd58f81e494ca97a13d97623b37b43ceafe6b0 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Steven Valdez4d71a9a2017-08-14 15:08:34 -040039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
Steven Valdez4d71a9a2017-08-14 15:08:34 -040065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400113 * Portions of the attached software ("Contribution") are developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE.
148 */
149
David Benjamin9e4e01e2015-09-15 01:48:04 -0400150#include <openssl/ssl.h>
151
David Benjamin81ea0bf2014-11-23 04:20:17 -0500152#include <assert.h>
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400153#include <limits.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400154#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamin31b0c9b2017-07-20 14:49:15 -0400156#include <utility>
157
David Benjaminabbbee12016-10-31 19:20:42 -0400158#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400159#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400161#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400162#include <openssl/ec_key.h>
163#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400164#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400167#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400168#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin17cf2cb2016-12-13 01:07:13 -0500170#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400171#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin86e95b82017-07-18 16:34:25 -0400174namespace bssl {
175
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400176enum ssl_client_hs_state_t {
177 state_start_connect = 0,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400178 state_enter_early_data,
179 state_read_hello_verify_request,
180 state_read_server_hello,
181 state_tls13,
182 state_read_server_certificate,
183 state_read_certificate_status,
184 state_verify_server_certificate,
185 state_read_server_key_exchange,
186 state_read_certificate_request,
187 state_read_server_hello_done,
188 state_send_client_certificate,
189 state_send_client_key_exchange,
190 state_send_client_certificate_verify,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400191 state_send_client_finished,
192 state_finish_flight,
193 state_read_session_ticket,
194 state_process_change_cipher_spec,
195 state_read_server_finished,
196 state_finish_client_handshake,
197 state_done,
198};
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjaminc11ea9422017-08-29 16:33:21 -0400200// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
201// disabled algorithms.
David Benjamin2fe6e222017-01-25 02:18:43 -0500202static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
203 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500204 *out_mask_a = 0;
205 *out_mask_k = 0;
206
David Benjaminc11ea9422017-08-29 16:33:21 -0400207 // PSK requires a client callback.
David Benjamin2fe6e222017-01-25 02:18:43 -0500208 if (ssl->psk_client_callback == NULL) {
209 *out_mask_a |= SSL_aPSK;
210 *out_mask_k |= SSL_kPSK;
211 }
212}
213
David Benjamin68161cb2017-06-20 14:49:43 -0400214static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
215 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500216 uint32_t mask_a, mask_k;
217 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamine8d53502015-10-10 14:13:23 -0400219 CBB child;
220 if (!CBB_add_u16_length_prefixed(out, &child)) {
221 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800222 }
223
David Benjaminc11ea9422017-08-29 16:33:21 -0400224 // Add a fake cipher suite. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -0400225 if (ssl->ctx->grease_enabled &&
226 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
227 return 0;
228 }
229
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
231 // hardware support.
David Benjamin68161cb2017-06-20 14:49:43 -0400232 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400233 if (!EVP_has_aes_hardware() &&
234 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
235 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500236 }
David Benjaminabbbee12016-10-31 19:20:42 -0400237 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
238 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
239 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400240 }
David Benjaminabbbee12016-10-31 19:20:42 -0400241 if (EVP_has_aes_hardware() &&
242 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400243 return 0;
244 }
245 }
246
David Benjamin68161cb2017-06-20 14:49:43 -0400247 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400248 int any_enabled = 0;
David Benjaminee910bf2017-07-25 22:36:00 -0400249 for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400250 // Skip disabled ciphers
David Benjamin2fe6e222017-01-25 02:18:43 -0500251 if ((cipher->algorithm_mkey & mask_k) ||
252 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400253 continue;
254 }
David Benjamin68161cb2017-06-20 14:49:43 -0400255 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
256 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400257 continue;
258 }
259 any_enabled = 1;
260 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
261 return 0;
262 }
263 }
264
David Benjaminc11ea9422017-08-29 16:33:21 -0400265 // If all ciphers were disabled, return the error to the caller.
David Benjamin68161cb2017-06-20 14:49:43 -0400266 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400267 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
268 return 0;
269 }
David Benjamine8d53502015-10-10 14:13:23 -0400270 }
271
David Benjaminc11ea9422017-08-29 16:33:21 -0400272 // For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
273 // added.
David Benjamin68161cb2017-06-20 14:49:43 -0400274 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400275 !ssl->s3->initial_handshake_complete) {
276 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
277 return 0;
278 }
David Benjamine8d53502015-10-10 14:13:23 -0400279 }
280
David Benjamine0ff7672016-09-19 18:40:03 -0400281 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200282 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
283 return 0;
284 }
David Benjamine8d53502015-10-10 14:13:23 -0400285 }
286
287 return CBB_flush(out);
288}
289
David Benjamin8c880a22016-12-03 02:20:34 -0500290int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
291 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400292 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -0400293 CBB body;
294 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
295 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400296 }
297
David Benjaminc11ea9422017-08-29 16:33:21 -0400298 // Renegotiations do not participate in session resumption.
Steven Valdez520e1222017-06-13 12:45:25 -0400299 int has_session_id = ssl->session != NULL &&
300 !ssl->s3->initial_handshake_complete &&
301 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400302
303 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500304 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400305 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400306 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400307 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400308 }
309
Steven Valdez520e1222017-06-13 12:45:25 -0400310 if (has_session_id) {
311 if (!CBB_add_bytes(&child, ssl->session->session_id,
312 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400313 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400314 }
315 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400316 // In TLS 1.3 experimental encodings, send a fake placeholder session ID
317 // when we do not otherwise have one to send.
Steven Valdez520e1222017-06-13 12:45:25 -0400318 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezc7d4d212017-09-11 13:53:08 -0400319 ssl_is_resumption_variant(ssl->tls13_variant) &&
Steven Valdez520e1222017-06-13 12:45:25 -0400320 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400321 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400322 }
323 }
324
David Benjamince079fd2016-08-02 16:22:34 -0400325 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400326 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400327 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400328 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400329 }
330 }
331
332 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400333 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400334 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400335 !CBB_add_u8(&body, 1 /* one compression method */) ||
336 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500337 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400338 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400339 }
340
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);
David Benjamind0beda02017-09-06 16:46:50 -0400433 // |session_reused| must be reset in case this is a renegotiation.
434 ssl->s3->session_reused = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400435
David Benjaminc11ea9422017-08-29 16:33:21 -0400436 // Freeze the version range.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400437 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
438 return ssl_hs_error;
439 }
440
Steven Valdezc7d4d212017-09-11 13:53:08 -0400441 // SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer
442 // version.
443 if (hs->max_version == SSL3_VERSION) {
444 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION);
445 }
446
David Benjaminc11ea9422017-08-29 16:33:21 -0400447 // Always advertise the ClientHello version from the original maximum version,
448 // even on renegotiation. The static RSA key exchange uses this field, and
449 // some servers fail when it changes across handshakes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400450 if (SSL_is_dtls(hs->ssl)) {
451 hs->client_version =
452 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
453 } else {
454 hs->client_version =
455 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
456 }
457
David Benjaminc11ea9422017-08-29 16:33:21 -0400458 // If the configured session has expired or was created at a disabled
459 // version, drop it.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400460 if (ssl->session != NULL) {
461 if (ssl->session->is_server ||
462 !ssl_supports_version(hs, ssl->session->ssl_version) ||
463 (ssl->session->session_id_length == 0 &&
464 ssl->session->tlsext_ticklen == 0) ||
465 ssl->session->not_resumable ||
466 !ssl_session_is_time_valid(ssl, ssl->session)) {
467 ssl_set_session(ssl, NULL);
468 }
469 }
470
Steven Valdez398085b2017-08-29 13:25:56 -0400471 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400472 return ssl_hs_error;
473 }
474
David Benjaminc11ea9422017-08-29 16:33:21 -0400475 // Initialize a random session ID for the experimental TLS 1.3 variant
476 // requiring a session id.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400477 if (ssl_is_resumption_variant(ssl->tls13_variant)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400478 hs->session_id_len = sizeof(hs->session_id);
479 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
480 return ssl_hs_error;
481 }
482 }
483
484 if (!ssl_write_client_hello(hs)) {
485 return ssl_hs_error;
486 }
487
488 hs->state = state_enter_early_data;
489 return ssl_hs_flush;
490}
491
492static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
493 SSL *const ssl = hs->ssl;
494
Steven Valdez398085b2017-08-29 13:25:56 -0400495 if (SSL_is_dtls(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400496 hs->state = state_read_hello_verify_request;
497 return ssl_hs_ok;
498 }
499
500 if (!hs->early_data_offered) {
501 hs->state = state_read_server_hello;
502 return ssl_hs_ok;
503 }
504
505 if (!tls13_init_early_key_schedule(hs) ||
506 !tls13_advance_key_schedule(hs, ssl->session->master_key,
507 ssl->session->master_key_length) ||
508 !tls13_derive_early_secrets(hs) ||
509 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
510 hs->hash_len)) {
511 return ssl_hs_error;
512 }
513
David Benjaminc11ea9422017-08-29 16:33:21 -0400514 // Stash the early data session, so connection properties may be queried out
515 // of it.
David Benjaminfd45ee72017-08-31 14:49:09 -0400516 hs->in_early_data = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400517 SSL_SESSION_up_ref(ssl->session);
518 hs->early_session.reset(ssl->session);
David Benjaminfd45ee72017-08-31 14:49:09 -0400519 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400520
521 hs->state = state_read_server_hello;
522 return ssl_hs_early_return;
523}
524
525static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
526 SSL *const ssl = hs->ssl;
527
528 assert(SSL_is_dtls(ssl));
529
530 SSLMessage msg;
531 if (!ssl->method->get_message(ssl, &msg)) {
532 return ssl_hs_read_message;
533 }
534
535 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400536 hs->state = state_read_server_hello;
537 return ssl_hs_ok;
538 }
539
540 CBS hello_verify_request = msg.body, cookie;
541 uint16_t server_version;
542 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
543 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
544 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
545 CBS_len(&hello_verify_request) != 0) {
546 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
547 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
548 return ssl_hs_error;
549 }
550
551 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
552 ssl->d1->cookie_len = CBS_len(&cookie);
553
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400554 ssl->method->next_message(ssl);
Steven Valdez398085b2017-08-29 13:25:56 -0400555
David Benjaminc11ea9422017-08-29 16:33:21 -0400556 // DTLS resets the handshake buffer after HelloVerifyRequest.
Steven Valdez398085b2017-08-29 13:25:56 -0400557 if (!hs->transcript.Init()) {
558 return ssl_hs_error;
559 }
560
561 if (!ssl_write_client_hello(hs)) {
562 return ssl_hs_error;
563 }
564
565 hs->state = state_read_server_hello;
566 return ssl_hs_flush;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400567}
568
569static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900570 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400571 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400572 if (!ssl->method->get_message(ssl, &msg)) {
573 return ssl_hs_read_server_hello;
Adam Langley1bea1732014-12-17 19:06:57 -0800574 }
Adam Langley95c29f32014-06-20 12:00:00 -0700575
David Benjaminb853f312017-07-14 18:40:34 -0400576 uint16_t server_version;
David Benjamin7934f082017-08-01 16:32:25 -0400577 if (!parse_server_version(hs, &server_version, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400578 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400579 }
580
Steven Valdez8f36c512017-06-20 10:55:02 -0400581 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400582 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400583 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400584 return ssl_hs_error;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400585 }
586
David Benjamin0d56f882015-12-19 17:05:56 -0500587 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
588 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400589 ssl->version = server_version;
David Benjaminc11ea9422017-08-29 16:33:21 -0400590 // At this point, the connection's version is known and ssl->version is
591 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400592 ssl->s3->have_version = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400593 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400594 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400595 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400596 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400597 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800598 }
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Steven Valdez143e8b32016-07-11 13:19:03 -0400600 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400601 hs->state = state_tls13;
602 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400603 }
604
Steven Valdez2d850622017-01-11 11:34:52 -0500605 if (hs->early_data_offered) {
606 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400607 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400608 return ssl_hs_error;
Steven Valdez2d850622017-01-11 11:34:52 -0500609 }
610
David Benjamin6e4fc332016-11-17 16:43:08 +0900611 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400612
David Benjamin7934f082017-08-01 16:32:25 -0400613 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400614 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400615 }
616
David Benjamin7934f082017-08-01 16:32:25 -0400617 CBS server_hello = msg.body, server_random, session_id;
David Benjaminb853f312017-07-14 18:40:34 -0400618 uint16_t cipher_suite;
619 uint8_t compression_method;
David Benjaminb853f312017-07-14 18:40:34 -0400620 if (!CBS_skip(&server_hello, 2 /* version */) ||
621 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400622 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
623 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
624 !CBS_get_u16(&server_hello, &cipher_suite) ||
625 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400626 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400627 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400628 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400629 }
630
David Benjaminc11ea9422017-08-29 16:33:21 -0400631 // Copy over the server random.
David Benjamind3bca042017-04-08 11:05:04 -0400632 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
633 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700634
David Benjaminc11ea9422017-08-29 16:33:21 -0400635 // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
636 // 1.3 is finalized and we are not implementing a draft version.
David Benjamin1f61f0d2016-07-10 12:20:35 -0400637
David Benjamin0d56f882015-12-19 17:05:56 -0500638 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400639 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500640 CBS_mem_equal(&session_id, ssl->session->session_id,
641 ssl->session->session_id_length)) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400642 ssl->s3->session_reused = true;
Adam Langley1bea1732014-12-17 19:06:57 -0800643 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400644 // The session wasn't resumed. Create a fresh SSL_SESSION to
645 // fill out.
David Benjamin4d0be242016-09-01 01:10:07 -0400646 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900647 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400648 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400649 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800650 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400651 // Note: session_id could be empty.
David Benjamin45738dd2017-02-09 20:01:26 -0500652 hs->new_session->session_id_length = CBS_len(&session_id);
653 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
654 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800655 }
David Benjamina03d95d2014-07-12 19:49:07 -0400656
David Benjaminca9e8f52017-08-09 15:02:34 -0400657 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
658 if (cipher == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400659 // unknown cipher
David Benjamin3570d732015-06-29 00:28:17 -0400660 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400661 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400662 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800663 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400664
David Benjaminc11ea9422017-08-29 16:33:21 -0400665 // The cipher must be allowed in the selected version and enabled.
David Benjamin2fe6e222017-01-25 02:18:43 -0500666 uint32_t mask_a, mask_k;
667 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
David Benjaminca9e8f52017-08-09 15:02:34 -0400668 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
669 SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
670 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) ||
671 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400672 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400673 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400674 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800675 }
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Steven Valdez87eab492016-06-27 16:34:59 -0400677 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500678 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400679 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400680 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400681 return ssl_hs_error;
David Benjaminece3de92015-03-16 18:02:20 -0400682 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400683 if (ssl->session->cipher != cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400684 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400685 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400686 return ssl_hs_error;
Steven Valdez803c77a2016-09-06 14:13:43 -0400687 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400688 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400689 // This is actually a client application bug.
Steven Valdez4aa154e2016-07-29 14:32:55 -0400690 OPENSSL_PUT_ERROR(SSL,
691 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400692 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400693 return ssl_hs_error;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400694 }
David Benjamin0623bce2015-12-25 15:40:14 -0500695 } else {
David Benjaminca9e8f52017-08-09 15:02:34 -0400696 hs->new_session->cipher = cipher;
Adam Langley1bea1732014-12-17 19:06:57 -0800697 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400698 hs->new_cipher = cipher;
David Benjaminbdf5e722014-11-11 00:52:15 -0500699
David Benjaminc11ea9422017-08-29 16:33:21 -0400700 // Now that the cipher is known, initialize the handshake hash and hash the
701 // ServerHello.
David Benjaminca9e8f52017-08-09 15:02:34 -0400702 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400703 !ssl_hash_message(hs, msg)) {
David Benjamind3bca042017-04-08 11:05:04 -0400704 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400705 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400706 }
707
David Benjaminc11ea9422017-08-29 16:33:21 -0400708 // If doing a full handshake, the server may request a client certificate
709 // which requires hashing the handshake transcript. Otherwise, the handshake
710 // buffer may be released.
David Benjamin3d458dc2016-09-12 22:40:27 +0000711 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500712 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400713 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -0800714 }
David Benjamina03d95d2014-07-12 19:49:07 -0400715
David Benjaminc11ea9422017-08-29 16:33:21 -0400716 // Only the NULL compression algorithm is supported.
Adam Langley1bea1732014-12-17 19:06:57 -0800717 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400718 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -0400719 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400720 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800721 }
Adam Langley95c29f32014-06-20 12:00:00 -0700722
David Benjaminc11ea9422017-08-29 16:33:21 -0400723 // TLS extensions
David Benjamin8c880a22016-12-03 02:20:34 -0500724 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400725 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400726 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800727 }
Adam Langley95c29f32014-06-20 12:00:00 -0700728
David Benjaminc11ea9422017-08-29 16:33:21 -0400729 // There should be nothing left over in the record.
Adam Langley1bea1732014-12-17 19:06:57 -0800730 if (CBS_len(&server_hello) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400731 // wrong packet length
David Benjamin639846e2016-09-09 11:41:18 -0400732 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400733 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400734 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800735 }
Adam Langley95c29f32014-06-20 12:00:00 -0700736
Steven Valdez87eab492016-06-27 16:34:59 -0400737 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -0500738 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -0500739 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400740 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700741 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400742 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700743 }
David Benjamind3bca042017-04-08 11:05:04 -0400744 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400745 return ssl_hs_error;
Adam Langleyba5934b2015-06-02 10:50:35 -0700746 }
747
David Benjamin8f94c312017-08-01 17:35:55 -0400748 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400749
750 if (ssl->session != NULL) {
751 hs->state = state_read_session_ticket;
752 return ssl_hs_ok;
753 }
754
755 hs->state = state_read_server_certificate;
756 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800757}
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400759static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
760 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
761 if (wait == ssl_hs_ok) {
762 hs->state = state_finish_client_handshake;
763 return ssl_hs_ok;
764 }
765
766 return wait;
767}
768
769static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900770 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400771
772 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
773 hs->state = state_read_certificate_status;
774 return ssl_hs_ok;
775 }
776
David Benjamin7934f082017-08-01 16:32:25 -0400777 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400778 if (!ssl->method->get_message(ssl, &msg)) {
779 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800780 }
Adam Langley95c29f32014-06-20 12:00:00 -0700781
David Benjamin7934f082017-08-01 16:32:25 -0400782 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
783 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400784 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -0500785 }
786
David Benjamin7934f082017-08-01 16:32:25 -0400787 CBS body = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800788 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400789 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
David Benjamin7934f082017-08-01 16:32:25 -0400790 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
David Benjamine664a532017-07-20 20:19:36 -0400791 ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400792 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400793 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800794 }
David Benjamine664a532017-07-20 20:19:36 -0400795 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
796 hs->new_session->certs = chain.release();
Adam Langley95c29f32014-06-20 12:00:00 -0700797
David Benjamin45738dd2017-02-09 20:01:26 -0500798 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -0400799 CBS_len(&body) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400800 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -0400801 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -0400802 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400803 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800804 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400805
Adam Langley05672202016-12-13 12:05:49 -0800806 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -0400807 hs, hs->peer_pubkey.get(),
David Benjamin45738dd2017-02-09 20:01:26 -0500808 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -0400809 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400810 return ssl_hs_error;
David Benjamin436bf822015-06-07 19:50:37 -0400811 }
812
David Benjamin8f94c312017-08-01 17:35:55 -0400813 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400814
815 hs->state = state_read_certificate_status;
816 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800817}
818
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400819static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900820 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400821
822 if (!hs->certificate_status_expected) {
823 hs->state = state_verify_server_certificate;
824 return ssl_hs_ok;
825 }
826
David Benjamin7934f082017-08-01 16:32:25 -0400827 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400828 if (!ssl->method->get_message(ssl, &msg)) {
829 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400830 }
831
David Benjamin7934f082017-08-01 16:32:25 -0400832 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400833 // A server may send status_request in ServerHello and then change its mind
834 // about sending CertificateStatus.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400835 hs->state = state_verify_server_certificate;
836 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400837 }
838
David Benjamin7934f082017-08-01 16:32:25 -0400839 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400840 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500841 }
842
David Benjamin7934f082017-08-01 16:32:25 -0400843 CBS certificate_status = msg.body, ocsp_response;
David Benjamin8d606e32017-06-15 22:43:04 -0400844 uint8_t status_type;
David Benjamin9f1dc822016-06-07 17:03:46 -0400845 if (!CBS_get_u8(&certificate_status, &status_type) ||
846 status_type != TLSEXT_STATUSTYPE_ocsp ||
847 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
848 CBS_len(&ocsp_response) == 0 ||
849 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400850 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400851 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400852 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400853 }
854
David Benjamin8fc2dc02017-08-22 15:07:51 -0700855 CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
856 hs->new_session->ocsp_response =
857 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
858 if (hs->new_session->ocsp_response == nullptr) {
David Benjamin8d606e32017-06-15 22:43:04 -0400859 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400860 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400861 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400862
David Benjamin8f94c312017-08-01 17:35:55 -0400863 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400864
865 hs->state = state_verify_server_certificate;
866 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400867}
868
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400869static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
870 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
871 hs->state = state_read_server_key_exchange;
872 return ssl_hs_ok;
873 }
874
875 switch (ssl_verify_peer_cert(hs)) {
876 case ssl_verify_ok:
877 break;
878 case ssl_verify_invalid:
879 return ssl_hs_error;
880 case ssl_verify_retry:
881 hs->state = state_verify_server_certificate;
882 return ssl_hs_certificate_verify;
883 }
884
885 hs->state = state_read_server_key_exchange;
886 return ssl_hs_ok;
887}
888
889static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900890 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400891 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400892 if (!ssl->method->get_message(ssl, &msg)) {
893 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800894 }
895
David Benjamin7934f082017-08-01 16:32:25 -0400896 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400897 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
David Benjamin45738dd2017-02-09 20:01:26 -0500898 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400899 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -0500900 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400901 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800902 }
903
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400904 hs->state = state_read_certificate_request;
905 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800906 }
907
David Benjamin7934f082017-08-01 16:32:25 -0400908 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400909 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500910 }
911
David Benjamin45738dd2017-02-09 20:01:26 -0500912 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
913 uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin7934f082017-08-01 16:32:25 -0400914 CBS server_key_exchange = msg.body;
Adam Langley1bea1732014-12-17 19:06:57 -0800915 if (alg_a & SSL_aPSK) {
916 CBS psk_identity_hint;
917
David Benjaminc11ea9422017-08-29 16:33:21 -0400918 // Each of the PSK key exchanges begins with a psk_identity_hint.
Adam Langley1bea1732014-12-17 19:06:57 -0800919 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
920 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400921 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400922 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400923 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800924 }
925
David Benjaminc11ea9422017-08-29 16:33:21 -0400926 // Store PSK identity hint for later use, hint is used in
927 // ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
928 // identity hint can be as long as the maximum length of a PSK identity.
929 // Also do not allow NULL characters; identities are saved as C strings.
930 //
931 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
932 // a specific identity.
Adam Langley1bea1732014-12-17 19:06:57 -0800933 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
934 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400935 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -0400936 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400937 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800938 }
939
David Benjaminc11ea9422017-08-29 16:33:21 -0400940 // Save non-empty identity hints as a C string. Empty identity hints we
941 // treat as missing. Plain PSK makes it possible to send either no hint
942 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
943 // empty hint. Having different capabilities is odd, so we interpret empty
944 // and missing as identical.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400945 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -0400946 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -0400947 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -0400948 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -0400949 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400950 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800951 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400952 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -0800953 }
954
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700955 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400956 // Parse the server parameters.
Steven Valdezce902a92016-05-17 11:47:53 -0400957 uint8_t group_type;
958 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -0500959 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -0400960 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
961 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -0700962 !CBS_get_u16(&server_key_exchange, &group_id) ||
963 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -0400964 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400965 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400966 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800967 }
David Benjamin45738dd2017-02-09 20:01:26 -0500968 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -0800969
David Benjaminc11ea9422017-08-29 16:33:21 -0400970 // Ensure the group is consistent with preferences.
Steven Valdezce902a92016-05-17 11:47:53 -0400971 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -0500972 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -0400973 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400974 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800975 }
David Benjamin4298d772015-12-19 00:18:25 -0500976
David Benjaminc11ea9422017-08-29 16:33:21 -0400977 // Initialize ECDH and save the peer public key for later.
David Benjaminc642aca2017-07-19 23:28:43 -0400978 hs->key_share = SSLKeyShare::Create(group_id);
979 if (!hs->key_share ||
David Benjaminc3c88822016-11-14 10:32:04 +0900980 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400981 return ssl_hs_error;
David Benjaminc1cc8582015-12-17 01:22:15 -0500982 }
Adam Langley1bea1732014-12-17 19:06:57 -0800983 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -0400984 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -0400985 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400986 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800987 }
988
David Benjaminc11ea9422017-08-29 16:33:21 -0400989 // At this point, |server_key_exchange| contains the signature, if any, while
990 // |msg.body| contains the entire message. From that, derive a CBS containing
991 // just the parameter.
David Benjaminb35d6842015-12-29 17:08:08 -0500992 CBS parameter;
David Benjamin7934f082017-08-01 16:32:25 -0400993 CBS_init(&parameter, CBS_data(&msg.body),
994 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
Adam Langley1bea1732014-12-17 19:06:57 -0800995
David Benjaminc11ea9422017-08-29 16:33:21 -0400996 // ServerKeyExchange should be signed by the server's public key.
David Benjamin45738dd2017-02-09 20:01:26 -0500997 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400998 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -0500999 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001000 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001001 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001002 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001003 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001004 }
David Benjamin8d606e32017-06-15 22:43:04 -04001005 uint8_t alert = SSL_AD_DECODE_ERROR;
1006 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1007 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001008 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001009 }
David Benjamin45738dd2017-02-09 20:01:26 -05001010 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001011 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001012 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001013 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001014 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001015 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001016 }
1017
David Benjaminc11ea9422017-08-29 16:33:21 -04001018 // The last field in |server_key_exchange| is the signature.
David Benjaminf4958e72015-06-07 11:00:53 -04001019 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001020 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1021 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001022 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001023 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001024 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001025 }
1026
David Benjamin1386aad2017-07-19 23:57:40 -04001027 ScopedCBB transcript;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001028 uint8_t *transcript_data;
1029 size_t transcript_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001030 if (!CBB_init(transcript.get(),
1031 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1032 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1033 SSL3_RANDOM_SIZE) ||
1034 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1035 SSL3_RANDOM_SIZE) ||
1036 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1037 CBS_len(&parameter)) ||
1038 !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001039 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001040 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001041 return ssl_hs_error;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001042 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001043
1044 int sig_ok = ssl_public_key_verify(
1045 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001046 hs->peer_pubkey.get(), transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001047 OPENSSL_free(transcript_data);
1048
David Benjaminbf82aed2016-03-01 22:57:40 -05001049#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1050 sig_ok = 1;
1051 ERR_clear_error();
1052#endif
1053 if (!sig_ok) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001054 // bad signature
David Benjamin3570d732015-06-29 00:28:17 -04001055 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001056 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001057 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001058 }
1059 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001060 // PSK ciphers are the only supported certificate-less ciphers.
David Benjaminf4958e72015-06-07 11:00:53 -04001061 assert(alg_a == SSL_aPSK);
1062
Adam Langley1bea1732014-12-17 19:06:57 -08001063 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001064 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001065 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001066 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001067 }
1068 }
David Benjamin8f94c312017-08-01 17:35:55 -04001069
1070 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001071 hs->state = state_read_certificate_request;
1072 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001073}
Adam Langley95c29f32014-06-20 12:00:00 -07001074
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001075static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001076 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001077
1078 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1079 hs->state = state_read_server_hello_done;
1080 return ssl_hs_ok;
1081 }
1082
David Benjamin7934f082017-08-01 16:32:25 -04001083 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001084 if (!ssl->method->get_message(ssl, &msg)) {
1085 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001086 }
David Benjamin688d8df2014-11-02 23:06:42 -05001087
David Benjamin7934f082017-08-01 16:32:25 -04001088 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001089 // If we get here we don't need the handshake buffer as we won't be doing
1090 // client auth.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001091 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001092 hs->state = state_read_server_hello_done;
1093 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001094 }
David Benjamined439582014-07-14 19:13:02 -04001095
David Benjamin7934f082017-08-01 16:32:25 -04001096 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1097 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001098 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
David Benjaminc11ea9422017-08-29 16:33:21 -04001101 // Get the certificate types.
David Benjamin7934f082017-08-01 16:32:25 -04001102 CBS body = msg.body, certificate_types;
1103 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001104 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001105 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001106 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001107 }
David Benjamined439582014-07-14 19:13:02 -04001108
David Benjaminc3c88822016-11-14 10:32:04 +09001109 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1110 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001111 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001112 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001113 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001114
David Benjamina1e9cab2015-12-30 00:08:49 -05001115 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001116 CBS supported_signature_algorithms;
David Benjamin7934f082017-08-01 16:32:25 -04001117 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001118 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001119 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001120 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001121 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001122 }
Adam Langley1bea1732014-12-17 19:06:57 -08001123 }
David Benjamined439582014-07-14 19:13:02 -04001124
Adam Langleyc68e5b92017-02-08 13:33:15 -08001125 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001126 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -04001127 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001128 if (!ca_names) {
David Benjamine0332e82016-07-13 22:40:36 -04001129 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001130 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
David Benjamin7934f082017-08-01 16:32:25 -04001133 if (CBS_len(&body) != 0) {
David Benjamin639846e2016-09-09 11:41:18 -04001134 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001135 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001136 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001137 }
1138
David Benjaminfd45ee72017-08-31 14:49:09 -04001139 hs->cert_request = true;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001140 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001141 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001142
David Benjamin8f94c312017-08-01 17:35:55 -04001143 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001144 hs->state = state_read_server_hello_done;
1145 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001146}
Adam Langley95c29f32014-06-20 12:00:00 -07001147
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001148static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001149 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001150 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001151 if (!ssl->method->get_message(ssl, &msg)) {
1152 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001153 }
David Benjamin46062682014-07-14 19:14:32 -04001154
David Benjamin7934f082017-08-01 16:32:25 -04001155 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1156 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001157 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001158 }
1159
David Benjaminc11ea9422017-08-29 16:33:21 -04001160 // ServerHelloDone is empty.
David Benjamin7934f082017-08-01 16:32:25 -04001161 if (CBS_len(&msg.body) != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001162 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001163 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001164 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001165 }
David Benjamin46062682014-07-14 19:14:32 -04001166
David Benjamin8f94c312017-08-01 17:35:55 -04001167 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001168 hs->state = state_send_client_certificate;
1169 return ssl_hs_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001170}
Adam Langley1258b6a2014-06-20 12:00:00 -07001171
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001172static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001173 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001174
David Benjaminc11ea9422017-08-29 16:33:21 -04001175 // The peer didn't request a certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001176 if (!hs->cert_request) {
1177 hs->state = state_send_client_key_exchange;
1178 return ssl_hs_ok;
1179 }
1180
David Benjaminc11ea9422017-08-29 16:33:21 -04001181 // Call cert_cb to update the certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001182 if (ssl->cert->cert_cb != NULL) {
1183 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1184 if (rv == 0) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001185 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001186 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1187 return ssl_hs_error;
1188 }
1189 if (rv < 0) {
1190 hs->state = state_send_client_certificate;
1191 return ssl_hs_x509_lookup;
David Benjamin9f1dc822016-06-07 17:03:46 -04001192 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001193 }
1194
David Benjamin5edfc8c2016-12-10 15:46:58 -05001195 if (!ssl_has_certificate(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001196 // Without a client certificate, the handshake buffer may be released.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001197 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001198
David Benjaminc11ea9422017-08-29 16:33:21 -04001199 // In SSL 3.0, the Certificate message is replaced with a warning alert.
David Benjamin5edfc8c2016-12-10 15:46:58 -05001200 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001201 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1202 SSL_AD_NO_CERTIFICATE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001203 return ssl_hs_error;
David Benjamindaf207a2017-01-03 18:37:41 -05001204 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001205 hs->state = state_send_client_key_exchange;
1206 return ssl_hs_ok;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001207 }
1208 }
1209
David Benjamina232a712017-03-30 15:51:53 -05001210 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001211 !ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001212 return ssl_hs_error;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001213 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001214
1215
1216 hs->state = state_send_client_key_exchange;
1217 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001218}
1219
David Benjamina3d76d02017-07-14 19:36:07 -04001220static_assert(sizeof(size_t) >= sizeof(unsigned),
1221 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001222
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001223static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001224 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001225 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001226 CBB body;
1227 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001228 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001229 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001230 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001231
David Benjamind781fc42017-07-12 16:25:57 -04001232 uint8_t *pms = NULL;
1233 size_t pms_len = 0;
David Benjamin45738dd2017-02-09 20:01:26 -05001234 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1235 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001236
David Benjaminc11ea9422017-08-29 16:33:21 -04001237 // If using a PSK key exchange, prepare the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001238 unsigned psk_len = 0;
1239 uint8_t psk[PSK_MAX_PSK_LEN];
1240 if (alg_a & SSL_aPSK) {
1241 if (ssl->psk_client_callback == NULL) {
1242 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1243 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001244 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001245
David Benjaminae0eaaa2015-12-17 02:01:25 -05001246 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001247 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001248 psk_len =
David Benjamin31b0c9b2017-07-20 14:49:15 -04001249 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
1250 identity, sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001251 if (psk_len == 0) {
1252 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1253 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1254 goto err;
1255 }
1256 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001257
David Benjamin45738dd2017-02-09 20:01:26 -05001258 OPENSSL_free(hs->new_session->psk_identity);
1259 hs->new_session->psk_identity = BUF_strdup(identity);
1260 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001261 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1262 goto err;
1263 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001264
David Benjaminc11ea9422017-08-29 16:33:21 -04001265 // Write out psk_identity.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001266 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001267 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001268 !CBB_add_bytes(&child, (const uint8_t *)identity,
1269 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001270 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001271 goto err;
1272 }
1273 }
David Benjamin758d1272015-11-20 17:47:25 -05001274
David Benjaminc11ea9422017-08-29 16:33:21 -04001275 // Depending on the key exchange method, compute |pms| and |pms_len|.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001276 if (alg_k & SSL_kRSA) {
1277 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001278 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001279 if (pms == NULL) {
1280 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1281 goto err;
1282 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001283
David Benjamin31b0c9b2017-07-20 14:49:15 -04001284 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001285 if (rsa == NULL) {
1286 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001287 goto err;
1288 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001289
David Benjaminf04c2e92016-12-06 13:35:25 -05001290 pms[0] = hs->client_version >> 8;
1291 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001292 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1293 goto err;
1294 }
1295
David Benjamin75836432016-06-17 18:48:29 -04001296 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001297 size_t enc_pms_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001298 // In TLS, there is a length prefix.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001299 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001300 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001301 goto err;
1302 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001303 enc_pms = &child;
1304 }
Adam Langley1bea1732014-12-17 19:06:57 -08001305
David Benjaminae0eaaa2015-12-17 02:01:25 -05001306 uint8_t *ptr;
1307 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1308 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1309 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001310 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001311 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001312 goto err;
1313 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001314 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001315 // Generate a keypair and serialize the public half.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001316 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001317 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001318 goto err;
1319 }
1320
David Benjaminc11ea9422017-08-29 16:33:21 -04001321 // Compute the premaster.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001322 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001323 if (!hs->key_share->Accept(&child, &pms, &pms_len, &alert, hs->peer_key,
1324 hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001325 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1326 goto err;
1327 }
David Benjamin75836432016-06-17 18:48:29 -04001328 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001329 goto err;
1330 }
David Benjamin4298d772015-12-19 00:18:25 -05001331
David Benjaminc11ea9422017-08-29 16:33:21 -04001332 // The key exchange state may now be discarded.
David Benjaminc642aca2017-07-19 23:28:43 -04001333 hs->key_share.reset();
David Benjaminc3c88822016-11-14 10:32:04 +09001334 OPENSSL_free(hs->peer_key);
1335 hs->peer_key = NULL;
1336 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001337 } else if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001338 // For plain PSK, other_secret is a block of 0s with the same length as
1339 // the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001340 pms_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001341 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001342 if (pms == NULL) {
1343 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1344 goto err;
1345 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001346 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001347 } else {
1348 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1349 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1350 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001351 }
1352
David Benjaminc11ea9422017-08-29 16:33:21 -04001353 // For a PSK cipher suite, other_secret is combined with the pre-shared
1354 // key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001355 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001356 ScopedCBB pms_cbb;
1357 CBB child;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001358 uint8_t *new_pms;
1359 size_t new_pms_len;
1360
David Benjamin1386aad2017-07-19 23:57:40 -04001361 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms_len) ||
1362 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001363 !CBB_add_bytes(&child, pms, pms_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001364 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001365 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001366 !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001367 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1368 goto err;
1369 }
1370 OPENSSL_cleanse(pms, pms_len);
1371 OPENSSL_free(pms);
1372 pms = new_pms;
1373 pms_len = new_pms_len;
1374 }
1375
David Benjaminc11ea9422017-08-29 16:33:21 -04001376 // The message must be added to the finished hash before calculating the
1377 // master secret.
David Benjamind781fc42017-07-12 16:25:57 -04001378 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001379 goto err;
1380 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001381
David Benjamin45738dd2017-02-09 20:01:26 -05001382 hs->new_session->master_key_length = tls1_generate_master_secret(
1383 hs, hs->new_session->master_key, pms, pms_len);
1384 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001385 goto err;
1386 }
David Benjaminfc02b592017-02-17 16:26:01 -05001387 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001388 OPENSSL_cleanse(pms, pms_len);
1389 OPENSSL_free(pms);
1390
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001391 hs->state = state_send_client_certificate_verify;
1392 return ssl_hs_ok;
Adam Langley1258b6a2014-06-20 12:00:00 -07001393
1394err:
David Benjaminae0eaaa2015-12-17 02:01:25 -05001395 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001396 OPENSSL_cleanse(pms, pms_len);
1397 OPENSSL_free(pms);
1398 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001399 return ssl_hs_error;
1400
Adam Langley1bea1732014-12-17 19:06:57 -08001401}
Adam Langley1258b6a2014-06-20 12:00:00 -07001402
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001403static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001404 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001405
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001406 if (!hs->cert_request || !ssl_has_certificate(ssl)) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001407 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001408 return ssl_hs_ok;
1409 }
1410
1411 assert(ssl_has_private_key(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -04001412 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001413 CBB body, child;
1414 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001415 SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001416 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001417 }
1418
David Benjaminea9a0d52016-07-08 15:52:59 -07001419 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001420 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001421 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001422 }
David Benjamin429fdc02016-06-17 17:33:30 -04001423 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001424 // Write out the digest type in TLS 1.2.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001425 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001426 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001427 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001428 }
1429 }
David Benjaminfc825122015-12-18 01:57:43 -05001430
David Benjaminc11ea9422017-08-29 16:33:21 -04001431 // Set aside space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001432 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001433 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001434 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001435 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001436 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001437 }
1438
Steven Valdez2b8415e2016-06-30 13:27:23 -04001439 size_t sig_len = max_sig_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001440 // The SSL3 construction for CertificateVerify does not decompose into a
1441 // single final digest and signature, and must be special-cased.
David Benjamin44148742017-06-17 13:20:59 -04001442 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1443 if (ssl->cert->key_method != NULL) {
1444 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001445 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001446 }
1447
David Benjamin44148742017-06-17 13:20:59 -04001448 uint8_t digest[EVP_MAX_MD_SIZE];
1449 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001450 if (!hs->transcript.GetSSL3CertVerifyHash(
1451 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001452 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001453 }
David Benjaminfc825122015-12-18 01:57:43 -05001454
David Benjamin1386aad2017-07-19 23:57:40 -04001455 UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
1456 if (!pctx ||
1457 !EVP_PKEY_sign_init(pctx.get()) ||
1458 !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001459 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001460 }
1461 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001462 switch (ssl_private_key_sign(
1463 hs, ptr, &sig_len, max_sig_len, signature_algorithm,
1464 hs->transcript.buffer_data(), hs->transcript.buffer_len())) {
David Benjamin44148742017-06-17 13:20:59 -04001465 case ssl_private_key_success:
1466 break;
1467 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001468 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001469 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001470 hs->state = state_send_client_certificate_verify;
1471 return ssl_hs_private_key_operation;
David Benjamin44148742017-06-17 13:20:59 -04001472 }
David Benjaminfc825122015-12-18 01:57:43 -05001473 }
1474
David Benjaminfc825122015-12-18 01:57:43 -05001475 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001476 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001477 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001478 }
1479
David Benjaminc11ea9422017-08-29 16:33:21 -04001480 // The handshake buffer is no longer necessary.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001481 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001482
David Benjamin4cbb9312017-08-31 14:52:31 -04001483 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001484 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001485}
1486
David Benjamin4cbb9312017-08-31 14:52:31 -04001487static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001488 SSL *const ssl = hs->ssl;
David Benjamin4cbb9312017-08-31 14:52:31 -04001489 // Resolve Channel ID first, before any non-idempotent operations.
1490 if (ssl->s3->tlsext_channel_id_valid) {
1491 if (!ssl_do_channel_id_callback(ssl)) {
1492 return ssl_hs_error;
1493 }
1494
1495 if (ssl->tlsext_channel_id_private == NULL) {
1496 hs->state = state_send_client_finished;
1497 return ssl_hs_channel_id_lookup;
1498 }
1499 }
David Benjamine97b9622015-10-10 14:59:09 -04001500
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001501 if (!ssl->method->add_change_cipher_spec(ssl) ||
1502 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
1503 return ssl_hs_error;
David Benjamine97b9622015-10-10 14:59:09 -04001504 }
1505
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001506 if (hs->next_proto_neg_seen) {
1507 static const uint8_t kZero[32] = {0};
1508 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
1509
1510 ScopedCBB cbb;
1511 CBB body, child;
1512 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1513 !CBB_add_u8_length_prefixed(&body, &child) ||
1514 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1515 ssl->s3->next_proto_negotiated_len) ||
1516 !CBB_add_u8_length_prefixed(&body, &child) ||
1517 !CBB_add_bytes(&child, kZero, padding_len) ||
1518 !ssl_add_message_cbb(ssl, cbb.get())) {
1519 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1520 return ssl_hs_error;
1521 }
1522 }
1523
David Benjamin4cbb9312017-08-31 14:52:31 -04001524 if (ssl->s3->tlsext_channel_id_valid) {
1525 ScopedCBB cbb;
1526 CBB body;
1527 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1528 !tls1_write_channel_id(hs, &body) ||
1529 !ssl_add_message_cbb(ssl, cbb.get())) {
1530 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1531 return ssl_hs_error;
1532 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001533 }
1534
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001535 if (!ssl3_send_finished(hs)) {
1536 return ssl_hs_error;
1537 }
1538
1539 hs->state = state_finish_flight;
1540 return ssl_hs_flush;
1541}
1542
1543static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001544 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001545 if (ssl->session != NULL) {
1546 hs->state = state_finish_client_handshake;
1547 return ssl_hs_ok;
1548 }
1549
David Benjaminc11ea9422017-08-29 16:33:21 -04001550 // This is a full handshake. If it involves ChannelID, then record the
1551 // handshake hashes at this point in the session so that any resumption of
1552 // this session with ChannelID can sign those hashes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001553 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1554 return ssl_hs_error;
1555 }
1556
1557 hs->state = state_read_session_ticket;
1558
1559 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1560 ssl3_can_false_start(ssl) &&
David Benjaminc11ea9422017-08-29 16:33:21 -04001561 // No False Start on renegotiation (would complicate the state machine).
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001562 !ssl->s3->initial_handshake_complete) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001563 hs->in_false_start = true;
1564 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001565 return ssl_hs_early_return;
1566 }
1567
1568 return ssl_hs_ok;
1569}
1570
1571static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1572 SSL *const ssl = hs->ssl;
1573
1574 if (!hs->ticket_expected) {
1575 hs->state = state_process_change_cipher_spec;
1576 return ssl_hs_read_change_cipher_spec;
1577 }
1578
David Benjamin7934f082017-08-01 16:32:25 -04001579 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001580 if (!ssl->method->get_message(ssl, &msg)) {
1581 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001582 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001583
David Benjamin7934f082017-08-01 16:32:25 -04001584 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1585 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001586 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001587 }
1588
David Benjamin7934f082017-08-01 16:32:25 -04001589 CBS new_session_ticket = msg.body, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001590 uint32_t tlsext_tick_lifetime_hint;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001591 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001592 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1593 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001594 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001595 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001596 return ssl_hs_error;
David Benjamin454aa4c2016-03-10 08:43:27 -05001597 }
1598
David Benjamin9f1dc822016-06-07 17:03:46 -04001599 if (CBS_len(&ticket) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001600 // RFC 5077 allows a server to change its mind and send no ticket after
1601 // negotiating the extension. The value of |ticket_expected| is checked in
1602 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
David Benjaminfd45ee72017-08-31 14:49:09 -04001603 hs->ticket_expected = false;
David Benjamin8f94c312017-08-01 17:35:55 -04001604 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001605 hs->state = state_process_change_cipher_spec;
1606 return ssl_hs_read_change_cipher_spec;
David Benjamin9f1dc822016-06-07 17:03:46 -04001607 }
1608
David Benjamin31b0c9b2017-07-20 14:49:15 -04001609 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001610 UniquePtr<SSL_SESSION> renewed_session;
1611 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001612 // The server is sending a new ticket for an existing session. Sessions are
1613 // immutable once established, so duplicate all but the ticket of the
1614 // existing session.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001615 renewed_session =
1616 SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001617 if (!renewed_session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001618 // This should never happen.
David Benjamin9f1dc822016-06-07 17:03:46 -04001619 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001620 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001621 }
David Benjamin1386aad2017-07-19 23:57:40 -04001622 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001623 }
1624
David Benjaminc11ea9422017-08-29 16:33:21 -04001625 // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued.
David Benjamin17b30832017-01-28 14:00:32 -05001626 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001627
Steven Valdez87eab492016-06-27 16:34:59 -04001628 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001629 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001630 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001631 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001632 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001633
David Benjaminc11ea9422017-08-29 16:33:21 -04001634 // Generate a session ID for this session based on the session ticket. We use
1635 // the session ID mechanism for detecting ticket resumption. This also fits in
1636 // with assumptions elsewhere in OpenSSL.
Steven Valdez87eab492016-06-27 16:34:59 -04001637 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1638 session->session_id, &session->session_id_length,
1639 EVP_sha256(), NULL)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001640 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001641 }
1642
David Benjamin1386aad2017-07-19 23:57:40 -04001643 if (renewed_session) {
Steven Valdez87eab492016-06-27 16:34:59 -04001644 session->not_resumable = 0;
1645 SSL_SESSION_free(ssl->session);
David Benjamin1386aad2017-07-19 23:57:40 -04001646 ssl->session = renewed_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -04001647 }
1648
David Benjamin8f94c312017-08-01 17:35:55 -04001649 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001650 hs->state = state_process_change_cipher_spec;
1651 return ssl_hs_read_change_cipher_spec;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001652}
David Benjamin86e95b82017-07-18 16:34:25 -04001653
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001654static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1655 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
1656 return ssl_hs_error;
1657 }
1658
1659 hs->state = state_read_server_finished;
1660 return ssl_hs_ok;
1661}
1662
1663static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1664 SSL *const ssl = hs->ssl;
1665 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1666 if (wait != ssl_hs_ok) {
1667 return wait;
1668 }
1669
1670 if (ssl->session != NULL) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001671 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001672 return ssl_hs_ok;
1673 }
1674
1675 hs->state = state_finish_client_handshake;
1676 return ssl_hs_ok;
1677}
1678
1679static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1680 SSL *const ssl = hs->ssl;
1681
1682 ssl->method->on_handshake_complete(ssl);
1683
1684 SSL_SESSION_free(ssl->s3->established_session);
1685 if (ssl->session != NULL) {
1686 SSL_SESSION_up_ref(ssl->session);
1687 ssl->s3->established_session = ssl->session;
1688 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001689 // We make a copy of the session in order to maintain the immutability
1690 // of the new established_session due to False Start. The caller may
1691 // have taken a reference to the temporary session.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001692 ssl->s3->established_session =
1693 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL)
1694 .release();
1695 if (ssl->s3->established_session == NULL) {
1696 return ssl_hs_error;
1697 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001698 // Renegotiations do not participate in session resumption.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001699 if (!ssl->s3->initial_handshake_complete) {
1700 ssl->s3->established_session->not_resumable = 0;
1701 }
1702
1703 hs->new_session.reset();
1704 }
1705
David Benjaminfd45ee72017-08-31 14:49:09 -04001706 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001707 ssl->s3->initial_handshake_complete = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001708 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
1709
1710 hs->state = state_done;
1711 return ssl_hs_ok;
1712}
1713
1714enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1715 while (hs->state != state_done) {
1716 enum ssl_hs_wait_t ret = ssl_hs_error;
1717 enum ssl_client_hs_state_t state =
1718 static_cast<enum ssl_client_hs_state_t>(hs->state);
1719 switch (state) {
1720 case state_start_connect:
1721 ret = do_start_connect(hs);
1722 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001723 case state_enter_early_data:
1724 ret = do_enter_early_data(hs);
1725 break;
1726 case state_read_hello_verify_request:
1727 ret = do_read_hello_verify_request(hs);
1728 break;
1729 case state_read_server_hello:
1730 ret = do_read_server_hello(hs);
1731 break;
1732 case state_tls13:
1733 ret = do_tls13(hs);
1734 break;
1735 case state_read_server_certificate:
1736 ret = do_read_server_certificate(hs);
1737 break;
1738 case state_read_certificate_status:
1739 ret = do_read_certificate_status(hs);
1740 break;
1741 case state_verify_server_certificate:
1742 ret = do_verify_server_certificate(hs);
1743 break;
1744 case state_read_server_key_exchange:
1745 ret = do_read_server_key_exchange(hs);
1746 break;
1747 case state_read_certificate_request:
1748 ret = do_read_certificate_request(hs);
1749 break;
1750 case state_read_server_hello_done:
1751 ret = do_read_server_hello_done(hs);
1752 break;
1753 case state_send_client_certificate:
1754 ret = do_send_client_certificate(hs);
1755 break;
1756 case state_send_client_key_exchange:
1757 ret = do_send_client_key_exchange(hs);
1758 break;
1759 case state_send_client_certificate_verify:
1760 ret = do_send_client_certificate_verify(hs);
1761 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001762 case state_send_client_finished:
1763 ret = do_send_client_finished(hs);
1764 break;
1765 case state_finish_flight:
1766 ret = do_finish_flight(hs);
1767 break;
1768 case state_read_session_ticket:
1769 ret = do_read_session_ticket(hs);
1770 break;
1771 case state_process_change_cipher_spec:
1772 ret = do_process_change_cipher_spec(hs);
1773 break;
1774 case state_read_server_finished:
1775 ret = do_read_server_finished(hs);
1776 break;
1777 case state_finish_client_handshake:
1778 ret = do_finish_client_handshake(hs);
1779 break;
1780 case state_done:
1781 ret = ssl_hs_ok;
1782 break;
1783 }
1784
1785 if (hs->state != state) {
1786 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1787 }
1788
1789 if (ret != ssl_hs_ok) {
1790 return ret;
1791 }
1792 }
1793
1794 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1795 return ssl_hs_ok;
1796}
1797
1798const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1799 enum ssl_client_hs_state_t state =
1800 static_cast<enum ssl_client_hs_state_t>(hs->state);
1801 switch (state) {
1802 case state_start_connect:
1803 return "TLS client start_connect";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001804 case state_enter_early_data:
1805 return "TLS client enter_early_data";
1806 case state_read_hello_verify_request:
1807 return "TLS client read_hello_verify_request";
1808 case state_read_server_hello:
1809 return "TLS client read_server_hello";
1810 case state_tls13:
1811 return tls13_client_handshake_state(hs);
1812 case state_read_server_certificate:
1813 return "TLS client read_server_certificate";
1814 case state_read_certificate_status:
1815 return "TLS client read_certificate_status";
1816 case state_verify_server_certificate:
1817 return "TLS client verify_server_certificate";
1818 case state_read_server_key_exchange:
1819 return "TLS client read_server_key_exchange";
1820 case state_read_certificate_request:
1821 return "TLS client read_certificate_request";
1822 case state_read_server_hello_done:
1823 return "TLS client read_server_hello_done";
1824 case state_send_client_certificate:
1825 return "TLS client send_client_certificate";
1826 case state_send_client_key_exchange:
1827 return "TLS client send_client_key_exchange";
1828 case state_send_client_certificate_verify:
1829 return "TLS client send_client_certificate_verify";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001830 case state_send_client_finished:
1831 return "TLS client send_client_finished";
1832 case state_finish_flight:
1833 return "TLS client finish_flight";
1834 case state_read_session_ticket:
1835 return "TLS client read_session_ticket";
1836 case state_process_change_cipher_spec:
1837 return "TLS client process_change_cipher_spec";
1838 case state_read_server_finished:
1839 return "TLS client read_server_finished";
1840 case state_finish_client_handshake:
1841 return "TLS client finish_client_handshake";
1842 case state_done:
1843 return "TLS client done";
1844 }
1845
1846 return "TLS client unknown";
1847}
1848
1849}