blob: 9efbf0ad5801774836ded56533c297ccd579c8f6 [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);
176static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
177static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
178static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
179static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
180static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
181static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
182static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
183static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
184static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
185static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400186
David Benjamince8c9d22016-11-14 10:45:16 +0900187int ssl3_connect(SSL_HANDSHAKE *hs) {
188 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800189 int ret = -1;
David Benjaminbeb47022014-11-30 02:58:52 -0500190
David Benjamin0d56f882015-12-19 17:05:56 -0500191 assert(ssl->handshake_func == ssl3_connect);
192 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley1bea1732014-12-17 19:06:57 -0800194 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500195 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjamincb0c29f2016-12-12 17:00:50 -0500197 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400198 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400199 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800201 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langley1bea1732014-12-17 19:06:57 -0800203 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900204 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800205 if (ret <= 0) {
206 goto end;
207 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400208
David Benjamince079fd2016-08-02 16:22:34 -0400209 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
Steven Valdez2d850622017-01-11 11:34:52 -0500210 if (hs->early_data_offered) {
211 if (!tls13_init_early_key_schedule(hs) ||
212 !tls13_advance_key_schedule(hs, ssl->session->master_key,
213 ssl->session->master_key_length) ||
214 !tls13_derive_early_secrets(hs) ||
215 !tls13_set_traffic_key(ssl, evp_aead_seal,
216 hs->early_traffic_secret,
217 hs->hash_len)) {
218 ret = -1;
219 goto end;
220 }
Steven Valdeze831a812017-03-09 14:56:07 -0500221 hs->next_state = SSL3_ST_WRITE_EARLY_DATA;
222 } else {
223 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
Steven Valdez2d850622017-01-11 11:34:52 -0500224 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400225 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500226 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400227 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500228 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800229 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjaminaa7734b2016-06-07 16:40:46 -0400231 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400232 assert(SSL_is_dtls(ssl));
David Benjamin8d606e32017-06-15 22:43:04 -0400233 ret = dtls1_get_hello_verify_request(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400234 if (ret <= 0) {
235 goto end;
236 }
237 if (ssl->d1->send_cookie) {
238 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500239 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400240 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500241 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400242 }
243 break;
244
Steven Valdeze831a812017-03-09 14:56:07 -0500245 case SSL3_ST_WRITE_EARLY_DATA:
246 /* Stash the early data session, so connection properties may be queried
247 * out of it. */
248 hs->in_early_data = 1;
249 hs->early_session = ssl->session;
250 SSL_SESSION_up_ref(ssl->session);
251
252 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
253 hs->can_early_write = 1;
254 ret = 1;
255 goto end;
256
Adam Langley1bea1732014-12-17 19:06:57 -0800257 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900258 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500259 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400260 break;
261 }
Adam Langley1bea1732014-12-17 19:06:57 -0800262 if (ret <= 0) {
263 goto end;
264 }
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Steven Valdez87eab492016-06-27 16:34:59 -0400266 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500267 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800268 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500269 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800270 }
Adam Langley1bea1732014-12-17 19:06:57 -0800271 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400272
Adam Langley1bea1732014-12-17 19:06:57 -0800273 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500274 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900275 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800276 if (ret <= 0) {
277 goto end;
278 }
Adam Langley1bea1732014-12-17 19:06:57 -0800279 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500280 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800281 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700282
David Benjamin9f1dc822016-06-07 17:03:46 -0400283 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900284 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900285 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400286 if (ret <= 0) {
287 goto end;
288 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400289 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500290 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400291 break;
292
Paul Lietar8f1c2682015-08-18 12:21:54 +0100293 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500294 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900295 ret = ssl3_verify_server_cert(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400296 if (ret <= 0) {
297 goto end;
298 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100299 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500300 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100301 break;
302
Adam Langley1bea1732014-12-17 19:06:57 -0800303 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900304 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800305 if (ret <= 0) {
306 goto end;
307 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500308 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800309 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langley1bea1732014-12-17 19:06:57 -0800311 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500312 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900313 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400314 if (ret <= 0) {
315 goto end;
316 }
Adam Langley1bea1732014-12-17 19:06:57 -0800317 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500318 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900322 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800323 if (ret <= 0) {
324 goto end;
325 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400326 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500327 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800328 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langley1bea1732014-12-17 19:06:57 -0800330 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900331 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900332 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400333 if (ret <= 0) {
334 goto end;
335 }
Adam Langley1bea1732014-12-17 19:06:57 -0800336 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500337 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800338 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley1bea1732014-12-17 19:06:57 -0800340 case SSL3_ST_CW_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900341 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800342 if (ret <= 0) {
343 goto end;
344 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500345 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800346 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley1bea1732014-12-17 19:06:57 -0800348 case SSL3_ST_CW_CERT_VRFY_A:
David Benjamina660e7a2017-01-02 08:15:35 -0500349 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900350 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400351 if (ret <= 0) {
352 goto end;
353 }
Adam Langley1bea1732014-12-17 19:06:57 -0800354 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500355 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800356 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700357
David Benjamin352d0a92016-06-28 11:22:02 -0400358 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500359 if (!ssl->method->add_change_cipher_spec(ssl) ||
360 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800361 ret = -1;
362 goto end;
363 }
Adam Langley95c29f32014-06-20 12:00:00 -0700364
David Benjamindaf207a2017-01-03 18:37:41 -0500365 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800366 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langley1bea1732014-12-17 19:06:57 -0800368 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900369 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900370 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400371 if (ret <= 0) {
372 goto end;
373 }
Adam Langley1bea1732014-12-17 19:06:57 -0800374 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500375 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langley1bea1732014-12-17 19:06:57 -0800378 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400379 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900380 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400381 if (ret <= 0) {
382 goto end;
383 }
Adam Langley1bea1732014-12-17 19:06:57 -0800384 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500385 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800386 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley1bea1732014-12-17 19:06:57 -0800388 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500389 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800390 if (ret <= 0) {
391 goto end;
392 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500393 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Steven Valdez87eab492016-06-27 16:34:59 -0400395 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500396 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800397 } else {
398 /* This is a non-resumption handshake. If it involves ChannelID, then
399 * record the handshake hashes at this point in the session so that
400 * any resumption of this session with ChannelID can sign those
401 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500402 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700403 if (ret <= 0) {
404 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800405 }
David Benjamin0d56f882015-12-19 17:05:56 -0500406 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
407 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500408 /* No False Start on renegotiation (would complicate the state
409 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500410 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500411 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800412 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500413 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800414 }
415 }
Adam Langley1bea1732014-12-17 19:06:57 -0800416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
David Benjamin9f1dc822016-06-07 17:03:46 -0400418 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500419 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900420 hs->in_false_start = 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500421 hs->can_early_write = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400422 ret = 1;
423 goto end;
424
Adam Langley1bea1732014-12-17 19:06:57 -0800425 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900426 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900427 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400428 if (ret <= 0) {
429 goto end;
430 }
Adam Langley1bea1732014-12-17 19:06:57 -0800431 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500432 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800433 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langley1bea1732014-12-17 19:06:57 -0800435 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400436 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500437 if (ret <= 0) {
438 goto end;
439 }
440
David Benjamin67739722016-11-17 17:03:59 +0900441 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800442 ret = -1;
443 goto end;
444 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500445 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800446 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700447
Adam Langley1bea1732014-12-17 19:06:57 -0800448 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900449 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800450 if (ret <= 0) {
451 goto end;
452 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400453 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Steven Valdez87eab492016-06-27 16:34:59 -0400455 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500456 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800457 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500458 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800459 }
Adam Langley1bea1732014-12-17 19:06:57 -0800460 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langley1bea1732014-12-17 19:06:57 -0800462 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500463 ret = ssl->method->flush_flight(ssl);
464 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800465 goto end;
466 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500467 hs->state = hs->next_state;
David Benjamin77458a42017-02-10 23:33:27 -0500468 if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400469 ssl->method->expect_flight(ssl);
470 }
Adam Langley1bea1732014-12-17 19:06:57 -0800471 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700472
Steven Valdez681eb6a2016-12-19 13:19:29 -0500473 case SSL_ST_TLS13: {
474 int early_return = 0;
475 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400476 if (ret <= 0) {
477 goto end;
478 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500479
480 if (early_return) {
481 ret = 1;
482 goto end;
483 }
484
David Benjamin77458a42017-02-10 23:33:27 -0500485 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400486 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500487 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400488
David Benjamin77458a42017-02-10 23:33:27 -0500489 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin4497e582016-07-27 17:51:49 -0400490 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Steven Valdez87eab492016-06-27 16:34:59 -0400492 SSL_SESSION_free(ssl->s3->established_session);
493 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400494 SSL_SESSION_up_ref(ssl->session);
495 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400496 } else {
497 /* We make a copy of the session in order to maintain the immutability
498 * of the new established_session due to False Start. The caller may
499 * have taken a reference to the temporary session. */
500 ssl->s3->established_session =
David Benjamin45738dd2017-02-09 20:01:26 -0500501 SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400502 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400503 ret = -1;
504 goto end;
505 }
506 ssl->s3->established_session->not_resumable = 0;
507
David Benjamin45738dd2017-02-09 20:01:26 -0500508 SSL_SESSION_free(hs->new_session);
509 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400510 }
511
David Benjamin77458a42017-02-10 23:33:27 -0500512 hs->state = SSL_ST_OK;
513 break;
514
515 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500516 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500517 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400518 if (is_initial_handshake) {
519 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900520 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400521 }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langley1bea1732014-12-17 19:06:57 -0800523 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400524 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800525 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500526 }
Steven Valdez87eab492016-06-27 16:34:59 -0400527
Adam Langley1bea1732014-12-17 19:06:57 -0800528 default:
David Benjamin3570d732015-06-29 00:28:17 -0400529 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800530 ret = -1;
531 goto end;
532 }
533
David Benjamin16b1b1d2017-02-10 23:41:14 -0500534 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400535 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800536 }
Adam Langley1bea1732014-12-17 19:06:57 -0800537 }
538
Adam Langley95c29f32014-06-20 12:00:00 -0700539end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400540 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800541 return ret;
542}
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin65ac9972016-09-02 21:35:25 -0400544uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
545 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
546 * a single byte repeatedly and ensures the values are deterministic. This
547 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
548 * same group be advertised in both supported_groups and key_shares. */
549 uint16_t ret = ssl->s3->client_random[index];
550 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
551 ret = (ret & 0xf0) | 0x0a;
552 ret |= ret << 8;
553 return ret;
554}
555
David Benjamin2fe6e222017-01-25 02:18:43 -0500556/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
557 * disabled algorithms. */
558static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
559 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500560 *out_mask_a = 0;
561 *out_mask_k = 0;
562
David Benjamin2fe6e222017-01-25 02:18:43 -0500563 /* PSK requires a client callback. */
564 if (ssl->psk_client_callback == NULL) {
565 *out_mask_a |= SSL_aPSK;
566 *out_mask_k |= SSL_kPSK;
567 }
568}
569
David Benjamin68161cb2017-06-20 14:49:43 -0400570static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
571 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500572 uint32_t mask_a, mask_k;
573 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700574
David Benjamine8d53502015-10-10 14:13:23 -0400575 CBB child;
576 if (!CBB_add_u16_length_prefixed(out, &child)) {
577 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800578 }
579
David Benjamin65ac9972016-09-02 21:35:25 -0400580 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
581 if (ssl->ctx->grease_enabled &&
582 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
583 return 0;
584 }
585
David Benjaminabbbee12016-10-31 19:20:42 -0400586 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
587 * hardware support. */
David Benjamin68161cb2017-06-20 14:49:43 -0400588 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400589 if (!EVP_has_aes_hardware() &&
590 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
591 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500592 }
David Benjaminabbbee12016-10-31 19:20:42 -0400593 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
594 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
595 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400596 }
David Benjaminabbbee12016-10-31 19:20:42 -0400597 if (EVP_has_aes_hardware() &&
598 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400599 return 0;
600 }
601 }
602
David Benjamin68161cb2017-06-20 14:49:43 -0400603 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400604 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
605 int any_enabled = 0;
606 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
607 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
608 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500609 if ((cipher->algorithm_mkey & mask_k) ||
610 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400611 continue;
612 }
David Benjamin68161cb2017-06-20 14:49:43 -0400613 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
614 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400615 continue;
616 }
617 any_enabled = 1;
618 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
619 return 0;
620 }
621 }
622
623 /* If all ciphers were disabled, return the error to the caller. */
David Benjamin68161cb2017-06-20 14:49:43 -0400624 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400625 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
626 return 0;
627 }
David Benjamine8d53502015-10-10 14:13:23 -0400628 }
629
630 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
631 * added. */
David Benjamin68161cb2017-06-20 14:49:43 -0400632 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400633 !ssl->s3->initial_handshake_complete) {
634 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
635 return 0;
636 }
David Benjamine8d53502015-10-10 14:13:23 -0400637 }
638
David Benjamine0ff7672016-09-19 18:40:03 -0400639 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200640 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
641 return 0;
642 }
David Benjamine8d53502015-10-10 14:13:23 -0400643 }
644
645 return CBB_flush(out);
646}
647
David Benjamin8c880a22016-12-03 02:20:34 -0500648int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
649 SSL *const ssl = hs->ssl;
David Benjamind781fc42017-07-12 16:25:57 -0400650 bssl::ScopedCBB cbb;
651 CBB body;
652 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
653 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400654 }
655
Steven Valdez5440fe02016-07-18 12:40:30 -0400656 /* Renegotiations do not participate in session resumption. */
Steven Valdez520e1222017-06-13 12:45:25 -0400657 int has_session_id = ssl->session != NULL &&
658 !ssl->s3->initial_handshake_complete &&
659 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400660
661 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500662 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400663 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400664 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400665 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400666 }
667
Steven Valdez520e1222017-06-13 12:45:25 -0400668 if (has_session_id) {
669 if (!CBB_add_bytes(&child, ssl->session->session_id,
670 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400671 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400672 }
673 } else {
674 /* In TLS 1.3 experimental encodings, send a fake placeholder session ID
675 * when we do not otherwise have one to send. */
676 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400677 ssl->tls13_variant == tls13_experiment &&
Steven Valdez520e1222017-06-13 12:45:25 -0400678 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400679 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400680 }
681 }
682
David Benjamince079fd2016-08-02 16:22:34 -0400683 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400684 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400685 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400686 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400687 }
688 }
689
690 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400691 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400692 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400693 !CBB_add_u8(&body, 1 /* one compression method */) ||
694 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500695 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400696 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400697 }
698
Steven Valdeza833c352016-11-01 13:39:36 -0400699 uint8_t *msg = NULL;
700 size_t len;
David Benjamind781fc42017-07-12 16:25:57 -0400701 if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) {
702 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400703 }
704
705 /* Now that the length prefixes have been computed, fill in the placeholder
706 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500707 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500708 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400709 OPENSSL_free(msg);
David Benjamind781fc42017-07-12 16:25:57 -0400710 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400711 }
712
David Benjamindaf207a2017-01-03 18:37:41 -0500713 return ssl->method->add_message(ssl, msg, len);
Steven Valdez5440fe02016-07-18 12:40:30 -0400714}
715
David Benjaminc3c88822016-11-14 10:32:04 +0900716static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
717 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400718 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
719 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
Steven Valdez908ac192017-01-12 13:17:07 -0500720 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500721 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
722 return -1;
723 }
724
David Benjamin68161cb2017-06-20 14:49:43 -0400725 /* Freeze the version range. */
726 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400727 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400728 }
729
David Benjaminf04c2e92016-12-06 13:35:25 -0500730 /* Always advertise the ClientHello version from the original maximum version,
731 * even on renegotiation. The static RSA key exchange uses this field, and
732 * some servers fail when it changes across handshakes. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400733 if (SSL_is_dtls(hs->ssl)) {
734 hs->client_version =
735 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
736 } else {
737 hs->client_version =
738 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
David Benjamine8d53502015-10-10 14:13:23 -0400739 }
740
David Benjamine29ea162016-03-23 16:10:44 -0400741 /* If the configured session has expired or was created at a disabled
742 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400743 if (ssl->session != NULL) {
Adam Langley364f7a62016-12-12 10:51:00 -0800744 if (ssl->session->is_server ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400745 !ssl_supports_version(hs, ssl->session->ssl_version) ||
746 (ssl->session->session_id_length == 0 &&
747 ssl->session->tlsext_ticklen == 0) ||
Steven Valdez4aa154e2016-07-29 14:32:55 -0400748 ssl->session->not_resumable ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400749 !ssl_session_is_time_valid(ssl, ssl->session)) {
David Benjamin4d0be242016-09-01 01:10:07 -0400750 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400751 }
David Benjamine8d53502015-10-10 14:13:23 -0400752 }
753
754 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
755 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400756 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400757 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400758 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400759 }
760
Steven Valdez520e1222017-06-13 12:45:25 -0400761 /* Initialize a random session ID for the experimental TLS 1.3 variant. */
Steven Valdezdbe01582017-07-14 10:39:28 -0400762 if (ssl->tls13_variant == tls13_experiment) {
Steven Valdez520e1222017-06-13 12:45:25 -0400763 hs->session_id_len = sizeof(hs->session_id);
764 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
765 return -1;
766 }
767 }
768
David Benjamin8c880a22016-12-03 02:20:34 -0500769 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400770 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400771 }
772
David Benjamin16315f72017-01-12 20:02:05 -0500773 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800774}
Adam Langley95c29f32014-06-20 12:00:00 -0700775
David Benjamin8d606e32017-06-15 22:43:04 -0400776static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900777 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400778 CBS hello_verify_request, cookie;
779 uint16_t server_version;
780
David Benjaminf71036e2017-01-21 14:49:39 -0500781 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700782 if (ret <= 0) {
783 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400784 }
785
786 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
787 ssl->d1->send_cookie = 0;
788 ssl->s3->tmp.reuse_message = 1;
789 return 1;
790 }
791
David Benjaminf71036e2017-01-21 14:49:39 -0500792 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400793 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
794 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
David Benjamin8d606e32017-06-15 22:43:04 -0400795 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
David Benjaminaa7734b2016-06-07 16:40:46 -0400796 CBS_len(&hello_verify_request) != 0) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400797 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400798 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
799 return -1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400800 }
801
David Benjamin17cf2cb2016-12-13 01:07:13 -0500802 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400803 ssl->d1->cookie_len = CBS_len(&cookie);
804
805 ssl->d1->send_cookie = 1;
806 return 1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400807}
808
David Benjaminb853f312017-07-14 18:40:34 -0400809static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out) {
810 SSL *const ssl = hs->ssl;
811 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
812 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
813 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
814 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
815 return 0;
816 }
817
818 CBS server_hello;
819 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
820 if (!CBS_get_u16(&server_hello, out)) {
821 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
822 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
823 return 0;
824 }
825
826 /* The server version may also be in the supported_versions extension if
827 * applicable. */
828 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO ||
829 *out != TLS1_2_VERSION) {
830 return 1;
831 }
832
833 uint8_t sid_length;
834 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
835 !CBS_get_u8(&server_hello, &sid_length) ||
836 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
837 1 /* compression_method */)) {
838 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
839 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
840 return 0;
841 }
842
843 /* The extensions block may not be present. */
844 if (CBS_len(&server_hello) == 0) {
845 return 1;
846 }
847
848 CBS extensions;
849 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
850 CBS_len(&server_hello) != 0) {
851 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
852 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
853 return 0;
854 }
855
856 int have_supported_versions;
857 CBS supported_versions;
858 const SSL_EXTENSION_TYPE ext_types[] = {
859 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
860 &supported_versions},
861 };
862
863 uint8_t alert = SSL_AD_DECODE_ERROR;
864 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
865 OPENSSL_ARRAY_SIZE(ext_types),
866 1 /* ignore unknown */)) {
867 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
868 return 0;
869 }
870
871 if (have_supported_versions &&
872 (!CBS_get_u16(&supported_versions, out) ||
873 CBS_len(&supported_versions) != 0)) {
874 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
875 return 0;
876 }
877
878 return 1;
879}
880
David Benjaminc3c88822016-11-14 10:32:04 +0900881static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
882 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -0500883 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700884 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500885 uint32_t err = ERR_peek_error();
886 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
887 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
888 /* Add a dedicated error code to the queue for a handshake_failure alert
889 * in response to ClientHello. This matches NSS's client behavior and
890 * gives a better error on a (probable) failure to negotiate initial
891 * parameters. Note: this error code comes after the original one.
892 *
893 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400894 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500895 }
David Benjamin09eb6552016-07-08 14:32:11 -0700896 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
David Benjaminb853f312017-07-14 18:40:34 -0400899 uint16_t server_version;
900 if (!parse_server_version(hs, &server_version)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400901 return -1;
902 }
903
Steven Valdez8f36c512017-06-20 10:55:02 -0400904 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400905 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400906 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
907 return -1;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400908 }
909
David Benjamin0d56f882015-12-19 17:05:56 -0500910 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
911 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400912 ssl->version = server_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500913 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800914 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500915 ssl->s3->have_version = 1;
Steven Valdez8f36c512017-06-20 10:55:02 -0400916 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400917 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400918 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
919 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800920 }
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Steven Valdez143e8b32016-07-11 13:19:03 -0400922 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500923 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900924 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400925 return 1;
926 }
927
Steven Valdez2d850622017-01-11 11:34:52 -0500928 if (hs->early_data_offered) {
929 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400930 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
931 return -1;
Steven Valdez2d850622017-01-11 11:34:52 -0500932 }
933
David Benjamin6e4fc332016-11-17 16:43:08 +0900934 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400935
David Benjamin276b7e82017-01-21 14:13:39 -0500936 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400937 return -1;
938 }
939
David Benjaminb853f312017-07-14 18:40:34 -0400940 CBS server_hello, server_random, session_id;
941 uint16_t cipher_suite;
942 uint8_t compression_method;
943 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
944 if (!CBS_skip(&server_hello, 2 /* version */) ||
945 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400946 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
947 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
948 !CBS_get_u16(&server_hello, &cipher_suite) ||
949 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400950 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400951 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
952 return -1;
Steven Valdez143e8b32016-07-11 13:19:03 -0400953 }
954
Adam Langley1bea1732014-12-17 19:06:57 -0800955 /* Copy over the server random. */
David Benjamind3bca042017-04-08 11:05:04 -0400956 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
957 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700958
David Benjamin55108632016-08-11 22:01:18 -0400959 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
960 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400961
David Benjamin0d56f882015-12-19 17:05:56 -0500962 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400963 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500964 CBS_mem_equal(&session_id, ssl->session->session_id,
965 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400966 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800967 } else {
968 /* The session wasn't resumed. Create a fresh SSL_SESSION to
969 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400970 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900971 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400972 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
973 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800974 }
975 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500976 hs->new_session->session_id_length = CBS_len(&session_id);
977 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
978 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800979 }
David Benjamina03d95d2014-07-12 19:49:07 -0400980
David Benjamind2cb1c12016-11-02 17:49:09 -0400981 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800982 if (c == NULL) {
983 /* unknown cipher */
David Benjamin3570d732015-06-29 00:28:17 -0400984 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400985 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
986 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800987 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400988
989 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500990 uint32_t mask_a, mask_k;
991 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
992 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700993 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400994 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
995 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
David Benjamin3570d732015-06-29 00:28:17 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400997 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
998 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800999 }
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Steven Valdez87eab492016-06-27 16:34:59 -04001001 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001002 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001004 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1005 return -1;
David Benjaminece3de92015-03-16 18:02:20 -04001006 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001007 if (ssl->session->cipher != c) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001008 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001009 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1010 return -1;
Steven Valdez803c77a2016-09-06 14:13:43 -04001011 }
Steven Valdez4aa154e2016-07-29 14:32:55 -04001012 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
1013 /* This is actually a client application bug. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001014 OPENSSL_PUT_ERROR(SSL,
1015 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001016 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1017 return -1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001018 }
David Benjamin0623bce2015-12-25 15:40:14 -05001019 } else {
David Benjamin45738dd2017-02-09 20:01:26 -05001020 hs->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -08001021 }
David Benjamin45738dd2017-02-09 20:01:26 -05001022 hs->new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -05001023
David Benjaminf71036e2017-01-21 14:49:39 -05001024 /* Now that the cipher is known, initialize the handshake hash and hash the
1025 * ServerHello. */
Steven Valdez908ac192017-01-12 13:17:07 -05001026 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
1027 c->algorithm_prf) ||
1028 !ssl_hash_current_message(hs)) {
David Benjamind3bca042017-04-08 11:05:04 -04001029 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1030 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001031 }
1032
Steven Valdez2b8415e2016-06-30 13:27:23 -04001033 /* If doing a full handshake, the server may request a client certificate
1034 * which requires hashing the handshake transcript. Otherwise, the handshake
1035 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001036 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -05001037 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez908ac192017-01-12 13:17:07 -05001038 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001039 }
David Benjamina03d95d2014-07-12 19:49:07 -04001040
Adam Langley1bea1732014-12-17 19:06:57 -08001041 /* Only the NULL compression algorithm is supported. */
1042 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001043 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -04001044 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1045 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001046 }
Adam Langley95c29f32014-06-20 12:00:00 -07001047
Adam Langley1bea1732014-12-17 19:06:57 -08001048 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -05001049 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -04001050 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001051 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001052 }
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langley1bea1732014-12-17 19:06:57 -08001054 /* There should be nothing left over in the record. */
1055 if (CBS_len(&server_hello) != 0) {
1056 /* wrong packet length */
David Benjamin639846e2016-09-09 11:41:18 -04001057 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -04001058 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1059 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001060 }
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Steven Valdez87eab492016-06-27 16:34:59 -04001062 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -05001063 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -05001064 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001065 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001066 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001067 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001068 }
David Benjamind3bca042017-04-08 11:05:04 -04001069 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1070 return -1;
Adam Langleyba5934b2015-06-02 10:50:35 -07001071 }
1072
Adam Langley1bea1732014-12-17 19:06:57 -08001073 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001074}
Adam Langley95c29f32014-06-20 12:00:00 -07001075
David Benjaminc3c88822016-11-14 10:32:04 +09001076static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1077 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001078 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001079 if (ret <= 0) {
1080 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001081 }
Adam Langley95c29f32014-06-20 12:00:00 -07001082
David Benjaminf71036e2017-01-21 14:49:39 -05001083 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001084 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001085 return -1;
1086 }
1087
David Benjamin5c900c82016-07-13 23:03:26 -04001088 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001089 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001090
Adam Langleyc68e5b92017-02-08 13:33:15 -08001091 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001092 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001093 EVP_PKEY_free(hs->peer_pubkey);
1094 hs->peer_pubkey = NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001095 hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
1096 &cbs, ssl->ctx->pool);
1097 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001098 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001099 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
David Benjamin45738dd2017-02-09 20:01:26 -05001102 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
Adam Langley68e71242016-12-12 11:06:16 -08001103 CBS_len(&cbs) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001104 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin3570d732015-06-29 00:28:17 -04001105 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001106 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001107 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001108 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001109
Adam Langley05672202016-12-13 12:05:49 -08001110 if (!ssl_check_leaf_certificate(
David Benjamin45738dd2017-02-09 20:01:26 -05001111 hs, hs->peer_pubkey,
1112 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001113 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001114 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001115 }
1116
David Benjamina58baaf2017-02-28 20:54:28 -05001117 /* Disallow the server certificate from changing during a renegotiation. See
1118 * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
1119 * so this check is sufficient. */
1120 if (ssl->s3->established_session != NULL) {
1121 if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
1122 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
1123 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1124 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1125 return -1;
1126 }
1127
1128 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
1129 const CRYPTO_BUFFER *old_cert =
1130 sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
1131 const CRYPTO_BUFFER *new_cert =
1132 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
1133 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
1134 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
1135 CRYPTO_BUFFER_data(new_cert),
1136 CRYPTO_BUFFER_len(old_cert)) != 0) {
1137 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1138 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1139 return -1;
1140 }
1141 }
1142 }
1143
David Benjamin5c900c82016-07-13 23:03:26 -04001144 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001145}
1146
David Benjaminc3c88822016-11-14 10:32:04 +09001147static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1148 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001149 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001150 if (ret <= 0) {
1151 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001152 }
1153
1154 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1155 /* A server may send status_request in ServerHello and then change
1156 * its mind about sending CertificateStatus. */
1157 ssl->s3->tmp.reuse_message = 1;
1158 return 1;
1159 }
1160
Steven Valdez908ac192017-01-12 13:17:07 -05001161 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001162 return -1;
1163 }
1164
David Benjamin8d606e32017-06-15 22:43:04 -04001165 CBS certificate_status, ocsp_response;
1166 uint8_t status_type;
David Benjamin09eb6552016-07-08 14:32:11 -07001167 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001168 if (!CBS_get_u8(&certificate_status, &status_type) ||
1169 status_type != TLSEXT_STATUSTYPE_ocsp ||
1170 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1171 CBS_len(&ocsp_response) == 0 ||
1172 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001173 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001174 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1175 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001176 }
1177
David Benjamin45738dd2017-02-09 20:01:26 -05001178 if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
1179 &hs->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001180 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001181 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1182 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001183 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001184
David Benjamin8d606e32017-06-15 22:43:04 -04001185 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001186}
1187
David Benjaminc3c88822016-11-14 10:32:04 +09001188static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1189 SSL *const ssl = hs->ssl;
Adam Langley2a3b3432017-02-10 13:56:48 -08001190 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
David Benjamine455e512016-08-01 20:11:13 -04001191 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001192 }
1193
Adam Langley9498e742016-07-18 10:17:16 -07001194 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001195}
1196
David Benjaminc3c88822016-11-14 10:32:04 +09001197static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1198 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -08001199 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001200 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001201
David Benjaminf71036e2017-01-21 14:49:39 -05001202 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001203 if (ret <= 0) {
1204 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001205 }
1206
David Benjamin0d56f882015-12-19 17:05:56 -05001207 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001208 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001209 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001210 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001211 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001212 return -1;
1213 }
1214
David Benjamin0d56f882015-12-19 17:05:56 -05001215 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001216 return 1;
1217 }
1218
Steven Valdez908ac192017-01-12 13:17:07 -05001219 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001220 return -1;
1221 }
1222
Adam Langley1bea1732014-12-17 19:06:57 -08001223 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001224 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001225 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001226 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001227
David Benjamin45738dd2017-02-09 20:01:26 -05001228 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1229 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001230
1231 if (alg_a & SSL_aPSK) {
1232 CBS psk_identity_hint;
1233
1234 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1235 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1236 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001237 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001238 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1239 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001240 }
1241
1242 /* Store PSK identity hint for later use, hint is used in
1243 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1244 * identity hint can be as long as the maximum length of a PSK identity.
1245 * Also do not allow NULL characters; identities are saved as C strings.
1246 *
1247 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1248 * a specific identity. */
1249 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1250 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001251 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -04001252 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1253 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001254 }
1255
David Benjamin78679342016-09-16 19:42:05 -04001256 /* Save non-empty identity hints as a C string. Empty identity hints we
1257 * treat as missing. Plain PSK makes it possible to send either no hint
1258 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1259 * empty hint. Having different capabilities is odd, so we interpret empty
1260 * and missing as identical. */
1261 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001262 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001263 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001264 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1265 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001266 }
1267 }
1268
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001269 if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001270 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001271 uint8_t group_type;
1272 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001273 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001274 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1275 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001276 !CBS_get_u16(&server_key_exchange, &group_id) ||
1277 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -04001278 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001279 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1280 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001281 }
David Benjamin45738dd2017-02-09 20:01:26 -05001282 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001283
Steven Valdezce902a92016-05-17 11:47:53 -04001284 /* Ensure the group is consistent with preferences. */
1285 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001286 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -04001287 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1288 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001289 }
David Benjamin4298d772015-12-19 00:18:25 -05001290
Matt Braithwaite053931e2016-05-25 12:06:05 -07001291 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001292 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1293 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001294 goto err;
1295 }
Adam Langley1bea1732014-12-17 19:06:57 -08001296 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -04001297 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001298 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1299 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001300 }
1301
1302 /* At this point, |server_key_exchange| contains the signature, if any, while
1303 * |server_key_exchange_orig| contains the entire message. From that, derive
1304 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001305 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001306 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1307 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1308
David Benjaminf4958e72015-06-07 11:00:53 -04001309 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001310 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001311 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001312 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001313 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001314 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001315 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1316 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001317 }
David Benjamin8d606e32017-06-15 22:43:04 -04001318 uint8_t alert = SSL_AD_DECODE_ERROR;
1319 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1320 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1321 goto err;
David Benjamin6e807652015-11-02 12:02:20 -05001322 }
David Benjamin45738dd2017-02-09 20:01:26 -05001323 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001324 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1325 hs->peer_pubkey)) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001326 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001327 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1328 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001329 }
1330
1331 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001332 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001333 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1334 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001335 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001336 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1337 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001338 }
1339
Steven Valdez2b8415e2016-06-30 13:27:23 -04001340 CBB transcript;
1341 uint8_t *transcript_data;
1342 size_t transcript_len;
1343 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1344 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1345 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1346 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1347 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1348 CBB_cleanup(&transcript);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001349 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001350 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1351 goto err;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001352 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001353
1354 int sig_ok = ssl_public_key_verify(
1355 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001356 hs->peer_pubkey, transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001357 OPENSSL_free(transcript_data);
1358
David Benjaminbf82aed2016-03-01 22:57:40 -05001359#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1360 sig_ok = 1;
1361 ERR_clear_error();
1362#endif
1363 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001364 /* bad signature */
David Benjamin3570d732015-06-29 00:28:17 -04001365 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001366 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1367 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001368 }
1369 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001370 /* PSK ciphers are the only supported certificate-less ciphers. */
1371 assert(alg_a == SSL_aPSK);
1372
Adam Langley1bea1732014-12-17 19:06:57 -08001373 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001374 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001375 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1376 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001377 }
1378 }
Adam Langley1bea1732014-12-17 19:06:57 -08001379 return 1;
1380
Adam Langley95c29f32014-06-20 12:00:00 -07001381err:
Adam Langley1bea1732014-12-17 19:06:57 -08001382 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001383 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001384 return -1;
1385}
Adam Langley95c29f32014-06-20 12:00:00 -07001386
David Benjaminc3c88822016-11-14 10:32:04 +09001387static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1388 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001389 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001390 if (msg_ret <= 0) {
1391 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001392 }
David Benjamin688d8df2014-11-02 23:06:42 -05001393
David Benjamin3dcec452016-06-07 17:13:28 -04001394 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001395 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001396 /* If we get here we don't need the handshake buffer as we won't be doing
1397 * client auth. */
Steven Valdez908ac192017-01-12 13:17:07 -05001398 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001399 return 1;
1400 }
David Benjamined439582014-07-14 19:13:02 -04001401
David Benjaminf71036e2017-01-21 14:49:39 -05001402 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001403 !ssl_hash_current_message(hs)) {
David Benjamine0332e82016-07-13 22:40:36 -04001404 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001405 }
Adam Langley95c29f32014-06-20 12:00:00 -07001406
David Benjamina5177cb2016-03-11 19:38:17 -05001407 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001408 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001409
David Benjamine0332e82016-07-13 22:40:36 -04001410 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001411 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001412 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001413 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001414 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001415 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001416 }
David Benjamined439582014-07-14 19:13:02 -04001417
David Benjaminc3c88822016-11-14 10:32:04 +09001418 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1419 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001420 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001421 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001422 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001423
David Benjamina1e9cab2015-12-30 00:08:49 -05001424 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001425 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001426 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001427 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001428 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001429 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001430 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001431 }
Adam Langley1bea1732014-12-17 19:06:57 -08001432 }
David Benjamined439582014-07-14 19:13:02 -04001433
Adam Langleyc68e5b92017-02-08 13:33:15 -08001434 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Langley34b4c822017-02-02 10:57:17 -08001435 STACK_OF(CRYPTO_BUFFER) *ca_names =
1436 ssl_parse_client_CA_list(ssl, &alert, &cbs);
1437 if (ca_names == NULL) {
David Benjamine0332e82016-07-13 22:40:36 -04001438 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1439 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001440 }
Adam Langley95c29f32014-06-20 12:00:00 -07001441
David Benjamin639846e2016-09-09 11:41:18 -04001442 if (CBS_len(&cbs) != 0) {
1443 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley34b4c822017-02-02 10:57:17 -08001444 sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
David Benjamin639846e2016-09-09 11:41:18 -04001445 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1446 return -1;
1447 }
1448
David Benjaminc3c88822016-11-14 10:32:04 +09001449 hs->cert_request = 1;
Adam Langley34b4c822017-02-02 10:57:17 -08001450 sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
1451 hs->ca_names = ca_names;
1452 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamine0332e82016-07-13 22:40:36 -04001453 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001454}
Adam Langley95c29f32014-06-20 12:00:00 -07001455
David Benjaminc3c88822016-11-14 10:32:04 +09001456static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1457 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001458 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001459 if (ret <= 0) {
1460 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001461 }
David Benjamin46062682014-07-14 19:14:32 -04001462
David Benjaminf71036e2017-01-21 14:49:39 -05001463 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001464 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001465 return -1;
1466 }
1467
David Benjamin09eb6552016-07-08 14:32:11 -07001468 /* ServerHelloDone is empty. */
1469 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001470 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001471 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001472 return -1;
1473 }
David Benjamin46062682014-07-14 19:14:32 -04001474
Adam Langley1bea1732014-12-17 19:06:57 -08001475 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001476}
Adam Langley1258b6a2014-06-20 12:00:00 -07001477
David Benjaminc3c88822016-11-14 10:32:04 +09001478static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1479 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001480 /* Call cert_cb to update the certificate. */
1481 if (ssl->cert->cert_cb) {
1482 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1483 if (ret < 0) {
1484 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001485 return -1;
1486 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001487 if (ret == 0) {
1488 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1489 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001490 return -1;
1491 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001492 }
1493
David Benjamin5edfc8c2016-12-10 15:46:58 -05001494 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001495 /* Without a client certificate, the handshake buffer may be released. */
Steven Valdez908ac192017-01-12 13:17:07 -05001496 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin5edfc8c2016-12-10 15:46:58 -05001497
David Benjamindaf207a2017-01-03 18:37:41 -05001498 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001499 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001500 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1501 SSL_AD_NO_CERTIFICATE)) {
1502 return -1;
1503 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001504 return 1;
1505 }
1506 }
1507
David Benjamina232a712017-03-30 15:51:53 -05001508 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001509 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001510 return -1;
1511 }
David Benjamin16315f72017-01-12 20:02:05 -05001512 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001513}
1514
David Benjamina3d76d02017-07-14 19:36:07 -04001515static_assert(sizeof(size_t) >= sizeof(unsigned),
1516 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001517
David Benjaminc3c88822016-11-14 10:32:04 +09001518static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1519 SSL *const ssl = hs->ssl;
David Benjamind781fc42017-07-12 16:25:57 -04001520 bssl::ScopedCBB cbb;
1521 CBB body;
1522 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001523 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjamind781fc42017-07-12 16:25:57 -04001524 return -1;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001525 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001526
David Benjamind781fc42017-07-12 16:25:57 -04001527 uint8_t *pms = NULL;
1528 size_t pms_len = 0;
David Benjamin45738dd2017-02-09 20:01:26 -05001529 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1530 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001531
David Benjaminae0eaaa2015-12-17 02:01:25 -05001532 /* If using a PSK key exchange, prepare the pre-shared key. */
1533 unsigned psk_len = 0;
1534 uint8_t psk[PSK_MAX_PSK_LEN];
1535 if (alg_a & SSL_aPSK) {
1536 if (ssl->psk_client_callback == NULL) {
1537 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1538 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001539 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001540
David Benjaminae0eaaa2015-12-17 02:01:25 -05001541 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001542 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001543 psk_len =
1544 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1545 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001546 if (psk_len == 0) {
1547 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1548 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1549 goto err;
1550 }
1551 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001552
David Benjamin45738dd2017-02-09 20:01:26 -05001553 OPENSSL_free(hs->new_session->psk_identity);
1554 hs->new_session->psk_identity = BUF_strdup(identity);
1555 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001556 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1557 goto err;
1558 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001559
David Benjaminae0eaaa2015-12-17 02:01:25 -05001560 /* Write out psk_identity. */
1561 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001562 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001563 !CBB_add_bytes(&child, (const uint8_t *)identity,
1564 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001565 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001566 goto err;
1567 }
1568 }
David Benjamin758d1272015-11-20 17:47:25 -05001569
David Benjaminae0eaaa2015-12-17 02:01:25 -05001570 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1571 if (alg_k & SSL_kRSA) {
1572 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001573 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001574 if (pms == NULL) {
1575 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1576 goto err;
1577 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001578
Adam Langleyd5157222016-12-12 11:37:43 -08001579 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001580 if (rsa == NULL) {
1581 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001582 goto err;
1583 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001584
David Benjaminf04c2e92016-12-06 13:35:25 -05001585 pms[0] = hs->client_version >> 8;
1586 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001587 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1588 goto err;
1589 }
1590
David Benjamin75836432016-06-17 18:48:29 -04001591 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001592 size_t enc_pms_len;
1593 /* In TLS, there is a length prefix. */
1594 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001595 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001596 goto err;
1597 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001598 enc_pms = &child;
1599 }
Adam Langley1bea1732014-12-17 19:06:57 -08001600
David Benjaminae0eaaa2015-12-17 02:01:25 -05001601 uint8_t *ptr;
1602 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1603 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1604 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001605 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001606 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001607 goto err;
1608 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001609 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001610 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001611 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001612 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001613 goto err;
1614 }
1615
David Benjamin4298d772015-12-19 00:18:25 -05001616 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001617 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001618 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1619 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001620 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1621 goto err;
1622 }
David Benjamin75836432016-06-17 18:48:29 -04001623 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001624 goto err;
1625 }
David Benjamin4298d772015-12-19 00:18:25 -05001626
1627 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001628 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1629 OPENSSL_free(hs->peer_key);
1630 hs->peer_key = NULL;
1631 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001632 } else if (alg_k & SSL_kPSK) {
1633 /* For plain PSK, other_secret is a block of 0s with the same length as
1634 * the pre-shared key. */
1635 pms_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001636 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001637 if (pms == NULL) {
1638 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1639 goto err;
1640 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001641 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001642 } else {
1643 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1644 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1645 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001646 }
1647
David Benjaminae0eaaa2015-12-17 02:01:25 -05001648 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1649 * key. */
1650 if (alg_a & SSL_aPSK) {
1651 CBB pms_cbb, child;
1652 uint8_t *new_pms;
1653 size_t new_pms_len;
1654
1655 CBB_zero(&pms_cbb);
1656 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1657 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1658 !CBB_add_bytes(&child, pms, pms_len) ||
1659 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1660 !CBB_add_bytes(&child, psk, psk_len) ||
1661 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1662 CBB_cleanup(&pms_cbb);
1663 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1664 goto err;
1665 }
1666 OPENSSL_cleanse(pms, pms_len);
1667 OPENSSL_free(pms);
1668 pms = new_pms;
1669 pms_len = new_pms_len;
1670 }
1671
1672 /* The message must be added to the finished hash before calculating the
1673 * master secret. */
David Benjamind781fc42017-07-12 16:25:57 -04001674 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001675 goto err;
1676 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001677
David Benjamin45738dd2017-02-09 20:01:26 -05001678 hs->new_session->master_key_length = tls1_generate_master_secret(
1679 hs, hs->new_session->master_key, pms, pms_len);
1680 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001681 goto err;
1682 }
David Benjaminfc02b592017-02-17 16:26:01 -05001683 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001684 OPENSSL_cleanse(pms, pms_len);
1685 OPENSSL_free(pms);
1686
David Benjamin16315f72017-01-12 20:02:05 -05001687 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001688
1689err:
David Benjaminae0eaaa2015-12-17 02:01:25 -05001690 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001691 OPENSSL_cleanse(pms, pms_len);
1692 OPENSSL_free(pms);
1693 }
1694 return -1;
1695}
Adam Langley1258b6a2014-06-20 12:00:00 -07001696
David Benjaminc3c88822016-11-14 10:32:04 +09001697static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1698 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001699 assert(ssl_has_private_key(ssl));
1700
David Benjamind781fc42017-07-12 16:25:57 -04001701 bssl::ScopedCBB cbb;
1702 CBB body, child;
1703 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001704 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjamind781fc42017-07-12 16:25:57 -04001705 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001706 }
1707
David Benjaminea9a0d52016-07-08 15:52:59 -07001708 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001709 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001710 return -1;
David Benjaminea9a0d52016-07-08 15:52:59 -07001711 }
David Benjamin429fdc02016-06-17 17:33:30 -04001712 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001713 /* Write out the digest type in TLS 1.2. */
1714 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001715 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001716 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001717 }
1718 }
David Benjaminfc825122015-12-18 01:57:43 -05001719
David Benjamin429fdc02016-06-17 17:33:30 -04001720 /* Set aside space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001721 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjamin429fdc02016-06-17 17:33:30 -04001722 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001723 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001724 !CBB_reserve(&child, &ptr, max_sig_len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001725 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001726 }
1727
Steven Valdez2b8415e2016-06-30 13:27:23 -04001728 size_t sig_len = max_sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001729 /* The SSL3 construction for CertificateVerify does not decompose into a
1730 * single final digest and signature, and must be special-cased. */
1731 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1732 if (ssl->cert->key_method != NULL) {
1733 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
David Benjamind781fc42017-07-12 16:25:57 -04001734 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001735 }
1736
David Benjamin44148742017-06-17 13:20:59 -04001737 uint8_t digest[EVP_MAX_MD_SIZE];
1738 size_t digest_len;
1739 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
1740 &digest_len, hs->new_session,
1741 signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001742 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001743 }
David Benjaminfc825122015-12-18 01:57:43 -05001744
David Benjamin44148742017-06-17 13:20:59 -04001745 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1746 int ok = pctx != NULL &&
1747 EVP_PKEY_sign_init(pctx) &&
1748 EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len);
1749 EVP_PKEY_CTX_free(pctx);
1750 if (!ok) {
David Benjamind781fc42017-07-12 16:25:57 -04001751 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001752 }
1753 } else {
1754 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1755 signature_algorithm,
1756 (const uint8_t *)hs->transcript.buffer->data,
1757 hs->transcript.buffer->length)) {
1758 case ssl_private_key_success:
1759 break;
1760 case ssl_private_key_failure:
David Benjamind781fc42017-07-12 16:25:57 -04001761 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001762 case ssl_private_key_retry:
1763 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind781fc42017-07-12 16:25:57 -04001764 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001765 }
David Benjaminfc825122015-12-18 01:57:43 -05001766 }
1767
David Benjaminfc825122015-12-18 01:57:43 -05001768 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001769 !ssl_add_message_cbb(ssl, cbb.get())) {
1770 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001771 }
1772
David Benjamin44148742017-06-17 13:20:59 -04001773 /* The handshake buffer is no longer necessary. */
1774 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin16315f72017-01-12 20:02:05 -05001775 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001776}
1777
David Benjaminc3c88822016-11-14 10:32:04 +09001778static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1779 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001780 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001781 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001782
David Benjamin75836432016-06-17 18:48:29 -04001783 CBB cbb, body, child;
1784 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1785 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001786 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1787 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001788 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001789 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001790 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001791 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1792 CBB_cleanup(&cbb);
1793 return -1;
1794 }
1795
David Benjamin16315f72017-01-12 20:02:05 -05001796 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001797}
1798
David Benjaminc3c88822016-11-14 10:32:04 +09001799static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1800 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001801 if (!ssl_do_channel_id_callback(ssl)) {
1802 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001803 }
1804
David Benjamin96e97b12015-10-10 16:30:22 -04001805 if (ssl->tlsext_channel_id_private == NULL) {
1806 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001807 return -1;
1808 }
Adam Langley1bea1732014-12-17 19:06:57 -08001809
Nick Harper60a85cb2016-09-23 16:25:11 -07001810 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001811 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001812 !tls1_write_channel_id(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001813 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001814 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001815 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001816 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001817 }
1818
David Benjamin16315f72017-01-12 20:02:05 -05001819 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001820}
1821
David Benjaminc3c88822016-11-14 10:32:04 +09001822static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1823 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001824 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001825 if (ret <= 0) {
1826 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001827 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001828
David Benjaminf71036e2017-01-21 14:49:39 -05001829 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001830 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001831 return -1;
1832 }
1833
David Benjamin9f1dc822016-06-07 17:03:46 -04001834 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001835 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001836 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001837 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001838 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1839 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001840 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001841 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001842 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001843 }
1844
David Benjamin9f1dc822016-06-07 17:03:46 -04001845 if (CBS_len(&ticket) == 0) {
1846 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001847 * negotiating the extension. The value of |ticket_expected| is checked in
1848 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001849 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001850 return 1;
1851 }
1852
Steven Valdez87eab492016-06-27 16:34:59 -04001853 int session_renewed = ssl->session != NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001854 SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001855 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001856 /* The server is sending a new ticket for an existing session. Sessions are
1857 * immutable once established, so duplicate all but the ticket of the
1858 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001859 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001860 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001861 /* This should never happen. */
1862 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1863 goto err;
1864 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001865 }
1866
David Benjamin123db572016-11-03 16:59:25 -04001867 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001868 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001869
Steven Valdez87eab492016-06-27 16:34:59 -04001870 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001871 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1872 goto err;
1873 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001874 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001875
1876 /* Generate a session ID for this session based on the session ticket. We use
1877 * the session ID mechanism for detecting ticket resumption. This also fits in
1878 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001879 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1880 session->session_id, &session->session_id_length,
1881 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001882 goto err;
1883 }
1884
Steven Valdez87eab492016-06-27 16:34:59 -04001885 if (session_renewed) {
1886 session->not_resumable = 0;
1887 SSL_SESSION_free(ssl->session);
1888 ssl->session = session;
1889 }
1890
David Benjamin454aa4c2016-03-10 08:43:27 -05001891 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001892
David Benjamin9f1dc822016-06-07 17:03:46 -04001893err:
Steven Valdez87eab492016-06-27 16:34:59 -04001894 if (session_renewed) {
1895 SSL_SESSION_free(session);
1896 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001897 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001898}