blob: e649680c25b14daa9ed1f17003d2e8103b16085a [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/dh.h>
160#include <openssl/ec_key.h>
161#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400162#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700164#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400165#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400166#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin17cf2cb2016-12-13 01:07:13 -0500168#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400169#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjaminc3c88822016-11-14 10:32:04 +0900172static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
173static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
174static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
175static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
176static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
177static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
178static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
179static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
180static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
181static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
182static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
183static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
184static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
185static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
186static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400187
David Benjamince8c9d22016-11-14 10:45:16 +0900188int ssl3_connect(SSL_HANDSHAKE *hs) {
189 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800190 int ret = -1;
David Benjaminbeb47022014-11-30 02:58:52 -0500191
David Benjamin0d56f882015-12-19 17:05:56 -0500192 assert(ssl->handshake_func == ssl3_connect);
193 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley1bea1732014-12-17 19:06:57 -0800195 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500196 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjamincb0c29f2016-12-12 17:00:50 -0500198 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400199 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400200 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500201 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800202 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley1bea1732014-12-17 19:06:57 -0800204 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900205 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800206 if (ret <= 0) {
207 goto end;
208 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400209
David Benjamince079fd2016-08-02 16:22:34 -0400210 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
Steven Valdez2d850622017-01-11 11:34:52 -0500211 if (hs->early_data_offered) {
212 if (!tls13_init_early_key_schedule(hs) ||
213 !tls13_advance_key_schedule(hs, ssl->session->master_key,
214 ssl->session->master_key_length) ||
215 !tls13_derive_early_secrets(hs) ||
216 !tls13_set_traffic_key(ssl, evp_aead_seal,
217 hs->early_traffic_secret,
218 hs->hash_len)) {
219 ret = -1;
220 goto end;
221 }
222 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500223 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400224 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500225 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400226 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500227 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800228 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700229
David Benjaminaa7734b2016-06-07 16:40:46 -0400230 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400231 assert(SSL_is_dtls(ssl));
David Benjaminc3c88822016-11-14 10:32:04 +0900232 ret = dtls1_get_hello_verify(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400233 if (ret <= 0) {
234 goto end;
235 }
236 if (ssl->d1->send_cookie) {
237 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500238 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400239 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500240 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400241 }
242 break;
243
Adam Langley1bea1732014-12-17 19:06:57 -0800244 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900245 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500246 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400247 break;
248 }
Adam Langley1bea1732014-12-17 19:06:57 -0800249 if (ret <= 0) {
250 goto end;
251 }
Adam Langley95c29f32014-06-20 12:00:00 -0700252
Steven Valdez87eab492016-06-27 16:34:59 -0400253 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500254 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800255 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500256 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800257 }
Adam Langley1bea1732014-12-17 19:06:57 -0800258 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400259
Adam Langley1bea1732014-12-17 19:06:57 -0800260 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500261 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900262 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800263 if (ret <= 0) {
264 goto end;
265 }
Adam Langley1bea1732014-12-17 19:06:57 -0800266 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500267 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800268 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamin9f1dc822016-06-07 17:03:46 -0400270 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900271 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900272 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400273 if (ret <= 0) {
274 goto end;
275 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400276 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500277 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400278 break;
279
Paul Lietar8f1c2682015-08-18 12:21:54 +0100280 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500281 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900282 ret = ssl3_verify_server_cert(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400283 if (ret <= 0) {
284 goto end;
285 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100286 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500287 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100288 break;
289
Adam Langley1bea1732014-12-17 19:06:57 -0800290 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900291 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800292 if (ret <= 0) {
293 goto end;
294 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500295 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800296 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley1bea1732014-12-17 19:06:57 -0800298 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500299 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900300 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400301 if (ret <= 0) {
302 goto end;
303 }
Adam Langley1bea1732014-12-17 19:06:57 -0800304 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500305 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800306 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley1bea1732014-12-17 19:06:57 -0800308 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900309 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800310 if (ret <= 0) {
311 goto end;
312 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400313 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500314 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800315 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langley1bea1732014-12-17 19:06:57 -0800317 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900318 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900319 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400320 if (ret <= 0) {
321 goto end;
322 }
Adam Langley1bea1732014-12-17 19:06:57 -0800323 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500324 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley1bea1732014-12-17 19:06:57 -0800327 case SSL3_ST_CW_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900328 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800329 if (ret <= 0) {
330 goto end;
331 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500332 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800333 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langley1bea1732014-12-17 19:06:57 -0800335 case SSL3_ST_CW_CERT_VRFY_A:
336 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamina660e7a2017-01-02 08:15:35 -0500337 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900338 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400339 if (ret <= 0) {
340 goto end;
341 }
Adam Langley1bea1732014-12-17 19:06:57 -0800342 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500343 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800344 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin352d0a92016-06-28 11:22:02 -0400346 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500347 if (!ssl->method->add_change_cipher_spec(ssl) ||
348 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800349 ret = -1;
350 goto end;
351 }
Adam Langley95c29f32014-06-20 12:00:00 -0700352
David Benjamindaf207a2017-01-03 18:37:41 -0500353 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800354 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700355
Adam Langley1bea1732014-12-17 19:06:57 -0800356 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900357 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900358 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400359 if (ret <= 0) {
360 goto end;
361 }
Adam Langley1bea1732014-12-17 19:06:57 -0800362 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500363 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800364 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langley1bea1732014-12-17 19:06:57 -0800366 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400367 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900368 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400369 if (ret <= 0) {
370 goto end;
371 }
Adam Langley1bea1732014-12-17 19:06:57 -0800372 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500373 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800374 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley1bea1732014-12-17 19:06:57 -0800376 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500377 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800378 if (ret <= 0) {
379 goto end;
380 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500381 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Steven Valdez87eab492016-06-27 16:34:59 -0400383 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500384 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800385 } else {
386 /* This is a non-resumption handshake. If it involves ChannelID, then
387 * record the handshake hashes at this point in the session so that
388 * any resumption of this session with ChannelID can sign those
389 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500390 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700391 if (ret <= 0) {
392 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800393 }
David Benjamin0d56f882015-12-19 17:05:56 -0500394 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
395 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500396 /* No False Start on renegotiation (would complicate the state
397 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500398 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500399 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800400 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500401 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800402 }
403 }
Adam Langley1bea1732014-12-17 19:06:57 -0800404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
David Benjamin9f1dc822016-06-07 17:03:46 -0400406 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500407 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900408 hs->in_false_start = 1;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500409 hs->can_early_write = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400410 ret = 1;
411 goto end;
412
Adam Langley1bea1732014-12-17 19:06:57 -0800413 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900414 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900415 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400416 if (ret <= 0) {
417 goto end;
418 }
Adam Langley1bea1732014-12-17 19:06:57 -0800419 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500420 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800421 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
Adam Langley1bea1732014-12-17 19:06:57 -0800423 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400424 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500425 if (ret <= 0) {
426 goto end;
427 }
428
David Benjamin67739722016-11-17 17:03:59 +0900429 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800430 ret = -1;
431 goto end;
432 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500433 hs->state = SSL3_ST_CR_FINISHED_A;
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_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900437 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800438 if (ret <= 0) {
439 goto end;
440 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400441 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Steven Valdez87eab492016-06-27 16:34:59 -0400443 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500444 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800445 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500446 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800447 }
Adam Langley1bea1732014-12-17 19:06:57 -0800448 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langley1bea1732014-12-17 19:06:57 -0800450 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500451 ret = ssl->method->flush_flight(ssl);
452 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800453 goto end;
454 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500455 hs->state = hs->next_state;
David Benjamin77458a42017-02-10 23:33:27 -0500456 if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400457 ssl->method->expect_flight(ssl);
458 }
Adam Langley1bea1732014-12-17 19:06:57 -0800459 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700460
Steven Valdez681eb6a2016-12-19 13:19:29 -0500461 case SSL_ST_TLS13: {
462 int early_return = 0;
463 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400464 if (ret <= 0) {
465 goto end;
466 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500467
468 if (early_return) {
469 ret = 1;
470 goto end;
471 }
472
David Benjamin77458a42017-02-10 23:33:27 -0500473 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400474 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500475 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400476
David Benjamin77458a42017-02-10 23:33:27 -0500477 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin4497e582016-07-27 17:51:49 -0400478 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700479
Steven Valdez87eab492016-06-27 16:34:59 -0400480 SSL_SESSION_free(ssl->s3->established_session);
481 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400482 SSL_SESSION_up_ref(ssl->session);
483 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400484 } else {
485 /* We make a copy of the session in order to maintain the immutability
486 * of the new established_session due to False Start. The caller may
487 * have taken a reference to the temporary session. */
488 ssl->s3->established_session =
David Benjamin45738dd2017-02-09 20:01:26 -0500489 SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400490 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400491 ret = -1;
492 goto end;
493 }
494 ssl->s3->established_session->not_resumable = 0;
495
David Benjamin45738dd2017-02-09 20:01:26 -0500496 SSL_SESSION_free(hs->new_session);
497 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400498 }
499
David Benjamin77458a42017-02-10 23:33:27 -0500500 hs->state = SSL_ST_OK;
501 break;
502
503 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500504 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500505 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400506 if (is_initial_handshake) {
507 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900508 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400509 }
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langley1bea1732014-12-17 19:06:57 -0800511 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400512 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800513 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500514 }
Steven Valdez87eab492016-06-27 16:34:59 -0400515
Adam Langley1bea1732014-12-17 19:06:57 -0800516 default:
David Benjamin3570d732015-06-29 00:28:17 -0400517 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800518 ret = -1;
519 goto end;
520 }
521
David Benjamin16b1b1d2017-02-10 23:41:14 -0500522 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400523 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800524 }
Adam Langley1bea1732014-12-17 19:06:57 -0800525 }
526
Adam Langley95c29f32014-06-20 12:00:00 -0700527end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400528 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800529 return ret;
530}
Adam Langley95c29f32014-06-20 12:00:00 -0700531
David Benjamin65ac9972016-09-02 21:35:25 -0400532uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
533 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
534 * a single byte repeatedly and ensures the values are deterministic. This
535 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
536 * same group be advertised in both supported_groups and key_shares. */
537 uint16_t ret = ssl->s3->client_random[index];
538 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
539 ret = (ret & 0xf0) | 0x0a;
540 ret |= ret << 8;
541 return ret;
542}
543
David Benjamin2fe6e222017-01-25 02:18:43 -0500544/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
545 * disabled algorithms. */
546static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
547 uint32_t *out_mask_k) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500548 *out_mask_a = 0;
549 *out_mask_k = 0;
550
David Benjamin2fe6e222017-01-25 02:18:43 -0500551 /* PSK requires a client callback. */
552 if (ssl->psk_client_callback == NULL) {
553 *out_mask_a |= SSL_aPSK;
554 *out_mask_k |= SSL_kPSK;
555 }
556}
557
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200558static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
559 uint16_t min_version,
David Benjamine0ff7672016-09-19 18:40:03 -0400560 uint16_t max_version) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500561 uint32_t mask_a, mask_k;
562 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700563
David Benjamine8d53502015-10-10 14:13:23 -0400564 CBB child;
565 if (!CBB_add_u16_length_prefixed(out, &child)) {
566 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800567 }
568
David Benjamin65ac9972016-09-02 21:35:25 -0400569 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
570 if (ssl->ctx->grease_enabled &&
571 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
572 return 0;
573 }
574
David Benjaminabbbee12016-10-31 19:20:42 -0400575 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
576 * hardware support. */
577 if (max_version >= TLS1_3_VERSION) {
578 if (!EVP_has_aes_hardware() &&
579 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
580 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500581 }
David Benjaminabbbee12016-10-31 19:20:42 -0400582 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
583 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
584 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400585 }
David Benjaminabbbee12016-10-31 19:20:42 -0400586 if (EVP_has_aes_hardware() &&
587 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400588 return 0;
589 }
590 }
591
David Benjaminabbbee12016-10-31 19:20:42 -0400592 if (min_version < TLS1_3_VERSION) {
593 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
594 int any_enabled = 0;
595 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
596 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
597 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500598 if ((cipher->algorithm_mkey & mask_k) ||
599 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400600 continue;
601 }
602 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
603 SSL_CIPHER_get_max_version(cipher) < min_version) {
604 continue;
605 }
606 any_enabled = 1;
607 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
608 return 0;
609 }
610 }
611
612 /* If all ciphers were disabled, return the error to the caller. */
613 if (!any_enabled && max_version < TLS1_3_VERSION) {
614 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
615 return 0;
616 }
David Benjamine8d53502015-10-10 14:13:23 -0400617 }
618
619 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
620 * added. */
David Benjaminf04c2e92016-12-06 13:35:25 -0500621 if (max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400622 !ssl->s3->initial_handshake_complete) {
623 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
624 return 0;
625 }
David Benjamine8d53502015-10-10 14:13:23 -0400626 }
627
David Benjamine0ff7672016-09-19 18:40:03 -0400628 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200629 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
630 return 0;
631 }
David Benjamine8d53502015-10-10 14:13:23 -0400632 }
633
634 return CBB_flush(out);
635}
636
David Benjamin8c880a22016-12-03 02:20:34 -0500637int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
638 SSL *const ssl = hs->ssl;
David Benjamine0ff7672016-09-19 18:40:03 -0400639 uint16_t min_version, max_version;
640 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400641 return 0;
642 }
643
Steven Valdeza833c352016-11-01 13:39:36 -0400644 CBB cbb, body;
645 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
646 goto err;
647 }
648
Steven Valdez5440fe02016-07-18 12:40:30 -0400649 /* Renegotiations do not participate in session resumption. */
650 int has_session = ssl->session != NULL &&
651 !ssl->s3->initial_handshake_complete;
652
653 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500654 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400655 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
656 !CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400657 (has_session &&
658 !CBB_add_bytes(&child, ssl->session->session_id,
659 ssl->session->session_id_length))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400660 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400661 }
662
David Benjamince079fd2016-08-02 16:22:34 -0400663 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400664 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400665 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400666 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400667 }
668 }
669
670 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400671 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdeza833c352016-11-01 13:39:36 -0400672 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
673 !CBB_add_u8(&body, 1 /* one compression method */) ||
674 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500675 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400676 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400677 }
678
Steven Valdeza833c352016-11-01 13:39:36 -0400679 uint8_t *msg = NULL;
680 size_t len;
681 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
682 goto err;
683 }
684
685 /* Now that the length prefixes have been computed, fill in the placeholder
686 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500687 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500688 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400689 OPENSSL_free(msg);
690 goto err;
691 }
692
David Benjamindaf207a2017-01-03 18:37:41 -0500693 return ssl->method->add_message(ssl, msg, len);
Steven Valdeza833c352016-11-01 13:39:36 -0400694
695 err:
696 CBB_cleanup(&cbb);
697 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400698}
699
David Benjaminc3c88822016-11-14 10:32:04 +0900700static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
701 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400702 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
703 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
Steven Valdez908ac192017-01-12 13:17:07 -0500704 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500705 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
706 return -1;
707 }
708
David Benjaminb6a0a512016-06-21 10:33:21 -0400709 uint16_t min_version, max_version;
710 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400711 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400712 }
713
David Benjaminf04c2e92016-12-06 13:35:25 -0500714 uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
David Benjamincb0c29f2016-12-12 17:00:50 -0500715 assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
David Benjamine8d53502015-10-10 14:13:23 -0400716 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500717 ssl->version = max_wire_version;
718 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400719
David Benjaminf04c2e92016-12-06 13:35:25 -0500720 /* Always advertise the ClientHello version from the original maximum version,
721 * even on renegotiation. The static RSA key exchange uses this field, and
722 * some servers fail when it changes across handshakes. */
723 hs->client_version = max_wire_version;
724 if (max_version >= TLS1_3_VERSION) {
725 hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
David Benjamine8d53502015-10-10 14:13:23 -0400726 }
727
David Benjamine29ea162016-03-23 16:10:44 -0400728 /* If the configured session has expired or was created at a disabled
729 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400730 if (ssl->session != NULL) {
David Benjamin2dc02042016-09-19 19:57:37 -0400731 uint16_t session_version;
Adam Langley364f7a62016-12-12 10:51:00 -0800732 if (ssl->session->is_server ||
733 !ssl->method->version_from_wire(&session_version,
David Benjamin2dc02042016-09-19 19:57:37 -0400734 ssl->session->ssl_version) ||
735 (session_version < TLS1_3_VERSION &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400736 ssl->session->session_id_length == 0) ||
737 ssl->session->not_resumable ||
738 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminb6a0a512016-06-21 10:33:21 -0400739 session_version < min_version || session_version > max_version) {
David Benjamin4d0be242016-09-01 01:10:07 -0400740 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400741 }
David Benjamine8d53502015-10-10 14:13:23 -0400742 }
743
744 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
745 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400746 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400747 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400748 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400749 }
750
David Benjamin8c880a22016-12-03 02:20:34 -0500751 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400752 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400753 }
754
David Benjamin16315f72017-01-12 20:02:05 -0500755 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800756}
Adam Langley95c29f32014-06-20 12:00:00 -0700757
David Benjaminc3c88822016-11-14 10:32:04 +0900758static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
759 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -0700760 int al;
David Benjaminaa7734b2016-06-07 16:40:46 -0400761 CBS hello_verify_request, cookie;
762 uint16_t server_version;
763
David Benjaminf71036e2017-01-21 14:49:39 -0500764 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700765 if (ret <= 0) {
766 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400767 }
768
769 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
770 ssl->d1->send_cookie = 0;
771 ssl->s3->tmp.reuse_message = 1;
772 return 1;
773 }
774
David Benjaminf71036e2017-01-21 14:49:39 -0500775 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400776 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
777 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
778 CBS_len(&hello_verify_request) != 0) {
779 al = SSL_AD_DECODE_ERROR;
780 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
781 goto f_err;
782 }
783
784 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
785 al = SSL_AD_ILLEGAL_PARAMETER;
786 goto f_err;
787 }
788
David Benjamin17cf2cb2016-12-13 01:07:13 -0500789 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400790 ssl->d1->cookie_len = CBS_len(&cookie);
791
792 ssl->d1->send_cookie = 1;
793 return 1;
794
795f_err:
796 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
797 return -1;
798}
799
David Benjaminc3c88822016-11-14 10:32:04 +0900800static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
801 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800802 CBS server_hello, server_random, session_id;
David Benjamin2dc02042016-09-19 19:57:37 -0400803 uint16_t server_wire_version, cipher_suite;
Adam Langley1bea1732014-12-17 19:06:57 -0800804 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700805
David Benjaminf71036e2017-01-21 14:49:39 -0500806 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700807 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500808 uint32_t err = ERR_peek_error();
809 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
810 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
811 /* Add a dedicated error code to the queue for a handshake_failure alert
812 * in response to ClientHello. This matches NSS's client behavior and
813 * gives a better error on a (probable) failure to negotiate initial
814 * parameters. Note: this error code comes after the original one.
815 *
816 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400817 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500818 }
David Benjamin09eb6552016-07-08 14:32:11 -0700819 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Steven Valdez143e8b32016-07-11 13:19:03 -0400822 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
823 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
824 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
825 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
826 return -1;
827 }
828
David Benjamin09eb6552016-07-08 14:32:11 -0700829 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
David Benjamina03d95d2014-07-12 19:49:07 -0400830
Steven Valdez143e8b32016-07-11 13:19:03 -0400831 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400832 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400833 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
834 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800835 }
David Benjamina03d95d2014-07-12 19:49:07 -0400836
David Benjamin2dc02042016-09-19 19:57:37 -0400837 uint16_t min_version, max_version, server_version;
David Benjamine0ff7672016-09-19 18:40:03 -0400838 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
David Benjamin2dc02042016-09-19 19:57:37 -0400839 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjamin1f61f0d2016-07-10 12:20:35 -0400840 server_version < min_version || server_version > max_version) {
841 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamind3bca042017-04-08 11:05:04 -0400842 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
843 return -1;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400844 }
845
David Benjamin0d56f882015-12-19 17:05:56 -0500846 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
847 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400848 ssl->version = server_wire_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500849 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800850 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500851 ssl->s3->have_version = 1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400852 } else if (server_wire_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400853 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
David Benjamind3bca042017-04-08 11:05:04 -0400854 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
855 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Steven Valdez143e8b32016-07-11 13:19:03 -0400858 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500859 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900860 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400861 return 1;
862 }
863
Steven Valdez2d850622017-01-11 11:34:52 -0500864 if (hs->early_data_offered) {
865 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
David Benjamind3bca042017-04-08 11:05:04 -0400866 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
867 return -1;
Steven Valdez2d850622017-01-11 11:34:52 -0500868 }
869
David Benjamin6e4fc332016-11-17 16:43:08 +0900870 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400871
David Benjamin276b7e82017-01-21 14:13:39 -0500872 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400873 return -1;
874 }
875
876 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
877 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
878 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
879 !CBS_get_u16(&server_hello, &cipher_suite) ||
880 !CBS_get_u8(&server_hello, &compression_method)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400881 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400882 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
883 return -1;
Steven Valdez143e8b32016-07-11 13:19:03 -0400884 }
885
Adam Langley1bea1732014-12-17 19:06:57 -0800886 /* Copy over the server random. */
David Benjamind3bca042017-04-08 11:05:04 -0400887 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
888 SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700889
David Benjamin55108632016-08-11 22:01:18 -0400890 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
891 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400892
David Benjamin0d56f882015-12-19 17:05:56 -0500893 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400894 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500895 CBS_mem_equal(&session_id, ssl->session->session_id,
896 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400897 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800898 } else {
899 /* The session wasn't resumed. Create a fresh SSL_SESSION to
900 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400901 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900902 if (!ssl_get_new_session(hs, 0 /* client */)) {
David Benjamind3bca042017-04-08 11:05:04 -0400903 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
904 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800905 }
906 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500907 hs->new_session->session_id_length = CBS_len(&session_id);
908 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
909 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800910 }
David Benjamina03d95d2014-07-12 19:49:07 -0400911
David Benjamind2cb1c12016-11-02 17:49:09 -0400912 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800913 if (c == NULL) {
914 /* unknown cipher */
David Benjamin3570d732015-06-29 00:28:17 -0400915 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400916 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
917 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800918 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400919
920 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500921 uint32_t mask_a, mask_k;
922 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
923 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700924 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400925 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
926 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
David Benjamin3570d732015-06-29 00:28:17 -0400927 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400928 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
929 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Steven Valdez87eab492016-06-27 16:34:59 -0400932 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500933 if (ssl->session->ssl_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400934 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400935 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
936 return -1;
David Benjaminece3de92015-03-16 18:02:20 -0400937 }
Steven Valdez803c77a2016-09-06 14:13:43 -0400938 if (ssl->session->cipher != c) {
Steven Valdez803c77a2016-09-06 14:13:43 -0400939 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjamind3bca042017-04-08 11:05:04 -0400940 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
941 return -1;
Steven Valdez803c77a2016-09-06 14:13:43 -0400942 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400943 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
944 /* This is actually a client application bug. */
Steven Valdez4aa154e2016-07-29 14:32:55 -0400945 OPENSSL_PUT_ERROR(SSL,
946 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400947 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
948 return -1;
Steven Valdez4aa154e2016-07-29 14:32:55 -0400949 }
David Benjamin0623bce2015-12-25 15:40:14 -0500950 } else {
David Benjamin45738dd2017-02-09 20:01:26 -0500951 hs->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800952 }
David Benjamin45738dd2017-02-09 20:01:26 -0500953 hs->new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500954
David Benjaminf71036e2017-01-21 14:49:39 -0500955 /* Now that the cipher is known, initialize the handshake hash and hash the
956 * ServerHello. */
Steven Valdez908ac192017-01-12 13:17:07 -0500957 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
958 c->algorithm_prf) ||
959 !ssl_hash_current_message(hs)) {
David Benjamind3bca042017-04-08 11:05:04 -0400960 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
961 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -0400962 }
963
Steven Valdez2b8415e2016-06-30 13:27:23 -0400964 /* If doing a full handshake, the server may request a client certificate
965 * which requires hashing the handshake transcript. Otherwise, the handshake
966 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000967 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500968 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500969 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -0800970 }
David Benjamina03d95d2014-07-12 19:49:07 -0400971
Adam Langley1bea1732014-12-17 19:06:57 -0800972 /* Only the NULL compression algorithm is supported. */
973 if (compression_method != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400974 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
David Benjamind3bca042017-04-08 11:05:04 -0400975 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
976 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800977 }
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langley1bea1732014-12-17 19:06:57 -0800979 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -0500980 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400981 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
David Benjamind3bca042017-04-08 11:05:04 -0400982 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800983 }
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Adam Langley1bea1732014-12-17 19:06:57 -0800985 /* There should be nothing left over in the record. */
986 if (CBS_len(&server_hello) != 0) {
987 /* wrong packet length */
David Benjamin639846e2016-09-09 11:41:18 -0400988 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind3bca042017-04-08 11:05:04 -0400989 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
990 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -0800991 }
Adam Langley95c29f32014-06-20 12:00:00 -0700992
Steven Valdez87eab492016-06-27 16:34:59 -0400993 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -0500994 hs->extended_master_secret != ssl->session->extended_master_secret) {
David Benjamin0d56f882015-12-19 17:05:56 -0500995 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700997 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400998 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700999 }
David Benjamind3bca042017-04-08 11:05:04 -04001000 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1001 return -1;
Adam Langleyba5934b2015-06-02 10:50:35 -07001002 }
1003
Adam Langley1bea1732014-12-17 19:06:57 -08001004 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001005}
Adam Langley95c29f32014-06-20 12:00:00 -07001006
David Benjaminc3c88822016-11-14 10:32:04 +09001007static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1008 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001009 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001010 if (ret <= 0) {
1011 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001012 }
Adam Langley95c29f32014-06-20 12:00:00 -07001013
David Benjaminf71036e2017-01-21 14:49:39 -05001014 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001015 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001016 return -1;
1017 }
1018
David Benjamin5c900c82016-07-13 23:03:26 -04001019 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001020 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001021
Adam Langleyc68e5b92017-02-08 13:33:15 -08001022 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001023 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001024 EVP_PKEY_free(hs->peer_pubkey);
1025 hs->peer_pubkey = NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001026 hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
1027 &cbs, ssl->ctx->pool);
1028 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001029 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001030 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001031 }
Adam Langley95c29f32014-06-20 12:00:00 -07001032
David Benjamin45738dd2017-02-09 20:01:26 -05001033 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
Adam Langley68e71242016-12-12 11:06:16 -08001034 CBS_len(&cbs) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001035 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin3570d732015-06-29 00:28:17 -04001036 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001037 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001038 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001039 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001040
Adam Langley05672202016-12-13 12:05:49 -08001041 if (!ssl_check_leaf_certificate(
David Benjamin45738dd2017-02-09 20:01:26 -05001042 hs, hs->peer_pubkey,
1043 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001044 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001045 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001046 }
1047
David Benjamina58baaf2017-02-28 20:54:28 -05001048 /* Disallow the server certificate from changing during a renegotiation. See
1049 * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
1050 * so this check is sufficient. */
1051 if (ssl->s3->established_session != NULL) {
1052 if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
1053 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
1054 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1055 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1056 return -1;
1057 }
1058
1059 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
1060 const CRYPTO_BUFFER *old_cert =
1061 sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
1062 const CRYPTO_BUFFER *new_cert =
1063 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
1064 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
1065 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
1066 CRYPTO_BUFFER_data(new_cert),
1067 CRYPTO_BUFFER_len(old_cert)) != 0) {
1068 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1069 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1070 return -1;
1071 }
1072 }
1073 }
1074
David Benjamin5c900c82016-07-13 23:03:26 -04001075 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001076}
1077
David Benjaminc3c88822016-11-14 10:32:04 +09001078static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1079 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001080 int al;
David Benjamin9f1dc822016-06-07 17:03:46 -04001081 CBS certificate_status, ocsp_response;
1082 uint8_t status_type;
1083
David Benjaminf71036e2017-01-21 14:49:39 -05001084 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001085 if (ret <= 0) {
1086 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001087 }
1088
1089 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1090 /* A server may send status_request in ServerHello and then change
1091 * its mind about sending CertificateStatus. */
1092 ssl->s3->tmp.reuse_message = 1;
1093 return 1;
1094 }
1095
Steven Valdez908ac192017-01-12 13:17:07 -05001096 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001097 return -1;
1098 }
1099
David Benjamin09eb6552016-07-08 14:32:11 -07001100 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001101 if (!CBS_get_u8(&certificate_status, &status_type) ||
1102 status_type != TLSEXT_STATUSTYPE_ocsp ||
1103 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1104 CBS_len(&ocsp_response) == 0 ||
1105 CBS_len(&certificate_status) != 0) {
1106 al = SSL_AD_DECODE_ERROR;
1107 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1108 goto f_err;
1109 }
1110
David Benjamin45738dd2017-02-09 20:01:26 -05001111 if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
1112 &hs->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001113 al = SSL_AD_INTERNAL_ERROR;
1114 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1115 goto f_err;
1116 }
1117 return 1;
1118
1119f_err:
1120 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1121 return -1;
1122}
1123
David Benjaminc3c88822016-11-14 10:32:04 +09001124static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1125 SSL *const ssl = hs->ssl;
Adam Langley2a3b3432017-02-10 13:56:48 -08001126 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
David Benjamine455e512016-08-01 20:11:13 -04001127 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001128 }
1129
Adam Langley9498e742016-07-18 10:17:16 -07001130 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001131}
1132
David Benjaminc3c88822016-11-14 10:32:04 +09001133static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1134 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001135 int al;
Adam Langley1bea1732014-12-17 19:06:57 -08001136 DH *dh = NULL;
1137 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001138 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001139
David Benjaminf71036e2017-01-21 14:49:39 -05001140 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001141 if (ret <= 0) {
1142 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001143 }
1144
David Benjamin0d56f882015-12-19 17:05:56 -05001145 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001146 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001147 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001148 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001149 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001150 return -1;
1151 }
1152
David Benjamin0d56f882015-12-19 17:05:56 -05001153 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001154 return 1;
1155 }
1156
Steven Valdez908ac192017-01-12 13:17:07 -05001157 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001158 return -1;
1159 }
1160
Adam Langley1bea1732014-12-17 19:06:57 -08001161 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001162 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001163 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001164 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001165
David Benjamin45738dd2017-02-09 20:01:26 -05001166 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1167 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001168
1169 if (alg_a & SSL_aPSK) {
1170 CBS psk_identity_hint;
1171
1172 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1173 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1174 &psk_identity_hint)) {
1175 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001176 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001177 goto f_err;
1178 }
1179
1180 /* Store PSK identity hint for later use, hint is used in
1181 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1182 * identity hint can be as long as the maximum length of a PSK identity.
1183 * Also do not allow NULL characters; identities are saved as C strings.
1184 *
1185 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1186 * a specific identity. */
1187 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1188 CBS_contains_zero_byte(&psk_identity_hint)) {
1189 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001190 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001191 goto f_err;
1192 }
1193
David Benjamin78679342016-09-16 19:42:05 -04001194 /* Save non-empty identity hints as a C string. Empty identity hints we
1195 * treat as missing. Plain PSK makes it possible to send either no hint
1196 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1197 * empty hint. Having different capabilities is odd, so we interpret empty
1198 * and missing as identical. */
1199 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001200 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001201 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001202 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001203 goto f_err;
1204 }
1205 }
1206
David Benjamin7061e282015-03-19 11:10:48 -04001207 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001208 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001209 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1210 CBS_len(&dh_p) == 0 ||
1211 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1212 CBS_len(&dh_g) == 0 ||
1213 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1214 CBS_len(&dh_Ys) == 0) {
1215 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001216 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001217 goto f_err;
1218 }
1219
1220 dh = DH_new();
1221 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001222 goto err;
1223 }
1224
David Benjamin48cce662015-12-17 01:33:08 -05001225 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1226 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001227 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001228 goto err;
1229 }
1230
David Benjamin4882a6c2016-12-11 02:48:12 -05001231 unsigned bits = DH_num_bits(dh);
1232 if (bits < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001233 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001234 goto err;
David Benjamin4882a6c2016-12-11 02:48:12 -05001235 } else if (bits > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001236 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1237 * to prevent a server from causing us to perform too expensive of a
1238 * computation. */
1239 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1240 goto err;
1241 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001242
David Benjaminc3c88822016-11-14 10:32:04 +09001243 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001244 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001245
1246 /* Save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001247 if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001248 goto err;
1249 }
David Benjamin7061e282015-03-19 11:10:48 -04001250 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001251 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001252 uint8_t group_type;
1253 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001254 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001255 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1256 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001257 !CBS_get_u16(&server_key_exchange, &group_id) ||
1258 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001259 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001260 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001261 goto f_err;
1262 }
David Benjamin45738dd2017-02-09 20:01:26 -05001263 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001264
Steven Valdezce902a92016-05-17 11:47:53 -04001265 /* Ensure the group is consistent with preferences. */
1266 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001267 al = SSL_AD_ILLEGAL_PARAMETER;
1268 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001269 goto f_err;
1270 }
David Benjamin4298d772015-12-19 00:18:25 -05001271
Matt Braithwaite053931e2016-05-25 12:06:05 -07001272 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001273 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1274 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001275 goto err;
1276 }
Adam Langley1bea1732014-12-17 19:06:57 -08001277 } else if (!(alg_k & SSL_kPSK)) {
1278 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001279 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001280 goto f_err;
1281 }
1282
1283 /* At this point, |server_key_exchange| contains the signature, if any, while
1284 * |server_key_exchange_orig| contains the entire message. From that, derive
1285 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001286 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001287 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1288 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1289
David Benjaminf4958e72015-06-07 11:00:53 -04001290 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001291 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001292 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001293 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001294 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001295 al = SSL_AD_DECODE_ERROR;
1296 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001297 goto f_err;
1298 }
David Benjamin887c3002016-07-08 16:15:32 -07001299 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001300 goto f_err;
1301 }
David Benjamin45738dd2017-02-09 20:01:26 -05001302 hs->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001303 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001304 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001305 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001306 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001307 } else {
1308 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1309 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1310 goto f_err;
Adam Langley1bea1732014-12-17 19:06:57 -08001311 }
1312
1313 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001314 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001315 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1316 CBS_len(&server_key_exchange) != 0) {
1317 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001318 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001319 goto f_err;
1320 }
1321
Steven Valdez2b8415e2016-06-30 13:27:23 -04001322 CBB transcript;
1323 uint8_t *transcript_data;
1324 size_t transcript_len;
1325 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1326 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1327 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1328 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1329 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1330 CBB_cleanup(&transcript);
1331 al = SSL_AD_INTERNAL_ERROR;
1332 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001333 goto f_err;
1334 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001335
1336 int sig_ok = ssl_public_key_verify(
1337 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001338 hs->peer_pubkey, transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001339 OPENSSL_free(transcript_data);
1340
David Benjaminbf82aed2016-03-01 22:57:40 -05001341#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1342 sig_ok = 1;
1343 ERR_clear_error();
1344#endif
1345 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001346 /* bad signature */
1347 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001348 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001349 goto f_err;
1350 }
1351 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001352 /* PSK ciphers are the only supported certificate-less ciphers. */
1353 assert(alg_a == SSL_aPSK);
1354
Adam Langley1bea1732014-12-17 19:06:57 -08001355 if (CBS_len(&server_key_exchange) > 0) {
1356 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001357 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001358 goto f_err;
1359 }
1360 }
Adam Langley1bea1732014-12-17 19:06:57 -08001361 return 1;
1362
Adam Langley95c29f32014-06-20 12:00:00 -07001363f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001364 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001365err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001366 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001367 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001368 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001369 return -1;
1370}
Adam Langley95c29f32014-06-20 12:00:00 -07001371
David Benjaminc3c88822016-11-14 10:32:04 +09001372static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1373 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001374 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001375 if (msg_ret <= 0) {
1376 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001377 }
David Benjamin688d8df2014-11-02 23:06:42 -05001378
David Benjamin3dcec452016-06-07 17:13:28 -04001379 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001380 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001381 /* If we get here we don't need the handshake buffer as we won't be doing
1382 * client auth. */
Steven Valdez908ac192017-01-12 13:17:07 -05001383 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001384 return 1;
1385 }
David Benjamined439582014-07-14 19:13:02 -04001386
David Benjaminf71036e2017-01-21 14:49:39 -05001387 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001388 !ssl_hash_current_message(hs)) {
David Benjamine0332e82016-07-13 22:40:36 -04001389 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001390 }
Adam Langley95c29f32014-06-20 12:00:00 -07001391
David Benjamina5177cb2016-03-11 19:38:17 -05001392 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001393 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001394
David Benjamine0332e82016-07-13 22:40:36 -04001395 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001396 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001397 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001398 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001399 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001400 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001401 }
David Benjamined439582014-07-14 19:13:02 -04001402
David Benjaminc3c88822016-11-14 10:32:04 +09001403 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1404 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001405 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001406 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001407 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001408
David Benjamina1e9cab2015-12-30 00:08:49 -05001409 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001410 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001411 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001412 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001413 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001414 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001415 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001416 }
Adam Langley1bea1732014-12-17 19:06:57 -08001417 }
David Benjamined439582014-07-14 19:13:02 -04001418
Adam Langleyc68e5b92017-02-08 13:33:15 -08001419 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Langley34b4c822017-02-02 10:57:17 -08001420 STACK_OF(CRYPTO_BUFFER) *ca_names =
1421 ssl_parse_client_CA_list(ssl, &alert, &cbs);
1422 if (ca_names == NULL) {
David Benjamine0332e82016-07-13 22:40:36 -04001423 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1424 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001425 }
Adam Langley95c29f32014-06-20 12:00:00 -07001426
David Benjamin639846e2016-09-09 11:41:18 -04001427 if (CBS_len(&cbs) != 0) {
1428 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley34b4c822017-02-02 10:57:17 -08001429 sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
David Benjamin639846e2016-09-09 11:41:18 -04001430 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1431 return -1;
1432 }
1433
David Benjaminc3c88822016-11-14 10:32:04 +09001434 hs->cert_request = 1;
Adam Langley34b4c822017-02-02 10:57:17 -08001435 sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
1436 hs->ca_names = ca_names;
1437 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamine0332e82016-07-13 22:40:36 -04001438 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001439}
Adam Langley95c29f32014-06-20 12:00:00 -07001440
David Benjaminc3c88822016-11-14 10:32:04 +09001441static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1442 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001443 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001444 if (ret <= 0) {
1445 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001446 }
David Benjamin46062682014-07-14 19:14:32 -04001447
David Benjaminf71036e2017-01-21 14:49:39 -05001448 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001449 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001450 return -1;
1451 }
1452
David Benjamin09eb6552016-07-08 14:32:11 -07001453 /* ServerHelloDone is empty. */
1454 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001455 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001456 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001457 return -1;
1458 }
David Benjamin46062682014-07-14 19:14:32 -04001459
Adam Langley1bea1732014-12-17 19:06:57 -08001460 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001461}
Adam Langley1258b6a2014-06-20 12:00:00 -07001462
David Benjaminc3c88822016-11-14 10:32:04 +09001463static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1464 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001465 /* Call cert_cb to update the certificate. */
1466 if (ssl->cert->cert_cb) {
1467 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1468 if (ret < 0) {
1469 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001470 return -1;
1471 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001472 if (ret == 0) {
1473 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1474 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001475 return -1;
1476 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001477 }
1478
David Benjamin5edfc8c2016-12-10 15:46:58 -05001479 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001480 /* Without a client certificate, the handshake buffer may be released. */
Steven Valdez908ac192017-01-12 13:17:07 -05001481 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin5edfc8c2016-12-10 15:46:58 -05001482
David Benjamindaf207a2017-01-03 18:37:41 -05001483 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001484 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001485 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1486 SSL_AD_NO_CERTIFICATE)) {
1487 return -1;
1488 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001489 return 1;
1490 }
1491 }
1492
David Benjamina232a712017-03-30 15:51:53 -05001493 if (!ssl_on_certificate_selected(hs) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001494 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001495 return -1;
1496 }
David Benjamin16315f72017-01-12 20:02:05 -05001497 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001498}
1499
Brian Smith274341d2015-10-08 17:10:15 -10001500OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1501 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001502
David Benjaminc3c88822016-11-14 10:32:04 +09001503static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1504 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -08001505 uint8_t *pms = NULL;
1506 size_t pms_len = 0;
David Benjamin75836432016-06-17 18:48:29 -04001507 CBB cbb, body;
1508 if (!ssl->method->init_message(ssl, &cbb, &body,
1509 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001510 goto err;
1511 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001512
David Benjamin45738dd2017-02-09 20:01:26 -05001513 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1514 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001515
David Benjaminae0eaaa2015-12-17 02:01:25 -05001516 /* If using a PSK key exchange, prepare the pre-shared key. */
1517 unsigned psk_len = 0;
1518 uint8_t psk[PSK_MAX_PSK_LEN];
1519 if (alg_a & SSL_aPSK) {
1520 if (ssl->psk_client_callback == NULL) {
1521 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1522 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001523 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001524
David Benjaminae0eaaa2015-12-17 02:01:25 -05001525 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001526 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001527 psk_len =
1528 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1529 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001530 if (psk_len == 0) {
1531 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1532 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1533 goto err;
1534 }
1535 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001536
David Benjamin45738dd2017-02-09 20:01:26 -05001537 OPENSSL_free(hs->new_session->psk_identity);
1538 hs->new_session->psk_identity = BUF_strdup(identity);
1539 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001540 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1541 goto err;
1542 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001543
David Benjaminae0eaaa2015-12-17 02:01:25 -05001544 /* Write out psk_identity. */
1545 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001546 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001547 !CBB_add_bytes(&child, (const uint8_t *)identity,
1548 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001549 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001550 goto err;
1551 }
1552 }
David Benjamin758d1272015-11-20 17:47:25 -05001553
David Benjaminae0eaaa2015-12-17 02:01:25 -05001554 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1555 if (alg_k & SSL_kRSA) {
1556 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1557 pms = OPENSSL_malloc(pms_len);
1558 if (pms == NULL) {
1559 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1560 goto err;
1561 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001562
Adam Langleyd5157222016-12-12 11:37:43 -08001563 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001564 if (rsa == NULL) {
1565 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001566 goto err;
1567 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001568
David Benjaminf04c2e92016-12-06 13:35:25 -05001569 pms[0] = hs->client_version >> 8;
1570 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001571 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1572 goto err;
1573 }
1574
David Benjamin75836432016-06-17 18:48:29 -04001575 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001576 size_t enc_pms_len;
1577 /* In TLS, there is a length prefix. */
1578 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001579 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001580 goto err;
1581 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001582 enc_pms = &child;
1583 }
Adam Langley1bea1732014-12-17 19:06:57 -08001584
David Benjaminae0eaaa2015-12-17 02:01:25 -05001585 uint8_t *ptr;
1586 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1587 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1588 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001589 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001590 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001591 goto err;
1592 }
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001593 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001594 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001595 CBB child;
David Benjaminc3c88822016-11-14 10:32:04 +09001596 if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001597 goto err;
1598 }
1599
David Benjamin4298d772015-12-19 00:18:25 -05001600 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001601 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001602 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1603 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001604 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1605 goto err;
1606 }
David Benjamin75836432016-06-17 18:48:29 -04001607 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001608 goto err;
1609 }
David Benjamin4298d772015-12-19 00:18:25 -05001610
1611 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001612 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1613 OPENSSL_free(hs->peer_key);
1614 hs->peer_key = NULL;
1615 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001616 } else if (alg_k & SSL_kPSK) {
1617 /* For plain PSK, other_secret is a block of 0s with the same length as
1618 * the pre-shared key. */
1619 pms_len = psk_len;
1620 pms = OPENSSL_malloc(pms_len);
1621 if (pms == NULL) {
1622 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1623 goto err;
1624 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001625 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001626 } else {
1627 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1628 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1629 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001630 }
1631
David Benjaminae0eaaa2015-12-17 02:01:25 -05001632 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1633 * key. */
1634 if (alg_a & SSL_aPSK) {
1635 CBB pms_cbb, child;
1636 uint8_t *new_pms;
1637 size_t new_pms_len;
1638
1639 CBB_zero(&pms_cbb);
1640 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1641 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1642 !CBB_add_bytes(&child, pms, pms_len) ||
1643 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1644 !CBB_add_bytes(&child, psk, psk_len) ||
1645 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1646 CBB_cleanup(&pms_cbb);
1647 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1648 goto err;
1649 }
1650 OPENSSL_cleanse(pms, pms_len);
1651 OPENSSL_free(pms);
1652 pms = new_pms;
1653 pms_len = new_pms_len;
1654 }
1655
1656 /* The message must be added to the finished hash before calculating the
1657 * master secret. */
David Benjamindaf207a2017-01-03 18:37:41 -05001658 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001659 goto err;
1660 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001661
David Benjamin45738dd2017-02-09 20:01:26 -05001662 hs->new_session->master_key_length = tls1_generate_master_secret(
1663 hs, hs->new_session->master_key, pms, pms_len);
1664 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001665 goto err;
1666 }
David Benjaminfc02b592017-02-17 16:26:01 -05001667 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001668 OPENSSL_cleanse(pms, pms_len);
1669 OPENSSL_free(pms);
1670
David Benjamin16315f72017-01-12 20:02:05 -05001671 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001672
1673err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001674 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001675 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001676 OPENSSL_cleanse(pms, pms_len);
1677 OPENSSL_free(pms);
1678 }
1679 return -1;
1680}
Adam Langley1258b6a2014-06-20 12:00:00 -07001681
David Benjaminc3c88822016-11-14 10:32:04 +09001682static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1683 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001684 assert(ssl_has_private_key(ssl));
1685
David Benjamin75836432016-06-17 18:48:29 -04001686 CBB cbb, body, child;
1687 if (!ssl->method->init_message(ssl, &cbb, &body,
1688 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjaminfc825122015-12-18 01:57:43 -05001689 goto err;
1690 }
1691
David Benjaminea9a0d52016-07-08 15:52:59 -07001692 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001693 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001694 goto err;
1695 }
David Benjamin429fdc02016-06-17 17:33:30 -04001696 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001697 /* Write out the digest type in TLS 1.2. */
1698 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001699 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1700 goto err;
1701 }
1702 }
David Benjaminfc825122015-12-18 01:57:43 -05001703
David Benjamin429fdc02016-06-17 17:33:30 -04001704 /* Set aside space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001705 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjamin429fdc02016-06-17 17:33:30 -04001706 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001707 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001708 !CBB_reserve(&child, &ptr, max_sig_len)) {
1709 goto err;
1710 }
1711
Steven Valdez2b8415e2016-06-30 13:27:23 -04001712 size_t sig_len = max_sig_len;
David Benjaminfc825122015-12-18 01:57:43 -05001713 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001714 if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001715 /* The SSL3 construction for CertificateVerify does not decompose into a
1716 * single final digest and signature, and must be special-cased. */
1717 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1718 if (ssl->cert->key_method != NULL) {
1719 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1720 goto err;
1721 }
1722
1723 uint8_t digest[EVP_MAX_MD_SIZE];
1724 size_t digest_len;
David Benjamin45738dd2017-02-09 20:01:26 -05001725 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
1726 &digest_len, hs->new_session,
1727 signature_algorithm)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001728 goto err;
1729 }
1730
1731 sign_result = ssl_private_key_success;
1732
1733 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1734 if (pctx == NULL ||
1735 !EVP_PKEY_sign_init(pctx) ||
1736 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1737 EVP_PKEY_CTX_free(pctx);
1738 sign_result = ssl_private_key_failure;
1739 goto err;
1740 }
1741 EVP_PKEY_CTX_free(pctx);
1742 } else {
1743 sign_result = ssl_private_key_sign(
1744 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001745 (const uint8_t *)hs->transcript.buffer->data,
1746 hs->transcript.buffer->length);
David Benjaminfc825122015-12-18 01:57:43 -05001747 }
1748
1749 /* The handshake buffer is no longer necessary. */
Steven Valdez908ac192017-01-12 13:17:07 -05001750 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjaminfc825122015-12-18 01:57:43 -05001751 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001752 assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjamind3440b42016-07-14 14:52:41 -04001753 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
David Benjaminfc825122015-12-18 01:57:43 -05001754 }
1755
1756 switch (sign_result) {
1757 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001758 break;
1759 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001760 goto err;
1761 case ssl_private_key_retry:
1762 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001763 hs->state = SSL3_ST_CW_CERT_VRFY_B;
David Benjaminfc825122015-12-18 01:57:43 -05001764 goto err;
1765 }
1766
David Benjaminfc825122015-12-18 01:57:43 -05001767 if (!CBB_did_write(&child, sig_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001768 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminfc825122015-12-18 01:57:43 -05001769 goto err;
1770 }
1771
David Benjamin16315f72017-01-12 20:02:05 -05001772 return 1;
David Benjaminfc825122015-12-18 01:57:43 -05001773
1774err:
1775 CBB_cleanup(&cbb);
1776 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001777}
1778
David Benjaminc3c88822016-11-14 10:32:04 +09001779static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1780 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001781 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001782 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001783
David Benjamin75836432016-06-17 18:48:29 -04001784 CBB cbb, body, child;
1785 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1786 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001787 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1788 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001789 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001790 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001791 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001792 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1793 CBB_cleanup(&cbb);
1794 return -1;
1795 }
1796
David Benjamin16315f72017-01-12 20:02:05 -05001797 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001798}
1799
David Benjaminc3c88822016-11-14 10:32:04 +09001800static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1801 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001802 if (!ssl_do_channel_id_callback(ssl)) {
1803 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001804 }
1805
David Benjamin96e97b12015-10-10 16:30:22 -04001806 if (ssl->tlsext_channel_id_private == NULL) {
1807 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001808 return -1;
1809 }
Adam Langley1bea1732014-12-17 19:06:57 -08001810
Nick Harper60a85cb2016-09-23 16:25:11 -07001811 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001812 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001813 !tls1_write_channel_id(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001814 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001815 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001816 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001817 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001818 }
1819
David Benjamin16315f72017-01-12 20:02:05 -05001820 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001821}
1822
David Benjaminc3c88822016-11-14 10:32:04 +09001823static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1824 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001825 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001826 if (ret <= 0) {
1827 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001828 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001829
David Benjaminf71036e2017-01-21 14:49:39 -05001830 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001831 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001832 return -1;
1833 }
1834
David Benjamin9f1dc822016-06-07 17:03:46 -04001835 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001836 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001837 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001838 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001839 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1840 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001841 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001842 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001843 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001844 }
1845
David Benjamin9f1dc822016-06-07 17:03:46 -04001846 if (CBS_len(&ticket) == 0) {
1847 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001848 * negotiating the extension. The value of |ticket_expected| is checked in
1849 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001850 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001851 return 1;
1852 }
1853
Steven Valdez87eab492016-06-27 16:34:59 -04001854 int session_renewed = ssl->session != NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001855 SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001856 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001857 /* The server is sending a new ticket for an existing session. Sessions are
1858 * immutable once established, so duplicate all but the ticket of the
1859 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001860 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001861 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001862 /* This should never happen. */
1863 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1864 goto err;
1865 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001866 }
1867
David Benjamin123db572016-11-03 16:59:25 -04001868 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001869 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001870
Steven Valdez87eab492016-06-27 16:34:59 -04001871 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001872 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1873 goto err;
1874 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001875 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001876
1877 /* Generate a session ID for this session based on the session ticket. We use
1878 * the session ID mechanism for detecting ticket resumption. This also fits in
1879 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001880 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1881 session->session_id, &session->session_id_length,
1882 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001883 goto err;
1884 }
1885
Steven Valdez87eab492016-06-27 16:34:59 -04001886 if (session_renewed) {
1887 session->not_resumable = 0;
1888 SSL_SESSION_free(ssl->session);
1889 ssl->session = session;
1890 }
1891
David Benjamin454aa4c2016-03-10 08:43:27 -05001892 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001893
David Benjamin9f1dc822016-06-07 17:03:46 -04001894err:
Steven Valdez87eab492016-06-27 16:34:59 -04001895 if (session_renewed) {
1896 SSL_SESSION_free(session);
1897 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001898 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001899}