blob: 4bac0d5b2f1a8cdf6d50a83ae4454b1e63656a0f [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 Benjamin0d56f882015-12-19 17:05:56 -0500198 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamin0d56f882015-12-19 17:05:56 -0500200 switch (ssl->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400201 case SSL_ST_INIT:
202 ssl->state = SSL_ST_ACCEPT;
203 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 Benjamin0d56f882015-12-19 17:05:56 -0500222 ssl->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);
Steven Valdez143e8b32016-07-11 13:19:03 -0400231 if (ssl->state == SSL_ST_TLS13) {
232 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 Benjamin0d56f882015-12-19 17:05:56 -0500238 ssl->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 Benjamin47749a62016-06-13 13:56:33 -0400248 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500250 ssl->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 Benjamin47749a62016-06-13 13:56:33 -0400264 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500277 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500296 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500309 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500318 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
319 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500329 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500338 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500347 ssl->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 Benjamin47749a62016-06-13 13:56:33 -0400361 ssl->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 Benjamin47749a62016-06-13 13:56:33 -0400373 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500385 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500396 ssl->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400398 ssl->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 Benjamin352d0a92016-06-28 11:22:02 -0400422 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500430 ssl->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 Benjamin0d56f882015-12-19 17:05:56 -0500445 ssl->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400446 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500447 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500449 ssl->s3->tmp.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
460 ssl->state = ssl->s3->tmp.next_state;
461 if (ssl->state != SSL_ST_OK) {
462 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 }
471 ssl->state = SSL_ST_OK;
472 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 Benjamin4e9cc712016-06-01 20:16:03 -0400513 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
514 int new_state = ssl->state;
David Benjamin0d56f882015-12-19 17:05:56 -0500515 ssl->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400516 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamin0d56f882015-12-19 17:05:56 -0500517 ssl->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 Benjamin731058e2016-12-03 23:15:13 -0500547static int negotiate_version(SSL *ssl, uint8_t *out_alert,
548 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400549 uint16_t min_version, max_version;
550 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
551 *out_alert = SSL_AD_PROTOCOL_VERSION;
552 return 0;
553 }
554
David Benjamin2dc02042016-09-19 19:57:37 -0400555 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400556 /* Check supported_versions extension if it is present. */
557 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500558 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
559 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400560 CBS versions;
561 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
562 CBS_len(&supported_versions) != 0 ||
563 CBS_len(&versions) == 0) {
564 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
565 *out_alert = SSL_AD_DECODE_ERROR;
566 return 0;
567 }
568
Brian Smithf85d3232016-10-28 10:34:06 -1000569 /* Choose the newest commonly-supported version advertised by the client.
570 * The client orders the versions according to its preferences, but we're
571 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400572 int found_version = 0;
573 while (CBS_len(&versions) != 0) {
574 uint16_t ext_version;
575 if (!CBS_get_u16(&versions, &ext_version)) {
576 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
577 *out_alert = SSL_AD_DECODE_ERROR;
578 return 0;
579 }
580 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
581 continue;
582 }
583 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000584 ext_version <= max_version &&
585 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400586 version = ext_version;
587 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400588 }
589 }
590
591 if (!found_version) {
592 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400593 }
594 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400595 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
596 * do not use this mechanism. */
597 if (SSL_is_dtls(ssl)) {
598 if (client_hello->version <= DTLS1_2_VERSION) {
599 version = TLS1_2_VERSION;
600 } else if (client_hello->version <= DTLS1_VERSION) {
601 version = TLS1_1_VERSION;
602 } else {
603 goto unsupported_protocol;
604 }
605 } else {
606 if (client_hello->version >= TLS1_2_VERSION) {
607 version = TLS1_2_VERSION;
608 } else if (client_hello->version >= TLS1_1_VERSION) {
609 version = TLS1_1_VERSION;
610 } else if (client_hello->version >= TLS1_VERSION) {
611 version = TLS1_VERSION;
612 } else if (client_hello->version >= SSL3_VERSION) {
613 version = SSL3_VERSION;
614 } else {
615 goto unsupported_protocol;
616 }
David Benjamin2dc02042016-09-19 19:57:37 -0400617 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400618
Steven Valdezfdd10992016-09-15 16:27:05 -0400619 /* Apply our minimum and maximum version. */
620 if (version > max_version) {
621 version = max_version;
622 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400623
Steven Valdezfdd10992016-09-15 16:27:05 -0400624 if (version < min_version) {
625 goto unsupported_protocol;
626 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400627 }
628
629 /* Handle FALLBACK_SCSV. */
630 if (ssl_client_cipher_list_contains_cipher(client_hello,
631 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
632 version < max_version) {
633 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
634 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
635 return 0;
636 }
637
David Benjamin2dc02042016-09-19 19:57:37 -0400638 ssl->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400639 ssl->version = ssl->method->version_to_wire(version);
640 ssl->s3->enc_method = ssl3_get_enc_method(version);
641 assert(ssl->s3->enc_method != NULL);
642
643 /* At this point, the connection's version is known and |ssl->version| is
644 * fixed. Begin enforcing the record-layer version. */
645 ssl->s3->have_version = 1;
646
647 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400648
649unsupported_protocol:
650 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
651 *out_alert = SSL_AD_PROTOCOL_VERSION;
652 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400653}
654
David Benjamin2578b292016-12-03 23:19:55 -0500655static STACK_OF(SSL_CIPHER) *
656 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
657 CBS cipher_suites;
658 CBS_init(&cipher_suites, client_hello->cipher_suites,
659 client_hello->cipher_suites_len);
660
661 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
662 if (sk == NULL) {
663 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
664 goto err;
665 }
666
667 while (CBS_len(&cipher_suites) > 0) {
668 uint16_t cipher_suite;
669
670 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
671 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
672 goto err;
673 }
674
675 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
676 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
677 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
678 goto err;
679 }
680 }
681
682 return sk;
683
684err:
685 sk_SSL_CIPHER_free(sk);
686 return NULL;
687}
688
689/* ssl_get_compatible_server_ciphers determines the key exchange and
690 * authentication cipher suite masks compatible with the server configuration
691 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
692 * exchange mask and |*out_mask_a| to the authentication mask. */
693static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
694 uint32_t *out_mask_k,
695 uint32_t *out_mask_a) {
696 SSL *const ssl = hs->ssl;
697 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
698 *out_mask_k = SSL_kGENERIC;
699 *out_mask_a = SSL_aGENERIC;
700 return;
701 }
702
703 uint32_t mask_k = 0;
704 uint32_t mask_a = 0;
705
706 if (ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl)) {
707 int type = ssl_private_key_type(ssl);
708 if (type == NID_rsaEncryption) {
709 mask_k |= SSL_kRSA;
710 mask_a |= SSL_aRSA;
711 } else if (ssl_is_ecdsa_key_type(type)) {
712 mask_a |= SSL_aECDSA;
713 }
714 }
715
716 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
717 mask_k |= SSL_kDHE;
718 }
719
720 /* Check for a shared group to consider ECDHE ciphers. */
721 uint16_t unused;
722 if (tls1_get_shared_group(hs, &unused)) {
723 mask_k |= SSL_kECDHE;
724 }
725
726 /* CECPQ1 ciphers are always acceptable if supported by both sides. */
727 mask_k |= SSL_kCECPQ1;
728
729 /* PSK requires a server callback. */
730 if (ssl->psk_server_callback != NULL) {
731 mask_k |= SSL_kPSK;
732 mask_a |= SSL_aPSK;
733 }
734
735 *out_mask_k = mask_k;
736 *out_mask_a = mask_a;
737}
738
739static const SSL_CIPHER *ssl3_choose_cipher(
740 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
741 const struct ssl_cipher_preference_list_st *server_pref) {
742 SSL *const ssl = hs->ssl;
743 const SSL_CIPHER *c, *ret = NULL;
744 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
745 int ok;
746 size_t cipher_index;
747 uint32_t alg_k, alg_a, mask_k, mask_a;
748 /* in_group_flags will either be NULL, or will point to an array of bytes
749 * which indicate equal-preference groups in the |prio| stack. See the
750 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
751 * struct. */
752 const uint8_t *in_group_flags;
753 /* group_min contains the minimal index so far found in a group, or -1 if no
754 * such value exists yet. */
755 int group_min = -1;
756
757 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
758 if (clnt == NULL) {
759 return NULL;
760 }
761
762 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
763 prio = srvr;
764 in_group_flags = server_pref->in_group_flags;
765 allow = clnt;
766 } else {
767 prio = clnt;
768 in_group_flags = NULL;
769 allow = srvr;
770 }
771
772 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
773
774 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
775 c = sk_SSL_CIPHER_value(prio, i);
776
777 ok = 1;
778
779 /* Check the TLS version. */
780 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
781 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
782 ok = 0;
783 }
784
785 alg_k = c->algorithm_mkey;
786 alg_a = c->algorithm_auth;
787
788 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
789
790 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
791 if (in_group_flags != NULL && in_group_flags[i] == 1) {
792 /* This element of |prio| is in a group. Update the minimum index found
793 * so far and continue looking. */
794 if (group_min == -1 || (size_t)group_min > cipher_index) {
795 group_min = cipher_index;
796 }
797 } else {
798 if (group_min != -1 && (size_t)group_min < cipher_index) {
799 cipher_index = group_min;
800 }
801 ret = sk_SSL_CIPHER_value(allow, cipher_index);
802 break;
803 }
804 }
805
806 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
807 /* We are about to leave a group, but we found a match in it, so that's
808 * our answer. */
809 ret = sk_SSL_CIPHER_value(allow, group_min);
810 break;
811 }
812 }
813
814 sk_SSL_CIPHER_free(clnt);
815 return ret;
816}
817
David Benjaminc3c88822016-11-14 10:32:04 +0900818static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
819 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400820 uint8_t al = SSL_AD_INTERNAL_ERROR;
821 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400822 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700823
David Benjamine14ff062016-08-09 16:21:24 -0400824 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) {
825 /* The first time around, read the ClientHello. */
826 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
827 ssl_hash_message);
828 if (msg_ret <= 0) {
829 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700830 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400831
David Benjamine14ff062016-08-09 16:21:24 -0400832 ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
David Benjamin731058e2016-12-03 23:15:13 -0500835 SSL_CLIENT_HELLO client_hello;
836 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
837 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800838 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400839 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 goto f_err;
841 }
Adam Langley95c29f32014-06-20 12:00:00 -0700842
David Benjamine14ff062016-08-09 16:21:24 -0400843 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) {
844 /* Unlike other callbacks, the early callback is not run a second time if
845 * paused. */
846 ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjaminb6a0a512016-06-21 10:33:21 -0400847
David Benjamine14ff062016-08-09 16:21:24 -0400848 /* Run the early callback. */
849 if (ssl->ctx->select_certificate_cb != NULL) {
850 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
851 case 0:
852 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
853 goto err;
854
855 case -1:
856 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400857 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400858 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
859 goto f_err;
860
861 default:
862 /* fallthrough */;
863 }
864 }
865 }
866
David Benjamin25fe85b2016-08-09 20:00:32 -0400867 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500868 if (!ssl->s3->have_version) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400869 if (!negotiate_version(ssl, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 goto f_err;
871 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400872
David Benjamin25fe85b2016-08-09 20:00:32 -0400873 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
874 ssl->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900875 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400876 return 1;
877 }
878 }
879
880 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) {
881 /* Load the client random. */
882 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
883 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
884 return -1;
885 }
886 memcpy(ssl->s3->client_random, client_hello.random,
887 client_hello.random_len);
888
David Benjamin4eb95cc2016-11-16 17:08:23 +0900889 /* Only null compression is supported. */
890 if (memchr(client_hello.compression_methods, 0,
891 client_hello.compression_methods_len) == NULL) {
892 al = SSL_AD_ILLEGAL_PARAMETER;
893 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
894 goto f_err;
895 }
896
897 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500898 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900899 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
900 goto err;
901 }
902
903 ssl->state = SSL3_ST_SR_CLNT_HELLO_D;
904 }
905
906 if (ssl->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400907 /* Call |cert_cb| to update server certificates if required. */
908 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500909 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 if (rv == 0) {
911 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400912 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 goto f_err;
914 }
915 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500916 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 goto err;
918 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800920
David Benjaminf01f42a2016-11-16 19:05:33 +0900921 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
922 * certificate is finalized. */
923 ssl->s3->tmp.new_cipher =
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900924 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjaminf01f42a2016-11-16 19:05:33 +0900925 if (ssl->s3->tmp.new_cipher == NULL) {
926 al = SSL_AD_HANDSHAKE_FAILURE;
927 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
928 goto f_err;
929 }
930
David Benjamin34202b92016-11-16 19:07:53 +0900931 ssl->state = SSL3_ST_SR_CLNT_HELLO_E;
932 }
933
934 assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_E);
935
936 /* Determine whether we are doing session resumption. */
937 int tickets_supported = 0, renew_ticket = 0;
938 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
939 &client_hello)) {
940 case ssl_session_success:
941 break;
942 case ssl_session_error:
943 goto err;
944 case ssl_session_retry:
945 ssl->rwstate = SSL_PENDING_SESSION;
946 goto err;
947 }
948
949 if (session != NULL) {
950 if (session->extended_master_secret &&
951 !ssl->s3->tmp.extended_master_secret) {
952 /* A ClientHello without EMS that attempts to resume a session with EMS
953 * is fatal to the connection. */
954 al = SSL_AD_HANDSHAKE_FAILURE;
955 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
956 goto f_err;
957 }
958
959 if (!ssl_session_is_resumable(ssl, session) ||
960 /* If the client offers the EMS extension, but the previous session
961 * didn't use it, then negotiate a new session. */
962 ssl->s3->tmp.extended_master_secret !=
963 session->extended_master_secret) {
964 SSL_SESSION_free(session);
965 session = NULL;
966 }
967 }
968
969 if (session != NULL) {
970 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900971 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900972 ssl->session = session;
973 session = NULL;
974 ssl->s3->session_reused = 1;
975 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900976 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900977 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900978 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900979 goto err;
980 }
981
982 /* Clear the session ID if we want the session to be single-use. */
983 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
984 ssl->s3->new_session->session_id_length = 0;
985 }
986 }
987
988 if (ssl->ctx->dos_protection_cb != NULL &&
989 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
990 /* Connection rejected for DOS reasons. */
991 al = SSL_AD_INTERNAL_ERROR;
992 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
993 goto f_err;
994 }
995
David Benjaminf01f42a2016-11-16 19:05:33 +0900996 if (ssl->session == NULL) {
997 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400998
David Benjamin4eb95cc2016-11-16 17:08:23 +0900999 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +09001000 if (hs->hostname != NULL) {
1001 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
David Benjamin4eb95cc2016-11-16 17:08:23 +09001002 if (ssl->s3->new_session->tlsext_hostname == NULL) {
1003 al = SSL_AD_INTERNAL_ERROR;
1004 goto f_err;
1005 }
1006 }
1007
David Benjamin5c1ce292015-05-26 16:59:43 -04001008 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +09001009 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -04001010 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -05001011 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1012 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +09001013 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001014 }
David Benjaminc032dfa2016-05-12 14:54:57 -04001015 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001016 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +09001017 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001018 }
Adam Langley37646832016-08-01 16:16:46 -07001019
David Benjaminc3c88822016-11-14 10:32:04 +09001020 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001021 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001022 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001023 ssl->s3->new_session->verify_result = X509_V_OK;
1024 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 }
1026
David Benjaminf01f42a2016-11-16 19:05:33 +09001027 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1028 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001029 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001030 goto f_err;
1031 }
1032
David Benjaminf01f42a2016-11-16 19:05:33 +09001033 /* Now that all parameters are known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -05001034 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001035 goto f_err;
1036 }
1037
Steven Valdez2b8415e2016-06-30 13:27:23 -04001038 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001039 if (!hs->cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -05001040 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 }
1042
David Benjamin0bd71eb2015-12-01 14:14:40 -05001043 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001044
1045 if (0) {
1046 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001047 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 }
1049
Adam Langley95c29f32014-06-20 12:00:00 -07001050err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001051 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 return ret;
1053}
Adam Langley95c29f32014-06-20 12:00:00 -07001054
David Benjaminc3c88822016-11-14 10:32:04 +09001055static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1056 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04001057 if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -04001058 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 }
Adam Langley95c29f32014-06-20 12:00:00 -07001060
David Benjamin56380462015-10-10 14:59:09 -04001061 assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
1062
1063 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1064 * known attack while we fix ChannelID itself. */
1065 if (ssl->s3->tlsext_channel_id_valid &&
1066 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1067 ssl->s3->tlsext_channel_id_valid = 0;
1068 }
1069
1070 /* If this is a resumption and the original handshake didn't support
1071 * ChannelID then we didn't record the original handshake hashes in the
1072 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001073 if (ssl->session != NULL &&
1074 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001075 ssl->s3->tlsext_channel_id_valid = 0;
1076 }
1077
David Benjamin721e8b72016-08-03 13:13:17 -04001078 struct timeval now;
1079 ssl_get_current_time(ssl, &now);
1080 ssl->s3->server_random[0] = now.tv_sec >> 24;
1081 ssl->s3->server_random[1] = now.tv_sec >> 16;
1082 ssl->s3->server_random[2] = now.tv_sec >> 8;
1083 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001084 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001085 return -1;
1086 }
1087
David Benjamin55108632016-08-11 22:01:18 -04001088 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1089 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001090
Steven Valdez87eab492016-06-27 16:34:59 -04001091 const SSL_SESSION *session = ssl->s3->new_session;
1092 if (ssl->session != NULL) {
1093 session = ssl->session;
1094 }
1095
David Benjamin75836432016-06-17 18:48:29 -04001096 CBB cbb, body, session_id;
1097 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1098 !CBB_add_u16(&body, ssl->version) ||
1099 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1100 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001101 !CBB_add_bytes(&session_id, session->session_id,
1102 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -04001103 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1104 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001105 !ssl_add_serverhello_tlsext(hs, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001106 !ssl_complete_message(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001107 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1108 CBB_cleanup(&cbb);
1109 return -1;
1110 }
1111
1112 ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -04001113 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001114}
Adam Langley95c29f32014-06-20 12:00:00 -07001115
David Benjaminc3c88822016-11-14 10:32:04 +09001116static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1117 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001118 if (ssl->state == SSL3_ST_SW_CERT_B) {
1119 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001120 }
1121
David Benjamin32a66d52016-07-13 22:03:11 -04001122 if (!ssl_has_certificate(ssl)) {
1123 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
1124 return 0;
1125 }
1126
David Benjamin75836432016-06-17 18:48:29 -04001127 if (!ssl3_output_cert_chain(ssl)) {
1128 return 0;
1129 }
1130 ssl->state = SSL3_ST_SW_CERT_B;
1131 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001132}
1133
David Benjaminc3c88822016-11-14 10:32:04 +09001134static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1135 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001136 if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) {
1137 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001138 }
1139
David Benjamin75836432016-06-17 18:48:29 -04001140 CBB cbb, body, ocsp_response;
1141 if (!ssl->method->init_message(ssl, &cbb, &body,
1142 SSL3_MT_CERTIFICATE_STATUS) ||
1143 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1144 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
1145 !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
1146 ssl->ctx->ocsp_response_length) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001147 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001148 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1149 CBB_cleanup(&cbb);
1150 return -1;
1151 }
1152
1153 ssl->state = SSL3_ST_SW_CERT_STATUS_B;
1154 return ssl->method->write_message(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001155}
1156
David Benjaminc3c88822016-11-14 10:32:04 +09001157static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1158 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -05001159 if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
David Benjamin75836432016-06-17 18:48:29 -04001160 return ssl->method->write_message(ssl);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001161 }
1162
David Benjamin2a0b3912015-12-18 01:01:21 -05001163 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001164 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001165
David Benjaminc42acee2016-06-17 17:47:58 -04001166 /* Put together the parameters. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001167 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001168 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1169 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001170
David Benjaminc42acee2016-06-17 17:47:58 -04001171 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1172 if (!CBB_init(&cbb, 128)) {
1173 goto err;
1174 }
1175
David Benjamin2a0b3912015-12-18 01:01:21 -05001176 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001177 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001178 size_t len =
1179 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1180 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1181 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1182 len)) {
1183 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001184 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186
David Benjamin7061e282015-03-19 11:10:48 -04001187 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001188 /* Determine the group to use. */
1189 DH *params = ssl->cert->dh_tmp;
1190 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1191 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001193 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001194 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001195 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001196 goto err;
1197 }
Steven Valdez87eab492016-06-27 16:34:59 -04001198 ssl->s3->new_session->key_exchange_info = DH_num_bits(params);
David Benjamin2a0b3912015-12-18 01:01:21 -05001199
David Benjamin974c7ba2015-12-19 16:58:04 -05001200 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001201 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001202 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001203 goto err;
1204 }
David Benjamin9f226a52015-04-26 22:12:32 -04001205
David Benjaminc3c88822016-11-14 10:32:04 +09001206 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001207 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001208 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001209 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001210 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001211 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001212 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001213 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001214 }
David Benjamin7061e282015-03-19 11:10:48 -04001215 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001216 /* Determine the group to use. */
1217 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001218 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001219 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001220 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001221 goto err;
1222 }
Steven Valdez87eab492016-06-27 16:34:59 -04001223 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001224
David Benjamin4298d772015-12-19 00:18:25 -05001225 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001226 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001227 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001228 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001229 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001230 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001231 goto err;
1232 }
1233 } else if (alg_k & SSL_kCECPQ1) {
David Benjaminc3c88822016-11-14 10:32:04 +09001234 SSL_ECDH_CTX_init_for_cecpq1(&hs->ecdh_ctx);
David Benjamin18a35182016-06-30 13:46:24 -04001235 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001236 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001237 goto err;
1238 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001239 } else {
1240 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 }
Adam Langley95c29f32014-06-20 12:00:00 -07001242
David Benjaminc3c88822016-11-14 10:32:04 +09001243 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001244 goto err;
1245 }
David Benjaminc42acee2016-06-17 17:47:58 -04001246 }
1247
1248 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001249 CBB body;
1250 if (!ssl->method->init_message(ssl, &cbb, &body,
1251 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001252 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001253 goto err;
1254 }
Adam Langley95c29f32014-06-20 12:00:00 -07001255
David Benjamin2a0b3912015-12-18 01:01:21 -05001256 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001257 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001258 if (!ssl_has_private_key(ssl)) {
1259 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001260 goto err;
1261 }
Adam Langley95c29f32014-06-20 12:00:00 -07001262
David Benjaminc42acee2016-06-17 17:47:58 -04001263 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001264 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001265 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001266 goto err;
1267 }
David Benjaminc42acee2016-06-17 17:47:58 -04001268 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001269 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001270 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1271 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1272 goto err;
1273 }
David Benjaminc42acee2016-06-17 17:47:58 -04001274 }
1275
1276 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001277 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001278 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001279 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001280 !CBB_reserve(&child, &ptr, max_sig_len)) {
1281 goto err;
1282 }
1283
David Benjamin2a0b3912015-12-18 01:01:21 -05001284 size_t sig_len;
1285 enum ssl_private_key_result_t sign_result;
1286 if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001287 CBB transcript;
1288 uint8_t *transcript_data;
1289 size_t transcript_len;
1290 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001291 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1292 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1293 SSL3_RANDOM_SIZE) ||
1294 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1295 SSL3_RANDOM_SIZE) ||
1296 !CBB_add_bytes(&transcript, hs->server_params,
1297 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001298 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1299 CBB_cleanup(&transcript);
1300 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1301 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001302 goto err;
1303 }
1304
Steven Valdezf0451ca2016-06-29 13:16:27 -04001305 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001306 signature_algorithm, transcript_data,
1307 transcript_len);
1308 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001309 } else {
David Benjamin2a0b3912015-12-18 01:01:21 -05001310 assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001311 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001313
1314 switch (sign_result) {
1315 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001316 if (!CBB_did_write(&child, sig_len)) {
1317 goto err;
1318 }
1319 break;
1320 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001321 goto err;
1322 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001323 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1324 ssl->state = SSL3_ST_SW_KEY_EXCH_B;
1325 goto err;
1326 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001327 }
1328
Steven Valdez5eead162016-11-11 22:23:25 -05001329 if (!ssl_complete_message(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001330 goto err;
1331 }
David Benjaminc42acee2016-06-17 17:47:58 -04001332
David Benjaminc3c88822016-11-14 10:32:04 +09001333 OPENSSL_free(hs->server_params);
1334 hs->server_params = NULL;
1335 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001336
David Benjamin2a0b3912015-12-18 01:01:21 -05001337 ssl->state = SSL3_ST_SW_KEY_EXCH_C;
David Benjamin75836432016-06-17 18:48:29 -04001338 return ssl->method->write_message(ssl);
nagendra modadugu601448a2015-07-24 09:31:31 -07001339
Adam Langley95c29f32014-06-20 12:00:00 -07001340err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001341 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001342 return -1;
1343}
Adam Langley95c29f32014-06-20 12:00:00 -07001344
David Benjamin75836432016-06-17 18:48:29 -04001345static int add_cert_types(SSL *ssl, CBB *cbb) {
1346 /* Get configured signature algorithms. */
1347 int have_rsa_sign = 0;
1348 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001349 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001350 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1351 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001352 switch (sig_algs[i]) {
1353 case SSL_SIGN_RSA_PKCS1_SHA512:
1354 case SSL_SIGN_RSA_PKCS1_SHA384:
1355 case SSL_SIGN_RSA_PKCS1_SHA256:
1356 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001357 have_rsa_sign = 1;
1358 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001359
Steven Valdez02563852016-06-23 13:33:05 -04001360 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1361 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1362 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1363 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001364 have_ecdsa_sign = 1;
1365 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001366 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001367 }
Adam Langley95c29f32014-06-20 12:00:00 -07001368
David Benjamin75836432016-06-17 18:48:29 -04001369 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1370 return 0;
1371 }
1372
1373 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1374 * check for SSL_kECDH or SSL_kECDHE. */
1375 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1376 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1377 return 0;
1378 }
1379
1380 return 1;
1381}
1382
David Benjaminc3c88822016-11-14 10:32:04 +09001383static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1384 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001385 if (ssl->state == SSL3_ST_SW_CERT_REQ_B) {
1386 return ssl->method->write_message(ssl);
1387 }
1388
David Benjamin32a66d52016-07-13 22:03:11 -04001389 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001390 if (!ssl->method->init_message(ssl, &cbb, &body,
1391 SSL3_MT_CERTIFICATE_REQUEST) ||
1392 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1393 !add_cert_types(ssl, &cert_types)) {
1394 goto err;
1395 }
1396
1397 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001398 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001399 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001400 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001401 goto err;
1402 }
Steven Valdez02563852016-06-23 13:33:05 -04001403
David Benjamin0fc37ef2016-08-17 15:29:46 -04001404 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001405 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1406 goto err;
1407 }
1408 }
David Benjamin75836432016-06-17 18:48:29 -04001409 }
1410
David Benjamin32a66d52016-07-13 22:03:11 -04001411 if (!ssl_add_client_CA_list(ssl, &body) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001412 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001413 goto err;
1414 }
1415
1416 ssl->state = SSL3_ST_SW_CERT_REQ_B;
1417 return ssl->method->write_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001418
Adam Langley95c29f32014-06-20 12:00:00 -07001419err:
David Benjamin75836432016-06-17 18:48:29 -04001420 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1421 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001422 return -1;
1423}
Adam Langley95c29f32014-06-20 12:00:00 -07001424
David Benjaminc3c88822016-11-14 10:32:04 +09001425static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1426 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001427 if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) {
1428 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001429 }
1430
David Benjamin75836432016-06-17 18:48:29 -04001431 CBB cbb, body;
1432 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
Steven Valdez5eead162016-11-11 22:23:25 -05001433 !ssl_complete_message(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001434 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1435 CBB_cleanup(&cbb);
1436 return -1;
1437 }
1438
1439 ssl->state = SSL3_ST_SW_SRVR_DONE_B;
1440 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001441}
1442
David Benjaminc3c88822016-11-14 10:32:04 +09001443static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1444 SSL *const ssl = hs->ssl;
1445 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001446
David Benjamin09eb6552016-07-08 14:32:11 -07001447 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1448 if (msg_ret <= 0) {
1449 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001450 }
1451
1452 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1453 if (ssl->version == SSL3_VERSION &&
1454 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001455 /* In SSL 3.0, the Certificate message is omitted to signal no
1456 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001457 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001458 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001459 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1460 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001461 }
1462
Adam Langley37646832016-08-01 16:16:46 -07001463 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001464 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001465 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001466 ssl->s3->tmp.reuse_message = 1;
1467 return 1;
1468 }
1469
David Benjamin9f1dc822016-06-07 17:03:46 -04001470 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001471 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1472 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001473 }
1474
David Benjamin5c900c82016-07-13 23:03:26 -04001475 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001476 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
David Benjamin5c900c82016-07-13 23:03:26 -04001477 uint8_t alert;
1478 STACK_OF(X509) *chain = ssl_parse_cert_chain(
David Benjaminbbaf3672016-11-17 10:53:09 +09001479 ssl, &alert, ssl->retain_only_sha256_of_client_certs
Steven Valdez87eab492016-06-27 16:34:59 -04001480 ? ssl->s3->new_session->peer_sha256
1481 : NULL,
David Benjamin5c900c82016-07-13 23:03:26 -04001482 &certificate_msg);
1483 if (chain == NULL) {
1484 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamin9f1dc822016-06-07 17:03:46 -04001485 goto err;
1486 }
1487
David Benjamin5c900c82016-07-13 23:03:26 -04001488 if (CBS_len(&certificate_msg) != 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001489 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001490 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1491 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001492 }
1493
David Benjamin5c900c82016-07-13 23:03:26 -04001494 if (sk_X509_num(chain) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001495 /* No client certificate so the handshake buffer may be discarded. */
1496 ssl3_free_handshake_buffer(ssl);
1497
David Benjamine455e512016-08-01 20:11:13 -04001498 /* In SSL 3.0, sending no certificate is signaled by omitting the
1499 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001500 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001501 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001502 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1503 goto err;
David Benjamine455e512016-08-01 20:11:13 -04001504 }
1505
1506 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001507 /* Fail for TLS only if we required a certificate */
1508 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001509 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1510 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001511 }
Adam Langley37646832016-08-01 16:16:46 -07001512
1513 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001514 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001515 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001516 } else {
David Benjamin5c900c82016-07-13 23:03:26 -04001517 /* The hash would have been filled in. */
David Benjaminbbaf3672016-11-17 10:53:09 +09001518 if (ssl->retain_only_sha256_of_client_certs) {
Steven Valdez87eab492016-06-27 16:34:59 -04001519 ssl->s3->new_session->peer_sha256_valid = 1;
David Benjamin5c900c82016-07-13 23:03:26 -04001520 }
1521
David Benjamin7aa31d62016-08-08 21:38:32 -04001522 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1523 chain)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001524 goto err;
David Benjamin9f1dc822016-06-07 17:03:46 -04001525 }
1526 }
1527
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001528 X509_free(ssl->s3->new_session->x509_peer);
1529 ssl->s3->new_session->x509_peer = sk_X509_shift(chain);
David Benjamin9f1dc822016-06-07 17:03:46 -04001530
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001531 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
1532 ssl->s3->new_session->x509_chain = chain;
1533 /* Inconsistency alert: x509_chain does *not* include the peer's own
David Benjamin9f1dc822016-06-07 17:03:46 -04001534 * certificate, while we do include it in s3_clnt.c */
1535
David Benjamin5c900c82016-07-13 23:03:26 -04001536 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001537
1538err:
David Benjamin5c900c82016-07-13 23:03:26 -04001539 sk_X509_pop_free(chain, X509_free);
1540 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001541}
1542
David Benjaminc3c88822016-11-14 10:32:04 +09001543static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1544 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001545 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001547 uint32_t alg_k;
1548 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 uint8_t *premaster_secret = NULL;
1550 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001551 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001552
David Benjamin4298d772015-12-19 00:18:25 -05001553 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001554 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001555
David Benjamin1e6d6df2016-05-13 18:28:17 -04001556 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin09eb6552016-07-08 14:32:11 -07001557 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE,
1558 ssl_hash_message);
1559 if (ret <= 0) {
1560 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001561 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 }
Adam Langley95c29f32014-06-20 12:00:00 -07001563
David Benjamin0d56f882015-12-19 17:05:56 -05001564 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1565 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1566 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001567
Adam Langleyfcf25832014-12-18 17:42:32 -08001568 /* If using a PSK key exchange, prepare the pre-shared key. */
1569 if (alg_a & SSL_aPSK) {
1570 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001571
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1573 * then this is the only field in the message. */
1574 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1575 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001576 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 al = SSL_AD_DECODE_ERROR;
1578 goto f_err;
1579 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001580
David Benjamin0d56f882015-12-19 17:05:56 -05001581 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001582 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001583 al = SSL_AD_INTERNAL_ERROR;
1584 goto f_err;
1585 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001586
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1588 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001589 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001590 al = SSL_AD_ILLEGAL_PARAMETER;
1591 goto f_err;
1592 }
David Benjamin35c02302014-07-13 04:14:59 -04001593
Steven Valdez87eab492016-06-27 16:34:59 -04001594 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001595 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001596 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001597 goto f_err;
1598 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001599
Adam Langleyfcf25832014-12-18 17:42:32 -08001600 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001601 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1602 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001603 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001604 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001605 al = SSL_AD_INTERNAL_ERROR;
1606 goto f_err;
1607 } else if (psk_len == 0) {
1608 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001609 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1611 goto f_err;
1612 }
1613 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001614
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 /* Depending on the key exchange method, compute |premaster_secret| and
1616 * |premaster_secret_len|. */
1617 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001618 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001619 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 decrypt_buf = OPENSSL_malloc(rsa_size);
1621 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001622 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001623 goto err;
1624 }
David Benjamin35c02302014-07-13 04:14:59 -04001625
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001626 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001627 size_t decrypt_len;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001628 if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001629 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001630 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001631 al = SSL_AD_HANDSHAKE_FAILURE;
1632 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1633 goto f_err;
1634 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001635 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001636 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001637 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1638 &encrypted_premaster_secret) ||
1639 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001640 al = SSL_AD_DECODE_ERROR;
1641 OPENSSL_PUT_ERROR(SSL,
1642 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1643 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001644 }
1645 } else {
1646 encrypted_premaster_secret = client_key_exchange;
1647 }
1648
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001649 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1650 * timing-sensitive code below. */
1651 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001652 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001653 CBS_data(&encrypted_premaster_secret),
1654 CBS_len(&encrypted_premaster_secret));
1655 } else {
David Benjamin1e6d6df2016-05-13 18:28:17 -04001656 assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001657 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001658 decrypt_result =
1659 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001661
1662 switch (decrypt_result) {
1663 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001664 break;
1665 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001666 goto err;
1667 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001668 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin1e6d6df2016-05-13 18:28:17 -04001669 ssl->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001670 goto err;
1671 }
1672
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001673 if (decrypt_len != rsa_size) {
1674 al = SSL_AD_DECRYPT_ERROR;
1675 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1676 goto f_err;
1677 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001678
David Benjamin3f5b43d2015-12-01 19:31:24 -05001679 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1680 * section 7.4.7.1. */
1681 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1682 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001683 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001684 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 goto err;
1686 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001687 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001688 goto err;
1689 }
1690
David Benjamin3f5b43d2015-12-01 19:31:24 -05001691 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1692 * publicly invalid. */
1693 if (decrypt_len < 11 + premaster_secret_len) {
1694 al = SSL_AD_DECRYPT_ERROR;
1695 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1696 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001697 }
David Benjamin35c02302014-07-13 04:14:59 -04001698
David Benjamin3f5b43d2015-12-01 19:31:24 -05001699 /* Check the padding. See RFC 3447, section 7.2.2. */
1700 size_t padding_len = decrypt_len - premaster_secret_len;
1701 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1702 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001703 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001704 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1705 }
1706 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1707
1708 /* The premaster secret must begin with |client_version|. This too must be
1709 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1710 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjamin0d56f882015-12-19 17:05:56 -05001711 (unsigned)(ssl->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001712 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjamin0d56f882015-12-19 17:05:56 -05001713 (unsigned)(ssl->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001714
1715 /* Select, in constant time, either the decrypted premaster or the random
1716 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001717 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001718 premaster_secret[i] = constant_time_select_8(
1719 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1720 }
1721
1722 OPENSSL_free(decrypt_buf);
1723 decrypt_buf = NULL;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001724 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001725 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001726 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001727 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001728 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001729 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001730 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 goto f_err;
1732 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001733
David Benjamin4298d772015-12-19 00:18:25 -05001734 /* Compute the premaster. */
1735 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001736 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001737 &premaster_secret_len, &alert, CBS_data(&peer_key),
1738 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001739 al = alert;
1740 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 }
Adam Langley95c29f32014-06-20 12:00:00 -07001742
David Benjamin4298d772015-12-19 00:18:25 -05001743 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001744 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 } else if (alg_k & SSL_kPSK) {
1746 /* For plain PSK, other_secret is a block of 0s with the same length as the
1747 * pre-shared key. */
1748 premaster_secret_len = psk_len;
1749 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1750 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001751 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001752 goto err;
1753 }
1754 memset(premaster_secret, 0, premaster_secret_len);
1755 } else {
1756 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001757 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 goto f_err;
1759 }
Adam Langley95c29f32014-06-20 12:00:00 -07001760
Adam Langleyfcf25832014-12-18 17:42:32 -08001761 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1762 * pre-shared key. */
1763 if (alg_a & SSL_aPSK) {
1764 CBB new_premaster, child;
1765 uint8_t *new_data;
1766 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001767
David Benjamina8653202015-06-28 01:26:10 -04001768 CBB_zero(&new_premaster);
1769 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1770 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1772 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1773 !CBB_add_bytes(&child, psk, psk_len) ||
1774 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001775 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001776 CBB_cleanup(&new_premaster);
1777 goto err;
1778 }
David Benjamin14c83e72014-07-13 04:54:57 -04001779
Adam Langleyfcf25832014-12-18 17:42:32 -08001780 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1781 OPENSSL_free(premaster_secret);
1782 premaster_secret = new_data;
1783 premaster_secret_len = new_len;
1784 }
David Benjamin14c83e72014-07-13 04:54:57 -04001785
Adam Langleyfcf25832014-12-18 17:42:32 -08001786 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001787 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1788 ssl, ssl->s3->new_session->master_key, premaster_secret,
1789 premaster_secret_len);
1790 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001791 goto err;
1792 }
Steven Valdez87eab492016-06-27 16:34:59 -04001793 ssl->s3->new_session->extended_master_secret =
1794 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001795
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1797 OPENSSL_free(premaster_secret);
1798 return 1;
1799
Adam Langley95c29f32014-06-20 12:00:00 -07001800f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001801 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001802err:
David Benjamin4298d772015-12-19 00:18:25 -05001803 if (premaster_secret != NULL) {
1804 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001805 OPENSSL_free(premaster_secret);
1806 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001807 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 return -1;
1810}
Adam Langley95c29f32014-06-20 12:00:00 -07001811
David Benjaminc3c88822016-11-14 10:32:04 +09001812static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1813 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001814 int al, ret = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001815 CBS certificate_verify, signature;
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001816 X509 *peer = ssl->s3->new_session->x509_peer;
Adam Langleyfcf25832014-12-18 17:42:32 -08001817 EVP_PKEY *pkey = NULL;
David Benjamin6553b372014-07-22 14:11:30 -04001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 /* Only RSA and ECDSA client certificates are supported, so a
1820 * CertificateVerify is required if and only if there's a client certificate.
1821 * */
1822 if (peer == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001823 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001824 return 1;
1825 }
Adam Langley95c29f32014-06-20 12:00:00 -07001826
David Benjamin09eb6552016-07-08 14:32:11 -07001827 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
1828 ssl_dont_hash_message);
1829 if (msg_ret <= 0) {
1830 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001831 }
David Benjaminef865502014-08-24 02:48:34 -04001832
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 /* Filter out unsupported certificate types. */
1834 pkey = X509_get_pubkey(peer);
David Benjamin9d0847a2015-02-16 03:57:55 -05001835 if (pkey == NULL) {
1836 goto err;
1837 }
David Benjamin6897dbe2014-07-12 20:18:28 -04001838
David Benjamin09eb6552016-07-08 14:32:11 -07001839 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001840
Adam Langleyfcf25832014-12-18 17:42:32 -08001841 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001842 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001843 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001844 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001845 al = SSL_AD_DECODE_ERROR;
1846 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1847 goto f_err;
1848 }
David Benjamin887c3002016-07-08 16:15:32 -07001849 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001850 goto f_err;
1851 }
Steven Valdez02563852016-06-23 13:33:05 -04001852 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Steven Valdezf0451ca2016-06-29 13:16:27 -04001853 } else if (pkey->type == EVP_PKEY_RSA) {
1854 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1855 } else if (pkey->type == EVP_PKEY_EC) {
1856 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001857 } else {
1858 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1859 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1860 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 }
David Benjamin854dd652014-08-26 00:32:30 -04001862
Adam Langleyfcf25832014-12-18 17:42:32 -08001863 /* Parse and verify the signature. */
1864 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1865 CBS_len(&certificate_verify) != 0) {
1866 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001867 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001868 goto f_err;
1869 }
Adam Langley95c29f32014-06-20 12:00:00 -07001870
Steven Valdez2b8415e2016-06-30 13:27:23 -04001871 int sig_ok;
1872 /* The SSL3 construction for CertificateVerify does not decompose into a
1873 * single final digest and signature, and must be special-cased. */
1874 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001875 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001876 uint8_t digest[EVP_MAX_MD_SIZE];
1877 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001878 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001879 signature_algorithm)) {
1880 goto err;
1881 }
1882
Steven Valdez2b8415e2016-06-30 13:27:23 -04001883 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
1884 sig_ok = pctx != NULL &&
1885 EVP_PKEY_verify_init(pctx) &&
1886 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1887 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1888 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001889 EVP_PKEY_CTX_free(pctx);
1890 } else {
1891 sig_ok = ssl_public_key_verify(
1892 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1893 pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
1894 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001896
David Benjaminbf82aed2016-03-01 22:57:40 -05001897#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1898 sig_ok = 1;
1899 ERR_clear_error();
1900#endif
1901 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001902 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001903 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001904 goto f_err;
1905 }
1906
Steven Valdez2b8415e2016-06-30 13:27:23 -04001907 /* The handshake buffer is no longer necessary, and we may hash the current
1908 * message.*/
1909 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001910 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001911 goto err;
1912 }
1913
Adam Langleyfcf25832014-12-18 17:42:32 -08001914 ret = 1;
1915
1916 if (0) {
1917 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001918 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001919 }
1920
David Benjamin854dd652014-08-26 00:32:30 -04001921err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001922 EVP_PKEY_free(pkey);
Adam Langley95c29f32014-06-20 12:00:00 -07001923
Adam Langleyfcf25832014-12-18 17:42:32 -08001924 return ret;
1925}
Adam Langley95c29f32014-06-20 12:00:00 -07001926
Adam Langley95c29f32014-06-20 12:00:00 -07001927/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1928 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001929static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1930 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001931 int ret =
1932 ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message);
1933 if (ret <= 0) {
1934 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001935 }
Adam Langley95c29f32014-06-20 12:00:00 -07001936
David Benjamin09eb6552016-07-08 14:32:11 -07001937 CBS next_protocol, selected_protocol, padding;
1938 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001939 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1940 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001941 CBS_len(&next_protocol) != 0) {
1942 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1943 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1944 return 0;
1945 }
1946
1947 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001948 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001949 return 0;
1950 }
Adam Langley95c29f32014-06-20 12:00:00 -07001951
Adam Langleyfcf25832014-12-18 17:42:32 -08001952 return 1;
1953}
Adam Langley95c29f32014-06-20 12:00:00 -07001954
Adam Langley1258b6a2014-06-20 12:00:00 -07001955/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001956static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1957 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001958 int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
1959 ssl_dont_hash_message);
1960 if (msg_ret <= 0) {
1961 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001962 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001963
Nick Harper60a85cb2016-09-23 16:25:11 -07001964 if (!tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001965 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001966 return -1;
1967 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001968 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001969}
David Benjamin9f1dc822016-06-07 17:03:46 -04001970
David Benjaminc3c88822016-11-14 10:32:04 +09001971static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1972 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001973 if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) {
1974 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001975 }
1976
David Benjamin123db572016-11-03 16:59:25 -04001977 const SSL_SESSION *session;
1978 SSL_SESSION *session_copy = NULL;
1979 if (ssl->session == NULL) {
1980 /* Fix the timeout to measure from the ticket issuance time. */
1981 ssl_session_refresh_time(ssl, ssl->s3->new_session);
1982 session = ssl->s3->new_session;
1983 } else {
1984 /* We are renewing an existing session. Duplicate the session to adjust the
1985 * timeout. */
1986 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1987 if (session_copy == NULL) {
1988 return -1;
1989 }
1990
1991 ssl_session_refresh_time(ssl, session_copy);
1992 session = session_copy;
1993 }
1994
David Benjamin75836432016-06-17 18:48:29 -04001995 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001996 int ok =
1997 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1998 CBB_add_u32(&body, session->timeout) &&
1999 CBB_add_u16_length_prefixed(&body, &ticket) &&
2000 ssl_encrypt_ticket(ssl, &ticket, session) &&
Steven Valdez5eead162016-11-11 22:23:25 -05002001 ssl_complete_message(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04002002
2003 SSL_SESSION_free(session_copy);
2004 CBB_cleanup(&cbb);
2005
2006 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04002007 return -1;
David Benjamin75836432016-06-17 18:48:29 -04002008 }
2009
2010 ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
Steven Valdez1e6f11a2016-07-27 11:10:52 -04002011 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04002012}