blob: cac65f64dcd308ce5568de4b0e2b10db01a9a696 [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) {
241 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500242 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400243 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500244 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400245 }
246 break;
247
Steven Valdeze831a812017-03-09 14:56:07 -0500248 case SSL3_ST_WRITE_EARLY_DATA:
249 /* Stash the early data session, so connection properties may be queried
250 * out of it. */
251 hs->in_early_data = 1;
Steven Valdeze831a812017-03-09 14:56:07 -0500252 SSL_SESSION_up_ref(ssl->session);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400253 hs->early_session.reset(ssl->session);
Steven Valdeze831a812017-03-09 14:56:07 -0500254
255 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
256 hs->can_early_write = 1;
257 ret = 1;
258 goto end;
259
Adam Langley1bea1732014-12-17 19:06:57 -0800260 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900261 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500262 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400263 break;
264 }
Adam Langley1bea1732014-12-17 19:06:57 -0800265 if (ret <= 0) {
266 goto end;
267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Steven Valdez87eab492016-06-27 16:34:59 -0400269 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500270 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800271 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500272 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800273 }
Adam Langley1bea1732014-12-17 19:06:57 -0800274 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400275
Adam Langley1bea1732014-12-17 19:06:57 -0800276 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500277 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900278 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800279 if (ret <= 0) {
280 goto end;
281 }
Adam Langley1bea1732014-12-17 19:06:57 -0800282 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500283 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800284 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700285
David Benjamin9f1dc822016-06-07 17:03:46 -0400286 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900287 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900288 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400289 if (ret <= 0) {
290 goto end;
291 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400292 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500293 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400294 break;
295
Paul Lietar8f1c2682015-08-18 12:21:54 +0100296 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500297 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin3a1dd462017-07-11 16:13:10 -0400298 switch (ssl_verify_peer_cert(hs)) {
299 case ssl_verify_ok:
300 break;
301 case ssl_verify_invalid:
302 ret = -1;
303 goto end;
304 case ssl_verify_retry:
305 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
306 ret = -1;
307 goto end;
David Benjamin47749a62016-06-13 13:56:33 -0400308 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100309 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500310 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100311 break;
312
Adam Langley1bea1732014-12-17 19:06:57 -0800313 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900314 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800315 if (ret <= 0) {
316 goto end;
317 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500318 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500322 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900323 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400324 if (ret <= 0) {
325 goto end;
326 }
Adam Langley1bea1732014-12-17 19:06:57 -0800327 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500328 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800329 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley1bea1732014-12-17 19:06:57 -0800331 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900332 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800333 if (ret <= 0) {
334 goto end;
335 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400336 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500337 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800338 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley1bea1732014-12-17 19:06:57 -0800340 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900341 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900342 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400343 if (ret <= 0) {
344 goto end;
345 }
Adam Langley1bea1732014-12-17 19:06:57 -0800346 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500347 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800348 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langley1bea1732014-12-17 19:06:57 -0800350 case SSL3_ST_CW_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900351 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800352 if (ret <= 0) {
353 goto end;
354 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500355 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800356 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langley1bea1732014-12-17 19:06:57 -0800358 case SSL3_ST_CW_CERT_VRFY_A:
David Benjamina660e7a2017-01-02 08:15:35 -0500359 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900360 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400361 if (ret <= 0) {
362 goto end;
363 }
Adam Langley1bea1732014-12-17 19:06:57 -0800364 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500365 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800366 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
David Benjamin352d0a92016-06-28 11:22:02 -0400368 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500369 if (!ssl->method->add_change_cipher_spec(ssl) ||
370 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800371 ret = -1;
372 goto end;
373 }
Adam Langley95c29f32014-06-20 12:00:00 -0700374
David Benjamindaf207a2017-01-03 18:37:41 -0500375 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langley1bea1732014-12-17 19:06:57 -0800378 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900379 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900380 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400381 if (ret <= 0) {
382 goto end;
383 }
Adam Langley1bea1732014-12-17 19:06:57 -0800384 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500385 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800386 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley1bea1732014-12-17 19:06:57 -0800388 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400389 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900390 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400391 if (ret <= 0) {
392 goto end;
393 }
Adam Langley1bea1732014-12-17 19:06:57 -0800394 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500395 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800396 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langley1bea1732014-12-17 19:06:57 -0800398 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500399 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800400 if (ret <= 0) {
401 goto end;
402 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500403 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Steven Valdez87eab492016-06-27 16:34:59 -0400405 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500406 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800407 } else {
408 /* This is a non-resumption handshake. If it involves ChannelID, then
409 * record the handshake hashes at this point in the session so that
410 * any resumption of this session with ChannelID can sign those
411 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500412 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700413 if (ret <= 0) {
414 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800415 }
David Benjamin0d56f882015-12-19 17:05:56 -0500416 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
417 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500418 /* No False Start on renegotiation (would complicate the state
419 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500420 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500421 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800422 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500423 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800424 }
425 }
Adam Langley1bea1732014-12-17 19:06:57 -0800426 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
David Benjamin9f1dc822016-06-07 17:03:46 -0400428 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500429 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900430 hs->in_false_start = 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500431 hs->can_early_write = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400432 ret = 1;
433 goto end;
434
Adam Langley1bea1732014-12-17 19:06:57 -0800435 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900436 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900437 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400438 if (ret <= 0) {
439 goto end;
440 }
Adam Langley1bea1732014-12-17 19:06:57 -0800441 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500442 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800443 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley1bea1732014-12-17 19:06:57 -0800445 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400446 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500447 if (ret <= 0) {
448 goto end;
449 }
450
David Benjamin67739722016-11-17 17:03:59 +0900451 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800452 ret = -1;
453 goto end;
454 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500455 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800456 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langley1bea1732014-12-17 19:06:57 -0800458 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900459 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800460 if (ret <= 0) {
461 goto end;
462 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400463 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Steven Valdez87eab492016-06-27 16:34:59 -0400465 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500466 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800467 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500468 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800469 }
Adam Langley1bea1732014-12-17 19:06:57 -0800470 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700471
Adam Langley1bea1732014-12-17 19:06:57 -0800472 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500473 ret = ssl->method->flush_flight(ssl);
474 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800475 goto end;
476 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500477 hs->state = hs->next_state;
David Benjamin77458a42017-02-10 23:33:27 -0500478 if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400479 ssl->method->expect_flight(ssl);
480 }
Adam Langley1bea1732014-12-17 19:06:57 -0800481 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700482
Steven Valdez681eb6a2016-12-19 13:19:29 -0500483 case SSL_ST_TLS13: {
484 int early_return = 0;
485 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400486 if (ret <= 0) {
487 goto end;
488 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500489
490 if (early_return) {
491 ret = 1;
492 goto end;
493 }
494
David Benjamin77458a42017-02-10 23:33:27 -0500495 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400496 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500497 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400498
David Benjamin77458a42017-02-10 23:33:27 -0500499 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin4497e582016-07-27 17:51:49 -0400500 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700501
Steven Valdez87eab492016-06-27 16:34:59 -0400502 SSL_SESSION_free(ssl->s3->established_session);
503 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400504 SSL_SESSION_up_ref(ssl->session);
505 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400506 } else {
507 /* We make a copy of the session in order to maintain the immutability
508 * of the new established_session due to False Start. The caller may
509 * have taken a reference to the temporary session. */
510 ssl->s3->established_session =
David Benjamin31b0c9b2017-07-20 14:49:15 -0400511 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL)
512 .release();
Steven Valdez87eab492016-06-27 16:34:59 -0400513 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400514 ret = -1;
515 goto end;
516 }
517 ssl->s3->established_session->not_resumable = 0;
518
David Benjamin31b0c9b2017-07-20 14:49:15 -0400519 hs->new_session.reset();
Steven Valdez87eab492016-06-27 16:34:59 -0400520 }
521
David Benjamin77458a42017-02-10 23:33:27 -0500522 hs->state = SSL_ST_OK;
523 break;
524
525 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500526 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500527 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400528 if (is_initial_handshake) {
529 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900530 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
Adam Langley1bea1732014-12-17 19:06:57 -0800533 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400534 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800535 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500536 }
Steven Valdez87eab492016-06-27 16:34:59 -0400537
Adam Langley1bea1732014-12-17 19:06:57 -0800538 default:
David Benjamin3570d732015-06-29 00:28:17 -0400539 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800540 ret = -1;
541 goto end;
542 }
543
David Benjamin16b1b1d2017-02-10 23:41:14 -0500544 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400545 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800546 }
Adam Langley1bea1732014-12-17 19:06:57 -0800547 }
548
Adam Langley95c29f32014-06-20 12:00:00 -0700549end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400550 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800551 return ret;
552}
Adam Langley95c29f32014-06-20 12:00:00 -0700553
David Benjamin2fe6e222017-01-25 02:18:43 -0500554/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
555 * disabled algorithms. */
556static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
557 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500558 *out_mask_a = 0;
559 *out_mask_k = 0;
560
David Benjamin2fe6e222017-01-25 02:18:43 -0500561 /* PSK requires a client callback. */
562 if (ssl->psk_client_callback == NULL) {
563 *out_mask_a |= SSL_aPSK;
564 *out_mask_k |= SSL_kPSK;
565 }
566}
567
David Benjamin68161cb2017-06-20 14:49:43 -0400568static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
569 SSL *const ssl = hs->ssl;
David Benjamin2fe6e222017-01-25 02:18:43 -0500570 uint32_t mask_a, mask_k;
571 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700572
David Benjamine8d53502015-10-10 14:13:23 -0400573 CBB child;
574 if (!CBB_add_u16_length_prefixed(out, &child)) {
575 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800576 }
577
David Benjamin65ac9972016-09-02 21:35:25 -0400578 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
579 if (ssl->ctx->grease_enabled &&
580 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
581 return 0;
582 }
583
David Benjaminabbbee12016-10-31 19:20:42 -0400584 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
585 * hardware support. */
David Benjamin68161cb2017-06-20 14:49:43 -0400586 if (hs->max_version >= TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400587 if (!EVP_has_aes_hardware() &&
588 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
589 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500590 }
David Benjaminabbbee12016-10-31 19:20:42 -0400591 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
592 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
593 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400594 }
David Benjaminabbbee12016-10-31 19:20:42 -0400595 if (EVP_has_aes_hardware() &&
596 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400597 return 0;
598 }
599 }
600
David Benjamin68161cb2017-06-20 14:49:43 -0400601 if (hs->min_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400602 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
603 int any_enabled = 0;
604 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
605 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
606 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500607 if ((cipher->algorithm_mkey & mask_k) ||
608 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400609 continue;
610 }
David Benjamin68161cb2017-06-20 14:49:43 -0400611 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
612 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
David Benjaminabbbee12016-10-31 19:20:42 -0400613 continue;
614 }
615 any_enabled = 1;
616 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
617 return 0;
618 }
619 }
620
621 /* If all ciphers were disabled, return the error to the caller. */
David Benjamin68161cb2017-06-20 14:49:43 -0400622 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
David Benjaminabbbee12016-10-31 19:20:42 -0400623 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
624 return 0;
625 }
David Benjamine8d53502015-10-10 14:13:23 -0400626 }
627
628 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
629 * added. */
David Benjamin68161cb2017-06-20 14:49:43 -0400630 if (hs->max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400631 !ssl->s3->initial_handshake_complete) {
632 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
633 return 0;
634 }
David Benjamine8d53502015-10-10 14:13:23 -0400635 }
636
David Benjamine0ff7672016-09-19 18:40:03 -0400637 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200638 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
639 return 0;
640 }
David Benjamine8d53502015-10-10 14:13:23 -0400641 }
642
643 return CBB_flush(out);
644}
645
David Benjamin8c880a22016-12-03 02:20:34 -0500646int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
647 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400648 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -0400649 CBB body;
650 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
651 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400652 }
653
Steven Valdez5440fe02016-07-18 12:40:30 -0400654 /* Renegotiations do not participate in session resumption. */
Steven Valdez520e1222017-06-13 12:45:25 -0400655 int has_session_id = ssl->session != NULL &&
656 !ssl->s3->initial_handshake_complete &&
657 ssl->session->session_id_length > 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400658
659 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500660 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400661 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
Steven Valdez520e1222017-06-13 12:45:25 -0400662 !CBB_add_u8_length_prefixed(&body, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -0400663 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400664 }
665
Steven Valdez520e1222017-06-13 12:45:25 -0400666 if (has_session_id) {
667 if (!CBB_add_bytes(&child, ssl->session->session_id,
668 ssl->session->session_id_length)) {
David Benjamind781fc42017-07-12 16:25:57 -0400669 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400670 }
671 } else {
672 /* In TLS 1.3 experimental encodings, send a fake placeholder session ID
673 * when we do not otherwise have one to send. */
674 if (hs->max_version >= TLS1_3_VERSION &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400675 ssl->tls13_variant == tls13_experiment &&
Steven Valdez520e1222017-06-13 12:45:25 -0400676 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400677 return 0;
Steven Valdez520e1222017-06-13 12:45:25 -0400678 }
679 }
680
David Benjamince079fd2016-08-02 16:22:34 -0400681 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400682 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400683 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
David Benjamind781fc42017-07-12 16:25:57 -0400684 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400685 }
686 }
687
688 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400689 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin68161cb2017-06-20 14:49:43 -0400690 if (!ssl_write_client_cipher_list(hs, &body) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400691 !CBB_add_u8(&body, 1 /* one compression method */) ||
692 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500693 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
David Benjamind781fc42017-07-12 16:25:57 -0400694 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400695 }
696
Steven Valdeza833c352016-11-01 13:39:36 -0400697 uint8_t *msg = NULL;
698 size_t len;
David Benjamind781fc42017-07-12 16:25:57 -0400699 if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) {
700 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400701 }
702
703 /* Now that the length prefixes have been computed, fill in the placeholder
704 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500705 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500706 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400707 OPENSSL_free(msg);
David Benjamind781fc42017-07-12 16:25:57 -0400708 return 0;
Steven Valdeza833c352016-11-01 13:39:36 -0400709 }
710
David Benjamindaf207a2017-01-03 18:37:41 -0500711 return ssl->method->add_message(ssl, msg, len);
Steven Valdez5440fe02016-07-18 12:40:30 -0400712}
713
David Benjaminc3c88822016-11-14 10:32:04 +0900714static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
715 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400716 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
717 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
David Benjamin6dc8bf62017-07-19 16:38:21 -0400718 if (!hs->transcript.Init()) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500719 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
720 return -1;
721 }
722
David Benjamin68161cb2017-06-20 14:49:43 -0400723 /* Freeze the version range. */
724 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400725 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400726 }
727
David Benjaminf04c2e92016-12-06 13:35:25 -0500728 /* Always advertise the ClientHello version from the original maximum version,
729 * even on renegotiation. The static RSA key exchange uses this field, and
730 * some servers fail when it changes across handshakes. */
Steven Valdez8f36c512017-06-20 10:55:02 -0400731 if (SSL_is_dtls(hs->ssl)) {
732 hs->client_version =
733 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
734 } else {
735 hs->client_version =
736 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
David Benjamine8d53502015-10-10 14:13:23 -0400737 }
738
David Benjamine29ea162016-03-23 16:10:44 -0400739 /* If the configured session has expired or was created at a disabled
740 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400741 if (ssl->session != NULL) {
Adam Langley364f7a62016-12-12 10:51:00 -0800742 if (ssl->session->is_server ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400743 !ssl_supports_version(hs, ssl->session->ssl_version) ||
744 (ssl->session->session_id_length == 0 &&
745 ssl->session->tlsext_ticklen == 0) ||
Steven Valdez4aa154e2016-07-29 14:32:55 -0400746 ssl->session->not_resumable ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400747 !ssl_session_is_time_valid(ssl, ssl->session)) {
David Benjamin4d0be242016-09-01 01:10:07 -0400748 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400749 }
David Benjamine8d53502015-10-10 14:13:23 -0400750 }
751
752 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
753 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400754 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400755 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400756 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400757 }
758
Steven Valdez0e4a4482017-07-17 11:12:34 -0400759 /* Initialize a random session ID for the experimental TLS 1.3 variant
760 * requiring a session id. */
Steven Valdezdbe01582017-07-14 10:39:28 -0400761 if (ssl->tls13_variant == tls13_experiment) {
Steven Valdez520e1222017-06-13 12:45:25 -0400762 hs->session_id_len = sizeof(hs->session_id);
763 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
764 return -1;
765 }
766 }
767
David Benjamin8c880a22016-12-03 02:20:34 -0500768 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400769 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400770 }
771
David Benjamin16315f72017-01-12 20:02:05 -0500772 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800773}
Adam Langley95c29f32014-06-20 12:00:00 -0700774
David Benjamin8d606e32017-06-15 22:43:04 -0400775static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900776 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400777 CBS hello_verify_request, cookie;
778 uint16_t server_version;
779
David Benjaminf71036e2017-01-21 14:49:39 -0500780 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700781 if (ret <= 0) {
782 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400783 }
784
785 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
786 ssl->d1->send_cookie = 0;
787 ssl->s3->tmp.reuse_message = 1;
788 return 1;
789 }
790
David Benjaminf71036e2017-01-21 14:49:39 -0500791 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400792 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
793 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
David Benjamin8d606e32017-06-15 22:43:04 -0400794 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
David Benjaminaa7734b2016-06-07 16:40:46 -0400795 CBS_len(&hello_verify_request) != 0) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400796 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -0400797 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
798 return -1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400799 }
800
David Benjamin17cf2cb2016-12-13 01:07:13 -0500801 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400802 ssl->d1->cookie_len = CBS_len(&cookie);
803
804 ssl->d1->send_cookie = 1;
805 return 1;
David Benjaminaa7734b2016-06-07 16:40:46 -0400806}
807
David Benjaminb853f312017-07-14 18:40:34 -0400808static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out) {
809 SSL *const ssl = hs->ssl;
810 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
811 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
812 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
813 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
814 return 0;
815 }
816
817 CBS server_hello;
818 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
819 if (!CBS_get_u16(&server_hello, out)) {
820 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
821 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
822 return 0;
823 }
824
825 /* The server version may also be in the supported_versions extension if
826 * applicable. */
827 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO ||
828 *out != TLS1_2_VERSION) {
829 return 1;
830 }
831
832 uint8_t sid_length;
833 if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
834 !CBS_get_u8(&server_hello, &sid_length) ||
835 !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
836 1 /* compression_method */)) {
837 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
838 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
839 return 0;
840 }
841
842 /* The extensions block may not be present. */
843 if (CBS_len(&server_hello) == 0) {
844 return 1;
845 }
846
847 CBS extensions;
848 if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
849 CBS_len(&server_hello) != 0) {
850 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
851 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
852 return 0;
853 }
854
855 int have_supported_versions;
856 CBS supported_versions;
857 const SSL_EXTENSION_TYPE ext_types[] = {
858 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
859 &supported_versions},
860 };
861
862 uint8_t alert = SSL_AD_DECODE_ERROR;
863 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
864 OPENSSL_ARRAY_SIZE(ext_types),
865 1 /* ignore unknown */)) {
866 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
867 return 0;
868 }
869
870 if (have_supported_versions &&
871 (!CBS_get_u16(&supported_versions, out) ||
872 CBS_len(&supported_versions) != 0)) {
873 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
874 return 0;
875 }
876
877 return 1;
878}
879
David Benjaminc3c88822016-11-14 10:32:04 +0900880static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
881 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -0500882 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700883 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500884 uint32_t err = ERR_peek_error();
885 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
886 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
887 /* Add a dedicated error code to the queue for a handshake_failure alert
888 * in response to ClientHello. This matches NSS's client behavior and
889 * gives a better error on a (probable) failure to negotiate initial
890 * parameters. Note: this error code comes after the original one.
891 *
892 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400893 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500894 }
David Benjamin09eb6552016-07-08 14:32:11 -0700895 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800896 }
Adam Langley95c29f32014-06-20 12:00:00 -0700897
David Benjaminb853f312017-07-14 18:40:34 -0400898 uint16_t server_version;
899 if (!parse_server_version(hs, &server_version)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400900 return -1;
901 }
902
Steven Valdez8f36c512017-06-20 10:55:02 -0400903 if (!ssl_supports_version(hs, server_version)) {
David Benjamin1f61f0d2016-07-10 12:20:35 -0400904 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400905 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
906 return -1;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400907 }
908
David Benjamin0d56f882015-12-19 17:05:56 -0500909 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
910 if (!ssl->s3->have_version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400911 ssl->version = server_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500912 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800913 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500914 ssl->s3->have_version = 1;
Steven Valdez8f36c512017-06-20 10:55:02 -0400915 } else if (server_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400916 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400917 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
918 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Steven Valdez143e8b32016-07-11 13:19:03 -0400921 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500922 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900923 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400924 return 1;
925 }
926
Steven Valdez2d850622017-01-11 11:34:52 -0500927 if (hs->early_data_offered) {
928 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400929 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
930 return -1;
Steven Valdez2d850622017-01-11 11:34:52 -0500931 }
932
David Benjamin6e4fc332016-11-17 16:43:08 +0900933 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400934
David Benjamin276b7e82017-01-21 14:13:39 -0500935 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400936 return -1;
937 }
938
David Benjaminb853f312017-07-14 18:40:34 -0400939 CBS server_hello, server_random, session_id;
940 uint16_t cipher_suite;
941 uint8_t compression_method;
942 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
943 if (!CBS_skip(&server_hello, 2 /* version */) ||
944 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Steven Valdez143e8b32016-07-11 13:19:03 -0400945 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
946 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
947 !CBS_get_u16(&server_hello, &cipher_suite) ||
948 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400949 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400950 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
951 return -1;
Steven Valdez143e8b32016-07-11 13:19:03 -0400952 }
953
Adam Langley1bea1732014-12-17 19:06:57 -0800954 /* Copy over the server random. */
David Benjamind3bca042017-04-08 11:05:04 -0400955 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
956 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700957
David Benjamin55108632016-08-11 22:01:18 -0400958 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
959 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400960
David Benjamin0d56f882015-12-19 17:05:56 -0500961 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400962 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500963 CBS_mem_equal(&session_id, ssl->session->session_id,
964 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400965 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800966 } else {
967 /* The session wasn't resumed. Create a fresh SSL_SESSION to
968 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400969 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900970 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400971 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
972 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800973 }
974 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500975 hs->new_session->session_id_length = CBS_len(&session_id);
976 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
977 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800978 }
David Benjamina03d95d2014-07-12 19:49:07 -0400979
David Benjamind2cb1c12016-11-02 17:49:09 -0400980 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800981 if (c == NULL) {
982 /* unknown cipher */
David Benjamin3570d732015-06-29 00:28:17 -0400983 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400984 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
985 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800986 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400987
988 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500989 uint32_t mask_a, mask_k;
990 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
991 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700992 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400993 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
994 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
David Benjamin3570d732015-06-29 00:28:17 -0400995 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400996 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
997 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Steven Valdez87eab492016-06-27 16:34:59 -04001000 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001001 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -04001002 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001003 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1004 return -1;
David Benjaminece3de92015-03-16 18:02:20 -04001005 }
Steven Valdez803c77a2016-09-06 14:13:43 -04001006 if (ssl->session->cipher != c) {
Steven Valdez803c77a2016-09-06 14:13:43 -04001007 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -04001008 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1009 return -1;
Steven Valdez803c77a2016-09-06 14:13:43 -04001010 }
Steven Valdez4aa154e2016-07-29 14:32:55 -04001011 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
1012 /* This is actually a client application bug. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001013 OPENSSL_PUT_ERROR(SSL,
1014 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001015 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1016 return -1;
Steven Valdez4aa154e2016-07-29 14:32:55 -04001017 }
David Benjamin0623bce2015-12-25 15:40:14 -05001018 } else {
David Benjamin45738dd2017-02-09 20:01:26 -05001019 hs->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -08001020 }
David Benjamin45738dd2017-02-09 20:01:26 -05001021 hs->new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -05001022
David Benjaminf71036e2017-01-21 14:49:39 -05001023 /* Now that the cipher is known, initialize the handshake hash and hash the
1024 * ServerHello. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001025 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), c->algorithm_prf) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001026 !ssl_hash_current_message(hs)) {
David Benjamind3bca042017-04-08 11:05:04 -04001027 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1028 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001029 }
1030
Steven Valdez2b8415e2016-06-30 13:27:23 -04001031 /* If doing a full handshake, the server may request a client certificate
1032 * which requires hashing the handshake transcript. Otherwise, the handshake
1033 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001034 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -05001035 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001036 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -08001037 }
David Benjamina03d95d2014-07-12 19:49:07 -04001038
Adam Langley1bea1732014-12-17 19:06:57 -08001039 /* Only the NULL compression algorithm is supported. */
1040 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001041 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -04001042 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1043 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langley1bea1732014-12-17 19:06:57 -08001046 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -05001047 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -04001048 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamind3bca042017-04-08 11:05:04 -04001049 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001050 }
Adam Langley95c29f32014-06-20 12:00:00 -07001051
Adam Langley1bea1732014-12-17 19:06:57 -08001052 /* There should be nothing left over in the record. */
1053 if (CBS_len(&server_hello) != 0) {
1054 /* wrong packet length */
David Benjamin639846e2016-09-09 11:41:18 -04001055 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -04001056 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1057 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001058 }
Adam Langley95c29f32014-06-20 12:00:00 -07001059
Steven Valdez87eab492016-06-27 16:34:59 -04001060 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -05001061 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -05001062 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001063 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001064 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001065 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001066 }
David Benjamind3bca042017-04-08 11:05:04 -04001067 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1068 return -1;
Adam Langleyba5934b2015-06-02 10:50:35 -07001069 }
1070
Adam Langley1bea1732014-12-17 19:06:57 -08001071 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001072}
Adam Langley95c29f32014-06-20 12:00:00 -07001073
David Benjaminc3c88822016-11-14 10:32:04 +09001074static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1075 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001076 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001077 if (ret <= 0) {
1078 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001079 }
Adam Langley95c29f32014-06-20 12:00:00 -07001080
David Benjaminf71036e2017-01-21 14:49:39 -05001081 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001082 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001083 return -1;
1084 }
1085
David Benjamin5c900c82016-07-13 23:03:26 -04001086 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001087 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001088
Adam Langleyc68e5b92017-02-08 13:33:15 -08001089 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001090 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001091 hs->peer_pubkey.reset();
1092 hs->new_session->certs =
1093 ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL, &cbs, ssl->ctx->pool)
1094 .release();
David Benjamin45738dd2017-02-09 20:01:26 -05001095 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001096 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001097 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001098 }
Adam Langley95c29f32014-06-20 12:00:00 -07001099
David Benjamin45738dd2017-02-09 20:01:26 -05001100 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
Adam Langley68e71242016-12-12 11:06:16 -08001101 CBS_len(&cbs) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001102 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin3570d732015-06-29 00:28:17 -04001103 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001104 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001105 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001106 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001107
Adam Langley05672202016-12-13 12:05:49 -08001108 if (!ssl_check_leaf_certificate(
David Benjamin31b0c9b2017-07-20 14:49:15 -04001109 hs, hs->peer_pubkey.get(),
David Benjamin45738dd2017-02-09 20:01:26 -05001110 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001111 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001112 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001113 }
1114
David Benjamina58baaf2017-02-28 20:54:28 -05001115 /* Disallow the server certificate from changing during a renegotiation. See
1116 * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
1117 * so this check is sufficient. */
1118 if (ssl->s3->established_session != NULL) {
1119 if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
1120 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
1121 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1122 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1123 return -1;
1124 }
1125
1126 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
1127 const CRYPTO_BUFFER *old_cert =
1128 sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
1129 const CRYPTO_BUFFER *new_cert =
1130 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
1131 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
1132 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
1133 CRYPTO_BUFFER_data(new_cert),
1134 CRYPTO_BUFFER_len(old_cert)) != 0) {
1135 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1136 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1137 return -1;
1138 }
1139 }
1140 }
1141
David Benjamin5c900c82016-07-13 23:03:26 -04001142 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001143}
1144
David Benjaminc3c88822016-11-14 10:32:04 +09001145static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1146 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001147 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001148 if (ret <= 0) {
1149 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001150 }
1151
1152 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1153 /* A server may send status_request in ServerHello and then change
1154 * its mind about sending CertificateStatus. */
1155 ssl->s3->tmp.reuse_message = 1;
1156 return 1;
1157 }
1158
Steven Valdez908ac192017-01-12 13:17:07 -05001159 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001160 return -1;
1161 }
1162
David Benjamin8d606e32017-06-15 22:43:04 -04001163 CBS certificate_status, ocsp_response;
1164 uint8_t status_type;
David Benjamin09eb6552016-07-08 14:32:11 -07001165 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001166 if (!CBS_get_u8(&certificate_status, &status_type) ||
1167 status_type != TLSEXT_STATUSTYPE_ocsp ||
1168 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1169 CBS_len(&ocsp_response) == 0 ||
1170 CBS_len(&certificate_status) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001171 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001172 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1173 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001174 }
1175
David Benjamin45738dd2017-02-09 20:01:26 -05001176 if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
1177 &hs->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001178 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001179 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1180 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001181 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001182
David Benjamin8d606e32017-06-15 22:43:04 -04001183 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001184}
1185
David Benjaminc3c88822016-11-14 10:32:04 +09001186static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1187 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001188 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001189 if (ret <= 0) {
1190 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001191 }
1192
David Benjamin0d56f882015-12-19 17:05:56 -05001193 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001194 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001195 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001196 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001197 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001198 return -1;
1199 }
1200
David Benjamin0d56f882015-12-19 17:05:56 -05001201 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001202 return 1;
1203 }
1204
Steven Valdez908ac192017-01-12 13:17:07 -05001205 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001206 return -1;
1207 }
1208
Adam Langley1bea1732014-12-17 19:06:57 -08001209 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001210 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001211 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001212 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001213
David Benjamin45738dd2017-02-09 20:01:26 -05001214 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1215 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001216
1217 if (alg_a & SSL_aPSK) {
1218 CBS psk_identity_hint;
1219
1220 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1221 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1222 &psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001223 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001224 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001225 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001226 }
1227
1228 /* Store PSK identity hint for later use, hint is used in
1229 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1230 * identity hint can be as long as the maximum length of a PSK identity.
1231 * Also do not allow NULL characters; identities are saved as C strings.
1232 *
1233 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1234 * a specific identity. */
1235 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1236 CBS_contains_zero_byte(&psk_identity_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -04001237 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamin8d606e32017-06-15 22:43:04 -04001238 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -04001239 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001240 }
1241
David Benjamin78679342016-09-16 19:42:05 -04001242 /* Save non-empty identity hints as a C string. Empty identity hints we
1243 * treat as missing. Plain PSK makes it possible to send either no hint
1244 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1245 * empty hint. Having different capabilities is odd, so we interpret empty
1246 * and missing as identical. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001247 char *raw = nullptr;
David Benjamin78679342016-09-16 19:42:05 -04001248 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin31b0c9b2017-07-20 14:49:15 -04001249 !CBS_strdup(&psk_identity_hint, &raw)) {
David Benjamin3570d732015-06-29 00:28:17 -04001250 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001251 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001252 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001253 }
David Benjamin31b0c9b2017-07-20 14:49:15 -04001254 hs->peer_psk_identity_hint.reset(raw);
Adam Langley1bea1732014-12-17 19:06:57 -08001255 }
1256
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001257 if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001258 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001259 uint8_t group_type;
1260 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001261 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001262 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1263 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001264 !CBS_get_u16(&server_key_exchange, &group_id) ||
1265 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
David Benjamin3570d732015-06-29 00:28:17 -04001266 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001267 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001268 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001269 }
David Benjamin45738dd2017-02-09 20:01:26 -05001270 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001271
Steven Valdezce902a92016-05-17 11:47:53 -04001272 /* Ensure the group is consistent with preferences. */
1273 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001274 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
David Benjamin8d606e32017-06-15 22:43:04 -04001275 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
David Benjamin1386aad2017-07-19 23:57:40 -04001276 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001277 }
David Benjamin4298d772015-12-19 00:18:25 -05001278
Matt Braithwaite053931e2016-05-25 12:06:05 -07001279 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001280 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1281 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001282 return -1;
David Benjaminc1cc8582015-12-17 01:22:15 -05001283 }
Adam Langley1bea1732014-12-17 19:06:57 -08001284 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin3570d732015-06-29 00:28:17 -04001285 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001286 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin1386aad2017-07-19 23:57:40 -04001287 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001288 }
1289
1290 /* At this point, |server_key_exchange| contains the signature, if any, while
1291 * |server_key_exchange_orig| contains the entire message. From that, derive
1292 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001293 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001294 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1295 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1296
David Benjaminf4958e72015-06-07 11:00:53 -04001297 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001298 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001299 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001300 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001301 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001302 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001303 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001304 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001305 }
David Benjamin8d606e32017-06-15 22:43:04 -04001306 uint8_t alert = SSL_AD_DECODE_ERROR;
1307 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
1308 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin1386aad2017-07-19 23:57:40 -04001309 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001310 }
David Benjamin45738dd2017-02-09 20:01:26 -05001311 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001312 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001313 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001314 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamin8d606e32017-06-15 22:43:04 -04001315 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
David Benjamin1386aad2017-07-19 23:57:40 -04001316 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001317 }
1318
1319 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001320 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001321 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1322 CBS_len(&server_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001323 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001324 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001325 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001326 }
1327
David Benjamin1386aad2017-07-19 23:57:40 -04001328 ScopedCBB transcript;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001329 uint8_t *transcript_data;
1330 size_t transcript_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001331 if (!CBB_init(transcript.get(),
1332 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1333 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1334 SSL3_RANDOM_SIZE) ||
1335 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1336 SSL3_RANDOM_SIZE) ||
1337 !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1338 CBS_len(&parameter)) ||
1339 !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001340 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8d606e32017-06-15 22:43:04 -04001341 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001342 return -1;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001343 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001344
1345 int sig_ok = ssl_public_key_verify(
1346 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001347 hs->peer_pubkey.get(), transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001348 OPENSSL_free(transcript_data);
1349
David Benjaminbf82aed2016-03-01 22:57:40 -05001350#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1351 sig_ok = 1;
1352 ERR_clear_error();
1353#endif
1354 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001355 /* bad signature */
David Benjamin3570d732015-06-29 00:28:17 -04001356 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamin8d606e32017-06-15 22:43:04 -04001357 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001358 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001359 }
1360 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001361 /* PSK ciphers are the only supported certificate-less ciphers. */
1362 assert(alg_a == SSL_aPSK);
1363
Adam Langley1bea1732014-12-17 19:06:57 -08001364 if (CBS_len(&server_key_exchange) > 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001365 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
David Benjamin8d606e32017-06-15 22:43:04 -04001366 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001367 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001368 }
1369 }
Adam Langley1bea1732014-12-17 19:06:57 -08001370 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001371}
Adam Langley95c29f32014-06-20 12:00:00 -07001372
David Benjaminc3c88822016-11-14 10:32:04 +09001373static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1374 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001375 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001376 if (msg_ret <= 0) {
1377 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001378 }
David Benjamin688d8df2014-11-02 23:06:42 -05001379
David Benjamin3dcec452016-06-07 17:13:28 -04001380 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001381 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001382 /* If we get here we don't need the handshake buffer as we won't be doing
1383 * client auth. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001384 hs->transcript.FreeBuffer();
Adam Langley1bea1732014-12-17 19:06:57 -08001385 return 1;
1386 }
David Benjamined439582014-07-14 19:13:02 -04001387
David Benjaminf71036e2017-01-21 14:49:39 -05001388 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001389 !ssl_hash_current_message(hs)) {
David Benjamine0332e82016-07-13 22:40:36 -04001390 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001391 }
Adam Langley95c29f32014-06-20 12:00:00 -07001392
David Benjamina5177cb2016-03-11 19:38:17 -05001393 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001394 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001395
David Benjamine0332e82016-07-13 22:40:36 -04001396 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001397 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001398 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001399 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001400 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001401 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001402 }
David Benjamined439582014-07-14 19:13:02 -04001403
David Benjaminc3c88822016-11-14 10:32:04 +09001404 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1405 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001406 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001407 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001408 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001409
David Benjamina1e9cab2015-12-30 00:08:49 -05001410 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001411 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001412 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001413 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001414 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001415 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001416 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001417 }
Adam Langley1bea1732014-12-17 19:06:57 -08001418 }
David Benjamined439582014-07-14 19:13:02 -04001419
Adam Langleyc68e5b92017-02-08 13:33:15 -08001420 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001421 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
Adam Langley34b4c822017-02-02 10:57:17 -08001422 ssl_parse_client_CA_list(ssl, &alert, &cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -04001423 if (!ca_names) {
David Benjamine0332e82016-07-13 22:40:36 -04001424 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1425 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001426 }
Adam Langley95c29f32014-06-20 12:00:00 -07001427
David Benjamin639846e2016-09-09 11:41:18 -04001428 if (CBS_len(&cbs) != 0) {
1429 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001430 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1431 return -1;
1432 }
1433
David Benjaminc3c88822016-11-14 10:32:04 +09001434 hs->cert_request = 1;
David Benjamin31b0c9b2017-07-20 14:49:15 -04001435 hs->ca_names = std::move(ca_names);
Adam Langley34b4c822017-02-02 10:57:17 -08001436 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamine0332e82016-07-13 22:40:36 -04001437 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001438}
Adam Langley95c29f32014-06-20 12:00:00 -07001439
David Benjaminc3c88822016-11-14 10:32:04 +09001440static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1441 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001442 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001443 if (ret <= 0) {
1444 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001445 }
David Benjamin46062682014-07-14 19:14:32 -04001446
David Benjaminf71036e2017-01-21 14:49:39 -05001447 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001448 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001449 return -1;
1450 }
1451
David Benjamin09eb6552016-07-08 14:32:11 -07001452 /* ServerHelloDone is empty. */
1453 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001454 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001455 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001456 return -1;
1457 }
David Benjamin46062682014-07-14 19:14:32 -04001458
Adam Langley1bea1732014-12-17 19:06:57 -08001459 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001460}
Adam Langley1258b6a2014-06-20 12:00:00 -07001461
David Benjaminc3c88822016-11-14 10:32:04 +09001462static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1463 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001464 /* Call cert_cb to update the certificate. */
1465 if (ssl->cert->cert_cb) {
1466 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1467 if (ret < 0) {
1468 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001469 return -1;
1470 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001471 if (ret == 0) {
1472 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1473 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001474 return -1;
1475 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001476 }
1477
David Benjamin5edfc8c2016-12-10 15:46:58 -05001478 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001479 /* Without a client certificate, the handshake buffer may be released. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001480 hs->transcript.FreeBuffer();
David Benjamin5edfc8c2016-12-10 15:46:58 -05001481
David Benjamindaf207a2017-01-03 18:37:41 -05001482 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001483 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001484 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1485 SSL_AD_NO_CERTIFICATE)) {
1486 return -1;
1487 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001488 return 1;
1489 }
1490 }
1491
David Benjamina232a712017-03-30 15:51:53 -05001492 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001493 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001494 return -1;
1495 }
David Benjamin16315f72017-01-12 20:02:05 -05001496 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001497}
1498
David Benjamina3d76d02017-07-14 19:36:07 -04001499static_assert(sizeof(size_t) >= sizeof(unsigned),
1500 "size_t is smaller than unsigned");
Adam Langley1258b6a2014-06-20 12:00:00 -07001501
David Benjaminc3c88822016-11-14 10:32:04 +09001502static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1503 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -04001504 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001505 CBB body;
1506 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001507 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjamind781fc42017-07-12 16:25:57 -04001508 return -1;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001509 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001510
David Benjamind781fc42017-07-12 16:25:57 -04001511 uint8_t *pms = NULL;
1512 size_t pms_len = 0;
David Benjamin45738dd2017-02-09 20:01:26 -05001513 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1514 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001515
David Benjaminae0eaaa2015-12-17 02:01:25 -05001516 /* If using a PSK key exchange, prepare the pre-shared key. */
1517 unsigned psk_len = 0;
1518 uint8_t psk[PSK_MAX_PSK_LEN];
1519 if (alg_a & SSL_aPSK) {
1520 if (ssl->psk_client_callback == NULL) {
1521 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1522 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001523 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001524
David Benjaminae0eaaa2015-12-17 02:01:25 -05001525 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001526 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001527 psk_len =
David Benjamin31b0c9b2017-07-20 14:49:15 -04001528 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
1529 identity, sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001530 if (psk_len == 0) {
1531 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1532 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1533 goto err;
1534 }
1535 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001536
David Benjamin45738dd2017-02-09 20:01:26 -05001537 OPENSSL_free(hs->new_session->psk_identity);
1538 hs->new_session->psk_identity = BUF_strdup(identity);
1539 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001540 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1541 goto err;
1542 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001543
David Benjaminae0eaaa2015-12-17 02:01:25 -05001544 /* Write out psk_identity. */
1545 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001546 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001547 !CBB_add_bytes(&child, (const uint8_t *)identity,
1548 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001549 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001550 goto err;
1551 }
1552 }
David Benjamin758d1272015-11-20 17:47:25 -05001553
David Benjaminae0eaaa2015-12-17 02:01:25 -05001554 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1555 if (alg_k & SSL_kRSA) {
1556 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001557 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001558 if (pms == NULL) {
1559 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1560 goto err;
1561 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001562
David Benjamin31b0c9b2017-07-20 14:49:15 -04001563 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
David Benjaminae0eaaa2015-12-17 02:01:25 -05001564 if (rsa == NULL) {
1565 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001566 goto err;
1567 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001568
David Benjaminf04c2e92016-12-06 13:35:25 -05001569 pms[0] = hs->client_version >> 8;
1570 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001571 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1572 goto err;
1573 }
1574
David Benjamin75836432016-06-17 18:48:29 -04001575 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001576 size_t enc_pms_len;
1577 /* In TLS, there is a length prefix. */
1578 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001579 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001580 goto err;
1581 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001582 enc_pms = &child;
1583 }
Adam Langley1bea1732014-12-17 19:06:57 -08001584
David Benjaminae0eaaa2015-12-17 02:01:25 -05001585 uint8_t *ptr;
1586 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1587 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1588 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001589 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001590 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001591 goto err;
1592 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001593 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001594 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001595 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001596 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001597 goto err;
1598 }
1599
David Benjamin4298d772015-12-19 00:18:25 -05001600 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001601 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001602 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1603 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001604 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1605 goto err;
1606 }
David Benjamin75836432016-06-17 18:48:29 -04001607 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001608 goto err;
1609 }
David Benjamin4298d772015-12-19 00:18:25 -05001610
1611 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001612 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1613 OPENSSL_free(hs->peer_key);
1614 hs->peer_key = NULL;
1615 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001616 } else if (alg_k & SSL_kPSK) {
1617 /* For plain PSK, other_secret is a block of 0s with the same length as
1618 * the pre-shared key. */
1619 pms_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001620 pms = (uint8_t *)OPENSSL_malloc(pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001621 if (pms == NULL) {
1622 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1623 goto err;
1624 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001625 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001626 } else {
1627 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1628 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1629 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001630 }
1631
David Benjaminae0eaaa2015-12-17 02:01:25 -05001632 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1633 * key. */
1634 if (alg_a & SSL_aPSK) {
David Benjamin1386aad2017-07-19 23:57:40 -04001635 ScopedCBB pms_cbb;
1636 CBB child;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001637 uint8_t *new_pms;
1638 size_t new_pms_len;
1639
David Benjamin1386aad2017-07-19 23:57:40 -04001640 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms_len) ||
1641 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001642 !CBB_add_bytes(&child, pms, pms_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001643 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001644 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001645 !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001646 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1647 goto err;
1648 }
1649 OPENSSL_cleanse(pms, pms_len);
1650 OPENSSL_free(pms);
1651 pms = new_pms;
1652 pms_len = new_pms_len;
1653 }
1654
1655 /* The message must be added to the finished hash before calculating the
1656 * master secret. */
David Benjamind781fc42017-07-12 16:25:57 -04001657 if (!ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001658 goto err;
1659 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001660
David Benjamin45738dd2017-02-09 20:01:26 -05001661 hs->new_session->master_key_length = tls1_generate_master_secret(
1662 hs, hs->new_session->master_key, pms, pms_len);
1663 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001664 goto err;
1665 }
David Benjaminfc02b592017-02-17 16:26:01 -05001666 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001667 OPENSSL_cleanse(pms, pms_len);
1668 OPENSSL_free(pms);
1669
David Benjamin16315f72017-01-12 20:02:05 -05001670 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001671
1672err:
David Benjaminae0eaaa2015-12-17 02:01:25 -05001673 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001674 OPENSSL_cleanse(pms, pms_len);
1675 OPENSSL_free(pms);
1676 }
1677 return -1;
1678}
Adam Langley1258b6a2014-06-20 12:00:00 -07001679
David Benjaminc3c88822016-11-14 10:32:04 +09001680static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1681 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001682 assert(ssl_has_private_key(ssl));
1683
David Benjamin86e95b82017-07-18 16:34:25 -04001684 ScopedCBB cbb;
David Benjamind781fc42017-07-12 16:25:57 -04001685 CBB body, child;
1686 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001687 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjamind781fc42017-07-12 16:25:57 -04001688 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001689 }
1690
David Benjaminea9a0d52016-07-08 15:52:59 -07001691 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001692 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001693 return -1;
David Benjaminea9a0d52016-07-08 15:52:59 -07001694 }
David Benjamin429fdc02016-06-17 17:33:30 -04001695 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001696 /* Write out the digest type in TLS 1.2. */
1697 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001698 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001699 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001700 }
1701 }
David Benjaminfc825122015-12-18 01:57:43 -05001702
David Benjamin429fdc02016-06-17 17:33:30 -04001703 /* Set aside space for the signature. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001704 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamin429fdc02016-06-17 17:33:30 -04001705 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001706 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001707 !CBB_reserve(&child, &ptr, max_sig_len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001708 return -1;
David Benjamin429fdc02016-06-17 17:33:30 -04001709 }
1710
Steven Valdez2b8415e2016-06-30 13:27:23 -04001711 size_t sig_len = max_sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001712 /* The SSL3 construction for CertificateVerify does not decompose into a
1713 * single final digest and signature, and must be special-cased. */
1714 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1715 if (ssl->cert->key_method != NULL) {
1716 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
David Benjamind781fc42017-07-12 16:25:57 -04001717 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001718 }
1719
David Benjamin44148742017-06-17 13:20:59 -04001720 uint8_t digest[EVP_MAX_MD_SIZE];
1721 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001722 if (!hs->transcript.GetSSL3CertVerifyHash(
1723 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
David Benjamind781fc42017-07-12 16:25:57 -04001724 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001725 }
David Benjaminfc825122015-12-18 01:57:43 -05001726
David Benjamin1386aad2017-07-19 23:57:40 -04001727 UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
1728 if (!pctx ||
1729 !EVP_PKEY_sign_init(pctx.get()) ||
1730 !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001731 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001732 }
1733 } else {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001734 switch (ssl_private_key_sign(
1735 hs, ptr, &sig_len, max_sig_len, signature_algorithm,
1736 hs->transcript.buffer_data(), hs->transcript.buffer_len())) {
David Benjamin44148742017-06-17 13:20:59 -04001737 case ssl_private_key_success:
1738 break;
1739 case ssl_private_key_failure:
David Benjamind781fc42017-07-12 16:25:57 -04001740 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001741 case ssl_private_key_retry:
1742 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamind781fc42017-07-12 16:25:57 -04001743 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001744 }
David Benjaminfc825122015-12-18 01:57:43 -05001745 }
1746
David Benjaminfc825122015-12-18 01:57:43 -05001747 if (!CBB_did_write(&child, sig_len) ||
David Benjamind781fc42017-07-12 16:25:57 -04001748 !ssl_add_message_cbb(ssl, cbb.get())) {
1749 return -1;
David Benjaminfc825122015-12-18 01:57:43 -05001750 }
1751
David Benjamin44148742017-06-17 13:20:59 -04001752 /* The handshake buffer is no longer necessary. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001753 hs->transcript.FreeBuffer();
David Benjamin16315f72017-01-12 20:02:05 -05001754 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001755}
1756
David Benjaminc3c88822016-11-14 10:32:04 +09001757static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1758 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001759 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001760 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001761
David Benjamin1386aad2017-07-19 23:57:40 -04001762 ScopedCBB cbb;
1763 CBB body, child;
1764 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
David Benjamin75836432016-06-17 18:48:29 -04001765 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001766 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1767 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001768 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001769 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001770 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamine97b9622015-10-10 14:59:09 -04001771 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamine97b9622015-10-10 14:59:09 -04001772 return -1;
1773 }
1774
David Benjamin16315f72017-01-12 20:02:05 -05001775 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001776}
1777
David Benjaminc3c88822016-11-14 10:32:04 +09001778static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1779 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001780 if (!ssl_do_channel_id_callback(ssl)) {
1781 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001782 }
1783
David Benjamin96e97b12015-10-10 16:30:22 -04001784 if (ssl->tlsext_channel_id_private == NULL) {
1785 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001786 return -1;
1787 }
Adam Langley1bea1732014-12-17 19:06:57 -08001788
David Benjamin1386aad2017-07-19 23:57:40 -04001789 ScopedCBB cbb;
1790 CBB body;
1791 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001792 !tls1_write_channel_id(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001793 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin3570d732015-06-29 00:28:17 -04001794 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Nick Harper60a85cb2016-09-23 16:25:11 -07001795 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001796 }
1797
David Benjamin16315f72017-01-12 20:02:05 -05001798 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001799}
1800
David Benjaminc3c88822016-11-14 10:32:04 +09001801static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1802 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001803 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001804 if (ret <= 0) {
1805 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001806 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001807
David Benjaminf71036e2017-01-21 14:49:39 -05001808 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001809 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001810 return -1;
1811 }
1812
David Benjamin9f1dc822016-06-07 17:03:46 -04001813 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001814 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001815 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001816 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001817 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1818 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001819 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001820 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001821 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001822 }
1823
David Benjamin9f1dc822016-06-07 17:03:46 -04001824 if (CBS_len(&ticket) == 0) {
1825 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001826 * negotiating the extension. The value of |ticket_expected| is checked in
1827 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001828 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001829 return 1;
1830 }
1831
David Benjamin31b0c9b2017-07-20 14:49:15 -04001832 SSL_SESSION *session = hs->new_session.get();
David Benjamin1386aad2017-07-19 23:57:40 -04001833 UniquePtr<SSL_SESSION> renewed_session;
1834 if (ssl->session != NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001835 /* The server is sending a new ticket for an existing session. Sessions are
1836 * immutable once established, so duplicate all but the ticket of the
1837 * existing session. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001838 renewed_session =
1839 SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001840 if (!renewed_session) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001841 /* This should never happen. */
1842 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001843 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001844 }
David Benjamin1386aad2017-07-19 23:57:40 -04001845 session = renewed_session.get();
David Benjamin9f1dc822016-06-07 17:03:46 -04001846 }
1847
David Benjamin123db572016-11-03 16:59:25 -04001848 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001849 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001850
Steven Valdez87eab492016-06-27 16:34:59 -04001851 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001852 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -04001853 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001854 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001855 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001856
1857 /* Generate a session ID for this session based on the session ticket. We use
1858 * the session ID mechanism for detecting ticket resumption. This also fits in
1859 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001860 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1861 session->session_id, &session->session_id_length,
1862 EVP_sha256(), NULL)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001863 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001864 }
1865
David Benjamin1386aad2017-07-19 23:57:40 -04001866 if (renewed_session) {
Steven Valdez87eab492016-06-27 16:34:59 -04001867 session->not_resumable = 0;
1868 SSL_SESSION_free(ssl->session);
David Benjamin1386aad2017-07-19 23:57:40 -04001869 ssl->session = renewed_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -04001870 }
1871
David Benjamin454aa4c2016-03-10 08:43:27 -05001872 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001873}
David Benjamin86e95b82017-07-18 16:34:25 -04001874
1875} // namespace bssl