blob: fd6c8e97aab27e326c69410367d128d0f1d3cf64 [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 Benjamin59bae5a2017-02-02 23:51:22 -0500174static int ssl3_process_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_select_certificate(SSL_HANDSHAKE *hs);
176static int ssl3_select_parameters(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900177static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
181static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
182static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
183static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
184static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
185static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
186static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
187static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
188static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400189
Steven Valdez258508f2017-01-25 15:47:49 -0500190static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
191 CRYPTO_STATIC_MUTEX_INIT;
192static uint64_t g_v2clienthello_count = 0;
193
194uint64_t SSL_get_v2clienthello_count(void) {
195 CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
196 uint64_t ret = g_v2clienthello_count;
197 CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
198 return ret;
199}
200
David Benjamince8c9d22016-11-14 10:45:16 +0900201int ssl3_accept(SSL_HANDSHAKE *hs) {
202 SSL *const ssl = hs->ssl;
David Benjamin3fa27772015-04-17 22:32:19 -0400203 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 int ret = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin0d56f882015-12-19 17:05:56 -0500206 assert(ssl->handshake_func == ssl3_accept);
207 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500210 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamincb0c29f2016-12-12 17:00:50 -0500212 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400213 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400214 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500215 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamin59bae5a2017-02-02 23:51:22 -0500219 ret = ssl->method->ssl_get_message(ssl);
220 if (ret <= 0) {
221 goto end;
Steven Valdez143e8b32016-07-11 13:19:03 -0400222 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500223 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
224 break;
225
226 case SSL3_ST_SR_CLNT_HELLO_B:
227 ret = ssl3_process_client_hello(hs);
228 if (ret <= 0) {
229 goto end;
230 }
231 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
232 break;
233
234 case SSL3_ST_SR_CLNT_HELLO_C:
235 ret = ssl3_select_certificate(hs);
236 if (ret <= 0) {
237 goto end;
238 }
239 if (hs->state != SSL_ST_TLS13) {
240 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
241 }
242 break;
243
244 case SSL3_ST_SR_CLNT_HELLO_D:
245 ret = ssl3_select_parameters(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (ret <= 0) {
247 goto end;
248 }
David Benjamindf50eec2016-06-07 16:49:42 -0400249 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500250 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500252
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900254 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 if (ret <= 0) {
256 goto end;
257 }
Steven Valdez87eab492016-06-27 16:34:59 -0400258 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500259 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500261 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 case SSL3_ST_SW_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500266 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900267 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 if (ret <= 0) {
269 goto end;
270 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500272 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100275 case SSL3_ST_SW_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900276 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900277 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400278 if (ret <= 0) {
279 goto end;
280 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100281 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500282 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100283 break;
284
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 case SSL3_ST_SW_KEY_EXCH_A:
286 case SSL3_ST_SW_KEY_EXCH_B:
David Benjamin45738dd2017-02-09 20:01:26 -0500287 alg_a = hs->new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700288
David Benjamindf50eec2016-06-07 16:49:42 -0400289 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin45738dd2017-02-09 20:01:26 -0500290 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500291 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900292 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500293 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500295 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
David Benjamincb0c29f2016-12-12 17:00:50 -0500298 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700300
Adam Langleyfcf25832014-12-18 17:42:32 -0800301 case SSL3_ST_SW_CERT_REQ_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900302 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900303 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 if (ret <= 0) {
305 goto end;
306 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500308 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 case SSL3_ST_SW_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900312 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 if (ret <= 0) {
314 goto end;
315 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500316 hs->next_state = SSL3_ST_SR_CERT_A;
317 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900321 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900322 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 if (ret <= 0) {
324 goto end;
325 }
326 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500327 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 case SSL3_ST_SR_KEY_EXCH_A:
331 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900332 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (ret <= 0) {
334 goto end;
335 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500336 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900340 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 if (ret <= 0) {
342 goto end;
343 }
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamincb0c29f2016-12-12 17:00:50 -0500345 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamina41280d2015-11-26 02:16:49 -0500348 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400349 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500350 if (ret <= 0) {
351 goto end;
352 }
Adam Langley95c29f32014-06-20 12:00:00 -0700353
David Benjamin67739722016-11-17 17:03:59 +0900354 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 ret = -1;
356 goto end;
357 }
David Benjamina41280d2015-11-26 02:16:49 -0500358
David Benjamincb0c29f2016-12-12 17:00:50 -0500359 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800360 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900363 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900364 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400365 if (ret <= 0) {
366 goto end;
367 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500369 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400373 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900374 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400375 if (ret <= 0) {
376 goto end;
377 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500379 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900383 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 if (ret <= 0) {
385 goto end;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
David Benjamindf50eec2016-06-07 16:49:42 -0400388 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400389 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500390 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500392 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 }
David Benjamindf50eec2016-06-07 16:49:42 -0400394
Steven Valdez87eab492016-06-27 16:34:59 -0400395 /* If this is a full handshake with ChannelID then record the handshake
David Benjamin45738dd2017-02-09 20:01:26 -0500396 * hashes in |hs->new_session| in case we need them to verify a
Steven Valdez87eab492016-06-27 16:34:59 -0400397 * ChannelID signature on a resumption of this session in the future. */
398 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Steven Valdez908ac192017-01-12 13:17:07 -0500399 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 if (ret <= 0) {
401 goto end;
402 }
403 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 case SSL3_ST_SW_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900407 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900408 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400409 if (ret <= 0) {
410 goto end;
411 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500413 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
David Benjamin352d0a92016-06-28 11:22:02 -0400416 case SSL3_ST_SW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500417 if (!ssl->method->add_change_cipher_spec(ssl) ||
418 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800419 ret = -1;
420 goto end;
421 }
David Benjamindaf207a2017-01-03 18:37:41 -0500422
423 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 case SSL3_ST_SW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500427 ret = ssl3_send_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 if (ret <= 0) {
429 goto end;
430 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500431 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400432 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500433 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500435 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
David Benjamin9f1dc822016-06-07 17:03:46 -0400439 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500440 ret = ssl->method->flush_flight(ssl);
441 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400442 goto end;
443 }
444
David Benjamincb0c29f2016-12-12 17:00:50 -0500445 hs->state = hs->next_state;
446 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400447 ssl->method->expect_flight(ssl);
448 }
449 break;
450
Steven Valdez143e8b32016-07-11 13:19:03 -0400451 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900452 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400453 if (ret <= 0) {
454 goto end;
455 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500456 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400457 break;
458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400460 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Steven Valdez87eab492016-06-27 16:34:59 -0400462 /* If we aren't retaining peer certificates then we can discard it
463 * now. */
David Benjamin45738dd2017-02-09 20:01:26 -0500464 if (hs->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900465 ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -0500466 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
467 hs->new_session->certs = NULL;
468 ssl->ctx->x509_method->session_clear(hs->new_session);
Steven Valdez87eab492016-06-27 16:34:59 -0400469 }
470
471 SSL_SESSION_free(ssl->s3->established_session);
472 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400473 SSL_SESSION_up_ref(ssl->session);
474 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400475 } else {
David Benjamin45738dd2017-02-09 20:01:26 -0500476 ssl->s3->established_session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -0400477 ssl->s3->established_session->not_resumable = 0;
David Benjamin45738dd2017-02-09 20:01:26 -0500478 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400479 }
480
Steven Valdez258508f2017-01-25 15:47:49 -0500481 if (hs->v2_clienthello) {
482 CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock);
483 g_v2clienthello_count++;
484 CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock);
485 }
486
David Benjamin78476f62016-11-12 11:20:55 +0900487 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900488 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900489
David Benjamin4e9cc712016-06-01 20:16:03 -0400490 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 ret = 1;
492 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700493
Adam Langleyfcf25832014-12-18 17:42:32 -0800494 default:
David Benjamin3570d732015-06-29 00:28:17 -0400495 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 ret = -1;
497 goto end;
498 }
Adam Langley95c29f32014-06-20 12:00:00 -0700499
David Benjamin16b1b1d2017-02-10 23:41:14 -0500500 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400501 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 }
504
Adam Langley95c29f32014-06-20 12:00:00 -0700505end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400506 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 return ret;
508}
Adam Langley95c29f32014-06-20 12:00:00 -0700509
David Benjamin731058e2016-12-03 23:15:13 -0500510int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
511 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400512 CBS cipher_suites;
513 CBS_init(&cipher_suites, client_hello->cipher_suites,
514 client_hello->cipher_suites_len);
515
516 while (CBS_len(&cipher_suites) > 0) {
517 uint16_t got_id;
518 if (!CBS_get_u16(&cipher_suites, &got_id)) {
519 return 0;
520 }
521
522 if (got_id == id) {
523 return 1;
524 }
525 }
526
527 return 0;
528}
529
David Benjaminf04c2e92016-12-06 13:35:25 -0500530static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500531 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500532 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500533 assert(!ssl->s3->have_version);
David Benjamin25fe85b2016-08-09 20:00:32 -0400534 uint16_t min_version, max_version;
535 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
536 *out_alert = SSL_AD_PROTOCOL_VERSION;
537 return 0;
538 }
539
David Benjamin2dc02042016-09-19 19:57:37 -0400540 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400541 /* Check supported_versions extension if it is present. */
542 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500543 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
544 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400545 CBS versions;
546 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
547 CBS_len(&supported_versions) != 0 ||
548 CBS_len(&versions) == 0) {
549 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
550 *out_alert = SSL_AD_DECODE_ERROR;
551 return 0;
552 }
553
Brian Smithf85d3232016-10-28 10:34:06 -1000554 /* Choose the newest commonly-supported version advertised by the client.
555 * The client orders the versions according to its preferences, but we're
556 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400557 int found_version = 0;
558 while (CBS_len(&versions) != 0) {
559 uint16_t ext_version;
560 if (!CBS_get_u16(&versions, &ext_version)) {
561 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
562 *out_alert = SSL_AD_DECODE_ERROR;
563 return 0;
564 }
565 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
566 continue;
567 }
568 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000569 ext_version <= max_version &&
570 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400571 version = ext_version;
572 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400573 }
574 }
575
576 if (!found_version) {
577 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400578 }
579 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400580 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
581 * do not use this mechanism. */
582 if (SSL_is_dtls(ssl)) {
583 if (client_hello->version <= DTLS1_2_VERSION) {
584 version = TLS1_2_VERSION;
585 } else if (client_hello->version <= DTLS1_VERSION) {
586 version = TLS1_1_VERSION;
587 } else {
588 goto unsupported_protocol;
589 }
590 } else {
591 if (client_hello->version >= TLS1_2_VERSION) {
592 version = TLS1_2_VERSION;
593 } else if (client_hello->version >= TLS1_1_VERSION) {
594 version = TLS1_1_VERSION;
595 } else if (client_hello->version >= TLS1_VERSION) {
596 version = TLS1_VERSION;
597 } else if (client_hello->version >= SSL3_VERSION) {
598 version = SSL3_VERSION;
599 } else {
600 goto unsupported_protocol;
601 }
David Benjamin2dc02042016-09-19 19:57:37 -0400602 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400603
Steven Valdezfdd10992016-09-15 16:27:05 -0400604 /* Apply our minimum and maximum version. */
605 if (version > max_version) {
606 version = max_version;
607 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400608
Steven Valdezfdd10992016-09-15 16:27:05 -0400609 if (version < min_version) {
610 goto unsupported_protocol;
611 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400612 }
613
614 /* Handle FALLBACK_SCSV. */
615 if (ssl_client_cipher_list_contains_cipher(client_hello,
616 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
617 version < max_version) {
618 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
619 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
620 return 0;
621 }
622
David Benjaminf04c2e92016-12-06 13:35:25 -0500623 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400624 ssl->version = ssl->method->version_to_wire(version);
David Benjamin25fe85b2016-08-09 20:00:32 -0400625
626 /* At this point, the connection's version is known and |ssl->version| is
627 * fixed. Begin enforcing the record-layer version. */
628 ssl->s3->have_version = 1;
629
630 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400631
632unsupported_protocol:
633 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
634 *out_alert = SSL_AD_PROTOCOL_VERSION;
635 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400636}
637
David Benjamin2578b292016-12-03 23:19:55 -0500638static STACK_OF(SSL_CIPHER) *
639 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
640 CBS cipher_suites;
641 CBS_init(&cipher_suites, client_hello->cipher_suites,
642 client_hello->cipher_suites_len);
643
644 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
645 if (sk == NULL) {
646 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
647 goto err;
648 }
649
650 while (CBS_len(&cipher_suites) > 0) {
651 uint16_t cipher_suite;
652
653 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
654 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
655 goto err;
656 }
657
658 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
659 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
660 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
661 goto err;
662 }
663 }
664
665 return sk;
666
667err:
668 sk_SSL_CIPHER_free(sk);
669 return NULL;
670}
671
672/* ssl_get_compatible_server_ciphers determines the key exchange and
673 * authentication cipher suite masks compatible with the server configuration
674 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
675 * exchange mask and |*out_mask_a| to the authentication mask. */
676static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
677 uint32_t *out_mask_k,
678 uint32_t *out_mask_a) {
679 SSL *const ssl = hs->ssl;
680 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
681 *out_mask_k = SSL_kGENERIC;
682 *out_mask_a = SSL_aGENERIC;
683 return;
684 }
685
686 uint32_t mask_k = 0;
687 uint32_t mask_a = 0;
688
Adam Langley3a2b47a2017-01-24 13:59:42 -0800689 if (ssl_has_certificate(ssl)) {
David Benjamin2578b292016-12-03 23:19:55 -0500690 int type = ssl_private_key_type(ssl);
691 if (type == NID_rsaEncryption) {
692 mask_k |= SSL_kRSA;
693 mask_a |= SSL_aRSA;
694 } else if (ssl_is_ecdsa_key_type(type)) {
695 mask_a |= SSL_aECDSA;
696 }
697 }
698
699 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
700 mask_k |= SSL_kDHE;
701 }
702
703 /* Check for a shared group to consider ECDHE ciphers. */
704 uint16_t unused;
705 if (tls1_get_shared_group(hs, &unused)) {
706 mask_k |= SSL_kECDHE;
707 }
708
David Benjamin2578b292016-12-03 23:19:55 -0500709 /* PSK requires a server callback. */
710 if (ssl->psk_server_callback != NULL) {
711 mask_k |= SSL_kPSK;
712 mask_a |= SSL_aPSK;
713 }
714
715 *out_mask_k = mask_k;
716 *out_mask_a = mask_a;
717}
718
719static const SSL_CIPHER *ssl3_choose_cipher(
720 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
721 const struct ssl_cipher_preference_list_st *server_pref) {
722 SSL *const ssl = hs->ssl;
723 const SSL_CIPHER *c, *ret = NULL;
724 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
725 int ok;
726 size_t cipher_index;
727 uint32_t alg_k, alg_a, mask_k, mask_a;
728 /* in_group_flags will either be NULL, or will point to an array of bytes
729 * which indicate equal-preference groups in the |prio| stack. See the
730 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
731 * struct. */
732 const uint8_t *in_group_flags;
733 /* group_min contains the minimal index so far found in a group, or -1 if no
734 * such value exists yet. */
735 int group_min = -1;
736
737 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
738 if (clnt == NULL) {
739 return NULL;
740 }
741
742 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
743 prio = srvr;
744 in_group_flags = server_pref->in_group_flags;
745 allow = clnt;
746 } else {
747 prio = clnt;
748 in_group_flags = NULL;
749 allow = srvr;
750 }
751
752 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
753
754 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
755 c = sk_SSL_CIPHER_value(prio, i);
756
757 ok = 1;
758
759 /* Check the TLS version. */
760 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
761 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
762 ok = 0;
763 }
764
765 alg_k = c->algorithm_mkey;
766 alg_a = c->algorithm_auth;
767
768 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
769
770 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
771 if (in_group_flags != NULL && in_group_flags[i] == 1) {
772 /* This element of |prio| is in a group. Update the minimum index found
773 * so far and continue looking. */
774 if (group_min == -1 || (size_t)group_min > cipher_index) {
775 group_min = cipher_index;
776 }
777 } else {
778 if (group_min != -1 && (size_t)group_min < cipher_index) {
779 cipher_index = group_min;
780 }
781 ret = sk_SSL_CIPHER_value(allow, cipher_index);
782 break;
783 }
784 }
785
786 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
787 /* We are about to leave a group, but we found a match in it, so that's
788 * our answer. */
789 ret = sk_SSL_CIPHER_value(allow, group_min);
790 break;
791 }
792 }
793
794 sk_SSL_CIPHER_free(clnt);
795 return ret;
796}
797
David Benjamin59bae5a2017-02-02 23:51:22 -0500798static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900799 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500800 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
801 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
David Benjamin731058e2016-12-03 23:15:13 -0500804 SSL_CLIENT_HELLO client_hello;
805 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
806 ssl->init_num)) {
David Benjamin3570d732015-06-29 00:28:17 -0400807 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin59bae5a2017-02-02 23:51:22 -0500808 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
809 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 }
Adam Langley95c29f32014-06-20 12:00:00 -0700811
David Benjamin59bae5a2017-02-02 23:51:22 -0500812 /* Run the early callback. */
813 if (ssl->ctx->select_certificate_cb != NULL) {
814 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000815 case ssl_select_cert_retry:
David Benjamin59bae5a2017-02-02 23:51:22 -0500816 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
817 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400818
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000819 case ssl_select_cert_error:
David Benjamin59bae5a2017-02-02 23:51:22 -0500820 /* Connection rejected. */
821 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
822 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
823 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400824
David Benjamin59bae5a2017-02-02 23:51:22 -0500825 default:
826 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400827 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500828 }
David Benjamine14ff062016-08-09 16:21:24 -0400829
Adam Langley04540a72017-02-09 10:11:50 -0800830 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500831 if (!negotiate_version(hs, &alert, &client_hello)) {
832 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
833 return -1;
834 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400835
David Benjamin59bae5a2017-02-02 23:51:22 -0500836 /* Load the client random. */
837 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
838 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
839 return -1;
840 }
841 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
842 client_hello.random_len);
843
844 /* Only null compression is supported. TLS 1.3 further requires the peer
845 * advertise no other compression. */
846 if (OPENSSL_memchr(client_hello.compression_methods, 0,
847 client_hello.compression_methods_len) == NULL ||
848 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
849 client_hello.compression_methods_len != 1)) {
850 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
851 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
852 return -1;
853 }
854
855 /* TLS extensions. */
856 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
857 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
858 return -1;
859 }
860
861 return 1;
862}
863
864static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
865 SSL *const ssl = hs->ssl;
866 /* Call |cert_cb| to update server certificates if required. */
867 if (ssl->cert->cert_cb != NULL) {
868 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
869 if (rv == 0) {
870 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
871 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin25fe85b2016-08-09 20:00:32 -0400872 return -1;
873 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500874 if (rv < 0) {
875 ssl->rwstate = SSL_X509_LOOKUP;
876 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900877 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900878 }
879
Adam Langley94a62e62017-02-10 15:24:56 -0800880 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(ssl)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500881 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900882 }
883
David Benjamin59bae5a2017-02-02 23:51:22 -0500884 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
885 /* Jump to the TLS 1.3 state machine. */
886 hs->state = SSL_ST_TLS13;
887 hs->do_tls13_handshake = tls13_server_handshake;
888 return 1;
889 }
890
891 SSL_CLIENT_HELLO client_hello;
892 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
893 ssl->init_num)) {
894 return -1;
895 }
896
897 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
898 * certificate is finalized. */
David Benjamin45738dd2017-02-09 20:01:26 -0500899 hs->new_cipher =
David Benjamin59bae5a2017-02-02 23:51:22 -0500900 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjamin45738dd2017-02-09 20:01:26 -0500901 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500902 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
903 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
904 return -1;
905 }
906
907 return 1;
908}
909
910static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
911 SSL *const ssl = hs->ssl;
912 uint8_t al = SSL_AD_INTERNAL_ERROR;
913 int ret = -1;
914 SSL_SESSION *session = NULL;
915
916 SSL_CLIENT_HELLO client_hello;
917 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
918 ssl->init_num)) {
919 return -1;
920 }
David Benjamin34202b92016-11-16 19:07:53 +0900921
922 /* Determine whether we are doing session resumption. */
923 int tickets_supported = 0, renew_ticket = 0;
924 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
925 &client_hello)) {
926 case ssl_session_success:
927 break;
928 case ssl_session_error:
929 goto err;
930 case ssl_session_retry:
931 ssl->rwstate = SSL_PENDING_SESSION;
932 goto err;
Adam Langley4c341d02017-03-08 19:33:21 -0800933 case ssl_session_ticket_retry:
934 ssl->rwstate = SSL_PENDING_TICKET;
935 goto err;
David Benjamin34202b92016-11-16 19:07:53 +0900936 }
937
938 if (session != NULL) {
David Benjaminfc02b592017-02-17 16:26:01 -0500939 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900940 /* A ClientHello without EMS that attempts to resume a session with EMS
941 * is fatal to the connection. */
942 al = SSL_AD_HANDSHAKE_FAILURE;
943 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
944 goto f_err;
945 }
946
David Benjamin45738dd2017-02-09 20:01:26 -0500947 if (!ssl_session_is_resumable(hs, session) ||
David Benjamin34202b92016-11-16 19:07:53 +0900948 /* If the client offers the EMS extension, but the previous session
949 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500950 hs->extended_master_secret != session->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900951 SSL_SESSION_free(session);
952 session = NULL;
953 }
954 }
955
956 if (session != NULL) {
957 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900958 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900959 ssl->session = session;
960 session = NULL;
961 ssl->s3->session_reused = 1;
962 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900963 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900964 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900965 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900966 goto err;
967 }
968
969 /* Clear the session ID if we want the session to be single-use. */
970 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500971 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900972 }
973 }
974
975 if (ssl->ctx->dos_protection_cb != NULL &&
976 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
977 /* Connection rejected for DOS reasons. */
978 al = SSL_AD_INTERNAL_ERROR;
979 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
980 goto f_err;
981 }
982
David Benjaminf01f42a2016-11-16 19:05:33 +0900983 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500984 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400985
David Benjamin4eb95cc2016-11-16 17:08:23 +0900986 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900987 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500988 OPENSSL_free(hs->new_session->tlsext_hostname);
989 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
990 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900991 al = SSL_AD_INTERNAL_ERROR;
992 goto f_err;
993 }
994 }
995
David Benjamin5c1ce292015-05-26 16:59:43 -0400996 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900997 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400998 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500999 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1000 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +09001001 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001002 }
David Benjaminc032dfa2016-05-12 14:54:57 -04001003 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -05001004 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +09001005 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001006 }
Adam Langley37646832016-08-01 16:16:46 -07001007
David Benjaminc3c88822016-11-14 10:32:04 +09001008 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001009 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001010 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001011 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -07001012 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 }
1014
David Benjaminf01f42a2016-11-16 19:05:33 +09001015 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1016 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001017 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001018 goto f_err;
1019 }
1020
David Benjaminf71036e2017-01-21 14:49:39 -05001021 /* Now that all parameters are known, initialize the handshake hash and hash
1022 * the ClientHello. */
Steven Valdez908ac192017-01-12 13:17:07 -05001023 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
David Benjamin45738dd2017-02-09 20:01:26 -05001024 hs->new_cipher->algorithm_prf) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001025 !ssl_hash_current_message(hs)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001026 goto f_err;
1027 }
1028
Steven Valdez2b8415e2016-06-30 13:27:23 -04001029 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001030 if (!hs->cert_request) {
Steven Valdez908ac192017-01-12 13:17:07 -05001031 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 }
1033
David Benjamin0bd71eb2015-12-01 14:14:40 -05001034 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001035
1036 if (0) {
1037 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001038 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 }
1040
Adam Langley95c29f32014-06-20 12:00:00 -07001041err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001042 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 return ret;
1044}
Adam Langley95c29f32014-06-20 12:00:00 -07001045
David Benjaminc3c88822016-11-14 10:32:04 +09001046static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1047 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04001048
1049 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1050 * known attack while we fix ChannelID itself. */
1051 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -05001052 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001053 ssl->s3->tlsext_channel_id_valid = 0;
1054 }
1055
1056 /* If this is a resumption and the original handshake didn't support
1057 * ChannelID then we didn't record the original handshake hashes in the
1058 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001059 if (ssl->session != NULL &&
1060 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001061 ssl->s3->tlsext_channel_id_valid = 0;
1062 }
1063
David Benjaminad8f5e12017-02-20 17:00:20 -05001064 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -04001065 ssl_get_current_time(ssl, &now);
1066 ssl->s3->server_random[0] = now.tv_sec >> 24;
1067 ssl->s3->server_random[1] = now.tv_sec >> 16;
1068 ssl->s3->server_random[2] = now.tv_sec >> 8;
1069 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001070 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001071 return -1;
1072 }
1073
David Benjamin55108632016-08-11 22:01:18 -04001074 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1075 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001076
David Benjamin45738dd2017-02-09 20:01:26 -05001077 const SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001078 if (ssl->session != NULL) {
1079 session = ssl->session;
1080 }
1081
David Benjamin75836432016-06-17 18:48:29 -04001082 CBB cbb, body, session_id;
1083 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1084 !CBB_add_u16(&body, ssl->version) ||
1085 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1086 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001087 !CBB_add_bytes(&session_id, session->session_id,
1088 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -05001089 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -04001090 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001091 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001092 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001093 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1094 CBB_cleanup(&cbb);
1095 return -1;
1096 }
1097
David Benjamin16315f72017-01-12 20:02:05 -05001098 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001099}
Adam Langley95c29f32014-06-20 12:00:00 -07001100
David Benjaminc3c88822016-11-14 10:32:04 +09001101static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1102 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001103 if (!ssl_has_certificate(ssl)) {
1104 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin16315f72017-01-12 20:02:05 -05001105 return -1;
David Benjamin32a66d52016-07-13 22:03:11 -04001106 }
1107
David Benjamin75836432016-06-17 18:48:29 -04001108 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin16315f72017-01-12 20:02:05 -05001109 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001110 }
David Benjamin16315f72017-01-12 20:02:05 -05001111 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001112}
1113
David Benjaminc3c88822016-11-14 10:32:04 +09001114static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1115 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001116 CBB cbb, body, ocsp_response;
1117 if (!ssl->method->init_message(ssl, &cbb, &body,
1118 SSL3_MT_CERTIFICATE_STATUS) ||
1119 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1120 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
David Benjamin83a32122017-02-14 18:34:54 -05001121 !CBB_add_bytes(&ocsp_response,
1122 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
1123 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001124 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001125 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1126 CBB_cleanup(&cbb);
1127 return -1;
1128 }
1129
David Benjamin16315f72017-01-12 20:02:05 -05001130 return 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001131}
1132
David Benjaminc3c88822016-11-14 10:32:04 +09001133static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1134 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -05001135 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001136 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001137
David Benjaminc42acee2016-06-17 17:47:58 -04001138 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001139 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin45738dd2017-02-09 20:01:26 -05001140 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1141 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001142
David Benjaminc42acee2016-06-17 17:47:58 -04001143 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1144 if (!CBB_init(&cbb, 128)) {
1145 goto err;
1146 }
1147
David Benjamin2a0b3912015-12-18 01:01:21 -05001148 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001150 size_t len =
1151 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1152 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1153 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1154 len)) {
1155 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 }
Adam Langley95c29f32014-06-20 12:00:00 -07001158
David Benjamin7061e282015-03-19 11:10:48 -04001159 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001160 /* Determine the group to use. */
1161 DH *params = ssl->cert->dh_tmp;
1162 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1163 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001164 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001165 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001166 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001167 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001168 goto err;
1169 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001170
David Benjamin974c7ba2015-12-19 16:58:04 -05001171 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001172 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001173 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001174 goto err;
1175 }
David Benjamin9f226a52015-04-26 22:12:32 -04001176
David Benjaminc3c88822016-11-14 10:32:04 +09001177 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001178 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001179 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001180 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001181 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001182 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001183 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001184 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001185 }
David Benjamin7061e282015-03-19 11:10:48 -04001186 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001187 /* Determine the group to use. */
1188 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001189 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001190 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001191 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 goto err;
1193 }
David Benjamin45738dd2017-02-09 20:01:26 -05001194 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001195
David Benjamin4298d772015-12-19 00:18:25 -05001196 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001197 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001198 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001199 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001200 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001201 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001202 goto err;
1203 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001204 } else {
1205 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001206 }
Adam Langley95c29f32014-06-20 12:00:00 -07001207
David Benjaminc3c88822016-11-14 10:32:04 +09001208 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001209 goto err;
1210 }
David Benjaminc42acee2016-06-17 17:47:58 -04001211 }
1212
1213 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001214 CBB body;
1215 if (!ssl->method->init_message(ssl, &cbb, &body,
1216 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001217 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001218 goto err;
1219 }
Adam Langley95c29f32014-06-20 12:00:00 -07001220
David Benjamin2a0b3912015-12-18 01:01:21 -05001221 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -05001222 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001223 if (!ssl_has_private_key(ssl)) {
1224 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001225 goto err;
1226 }
Adam Langley95c29f32014-06-20 12:00:00 -07001227
David Benjaminc42acee2016-06-17 17:47:58 -04001228 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001229 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001230 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001231 goto err;
1232 }
David Benjaminc42acee2016-06-17 17:47:58 -04001233 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001234 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001235 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1236 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1237 goto err;
1238 }
David Benjaminc42acee2016-06-17 17:47:58 -04001239 }
1240
1241 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001242 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001243 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001244 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001245 !CBB_reserve(&child, &ptr, max_sig_len)) {
1246 goto err;
1247 }
1248
David Benjamin2a0b3912015-12-18 01:01:21 -05001249 size_t sig_len;
1250 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001251 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001252 CBB transcript;
1253 uint8_t *transcript_data;
1254 size_t transcript_len;
1255 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001256 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1257 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1258 SSL3_RANDOM_SIZE) ||
1259 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1260 SSL3_RANDOM_SIZE) ||
1261 !CBB_add_bytes(&transcript, hs->server_params,
1262 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001263 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1264 CBB_cleanup(&transcript);
1265 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1266 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001267 goto err;
1268 }
1269
Steven Valdezf0451ca2016-06-29 13:16:27 -04001270 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001271 signature_algorithm, transcript_data,
1272 transcript_len);
1273 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001274 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001275 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001276 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001277 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001278
1279 switch (sign_result) {
1280 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001281 if (!CBB_did_write(&child, sig_len)) {
1282 goto err;
1283 }
1284 break;
1285 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001286 goto err;
1287 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001288 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001289 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001290 goto err;
1291 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001292 }
1293
David Benjamindaf207a2017-01-03 18:37:41 -05001294 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001295 goto err;
1296 }
David Benjaminc42acee2016-06-17 17:47:58 -04001297
David Benjaminc3c88822016-11-14 10:32:04 +09001298 OPENSSL_free(hs->server_params);
1299 hs->server_params = NULL;
1300 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001301
David Benjamin16315f72017-01-12 20:02:05 -05001302 return 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001303
Adam Langley95c29f32014-06-20 12:00:00 -07001304err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001305 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001306 return -1;
1307}
Adam Langley95c29f32014-06-20 12:00:00 -07001308
David Benjamin75836432016-06-17 18:48:29 -04001309static int add_cert_types(SSL *ssl, CBB *cbb) {
1310 /* Get configured signature algorithms. */
1311 int have_rsa_sign = 0;
1312 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001313 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001314 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1315 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001316 switch (sig_algs[i]) {
1317 case SSL_SIGN_RSA_PKCS1_SHA512:
1318 case SSL_SIGN_RSA_PKCS1_SHA384:
1319 case SSL_SIGN_RSA_PKCS1_SHA256:
1320 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001321 have_rsa_sign = 1;
1322 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001323
Steven Valdez02563852016-06-23 13:33:05 -04001324 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1325 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1326 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1327 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001328 have_ecdsa_sign = 1;
1329 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001331 }
Adam Langley95c29f32014-06-20 12:00:00 -07001332
David Benjamin75836432016-06-17 18:48:29 -04001333 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1334 return 0;
1335 }
1336
1337 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1338 * check for SSL_kECDH or SSL_kECDHE. */
1339 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1340 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
David Benjaminc3c88822016-11-14 10:32:04 +09001347static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1348 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001349 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001350 if (!ssl->method->init_message(ssl, &cbb, &body,
1351 SSL3_MT_CERTIFICATE_REQUEST) ||
1352 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1353 !add_cert_types(ssl, &cert_types)) {
1354 goto err;
1355 }
1356
1357 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001358 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001359 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001360 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001361 goto err;
1362 }
Steven Valdez02563852016-06-23 13:33:05 -04001363
David Benjamin0fc37ef2016-08-17 15:29:46 -04001364 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001365 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1366 goto err;
1367 }
1368 }
David Benjamin75836432016-06-17 18:48:29 -04001369 }
1370
David Benjamin32a66d52016-07-13 22:03:11 -04001371 if (!ssl_add_client_CA_list(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001372 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001373 goto err;
1374 }
1375
David Benjamin16315f72017-01-12 20:02:05 -05001376 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001377
Adam Langley95c29f32014-06-20 12:00:00 -07001378err:
David Benjamin75836432016-06-17 18:48:29 -04001379 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1380 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001381 return -1;
1382}
Adam Langley95c29f32014-06-20 12:00:00 -07001383
David Benjaminc3c88822016-11-14 10:32:04 +09001384static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1385 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001386 CBB cbb, body;
1387 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001388 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001389 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1390 CBB_cleanup(&cbb);
1391 return -1;
1392 }
1393
David Benjamin16315f72017-01-12 20:02:05 -05001394 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001395}
1396
David Benjaminc3c88822016-11-14 10:32:04 +09001397static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1398 SSL *const ssl = hs->ssl;
1399 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001400
David Benjaminf71036e2017-01-21 14:49:39 -05001401 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001402 if (msg_ret <= 0) {
1403 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001404 }
1405
1406 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1407 if (ssl->version == SSL3_VERSION &&
1408 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001409 /* In SSL 3.0, the Certificate message is omitted to signal no
1410 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001411 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001412 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001413 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1414 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001415 }
1416
Adam Langley37646832016-08-01 16:16:46 -07001417 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001418 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001419 hs->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001420 ssl->s3->tmp.reuse_message = 1;
1421 return 1;
1422 }
1423
David Benjamin9f1dc822016-06-07 17:03:46 -04001424 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001425 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1426 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001427 }
1428
Steven Valdez908ac192017-01-12 13:17:07 -05001429 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001430 return -1;
1431 }
1432
David Benjamin5c900c82016-07-13 23:03:26 -04001433 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001434 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001435
David Benjamin45738dd2017-02-09 20:01:26 -05001436 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001437 EVP_PKEY_free(hs->peer_pubkey);
1438 hs->peer_pubkey = NULL;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001439 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001440 hs->new_session->certs = ssl_parse_cert_chain(
1441 &alert, &hs->peer_pubkey,
1442 ssl->retain_only_sha256_of_client_certs ? hs->new_session->peer_sha256
1443 : NULL,
1444 &certificate_msg, ssl->ctx->pool);
1445 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001446 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001447 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001448 }
1449
Adam Langley68e71242016-12-12 11:06:16 -08001450 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001451 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001452 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001453 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001454 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001455 }
1456
David Benjamin45738dd2017-02-09 20:01:26 -05001457 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001458 /* No client certificate so the handshake buffer may be discarded. */
Steven Valdez908ac192017-01-12 13:17:07 -05001459 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin9f1dc822016-06-07 17:03:46 -04001460
David Benjamine455e512016-08-01 20:11:13 -04001461 /* In SSL 3.0, sending no certificate is signaled by omitting the
1462 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001463 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001464 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001465 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001466 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001467 }
1468
1469 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001470 /* Fail for TLS only if we required a certificate */
1471 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001472 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001473 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001474 }
Adam Langley37646832016-08-01 16:16:46 -07001475
1476 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001477 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001478 hs->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001479 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001480 }
1481
Adam Langley68e71242016-12-12 11:06:16 -08001482 /* The hash will have been filled in. */
1483 if (ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -05001484 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001485 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001486
Adam Langley2a3b3432017-02-10 13:56:48 -08001487 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
Adam Langley68e71242016-12-12 11:06:16 -08001488 return -1;
1489 }
Adam Langley2a3b3432017-02-10 13:56:48 -08001490
David Benjamin5c900c82016-07-13 23:03:26 -04001491 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001492}
1493
David Benjaminc3c88822016-11-14 10:32:04 +09001494static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1495 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001496 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001498 uint32_t alg_k;
1499 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001500 uint8_t *premaster_secret = NULL;
1501 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001502 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin4298d772015-12-19 00:18:25 -05001504 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001506
David Benjamincb0c29f2016-12-12 17:00:50 -05001507 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminf71036e2017-01-21 14:49:39 -05001508 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001509 if (ret <= 0) {
1510 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001511 }
David Benjamin276b7e82017-01-21 14:13:39 -05001512
David Benjaminf71036e2017-01-21 14:49:39 -05001513 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001514 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001515 return -1;
1516 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 }
Adam Langley95c29f32014-06-20 12:00:00 -07001518
David Benjamin0d56f882015-12-19 17:05:56 -05001519 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin45738dd2017-02-09 20:01:26 -05001520 alg_k = hs->new_cipher->algorithm_mkey;
1521 alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 /* If using a PSK key exchange, prepare the pre-shared key. */
1524 if (alg_a & SSL_aPSK) {
1525 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001526
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1528 * then this is the only field in the message. */
1529 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1530 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001531 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001532 al = SSL_AD_DECODE_ERROR;
1533 goto f_err;
1534 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001535
David Benjamin0d56f882015-12-19 17:05:56 -05001536 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001537 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001538 al = SSL_AD_INTERNAL_ERROR;
1539 goto f_err;
1540 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001541
Adam Langleyfcf25832014-12-18 17:42:32 -08001542 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1543 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001544 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 al = SSL_AD_ILLEGAL_PARAMETER;
1546 goto f_err;
1547 }
David Benjamin35c02302014-07-13 04:14:59 -04001548
David Benjamin45738dd2017-02-09 20:01:26 -05001549 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001550 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001551 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 goto f_err;
1553 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001554
Adam Langleyfcf25832014-12-18 17:42:32 -08001555 /* Look up the key for the identity. */
David Benjamin45738dd2017-02-09 20:01:26 -05001556 psk_len = ssl->psk_server_callback(ssl, hs->new_session->psk_identity, psk,
1557 sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001559 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 al = SSL_AD_INTERNAL_ERROR;
1561 goto f_err;
1562 } else if (psk_len == 0) {
1563 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001564 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001565 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1566 goto f_err;
1567 }
1568 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001569
Adam Langleyfcf25832014-12-18 17:42:32 -08001570 /* Depending on the key exchange method, compute |premaster_secret| and
1571 * |premaster_secret_len|. */
1572 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001573 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001574 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 decrypt_buf = OPENSSL_malloc(rsa_size);
1576 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001577 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001578 goto err;
1579 }
David Benjamin35c02302014-07-13 04:14:59 -04001580
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001581 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001582 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001583 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001584 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001585 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001586 al = SSL_AD_HANDSHAKE_FAILURE;
1587 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1588 goto f_err;
1589 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001590 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001591 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001592 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1593 &encrypted_premaster_secret) ||
1594 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001595 al = SSL_AD_DECODE_ERROR;
1596 OPENSSL_PUT_ERROR(SSL,
1597 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1598 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001599 }
1600 } else {
1601 encrypted_premaster_secret = client_key_exchange;
1602 }
1603
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001604 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1605 * timing-sensitive code below. */
1606 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001607 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001608 CBS_data(&encrypted_premaster_secret),
1609 CBS_len(&encrypted_premaster_secret));
1610 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001611 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001612 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001613 decrypt_result =
1614 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001615 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001616
1617 switch (decrypt_result) {
1618 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001619 break;
1620 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001621 goto err;
1622 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001623 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001624 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001625 goto err;
1626 }
1627
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001628 if (decrypt_len != rsa_size) {
1629 al = SSL_AD_DECRYPT_ERROR;
1630 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1631 goto f_err;
1632 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001633
David Benjamin3f5b43d2015-12-01 19:31:24 -05001634 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1635 * section 7.4.7.1. */
1636 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1637 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001639 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 goto err;
1641 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001642 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001643 goto err;
1644 }
1645
David Benjamin3f5b43d2015-12-01 19:31:24 -05001646 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1647 * publicly invalid. */
1648 if (decrypt_len < 11 + premaster_secret_len) {
1649 al = SSL_AD_DECRYPT_ERROR;
1650 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1651 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001652 }
David Benjamin35c02302014-07-13 04:14:59 -04001653
David Benjamin3f5b43d2015-12-01 19:31:24 -05001654 /* Check the padding. See RFC 3447, section 7.2.2. */
1655 size_t padding_len = decrypt_len - premaster_secret_len;
1656 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1657 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001658 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001659 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1660 }
1661 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1662
1663 /* The premaster secret must begin with |client_version|. This too must be
1664 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1665 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001666 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001667 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001668 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001669
1670 /* Select, in constant time, either the decrypted premaster or the random
1671 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001672 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001673 premaster_secret[i] = constant_time_select_8(
1674 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1675 }
1676
1677 OPENSSL_free(decrypt_buf);
1678 decrypt_buf = NULL;
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001679 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001680 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001681 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001682 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001683 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001684 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001685 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 goto f_err;
1687 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001688
David Benjamin4298d772015-12-19 00:18:25 -05001689 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001690 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001691 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001692 &premaster_secret_len, &alert, CBS_data(&peer_key),
1693 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001694 al = alert;
1695 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001696 }
Adam Langley95c29f32014-06-20 12:00:00 -07001697
David Benjamin4298d772015-12-19 00:18:25 -05001698 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001699 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001700 } else if (alg_k & SSL_kPSK) {
1701 /* For plain PSK, other_secret is a block of 0s with the same length as the
1702 * pre-shared key. */
1703 premaster_secret_len = psk_len;
1704 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1705 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001706 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001707 goto err;
1708 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001709 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001710 } else {
1711 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001712 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 goto f_err;
1714 }
Adam Langley95c29f32014-06-20 12:00:00 -07001715
Adam Langleyfcf25832014-12-18 17:42:32 -08001716 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1717 * pre-shared key. */
1718 if (alg_a & SSL_aPSK) {
1719 CBB new_premaster, child;
1720 uint8_t *new_data;
1721 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001722
David Benjamina8653202015-06-28 01:26:10 -04001723 CBB_zero(&new_premaster);
1724 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1725 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001726 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1727 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1728 !CBB_add_bytes(&child, psk, psk_len) ||
1729 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001730 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001731 CBB_cleanup(&new_premaster);
1732 goto err;
1733 }
David Benjamin14c83e72014-07-13 04:54:57 -04001734
Adam Langleyfcf25832014-12-18 17:42:32 -08001735 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1736 OPENSSL_free(premaster_secret);
1737 premaster_secret = new_data;
1738 premaster_secret_len = new_len;
1739 }
David Benjamin14c83e72014-07-13 04:54:57 -04001740
Adam Langleyfcf25832014-12-18 17:42:32 -08001741 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001742 hs->new_session->master_key_length = tls1_generate_master_secret(
1743 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1744 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001745 goto err;
1746 }
David Benjaminfc02b592017-02-17 16:26:01 -05001747 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001748
Adam Langleyfcf25832014-12-18 17:42:32 -08001749 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1750 OPENSSL_free(premaster_secret);
1751 return 1;
1752
Adam Langley95c29f32014-06-20 12:00:00 -07001753f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001754 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001755err:
David Benjamin4298d772015-12-19 00:18:25 -05001756 if (premaster_secret != NULL) {
1757 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 OPENSSL_free(premaster_secret);
1759 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001760 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001761
Adam Langleyfcf25832014-12-18 17:42:32 -08001762 return -1;
1763}
Adam Langley95c29f32014-06-20 12:00:00 -07001764
David Benjaminc3c88822016-11-14 10:32:04 +09001765static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1766 SSL *const ssl = hs->ssl;
Adam Langleyd5157222016-12-12 11:37:43 -08001767 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001769
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 /* Only RSA and ECDSA client certificates are supported, so a
1771 * CertificateVerify is required if and only if there's a client certificate.
1772 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001773 if (hs->peer_pubkey == NULL) {
Steven Valdez908ac192017-01-12 13:17:07 -05001774 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001775 return 1;
1776 }
Adam Langley95c29f32014-06-20 12:00:00 -07001777
David Benjaminf71036e2017-01-21 14:49:39 -05001778 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001779 if (msg_ret <= 0) {
1780 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001781 }
David Benjaminef865502014-08-24 02:48:34 -04001782
David Benjamin276b7e82017-01-21 14:13:39 -05001783 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1784 return -1;
1785 }
1786
David Benjamin09eb6552016-07-08 14:32:11 -07001787 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001788
Adam Langleyfcf25832014-12-18 17:42:32 -08001789 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001790 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001791 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001792 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001793 al = SSL_AD_DECODE_ERROR;
1794 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1795 goto f_err;
1796 }
David Benjamin887c3002016-07-08 16:15:32 -07001797 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001798 goto f_err;
1799 }
David Benjamin45738dd2017-02-09 20:01:26 -05001800 hs->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001801 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001802 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001803 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001804 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001805 } else {
1806 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1807 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1808 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 }
David Benjamin854dd652014-08-26 00:32:30 -04001810
Adam Langleyfcf25832014-12-18 17:42:32 -08001811 /* Parse and verify the signature. */
1812 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1813 CBS_len(&certificate_verify) != 0) {
1814 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001815 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 goto f_err;
1817 }
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Steven Valdez2b8415e2016-06-30 13:27:23 -04001819 int sig_ok;
1820 /* The SSL3 construction for CertificateVerify does not decompose into a
1821 * single final digest and signature, and must be special-cased. */
1822 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001823 uint8_t digest[EVP_MAX_MD_SIZE];
1824 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05001825 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
David Benjamin45738dd2017-02-09 20:01:26 -05001826 &digest_len, hs->new_session,
Steven Valdez908ac192017-01-12 13:17:07 -05001827 signature_algorithm)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001828 goto err;
1829 }
1830
Adam Langleyd5157222016-12-12 11:37:43 -08001831 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001832 sig_ok = pctx != NULL &&
1833 EVP_PKEY_verify_init(pctx) &&
Steven Valdez2b8415e2016-06-30 13:27:23 -04001834 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1835 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001836 EVP_PKEY_CTX_free(pctx);
1837 } else {
1838 sig_ok = ssl_public_key_verify(
1839 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001840 hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data,
1841 hs->transcript.buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001842 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001843
David Benjaminbf82aed2016-03-01 22:57:40 -05001844#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1845 sig_ok = 1;
1846 ERR_clear_error();
1847#endif
1848 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001849 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001850 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001851 goto f_err;
1852 }
1853
Steven Valdez2b8415e2016-06-30 13:27:23 -04001854 /* The handshake buffer is no longer necessary, and we may hash the current
1855 * message.*/
Steven Valdez908ac192017-01-12 13:17:07 -05001856 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
1857 if (!ssl_hash_current_message(hs)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001858 goto err;
1859 }
1860
Adam Langleyd5157222016-12-12 11:37:43 -08001861 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001862
Adam Langleyd5157222016-12-12 11:37:43 -08001863f_err:
1864 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin854dd652014-08-26 00:32:30 -04001865err:
Adam Langleyd5157222016-12-12 11:37:43 -08001866 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001867}
Adam Langley95c29f32014-06-20 12:00:00 -07001868
Adam Langley95c29f32014-06-20 12:00:00 -07001869/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1870 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001871static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1872 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001873 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001874 if (ret <= 0) {
1875 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001876 }
Adam Langley95c29f32014-06-20 12:00:00 -07001877
David Benjaminf71036e2017-01-21 14:49:39 -05001878 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001879 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001880 return -1;
1881 }
1882
David Benjamin09eb6552016-07-08 14:32:11 -07001883 CBS next_protocol, selected_protocol, padding;
1884 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001885 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1886 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001887 CBS_len(&next_protocol) != 0) {
1888 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1889 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1890 return 0;
1891 }
1892
1893 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001894 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 return 0;
1896 }
Adam Langley95c29f32014-06-20 12:00:00 -07001897
Adam Langleyfcf25832014-12-18 17:42:32 -08001898 return 1;
1899}
Adam Langley95c29f32014-06-20 12:00:00 -07001900
Adam Langley1258b6a2014-06-20 12:00:00 -07001901/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001902static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1903 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001904 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001905 if (msg_ret <= 0) {
1906 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001907 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001908
David Benjamin276b7e82017-01-21 14:13:39 -05001909 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001910 !tls1_verify_channel_id(hs) ||
1911 !ssl_hash_current_message(hs)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001912 return -1;
1913 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001914 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001915}
David Benjamin9f1dc822016-06-07 17:03:46 -04001916
David Benjaminc3c88822016-11-14 10:32:04 +09001917static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1918 SSL *const ssl = hs->ssl;
David Benjamin123db572016-11-03 16:59:25 -04001919 const SSL_SESSION *session;
1920 SSL_SESSION *session_copy = NULL;
1921 if (ssl->session == NULL) {
1922 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin45738dd2017-02-09 20:01:26 -05001923 ssl_session_rebase_time(ssl, hs->new_session);
1924 session = hs->new_session;
David Benjamin123db572016-11-03 16:59:25 -04001925 } else {
1926 /* We are renewing an existing session. Duplicate the session to adjust the
1927 * timeout. */
1928 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1929 if (session_copy == NULL) {
1930 return -1;
1931 }
1932
David Benjamin17b30832017-01-28 14:00:32 -05001933 ssl_session_rebase_time(ssl, session_copy);
David Benjamin123db572016-11-03 16:59:25 -04001934 session = session_copy;
1935 }
1936
David Benjamin75836432016-06-17 18:48:29 -04001937 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001938 int ok =
1939 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1940 CBB_add_u32(&body, session->timeout) &&
1941 CBB_add_u16_length_prefixed(&body, &ticket) &&
1942 ssl_encrypt_ticket(ssl, &ticket, session) &&
David Benjamindaf207a2017-01-03 18:37:41 -05001943 ssl_add_message_cbb(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001944
1945 SSL_SESSION_free(session_copy);
1946 CBB_cleanup(&cbb);
1947
1948 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001949 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001950 }
1951
David Benjamin16315f72017-01-12 20:02:05 -05001952 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001953}