blob: c457772b7ac63ca331c3e523a0b0c02602109615 [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#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400168#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin17cf2cb2016-12-13 01:07:13 -0500170#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400171#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjaminc3c88822016-11-14 10:32:04 +0900174static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
175static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
176static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
177static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
178static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
179static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
180static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
181static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
182static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
183static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
184static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
185static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
186static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
187static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
188static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400189
David Benjamince8c9d22016-11-14 10:45:16 +0900190int ssl3_connect(SSL_HANDSHAKE *hs) {
191 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800192 int ret = -1;
David Benjaminbeb47022014-11-30 02:58:52 -0500193
David Benjamin0d56f882015-12-19 17:05:56 -0500194 assert(ssl->handshake_func == ssl3_connect);
195 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langley1bea1732014-12-17 19:06:57 -0800197 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500198 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400201 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400202 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500203 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800204 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley1bea1732014-12-17 19:06:57 -0800206 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900207 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800208 if (ret <= 0) {
209 goto end;
210 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400211
David Benjamince079fd2016-08-02 16:22:34 -0400212 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500213 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400214 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500215 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400216 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500217 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800218 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjaminaa7734b2016-06-07 16:40:46 -0400220 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400221 assert(SSL_is_dtls(ssl));
David Benjaminc3c88822016-11-14 10:32:04 +0900222 ret = dtls1_get_hello_verify(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400223 if (ret <= 0) {
224 goto end;
225 }
226 if (ssl->d1->send_cookie) {
227 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500228 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400229 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500230 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400231 }
232 break;
233
Adam Langley1bea1732014-12-17 19:06:57 -0800234 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900235 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500236 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400237 break;
238 }
Adam Langley1bea1732014-12-17 19:06:57 -0800239 if (ret <= 0) {
240 goto end;
241 }
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Steven Valdez87eab492016-06-27 16:34:59 -0400243 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500244 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800245 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500246 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800247 }
Adam Langley1bea1732014-12-17 19:06:57 -0800248 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400249
Adam Langley1bea1732014-12-17 19:06:57 -0800250 case SSL3_ST_CR_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500251 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900252 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800253 if (ret <= 0) {
254 goto end;
255 }
Adam Langley1bea1732014-12-17 19:06:57 -0800256 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500257 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800258 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700259
David Benjamin9f1dc822016-06-07 17:03:46 -0400260 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900261 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900262 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400263 if (ret <= 0) {
264 goto end;
265 }
David Benjamin9f1dc822016-06-07 17:03:46 -0400266 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500267 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400268 break;
269
Paul Lietar8f1c2682015-08-18 12:21:54 +0100270 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin45738dd2017-02-09 20:01:26 -0500271 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900272 ret = ssl3_verify_server_cert(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400273 if (ret <= 0) {
274 goto end;
275 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100276 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500277 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100278 break;
279
Adam Langley1bea1732014-12-17 19:06:57 -0800280 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900281 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800282 if (ret <= 0) {
283 goto end;
284 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500285 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800286 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langley1bea1732014-12-17 19:06:57 -0800288 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500289 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900290 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400291 if (ret <= 0) {
292 goto end;
293 }
Adam Langley1bea1732014-12-17 19:06:57 -0800294 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500295 hs->state = SSL3_ST_CR_SRVR_DONE_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_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900299 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800300 if (ret <= 0) {
301 goto end;
302 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400303 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500304 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800305 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langley1bea1732014-12-17 19:06:57 -0800307 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900308 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900309 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400310 if (ret <= 0) {
311 goto end;
312 }
Adam Langley1bea1732014-12-17 19:06:57 -0800313 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500314 hs->state = SSL3_ST_CW_KEY_EXCH_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_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900318 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800319 if (ret <= 0) {
320 goto end;
321 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500322 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800323 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langley1bea1732014-12-17 19:06:57 -0800325 case SSL3_ST_CW_CERT_VRFY_A:
326 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamina660e7a2017-01-02 08:15:35 -0500327 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900328 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400329 if (ret <= 0) {
330 goto end;
331 }
Adam Langley1bea1732014-12-17 19:06:57 -0800332 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500333 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800334 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700335
David Benjamin352d0a92016-06-28 11:22:02 -0400336 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500337 if (!ssl->method->add_change_cipher_spec(ssl) ||
338 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800339 ret = -1;
340 goto end;
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamindaf207a2017-01-03 18:37:41 -0500343 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800344 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langley1bea1732014-12-17 19:06:57 -0800346 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900347 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900348 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400349 if (ret <= 0) {
350 goto end;
351 }
Adam Langley1bea1732014-12-17 19:06:57 -0800352 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500353 hs->state = SSL3_ST_CW_CHANNEL_ID_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_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400357 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900358 ret = ssl3_send_channel_id(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_FINISHED_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_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500367 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800368 if (ret <= 0) {
369 goto end;
370 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500371 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Steven Valdez87eab492016-06-27 16:34:59 -0400373 if (ssl->session != NULL) {
David Benjamin77458a42017-02-10 23:33:27 -0500374 hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800375 } else {
376 /* This is a non-resumption handshake. If it involves ChannelID, then
377 * record the handshake hashes at this point in the session so that
378 * any resumption of this session with ChannelID can sign those
379 * hashes. */
Steven Valdez908ac192017-01-12 13:17:07 -0500380 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langley49c7af12015-07-10 14:33:46 -0700381 if (ret <= 0) {
382 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800383 }
David Benjamin0d56f882015-12-19 17:05:56 -0500384 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
385 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500386 /* No False Start on renegotiation (would complicate the state
387 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500388 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500389 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800390 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500391 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800392 }
393 }
Adam Langley1bea1732014-12-17 19:06:57 -0800394 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
David Benjamin9f1dc822016-06-07 17:03:46 -0400396 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500397 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900398 hs->in_false_start = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400399 ret = 1;
400 goto end;
401
Adam Langley1bea1732014-12-17 19:06:57 -0800402 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900403 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900404 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400405 if (ret <= 0) {
406 goto end;
407 }
Adam Langley1bea1732014-12-17 19:06:57 -0800408 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500409 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langley1bea1732014-12-17 19:06:57 -0800412 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400413 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500414 if (ret <= 0) {
415 goto end;
416 }
417
David Benjamin67739722016-11-17 17:03:59 +0900418 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800419 ret = -1;
420 goto end;
421 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500422 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800423 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langley1bea1732014-12-17 19:06:57 -0800425 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900426 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800427 if (ret <= 0) {
428 goto end;
429 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400430 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Steven Valdez87eab492016-06-27 16:34:59 -0400432 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500433 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800434 } else {
David Benjamin77458a42017-02-10 23:33:27 -0500435 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Adam Langley1bea1732014-12-17 19:06:57 -0800436 }
Adam Langley1bea1732014-12-17 19:06:57 -0800437 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langley1bea1732014-12-17 19:06:57 -0800439 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500440 ret = ssl->method->flush_flight(ssl);
441 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800442 goto end;
443 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500444 hs->state = hs->next_state;
David Benjamin77458a42017-02-10 23:33:27 -0500445 if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400446 ssl->method->expect_flight(ssl);
447 }
Adam Langley1bea1732014-12-17 19:06:57 -0800448 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700449
Steven Valdez143e8b32016-07-11 13:19:03 -0400450 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900451 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400452 if (ret <= 0) {
453 goto end;
454 }
David Benjamin77458a42017-02-10 23:33:27 -0500455 hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
Steven Valdez143e8b32016-07-11 13:19:03 -0400456 break;
457
David Benjamin77458a42017-02-10 23:33:27 -0500458 case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
David Benjamin4497e582016-07-27 17:51:49 -0400459 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Steven Valdez87eab492016-06-27 16:34:59 -0400461 SSL_SESSION_free(ssl->s3->established_session);
462 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400463 SSL_SESSION_up_ref(ssl->session);
464 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400465 } else {
466 /* We make a copy of the session in order to maintain the immutability
467 * of the new established_session due to False Start. The caller may
468 * have taken a reference to the temporary session. */
469 ssl->s3->established_session =
David Benjamin45738dd2017-02-09 20:01:26 -0500470 SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400471 if (ssl->s3->established_session == NULL) {
Steven Valdez87eab492016-06-27 16:34:59 -0400472 ret = -1;
473 goto end;
474 }
475 ssl->s3->established_session->not_resumable = 0;
476
David Benjamin45738dd2017-02-09 20:01:26 -0500477 SSL_SESSION_free(hs->new_session);
478 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400479 }
480
David Benjamin77458a42017-02-10 23:33:27 -0500481 hs->state = SSL_ST_OK;
482 break;
483
484 case SSL_ST_OK: {
David Benjamin0d56f882015-12-19 17:05:56 -0500485 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500486 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400487 if (is_initial_handshake) {
488 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900489 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400490 }
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langley1bea1732014-12-17 19:06:57 -0800492 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400493 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800494 goto end;
David Benjamin77458a42017-02-10 23:33:27 -0500495 }
Steven Valdez87eab492016-06-27 16:34:59 -0400496
Adam Langley1bea1732014-12-17 19:06:57 -0800497 default:
David Benjamin3570d732015-06-29 00:28:17 -0400498 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800499 ret = -1;
500 goto end;
501 }
502
David Benjamin16b1b1d2017-02-10 23:41:14 -0500503 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400504 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800505 }
Adam Langley1bea1732014-12-17 19:06:57 -0800506 }
507
Adam Langley95c29f32014-06-20 12:00:00 -0700508end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400509 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800510 return ret;
511}
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamin65ac9972016-09-02 21:35:25 -0400513uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
514 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
515 * a single byte repeatedly and ensures the values are deterministic. This
516 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
517 * same group be advertised in both supported_groups and key_shares. */
518 uint16_t ret = ssl->s3->client_random[index];
519 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
520 ret = (ret & 0xf0) | 0x0a;
521 ret |= ret << 8;
522 return ret;
523}
524
David Benjamin2fe6e222017-01-25 02:18:43 -0500525/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
526 * disabled algorithms. */
527static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
528 uint32_t *out_mask_k) {
529 int have_rsa = 0, have_ecdsa = 0;
530 *out_mask_a = 0;
531 *out_mask_k = 0;
532
533 /* Now go through all signature algorithms seeing if we support any for RSA or
534 * ECDSA. Do this for all versions not just TLS 1.2. */
535 const uint16_t *sigalgs;
536 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
537 for (size_t i = 0; i < num_sigalgs; i++) {
538 switch (sigalgs[i]) {
539 case SSL_SIGN_RSA_PSS_SHA512:
540 case SSL_SIGN_RSA_PSS_SHA384:
541 case SSL_SIGN_RSA_PSS_SHA256:
542 case SSL_SIGN_RSA_PKCS1_SHA512:
543 case SSL_SIGN_RSA_PKCS1_SHA384:
544 case SSL_SIGN_RSA_PKCS1_SHA256:
545 case SSL_SIGN_RSA_PKCS1_SHA1:
546 have_rsa = 1;
547 break;
548
549 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
550 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
551 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
552 case SSL_SIGN_ECDSA_SHA1:
553 have_ecdsa = 1;
554 break;
555 }
556 }
557
558 /* Disable auth if we don't include any appropriate signature algorithms. */
559 if (!have_rsa) {
560 *out_mask_a |= SSL_aRSA;
561 }
562 if (!have_ecdsa) {
563 *out_mask_a |= SSL_aECDSA;
564 }
565
566 /* PSK requires a client callback. */
567 if (ssl->psk_client_callback == NULL) {
568 *out_mask_a |= SSL_aPSK;
569 *out_mask_k |= SSL_kPSK;
570 }
571}
572
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200573static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
574 uint16_t min_version,
David Benjamine0ff7672016-09-19 18:40:03 -0400575 uint16_t max_version) {
David Benjamin2fe6e222017-01-25 02:18:43 -0500576 uint32_t mask_a, mask_k;
577 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langley95c29f32014-06-20 12:00:00 -0700578
David Benjamine8d53502015-10-10 14:13:23 -0400579 CBB child;
580 if (!CBB_add_u16_length_prefixed(out, &child)) {
581 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800582 }
583
David Benjamin65ac9972016-09-02 21:35:25 -0400584 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
585 if (ssl->ctx->grease_enabled &&
586 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
587 return 0;
588 }
589
David Benjaminabbbee12016-10-31 19:20:42 -0400590 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
591 * hardware support. */
592 if (max_version >= TLS1_3_VERSION) {
593 if (!EVP_has_aes_hardware() &&
594 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
595 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500596 }
David Benjaminabbbee12016-10-31 19:20:42 -0400597 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
598 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
599 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400600 }
David Benjaminabbbee12016-10-31 19:20:42 -0400601 if (EVP_has_aes_hardware() &&
602 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400603 return 0;
604 }
605 }
606
David Benjaminabbbee12016-10-31 19:20:42 -0400607 if (min_version < TLS1_3_VERSION) {
608 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
609 int any_enabled = 0;
610 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
611 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
612 /* Skip disabled ciphers */
David Benjamin2fe6e222017-01-25 02:18:43 -0500613 if ((cipher->algorithm_mkey & mask_k) ||
614 (cipher->algorithm_auth & mask_a)) {
David Benjaminabbbee12016-10-31 19:20:42 -0400615 continue;
616 }
617 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
618 SSL_CIPHER_get_max_version(cipher) < min_version) {
619 continue;
620 }
621 any_enabled = 1;
622 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
623 return 0;
624 }
625 }
626
627 /* If all ciphers were disabled, return the error to the caller. */
628 if (!any_enabled && max_version < TLS1_3_VERSION) {
629 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
630 return 0;
631 }
David Benjamine8d53502015-10-10 14:13:23 -0400632 }
633
634 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
635 * added. */
David Benjaminf04c2e92016-12-06 13:35:25 -0500636 if (max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400637 !ssl->s3->initial_handshake_complete) {
638 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
639 return 0;
640 }
David Benjamine8d53502015-10-10 14:13:23 -0400641 }
642
David Benjamine0ff7672016-09-19 18:40:03 -0400643 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200644 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
645 return 0;
646 }
David Benjamine8d53502015-10-10 14:13:23 -0400647 }
648
649 return CBB_flush(out);
650}
651
David Benjamin8c880a22016-12-03 02:20:34 -0500652int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
653 SSL *const ssl = hs->ssl;
David Benjamine0ff7672016-09-19 18:40:03 -0400654 uint16_t min_version, max_version;
655 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400656 return 0;
657 }
658
Steven Valdeza833c352016-11-01 13:39:36 -0400659 CBB cbb, body;
660 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
661 goto err;
662 }
663
Steven Valdez5440fe02016-07-18 12:40:30 -0400664 /* Renegotiations do not participate in session resumption. */
665 int has_session = ssl->session != NULL &&
666 !ssl->s3->initial_handshake_complete;
667
668 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500669 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400670 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
671 !CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400672 (has_session &&
673 !CBB_add_bytes(&child, ssl->session->session_id,
674 ssl->session->session_id_length))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400675 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400676 }
677
David Benjamince079fd2016-08-02 16:22:34 -0400678 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400679 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400680 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400681 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400682 }
683 }
684
685 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400686 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdeza833c352016-11-01 13:39:36 -0400687 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
688 !CBB_add_u8(&body, 1 /* one compression method */) ||
689 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500690 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400691 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400692 }
693
Steven Valdeza833c352016-11-01 13:39:36 -0400694 uint8_t *msg = NULL;
695 size_t len;
696 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
697 goto err;
698 }
699
700 /* Now that the length prefixes have been computed, fill in the placeholder
701 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500702 if (hs->needs_psk_binder &&
Steven Valdez908ac192017-01-12 13:17:07 -0500703 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400704 OPENSSL_free(msg);
705 goto err;
706 }
707
David Benjamindaf207a2017-01-03 18:37:41 -0500708 return ssl->method->add_message(ssl, msg, len);
Steven Valdeza833c352016-11-01 13:39:36 -0400709
710 err:
711 CBB_cleanup(&cbb);
712 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400713}
714
David Benjaminc3c88822016-11-14 10:32:04 +0900715static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
716 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400717 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
718 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
Steven Valdez908ac192017-01-12 13:17:07 -0500719 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500720 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
721 return -1;
722 }
723
David Benjaminb6a0a512016-06-21 10:33:21 -0400724 uint16_t min_version, max_version;
725 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400726 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400727 }
728
David Benjaminf04c2e92016-12-06 13:35:25 -0500729 uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
David Benjamincb0c29f2016-12-12 17:00:50 -0500730 assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
David Benjamine8d53502015-10-10 14:13:23 -0400731 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500732 ssl->version = max_wire_version;
733 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400734
David Benjaminf04c2e92016-12-06 13:35:25 -0500735 /* Always advertise the ClientHello version from the original maximum version,
736 * even on renegotiation. The static RSA key exchange uses this field, and
737 * some servers fail when it changes across handshakes. */
738 hs->client_version = max_wire_version;
739 if (max_version >= TLS1_3_VERSION) {
740 hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
David Benjamine8d53502015-10-10 14:13:23 -0400741 }
742
David Benjamine29ea162016-03-23 16:10:44 -0400743 /* If the configured session has expired or was created at a disabled
744 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400745 if (ssl->session != NULL) {
David Benjamin2dc02042016-09-19 19:57:37 -0400746 uint16_t session_version;
Adam Langley364f7a62016-12-12 10:51:00 -0800747 if (ssl->session->is_server ||
748 !ssl->method->version_from_wire(&session_version,
David Benjamin2dc02042016-09-19 19:57:37 -0400749 ssl->session->ssl_version) ||
750 (session_version < TLS1_3_VERSION &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400751 ssl->session->session_id_length == 0) ||
752 ssl->session->not_resumable ||
753 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminb6a0a512016-06-21 10:33:21 -0400754 session_version < min_version || session_version > max_version) {
David Benjamin4d0be242016-09-01 01:10:07 -0400755 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400756 }
David Benjamine8d53502015-10-10 14:13:23 -0400757 }
758
759 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
760 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400761 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400762 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400763 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400764 }
765
David Benjamin8c880a22016-12-03 02:20:34 -0500766 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400767 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400768 }
769
David Benjamin16315f72017-01-12 20:02:05 -0500770 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800771}
Adam Langley95c29f32014-06-20 12:00:00 -0700772
David Benjaminc3c88822016-11-14 10:32:04 +0900773static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
774 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -0700775 int al;
David Benjaminaa7734b2016-06-07 16:40:46 -0400776 CBS hello_verify_request, cookie;
777 uint16_t server_version;
778
David Benjaminf71036e2017-01-21 14:49:39 -0500779 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700780 if (ret <= 0) {
781 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400782 }
783
784 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
785 ssl->d1->send_cookie = 0;
786 ssl->s3->tmp.reuse_message = 1;
787 return 1;
788 }
789
David Benjaminf71036e2017-01-21 14:49:39 -0500790 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400791 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
792 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
793 CBS_len(&hello_verify_request) != 0) {
794 al = SSL_AD_DECODE_ERROR;
795 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
796 goto f_err;
797 }
798
799 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
800 al = SSL_AD_ILLEGAL_PARAMETER;
801 goto f_err;
802 }
803
David Benjamin17cf2cb2016-12-13 01:07:13 -0500804 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400805 ssl->d1->cookie_len = CBS_len(&cookie);
806
807 ssl->d1->send_cookie = 1;
808 return 1;
809
810f_err:
811 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
812 return -1;
813}
814
David Benjaminc3c88822016-11-14 10:32:04 +0900815static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
816 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -0700817 int al = SSL_AD_INTERNAL_ERROR;
Adam Langley1bea1732014-12-17 19:06:57 -0800818 CBS server_hello, server_random, session_id;
David Benjamin2dc02042016-09-19 19:57:37 -0400819 uint16_t server_wire_version, cipher_suite;
Adam Langley1bea1732014-12-17 19:06:57 -0800820 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjaminf71036e2017-01-21 14:49:39 -0500822 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -0700823 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500824 uint32_t err = ERR_peek_error();
825 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
826 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
827 /* Add a dedicated error code to the queue for a handshake_failure alert
828 * in response to ClientHello. This matches NSS's client behavior and
829 * gives a better error on a (probable) failure to negotiate initial
830 * parameters. Note: this error code comes after the original one.
831 *
832 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400833 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500834 }
David Benjamin09eb6552016-07-08 14:32:11 -0700835 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
Steven Valdez143e8b32016-07-11 13:19:03 -0400838 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
839 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
840 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
841 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
842 return -1;
843 }
844
David Benjamin09eb6552016-07-08 14:32:11 -0700845 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
David Benjamina03d95d2014-07-12 19:49:07 -0400846
Steven Valdez143e8b32016-07-11 13:19:03 -0400847 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800848 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400849 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800850 goto f_err;
851 }
David Benjamina03d95d2014-07-12 19:49:07 -0400852
David Benjamin2dc02042016-09-19 19:57:37 -0400853 uint16_t min_version, max_version, server_version;
David Benjamine0ff7672016-09-19 18:40:03 -0400854 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
David Benjamin2dc02042016-09-19 19:57:37 -0400855 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjamin1f61f0d2016-07-10 12:20:35 -0400856 server_version < min_version || server_version > max_version) {
857 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
858 al = SSL_AD_PROTOCOL_VERSION;
859 goto f_err;
860 }
861
David Benjamin0d56f882015-12-19 17:05:56 -0500862 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
863 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400864 ssl->version = server_wire_version;
David Benjamin0d56f882015-12-19 17:05:56 -0500865 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800866 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500867 ssl->s3->have_version = 1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400868 } else if (server_wire_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400869 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800870 al = SSL_AD_PROTOCOL_VERSION;
871 goto f_err;
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Steven Valdez143e8b32016-07-11 13:19:03 -0400874 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500875 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900876 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400877 return 1;
878 }
879
David Benjamin6e4fc332016-11-17 16:43:08 +0900880 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400881
David Benjamin276b7e82017-01-21 14:13:39 -0500882 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400883 return -1;
884 }
885
886 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
887 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
888 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
889 !CBS_get_u16(&server_hello, &cipher_suite) ||
890 !CBS_get_u8(&server_hello, &compression_method)) {
891 al = SSL_AD_DECODE_ERROR;
892 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
893 goto f_err;
894 }
895
Adam Langley1bea1732014-12-17 19:06:57 -0800896 /* Copy over the server random. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500897 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700898
David Benjamin55108632016-08-11 22:01:18 -0400899 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
900 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400901
David Benjamin0d56f882015-12-19 17:05:56 -0500902 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400903 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500904 CBS_mem_equal(&session_id, ssl->session->session_id,
905 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400906 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800907 } else {
908 /* The session wasn't resumed. Create a fresh SSL_SESSION to
909 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400910 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900911 if (!ssl_get_new_session(hs, 0 /* client */)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800912 goto f_err;
913 }
914 /* Note: session_id could be empty. */
David Benjamin45738dd2017-02-09 20:01:26 -0500915 hs->new_session->session_id_length = CBS_len(&session_id);
916 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
917 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800918 }
David Benjamina03d95d2014-07-12 19:49:07 -0400919
David Benjamind2cb1c12016-11-02 17:49:09 -0400920 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800921 if (c == NULL) {
922 /* unknown cipher */
923 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400924 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800925 goto f_err;
926 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400927
928 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin2fe6e222017-01-25 02:18:43 -0500929 uint32_t mask_a, mask_k;
930 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
931 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700932 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400933 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
934 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800935 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400936 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800937 goto f_err;
938 }
Adam Langley95c29f32014-06-20 12:00:00 -0700939
Steven Valdez87eab492016-06-27 16:34:59 -0400940 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500941 if (ssl->session->ssl_version != ssl->version) {
David Benjaminece3de92015-03-16 18:02:20 -0400942 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400943 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400944 goto f_err;
945 }
Steven Valdez803c77a2016-09-06 14:13:43 -0400946 if (ssl->session->cipher != c) {
947 al = SSL_AD_ILLEGAL_PARAMETER;
948 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
949 goto f_err;
950 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400951 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
952 /* This is actually a client application bug. */
953 al = SSL_AD_ILLEGAL_PARAMETER;
954 OPENSSL_PUT_ERROR(SSL,
955 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
956 goto f_err;
957 }
David Benjamin0623bce2015-12-25 15:40:14 -0500958 } else {
David Benjamin45738dd2017-02-09 20:01:26 -0500959 hs->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800960 }
David Benjamin45738dd2017-02-09 20:01:26 -0500961 hs->new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500962
David Benjaminf71036e2017-01-21 14:49:39 -0500963 /* Now that the cipher is known, initialize the handshake hash and hash the
964 * ServerHello. */
Steven Valdez908ac192017-01-12 13:17:07 -0500965 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
966 c->algorithm_prf) ||
967 !ssl_hash_current_message(hs)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400968 goto f_err;
969 }
970
Steven Valdez2b8415e2016-06-30 13:27:23 -0400971 /* If doing a full handshake, the server may request a client certificate
972 * which requires hashing the handshake transcript. Otherwise, the handshake
973 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000974 if (ssl->session != NULL ||
David Benjamin45738dd2017-02-09 20:01:26 -0500975 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdez908ac192017-01-12 13:17:07 -0500976 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -0800977 }
David Benjamina03d95d2014-07-12 19:49:07 -0400978
Adam Langley1bea1732014-12-17 19:06:57 -0800979 /* Only the NULL compression algorithm is supported. */
980 if (compression_method != 0) {
981 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400982 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800983 goto f_err;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langley1bea1732014-12-17 19:06:57 -0800986 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -0500987 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400988 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800989 goto err;
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langley1bea1732014-12-17 19:06:57 -0800992 /* There should be nothing left over in the record. */
993 if (CBS_len(&server_hello) != 0) {
994 /* wrong packet length */
995 al = SSL_AD_DECODE_ERROR;
David Benjamin639846e2016-09-09 11:41:18 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800997 goto f_err;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Steven Valdez87eab492016-06-27 16:34:59 -04001000 if (ssl->session != NULL &&
David Benjaminfc02b592017-02-17 16:26:01 -05001001 hs->extended_master_secret != ssl->session->extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -07001002 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin0d56f882015-12-19 17:05:56 -05001003 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001004 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001005 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001006 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001007 }
1008 goto f_err;
1009 }
1010
Adam Langley1bea1732014-12-17 19:06:57 -08001011 return 1;
1012
Adam Langley95c29f32014-06-20 12:00:00 -07001013f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001014 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001015err:
Adam Langley1bea1732014-12-17 19:06:57 -08001016 return -1;
1017}
Adam Langley95c29f32014-06-20 12:00:00 -07001018
David Benjaminc3c88822016-11-14 10:32:04 +09001019static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1020 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001021 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001022 if (ret <= 0) {
1023 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001024 }
Adam Langley95c29f32014-06-20 12:00:00 -07001025
David Benjaminf71036e2017-01-21 14:49:39 -05001026 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001027 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001028 return -1;
1029 }
1030
David Benjamin5c900c82016-07-13 23:03:26 -04001031 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001032 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001033
Adam Langleyc68e5b92017-02-08 13:33:15 -08001034 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001035 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001036 EVP_PKEY_free(hs->peer_pubkey);
1037 hs->peer_pubkey = NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001038 hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
1039 &cbs, ssl->ctx->pool);
1040 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001041 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001042 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001043 }
Adam Langley95c29f32014-06-20 12:00:00 -07001044
David Benjamin45738dd2017-02-09 20:01:26 -05001045 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
Adam Langley68e71242016-12-12 11:06:16 -08001046 CBS_len(&cbs) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001047 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin3570d732015-06-29 00:28:17 -04001048 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001049 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001050 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001051 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001052
Adam Langley05672202016-12-13 12:05:49 -08001053 if (!ssl_check_leaf_certificate(
David Benjamin45738dd2017-02-09 20:01:26 -05001054 hs, hs->peer_pubkey,
1055 sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001056 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001057 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001058 }
1059
David Benjamina58baaf2017-02-28 20:54:28 -05001060 /* Disallow the server certificate from changing during a renegotiation. See
1061 * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
1062 * so this check is sufficient. */
1063 if (ssl->s3->established_session != NULL) {
1064 if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) !=
1065 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
1066 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1067 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1068 return -1;
1069 }
1070
1071 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
1072 const CRYPTO_BUFFER *old_cert =
1073 sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i);
1074 const CRYPTO_BUFFER *new_cert =
1075 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
1076 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
1077 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
1078 CRYPTO_BUFFER_data(new_cert),
1079 CRYPTO_BUFFER_len(old_cert)) != 0) {
1080 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
1081 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1082 return -1;
1083 }
1084 }
1085 }
1086
David Benjamin5c900c82016-07-13 23:03:26 -04001087 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001088}
1089
David Benjaminc3c88822016-11-14 10:32:04 +09001090static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1091 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001092 int al;
David Benjamin9f1dc822016-06-07 17:03:46 -04001093 CBS certificate_status, ocsp_response;
1094 uint8_t status_type;
1095
David Benjaminf71036e2017-01-21 14:49:39 -05001096 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001097 if (ret <= 0) {
1098 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001099 }
1100
1101 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1102 /* A server may send status_request in ServerHello and then change
1103 * its mind about sending CertificateStatus. */
1104 ssl->s3->tmp.reuse_message = 1;
1105 return 1;
1106 }
1107
Steven Valdez908ac192017-01-12 13:17:07 -05001108 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001109 return -1;
1110 }
1111
David Benjamin09eb6552016-07-08 14:32:11 -07001112 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001113 if (!CBS_get_u8(&certificate_status, &status_type) ||
1114 status_type != TLSEXT_STATUSTYPE_ocsp ||
1115 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1116 CBS_len(&ocsp_response) == 0 ||
1117 CBS_len(&certificate_status) != 0) {
1118 al = SSL_AD_DECODE_ERROR;
1119 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1120 goto f_err;
1121 }
1122
David Benjamin45738dd2017-02-09 20:01:26 -05001123 if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
1124 &hs->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001125 al = SSL_AD_INTERNAL_ERROR;
1126 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1127 goto f_err;
1128 }
1129 return 1;
1130
1131f_err:
1132 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1133 return -1;
1134}
1135
David Benjaminc3c88822016-11-14 10:32:04 +09001136static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1137 SSL *const ssl = hs->ssl;
Adam Langley2a3b3432017-02-10 13:56:48 -08001138 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
David Benjamine455e512016-08-01 20:11:13 -04001139 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001140 }
1141
Adam Langley9498e742016-07-18 10:17:16 -07001142 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001143}
1144
David Benjaminc3c88822016-11-14 10:32:04 +09001145static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1146 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001147 int al;
Adam Langley1bea1732014-12-17 19:06:57 -08001148 DH *dh = NULL;
1149 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001150 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001151
David Benjaminf71036e2017-01-21 14:49:39 -05001152 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001153 if (ret <= 0) {
1154 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001155 }
1156
David Benjamin0d56f882015-12-19 17:05:56 -05001157 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001158 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin45738dd2017-02-09 20:01:26 -05001159 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001160 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001161 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001162 return -1;
1163 }
1164
David Benjamin0d56f882015-12-19 17:05:56 -05001165 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001166 return 1;
1167 }
1168
Steven Valdez908ac192017-01-12 13:17:07 -05001169 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001170 return -1;
1171 }
1172
Adam Langley1bea1732014-12-17 19:06:57 -08001173 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001174 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001175 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001176 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001177
David Benjamin45738dd2017-02-09 20:01:26 -05001178 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1179 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001180
1181 if (alg_a & SSL_aPSK) {
1182 CBS psk_identity_hint;
1183
1184 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1185 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1186 &psk_identity_hint)) {
1187 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001188 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001189 goto f_err;
1190 }
1191
1192 /* Store PSK identity hint for later use, hint is used in
1193 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1194 * identity hint can be as long as the maximum length of a PSK identity.
1195 * Also do not allow NULL characters; identities are saved as C strings.
1196 *
1197 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1198 * a specific identity. */
1199 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1200 CBS_contains_zero_byte(&psk_identity_hint)) {
1201 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001202 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001203 goto f_err;
1204 }
1205
David Benjamin78679342016-09-16 19:42:05 -04001206 /* Save non-empty identity hints as a C string. Empty identity hints we
1207 * treat as missing. Plain PSK makes it possible to send either no hint
1208 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1209 * empty hint. Having different capabilities is odd, so we interpret empty
1210 * and missing as identical. */
1211 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001212 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001213 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001214 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001215 goto f_err;
1216 }
1217 }
1218
David Benjamin7061e282015-03-19 11:10:48 -04001219 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001220 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001221 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1222 CBS_len(&dh_p) == 0 ||
1223 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1224 CBS_len(&dh_g) == 0 ||
1225 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1226 CBS_len(&dh_Ys) == 0) {
1227 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001228 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001229 goto f_err;
1230 }
1231
1232 dh = DH_new();
1233 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001234 goto err;
1235 }
1236
David Benjamin48cce662015-12-17 01:33:08 -05001237 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1238 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001239 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001240 goto err;
1241 }
1242
David Benjamin4882a6c2016-12-11 02:48:12 -05001243 unsigned bits = DH_num_bits(dh);
1244 if (bits < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001245 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001246 goto err;
David Benjamin4882a6c2016-12-11 02:48:12 -05001247 } else if (bits > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001248 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1249 * to prevent a server from causing us to perform too expensive of a
1250 * computation. */
1251 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1252 goto err;
1253 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001254
David Benjaminc3c88822016-11-14 10:32:04 +09001255 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001256 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001257
1258 /* Save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001259 if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001260 goto err;
1261 }
David Benjamin7061e282015-03-19 11:10:48 -04001262 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001263 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001264 uint8_t group_type;
1265 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001266 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001267 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1268 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001269 !CBS_get_u16(&server_key_exchange, &group_id) ||
1270 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001271 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001272 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001273 goto f_err;
1274 }
David Benjamin45738dd2017-02-09 20:01:26 -05001275 hs->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001276
Steven Valdezce902a92016-05-17 11:47:53 -04001277 /* Ensure the group is consistent with preferences. */
1278 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001279 al = SSL_AD_ILLEGAL_PARAMETER;
1280 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001281 goto f_err;
1282 }
David Benjamin4298d772015-12-19 00:18:25 -05001283
Matt Braithwaite053931e2016-05-25 12:06:05 -07001284 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001285 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1286 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001287 goto err;
1288 }
Adam Langley1bea1732014-12-17 19:06:57 -08001289 } else if (!(alg_k & SSL_kPSK)) {
1290 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001291 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001292 goto f_err;
1293 }
1294
1295 /* At this point, |server_key_exchange| contains the signature, if any, while
1296 * |server_key_exchange_orig| contains the entire message. From that, derive
1297 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001298 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001299 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1300 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1301
David Benjaminf4958e72015-06-07 11:00:53 -04001302 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin45738dd2017-02-09 20:01:26 -05001303 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001304 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001305 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001306 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001307 al = SSL_AD_DECODE_ERROR;
1308 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001309 goto f_err;
1310 }
David Benjamin887c3002016-07-08 16:15:32 -07001311 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001312 goto f_err;
1313 }
David Benjamin45738dd2017-02-09 20:01:26 -05001314 hs->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001315 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001316 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001317 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001318 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001319 } else {
1320 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1321 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1322 goto f_err;
Adam Langley1bea1732014-12-17 19:06:57 -08001323 }
1324
1325 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001326 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001327 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1328 CBS_len(&server_key_exchange) != 0) {
1329 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001330 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001331 goto f_err;
1332 }
1333
Steven Valdez2b8415e2016-06-30 13:27:23 -04001334 CBB transcript;
1335 uint8_t *transcript_data;
1336 size_t transcript_len;
1337 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1338 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1339 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1340 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1341 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1342 CBB_cleanup(&transcript);
1343 al = SSL_AD_INTERNAL_ERROR;
1344 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001345 goto f_err;
1346 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001347
1348 int sig_ok = ssl_public_key_verify(
1349 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001350 hs->peer_pubkey, transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001351 OPENSSL_free(transcript_data);
1352
David Benjaminbf82aed2016-03-01 22:57:40 -05001353#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1354 sig_ok = 1;
1355 ERR_clear_error();
1356#endif
1357 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001358 /* bad signature */
1359 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001360 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001361 goto f_err;
1362 }
1363 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001364 /* PSK ciphers are the only supported certificate-less ciphers. */
1365 assert(alg_a == SSL_aPSK);
1366
Adam Langley1bea1732014-12-17 19:06:57 -08001367 if (CBS_len(&server_key_exchange) > 0) {
1368 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001369 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001370 goto f_err;
1371 }
1372 }
Adam Langley1bea1732014-12-17 19:06:57 -08001373 return 1;
1374
Adam Langley95c29f32014-06-20 12:00:00 -07001375f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001376 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001377err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001378 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001379 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001380 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001381 return -1;
1382}
Adam Langley95c29f32014-06-20 12:00:00 -07001383
David Benjaminc3c88822016-11-14 10:32:04 +09001384static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1385 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001386 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001387 if (msg_ret <= 0) {
1388 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001389 }
David Benjamin688d8df2014-11-02 23:06:42 -05001390
David Benjamin3dcec452016-06-07 17:13:28 -04001391 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001392 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001393 /* If we get here we don't need the handshake buffer as we won't be doing
1394 * client auth. */
Steven Valdez908ac192017-01-12 13:17:07 -05001395 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley1bea1732014-12-17 19:06:57 -08001396 return 1;
1397 }
David Benjamined439582014-07-14 19:13:02 -04001398
David Benjaminf71036e2017-01-21 14:49:39 -05001399 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001400 !ssl_hash_current_message(hs)) {
David Benjamine0332e82016-07-13 22:40:36 -04001401 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001402 }
Adam Langley95c29f32014-06-20 12:00:00 -07001403
David Benjamina5177cb2016-03-11 19:38:17 -05001404 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001405 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001406
David Benjamine0332e82016-07-13 22:40:36 -04001407 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001408 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001409 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001410 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001411 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001412 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001413 }
David Benjamined439582014-07-14 19:13:02 -04001414
David Benjaminc3c88822016-11-14 10:32:04 +09001415 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1416 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001417 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001418 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001419 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001420
David Benjamina1e9cab2015-12-30 00:08:49 -05001421 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001422 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001423 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001424 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001425 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001426 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001427 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001428 }
Adam Langley1bea1732014-12-17 19:06:57 -08001429 }
David Benjamined439582014-07-14 19:13:02 -04001430
Adam Langleyc68e5b92017-02-08 13:33:15 -08001431 uint8_t alert = SSL_AD_DECODE_ERROR;
Adam Langley34b4c822017-02-02 10:57:17 -08001432 STACK_OF(CRYPTO_BUFFER) *ca_names =
1433 ssl_parse_client_CA_list(ssl, &alert, &cbs);
1434 if (ca_names == NULL) {
David Benjamine0332e82016-07-13 22:40:36 -04001435 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1436 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin639846e2016-09-09 11:41:18 -04001439 if (CBS_len(&cbs) != 0) {
1440 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley34b4c822017-02-02 10:57:17 -08001441 sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free);
David Benjamin639846e2016-09-09 11:41:18 -04001442 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1443 return -1;
1444 }
1445
David Benjaminc3c88822016-11-14 10:32:04 +09001446 hs->cert_request = 1;
Adam Langley34b4c822017-02-02 10:57:17 -08001447 sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free);
1448 hs->ca_names = ca_names;
1449 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
David Benjamine0332e82016-07-13 22:40:36 -04001450 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001451}
Adam Langley95c29f32014-06-20 12:00:00 -07001452
David Benjaminc3c88822016-11-14 10:32:04 +09001453static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1454 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001455 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001456 if (ret <= 0) {
1457 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001458 }
David Benjamin46062682014-07-14 19:14:32 -04001459
David Benjaminf71036e2017-01-21 14:49:39 -05001460 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001461 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001462 return -1;
1463 }
1464
David Benjamin09eb6552016-07-08 14:32:11 -07001465 /* ServerHelloDone is empty. */
1466 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001467 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001468 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001469 return -1;
1470 }
David Benjamin46062682014-07-14 19:14:32 -04001471
Adam Langley1bea1732014-12-17 19:06:57 -08001472 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001473}
Adam Langley1258b6a2014-06-20 12:00:00 -07001474
David Benjaminc3c88822016-11-14 10:32:04 +09001475static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1476 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001477 /* Call cert_cb to update the certificate. */
1478 if (ssl->cert->cert_cb) {
1479 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1480 if (ret < 0) {
1481 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001482 return -1;
1483 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001484 if (ret == 0) {
1485 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1486 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001487 return -1;
1488 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001489 }
1490
David Benjamin5edfc8c2016-12-10 15:46:58 -05001491 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001492 /* Without a client certificate, the handshake buffer may be released. */
Steven Valdez908ac192017-01-12 13:17:07 -05001493 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin5edfc8c2016-12-10 15:46:58 -05001494
David Benjamindaf207a2017-01-03 18:37:41 -05001495 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001496 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001497 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1498 SSL_AD_NO_CERTIFICATE)) {
1499 return -1;
1500 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001501 return 1;
1502 }
1503 }
1504
Adam Langley94a62e62017-02-10 15:24:56 -08001505 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(ssl) ||
David Benjamin650aa1c2016-12-20 18:55:16 -05001506 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001507 return -1;
1508 }
David Benjamin16315f72017-01-12 20:02:05 -05001509 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001510}
1511
Brian Smith274341d2015-10-08 17:10:15 -10001512OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1513 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001514
David Benjaminc3c88822016-11-14 10:32:04 +09001515static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1516 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -08001517 uint8_t *pms = NULL;
1518 size_t pms_len = 0;
David Benjamin75836432016-06-17 18:48:29 -04001519 CBB cbb, body;
1520 if (!ssl->method->init_message(ssl, &cbb, &body,
1521 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001522 goto err;
1523 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001524
David Benjamin45738dd2017-02-09 20:01:26 -05001525 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1526 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001527
David Benjaminae0eaaa2015-12-17 02:01:25 -05001528 /* If using a PSK key exchange, prepare the pre-shared key. */
1529 unsigned psk_len = 0;
1530 uint8_t psk[PSK_MAX_PSK_LEN];
1531 if (alg_a & SSL_aPSK) {
1532 if (ssl->psk_client_callback == NULL) {
1533 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1534 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001535 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001536
David Benjaminae0eaaa2015-12-17 02:01:25 -05001537 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001538 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001539 psk_len =
1540 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1541 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001542 if (psk_len == 0) {
1543 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1544 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1545 goto err;
1546 }
1547 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001548
David Benjamin45738dd2017-02-09 20:01:26 -05001549 OPENSSL_free(hs->new_session->psk_identity);
1550 hs->new_session->psk_identity = BUF_strdup(identity);
1551 if (hs->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001552 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1553 goto err;
1554 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001555
David Benjaminae0eaaa2015-12-17 02:01:25 -05001556 /* Write out psk_identity. */
1557 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001558 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001559 !CBB_add_bytes(&child, (const uint8_t *)identity,
1560 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001561 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001562 goto err;
1563 }
1564 }
David Benjamin758d1272015-11-20 17:47:25 -05001565
David Benjaminae0eaaa2015-12-17 02:01:25 -05001566 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1567 if (alg_k & SSL_kRSA) {
1568 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1569 pms = OPENSSL_malloc(pms_len);
1570 if (pms == NULL) {
1571 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1572 goto err;
1573 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001574
Adam Langleyd5157222016-12-12 11:37:43 -08001575 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001576 if (rsa == NULL) {
1577 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001578 goto err;
1579 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001580
David Benjaminf04c2e92016-12-06 13:35:25 -05001581 pms[0] = hs->client_version >> 8;
1582 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001583 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1584 goto err;
1585 }
1586
David Benjamin75836432016-06-17 18:48:29 -04001587 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001588 size_t enc_pms_len;
1589 /* In TLS, there is a length prefix. */
1590 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001591 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001592 goto err;
1593 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001594 enc_pms = &child;
1595 }
Adam Langley1bea1732014-12-17 19:06:57 -08001596
David Benjaminae0eaaa2015-12-17 02:01:25 -05001597 uint8_t *ptr;
1598 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1599 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1600 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001601 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001602 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001603 goto err;
1604 }
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001605 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001606 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001607 CBB child;
David Benjaminc3c88822016-11-14 10:32:04 +09001608 if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001609 goto err;
1610 }
1611
David Benjamin4298d772015-12-19 00:18:25 -05001612 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001613 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001614 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1615 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001616 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1617 goto err;
1618 }
David Benjamin75836432016-06-17 18:48:29 -04001619 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001620 goto err;
1621 }
David Benjamin4298d772015-12-19 00:18:25 -05001622
1623 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001624 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1625 OPENSSL_free(hs->peer_key);
1626 hs->peer_key = NULL;
1627 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001628 } else if (alg_k & SSL_kPSK) {
1629 /* For plain PSK, other_secret is a block of 0s with the same length as
1630 * the pre-shared key. */
1631 pms_len = psk_len;
1632 pms = OPENSSL_malloc(pms_len);
1633 if (pms == NULL) {
1634 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1635 goto err;
1636 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001637 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001638 } else {
1639 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1640 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1641 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001642 }
1643
David Benjaminae0eaaa2015-12-17 02:01:25 -05001644 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1645 * key. */
1646 if (alg_a & SSL_aPSK) {
1647 CBB pms_cbb, child;
1648 uint8_t *new_pms;
1649 size_t new_pms_len;
1650
1651 CBB_zero(&pms_cbb);
1652 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1653 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1654 !CBB_add_bytes(&child, pms, pms_len) ||
1655 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1656 !CBB_add_bytes(&child, psk, psk_len) ||
1657 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1658 CBB_cleanup(&pms_cbb);
1659 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1660 goto err;
1661 }
1662 OPENSSL_cleanse(pms, pms_len);
1663 OPENSSL_free(pms);
1664 pms = new_pms;
1665 pms_len = new_pms_len;
1666 }
1667
1668 /* The message must be added to the finished hash before calculating the
1669 * master secret. */
David Benjamindaf207a2017-01-03 18:37:41 -05001670 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001671 goto err;
1672 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001673
David Benjamin45738dd2017-02-09 20:01:26 -05001674 hs->new_session->master_key_length = tls1_generate_master_secret(
1675 hs, hs->new_session->master_key, pms, pms_len);
1676 if (hs->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001677 goto err;
1678 }
David Benjaminfc02b592017-02-17 16:26:01 -05001679 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001680 OPENSSL_cleanse(pms, pms_len);
1681 OPENSSL_free(pms);
1682
David Benjamin16315f72017-01-12 20:02:05 -05001683 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001684
1685err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001686 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001687 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001688 OPENSSL_cleanse(pms, pms_len);
1689 OPENSSL_free(pms);
1690 }
1691 return -1;
1692}
Adam Langley1258b6a2014-06-20 12:00:00 -07001693
David Benjaminc3c88822016-11-14 10:32:04 +09001694static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1695 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001696 assert(ssl_has_private_key(ssl));
1697
David Benjamin75836432016-06-17 18:48:29 -04001698 CBB cbb, body, child;
1699 if (!ssl->method->init_message(ssl, &cbb, &body,
1700 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjaminfc825122015-12-18 01:57:43 -05001701 goto err;
1702 }
1703
David Benjaminea9a0d52016-07-08 15:52:59 -07001704 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001705 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001706 goto err;
1707 }
David Benjamin429fdc02016-06-17 17:33:30 -04001708 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001709 /* Write out the digest type in TLS 1.2. */
1710 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001711 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1712 goto err;
1713 }
1714 }
David Benjaminfc825122015-12-18 01:57:43 -05001715
David Benjamin429fdc02016-06-17 17:33:30 -04001716 /* Set aside space for the signature. */
David Benjaminfc825122015-12-18 01:57:43 -05001717 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjamin429fdc02016-06-17 17:33:30 -04001718 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001719 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001720 !CBB_reserve(&child, &ptr, max_sig_len)) {
1721 goto err;
1722 }
1723
Steven Valdez2b8415e2016-06-30 13:27:23 -04001724 size_t sig_len = max_sig_len;
David Benjaminfc825122015-12-18 01:57:43 -05001725 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001726 if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001727 /* The SSL3 construction for CertificateVerify does not decompose into a
1728 * single final digest and signature, and must be special-cased. */
1729 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1730 if (ssl->cert->key_method != NULL) {
1731 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1732 goto err;
1733 }
1734
1735 uint8_t digest[EVP_MAX_MD_SIZE];
1736 size_t digest_len;
David Benjamin45738dd2017-02-09 20:01:26 -05001737 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
1738 &digest_len, hs->new_session,
1739 signature_algorithm)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001740 goto err;
1741 }
1742
1743 sign_result = ssl_private_key_success;
1744
1745 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1746 if (pctx == NULL ||
1747 !EVP_PKEY_sign_init(pctx) ||
1748 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1749 EVP_PKEY_CTX_free(pctx);
1750 sign_result = ssl_private_key_failure;
1751 goto err;
1752 }
1753 EVP_PKEY_CTX_free(pctx);
1754 } else {
1755 sign_result = ssl_private_key_sign(
1756 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001757 (const uint8_t *)hs->transcript.buffer->data,
1758 hs->transcript.buffer->length);
David Benjaminfc825122015-12-18 01:57:43 -05001759 }
1760
1761 /* The handshake buffer is no longer necessary. */
Steven Valdez908ac192017-01-12 13:17:07 -05001762 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjaminfc825122015-12-18 01:57:43 -05001763 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001764 assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjamind3440b42016-07-14 14:52:41 -04001765 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
David Benjaminfc825122015-12-18 01:57:43 -05001766 }
1767
1768 switch (sign_result) {
1769 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001770 break;
1771 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001772 goto err;
1773 case ssl_private_key_retry:
1774 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001775 hs->state = SSL3_ST_CW_CERT_VRFY_B;
David Benjaminfc825122015-12-18 01:57:43 -05001776 goto err;
1777 }
1778
David Benjaminfc825122015-12-18 01:57:43 -05001779 if (!CBB_did_write(&child, sig_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001780 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminfc825122015-12-18 01:57:43 -05001781 goto err;
1782 }
1783
David Benjamin16315f72017-01-12 20:02:05 -05001784 return 1;
David Benjaminfc825122015-12-18 01:57:43 -05001785
1786err:
1787 CBB_cleanup(&cbb);
1788 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001789}
1790
David Benjaminc3c88822016-11-14 10:32:04 +09001791static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1792 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001793 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001794 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001795
David Benjamin75836432016-06-17 18:48:29 -04001796 CBB cbb, body, child;
1797 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1798 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001799 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1800 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001801 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001802 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001803 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001804 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1805 CBB_cleanup(&cbb);
1806 return -1;
1807 }
1808
David Benjamin16315f72017-01-12 20:02:05 -05001809 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001810}
1811
David Benjaminc3c88822016-11-14 10:32:04 +09001812static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1813 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001814 if (!ssl_do_channel_id_callback(ssl)) {
1815 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001816 }
1817
David Benjamin96e97b12015-10-10 16:30:22 -04001818 if (ssl->tlsext_channel_id_private == NULL) {
1819 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001820 return -1;
1821 }
Adam Langley1bea1732014-12-17 19:06:57 -08001822
Nick Harper60a85cb2016-09-23 16:25:11 -07001823 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001824 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001825 !tls1_write_channel_id(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001826 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001827 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001828 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001829 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001830 }
1831
David Benjamin16315f72017-01-12 20:02:05 -05001832 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001833}
1834
David Benjaminc3c88822016-11-14 10:32:04 +09001835static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1836 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001837 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001838 if (ret <= 0) {
1839 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001840 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001841
David Benjaminf71036e2017-01-21 14:49:39 -05001842 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001843 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001844 return -1;
1845 }
1846
David Benjamin9f1dc822016-06-07 17:03:46 -04001847 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001848 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001849 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001850 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001851 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1852 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001853 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001854 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001855 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001856 }
1857
David Benjamin9f1dc822016-06-07 17:03:46 -04001858 if (CBS_len(&ticket) == 0) {
1859 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001860 * negotiating the extension. The value of |ticket_expected| is checked in
1861 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001862 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001863 return 1;
1864 }
1865
Steven Valdez87eab492016-06-27 16:34:59 -04001866 int session_renewed = ssl->session != NULL;
David Benjamin45738dd2017-02-09 20:01:26 -05001867 SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001868 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001869 /* The server is sending a new ticket for an existing session. Sessions are
1870 * immutable once established, so duplicate all but the ticket of the
1871 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001872 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001873 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001874 /* This should never happen. */
1875 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1876 goto err;
1877 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001878 }
1879
David Benjamin123db572016-11-03 16:59:25 -04001880 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
David Benjamin17b30832017-01-28 14:00:32 -05001881 ssl_session_rebase_time(ssl, session);
David Benjamin123db572016-11-03 16:59:25 -04001882
Steven Valdez87eab492016-06-27 16:34:59 -04001883 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001884 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1885 goto err;
1886 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001887 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001888
1889 /* Generate a session ID for this session based on the session ticket. We use
1890 * the session ID mechanism for detecting ticket resumption. This also fits in
1891 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001892 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1893 session->session_id, &session->session_id_length,
1894 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001895 goto err;
1896 }
1897
Steven Valdez87eab492016-06-27 16:34:59 -04001898 if (session_renewed) {
1899 session->not_resumable = 0;
1900 SSL_SESSION_free(ssl->session);
1901 ssl->session = session;
1902 }
1903
David Benjamin454aa4c2016-03-10 08:43:27 -05001904 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001905
David Benjamin9f1dc822016-06-07 17:03:46 -04001906err:
Steven Valdez87eab492016-06-27 16:34:59 -04001907 if (session_renewed) {
1908 SSL_SESSION_free(session);
1909 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001910 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001911}