blob: 857cdf3e5b1412b88925413e051432ad583ac381 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE.
148 */
149
David Benjamin9e4e01e2015-09-15 01:48:04 -0400150#include <openssl/ssl.h>
151
David Benjamin81ea0bf2014-11-23 04:20:17 -0500152#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjaminabbbee12016-10-31 19:20:42 -0400155#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400156#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400158#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400159#include <openssl/ec_key.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400164#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400165#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin17cf2cb2016-12-13 01:07:13 -0500167#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400168#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjaminc3c88822016-11-14 10:32:04 +0900171static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
172static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
173static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
174static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
175static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
176static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
177static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
178static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
179static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
180static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
181static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
182static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
183static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
184static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
185static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400186
David Benjamince8c9d22016-11-14 10:45:16 +0900187int ssl3_connect(SSL_HANDSHAKE *hs) {
188 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800189 int ret = -1;
David Benjaminbeb47022014-11-30 02:58:52 -0500190
David Benjamin0d56f882015-12-19 17:05:56 -0500191 assert(ssl->handshake_func == ssl3_connect);
192 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700193
Adam Langley1bea1732014-12-17 19:06:57 -0800194 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500195 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjamincb0c29f2016-12-12 17:00:50 -0500197 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400198 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400199 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800201 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langley1bea1732014-12-17 19:06:57 -0800203 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900204 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800205 if (ret <= 0) {
206 goto end;
207 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400208
David Benjamince079fd2016-08-02 16:22:34 -0400209 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
Steven Valdez2d850622017-01-11 11:34:52 -0500210 if (hs->early_data_offered) {
211 if (!tls13_init_early_key_schedule(hs) ||
212 !tls13_advance_key_schedule(hs, ssl->session->master_key,
213 ssl->session->master_key_length) ||
214 !tls13_derive_early_secrets(hs) ||
215 !tls13_set_traffic_key(ssl, evp_aead_seal,
216 hs->early_traffic_secret,
217 hs->hash_len)) {
218 ret = -1;
219 goto end;
220 }
Steven Valdeze831a812017-03-09 14:56:07 -0500221 hs->next_state = SSL3_ST_WRITE_EARLY_DATA;
222 } else {
223 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
Steven Valdez2d850622017-01-11 11:34:52 -0500224 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400225 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500226 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400227 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500228 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800229 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjaminaa7734b2016-06-07 16:40:46 -0400231 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400232 assert(SSL_is_dtls(ssl));
David Benjaminc3c88822016-11-14 10:32:04 +0900233 ret = dtls1_get_hello_verify(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400234 if (ret <= 0) {
235 goto end;
236 }
237 if (ssl->d1->send_cookie) {
238 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500239 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400240 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500241 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400242 }
243 break;
244
Steven Valdeze831a812017-03-09 14:56:07 -0500245 case SSL3_ST_WRITE_EARLY_DATA:
246 /* Stash the early data session, so connection properties may be queried
247 * out of it. */
248 hs->in_early_data = 1;
249 hs->early_session = ssl->session;
250 SSL_SESSION_up_ref(ssl->session);
251
252 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
253 hs->can_early_write = 1;
254 ret = 1;
255 goto end;
256
Adam Langley1bea1732014-12-17 19:06:57 -0800257 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900258 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500259 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400260 break;
261 }
Adam Langley1bea1732014-12-17 19:06:57 -0800262 if (ret <= 0) {
263 goto end;
264 }
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Steven Valdez87eab492016-06-27 16:34:59 -0400266 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500267 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800268 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500269 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800270 }
Adam Langley1bea1732014-12-17 19:06:57 -0800271 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400272
Adam Langley1bea1732014-12-17 19:06:57 -0800273 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500274 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900275 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800276 if (ret <= 0) {
277 goto end;
278 }
Adam Langley1bea1732014-12-17 19:06:57 -0800279 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500280 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800281 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700282
David Benjamin9f1dc822016-06-07 17:03:46 -0400283 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900284 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900285 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400286 if (ret <= 0) {
287 goto end;
288 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400289 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500290 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400291 break;
292
Paul Lietar8f1c2682015-08-18 12:21:54 +0100293 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500294 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900295 ret = ssl3_verify_server_cert(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400296 if (ret <= 0) {
297 goto end;
298 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100299 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500300 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100301 break;
302
Adam Langley1bea1732014-12-17 19:06:57 -0800303 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900304 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800305 if (ret <= 0) {
306 goto end;
307 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500308 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800309 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langley1bea1732014-12-17 19:06:57 -0800311 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500312 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900313 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400314 if (ret <= 0) {
315 goto end;
316 }
Adam Langley1bea1732014-12-17 19:06:57 -0800317 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500318 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900322 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800323 if (ret <= 0) {
324 goto end;
325 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400326 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500327 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800328 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langley1bea1732014-12-17 19:06:57 -0800330 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900331 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900332 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400333 if (ret <= 0) {
334 goto end;
335 }
Adam Langley1bea1732014-12-17 19:06:57 -0800336 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500337 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800338 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley1bea1732014-12-17 19:06:57 -0800340 case SSL3_ST_CW_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900341 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800342 if (ret <= 0) {
343 goto end;
344 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500345 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800346 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley1bea1732014-12-17 19:06:57 -0800348 case SSL3_ST_CW_CERT_VRFY_A:
349 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamina660e7a2017-01-02 08:15:35 -0500350 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900351 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400352 if (ret <= 0) {
353 goto end;
354 }
Adam Langley1bea1732014-12-17 19:06:57 -0800355 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500356 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800357 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700358
David Benjamin352d0a92016-06-28 11:22:02 -0400359 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500360 if (!ssl->method->add_change_cipher_spec(ssl) ||
361 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800362 ret = -1;
363 goto end;
364 }
Adam Langley95c29f32014-06-20 12:00:00 -0700365
David Benjamindaf207a2017-01-03 18:37:41 -0500366 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800367 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langley1bea1732014-12-17 19:06:57 -0800369 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900370 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900371 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400372 if (ret <= 0) {
373 goto end;
374 }
Adam Langley1bea1732014-12-17 19:06:57 -0800375 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500376 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800377 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langley1bea1732014-12-17 19:06:57 -0800379 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400380 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900381 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400382 if (ret <= 0) {
383 goto end;
384 }
Adam Langley1bea1732014-12-17 19:06:57 -0800385 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500386 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800387 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langley1bea1732014-12-17 19:06:57 -0800389 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500390 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800391 if (ret <= 0) {
392 goto end;
393 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500394 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Steven Valdez87eab492016-06-27 16:34:59 -0400396 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500397 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800398 } else {
399 /* This is a non-resumption handshake. If it involves ChannelID, then
400 * record the handshake hashes at this point in the session so that
401 * any resumption of this session with ChannelID can sign those
402 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500403 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700404 if (ret <= 0) {
405 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800406 }
David Benjamin0d56f882015-12-19 17:05:56 -0500407 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
408 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500409 /* No False Start on renegotiation (would complicate the state
410 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500411 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500412 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800413 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500414 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800415 }
416 }
Adam Langley1bea1732014-12-17 19:06:57 -0800417 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700418
David Benjamin9f1dc822016-06-07 17:03:46 -0400419 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500420 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900421 hs->in_false_start = 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500422 hs->can_early_write = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400423 ret = 1;
424 goto end;
425
Adam Langley1bea1732014-12-17 19:06:57 -0800426 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900427 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900428 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400429 if (ret <= 0) {
430 goto end;
431 }
Adam Langley1bea1732014-12-17 19:06:57 -0800432 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500433 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800434 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langley1bea1732014-12-17 19:06:57 -0800436 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400437 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500438 if (ret <= 0) {
439 goto end;
440 }
441
David Benjamin67739722016-11-17 17:03:59 +0900442 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800443 ret = -1;
444 goto end;
445 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500446 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800447 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langley1bea1732014-12-17 19:06:57 -0800449 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900450 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800451 if (ret <= 0) {
452 goto end;
453 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400454 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Steven Valdez87eab492016-06-27 16:34:59 -0400456 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500457 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800458 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500459 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800460 }
Adam Langley1bea1732014-12-17 19:06:57 -0800461 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langley1bea1732014-12-17 19:06:57 -0800463 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500464 ret = ssl->method->flush_flight(ssl);
465 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800466 goto end;
467 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500468 hs->state = hs->next_state;
David Benjamin77458a42017-02-10 23:33:27 -0500469 if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400470 ssl->method->expect_flight(ssl);
471 }
Adam Langley1bea1732014-12-17 19:06:57 -0800472 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700473
Steven Valdez681eb6a2016-12-19 13:19:29 -0500474 case SSL_ST_TLS13: {
475 int early_return = 0;
476 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400477 if (ret <= 0) {
478 goto end;
479 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500480
481 if (early_return) {
482 ret = 1;
483 goto end;
484 }
485
David Benjamin77458a42017-02-10 23:33:27 -0500486 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400487 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500488 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400489
David Benjamin77458a42017-02-10 23:33:27 -0500490 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin4497e582016-07-27 17:51:49 -0400491 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700492
Steven Valdez87eab492016-06-27 16:34:59 -0400493 SSL_SESSION_free(ssl->s3->established_session);
494 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400495 SSL_SESSION_up_ref(ssl->session);
496 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400497 } else {
498 /* We make a copy of the session in order to maintain the immutability
499 * of the new established_session due to False Start. The caller may
500 * have taken a reference to the temporary session. */
501 ssl->s3->established_session =
David Benjamin45738dd2017-02-09 20:01:26 -0500502 SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400503 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400504 ret = -1;
505 goto end;
506 }
507 ssl->s3->established_session->not_resumable = 0;
508
David Benjamin45738dd2017-02-09 20:01:26 -0500509 SSL_SESSION_free(hs->new_session);
510 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400511 }
512
David Benjamin77458a42017-02-10 23:33:27 -0500513 hs->state = SSL_ST_OK;
514 break;
515
516 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500517 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500518 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400519 if (is_initial_handshake) {
520 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900521 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400522 }
Adam Langley95c29f32014-06-20 12:00:00 -0700523
Adam Langley1bea1732014-12-17 19:06:57 -0800524 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400525 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800526 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500527 }
Steven Valdez87eab492016-06-27 16:34:59 -0400528
Adam Langley1bea1732014-12-17 19:06:57 -0800529 default:
David Benjamin3570d732015-06-29 00:28:17 -0400530 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800531 ret = -1;
532 goto end;
533 }
534
David Benjamin16b1b1d2017-02-10 23:41:14 -0500535 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400536 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800537 }
Adam Langley1bea1732014-12-17 19:06:57 -0800538 }
539
Adam Langley95c29f32014-06-20 12:00:00 -0700540end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400541 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800542 return ret;
543}
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin65ac9972016-09-02 21:35:25 -0400545uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
546 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
547 * a single byte repeatedly and ensures the values are deterministic. This
548 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
549 * same group be advertised in both supported_groups and key_shares. */
550 uint16_t ret = ssl->s3->client_random[index];
551 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
552 ret = (ret & 0xf0) | 0x0a;
553 ret |= ret << 8;
554 return ret;
555}
556
David Benjamin2fe6e222017-01-25 02:18:43 -0500557/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
558 * disabled algorithms. */
559static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
560 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500561 *out_mask_a = 0;
562 *out_mask_k = 0;
563
David Benjamin2fe6e222017-01-25 02:18:43 -0500564 /* PSK requires a client callback. */
565 if (ssl->psk_client_callback == NULL) {
566 *out_mask_a |= SSL_aPSK;
567 *out_mask_k |= SSL_kPSK;
568 }
569}
570
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200571static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
572 uint16_t min_version,
David Benjamine0ff7672016-09-19 18:40:03 -0400573 uint16_t max_version) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500574 uint32_t mask_a, mask_k;
575 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700576
David Benjamine8d53502015-10-10 14:13:23 -0400577 CBB child;
578 if (!CBB_add_u16_length_prefixed(out, &child)) {
579 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800580 }
581
David Benjamin65ac9972016-09-02 21:35:25 -0400582 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
583 if (ssl->ctx->grease_enabled &&
584 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
585 return 0;
586 }
587
David Benjaminabbbee12016-10-31 19:20:42 -0400588 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
589 * hardware support. */
590 if (max_version >= TLS1_3_VERSION) {
591 if (!EVP_has_aes_hardware() &&
592 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
593 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500594 }
David Benjaminabbbee12016-10-31 19:20:42 -0400595 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
596 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
597 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400598 }
David Benjaminabbbee12016-10-31 19:20:42 -0400599 if (EVP_has_aes_hardware() &&
600 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400601 return 0;
602 }
603 }
604
David Benjaminabbbee12016-10-31 19:20:42 -0400605 if (min_version < TLS1_3_VERSION) {
606 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
607 int any_enabled = 0;
608 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
609 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
610 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500611 if ((cipher->algorithm_mkey & mask_k) ||
612 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400613 continue;
614 }
615 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
616 SSL_CIPHER_get_max_version(cipher) < min_version) {
617 continue;
618 }
619 any_enabled = 1;
620 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
621 return 0;
622 }
623 }
624
625 /* If all ciphers were disabled, return the error to the caller. */
626 if (!any_enabled && max_version < TLS1_3_VERSION) {
627 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
628 return 0;
629 }
David Benjamine8d53502015-10-10 14:13:23 -0400630 }
631
632 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
633 * added. */
David Benjaminf04c2e92016-12-06 13:35:25 -0500634 if (max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400635 !ssl->s3->initial_handshake_complete) {
636 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
637 return 0;
638 }
David Benjamine8d53502015-10-10 14:13:23 -0400639 }
640
David Benjamine0ff7672016-09-19 18:40:03 -0400641 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200642 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
643 return 0;
644 }
David Benjamine8d53502015-10-10 14:13:23 -0400645 }
646
647 return CBB_flush(out);
648}
649
David Benjamin8c880a22016-12-03 02:20:34 -0500650int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
651 SSL *const ssl = hs->ssl;
David Benjamine0ff7672016-09-19 18:40:03 -0400652 uint16_t min_version, max_version;
653 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400654 return 0;
655 }
656
Steven Valdeza833c352016-11-01 13:39:36 -0400657 CBB cbb, body;
658 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
659 goto err;
660 }
661
Steven Valdez5440fe02016-07-18 12:40:30 -0400662 /* Renegotiations do not participate in session resumption. */
663 int has_session = ssl->session != NULL &&
664 !ssl->s3->initial_handshake_complete;
665
666 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500667 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400668 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
669 !CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400670 (has_session &&
671 !CBB_add_bytes(&child, ssl->session->session_id,
672 ssl->session->session_id_length))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400673 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400674 }
675
David Benjamince079fd2016-08-02 16:22:34 -0400676 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400677 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400678 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400679 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400680 }
681 }
682
683 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400684 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdeza833c352016-11-01 13:39:36 -0400685 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
686 !CBB_add_u8(&body, 1 /* one compression method */) ||
687 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500688 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400689 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400690 }
691
Steven Valdeza833c352016-11-01 13:39:36 -0400692 uint8_t *msg = NULL;
693 size_t len;
694 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
695 goto err;
696 }
697
698 /* Now that the length prefixes have been computed, fill in the placeholder
699 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500700 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500701 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400702 OPENSSL_free(msg);
703 goto err;
704 }
705
David Benjamindaf207a2017-01-03 18:37:41 -0500706 return ssl->method->add_message(ssl, msg, len);
Steven Valdeza833c352016-11-01 13:39:36 -0400707
708 err:
709 CBB_cleanup(&cbb);
710 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400711}
712
David Benjaminc3c88822016-11-14 10:32:04 +0900713static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
714 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400715 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
716 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
Steven Valdez908ac192017-01-12 13:17:07 -0500717 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500718 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
719 return -1;
720 }
721
David Benjaminb6a0a512016-06-21 10:33:21 -0400722 uint16_t min_version, max_version;
723 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400724 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400725 }
726
David Benjaminf04c2e92016-12-06 13:35:25 -0500727 uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
David Benjamincb0c29f2016-12-12 17:00:50 -0500728 assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
David Benjamine8d53502015-10-10 14:13:23 -0400729 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500730 ssl->version = max_wire_version;
731 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400732
David Benjaminf04c2e92016-12-06 13:35:25 -0500733 /* Always advertise the ClientHello version from the original maximum version,
734 * even on renegotiation. The static RSA key exchange uses this field, and
735 * some servers fail when it changes across handshakes. */
736 hs->client_version = max_wire_version;
737 if (max_version >= TLS1_3_VERSION) {
738 hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
David Benjamine8d53502015-10-10 14:13:23 -0400739 }
740
David Benjamine29ea162016-03-23 16:10:44 -0400741 /* If the configured session has expired or was created at a disabled
742 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400743 if (ssl->session != NULL) {
David Benjamin2dc02042016-09-19 19:57:37 -0400744 uint16_t session_version;
Adam Langley364f7a62016-12-12 10:51:00 -0800745 if (ssl->session->is_server ||
746 !ssl->method->version_from_wire(&session_version,
David Benjamin2dc02042016-09-19 19:57:37 -0400747 ssl->session->ssl_version) ||
748 (session_version < TLS1_3_VERSION &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400749 ssl->session->session_id_length == 0) ||
750 ssl->session->not_resumable ||
751 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminb6a0a512016-06-21 10:33:21 -0400752 session_version < min_version || session_version > max_version) {
David Benjamin4d0be242016-09-01 01:10:07 -0400753 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400754 }
David Benjamine8d53502015-10-10 14:13:23 -0400755 }
756
757 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
758 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400759 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400760 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400761 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400762 }
763
David Benjamin8c880a22016-12-03 02:20:34 -0500764 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400765 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400766 }
767
David Benjamin16315f72017-01-12 20:02:05 -0500768 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800769}
Adam Langley95c29f32014-06-20 12:00:00 -0700770
David Benjaminc3c88822016-11-14 10:32:04 +0900771static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
772 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -0700773 int al;
David Benjaminaa7734b2016-06-07 16:40:46 -0400774 CBS hello_verify_request, cookie;
775 uint16_t server_version;
776
David Benjaminf71036e2017-01-21 14:49:39 -0500777 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700778 if (ret <= 0) {
779 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400780 }
781
782 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
783 ssl->d1->send_cookie = 0;
784 ssl->s3->tmp.reuse_message = 1;
785 return 1;
786 }
787
David Benjaminf71036e2017-01-21 14:49:39 -0500788 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400789 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
790 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
791 CBS_len(&hello_verify_request) != 0) {
792 al = SSL_AD_DECODE_ERROR;
793 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
794 goto f_err;
795 }
796
797 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
798 al = SSL_AD_ILLEGAL_PARAMETER;
799 goto f_err;
800 }
801
David Benjamin17cf2cb2016-12-13 01:07:13 -0500802 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400803 ssl->d1->cookie_len = CBS_len(&cookie);
804
805 ssl->d1->send_cookie = 1;
806 return 1;
807
808f_err:
809 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
810 return -1;
811}
812
David Benjaminc3c88822016-11-14 10:32:04 +0900813static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
814 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800815 CBS server_hello, server_random, session_id;
David Benjamin2dc02042016-09-19 19:57:37 -0400816 uint16_t server_wire_version, cipher_suite;
Adam Langley1bea1732014-12-17 19:06:57 -0800817 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700818
David Benjaminf71036e2017-01-21 14:49:39 -0500819 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700820 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500821 uint32_t err = ERR_peek_error();
822 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
823 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
824 /* Add a dedicated error code to the queue for a handshake_failure alert
825 * in response to ClientHello. This matches NSS's client behavior and
826 * gives a better error on a (probable) failure to negotiate initial
827 * parameters. Note: this error code comes after the original one.
828 *
829 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400830 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500831 }
David Benjamin09eb6552016-07-08 14:32:11 -0700832 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Steven Valdez143e8b32016-07-11 13:19:03 -0400835 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
836 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
837 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
838 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
839 return -1;
840 }
841
David Benjamin09eb6552016-07-08 14:32:11 -0700842 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
David Benjamina03d95d2014-07-12 19:49:07 -0400843
Steven Valdez143e8b32016-07-11 13:19:03 -0400844 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400845 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400846 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
847 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800848 }
David Benjamina03d95d2014-07-12 19:49:07 -0400849
David Benjamin2dc02042016-09-19 19:57:37 -0400850 uint16_t min_version, max_version, server_version;
David Benjamine0ff7672016-09-19 18:40:03 -0400851 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
David Benjamin2dc02042016-09-19 19:57:37 -0400852 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjamin1f61f0d2016-07-10 12:20:35 -0400853 server_version < min_version || server_version > max_version) {
854 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400855 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
856 return -1;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400857 }
858
David Benjamin0d56f882015-12-19 17:05:56 -0500859 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
860 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400861 ssl->version = server_wire_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500862 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800863 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500864 ssl->s3->have_version = 1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400865 } else if (server_wire_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400866 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400867 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
868 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800869 }
Adam Langley95c29f32014-06-20 12:00:00 -0700870
Steven Valdez143e8b32016-07-11 13:19:03 -0400871 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500872 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900873 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400874 return 1;
875 }
876
Steven Valdez2d850622017-01-11 11:34:52 -0500877 if (hs->early_data_offered) {
878 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400879 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
880 return -1;
Steven Valdez2d850622017-01-11 11:34:52 -0500881 }
882
David Benjamin6e4fc332016-11-17 16:43:08 +0900883 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400884
David Benjamin276b7e82017-01-21 14:13:39 -0500885 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400886 return -1;
887 }
888
889 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
890 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
891 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
892 !CBS_get_u16(&server_hello, &cipher_suite) ||
893 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400894 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400895 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
896 return -1;
Steven Valdez143e8b32016-07-11 13:19:03 -0400897 }
898
Adam Langley1bea1732014-12-17 19:06:57 -0800899 /* Copy over the server random. */
David Benjamind3bca042017-04-08 11:05:04 -0400900 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
901 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700902
David Benjamin55108632016-08-11 22:01:18 -0400903 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
904 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400905
David Benjamin0d56f882015-12-19 17:05:56 -0500906 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400907 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500908 CBS_mem_equal(&session_id, ssl->session->session_id,
909 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400910 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800911 } else {
912 /* The session wasn't resumed. Create a fresh SSL_SESSION to
913 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400914 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900915 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400916 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
917 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800918 }
919 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500920 hs->new_session->session_id_length = CBS_len(&session_id);
921 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
922 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800923 }
David Benjamina03d95d2014-07-12 19:49:07 -0400924
David Benjamind2cb1c12016-11-02 17:49:09 -0400925 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800926 if (c == NULL) {
927 /* unknown cipher */
David Benjamin3570d732015-06-29 00:28:17 -0400928 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400929 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
930 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800931 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400932
933 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500934 uint32_t mask_a, mask_k;
935 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
936 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700937 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400938 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
939 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
David Benjamin3570d732015-06-29 00:28:17 -0400940 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400941 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
942 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800943 }
Adam Langley95c29f32014-06-20 12:00:00 -0700944
Steven Valdez87eab492016-06-27 16:34:59 -0400945 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500946 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400947 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400948 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
949 return -1;
David Benjaminece3de92015-03-16 18:02:20 -0400950 }
Steven Valdez803c77a2016-09-06 14:13:43 -0400951 if (ssl->session->cipher != c) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400952 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400953 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
954 return -1;
Steven Valdez803c77a2016-09-06 14:13:43 -0400955 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400956 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
957 /* This is actually a client application bug. */
Steven Valdez4aa154e2016-07-29 14:32:55 -0400958 OPENSSL_PUT_ERROR(SSL,
959 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400960 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
961 return -1;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400962 }
David Benjamin0623bce2015-12-25 15:40:14 -0500963 } else {
David Benjamin45738dd2017-02-09 20:01:26 -0500964 hs->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800965 }
David Benjamin45738dd2017-02-09 20:01:26 -0500966 hs->new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500967
David Benjaminf71036e2017-01-21 14:49:39 -0500968 /* Now that the cipher is known, initialize the handshake hash and hash the
969 * ServerHello. */
Steven Valdez908ac192017-01-12 13:17:07 -0500970 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
971 c->algorithm_prf) ||
972 !ssl_hash_current_message(hs)) {
David Benjamind3bca042017-04-08 11:05:04 -0400973 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
974 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -0400975 }
976
Steven Valdez2b8415e2016-06-30 13:27:23 -0400977 /* If doing a full handshake, the server may request a client certificate
978 * which requires hashing the handshake transcript. Otherwise, the handshake
979 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000980 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500981 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500982 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -0800983 }
David Benjamina03d95d2014-07-12 19:49:07 -0400984
Adam Langley1bea1732014-12-17 19:06:57 -0800985 /* Only the NULL compression algorithm is supported. */
986 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400987 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -0400988 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
989 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langley1bea1732014-12-17 19:06:57 -0800992 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -0500993 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400994 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400995 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800996 }
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langley1bea1732014-12-17 19:06:57 -0800998 /* There should be nothing left over in the record. */
999 if (CBS_len(&server_hello) != 0) {
1000 /* wrong packet length */
David Benjamin639846e2016-09-09 11:41:18 -04001001 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -04001002 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1003 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001004 }
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Steven Valdez87eab492016-06-27 16:34:59 -04001006 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -05001007 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -05001008 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001009 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001010 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001011 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001012 }
David Benjamind3bca042017-04-08 11:05:04 -04001013 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1014 return -1;
Adam Langleyba5934b2015-06-02 10:50:35 -07001015 }
1016
Adam Langley1bea1732014-12-17 19:06:57 -08001017 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001018}
Adam Langley95c29f32014-06-20 12:00:00 -07001019
David Benjaminc3c88822016-11-14 10:32:04 +09001020static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1021 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001022 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001023 if (ret <= 0) {
1024 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001025 }
Adam Langley95c29f32014-06-20 12:00:00 -07001026
David Benjaminf71036e2017-01-21 14:49:39 -05001027 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001028 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001029 return -1;
1030 }
1031
David Benjamin5c900c82016-07-13 23:03:26 -04001032 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001033 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001034
Adam Langleyc68e5b92017-02-08 13:33:15 -08001035 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001036 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001037 EVP_PKEY_free(hs->peer_pubkey);
1038 hs->peer_pubkey = NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001039 hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
1040 &cbs, ssl->ctx->pool);
1041 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001042 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001043 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
David Benjamin45738dd2017-02-09 20:01:26 -05001046 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
Adam Langley68e71242016-12-12 11:06:16 -08001047 CBS_len(&cbs) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001048 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin3570d732015-06-29 00:28:17 -04001049 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001050 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001051 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001052 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001053
Adam Langley05672202016-12-13 12:05:49 -08001054 if (!ssl_check_leaf_certificate(
David Benjamin45738dd2017-02-09 20:01:26 -05001055 hs, hs->peer_pubkey,
1056 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001057 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001058 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001059 }
1060
David Benjamina58baaf2017-02-28 20:54:28 -05001061 /* Disallow the server certificate from changing during a renegotiation. See
1062 * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
1063 * so this check is sufficient. */
1064 if (ssl->s3->established_session != NULL) {
1065 if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
1066 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
1067 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1068 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1069 return -1;
1070 }
1071
1072 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
1073 const CRYPTO_BUFFER *old_cert =
1074 sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
1075 const CRYPTO_BUFFER *new_cert =
1076 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
1077 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
1078 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
1079 CRYPTO_BUFFER_data(new_cert),
1080 CRYPTO_BUFFER_len(old_cert)) != 0) {
1081 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1082 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1083 return -1;
1084 }
1085 }
1086 }
1087
David Benjamin5c900c82016-07-13 23:03:26 -04001088 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001089}
1090
David Benjaminc3c88822016-11-14 10:32:04 +09001091static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1092 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001093 int al;
David Benjamin9f1dc822016-06-07 17:03:46 -04001094 CBS certificate_status, ocsp_response;
1095 uint8_t status_type;
1096
David Benjaminf71036e2017-01-21 14:49:39 -05001097 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001098 if (ret <= 0) {
1099 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001100 }
1101
1102 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1103 /* A server may send status_request in ServerHello and then change
1104 * its mind about sending CertificateStatus. */
1105 ssl->s3->tmp.reuse_message = 1;
1106 return 1;
1107 }
1108
Steven Valdez908ac192017-01-12 13:17:07 -05001109 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001110 return -1;
1111 }
1112
David Benjamin09eb6552016-07-08 14:32:11 -07001113 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001114 if (!CBS_get_u8(&certificate_status, &status_type) ||
1115 status_type != TLSEXT_STATUSTYPE_ocsp ||
1116 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1117 CBS_len(&ocsp_response) == 0 ||
1118 CBS_len(&certificate_status) != 0) {
1119 al = SSL_AD_DECODE_ERROR;
1120 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1121 goto f_err;
1122 }
1123
David Benjamin45738dd2017-02-09 20:01:26 -05001124 if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
1125 &hs->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001126 al = SSL_AD_INTERNAL_ERROR;
1127 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1128 goto f_err;
1129 }
1130 return 1;
1131
1132f_err:
1133 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1134 return -1;
1135}
1136
David Benjaminc3c88822016-11-14 10:32:04 +09001137static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1138 SSL *const ssl = hs->ssl;
Adam Langley2a3b3432017-02-10 13:56:48 -08001139 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
David Benjamine455e512016-08-01 20:11:13 -04001140 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001141 }
1142
Adam Langley9498e742016-07-18 10:17:16 -07001143 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001144}
1145
David Benjaminc3c88822016-11-14 10:32:04 +09001146static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1147 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001148 int al;
Adam Langley1bea1732014-12-17 19:06:57 -08001149 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001150 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001151
David Benjaminf71036e2017-01-21 14:49:39 -05001152 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001153 if (ret <= 0) {
1154 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001155 }
1156
David Benjamin0d56f882015-12-19 17:05:56 -05001157 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001158 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001159 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001160 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001161 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001162 return -1;
1163 }
1164
David Benjamin0d56f882015-12-19 17:05:56 -05001165 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001166 return 1;
1167 }
1168
Steven Valdez908ac192017-01-12 13:17:07 -05001169 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001170 return -1;
1171 }
1172
Adam Langley1bea1732014-12-17 19:06:57 -08001173 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001174 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001175 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001176 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001177
David Benjamin45738dd2017-02-09 20:01:26 -05001178 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1179 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001180
1181 if (alg_a & SSL_aPSK) {
1182 CBS psk_identity_hint;
1183
1184 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1185 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1186 &psk_identity_hint)) {
1187 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001188 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001189 goto f_err;
1190 }
1191
1192 /* Store PSK identity hint for later use, hint is used in
1193 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1194 * identity hint can be as long as the maximum length of a PSK identity.
1195 * Also do not allow NULL characters; identities are saved as C strings.
1196 *
1197 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1198 * a specific identity. */
1199 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1200 CBS_contains_zero_byte(&psk_identity_hint)) {
1201 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001202 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001203 goto f_err;
1204 }
1205
David Benjamin78679342016-09-16 19:42:05 -04001206 /* Save non-empty identity hints as a C string. Empty identity hints we
1207 * treat as missing. Plain PSK makes it possible to send either no hint
1208 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1209 * empty hint. Having different capabilities is odd, so we interpret empty
1210 * and missing as identical. */
1211 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001212 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001213 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001214 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001215 goto f_err;
1216 }
1217 }
1218
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001219 if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001220 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001221 uint8_t group_type;
1222 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001223 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001224 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1225 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001226 !CBS_get_u16(&server_key_exchange, &group_id) ||
1227 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001228 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001230 goto f_err;
1231 }
David Benjamin45738dd2017-02-09 20:01:26 -05001232 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001233
Steven Valdezce902a92016-05-17 11:47:53 -04001234 /* Ensure the group is consistent with preferences. */
1235 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001236 al = SSL_AD_ILLEGAL_PARAMETER;
1237 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001238 goto f_err;
1239 }
David Benjamin4298d772015-12-19 00:18:25 -05001240
Matt Braithwaite053931e2016-05-25 12:06:05 -07001241 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001242 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1243 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001244 goto err;
1245 }
Adam Langley1bea1732014-12-17 19:06:57 -08001246 } else if (!(alg_k & SSL_kPSK)) {
1247 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001248 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001249 goto f_err;
1250 }
1251
1252 /* At this point, |server_key_exchange| contains the signature, if any, while
1253 * |server_key_exchange_orig| contains the entire message. From that, derive
1254 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001255 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001256 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1257 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1258
David Benjaminf4958e72015-06-07 11:00:53 -04001259 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001260 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001261 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001262 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001263 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001264 al = SSL_AD_DECODE_ERROR;
1265 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001266 goto f_err;
1267 }
David Benjamin887c3002016-07-08 16:15:32 -07001268 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001269 goto f_err;
1270 }
David Benjamin45738dd2017-02-09 20:01:26 -05001271 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001272 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1273 hs->peer_pubkey)) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001274 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1275 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1276 goto f_err;
Adam Langley1bea1732014-12-17 19:06:57 -08001277 }
1278
1279 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001280 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001281 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1282 CBS_len(&server_key_exchange) != 0) {
1283 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001284 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001285 goto f_err;
1286 }
1287
Steven Valdez2b8415e2016-06-30 13:27:23 -04001288 CBB transcript;
1289 uint8_t *transcript_data;
1290 size_t transcript_len;
1291 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1292 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1293 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1294 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1295 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1296 CBB_cleanup(&transcript);
1297 al = SSL_AD_INTERNAL_ERROR;
1298 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001299 goto f_err;
1300 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001301
1302 int sig_ok = ssl_public_key_verify(
1303 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001304 hs->peer_pubkey, transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001305 OPENSSL_free(transcript_data);
1306
David Benjaminbf82aed2016-03-01 22:57:40 -05001307#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1308 sig_ok = 1;
1309 ERR_clear_error();
1310#endif
1311 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001312 /* bad signature */
1313 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001314 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001315 goto f_err;
1316 }
1317 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001318 /* PSK ciphers are the only supported certificate-less ciphers. */
1319 assert(alg_a == SSL_aPSK);
1320
Adam Langley1bea1732014-12-17 19:06:57 -08001321 if (CBS_len(&server_key_exchange) > 0) {
1322 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001323 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001324 goto f_err;
1325 }
1326 }
Adam Langley1bea1732014-12-17 19:06:57 -08001327 return 1;
1328
Adam Langley95c29f32014-06-20 12:00:00 -07001329f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001330 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001331err:
Adam Langley1bea1732014-12-17 19:06:57 -08001332 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001333 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001334 return -1;
1335}
Adam Langley95c29f32014-06-20 12:00:00 -07001336
David Benjaminc3c88822016-11-14 10:32:04 +09001337static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1338 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001339 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001340 if (msg_ret <= 0) {
1341 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001342 }
David Benjamin688d8df2014-11-02 23:06:42 -05001343
David Benjamin3dcec452016-06-07 17:13:28 -04001344 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001345 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001346 /* If we get here we don't need the handshake buffer as we won't be doing
1347 * client auth. */
Steven Valdez908ac192017-01-12 13:17:07 -05001348 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001349 return 1;
1350 }
David Benjamined439582014-07-14 19:13:02 -04001351
David Benjaminf71036e2017-01-21 14:49:39 -05001352 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001353 !ssl_hash_current_message(hs)) {
David Benjamine0332e82016-07-13 22:40:36 -04001354 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001355 }
Adam Langley95c29f32014-06-20 12:00:00 -07001356
David Benjamina5177cb2016-03-11 19:38:17 -05001357 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001358 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001359
David Benjamine0332e82016-07-13 22:40:36 -04001360 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001361 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001362 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001363 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001364 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001365 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001366 }
David Benjamined439582014-07-14 19:13:02 -04001367
David Benjaminc3c88822016-11-14 10:32:04 +09001368 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1369 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001370 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001371 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001372 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001373
David Benjamina1e9cab2015-12-30 00:08:49 -05001374 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001375 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001376 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001377 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001378 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001379 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001380 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001381 }
Adam Langley1bea1732014-12-17 19:06:57 -08001382 }
David Benjamined439582014-07-14 19:13:02 -04001383
Adam Langleyc68e5b92017-02-08 13:33:15 -08001384 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Langley34b4c822017-02-02 10:57:17 -08001385 STACK_OF(CRYPTO_BUFFER) *ca_names =
1386 ssl_parse_client_CA_list(ssl, &alert, &cbs);
1387 if (ca_names == NULL) {
David Benjamine0332e82016-07-13 22:40:36 -04001388 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1389 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001390 }
Adam Langley95c29f32014-06-20 12:00:00 -07001391
David Benjamin639846e2016-09-09 11:41:18 -04001392 if (CBS_len(&cbs) != 0) {
1393 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley34b4c822017-02-02 10:57:17 -08001394 sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
David Benjamin639846e2016-09-09 11:41:18 -04001395 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1396 return -1;
1397 }
1398
David Benjaminc3c88822016-11-14 10:32:04 +09001399 hs->cert_request = 1;
Adam Langley34b4c822017-02-02 10:57:17 -08001400 sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
1401 hs->ca_names = ca_names;
1402 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamine0332e82016-07-13 22:40:36 -04001403 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001404}
Adam Langley95c29f32014-06-20 12:00:00 -07001405
David Benjaminc3c88822016-11-14 10:32:04 +09001406static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1407 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001408 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001409 if (ret <= 0) {
1410 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001411 }
David Benjamin46062682014-07-14 19:14:32 -04001412
David Benjaminf71036e2017-01-21 14:49:39 -05001413 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001414 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001415 return -1;
1416 }
1417
David Benjamin09eb6552016-07-08 14:32:11 -07001418 /* ServerHelloDone is empty. */
1419 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001420 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001421 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001422 return -1;
1423 }
David Benjamin46062682014-07-14 19:14:32 -04001424
Adam Langley1bea1732014-12-17 19:06:57 -08001425 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001426}
Adam Langley1258b6a2014-06-20 12:00:00 -07001427
David Benjaminc3c88822016-11-14 10:32:04 +09001428static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1429 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001430 /* Call cert_cb to update the certificate. */
1431 if (ssl->cert->cert_cb) {
1432 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1433 if (ret < 0) {
1434 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001435 return -1;
1436 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001437 if (ret == 0) {
1438 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1439 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001440 return -1;
1441 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001442 }
1443
David Benjamin5edfc8c2016-12-10 15:46:58 -05001444 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001445 /* Without a client certificate, the handshake buffer may be released. */
Steven Valdez908ac192017-01-12 13:17:07 -05001446 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin5edfc8c2016-12-10 15:46:58 -05001447
David Benjamindaf207a2017-01-03 18:37:41 -05001448 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001449 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001450 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1451 SSL_AD_NO_CERTIFICATE)) {
1452 return -1;
1453 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001454 return 1;
1455 }
1456 }
1457
David Benjamina232a712017-03-30 15:51:53 -05001458 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001459 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001460 return -1;
1461 }
David Benjamin16315f72017-01-12 20:02:05 -05001462 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001463}
1464
Brian Smith274341d2015-10-08 17:10:15 -10001465OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1466 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001467
David Benjaminc3c88822016-11-14 10:32:04 +09001468static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1469 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -08001470 uint8_t *pms = NULL;
1471 size_t pms_len = 0;
David Benjamin75836432016-06-17 18:48:29 -04001472 CBB cbb, body;
1473 if (!ssl->method->init_message(ssl, &cbb, &body,
1474 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001475 goto err;
1476 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001477
David Benjamin45738dd2017-02-09 20:01:26 -05001478 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1479 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001480
David Benjaminae0eaaa2015-12-17 02:01:25 -05001481 /* If using a PSK key exchange, prepare the pre-shared key. */
1482 unsigned psk_len = 0;
1483 uint8_t psk[PSK_MAX_PSK_LEN];
1484 if (alg_a & SSL_aPSK) {
1485 if (ssl->psk_client_callback == NULL) {
1486 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1487 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001488 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001489
David Benjaminae0eaaa2015-12-17 02:01:25 -05001490 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001491 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001492 psk_len =
1493 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1494 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001495 if (psk_len == 0) {
1496 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1497 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1498 goto err;
1499 }
1500 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001501
David Benjamin45738dd2017-02-09 20:01:26 -05001502 OPENSSL_free(hs->new_session->psk_identity);
1503 hs->new_session->psk_identity = BUF_strdup(identity);
1504 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001505 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1506 goto err;
1507 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001508
David Benjaminae0eaaa2015-12-17 02:01:25 -05001509 /* Write out psk_identity. */
1510 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001511 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001512 !CBB_add_bytes(&child, (const uint8_t *)identity,
1513 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001514 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001515 goto err;
1516 }
1517 }
David Benjamin758d1272015-11-20 17:47:25 -05001518
David Benjaminae0eaaa2015-12-17 02:01:25 -05001519 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1520 if (alg_k & SSL_kRSA) {
1521 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1522 pms = OPENSSL_malloc(pms_len);
1523 if (pms == NULL) {
1524 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1525 goto err;
1526 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001527
Adam Langleyd5157222016-12-12 11:37:43 -08001528 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001529 if (rsa == NULL) {
1530 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001531 goto err;
1532 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001533
David Benjaminf04c2e92016-12-06 13:35:25 -05001534 pms[0] = hs->client_version >> 8;
1535 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001536 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1537 goto err;
1538 }
1539
David Benjamin75836432016-06-17 18:48:29 -04001540 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001541 size_t enc_pms_len;
1542 /* In TLS, there is a length prefix. */
1543 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001544 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001545 goto err;
1546 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001547 enc_pms = &child;
1548 }
Adam Langley1bea1732014-12-17 19:06:57 -08001549
David Benjaminae0eaaa2015-12-17 02:01:25 -05001550 uint8_t *ptr;
1551 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1552 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1553 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001554 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001555 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001556 goto err;
1557 }
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001558 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001559 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001560 CBB child;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001561 if (!CBB_add_u8_length_prefixed(&body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001562 goto err;
1563 }
1564
David Benjamin4298d772015-12-19 00:18:25 -05001565 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001566 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001567 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1568 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001569 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1570 goto err;
1571 }
David Benjamin75836432016-06-17 18:48:29 -04001572 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001573 goto err;
1574 }
David Benjamin4298d772015-12-19 00:18:25 -05001575
1576 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001577 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1578 OPENSSL_free(hs->peer_key);
1579 hs->peer_key = NULL;
1580 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001581 } else if (alg_k & SSL_kPSK) {
1582 /* For plain PSK, other_secret is a block of 0s with the same length as
1583 * the pre-shared key. */
1584 pms_len = psk_len;
1585 pms = OPENSSL_malloc(pms_len);
1586 if (pms == NULL) {
1587 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1588 goto err;
1589 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001590 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001591 } else {
1592 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1593 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1594 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001595 }
1596
David Benjaminae0eaaa2015-12-17 02:01:25 -05001597 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1598 * key. */
1599 if (alg_a & SSL_aPSK) {
1600 CBB pms_cbb, child;
1601 uint8_t *new_pms;
1602 size_t new_pms_len;
1603
1604 CBB_zero(&pms_cbb);
1605 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1606 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1607 !CBB_add_bytes(&child, pms, pms_len) ||
1608 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1609 !CBB_add_bytes(&child, psk, psk_len) ||
1610 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1611 CBB_cleanup(&pms_cbb);
1612 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1613 goto err;
1614 }
1615 OPENSSL_cleanse(pms, pms_len);
1616 OPENSSL_free(pms);
1617 pms = new_pms;
1618 pms_len = new_pms_len;
1619 }
1620
1621 /* The message must be added to the finished hash before calculating the
1622 * master secret. */
David Benjamindaf207a2017-01-03 18:37:41 -05001623 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001624 goto err;
1625 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001626
David Benjamin45738dd2017-02-09 20:01:26 -05001627 hs->new_session->master_key_length = tls1_generate_master_secret(
1628 hs, hs->new_session->master_key, pms, pms_len);
1629 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001630 goto err;
1631 }
David Benjaminfc02b592017-02-17 16:26:01 -05001632 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001633 OPENSSL_cleanse(pms, pms_len);
1634 OPENSSL_free(pms);
1635
David Benjamin16315f72017-01-12 20:02:05 -05001636 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001637
1638err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001639 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001640 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001641 OPENSSL_cleanse(pms, pms_len);
1642 OPENSSL_free(pms);
1643 }
1644 return -1;
1645}
Adam Langley1258b6a2014-06-20 12:00:00 -07001646
David Benjaminc3c88822016-11-14 10:32:04 +09001647static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1648 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001649 assert(ssl_has_private_key(ssl));
1650
David Benjamin75836432016-06-17 18:48:29 -04001651 CBB cbb, body, child;
1652 if (!ssl->method->init_message(ssl, &cbb, &body,
1653 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjaminfc825122015-12-18 01:57:43 -05001654 goto err;
1655 }
1656
David Benjaminea9a0d52016-07-08 15:52:59 -07001657 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001658 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001659 goto err;
1660 }
David Benjamin429fdc02016-06-17 17:33:30 -04001661 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001662 /* Write out the digest type in TLS 1.2. */
1663 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001664 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1665 goto err;
1666 }
1667 }
David Benjaminfc825122015-12-18 01:57:43 -05001668
David Benjamin429fdc02016-06-17 17:33:30 -04001669 /* Set aside space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001670 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjamin429fdc02016-06-17 17:33:30 -04001671 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001672 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001673 !CBB_reserve(&child, &ptr, max_sig_len)) {
1674 goto err;
1675 }
1676
Steven Valdez2b8415e2016-06-30 13:27:23 -04001677 size_t sig_len = max_sig_len;
David Benjaminfc825122015-12-18 01:57:43 -05001678 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001679 if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001680 /* The SSL3 construction for CertificateVerify does not decompose into a
1681 * single final digest and signature, and must be special-cased. */
1682 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1683 if (ssl->cert->key_method != NULL) {
1684 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1685 goto err;
1686 }
1687
1688 uint8_t digest[EVP_MAX_MD_SIZE];
1689 size_t digest_len;
David Benjamin45738dd2017-02-09 20:01:26 -05001690 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
1691 &digest_len, hs->new_session,
1692 signature_algorithm)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001693 goto err;
1694 }
1695
1696 sign_result = ssl_private_key_success;
1697
1698 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1699 if (pctx == NULL ||
1700 !EVP_PKEY_sign_init(pctx) ||
1701 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1702 EVP_PKEY_CTX_free(pctx);
1703 sign_result = ssl_private_key_failure;
1704 goto err;
1705 }
1706 EVP_PKEY_CTX_free(pctx);
1707 } else {
1708 sign_result = ssl_private_key_sign(
1709 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001710 (const uint8_t *)hs->transcript.buffer->data,
1711 hs->transcript.buffer->length);
David Benjaminfc825122015-12-18 01:57:43 -05001712 }
1713
1714 /* The handshake buffer is no longer necessary. */
Steven Valdez908ac192017-01-12 13:17:07 -05001715 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjaminfc825122015-12-18 01:57:43 -05001716 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001717 assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjamind3440b42016-07-14 14:52:41 -04001718 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
David Benjaminfc825122015-12-18 01:57:43 -05001719 }
1720
1721 switch (sign_result) {
1722 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001723 break;
1724 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001725 goto err;
1726 case ssl_private_key_retry:
1727 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001728 hs->state = SSL3_ST_CW_CERT_VRFY_B;
David Benjaminfc825122015-12-18 01:57:43 -05001729 goto err;
1730 }
1731
David Benjaminfc825122015-12-18 01:57:43 -05001732 if (!CBB_did_write(&child, sig_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001733 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminfc825122015-12-18 01:57:43 -05001734 goto err;
1735 }
1736
David Benjamin16315f72017-01-12 20:02:05 -05001737 return 1;
David Benjaminfc825122015-12-18 01:57:43 -05001738
1739err:
1740 CBB_cleanup(&cbb);
1741 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001742}
1743
David Benjaminc3c88822016-11-14 10:32:04 +09001744static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1745 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001746 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001747 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001748
David Benjamin75836432016-06-17 18:48:29 -04001749 CBB cbb, body, child;
1750 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1751 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001752 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1753 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001754 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001755 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001756 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001757 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1758 CBB_cleanup(&cbb);
1759 return -1;
1760 }
1761
David Benjamin16315f72017-01-12 20:02:05 -05001762 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001763}
1764
David Benjaminc3c88822016-11-14 10:32:04 +09001765static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1766 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001767 if (!ssl_do_channel_id_callback(ssl)) {
1768 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001769 }
1770
David Benjamin96e97b12015-10-10 16:30:22 -04001771 if (ssl->tlsext_channel_id_private == NULL) {
1772 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001773 return -1;
1774 }
Adam Langley1bea1732014-12-17 19:06:57 -08001775
Nick Harper60a85cb2016-09-23 16:25:11 -07001776 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001777 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001778 !tls1_write_channel_id(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001779 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001780 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001781 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001782 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001783 }
1784
David Benjamin16315f72017-01-12 20:02:05 -05001785 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001786}
1787
David Benjaminc3c88822016-11-14 10:32:04 +09001788static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1789 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001790 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001791 if (ret <= 0) {
1792 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001793 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001794
David Benjaminf71036e2017-01-21 14:49:39 -05001795 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001796 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001797 return -1;
1798 }
1799
David Benjamin9f1dc822016-06-07 17:03:46 -04001800 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001801 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001802 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001803 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001804 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1805 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001806 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001807 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001808 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001809 }
1810
David Benjamin9f1dc822016-06-07 17:03:46 -04001811 if (CBS_len(&ticket) == 0) {
1812 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001813 * negotiating the extension. The value of |ticket_expected| is checked in
1814 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001815 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001816 return 1;
1817 }
1818
Steven Valdez87eab492016-06-27 16:34:59 -04001819 int session_renewed = ssl->session != NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001820 SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001821 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001822 /* The server is sending a new ticket for an existing session. Sessions are
1823 * immutable once established, so duplicate all but the ticket of the
1824 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001825 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001826 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001827 /* This should never happen. */
1828 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1829 goto err;
1830 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001831 }
1832
David Benjamin123db572016-11-03 16:59:25 -04001833 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001834 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001835
Steven Valdez87eab492016-06-27 16:34:59 -04001836 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001837 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1838 goto err;
1839 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001840 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001841
1842 /* Generate a session ID for this session based on the session ticket. We use
1843 * the session ID mechanism for detecting ticket resumption. This also fits in
1844 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001845 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1846 session->session_id, &session->session_id_length,
1847 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001848 goto err;
1849 }
1850
Steven Valdez87eab492016-06-27 16:34:59 -04001851 if (session_renewed) {
1852 session->not_resumable = 0;
1853 SSL_SESSION_free(ssl->session);
1854 ssl->session = session;
1855 }
1856
David Benjamin454aa4c2016-03-10 08:43:27 -05001857 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001858
David Benjamin9f1dc822016-06-07 17:03:46 -04001859err:
Steven Valdez87eab492016-06-27 16:34:59 -04001860 if (session_renewed) {
1861 SSL_SESSION_free(session);
1862 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001863 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001864}