blob: 59fefc4a78d775f3475015e283319649ad492391 [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 Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjaminc3c88822016-11-14 10:32:04 +0900173static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
174static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
175static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
176static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
178static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
179static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
180static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
181static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
182static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
183static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
184static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
185static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
186static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
187static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400188
David Benjamince8c9d22016-11-14 10:45:16 +0900189int ssl3_connect(SSL_HANDSHAKE *hs) {
190 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800191 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400192 int state, skip = 0;
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 Benjamincb0c29f2016-12-12 17:00:50 -0500198 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 Benjamincb0c29f2016-12-12 17:00:50 -0500202 hs->state = SSL_ST_CONNECT;
David Benjaminfc0c9d92016-09-01 01:33:33 -0400203 skip = 1;
204 break;
205
Adam Langley1bea1732014-12-17 19:06:57 -0800206 case SSL_ST_CONNECT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400207 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjaminb095f0f2016-05-05 21:50:24 -0400209 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800210 ret = -1;
211 goto end;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamincb0c29f2016-12-12 17:00:50 -0500214 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800215 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langley1bea1732014-12-17 19:06:57 -0800217 case SSL3_ST_CW_CLNT_HELLO_A:
218 case SSL3_ST_CW_CLNT_HELLO_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900219 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800220 if (ret <= 0) {
221 goto end;
222 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400223
David Benjamince079fd2016-08-02 16:22:34 -0400224 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500225 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400226 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500227 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400228 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500229 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800230 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700231
David Benjaminaa7734b2016-06-07 16:40:46 -0400232 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400233 assert(SSL_is_dtls(ssl));
David Benjaminc3c88822016-11-14 10:32:04 +0900234 ret = dtls1_get_hello_verify(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400235 if (ret <= 0) {
236 goto end;
237 }
238 if (ssl->d1->send_cookie) {
239 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500240 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400241 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500242 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400243 }
244 break;
245
Adam Langley1bea1732014-12-17 19:06:57 -0800246 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900247 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500248 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400249 break;
250 }
Adam Langley1bea1732014-12-17 19:06:57 -0800251 if (ret <= 0) {
252 goto end;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Steven Valdez87eab492016-06-27 16:34:59 -0400255 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500256 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800257 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500258 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800259 }
Adam Langley1bea1732014-12-17 19:06:57 -0800260 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400261
Adam Langley1bea1732014-12-17 19:06:57 -0800262 case SSL3_ST_CR_CERT_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000263 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900264 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800265 if (ret <= 0) {
266 goto end;
267 }
Adam Langley1bea1732014-12-17 19:06:57 -0800268 } else {
269 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800270 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500271 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800272 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamin9f1dc822016-06-07 17:03:46 -0400274 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900275 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900276 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400277 if (ret <= 0) {
278 goto end;
279 }
280 } else {
281 skip = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400282 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500283 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400284 break;
285
Paul Lietar8f1c2682015-08-18 12:21:54 +0100286 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin3d458dc2016-09-12 22:40:27 +0000287 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900288 ret = ssl3_verify_server_cert(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400289 if (ret <= 0) {
290 goto end;
291 }
292 } else {
293 skip = 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100294 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500295 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100296 break;
297
Adam Langley1bea1732014-12-17 19:06:57 -0800298 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900299 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800300 if (ret <= 0) {
301 goto end;
302 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500303 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800304 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley1bea1732014-12-17 19:06:57 -0800306 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000307 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900308 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400309 if (ret <= 0) {
310 goto end;
311 }
312 } else {
313 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800314 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500315 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800316 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley1bea1732014-12-17 19:06:57 -0800318 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900319 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800320 if (ret <= 0) {
321 goto end;
322 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400323 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500324 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley1bea1732014-12-17 19:06:57 -0800327 case SSL3_ST_CW_CERT_A:
328 case SSL3_ST_CW_CERT_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900329 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900330 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400331 if (ret <= 0) {
332 goto end;
333 }
334 } else {
335 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800336 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500337 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800338 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley1bea1732014-12-17 19:06:57 -0800340 case SSL3_ST_CW_KEY_EXCH_A:
341 case SSL3_ST_CW_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900342 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800343 if (ret <= 0) {
344 goto end;
345 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500346 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800347 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langley1bea1732014-12-17 19:06:57 -0800349 case SSL3_ST_CW_CERT_VRFY_A:
350 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400351 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamince8c9d22016-11-14 10:45:16 +0900352 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900353 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400354 if (ret <= 0) {
355 goto end;
356 }
357 } else {
358 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800359 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500360 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800361 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
David Benjamin352d0a92016-06-28 11:22:02 -0400363 case SSL3_ST_CW_CHANGE:
364 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800365 if (ret <= 0) {
366 goto end;
367 }
Adam Langley95c29f32014-06-20 12:00:00 -0700368
David Benjamincb0c29f2016-12-12 17:00:50 -0500369 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700370
David Benjamin67739722016-11-17 17:03:59 +0900371 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800372 ret = -1;
373 goto end;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley1bea1732014-12-17 19:06:57 -0800376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langley1bea1732014-12-17 19:06:57 -0800378 case SSL3_ST_CW_NEXT_PROTO_A:
379 case SSL3_ST_CW_NEXT_PROTO_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900380 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900381 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400382 if (ret <= 0) {
383 goto end;
384 }
Adam Langley1bea1732014-12-17 19:06:57 -0800385 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400386 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800387 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500388 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800389 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langley1bea1732014-12-17 19:06:57 -0800391 case SSL3_ST_CW_CHANNEL_ID_A:
392 case SSL3_ST_CW_CHANNEL_ID_B:
David Benjamin47749a62016-06-13 13:56:33 -0400393 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900394 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400395 if (ret <= 0) {
396 goto end;
397 }
398 } else {
399 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800400 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500401 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800402 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley1bea1732014-12-17 19:06:57 -0800404 case SSL3_ST_CW_FINISHED_A:
405 case SSL3_ST_CW_FINISHED_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900406 ret = ssl3_send_finished(hs, SSL3_ST_CW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500407 SSL3_ST_CW_FINISHED_B);
Adam Langley1bea1732014-12-17 19:06:57 -0800408 if (ret <= 0) {
409 goto end;
410 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500411 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Steven Valdez87eab492016-06-27 16:34:59 -0400413 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500414 hs->next_state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800415 } else {
416 /* This is a non-resumption handshake. If it involves ChannelID, then
417 * record the handshake hashes at this point in the session so that
418 * any resumption of this session with ChannelID can sign those
419 * hashes. */
David Benjamin0d56f882015-12-19 17:05:56 -0500420 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langley49c7af12015-07-10 14:33:46 -0700421 if (ret <= 0) {
422 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800423 }
David Benjamin0d56f882015-12-19 17:05:56 -0500424 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
425 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500426 /* No False Start on renegotiation (would complicate the state
427 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500428 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500429 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800430 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500431 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800432 }
433 }
Adam Langley1bea1732014-12-17 19:06:57 -0800434 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700435
David Benjamin9f1dc822016-06-07 17:03:46 -0400436 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500437 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900438 hs->in_false_start = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400439
440 ssl_free_wbio_buffer(ssl);
441 ret = 1;
442 goto end;
443
Adam Langley1bea1732014-12-17 19:06:57 -0800444 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900445 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900446 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400447 if (ret <= 0) {
448 goto end;
449 }
450 } else {
451 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800452 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500453 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800454 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langley1bea1732014-12-17 19:06:57 -0800456 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400457 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500458 if (ret <= 0) {
459 goto end;
460 }
461
David Benjamin67739722016-11-17 17:03:59 +0900462 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800463 ret = -1;
464 goto end;
465 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500466 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800467 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langley1bea1732014-12-17 19:06:57 -0800469 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900470 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800471 if (ret <= 0) {
472 goto end;
473 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400474 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Steven Valdez87eab492016-06-27 16:34:59 -0400476 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500477 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800478 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500479 hs->state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800480 }
Adam Langley1bea1732014-12-17 19:06:57 -0800481 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700482
Adam Langley1bea1732014-12-17 19:06:57 -0800483 case SSL3_ST_CW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500484 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500485 ssl->rwstate = SSL_WRITING;
Adam Langley1bea1732014-12-17 19:06:57 -0800486 ret = -1;
487 goto end;
488 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500489 hs->state = hs->next_state;
490 if (hs->state != SSL_ST_OK) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400491 ssl->method->expect_flight(ssl);
492 }
Adam Langley1bea1732014-12-17 19:06:57 -0800493 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700494
Steven Valdez143e8b32016-07-11 13:19:03 -0400495 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900496 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400497 if (ret <= 0) {
498 goto end;
499 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500500 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400501 break;
502
Adam Langley1bea1732014-12-17 19:06:57 -0800503 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400504 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Steven Valdez87eab492016-06-27 16:34:59 -0400506 SSL_SESSION_free(ssl->s3->established_session);
507 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400508 SSL_SESSION_up_ref(ssl->session);
509 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400510 } else {
511 /* We make a copy of the session in order to maintain the immutability
512 * of the new established_session due to False Start. The caller may
513 * have taken a reference to the temporary session. */
514 ssl->s3->established_session =
Steven Valdez4aa154e2016-07-29 14:32:55 -0400515 SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400516 if (ssl->s3->established_session == NULL) {
517 /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
518 * callers. */
David Benjamincb0c29f2016-12-12 17:00:50 -0500519 hs->state = SSL_ST_ERROR;
Steven Valdez87eab492016-06-27 16:34:59 -0400520 skip = 1;
521 ret = -1;
522 goto end;
523 }
524 ssl->s3->established_session->not_resumable = 0;
525
526 SSL_SESSION_free(ssl->s3->new_session);
527 ssl->s3->new_session = NULL;
528 }
529
Adam Langley1bea1732014-12-17 19:06:57 -0800530 /* Remove write buffering now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500531 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin0d56f882015-12-19 17:05:56 -0500533 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500534 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400535 if (is_initial_handshake) {
536 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900537 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400538 }
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langley1bea1732014-12-17 19:06:57 -0800540 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400541 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800542 goto end;
543
Steven Valdez87eab492016-06-27 16:34:59 -0400544 case SSL_ST_ERROR:
545 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
546 ret = -1;
547 goto end;
548
Adam Langley1bea1732014-12-17 19:06:57 -0800549 default:
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800551 ret = -1;
552 goto end;
553 }
554
David Benjamincb0c29f2016-12-12 17:00:50 -0500555 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
556 int new_state = hs->state;
557 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400558 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500559 hs->state = new_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800560 }
561 skip = 0;
562 }
563
Adam Langley95c29f32014-06-20 12:00:00 -0700564end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400565 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800566 return ret;
567}
Adam Langley95c29f32014-06-20 12:00:00 -0700568
David Benjamin65ac9972016-09-02 21:35:25 -0400569uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
570 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
571 * a single byte repeatedly and ensures the values are deterministic. This
572 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
573 * same group be advertised in both supported_groups and key_shares. */
574 uint16_t ret = ssl->s3->client_random[index];
575 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
576 ret = (ret & 0xf0) | 0x0a;
577 ret |= ret << 8;
578 return ret;
579}
580
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200581static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
582 uint16_t min_version,
David Benjamine0ff7672016-09-19 18:40:03 -0400583 uint16_t max_version) {
David Benjamine8d53502015-10-10 14:13:23 -0400584 /* Prepare disabled cipher masks. */
585 ssl_set_client_disabled(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700586
David Benjamine8d53502015-10-10 14:13:23 -0400587 CBB child;
588 if (!CBB_add_u16_length_prefixed(out, &child)) {
589 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800590 }
591
David Benjamin65ac9972016-09-02 21:35:25 -0400592 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
593 if (ssl->ctx->grease_enabled &&
594 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
595 return 0;
596 }
597
David Benjaminabbbee12016-10-31 19:20:42 -0400598 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
599 * hardware support. */
600 if (max_version >= TLS1_3_VERSION) {
601 if (!EVP_has_aes_hardware() &&
602 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
603 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500604 }
David Benjaminabbbee12016-10-31 19:20:42 -0400605 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
606 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
607 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400608 }
David Benjaminabbbee12016-10-31 19:20:42 -0400609 if (EVP_has_aes_hardware() &&
610 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400611 return 0;
612 }
613 }
614
David Benjaminabbbee12016-10-31 19:20:42 -0400615 if (min_version < TLS1_3_VERSION) {
616 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
617 int any_enabled = 0;
618 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
619 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
620 /* Skip disabled ciphers */
621 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
622 (cipher->algorithm_auth & ssl->cert->mask_a)) {
623 continue;
624 }
625 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
626 SSL_CIPHER_get_max_version(cipher) < min_version) {
627 continue;
628 }
629 any_enabled = 1;
630 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
631 return 0;
632 }
633 }
634
635 /* If all ciphers were disabled, return the error to the caller. */
636 if (!any_enabled && max_version < TLS1_3_VERSION) {
637 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
638 return 0;
639 }
David Benjamine8d53502015-10-10 14:13:23 -0400640 }
641
642 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
643 * added. */
David Benjaminf04c2e92016-12-06 13:35:25 -0500644 if (max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400645 !ssl->s3->initial_handshake_complete) {
646 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
647 return 0;
648 }
David Benjamine8d53502015-10-10 14:13:23 -0400649 }
650
David Benjamine0ff7672016-09-19 18:40:03 -0400651 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200652 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
653 return 0;
654 }
David Benjamine8d53502015-10-10 14:13:23 -0400655 }
656
657 return CBB_flush(out);
658}
659
David Benjamin8c880a22016-12-03 02:20:34 -0500660int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
661 SSL *const ssl = hs->ssl;
David Benjamine0ff7672016-09-19 18:40:03 -0400662 uint16_t min_version, max_version;
663 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400664 return 0;
665 }
666
Steven Valdeza833c352016-11-01 13:39:36 -0400667 CBB cbb, body;
668 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
669 goto err;
670 }
671
Steven Valdez5440fe02016-07-18 12:40:30 -0400672 /* Renegotiations do not participate in session resumption. */
673 int has_session = ssl->session != NULL &&
674 !ssl->s3->initial_handshake_complete;
675
676 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500677 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400678 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
679 !CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400680 (has_session &&
681 !CBB_add_bytes(&child, ssl->session->session_id,
682 ssl->session->session_id_length))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400683 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400684 }
685
David Benjamince079fd2016-08-02 16:22:34 -0400686 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400687 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400688 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400689 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400690 }
691 }
692
693 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400694 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdeza833c352016-11-01 13:39:36 -0400695 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
696 !CBB_add_u8(&body, 1 /* one compression method */) ||
697 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500698 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400699 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400700 }
701
Steven Valdeza833c352016-11-01 13:39:36 -0400702 uint8_t *msg = NULL;
703 size_t len;
704 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
705 goto err;
706 }
707
708 /* Now that the length prefixes have been computed, fill in the placeholder
709 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500710 if (hs->needs_psk_binder &&
Steven Valdeza833c352016-11-01 13:39:36 -0400711 !tls13_write_psk_binder(ssl, msg, len)) {
712 OPENSSL_free(msg);
713 goto err;
714 }
715
716 return ssl->method->queue_message(ssl, msg, len);
717
718 err:
719 CBB_cleanup(&cbb);
720 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400721}
722
David Benjaminc3c88822016-11-14 10:32:04 +0900723static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
724 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -0500725 if (hs->state == SSL3_ST_CW_CLNT_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -0400726 return ssl->method->write_message(ssl);
David Benjamine8d53502015-10-10 14:13:23 -0400727 }
728
David Benjaminaa7734b2016-06-07 16:40:46 -0400729 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
730 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
731 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500732 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
733 return -1;
734 }
735
David Benjaminb6a0a512016-06-21 10:33:21 -0400736 uint16_t min_version, max_version;
737 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400738 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400739 }
740
David Benjaminf04c2e92016-12-06 13:35:25 -0500741 uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
David Benjamincb0c29f2016-12-12 17:00:50 -0500742 assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
David Benjamine8d53502015-10-10 14:13:23 -0400743 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500744 ssl->version = max_wire_version;
745 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400746
David Benjaminf04c2e92016-12-06 13:35:25 -0500747 /* Always advertise the ClientHello version from the original maximum version,
748 * even on renegotiation. The static RSA key exchange uses this field, and
749 * some servers fail when it changes across handshakes. */
750 hs->client_version = max_wire_version;
751 if (max_version >= TLS1_3_VERSION) {
752 hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
David Benjamine8d53502015-10-10 14:13:23 -0400753 }
754
David Benjamine29ea162016-03-23 16:10:44 -0400755 /* If the configured session has expired or was created at a disabled
756 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400757 if (ssl->session != NULL) {
David Benjamin2dc02042016-09-19 19:57:37 -0400758 uint16_t session_version;
Adam Langley364f7a62016-12-12 10:51:00 -0800759 if (ssl->session->is_server ||
760 !ssl->method->version_from_wire(&session_version,
David Benjamin2dc02042016-09-19 19:57:37 -0400761 ssl->session->ssl_version) ||
762 (session_version < TLS1_3_VERSION &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400763 ssl->session->session_id_length == 0) ||
764 ssl->session->not_resumable ||
765 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminb6a0a512016-06-21 10:33:21 -0400766 session_version < min_version || session_version > max_version) {
David Benjamin4d0be242016-09-01 01:10:07 -0400767 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400768 }
David Benjamine8d53502015-10-10 14:13:23 -0400769 }
770
771 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
772 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400773 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400774 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400775 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400776 }
777
David Benjamin8c880a22016-12-03 02:20:34 -0500778 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400779 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400780 }
781
David Benjamincb0c29f2016-12-12 17:00:50 -0500782 hs->state = SSL3_ST_CW_CLNT_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -0400783 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800784}
Adam Langley95c29f32014-06-20 12:00:00 -0700785
David Benjaminc3c88822016-11-14 10:32:04 +0900786static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
787 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -0700788 int al;
David Benjaminaa7734b2016-06-07 16:40:46 -0400789 CBS hello_verify_request, cookie;
790 uint16_t server_version;
791
David Benjamin09eb6552016-07-08 14:32:11 -0700792 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
793 if (ret <= 0) {
794 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400795 }
796
797 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
798 ssl->d1->send_cookie = 0;
799 ssl->s3->tmp.reuse_message = 1;
800 return 1;
801 }
802
David Benjamin09eb6552016-07-08 14:32:11 -0700803 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400804
805 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
806 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
807 CBS_len(&hello_verify_request) != 0) {
808 al = SSL_AD_DECODE_ERROR;
809 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
810 goto f_err;
811 }
812
813 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
814 al = SSL_AD_ILLEGAL_PARAMETER;
815 goto f_err;
816 }
817
818 memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
819 ssl->d1->cookie_len = CBS_len(&cookie);
820
821 ssl->d1->send_cookie = 1;
822 return 1;
823
824f_err:
825 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
826 return -1;
827}
828
David Benjaminc3c88822016-11-14 10:32:04 +0900829static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
830 SSL *const ssl = hs->ssl;
David Benjamin0d56f882015-12-19 17:05:56 -0500831 CERT *ct = ssl->cert;
David Benjamin09eb6552016-07-08 14:32:11 -0700832 int al = SSL_AD_INTERNAL_ERROR;
Adam Langley1bea1732014-12-17 19:06:57 -0800833 CBS server_hello, server_random, session_id;
David Benjamin2dc02042016-09-19 19:57:37 -0400834 uint16_t server_wire_version, cipher_suite;
Adam Langley1bea1732014-12-17 19:06:57 -0800835 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700836
Steven Valdez143e8b32016-07-11 13:19:03 -0400837 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
David Benjamin09eb6552016-07-08 14:32:11 -0700838 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500839 uint32_t err = ERR_peek_error();
840 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
841 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
842 /* Add a dedicated error code to the queue for a handshake_failure alert
843 * in response to ClientHello. This matches NSS's client behavior and
844 * gives a better error on a (probable) failure to negotiate initial
845 * parameters. Note: this error code comes after the original one.
846 *
847 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400848 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500849 }
David Benjamin09eb6552016-07-08 14:32:11 -0700850 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Steven Valdez143e8b32016-07-11 13:19:03 -0400853 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
854 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
855 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
856 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
857 return -1;
858 }
859
David Benjamin09eb6552016-07-08 14:32:11 -0700860 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
David Benjamina03d95d2014-07-12 19:49:07 -0400861
Steven Valdez143e8b32016-07-11 13:19:03 -0400862 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800863 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400864 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800865 goto f_err;
866 }
David Benjamina03d95d2014-07-12 19:49:07 -0400867
David Benjamin2dc02042016-09-19 19:57:37 -0400868 uint16_t min_version, max_version, server_version;
David Benjamine0ff7672016-09-19 18:40:03 -0400869 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
David Benjamin2dc02042016-09-19 19:57:37 -0400870 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjamin1f61f0d2016-07-10 12:20:35 -0400871 server_version < min_version || server_version > max_version) {
872 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
873 al = SSL_AD_PROTOCOL_VERSION;
874 goto f_err;
875 }
876
David Benjamin0d56f882015-12-19 17:05:56 -0500877 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
878 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400879 ssl->version = server_wire_version;
David Benjaminb83003e2015-12-30 17:39:26 -0500880 ssl->s3->enc_method = ssl3_get_enc_method(server_version);
881 assert(ssl->s3->enc_method != NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500882 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800883 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500884 ssl->s3->have_version = 1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400885 } else if (server_wire_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400886 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800887 al = SSL_AD_PROTOCOL_VERSION;
888 goto f_err;
889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Steven Valdez143e8b32016-07-11 13:19:03 -0400891 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500892 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900893 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400894 return 1;
895 }
896
David Benjamin6e4fc332016-11-17 16:43:08 +0900897 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400898
Steven Valdez143e8b32016-07-11 13:19:03 -0400899 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
900 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
901 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
902 return -1;
903 }
904
905 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
906 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
907 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
908 !CBS_get_u16(&server_hello, &cipher_suite) ||
909 !CBS_get_u8(&server_hello, &compression_method)) {
910 al = SSL_AD_DECODE_ERROR;
911 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
912 goto f_err;
913 }
914
Adam Langley1bea1732014-12-17 19:06:57 -0800915 /* Copy over the server random. */
David Benjamin0d56f882015-12-19 17:05:56 -0500916 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700917
David Benjamin55108632016-08-11 22:01:18 -0400918 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
919 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400920
David Benjamin0d56f882015-12-19 17:05:56 -0500921 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400922 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500923 CBS_mem_equal(&session_id, ssl->session->session_id,
924 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400925 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800926 } else {
927 /* The session wasn't resumed. Create a fresh SSL_SESSION to
928 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400929 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900930 if (!ssl_get_new_session(hs, 0 /* client */)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800931 goto f_err;
932 }
933 /* Note: session_id could be empty. */
Steven Valdez87eab492016-06-27 16:34:59 -0400934 ssl->s3->new_session->session_id_length = CBS_len(&session_id);
935 memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
David Benjamin0d56f882015-12-19 17:05:56 -0500936 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800937 }
David Benjamina03d95d2014-07-12 19:49:07 -0400938
David Benjamind2cb1c12016-11-02 17:49:09 -0400939 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800940 if (c == NULL) {
941 /* unknown cipher */
942 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400943 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800944 goto f_err;
945 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400946
947 /* The cipher must be allowed in the selected version and enabled. */
David Benjamin0d56f882015-12-19 17:05:56 -0500948 if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700949 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400950 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
951 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800952 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400953 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800954 goto f_err;
955 }
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Steven Valdez87eab492016-06-27 16:34:59 -0400957 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500958 if (ssl->session->ssl_version != ssl->version) {
David Benjaminece3de92015-03-16 18:02:20 -0400959 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400960 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400961 goto f_err;
962 }
Steven Valdez803c77a2016-09-06 14:13:43 -0400963 if (ssl->session->cipher != c) {
964 al = SSL_AD_ILLEGAL_PARAMETER;
965 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
966 goto f_err;
967 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400968 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
969 /* This is actually a client application bug. */
970 al = SSL_AD_ILLEGAL_PARAMETER;
971 OPENSSL_PUT_ERROR(SSL,
972 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
973 goto f_err;
974 }
David Benjamin0623bce2015-12-25 15:40:14 -0500975 } else {
Steven Valdez87eab492016-06-27 16:34:59 -0400976 ssl->s3->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800977 }
David Benjamin0d56f882015-12-19 17:05:56 -0500978 ssl->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500979
David Benjamin9550c3a2015-08-05 08:50:34 -0400980 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500981 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400982 goto f_err;
983 }
984
Steven Valdez2b8415e2016-06-30 13:27:23 -0400985 /* If doing a full handshake, the server may request a client certificate
986 * which requires hashing the handshake transcript. Otherwise, the handshake
987 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000988 if (ssl->session != NULL ||
989 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500990 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800991 }
David Benjamina03d95d2014-07-12 19:49:07 -0400992
Adam Langley1bea1732014-12-17 19:06:57 -0800993 /* Only the NULL compression algorithm is supported. */
994 if (compression_method != 0) {
995 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400996 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800997 goto f_err;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langley1bea1732014-12-17 19:06:57 -08001000 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -05001001 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -04001002 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -08001003 goto err;
1004 }
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langley1bea1732014-12-17 19:06:57 -08001006 /* There should be nothing left over in the record. */
1007 if (CBS_len(&server_hello) != 0) {
1008 /* wrong packet length */
1009 al = SSL_AD_DECODE_ERROR;
David Benjamin639846e2016-09-09 11:41:18 -04001010 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001011 goto f_err;
1012 }
Adam Langley95c29f32014-06-20 12:00:00 -07001013
Steven Valdez87eab492016-06-27 16:34:59 -04001014 if (ssl->session != NULL &&
David Benjamin0d56f882015-12-19 17:05:56 -05001015 ssl->s3->tmp.extended_master_secret !=
1016 ssl->session->extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -07001017 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin0d56f882015-12-19 17:05:56 -05001018 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -04001019 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001020 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001021 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001022 }
1023 goto f_err;
1024 }
1025
Adam Langley1bea1732014-12-17 19:06:57 -08001026 return 1;
1027
Adam Langley95c29f32014-06-20 12:00:00 -07001028f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001029 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001030err:
Adam Langley1bea1732014-12-17 19:06:57 -08001031 return -1;
1032}
Adam Langley95c29f32014-06-20 12:00:00 -07001033
David Benjaminc3c88822016-11-14 10:32:04 +09001034static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1035 SSL *const ssl = hs->ssl;
David Benjamin5c900c82016-07-13 23:03:26 -04001036 int ret =
David Benjamin09eb6552016-07-08 14:32:11 -07001037 ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
David Benjamin5c900c82016-07-13 23:03:26 -04001038 if (ret <= 0) {
1039 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001040 }
Adam Langley95c29f32014-06-20 12:00:00 -07001041
David Benjamin5c900c82016-07-13 23:03:26 -04001042 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001043 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
David Benjamin5c900c82016-07-13 23:03:26 -04001044 uint8_t alert;
Adam Langleyc0fc7a12016-12-09 15:05:34 -08001045 STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs);
1046 if (chain == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001047 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley1bea1732014-12-17 19:06:57 -08001048 goto err;
1049 }
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Adam Langleyc0fc7a12016-12-09 15:05:34 -08001051 if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001052 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001053 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1054 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001055 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001056
David Benjamin5c900c82016-07-13 23:03:26 -04001057 X509 *leaf = sk_X509_value(chain, 0);
Steven Valdezbf5aa842016-07-15 07:07:40 -04001058 if (!ssl_check_leaf_certificate(ssl, leaf)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001059 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1060 goto err;
David Benjamin436bf822015-06-07 19:50:37 -04001061 }
1062
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001063 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
1064 ssl->s3->new_session->x509_chain = chain;
Adam Langley364f7a62016-12-12 10:51:00 -08001065 sk_X509_pop_free(ssl->s3->new_session->x509_chain_without_leaf, X509_free);
1066 ssl->s3->new_session->x509_chain_without_leaf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001067
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001068 X509_free(ssl->s3->new_session->x509_peer);
David Benjamin96a16cd2016-08-11 12:14:47 -04001069 X509_up_ref(leaf);
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001070 ssl->s3->new_session->x509_peer = leaf;
David Benjamind26aea62014-07-12 00:13:56 -04001071
David Benjamin5c900c82016-07-13 23:03:26 -04001072 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001073
1074err:
David Benjamin5c900c82016-07-13 23:03:26 -04001075 sk_X509_pop_free(chain, X509_free);
1076 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001077}
1078
David Benjaminc3c88822016-11-14 10:32:04 +09001079static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1080 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001081 int al;
David Benjamin9f1dc822016-06-07 17:03:46 -04001082 CBS certificate_status, ocsp_response;
1083 uint8_t status_type;
1084
David Benjamin09eb6552016-07-08 14:32:11 -07001085 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1086 if (ret <= 0) {
1087 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001088 }
1089
1090 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1091 /* A server may send status_request in ServerHello and then change
1092 * its mind about sending CertificateStatus. */
1093 ssl->s3->tmp.reuse_message = 1;
1094 return 1;
1095 }
1096
David Benjamin09eb6552016-07-08 14:32:11 -07001097 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001098 if (!CBS_get_u8(&certificate_status, &status_type) ||
1099 status_type != TLSEXT_STATUSTYPE_ocsp ||
1100 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1101 CBS_len(&ocsp_response) == 0 ||
1102 CBS_len(&certificate_status) != 0) {
1103 al = SSL_AD_DECODE_ERROR;
1104 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1105 goto f_err;
1106 }
1107
Steven Valdez87eab492016-06-27 16:34:59 -04001108 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1109 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001110 al = SSL_AD_INTERNAL_ERROR;
1111 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1112 goto f_err;
1113 }
1114 return 1;
1115
1116f_err:
1117 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1118 return -1;
1119}
1120
David Benjaminc3c88822016-11-14 10:32:04 +09001121static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1122 SSL *const ssl = hs->ssl;
David Benjamin7aa31d62016-08-08 21:38:32 -04001123 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001124 ssl->s3->new_session->x509_chain)) {
David Benjamine455e512016-08-01 20:11:13 -04001125 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001126 }
1127
Adam Langley9498e742016-07-18 10:17:16 -07001128 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001129}
1130
David Benjaminc3c88822016-11-14 10:32:04 +09001131static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1132 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001133 int al;
Adam Langley1bea1732014-12-17 19:06:57 -08001134 EVP_PKEY *pkey = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001135 DH *dh = NULL;
1136 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001137 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001138
David Benjamin09eb6552016-07-08 14:32:11 -07001139 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1140 if (ret <= 0) {
1141 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001142 }
1143
David Benjamin0d56f882015-12-19 17:05:56 -05001144 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001145 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin0d56f882015-12-19 17:05:56 -05001146 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001147 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001148 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001149 return -1;
1150 }
1151
David Benjamin0d56f882015-12-19 17:05:56 -05001152 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001153 return 1;
1154 }
1155
1156 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001157 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001158 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001159 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001160
David Benjaminb35d6842015-12-29 17:08:08 -05001161 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1162 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001163
1164 if (alg_a & SSL_aPSK) {
1165 CBS psk_identity_hint;
1166
1167 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1168 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1169 &psk_identity_hint)) {
1170 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001171 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001172 goto f_err;
1173 }
1174
1175 /* Store PSK identity hint for later use, hint is used in
1176 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1177 * identity hint can be as long as the maximum length of a PSK identity.
1178 * Also do not allow NULL characters; identities are saved as C strings.
1179 *
1180 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1181 * a specific identity. */
1182 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1183 CBS_contains_zero_byte(&psk_identity_hint)) {
1184 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001185 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001186 goto f_err;
1187 }
1188
David Benjamin78679342016-09-16 19:42:05 -04001189 /* Save non-empty identity hints as a C string. Empty identity hints we
1190 * treat as missing. Plain PSK makes it possible to send either no hint
1191 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1192 * empty hint. Having different capabilities is odd, so we interpret empty
1193 * and missing as identical. */
1194 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001195 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001196 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001197 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001198 goto f_err;
1199 }
1200 }
1201
David Benjamin7061e282015-03-19 11:10:48 -04001202 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001203 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001204 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1205 CBS_len(&dh_p) == 0 ||
1206 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1207 CBS_len(&dh_g) == 0 ||
1208 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1209 CBS_len(&dh_Ys) == 0) {
1210 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001211 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001212 goto f_err;
1213 }
1214
1215 dh = DH_new();
1216 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001217 goto err;
1218 }
1219
David Benjamin48cce662015-12-17 01:33:08 -05001220 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1221 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001222 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001223 goto err;
1224 }
1225
Steven Valdez87eab492016-06-27 16:34:59 -04001226 ssl->s3->new_session->key_exchange_info = DH_num_bits(dh);
1227 if (ssl->s3->new_session->key_exchange_info < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001228 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001229 goto err;
Steven Valdez87eab492016-06-27 16:34:59 -04001230 } else if (ssl->s3->new_session->key_exchange_info > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001231 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1232 * to prevent a server from causing us to perform too expensive of a
1233 * computation. */
1234 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1235 goto err;
1236 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001237
David Benjaminc3c88822016-11-14 10:32:04 +09001238 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001239 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001240
1241 /* Save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001242 if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001243 goto err;
1244 }
David Benjamin7061e282015-03-19 11:10:48 -04001245 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001246 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001247 uint8_t group_type;
1248 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001249 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001250 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1251 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001252 !CBS_get_u16(&server_key_exchange, &group_id) ||
1253 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001254 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001255 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001256 goto f_err;
1257 }
Steven Valdez87eab492016-06-27 16:34:59 -04001258 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001259
Steven Valdezce902a92016-05-17 11:47:53 -04001260 /* Ensure the group is consistent with preferences. */
1261 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001262 al = SSL_AD_ILLEGAL_PARAMETER;
1263 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001264 goto f_err;
1265 }
David Benjamin4298d772015-12-19 00:18:25 -05001266
Matt Braithwaite053931e2016-05-25 12:06:05 -07001267 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001268 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1269 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001270 goto err;
1271 }
Adam Langley1bea1732014-12-17 19:06:57 -08001272 } else if (!(alg_k & SSL_kPSK)) {
1273 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001274 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001275 goto f_err;
1276 }
1277
1278 /* At this point, |server_key_exchange| contains the signature, if any, while
1279 * |server_key_exchange_orig| contains the entire message. From that, derive
1280 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001281 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001282 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1283 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1284
David Benjaminf4958e72015-06-07 11:00:53 -04001285 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001286 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001287 pkey = X509_get_pubkey(ssl->s3->new_session->x509_peer);
David Benjaminf4958e72015-06-07 11:00:53 -04001288 if (pkey == NULL) {
1289 goto err;
1290 }
Adam Langley1bea1732014-12-17 19:06:57 -08001291
Steven Valdezf0451ca2016-06-29 13:16:27 -04001292 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001293 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001294 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001295 al = SSL_AD_DECODE_ERROR;
1296 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001297 goto f_err;
1298 }
David Benjamin887c3002016-07-08 16:15:32 -07001299 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001300 goto f_err;
1301 }
Steven Valdez02563852016-06-23 13:33:05 -04001302 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Adam Langley1bea1732014-12-17 19:06:57 -08001303 } else if (pkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001304 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1305 } else if (pkey->type == EVP_PKEY_EC) {
1306 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001307 } else {
1308 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1309 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1310 goto f_err;
Adam Langley1bea1732014-12-17 19:06:57 -08001311 }
1312
1313 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001314 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001315 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1316 CBS_len(&server_key_exchange) != 0) {
1317 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001318 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001319 goto f_err;
1320 }
1321
Steven Valdez2b8415e2016-06-30 13:27:23 -04001322 CBB transcript;
1323 uint8_t *transcript_data;
1324 size_t transcript_len;
1325 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1326 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1327 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1328 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1329 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1330 CBB_cleanup(&transcript);
1331 al = SSL_AD_INTERNAL_ERROR;
1332 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001333 goto f_err;
1334 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001335
1336 int sig_ok = ssl_public_key_verify(
1337 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1338 pkey, transcript_data, transcript_len);
1339 OPENSSL_free(transcript_data);
1340
David Benjaminbf82aed2016-03-01 22:57:40 -05001341#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1342 sig_ok = 1;
1343 ERR_clear_error();
1344#endif
1345 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001346 /* bad signature */
1347 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001348 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001349 goto f_err;
1350 }
1351 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001352 /* PSK ciphers are the only supported certificate-less ciphers. */
1353 assert(alg_a == SSL_aPSK);
1354
Adam Langley1bea1732014-12-17 19:06:57 -08001355 if (CBS_len(&server_key_exchange) > 0) {
1356 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001357 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001358 goto f_err;
1359 }
1360 }
1361 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001362 return 1;
1363
Adam Langley95c29f32014-06-20 12:00:00 -07001364f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001365 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001366err:
Adam Langley1bea1732014-12-17 19:06:57 -08001367 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001368 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001369 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001370 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001371 return -1;
1372}
Adam Langley95c29f32014-06-20 12:00:00 -07001373
David Benjaminc3c88822016-11-14 10:32:04 +09001374static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1375 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001376 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1377 if (msg_ret <= 0) {
1378 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001379 }
David Benjamin688d8df2014-11-02 23:06:42 -05001380
David Benjamin3dcec452016-06-07 17:13:28 -04001381 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001382 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001383 /* If we get here we don't need the handshake buffer as we won't be doing
1384 * client auth. */
David Benjamin0d56f882015-12-19 17:05:56 -05001385 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001386 return 1;
1387 }
David Benjamined439582014-07-14 19:13:02 -04001388
David Benjamin0d56f882015-12-19 17:05:56 -05001389 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1390 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamindf28c3a2016-03-10 16:11:51 -05001391 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamine0332e82016-07-13 22:40:36 -04001392 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001393 }
Adam Langley95c29f32014-06-20 12:00:00 -07001394
David Benjamina5177cb2016-03-11 19:38:17 -05001395 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001396 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001397
David Benjamine0332e82016-07-13 22:40:36 -04001398 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001399 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001400 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001401 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001402 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001403 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001404 }
David Benjamined439582014-07-14 19:13:02 -04001405
David Benjaminc3c88822016-11-14 10:32:04 +09001406 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1407 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001408 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001409 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001410 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001411
David Benjamina1e9cab2015-12-30 00:08:49 -05001412 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001413 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001414 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001415 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001416 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001417 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001418 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001419 }
Adam Langley1bea1732014-12-17 19:06:57 -08001420 }
David Benjamined439582014-07-14 19:13:02 -04001421
David Benjamine0332e82016-07-13 22:40:36 -04001422 uint8_t alert;
1423 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
1424 if (ca_sk == NULL) {
1425 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1426 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001427 }
Adam Langley95c29f32014-06-20 12:00:00 -07001428
David Benjamin639846e2016-09-09 11:41:18 -04001429 if (CBS_len(&cbs) != 0) {
1430 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1431 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1432 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1433 return -1;
1434 }
1435
David Benjaminc3c88822016-11-14 10:32:04 +09001436 hs->cert_request = 1;
1437 sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
1438 hs->ca_names = ca_sk;
David Benjamine0332e82016-07-13 22:40:36 -04001439 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001440}
Adam Langley95c29f32014-06-20 12:00:00 -07001441
David Benjaminc3c88822016-11-14 10:32:04 +09001442static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1443 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001444 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
1445 ssl_hash_message);
1446 if (ret <= 0) {
1447 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001448 }
David Benjamin46062682014-07-14 19:14:32 -04001449
David Benjamin09eb6552016-07-08 14:32:11 -07001450 /* ServerHelloDone is empty. */
1451 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001452 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001453 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001454 return -1;
1455 }
David Benjamin46062682014-07-14 19:14:32 -04001456
Adam Langley1bea1732014-12-17 19:06:57 -08001457 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001458}
Adam Langley1258b6a2014-06-20 12:00:00 -07001459
David Benjaminc3c88822016-11-14 10:32:04 +09001460static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1461 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001462 if (hs->state == SSL3_ST_CW_CERT_B) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001463 return ssl->method->write_message(ssl);
1464 }
David Benjamincb0c29f2016-12-12 17:00:50 -05001465 assert(hs->state == SSL3_ST_CW_CERT_A);
David Benjamin5edfc8c2016-12-10 15:46:58 -05001466
1467 /* Call cert_cb to update the certificate. */
1468 if (ssl->cert->cert_cb) {
1469 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1470 if (ret < 0) {
1471 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001472 return -1;
1473 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001474 if (ret == 0) {
1475 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1476 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001477 return -1;
1478 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001479 }
1480
David Benjamin5edfc8c2016-12-10 15:46:58 -05001481 if (!ssl_has_certificate(ssl)) {
1482 hs->cert_request = 0;
1483 /* Without a client certificate, the handshake buffer may be released. */
1484 ssl3_free_handshake_buffer(ssl);
1485
1486 if (ssl->version == SSL3_VERSION) {
1487 /* In SSL 3.0, send no certificate by skipping both messages. */
1488 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1489 return 1;
1490 }
1491 }
1492
1493 if (!ssl3_output_cert_chain(ssl)) {
1494 return -1;
1495 }
David Benjamincb0c29f2016-12-12 17:00:50 -05001496 hs->state = SSL3_ST_CW_CERT_B;
David Benjamin75836432016-06-17 18:48:29 -04001497 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001498}
1499
Brian Smith274341d2015-10-08 17:10:15 -10001500OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1501 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001502
David Benjaminc3c88822016-11-14 10:32:04 +09001503static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1504 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001505 if (hs->state == SSL3_ST_CW_KEY_EXCH_B) {
David Benjamin75836432016-06-17 18:48:29 -04001506 return ssl->method->write_message(ssl);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001507 }
David Benjamincb0c29f2016-12-12 17:00:50 -05001508 assert(hs->state == SSL3_ST_CW_KEY_EXCH_A);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001509
Adam Langley1bea1732014-12-17 19:06:57 -08001510 uint8_t *pms = NULL;
1511 size_t pms_len = 0;
David Benjamin75836432016-06-17 18:48:29 -04001512 CBB cbb, body;
1513 if (!ssl->method->init_message(ssl, &cbb, &body,
1514 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001515 goto err;
1516 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001517
David Benjaminae0eaaa2015-12-17 02:01:25 -05001518 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1519 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001520
David Benjaminae0eaaa2015-12-17 02:01:25 -05001521 /* If using a PSK key exchange, prepare the pre-shared key. */
1522 unsigned psk_len = 0;
1523 uint8_t psk[PSK_MAX_PSK_LEN];
1524 if (alg_a & SSL_aPSK) {
1525 if (ssl->psk_client_callback == NULL) {
1526 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1527 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001528 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001529
David Benjaminae0eaaa2015-12-17 02:01:25 -05001530 char identity[PSK_MAX_IDENTITY_LEN + 1];
1531 memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001532 psk_len =
1533 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1534 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001535 if (psk_len == 0) {
1536 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1537 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1538 goto err;
1539 }
1540 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001541
Steven Valdez87eab492016-06-27 16:34:59 -04001542 OPENSSL_free(ssl->s3->new_session->psk_identity);
1543 ssl->s3->new_session->psk_identity = BUF_strdup(identity);
1544 if (ssl->s3->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001545 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1546 goto err;
1547 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001548
David Benjaminae0eaaa2015-12-17 02:01:25 -05001549 /* Write out psk_identity. */
1550 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001551 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001552 !CBB_add_bytes(&child, (const uint8_t *)identity,
1553 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001554 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001555 goto err;
1556 }
1557 }
David Benjamin758d1272015-11-20 17:47:25 -05001558
David Benjaminae0eaaa2015-12-17 02:01:25 -05001559 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1560 if (alg_k & SSL_kRSA) {
1561 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1562 pms = OPENSSL_malloc(pms_len);
1563 if (pms == NULL) {
1564 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1565 goto err;
1566 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001567
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001568 EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->x509_peer);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001569 if (pkey == NULL) {
1570 goto err;
1571 }
1572
1573 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
1574 if (rsa == NULL) {
1575 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001576 EVP_PKEY_free(pkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001577 goto err;
1578 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001579
David Benjaminae0eaaa2015-12-17 02:01:25 -05001580 EVP_PKEY_free(pkey);
1581
David Benjaminf04c2e92016-12-06 13:35:25 -05001582 pms[0] = hs->client_version >> 8;
1583 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001584 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1585 goto err;
1586 }
1587
David Benjamin75836432016-06-17 18:48:29 -04001588 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001589 size_t enc_pms_len;
1590 /* In TLS, there is a length prefix. */
1591 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001592 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001593 goto err;
1594 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001595 enc_pms = &child;
1596 }
Adam Langley1bea1732014-12-17 19:06:57 -08001597
David Benjaminae0eaaa2015-12-17 02:01:25 -05001598 uint8_t *ptr;
1599 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1600 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1601 RSA_PKCS1_PADDING) ||
1602 /* Log the premaster secret, if logging is enabled. */
1603 !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1604 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001605 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001606 goto err;
1607 }
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001608 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001609 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001610 CBB child;
David Benjaminc3c88822016-11-14 10:32:04 +09001611 if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001612 goto err;
1613 }
1614
David Benjamin4298d772015-12-19 00:18:25 -05001615 /* Compute the premaster. */
1616 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001617 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1618 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001619 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1620 goto err;
1621 }
David Benjamin75836432016-06-17 18:48:29 -04001622 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001623 goto err;
1624 }
David Benjamin4298d772015-12-19 00:18:25 -05001625
1626 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001627 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1628 OPENSSL_free(hs->peer_key);
1629 hs->peer_key = NULL;
1630 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001631 } else if (alg_k & SSL_kPSK) {
1632 /* For plain PSK, other_secret is a block of 0s with the same length as
1633 * the pre-shared key. */
1634 pms_len = psk_len;
1635 pms = OPENSSL_malloc(pms_len);
1636 if (pms == NULL) {
1637 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1638 goto err;
1639 }
1640 memset(pms, 0, pms_len);
1641 } else {
1642 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1643 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1644 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001645 }
1646
David Benjaminae0eaaa2015-12-17 02:01:25 -05001647 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1648 * key. */
1649 if (alg_a & SSL_aPSK) {
1650 CBB pms_cbb, child;
1651 uint8_t *new_pms;
1652 size_t new_pms_len;
1653
1654 CBB_zero(&pms_cbb);
1655 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1656 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1657 !CBB_add_bytes(&child, pms, pms_len) ||
1658 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1659 !CBB_add_bytes(&child, psk, psk_len) ||
1660 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1661 CBB_cleanup(&pms_cbb);
1662 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1663 goto err;
1664 }
1665 OPENSSL_cleanse(pms, pms_len);
1666 OPENSSL_free(pms);
1667 pms = new_pms;
1668 pms_len = new_pms_len;
1669 }
1670
1671 /* The message must be added to the finished hash before calculating the
1672 * master secret. */
Steven Valdez5eead162016-11-11 22:23:25 -05001673 if (!ssl_complete_message(ssl, &cbb)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001674 goto err;
1675 }
David Benjamincb0c29f2016-12-12 17:00:50 -05001676 hs->state = SSL3_ST_CW_KEY_EXCH_B;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001677
Steven Valdez87eab492016-06-27 16:34:59 -04001678 ssl->s3->new_session->master_key_length =
1679 tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
1680 pms_len);
1681 if (ssl->s3->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001682 goto err;
1683 }
Steven Valdez87eab492016-06-27 16:34:59 -04001684 ssl->s3->new_session->extended_master_secret =
1685 ssl->s3->tmp.extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001686 OPENSSL_cleanse(pms, pms_len);
1687 OPENSSL_free(pms);
1688
David Benjamin75836432016-06-17 18:48:29 -04001689 return ssl->method->write_message(ssl);
Adam Langley1258b6a2014-06-20 12:00:00 -07001690
1691err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001692 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001693 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001694 OPENSSL_cleanse(pms, pms_len);
1695 OPENSSL_free(pms);
1696 }
1697 return -1;
1698}
Adam Langley1258b6a2014-06-20 12:00:00 -07001699
David Benjaminc3c88822016-11-14 10:32:04 +09001700static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1701 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001702 if (hs->state == SSL3_ST_CW_CERT_VRFY_C) {
David Benjamin75836432016-06-17 18:48:29 -04001703 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001704 }
1705
David Benjamin429fdc02016-06-17 17:33:30 -04001706 assert(ssl_has_private_key(ssl));
1707
David Benjamin75836432016-06-17 18:48:29 -04001708 CBB cbb, body, child;
1709 if (!ssl->method->init_message(ssl, &cbb, &body,
1710 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjaminfc825122015-12-18 01:57:43 -05001711 goto err;
1712 }
1713
David Benjaminea9a0d52016-07-08 15:52:59 -07001714 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001715 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001716 goto err;
1717 }
David Benjamin429fdc02016-06-17 17:33:30 -04001718 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001719 /* Write out the digest type in TLS 1.2. */
1720 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001721 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1722 goto err;
1723 }
1724 }
David Benjaminfc825122015-12-18 01:57:43 -05001725
David Benjamin429fdc02016-06-17 17:33:30 -04001726 /* Set aside space for the signature. */
David Benjaminfc825122015-12-18 01:57:43 -05001727 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjamin429fdc02016-06-17 17:33:30 -04001728 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001729 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001730 !CBB_reserve(&child, &ptr, max_sig_len)) {
1731 goto err;
1732 }
1733
Steven Valdez2b8415e2016-06-30 13:27:23 -04001734 size_t sig_len = max_sig_len;
David Benjaminfc825122015-12-18 01:57:43 -05001735 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001736 if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001737 /* The SSL3 construction for CertificateVerify does not decompose into a
1738 * single final digest and signature, and must be special-cased. */
1739 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1740 if (ssl->cert->key_method != NULL) {
1741 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1742 goto err;
1743 }
1744
David Benjamin0aa25bd2016-07-08 14:44:56 -07001745 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001746 uint8_t digest[EVP_MAX_MD_SIZE];
1747 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001748 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001749 signature_algorithm)) {
1750 goto err;
1751 }
1752
1753 sign_result = ssl_private_key_success;
1754
1755 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1756 if (pctx == NULL ||
1757 !EVP_PKEY_sign_init(pctx) ||
David Benjamin0aa25bd2016-07-08 14:44:56 -07001758 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001759 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1760 EVP_PKEY_CTX_free(pctx);
1761 sign_result = ssl_private_key_failure;
1762 goto err;
1763 }
1764 EVP_PKEY_CTX_free(pctx);
1765 } else {
1766 sign_result = ssl_private_key_sign(
1767 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
1768 (const uint8_t *)ssl->s3->handshake_buffer->data,
1769 ssl->s3->handshake_buffer->length);
David Benjaminfc825122015-12-18 01:57:43 -05001770 }
1771
1772 /* The handshake buffer is no longer necessary. */
1773 ssl3_free_handshake_buffer(ssl);
David Benjaminfc825122015-12-18 01:57:43 -05001774 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001775 assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjamind3440b42016-07-14 14:52:41 -04001776 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
David Benjaminfc825122015-12-18 01:57:43 -05001777 }
1778
1779 switch (sign_result) {
1780 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001781 break;
1782 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001783 goto err;
1784 case ssl_private_key_retry:
1785 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001786 hs->state = SSL3_ST_CW_CERT_VRFY_B;
David Benjaminfc825122015-12-18 01:57:43 -05001787 goto err;
1788 }
1789
David Benjaminfc825122015-12-18 01:57:43 -05001790 if (!CBB_did_write(&child, sig_len) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001791 !ssl_complete_message(ssl, &cbb)) {
David Benjaminfc825122015-12-18 01:57:43 -05001792 goto err;
1793 }
1794
David Benjamincb0c29f2016-12-12 17:00:50 -05001795 hs->state = SSL3_ST_CW_CERT_VRFY_C;
David Benjamin75836432016-06-17 18:48:29 -04001796 return ssl->method->write_message(ssl);
David Benjaminfc825122015-12-18 01:57:43 -05001797
1798err:
1799 CBB_cleanup(&cbb);
1800 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001801}
1802
David Benjaminc3c88822016-11-14 10:32:04 +09001803static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1804 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001805 if (hs->state == SSL3_ST_CW_NEXT_PROTO_B) {
David Benjamin75836432016-06-17 18:48:29 -04001806 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001807 }
1808
David Benjamincb0c29f2016-12-12 17:00:50 -05001809 assert(hs->state == SSL3_ST_CW_NEXT_PROTO_A);
David Benjamine97b9622015-10-10 14:59:09 -04001810
1811 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001812 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001813
David Benjamin75836432016-06-17 18:48:29 -04001814 CBB cbb, body, child;
1815 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1816 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001817 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1818 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001819 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001820 !CBB_add_bytes(&child, kZero, padding_len) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001821 !ssl_complete_message(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001822 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1823 CBB_cleanup(&cbb);
1824 return -1;
1825 }
1826
David Benjamincb0c29f2016-12-12 17:00:50 -05001827 hs->state = SSL3_ST_CW_NEXT_PROTO_B;
David Benjamin75836432016-06-17 18:48:29 -04001828 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001829}
1830
David Benjaminc3c88822016-11-14 10:32:04 +09001831static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1832 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001833 if (hs->state == SSL3_ST_CW_CHANNEL_ID_B) {
David Benjamin75836432016-06-17 18:48:29 -04001834 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001835 }
1836
David Benjamincb0c29f2016-12-12 17:00:50 -05001837 assert(hs->state == SSL3_ST_CW_CHANNEL_ID_A);
David Benjamin96e97b12015-10-10 16:30:22 -04001838
Nick Harper60a85cb2016-09-23 16:25:11 -07001839 if (!ssl_do_channel_id_callback(ssl)) {
1840 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001841 }
1842
David Benjamin96e97b12015-10-10 16:30:22 -04001843 if (ssl->tlsext_channel_id_private == NULL) {
1844 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001845 return -1;
1846 }
Adam Langley1bea1732014-12-17 19:06:57 -08001847
Nick Harper60a85cb2016-09-23 16:25:11 -07001848 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001849 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Nick Harper60a85cb2016-09-23 16:25:11 -07001850 !tls1_write_channel_id(ssl, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001851 !ssl_complete_message(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001852 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001853 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001854 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001855 }
1856
David Benjamincb0c29f2016-12-12 17:00:50 -05001857 hs->state = SSL3_ST_CW_CHANNEL_ID_B;
Nick Harper60a85cb2016-09-23 16:25:11 -07001858 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001859}
1860
David Benjaminc3c88822016-11-14 10:32:04 +09001861static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1862 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001863 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
1864 ssl_hash_message);
1865 if (ret <= 0) {
1866 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001867 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001868
David Benjamin9f1dc822016-06-07 17:03:46 -04001869 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001870 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001871 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001872 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001873 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1874 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001875 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001876 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001877 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001878 }
1879
David Benjamin9f1dc822016-06-07 17:03:46 -04001880 if (CBS_len(&ticket) == 0) {
1881 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001882 * negotiating the extension. The value of |ticket_expected| is checked in
1883 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001884 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001885 return 1;
1886 }
1887
Steven Valdez87eab492016-06-27 16:34:59 -04001888 int session_renewed = ssl->session != NULL;
1889 SSL_SESSION *session = ssl->s3->new_session;
1890 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001891 /* The server is sending a new ticket for an existing session. Sessions are
1892 * immutable once established, so duplicate all but the ticket of the
1893 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001894 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001895 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001896 /* This should never happen. */
1897 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1898 goto err;
1899 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001900 }
1901
David Benjamin123db572016-11-03 16:59:25 -04001902 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
1903 ssl_session_refresh_time(ssl, session);
1904
Steven Valdez87eab492016-06-27 16:34:59 -04001905 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001906 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1907 goto err;
1908 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001909 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001910
1911 /* Generate a session ID for this session based on the session ticket. We use
1912 * the session ID mechanism for detecting ticket resumption. This also fits in
1913 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001914 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1915 session->session_id, &session->session_id_length,
1916 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001917 goto err;
1918 }
1919
Steven Valdez87eab492016-06-27 16:34:59 -04001920 if (session_renewed) {
1921 session->not_resumable = 0;
1922 SSL_SESSION_free(ssl->session);
1923 ssl->session = session;
1924 }
1925
David Benjamin454aa4c2016-03-10 08:43:27 -05001926 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001927
David Benjamin9f1dc822016-06-07 17:03:46 -04001928err:
Steven Valdez87eab492016-06-27 16:34:59 -04001929 if (session_renewed) {
1930 SSL_SESSION_free(session);
1931 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001932 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001933}