blob: 2c4269892ab8cc99336706ddf46ba5abbf458fdb [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04007 *
Adam Langley95c29f32014-06-20 12:00:00 -07008 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040014 *
Adam Langley95c29f32014-06-20 12:00:00 -070015 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040021 *
Adam Langley95c29f32014-06-20 12:00:00 -070022 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
Steven Valdez4d71a9a2017-08-14 15:08:34 -040036 * 4. If you include any Windows specific code (or a derivative thereof) from
Adam Langley95c29f32014-06-20 12:00:00 -070037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Steven Valdez4d71a9a2017-08-14 15:08:34 -040039 *
Adam Langley95c29f32014-06-20 12:00:00 -070040 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
Steven Valdez4d71a9a2017-08-14 15:08:34 -040051 *
Adam Langley95c29f32014-06-20 12:00:00 -070052 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
Steven Valdez4d71a9a2017-08-14 15:08:34 -040065 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070066 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400113 * Portions of the attached software ("Contribution") are developed by
Adam Langley95c29f32014-06-20 12:00:00 -0700114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE.
148 */
149
David Benjamin9e4e01e2015-09-15 01:48:04 -0400150#include <openssl/ssl.h>
151
David Benjamin81ea0bf2014-11-23 04:20:17 -0500152#include <assert.h>
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400153#include <limits.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400154#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamin31b0c9b2017-07-20 14:49:15 -0400156#include <utility>
157
David Benjaminabbbee12016-10-31 19:20:42 -0400158#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400159#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700160#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400161#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400162#include <openssl/ec_key.h>
163#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400164#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400167#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400168#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin17cf2cb2016-12-13 01:07:13 -0500170#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400171#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin86e95b82017-07-18 16:34:25 -0400174namespace bssl {
175
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400176enum ssl_client_hs_state_t {
177 state_start_connect = 0,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400178 state_enter_early_data,
179 state_read_hello_verify_request,
180 state_read_server_hello,
181 state_tls13,
182 state_read_server_certificate,
183 state_read_certificate_status,
184 state_verify_server_certificate,
185 state_read_server_key_exchange,
186 state_read_certificate_request,
187 state_read_server_hello_done,
188 state_send_client_certificate,
189 state_send_client_key_exchange,
190 state_send_client_certificate_verify,
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400191 state_send_client_finished,
192 state_finish_flight,
193 state_read_session_ticket,
194 state_process_change_cipher_spec,
195 state_read_server_finished,
196 state_finish_client_handshake,
197 state_done,
198};
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjaminc11ea9422017-08-29 16:33:21 -0400200// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
201// disabled algorithms.
David Benjamin2fe6e222017-01-25 02:18:43 -0500202static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
203 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500204 *out_mask_a = 0;
205 *out_mask_k = 0;
206
David Benjaminc11ea9422017-08-29 16:33:21 -0400207 // PSK requires a client callback.
David Benjamin2fe6e222017-01-25 02:18:43 -0500208 if (ssl->psk_client_callback == NULL) {
209 *out_mask_a |= SSL_aPSK;
210 *out_mask_k |= SSL_kPSK;
211 }
212}
213
David Benjamin68161cb2017-06-20 14:49:43 -0400214static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
215 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500216 uint32_t mask_a, mask_k;
217 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamine8d53502015-10-10 14:13:23 -0400219 CBB child;
220 if (!CBB_add_u16_length_prefixed(out, &child)) {
221 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800222 }
223
David Benjaminc11ea9422017-08-29 16:33:21 -0400224 // Add a fake cipher suite. See draft-davidben-tls-grease-01.
David Benjamin65ac9972016-09-02 21:35:25 -0400225 if (ssl->ctx->grease_enabled &&
226 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
227 return 0;
228 }
229
David Benjaminc11ea9422017-08-29 16:33:21 -0400230 // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
231 // hardware support.
David Benjamin68161cb2017-06-20 14:49:43 -0400232 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400233 if (!EVP_has_aes_hardware() &&
234 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
235 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500236 }
David Benjaminabbbee12016-10-31 19:20:42 -0400237 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
238 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
239 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400240 }
David Benjaminabbbee12016-10-31 19:20:42 -0400241 if (EVP_has_aes_hardware() &&
242 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400243 return 0;
244 }
245 }
246
David Benjamin68161cb2017-06-20 14:49:43 -0400247 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400248 int any_enabled = 0;
David Benjaminee910bf2017-07-25 22:36:00 -0400249 for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400250 // Skip disabled ciphers
David Benjamin2fe6e222017-01-25 02:18:43 -0500251 if ((cipher->algorithm_mkey & mask_k) ||
252 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400253 continue;
254 }
David Benjamin68161cb2017-06-20 14:49:43 -0400255 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
256 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400257 continue;
258 }
259 any_enabled = 1;
260 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
261 return 0;
262 }
263 }
264
David Benjaminc11ea9422017-08-29 16:33:21 -0400265 // If all ciphers were disabled, return the error to the caller.
David Benjamin68161cb2017-06-20 14:49:43 -0400266 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400267 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
268 return 0;
269 }
David Benjamine8d53502015-10-10 14:13:23 -0400270 }
271
David Benjaminc11ea9422017-08-29 16:33:21 -0400272 // For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
273 // added.
David Benjamin68161cb2017-06-20 14:49:43 -0400274 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400275 !ssl->s3->initial_handshake_complete) {
276 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
277 return 0;
278 }
David Benjamine8d53502015-10-10 14:13:23 -0400279 }
280
David Benjamine0ff7672016-09-19 18:40:03 -0400281 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200282 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
283 return 0;
284 }
David Benjamine8d53502015-10-10 14:13:23 -0400285 }
286
287 return CBB_flush(out);
288}
289
David Benjamin8c880a22016-12-03 02:20:34 -0500290int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
291 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400292 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -0400293 CBB body;
294 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
295 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400296 }
297
David Benjaminc11ea9422017-08-29 16:33:21 -0400298 // Renegotiations do not participate in session resumption.
Steven Valdez520e1222017-06-13 12:45:25 -0400299 int has_session_id = ssl->session != NULL &&
300 !ssl->s3->initial_handshake_complete &&
301 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400302
303 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500304 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400305 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400306 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400307 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400308 }
309
Steven Valdez520e1222017-06-13 12:45:25 -0400310 if (has_session_id) {
311 if (!CBB_add_bytes(&child, ssl->session->session_id,
312 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400313 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400314 }
315 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400316 // In TLS 1.3 experimental encodings, send a fake placeholder session ID
317 // when we do not otherwise have one to send.
Steven Valdez520e1222017-06-13 12:45:25 -0400318 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezc7d4d212017-09-11 13:53:08 -0400319 ssl_is_resumption_variant(ssl->tls13_variant) &&
Steven Valdez520e1222017-06-13 12:45:25 -0400320 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400321 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400322 }
323 }
324
David Benjamince079fd2016-08-02 16:22:34 -0400325 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400326 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400327 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400328 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400329 }
330 }
331
332 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400333 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400334 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400335 !CBB_add_u8(&body, 1 /* one compression method */) ||
336 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500337 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400338 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400339 }
340
David Benjamin879efc32017-09-21 11:20:53 -0400341 Array<uint8_t> msg;
342 if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
David Benjamind781fc42017-07-12 16:25:57 -0400343 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400344 }
345
David Benjaminc11ea9422017-08-29 16:33:21 -0400346 // Now that the length prefixes have been computed, fill in the placeholder
347 // PSK binder.
David Benjamin8c880a22016-12-03 02:20:34 -0500348 if (hs->needs_psk_binder &&
David Benjamin879efc32017-09-21 11:20:53 -0400349 !tls13_write_psk_binder(hs, msg.data(), msg.size())) {
David Benjamind781fc42017-07-12 16:25:57 -0400350 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400351 }
352
David Benjamin879efc32017-09-21 11:20:53 -0400353 return ssl->method->add_message(ssl, std::move(msg));
Steven Valdez5440fe02016-07-18 12:40:30 -0400354}
355
David Benjamin7934f082017-08-01 16:32:25 -0400356static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out,
357 const SSLMessage &msg) {
David Benjaminb853f312017-07-14 18:40:34 -0400358 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400359 if (msg.type != SSL3_MT_SERVER_HELLO &&
360 msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
David Benjaminb853f312017-07-14 18:40:34 -0400361 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
362 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
363 return 0;
364 }
365
David Benjamin7934f082017-08-01 16:32:25 -0400366 CBS server_hello = msg.body;
David Benjaminb853f312017-07-14 18:40:34 -0400367 if (!CBS_get_u16(&server_hello, out)) {
368 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
369 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
370 return 0;
371 }
372
David Benjaminc11ea9422017-08-29 16:33:21 -0400373 // The server version may also be in the supported_versions extension if
374 // applicable.
David Benjamin7934f082017-08-01 16:32:25 -0400375 if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) {
David Benjaminb853f312017-07-14 18:40:34 -0400376 return 1;
377 }
378
379 uint8_t sid_length;
380 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
381 !CBS_get_u8(&server_hello, &sid_length) ||
382 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
383 1 /* compression_method */)) {
384 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
385 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
386 return 0;
387 }
388
David Benjaminc11ea9422017-08-29 16:33:21 -0400389 // The extensions block may not be present.
David Benjaminb853f312017-07-14 18:40:34 -0400390 if (CBS_len(&server_hello) == 0) {
391 return 1;
392 }
393
394 CBS extensions;
395 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
396 CBS_len(&server_hello) != 0) {
397 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
398 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
399 return 0;
400 }
401
David Benjamin74795b32017-08-31 15:13:12 -0400402 bool have_supported_versions;
David Benjaminb853f312017-07-14 18:40:34 -0400403 CBS supported_versions;
404 const SSL_EXTENSION_TYPE ext_types[] = {
405 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
406 &supported_versions},
407 };
408
409 uint8_t alert = SSL_AD_DECODE_ERROR;
410 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
411 OPENSSL_ARRAY_SIZE(ext_types),
412 1 /* ignore unknown */)) {
413 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
414 return 0;
415 }
416
417 if (have_supported_versions &&
418 (!CBS_get_u16(&supported_versions, out) ||
419 CBS_len(&supported_versions) != 0)) {
420 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
421 return 0;
422 }
423
424 return 1;
425}
426
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400427static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400428 SSL *const ssl = hs->ssl;
429
Steven Valdez398085b2017-08-29 13:25:56 -0400430 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamind0beda02017-09-06 16:46:50 -0400431 // |session_reused| must be reset in case this is a renegotiation.
432 ssl->s3->session_reused = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400433
David Benjaminc11ea9422017-08-29 16:33:21 -0400434 // Freeze the version range.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400435 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
436 return ssl_hs_error;
437 }
438
Steven Valdezc7d4d212017-09-11 13:53:08 -0400439 // SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer
440 // version.
441 if (hs->max_version == SSL3_VERSION) {
442 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION);
443 }
444
David Benjaminc11ea9422017-08-29 16:33:21 -0400445 // Always advertise the ClientHello version from the original maximum version,
446 // even on renegotiation. The static RSA key exchange uses this field, and
447 // some servers fail when it changes across handshakes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400448 if (SSL_is_dtls(hs->ssl)) {
449 hs->client_version =
450 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
451 } else {
452 hs->client_version =
453 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
454 }
455
David Benjaminc11ea9422017-08-29 16:33:21 -0400456 // If the configured session has expired or was created at a disabled
457 // version, drop it.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400458 if (ssl->session != NULL) {
459 if (ssl->session->is_server ||
460 !ssl_supports_version(hs, ssl->session->ssl_version) ||
461 (ssl->session->session_id_length == 0 &&
462 ssl->session->tlsext_ticklen == 0) ||
463 ssl->session->not_resumable ||
464 !ssl_session_is_time_valid(ssl, ssl->session)) {
465 ssl_set_session(ssl, NULL);
466 }
467 }
468
Steven Valdez398085b2017-08-29 13:25:56 -0400469 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400470 return ssl_hs_error;
471 }
472
David Benjaminc11ea9422017-08-29 16:33:21 -0400473 // Initialize a random session ID for the experimental TLS 1.3 variant
474 // requiring a session id.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400475 if (ssl_is_resumption_variant(ssl->tls13_variant)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400476 hs->session_id_len = sizeof(hs->session_id);
477 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
478 return ssl_hs_error;
479 }
480 }
481
482 if (!ssl_write_client_hello(hs)) {
483 return ssl_hs_error;
484 }
485
486 hs->state = state_enter_early_data;
487 return ssl_hs_flush;
488}
489
490static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
491 SSL *const ssl = hs->ssl;
492
Steven Valdez398085b2017-08-29 13:25:56 -0400493 if (SSL_is_dtls(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400494 hs->state = state_read_hello_verify_request;
495 return ssl_hs_ok;
496 }
497
498 if (!hs->early_data_offered) {
499 hs->state = state_read_server_hello;
500 return ssl_hs_ok;
501 }
502
503 if (!tls13_init_early_key_schedule(hs) ||
504 !tls13_advance_key_schedule(hs, ssl->session->master_key,
505 ssl->session->master_key_length) ||
506 !tls13_derive_early_secrets(hs) ||
507 !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
508 hs->hash_len)) {
509 return ssl_hs_error;
510 }
511
David Benjaminc11ea9422017-08-29 16:33:21 -0400512 // Stash the early data session, so connection properties may be queried out
513 // of it.
David Benjaminfd45ee72017-08-31 14:49:09 -0400514 hs->in_early_data = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400515 SSL_SESSION_up_ref(ssl->session);
516 hs->early_session.reset(ssl->session);
David Benjaminfd45ee72017-08-31 14:49:09 -0400517 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400518
519 hs->state = state_read_server_hello;
520 return ssl_hs_early_return;
521}
522
523static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
524 SSL *const ssl = hs->ssl;
525
526 assert(SSL_is_dtls(ssl));
527
528 SSLMessage msg;
529 if (!ssl->method->get_message(ssl, &msg)) {
530 return ssl_hs_read_message;
531 }
532
533 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400534 hs->state = state_read_server_hello;
535 return ssl_hs_ok;
536 }
537
538 CBS hello_verify_request = msg.body, cookie;
539 uint16_t server_version;
540 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
541 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
542 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
543 CBS_len(&hello_verify_request) != 0) {
544 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
545 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
546 return ssl_hs_error;
547 }
548
549 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
550 ssl->d1->cookie_len = CBS_len(&cookie);
551
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400552 ssl->method->next_message(ssl);
Steven Valdez398085b2017-08-29 13:25:56 -0400553
David Benjaminc11ea9422017-08-29 16:33:21 -0400554 // DTLS resets the handshake buffer after HelloVerifyRequest.
Steven Valdez398085b2017-08-29 13:25:56 -0400555 if (!hs->transcript.Init()) {
556 return ssl_hs_error;
557 }
558
559 if (!ssl_write_client_hello(hs)) {
560 return ssl_hs_error;
561 }
562
563 hs->state = state_read_server_hello;
564 return ssl_hs_flush;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400565}
566
567static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900568 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400569 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400570 if (!ssl->method->get_message(ssl, &msg)) {
571 return ssl_hs_read_server_hello;
Adam Langley1bea1732014-12-17 19:06:57 -0800572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
David Benjaminb853f312017-07-14 18:40:34 -0400574 uint16_t server_version;
David Benjamin7934f082017-08-01 16:32:25 -0400575 if (!parse_server_version(hs, &server_version, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400576 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400577 }
578
Steven Valdez8f36c512017-06-20 10:55:02 -0400579 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400580 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400581 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400582 return ssl_hs_error;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400583 }
584
David Benjamin0d56f882015-12-19 17:05:56 -0500585 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
586 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400587 ssl->version = server_version;
David Benjaminc11ea9422017-08-29 16:33:21 -0400588 // At this point, the connection's version is known and ssl->version is
589 // fixed. Begin enforcing the record-layer version.
David Benjamin046bc1f2017-08-31 15:06:42 -0400590 ssl->s3->have_version = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400591 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400592 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400593 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400594 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400595 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800596 }
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Steven Valdez143e8b32016-07-11 13:19:03 -0400598 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400599 hs->state = state_tls13;
600 return ssl_hs_ok;
Steven Valdez143e8b32016-07-11 13:19:03 -0400601 }
602
Steven Valdez2d850622017-01-11 11:34:52 -0500603 if (hs->early_data_offered) {
604 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400605 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400606 return ssl_hs_error;
Steven Valdez2d850622017-01-11 11:34:52 -0500607 }
608
David Benjamin6e4fc332016-11-17 16:43:08 +0900609 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400610
David Benjamin7934f082017-08-01 16:32:25 -0400611 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400612 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400613 }
614
David Benjamin7934f082017-08-01 16:32:25 -0400615 CBS server_hello = msg.body, server_random, session_id;
David Benjaminb853f312017-07-14 18:40:34 -0400616 uint16_t cipher_suite;
617 uint8_t compression_method;
David Benjaminb853f312017-07-14 18:40:34 -0400618 if (!CBS_skip(&server_hello, 2 /* version */) ||
619 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400620 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
621 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
622 !CBS_get_u16(&server_hello, &cipher_suite) ||
623 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400624 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400625 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400626 return ssl_hs_error;
Steven Valdez143e8b32016-07-11 13:19:03 -0400627 }
628
David Benjaminc11ea9422017-08-29 16:33:21 -0400629 // Copy over the server random.
David Benjamind3bca042017-04-08 11:05:04 -0400630 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
631 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700632
David Benjaminc11ea9422017-08-29 16:33:21 -0400633 // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
634 // 1.3 is finalized and we are not implementing a draft version.
David Benjamin1f61f0d2016-07-10 12:20:35 -0400635
David Benjamin0d56f882015-12-19 17:05:56 -0500636 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400637 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500638 CBS_mem_equal(&session_id, ssl->session->session_id,
639 ssl->session->session_id_length)) {
David Benjamin046bc1f2017-08-31 15:06:42 -0400640 ssl->s3->session_reused = true;
Adam Langley1bea1732014-12-17 19:06:57 -0800641 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400642 // The session wasn't resumed. Create a fresh SSL_SESSION to
643 // fill out.
David Benjamin4d0be242016-09-01 01:10:07 -0400644 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900645 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400646 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400647 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800648 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400649 // Note: session_id could be empty.
David Benjamin45738dd2017-02-09 20:01:26 -0500650 hs->new_session->session_id_length = CBS_len(&session_id);
651 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
652 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800653 }
David Benjamina03d95d2014-07-12 19:49:07 -0400654
David Benjaminca9e8f52017-08-09 15:02:34 -0400655 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
656 if (cipher == NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400657 // unknown cipher
David Benjamin3570d732015-06-29 00:28:17 -0400658 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400659 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400660 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800661 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400662
David Benjaminc11ea9422017-08-29 16:33:21 -0400663 // The cipher must be allowed in the selected version and enabled.
David Benjamin2fe6e222017-01-25 02:18:43 -0500664 uint32_t mask_a, mask_k;
665 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
David Benjaminca9e8f52017-08-09 15:02:34 -0400666 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
667 SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
668 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) ||
669 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400670 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400671 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400672 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800673 }
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Steven Valdez87eab492016-06-27 16:34:59 -0400675 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500676 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400677 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400678 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400679 return ssl_hs_error;
David Benjaminece3de92015-03-16 18:02:20 -0400680 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400681 if (ssl->session->cipher != cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400682 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400683 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400684 return ssl_hs_error;
Steven Valdez803c77a2016-09-06 14:13:43 -0400685 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400686 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400687 // This is actually a client application bug.
Steven Valdez4aa154e2016-07-29 14:32:55 -0400688 OPENSSL_PUT_ERROR(SSL,
689 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400690 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400691 return ssl_hs_error;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400692 }
David Benjamin0623bce2015-12-25 15:40:14 -0500693 } else {
David Benjaminca9e8f52017-08-09 15:02:34 -0400694 hs->new_session->cipher = cipher;
Adam Langley1bea1732014-12-17 19:06:57 -0800695 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400696 hs->new_cipher = cipher;
David Benjaminbdf5e722014-11-11 00:52:15 -0500697
David Benjaminc11ea9422017-08-29 16:33:21 -0400698 // Now that the cipher is known, initialize the handshake hash and hash the
699 // ServerHello.
David Benjaminca9e8f52017-08-09 15:02:34 -0400700 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400701 !ssl_hash_message(hs, msg)) {
David Benjamind3bca042017-04-08 11:05:04 -0400702 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400703 return ssl_hs_error;
David Benjamin9550c3a2015-08-05 08:50:34 -0400704 }
705
David Benjaminc11ea9422017-08-29 16:33:21 -0400706 // If doing a full handshake, the server may request a client certificate
707 // which requires hashing the handshake transcript. Otherwise, the handshake
708 // buffer may be released.
David Benjamin3d458dc2016-09-12 22:40:27 +0000709 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500710 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400711 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -0800712 }
David Benjamina03d95d2014-07-12 19:49:07 -0400713
David Benjaminc11ea9422017-08-29 16:33:21 -0400714 // Only the NULL compression algorithm is supported.
Adam Langley1bea1732014-12-17 19:06:57 -0800715 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400716 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -0400717 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400718 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800719 }
Adam Langley95c29f32014-06-20 12:00:00 -0700720
David Benjaminc11ea9422017-08-29 16:33:21 -0400721 // TLS extensions
David Benjamin8c880a22016-12-03 02:20:34 -0500722 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400723 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400724 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800725 }
Adam Langley95c29f32014-06-20 12:00:00 -0700726
David Benjaminc11ea9422017-08-29 16:33:21 -0400727 // There should be nothing left over in the record.
Adam Langley1bea1732014-12-17 19:06:57 -0800728 if (CBS_len(&server_hello) != 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400729 // wrong packet length
David Benjamin639846e2016-09-09 11:41:18 -0400730 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400731 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400732 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800733 }
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Steven Valdez87eab492016-06-27 16:34:59 -0400735 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -0500736 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -0500737 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400738 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700739 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400740 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700741 }
David Benjamind3bca042017-04-08 11:05:04 -0400742 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400743 return ssl_hs_error;
Adam Langleyba5934b2015-06-02 10:50:35 -0700744 }
745
David Benjamin8f94c312017-08-01 17:35:55 -0400746 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400747
748 if (ssl->session != NULL) {
749 hs->state = state_read_session_ticket;
750 return ssl_hs_ok;
751 }
752
753 hs->state = state_read_server_certificate;
754 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400757static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
758 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
759 if (wait == ssl_hs_ok) {
760 hs->state = state_finish_client_handshake;
761 return ssl_hs_ok;
762 }
763
764 return wait;
765}
766
767static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900768 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400769
770 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
771 hs->state = state_read_certificate_status;
772 return ssl_hs_ok;
773 }
774
David Benjamin7934f082017-08-01 16:32:25 -0400775 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400776 if (!ssl->method->get_message(ssl, &msg)) {
777 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800778 }
Adam Langley95c29f32014-06-20 12:00:00 -0700779
David Benjamin7934f082017-08-01 16:32:25 -0400780 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
781 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400782 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -0500783 }
784
David Benjamin7934f082017-08-01 16:32:25 -0400785 CBS body = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800786 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400787 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
David Benjamin7934f082017-08-01 16:32:25 -0400788 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
David Benjamine664a532017-07-20 20:19:36 -0400789 ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400790 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400791 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800792 }
David Benjamine664a532017-07-20 20:19:36 -0400793 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
794 hs->new_session->certs = chain.release();
Adam Langley95c29f32014-06-20 12:00:00 -0700795
David Benjamin45738dd2017-02-09 20:01:26 -0500796 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -0400797 CBS_len(&body) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400798 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -0400799 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -0400800 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400801 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800802 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -0400803
Adam Langley05672202016-12-13 12:05:49 -0800804 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -0400805 hs, hs->peer_pubkey.get(),
David Benjamin45738dd2017-02-09 20:01:26 -0500806 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -0400807 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400808 return ssl_hs_error;
David Benjamin436bf822015-06-07 19:50:37 -0400809 }
810
David Benjamin8f94c312017-08-01 17:35:55 -0400811 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400812
813 hs->state = state_read_certificate_status;
814 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800815}
816
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400817static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900818 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400819
820 if (!hs->certificate_status_expected) {
821 hs->state = state_verify_server_certificate;
822 return ssl_hs_ok;
823 }
824
David Benjamin7934f082017-08-01 16:32:25 -0400825 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400826 if (!ssl->method->get_message(ssl, &msg)) {
827 return ssl_hs_read_message;
David Benjamin9f1dc822016-06-07 17:03:46 -0400828 }
829
David Benjamin7934f082017-08-01 16:32:25 -0400830 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400831 // A server may send status_request in ServerHello and then change its mind
832 // about sending CertificateStatus.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400833 hs->state = state_verify_server_certificate;
834 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400835 }
836
David Benjamin7934f082017-08-01 16:32:25 -0400837 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400838 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500839 }
840
David Benjamin7934f082017-08-01 16:32:25 -0400841 CBS certificate_status = msg.body, ocsp_response;
David Benjamin8d606e32017-06-15 22:43:04 -0400842 uint8_t status_type;
David Benjamin9f1dc822016-06-07 17:03:46 -0400843 if (!CBS_get_u8(&certificate_status, &status_type) ||
844 status_type != TLSEXT_STATUSTYPE_ocsp ||
845 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
846 CBS_len(&ocsp_response) == 0 ||
847 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400848 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400849 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400850 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400851 }
852
David Benjamin8fc2dc02017-08-22 15:07:51 -0700853 CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
854 hs->new_session->ocsp_response =
855 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
856 if (hs->new_session->ocsp_response == nullptr) {
David Benjamin8d606e32017-06-15 22:43:04 -0400857 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400858 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -0400859 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400860
David Benjamin8f94c312017-08-01 17:35:55 -0400861 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400862
863 hs->state = state_verify_server_certificate;
864 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -0400865}
866
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400867static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
868 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
869 hs->state = state_read_server_key_exchange;
870 return ssl_hs_ok;
871 }
872
873 switch (ssl_verify_peer_cert(hs)) {
874 case ssl_verify_ok:
875 break;
876 case ssl_verify_invalid:
877 return ssl_hs_error;
878 case ssl_verify_retry:
879 hs->state = state_verify_server_certificate;
880 return ssl_hs_certificate_verify;
881 }
882
883 hs->state = state_read_server_key_exchange;
884 return ssl_hs_ok;
885}
886
887static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900888 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400889 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400890 if (!ssl->method->get_message(ssl, &msg)) {
891 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -0800892 }
893
David Benjamin7934f082017-08-01 16:32:25 -0400894 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400895 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
David Benjamin45738dd2017-02-09 20:01:26 -0500896 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400897 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -0500898 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400899 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800900 }
901
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400902 hs->state = state_read_certificate_request;
903 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -0800904 }
905
David Benjamin7934f082017-08-01 16:32:25 -0400906 if (!ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400907 return ssl_hs_error;
David Benjaminf71036e2017-01-21 14:49:39 -0500908 }
909
David Benjamin45738dd2017-02-09 20:01:26 -0500910 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
911 uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin7934f082017-08-01 16:32:25 -0400912 CBS server_key_exchange = msg.body;
Adam Langley1bea1732014-12-17 19:06:57 -0800913 if (alg_a & SSL_aPSK) {
914 CBS psk_identity_hint;
915
David Benjaminc11ea9422017-08-29 16:33:21 -0400916 // Each of the PSK key exchanges begins with a psk_identity_hint.
Adam Langley1bea1732014-12-17 19:06:57 -0800917 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
918 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400920 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400921 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800922 }
923
David Benjaminc11ea9422017-08-29 16:33:21 -0400924 // Store PSK identity hint for later use, hint is used in
925 // ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
926 // identity hint can be as long as the maximum length of a PSK identity.
927 // Also do not allow NULL characters; identities are saved as C strings.
928 //
929 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
930 // a specific identity.
Adam Langley1bea1732014-12-17 19:06:57 -0800931 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
932 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400933 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -0400934 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400935 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800936 }
937
David Benjaminc11ea9422017-08-29 16:33:21 -0400938 // Save non-empty identity hints as a C string. Empty identity hints we
939 // treat as missing. Plain PSK makes it possible to send either no hint
940 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
941 // empty hint. Having different capabilities is odd, so we interpret empty
942 // and missing as identical.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400943 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -0400944 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -0400945 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -0400946 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -0400947 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400948 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800949 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400950 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -0800951 }
952
Matthew Braithwaite7e06de52017-04-10 15:52:14 -0700953 if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400954 // Parse the server parameters.
Steven Valdezce902a92016-05-17 11:47:53 -0400955 uint8_t group_type;
956 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -0500957 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -0400958 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
959 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -0700960 !CBS_get_u16(&server_key_exchange, &group_id) ||
961 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -0400962 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400963 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400964 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800965 }
David Benjamin45738dd2017-02-09 20:01:26 -0500966 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -0800967
David Benjaminc11ea9422017-08-29 16:33:21 -0400968 // Ensure the group is consistent with preferences.
Steven Valdezce902a92016-05-17 11:47:53 -0400969 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -0500970 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -0400971 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400972 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800973 }
David Benjamin4298d772015-12-19 00:18:25 -0500974
David Benjaminc11ea9422017-08-29 16:33:21 -0400975 // Initialize ECDH and save the peer public key for later.
David Benjaminc642aca2017-07-19 23:28:43 -0400976 hs->key_share = SSLKeyShare::Create(group_id);
977 if (!hs->key_share ||
David Benjamin08f5c762017-09-21 02:43:05 -0400978 !hs->peer_key.CopyFrom(point)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400979 return ssl_hs_error;
David Benjaminc1cc8582015-12-17 01:22:15 -0500980 }
Adam Langley1bea1732014-12-17 19:06:57 -0800981 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -0400982 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -0400983 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400984 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -0800985 }
986
David Benjaminc11ea9422017-08-29 16:33:21 -0400987 // At this point, |server_key_exchange| contains the signature, if any, while
988 // |msg.body| contains the entire message. From that, derive a CBS containing
989 // just the parameter.
David Benjaminb35d6842015-12-29 17:08:08 -0500990 CBS parameter;
David Benjamin7934f082017-08-01 16:32:25 -0400991 CBS_init(&parameter, CBS_data(&msg.body),
992 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
Adam Langley1bea1732014-12-17 19:06:57 -0800993
David Benjaminc11ea9422017-08-29 16:33:21 -0400994 // ServerKeyExchange should be signed by the server's public key.
David Benjamin45738dd2017-02-09 20:01:26 -0500995 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -0400996 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -0500997 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -0400998 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -0500999 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001000 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001001 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001002 }
David Benjamin8d606e32017-06-15 22:43:04 -04001003 uint8_t alert = SSL_AD_DECODE_ERROR;
1004 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1005 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001006 return ssl_hs_error;
David Benjamin6e807652015-11-02 12:02:20 -05001007 }
David Benjamin45738dd2017-02-09 20:01:26 -05001008 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001009 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001010 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001012 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001013 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001014 }
1015
David Benjaminc11ea9422017-08-29 16:33:21 -04001016 // The last field in |server_key_exchange| is the signature.
David Benjaminf4958e72015-06-07 11:00:53 -04001017 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001018 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1019 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001020 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001021 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001022 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001023 }
1024
David Benjamin1386aad2017-07-19 23:57:40 -04001025 ScopedCBB transcript;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001026 uint8_t *transcript_data;
1027 size_t transcript_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001028 if (!CBB_init(transcript.get(),
1029 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1030 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1031 SSL3_RANDOM_SIZE) ||
1032 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1033 SSL3_RANDOM_SIZE) ||
1034 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1035 CBS_len(&parameter)) ||
1036 !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001037 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001038 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001039 return ssl_hs_error;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001040 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001041
1042 int sig_ok = ssl_public_key_verify(
1043 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001044 hs->peer_pubkey.get(), transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001045 OPENSSL_free(transcript_data);
1046
David Benjaminbf82aed2016-03-01 22:57:40 -05001047#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1048 sig_ok = 1;
1049 ERR_clear_error();
1050#endif
1051 if (!sig_ok) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001052 // bad signature
David Benjamin3570d732015-06-29 00:28:17 -04001053 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001054 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001055 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001056 }
1057 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001058 // PSK ciphers are the only supported certificate-less ciphers.
David Benjaminf4958e72015-06-07 11:00:53 -04001059 assert(alg_a == SSL_aPSK);
1060
Adam Langley1bea1732014-12-17 19:06:57 -08001061 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001062 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001063 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001064 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001065 }
1066 }
David Benjamin8f94c312017-08-01 17:35:55 -04001067
1068 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001069 hs->state = state_read_certificate_request;
1070 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001071}
Adam Langley95c29f32014-06-20 12:00:00 -07001072
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001073static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001074 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001075
1076 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1077 hs->state = state_read_server_hello_done;
1078 return ssl_hs_ok;
1079 }
1080
David Benjamin7934f082017-08-01 16:32:25 -04001081 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001082 if (!ssl->method->get_message(ssl, &msg)) {
1083 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001084 }
David Benjamin688d8df2014-11-02 23:06:42 -05001085
David Benjamin7934f082017-08-01 16:32:25 -04001086 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001087 // If we get here we don't need the handshake buffer as we won't be doing
1088 // client auth.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001089 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001090 hs->state = state_read_server_hello_done;
1091 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001092 }
David Benjamined439582014-07-14 19:13:02 -04001093
David Benjamin7934f082017-08-01 16:32:25 -04001094 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1095 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001096 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001097 }
Adam Langley95c29f32014-06-20 12:00:00 -07001098
David Benjaminc11ea9422017-08-29 16:33:21 -04001099 // Get the certificate types.
David Benjamin7934f082017-08-01 16:32:25 -04001100 CBS body = msg.body, certificate_types;
1101 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001102 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001103 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001104 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001105 }
David Benjamined439582014-07-14 19:13:02 -04001106
David Benjamin08f5c762017-09-21 02:43:05 -04001107 if (!hs->certificate_types.CopyFrom(certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001108 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001109 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001110 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001111
David Benjamina1e9cab2015-12-30 00:08:49 -05001112 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001113 CBS supported_signature_algorithms;
David Benjamin7934f082017-08-01 16:32:25 -04001114 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001115 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001116 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001117 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001118 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001119 }
Adam Langley1bea1732014-12-17 19:06:57 -08001120 }
David Benjamined439582014-07-14 19:13:02 -04001121
Adam Langleyc68e5b92017-02-08 13:33:15 -08001122 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001123 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -04001124 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001125 if (!ca_names) {
David Benjamine0332e82016-07-13 22:40:36 -04001126 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001127 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001128 }
Adam Langley95c29f32014-06-20 12:00:00 -07001129
David Benjamin7934f082017-08-01 16:32:25 -04001130 if (CBS_len(&body) != 0) {
David Benjamin639846e2016-09-09 11:41:18 -04001131 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001132 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001133 return ssl_hs_error;
David Benjamin639846e2016-09-09 11:41:18 -04001134 }
1135
David Benjaminfd45ee72017-08-31 14:49:09 -04001136 hs->cert_request = true;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001137 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001138 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001139
David Benjamin8f94c312017-08-01 17:35:55 -04001140 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001141 hs->state = state_read_server_hello_done;
1142 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001143}
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001145static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001146 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001147 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001148 if (!ssl->method->get_message(ssl, &msg)) {
1149 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001150 }
David Benjamin46062682014-07-14 19:14:32 -04001151
David Benjamin7934f082017-08-01 16:32:25 -04001152 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1153 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001154 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001155 }
1156
David Benjaminc11ea9422017-08-29 16:33:21 -04001157 // ServerHelloDone is empty.
David Benjamin7934f082017-08-01 16:32:25 -04001158 if (CBS_len(&msg.body) != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001159 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001160 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001161 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001162 }
David Benjamin46062682014-07-14 19:14:32 -04001163
David Benjamin8f94c312017-08-01 17:35:55 -04001164 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001165 hs->state = state_send_client_certificate;
1166 return ssl_hs_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07001167}
Adam Langley1258b6a2014-06-20 12:00:00 -07001168
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001169static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001170 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001171
David Benjaminc11ea9422017-08-29 16:33:21 -04001172 // The peer didn't request a certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001173 if (!hs->cert_request) {
1174 hs->state = state_send_client_key_exchange;
1175 return ssl_hs_ok;
1176 }
1177
David Benjaminc11ea9422017-08-29 16:33:21 -04001178 // Call cert_cb to update the certificate.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001179 if (ssl->cert->cert_cb != NULL) {
1180 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1181 if (rv == 0) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001182 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001183 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1184 return ssl_hs_error;
1185 }
1186 if (rv < 0) {
1187 hs->state = state_send_client_certificate;
1188 return ssl_hs_x509_lookup;
David Benjamin9f1dc822016-06-07 17:03:46 -04001189 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001190 }
1191
David Benjamin5edfc8c2016-12-10 15:46:58 -05001192 if (!ssl_has_certificate(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001193 // Without a client certificate, the handshake buffer may be released.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001194 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001195
David Benjaminc11ea9422017-08-29 16:33:21 -04001196 // In SSL 3.0, the Certificate message is replaced with a warning alert.
David Benjamin5edfc8c2016-12-10 15:46:58 -05001197 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001198 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1199 SSL_AD_NO_CERTIFICATE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001200 return ssl_hs_error;
David Benjamindaf207a2017-01-03 18:37:41 -05001201 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001202 hs->state = state_send_client_key_exchange;
1203 return ssl_hs_ok;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001204 }
1205 }
1206
David Benjamina232a712017-03-30 15:51:53 -05001207 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001208 !ssl3_output_cert_chain(ssl)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001209 return ssl_hs_error;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001210 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001211
1212
1213 hs->state = state_send_client_key_exchange;
1214 return ssl_hs_ok;
David Benjamin9f1dc822016-06-07 17:03:46 -04001215}
1216
David Benjamina3d76d02017-07-14 19:36:07 -04001217static_assert(sizeof(size_t) >= sizeof(unsigned),
1218 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001219
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001220static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001221 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001222 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001223 CBB body;
1224 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001225 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001226 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001227 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001228
David Benjamin499742c2017-07-22 12:45:38 -04001229 Array<uint8_t> pms;
David Benjamin45738dd2017-02-09 20:01:26 -05001230 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1231 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001232
David Benjaminc11ea9422017-08-29 16:33:21 -04001233 // If using a PSK key exchange, prepare the pre-shared key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001234 unsigned psk_len = 0;
1235 uint8_t psk[PSK_MAX_PSK_LEN];
1236 if (alg_a & SSL_aPSK) {
1237 if (ssl->psk_client_callback == NULL) {
1238 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
David Benjamin499742c2017-07-22 12:45:38 -04001239 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001240 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001241
David Benjaminae0eaaa2015-12-17 02:01:25 -05001242 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001243 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001244 psk_len =
David Benjamin31b0c9b2017-07-20 14:49:15 -04001245 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
1246 identity, sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001247 if (psk_len == 0) {
1248 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1249 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001250 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001251 }
1252 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001253
David Benjamin45738dd2017-02-09 20:01:26 -05001254 OPENSSL_free(hs->new_session->psk_identity);
1255 hs->new_session->psk_identity = BUF_strdup(identity);
1256 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001257 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001258 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001259 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001260
David Benjaminc11ea9422017-08-29 16:33:21 -04001261 // Write out psk_identity.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001262 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001263 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001264 !CBB_add_bytes(&child, (const uint8_t *)identity,
1265 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001266 !CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001267 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001268 }
1269 }
David Benjamin758d1272015-11-20 17:47:25 -05001270
David Benjamin499742c2017-07-22 12:45:38 -04001271 // Depending on the key exchange method, compute |pms|.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001272 if (alg_k & SSL_kRSA) {
David Benjamin499742c2017-07-22 12:45:38 -04001273 if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
1274 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001275 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001276
David Benjamin31b0c9b2017-07-20 14:49:15 -04001277 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001278 if (rsa == NULL) {
1279 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001280 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001281 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001282
David Benjaminf04c2e92016-12-06 13:35:25 -05001283 pms[0] = hs->client_version >> 8;
1284 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001285 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
David Benjamin499742c2017-07-22 12:45:38 -04001286 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001287 }
1288
David Benjamin75836432016-06-17 18:48:29 -04001289 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001290 size_t enc_pms_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001291 // In TLS, there is a length prefix.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001292 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001293 if (!CBB_add_u16_length_prefixed(&body, &child)) {
David Benjamin499742c2017-07-22 12:45:38 -04001294 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001295 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001296 enc_pms = &child;
1297 }
Adam Langley1bea1732014-12-17 19:06:57 -08001298
David Benjaminae0eaaa2015-12-17 02:01:25 -05001299 uint8_t *ptr;
1300 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
David Benjamin499742c2017-07-22 12:45:38 -04001301 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
1302 pms.size(), RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001303 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001304 !CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001305 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001306 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001307 } else if (alg_k & SSL_kECDHE) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001308 // Generate a keypair and serialize the public half.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001309 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001310 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamin499742c2017-07-22 12:45:38 -04001311 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001312 }
1313
David Benjaminc11ea9422017-08-29 16:33:21 -04001314 // Compute the premaster.
Adam Langleyc68e5b92017-02-08 13:33:15 -08001315 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin499742c2017-07-22 12:45:38 -04001316 if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
David Benjamin4298d772015-12-19 00:18:25 -05001317 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin499742c2017-07-22 12:45:38 -04001318 return ssl_hs_error;
David Benjamin4298d772015-12-19 00:18:25 -05001319 }
David Benjamin75836432016-06-17 18:48:29 -04001320 if (!CBB_flush(&body)) {
David Benjamin499742c2017-07-22 12:45:38 -04001321 return ssl_hs_error;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001322 }
David Benjamin4298d772015-12-19 00:18:25 -05001323
David Benjaminc11ea9422017-08-29 16:33:21 -04001324 // The key exchange state may now be discarded.
David Benjaminc642aca2017-07-19 23:28:43 -04001325 hs->key_share.reset();
David Benjamin499742c2017-07-22 12:45:38 -04001326 hs->peer_key.Reset();
David Benjaminae0eaaa2015-12-17 02:01:25 -05001327 } else if (alg_k & SSL_kPSK) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001328 // For plain PSK, other_secret is a block of 0s with the same length as
1329 // the pre-shared key.
David Benjamin499742c2017-07-22 12:45:38 -04001330 if (!pms.Init(psk_len)) {
1331 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001332 }
David Benjamin499742c2017-07-22 12:45:38 -04001333 OPENSSL_memset(pms.data(), 0, pms.size());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001334 } else {
1335 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1336 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin499742c2017-07-22 12:45:38 -04001337 return ssl_hs_error;
Adam Langley1bea1732014-12-17 19:06:57 -08001338 }
1339
David Benjaminc11ea9422017-08-29 16:33:21 -04001340 // For a PSK cipher suite, other_secret is combined with the pre-shared
1341 // key.
David Benjaminae0eaaa2015-12-17 02:01:25 -05001342 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001343 ScopedCBB pms_cbb;
1344 CBB child;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001345 uint8_t *new_pms;
1346 size_t new_pms_len;
1347
David Benjamin499742c2017-07-22 12:45:38 -04001348 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001349 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjamin499742c2017-07-22 12:45:38 -04001350 !CBB_add_bytes(&child, pms.data(), pms.size()) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001351 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001352 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001353 !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001354 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin499742c2017-07-22 12:45:38 -04001355 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001356 }
David Benjamin499742c2017-07-22 12:45:38 -04001357 pms.Reset(new_pms, new_pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001358 }
1359
David Benjaminc11ea9422017-08-29 16:33:21 -04001360 // The message must be added to the finished hash before calculating the
1361 // master secret.
David Benjamind781fc42017-07-12 16:25:57 -04001362 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin499742c2017-07-22 12:45:38 -04001363 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001364 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001365
David Benjamin45738dd2017-02-09 20:01:26 -05001366 hs->new_session->master_key_length = tls1_generate_master_secret(
David Benjamin499742c2017-07-22 12:45:38 -04001367 hs, hs->new_session->master_key, pms.data(), pms.size());
David Benjamin45738dd2017-02-09 20:01:26 -05001368 if (hs->new_session->master_key_length == 0) {
David Benjamin499742c2017-07-22 12:45:38 -04001369 return ssl_hs_error;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001370 }
David Benjaminfc02b592017-02-17 16:26:01 -05001371 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001372
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001373 hs->state = state_send_client_certificate_verify;
1374 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001375}
Adam Langley1258b6a2014-06-20 12:00:00 -07001376
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001377static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001378 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001379
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001380 if (!hs->cert_request || !ssl_has_certificate(ssl)) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001381 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001382 return ssl_hs_ok;
1383 }
1384
1385 assert(ssl_has_private_key(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -04001386 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001387 CBB body, child;
1388 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001389 SSL3_MT_CERTIFICATE_VERIFY)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001390 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001391 }
1392
David Benjaminea9a0d52016-07-08 15:52:59 -07001393 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001394 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001395 return ssl_hs_error;
David Benjaminea9a0d52016-07-08 15:52:59 -07001396 }
David Benjamin429fdc02016-06-17 17:33:30 -04001397 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001398 // Write out the digest type in TLS 1.2.
Steven Valdezf0451ca2016-06-29 13:16:27 -04001399 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001400 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001401 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001402 }
1403 }
David Benjaminfc825122015-12-18 01:57:43 -05001404
David Benjaminc11ea9422017-08-29 16:33:21 -04001405 // Set aside space for the signature.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001406 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001407 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001408 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001409 !CBB_reserve(&child, &ptr, max_sig_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001410 return ssl_hs_error;
David Benjamin429fdc02016-06-17 17:33:30 -04001411 }
1412
Steven Valdez2b8415e2016-06-30 13:27:23 -04001413 size_t sig_len = max_sig_len;
David Benjaminc11ea9422017-08-29 16:33:21 -04001414 // The SSL3 construction for CertificateVerify does not decompose into a
1415 // single final digest and signature, and must be special-cased.
David Benjamin44148742017-06-17 13:20:59 -04001416 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1417 if (ssl->cert->key_method != NULL) {
1418 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001419 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001420 }
1421
David Benjamin44148742017-06-17 13:20:59 -04001422 uint8_t digest[EVP_MAX_MD_SIZE];
1423 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001424 if (!hs->transcript.GetSSL3CertVerifyHash(
1425 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001426 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001427 }
David Benjaminfc825122015-12-18 01:57:43 -05001428
David Benjamin1386aad2017-07-19 23:57:40 -04001429 UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
1430 if (!pctx ||
1431 !EVP_PKEY_sign_init(pctx.get()) ||
1432 !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001433 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001434 }
1435 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001436 switch (ssl_private_key_sign(
1437 hs, ptr, &sig_len, max_sig_len, signature_algorithm,
1438 hs->transcript.buffer_data(), hs->transcript.buffer_len())) {
David Benjamin44148742017-06-17 13:20:59 -04001439 case ssl_private_key_success:
1440 break;
1441 case ssl_private_key_failure:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001442 return ssl_hs_error;
David Benjamin44148742017-06-17 13:20:59 -04001443 case ssl_private_key_retry:
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001444 hs->state = state_send_client_certificate_verify;
1445 return ssl_hs_private_key_operation;
David Benjamin44148742017-06-17 13:20:59 -04001446 }
David Benjaminfc825122015-12-18 01:57:43 -05001447 }
1448
David Benjaminfc825122015-12-18 01:57:43 -05001449 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001450 !ssl_add_message_cbb(ssl, cbb.get())) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001451 return ssl_hs_error;
David Benjaminfc825122015-12-18 01:57:43 -05001452 }
1453
David Benjaminc11ea9422017-08-29 16:33:21 -04001454 // The handshake buffer is no longer necessary.
David Benjamin6dc8bf62017-07-19 16:38:21 -04001455 hs->transcript.FreeBuffer();
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001456
David Benjamin4cbb9312017-08-31 14:52:31 -04001457 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001458 return ssl_hs_ok;
Adam Langley1bea1732014-12-17 19:06:57 -08001459}
1460
David Benjamin4cbb9312017-08-31 14:52:31 -04001461static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001462 SSL *const ssl = hs->ssl;
David Benjamin4cbb9312017-08-31 14:52:31 -04001463 // Resolve Channel ID first, before any non-idempotent operations.
1464 if (ssl->s3->tlsext_channel_id_valid) {
1465 if (!ssl_do_channel_id_callback(ssl)) {
1466 return ssl_hs_error;
1467 }
1468
1469 if (ssl->tlsext_channel_id_private == NULL) {
1470 hs->state = state_send_client_finished;
1471 return ssl_hs_channel_id_lookup;
1472 }
1473 }
David Benjamine97b9622015-10-10 14:59:09 -04001474
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001475 if (!ssl->method->add_change_cipher_spec(ssl) ||
David Benjamine58f8a62017-09-21 19:07:15 -04001476 !tls1_change_cipher_state(hs, evp_aead_seal)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001477 return ssl_hs_error;
David Benjamine97b9622015-10-10 14:59:09 -04001478 }
1479
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001480 if (hs->next_proto_neg_seen) {
1481 static const uint8_t kZero[32] = {0};
1482 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
1483
1484 ScopedCBB cbb;
1485 CBB body, child;
1486 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1487 !CBB_add_u8_length_prefixed(&body, &child) ||
1488 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1489 ssl->s3->next_proto_negotiated_len) ||
1490 !CBB_add_u8_length_prefixed(&body, &child) ||
1491 !CBB_add_bytes(&child, kZero, padding_len) ||
1492 !ssl_add_message_cbb(ssl, cbb.get())) {
1493 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1494 return ssl_hs_error;
1495 }
1496 }
1497
David Benjamin4cbb9312017-08-31 14:52:31 -04001498 if (ssl->s3->tlsext_channel_id_valid) {
1499 ScopedCBB cbb;
1500 CBB body;
1501 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1502 !tls1_write_channel_id(hs, &body) ||
1503 !ssl_add_message_cbb(ssl, cbb.get())) {
1504 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1505 return ssl_hs_error;
1506 }
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001507 }
1508
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001509 if (!ssl3_send_finished(hs)) {
1510 return ssl_hs_error;
1511 }
1512
1513 hs->state = state_finish_flight;
1514 return ssl_hs_flush;
1515}
1516
1517static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001518 SSL *const ssl = hs->ssl;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001519 if (ssl->session != NULL) {
1520 hs->state = state_finish_client_handshake;
1521 return ssl_hs_ok;
1522 }
1523
David Benjaminc11ea9422017-08-29 16:33:21 -04001524 // This is a full handshake. If it involves ChannelID, then record the
1525 // handshake hashes at this point in the session so that any resumption of
1526 // this session with ChannelID can sign those hashes.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001527 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1528 return ssl_hs_error;
1529 }
1530
1531 hs->state = state_read_session_ticket;
1532
1533 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1534 ssl3_can_false_start(ssl) &&
David Benjaminc11ea9422017-08-29 16:33:21 -04001535 // No False Start on renegotiation (would complicate the state machine).
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001536 !ssl->s3->initial_handshake_complete) {
David Benjaminfd45ee72017-08-31 14:49:09 -04001537 hs->in_false_start = true;
1538 hs->can_early_write = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001539 return ssl_hs_early_return;
1540 }
1541
1542 return ssl_hs_ok;
1543}
1544
1545static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1546 SSL *const ssl = hs->ssl;
1547
1548 if (!hs->ticket_expected) {
1549 hs->state = state_process_change_cipher_spec;
1550 return ssl_hs_read_change_cipher_spec;
1551 }
1552
David Benjamin7934f082017-08-01 16:32:25 -04001553 SSLMessage msg;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001554 if (!ssl->method->get_message(ssl, &msg)) {
1555 return ssl_hs_read_message;
Adam Langley1bea1732014-12-17 19:06:57 -08001556 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001557
David Benjamin7934f082017-08-01 16:32:25 -04001558 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1559 !ssl_hash_message(hs, msg)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001560 return ssl_hs_error;
David Benjamin276b7e82017-01-21 14:13:39 -05001561 }
1562
David Benjamin7934f082017-08-01 16:32:25 -04001563 CBS new_session_ticket = msg.body, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001564 uint32_t tlsext_tick_lifetime_hint;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001565 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001566 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1567 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001568 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001569 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001570 return ssl_hs_error;
David Benjamin454aa4c2016-03-10 08:43:27 -05001571 }
1572
David Benjamin9f1dc822016-06-07 17:03:46 -04001573 if (CBS_len(&ticket) == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001574 // RFC 5077 allows a server to change its mind and send no ticket after
1575 // negotiating the extension. The value of |ticket_expected| is checked in
1576 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
David Benjaminfd45ee72017-08-31 14:49:09 -04001577 hs->ticket_expected = false;
David Benjamin8f94c312017-08-01 17:35:55 -04001578 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001579 hs->state = state_process_change_cipher_spec;
1580 return ssl_hs_read_change_cipher_spec;
David Benjamin9f1dc822016-06-07 17:03:46 -04001581 }
1582
David Benjamin31b0c9b2017-07-20 14:49:15 -04001583 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001584 UniquePtr<SSL_SESSION> renewed_session;
1585 if (ssl->session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001586 // The server is sending a new ticket for an existing session. Sessions are
1587 // immutable once established, so duplicate all but the ticket of the
1588 // existing session.
David Benjamin31b0c9b2017-07-20 14:49:15 -04001589 renewed_session =
1590 SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001591 if (!renewed_session) {
David Benjaminc11ea9422017-08-29 16:33:21 -04001592 // This should never happen.
David Benjamin9f1dc822016-06-07 17:03:46 -04001593 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001594 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001595 }
David Benjamin1386aad2017-07-19 23:57:40 -04001596 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001597 }
1598
David Benjaminc11ea9422017-08-29 16:33:21 -04001599 // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued.
David Benjamin17b30832017-01-28 14:00:32 -05001600 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001601
Steven Valdez87eab492016-06-27 16:34:59 -04001602 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001603 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001604 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001605 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001606 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001607
David Benjaminc11ea9422017-08-29 16:33:21 -04001608 // Generate a session ID for this session based on the session ticket. We use
1609 // the session ID mechanism for detecting ticket resumption. This also fits in
1610 // with assumptions elsewhere in OpenSSL.
Steven Valdez87eab492016-06-27 16:34:59 -04001611 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1612 session->session_id, &session->session_id_length,
1613 EVP_sha256(), NULL)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001614 return ssl_hs_error;
David Benjamin9f1dc822016-06-07 17:03:46 -04001615 }
1616
David Benjamin1386aad2017-07-19 23:57:40 -04001617 if (renewed_session) {
Steven Valdez87eab492016-06-27 16:34:59 -04001618 session->not_resumable = 0;
1619 SSL_SESSION_free(ssl->session);
David Benjamin1386aad2017-07-19 23:57:40 -04001620 ssl->session = renewed_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -04001621 }
1622
David Benjamin8f94c312017-08-01 17:35:55 -04001623 ssl->method->next_message(ssl);
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001624 hs->state = state_process_change_cipher_spec;
1625 return ssl_hs_read_change_cipher_spec;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001626}
David Benjamin86e95b82017-07-18 16:34:25 -04001627
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001628static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
David Benjamine58f8a62017-09-21 19:07:15 -04001629 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001630 return ssl_hs_error;
1631 }
1632
1633 hs->state = state_read_server_finished;
1634 return ssl_hs_ok;
1635}
1636
1637static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1638 SSL *const ssl = hs->ssl;
1639 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1640 if (wait != ssl_hs_ok) {
1641 return wait;
1642 }
1643
1644 if (ssl->session != NULL) {
David Benjamin4cbb9312017-08-31 14:52:31 -04001645 hs->state = state_send_client_finished;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001646 return ssl_hs_ok;
1647 }
1648
1649 hs->state = state_finish_client_handshake;
1650 return ssl_hs_ok;
1651}
1652
1653static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1654 SSL *const ssl = hs->ssl;
1655
1656 ssl->method->on_handshake_complete(ssl);
1657
1658 SSL_SESSION_free(ssl->s3->established_session);
1659 if (ssl->session != NULL) {
1660 SSL_SESSION_up_ref(ssl->session);
1661 ssl->s3->established_session = ssl->session;
1662 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -04001663 // We make a copy of the session in order to maintain the immutability
1664 // of the new established_session due to False Start. The caller may
1665 // have taken a reference to the temporary session.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001666 ssl->s3->established_session =
1667 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL)
1668 .release();
1669 if (ssl->s3->established_session == NULL) {
1670 return ssl_hs_error;
1671 }
David Benjaminc11ea9422017-08-29 16:33:21 -04001672 // Renegotiations do not participate in session resumption.
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001673 if (!ssl->s3->initial_handshake_complete) {
1674 ssl->s3->established_session->not_resumable = 0;
1675 }
1676
1677 hs->new_session.reset();
1678 }
1679
David Benjaminfd45ee72017-08-31 14:49:09 -04001680 hs->handshake_finalized = true;
David Benjamin046bc1f2017-08-31 15:06:42 -04001681 ssl->s3->initial_handshake_complete = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001682 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
1683
1684 hs->state = state_done;
1685 return ssl_hs_ok;
1686}
1687
1688enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1689 while (hs->state != state_done) {
1690 enum ssl_hs_wait_t ret = ssl_hs_error;
1691 enum ssl_client_hs_state_t state =
1692 static_cast<enum ssl_client_hs_state_t>(hs->state);
1693 switch (state) {
1694 case state_start_connect:
1695 ret = do_start_connect(hs);
1696 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001697 case state_enter_early_data:
1698 ret = do_enter_early_data(hs);
1699 break;
1700 case state_read_hello_verify_request:
1701 ret = do_read_hello_verify_request(hs);
1702 break;
1703 case state_read_server_hello:
1704 ret = do_read_server_hello(hs);
1705 break;
1706 case state_tls13:
1707 ret = do_tls13(hs);
1708 break;
1709 case state_read_server_certificate:
1710 ret = do_read_server_certificate(hs);
1711 break;
1712 case state_read_certificate_status:
1713 ret = do_read_certificate_status(hs);
1714 break;
1715 case state_verify_server_certificate:
1716 ret = do_verify_server_certificate(hs);
1717 break;
1718 case state_read_server_key_exchange:
1719 ret = do_read_server_key_exchange(hs);
1720 break;
1721 case state_read_certificate_request:
1722 ret = do_read_certificate_request(hs);
1723 break;
1724 case state_read_server_hello_done:
1725 ret = do_read_server_hello_done(hs);
1726 break;
1727 case state_send_client_certificate:
1728 ret = do_send_client_certificate(hs);
1729 break;
1730 case state_send_client_key_exchange:
1731 ret = do_send_client_key_exchange(hs);
1732 break;
1733 case state_send_client_certificate_verify:
1734 ret = do_send_client_certificate_verify(hs);
1735 break;
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001736 case state_send_client_finished:
1737 ret = do_send_client_finished(hs);
1738 break;
1739 case state_finish_flight:
1740 ret = do_finish_flight(hs);
1741 break;
1742 case state_read_session_ticket:
1743 ret = do_read_session_ticket(hs);
1744 break;
1745 case state_process_change_cipher_spec:
1746 ret = do_process_change_cipher_spec(hs);
1747 break;
1748 case state_read_server_finished:
1749 ret = do_read_server_finished(hs);
1750 break;
1751 case state_finish_client_handshake:
1752 ret = do_finish_client_handshake(hs);
1753 break;
1754 case state_done:
1755 ret = ssl_hs_ok;
1756 break;
1757 }
1758
1759 if (hs->state != state) {
1760 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1761 }
1762
1763 if (ret != ssl_hs_ok) {
1764 return ret;
1765 }
1766 }
1767
1768 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1769 return ssl_hs_ok;
1770}
1771
1772const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1773 enum ssl_client_hs_state_t state =
1774 static_cast<enum ssl_client_hs_state_t>(hs->state);
1775 switch (state) {
1776 case state_start_connect:
1777 return "TLS client start_connect";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001778 case state_enter_early_data:
1779 return "TLS client enter_early_data";
1780 case state_read_hello_verify_request:
1781 return "TLS client read_hello_verify_request";
1782 case state_read_server_hello:
1783 return "TLS client read_server_hello";
1784 case state_tls13:
1785 return tls13_client_handshake_state(hs);
1786 case state_read_server_certificate:
1787 return "TLS client read_server_certificate";
1788 case state_read_certificate_status:
1789 return "TLS client read_certificate_status";
1790 case state_verify_server_certificate:
1791 return "TLS client verify_server_certificate";
1792 case state_read_server_key_exchange:
1793 return "TLS client read_server_key_exchange";
1794 case state_read_certificate_request:
1795 return "TLS client read_certificate_request";
1796 case state_read_server_hello_done:
1797 return "TLS client read_server_hello_done";
1798 case state_send_client_certificate:
1799 return "TLS client send_client_certificate";
1800 case state_send_client_key_exchange:
1801 return "TLS client send_client_key_exchange";
1802 case state_send_client_certificate_verify:
1803 return "TLS client send_client_certificate_verify";
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001804 case state_send_client_finished:
1805 return "TLS client send_client_finished";
1806 case state_finish_flight:
1807 return "TLS client finish_flight";
1808 case state_read_session_ticket:
1809 return "TLS client read_session_ticket";
1810 case state_process_change_cipher_spec:
1811 return "TLS client process_change_cipher_spec";
1812 case state_read_server_finished:
1813 return "TLS client read_server_finished";
1814 case state_finish_client_handshake:
1815 return "TLS client finish_client_handshake";
1816 case state_done:
1817 return "TLS client done";
1818 }
1819
1820 return "TLS client unknown";
1821}
1822
1823}