blob: f018c0ec1ca39069ea8311a84b95df560e717fcc [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 Benjamin31b0c9b2017-07-20 14:49:15 -0400155#include <utility>
156
David Benjaminabbbee12016-10-31 19:20:42 -0400157#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400158#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700159#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400160#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400161#include <openssl/ec_key.h>
162#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400163#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700164#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700165#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400166#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400167#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin17cf2cb2016-12-13 01:07:13 -0500169#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjamin86e95b82017-07-18 16:34:25 -0400173namespace bssl {
174
David Benjaminc3c88822016-11-14 10:32:04 +0900175static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
David Benjamin8d606e32017-06-15 22:43:04 -0400176static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900177static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
178static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
179static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900180static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
181static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
182static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
183static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
184static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
185static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
186static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
187static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
188static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400189
David Benjamince8c9d22016-11-14 10:45:16 +0900190int ssl3_connect(SSL_HANDSHAKE *hs) {
191 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800192 int ret = -1;
David Benjaminbeb47022014-11-30 02:58:52 -0500193
David Benjamin0d56f882015-12-19 17:05:56 -0500194 assert(ssl->handshake_func == ssl3_connect);
195 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langley1bea1732014-12-17 19:06:57 -0800197 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500198 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400201 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400202 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500203 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800204 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley1bea1732014-12-17 19:06:57 -0800206 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900207 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800208 if (ret <= 0) {
209 goto end;
210 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400211
David Benjamince079fd2016-08-02 16:22:34 -0400212 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
Steven Valdez2d850622017-01-11 11:34:52 -0500213 if (hs->early_data_offered) {
214 if (!tls13_init_early_key_schedule(hs) ||
215 !tls13_advance_key_schedule(hs, ssl->session->master_key,
216 ssl->session->master_key_length) ||
217 !tls13_derive_early_secrets(hs) ||
218 !tls13_set_traffic_key(ssl, evp_aead_seal,
219 hs->early_traffic_secret,
220 hs->hash_len)) {
221 ret = -1;
222 goto end;
223 }
Steven Valdeze831a812017-03-09 14:56:07 -0500224 hs->next_state = SSL3_ST_WRITE_EARLY_DATA;
225 } else {
226 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
Steven Valdez2d850622017-01-11 11:34:52 -0500227 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400228 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500229 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400230 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500231 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800232 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700233
David Benjaminaa7734b2016-06-07 16:40:46 -0400234 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400235 assert(SSL_is_dtls(ssl));
David Benjamin8d606e32017-06-15 22:43:04 -0400236 ret = dtls1_get_hello_verify_request(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400237 if (ret <= 0) {
238 goto end;
239 }
240 if (ssl->d1->send_cookie) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500241 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400242 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500243 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400244 }
245 break;
246
Steven Valdeze831a812017-03-09 14:56:07 -0500247 case SSL3_ST_WRITE_EARLY_DATA:
248 /* Stash the early data session, so connection properties may be queried
249 * out of it. */
250 hs->in_early_data = 1;
Steven Valdeze831a812017-03-09 14:56:07 -0500251 SSL_SESSION_up_ref(ssl->session);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400252 hs->early_session.reset(ssl->session);
Steven Valdeze831a812017-03-09 14:56:07 -0500253
254 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
255 hs->can_early_write = 1;
256 ret = 1;
257 goto end;
258
Adam Langley1bea1732014-12-17 19:06:57 -0800259 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900260 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500261 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400262 break;
263 }
Adam Langley1bea1732014-12-17 19:06:57 -0800264 if (ret <= 0) {
265 goto end;
266 }
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Steven Valdez87eab492016-06-27 16:34:59 -0400268 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500269 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800270 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500271 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800272 }
Adam Langley1bea1732014-12-17 19:06:57 -0800273 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400274
Adam Langley1bea1732014-12-17 19:06:57 -0800275 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500276 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900277 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800278 if (ret <= 0) {
279 goto end;
280 }
Adam Langley1bea1732014-12-17 19:06:57 -0800281 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500282 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800283 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamin9f1dc822016-06-07 17:03:46 -0400285 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900286 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900287 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400288 if (ret <= 0) {
289 goto end;
290 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400291 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500292 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400293 break;
294
Paul Lietar8f1c2682015-08-18 12:21:54 +0100295 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500296 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin3a1dd462017-07-11 16:13:10 -0400297 switch (ssl_verify_peer_cert(hs)) {
298 case ssl_verify_ok:
299 break;
300 case ssl_verify_invalid:
301 ret = -1;
302 goto end;
303 case ssl_verify_retry:
304 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
305 ret = -1;
306 goto end;
David Benjamin47749a62016-06-13 13:56:33 -0400307 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100308 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500309 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100310 break;
311
Adam Langley1bea1732014-12-17 19:06:57 -0800312 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900313 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800314 if (ret <= 0) {
315 goto end;
316 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500317 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800318 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langley1bea1732014-12-17 19:06:57 -0800320 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500321 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900322 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400323 if (ret <= 0) {
324 goto end;
325 }
Adam Langley1bea1732014-12-17 19:06:57 -0800326 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500327 hs->state = SSL3_ST_CR_SRVR_DONE_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_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900331 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800332 if (ret <= 0) {
333 goto end;
334 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500335 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800336 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langley1bea1732014-12-17 19:06:57 -0800338 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900339 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900340 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400341 if (ret <= 0) {
342 goto end;
343 }
Adam Langley1bea1732014-12-17 19:06:57 -0800344 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500345 hs->state = SSL3_ST_CW_KEY_EXCH_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_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900349 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800350 if (ret <= 0) {
351 goto end;
352 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500353 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800354 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langley1bea1732014-12-17 19:06:57 -0800356 case SSL3_ST_CW_CERT_VRFY_A:
David Benjamina660e7a2017-01-02 08:15:35 -0500357 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900358 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400359 if (ret <= 0) {
360 goto end;
361 }
Adam Langley1bea1732014-12-17 19:06:57 -0800362 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500363 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800364 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700365
David Benjamin352d0a92016-06-28 11:22:02 -0400366 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500367 if (!ssl->method->add_change_cipher_spec(ssl) ||
368 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800369 ret = -1;
370 goto end;
371 }
Adam Langley95c29f32014-06-20 12:00:00 -0700372
David Benjamindaf207a2017-01-03 18:37:41 -0500373 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800374 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley1bea1732014-12-17 19:06:57 -0800376 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900377 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900378 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400379 if (ret <= 0) {
380 goto end;
381 }
Adam Langley1bea1732014-12-17 19:06:57 -0800382 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500383 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800384 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley1bea1732014-12-17 19:06:57 -0800386 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400387 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900388 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400389 if (ret <= 0) {
390 goto end;
391 }
Adam Langley1bea1732014-12-17 19:06:57 -0800392 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500393 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800394 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langley1bea1732014-12-17 19:06:57 -0800396 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500397 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800398 if (ret <= 0) {
399 goto end;
400 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500401 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Steven Valdez87eab492016-06-27 16:34:59 -0400403 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500404 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800405 } else {
406 /* This is a non-resumption handshake. If it involves ChannelID, then
407 * record the handshake hashes at this point in the session so that
408 * any resumption of this session with ChannelID can sign those
409 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500410 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700411 if (ret <= 0) {
412 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800413 }
David Benjamin0d56f882015-12-19 17:05:56 -0500414 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
415 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500416 /* No False Start on renegotiation (would complicate the state
417 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500418 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500419 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800420 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500421 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800422 }
423 }
Adam Langley1bea1732014-12-17 19:06:57 -0800424 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700425
David Benjamin9f1dc822016-06-07 17:03:46 -0400426 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500427 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900428 hs->in_false_start = 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500429 hs->can_early_write = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400430 ret = 1;
431 goto end;
432
Adam Langley1bea1732014-12-17 19:06:57 -0800433 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900434 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900435 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400436 if (ret <= 0) {
437 goto end;
438 }
Adam Langley1bea1732014-12-17 19:06:57 -0800439 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500440 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800441 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langley1bea1732014-12-17 19:06:57 -0800443 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400444 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500445 if (ret <= 0) {
446 goto end;
447 }
448
David Benjamin67739722016-11-17 17:03:59 +0900449 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800450 ret = -1;
451 goto end;
452 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500453 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800454 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langley1bea1732014-12-17 19:06:57 -0800456 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900457 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800458 if (ret <= 0) {
459 goto end;
460 }
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Steven Valdez87eab492016-06-27 16:34:59 -0400462 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500463 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800464 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500465 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800466 }
Adam Langley1bea1732014-12-17 19:06:57 -0800467 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langley1bea1732014-12-17 19:06:57 -0800469 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500470 ret = ssl->method->flush_flight(ssl);
471 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800472 goto end;
473 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500474 hs->state = hs->next_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800475 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700476
Steven Valdez681eb6a2016-12-19 13:19:29 -0500477 case SSL_ST_TLS13: {
478 int early_return = 0;
479 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400480 if (ret <= 0) {
481 goto end;
482 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500483
484 if (early_return) {
485 ret = 1;
486 goto end;
487 }
488
David Benjamin77458a42017-02-10 23:33:27 -0500489 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400490 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500491 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400492
David Benjamin77458a42017-02-10 23:33:27 -0500493 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin9bbdf582017-08-02 19:46:29 -0400494 ssl->method->on_handshake_complete(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Steven Valdez87eab492016-06-27 16:34:59 -0400496 SSL_SESSION_free(ssl->s3->established_session);
497 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400498 SSL_SESSION_up_ref(ssl->session);
499 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400500 } else {
501 /* We make a copy of the session in order to maintain the immutability
502 * of the new established_session due to False Start. The caller may
503 * have taken a reference to the temporary session. */
504 ssl->s3->established_session =
David Benjamin31b0c9b2017-07-20 14:49:15 -0400505 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL)
506 .release();
Steven Valdez87eab492016-06-27 16:34:59 -0400507 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400508 ret = -1;
509 goto end;
510 }
David Benjamin5ef40c62017-08-23 21:28:29 -0700511 /* Renegotiations do not participate in session resumption. */
512 if (!ssl->s3->initial_handshake_complete) {
513 ssl->s3->established_session->not_resumable = 0;
514 }
Steven Valdez87eab492016-06-27 16:34:59 -0400515
David Benjamin31b0c9b2017-07-20 14:49:15 -0400516 hs->new_session.reset();
Steven Valdez87eab492016-06-27 16:34:59 -0400517 }
518
David Benjamin77458a42017-02-10 23:33:27 -0500519 hs->state = SSL_ST_OK;
520 break;
521
522 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500523 ssl->s3->initial_handshake_complete = 1;
David Benjamin5ef40c62017-08-23 21:28:29 -0700524 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langley1bea1732014-12-17 19:06:57 -0800526 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400527 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800528 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500529 }
Steven Valdez87eab492016-06-27 16:34:59 -0400530
Adam Langley1bea1732014-12-17 19:06:57 -0800531 default:
David Benjamin3570d732015-06-29 00:28:17 -0400532 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800533 ret = -1;
534 goto end;
535 }
536
David Benjamin16b1b1d2017-02-10 23:41:14 -0500537 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400538 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800539 }
Adam Langley1bea1732014-12-17 19:06:57 -0800540 }
541
Adam Langley95c29f32014-06-20 12:00:00 -0700542end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400543 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800544 return ret;
545}
Adam Langley95c29f32014-06-20 12:00:00 -0700546
David Benjamin2fe6e222017-01-25 02:18:43 -0500547/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
548 * disabled algorithms. */
549static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
550 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500551 *out_mask_a = 0;
552 *out_mask_k = 0;
553
David Benjamin2fe6e222017-01-25 02:18:43 -0500554 /* PSK requires a client callback. */
555 if (ssl->psk_client_callback == NULL) {
556 *out_mask_a |= SSL_aPSK;
557 *out_mask_k |= SSL_kPSK;
558 }
559}
560
David Benjamin68161cb2017-06-20 14:49:43 -0400561static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
562 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500563 uint32_t mask_a, mask_k;
564 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700565
David Benjamine8d53502015-10-10 14:13:23 -0400566 CBB child;
567 if (!CBB_add_u16_length_prefixed(out, &child)) {
568 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800569 }
570
David Benjamin65ac9972016-09-02 21:35:25 -0400571 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
572 if (ssl->ctx->grease_enabled &&
573 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
574 return 0;
575 }
576
David Benjaminabbbee12016-10-31 19:20:42 -0400577 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
578 * hardware support. */
David Benjamin68161cb2017-06-20 14:49:43 -0400579 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400580 if (!EVP_has_aes_hardware() &&
581 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
582 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500583 }
David Benjaminabbbee12016-10-31 19:20:42 -0400584 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
585 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
586 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400587 }
David Benjaminabbbee12016-10-31 19:20:42 -0400588 if (EVP_has_aes_hardware() &&
589 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400590 return 0;
591 }
592 }
593
David Benjamin68161cb2017-06-20 14:49:43 -0400594 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400595 int any_enabled = 0;
David Benjaminee910bf2017-07-25 22:36:00 -0400596 for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400597 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500598 if ((cipher->algorithm_mkey & mask_k) ||
599 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400600 continue;
601 }
David Benjamin68161cb2017-06-20 14:49:43 -0400602 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
603 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400604 continue;
605 }
606 any_enabled = 1;
607 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
608 return 0;
609 }
610 }
611
612 /* If all ciphers were disabled, return the error to the caller. */
David Benjamin68161cb2017-06-20 14:49:43 -0400613 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400614 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
615 return 0;
616 }
David Benjamine8d53502015-10-10 14:13:23 -0400617 }
618
619 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
620 * added. */
David Benjamin68161cb2017-06-20 14:49:43 -0400621 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400622 !ssl->s3->initial_handshake_complete) {
623 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
624 return 0;
625 }
David Benjamine8d53502015-10-10 14:13:23 -0400626 }
627
David Benjamine0ff7672016-09-19 18:40:03 -0400628 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200629 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
630 return 0;
631 }
David Benjamine8d53502015-10-10 14:13:23 -0400632 }
633
634 return CBB_flush(out);
635}
636
David Benjamin8c880a22016-12-03 02:20:34 -0500637int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
638 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400639 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -0400640 CBB body;
641 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
642 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400643 }
644
Steven Valdez5440fe02016-07-18 12:40:30 -0400645 /* Renegotiations do not participate in session resumption. */
Steven Valdez520e1222017-06-13 12:45:25 -0400646 int has_session_id = ssl->session != NULL &&
647 !ssl->s3->initial_handshake_complete &&
648 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400649
650 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500651 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400652 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400653 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400654 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400655 }
656
Steven Valdez520e1222017-06-13 12:45:25 -0400657 if (has_session_id) {
658 if (!CBB_add_bytes(&child, ssl->session->session_id,
659 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400660 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400661 }
662 } else {
663 /* In TLS 1.3 experimental encodings, send a fake placeholder session ID
664 * when we do not otherwise have one to send. */
665 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400666 ssl->tls13_variant == tls13_experiment &&
Steven Valdez520e1222017-06-13 12:45:25 -0400667 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400668 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400669 }
670 }
671
David Benjamince079fd2016-08-02 16:22:34 -0400672 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400673 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400674 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400675 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400676 }
677 }
678
679 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400680 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400681 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400682 !CBB_add_u8(&body, 1 /* one compression method */) ||
683 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500684 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400685 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400686 }
687
Steven Valdeza833c352016-11-01 13:39:36 -0400688 uint8_t *msg = NULL;
689 size_t len;
David Benjamind781fc42017-07-12 16:25:57 -0400690 if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) {
691 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400692 }
693
694 /* Now that the length prefixes have been computed, fill in the placeholder
695 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500696 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500697 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400698 OPENSSL_free(msg);
David Benjamind781fc42017-07-12 16:25:57 -0400699 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400700 }
701
David Benjamindaf207a2017-01-03 18:37:41 -0500702 return ssl->method->add_message(ssl, msg, len);
Steven Valdez5440fe02016-07-18 12:40:30 -0400703}
704
David Benjaminc3c88822016-11-14 10:32:04 +0900705static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
706 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400707 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
708 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
David Benjamin6dc8bf62017-07-19 16:38:21 -0400709 if (!hs->transcript.Init()) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500710 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
711 return -1;
712 }
713
David Benjamin68161cb2017-06-20 14:49:43 -0400714 /* Freeze the version range. */
715 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400716 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400717 }
718
David Benjaminf04c2e92016-12-06 13:35:25 -0500719 /* Always advertise the ClientHello version from the original maximum version,
720 * even on renegotiation. The static RSA key exchange uses this field, and
721 * some servers fail when it changes across handshakes. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400722 if (SSL_is_dtls(hs->ssl)) {
723 hs->client_version =
724 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
725 } else {
726 hs->client_version =
727 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
David Benjamine8d53502015-10-10 14:13:23 -0400728 }
729
David Benjamine29ea162016-03-23 16:10:44 -0400730 /* If the configured session has expired or was created at a disabled
731 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400732 if (ssl->session != NULL) {
Adam Langley364f7a62016-12-12 10:51:00 -0800733 if (ssl->session->is_server ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400734 !ssl_supports_version(hs, ssl->session->ssl_version) ||
735 (ssl->session->session_id_length == 0 &&
736 ssl->session->tlsext_ticklen == 0) ||
Steven Valdez4aa154e2016-07-29 14:32:55 -0400737 ssl->session->not_resumable ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400738 !ssl_session_is_time_valid(ssl, ssl->session)) {
David Benjamin4d0be242016-09-01 01:10:07 -0400739 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400740 }
David Benjamine8d53502015-10-10 14:13:23 -0400741 }
742
743 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
744 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400745 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400746 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400747 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400748 }
749
Steven Valdez0e4a4482017-07-17 11:12:34 -0400750 /* Initialize a random session ID for the experimental TLS 1.3 variant
751 * requiring a session id. */
Steven Valdezdbe01582017-07-14 10:39:28 -0400752 if (ssl->tls13_variant == tls13_experiment) {
Steven Valdez520e1222017-06-13 12:45:25 -0400753 hs->session_id_len = sizeof(hs->session_id);
754 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
755 return -1;
756 }
757 }
758
David Benjamin8c880a22016-12-03 02:20:34 -0500759 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400760 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400761 }
762
David Benjamin16315f72017-01-12 20:02:05 -0500763 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800764}
Adam Langley95c29f32014-06-20 12:00:00 -0700765
David Benjamin8d606e32017-06-15 22:43:04 -0400766static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900767 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400768 SSLMessage msg;
769 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -0700770 if (ret <= 0) {
771 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400772 }
773
David Benjamin7934f082017-08-01 16:32:25 -0400774 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
David Benjaminb0c761e2017-06-25 22:42:55 -0400775 ssl->d1->send_cookie = false;
David Benjaminaa7734b2016-06-07 16:40:46 -0400776 return 1;
777 }
778
David Benjamin7934f082017-08-01 16:32:25 -0400779 CBS hello_verify_request = msg.body, cookie;
780 uint16_t server_version;
David Benjaminaa7734b2016-06-07 16:40:46 -0400781 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
782 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
David Benjamin8d606e32017-06-15 22:43:04 -0400783 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
David Benjaminaa7734b2016-06-07 16:40:46 -0400784 CBS_len(&hello_verify_request) != 0) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400785 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400786 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
787 return -1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400788 }
789
David Benjamin17cf2cb2016-12-13 01:07:13 -0500790 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400791 ssl->d1->cookie_len = CBS_len(&cookie);
792
David Benjaminb0c761e2017-06-25 22:42:55 -0400793 ssl->d1->send_cookie = true;
David Benjamin8f94c312017-08-01 17:35:55 -0400794 ssl->method->next_message(ssl);
David Benjaminaa7734b2016-06-07 16:40:46 -0400795 return 1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400796}
797
David Benjamin7934f082017-08-01 16:32:25 -0400798static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out,
799 const SSLMessage &msg) {
David Benjaminb853f312017-07-14 18:40:34 -0400800 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400801 if (msg.type != SSL3_MT_SERVER_HELLO &&
802 msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
David Benjaminb853f312017-07-14 18:40:34 -0400803 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
804 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
805 return 0;
806 }
807
David Benjamin7934f082017-08-01 16:32:25 -0400808 CBS server_hello = msg.body;
David Benjaminb853f312017-07-14 18:40:34 -0400809 if (!CBS_get_u16(&server_hello, out)) {
810 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
811 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
812 return 0;
813 }
814
815 /* The server version may also be in the supported_versions extension if
816 * applicable. */
David Benjamin7934f082017-08-01 16:32:25 -0400817 if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) {
David Benjaminb853f312017-07-14 18:40:34 -0400818 return 1;
819 }
820
821 uint8_t sid_length;
822 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
823 !CBS_get_u8(&server_hello, &sid_length) ||
824 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
825 1 /* compression_method */)) {
826 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
827 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
828 return 0;
829 }
830
831 /* The extensions block may not be present. */
832 if (CBS_len(&server_hello) == 0) {
833 return 1;
834 }
835
836 CBS extensions;
837 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
838 CBS_len(&server_hello) != 0) {
839 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
840 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
841 return 0;
842 }
843
844 int have_supported_versions;
845 CBS supported_versions;
846 const SSL_EXTENSION_TYPE ext_types[] = {
847 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
848 &supported_versions},
849 };
850
851 uint8_t alert = SSL_AD_DECODE_ERROR;
852 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
853 OPENSSL_ARRAY_SIZE(ext_types),
854 1 /* ignore unknown */)) {
855 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
856 return 0;
857 }
858
859 if (have_supported_versions &&
860 (!CBS_get_u16(&supported_versions, out) ||
861 CBS_len(&supported_versions) != 0)) {
862 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
863 return 0;
864 }
865
866 return 1;
867}
868
David Benjaminc3c88822016-11-14 10:32:04 +0900869static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
870 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400871 SSLMessage msg;
872 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -0700873 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500874 uint32_t err = ERR_peek_error();
875 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
876 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
877 /* Add a dedicated error code to the queue for a handshake_failure alert
878 * in response to ClientHello. This matches NSS's client behavior and
879 * gives a better error on a (probable) failure to negotiate initial
880 * parameters. Note: this error code comes after the original one.
881 *
882 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400883 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500884 }
David Benjamin09eb6552016-07-08 14:32:11 -0700885 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800886 }
Adam Langley95c29f32014-06-20 12:00:00 -0700887
David Benjaminb853f312017-07-14 18:40:34 -0400888 uint16_t server_version;
David Benjamin7934f082017-08-01 16:32:25 -0400889 if (!parse_server_version(hs, &server_version, msg)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400890 return -1;
891 }
892
Steven Valdez8f36c512017-06-20 10:55:02 -0400893 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400894 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400895 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
896 return -1;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400897 }
898
David Benjamin0d56f882015-12-19 17:05:56 -0500899 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
900 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400901 ssl->version = server_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500902 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800903 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500904 ssl->s3->have_version = 1;
Steven Valdez8f36c512017-06-20 10:55:02 -0400905 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400906 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400907 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
908 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800909 }
Adam Langley95c29f32014-06-20 12:00:00 -0700910
Steven Valdez143e8b32016-07-11 13:19:03 -0400911 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500912 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900913 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400914 return 1;
915 }
916
Steven Valdez2d850622017-01-11 11:34:52 -0500917 if (hs->early_data_offered) {
918 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400919 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
920 return -1;
Steven Valdez2d850622017-01-11 11:34:52 -0500921 }
922
David Benjamin6e4fc332016-11-17 16:43:08 +0900923 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400924
David Benjamin7934f082017-08-01 16:32:25 -0400925 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400926 return -1;
927 }
928
David Benjamin7934f082017-08-01 16:32:25 -0400929 CBS server_hello = msg.body, server_random, session_id;
David Benjaminb853f312017-07-14 18:40:34 -0400930 uint16_t cipher_suite;
931 uint8_t compression_method;
David Benjaminb853f312017-07-14 18:40:34 -0400932 if (!CBS_skip(&server_hello, 2 /* version */) ||
933 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400934 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
935 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
936 !CBS_get_u16(&server_hello, &cipher_suite) ||
937 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400938 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400939 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
940 return -1;
Steven Valdez143e8b32016-07-11 13:19:03 -0400941 }
942
Adam Langley1bea1732014-12-17 19:06:57 -0800943 /* Copy over the server random. */
David Benjamind3bca042017-04-08 11:05:04 -0400944 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
945 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700946
David Benjamin55108632016-08-11 22:01:18 -0400947 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
948 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400949
David Benjamin0d56f882015-12-19 17:05:56 -0500950 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400951 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500952 CBS_mem_equal(&session_id, ssl->session->session_id,
953 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400954 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800955 } else {
956 /* The session wasn't resumed. Create a fresh SSL_SESSION to
957 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400958 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900959 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400960 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
961 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800962 }
963 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500964 hs->new_session->session_id_length = CBS_len(&session_id);
965 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
966 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800967 }
David Benjamina03d95d2014-07-12 19:49:07 -0400968
David Benjaminca9e8f52017-08-09 15:02:34 -0400969 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
970 if (cipher == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -0800971 /* unknown cipher */
David Benjamin3570d732015-06-29 00:28:17 -0400972 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400973 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
974 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800975 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400976
977 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500978 uint32_t mask_a, mask_k;
979 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
David Benjaminca9e8f52017-08-09 15:02:34 -0400980 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
981 SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) ||
982 SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) ||
983 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -0400984 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_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 }
Adam Langley95c29f32014-06-20 12:00:00 -0700988
Steven Valdez87eab492016-06-27 16:34:59 -0400989 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500990 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400991 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400992 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
993 return -1;
David Benjaminece3de92015-03-16 18:02:20 -0400994 }
David Benjaminca9e8f52017-08-09 15:02:34 -0400995 if (ssl->session->cipher != cipher) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400997 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
998 return -1;
Steven Valdez803c77a2016-09-06 14:13:43 -0400999 }
Steven Valdez4aa154e2016-07-29 14:32:55 -04001000 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
1001 /* This is actually a client application bug. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001002 OPENSSL_PUT_ERROR(SSL,
1003 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001004 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1005 return -1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001006 }
David Benjamin0623bce2015-12-25 15:40:14 -05001007 } else {
David Benjaminca9e8f52017-08-09 15:02:34 -04001008 hs->new_session->cipher = cipher;
Adam Langley1bea1732014-12-17 19:06:57 -08001009 }
David Benjaminca9e8f52017-08-09 15:02:34 -04001010 hs->new_cipher = cipher;
David Benjaminbdf5e722014-11-11 00:52:15 -05001011
David Benjaminf71036e2017-01-21 14:49:39 -05001012 /* Now that the cipher is known, initialize the handshake hash and hash the
1013 * ServerHello. */
David Benjaminca9e8f52017-08-09 15:02:34 -04001014 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -04001015 !ssl_hash_message(hs, msg)) {
David Benjamind3bca042017-04-08 11:05:04 -04001016 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1017 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001018 }
1019
Steven Valdez2b8415e2016-06-30 13:27:23 -04001020 /* If doing a full handshake, the server may request a client certificate
1021 * which requires hashing the handshake transcript. Otherwise, the handshake
1022 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001023 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -05001024 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001025 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -08001026 }
David Benjamina03d95d2014-07-12 19:49:07 -04001027
Adam Langley1bea1732014-12-17 19:06:57 -08001028 /* Only the NULL compression algorithm is supported. */
1029 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001030 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -04001031 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1032 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001033 }
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langley1bea1732014-12-17 19:06:57 -08001035 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -05001036 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -04001037 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001038 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001039 }
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langley1bea1732014-12-17 19:06:57 -08001041 /* There should be nothing left over in the record. */
1042 if (CBS_len(&server_hello) != 0) {
1043 /* wrong packet length */
David Benjamin639846e2016-09-09 11:41:18 -04001044 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -04001045 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1046 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001047 }
Adam Langley95c29f32014-06-20 12:00:00 -07001048
Steven Valdez87eab492016-06-27 16:34:59 -04001049 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -05001050 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -05001051 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001052 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001053 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001054 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001055 }
David Benjamind3bca042017-04-08 11:05:04 -04001056 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1057 return -1;
Adam Langleyba5934b2015-06-02 10:50:35 -07001058 }
1059
David Benjamin8f94c312017-08-01 17:35:55 -04001060 ssl->method->next_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001061 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001062}
Adam Langley95c29f32014-06-20 12:00:00 -07001063
David Benjaminc3c88822016-11-14 10:32:04 +09001064static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1065 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001066 SSLMessage msg;
1067 int ret = ssl_read_message(ssl, &msg);
David Benjamin5c900c82016-07-13 23:03:26 -04001068 if (ret <= 0) {
1069 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001070 }
Adam Langley95c29f32014-06-20 12:00:00 -07001071
David Benjamin7934f082017-08-01 16:32:25 -04001072 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
1073 !ssl_hash_message(hs, msg)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001074 return -1;
1075 }
1076
David Benjamin7934f082017-08-01 16:32:25 -04001077 CBS body = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001078 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -04001079 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
David Benjamin7934f082017-08-01 16:32:25 -04001080 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
David Benjamine664a532017-07-20 20:19:36 -04001081 ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001082 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001083 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001084 }
David Benjamine664a532017-07-20 20:19:36 -04001085 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1086 hs->new_session->certs = chain.release();
Adam Langley95c29f32014-06-20 12:00:00 -07001087
David Benjamin45738dd2017-02-09 20:01:26 -05001088 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
David Benjamin7934f082017-08-01 16:32:25 -04001089 CBS_len(&body) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001090 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -04001091 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001092 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001093 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001094 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001095
Adam Langley05672202016-12-13 12:05:49 -08001096 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -04001097 hs, hs->peer_pubkey.get(),
David Benjamin45738dd2017-02-09 20:01:26 -05001098 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001099 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001100 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001101 }
1102
David Benjamin8f94c312017-08-01 17:35:55 -04001103 ssl->method->next_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001104 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001105}
1106
David Benjaminc3c88822016-11-14 10:32:04 +09001107static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1108 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001109 SSLMessage msg;
1110 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -07001111 if (ret <= 0) {
1112 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001113 }
1114
David Benjamin7934f082017-08-01 16:32:25 -04001115 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001116 /* A server may send status_request in ServerHello and then change
1117 * its mind about sending CertificateStatus. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001118 return 1;
1119 }
1120
David Benjamin7934f082017-08-01 16:32:25 -04001121 if (!ssl_hash_message(hs, msg)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001122 return -1;
1123 }
1124
David Benjamin7934f082017-08-01 16:32:25 -04001125 CBS certificate_status = msg.body, ocsp_response;
David Benjamin8d606e32017-06-15 22:43:04 -04001126 uint8_t status_type;
David Benjamin9f1dc822016-06-07 17:03:46 -04001127 if (!CBS_get_u8(&certificate_status, &status_type) ||
1128 status_type != TLSEXT_STATUSTYPE_ocsp ||
1129 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1130 CBS_len(&ocsp_response) == 0 ||
1131 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001132 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001133 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1134 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001135 }
1136
David Benjamin8fc2dc02017-08-22 15:07:51 -07001137 CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
1138 hs->new_session->ocsp_response =
1139 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
1140 if (hs->new_session->ocsp_response == nullptr) {
David Benjamin8d606e32017-06-15 22:43:04 -04001141 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1142 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001143 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001144
David Benjamin8f94c312017-08-01 17:35:55 -04001145 ssl->method->next_message(ssl);
David Benjamin8d606e32017-06-15 22:43:04 -04001146 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001147}
1148
David Benjaminc3c88822016-11-14 10:32:04 +09001149static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1150 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001151 SSLMessage msg;
1152 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -07001153 if (ret <= 0) {
1154 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001155 }
1156
David Benjamin7934f082017-08-01 16:32:25 -04001157 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001158 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001159 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001160 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001161 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001162 return -1;
1163 }
1164
Adam Langley1bea1732014-12-17 19:06:57 -08001165 return 1;
1166 }
1167
David Benjamin7934f082017-08-01 16:32:25 -04001168 if (!ssl_hash_message(hs, msg)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001169 return -1;
1170 }
1171
David Benjamin45738dd2017-02-09 20:01:26 -05001172 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1173 uint32_t alg_a = hs->new_cipher->algorithm_auth;
David Benjamin7934f082017-08-01 16:32:25 -04001174 CBS server_key_exchange = msg.body;
Adam Langley1bea1732014-12-17 19:06:57 -08001175 if (alg_a & SSL_aPSK) {
1176 CBS psk_identity_hint;
1177
1178 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1179 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1180 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001181 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001182 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001183 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001184 }
1185
1186 /* Store PSK identity hint for later use, hint is used in
1187 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1188 * identity hint can be as long as the maximum length of a PSK identity.
1189 * Also do not allow NULL characters; identities are saved as C strings.
1190 *
1191 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1192 * a specific identity. */
1193 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1194 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001195 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -04001196 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -04001197 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001198 }
1199
David Benjamin78679342016-09-16 19:42:05 -04001200 /* Save non-empty identity hints as a C string. Empty identity hints we
1201 * treat as missing. Plain PSK makes it possible to send either no hint
1202 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1203 * empty hint. Having different capabilities is odd, so we interpret empty
1204 * and missing as identical. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001205 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -04001206 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -04001207 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -04001208 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001209 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001210 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001211 }
David Benjamin31b0c9b2017-07-20 14:49:15 -04001212 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -08001213 }
1214
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001215 if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001216 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001217 uint8_t group_type;
1218 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001219 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001220 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1221 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001222 !CBS_get_u16(&server_key_exchange, &group_id) ||
1223 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -04001224 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001225 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001226 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001227 }
David Benjamin45738dd2017-02-09 20:01:26 -05001228 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001229
Steven Valdezce902a92016-05-17 11:47:53 -04001230 /* Ensure the group is consistent with preferences. */
1231 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001232 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -04001233 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin1386aad2017-07-19 23:57:40 -04001234 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001235 }
David Benjamin4298d772015-12-19 00:18:25 -05001236
Matt Braithwaite053931e2016-05-25 12:06:05 -07001237 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc642aca2017-07-19 23:28:43 -04001238 hs->key_share = SSLKeyShare::Create(group_id);
1239 if (!hs->key_share ||
David Benjaminc3c88822016-11-14 10:32:04 +09001240 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001241 return -1;
David Benjaminc1cc8582015-12-17 01:22:15 -05001242 }
Adam Langley1bea1732014-12-17 19:06:57 -08001243 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -04001244 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001245 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin1386aad2017-07-19 23:57:40 -04001246 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001247 }
1248
1249 /* At this point, |server_key_exchange| contains the signature, if any, while
David Benjamin7934f082017-08-01 16:32:25 -04001250 * |msg.body| contains the entire message. From that, derive a CBS containing
1251 * just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001252 CBS parameter;
David Benjamin7934f082017-08-01 16:32:25 -04001253 CBS_init(&parameter, CBS_data(&msg.body),
1254 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
Adam Langley1bea1732014-12-17 19:06:57 -08001255
David Benjaminf4958e72015-06-07 11:00:53 -04001256 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001257 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001258 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001259 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001260 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001261 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001262 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001263 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001264 }
David Benjamin8d606e32017-06-15 22:43:04 -04001265 uint8_t alert = SSL_AD_DECODE_ERROR;
1266 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1267 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin1386aad2017-07-19 23:57:40 -04001268 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001269 }
David Benjamin45738dd2017-02-09 20:01:26 -05001270 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001271 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001272 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001273 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001274 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
David Benjamin1386aad2017-07-19 23:57:40 -04001275 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001276 }
1277
1278 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001279 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001280 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1281 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001282 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001283 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001284 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001285 }
1286
David Benjamin1386aad2017-07-19 23:57:40 -04001287 ScopedCBB transcript;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001288 uint8_t *transcript_data;
1289 size_t transcript_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001290 if (!CBB_init(transcript.get(),
1291 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1292 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1293 SSL3_RANDOM_SIZE) ||
1294 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1295 SSL3_RANDOM_SIZE) ||
1296 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1297 CBS_len(&parameter)) ||
1298 !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001299 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001300 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001301 return -1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001302 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001303
1304 int sig_ok = ssl_public_key_verify(
1305 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001306 hs->peer_pubkey.get(), transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001307 OPENSSL_free(transcript_data);
1308
David Benjaminbf82aed2016-03-01 22:57:40 -05001309#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1310 sig_ok = 1;
1311 ERR_clear_error();
1312#endif
1313 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001314 /* bad signature */
David Benjamin3570d732015-06-29 00:28:17 -04001315 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001316 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001317 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001318 }
1319 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001320 /* PSK ciphers are the only supported certificate-less ciphers. */
1321 assert(alg_a == SSL_aPSK);
1322
Adam Langley1bea1732014-12-17 19:06:57 -08001323 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001324 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001325 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001326 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001327 }
1328 }
David Benjamin8f94c312017-08-01 17:35:55 -04001329
1330 ssl->method->next_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001331 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001332}
Adam Langley95c29f32014-06-20 12:00:00 -07001333
David Benjaminc3c88822016-11-14 10:32:04 +09001334static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1335 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001336 SSLMessage msg;
1337 int ret = ssl_read_message(ssl, &msg);
1338 if (ret <= 0) {
1339 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001340 }
David Benjamin688d8df2014-11-02 23:06:42 -05001341
David Benjamin7934f082017-08-01 16:32:25 -04001342 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001343 /* If we get here we don't need the handshake buffer as we won't be doing
1344 * client auth. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001345 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -08001346 return 1;
1347 }
David Benjamined439582014-07-14 19:13:02 -04001348
David Benjamin7934f082017-08-01 16:32:25 -04001349 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1350 !ssl_hash_message(hs, msg)) {
David Benjamine0332e82016-07-13 22:40:36 -04001351 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001352 }
Adam Langley95c29f32014-06-20 12:00:00 -07001353
David Benjamine0332e82016-07-13 22:40:36 -04001354 /* Get the certificate types. */
David Benjamin7934f082017-08-01 16:32:25 -04001355 CBS body = msg.body, certificate_types;
1356 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001357 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001358 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001359 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001360 }
David Benjamined439582014-07-14 19:13:02 -04001361
David Benjaminc3c88822016-11-14 10:32:04 +09001362 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1363 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001364 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001365 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001366 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001367
David Benjamina1e9cab2015-12-30 00:08:49 -05001368 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001369 CBS supported_signature_algorithms;
David Benjamin7934f082017-08-01 16:32:25 -04001370 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001371 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001372 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001373 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001374 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001375 }
Adam Langley1bea1732014-12-17 19:06:57 -08001376 }
David Benjamined439582014-07-14 19:13:02 -04001377
Adam Langleyc68e5b92017-02-08 13:33:15 -08001378 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001379 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
David Benjamin7934f082017-08-01 16:32:25 -04001380 ssl_parse_client_CA_list(ssl, &alert, &body);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001381 if (!ca_names) {
David Benjamine0332e82016-07-13 22:40:36 -04001382 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1383 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001384 }
Adam Langley95c29f32014-06-20 12:00:00 -07001385
David Benjamin7934f082017-08-01 16:32:25 -04001386 if (CBS_len(&body) != 0) {
David Benjamin639846e2016-09-09 11:41:18 -04001387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001388 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1389 return -1;
1390 }
1391
David Benjaminc3c88822016-11-14 10:32:04 +09001392 hs->cert_request = 1;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001393 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001394 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamin8f94c312017-08-01 17:35:55 -04001395 ssl->method->next_message(ssl);
David Benjamine0332e82016-07-13 22:40:36 -04001396 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001397}
Adam Langley95c29f32014-06-20 12:00:00 -07001398
David Benjaminc3c88822016-11-14 10:32:04 +09001399static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1400 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001401 SSLMessage msg;
1402 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -07001403 if (ret <= 0) {
1404 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001405 }
David Benjamin46062682014-07-14 19:14:32 -04001406
David Benjamin7934f082017-08-01 16:32:25 -04001407 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1408 !ssl_hash_message(hs, msg)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001409 return -1;
1410 }
1411
David Benjamin09eb6552016-07-08 14:32:11 -07001412 /* ServerHelloDone is empty. */
David Benjamin7934f082017-08-01 16:32:25 -04001413 if (CBS_len(&msg.body) != 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001414 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001415 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001416 return -1;
1417 }
David Benjamin46062682014-07-14 19:14:32 -04001418
David Benjamin8f94c312017-08-01 17:35:55 -04001419 ssl->method->next_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001420 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001421}
Adam Langley1258b6a2014-06-20 12:00:00 -07001422
David Benjaminc3c88822016-11-14 10:32:04 +09001423static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1424 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001425 /* Call cert_cb to update the certificate. */
1426 if (ssl->cert->cert_cb) {
1427 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1428 if (ret < 0) {
1429 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001430 return -1;
1431 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001432 if (ret == 0) {
1433 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1434 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001435 return -1;
1436 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001437 }
1438
David Benjamin5edfc8c2016-12-10 15:46:58 -05001439 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001440 /* Without a client certificate, the handshake buffer may be released. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001441 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001442
David Benjamindaf207a2017-01-03 18:37:41 -05001443 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001444 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001445 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1446 SSL_AD_NO_CERTIFICATE)) {
1447 return -1;
1448 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001449 return 1;
1450 }
1451 }
1452
David Benjamina232a712017-03-30 15:51:53 -05001453 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001454 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001455 return -1;
1456 }
David Benjamin16315f72017-01-12 20:02:05 -05001457 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001458}
1459
David Benjamina3d76d02017-07-14 19:36:07 -04001460static_assert(sizeof(size_t) >= sizeof(unsigned),
1461 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001462
David Benjaminc3c88822016-11-14 10:32:04 +09001463static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1464 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001465 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001466 CBB body;
1467 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001468 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjamind781fc42017-07-12 16:25:57 -04001469 return -1;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001470 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001471
David Benjamind781fc42017-07-12 16:25:57 -04001472 uint8_t *pms = NULL;
1473 size_t pms_len = 0;
David Benjamin45738dd2017-02-09 20:01:26 -05001474 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1475 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001476
David Benjaminae0eaaa2015-12-17 02:01:25 -05001477 /* If using a PSK key exchange, prepare the pre-shared key. */
1478 unsigned psk_len = 0;
1479 uint8_t psk[PSK_MAX_PSK_LEN];
1480 if (alg_a & SSL_aPSK) {
1481 if (ssl->psk_client_callback == NULL) {
1482 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1483 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001484 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001485
David Benjaminae0eaaa2015-12-17 02:01:25 -05001486 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001487 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001488 psk_len =
David Benjamin31b0c9b2017-07-20 14:49:15 -04001489 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
1490 identity, sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001491 if (psk_len == 0) {
1492 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1493 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1494 goto err;
1495 }
1496 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001497
David Benjamin45738dd2017-02-09 20:01:26 -05001498 OPENSSL_free(hs->new_session->psk_identity);
1499 hs->new_session->psk_identity = BUF_strdup(identity);
1500 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001501 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1502 goto err;
1503 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001504
David Benjaminae0eaaa2015-12-17 02:01:25 -05001505 /* Write out psk_identity. */
1506 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001507 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001508 !CBB_add_bytes(&child, (const uint8_t *)identity,
1509 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001510 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001511 goto err;
1512 }
1513 }
David Benjamin758d1272015-11-20 17:47:25 -05001514
David Benjaminae0eaaa2015-12-17 02:01:25 -05001515 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1516 if (alg_k & SSL_kRSA) {
1517 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001518 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001519 if (pms == NULL) {
1520 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1521 goto err;
1522 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001523
David Benjamin31b0c9b2017-07-20 14:49:15 -04001524 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001525 if (rsa == NULL) {
1526 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001527 goto err;
1528 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001529
David Benjaminf04c2e92016-12-06 13:35:25 -05001530 pms[0] = hs->client_version >> 8;
1531 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001532 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1533 goto err;
1534 }
1535
David Benjamin75836432016-06-17 18:48:29 -04001536 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001537 size_t enc_pms_len;
1538 /* In TLS, there is a length prefix. */
1539 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001540 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001541 goto err;
1542 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001543 enc_pms = &child;
1544 }
Adam Langley1bea1732014-12-17 19:06:57 -08001545
David Benjaminae0eaaa2015-12-17 02:01:25 -05001546 uint8_t *ptr;
1547 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1548 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1549 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001550 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001551 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001552 goto err;
1553 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001554 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001555 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001556 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001557 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001558 goto err;
1559 }
1560
David Benjamin4298d772015-12-19 00:18:25 -05001561 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001562 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001563 if (!hs->key_share->Accept(&child, &pms, &pms_len, &alert, hs->peer_key,
1564 hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001565 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1566 goto err;
1567 }
David Benjamin75836432016-06-17 18:48:29 -04001568 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001569 goto err;
1570 }
David Benjamin4298d772015-12-19 00:18:25 -05001571
1572 /* The key exchange state may now be discarded. */
David Benjaminc642aca2017-07-19 23:28:43 -04001573 hs->key_share.reset();
David Benjaminc3c88822016-11-14 10:32:04 +09001574 OPENSSL_free(hs->peer_key);
1575 hs->peer_key = NULL;
1576 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001577 } else if (alg_k & SSL_kPSK) {
1578 /* For plain PSK, other_secret is a block of 0s with the same length as
1579 * the pre-shared key. */
1580 pms_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001581 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001582 if (pms == NULL) {
1583 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1584 goto err;
1585 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001586 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001587 } else {
1588 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1589 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1590 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001591 }
1592
David Benjaminae0eaaa2015-12-17 02:01:25 -05001593 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1594 * key. */
1595 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001596 ScopedCBB pms_cbb;
1597 CBB child;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001598 uint8_t *new_pms;
1599 size_t new_pms_len;
1600
David Benjamin1386aad2017-07-19 23:57:40 -04001601 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms_len) ||
1602 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001603 !CBB_add_bytes(&child, pms, pms_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001604 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001605 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001606 !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001607 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1608 goto err;
1609 }
1610 OPENSSL_cleanse(pms, pms_len);
1611 OPENSSL_free(pms);
1612 pms = new_pms;
1613 pms_len = new_pms_len;
1614 }
1615
1616 /* The message must be added to the finished hash before calculating the
1617 * master secret. */
David Benjamind781fc42017-07-12 16:25:57 -04001618 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001619 goto err;
1620 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001621
David Benjamin45738dd2017-02-09 20:01:26 -05001622 hs->new_session->master_key_length = tls1_generate_master_secret(
1623 hs, hs->new_session->master_key, pms, pms_len);
1624 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001625 goto err;
1626 }
David Benjaminfc02b592017-02-17 16:26:01 -05001627 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001628 OPENSSL_cleanse(pms, pms_len);
1629 OPENSSL_free(pms);
1630
David Benjamin16315f72017-01-12 20:02:05 -05001631 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001632
1633err:
David Benjaminae0eaaa2015-12-17 02:01:25 -05001634 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001635 OPENSSL_cleanse(pms, pms_len);
1636 OPENSSL_free(pms);
1637 }
1638 return -1;
1639}
Adam Langley1258b6a2014-06-20 12:00:00 -07001640
David Benjaminc3c88822016-11-14 10:32:04 +09001641static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1642 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001643 assert(ssl_has_private_key(ssl));
1644
David Benjamin86e95b82017-07-18 16:34:25 -04001645 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001646 CBB body, child;
1647 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001648 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjamind781fc42017-07-12 16:25:57 -04001649 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001650 }
1651
David Benjaminea9a0d52016-07-08 15:52:59 -07001652 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001653 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001654 return -1;
David Benjaminea9a0d52016-07-08 15:52:59 -07001655 }
David Benjamin429fdc02016-06-17 17:33:30 -04001656 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001657 /* Write out the digest type in TLS 1.2. */
1658 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001659 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001660 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001661 }
1662 }
David Benjaminfc825122015-12-18 01:57:43 -05001663
David Benjamin429fdc02016-06-17 17:33:30 -04001664 /* Set aside space for the signature. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001665 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001666 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001667 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001668 !CBB_reserve(&child, &ptr, max_sig_len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001669 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001670 }
1671
Steven Valdez2b8415e2016-06-30 13:27:23 -04001672 size_t sig_len = max_sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001673 /* The SSL3 construction for CertificateVerify does not decompose into a
1674 * single final digest and signature, and must be special-cased. */
1675 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1676 if (ssl->cert->key_method != NULL) {
1677 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
David Benjamind781fc42017-07-12 16:25:57 -04001678 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001679 }
1680
David Benjamin44148742017-06-17 13:20:59 -04001681 uint8_t digest[EVP_MAX_MD_SIZE];
1682 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001683 if (!hs->transcript.GetSSL3CertVerifyHash(
1684 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001685 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001686 }
David Benjaminfc825122015-12-18 01:57:43 -05001687
David Benjamin1386aad2017-07-19 23:57:40 -04001688 UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
1689 if (!pctx ||
1690 !EVP_PKEY_sign_init(pctx.get()) ||
1691 !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001692 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001693 }
1694 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001695 switch (ssl_private_key_sign(
1696 hs, ptr, &sig_len, max_sig_len, signature_algorithm,
1697 hs->transcript.buffer_data(), hs->transcript.buffer_len())) {
David Benjamin44148742017-06-17 13:20:59 -04001698 case ssl_private_key_success:
1699 break;
1700 case ssl_private_key_failure:
David Benjamind781fc42017-07-12 16:25:57 -04001701 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001702 case ssl_private_key_retry:
1703 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind781fc42017-07-12 16:25:57 -04001704 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001705 }
David Benjaminfc825122015-12-18 01:57:43 -05001706 }
1707
David Benjaminfc825122015-12-18 01:57:43 -05001708 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001709 !ssl_add_message_cbb(ssl, cbb.get())) {
1710 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001711 }
1712
David Benjamin44148742017-06-17 13:20:59 -04001713 /* The handshake buffer is no longer necessary. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001714 hs->transcript.FreeBuffer();
David Benjamin16315f72017-01-12 20:02:05 -05001715 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001716}
1717
David Benjaminc3c88822016-11-14 10:32:04 +09001718static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1719 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001720 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001721 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001722
David Benjamin1386aad2017-07-19 23:57:40 -04001723 ScopedCBB cbb;
1724 CBB body, child;
1725 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
David Benjamin75836432016-06-17 18:48:29 -04001726 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001727 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1728 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001729 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001730 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001731 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamine97b9622015-10-10 14:59:09 -04001732 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine97b9622015-10-10 14:59:09 -04001733 return -1;
1734 }
1735
David Benjamin16315f72017-01-12 20:02:05 -05001736 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001737}
1738
David Benjaminc3c88822016-11-14 10:32:04 +09001739static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1740 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001741 if (!ssl_do_channel_id_callback(ssl)) {
1742 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001743 }
1744
David Benjamin96e97b12015-10-10 16:30:22 -04001745 if (ssl->tlsext_channel_id_private == NULL) {
1746 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001747 return -1;
1748 }
Adam Langley1bea1732014-12-17 19:06:57 -08001749
David Benjamin1386aad2017-07-19 23:57:40 -04001750 ScopedCBB cbb;
1751 CBB body;
1752 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001753 !tls1_write_channel_id(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001754 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin3570d732015-06-29 00:28:17 -04001755 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper60a85cb2016-09-23 16:25:11 -07001756 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001757 }
1758
David Benjamin16315f72017-01-12 20:02:05 -05001759 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001760}
1761
David Benjaminc3c88822016-11-14 10:32:04 +09001762static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1763 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001764 SSLMessage msg;
1765 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -07001766 if (ret <= 0) {
1767 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001768 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001769
David Benjamin7934f082017-08-01 16:32:25 -04001770 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1771 !ssl_hash_message(hs, msg)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001772 return -1;
1773 }
1774
David Benjamin7934f082017-08-01 16:32:25 -04001775 CBS new_session_ticket = msg.body, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001776 uint32_t tlsext_tick_lifetime_hint;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001777 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001778 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1779 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001780 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001781 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001782 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001783 }
1784
David Benjamin9f1dc822016-06-07 17:03:46 -04001785 if (CBS_len(&ticket) == 0) {
1786 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001787 * negotiating the extension. The value of |ticket_expected| is checked in
1788 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001789 hs->ticket_expected = 0;
David Benjamin8f94c312017-08-01 17:35:55 -04001790 ssl->method->next_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001791 return 1;
1792 }
1793
David Benjamin31b0c9b2017-07-20 14:49:15 -04001794 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001795 UniquePtr<SSL_SESSION> renewed_session;
1796 if (ssl->session != NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001797 /* The server is sending a new ticket for an existing session. Sessions are
1798 * immutable once established, so duplicate all but the ticket of the
1799 * existing session. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001800 renewed_session =
1801 SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001802 if (!renewed_session) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001803 /* This should never happen. */
1804 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001805 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001806 }
David Benjamin1386aad2017-07-19 23:57:40 -04001807 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001808 }
1809
David Benjamin123db572016-11-03 16:59:25 -04001810 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001811 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001812
Steven Valdez87eab492016-06-27 16:34:59 -04001813 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001814 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -04001815 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001816 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001817 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001818
1819 /* Generate a session ID for this session based on the session ticket. We use
1820 * the session ID mechanism for detecting ticket resumption. This also fits in
1821 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001822 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1823 session->session_id, &session->session_id_length,
1824 EVP_sha256(), NULL)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001825 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001826 }
1827
David Benjamin1386aad2017-07-19 23:57:40 -04001828 if (renewed_session) {
Steven Valdez87eab492016-06-27 16:34:59 -04001829 session->not_resumable = 0;
1830 SSL_SESSION_free(ssl->session);
David Benjamin1386aad2017-07-19 23:57:40 -04001831 ssl->session = renewed_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -04001832 }
1833
David Benjamin8f94c312017-08-01 17:35:55 -04001834 ssl->method->next_message(ssl);
David Benjamin454aa4c2016-03-10 08:43:27 -05001835 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001836}
David Benjamin86e95b82017-07-18 16:34:25 -04001837
1838} // namespace bssl