blob: a4396f40be8060cbbab611aafe905b04b427e281 [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>
168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
David Benjaminc3c88822016-11-14 10:32:04 +0900174static int ssl3_get_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
176static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
181static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
183static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
184static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
185static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
186static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400187
David Benjamince8c9d22016-11-14 10:45:16 +0900188int ssl3_accept(SSL_HANDSHAKE *hs) {
189 SSL *const ssl = hs->ssl;
David Benjamin3fa27772015-04-17 22:32:19 -0400190 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400192 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin0d56f882015-12-19 17:05:56 -0500194 assert(ssl->handshake_func == ssl3_accept);
195 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 for (;;) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500198 state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400201 case SSL_ST_INIT:
David Benjamincb0c29f2016-12-12 17:00:50 -0500202 hs->state = SSL_ST_ACCEPT;
David Benjaminfc0c9d92016-09-01 01:33:33 -0400203 skip = 1;
204 break;
205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 case SSL_ST_ACCEPT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400207 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin6a08da22015-05-08 22:58:12 -0400209 /* Enable a write buffer. This groups handshake messages within a flight
210 * into a single write. */
David Benjaminb095f0f2016-05-05 21:50:24 -0400211 if (!ssl_init_wbio_buffer(ssl)) {
David Benjamin6a08da22015-05-08 22:58:12 -0400212 ret = -1;
213 goto end;
214 }
215
David Benjamin0d56f882015-12-19 17:05:56 -0500216 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400217 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 ret = -1;
219 goto end;
220 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500221
David Benjamincb0c29f2016-12-12 17:00:50 -0500222 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 case SSL3_ST_SR_CLNT_HELLO_A:
226 case SSL3_ST_SR_CLNT_HELLO_B:
227 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin25fe85b2016-08-09 20:00:32 -0400228 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin4eb95cc2016-11-16 17:08:23 +0900229 case SSL3_ST_SR_CLNT_HELLO_E:
David Benjaminc3c88822016-11-14 10:32:04 +0900230 ret = ssl3_get_client_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500231 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400232 break;
233 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 if (ret <= 0) {
235 goto end;
236 }
David Benjamindf50eec2016-06-07 16:49:42 -0400237 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500238 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800239 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 case SSL3_ST_SW_SRVR_HELLO_A:
242 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900243 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 if (ret <= 0) {
245 goto end;
246 }
Steven Valdez87eab492016-06-27 16:34:59 -0400247 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500248 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500250 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 case SSL3_ST_SW_CERT_A:
255 case SSL3_ST_SW_CERT_B:
David Benjamin3d458dc2016-09-12 22:40:27 +0000256 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900257 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 if (ret <= 0) {
259 goto end;
260 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 } else {
262 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500264 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100267 case SSL3_ST_SW_CERT_STATUS_A:
268 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900269 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900270 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400271 if (ret <= 0) {
272 goto end;
273 }
274 } else {
275 skip = 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100276 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500277 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100278 break;
279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 case SSL3_ST_SW_KEY_EXCH_A:
281 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700282 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500283 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamindf50eec2016-06-07 16:49:42 -0400285 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500286 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
287 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900288 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500289 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500291 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 } else {
293 skip = 1;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
David Benjamincb0c29f2016-12-12 17:00:50 -0500296 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 case SSL3_ST_SW_CERT_REQ_A:
300 case SSL3_ST_SW_CERT_REQ_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900301 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900302 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 if (ret <= 0) {
304 goto end;
305 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400306 } else {
307 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500309 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 case SSL3_ST_SW_SRVR_DONE_A:
313 case SSL3_ST_SW_SRVR_DONE_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900314 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 if (ret <= 0) {
316 goto end;
317 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500318 hs->next_state = SSL3_ST_SR_CERT_A;
319 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900323 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900324 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 if (ret <= 0) {
326 goto end;
327 }
328 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500329 hs->state = SSL3_ST_SR_KEY_EXCH_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_SR_KEY_EXCH_A:
333 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900334 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 if (ret <= 0) {
336 goto end;
337 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500338 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900342 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 if (ret <= 0) {
344 goto end;
345 }
Adam Langley95c29f32014-06-20 12:00:00 -0700346
David Benjamincb0c29f2016-12-12 17:00:50 -0500347 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamina41280d2015-11-26 02:16:49 -0500350 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400351 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500352 if (ret <= 0) {
353 goto end;
354 }
Adam Langley95c29f32014-06-20 12:00:00 -0700355
David Benjamin67739722016-11-17 17:03:59 +0900356 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 ret = -1;
358 goto end;
359 }
David Benjamina41280d2015-11-26 02:16:49 -0500360
David Benjamincb0c29f2016-12-12 17:00:50 -0500361 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700363
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900365 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900366 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400367 if (ret <= 0) {
368 goto end;
369 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400371 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500373 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400377 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900378 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400379 if (ret <= 0) {
380 goto end;
381 }
382 } else {
383 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500385 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900389 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 if (ret <= 0) {
391 goto end;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
David Benjamindf50eec2016-06-07 16:49:42 -0400394 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400395 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500396 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500398 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 }
David Benjamindf50eec2016-06-07 16:49:42 -0400400
Steven Valdez87eab492016-06-27 16:34:59 -0400401 /* If this is a full handshake with ChannelID then record the handshake
402 * hashes in |ssl->s3->new_session| in case we need them to verify a
403 * ChannelID signature on a resumption of this session in the future. */
404 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
David Benjamin0d56f882015-12-19 17:05:56 -0500405 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 if (ret <= 0) {
407 goto end;
408 }
409 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 case SSL3_ST_SW_SESSION_TICKET_A:
413 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamince8c9d22016-11-14 10:45:16 +0900414 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900415 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400416 if (ret <= 0) {
417 goto end;
418 }
419 } else {
420 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500422 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
David Benjamin352d0a92016-06-28 11:22:02 -0400425 case SSL3_ST_SW_CHANGE:
426 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 if (ret <= 0) {
428 goto end;
429 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500430 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
David Benjamin67739722016-11-17 17:03:59 +0900432 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 ret = -1;
434 goto end;
435 }
436 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 case SSL3_ST_SW_FINISHED_A:
439 case SSL3_ST_SW_FINISHED_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900440 ret = ssl3_send_finished(hs, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500441 SSL3_ST_SW_FINISHED_B);
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 if (ret <= 0) {
443 goto end;
444 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500445 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400446 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500447 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500449 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700452
David Benjamin9f1dc822016-06-07 17:03:46 -0400453 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500454 ret = ssl->method->flush_flight(ssl);
455 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400456 goto end;
457 }
458
David Benjamincb0c29f2016-12-12 17:00:50 -0500459 hs->state = hs->next_state;
460 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400461 ssl->method->expect_flight(ssl);
462 }
463 break;
464
Steven Valdez143e8b32016-07-11 13:19:03 -0400465 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900466 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400467 if (ret <= 0) {
468 goto end;
469 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500470 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400471 break;
472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400474 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Steven Valdez87eab492016-06-27 16:34:59 -0400476 /* If we aren't retaining peer certificates then we can discard it
477 * now. */
478 if (ssl->s3->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900479 ssl->retain_only_sha256_of_client_certs) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800480 X509_free(ssl->s3->new_session->x509_peer);
481 ssl->s3->new_session->x509_peer = NULL;
482 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
483 ssl->s3->new_session->x509_chain = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400484 }
485
486 SSL_SESSION_free(ssl->s3->established_session);
487 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400488 SSL_SESSION_up_ref(ssl->session);
489 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400490 } else {
491 ssl->s3->established_session = ssl->s3->new_session;
492 ssl->s3->established_session->not_resumable = 0;
493 ssl->s3->new_session = NULL;
494 }
495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500497 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700498
David Benjamin78476f62016-11-12 11:20:55 +0900499 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900500 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900501
David Benjamin4e9cc712016-06-01 20:16:03 -0400502 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 ret = 1;
504 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 default:
David Benjamin3570d732015-06-29 00:28:17 -0400507 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800508 ret = -1;
509 goto end;
510 }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamincb0c29f2016-12-12 17:00:50 -0500512 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
513 int new_state = hs->state;
514 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400515 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500516 hs->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 }
518 skip = 0;
519 }
520
Adam Langley95c29f32014-06-20 12:00:00 -0700521end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400522 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 return ret;
524}
Adam Langley95c29f32014-06-20 12:00:00 -0700525
David Benjamin731058e2016-12-03 23:15:13 -0500526int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
527 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400528 CBS cipher_suites;
529 CBS_init(&cipher_suites, client_hello->cipher_suites,
530 client_hello->cipher_suites_len);
531
532 while (CBS_len(&cipher_suites) > 0) {
533 uint16_t got_id;
534 if (!CBS_get_u16(&cipher_suites, &got_id)) {
535 return 0;
536 }
537
538 if (got_id == id) {
539 return 1;
540 }
541 }
542
543 return 0;
544}
545
David Benjaminf04c2e92016-12-06 13:35:25 -0500546static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500547 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500548 SSL *const ssl = hs->ssl;
David Benjamin25fe85b2016-08-09 20:00:32 -0400549 uint16_t min_version, max_version;
550 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
551 *out_alert = SSL_AD_PROTOCOL_VERSION;
552 return 0;
553 }
554
David Benjamin2dc02042016-09-19 19:57:37 -0400555 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400556 /* Check supported_versions extension if it is present. */
557 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500558 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
559 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400560 CBS versions;
561 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
562 CBS_len(&supported_versions) != 0 ||
563 CBS_len(&versions) == 0) {
564 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
565 *out_alert = SSL_AD_DECODE_ERROR;
566 return 0;
567 }
568
Brian Smithf85d3232016-10-28 10:34:06 -1000569 /* Choose the newest commonly-supported version advertised by the client.
570 * The client orders the versions according to its preferences, but we're
571 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400572 int found_version = 0;
573 while (CBS_len(&versions) != 0) {
574 uint16_t ext_version;
575 if (!CBS_get_u16(&versions, &ext_version)) {
576 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
577 *out_alert = SSL_AD_DECODE_ERROR;
578 return 0;
579 }
580 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
581 continue;
582 }
583 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000584 ext_version <= max_version &&
585 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400586 version = ext_version;
587 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400588 }
589 }
590
591 if (!found_version) {
592 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400593 }
594 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400595 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
596 * do not use this mechanism. */
597 if (SSL_is_dtls(ssl)) {
598 if (client_hello->version <= DTLS1_2_VERSION) {
599 version = TLS1_2_VERSION;
600 } else if (client_hello->version <= DTLS1_VERSION) {
601 version = TLS1_1_VERSION;
602 } else {
603 goto unsupported_protocol;
604 }
605 } else {
606 if (client_hello->version >= TLS1_2_VERSION) {
607 version = TLS1_2_VERSION;
608 } else if (client_hello->version >= TLS1_1_VERSION) {
609 version = TLS1_1_VERSION;
610 } else if (client_hello->version >= TLS1_VERSION) {
611 version = TLS1_VERSION;
612 } else if (client_hello->version >= SSL3_VERSION) {
613 version = SSL3_VERSION;
614 } else {
615 goto unsupported_protocol;
616 }
David Benjamin2dc02042016-09-19 19:57:37 -0400617 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400618
Steven Valdezfdd10992016-09-15 16:27:05 -0400619 /* Apply our minimum and maximum version. */
620 if (version > max_version) {
621 version = max_version;
622 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400623
Steven Valdezfdd10992016-09-15 16:27:05 -0400624 if (version < min_version) {
625 goto unsupported_protocol;
626 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400627 }
628
629 /* Handle FALLBACK_SCSV. */
630 if (ssl_client_cipher_list_contains_cipher(client_hello,
631 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
632 version < max_version) {
633 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
634 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
635 return 0;
636 }
637
David Benjaminf04c2e92016-12-06 13:35:25 -0500638 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400639 ssl->version = ssl->method->version_to_wire(version);
640 ssl->s3->enc_method = ssl3_get_enc_method(version);
641 assert(ssl->s3->enc_method != NULL);
642
643 /* At this point, the connection's version is known and |ssl->version| is
644 * fixed. Begin enforcing the record-layer version. */
645 ssl->s3->have_version = 1;
646
647 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400648
649unsupported_protocol:
650 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
651 *out_alert = SSL_AD_PROTOCOL_VERSION;
652 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400653}
654
David Benjamin2578b292016-12-03 23:19:55 -0500655static STACK_OF(SSL_CIPHER) *
656 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
657 CBS cipher_suites;
658 CBS_init(&cipher_suites, client_hello->cipher_suites,
659 client_hello->cipher_suites_len);
660
661 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
662 if (sk == NULL) {
663 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
664 goto err;
665 }
666
667 while (CBS_len(&cipher_suites) > 0) {
668 uint16_t cipher_suite;
669
670 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
671 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
672 goto err;
673 }
674
675 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
676 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
677 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
678 goto err;
679 }
680 }
681
682 return sk;
683
684err:
685 sk_SSL_CIPHER_free(sk);
686 return NULL;
687}
688
689/* ssl_get_compatible_server_ciphers determines the key exchange and
690 * authentication cipher suite masks compatible with the server configuration
691 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
692 * exchange mask and |*out_mask_a| to the authentication mask. */
693static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
694 uint32_t *out_mask_k,
695 uint32_t *out_mask_a) {
696 SSL *const ssl = hs->ssl;
697 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
698 *out_mask_k = SSL_kGENERIC;
699 *out_mask_a = SSL_aGENERIC;
700 return;
701 }
702
703 uint32_t mask_k = 0;
704 uint32_t mask_a = 0;
705
706 if (ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl)) {
707 int type = ssl_private_key_type(ssl);
708 if (type == NID_rsaEncryption) {
709 mask_k |= SSL_kRSA;
710 mask_a |= SSL_aRSA;
711 } else if (ssl_is_ecdsa_key_type(type)) {
712 mask_a |= SSL_aECDSA;
713 }
714 }
715
716 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
717 mask_k |= SSL_kDHE;
718 }
719
720 /* Check for a shared group to consider ECDHE ciphers. */
721 uint16_t unused;
722 if (tls1_get_shared_group(hs, &unused)) {
723 mask_k |= SSL_kECDHE;
724 }
725
David Benjamin2578b292016-12-03 23:19:55 -0500726 /* PSK requires a server callback. */
727 if (ssl->psk_server_callback != NULL) {
728 mask_k |= SSL_kPSK;
729 mask_a |= SSL_aPSK;
730 }
731
732 *out_mask_k = mask_k;
733 *out_mask_a = mask_a;
734}
735
736static const SSL_CIPHER *ssl3_choose_cipher(
737 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
738 const struct ssl_cipher_preference_list_st *server_pref) {
739 SSL *const ssl = hs->ssl;
740 const SSL_CIPHER *c, *ret = NULL;
741 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
742 int ok;
743 size_t cipher_index;
744 uint32_t alg_k, alg_a, mask_k, mask_a;
745 /* in_group_flags will either be NULL, or will point to an array of bytes
746 * which indicate equal-preference groups in the |prio| stack. See the
747 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
748 * struct. */
749 const uint8_t *in_group_flags;
750 /* group_min contains the minimal index so far found in a group, or -1 if no
751 * such value exists yet. */
752 int group_min = -1;
753
754 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
755 if (clnt == NULL) {
756 return NULL;
757 }
758
759 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
760 prio = srvr;
761 in_group_flags = server_pref->in_group_flags;
762 allow = clnt;
763 } else {
764 prio = clnt;
765 in_group_flags = NULL;
766 allow = srvr;
767 }
768
769 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
770
771 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
772 c = sk_SSL_CIPHER_value(prio, i);
773
774 ok = 1;
775
776 /* Check the TLS version. */
777 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
778 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
779 ok = 0;
780 }
781
782 alg_k = c->algorithm_mkey;
783 alg_a = c->algorithm_auth;
784
785 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
786
787 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
788 if (in_group_flags != NULL && in_group_flags[i] == 1) {
789 /* This element of |prio| is in a group. Update the minimum index found
790 * so far and continue looking. */
791 if (group_min == -1 || (size_t)group_min > cipher_index) {
792 group_min = cipher_index;
793 }
794 } else {
795 if (group_min != -1 && (size_t)group_min < cipher_index) {
796 cipher_index = group_min;
797 }
798 ret = sk_SSL_CIPHER_value(allow, cipher_index);
799 break;
800 }
801 }
802
803 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
804 /* We are about to leave a group, but we found a match in it, so that's
805 * our answer. */
806 ret = sk_SSL_CIPHER_value(allow, group_min);
807 break;
808 }
809 }
810
811 sk_SSL_CIPHER_free(clnt);
812 return ret;
813}
814
David Benjaminc3c88822016-11-14 10:32:04 +0900815static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
816 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400817 uint8_t al = SSL_AD_INTERNAL_ERROR;
818 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400819 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700820
David Benjamincb0c29f2016-12-12 17:00:50 -0500821 if (hs->state == SSL3_ST_SR_CLNT_HELLO_A) {
David Benjamine14ff062016-08-09 16:21:24 -0400822 /* The first time around, read the ClientHello. */
823 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
824 ssl_hash_message);
825 if (msg_ret <= 0) {
826 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700827 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400828
David Benjamincb0c29f2016-12-12 17:00:50 -0500829 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 }
Adam Langley95c29f32014-06-20 12:00:00 -0700831
David Benjamin731058e2016-12-03 23:15:13 -0500832 SSL_CLIENT_HELLO client_hello;
833 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
834 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400836 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800837 goto f_err;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
David Benjamincb0c29f2016-12-12 17:00:50 -0500840 if (hs->state == SSL3_ST_SR_CLNT_HELLO_B) {
David Benjamine14ff062016-08-09 16:21:24 -0400841 /* Run the early callback. */
842 if (ssl->ctx->select_certificate_cb != NULL) {
843 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
844 case 0:
845 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
846 goto err;
847
848 case -1:
849 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400850 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400851 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
852 goto f_err;
853
854 default:
855 /* fallthrough */;
856 }
857 }
Alessandro Ghedini958346a2016-12-20 19:42:15 +0000858 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjamine14ff062016-08-09 16:21:24 -0400859 }
860
David Benjamin25fe85b2016-08-09 20:00:32 -0400861 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500862 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500863 if (!negotiate_version(hs, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 goto f_err;
865 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400866
David Benjamin25fe85b2016-08-09 20:00:32 -0400867 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500868 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900869 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400870 return 1;
871 }
872 }
873
David Benjamincb0c29f2016-12-12 17:00:50 -0500874 if (hs->state == SSL3_ST_SR_CLNT_HELLO_C) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400875 /* Load the client random. */
876 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
877 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
878 return -1;
879 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500880 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
881 client_hello.random_len);
David Benjamin25fe85b2016-08-09 20:00:32 -0400882
David Benjamin4eb95cc2016-11-16 17:08:23 +0900883 /* Only null compression is supported. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500884 if (OPENSSL_memchr(client_hello.compression_methods, 0,
885 client_hello.compression_methods_len) == NULL) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900886 al = SSL_AD_ILLEGAL_PARAMETER;
887 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
888 goto f_err;
889 }
890
891 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500892 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900893 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
894 goto err;
895 }
896
David Benjamincb0c29f2016-12-12 17:00:50 -0500897 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900898 }
899
David Benjamincb0c29f2016-12-12 17:00:50 -0500900 if (hs->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400901 /* Call |cert_cb| to update server certificates if required. */
902 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500903 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 if (rv == 0) {
905 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400906 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 goto f_err;
908 }
909 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500910 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 goto err;
912 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800914
David Benjamin650aa1c2016-12-20 18:55:16 -0500915 if (!ssl_auto_chain_if_needed(ssl)) {
916 goto err;
917 }
918
David Benjaminf01f42a2016-11-16 19:05:33 +0900919 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
920 * certificate is finalized. */
921 ssl->s3->tmp.new_cipher =
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900922 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjaminf01f42a2016-11-16 19:05:33 +0900923 if (ssl->s3->tmp.new_cipher == NULL) {
924 al = SSL_AD_HANDSHAKE_FAILURE;
925 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
926 goto f_err;
927 }
928
David Benjamincb0c29f2016-12-12 17:00:50 -0500929 hs->state = SSL3_ST_SR_CLNT_HELLO_E;
David Benjamin34202b92016-11-16 19:07:53 +0900930 }
931
David Benjamincb0c29f2016-12-12 17:00:50 -0500932 assert(hs->state == SSL3_ST_SR_CLNT_HELLO_E);
David Benjamin34202b92016-11-16 19:07:53 +0900933
934 /* Determine whether we are doing session resumption. */
935 int tickets_supported = 0, renew_ticket = 0;
936 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
937 &client_hello)) {
938 case ssl_session_success:
939 break;
940 case ssl_session_error:
941 goto err;
942 case ssl_session_retry:
943 ssl->rwstate = SSL_PENDING_SESSION;
944 goto err;
945 }
946
947 if (session != NULL) {
948 if (session->extended_master_secret &&
949 !ssl->s3->tmp.extended_master_secret) {
950 /* A ClientHello without EMS that attempts to resume a session with EMS
951 * is fatal to the connection. */
952 al = SSL_AD_HANDSHAKE_FAILURE;
953 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
954 goto f_err;
955 }
956
957 if (!ssl_session_is_resumable(ssl, session) ||
958 /* If the client offers the EMS extension, but the previous session
959 * didn't use it, then negotiate a new session. */
960 ssl->s3->tmp.extended_master_secret !=
961 session->extended_master_secret) {
962 SSL_SESSION_free(session);
963 session = NULL;
964 }
965 }
966
967 if (session != NULL) {
968 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900969 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900970 ssl->session = session;
971 session = NULL;
972 ssl->s3->session_reused = 1;
973 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900974 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900975 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900976 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900977 goto err;
978 }
979
980 /* Clear the session ID if we want the session to be single-use. */
981 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
982 ssl->s3->new_session->session_id_length = 0;
983 }
984 }
985
986 if (ssl->ctx->dos_protection_cb != NULL &&
987 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
988 /* Connection rejected for DOS reasons. */
989 al = SSL_AD_INTERNAL_ERROR;
990 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
991 goto f_err;
992 }
993
David Benjaminf01f42a2016-11-16 19:05:33 +0900994 if (ssl->session == NULL) {
995 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400996
David Benjamin4eb95cc2016-11-16 17:08:23 +0900997 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900998 if (hs->hostname != NULL) {
999 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
David Benjamin4eb95cc2016-11-16 17:08:23 +09001000 if (ssl->s3->new_session->tlsext_hostname == NULL) {
1001 al = SSL_AD_INTERNAL_ERROR;
1002 goto f_err;
1003 }
1004 }
1005
David Benjamin5c1ce292015-05-26 16:59:43 -04001006 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +09001007 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -04001008 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -05001009 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1010 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +09001011 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001012 }
David Benjaminc032dfa2016-05-12 14:54:57 -04001013 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001014 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +09001015 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001016 }
Adam Langley37646832016-08-01 16:16:46 -07001017
David Benjaminc3c88822016-11-14 10:32:04 +09001018 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001019 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001020 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001021 ssl->s3->new_session->verify_result = X509_V_OK;
1022 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 }
1024
David Benjaminf01f42a2016-11-16 19:05:33 +09001025 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1026 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001027 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001028 goto f_err;
1029 }
1030
David Benjaminf01f42a2016-11-16 19:05:33 +09001031 /* Now that all parameters are known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -05001032 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001033 goto f_err;
1034 }
1035
Steven Valdez2b8415e2016-06-30 13:27:23 -04001036 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001037 if (!hs->cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -05001038 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 }
1040
David Benjamin0bd71eb2015-12-01 14:14:40 -05001041 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001042
1043 if (0) {
1044 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001045 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 }
1047
Adam Langley95c29f32014-06-20 12:00:00 -07001048err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001049 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 return ret;
1051}
Adam Langley95c29f32014-06-20 12:00:00 -07001052
David Benjaminc3c88822016-11-14 10:32:04 +09001053static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1054 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001055 if (hs->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -04001056 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
David Benjamincb0c29f2016-12-12 17:00:50 -05001059 assert(hs->state == SSL3_ST_SW_SRVR_HELLO_A);
David Benjamin56380462015-10-10 14:59:09 -04001060
1061 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1062 * known attack while we fix ChannelID itself. */
1063 if (ssl->s3->tlsext_channel_id_valid &&
1064 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1065 ssl->s3->tlsext_channel_id_valid = 0;
1066 }
1067
1068 /* If this is a resumption and the original handshake didn't support
1069 * ChannelID then we didn't record the original handshake hashes in the
1070 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001071 if (ssl->session != NULL &&
1072 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001073 ssl->s3->tlsext_channel_id_valid = 0;
1074 }
1075
David Benjamin721e8b72016-08-03 13:13:17 -04001076 struct timeval now;
1077 ssl_get_current_time(ssl, &now);
1078 ssl->s3->server_random[0] = now.tv_sec >> 24;
1079 ssl->s3->server_random[1] = now.tv_sec >> 16;
1080 ssl->s3->server_random[2] = now.tv_sec >> 8;
1081 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001082 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001083 return -1;
1084 }
1085
David Benjamin55108632016-08-11 22:01:18 -04001086 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1087 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001088
Steven Valdez87eab492016-06-27 16:34:59 -04001089 const SSL_SESSION *session = ssl->s3->new_session;
1090 if (ssl->session != NULL) {
1091 session = ssl->session;
1092 }
1093
David Benjamin75836432016-06-17 18:48:29 -04001094 CBB cbb, body, session_id;
1095 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1096 !CBB_add_u16(&body, ssl->version) ||
1097 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1098 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001099 !CBB_add_bytes(&session_id, session->session_id,
1100 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -04001101 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1102 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001103 !ssl_add_serverhello_tlsext(hs, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001104 !ssl_complete_message(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001105 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1106 CBB_cleanup(&cbb);
1107 return -1;
1108 }
1109
David Benjamincb0c29f2016-12-12 17:00:50 -05001110 hs->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -04001111 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001112}
Adam Langley95c29f32014-06-20 12:00:00 -07001113
David Benjaminc3c88822016-11-14 10:32:04 +09001114static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1115 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001116 if (hs->state == SSL3_ST_SW_CERT_B) {
David Benjamin75836432016-06-17 18:48:29 -04001117 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001118 }
1119
David Benjamin32a66d52016-07-13 22:03:11 -04001120 if (!ssl_has_certificate(ssl)) {
1121 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
1122 return 0;
1123 }
1124
David Benjamin75836432016-06-17 18:48:29 -04001125 if (!ssl3_output_cert_chain(ssl)) {
1126 return 0;
1127 }
David Benjamincb0c29f2016-12-12 17:00:50 -05001128 hs->state = SSL3_ST_SW_CERT_B;
David Benjamin75836432016-06-17 18:48:29 -04001129 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001130}
1131
David Benjaminc3c88822016-11-14 10:32:04 +09001132static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1133 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001134 if (hs->state == SSL3_ST_SW_CERT_STATUS_B) {
David Benjamin75836432016-06-17 18:48:29 -04001135 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001136 }
1137
David Benjamin75836432016-06-17 18:48:29 -04001138 CBB cbb, body, ocsp_response;
1139 if (!ssl->method->init_message(ssl, &cbb, &body,
1140 SSL3_MT_CERTIFICATE_STATUS) ||
1141 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1142 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001143 !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
1144 CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001145 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001146 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1147 CBB_cleanup(&cbb);
1148 return -1;
1149 }
1150
David Benjamincb0c29f2016-12-12 17:00:50 -05001151 hs->state = SSL3_ST_SW_CERT_STATUS_B;
David Benjamin75836432016-06-17 18:48:29 -04001152 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001153}
1154
David Benjaminc3c88822016-11-14 10:32:04 +09001155static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1156 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001157 if (hs->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjamin75836432016-06-17 18:48:29 -04001158 return ssl->method->write_message(ssl);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001159 }
1160
David Benjamin2a0b3912015-12-18 01:01:21 -05001161 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001162 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001163
David Benjaminc42acee2016-06-17 17:47:58 -04001164 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001165 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001166 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1167 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001168
David Benjaminc42acee2016-06-17 17:47:58 -04001169 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1170 if (!CBB_init(&cbb, 128)) {
1171 goto err;
1172 }
1173
David Benjamin2a0b3912015-12-18 01:01:21 -05001174 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001175 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001176 size_t len =
1177 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1178 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1179 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1180 len)) {
1181 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001182 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184
David Benjamin7061e282015-03-19 11:10:48 -04001185 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001186 /* Determine the group to use. */
1187 DH *params = ssl->cert->dh_tmp;
1188 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1189 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001190 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001191 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001192 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001193 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001194 goto err;
1195 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001196
David Benjamin974c7ba2015-12-19 16:58:04 -05001197 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001198 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001199 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001200 goto err;
1201 }
David Benjamin9f226a52015-04-26 22:12:32 -04001202
David Benjaminc3c88822016-11-14 10:32:04 +09001203 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001204 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001205 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001206 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001207 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001208 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001209 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001210 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001211 }
David Benjamin7061e282015-03-19 11:10:48 -04001212 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001213 /* Determine the group to use. */
1214 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001215 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001216 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001217 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001218 goto err;
1219 }
David Benjamin4882a6c2016-12-11 02:48:12 -05001220 ssl->s3->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001221
David Benjamin4298d772015-12-19 00:18:25 -05001222 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001223 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001224 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001225 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001226 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001227 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001228 goto err;
1229 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001230 } else {
1231 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001232 }
Adam Langley95c29f32014-06-20 12:00:00 -07001233
David Benjaminc3c88822016-11-14 10:32:04 +09001234 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001235 goto err;
1236 }
David Benjaminc42acee2016-06-17 17:47:58 -04001237 }
1238
1239 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001240 CBB body;
1241 if (!ssl->method->init_message(ssl, &cbb, &body,
1242 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001243 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001244 goto err;
1245 }
Adam Langley95c29f32014-06-20 12:00:00 -07001246
David Benjamin2a0b3912015-12-18 01:01:21 -05001247 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001248 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001249 if (!ssl_has_private_key(ssl)) {
1250 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001251 goto err;
1252 }
Adam Langley95c29f32014-06-20 12:00:00 -07001253
David Benjaminc42acee2016-06-17 17:47:58 -04001254 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001255 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001256 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001257 goto err;
1258 }
David Benjaminc42acee2016-06-17 17:47:58 -04001259 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001260 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001261 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1262 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1263 goto err;
1264 }
David Benjaminc42acee2016-06-17 17:47:58 -04001265 }
1266
1267 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001268 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001269 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001270 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001271 !CBB_reserve(&child, &ptr, max_sig_len)) {
1272 goto err;
1273 }
1274
David Benjamin2a0b3912015-12-18 01:01:21 -05001275 size_t sig_len;
1276 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001277 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001278 CBB transcript;
1279 uint8_t *transcript_data;
1280 size_t transcript_len;
1281 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001282 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1283 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1284 SSL3_RANDOM_SIZE) ||
1285 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1286 SSL3_RANDOM_SIZE) ||
1287 !CBB_add_bytes(&transcript, hs->server_params,
1288 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001289 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1290 CBB_cleanup(&transcript);
1291 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1292 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001293 goto err;
1294 }
1295
Steven Valdezf0451ca2016-06-29 13:16:27 -04001296 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001297 signature_algorithm, transcript_data,
1298 transcript_len);
1299 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001300 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001301 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001302 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001304
1305 switch (sign_result) {
1306 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001307 if (!CBB_did_write(&child, sig_len)) {
1308 goto err;
1309 }
1310 break;
1311 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001312 goto err;
1313 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001314 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001315 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001316 goto err;
1317 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001318 }
1319
Steven Valdez5eead162016-11-11 22:23:25 -05001320 if (!ssl_complete_message(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001321 goto err;
1322 }
David Benjaminc42acee2016-06-17 17:47:58 -04001323
David Benjaminc3c88822016-11-14 10:32:04 +09001324 OPENSSL_free(hs->server_params);
1325 hs->server_params = NULL;
1326 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001327
David Benjamincb0c29f2016-12-12 17:00:50 -05001328 hs->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjamin75836432016-06-17 18:48:29 -04001329 return ssl->method->write_message(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001330
Adam Langley95c29f32014-06-20 12:00:00 -07001331err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001332 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 return -1;
1334}
Adam Langley95c29f32014-06-20 12:00:00 -07001335
David Benjamin75836432016-06-17 18:48:29 -04001336static int add_cert_types(SSL *ssl, CBB *cbb) {
1337 /* Get configured signature algorithms. */
1338 int have_rsa_sign = 0;
1339 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001340 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001341 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1342 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001343 switch (sig_algs[i]) {
1344 case SSL_SIGN_RSA_PKCS1_SHA512:
1345 case SSL_SIGN_RSA_PKCS1_SHA384:
1346 case SSL_SIGN_RSA_PKCS1_SHA256:
1347 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001348 have_rsa_sign = 1;
1349 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001350
Steven Valdez02563852016-06-23 13:33:05 -04001351 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1352 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1353 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1354 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001355 have_ecdsa_sign = 1;
1356 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 }
Adam Langley95c29f32014-06-20 12:00:00 -07001359
David Benjamin75836432016-06-17 18:48:29 -04001360 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1361 return 0;
1362 }
1363
1364 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1365 * check for SSL_kECDH or SSL_kECDHE. */
1366 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1367 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1368 return 0;
1369 }
1370
1371 return 1;
1372}
1373
David Benjaminc3c88822016-11-14 10:32:04 +09001374static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1375 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001376 if (hs->state == SSL3_ST_SW_CERT_REQ_B) {
David Benjamin75836432016-06-17 18:48:29 -04001377 return ssl->method->write_message(ssl);
1378 }
1379
David Benjamin32a66d52016-07-13 22:03:11 -04001380 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001381 if (!ssl->method->init_message(ssl, &cbb, &body,
1382 SSL3_MT_CERTIFICATE_REQUEST) ||
1383 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1384 !add_cert_types(ssl, &cert_types)) {
1385 goto err;
1386 }
1387
1388 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001389 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001390 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001391 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001392 goto err;
1393 }
Steven Valdez02563852016-06-23 13:33:05 -04001394
David Benjamin0fc37ef2016-08-17 15:29:46 -04001395 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001396 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1397 goto err;
1398 }
1399 }
David Benjamin75836432016-06-17 18:48:29 -04001400 }
1401
David Benjamin32a66d52016-07-13 22:03:11 -04001402 if (!ssl_add_client_CA_list(ssl, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001403 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001404 goto err;
1405 }
1406
David Benjamincb0c29f2016-12-12 17:00:50 -05001407 hs->state = SSL3_ST_SW_CERT_REQ_B;
David Benjamin75836432016-06-17 18:48:29 -04001408 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001409
Adam Langley95c29f32014-06-20 12:00:00 -07001410err:
David Benjamin75836432016-06-17 18:48:29 -04001411 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1412 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001413 return -1;
1414}
Adam Langley95c29f32014-06-20 12:00:00 -07001415
David Benjaminc3c88822016-11-14 10:32:04 +09001416static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1417 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001418 if (hs->state == SSL3_ST_SW_SRVR_DONE_B) {
David Benjamin75836432016-06-17 18:48:29 -04001419 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001420 }
1421
David Benjamin75836432016-06-17 18:48:29 -04001422 CBB cbb, body;
1423 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001424 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001425 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1426 CBB_cleanup(&cbb);
1427 return -1;
1428 }
1429
David Benjamincb0c29f2016-12-12 17:00:50 -05001430 hs->state = SSL3_ST_SW_SRVR_DONE_B;
David Benjamin75836432016-06-17 18:48:29 -04001431 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001432}
1433
David Benjaminc3c88822016-11-14 10:32:04 +09001434static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1435 SSL *const ssl = hs->ssl;
1436 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001437
David Benjamin09eb6552016-07-08 14:32:11 -07001438 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1439 if (msg_ret <= 0) {
1440 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001441 }
1442
1443 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1444 if (ssl->version == SSL3_VERSION &&
1445 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001446 /* In SSL 3.0, the Certificate message is omitted to signal no
1447 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001448 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001449 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001450 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1451 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001452 }
1453
Adam Langley37646832016-08-01 16:16:46 -07001454 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001455 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001456 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001457 ssl->s3->tmp.reuse_message = 1;
1458 return 1;
1459 }
1460
David Benjamin9f1dc822016-06-07 17:03:46 -04001461 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001462 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1463 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001464 }
1465
David Benjamin5c900c82016-07-13 23:03:26 -04001466 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001467 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001468
1469 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001470 EVP_PKEY_free(hs->peer_pubkey);
1471 hs->peer_pubkey = NULL;
David Benjamin5c900c82016-07-13 23:03:26 -04001472 uint8_t alert;
Adam Langley68e71242016-12-12 11:06:16 -08001473 ssl->s3->new_session->certs =
Adam Langleyd5157222016-12-12 11:37:43 -08001474 ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
1475 ssl->retain_only_sha256_of_client_certs
1476 ? ssl->s3->new_session->peer_sha256
1477 : NULL,
Adam Langleyd519bf62016-12-12 11:16:44 -08001478 &certificate_msg, ssl->ctx->pool);
Adam Langley68e71242016-12-12 11:06:16 -08001479 if (ssl->s3->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001480 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001481 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001482 }
1483
Adam Langley68e71242016-12-12 11:06:16 -08001484 if (CBS_len(&certificate_msg) != 0 ||
1485 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001486 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001487 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001488 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001489 }
1490
Adam Langley68e71242016-12-12 11:06:16 -08001491 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001492 /* No client certificate so the handshake buffer may be discarded. */
1493 ssl3_free_handshake_buffer(ssl);
1494
David Benjamine455e512016-08-01 20:11:13 -04001495 /* In SSL 3.0, sending no certificate is signaled by omitting the
1496 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001497 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001498 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001499 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001500 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001501 }
1502
1503 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001504 /* Fail for TLS only if we required a certificate */
1505 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001506 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001507 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001508 }
Adam Langley37646832016-08-01 16:16:46 -07001509
1510 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001511 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001512 ssl->s3->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001513 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001514 }
1515
Adam Langley68e71242016-12-12 11:06:16 -08001516 /* The hash will have been filled in. */
1517 if (ssl->retain_only_sha256_of_client_certs) {
1518 ssl->s3->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001519 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001520
Adam Langley68e71242016-12-12 11:06:16 -08001521 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1522 ssl->s3->new_session->x509_chain)) {
1523 return -1;
1524 }
David Benjamin5c900c82016-07-13 23:03:26 -04001525 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001526}
1527
David Benjaminc3c88822016-11-14 10:32:04 +09001528static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1529 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001530 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001531 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001532 uint32_t alg_k;
1533 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 uint8_t *premaster_secret = NULL;
1535 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001536 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001537
David Benjamin4298d772015-12-19 00:18:25 -05001538 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001540
David Benjamincb0c29f2016-12-12 17:00:50 -05001541 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001542 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1543 ssl_hash_message);
1544 if (ret <= 0) {
1545 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001546 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001547 }
Adam Langley95c29f32014-06-20 12:00:00 -07001548
David Benjamin0d56f882015-12-19 17:05:56 -05001549 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1550 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1551 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001552
Adam Langleyfcf25832014-12-18 17:42:32 -08001553 /* If using a PSK key exchange, prepare the pre-shared key. */
1554 if (alg_a & SSL_aPSK) {
1555 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001556
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1558 * then this is the only field in the message. */
1559 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1560 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001561 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 al = SSL_AD_DECODE_ERROR;
1563 goto f_err;
1564 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001565
David Benjamin0d56f882015-12-19 17:05:56 -05001566 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001567 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 al = SSL_AD_INTERNAL_ERROR;
1569 goto f_err;
1570 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001571
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1573 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001574 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 al = SSL_AD_ILLEGAL_PARAMETER;
1576 goto f_err;
1577 }
David Benjamin35c02302014-07-13 04:14:59 -04001578
Steven Valdez87eab492016-06-27 16:34:59 -04001579 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001581 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 goto f_err;
1583 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001584
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001586 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1587 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001589 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 al = SSL_AD_INTERNAL_ERROR;
1591 goto f_err;
1592 } else if (psk_len == 0) {
1593 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001594 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1596 goto f_err;
1597 }
1598 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 /* Depending on the key exchange method, compute |premaster_secret| and
1601 * |premaster_secret_len|. */
1602 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001604 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 decrypt_buf = OPENSSL_malloc(rsa_size);
1606 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001607 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 goto err;
1609 }
David Benjamin35c02302014-07-13 04:14:59 -04001610
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001611 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001612 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001613 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001614 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001615 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001616 al = SSL_AD_HANDSHAKE_FAILURE;
1617 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1618 goto f_err;
1619 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001620 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001621 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001622 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1623 &encrypted_premaster_secret) ||
1624 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001625 al = SSL_AD_DECODE_ERROR;
1626 OPENSSL_PUT_ERROR(SSL,
1627 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1628 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001629 }
1630 } else {
1631 encrypted_premaster_secret = client_key_exchange;
1632 }
1633
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001634 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1635 * timing-sensitive code below. */
1636 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001637 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001638 CBS_data(&encrypted_premaster_secret),
1639 CBS_len(&encrypted_premaster_secret));
1640 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001641 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001642 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001643 decrypt_result =
1644 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001646
1647 switch (decrypt_result) {
1648 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001649 break;
1650 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001651 goto err;
1652 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001653 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001654 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001655 goto err;
1656 }
1657
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001658 if (decrypt_len != rsa_size) {
1659 al = SSL_AD_DECRYPT_ERROR;
1660 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1661 goto f_err;
1662 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001663
David Benjamin3f5b43d2015-12-01 19:31:24 -05001664 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1665 * section 7.4.7.1. */
1666 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1667 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001669 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001670 goto err;
1671 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001672 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001673 goto err;
1674 }
1675
David Benjamin3f5b43d2015-12-01 19:31:24 -05001676 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1677 * publicly invalid. */
1678 if (decrypt_len < 11 + premaster_secret_len) {
1679 al = SSL_AD_DECRYPT_ERROR;
1680 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1681 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 }
David Benjamin35c02302014-07-13 04:14:59 -04001683
David Benjamin3f5b43d2015-12-01 19:31:24 -05001684 /* Check the padding. See RFC 3447, section 7.2.2. */
1685 size_t padding_len = decrypt_len - premaster_secret_len;
1686 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1687 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001688 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001689 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1690 }
1691 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1692
1693 /* The premaster secret must begin with |client_version|. This too must be
1694 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1695 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001696 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001697 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001698 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001699
1700 /* Select, in constant time, either the decrypted premaster or the random
1701 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001702 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001703 premaster_secret[i] = constant_time_select_8(
1704 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1705 }
1706
1707 OPENSSL_free(decrypt_buf);
1708 decrypt_buf = NULL;
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001709 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001710 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001711 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001712 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001713 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001715 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001716 goto f_err;
1717 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001718
David Benjamin4298d772015-12-19 00:18:25 -05001719 /* Compute the premaster. */
1720 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001721 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001722 &premaster_secret_len, &alert, CBS_data(&peer_key),
1723 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001724 al = alert;
1725 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 }
Adam Langley95c29f32014-06-20 12:00:00 -07001727
David Benjamin4298d772015-12-19 00:18:25 -05001728 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001729 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 } else if (alg_k & SSL_kPSK) {
1731 /* For plain PSK, other_secret is a block of 0s with the same length as the
1732 * pre-shared key. */
1733 premaster_secret_len = psk_len;
1734 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1735 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001736 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 goto err;
1738 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001739 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001740 } else {
1741 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001742 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001743 goto f_err;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langleyfcf25832014-12-18 17:42:32 -08001746 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1747 * pre-shared key. */
1748 if (alg_a & SSL_aPSK) {
1749 CBB new_premaster, child;
1750 uint8_t *new_data;
1751 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001752
David Benjamina8653202015-06-28 01:26:10 -04001753 CBB_zero(&new_premaster);
1754 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1755 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001756 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1757 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1758 !CBB_add_bytes(&child, psk, psk_len) ||
1759 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001760 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 CBB_cleanup(&new_premaster);
1762 goto err;
1763 }
David Benjamin14c83e72014-07-13 04:54:57 -04001764
Adam Langleyfcf25832014-12-18 17:42:32 -08001765 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1766 OPENSSL_free(premaster_secret);
1767 premaster_secret = new_data;
1768 premaster_secret_len = new_len;
1769 }
David Benjamin14c83e72014-07-13 04:54:57 -04001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001772 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1773 ssl, ssl->s3->new_session->master_key, premaster_secret,
1774 premaster_secret_len);
1775 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 goto err;
1777 }
Steven Valdez87eab492016-06-27 16:34:59 -04001778 ssl->s3->new_session->extended_master_secret =
1779 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001780
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1782 OPENSSL_free(premaster_secret);
1783 return 1;
1784
Adam Langley95c29f32014-06-20 12:00:00 -07001785f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001786 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001787err:
David Benjamin4298d772015-12-19 00:18:25 -05001788 if (premaster_secret != NULL) {
1789 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 OPENSSL_free(premaster_secret);
1791 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001792 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001793
Adam Langleyfcf25832014-12-18 17:42:32 -08001794 return -1;
1795}
Adam Langley95c29f32014-06-20 12:00:00 -07001796
David Benjaminc3c88822016-11-14 10:32:04 +09001797static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1798 SSL *const ssl = hs->ssl;
Adam Langleyd5157222016-12-12 11:37:43 -08001799 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001801
Adam Langleyfcf25832014-12-18 17:42:32 -08001802 /* Only RSA and ECDSA client certificates are supported, so a
1803 * CertificateVerify is required if and only if there's a client certificate.
1804 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001805 if (hs->peer_pubkey == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001806 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001807 return 1;
1808 }
Adam Langley95c29f32014-06-20 12:00:00 -07001809
David Benjamin09eb6552016-07-08 14:32:11 -07001810 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1811 ssl_dont_hash_message);
1812 if (msg_ret <= 0) {
1813 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001814 }
David Benjaminef865502014-08-24 02:48:34 -04001815
David Benjamin09eb6552016-07-08 14:32:11 -07001816 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001817
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001819 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001820 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001821 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001822 al = SSL_AD_DECODE_ERROR;
1823 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1824 goto f_err;
1825 }
David Benjamin887c3002016-07-08 16:15:32 -07001826 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001827 goto f_err;
1828 }
David Benjaminf1050fd2016-12-13 20:05:36 -05001829 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001830 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001831 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001832 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001833 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001834 } else {
1835 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1836 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1837 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001838 }
David Benjamin854dd652014-08-26 00:32:30 -04001839
Adam Langleyfcf25832014-12-18 17:42:32 -08001840 /* Parse and verify the signature. */
1841 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1842 CBS_len(&certificate_verify) != 0) {
1843 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001844 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001845 goto f_err;
1846 }
Adam Langley95c29f32014-06-20 12:00:00 -07001847
Steven Valdez2b8415e2016-06-30 13:27:23 -04001848 int sig_ok;
1849 /* The SSL3 construction for CertificateVerify does not decompose into a
1850 * single final digest and signature, and must be special-cased. */
1851 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001852 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001853 uint8_t digest[EVP_MAX_MD_SIZE];
1854 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001855 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001856 signature_algorithm)) {
1857 goto err;
1858 }
1859
Adam Langleyd5157222016-12-12 11:37:43 -08001860 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001861 sig_ok = pctx != NULL &&
1862 EVP_PKEY_verify_init(pctx) &&
1863 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1864 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1865 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001866 EVP_PKEY_CTX_free(pctx);
1867 } else {
1868 sig_ok = ssl_public_key_verify(
1869 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001870 hs->peer_pubkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001871 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001872 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001873
David Benjaminbf82aed2016-03-01 22:57:40 -05001874#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1875 sig_ok = 1;
1876 ERR_clear_error();
1877#endif
1878 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001879 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001880 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001881 goto f_err;
1882 }
1883
Steven Valdez2b8415e2016-06-30 13:27:23 -04001884 /* The handshake buffer is no longer necessary, and we may hash the current
1885 * message.*/
1886 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001887 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001888 goto err;
1889 }
1890
Adam Langleyd5157222016-12-12 11:37:43 -08001891 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001892
Adam Langleyd5157222016-12-12 11:37:43 -08001893f_err:
1894 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin854dd652014-08-26 00:32:30 -04001895err:
Adam Langleyd5157222016-12-12 11:37:43 -08001896 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001897}
Adam Langley95c29f32014-06-20 12:00:00 -07001898
Adam Langley95c29f32014-06-20 12:00:00 -07001899/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1900 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001901static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1902 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001903 int ret =
1904 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1905 if (ret <= 0) {
1906 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
David Benjamin09eb6552016-07-08 14:32:11 -07001909 CBS next_protocol, selected_protocol, padding;
1910 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001911 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1912 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001913 CBS_len(&next_protocol) != 0) {
1914 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1915 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1916 return 0;
1917 }
1918
1919 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001920 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001921 return 0;
1922 }
Adam Langley95c29f32014-06-20 12:00:00 -07001923
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 return 1;
1925}
Adam Langley95c29f32014-06-20 12:00:00 -07001926
Adam Langley1258b6a2014-06-20 12:00:00 -07001927/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001928static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1929 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001930 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1931 ssl_dont_hash_message);
1932 if (msg_ret <= 0) {
1933 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001934 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001935
Nick Harper60a85cb2016-09-23 16:25:11 -07001936 if (!tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001937 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001938 return -1;
1939 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001940 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001941}
David Benjamin9f1dc822016-06-07 17:03:46 -04001942
David Benjaminc3c88822016-11-14 10:32:04 +09001943static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1944 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001945 if (hs->state == SSL3_ST_SW_SESSION_TICKET_B) {
David Benjamin75836432016-06-17 18:48:29 -04001946 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001947 }
1948
David Benjamin123db572016-11-03 16:59:25 -04001949 const SSL_SESSION *session;
1950 SSL_SESSION *session_copy = NULL;
1951 if (ssl->session == NULL) {
1952 /* Fix the timeout to measure from the ticket issuance time. */
1953 ssl_session_refresh_time(ssl, ssl->s3->new_session);
1954 session = ssl->s3->new_session;
1955 } else {
1956 /* We are renewing an existing session. Duplicate the session to adjust the
1957 * timeout. */
1958 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1959 if (session_copy == NULL) {
1960 return -1;
1961 }
1962
1963 ssl_session_refresh_time(ssl, session_copy);
1964 session = session_copy;
1965 }
1966
David Benjamin75836432016-06-17 18:48:29 -04001967 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001968 int ok =
1969 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1970 CBB_add_u32(&body, session->timeout) &&
1971 CBB_add_u16_length_prefixed(&body, &ticket) &&
1972 ssl_encrypt_ticket(ssl, &ticket, session) &&
Steven Valdez5eead162016-11-11 22:23:25 -05001973 ssl_complete_message(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001974
1975 SSL_SESSION_free(session_copy);
1976 CBB_cleanup(&cbb);
1977
1978 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001979 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001980 }
1981
David Benjamincb0c29f2016-12-12 17:00:50 -05001982 hs->state = SSL3_ST_SW_SESSION_TICKET_B;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001983 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001984}