blob: 9b130237ab27cf9f37fd3ba490b81f1fc59ba744 [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:
454 if (BIO_flush(ssl->wbio) <= 0) {
455 ssl->rwstate = SSL_WRITING;
456 ret = -1;
457 goto end;
458 }
459
David Benjamincb0c29f2016-12-12 17:00:50 -0500460 hs->state = hs->next_state;
461 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400462 ssl->method->expect_flight(ssl);
463 }
464 break;
465
Steven Valdez143e8b32016-07-11 13:19:03 -0400466 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900467 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400468 if (ret <= 0) {
469 goto end;
470 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500471 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400472 break;
473
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400475 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Steven Valdez87eab492016-06-27 16:34:59 -0400477 /* If we aren't retaining peer certificates then we can discard it
478 * now. */
479 if (ssl->s3->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900480 ssl->retain_only_sha256_of_client_certs) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800481 X509_free(ssl->s3->new_session->x509_peer);
482 ssl->s3->new_session->x509_peer = NULL;
483 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
484 ssl->s3->new_session->x509_chain = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400485 }
486
487 SSL_SESSION_free(ssl->s3->established_session);
488 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400489 SSL_SESSION_up_ref(ssl->session);
490 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400491 } else {
492 ssl->s3->established_session = ssl->s3->new_session;
493 ssl->s3->established_session->not_resumable = 0;
494 ssl->s3->new_session = NULL;
495 }
496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500498 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700499
David Benjamin78476f62016-11-12 11:20:55 +0900500 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900501 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900502
David Benjamin4e9cc712016-06-01 20:16:03 -0400503 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800504 ret = 1;
505 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 default:
David Benjamin3570d732015-06-29 00:28:17 -0400508 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 ret = -1;
510 goto end;
511 }
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamincb0c29f2016-12-12 17:00:50 -0500513 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
514 int new_state = hs->state;
515 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400516 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500517 hs->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 }
519 skip = 0;
520 }
521
Adam Langley95c29f32014-06-20 12:00:00 -0700522end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400523 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 return ret;
525}
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin731058e2016-12-03 23:15:13 -0500527int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
528 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400529 CBS cipher_suites;
530 CBS_init(&cipher_suites, client_hello->cipher_suites,
531 client_hello->cipher_suites_len);
532
533 while (CBS_len(&cipher_suites) > 0) {
534 uint16_t got_id;
535 if (!CBS_get_u16(&cipher_suites, &got_id)) {
536 return 0;
537 }
538
539 if (got_id == id) {
540 return 1;
541 }
542 }
543
544 return 0;
545}
546
David Benjaminf04c2e92016-12-06 13:35:25 -0500547static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500548 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500549 SSL *const ssl = hs->ssl;
David Benjamin25fe85b2016-08-09 20:00:32 -0400550 uint16_t min_version, max_version;
551 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
552 *out_alert = SSL_AD_PROTOCOL_VERSION;
553 return 0;
554 }
555
David Benjamin2dc02042016-09-19 19:57:37 -0400556 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400557 /* Check supported_versions extension if it is present. */
558 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500559 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
560 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400561 CBS versions;
562 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
563 CBS_len(&supported_versions) != 0 ||
564 CBS_len(&versions) == 0) {
565 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
566 *out_alert = SSL_AD_DECODE_ERROR;
567 return 0;
568 }
569
Brian Smithf85d3232016-10-28 10:34:06 -1000570 /* Choose the newest commonly-supported version advertised by the client.
571 * The client orders the versions according to its preferences, but we're
572 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400573 int found_version = 0;
574 while (CBS_len(&versions) != 0) {
575 uint16_t ext_version;
576 if (!CBS_get_u16(&versions, &ext_version)) {
577 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
578 *out_alert = SSL_AD_DECODE_ERROR;
579 return 0;
580 }
581 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
582 continue;
583 }
584 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000585 ext_version <= max_version &&
586 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400587 version = ext_version;
588 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400589 }
590 }
591
592 if (!found_version) {
593 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400594 }
595 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400596 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
597 * do not use this mechanism. */
598 if (SSL_is_dtls(ssl)) {
599 if (client_hello->version <= DTLS1_2_VERSION) {
600 version = TLS1_2_VERSION;
601 } else if (client_hello->version <= DTLS1_VERSION) {
602 version = TLS1_1_VERSION;
603 } else {
604 goto unsupported_protocol;
605 }
606 } else {
607 if (client_hello->version >= TLS1_2_VERSION) {
608 version = TLS1_2_VERSION;
609 } else if (client_hello->version >= TLS1_1_VERSION) {
610 version = TLS1_1_VERSION;
611 } else if (client_hello->version >= TLS1_VERSION) {
612 version = TLS1_VERSION;
613 } else if (client_hello->version >= SSL3_VERSION) {
614 version = SSL3_VERSION;
615 } else {
616 goto unsupported_protocol;
617 }
David Benjamin2dc02042016-09-19 19:57:37 -0400618 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400619
Steven Valdezfdd10992016-09-15 16:27:05 -0400620 /* Apply our minimum and maximum version. */
621 if (version > max_version) {
622 version = max_version;
623 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400624
Steven Valdezfdd10992016-09-15 16:27:05 -0400625 if (version < min_version) {
626 goto unsupported_protocol;
627 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400628 }
629
630 /* Handle FALLBACK_SCSV. */
631 if (ssl_client_cipher_list_contains_cipher(client_hello,
632 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
633 version < max_version) {
634 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
635 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
636 return 0;
637 }
638
David Benjaminf04c2e92016-12-06 13:35:25 -0500639 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400640 ssl->version = ssl->method->version_to_wire(version);
641 ssl->s3->enc_method = ssl3_get_enc_method(version);
642 assert(ssl->s3->enc_method != NULL);
643
644 /* At this point, the connection's version is known and |ssl->version| is
645 * fixed. Begin enforcing the record-layer version. */
646 ssl->s3->have_version = 1;
647
648 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400649
650unsupported_protocol:
651 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
652 *out_alert = SSL_AD_PROTOCOL_VERSION;
653 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400654}
655
David Benjamin2578b292016-12-03 23:19:55 -0500656static STACK_OF(SSL_CIPHER) *
657 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
658 CBS cipher_suites;
659 CBS_init(&cipher_suites, client_hello->cipher_suites,
660 client_hello->cipher_suites_len);
661
662 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
663 if (sk == NULL) {
664 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
665 goto err;
666 }
667
668 while (CBS_len(&cipher_suites) > 0) {
669 uint16_t cipher_suite;
670
671 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
672 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
673 goto err;
674 }
675
676 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
677 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
678 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
679 goto err;
680 }
681 }
682
683 return sk;
684
685err:
686 sk_SSL_CIPHER_free(sk);
687 return NULL;
688}
689
690/* ssl_get_compatible_server_ciphers determines the key exchange and
691 * authentication cipher suite masks compatible with the server configuration
692 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
693 * exchange mask and |*out_mask_a| to the authentication mask. */
694static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
695 uint32_t *out_mask_k,
696 uint32_t *out_mask_a) {
697 SSL *const ssl = hs->ssl;
698 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
699 *out_mask_k = SSL_kGENERIC;
700 *out_mask_a = SSL_aGENERIC;
701 return;
702 }
703
704 uint32_t mask_k = 0;
705 uint32_t mask_a = 0;
706
707 if (ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl)) {
708 int type = ssl_private_key_type(ssl);
709 if (type == NID_rsaEncryption) {
710 mask_k |= SSL_kRSA;
711 mask_a |= SSL_aRSA;
712 } else if (ssl_is_ecdsa_key_type(type)) {
713 mask_a |= SSL_aECDSA;
714 }
715 }
716
717 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
718 mask_k |= SSL_kDHE;
719 }
720
721 /* Check for a shared group to consider ECDHE ciphers. */
722 uint16_t unused;
723 if (tls1_get_shared_group(hs, &unused)) {
724 mask_k |= SSL_kECDHE;
725 }
726
David Benjamin2578b292016-12-03 23:19:55 -0500727 /* PSK requires a server callback. */
728 if (ssl->psk_server_callback != NULL) {
729 mask_k |= SSL_kPSK;
730 mask_a |= SSL_aPSK;
731 }
732
733 *out_mask_k = mask_k;
734 *out_mask_a = mask_a;
735}
736
737static const SSL_CIPHER *ssl3_choose_cipher(
738 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
739 const struct ssl_cipher_preference_list_st *server_pref) {
740 SSL *const ssl = hs->ssl;
741 const SSL_CIPHER *c, *ret = NULL;
742 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
743 int ok;
744 size_t cipher_index;
745 uint32_t alg_k, alg_a, mask_k, mask_a;
746 /* in_group_flags will either be NULL, or will point to an array of bytes
747 * which indicate equal-preference groups in the |prio| stack. See the
748 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
749 * struct. */
750 const uint8_t *in_group_flags;
751 /* group_min contains the minimal index so far found in a group, or -1 if no
752 * such value exists yet. */
753 int group_min = -1;
754
755 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
756 if (clnt == NULL) {
757 return NULL;
758 }
759
760 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
761 prio = srvr;
762 in_group_flags = server_pref->in_group_flags;
763 allow = clnt;
764 } else {
765 prio = clnt;
766 in_group_flags = NULL;
767 allow = srvr;
768 }
769
770 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
771
772 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
773 c = sk_SSL_CIPHER_value(prio, i);
774
775 ok = 1;
776
777 /* Check the TLS version. */
778 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
779 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
780 ok = 0;
781 }
782
783 alg_k = c->algorithm_mkey;
784 alg_a = c->algorithm_auth;
785
786 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
787
788 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
789 if (in_group_flags != NULL && in_group_flags[i] == 1) {
790 /* This element of |prio| is in a group. Update the minimum index found
791 * so far and continue looking. */
792 if (group_min == -1 || (size_t)group_min > cipher_index) {
793 group_min = cipher_index;
794 }
795 } else {
796 if (group_min != -1 && (size_t)group_min < cipher_index) {
797 cipher_index = group_min;
798 }
799 ret = sk_SSL_CIPHER_value(allow, cipher_index);
800 break;
801 }
802 }
803
804 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
805 /* We are about to leave a group, but we found a match in it, so that's
806 * our answer. */
807 ret = sk_SSL_CIPHER_value(allow, group_min);
808 break;
809 }
810 }
811
812 sk_SSL_CIPHER_free(clnt);
813 return ret;
814}
815
David Benjaminc3c88822016-11-14 10:32:04 +0900816static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
817 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400818 uint8_t al = SSL_AD_INTERNAL_ERROR;
819 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400820 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjamincb0c29f2016-12-12 17:00:50 -0500822 if (hs->state == SSL3_ST_SR_CLNT_HELLO_A) {
David Benjamine14ff062016-08-09 16:21:24 -0400823 /* The first time around, read the ClientHello. */
824 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
825 ssl_hash_message);
826 if (msg_ret <= 0) {
827 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700828 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400829
David Benjamincb0c29f2016-12-12 17:00:50 -0500830 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 }
Adam Langley95c29f32014-06-20 12:00:00 -0700832
David Benjamin731058e2016-12-03 23:15:13 -0500833 SSL_CLIENT_HELLO client_hello;
834 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
835 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400837 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 goto f_err;
839 }
Adam Langley95c29f32014-06-20 12:00:00 -0700840
David Benjamincb0c29f2016-12-12 17:00:50 -0500841 if (hs->state == SSL3_ST_SR_CLNT_HELLO_B) {
David Benjamine14ff062016-08-09 16:21:24 -0400842 /* Unlike other callbacks, the early callback is not run a second time if
843 * paused. */
David Benjamincb0c29f2016-12-12 17:00:50 -0500844 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjaminb6a0a512016-06-21 10:33:21 -0400845
David Benjamine14ff062016-08-09 16:21:24 -0400846 /* Run the early callback. */
847 if (ssl->ctx->select_certificate_cb != NULL) {
848 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
849 case 0:
850 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
851 goto err;
852
853 case -1:
854 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400855 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400856 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
857 goto f_err;
858
859 default:
860 /* fallthrough */;
861 }
862 }
863 }
864
David Benjamin25fe85b2016-08-09 20:00:32 -0400865 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500866 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500867 if (!negotiate_version(hs, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800868 goto f_err;
869 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400870
David Benjamin25fe85b2016-08-09 20:00:32 -0400871 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500872 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900873 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400874 return 1;
875 }
876 }
877
David Benjamincb0c29f2016-12-12 17:00:50 -0500878 if (hs->state == SSL3_ST_SR_CLNT_HELLO_C) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400879 /* Load the client random. */
880 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
881 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
882 return -1;
883 }
884 memcpy(ssl->s3->client_random, client_hello.random,
885 client_hello.random_len);
886
David Benjamin4eb95cc2016-11-16 17:08:23 +0900887 /* Only null compression is supported. */
888 if (memchr(client_hello.compression_methods, 0,
889 client_hello.compression_methods_len) == NULL) {
890 al = SSL_AD_ILLEGAL_PARAMETER;
891 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
892 goto f_err;
893 }
894
895 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500896 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900897 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
898 goto err;
899 }
900
David Benjamincb0c29f2016-12-12 17:00:50 -0500901 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900902 }
903
David Benjamincb0c29f2016-12-12 17:00:50 -0500904 if (hs->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400905 /* Call |cert_cb| to update server certificates if required. */
906 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500907 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 if (rv == 0) {
909 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400910 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 goto f_err;
912 }
913 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500914 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 goto err;
916 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800918
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 }
Steven Valdez87eab492016-06-27 16:34:59 -04001196 ssl->s3->new_session->key_exchange_info = DH_num_bits(params);
David Benjamin2a0b3912015-12-18 01:01:21 -05001197
David Benjamin974c7ba2015-12-19 16:58:04 -05001198 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001199 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001200 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001201 goto err;
1202 }
David Benjamin9f226a52015-04-26 22:12:32 -04001203
David Benjaminc3c88822016-11-14 10:32:04 +09001204 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001205 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001206 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001207 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001208 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001209 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001210 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001211 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 }
David Benjamin7061e282015-03-19 11:10:48 -04001213 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001214 /* Determine the group to use. */
1215 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001216 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001217 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001218 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001219 goto err;
1220 }
Steven Valdez87eab492016-06-27 16:34:59 -04001221 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001222
David Benjamin4298d772015-12-19 00:18:25 -05001223 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001224 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001225 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001226 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001227 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001228 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001229 goto err;
1230 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001231 } else {
1232 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001233 }
Adam Langley95c29f32014-06-20 12:00:00 -07001234
David Benjaminc3c88822016-11-14 10:32:04 +09001235 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001236 goto err;
1237 }
David Benjaminc42acee2016-06-17 17:47:58 -04001238 }
1239
1240 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001241 CBB body;
1242 if (!ssl->method->init_message(ssl, &cbb, &body,
1243 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001244 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001245 goto err;
1246 }
Adam Langley95c29f32014-06-20 12:00:00 -07001247
David Benjamin2a0b3912015-12-18 01:01:21 -05001248 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001249 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001250 if (!ssl_has_private_key(ssl)) {
1251 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001252 goto err;
1253 }
Adam Langley95c29f32014-06-20 12:00:00 -07001254
David Benjaminc42acee2016-06-17 17:47:58 -04001255 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001256 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001257 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001258 goto err;
1259 }
David Benjaminc42acee2016-06-17 17:47:58 -04001260 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001261 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001262 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1263 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1264 goto err;
1265 }
David Benjaminc42acee2016-06-17 17:47:58 -04001266 }
1267
1268 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001269 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001270 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001271 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001272 !CBB_reserve(&child, &ptr, max_sig_len)) {
1273 goto err;
1274 }
1275
David Benjamin2a0b3912015-12-18 01:01:21 -05001276 size_t sig_len;
1277 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001278 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001279 CBB transcript;
1280 uint8_t *transcript_data;
1281 size_t transcript_len;
1282 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001283 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1284 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1285 SSL3_RANDOM_SIZE) ||
1286 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1287 SSL3_RANDOM_SIZE) ||
1288 !CBB_add_bytes(&transcript, hs->server_params,
1289 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001290 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1291 CBB_cleanup(&transcript);
1292 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1293 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001294 goto err;
1295 }
1296
Steven Valdezf0451ca2016-06-29 13:16:27 -04001297 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001298 signature_algorithm, transcript_data,
1299 transcript_len);
1300 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001301 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001302 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001303 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001304 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001305
1306 switch (sign_result) {
1307 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001308 if (!CBB_did_write(&child, sig_len)) {
1309 goto err;
1310 }
1311 break;
1312 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001313 goto err;
1314 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001315 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001316 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001317 goto err;
1318 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001319 }
1320
Steven Valdez5eead162016-11-11 22:23:25 -05001321 if (!ssl_complete_message(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001322 goto err;
1323 }
David Benjaminc42acee2016-06-17 17:47:58 -04001324
David Benjaminc3c88822016-11-14 10:32:04 +09001325 OPENSSL_free(hs->server_params);
1326 hs->server_params = NULL;
1327 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001328
David Benjamincb0c29f2016-12-12 17:00:50 -05001329 hs->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjamin75836432016-06-17 18:48:29 -04001330 return ssl->method->write_message(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001331
Adam Langley95c29f32014-06-20 12:00:00 -07001332err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001333 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 return -1;
1335}
Adam Langley95c29f32014-06-20 12:00:00 -07001336
David Benjamin75836432016-06-17 18:48:29 -04001337static int add_cert_types(SSL *ssl, CBB *cbb) {
1338 /* Get configured signature algorithms. */
1339 int have_rsa_sign = 0;
1340 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001341 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001342 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1343 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001344 switch (sig_algs[i]) {
1345 case SSL_SIGN_RSA_PKCS1_SHA512:
1346 case SSL_SIGN_RSA_PKCS1_SHA384:
1347 case SSL_SIGN_RSA_PKCS1_SHA256:
1348 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001349 have_rsa_sign = 1;
1350 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001351
Steven Valdez02563852016-06-23 13:33:05 -04001352 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1353 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1354 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1355 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001356 have_ecdsa_sign = 1;
1357 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001358 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001359 }
Adam Langley95c29f32014-06-20 12:00:00 -07001360
David Benjamin75836432016-06-17 18:48:29 -04001361 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1362 return 0;
1363 }
1364
1365 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1366 * check for SSL_kECDH or SSL_kECDHE. */
1367 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1368 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1369 return 0;
1370 }
1371
1372 return 1;
1373}
1374
David Benjaminc3c88822016-11-14 10:32:04 +09001375static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1376 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001377 if (hs->state == SSL3_ST_SW_CERT_REQ_B) {
David Benjamin75836432016-06-17 18:48:29 -04001378 return ssl->method->write_message(ssl);
1379 }
1380
David Benjamin32a66d52016-07-13 22:03:11 -04001381 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001382 if (!ssl->method->init_message(ssl, &cbb, &body,
1383 SSL3_MT_CERTIFICATE_REQUEST) ||
1384 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1385 !add_cert_types(ssl, &cert_types)) {
1386 goto err;
1387 }
1388
1389 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001390 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001391 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001392 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001393 goto err;
1394 }
Steven Valdez02563852016-06-23 13:33:05 -04001395
David Benjamin0fc37ef2016-08-17 15:29:46 -04001396 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001397 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1398 goto err;
1399 }
1400 }
David Benjamin75836432016-06-17 18:48:29 -04001401 }
1402
David Benjamin32a66d52016-07-13 22:03:11 -04001403 if (!ssl_add_client_CA_list(ssl, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001404 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001405 goto err;
1406 }
1407
David Benjamincb0c29f2016-12-12 17:00:50 -05001408 hs->state = SSL3_ST_SW_CERT_REQ_B;
David Benjamin75836432016-06-17 18:48:29 -04001409 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001410
Adam Langley95c29f32014-06-20 12:00:00 -07001411err:
David Benjamin75836432016-06-17 18:48:29 -04001412 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1413 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001414 return -1;
1415}
Adam Langley95c29f32014-06-20 12:00:00 -07001416
David Benjaminc3c88822016-11-14 10:32:04 +09001417static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1418 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001419 if (hs->state == SSL3_ST_SW_SRVR_DONE_B) {
David Benjamin75836432016-06-17 18:48:29 -04001420 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001421 }
1422
David Benjamin75836432016-06-17 18:48:29 -04001423 CBB cbb, body;
1424 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001425 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001426 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1427 CBB_cleanup(&cbb);
1428 return -1;
1429 }
1430
David Benjamincb0c29f2016-12-12 17:00:50 -05001431 hs->state = SSL3_ST_SW_SRVR_DONE_B;
David Benjamin75836432016-06-17 18:48:29 -04001432 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001433}
1434
David Benjaminc3c88822016-11-14 10:32:04 +09001435static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1436 SSL *const ssl = hs->ssl;
1437 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001438
David Benjamin09eb6552016-07-08 14:32:11 -07001439 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1440 if (msg_ret <= 0) {
1441 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001442 }
1443
1444 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1445 if (ssl->version == SSL3_VERSION &&
1446 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001447 /* In SSL 3.0, the Certificate message is omitted to signal no
1448 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001449 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001450 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001451 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1452 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001453 }
1454
Adam Langley37646832016-08-01 16:16:46 -07001455 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001456 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001457 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001458 ssl->s3->tmp.reuse_message = 1;
1459 return 1;
1460 }
1461
David Benjamin9f1dc822016-06-07 17:03:46 -04001462 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001463 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1464 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001465 }
1466
David Benjamin5c900c82016-07-13 23:03:26 -04001467 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001468 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001469
1470 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
David Benjamin5c900c82016-07-13 23:03:26 -04001471 uint8_t alert;
Adam Langley68e71242016-12-12 11:06:16 -08001472 ssl->s3->new_session->certs =
1473 ssl_parse_cert_chain(&alert, ssl->retain_only_sha256_of_client_certs
1474 ? ssl->s3->new_session->peer_sha256
1475 : NULL,
1476 &certificate_msg);
1477 if (ssl->s3->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001478 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001479 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001480 }
1481
Adam Langley68e71242016-12-12 11:06:16 -08001482 if (CBS_len(&certificate_msg) != 0 ||
1483 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001484 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001485 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001486 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001487 }
1488
Adam Langley68e71242016-12-12 11:06:16 -08001489 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001490 /* No client certificate so the handshake buffer may be discarded. */
1491 ssl3_free_handshake_buffer(ssl);
1492
David Benjamine455e512016-08-01 20:11:13 -04001493 /* In SSL 3.0, sending no certificate is signaled by omitting the
1494 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001495 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001496 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001497 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001498 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001499 }
1500
1501 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001502 /* Fail for TLS only if we required a certificate */
1503 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001504 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001505 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001506 }
Adam Langley37646832016-08-01 16:16:46 -07001507
1508 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001509 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001510 ssl->s3->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001511 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001512 }
1513
Adam Langley68e71242016-12-12 11:06:16 -08001514 /* The hash will have been filled in. */
1515 if (ssl->retain_only_sha256_of_client_certs) {
1516 ssl->s3->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001517 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001518
Adam Langley68e71242016-12-12 11:06:16 -08001519 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1520 ssl->s3->new_session->x509_chain)) {
1521 return -1;
1522 }
David Benjamin5c900c82016-07-13 23:03:26 -04001523 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001524}
1525
David Benjaminc3c88822016-11-14 10:32:04 +09001526static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1527 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001528 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001530 uint32_t alg_k;
1531 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 uint8_t *premaster_secret = NULL;
1533 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001535
David Benjamin4298d772015-12-19 00:18:25 -05001536 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001537 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001538
David Benjamincb0c29f2016-12-12 17:00:50 -05001539 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001540 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1541 ssl_hash_message);
1542 if (ret <= 0) {
1543 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001544 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 }
Adam Langley95c29f32014-06-20 12:00:00 -07001546
David Benjamin0d56f882015-12-19 17:05:56 -05001547 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1548 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1549 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001550
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 /* If using a PSK key exchange, prepare the pre-shared key. */
1552 if (alg_a & SSL_aPSK) {
1553 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1556 * then this is the only field in the message. */
1557 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1558 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001559 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 al = SSL_AD_DECODE_ERROR;
1561 goto f_err;
1562 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001563
David Benjamin0d56f882015-12-19 17:05:56 -05001564 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001565 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001566 al = SSL_AD_INTERNAL_ERROR;
1567 goto f_err;
1568 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001569
Adam Langleyfcf25832014-12-18 17:42:32 -08001570 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1571 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001572 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 al = SSL_AD_ILLEGAL_PARAMETER;
1574 goto f_err;
1575 }
David Benjamin35c02302014-07-13 04:14:59 -04001576
Steven Valdez87eab492016-06-27 16:34:59 -04001577 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001579 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 goto f_err;
1581 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001582
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001584 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1585 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001586 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001587 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 al = SSL_AD_INTERNAL_ERROR;
1589 goto f_err;
1590 } else if (psk_len == 0) {
1591 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001592 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001593 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1594 goto f_err;
1595 }
1596 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001597
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 /* Depending on the key exchange method, compute |premaster_secret| and
1599 * |premaster_secret_len|. */
1600 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001601 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001602 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 decrypt_buf = OPENSSL_malloc(rsa_size);
1604 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001605 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001606 goto err;
1607 }
David Benjamin35c02302014-07-13 04:14:59 -04001608
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001609 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001610 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001611 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001612 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001613 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001614 al = SSL_AD_HANDSHAKE_FAILURE;
1615 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1616 goto f_err;
1617 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001618 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001619 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001620 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1621 &encrypted_premaster_secret) ||
1622 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001623 al = SSL_AD_DECODE_ERROR;
1624 OPENSSL_PUT_ERROR(SSL,
1625 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1626 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001627 }
1628 } else {
1629 encrypted_premaster_secret = client_key_exchange;
1630 }
1631
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001632 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1633 * timing-sensitive code below. */
1634 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001635 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001636 CBS_data(&encrypted_premaster_secret),
1637 CBS_len(&encrypted_premaster_secret));
1638 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001639 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001640 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001641 decrypt_result =
1642 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001643 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001644
1645 switch (decrypt_result) {
1646 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001647 break;
1648 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001649 goto err;
1650 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001651 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001652 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001653 goto err;
1654 }
1655
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001656 if (decrypt_len != rsa_size) {
1657 al = SSL_AD_DECRYPT_ERROR;
1658 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1659 goto f_err;
1660 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001661
David Benjamin3f5b43d2015-12-01 19:31:24 -05001662 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1663 * section 7.4.7.1. */
1664 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1665 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001666 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001667 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001668 goto err;
1669 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001670 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001671 goto err;
1672 }
1673
David Benjamin3f5b43d2015-12-01 19:31:24 -05001674 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1675 * publicly invalid. */
1676 if (decrypt_len < 11 + premaster_secret_len) {
1677 al = SSL_AD_DECRYPT_ERROR;
1678 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1679 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 }
David Benjamin35c02302014-07-13 04:14:59 -04001681
David Benjamin3f5b43d2015-12-01 19:31:24 -05001682 /* Check the padding. See RFC 3447, section 7.2.2. */
1683 size_t padding_len = decrypt_len - premaster_secret_len;
1684 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1685 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001686 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001687 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1688 }
1689 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1690
1691 /* The premaster secret must begin with |client_version|. This too must be
1692 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1693 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001694 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001695 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001696 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001697
1698 /* Select, in constant time, either the decrypted premaster or the random
1699 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001700 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001701 premaster_secret[i] = constant_time_select_8(
1702 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1703 }
1704
1705 OPENSSL_free(decrypt_buf);
1706 decrypt_buf = NULL;
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001707 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001708 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001709 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001710 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001711 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001712 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001713 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 goto f_err;
1715 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001716
David Benjamin4298d772015-12-19 00:18:25 -05001717 /* Compute the premaster. */
1718 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001719 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001720 &premaster_secret_len, &alert, CBS_data(&peer_key),
1721 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001722 al = alert;
1723 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001724 }
Adam Langley95c29f32014-06-20 12:00:00 -07001725
David Benjamin4298d772015-12-19 00:18:25 -05001726 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001727 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001728 } else if (alg_k & SSL_kPSK) {
1729 /* For plain PSK, other_secret is a block of 0s with the same length as the
1730 * pre-shared key. */
1731 premaster_secret_len = psk_len;
1732 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1733 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001734 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 goto err;
1736 }
1737 memset(premaster_secret, 0, premaster_secret_len);
1738 } else {
1739 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001740 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 goto f_err;
1742 }
Adam Langley95c29f32014-06-20 12:00:00 -07001743
Adam Langleyfcf25832014-12-18 17:42:32 -08001744 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1745 * pre-shared key. */
1746 if (alg_a & SSL_aPSK) {
1747 CBB new_premaster, child;
1748 uint8_t *new_data;
1749 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001750
David Benjamina8653202015-06-28 01:26:10 -04001751 CBB_zero(&new_premaster);
1752 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1753 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001754 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1755 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1756 !CBB_add_bytes(&child, psk, psk_len) ||
1757 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001758 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001759 CBB_cleanup(&new_premaster);
1760 goto err;
1761 }
David Benjamin14c83e72014-07-13 04:54:57 -04001762
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1764 OPENSSL_free(premaster_secret);
1765 premaster_secret = new_data;
1766 premaster_secret_len = new_len;
1767 }
David Benjamin14c83e72014-07-13 04:54:57 -04001768
Adam Langleyfcf25832014-12-18 17:42:32 -08001769 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001770 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1771 ssl, ssl->s3->new_session->master_key, premaster_secret,
1772 premaster_secret_len);
1773 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001774 goto err;
1775 }
Steven Valdez87eab492016-06-27 16:34:59 -04001776 ssl->s3->new_session->extended_master_secret =
1777 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1780 OPENSSL_free(premaster_secret);
1781 return 1;
1782
Adam Langley95c29f32014-06-20 12:00:00 -07001783f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001784 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001785err:
David Benjamin4298d772015-12-19 00:18:25 -05001786 if (premaster_secret != NULL) {
1787 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001788 OPENSSL_free(premaster_secret);
1789 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001790 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001791
Adam Langleyfcf25832014-12-18 17:42:32 -08001792 return -1;
1793}
Adam Langley95c29f32014-06-20 12:00:00 -07001794
David Benjaminc3c88822016-11-14 10:32:04 +09001795static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1796 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001797 int al, ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 CBS certificate_verify, signature;
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001799 X509 *peer = ssl->s3->new_session->x509_peer;
Adam Langleyfcf25832014-12-18 17:42:32 -08001800 EVP_PKEY *pkey = NULL;
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 * */
1805 if (peer == 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
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 /* Filter out unsupported certificate types. */
1817 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05001818 if (pkey == NULL) {
1819 goto err;
1820 }
David Benjamin6897dbe2014-07-12 20:18:28 -04001821
David Benjamin09eb6552016-07-08 14:32:11 -07001822 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001823
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001825 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001826 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001827 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001828 al = SSL_AD_DECODE_ERROR;
1829 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1830 goto f_err;
1831 }
David Benjamin887c3002016-07-08 16:15:32 -07001832 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001833 goto f_err;
1834 }
Steven Valdez02563852016-06-23 13:33:05 -04001835 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001836 } else if (pkey->type == EVP_PKEY_RSA) {
1837 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1838 } else if (pkey->type == EVP_PKEY_EC) {
1839 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001840 } else {
1841 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1842 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1843 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001844 }
David Benjamin854dd652014-08-26 00:32:30 -04001845
Adam Langleyfcf25832014-12-18 17:42:32 -08001846 /* Parse and verify the signature. */
1847 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1848 CBS_len(&certificate_verify) != 0) {
1849 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001850 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 goto f_err;
1852 }
Adam Langley95c29f32014-06-20 12:00:00 -07001853
Steven Valdez2b8415e2016-06-30 13:27:23 -04001854 int sig_ok;
1855 /* The SSL3 construction for CertificateVerify does not decompose into a
1856 * single final digest and signature, and must be special-cased. */
1857 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001858 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001859 uint8_t digest[EVP_MAX_MD_SIZE];
1860 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001861 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001862 signature_algorithm)) {
1863 goto err;
1864 }
1865
Steven Valdez2b8415e2016-06-30 13:27:23 -04001866 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
1867 sig_ok = pctx != NULL &&
1868 EVP_PKEY_verify_init(pctx) &&
1869 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1870 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1871 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001872 EVP_PKEY_CTX_free(pctx);
1873 } else {
1874 sig_ok = ssl_public_key_verify(
1875 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1876 pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
1877 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001878 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001879
David Benjaminbf82aed2016-03-01 22:57:40 -05001880#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1881 sig_ok = 1;
1882 ERR_clear_error();
1883#endif
1884 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001886 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001887 goto f_err;
1888 }
1889
Steven Valdez2b8415e2016-06-30 13:27:23 -04001890 /* The handshake buffer is no longer necessary, and we may hash the current
1891 * message.*/
1892 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001893 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001894 goto err;
1895 }
1896
Adam Langleyfcf25832014-12-18 17:42:32 -08001897 ret = 1;
1898
1899 if (0) {
1900 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001901 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 }
1903
David Benjamin854dd652014-08-26 00:32:30 -04001904err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001906
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 return ret;
1908}
Adam Langley95c29f32014-06-20 12:00:00 -07001909
Adam Langley95c29f32014-06-20 12:00:00 -07001910/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1911 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001912static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1913 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001914 int ret =
1915 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1916 if (ret <= 0) {
1917 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001918 }
Adam Langley95c29f32014-06-20 12:00:00 -07001919
David Benjamin09eb6552016-07-08 14:32:11 -07001920 CBS next_protocol, selected_protocol, padding;
1921 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001922 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1923 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001924 CBS_len(&next_protocol) != 0) {
1925 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1926 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1927 return 0;
1928 }
1929
1930 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001931 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001932 return 0;
1933 }
Adam Langley95c29f32014-06-20 12:00:00 -07001934
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 return 1;
1936}
Adam Langley95c29f32014-06-20 12:00:00 -07001937
Adam Langley1258b6a2014-06-20 12:00:00 -07001938/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001939static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1940 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001941 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1942 ssl_dont_hash_message);
1943 if (msg_ret <= 0) {
1944 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001945 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001946
Nick Harper60a85cb2016-09-23 16:25:11 -07001947 if (!tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001948 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 return -1;
1950 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001951 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001952}
David Benjamin9f1dc822016-06-07 17:03:46 -04001953
David Benjaminc3c88822016-11-14 10:32:04 +09001954static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1955 SSL *const ssl = hs->ssl;
David Benjamincb0c29f2016-12-12 17:00:50 -05001956 if (hs->state == SSL3_ST_SW_SESSION_TICKET_B) {
David Benjamin75836432016-06-17 18:48:29 -04001957 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001958 }
1959
David Benjamin123db572016-11-03 16:59:25 -04001960 const SSL_SESSION *session;
1961 SSL_SESSION *session_copy = NULL;
1962 if (ssl->session == NULL) {
1963 /* Fix the timeout to measure from the ticket issuance time. */
1964 ssl_session_refresh_time(ssl, ssl->s3->new_session);
1965 session = ssl->s3->new_session;
1966 } else {
1967 /* We are renewing an existing session. Duplicate the session to adjust the
1968 * timeout. */
1969 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1970 if (session_copy == NULL) {
1971 return -1;
1972 }
1973
1974 ssl_session_refresh_time(ssl, session_copy);
1975 session = session_copy;
1976 }
1977
David Benjamin75836432016-06-17 18:48:29 -04001978 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001979 int ok =
1980 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1981 CBB_add_u32(&body, session->timeout) &&
1982 CBB_add_u16_length_prefixed(&body, &ticket) &&
1983 ssl_encrypt_ticket(ssl, &ticket, session) &&
Steven Valdez5eead162016-11-11 22:23:25 -05001984 ssl_complete_message(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001985
1986 SSL_SESSION_free(session_copy);
1987 CBB_cleanup(&cbb);
1988
1989 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001990 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001991 }
1992
David Benjamincb0c29f2016-12-12 17:00:50 -05001993 hs->state = SSL3_ST_SW_SESSION_TICKET_B;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04001994 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001995}