blob: e60b6ae6cef811e864394b308418de7f81dd5933 [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.
7 *
8 * 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).
14 *
15 * 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.
21 *
22 * 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 :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * 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.
51 *
52 * 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
65 * notice, this list of conditions and the following disclaimer.
66 *
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 *
113 * Portions of the attached software ("Contribution") are developed by
114 * 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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjaminabbbee12016-10-31 19:20:42 -0400155#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400156#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400158#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400159#include <openssl/ec_key.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400164#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400165#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin17cf2cb2016-12-13 01:07:13 -0500167#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400168#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjaminc3c88822016-11-14 10:32:04 +0900171static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
David Benjamin8d606e32017-06-15 22:43:04 -0400172static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900173static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
174static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
175static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900176static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
177static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
178static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
179static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
180static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
181static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
182static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
183static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
184static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400185
David Benjamince8c9d22016-11-14 10:45:16 +0900186int ssl3_connect(SSL_HANDSHAKE *hs) {
187 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800188 int ret = -1;
David Benjaminbeb47022014-11-30 02:58:52 -0500189
David Benjamin0d56f882015-12-19 17:05:56 -0500190 assert(ssl->handshake_func == ssl3_connect);
191 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langley1bea1732014-12-17 19:06:57 -0800193 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500194 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamincb0c29f2016-12-12 17:00:50 -0500196 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400197 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400198 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500199 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800200 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley1bea1732014-12-17 19:06:57 -0800202 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900203 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800204 if (ret <= 0) {
205 goto end;
206 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400207
David Benjamince079fd2016-08-02 16:22:34 -0400208 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
Steven Valdez2d850622017-01-11 11:34:52 -0500209 if (hs->early_data_offered) {
210 if (!tls13_init_early_key_schedule(hs) ||
211 !tls13_advance_key_schedule(hs, ssl->session->master_key,
212 ssl->session->master_key_length) ||
213 !tls13_derive_early_secrets(hs) ||
214 !tls13_set_traffic_key(ssl, evp_aead_seal,
215 hs->early_traffic_secret,
216 hs->hash_len)) {
217 ret = -1;
218 goto end;
219 }
Steven Valdeze831a812017-03-09 14:56:07 -0500220 hs->next_state = SSL3_ST_WRITE_EARLY_DATA;
221 } else {
222 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
Steven Valdez2d850622017-01-11 11:34:52 -0500223 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400224 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500225 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400226 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500227 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800228 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700229
David Benjaminaa7734b2016-06-07 16:40:46 -0400230 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400231 assert(SSL_is_dtls(ssl));
David Benjamin8d606e32017-06-15 22:43:04 -0400232 ret = dtls1_get_hello_verify_request(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400233 if (ret <= 0) {
234 goto end;
235 }
236 if (ssl->d1->send_cookie) {
237 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500238 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400239 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500240 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400241 }
242 break;
243
Steven Valdeze831a812017-03-09 14:56:07 -0500244 case SSL3_ST_WRITE_EARLY_DATA:
245 /* Stash the early data session, so connection properties may be queried
246 * out of it. */
247 hs->in_early_data = 1;
248 hs->early_session = ssl->session;
249 SSL_SESSION_up_ref(ssl->session);
250
251 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
252 hs->can_early_write = 1;
253 ret = 1;
254 goto end;
255
Adam Langley1bea1732014-12-17 19:06:57 -0800256 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900257 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500258 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400259 break;
260 }
Adam Langley1bea1732014-12-17 19:06:57 -0800261 if (ret <= 0) {
262 goto end;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Steven Valdez87eab492016-06-27 16:34:59 -0400265 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500266 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800267 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500268 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800269 }
Adam Langley1bea1732014-12-17 19:06:57 -0800270 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400271
Adam Langley1bea1732014-12-17 19:06:57 -0800272 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500273 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900274 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800275 if (ret <= 0) {
276 goto end;
277 }
Adam Langley1bea1732014-12-17 19:06:57 -0800278 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500279 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800280 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
David Benjamin9f1dc822016-06-07 17:03:46 -0400282 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900283 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900284 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400285 if (ret <= 0) {
286 goto end;
287 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400288 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500289 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400290 break;
291
Paul Lietar8f1c2682015-08-18 12:21:54 +0100292 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500293 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin3a1dd462017-07-11 16:13:10 -0400294 switch (ssl_verify_peer_cert(hs)) {
295 case ssl_verify_ok:
296 break;
297 case ssl_verify_invalid:
298 ret = -1;
299 goto end;
300 case ssl_verify_retry:
301 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
302 ret = -1;
303 goto end;
David Benjamin47749a62016-06-13 13:56:33 -0400304 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100305 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500306 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100307 break;
308
Adam Langley1bea1732014-12-17 19:06:57 -0800309 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900310 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800311 if (ret <= 0) {
312 goto end;
313 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500314 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800315 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langley1bea1732014-12-17 19:06:57 -0800317 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500318 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900319 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400320 if (ret <= 0) {
321 goto end;
322 }
Adam Langley1bea1732014-12-17 19:06:57 -0800323 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500324 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley1bea1732014-12-17 19:06:57 -0800327 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900328 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800329 if (ret <= 0) {
330 goto end;
331 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400332 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500333 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800334 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700335
Adam Langley1bea1732014-12-17 19:06:57 -0800336 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900337 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900338 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400339 if (ret <= 0) {
340 goto end;
341 }
Adam Langley1bea1732014-12-17 19:06:57 -0800342 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500343 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800344 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langley1bea1732014-12-17 19:06:57 -0800346 case SSL3_ST_CW_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900347 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800348 if (ret <= 0) {
349 goto end;
350 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500351 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800352 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langley1bea1732014-12-17 19:06:57 -0800354 case SSL3_ST_CW_CERT_VRFY_A:
David Benjamina660e7a2017-01-02 08:15:35 -0500355 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900356 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400357 if (ret <= 0) {
358 goto end;
359 }
Adam Langley1bea1732014-12-17 19:06:57 -0800360 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500361 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800362 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
David Benjamin352d0a92016-06-28 11:22:02 -0400364 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500365 if (!ssl->method->add_change_cipher_spec(ssl) ||
366 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800367 ret = -1;
368 goto end;
369 }
Adam Langley95c29f32014-06-20 12:00:00 -0700370
David Benjamindaf207a2017-01-03 18:37:41 -0500371 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800372 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langley1bea1732014-12-17 19:06:57 -0800374 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900375 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900376 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400377 if (ret <= 0) {
378 goto end;
379 }
Adam Langley1bea1732014-12-17 19:06:57 -0800380 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500381 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800382 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langley1bea1732014-12-17 19:06:57 -0800384 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400385 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900386 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400387 if (ret <= 0) {
388 goto end;
389 }
Adam Langley1bea1732014-12-17 19:06:57 -0800390 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500391 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800392 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langley1bea1732014-12-17 19:06:57 -0800394 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500395 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800396 if (ret <= 0) {
397 goto end;
398 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500399 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Steven Valdez87eab492016-06-27 16:34:59 -0400401 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500402 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800403 } else {
404 /* This is a non-resumption handshake. If it involves ChannelID, then
405 * record the handshake hashes at this point in the session so that
406 * any resumption of this session with ChannelID can sign those
407 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500408 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700409 if (ret <= 0) {
410 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800411 }
David Benjamin0d56f882015-12-19 17:05:56 -0500412 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
413 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500414 /* No False Start on renegotiation (would complicate the state
415 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500416 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500417 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800418 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500419 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800420 }
421 }
Adam Langley1bea1732014-12-17 19:06:57 -0800422 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjamin9f1dc822016-06-07 17:03:46 -0400424 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500425 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900426 hs->in_false_start = 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500427 hs->can_early_write = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400428 ret = 1;
429 goto end;
430
Adam Langley1bea1732014-12-17 19:06:57 -0800431 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900432 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900433 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400434 if (ret <= 0) {
435 goto end;
436 }
Adam Langley1bea1732014-12-17 19:06:57 -0800437 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500438 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800439 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langley1bea1732014-12-17 19:06:57 -0800441 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400442 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500443 if (ret <= 0) {
444 goto end;
445 }
446
David Benjamin67739722016-11-17 17:03:59 +0900447 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800448 ret = -1;
449 goto end;
450 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500451 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800452 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langley1bea1732014-12-17 19:06:57 -0800454 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900455 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800456 if (ret <= 0) {
457 goto end;
458 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400459 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Steven Valdez87eab492016-06-27 16:34:59 -0400461 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500462 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800463 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500464 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800465 }
Adam Langley1bea1732014-12-17 19:06:57 -0800466 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Adam Langley1bea1732014-12-17 19:06:57 -0800468 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500469 ret = ssl->method->flush_flight(ssl);
470 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800471 goto end;
472 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500473 hs->state = hs->next_state;
David Benjamin77458a42017-02-10 23:33:27 -0500474 if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400475 ssl->method->expect_flight(ssl);
476 }
Adam Langley1bea1732014-12-17 19:06:57 -0800477 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700478
Steven Valdez681eb6a2016-12-19 13:19:29 -0500479 case SSL_ST_TLS13: {
480 int early_return = 0;
481 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400482 if (ret <= 0) {
483 goto end;
484 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500485
486 if (early_return) {
487 ret = 1;
488 goto end;
489 }
490
David Benjamin77458a42017-02-10 23:33:27 -0500491 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400492 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500493 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400494
David Benjamin77458a42017-02-10 23:33:27 -0500495 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin4497e582016-07-27 17:51:49 -0400496 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Steven Valdez87eab492016-06-27 16:34:59 -0400498 SSL_SESSION_free(ssl->s3->established_session);
499 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400500 SSL_SESSION_up_ref(ssl->session);
501 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400502 } else {
503 /* We make a copy of the session in order to maintain the immutability
504 * of the new established_session due to False Start. The caller may
505 * have taken a reference to the temporary session. */
506 ssl->s3->established_session =
David Benjamin45738dd2017-02-09 20:01:26 -0500507 SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400508 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400509 ret = -1;
510 goto end;
511 }
512 ssl->s3->established_session->not_resumable = 0;
513
David Benjamin45738dd2017-02-09 20:01:26 -0500514 SSL_SESSION_free(hs->new_session);
515 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400516 }
517
David Benjamin77458a42017-02-10 23:33:27 -0500518 hs->state = SSL_ST_OK;
519 break;
520
521 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500522 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500523 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400524 if (is_initial_handshake) {
525 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900526 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400527 }
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langley1bea1732014-12-17 19:06:57 -0800529 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400530 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800531 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500532 }
Steven Valdez87eab492016-06-27 16:34:59 -0400533
Adam Langley1bea1732014-12-17 19:06:57 -0800534 default:
David Benjamin3570d732015-06-29 00:28:17 -0400535 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800536 ret = -1;
537 goto end;
538 }
539
David Benjamin16b1b1d2017-02-10 23:41:14 -0500540 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400541 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800542 }
Adam Langley1bea1732014-12-17 19:06:57 -0800543 }
544
Adam Langley95c29f32014-06-20 12:00:00 -0700545end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400546 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800547 return ret;
548}
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamin65ac9972016-09-02 21:35:25 -0400550uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
551 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
552 * a single byte repeatedly and ensures the values are deterministic. This
553 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
554 * same group be advertised in both supported_groups and key_shares. */
555 uint16_t ret = ssl->s3->client_random[index];
556 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
557 ret = (ret & 0xf0) | 0x0a;
558 ret |= ret << 8;
559 return ret;
560}
561
David Benjamin2fe6e222017-01-25 02:18:43 -0500562/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
563 * disabled algorithms. */
564static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
565 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500566 *out_mask_a = 0;
567 *out_mask_k = 0;
568
David Benjamin2fe6e222017-01-25 02:18:43 -0500569 /* PSK requires a client callback. */
570 if (ssl->psk_client_callback == NULL) {
571 *out_mask_a |= SSL_aPSK;
572 *out_mask_k |= SSL_kPSK;
573 }
574}
575
David Benjamin68161cb2017-06-20 14:49:43 -0400576static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
577 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500578 uint32_t mask_a, mask_k;
579 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700580
David Benjamine8d53502015-10-10 14:13:23 -0400581 CBB child;
582 if (!CBB_add_u16_length_prefixed(out, &child)) {
583 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800584 }
585
David Benjamin65ac9972016-09-02 21:35:25 -0400586 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
587 if (ssl->ctx->grease_enabled &&
588 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
589 return 0;
590 }
591
David Benjaminabbbee12016-10-31 19:20:42 -0400592 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
593 * hardware support. */
David Benjamin68161cb2017-06-20 14:49:43 -0400594 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400595 if (!EVP_has_aes_hardware() &&
596 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
597 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500598 }
David Benjaminabbbee12016-10-31 19:20:42 -0400599 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
600 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
601 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400602 }
David Benjaminabbbee12016-10-31 19:20:42 -0400603 if (EVP_has_aes_hardware() &&
604 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400605 return 0;
606 }
607 }
608
David Benjamin68161cb2017-06-20 14:49:43 -0400609 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400610 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
611 int any_enabled = 0;
612 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
613 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
614 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500615 if ((cipher->algorithm_mkey & mask_k) ||
616 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400617 continue;
618 }
David Benjamin68161cb2017-06-20 14:49:43 -0400619 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
620 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400621 continue;
622 }
623 any_enabled = 1;
624 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
625 return 0;
626 }
627 }
628
629 /* If all ciphers were disabled, return the error to the caller. */
David Benjamin68161cb2017-06-20 14:49:43 -0400630 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400631 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
632 return 0;
633 }
David Benjamine8d53502015-10-10 14:13:23 -0400634 }
635
636 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
637 * added. */
David Benjamin68161cb2017-06-20 14:49:43 -0400638 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400639 !ssl->s3->initial_handshake_complete) {
640 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
641 return 0;
642 }
David Benjamine8d53502015-10-10 14:13:23 -0400643 }
644
David Benjamine0ff7672016-09-19 18:40:03 -0400645 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200646 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
647 return 0;
648 }
David Benjamine8d53502015-10-10 14:13:23 -0400649 }
650
651 return CBB_flush(out);
652}
653
David Benjamin8c880a22016-12-03 02:20:34 -0500654int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
655 SSL *const ssl = hs->ssl;
David Benjamind781fc42017-07-12 16:25:57 -0400656 bssl::ScopedCBB cbb;
657 CBB body;
658 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
659 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400660 }
661
Steven Valdez5440fe02016-07-18 12:40:30 -0400662 /* Renegotiations do not participate in session resumption. */
Steven Valdez520e1222017-06-13 12:45:25 -0400663 int has_session_id = ssl->session != NULL &&
664 !ssl->s3->initial_handshake_complete &&
665 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400666
667 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500668 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400669 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400670 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400671 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400672 }
673
Steven Valdez520e1222017-06-13 12:45:25 -0400674 if (has_session_id) {
675 if (!CBB_add_bytes(&child, ssl->session->session_id,
676 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400677 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400678 }
679 } else {
680 /* In TLS 1.3 experimental encodings, send a fake placeholder session ID
681 * when we do not otherwise have one to send. */
682 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400683 ssl->tls13_variant == tls13_experiment &&
Steven Valdez520e1222017-06-13 12:45:25 -0400684 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400685 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400686 }
687 }
688
David Benjamince079fd2016-08-02 16:22:34 -0400689 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400690 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400691 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400692 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400693 }
694 }
695
696 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400697 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400698 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400699 !CBB_add_u8(&body, 1 /* one compression method */) ||
700 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500701 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400702 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400703 }
704
Steven Valdeza833c352016-11-01 13:39:36 -0400705 uint8_t *msg = NULL;
706 size_t len;
David Benjamind781fc42017-07-12 16:25:57 -0400707 if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) {
708 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400709 }
710
711 /* Now that the length prefixes have been computed, fill in the placeholder
712 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500713 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500714 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400715 OPENSSL_free(msg);
David Benjamind781fc42017-07-12 16:25:57 -0400716 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400717 }
718
David Benjamindaf207a2017-01-03 18:37:41 -0500719 return ssl->method->add_message(ssl, msg, len);
Steven Valdez5440fe02016-07-18 12:40:30 -0400720}
721
David Benjaminc3c88822016-11-14 10:32:04 +0900722static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
723 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400724 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
725 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
Steven Valdez908ac192017-01-12 13:17:07 -0500726 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500727 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
728 return -1;
729 }
730
David Benjamin68161cb2017-06-20 14:49:43 -0400731 /* Freeze the version range. */
732 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400733 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400734 }
735
David Benjaminf04c2e92016-12-06 13:35:25 -0500736 /* Always advertise the ClientHello version from the original maximum version,
737 * even on renegotiation. The static RSA key exchange uses this field, and
738 * some servers fail when it changes across handshakes. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400739 if (SSL_is_dtls(hs->ssl)) {
740 hs->client_version =
741 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
742 } else {
743 hs->client_version =
744 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
David Benjamine8d53502015-10-10 14:13:23 -0400745 }
746
David Benjamine29ea162016-03-23 16:10:44 -0400747 /* If the configured session has expired or was created at a disabled
748 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400749 if (ssl->session != NULL) {
Adam Langley364f7a62016-12-12 10:51:00 -0800750 if (ssl->session->is_server ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400751 !ssl_supports_version(hs, ssl->session->ssl_version) ||
752 (ssl->session->session_id_length == 0 &&
753 ssl->session->tlsext_ticklen == 0) ||
Steven Valdez4aa154e2016-07-29 14:32:55 -0400754 ssl->session->not_resumable ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400755 !ssl_session_is_time_valid(ssl, ssl->session)) {
David Benjamin4d0be242016-09-01 01:10:07 -0400756 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400757 }
David Benjamine8d53502015-10-10 14:13:23 -0400758 }
759
760 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
761 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400762 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400763 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400764 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400765 }
766
Steven Valdez0e4a4482017-07-17 11:12:34 -0400767 /* Initialize a random session ID for the experimental TLS 1.3 variant
768 * requiring a session id. */
Steven Valdezdbe01582017-07-14 10:39:28 -0400769 if (ssl->tls13_variant == tls13_experiment) {
Steven Valdez520e1222017-06-13 12:45:25 -0400770 hs->session_id_len = sizeof(hs->session_id);
771 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
772 return -1;
773 }
774 }
775
David Benjamin8c880a22016-12-03 02:20:34 -0500776 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400777 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400778 }
779
David Benjamin16315f72017-01-12 20:02:05 -0500780 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800781}
Adam Langley95c29f32014-06-20 12:00:00 -0700782
David Benjamin8d606e32017-06-15 22:43:04 -0400783static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900784 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400785 CBS hello_verify_request, cookie;
786 uint16_t server_version;
787
David Benjaminf71036e2017-01-21 14:49:39 -0500788 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700789 if (ret <= 0) {
790 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400791 }
792
793 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
794 ssl->d1->send_cookie = 0;
795 ssl->s3->tmp.reuse_message = 1;
796 return 1;
797 }
798
David Benjaminf71036e2017-01-21 14:49:39 -0500799 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400800 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
801 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
David Benjamin8d606e32017-06-15 22:43:04 -0400802 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
David Benjaminaa7734b2016-06-07 16:40:46 -0400803 CBS_len(&hello_verify_request) != 0) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400804 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400805 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
806 return -1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400807 }
808
David Benjamin17cf2cb2016-12-13 01:07:13 -0500809 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400810 ssl->d1->cookie_len = CBS_len(&cookie);
811
812 ssl->d1->send_cookie = 1;
813 return 1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400814}
815
David Benjaminb853f312017-07-14 18:40:34 -0400816static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out) {
817 SSL *const ssl = hs->ssl;
818 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
819 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
820 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
821 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
822 return 0;
823 }
824
825 CBS server_hello;
826 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
827 if (!CBS_get_u16(&server_hello, out)) {
828 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
829 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
830 return 0;
831 }
832
833 /* The server version may also be in the supported_versions extension if
834 * applicable. */
835 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO ||
836 *out != TLS1_2_VERSION) {
837 return 1;
838 }
839
840 uint8_t sid_length;
841 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
842 !CBS_get_u8(&server_hello, &sid_length) ||
843 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
844 1 /* compression_method */)) {
845 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
846 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
847 return 0;
848 }
849
850 /* The extensions block may not be present. */
851 if (CBS_len(&server_hello) == 0) {
852 return 1;
853 }
854
855 CBS extensions;
856 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
857 CBS_len(&server_hello) != 0) {
858 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
859 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
860 return 0;
861 }
862
863 int have_supported_versions;
864 CBS supported_versions;
865 const SSL_EXTENSION_TYPE ext_types[] = {
866 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
867 &supported_versions},
868 };
869
870 uint8_t alert = SSL_AD_DECODE_ERROR;
871 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
872 OPENSSL_ARRAY_SIZE(ext_types),
873 1 /* ignore unknown */)) {
874 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
875 return 0;
876 }
877
878 if (have_supported_versions &&
879 (!CBS_get_u16(&supported_versions, out) ||
880 CBS_len(&supported_versions) != 0)) {
881 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
882 return 0;
883 }
884
885 return 1;
886}
887
David Benjaminc3c88822016-11-14 10:32:04 +0900888static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
889 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -0500890 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700891 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500892 uint32_t err = ERR_peek_error();
893 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
894 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
895 /* Add a dedicated error code to the queue for a handshake_failure alert
896 * in response to ClientHello. This matches NSS's client behavior and
897 * gives a better error on a (probable) failure to negotiate initial
898 * parameters. Note: this error code comes after the original one.
899 *
900 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400901 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500902 }
David Benjamin09eb6552016-07-08 14:32:11 -0700903 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
David Benjaminb853f312017-07-14 18:40:34 -0400906 uint16_t server_version;
907 if (!parse_server_version(hs, &server_version)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400908 return -1;
909 }
910
Steven Valdez8f36c512017-06-20 10:55:02 -0400911 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400912 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400913 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
914 return -1;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400915 }
916
David Benjamin0d56f882015-12-19 17:05:56 -0500917 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
918 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400919 ssl->version = server_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500920 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800921 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500922 ssl->s3->have_version = 1;
Steven Valdez8f36c512017-06-20 10:55:02 -0400923 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400924 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400925 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
926 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Steven Valdez143e8b32016-07-11 13:19:03 -0400929 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500930 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900931 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400932 return 1;
933 }
934
Steven Valdez2d850622017-01-11 11:34:52 -0500935 if (hs->early_data_offered) {
936 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400937 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
938 return -1;
Steven Valdez2d850622017-01-11 11:34:52 -0500939 }
940
David Benjamin6e4fc332016-11-17 16:43:08 +0900941 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400942
David Benjamin276b7e82017-01-21 14:13:39 -0500943 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400944 return -1;
945 }
946
David Benjaminb853f312017-07-14 18:40:34 -0400947 CBS server_hello, server_random, session_id;
948 uint16_t cipher_suite;
949 uint8_t compression_method;
950 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
951 if (!CBS_skip(&server_hello, 2 /* version */) ||
952 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400953 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
954 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
955 !CBS_get_u16(&server_hello, &cipher_suite) ||
956 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400957 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400958 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
959 return -1;
Steven Valdez143e8b32016-07-11 13:19:03 -0400960 }
961
Adam Langley1bea1732014-12-17 19:06:57 -0800962 /* Copy over the server random. */
David Benjamind3bca042017-04-08 11:05:04 -0400963 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
964 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700965
David Benjamin55108632016-08-11 22:01:18 -0400966 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
967 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400968
David Benjamin0d56f882015-12-19 17:05:56 -0500969 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400970 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500971 CBS_mem_equal(&session_id, ssl->session->session_id,
972 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400973 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800974 } else {
975 /* The session wasn't resumed. Create a fresh SSL_SESSION to
976 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400977 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900978 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400979 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
980 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800981 }
982 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500983 hs->new_session->session_id_length = CBS_len(&session_id);
984 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
985 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800986 }
David Benjamina03d95d2014-07-12 19:49:07 -0400987
David Benjamind2cb1c12016-11-02 17:49:09 -0400988 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800989 if (c == NULL) {
990 /* unknown cipher */
David Benjamin3570d732015-06-29 00:28:17 -0400991 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400992 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
993 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800994 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400995
996 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500997 uint32_t mask_a, mask_k;
998 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
999 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -07001000 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -04001001 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
1002 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001004 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1005 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001006 }
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Steven Valdez87eab492016-06-27 16:34:59 -04001008 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001009 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -04001010 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001011 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1012 return -1;
David Benjaminece3de92015-03-16 18:02:20 -04001013 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001014 if (ssl->session->cipher != c) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001015 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001016 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1017 return -1;
Steven Valdez803c77a2016-09-06 14:13:43 -04001018 }
Steven Valdez4aa154e2016-07-29 14:32:55 -04001019 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
1020 /* This is actually a client application bug. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001021 OPENSSL_PUT_ERROR(SSL,
1022 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001023 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1024 return -1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001025 }
David Benjamin0623bce2015-12-25 15:40:14 -05001026 } else {
David Benjamin45738dd2017-02-09 20:01:26 -05001027 hs->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -08001028 }
David Benjamin45738dd2017-02-09 20:01:26 -05001029 hs->new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -05001030
David Benjaminf71036e2017-01-21 14:49:39 -05001031 /* Now that the cipher is known, initialize the handshake hash and hash the
1032 * ServerHello. */
Steven Valdez908ac192017-01-12 13:17:07 -05001033 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
1034 c->algorithm_prf) ||
1035 !ssl_hash_current_message(hs)) {
David Benjamind3bca042017-04-08 11:05:04 -04001036 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1037 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001038 }
1039
Steven Valdez2b8415e2016-06-30 13:27:23 -04001040 /* If doing a full handshake, the server may request a client certificate
1041 * which requires hashing the handshake transcript. Otherwise, the handshake
1042 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001043 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -05001044 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez908ac192017-01-12 13:17:07 -05001045 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001046 }
David Benjamina03d95d2014-07-12 19:49:07 -04001047
Adam Langley1bea1732014-12-17 19:06:57 -08001048 /* Only the NULL compression algorithm is supported. */
1049 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001050 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -04001051 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1052 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langley1bea1732014-12-17 19:06:57 -08001055 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -05001056 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -04001057 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001058 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001059 }
Adam Langley95c29f32014-06-20 12:00:00 -07001060
Adam Langley1bea1732014-12-17 19:06:57 -08001061 /* There should be nothing left over in the record. */
1062 if (CBS_len(&server_hello) != 0) {
1063 /* wrong packet length */
David Benjamin639846e2016-09-09 11:41:18 -04001064 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -04001065 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1066 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001067 }
Adam Langley95c29f32014-06-20 12:00:00 -07001068
Steven Valdez87eab492016-06-27 16:34:59 -04001069 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -05001070 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -05001071 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001072 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001073 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001074 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001075 }
David Benjamind3bca042017-04-08 11:05:04 -04001076 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1077 return -1;
Adam Langleyba5934b2015-06-02 10:50:35 -07001078 }
1079
Adam Langley1bea1732014-12-17 19:06:57 -08001080 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001081}
Adam Langley95c29f32014-06-20 12:00:00 -07001082
David Benjaminc3c88822016-11-14 10:32:04 +09001083static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1084 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001085 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001086 if (ret <= 0) {
1087 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
David Benjaminf71036e2017-01-21 14:49:39 -05001090 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001091 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001092 return -1;
1093 }
1094
David Benjamin5c900c82016-07-13 23:03:26 -04001095 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001096 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001097
Adam Langleyc68e5b92017-02-08 13:33:15 -08001098 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001099 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001100 EVP_PKEY_free(hs->peer_pubkey);
1101 hs->peer_pubkey = NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001102 hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
1103 &cbs, ssl->ctx->pool);
1104 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001105 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001106 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001107 }
Adam Langley95c29f32014-06-20 12:00:00 -07001108
David Benjamin45738dd2017-02-09 20:01:26 -05001109 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
Adam Langley68e71242016-12-12 11:06:16 -08001110 CBS_len(&cbs) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001111 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin3570d732015-06-29 00:28:17 -04001112 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001113 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001114 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001115 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001116
Adam Langley05672202016-12-13 12:05:49 -08001117 if (!ssl_check_leaf_certificate(
David Benjamin45738dd2017-02-09 20:01:26 -05001118 hs, hs->peer_pubkey,
1119 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001120 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001121 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001122 }
1123
David Benjamina58baaf2017-02-28 20:54:28 -05001124 /* Disallow the server certificate from changing during a renegotiation. See
1125 * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
1126 * so this check is sufficient. */
1127 if (ssl->s3->established_session != NULL) {
1128 if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
1129 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
1130 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1131 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1132 return -1;
1133 }
1134
1135 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
1136 const CRYPTO_BUFFER *old_cert =
1137 sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
1138 const CRYPTO_BUFFER *new_cert =
1139 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
1140 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
1141 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
1142 CRYPTO_BUFFER_data(new_cert),
1143 CRYPTO_BUFFER_len(old_cert)) != 0) {
1144 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1145 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1146 return -1;
1147 }
1148 }
1149 }
1150
David Benjamin5c900c82016-07-13 23:03:26 -04001151 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001152}
1153
David Benjaminc3c88822016-11-14 10:32:04 +09001154static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1155 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001156 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001157 if (ret <= 0) {
1158 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001159 }
1160
1161 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1162 /* A server may send status_request in ServerHello and then change
1163 * its mind about sending CertificateStatus. */
1164 ssl->s3->tmp.reuse_message = 1;
1165 return 1;
1166 }
1167
Steven Valdez908ac192017-01-12 13:17:07 -05001168 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001169 return -1;
1170 }
1171
David Benjamin8d606e32017-06-15 22:43:04 -04001172 CBS certificate_status, ocsp_response;
1173 uint8_t status_type;
David Benjamin09eb6552016-07-08 14:32:11 -07001174 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001175 if (!CBS_get_u8(&certificate_status, &status_type) ||
1176 status_type != TLSEXT_STATUSTYPE_ocsp ||
1177 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1178 CBS_len(&ocsp_response) == 0 ||
1179 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001180 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001181 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1182 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001183 }
1184
David Benjamin45738dd2017-02-09 20:01:26 -05001185 if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
1186 &hs->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001187 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001188 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1189 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001190 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001191
David Benjamin8d606e32017-06-15 22:43:04 -04001192 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001193}
1194
David Benjaminc3c88822016-11-14 10:32:04 +09001195static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1196 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -08001197 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001198 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001199
David Benjaminf71036e2017-01-21 14:49:39 -05001200 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001201 if (ret <= 0) {
1202 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001203 }
1204
David Benjamin0d56f882015-12-19 17:05:56 -05001205 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001206 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001207 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001208 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001209 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001210 return -1;
1211 }
1212
David Benjamin0d56f882015-12-19 17:05:56 -05001213 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001214 return 1;
1215 }
1216
Steven Valdez908ac192017-01-12 13:17:07 -05001217 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001218 return -1;
1219 }
1220
Adam Langley1bea1732014-12-17 19:06:57 -08001221 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001222 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001223 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001224 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001225
David Benjamin45738dd2017-02-09 20:01:26 -05001226 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1227 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001228
1229 if (alg_a & SSL_aPSK) {
1230 CBS psk_identity_hint;
1231
1232 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1233 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1234 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001235 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001236 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1237 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001238 }
1239
1240 /* Store PSK identity hint for later use, hint is used in
1241 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1242 * identity hint can be as long as the maximum length of a PSK identity.
1243 * Also do not allow NULL characters; identities are saved as C strings.
1244 *
1245 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1246 * a specific identity. */
1247 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1248 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001249 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -04001250 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1251 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001252 }
1253
David Benjamin78679342016-09-16 19:42:05 -04001254 /* Save non-empty identity hints as a C string. Empty identity hints we
1255 * treat as missing. Plain PSK makes it possible to send either no hint
1256 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1257 * empty hint. Having different capabilities is odd, so we interpret empty
1258 * and missing as identical. */
1259 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001260 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001261 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001262 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1263 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001264 }
1265 }
1266
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001267 if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001268 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001269 uint8_t group_type;
1270 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001271 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001272 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1273 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001274 !CBS_get_u16(&server_key_exchange, &group_id) ||
1275 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -04001276 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001277 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1278 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001279 }
David Benjamin45738dd2017-02-09 20:01:26 -05001280 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001281
Steven Valdezce902a92016-05-17 11:47:53 -04001282 /* Ensure the group is consistent with preferences. */
1283 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001284 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -04001285 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1286 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001287 }
David Benjamin4298d772015-12-19 00:18:25 -05001288
Matt Braithwaite053931e2016-05-25 12:06:05 -07001289 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001290 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1291 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001292 goto err;
1293 }
Adam Langley1bea1732014-12-17 19:06:57 -08001294 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -04001295 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001296 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1297 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001298 }
1299
1300 /* At this point, |server_key_exchange| contains the signature, if any, while
1301 * |server_key_exchange_orig| contains the entire message. From that, derive
1302 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001303 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001304 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1305 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1306
David Benjaminf4958e72015-06-07 11:00:53 -04001307 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001308 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001309 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001310 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001311 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001312 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001313 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1314 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001315 }
David Benjamin8d606e32017-06-15 22:43:04 -04001316 uint8_t alert = SSL_AD_DECODE_ERROR;
1317 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1318 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1319 goto err;
David Benjamin6e807652015-11-02 12:02:20 -05001320 }
David Benjamin45738dd2017-02-09 20:01:26 -05001321 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001322 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1323 hs->peer_pubkey)) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001324 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001325 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1326 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001327 }
1328
1329 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001330 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001331 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1332 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001333 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001334 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1335 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001336 }
1337
Steven Valdez2b8415e2016-06-30 13:27:23 -04001338 CBB transcript;
1339 uint8_t *transcript_data;
1340 size_t transcript_len;
1341 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1342 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1343 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1344 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1345 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1346 CBB_cleanup(&transcript);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001347 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001348 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1349 goto err;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001350 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001351
1352 int sig_ok = ssl_public_key_verify(
1353 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001354 hs->peer_pubkey, transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001355 OPENSSL_free(transcript_data);
1356
David Benjaminbf82aed2016-03-01 22:57:40 -05001357#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1358 sig_ok = 1;
1359 ERR_clear_error();
1360#endif
1361 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001362 /* bad signature */
David Benjamin3570d732015-06-29 00:28:17 -04001363 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001364 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1365 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001366 }
1367 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001368 /* PSK ciphers are the only supported certificate-less ciphers. */
1369 assert(alg_a == SSL_aPSK);
1370
Adam Langley1bea1732014-12-17 19:06:57 -08001371 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001372 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001373 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1374 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001375 }
1376 }
Adam Langley1bea1732014-12-17 19:06:57 -08001377 return 1;
1378
Adam Langley95c29f32014-06-20 12:00:00 -07001379err:
Adam Langley1bea1732014-12-17 19:06:57 -08001380 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001381 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001382 return -1;
1383}
Adam Langley95c29f32014-06-20 12:00:00 -07001384
David Benjaminc3c88822016-11-14 10:32:04 +09001385static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1386 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001387 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001388 if (msg_ret <= 0) {
1389 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001390 }
David Benjamin688d8df2014-11-02 23:06:42 -05001391
David Benjamin3dcec452016-06-07 17:13:28 -04001392 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001393 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001394 /* If we get here we don't need the handshake buffer as we won't be doing
1395 * client auth. */
Steven Valdez908ac192017-01-12 13:17:07 -05001396 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001397 return 1;
1398 }
David Benjamined439582014-07-14 19:13:02 -04001399
David Benjaminf71036e2017-01-21 14:49:39 -05001400 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001401 !ssl_hash_current_message(hs)) {
David Benjamine0332e82016-07-13 22:40:36 -04001402 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001403 }
Adam Langley95c29f32014-06-20 12:00:00 -07001404
David Benjamina5177cb2016-03-11 19:38:17 -05001405 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001406 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001407
David Benjamine0332e82016-07-13 22:40:36 -04001408 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001409 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001410 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001411 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001412 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001413 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001414 }
David Benjamined439582014-07-14 19:13:02 -04001415
David Benjaminc3c88822016-11-14 10:32:04 +09001416 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1417 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001418 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001419 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001420 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001421
David Benjamina1e9cab2015-12-30 00:08:49 -05001422 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001423 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001424 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001425 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001426 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001427 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001428 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001429 }
Adam Langley1bea1732014-12-17 19:06:57 -08001430 }
David Benjamined439582014-07-14 19:13:02 -04001431
Adam Langleyc68e5b92017-02-08 13:33:15 -08001432 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Langley34b4c822017-02-02 10:57:17 -08001433 STACK_OF(CRYPTO_BUFFER) *ca_names =
1434 ssl_parse_client_CA_list(ssl, &alert, &cbs);
1435 if (ca_names == NULL) {
David Benjamine0332e82016-07-13 22:40:36 -04001436 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1437 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001438 }
Adam Langley95c29f32014-06-20 12:00:00 -07001439
David Benjamin639846e2016-09-09 11:41:18 -04001440 if (CBS_len(&cbs) != 0) {
1441 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley34b4c822017-02-02 10:57:17 -08001442 sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
David Benjamin639846e2016-09-09 11:41:18 -04001443 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1444 return -1;
1445 }
1446
David Benjaminc3c88822016-11-14 10:32:04 +09001447 hs->cert_request = 1;
Adam Langley34b4c822017-02-02 10:57:17 -08001448 sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
1449 hs->ca_names = ca_names;
1450 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamine0332e82016-07-13 22:40:36 -04001451 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001452}
Adam Langley95c29f32014-06-20 12:00:00 -07001453
David Benjaminc3c88822016-11-14 10:32:04 +09001454static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1455 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001456 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001457 if (ret <= 0) {
1458 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001459 }
David Benjamin46062682014-07-14 19:14:32 -04001460
David Benjaminf71036e2017-01-21 14:49:39 -05001461 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001462 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001463 return -1;
1464 }
1465
David Benjamin09eb6552016-07-08 14:32:11 -07001466 /* ServerHelloDone is empty. */
1467 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001468 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001469 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001470 return -1;
1471 }
David Benjamin46062682014-07-14 19:14:32 -04001472
Adam Langley1bea1732014-12-17 19:06:57 -08001473 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001474}
Adam Langley1258b6a2014-06-20 12:00:00 -07001475
David Benjaminc3c88822016-11-14 10:32:04 +09001476static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1477 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001478 /* Call cert_cb to update the certificate. */
1479 if (ssl->cert->cert_cb) {
1480 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1481 if (ret < 0) {
1482 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001483 return -1;
1484 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001485 if (ret == 0) {
1486 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1487 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001488 return -1;
1489 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001490 }
1491
David Benjamin5edfc8c2016-12-10 15:46:58 -05001492 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001493 /* Without a client certificate, the handshake buffer may be released. */
Steven Valdez908ac192017-01-12 13:17:07 -05001494 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin5edfc8c2016-12-10 15:46:58 -05001495
David Benjamindaf207a2017-01-03 18:37:41 -05001496 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001497 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001498 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1499 SSL_AD_NO_CERTIFICATE)) {
1500 return -1;
1501 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001502 return 1;
1503 }
1504 }
1505
David Benjamina232a712017-03-30 15:51:53 -05001506 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001507 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001508 return -1;
1509 }
David Benjamin16315f72017-01-12 20:02:05 -05001510 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001511}
1512
David Benjamina3d76d02017-07-14 19:36:07 -04001513static_assert(sizeof(size_t) >= sizeof(unsigned),
1514 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001515
David Benjaminc3c88822016-11-14 10:32:04 +09001516static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1517 SSL *const ssl = hs->ssl;
David Benjamind781fc42017-07-12 16:25:57 -04001518 bssl::ScopedCBB cbb;
1519 CBB body;
1520 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001521 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjamind781fc42017-07-12 16:25:57 -04001522 return -1;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001523 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001524
David Benjamind781fc42017-07-12 16:25:57 -04001525 uint8_t *pms = NULL;
1526 size_t pms_len = 0;
David Benjamin45738dd2017-02-09 20:01:26 -05001527 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1528 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001529
David Benjaminae0eaaa2015-12-17 02:01:25 -05001530 /* If using a PSK key exchange, prepare the pre-shared key. */
1531 unsigned psk_len = 0;
1532 uint8_t psk[PSK_MAX_PSK_LEN];
1533 if (alg_a & SSL_aPSK) {
1534 if (ssl->psk_client_callback == NULL) {
1535 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1536 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001537 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001538
David Benjaminae0eaaa2015-12-17 02:01:25 -05001539 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001540 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001541 psk_len =
1542 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1543 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001544 if (psk_len == 0) {
1545 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1546 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1547 goto err;
1548 }
1549 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001550
David Benjamin45738dd2017-02-09 20:01:26 -05001551 OPENSSL_free(hs->new_session->psk_identity);
1552 hs->new_session->psk_identity = BUF_strdup(identity);
1553 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001554 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1555 goto err;
1556 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001557
David Benjaminae0eaaa2015-12-17 02:01:25 -05001558 /* Write out psk_identity. */
1559 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001560 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001561 !CBB_add_bytes(&child, (const uint8_t *)identity,
1562 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001563 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001564 goto err;
1565 }
1566 }
David Benjamin758d1272015-11-20 17:47:25 -05001567
David Benjaminae0eaaa2015-12-17 02:01:25 -05001568 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1569 if (alg_k & SSL_kRSA) {
1570 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001571 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001572 if (pms == NULL) {
1573 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1574 goto err;
1575 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001576
Adam Langleyd5157222016-12-12 11:37:43 -08001577 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001578 if (rsa == NULL) {
1579 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001580 goto err;
1581 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001582
David Benjaminf04c2e92016-12-06 13:35:25 -05001583 pms[0] = hs->client_version >> 8;
1584 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001585 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1586 goto err;
1587 }
1588
David Benjamin75836432016-06-17 18:48:29 -04001589 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001590 size_t enc_pms_len;
1591 /* In TLS, there is a length prefix. */
1592 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001593 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001594 goto err;
1595 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001596 enc_pms = &child;
1597 }
Adam Langley1bea1732014-12-17 19:06:57 -08001598
David Benjaminae0eaaa2015-12-17 02:01:25 -05001599 uint8_t *ptr;
1600 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1601 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1602 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001603 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001604 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001605 goto err;
1606 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001607 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001608 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001609 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001610 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001611 goto err;
1612 }
1613
David Benjamin4298d772015-12-19 00:18:25 -05001614 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001615 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001616 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1617 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001618 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1619 goto err;
1620 }
David Benjamin75836432016-06-17 18:48:29 -04001621 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001622 goto err;
1623 }
David Benjamin4298d772015-12-19 00:18:25 -05001624
1625 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001626 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1627 OPENSSL_free(hs->peer_key);
1628 hs->peer_key = NULL;
1629 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001630 } else if (alg_k & SSL_kPSK) {
1631 /* For plain PSK, other_secret is a block of 0s with the same length as
1632 * the pre-shared key. */
1633 pms_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001634 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001635 if (pms == NULL) {
1636 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1637 goto err;
1638 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001639 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001640 } else {
1641 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1642 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1643 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001644 }
1645
David Benjaminae0eaaa2015-12-17 02:01:25 -05001646 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1647 * key. */
1648 if (alg_a & SSL_aPSK) {
1649 CBB pms_cbb, child;
1650 uint8_t *new_pms;
1651 size_t new_pms_len;
1652
1653 CBB_zero(&pms_cbb);
1654 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1655 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1656 !CBB_add_bytes(&child, pms, pms_len) ||
1657 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1658 !CBB_add_bytes(&child, psk, psk_len) ||
1659 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1660 CBB_cleanup(&pms_cbb);
1661 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1662 goto err;
1663 }
1664 OPENSSL_cleanse(pms, pms_len);
1665 OPENSSL_free(pms);
1666 pms = new_pms;
1667 pms_len = new_pms_len;
1668 }
1669
1670 /* The message must be added to the finished hash before calculating the
1671 * master secret. */
David Benjamind781fc42017-07-12 16:25:57 -04001672 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001673 goto err;
1674 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001675
David Benjamin45738dd2017-02-09 20:01:26 -05001676 hs->new_session->master_key_length = tls1_generate_master_secret(
1677 hs, hs->new_session->master_key, pms, pms_len);
1678 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001679 goto err;
1680 }
David Benjaminfc02b592017-02-17 16:26:01 -05001681 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001682 OPENSSL_cleanse(pms, pms_len);
1683 OPENSSL_free(pms);
1684
David Benjamin16315f72017-01-12 20:02:05 -05001685 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001686
1687err:
David Benjaminae0eaaa2015-12-17 02:01:25 -05001688 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001689 OPENSSL_cleanse(pms, pms_len);
1690 OPENSSL_free(pms);
1691 }
1692 return -1;
1693}
Adam Langley1258b6a2014-06-20 12:00:00 -07001694
David Benjaminc3c88822016-11-14 10:32:04 +09001695static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1696 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001697 assert(ssl_has_private_key(ssl));
1698
David Benjamind781fc42017-07-12 16:25:57 -04001699 bssl::ScopedCBB cbb;
1700 CBB body, child;
1701 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001702 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjamind781fc42017-07-12 16:25:57 -04001703 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001704 }
1705
David Benjaminea9a0d52016-07-08 15:52:59 -07001706 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001707 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001708 return -1;
David Benjaminea9a0d52016-07-08 15:52:59 -07001709 }
David Benjamin429fdc02016-06-17 17:33:30 -04001710 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001711 /* Write out the digest type in TLS 1.2. */
1712 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001713 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001714 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001715 }
1716 }
David Benjaminfc825122015-12-18 01:57:43 -05001717
David Benjamin429fdc02016-06-17 17:33:30 -04001718 /* Set aside space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001719 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjamin429fdc02016-06-17 17:33:30 -04001720 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001721 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001722 !CBB_reserve(&child, &ptr, max_sig_len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001723 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001724 }
1725
Steven Valdez2b8415e2016-06-30 13:27:23 -04001726 size_t sig_len = max_sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001727 /* The SSL3 construction for CertificateVerify does not decompose into a
1728 * single final digest and signature, and must be special-cased. */
1729 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1730 if (ssl->cert->key_method != NULL) {
1731 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
David Benjamind781fc42017-07-12 16:25:57 -04001732 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001733 }
1734
David Benjamin44148742017-06-17 13:20:59 -04001735 uint8_t digest[EVP_MAX_MD_SIZE];
1736 size_t digest_len;
1737 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
1738 &digest_len, hs->new_session,
1739 signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001740 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001741 }
David Benjaminfc825122015-12-18 01:57:43 -05001742
David Benjamin44148742017-06-17 13:20:59 -04001743 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1744 int ok = pctx != NULL &&
1745 EVP_PKEY_sign_init(pctx) &&
1746 EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len);
1747 EVP_PKEY_CTX_free(pctx);
1748 if (!ok) {
David Benjamind781fc42017-07-12 16:25:57 -04001749 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001750 }
1751 } else {
1752 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1753 signature_algorithm,
1754 (const uint8_t *)hs->transcript.buffer->data,
1755 hs->transcript.buffer->length)) {
1756 case ssl_private_key_success:
1757 break;
1758 case ssl_private_key_failure:
David Benjamind781fc42017-07-12 16:25:57 -04001759 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001760 case ssl_private_key_retry:
1761 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind781fc42017-07-12 16:25:57 -04001762 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001763 }
David Benjaminfc825122015-12-18 01:57:43 -05001764 }
1765
David Benjaminfc825122015-12-18 01:57:43 -05001766 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001767 !ssl_add_message_cbb(ssl, cbb.get())) {
1768 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001769 }
1770
David Benjamin44148742017-06-17 13:20:59 -04001771 /* The handshake buffer is no longer necessary. */
1772 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin16315f72017-01-12 20:02:05 -05001773 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001774}
1775
David Benjaminc3c88822016-11-14 10:32:04 +09001776static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1777 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001778 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001779 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001780
David Benjamin75836432016-06-17 18:48:29 -04001781 CBB cbb, body, child;
1782 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1783 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001784 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1785 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001786 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001787 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001788 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001789 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1790 CBB_cleanup(&cbb);
1791 return -1;
1792 }
1793
David Benjamin16315f72017-01-12 20:02:05 -05001794 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001795}
1796
David Benjaminc3c88822016-11-14 10:32:04 +09001797static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1798 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001799 if (!ssl_do_channel_id_callback(ssl)) {
1800 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001801 }
1802
David Benjamin96e97b12015-10-10 16:30:22 -04001803 if (ssl->tlsext_channel_id_private == NULL) {
1804 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001805 return -1;
1806 }
Adam Langley1bea1732014-12-17 19:06:57 -08001807
Nick Harper60a85cb2016-09-23 16:25:11 -07001808 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001809 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001810 !tls1_write_channel_id(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001811 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001812 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001813 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001814 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001815 }
1816
David Benjamin16315f72017-01-12 20:02:05 -05001817 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001818}
1819
David Benjaminc3c88822016-11-14 10:32:04 +09001820static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1821 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001822 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001823 if (ret <= 0) {
1824 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001825 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001826
David Benjaminf71036e2017-01-21 14:49:39 -05001827 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001828 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001829 return -1;
1830 }
1831
David Benjamin9f1dc822016-06-07 17:03:46 -04001832 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001833 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001834 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001835 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001836 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1837 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001838 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001839 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001840 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001841 }
1842
David Benjamin9f1dc822016-06-07 17:03:46 -04001843 if (CBS_len(&ticket) == 0) {
1844 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001845 * negotiating the extension. The value of |ticket_expected| is checked in
1846 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001847 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001848 return 1;
1849 }
1850
Steven Valdez87eab492016-06-27 16:34:59 -04001851 int session_renewed = ssl->session != NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001852 SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001853 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001854 /* The server is sending a new ticket for an existing session. Sessions are
1855 * immutable once established, so duplicate all but the ticket of the
1856 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001857 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001858 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001859 /* This should never happen. */
1860 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1861 goto err;
1862 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001863 }
1864
David Benjamin123db572016-11-03 16:59:25 -04001865 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001866 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001867
Steven Valdez87eab492016-06-27 16:34:59 -04001868 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001869 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1870 goto err;
1871 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001872 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001873
1874 /* Generate a session ID for this session based on the session ticket. We use
1875 * the session ID mechanism for detecting ticket resumption. This also fits in
1876 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001877 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1878 session->session_id, &session->session_id_length,
1879 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001880 goto err;
1881 }
1882
Steven Valdez87eab492016-06-27 16:34:59 -04001883 if (session_renewed) {
1884 session->not_resumable = 0;
1885 SSL_SESSION_free(ssl->session);
1886 ssl->session = session;
1887 }
1888
David Benjamin454aa4c2016-03-10 08:43:27 -05001889 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001890
David Benjamin9f1dc822016-06-07 17:03:46 -04001891err:
Steven Valdez87eab492016-06-27 16:34:59 -04001892 if (session_renewed) {
1893 SSL_SESSION_free(session);
1894 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001895 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001896}