blob: 481b3b6533966458e0bb27568f146f6ddd7e035d [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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin880b14e2014-08-25 22:35:07 -0400151#include <assert.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/cipher.h>
158#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400166#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/rand.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700168#include <openssl/sha.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169#include <openssl/x509.h>
170
David Benjamin2ee94aa2015-04-07 22:38:30 -0400171#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800172#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700173#include "../crypto/dh/internal.h"
174
Adam Langleyfcf25832014-12-18 17:42:32 -0800175
David Benjamindf50eec2016-06-07 16:49:42 -0400176static int ssl3_get_initial_bytes(SSL *ssl);
177static int ssl3_get_v2_client_hello(SSL *ssl);
178static int ssl3_get_client_hello(SSL *ssl);
179static int ssl3_send_server_hello(SSL *ssl);
180static int ssl3_send_certificate_status(SSL *ssl);
181static int ssl3_send_server_done(SSL *ssl);
182static int ssl3_send_server_key_exchange(SSL *ssl);
183static int ssl3_send_certificate_request(SSL *ssl);
184static int ssl3_get_client_key_exchange(SSL *ssl);
185static int ssl3_get_cert_verify(SSL *ssl);
186static int ssl3_get_client_certificate(SSL *ssl);
187static int ssl3_send_server_certificate(SSL *ssl);
188static int ssl3_send_new_session_ticket(SSL *ssl);
189static int ssl3_get_next_proto(SSL *ssl);
190static int ssl3_get_channel_id(SSL *ssl);
191
David Benjamin0d56f882015-12-19 17:05:56 -0500192int ssl3_accept(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 BUF_MEM *buf = NULL;
David Benjamin3fa27772015-04-17 22:32:19 -0400194 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400196 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjamin0d56f882015-12-19 17:05:56 -0500198 assert(ssl->handshake_func == ssl3_accept);
199 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500202 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin0d56f882015-12-19 17:05:56 -0500204 switch (ssl->state) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 case SSL_ST_ACCEPT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400206 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin0d56f882015-12-19 17:05:56 -0500208 if (ssl->init_buf == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 buf = BUF_MEM_new();
David Benjamina6338be2016-05-13 18:12:19 -0400210 if (!buf || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 ret = -1;
212 goto end;
213 }
David Benjamin0d56f882015-12-19 17:05:56 -0500214 ssl->init_buf = buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 buf = NULL;
216 }
David Benjamin0d56f882015-12-19 17:05:56 -0500217 ssl->init_num = 0;
David Benjamin4b755cb2014-12-12 03:58:07 -0500218
David Benjamin6a08da22015-05-08 22:58:12 -0400219 /* Enable a write buffer. This groups handshake messages within a flight
220 * into a single write. */
David Benjaminb095f0f2016-05-05 21:50:24 -0400221 if (!ssl_init_wbio_buffer(ssl)) {
David Benjamin6a08da22015-05-08 22:58:12 -0400222 ret = -1;
223 goto end;
224 }
225
David Benjamin0d56f882015-12-19 17:05:56 -0500226 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400227 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 ret = -1;
229 goto end;
230 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500231
David Benjamindf50eec2016-06-07 16:49:42 -0400232 if (!ssl->s3->have_version && !SSL_IS_DTLS(ssl)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500233 ssl->state = SSL3_ST_SR_INITIAL_BYTES;
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500235 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500238
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 case SSL3_ST_SR_INITIAL_BYTES:
David Benjamindf50eec2016-06-07 16:49:42 -0400240 assert(!SSL_IS_DTLS(ssl));
David Benjamin0d56f882015-12-19 17:05:56 -0500241 ret = ssl3_get_initial_bytes(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (ret <= 0) {
243 goto end;
244 }
David Benjamin0d56f882015-12-19 17:05:56 -0500245 /* ssl3_get_initial_bytes sets ssl->state to one of
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */
247 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 case SSL3_ST_SR_V2_CLIENT_HELLO:
David Benjamindf50eec2016-06-07 16:49:42 -0400250 assert(!SSL_IS_DTLS(ssl));
David Benjamin0d56f882015-12-19 17:05:56 -0500251 ret = ssl3_get_v2_client_hello(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 if (ret <= 0) {
253 goto end;
254 }
David Benjamin0d56f882015-12-19 17:05:56 -0500255 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500257
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 case SSL3_ST_SR_CLNT_HELLO_A:
259 case SSL3_ST_SR_CLNT_HELLO_B:
260 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500261 ret = ssl3_get_client_hello(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 if (ret <= 0) {
263 goto end;
264 }
David Benjamindf50eec2016-06-07 16:49:42 -0400265 ssl->method->received_flight(ssl);
David Benjamin0d56f882015-12-19 17:05:56 -0500266 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 case SSL3_ST_SW_SRVR_HELLO_A:
270 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500271 ret = ssl3_send_server_hello(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 if (ret <= 0) {
273 goto end;
274 }
David Benjamin0d56f882015-12-19 17:05:56 -0500275 if (ssl->hit) {
276 if (ssl->tlsext_ticket_expected) {
277 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500279 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 }
281 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500282 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 case SSL3_ST_SW_CERT_A:
287 case SSL3_ST_SW_CERT_B:
David Benjaminc032dfa2016-05-12 14:54:57 -0400288 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500289 ret = ssl3_send_server_certificate(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 if (ret <= 0) {
291 goto end;
292 }
David Benjamin0d56f882015-12-19 17:05:56 -0500293 if (ssl->s3->tmp.certificate_status_expected) {
294 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500296 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 }
298 } else {
299 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500300 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800301 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100304 case SSL3_ST_SW_CERT_STATUS_A:
305 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500306 ret = ssl3_send_certificate_status(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100307 if (ret <= 0) {
308 goto end;
309 }
David Benjamin0d56f882015-12-19 17:05:56 -0500310 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100311 break;
312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 case SSL3_ST_SW_KEY_EXCH_A:
314 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700315 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500316 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
David Benjamindf50eec2016-06-07 16:49:42 -0400318 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500319 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
320 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
321 ret = ssl3_send_server_key_exchange(ssl);
David Benjamin6eb000d2015-02-11 01:17:41 -0500322 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500324 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 } else {
326 skip = 1;
327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
David Benjamin0d56f882015-12-19 17:05:56 -0500329 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 case SSL3_ST_SW_CERT_REQ_A:
333 case SSL3_ST_SW_CERT_REQ_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500334 if (ssl->s3->tmp.cert_request) {
335 ret = ssl3_send_certificate_request(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 if (ret <= 0) {
337 goto end;
338 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400339 } else {
340 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 }
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 case SSL3_ST_SW_SRVR_DONE_A:
346 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500347 ret = ssl3_send_server_done(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 if (ret <= 0) {
349 goto end;
350 }
David Benjamin0d56f882015-12-19 17:05:56 -0500351 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
352 ssl->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 case SSL3_ST_SW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500356 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500357 ssl->rwstate = SSL_WRITING;
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 ret = -1;
359 goto end;
360 }
Adam Langley95c29f32014-06-20 12:00:00 -0700361
David Benjamin0d56f882015-12-19 17:05:56 -0500362 ssl->state = ssl->s3->tmp.next_state;
David Benjamindf50eec2016-06-07 16:49:42 -0400363 if (ssl->state != SSL_ST_OK) {
364 ssl->method->expect_flight(ssl);
365 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 case SSL3_ST_SR_CERT_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500369 if (ssl->s3->tmp.cert_request) {
370 ret = ssl3_get_client_certificate(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 if (ret <= 0) {
372 goto end;
373 }
374 }
David Benjamin0d56f882015-12-19 17:05:56 -0500375 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 case SSL3_ST_SR_KEY_EXCH_A:
379 case SSL3_ST_SR_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500380 ret = ssl3_get_client_key_exchange(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 if (ret <= 0) {
382 goto end;
383 }
David Benjamin0d56f882015-12-19 17:05:56 -0500384 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 case SSL3_ST_SR_CERT_VRFY_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ret = ssl3_get_cert_verify(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 if (ret <= 0) {
390 goto end;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
David Benjamin0d56f882015-12-19 17:05:56 -0500393 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
David Benjamina41280d2015-11-26 02:16:49 -0500396 case SSL3_ST_SR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500397 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500398 if (ret <= 0) {
399 goto end;
400 }
Adam Langley95c29f32014-06-20 12:00:00 -0700401
David Benjamin57997da2015-12-25 15:48:39 -0500402 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 ret = -1;
404 goto end;
405 }
David Benjamina41280d2015-11-26 02:16:49 -0500406
David Benjamin0d56f882015-12-19 17:05:56 -0500407 if (ssl->s3->next_proto_neg_seen) {
408 ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
409 } else if (ssl->s3->tlsext_channel_id_valid) {
410 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500412 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 }
414 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500417 ret = ssl3_get_next_proto(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 if (ret <= 0) {
419 goto end;
420 }
David Benjamin0d56f882015-12-19 17:05:56 -0500421 if (ssl->s3->tlsext_channel_id_valid) {
422 ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500424 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 }
426 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500429 ret = ssl3_get_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 if (ret <= 0) {
431 goto end;
432 }
David Benjamin0d56f882015-12-19 17:05:56 -0500433 ssl->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700435
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 case SSL3_ST_SR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400437 ret = ssl3_get_finished(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 if (ret <= 0) {
439 goto end;
440 }
Adam Langley95c29f32014-06-20 12:00:00 -0700441
David Benjamindf50eec2016-06-07 16:49:42 -0400442 ssl->method->received_flight(ssl);
David Benjamin0d56f882015-12-19 17:05:56 -0500443 if (ssl->hit) {
444 ssl->state = SSL_ST_OK;
445 } else if (ssl->tlsext_ticket_expected) {
446 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500448 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 }
David Benjamindf50eec2016-06-07 16:49:42 -0400450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 /* If this is a full handshake with ChannelID then record the hashshake
David Benjamin0d56f882015-12-19 17:05:56 -0500452 * hashes in |ssl->session| in case we need them to verify a ChannelID
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 * signature on a resumption of this session in the future. */
David Benjamin0d56f882015-12-19 17:05:56 -0500454 if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
455 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 if (ret <= 0) {
457 goto end;
458 }
459 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 case SSL3_ST_SW_SESSION_TICKET_A:
463 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500464 ret = ssl3_send_new_session_ticket(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 if (ret <= 0) {
466 goto end;
467 }
David Benjamin0d56f882015-12-19 17:05:56 -0500468 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 case SSL3_ST_SW_CHANGE_A:
472 case SSL3_ST_SW_CHANGE_B:
David Benjamindf50eec2016-06-07 16:49:42 -0400473 ret = ssl->method->send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
474 SSL3_ST_SW_CHANGE_B);
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 if (ret <= 0) {
476 goto end;
477 }
David Benjamin0d56f882015-12-19 17:05:56 -0500478 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700479
David Benjamin57997da2015-12-25 15:48:39 -0500480 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 ret = -1;
482 goto end;
483 }
484 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 case SSL3_ST_SW_FINISHED_A:
487 case SSL3_ST_SW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500488 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500489 SSL3_ST_SW_FINISHED_B);
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 if (ret <= 0) {
491 goto end;
492 }
David Benjamin0d56f882015-12-19 17:05:56 -0500493 ssl->state = SSL3_ST_SW_FLUSH;
494 if (ssl->hit) {
495 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500497 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800499 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 case SSL_ST_OK:
502 /* clean a few things up */
David Benjamin0d56f882015-12-19 17:05:56 -0500503 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamindf50eec2016-06-07 16:49:42 -0400505 /* In DTLS, |init_buf| cannot be released because post-handshake
506 * retransmit relies on that buffer being available as scratch space.
507 *
508 * TODO(davidben): Fix this. */
509 if (!SSL_IS_DTLS(ssl)) {
510 BUF_MEM_free(ssl->init_buf);
511 ssl->init_buf = NULL;
512 ssl->init_num = 0;
513 }
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500516 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 /* If we aren't retaining peer certificates then we can discard it
519 * now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500520 if (ssl->ctx->retain_only_sha256_of_client_certs) {
521 X509_free(ssl->session->peer);
522 ssl->session->peer = NULL;
523 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
524 ssl->session->cert_chain = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamindf50eec2016-06-07 16:49:42 -0400527 if (SSL_IS_DTLS(ssl)) {
528 ssl->d1->handshake_read_seq = 0;
529 ssl->d1->handshake_write_seq = 0;
530 ssl->d1->next_handshake_write_seq = 0;
531 }
532
David Benjamin0d56f882015-12-19 17:05:56 -0500533 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700534
David Benjamin0d56f882015-12-19 17:05:56 -0500535 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjamin4e9cc712016-06-01 20:16:03 -0400537 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley75872532014-06-20 12:00:00 -0700538
Adam Langleyfcf25832014-12-18 17:42:32 -0800539 ret = 1;
540 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 default:
David Benjamin3570d732015-06-29 00:28:17 -0400543 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 ret = -1;
545 goto end;
546 }
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin4e9cc712016-06-01 20:16:03 -0400548 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
549 int new_state = ssl->state;
David Benjamin0d56f882015-12-19 17:05:56 -0500550 ssl->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400551 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamin0d56f882015-12-19 17:05:56 -0500552 ssl->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 }
554 skip = 0;
555 }
556
Adam Langley95c29f32014-06-20 12:00:00 -0700557end:
David Benjamin2755a3e2015-04-22 16:17:58 -0400558 BUF_MEM_free(buf);
David Benjamin4e9cc712016-06-01 20:16:03 -0400559 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 return ret;
561}
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamindf50eec2016-06-07 16:49:42 -0400563static int ssl3_get_initial_bytes(SSL *ssl) {
David Benjamin894f48c2015-07-24 23:48:52 -0400564 /* Read the first 5 bytes, the size of the TLS record header. This is
565 * sufficient to detect a V2ClientHello and ensures that we never read beyond
566 * the first record. */
David Benjamin0d56f882015-12-19 17:05:56 -0500567 int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 if (ret <= 0) {
569 return ret;
570 }
David Benjamin0d56f882015-12-19 17:05:56 -0500571 assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH);
572 const uint8_t *p = ssl_read_buffer(ssl);
David Benjamin4b755cb2014-12-12 03:58:07 -0500573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 /* Some dedicated error codes for protocol mixups should the application wish
575 * to interpret them differently. (These do not overlap with ClientHello or
576 * V2ClientHello.) */
577 if (strncmp("GET ", (const char *)p, 4) == 0 ||
578 strncmp("POST ", (const char *)p, 5) == 0 ||
579 strncmp("HEAD ", (const char *)p, 5) == 0 ||
580 strncmp("PUT ", (const char *)p, 4) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400581 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 return -1;
583 }
David Benjamin894f48c2015-07-24 23:48:52 -0400584 if (strncmp("CONNE", (const char *)p, 5) == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400585 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 return -1;
587 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500588
David Benjamin97760d52015-07-24 23:02:49 -0400589 /* Determine if this is a V2ClientHello. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO &&
591 p[3] >= SSL3_VERSION_MAJOR) {
592 /* This is a V2ClientHello. */
David Benjamin0d56f882015-12-19 17:05:56 -0500593 ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO;
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 return 1;
595 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500596
David Benjaminb8d28cf2015-07-28 21:34:45 -0400597 /* Fall through to the standard logic. */
David Benjamin0d56f882015-12-19 17:05:56 -0500598 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
David Benjamin97760d52015-07-24 23:02:49 -0400599 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800600}
David Benjamin4b755cb2014-12-12 03:58:07 -0500601
David Benjamindf50eec2016-06-07 16:49:42 -0400602static int ssl3_get_v2_client_hello(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 const uint8_t *p;
604 int ret;
605 CBS v2_client_hello, cipher_specs, session_id, challenge;
David Benjamina6338be2016-05-13 18:12:19 -0400606 size_t msg_length, rand_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 uint8_t msg_type;
608 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
609 CBB client_hello, hello_body, cipher_suites;
610 uint8_t random[SSL3_RANDOM_SIZE];
David Benjamin4b755cb2014-12-12 03:58:07 -0500611
David Benjamin45c6c3e2015-07-24 23:24:12 -0400612 /* Determine the length of the V2ClientHello. */
David Benjamin0d56f882015-12-19 17:05:56 -0500613 assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH);
614 p = ssl_read_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 msg_length = ((p[0] & 0x7f) << 8) | p[1];
616 if (msg_length > (1024 * 4)) {
David Benjamin3570d732015-06-29 00:28:17 -0400617 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 return -1;
619 }
David Benjamin894f48c2015-07-24 23:48:52 -0400620 if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
David Benjamin45c6c3e2015-07-24 23:24:12 -0400621 /* Reject lengths that are too short early. We have already read
David Benjamin894f48c2015-07-24 23:48:52 -0400622 * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
David Benjamin45c6c3e2015-07-24 23:24:12 -0400623 * (invalid) V2ClientHello which would be shorter than that. */
David Benjamin3570d732015-06-29 00:28:17 -0400624 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 return -1;
626 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500627
David Benjaminb8d28cf2015-07-28 21:34:45 -0400628 /* Read the remainder of the V2ClientHello. */
David Benjamin0d56f882015-12-19 17:05:56 -0500629 ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 if (ret <= 0) {
631 return ret;
632 }
David Benjamin0d56f882015-12-19 17:05:56 -0500633 assert(ssl_read_buffer_len(ssl) == msg_length + 2);
634 CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
David Benjamin4b755cb2014-12-12 03:58:07 -0500635
David Benjamin9550c3a2015-08-05 08:50:34 -0400636 /* The V2ClientHello without the length is incorporated into the handshake
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 * hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500638 if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
David Benjamin9550c3a2015-08-05 08:50:34 -0400639 CBS_len(&v2_client_hello))) {
David Benjaminfbdfefb2015-02-16 19:33:53 -0500640 return -1;
641 }
David Benjamin4e9cc712016-06-01 20:16:03 -0400642
643 ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0,
644 CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
David Benjamin4b755cb2014-12-12 03:58:07 -0500645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
647 !CBS_get_u16(&v2_client_hello, &version) ||
648 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
649 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
650 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
651 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
652 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
653 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
654 CBS_len(&v2_client_hello) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400655 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800656 return -1;
657 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500658
Adam Langleyfcf25832014-12-18 17:42:32 -0800659 /* msg_type has already been checked. */
660 assert(msg_type == SSL2_MT_CLIENT_HELLO);
David Benjamin4b755cb2014-12-12 03:58:07 -0500661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 /* The client_random is the V2ClientHello challenge. Truncate or
663 * left-pad with zeros as needed. */
664 memset(random, 0, SSL3_RANDOM_SIZE);
665 rand_len = CBS_len(&challenge);
666 if (rand_len > SSL3_RANDOM_SIZE) {
667 rand_len = SSL3_RANDOM_SIZE;
668 }
669 memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
670 rand_len);
David Benjamin4b755cb2014-12-12 03:58:07 -0500671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 /* Write out an equivalent SSLv3 ClientHello. */
David Benjamina8653202015-06-28 01:26:10 -0400673 CBB_zero(&client_hello);
David Benjamin0d56f882015-12-19 17:05:56 -0500674 if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
675 ssl->init_buf->max) ||
David Benjamina8653202015-06-28 01:26:10 -0400676 !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
678 !CBB_add_u16(&hello_body, version) ||
679 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
680 /* No session id. */
681 !CBB_add_u8(&hello_body, 0) ||
682 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
683 CBB_cleanup(&client_hello);
David Benjamin3570d732015-06-29 00:28:17 -0400684 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 return -1;
686 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500687
Adam Langleyfcf25832014-12-18 17:42:32 -0800688 /* Copy the cipher suites. */
689 while (CBS_len(&cipher_specs) > 0) {
690 uint32_t cipher_spec;
691 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
692 CBB_cleanup(&client_hello);
David Benjamin3570d732015-06-29 00:28:17 -0400693 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800694 return -1;
695 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500696
Adam Langleyfcf25832014-12-18 17:42:32 -0800697 /* Skip SSLv2 ciphers. */
698 if ((cipher_spec & 0xff0000) != 0) {
699 continue;
700 }
701 if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
702 CBB_cleanup(&client_hello);
David Benjamin3570d732015-06-29 00:28:17 -0400703 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 return -1;
705 }
706 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500707
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 /* Add the null compression scheme and finish. */
709 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
David Benjamina6338be2016-05-13 18:12:19 -0400710 !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800711 CBB_cleanup(&client_hello);
David Benjamin3570d732015-06-29 00:28:17 -0400712 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 return -1;
714 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716 /* Mark the message for "re"-use by the version-specific method. */
David Benjamin0d56f882015-12-19 17:05:56 -0500717 ssl->s3->tmp.reuse_message = 1;
718 ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
David Benjamina6338be2016-05-13 18:12:19 -0400719 ssl->s3->tmp.message_complete = 1;
David Benjamin4b755cb2014-12-12 03:58:07 -0500720
David Benjaminb8d28cf2015-07-28 21:34:45 -0400721 /* Consume and discard the V2ClientHello. */
David Benjamin0d56f882015-12-19 17:05:56 -0500722 ssl_read_buffer_consume(ssl, 2 + msg_length);
723 ssl_read_buffer_discard(ssl);
David Benjamin4b755cb2014-12-12 03:58:07 -0500724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 return 1;
726}
David Benjamin4b755cb2014-12-12 03:58:07 -0500727
David Benjamindf50eec2016-06-07 16:49:42 -0400728static int ssl3_get_client_hello(SSL *ssl) {
David Benjamin4b30b282015-05-26 16:37:58 -0400729 int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800730 long n;
731 const SSL_CIPHER *c;
732 STACK_OF(SSL_CIPHER) *ciphers = NULL;
733 struct ssl_early_callback_ctx early_ctx;
734 CBS client_hello;
735 uint16_t client_version;
736 CBS client_random, session_id, cipher_suites, compression_methods;
David Benjamine3aa1d92015-06-16 15:34:50 -0400737 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700738
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 /* We do this so that we will respond with our native type. If we are TLSv1
740 * and we get SSLv3, we will respond with TLSv1, This down switching should
741 * be handled by a different method. If we are SSLv3, we will respond with
742 * SSLv3, even if prompted with TLSv1. */
David Benjamin0d56f882015-12-19 17:05:56 -0500743 switch (ssl->state) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400745 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
746 ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langleyfcf25832014-12-18 17:42:32 -0800748 if (!ok) {
749 return n;
750 }
Adam Langley95c29f32014-06-20 12:00:00 -0700751
David Benjamin1e6d6df2016-05-13 18:28:17 -0400752 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 /* fallthrough */
David Benjamin1e6d6df2016-05-13 18:28:17 -0400754 case SSL3_ST_SR_CLNT_HELLO_B:
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 case SSL3_ST_SR_CLNT_HELLO_C:
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 /* We have previously parsed the ClientHello message, and can't call
757 * ssl_get_message again without hashing the message into the Finished
758 * digest again. */
David Benjamin0d56f882015-12-19 17:05:56 -0500759 n = ssl->init_num;
Adam Langleydc9b1412014-06-20 12:00:00 -0700760
Adam Langleyfcf25832014-12-18 17:42:32 -0800761 memset(&early_ctx, 0, sizeof(early_ctx));
David Benjamin0d56f882015-12-19 17:05:56 -0500762 early_ctx.ssl = ssl;
763 early_ctx.client_hello = ssl->init_msg;
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 early_ctx.client_hello_len = n;
765 if (!ssl_early_callback_init(&early_ctx)) {
766 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400767 OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 goto f_err;
769 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400770
David Benjamin1e6d6df2016-05-13 18:28:17 -0400771 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B &&
David Benjamin0d56f882015-12-19 17:05:56 -0500772 ssl->ctx->select_certificate_cb != NULL) {
David Benjamin1e6d6df2016-05-13 18:28:17 -0400773 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjamin0d56f882015-12-19 17:05:56 -0500774 switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800775 case 0:
David Benjamin0d56f882015-12-19 17:05:56 -0500776 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
David Benjamin2fff5bf2015-02-09 04:32:28 -0500777 goto err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 case -1:
780 /* Connection rejected. */
781 al = SSL_AD_ACCESS_DENIED;
David Benjamin3570d732015-06-29 00:28:17 -0400782 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 goto f_err;
Adam Langleydc9b1412014-06-20 12:00:00 -0700784
Adam Langleyfcf25832014-12-18 17:42:32 -0800785 default:
786 /* fallthrough */;
787 }
788 }
David Benjamin1e6d6df2016-05-13 18:28:17 -0400789 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 break;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700791
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 default:
David Benjamin3570d732015-06-29 00:28:17 -0400793 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 return -1;
795 }
Adam Langley95c29f32014-06-20 12:00:00 -0700796
David Benjamin0d56f882015-12-19 17:05:56 -0500797 CBS_init(&client_hello, ssl->init_msg, n);
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 if (!CBS_get_u16(&client_hello, &client_version) ||
799 !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
800 !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
801 CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
802 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400803 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 goto f_err;
805 }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807 /* use version from inside client hello, not from record header (may differ:
808 * see RFC 2246, Appendix E, second paragraph) */
David Benjamin0d56f882015-12-19 17:05:56 -0500809 ssl->client_version = client_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 /* Load the client random. */
David Benjamin0d56f882015-12-19 17:05:56 -0500812 memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700813
David Benjamin0d56f882015-12-19 17:05:56 -0500814 if (SSL_IS_DTLS(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 CBS cookie;
Adam Langley95c29f32014-06-20 12:00:00 -0700816
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
818 CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
819 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400820 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 goto f_err;
822 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 }
David Benjamin8b8c0062014-11-23 02:47:52 -0500824
David Benjamine6df0542015-05-12 22:02:08 -0400825 /* Note: This codepath may run twice if |ssl_get_prev_session| completes
826 * asynchronously.
827 *
828 * TODO(davidben): Clean up the order of events around ClientHello
829 * processing. */
David Benjamin0d56f882015-12-19 17:05:56 -0500830 if (!ssl->s3->have_version) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 /* Select version to use */
David Benjamin0d56f882015-12-19 17:05:56 -0500832 uint16_t version = ssl3_get_mutual_version(ssl, client_version);
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 if (version == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
David Benjamin0d56f882015-12-19 17:05:56 -0500835 ssl->version = ssl->client_version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 al = SSL_AD_PROTOCOL_VERSION;
837 goto f_err;
838 }
David Benjamin0d56f882015-12-19 17:05:56 -0500839 ssl->version = version;
David Benjaminb83003e2015-12-30 17:39:26 -0500840 ssl->s3->enc_method = ssl3_get_enc_method(version);
841 assert(ssl->s3->enc_method != NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500842 /* At this point, the connection's version is known and |ssl->version| is
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500844 ssl->s3->have_version = 1;
845 } else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version)
846 : (ssl->client_version < ssl->version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400847 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 al = SSL_AD_PROTOCOL_VERSION;
849 goto f_err;
850 }
David Benjamin8b8c0062014-11-23 02:47:52 -0500851
David Benjamin0d56f882015-12-19 17:05:56 -0500852 ssl->hit = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -0400853 int send_new_ticket = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500854 switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
David Benjamine3aa1d92015-06-16 15:34:50 -0400855 case ssl_session_success:
856 break;
857 case ssl_session_error:
858 goto err;
859 case ssl_session_retry:
David Benjamin0d56f882015-12-19 17:05:56 -0500860 ssl->rwstate = SSL_PENDING_SESSION;
David Benjamine3aa1d92015-06-16 15:34:50 -0400861 goto err;
David Benjamin4b30b282015-05-26 16:37:58 -0400862 }
David Benjamin0d56f882015-12-19 17:05:56 -0500863 ssl->tlsext_ticket_expected = send_new_ticket;
David Benjamin4b30b282015-05-26 16:37:58 -0400864
Adam Langleyba5934b2015-06-02 10:50:35 -0700865 /* The EMS state is needed when making the resumption decision, but
866 * extensions are not normally parsed until later. This detects the EMS
867 * extension for the resumption decision and it's checked against the result
868 * of the normal parse later in this function. */
869 const uint8_t *ems_data;
870 size_t ems_len;
871 int have_extended_master_secret =
David Benjamin0d56f882015-12-19 17:05:56 -0500872 ssl->version != SSL3_VERSION &&
Adam Langleyba5934b2015-06-02 10:50:35 -0700873 SSL_early_callback_ctx_extension_get(&early_ctx,
874 TLSEXT_TYPE_extended_master_secret,
875 &ems_data, &ems_len) &&
876 ems_len == 0;
877
David Benjamine3aa1d92015-06-16 15:34:50 -0400878 if (session != NULL) {
879 if (session->extended_master_secret &&
Adam Langleyba5934b2015-06-02 10:50:35 -0700880 !have_extended_master_secret) {
881 /* A ClientHello without EMS that attempts to resume a session with EMS
882 * is fatal to the connection. */
883 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -0400884 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700885 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 }
Adam Langleyba5934b2015-06-02 10:50:35 -0700887
David Benjamin0d56f882015-12-19 17:05:56 -0500888 ssl->hit =
Adam Langleyba5934b2015-06-02 10:50:35 -0700889 /* Only resume if the session's version matches the negotiated version:
890 * most clients do not accept a mismatch. */
David Benjamin0d56f882015-12-19 17:05:56 -0500891 ssl->version == session->ssl_version &&
Adam Langleyba5934b2015-06-02 10:50:35 -0700892 /* If the client offers the EMS extension, but the previous session
893 * didn't use it, then negotiate a new session. */
David Benjamine3aa1d92015-06-16 15:34:50 -0400894 have_extended_master_secret == session->extended_master_secret;
Adam Langleyba5934b2015-06-02 10:50:35 -0700895 }
896
David Benjamin0d56f882015-12-19 17:05:56 -0500897 if (ssl->hit) {
David Benjamine3aa1d92015-06-16 15:34:50 -0400898 /* Use the new session. */
David Benjamin0d56f882015-12-19 17:05:56 -0500899 SSL_SESSION_free(ssl->session);
900 ssl->session = session;
David Benjamine3aa1d92015-06-16 15:34:50 -0400901 session = NULL;
902
David Benjamin0d56f882015-12-19 17:05:56 -0500903 ssl->verify_result = ssl->session->verify_result;
David Benjamin56380462015-10-10 14:59:09 -0400904 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500905 if (!ssl_get_new_session(ssl, 1 /* server */)) {
David Benjamin56380462015-10-10 14:59:09 -0400906 goto err;
907 }
908
909 /* Clear the session ID if we want the session to be single-use. */
David Benjamin0d56f882015-12-19 17:05:56 -0500910 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
911 ssl->session->session_id_length = 0;
David Benjamin56380462015-10-10 14:59:09 -0400912 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400914
David Benjamin0d56f882015-12-19 17:05:56 -0500915 if (ssl->ctx->dos_protection_cb != NULL &&
916 ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
Adam Langley524e7172015-02-20 16:04:00 -0800917 /* Connection rejected for DOS reasons. */
918 al = SSL_AD_ACCESS_DENIED;
David Benjamin3570d732015-06-29 00:28:17 -0400919 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
Adam Langley524e7172015-02-20 16:04:00 -0800920 goto f_err;
921 }
922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
David Benjamin93de5e52015-04-17 22:33:25 -0400924 CBS_len(&cipher_suites) == 0 ||
925 CBS_len(&cipher_suites) % 2 != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
927 CBS_len(&compression_methods) == 0) {
928 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400929 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 goto f_err;
931 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400932
David Benjamin0d56f882015-12-19 17:05:56 -0500933 ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites);
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (ciphers == NULL) {
935 goto err;
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 /* If it is a hit, check that the cipher is in the list. */
David Benjamin0d56f882015-12-19 17:05:56 -0500939 if (ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 size_t j;
941 int found_cipher = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500942 uint32_t id = ssl->session->cipher->id;
Adam Langley95c29f32014-06-20 12:00:00 -0700943
Adam Langleyfcf25832014-12-18 17:42:32 -0800944 for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
945 c = sk_SSL_CIPHER_value(ciphers, j);
946 if (c->id == id) {
947 found_cipher = 1;
948 break;
949 }
950 }
Adam Langley95c29f32014-06-20 12:00:00 -0700951
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 if (!found_cipher) {
953 /* we need to have the cipher in the cipher list if we are asked to reuse
954 * it */
955 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400956 OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 goto f_err;
958 }
959 }
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 /* Only null compression is supported. */
962 if (memchr(CBS_data(&compression_methods), 0,
963 CBS_len(&compression_methods)) == NULL) {
964 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400965 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 goto f_err;
967 }
David Benjamindc72ff72014-06-25 12:36:10 -0400968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 /* TLS extensions. */
David Benjamin0d56f882015-12-19 17:05:56 -0500970 if (ssl->version >= SSL3_VERSION &&
971 !ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400972 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langleyfcf25832014-12-18 17:42:32 -0800973 goto err;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 /* There should be nothing left over in the record. */
977 if (CBS_len(&client_hello) != 0) {
978 /* wrong packet length */
979 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400980 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800981 goto f_err;
982 }
Adam Langley95c29f32014-06-20 12:00:00 -0700983
David Benjamin0d56f882015-12-19 17:05:56 -0500984 if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -0700985 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400986 OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
Adam Langleyba5934b2015-06-02 10:50:35 -0700987 goto f_err;
988 }
989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
David Benjamin0d56f882015-12-19 17:05:56 -0500991 if (!ssl->hit) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 if (ciphers == NULL) {
993 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400994 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 goto f_err;
996 }
Adam Langley95c29f32014-06-20 12:00:00 -0700997
Adam Langleyfcf25832014-12-18 17:42:32 -0800998 /* Let cert callback update server certificates if required */
David Benjamin0d56f882015-12-19 17:05:56 -0500999 if (ssl->cert->cert_cb) {
1000 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 if (rv == 0) {
1002 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001003 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001004 goto f_err;
1005 }
1006 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001007 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 goto err;
1009 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 }
David Benjamin0d56f882015-12-19 17:05:56 -05001011 c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -08001012
1013 if (c == NULL) {
1014 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001015 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 goto f_err;
1017 }
David Benjamin0623bce2015-12-25 15:40:14 -05001018 ssl->session->cipher = c;
David Benjamin0d56f882015-12-19 17:05:56 -05001019 ssl->s3->tmp.new_cipher = c;
David Benjamin5c1ce292015-05-26 16:59:43 -04001020
1021 /* Determine whether to request a client certificate. */
David Benjamin0d56f882015-12-19 17:05:56 -05001022 ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -04001023 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -05001024 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1025 ssl->s3->tlsext_channel_id_valid) {
1026 ssl->s3->tmp.cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001027 }
David Benjaminc032dfa2016-05-12 14:54:57 -04001028 /* CertificateRequest may only be sent in certificate-based ciphers. */
1029 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001030 ssl->s3->tmp.cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001031 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 } else {
1033 /* Session-id reuse */
David Benjamin0d56f882015-12-19 17:05:56 -05001034 ssl->s3->tmp.new_cipher = ssl->session->cipher;
1035 ssl->s3->tmp.cert_request = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 }
1037
David Benjamin9550c3a2015-08-05 08:50:34 -04001038 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -05001039 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001040 goto f_err;
1041 }
1042
David Benjamin5c1ce292015-05-26 16:59:43 -04001043 /* In TLS 1.2, client authentication requires hashing the handshake transcript
1044 * under a different hash. Otherwise, release the handshake buffer. */
David Benjamina1e9cab2015-12-30 00:08:49 -05001045 if (!ssl->s3->tmp.cert_request ||
1046 ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
David Benjamin0d56f882015-12-19 17:05:56 -05001047 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 }
1049
1050 /* we now have the following setup;
1051 * client_random
1052 * cipher_list - our prefered list of ciphers
1053 * ciphers - the clients prefered list of ciphers
1054 * compression - basically ignored right now
1055 * ssl version is set - sslv3
David Benjamin0d56f882015-12-19 17:05:56 -05001056 * ssl->session - The ssl session has been setup.
1057 * ssl->hit - session reuse flag
1058 * ssl->tmp.new_cipher - the new cipher to use. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001059
David Benjamin0bd71eb2015-12-01 14:14:40 -05001060 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001061
1062 if (0) {
1063 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001064 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 }
1066
Adam Langley95c29f32014-06-20 12:00:00 -07001067err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001068 sk_SSL_CIPHER_free(ciphers);
David Benjamine3aa1d92015-06-16 15:34:50 -04001069 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 return ret;
1071}
Adam Langley95c29f32014-06-20 12:00:00 -07001072
David Benjamindf50eec2016-06-07 16:49:42 -04001073static int ssl3_send_server_hello(SSL *ssl) {
David Benjamin56380462015-10-10 14:59:09 -04001074 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
1075 return ssl_do_write(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001076 }
Adam Langley95c29f32014-06-20 12:00:00 -07001077
David Benjamin56380462015-10-10 14:59:09 -04001078 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1079
1080 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1081 * known attack while we fix ChannelID itself. */
1082 if (ssl->s3->tlsext_channel_id_valid &&
1083 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1084 ssl->s3->tlsext_channel_id_valid = 0;
1085 }
1086
1087 /* If this is a resumption and the original handshake didn't support
1088 * ChannelID then we didn't record the original handshake hashes in the
1089 * session and so cannot resume with ChannelIDs. */
1090 if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
1091 ssl->s3->tlsext_channel_id_valid = 0;
1092 }
1093
1094 if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
1095 1 /* server */)) {
1096 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1097 return -1;
1098 }
1099
1100 CBB cbb, session_id;
1101 size_t length;
1102 CBB_zero(&cbb);
1103 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1104 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1105 !CBB_add_u16(&cbb, ssl->version) ||
1106 !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1107 !CBB_add_u8_length_prefixed(&cbb, &session_id) ||
1108 !CBB_add_bytes(&session_id, ssl->session->session_id,
1109 ssl->session->session_id_length) ||
1110 !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1111 !CBB_add_u8(&cbb, 0 /* no compression */) ||
1112 !ssl_add_serverhello_tlsext(ssl, &cbb) ||
1113 !CBB_finish(&cbb, NULL, &length) ||
1114 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) {
1115 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1116 CBB_cleanup(&cbb);
1117 return -1;
1118 }
1119
1120 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
1121 return ssl_do_write(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001122}
Adam Langley95c29f32014-06-20 12:00:00 -07001123
David Benjamindf50eec2016-06-07 16:49:42 -04001124static int ssl3_send_certificate_status(SSL *ssl) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001125 if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) {
1126 CBB out, ocsp_response;
1127 size_t length;
1128
1129 CBB_zero(&out);
1130 if (!CBB_init_fixed(&out, ssl_handshake_start(ssl),
1131 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) ||
1132 !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) ||
1133 !CBB_add_u24_length_prefixed(&out, &ocsp_response) ||
1134 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1135 ssl->ctx->ocsp_response_length) ||
1136 !CBB_finish(&out, NULL, &length) ||
1137 !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) {
1138 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1139 CBB_cleanup(&out);
1140 return -1;
1141 }
1142
1143 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1144 }
1145
1146 /* SSL3_ST_SW_CERT_STATUS_B */
1147 return ssl_do_write(ssl);
1148}
1149
David Benjamindf50eec2016-06-07 16:49:42 -04001150static int ssl3_send_server_done(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -05001151 if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) {
1152 if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_DONE, 0)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001153 return -1;
1154 }
David Benjamin0d56f882015-12-19 17:05:56 -05001155 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 }
Adam Langley95c29f32014-06-20 12:00:00 -07001157
Adam Langleyfcf25832014-12-18 17:42:32 -08001158 /* SSL3_ST_SW_SRVR_DONE_B */
David Benjamin0d56f882015-12-19 17:05:56 -05001159 return ssl_do_write(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001160}
Adam Langley95c29f32014-06-20 12:00:00 -07001161
David Benjamindf50eec2016-06-07 16:49:42 -04001162static int ssl3_send_server_key_exchange(SSL *ssl) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001163 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
1164 return ssl_do_write(ssl);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001165 }
1166
David Benjamin2a0b3912015-12-18 01:01:21 -05001167 CBB cbb, child;
1168 if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl),
1169 ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) {
1170 goto err;
nagendra modadugu601448a2015-07-24 09:31:31 -07001171 }
1172
David Benjamin2a0b3912015-12-18 01:01:21 -05001173 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1174 /* This is the first iteration, so write parameters. */
1175 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1176 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001177
David Benjamin2a0b3912015-12-18 01:01:21 -05001178 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001179 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001180 size_t len =
1181 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1182 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1183 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1184 len)) {
1185 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001186 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001187 }
Adam Langley95c29f32014-06-20 12:00:00 -07001188
David Benjamin7061e282015-03-19 11:10:48 -04001189 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001190 /* Determine the group to use. */
1191 DH *params = ssl->cert->dh_tmp;
1192 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1193 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001195 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001196 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001197 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 goto err;
1199 }
David Benjamin4298d772015-12-19 00:18:25 -05001200 ssl->session->key_exchange_info = DH_num_bits(params);
David Benjamin2a0b3912015-12-18 01:01:21 -05001201
David Benjamin974c7ba2015-12-19 16:58:04 -05001202 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001203 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001204 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001205 goto err;
1206 }
David Benjamin9f226a52015-04-26 22:12:32 -04001207
David Benjamin974c7ba2015-12-19 16:58:04 -05001208 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001209 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001210 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001211 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001212 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001213 !CBB_add_u16_length_prefixed(&cbb, &child) ||
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001214 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001215 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001216 }
David Benjamin7061e282015-03-19 11:10:48 -04001217 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001218 /* Determine the group to use. */
1219 uint16_t group_id;
1220 if (!tls1_get_shared_group(ssl, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001221 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001222 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001223 goto err;
1224 }
Steven Valdezce902a92016-05-17 11:47:53 -04001225 ssl->session->key_exchange_info = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001226
David Benjamin4298d772015-12-19 00:18:25 -05001227 /* Set up ECDH, generate a key, and emit the public half. */
Steven Valdezce902a92016-05-17 11:47:53 -04001228 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001229 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001230 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001231 !CBB_add_u8_length_prefixed(&cbb, &child) ||
1232 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
1233 goto err;
1234 }
1235 } else if (alg_k & SSL_kCECPQ1) {
1236 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1) ||
1237 !CBB_add_u16_length_prefixed(&cbb, &child) ||
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001238 !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001239 goto err;
1240 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001241 } else {
1242 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001243 }
Adam Langley95c29f32014-06-20 12:00:00 -07001244
David Benjamin2a0b3912015-12-18 01:01:21 -05001245 /* Otherwise, restore |cbb| from the previous iteration.
1246 * TODO(davidben): When |ssl->init_buf| is gone, come up with a simpler
1247 * pattern. Probably keep the |CBB| around in the handshake state. */
1248 } else if (!CBB_did_write(&cbb, ssl->init_num - SSL_HM_HEADER_LENGTH(ssl))) {
1249 goto err;
1250 }
Adam Langley95c29f32014-06-20 12:00:00 -07001251
David Benjamin2a0b3912015-12-18 01:01:21 -05001252 /* Add a signature. */
David Benjaminc032dfa2016-05-12 14:54:57 -04001253 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001254 if (!ssl_has_private_key(ssl)) {
1255 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001256 goto err;
1257 }
Adam Langley95c29f32014-06-20 12:00:00 -07001258
David Benjamin2a0b3912015-12-18 01:01:21 -05001259 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
1260 size_t sig_len;
1261 enum ssl_private_key_result_t sign_result;
1262 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
1263 /* This is the first iteration, so set up the signature. Sample the
1264 * parameter length before adding a signature algorithm. */
1265 if (!CBB_flush(&cbb)) {
Adam Langleyc4f25ce2015-11-26 16:39:08 -08001266 goto err;
1267 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001268 size_t params_len = CBB_len(&cbb);
Adam Langley95c29f32014-06-20 12:00:00 -07001269
Adam Langleyfcf25832014-12-18 17:42:32 -08001270 /* Determine signature algorithm. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001271 const EVP_MD *md;
David Benjamina1e9cab2015-12-30 00:08:49 -05001272 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001273 md = tls1_choose_signing_digest(ssl);
David Benjaminfc825122015-12-18 01:57:43 -05001274 if (!tls12_add_sigandhash(ssl, &cbb, md)) {
David Benjamin3570d732015-06-29 00:28:17 -04001275 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -05001276 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1277 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001279 } else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001280 md = EVP_md5_sha1();
1281 } else {
1282 md = EVP_sha1();
1283 }
1284
David Benjamin2a0b3912015-12-18 01:01:21 -05001285 /* Compute the digest and sign it. */
1286 uint8_t digest[EVP_MAX_MD_SIZE];
Adam Langley5fb18c62015-12-22 09:37:19 -08001287 unsigned digest_len = 0;
David Benjamin2a0b3912015-12-18 01:01:21 -05001288 EVP_MD_CTX md_ctx;
1289 EVP_MD_CTX_init(&md_ctx);
1290 int digest_ret =
1291 EVP_DigestInit_ex(&md_ctx, md, NULL) &&
1292 EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) &&
1293 EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) &&
1294 EVP_DigestUpdate(&md_ctx, CBB_data(&cbb), params_len) &&
1295 EVP_DigestFinal_ex(&md_ctx, digest, &digest_len);
1296 EVP_MD_CTX_cleanup(&md_ctx);
David Benjaminfc825122015-12-18 01:57:43 -05001297 uint8_t *ptr;
David Benjamin2a0b3912015-12-18 01:01:21 -05001298 if (!digest_ret ||
1299 !CBB_add_u16_length_prefixed(&cbb, &child) ||
1300 !CBB_reserve(&child, &ptr, max_sig_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001301 goto err;
1302 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001303 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md,
1304 digest, digest_len);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001305 } else {
David Benjamin2a0b3912015-12-18 01:01:21 -05001306 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
1307
1308 /* Retry the signature. */
1309 uint8_t *ptr;
1310 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1311 !CBB_reserve(&child, &ptr, max_sig_len)) {
1312 goto err;
1313 }
1314 sign_result =
1315 ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001317
1318 switch (sign_result) {
1319 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001320 if (!CBB_did_write(&child, sig_len)) {
1321 goto err;
1322 }
1323 break;
1324 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001325 goto err;
1326 case ssl_private_key_retry:
1327 /* Discard the unfinished signature and save the state of |cbb| for the
1328 * next iteration. */
1329 CBB_discard_child(&cbb);
1330 ssl->init_num = SSL_HM_HEADER_LENGTH(ssl) + CBB_len(&cbb);
1331 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1332 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1333 goto err;
1334 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001335 }
1336
David Benjamin2a0b3912015-12-18 01:01:21 -05001337 size_t length;
1338 if (!CBB_finish(&cbb, NULL, &length) ||
1339 !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001340 goto err;
1341 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001342 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
1343 return ssl_do_write(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001344
Adam Langley95c29f32014-06-20 12:00:00 -07001345err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001346 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001347 return -1;
1348}
Adam Langley95c29f32014-06-20 12:00:00 -07001349
David Benjamindf50eec2016-06-07 16:49:42 -04001350static int ssl3_send_certificate_request(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001351 uint8_t *p, *d;
1352 size_t i;
1353 int j, nl, off, n;
1354 STACK_OF(X509_NAME) *sk = NULL;
1355 X509_NAME *name;
1356 BUF_MEM *buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001357
David Benjamin0d56f882015-12-19 17:05:56 -05001358 if (ssl->state == SSL3_ST_SW_CERT_REQ_A) {
1359 buf = ssl->init_buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001360
David Benjamin0d56f882015-12-19 17:05:56 -05001361 d = p = ssl_handshake_start(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -07001362
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 /* get the list of acceptable cert types */
1364 p++;
David Benjamin0d56f882015-12-19 17:05:56 -05001365 n = ssl3_get_req_cert_type(ssl, p);
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 d[0] = n;
1367 p += n;
1368 n++;
Adam Langley95c29f32014-06-20 12:00:00 -07001369
David Benjamina1e9cab2015-12-30 00:08:49 -05001370 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001371 const uint8_t *psigs;
David Benjamin0d56f882015-12-19 17:05:56 -05001372 nl = tls12_get_psigalgs(ssl, &psigs);
Adam Langleyfcf25832014-12-18 17:42:32 -08001373 s2n(nl, p);
1374 memcpy(p, psigs, nl);
1375 p += nl;
1376 n += nl + 2;
1377 }
Adam Langley95c29f32014-06-20 12:00:00 -07001378
Adam Langleyfcf25832014-12-18 17:42:32 -08001379 off = n;
1380 p += 2;
1381 n += 2;
Adam Langley95c29f32014-06-20 12:00:00 -07001382
David Benjamin0d56f882015-12-19 17:05:56 -05001383 sk = SSL_get_client_CA_list(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001384 nl = 0;
1385 if (sk != NULL) {
1386 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1387 name = sk_X509_NAME_value(sk, i);
1388 j = i2d_X509_NAME(name, NULL);
David Benjamin0d56f882015-12-19 17:05:56 -05001389 if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) {
David Benjamin3570d732015-06-29 00:28:17 -04001390 OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001391 goto err;
1392 }
David Benjamin0d56f882015-12-19 17:05:56 -05001393 p = ssl_handshake_start(ssl) + n;
Adam Langleyfcf25832014-12-18 17:42:32 -08001394 s2n(j, p);
1395 i2d_X509_NAME(name, &p);
1396 n += 2 + j;
1397 nl += 2 + j;
1398 }
1399 }
Adam Langley95c29f32014-06-20 12:00:00 -07001400
Adam Langleyfcf25832014-12-18 17:42:32 -08001401 /* else no CA names */
David Benjamin0d56f882015-12-19 17:05:56 -05001402 p = ssl_handshake_start(ssl) + off;
Adam Langleyfcf25832014-12-18 17:42:32 -08001403 s2n(nl, p);
1404
David Benjamin0d56f882015-12-19 17:05:56 -05001405 if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001406 goto err;
1407 }
David Benjamin0d56f882015-12-19 17:05:56 -05001408 ssl->state = SSL3_ST_SW_CERT_REQ_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08001409 }
Adam Langley95c29f32014-06-20 12:00:00 -07001410
Adam Langleyfcf25832014-12-18 17:42:32 -08001411 /* SSL3_ST_SW_CERT_REQ_B */
David Benjamin0d56f882015-12-19 17:05:56 -05001412 return ssl_do_write(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001413
Adam Langley95c29f32014-06-20 12:00:00 -07001414err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001415 return -1;
1416}
Adam Langley95c29f32014-06-20 12:00:00 -07001417
David Benjamindf50eec2016-06-07 16:49:42 -04001418static int ssl3_get_client_key_exchange(SSL *ssl) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001419 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001421 uint32_t alg_k;
1422 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001423 uint8_t *premaster_secret = NULL;
1424 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001426
David Benjamin4298d772015-12-19 00:18:25 -05001427 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001429
David Benjamin1e6d6df2016-05-13 18:28:17 -04001430 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001431 int ok;
David Benjamin0d56f882015-12-19 17:05:56 -05001432 const long n = ssl->method->ssl_get_message(
David Benjamin1e6d6df2016-05-13 18:28:17 -04001433 ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, ssl_hash_message, &ok);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001434 if (!ok) {
1435 return n;
1436 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin0d56f882015-12-19 17:05:56 -05001439 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1440 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1441 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 /* If using a PSK key exchange, prepare the pre-shared key. */
1444 if (alg_a & SSL_aPSK) {
1445 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1448 * then this is the only field in the message. */
1449 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1450 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001451 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001452 al = SSL_AD_DECODE_ERROR;
1453 goto f_err;
1454 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001455
David Benjamin0d56f882015-12-19 17:05:56 -05001456 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001457 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001458 al = SSL_AD_INTERNAL_ERROR;
1459 goto f_err;
1460 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1463 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001464 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001465 al = SSL_AD_ILLEGAL_PARAMETER;
1466 goto f_err;
1467 }
David Benjamin35c02302014-07-13 04:14:59 -04001468
David Benjamin0d56f882015-12-19 17:05:56 -05001469 if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001470 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001471 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001472 goto f_err;
1473 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001474
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 /* Look up the key for the identity. */
David Benjamin0d56f882015-12-19 17:05:56 -05001476 psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk,
1477 sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001479 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001480 al = SSL_AD_INTERNAL_ERROR;
1481 goto f_err;
1482 } else if (psk_len == 0) {
1483 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001484 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1486 goto f_err;
1487 }
1488 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 /* Depending on the key exchange method, compute |premaster_secret| and
1491 * |premaster_secret_len|. */
1492 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001493 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001494 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 decrypt_buf = OPENSSL_malloc(rsa_size);
1496 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001497 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 goto err;
1499 }
David Benjamin35c02302014-07-13 04:14:59 -04001500
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001501 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001502 size_t decrypt_len;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001503 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001504 if (!ssl_has_private_key(ssl) ||
1505 ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001506 al = SSL_AD_HANDSHAKE_FAILURE;
1507 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1508 goto f_err;
1509 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001510 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001511 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001512 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1513 &encrypted_premaster_secret) ||
1514 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001515 al = SSL_AD_DECODE_ERROR;
1516 OPENSSL_PUT_ERROR(SSL,
1517 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1518 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001519 }
1520 } else {
1521 encrypted_premaster_secret = client_key_exchange;
1522 }
1523
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001524 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1525 * timing-sensitive code below. */
1526 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001527 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001528 CBS_data(&encrypted_premaster_secret),
1529 CBS_len(&encrypted_premaster_secret));
1530 } else {
David Benjamin1e6d6df2016-05-13 18:28:17 -04001531 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001532 /* Complete async decrypt. */
1533 decrypt_result = ssl_private_key_decrypt_complete(
David Benjamin0d56f882015-12-19 17:05:56 -05001534 ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001536
1537 switch (decrypt_result) {
1538 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001539 break;
1540 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001541 goto err;
1542 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001543 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001544 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001545 goto err;
1546 }
1547
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001548 if (decrypt_len != rsa_size) {
1549 al = SSL_AD_DECRYPT_ERROR;
1550 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1551 goto f_err;
1552 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001553
David Benjamin3f5b43d2015-12-01 19:31:24 -05001554 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1555 * section 7.4.7.1. */
1556 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1557 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001559 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 goto err;
1561 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001562 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001563 goto err;
1564 }
1565
David Benjamin3f5b43d2015-12-01 19:31:24 -05001566 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1567 * publicly invalid. */
1568 if (decrypt_len < 11 + premaster_secret_len) {
1569 al = SSL_AD_DECRYPT_ERROR;
1570 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1571 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 }
David Benjamin35c02302014-07-13 04:14:59 -04001573
David Benjamin3f5b43d2015-12-01 19:31:24 -05001574 /* Check the padding. See RFC 3447, section 7.2.2. */
1575 size_t padding_len = decrypt_len - premaster_secret_len;
1576 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1577 constant_time_eq_int_8(decrypt_buf[1], 2);
1578 size_t i;
1579 for (i = 2; i < padding_len - 1; i++) {
1580 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1581 }
1582 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1583
1584 /* The premaster secret must begin with |client_version|. This too must be
1585 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1586 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin0d56f882015-12-19 17:05:56 -05001587 (unsigned)(ssl->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001588 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin0d56f882015-12-19 17:05:56 -05001589 (unsigned)(ssl->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001590
1591 /* Select, in constant time, either the decrypted premaster or the random
1592 * premaster based on |good|. */
1593 for (i = 0; i < premaster_secret_len; i++) {
1594 premaster_secret[i] = constant_time_select_8(
1595 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1596 }
1597
1598 OPENSSL_free(decrypt_buf);
1599 decrypt_buf = NULL;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001600 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001601 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001602 CBS peer_key;
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001603 if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
1604 &peer_key) ||
1605 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001607 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 goto f_err;
1609 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001610
David Benjamin4298d772015-12-19 00:18:25 -05001611 /* Compute the premaster. */
1612 uint8_t alert;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001613 if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
1614 &premaster_secret_len, &alert, CBS_data(&peer_key),
1615 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001616 al = alert;
1617 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 }
Adam Langley95c29f32014-06-20 12:00:00 -07001619
David Benjamin4298d772015-12-19 00:18:25 -05001620 /* The key exchange state may now be discarded. */
David Benjamin0d56f882015-12-19 17:05:56 -05001621 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 } else if (alg_k & SSL_kPSK) {
1623 /* For plain PSK, other_secret is a block of 0s with the same length as the
1624 * pre-shared key. */
1625 premaster_secret_len = psk_len;
1626 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1627 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001628 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001629 goto err;
1630 }
1631 memset(premaster_secret, 0, premaster_secret_len);
1632 } else {
1633 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001634 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001635 goto f_err;
1636 }
Adam Langley95c29f32014-06-20 12:00:00 -07001637
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1639 * pre-shared key. */
1640 if (alg_a & SSL_aPSK) {
1641 CBB new_premaster, child;
1642 uint8_t *new_data;
1643 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001644
David Benjamina8653202015-06-28 01:26:10 -04001645 CBB_zero(&new_premaster);
1646 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1647 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001648 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1649 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1650 !CBB_add_bytes(&child, psk, psk_len) ||
1651 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001652 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001653 CBB_cleanup(&new_premaster);
1654 goto err;
1655 }
David Benjamin14c83e72014-07-13 04:54:57 -04001656
Adam Langleyfcf25832014-12-18 17:42:32 -08001657 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1658 OPENSSL_free(premaster_secret);
1659 premaster_secret = new_data;
1660 premaster_secret_len = new_len;
1661 }
David Benjamin14c83e72014-07-13 04:54:57 -04001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 /* Compute the master secret */
David Benjaminf8d80712015-12-29 18:56:28 -05001664 ssl->session->master_key_length = tls1_generate_master_secret(
David Benjamin0d56f882015-12-19 17:05:56 -05001665 ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
1666 if (ssl->session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001667 goto err;
1668 }
David Benjamin0d56f882015-12-19 17:05:56 -05001669 ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001670
Adam Langleyfcf25832014-12-18 17:42:32 -08001671 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1672 OPENSSL_free(premaster_secret);
1673 return 1;
1674
Adam Langley95c29f32014-06-20 12:00:00 -07001675f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001676 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001677err:
David Benjamin4298d772015-12-19 00:18:25 -05001678 if (premaster_secret != NULL) {
1679 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 OPENSSL_free(premaster_secret);
1681 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001682 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001683
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 return -1;
1685}
Adam Langley95c29f32014-06-20 12:00:00 -07001686
David Benjamindf50eec2016-06-07 16:49:42 -04001687static int ssl3_get_cert_verify(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001688 int al, ok, ret = 0;
1689 long n;
1690 CBS certificate_verify, signature;
David Benjamin0d56f882015-12-19 17:05:56 -05001691 X509 *peer = ssl->session->peer;
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 EVP_PKEY *pkey = NULL;
1693 const EVP_MD *md = NULL;
1694 uint8_t digest[EVP_MAX_MD_SIZE];
1695 size_t digest_length;
1696 EVP_PKEY_CTX *pctx = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04001697
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 /* Only RSA and ECDSA client certificates are supported, so a
1699 * CertificateVerify is required if and only if there's a client certificate.
1700 * */
1701 if (peer == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001702 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 return 1;
1704 }
Adam Langley95c29f32014-06-20 12:00:00 -07001705
David Benjamin1e6d6df2016-05-13 18:28:17 -04001706 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1707 ssl_dont_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001708
Adam Langleyfcf25832014-12-18 17:42:32 -08001709 if (!ok) {
1710 return n;
1711 }
David Benjaminef865502014-08-24 02:48:34 -04001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 /* Filter out unsupported certificate types. */
1714 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05001715 if (pkey == NULL) {
1716 goto err;
1717 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001718 if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
1719 (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
1720 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
David Benjamin3570d732015-06-29 00:28:17 -04001721 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001722 goto f_err;
1723 }
David Benjamin6897dbe2014-07-12 20:18:28 -04001724
David Benjamin0d56f882015-12-19 17:05:56 -05001725 CBS_init(&certificate_verify, ssl->init_msg, n);
David Benjamin6897dbe2014-07-12 20:18:28 -04001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 /* Determine the digest type if needbe. */
David Benjamina1e9cab2015-12-30 00:08:49 -05001728 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjamin6e807652015-11-02 12:02:20 -05001729 uint8_t hash, signature_type;
1730 if (!CBS_get_u8(&certificate_verify, &hash) ||
1731 !CBS_get_u8(&certificate_verify, &signature_type)) {
1732 al = SSL_AD_DECODE_ERROR;
1733 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1734 goto f_err;
1735 }
David Benjamin0d56f882015-12-19 17:05:56 -05001736 if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) {
David Benjamin6e807652015-11-02 12:02:20 -05001737 goto f_err;
1738 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001739 }
David Benjamin854dd652014-08-26 00:32:30 -04001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 /* Compute the digest. */
David Benjamin0d56f882015-12-19 17:05:56 -05001742 if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 goto err;
1744 }
David Benjamin854dd652014-08-26 00:32:30 -04001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 /* The handshake buffer is no longer necessary, and we may hash the current
1747 * message.*/
David Benjamin0d56f882015-12-19 17:05:56 -05001748 ssl3_free_handshake_buffer(ssl);
1749 if (!ssl3_hash_current_message(ssl)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05001750 goto err;
1751 }
Adam Langley95c29f32014-06-20 12:00:00 -07001752
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 /* Parse and verify the signature. */
1754 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1755 CBS_len(&certificate_verify) != 0) {
1756 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001757 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 goto f_err;
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 pctx = EVP_PKEY_CTX_new(pkey, NULL);
1762 if (pctx == NULL) {
1763 goto err;
1764 }
David Benjaminbf82aed2016-03-01 22:57:40 -05001765 int sig_ok = EVP_PKEY_verify_init(pctx) &&
1766 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1767 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1768 digest, digest_length);
1769#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1770 sig_ok = 1;
1771 ERR_clear_error();
1772#endif
1773 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001775 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 goto f_err;
1777 }
1778
1779 ret = 1;
1780
1781 if (0) {
1782 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001783 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 }
1785
David Benjamin854dd652014-08-26 00:32:30 -04001786err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 EVP_PKEY_CTX_free(pctx);
1788 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 return ret;
1791}
Adam Langley95c29f32014-06-20 12:00:00 -07001792
David Benjamindf50eec2016-06-07 16:49:42 -04001793static int ssl3_get_client_certificate(SSL *ssl) {
David Benjamina8571592016-03-10 01:41:55 -05001794 int ok, al, ret = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001795 X509 *x = NULL;
1796 unsigned long n;
1797 STACK_OF(X509) *sk = NULL;
1798 SHA256_CTX sha256;
1799 CBS certificate_msg, certificate_list;
1800 int is_first_certificate = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001801
David Benjamindf28c3a2016-03-10 16:11:51 -05001802 assert(ssl->s3->tmp.cert_request);
David Benjamin1e6d6df2016-05-13 18:28:17 -04001803 n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07001804
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 if (!ok) {
1806 return n;
1807 }
Adam Langley95c29f32014-06-20 12:00:00 -07001808
David Benjamin0d56f882015-12-19 17:05:56 -05001809 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
David Benjamindf28c3a2016-03-10 16:11:51 -05001810 if (ssl->version == SSL3_VERSION &&
1811 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
1812 /* In SSL 3.0, the Certificate message is omitted to signal no certificate. */
1813 if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
1814 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
1815 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1816 al = SSL_AD_HANDSHAKE_FAILURE;
1817 goto f_err;
1818 }
1819
1820 ssl->s3->tmp.reuse_message = 1;
1821 return 1;
1822 }
1823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamindf28c3a2016-03-10 16:11:51 -05001825 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001826 goto f_err;
1827 }
David Benjamind5b1f842014-07-11 23:43:48 -04001828
David Benjamin0d56f882015-12-19 17:05:56 -05001829 CBS_init(&certificate_msg, ssl->init_msg, n);
David Benjamind5b1f842014-07-11 23:43:48 -04001830
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 sk = sk_X509_new_null();
1832 if (sk == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001833 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001834 goto err;
1835 }
Adam Langley95c29f32014-06-20 12:00:00 -07001836
Adam Langleyfcf25832014-12-18 17:42:32 -08001837 if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
1838 CBS_len(&certificate_msg) != 0) {
1839 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001840 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 goto f_err;
1842 }
Adam Langley95c29f32014-06-20 12:00:00 -07001843
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 while (CBS_len(&certificate_list) > 0) {
1845 CBS certificate;
1846 const uint8_t *data;
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Adam Langleyfcf25832014-12-18 17:42:32 -08001848 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
1849 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001850 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 goto f_err;
1852 }
Adam Langley95c29f32014-06-20 12:00:00 -07001853
David Benjamin0d56f882015-12-19 17:05:56 -05001854 if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001855 /* If this is the first certificate, and we don't want to keep peer
1856 * certificates in memory, then we hash it right away. */
1857 SHA256_Init(&sha256);
1858 SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
David Benjamin0d56f882015-12-19 17:05:56 -05001859 SHA256_Final(ssl->session->peer_sha256, &sha256);
1860 ssl->session->peer_sha256_valid = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 }
1862 is_first_certificate = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001863
David Benjaminaf073652015-11-12 23:09:30 -08001864 /* A u24 length cannot overflow a long. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001865 data = CBS_data(&certificate);
David Benjaminaf073652015-11-12 23:09:30 -08001866 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
Adam Langleyfcf25832014-12-18 17:42:32 -08001867 if (x == NULL) {
1868 al = SSL_AD_BAD_CERTIFICATE;
David Benjamin3570d732015-06-29 00:28:17 -04001869 OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001870 goto f_err;
1871 }
1872 if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
1873 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001874 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Adam Langleyfcf25832014-12-18 17:42:32 -08001875 goto f_err;
1876 }
1877 if (!sk_X509_push(sk, x)) {
David Benjamin3570d732015-06-29 00:28:17 -04001878 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 goto err;
1880 }
1881 x = NULL;
1882 }
1883
1884 if (sk_X509_num(sk) <= 0) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001885 /* No client certificate so the handshake buffer may be discarded. */
David Benjamin0d56f882015-12-19 17:05:56 -05001886 ssl3_free_handshake_buffer(ssl);
David Benjamin9550c3a2015-08-05 08:50:34 -04001887
Adam Langleyfcf25832014-12-18 17:42:32 -08001888 /* TLS does not mind 0 certs returned */
David Benjamin0d56f882015-12-19 17:05:56 -05001889 if (ssl->version == SSL3_VERSION) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001890 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001891 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001892 goto f_err;
David Benjamin0d56f882015-12-19 17:05:56 -05001893 } else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
David Benjamindf28c3a2016-03-10 16:11:51 -05001894 (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001895 /* Fail for TLS only if we required a certificate */
David Benjamin3570d732015-06-29 00:28:17 -04001896 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 al = SSL_AD_HANDSHAKE_FAILURE;
1898 goto f_err;
1899 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001900 } else {
David Benjamina8571592016-03-10 01:41:55 -05001901 if (ssl_verify_cert_chain(ssl, sk) <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001902 al = ssl_verify_alarm_type(ssl->verify_result);
David Benjamin3570d732015-06-29 00:28:17 -04001903 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 goto f_err;
1905 }
1906 }
1907
David Benjamin0d56f882015-12-19 17:05:56 -05001908 X509_free(ssl->session->peer);
1909 ssl->session->peer = sk_X509_shift(sk);
1910 ssl->session->verify_result = ssl->verify_result;
Adam Langleyfcf25832014-12-18 17:42:32 -08001911
David Benjamin0d56f882015-12-19 17:05:56 -05001912 sk_X509_pop_free(ssl->session->cert_chain, X509_free);
1913 ssl->session->cert_chain = sk;
Adam Langleyfcf25832014-12-18 17:42:32 -08001914 /* Inconsistency alert: cert_chain does *not* include the peer's own
1915 * certificate, while we do include it in s3_clnt.c */
1916
1917 sk = NULL;
1918
1919 ret = 1;
1920
1921 if (0) {
1922 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001923 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 }
1925
Adam Langley95c29f32014-06-20 12:00:00 -07001926err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001927 X509_free(x);
1928 sk_X509_pop_free(sk, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -08001929 return ret;
1930}
Adam Langley95c29f32014-06-20 12:00:00 -07001931
David Benjamindf50eec2016-06-07 16:49:42 -04001932static int ssl3_send_server_certificate(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -05001933 if (ssl->state == SSL3_ST_SW_CERT_A) {
1934 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin9d0847a2015-02-16 03:57:55 -05001935 return 0;
1936 }
David Benjamin0d56f882015-12-19 17:05:56 -05001937 ssl->state = SSL3_ST_SW_CERT_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 }
Adam Langley95c29f32014-06-20 12:00:00 -07001939
Adam Langleyfcf25832014-12-18 17:42:32 -08001940 /* SSL3_ST_SW_CERT_B */
David Benjamin0d56f882015-12-19 17:05:56 -05001941 return ssl_do_write(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001942}
Adam Langley95c29f32014-06-20 12:00:00 -07001943
Adam Langley95c29f32014-06-20 12:00:00 -07001944/* send a new session ticket (not necessarily for a new session) */
David Benjamindf50eec2016-06-07 16:49:42 -04001945static int ssl3_send_new_session_ticket(SSL *ssl) {
David Benjaminbcd37452015-03-09 14:48:28 -04001946 int ret = -1;
1947 uint8_t *session = NULL;
1948 size_t session_len;
1949 EVP_CIPHER_CTX ctx;
1950 HMAC_CTX hctx;
1951
1952 EVP_CIPHER_CTX_init(&ctx);
1953 HMAC_CTX_init(&hctx);
1954
David Benjamin0d56f882015-12-19 17:05:56 -05001955 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001956 uint8_t *p, *macstart;
1957 int len;
1958 unsigned int hlen;
David Benjamin0d56f882015-12-19 17:05:56 -05001959 SSL_CTX *tctx = ssl->initial_ctx;
Adam Langleyfcf25832014-12-18 17:42:32 -08001960 uint8_t iv[EVP_MAX_IV_LENGTH];
1961 uint8_t key_name[16];
1962 /* The maximum overhead of encrypting the session is 16 (key name) + IV +
1963 * one block of encryption overhead + HMAC. */
1964 const size_t max_ticket_overhead =
1965 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
Adam Langley95c29f32014-06-20 12:00:00 -07001966
Adam Langleyfcf25832014-12-18 17:42:32 -08001967 /* Serialize the SSL_SESSION to be encoded into the ticket. */
David Benjamin0d56f882015-12-19 17:05:56 -05001968 if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
1969 &session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04001970 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001971 }
Adam Langley95c29f32014-06-20 12:00:00 -07001972
Adam Langleyfcf25832014-12-18 17:42:32 -08001973 /* If the session is too long, emit a dummy value rather than abort the
1974 * connection. */
1975 if (session_len > 0xFFFF - max_ticket_overhead) {
1976 static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
1977 const size_t placeholder_len = strlen(kTicketPlaceholder);
David Benjamin3cac4502014-10-21 01:46:30 -04001978
Adam Langleyfcf25832014-12-18 17:42:32 -08001979 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04001980 session = NULL;
David Benjamin3cac4502014-10-21 01:46:30 -04001981
David Benjamin0d56f882015-12-19 17:05:56 -05001982 p = ssl_handshake_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001983 /* Emit ticket_lifetime_hint. */
1984 l2n(0, p);
1985 /* Emit ticket. */
1986 s2n(placeholder_len, p);
1987 memcpy(p, kTicketPlaceholder, placeholder_len);
1988 p += placeholder_len;
David Benjamin3cac4502014-10-21 01:46:30 -04001989
David Benjamin0d56f882015-12-19 17:05:56 -05001990 len = p - ssl_handshake_start(ssl);
Steven Valdezbbd43b52016-06-01 11:46:58 -04001991 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04001992 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05001993 }
David Benjamin0d56f882015-12-19 17:05:56 -05001994 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
1995 return ssl_do_write(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001996 }
Adam Langley95c29f32014-06-20 12:00:00 -07001997
Adam Langleyfcf25832014-12-18 17:42:32 -08001998 /* Grow buffer if need be: the length calculation is as follows:
1999 * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) +
2000 * max_ticket_overhead + * session_length */
David Benjamin0d56f882015-12-19 17:05:56 -05002001 if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 +
Adam Langleyfcf25832014-12-18 17:42:32 -08002002 max_ticket_overhead + session_len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002003 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002004 }
David Benjamin0d56f882015-12-19 17:05:56 -05002005 p = ssl_handshake_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002006 /* Initialize HMAC and cipher contexts. If callback present it does all the
2007 * work otherwise use generated values from parent ctx. */
2008 if (tctx->tlsext_ticket_key_cb) {
David Benjamin0d56f882015-12-19 17:05:56 -05002009 if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
David Benjaminadcc3952015-04-26 13:07:57 -04002010 1 /* encrypt */) < 0) {
David Benjaminbcd37452015-03-09 14:48:28 -04002011 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002012 }
2013 } else {
2014 if (!RAND_bytes(iv, 16) ||
2015 !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2016 tctx->tlsext_tick_aes_key, iv) ||
2017 !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
2018 NULL)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002019 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08002020 }
2021 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2022 }
Adam Langley95c29f32014-06-20 12:00:00 -07002023
Adam Langleyfcf25832014-12-18 17:42:32 -08002024 /* Ticket lifetime hint (advisory only): We leave this unspecified for
2025 * resumed session (for simplicity), and guess that tickets for new
2026 * sessions will live as long as their sessions. */
David Benjamin0d56f882015-12-19 17:05:56 -05002027 l2n(ssl->hit ? 0 : ssl->session->timeout, p);
Adam Langley95c29f32014-06-20 12:00:00 -07002028
Adam Langleyfcf25832014-12-18 17:42:32 -08002029 /* Skip ticket length for now */
2030 p += 2;
2031 /* Output key name */
2032 macstart = p;
2033 memcpy(p, key_name, 16);
2034 p += 16;
2035 /* output IV */
2036 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2037 p += EVP_CIPHER_CTX_iv_length(&ctx);
2038 /* Encrypt session data */
David Benjaminbcd37452015-03-09 14:48:28 -04002039 if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) {
2040 goto err;
2041 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002042 p += len;
David Benjaminbcd37452015-03-09 14:48:28 -04002043 if (!EVP_EncryptFinal_ex(&ctx, p, &len)) {
2044 goto err;
2045 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002046 p += len;
Adam Langley95c29f32014-06-20 12:00:00 -07002047
David Benjaminbcd37452015-03-09 14:48:28 -04002048 if (!HMAC_Update(&hctx, macstart, p - macstart) ||
2049 !HMAC_Final(&hctx, p, &hlen)) {
2050 goto err;
2051 }
Adam Langley95c29f32014-06-20 12:00:00 -07002052
Adam Langleyfcf25832014-12-18 17:42:32 -08002053 p += hlen;
2054 /* Now write out lengths: p points to end of data written */
2055 /* Total length */
David Benjamin0d56f882015-12-19 17:05:56 -05002056 len = p - ssl_handshake_start(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08002057 /* Skip ticket lifetime hint */
David Benjamin0d56f882015-12-19 17:05:56 -05002058 p = ssl_handshake_start(ssl) + 4;
Adam Langleyfcf25832014-12-18 17:42:32 -08002059 s2n(len - 6, p);
Steven Valdezbbd43b52016-06-01 11:46:58 -04002060 if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) {
David Benjaminbcd37452015-03-09 14:48:28 -04002061 goto err;
David Benjaminfbdfefb2015-02-16 19:33:53 -05002062 }
David Benjamin0d56f882015-12-19 17:05:56 -05002063 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
Adam Langleyfcf25832014-12-18 17:42:32 -08002064 }
Adam Langley95c29f32014-06-20 12:00:00 -07002065
Adam Langleyfcf25832014-12-18 17:42:32 -08002066 /* SSL3_ST_SW_SESSION_TICKET_B */
David Benjamin0d56f882015-12-19 17:05:56 -05002067 ret = ssl_do_write(ssl);
David Benjaminbcd37452015-03-09 14:48:28 -04002068
2069err:
David Benjamin2755a3e2015-04-22 16:17:58 -04002070 OPENSSL_free(session);
David Benjaminbcd37452015-03-09 14:48:28 -04002071 EVP_CIPHER_CTX_cleanup(&ctx);
2072 HMAC_CTX_cleanup(&hctx);
2073 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08002074}
Adam Langley95c29f32014-06-20 12:00:00 -07002075
Adam Langley95c29f32014-06-20 12:00:00 -07002076/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
2077 * sets the next_proto member in s if found */
David Benjamindf50eec2016-06-07 16:49:42 -04002078static int ssl3_get_next_proto(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002079 int ok;
2080 long n;
2081 CBS next_protocol, selected_protocol, padding;
Adam Langley95c29f32014-06-20 12:00:00 -07002082
Adam Langleyfcf25832014-12-18 17:42:32 -08002083 /* Clients cannot send a NextProtocol message if we didn't see the extension
2084 * in their ClientHello */
David Benjamin0d56f882015-12-19 17:05:56 -05002085 if (!ssl->s3->next_proto_neg_seen) {
David Benjamin3570d732015-06-29 00:28:17 -04002086 OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
Adam Langleyfcf25832014-12-18 17:42:32 -08002087 return -1;
2088 }
Adam Langley95c29f32014-06-20 12:00:00 -07002089
David Benjamin1e6d6df2016-05-13 18:28:17 -04002090 n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message,
2091 &ok);
Adam Langley95c29f32014-06-20 12:00:00 -07002092
Adam Langleyfcf25832014-12-18 17:42:32 -08002093 if (!ok) {
2094 return n;
2095 }
Adam Langley95c29f32014-06-20 12:00:00 -07002096
David Benjamin0d56f882015-12-19 17:05:56 -05002097 CBS_init(&next_protocol, ssl->init_msg, n);
Adam Langley95c29f32014-06-20 12:00:00 -07002098
Adam Langleyfcf25832014-12-18 17:42:32 -08002099 /* The payload looks like:
2100 * uint8 proto_len;
2101 * uint8 proto[proto_len];
2102 * uint8 padding_len;
2103 * uint8 padding[padding_len]; */
2104 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
2105 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
2106 CBS_len(&next_protocol) != 0 ||
David Benjamin79978df2015-12-25 15:56:49 -05002107 !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
2108 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002109 return 0;
2110 }
Adam Langley95c29f32014-06-20 12:00:00 -07002111
Adam Langleyfcf25832014-12-18 17:42:32 -08002112 return 1;
2113}
Adam Langley95c29f32014-06-20 12:00:00 -07002114
Adam Langley1258b6a2014-06-20 12:00:00 -07002115/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjamindf50eec2016-06-07 16:49:42 -04002116static int ssl3_get_channel_id(SSL *ssl) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002117 int ret = -1, ok;
2118 long n;
David Benjamind6a4ae92015-08-06 11:10:51 -04002119 uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
2120 size_t channel_id_hash_len;
Adam Langleyfcf25832014-12-18 17:42:32 -08002121 const uint8_t *p;
Adam Langley49c7af12015-07-10 14:33:46 -07002122 uint16_t extension_type;
Adam Langleyfcf25832014-12-18 17:42:32 -08002123 EC_GROUP *p256 = NULL;
2124 EC_KEY *key = NULL;
2125 EC_POINT *point = NULL;
2126 ECDSA_SIG sig;
2127 BIGNUM x, y;
2128 CBS encrypted_extensions, extension;
Adam Langley1258b6a2014-06-20 12:00:00 -07002129
Steven Valdez1eca1d32016-05-23 16:29:25 -04002130 n = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS,
David Benjamin1e6d6df2016-05-13 18:28:17 -04002131 ssl_dont_hash_message, &ok);
Adam Langley1258b6a2014-06-20 12:00:00 -07002132
Adam Langleyfcf25832014-12-18 17:42:32 -08002133 if (!ok) {
2134 return n;
2135 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002136
Adam Langleyfcf25832014-12-18 17:42:32 -08002137 /* Before incorporating the EncryptedExtensions message to the handshake
2138 * hash, compute the hash that should have been signed. */
David Benjamin0d56f882015-12-19 17:05:56 -05002139 if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002140 return -1;
2141 }
Adam Langleyfcf25832014-12-18 17:42:32 -08002142 assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
David Benjamin880b14e2014-08-25 22:35:07 -04002143
David Benjamin0d56f882015-12-19 17:05:56 -05002144 if (!ssl3_hash_current_message(ssl)) {
David Benjaminfbdfefb2015-02-16 19:33:53 -05002145 return -1;
2146 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002147
David Benjamin0d56f882015-12-19 17:05:56 -05002148 CBS_init(&encrypted_extensions, ssl->init_msg, n);
Adam Langley1258b6a2014-06-20 12:00:00 -07002149
Adam Langleyfcf25832014-12-18 17:42:32 -08002150 /* EncryptedExtensions could include multiple extensions, but the only
2151 * extension that could be negotiated is ChannelID, so there can only be one
2152 * entry.
2153 *
2154 * The payload looks like:
2155 * uint16 extension_type
2156 * uint16 extension_len;
2157 * uint8 x[32];
2158 * uint8 y[32];
2159 * uint8 r[32];
2160 * uint8 s[32]; */
Adam Langley1258b6a2014-06-20 12:00:00 -07002161
Adam Langleyfcf25832014-12-18 17:42:32 -08002162 if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
2163 !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
2164 CBS_len(&encrypted_extensions) != 0 ||
Adam Langley49c7af12015-07-10 14:33:46 -07002165 extension_type != TLSEXT_TYPE_channel_id ||
Adam Langleyfcf25832014-12-18 17:42:32 -08002166 CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
David Benjamin3570d732015-06-29 00:28:17 -04002167 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
Adam Langleyfcf25832014-12-18 17:42:32 -08002168 return -1;
2169 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002170
Adam Langleyfcf25832014-12-18 17:42:32 -08002171 p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
2172 if (!p256) {
David Benjamin3570d732015-06-29 00:28:17 -04002173 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
Adam Langleyfcf25832014-12-18 17:42:32 -08002174 return -1;
2175 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002176
Adam Langleyfcf25832014-12-18 17:42:32 -08002177 BN_init(&x);
2178 BN_init(&y);
2179 sig.r = BN_new();
2180 sig.s = BN_new();
David Benjamin9d0847a2015-02-16 03:57:55 -05002181 if (sig.r == NULL || sig.s == NULL) {
2182 goto err;
2183 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002184
Adam Langleyfcf25832014-12-18 17:42:32 -08002185 p = CBS_data(&extension);
2186 if (BN_bin2bn(p + 0, 32, &x) == NULL ||
2187 BN_bin2bn(p + 32, 32, &y) == NULL ||
2188 BN_bin2bn(p + 64, 32, sig.r) == NULL ||
2189 BN_bin2bn(p + 96, 32, sig.s) == NULL) {
2190 goto err;
2191 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002192
Adam Langleyfcf25832014-12-18 17:42:32 -08002193 point = EC_POINT_new(p256);
David Benjamin0d56f882015-12-19 17:05:56 -05002194 if (!point ||
2195 !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08002196 goto err;
2197 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002198
Adam Langleyfcf25832014-12-18 17:42:32 -08002199 key = EC_KEY_new();
2200 if (!key || !EC_KEY_set_group(key, p256) ||
2201 !EC_KEY_set_public_key(key, point)) {
2202 goto err;
2203 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002204
Adam Langleyfcf25832014-12-18 17:42:32 -08002205 /* We stored the handshake hash in |tlsext_channel_id| the first time that we
2206 * were called. */
2207 if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
David Benjamin3570d732015-06-29 00:28:17 -04002208 OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
David Benjamin0d56f882015-12-19 17:05:56 -05002209 ssl->s3->tlsext_channel_id_valid = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08002210 goto err;
2211 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002212
David Benjamin0d56f882015-12-19 17:05:56 -05002213 memcpy(ssl->s3->tlsext_channel_id, p, 64);
Adam Langleyfcf25832014-12-18 17:42:32 -08002214 ret = 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07002215
2216err:
Adam Langleyfcf25832014-12-18 17:42:32 -08002217 BN_free(&x);
2218 BN_free(&y);
2219 BN_free(sig.r);
2220 BN_free(sig.s);
David Benjamin2755a3e2015-04-22 16:17:58 -04002221 EC_KEY_free(key);
2222 EC_POINT_free(point);
2223 EC_GROUP_free(p256);
Adam Langleyfcf25832014-12-18 17:42:32 -08002224 return ret;
2225}