blob: 8025030256e1a0bb26a508549c53a46499301861 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 *
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115 *
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
118 *
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121 *
122 */
123/* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
125 *
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128 * license.
129 *
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
133 *
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
137 *
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
142 *
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147 * OTHERWISE. */
148
David Benjamin9e4e01e2015-09-15 01:48:04 -0400149#include <openssl/ssl.h>
150
David Benjamin880b14e2014-08-25 22:35:07 -0400151#include <assert.h>
David Benjamin22f9bcc2014-07-13 12:29:21 -0400152#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700153
154#include <openssl/bn.h>
155#include <openssl/buf.h>
David Benjamindc72ff72014-06-25 12:36:10 -0400156#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/cipher.h>
158#include <openssl/dh.h>
Adam Langley1258b6a2014-06-20 12:00:00 -0700159#include <openssl/ec.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
163#include <openssl/hmac.h>
164#include <openssl/md5.h>
165#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400166#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/rand.h>
168#include <openssl/x509.h>
169
David Benjamin2ee94aa2015-04-07 22:38:30 -0400170#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800171#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173
David Benjaminc3c88822016-11-14 10:32:04 +0900174static int ssl3_get_client_hello(SSL_HANDSHAKE *hs);
175static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
176static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
178static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
179static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
180static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
181static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
183static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
184static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
185static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
186static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400187
Steven Valdez258508f2017-01-25 15:47:49 -0500188static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
189 CRYPTO_STATIC_MUTEX_INIT;
190static uint64_t g_v2clienthello_count = 0;
191
192uint64_t SSL_get_v2clienthello_count(void) {
193 CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
194 uint64_t ret = g_v2clienthello_count;
195 CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
196 return ret;
197}
198
David Benjamince8c9d22016-11-14 10:45:16 +0900199int ssl3_accept(SSL_HANDSHAKE *hs) {
200 SSL *const ssl = hs->ssl;
David Benjamin3fa27772015-04-17 22:32:19 -0400201 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800202 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400203 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700204
David Benjamin0d56f882015-12-19 17:05:56 -0500205 assert(ssl->handshake_func == ssl3_accept);
206 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500207
Adam Langleyfcf25832014-12-18 17:42:32 -0800208 for (;;) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500209 state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamincb0c29f2016-12-12 17:00:50 -0500211 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400212 case SSL_ST_INIT:
David Benjamincb0c29f2016-12-12 17:00:50 -0500213 hs->state = SSL_ST_ACCEPT;
David Benjaminfc0c9d92016-09-01 01:33:33 -0400214 skip = 1;
215 break;
216
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 case SSL_ST_ACCEPT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400218 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjamin0d56f882015-12-19 17:05:56 -0500220 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400221 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 ret = -1;
223 goto end;
224 }
David Benjamin4b755cb2014-12-12 03:58:07 -0500225
David Benjamincb0c29f2016-12-12 17:00:50 -0500226 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 case SSL3_ST_SR_CLNT_HELLO_A:
230 case SSL3_ST_SR_CLNT_HELLO_B:
231 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin25fe85b2016-08-09 20:00:32 -0400232 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin4eb95cc2016-11-16 17:08:23 +0900233 case SSL3_ST_SR_CLNT_HELLO_E:
David Benjaminc3c88822016-11-14 10:32:04 +0900234 ret = ssl3_get_client_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500235 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400236 break;
237 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 if (ret <= 0) {
239 goto end;
240 }
David Benjamindf50eec2016-06-07 16:49:42 -0400241 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500242 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900246 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 if (ret <= 0) {
248 goto end;
249 }
Steven Valdez87eab492016-06-27 16:34:59 -0400250 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500251 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500253 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 case SSL3_ST_SW_CERT_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000258 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900259 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 if (ret <= 0) {
261 goto end;
262 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 } else {
264 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500266 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100269 case SSL3_ST_SW_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900270 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900271 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400272 if (ret <= 0) {
273 goto end;
274 }
275 } else {
276 skip = 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100277 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500278 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100279 break;
280
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 case SSL3_ST_SW_KEY_EXCH_A:
282 case SSL3_ST_SW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500283 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamindf50eec2016-06-07 16:49:42 -0400285 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin0d56f882015-12-19 17:05:56 -0500286 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
287 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900288 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500289 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500291 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 } else {
293 skip = 1;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
David Benjamincb0c29f2016-12-12 17:00:50 -0500296 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700298
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 case SSL3_ST_SW_CERT_REQ_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900300 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900301 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 if (ret <= 0) {
303 goto end;
304 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400305 } else {
306 skip = 1;
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 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400369 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500371 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700373
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400375 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900376 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400377 if (ret <= 0) {
378 goto end;
379 }
380 } else {
381 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500383 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900387 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 if (ret <= 0) {
389 goto end;
390 }
Adam Langley95c29f32014-06-20 12:00:00 -0700391
David Benjamindf50eec2016-06-07 16:49:42 -0400392 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400393 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500394 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500396 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 }
David Benjamindf50eec2016-06-07 16:49:42 -0400398
Steven Valdez87eab492016-06-27 16:34:59 -0400399 /* If this is a full handshake with ChannelID then record the handshake
400 * hashes in |ssl->s3->new_session| in case we need them to verify a
401 * ChannelID signature on a resumption of this session in the future. */
402 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
David Benjamin0d56f882015-12-19 17:05:56 -0500403 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 if (ret <= 0) {
405 goto end;
406 }
407 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 case SSL3_ST_SW_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900411 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900412 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400413 if (ret <= 0) {
414 goto end;
415 }
416 } else {
417 skip = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500419 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700421
David Benjamin352d0a92016-06-28 11:22:02 -0400422 case SSL3_ST_SW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500423 if (!ssl->method->add_change_cipher_spec(ssl) ||
424 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 ret = -1;
426 goto end;
427 }
David Benjamindaf207a2017-01-03 18:37:41 -0500428
429 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 case SSL3_ST_SW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500433 ret = ssl3_send_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 if (ret <= 0) {
435 goto end;
436 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500437 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400438 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500439 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500441 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
David Benjamin9f1dc822016-06-07 17:03:46 -0400445 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500446 ret = ssl->method->flush_flight(ssl);
447 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400448 goto end;
449 }
450
David Benjamincb0c29f2016-12-12 17:00:50 -0500451 hs->state = hs->next_state;
452 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400453 ssl->method->expect_flight(ssl);
454 }
455 break;
456
Steven Valdez143e8b32016-07-11 13:19:03 -0400457 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900458 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400459 if (ret <= 0) {
460 goto end;
461 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500462 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400463 break;
464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400466 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Steven Valdez87eab492016-06-27 16:34:59 -0400468 /* If we aren't retaining peer certificates then we can discard it
469 * now. */
470 if (ssl->s3->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900471 ssl->retain_only_sha256_of_client_certs) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800472 X509_free(ssl->s3->new_session->x509_peer);
473 ssl->s3->new_session->x509_peer = NULL;
474 sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free);
475 ssl->s3->new_session->x509_chain = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400476 }
477
478 SSL_SESSION_free(ssl->s3->established_session);
479 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400480 SSL_SESSION_up_ref(ssl->session);
481 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400482 } else {
483 ssl->s3->established_session = ssl->s3->new_session;
484 ssl->s3->established_session->not_resumable = 0;
485 ssl->s3->new_session = NULL;
486 }
487
Steven Valdez258508f2017-01-25 15:47:49 -0500488 if (hs->v2_clienthello) {
489 CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock);
490 g_v2clienthello_count++;
491 CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock);
492 }
493
David Benjamin78476f62016-11-12 11:20:55 +0900494 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900495 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900496
David Benjamin4e9cc712016-06-01 20:16:03 -0400497 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 ret = 1;
499 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 default:
David Benjamin3570d732015-06-29 00:28:17 -0400502 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 ret = -1;
504 goto end;
505 }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
David Benjamincb0c29f2016-12-12 17:00:50 -0500507 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
508 int new_state = hs->state;
509 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400510 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500511 hs->state = new_state;
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 }
513 skip = 0;
514 }
515
Adam Langley95c29f32014-06-20 12:00:00 -0700516end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400517 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return ret;
519}
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin731058e2016-12-03 23:15:13 -0500521int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
522 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400523 CBS cipher_suites;
524 CBS_init(&cipher_suites, client_hello->cipher_suites,
525 client_hello->cipher_suites_len);
526
527 while (CBS_len(&cipher_suites) > 0) {
528 uint16_t got_id;
529 if (!CBS_get_u16(&cipher_suites, &got_id)) {
530 return 0;
531 }
532
533 if (got_id == id) {
534 return 1;
535 }
536 }
537
538 return 0;
539}
540
David Benjaminf04c2e92016-12-06 13:35:25 -0500541static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500542 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500543 SSL *const ssl = hs->ssl;
David Benjamin25fe85b2016-08-09 20:00:32 -0400544 uint16_t min_version, max_version;
545 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
546 *out_alert = SSL_AD_PROTOCOL_VERSION;
547 return 0;
548 }
549
David Benjamin2dc02042016-09-19 19:57:37 -0400550 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400551 /* Check supported_versions extension if it is present. */
552 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500553 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
554 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400555 CBS versions;
556 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
557 CBS_len(&supported_versions) != 0 ||
558 CBS_len(&versions) == 0) {
559 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
560 *out_alert = SSL_AD_DECODE_ERROR;
561 return 0;
562 }
563
Brian Smithf85d3232016-10-28 10:34:06 -1000564 /* Choose the newest commonly-supported version advertised by the client.
565 * The client orders the versions according to its preferences, but we're
566 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400567 int found_version = 0;
568 while (CBS_len(&versions) != 0) {
569 uint16_t ext_version;
570 if (!CBS_get_u16(&versions, &ext_version)) {
571 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
572 *out_alert = SSL_AD_DECODE_ERROR;
573 return 0;
574 }
575 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
576 continue;
577 }
578 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000579 ext_version <= max_version &&
580 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400581 version = ext_version;
582 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400583 }
584 }
585
586 if (!found_version) {
587 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400588 }
589 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400590 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
591 * do not use this mechanism. */
592 if (SSL_is_dtls(ssl)) {
593 if (client_hello->version <= DTLS1_2_VERSION) {
594 version = TLS1_2_VERSION;
595 } else if (client_hello->version <= DTLS1_VERSION) {
596 version = TLS1_1_VERSION;
597 } else {
598 goto unsupported_protocol;
599 }
600 } else {
601 if (client_hello->version >= TLS1_2_VERSION) {
602 version = TLS1_2_VERSION;
603 } else if (client_hello->version >= TLS1_1_VERSION) {
604 version = TLS1_1_VERSION;
605 } else if (client_hello->version >= TLS1_VERSION) {
606 version = TLS1_VERSION;
607 } else if (client_hello->version >= SSL3_VERSION) {
608 version = SSL3_VERSION;
609 } else {
610 goto unsupported_protocol;
611 }
David Benjamin2dc02042016-09-19 19:57:37 -0400612 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400613
Steven Valdezfdd10992016-09-15 16:27:05 -0400614 /* Apply our minimum and maximum version. */
615 if (version > max_version) {
616 version = max_version;
617 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400618
Steven Valdezfdd10992016-09-15 16:27:05 -0400619 if (version < min_version) {
620 goto unsupported_protocol;
621 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400622 }
623
624 /* Handle FALLBACK_SCSV. */
625 if (ssl_client_cipher_list_contains_cipher(client_hello,
626 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
627 version < max_version) {
628 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
629 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
630 return 0;
631 }
632
David Benjaminf04c2e92016-12-06 13:35:25 -0500633 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400634 ssl->version = ssl->method->version_to_wire(version);
635 ssl->s3->enc_method = ssl3_get_enc_method(version);
636 assert(ssl->s3->enc_method != NULL);
637
638 /* At this point, the connection's version is known and |ssl->version| is
639 * fixed. Begin enforcing the record-layer version. */
640 ssl->s3->have_version = 1;
641
642 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400643
644unsupported_protocol:
645 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
646 *out_alert = SSL_AD_PROTOCOL_VERSION;
647 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400648}
649
David Benjamin2578b292016-12-03 23:19:55 -0500650static STACK_OF(SSL_CIPHER) *
651 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
652 CBS cipher_suites;
653 CBS_init(&cipher_suites, client_hello->cipher_suites,
654 client_hello->cipher_suites_len);
655
656 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
657 if (sk == NULL) {
658 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
659 goto err;
660 }
661
662 while (CBS_len(&cipher_suites) > 0) {
663 uint16_t cipher_suite;
664
665 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
666 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
667 goto err;
668 }
669
670 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
671 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
672 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
673 goto err;
674 }
675 }
676
677 return sk;
678
679err:
680 sk_SSL_CIPHER_free(sk);
681 return NULL;
682}
683
684/* ssl_get_compatible_server_ciphers determines the key exchange and
685 * authentication cipher suite masks compatible with the server configuration
686 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
687 * exchange mask and |*out_mask_a| to the authentication mask. */
688static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
689 uint32_t *out_mask_k,
690 uint32_t *out_mask_a) {
691 SSL *const ssl = hs->ssl;
692 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
693 *out_mask_k = SSL_kGENERIC;
694 *out_mask_a = SSL_aGENERIC;
695 return;
696 }
697
698 uint32_t mask_k = 0;
699 uint32_t mask_a = 0;
700
Adam Langley3a2b47a2017-01-24 13:59:42 -0800701 if (ssl_has_certificate(ssl)) {
David Benjamin2578b292016-12-03 23:19:55 -0500702 int type = ssl_private_key_type(ssl);
703 if (type == NID_rsaEncryption) {
704 mask_k |= SSL_kRSA;
705 mask_a |= SSL_aRSA;
706 } else if (ssl_is_ecdsa_key_type(type)) {
707 mask_a |= SSL_aECDSA;
708 }
709 }
710
711 if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) {
712 mask_k |= SSL_kDHE;
713 }
714
715 /* Check for a shared group to consider ECDHE ciphers. */
716 uint16_t unused;
717 if (tls1_get_shared_group(hs, &unused)) {
718 mask_k |= SSL_kECDHE;
719 }
720
David Benjamin2578b292016-12-03 23:19:55 -0500721 /* PSK requires a server callback. */
722 if (ssl->psk_server_callback != NULL) {
723 mask_k |= SSL_kPSK;
724 mask_a |= SSL_aPSK;
725 }
726
727 *out_mask_k = mask_k;
728 *out_mask_a = mask_a;
729}
730
731static const SSL_CIPHER *ssl3_choose_cipher(
732 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
733 const struct ssl_cipher_preference_list_st *server_pref) {
734 SSL *const ssl = hs->ssl;
735 const SSL_CIPHER *c, *ret = NULL;
736 STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow;
737 int ok;
738 size_t cipher_index;
739 uint32_t alg_k, alg_a, mask_k, mask_a;
740 /* in_group_flags will either be NULL, or will point to an array of bytes
741 * which indicate equal-preference groups in the |prio| stack. See the
742 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
743 * struct. */
744 const uint8_t *in_group_flags;
745 /* group_min contains the minimal index so far found in a group, or -1 if no
746 * such value exists yet. */
747 int group_min = -1;
748
749 STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello);
750 if (clnt == NULL) {
751 return NULL;
752 }
753
754 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
755 prio = srvr;
756 in_group_flags = server_pref->in_group_flags;
757 allow = clnt;
758 } else {
759 prio = clnt;
760 in_group_flags = NULL;
761 allow = srvr;
762 }
763
764 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
765
766 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
767 c = sk_SSL_CIPHER_value(prio, i);
768
769 ok = 1;
770
771 /* Check the TLS version. */
772 if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
773 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
774 ok = 0;
775 }
776
777 alg_k = c->algorithm_mkey;
778 alg_a = c->algorithm_auth;
779
780 ok = ok && (alg_k & mask_k) && (alg_a & mask_a);
781
782 if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
783 if (in_group_flags != NULL && in_group_flags[i] == 1) {
784 /* This element of |prio| is in a group. Update the minimum index found
785 * so far and continue looking. */
786 if (group_min == -1 || (size_t)group_min > cipher_index) {
787 group_min = cipher_index;
788 }
789 } else {
790 if (group_min != -1 && (size_t)group_min < cipher_index) {
791 cipher_index = group_min;
792 }
793 ret = sk_SSL_CIPHER_value(allow, cipher_index);
794 break;
795 }
796 }
797
798 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
799 /* We are about to leave a group, but we found a match in it, so that's
800 * our answer. */
801 ret = sk_SSL_CIPHER_value(allow, group_min);
802 break;
803 }
804 }
805
806 sk_SSL_CIPHER_free(clnt);
807 return ret;
808}
809
David Benjaminc3c88822016-11-14 10:32:04 +0900810static int ssl3_get_client_hello(SSL_HANDSHAKE *hs) {
811 SSL *const ssl = hs->ssl;
David Benjamin9ef31f02016-10-31 18:01:13 -0400812 uint8_t al = SSL_AD_INTERNAL_ERROR;
813 int ret = -1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400814 SSL_SESSION *session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700815
David Benjamincb0c29f2016-12-12 17:00:50 -0500816 if (hs->state == SSL3_ST_SR_CLNT_HELLO_A) {
David Benjamine14ff062016-08-09 16:21:24 -0400817 /* The first time around, read the ClientHello. */
David Benjaminf71036e2017-01-21 14:49:39 -0500818 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamine14ff062016-08-09 16:21:24 -0400819 if (msg_ret <= 0) {
820 return msg_ret;
David Benjamin09eb6552016-07-08 14:32:11 -0700821 }
David Benjamin22f9bcc2014-07-13 12:29:21 -0400822
David Benjamin276b7e82017-01-21 14:13:39 -0500823 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
824 return -1;
825 }
826
David Benjamincb0c29f2016-12-12 17:00:50 -0500827 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 }
Adam Langley95c29f32014-06-20 12:00:00 -0700829
David Benjamin731058e2016-12-03 23:15:13 -0500830 SSL_CLIENT_HELLO client_hello;
831 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
832 ssl->init_num)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 goto f_err;
836 }
Adam Langley95c29f32014-06-20 12:00:00 -0700837
David Benjamincb0c29f2016-12-12 17:00:50 -0500838 if (hs->state == SSL3_ST_SR_CLNT_HELLO_B) {
David Benjamine14ff062016-08-09 16:21:24 -0400839 /* Run the early callback. */
840 if (ssl->ctx->select_certificate_cb != NULL) {
841 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
842 case 0:
843 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
844 goto err;
845
846 case -1:
847 /* Connection rejected. */
David Benjamin2c66e072016-09-16 15:58:00 -0400848 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamine14ff062016-08-09 16:21:24 -0400849 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
850 goto f_err;
851
852 default:
853 /* fallthrough */;
854 }
855 }
Alessandro Ghedini958346a2016-12-20 19:42:15 +0000856 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjamine14ff062016-08-09 16:21:24 -0400857 }
858
David Benjamin25fe85b2016-08-09 20:00:32 -0400859 /* Negotiate the protocol version if we have not done so yet. */
David Benjamin0d56f882015-12-19 17:05:56 -0500860 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500861 if (!negotiate_version(hs, &al, &client_hello)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 goto f_err;
863 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400864
David Benjamin25fe85b2016-08-09 20:00:32 -0400865 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500866 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900867 hs->do_tls13_handshake = tls13_server_handshake;
David Benjamin25fe85b2016-08-09 20:00:32 -0400868 return 1;
869 }
870 }
871
David Benjamincb0c29f2016-12-12 17:00:50 -0500872 if (hs->state == SSL3_ST_SR_CLNT_HELLO_C) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400873 /* Load the client random. */
874 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
875 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
876 return -1;
877 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500878 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
879 client_hello.random_len);
David Benjamin25fe85b2016-08-09 20:00:32 -0400880
David Benjamin4eb95cc2016-11-16 17:08:23 +0900881 /* Only null compression is supported. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500882 if (OPENSSL_memchr(client_hello.compression_methods, 0,
883 client_hello.compression_methods_len) == NULL) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900884 al = SSL_AD_ILLEGAL_PARAMETER;
885 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
886 goto f_err;
887 }
888
889 /* TLS extensions. */
David Benjamin8c880a22016-12-03 02:20:34 -0500890 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900891 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
892 goto err;
893 }
894
David Benjamincb0c29f2016-12-12 17:00:50 -0500895 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900896 }
897
David Benjamincb0c29f2016-12-12 17:00:50 -0500898 if (hs->state == SSL3_ST_SR_CLNT_HELLO_D) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400899 /* Call |cert_cb| to update server certificates if required. */
900 if (ssl->cert->cert_cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500901 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 if (rv == 0) {
903 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400904 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800905 goto f_err;
906 }
907 if (rv < 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500908 ssl->rwstate = SSL_X509_LOOKUP;
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 goto err;
910 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800911 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800912
David Benjamin650aa1c2016-12-20 18:55:16 -0500913 if (!ssl_auto_chain_if_needed(ssl)) {
914 goto err;
915 }
916
David Benjaminf01f42a2016-11-16 19:05:33 +0900917 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
918 * certificate is finalized. */
919 ssl->s3->tmp.new_cipher =
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900920 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjaminf01f42a2016-11-16 19:05:33 +0900921 if (ssl->s3->tmp.new_cipher == NULL) {
922 al = SSL_AD_HANDSHAKE_FAILURE;
923 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
924 goto f_err;
925 }
926
David Benjamincb0c29f2016-12-12 17:00:50 -0500927 hs->state = SSL3_ST_SR_CLNT_HELLO_E;
David Benjamin34202b92016-11-16 19:07:53 +0900928 }
929
David Benjamincb0c29f2016-12-12 17:00:50 -0500930 assert(hs->state == SSL3_ST_SR_CLNT_HELLO_E);
David Benjamin34202b92016-11-16 19:07:53 +0900931
932 /* Determine whether we are doing session resumption. */
933 int tickets_supported = 0, renew_ticket = 0;
934 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
935 &client_hello)) {
936 case ssl_session_success:
937 break;
938 case ssl_session_error:
939 goto err;
940 case ssl_session_retry:
941 ssl->rwstate = SSL_PENDING_SESSION;
942 goto err;
943 }
944
945 if (session != NULL) {
946 if (session->extended_master_secret &&
947 !ssl->s3->tmp.extended_master_secret) {
948 /* A ClientHello without EMS that attempts to resume a session with EMS
949 * is fatal to the connection. */
950 al = SSL_AD_HANDSHAKE_FAILURE;
951 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
952 goto f_err;
953 }
954
955 if (!ssl_session_is_resumable(ssl, session) ||
956 /* If the client offers the EMS extension, but the previous session
957 * didn't use it, then negotiate a new session. */
958 ssl->s3->tmp.extended_master_secret !=
959 session->extended_master_secret) {
960 SSL_SESSION_free(session);
961 session = NULL;
962 }
963 }
964
965 if (session != NULL) {
966 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900967 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900968 ssl->session = session;
969 session = NULL;
970 ssl->s3->session_reused = 1;
971 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900972 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900973 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900974 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900975 goto err;
976 }
977
978 /* Clear the session ID if we want the session to be single-use. */
979 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
980 ssl->s3->new_session->session_id_length = 0;
981 }
982 }
983
984 if (ssl->ctx->dos_protection_cb != NULL &&
985 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
986 /* Connection rejected for DOS reasons. */
987 al = SSL_AD_INTERNAL_ERROR;
988 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
989 goto f_err;
990 }
991
David Benjaminf01f42a2016-11-16 19:05:33 +0900992 if (ssl->session == NULL) {
993 ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400994
David Benjamin4eb95cc2016-11-16 17:08:23 +0900995 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900996 if (hs->hostname != NULL) {
997 ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
David Benjamin4eb95cc2016-11-16 17:08:23 +0900998 if (ssl->s3->new_session->tlsext_hostname == NULL) {
999 al = SSL_AD_INTERNAL_ERROR;
1000 goto f_err;
1001 }
1002 }
1003
David Benjamin5c1ce292015-05-26 16:59:43 -04001004 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +09001005 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -04001006 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -05001007 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
1008 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +09001009 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001010 }
David Benjaminc032dfa2016-05-12 14:54:57 -04001011 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001012 if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +09001013 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001014 }
Adam Langley37646832016-08-01 16:16:46 -07001015
David Benjaminc3c88822016-11-14 10:32:04 +09001016 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001017 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001018 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001019 ssl->s3->new_session->verify_result = X509_V_OK;
1020 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001021 }
1022
David Benjaminf01f42a2016-11-16 19:05:33 +09001023 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1024 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001025 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001026 goto f_err;
1027 }
1028
David Benjaminf71036e2017-01-21 14:49:39 -05001029 /* Now that all parameters are known, initialize the handshake hash and hash
1030 * the ClientHello. */
1031 if (!ssl3_init_handshake_hash(ssl) ||
1032 !ssl_hash_current_message(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001033 goto f_err;
1034 }
1035
Steven Valdez2b8415e2016-06-30 13:27:23 -04001036 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001037 if (!hs->cert_request) {
David Benjamin0d56f882015-12-19 17:05:56 -05001038 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 }
1040
David Benjamin0bd71eb2015-12-01 14:14:40 -05001041 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001042
1043 if (0) {
1044 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001045 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 }
1047
Adam Langley95c29f32014-06-20 12:00:00 -07001048err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001049 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 return ret;
1051}
Adam Langley95c29f32014-06-20 12:00:00 -07001052
David Benjaminc3c88822016-11-14 10:32:04 +09001053static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1054 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04001055
1056 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1057 * known attack while we fix ChannelID itself. */
1058 if (ssl->s3->tlsext_channel_id_valid &&
1059 (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
1060 ssl->s3->tlsext_channel_id_valid = 0;
1061 }
1062
1063 /* If this is a resumption and the original handshake didn't support
1064 * ChannelID then we didn't record the original handshake hashes in the
1065 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001066 if (ssl->session != NULL &&
1067 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001068 ssl->s3->tlsext_channel_id_valid = 0;
1069 }
1070
David Benjamin721e8b72016-08-03 13:13:17 -04001071 struct timeval now;
1072 ssl_get_current_time(ssl, &now);
1073 ssl->s3->server_random[0] = now.tv_sec >> 24;
1074 ssl->s3->server_random[1] = now.tv_sec >> 16;
1075 ssl->s3->server_random[2] = now.tv_sec >> 8;
1076 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001077 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001078 return -1;
1079 }
1080
David Benjamin55108632016-08-11 22:01:18 -04001081 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1082 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001083
Steven Valdez87eab492016-06-27 16:34:59 -04001084 const SSL_SESSION *session = ssl->s3->new_session;
1085 if (ssl->session != NULL) {
1086 session = ssl->session;
1087 }
1088
David Benjamin75836432016-06-17 18:48:29 -04001089 CBB cbb, body, session_id;
1090 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1091 !CBB_add_u16(&body, ssl->version) ||
1092 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1093 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001094 !CBB_add_bytes(&session_id, session->session_id,
1095 session->session_id_length) ||
David Benjamin75836432016-06-17 18:48:29 -04001096 !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
1097 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001098 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001099 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001100 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1101 CBB_cleanup(&cbb);
1102 return -1;
1103 }
1104
David Benjamin16315f72017-01-12 20:02:05 -05001105 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001106}
Adam Langley95c29f32014-06-20 12:00:00 -07001107
David Benjaminc3c88822016-11-14 10:32:04 +09001108static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1109 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001110 if (!ssl_has_certificate(ssl)) {
1111 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin16315f72017-01-12 20:02:05 -05001112 return -1;
David Benjamin32a66d52016-07-13 22:03:11 -04001113 }
1114
David Benjamin75836432016-06-17 18:48:29 -04001115 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin16315f72017-01-12 20:02:05 -05001116 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001117 }
David Benjamin16315f72017-01-12 20:02:05 -05001118 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001119}
1120
David Benjaminc3c88822016-11-14 10:32:04 +09001121static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1122 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001123 CBB cbb, body, ocsp_response;
1124 if (!ssl->method->init_message(ssl, &cbb, &body,
1125 SSL3_MT_CERTIFICATE_STATUS) ||
1126 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1127 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
Alessandro Ghedini559f0642016-12-07 12:55:32 +00001128 !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response),
1129 CRYPTO_BUFFER_len(ssl->ocsp_response)) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001130 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001131 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1132 CBB_cleanup(&cbb);
1133 return -1;
1134 }
1135
David Benjamin16315f72017-01-12 20:02:05 -05001136 return 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001137}
1138
David Benjaminc3c88822016-11-14 10:32:04 +09001139static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1140 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -05001141 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001142 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001143
David Benjaminc42acee2016-06-17 17:47:58 -04001144 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001145 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001146 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1147 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001148
David Benjaminc42acee2016-06-17 17:47:58 -04001149 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1150 if (!CBB_init(&cbb, 128)) {
1151 goto err;
1152 }
1153
David Benjamin2a0b3912015-12-18 01:01:21 -05001154 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001155 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001156 size_t len =
1157 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1158 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1159 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1160 len)) {
1161 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
David Benjamin7061e282015-03-19 11:10:48 -04001165 if (alg_k & SSL_kDHE) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001166 /* Determine the group to use. */
1167 DH *params = ssl->cert->dh_tmp;
1168 if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
1169 params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001171 if (params == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001172 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001173 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001174 goto err;
1175 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001176
David Benjamin974c7ba2015-12-19 16:58:04 -05001177 /* Set up DH, generate a key, and emit the public half. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001178 DH *dh = DHparams_dup(params);
David Benjamin974c7ba2015-12-19 16:58:04 -05001179 if (dh == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001180 goto err;
1181 }
David Benjamin9f226a52015-04-26 22:12:32 -04001182
David Benjaminc3c88822016-11-14 10:32:04 +09001183 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
David Benjamin2a0b3912015-12-18 01:01:21 -05001184 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001185 !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001186 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjamin974c7ba2015-12-19 16:58:04 -05001187 !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001188 !CBB_add_u16_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001189 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamin9f226a52015-04-26 22:12:32 -04001190 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 }
David Benjamin7061e282015-03-19 11:10:48 -04001192 } else if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001193 /* Determine the group to use. */
1194 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001195 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001196 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001197 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001198 goto err;
1199 }
David Benjamin4882a6c2016-12-11 02:48:12 -05001200 ssl->s3->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001201
David Benjamin4298d772015-12-19 00:18:25 -05001202 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001203 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001204 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001205 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001206 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001207 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001208 goto err;
1209 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001210 } else {
1211 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001212 }
Adam Langley95c29f32014-06-20 12:00:00 -07001213
David Benjaminc3c88822016-11-14 10:32:04 +09001214 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001215 goto err;
1216 }
David Benjaminc42acee2016-06-17 17:47:58 -04001217 }
1218
1219 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001220 CBB body;
1221 if (!ssl->method->init_message(ssl, &cbb, &body,
1222 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001223 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001224 goto err;
1225 }
Adam Langley95c29f32014-06-20 12:00:00 -07001226
David Benjamin2a0b3912015-12-18 01:01:21 -05001227 /* Add a signature. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001228 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001229 if (!ssl_has_private_key(ssl)) {
1230 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001231 goto err;
1232 }
Adam Langley95c29f32014-06-20 12:00:00 -07001233
David Benjaminc42acee2016-06-17 17:47:58 -04001234 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001235 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001236 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001237 goto err;
1238 }
David Benjaminc42acee2016-06-17 17:47:58 -04001239 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001240 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001241 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1242 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1243 goto err;
1244 }
David Benjaminc42acee2016-06-17 17:47:58 -04001245 }
1246
1247 /* Add space for the signature. */
David Benjamin2a0b3912015-12-18 01:01:21 -05001248 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc42acee2016-06-17 17:47:58 -04001249 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001250 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001251 !CBB_reserve(&child, &ptr, max_sig_len)) {
1252 goto err;
1253 }
1254
David Benjamin2a0b3912015-12-18 01:01:21 -05001255 size_t sig_len;
1256 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001257 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001258 CBB transcript;
1259 uint8_t *transcript_data;
1260 size_t transcript_len;
1261 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001262 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1263 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1264 SSL3_RANDOM_SIZE) ||
1265 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1266 SSL3_RANDOM_SIZE) ||
1267 !CBB_add_bytes(&transcript, hs->server_params,
1268 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001269 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1270 CBB_cleanup(&transcript);
1271 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1272 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001273 goto err;
1274 }
1275
Steven Valdezf0451ca2016-06-29 13:16:27 -04001276 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001277 signature_algorithm, transcript_data,
1278 transcript_len);
1279 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001280 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001281 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001282 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001283 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001284
1285 switch (sign_result) {
1286 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001287 if (!CBB_did_write(&child, sig_len)) {
1288 goto err;
1289 }
1290 break;
1291 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001292 goto err;
1293 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001294 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001295 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001296 goto err;
1297 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001298 }
1299
David Benjamindaf207a2017-01-03 18:37:41 -05001300 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001301 goto err;
1302 }
David Benjaminc42acee2016-06-17 17:47:58 -04001303
David Benjaminc3c88822016-11-14 10:32:04 +09001304 OPENSSL_free(hs->server_params);
1305 hs->server_params = NULL;
1306 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001307
David Benjamin16315f72017-01-12 20:02:05 -05001308 return 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001309
Adam Langley95c29f32014-06-20 12:00:00 -07001310err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001311 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 return -1;
1313}
Adam Langley95c29f32014-06-20 12:00:00 -07001314
David Benjamin75836432016-06-17 18:48:29 -04001315static int add_cert_types(SSL *ssl, CBB *cbb) {
1316 /* Get configured signature algorithms. */
1317 int have_rsa_sign = 0;
1318 int have_ecdsa_sign = 0;
Steven Valdez02563852016-06-23 13:33:05 -04001319 const uint16_t *sig_algs;
David Benjamin3ef76972016-10-17 17:59:54 -04001320 size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs);
1321 for (size_t i = 0; i < num_sig_algs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001322 switch (sig_algs[i]) {
1323 case SSL_SIGN_RSA_PKCS1_SHA512:
1324 case SSL_SIGN_RSA_PKCS1_SHA384:
1325 case SSL_SIGN_RSA_PKCS1_SHA256:
1326 case SSL_SIGN_RSA_PKCS1_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001327 have_rsa_sign = 1;
1328 break;
Adam Langley95c29f32014-06-20 12:00:00 -07001329
Steven Valdez02563852016-06-23 13:33:05 -04001330 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
1331 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
1332 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
1333 case SSL_SIGN_ECDSA_SHA1:
David Benjamin75836432016-06-17 18:48:29 -04001334 have_ecdsa_sign = 1;
1335 break;
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001337 }
Adam Langley95c29f32014-06-20 12:00:00 -07001338
David Benjamin75836432016-06-17 18:48:29 -04001339 if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
1340 return 0;
1341 }
1342
1343 /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
1344 * check for SSL_kECDH or SSL_kECDHE. */
1345 if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
1346 !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
1347 return 0;
1348 }
1349
1350 return 1;
1351}
1352
David Benjaminc3c88822016-11-14 10:32:04 +09001353static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1354 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001355 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001356 if (!ssl->method->init_message(ssl, &cbb, &body,
1357 SSL3_MT_CERTIFICATE_REQUEST) ||
1358 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1359 !add_cert_types(ssl, &cert_types)) {
1360 goto err;
1361 }
1362
1363 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001364 const uint16_t *sigalgs;
David Benjamin3ef76972016-10-17 17:59:54 -04001365 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
Steven Valdez02563852016-06-23 13:33:05 -04001366 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001367 goto err;
1368 }
Steven Valdez02563852016-06-23 13:33:05 -04001369
David Benjamin0fc37ef2016-08-17 15:29:46 -04001370 for (size_t i = 0; i < num_sigalgs; i++) {
Steven Valdez02563852016-06-23 13:33:05 -04001371 if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
1372 goto err;
1373 }
1374 }
David Benjamin75836432016-06-17 18:48:29 -04001375 }
1376
David Benjamin32a66d52016-07-13 22:03:11 -04001377 if (!ssl_add_client_CA_list(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001378 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001379 goto err;
1380 }
1381
David Benjamin16315f72017-01-12 20:02:05 -05001382 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001383
Adam Langley95c29f32014-06-20 12:00:00 -07001384err:
David Benjamin75836432016-06-17 18:48:29 -04001385 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1386 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001387 return -1;
1388}
Adam Langley95c29f32014-06-20 12:00:00 -07001389
David Benjaminc3c88822016-11-14 10:32:04 +09001390static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1391 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001392 CBB cbb, body;
1393 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001394 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001395 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1396 CBB_cleanup(&cbb);
1397 return -1;
1398 }
1399
David Benjamin16315f72017-01-12 20:02:05 -05001400 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001401}
1402
David Benjaminc3c88822016-11-14 10:32:04 +09001403static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1404 SSL *const ssl = hs->ssl;
1405 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001406
David Benjaminf71036e2017-01-21 14:49:39 -05001407 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001408 if (msg_ret <= 0) {
1409 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001410 }
1411
1412 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1413 if (ssl->version == SSL3_VERSION &&
1414 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001415 /* In SSL 3.0, the Certificate message is omitted to signal no
1416 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001417 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001418 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001419 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1420 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001421 }
1422
Adam Langley37646832016-08-01 16:16:46 -07001423 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001424 * classed by them as a bug, but it's assumed by at least NGINX. */
Adam Langley37646832016-08-01 16:16:46 -07001425 ssl->s3->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001426 ssl->s3->tmp.reuse_message = 1;
1427 return 1;
1428 }
1429
David Benjamin9f1dc822016-06-07 17:03:46 -04001430 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001431 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1432 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001433 }
1434
David Benjaminf71036e2017-01-21 14:49:39 -05001435 if (!ssl_hash_current_message(ssl)) {
1436 return -1;
1437 }
1438
David Benjamin5c900c82016-07-13 23:03:26 -04001439 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001440 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001441
1442 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001443 EVP_PKEY_free(hs->peer_pubkey);
1444 hs->peer_pubkey = NULL;
David Benjamin5c900c82016-07-13 23:03:26 -04001445 uint8_t alert;
Adam Langley68e71242016-12-12 11:06:16 -08001446 ssl->s3->new_session->certs =
Adam Langleyd5157222016-12-12 11:37:43 -08001447 ssl_parse_cert_chain(&alert, &hs->peer_pubkey,
1448 ssl->retain_only_sha256_of_client_certs
1449 ? ssl->s3->new_session->peer_sha256
1450 : NULL,
Adam Langleyd519bf62016-12-12 11:16:44 -08001451 &certificate_msg, ssl->ctx->pool);
Adam Langley68e71242016-12-12 11:06:16 -08001452 if (ssl->s3->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001453 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001454 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001455 }
1456
Adam Langley68e71242016-12-12 11:06:16 -08001457 if (CBS_len(&certificate_msg) != 0 ||
1458 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001459 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001460 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001461 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001462 }
1463
Adam Langley68e71242016-12-12 11:06:16 -08001464 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001465 /* No client certificate so the handshake buffer may be discarded. */
1466 ssl3_free_handshake_buffer(ssl);
1467
David Benjamine455e512016-08-01 20:11:13 -04001468 /* In SSL 3.0, sending no certificate is signaled by omitting the
1469 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001470 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001471 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
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 Benjamine455e512016-08-01 20:11:13 -04001474 }
1475
1476 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001477 /* Fail for TLS only if we required a certificate */
1478 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001479 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001480 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001481 }
Adam Langley37646832016-08-01 16:16:46 -07001482
1483 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001484 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin7aa31d62016-08-08 21:38:32 -04001485 ssl->s3->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001486 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001487 }
1488
Adam Langley68e71242016-12-12 11:06:16 -08001489 /* The hash will have been filled in. */
1490 if (ssl->retain_only_sha256_of_client_certs) {
1491 ssl->s3->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001492 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001493
Adam Langley68e71242016-12-12 11:06:16 -08001494 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1495 ssl->s3->new_session->x509_chain)) {
1496 return -1;
1497 }
David Benjamin5c900c82016-07-13 23:03:26 -04001498 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001499}
1500
David Benjaminc3c88822016-11-14 10:32:04 +09001501static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1502 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001503 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001504 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001505 uint32_t alg_k;
1506 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 uint8_t *premaster_secret = NULL;
1508 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001509 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001510
David Benjamin4298d772015-12-19 00:18:25 -05001511 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001512 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001513
David Benjamincb0c29f2016-12-12 17:00:50 -05001514 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminf71036e2017-01-21 14:49:39 -05001515 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001516 if (ret <= 0) {
1517 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001518 }
David Benjamin276b7e82017-01-21 14:13:39 -05001519
David Benjaminf71036e2017-01-21 14:49:39 -05001520 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
1521 !ssl_hash_current_message(ssl)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001522 return -1;
1523 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001524 }
Adam Langley95c29f32014-06-20 12:00:00 -07001525
David Benjamin0d56f882015-12-19 17:05:56 -05001526 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
1527 alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1528 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001529
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 /* If using a PSK key exchange, prepare the pre-shared key. */
1531 if (alg_a & SSL_aPSK) {
1532 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1535 * then this is the only field in the message. */
1536 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1537 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001538 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 al = SSL_AD_DECODE_ERROR;
1540 goto f_err;
1541 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001542
David Benjamin0d56f882015-12-19 17:05:56 -05001543 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001544 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001545 al = SSL_AD_INTERNAL_ERROR;
1546 goto f_err;
1547 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001548
Adam Langleyfcf25832014-12-18 17:42:32 -08001549 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1550 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001551 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 al = SSL_AD_ILLEGAL_PARAMETER;
1553 goto f_err;
1554 }
David Benjamin35c02302014-07-13 04:14:59 -04001555
Steven Valdez87eab492016-06-27 16:34:59 -04001556 if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001557 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001558 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001559 goto f_err;
1560 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001561
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 /* Look up the key for the identity. */
Steven Valdez87eab492016-06-27 16:34:59 -04001563 psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity,
1564 psk, sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001565 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001566 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 al = SSL_AD_INTERNAL_ERROR;
1568 goto f_err;
1569 } else if (psk_len == 0) {
1570 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001571 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1573 goto f_err;
1574 }
1575 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001576
Adam Langleyfcf25832014-12-18 17:42:32 -08001577 /* Depending on the key exchange method, compute |premaster_secret| and
1578 * |premaster_secret_len|. */
1579 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001580 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin0d56f882015-12-19 17:05:56 -05001581 const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001582 decrypt_buf = OPENSSL_malloc(rsa_size);
1583 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001584 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001585 goto err;
1586 }
David Benjamin35c02302014-07-13 04:14:59 -04001587
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001588 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001589 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001590 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001591 if (!ssl_has_private_key(ssl) ||
David Benjamin0c0b7e12016-07-14 13:47:55 -04001592 ssl_private_key_type(ssl) != NID_rsaEncryption) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001593 al = SSL_AD_HANDSHAKE_FAILURE;
1594 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1595 goto f_err;
1596 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001597 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001598 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001599 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1600 &encrypted_premaster_secret) ||
1601 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001602 al = SSL_AD_DECODE_ERROR;
1603 OPENSSL_PUT_ERROR(SSL,
1604 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1605 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001606 }
1607 } else {
1608 encrypted_premaster_secret = client_key_exchange;
1609 }
1610
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001611 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1612 * timing-sensitive code below. */
1613 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001614 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001615 CBS_data(&encrypted_premaster_secret),
1616 CBS_len(&encrypted_premaster_secret));
1617 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001618 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001619 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001620 decrypt_result =
1621 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001623
1624 switch (decrypt_result) {
1625 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001626 break;
1627 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001628 goto err;
1629 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001630 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001631 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001632 goto err;
1633 }
1634
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001635 if (decrypt_len != rsa_size) {
1636 al = SSL_AD_DECRYPT_ERROR;
1637 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1638 goto f_err;
1639 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001640
David Benjamin3f5b43d2015-12-01 19:31:24 -05001641 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1642 * section 7.4.7.1. */
1643 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1644 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001645 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001646 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 goto err;
1648 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001649 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001650 goto err;
1651 }
1652
David Benjamin3f5b43d2015-12-01 19:31:24 -05001653 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1654 * publicly invalid. */
1655 if (decrypt_len < 11 + premaster_secret_len) {
1656 al = SSL_AD_DECRYPT_ERROR;
1657 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1658 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 }
David Benjamin35c02302014-07-13 04:14:59 -04001660
David Benjamin3f5b43d2015-12-01 19:31:24 -05001661 /* Check the padding. See RFC 3447, section 7.2.2. */
1662 size_t padding_len = decrypt_len - premaster_secret_len;
1663 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1664 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001665 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001666 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1667 }
1668 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1669
1670 /* The premaster secret must begin with |client_version|. This too must be
1671 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1672 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001673 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001674 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001675 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001676
1677 /* Select, in constant time, either the decrypted premaster or the random
1678 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001679 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001680 premaster_secret[i] = constant_time_select_8(
1681 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1682 }
1683
1684 OPENSSL_free(decrypt_buf);
1685 decrypt_buf = NULL;
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001686 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001687 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001688 CBS peer_key;
David Benjaminc3c88822016-11-14 10:32:04 +09001689 if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001690 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001691 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001692 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001693 goto f_err;
1694 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001695
David Benjamin4298d772015-12-19 00:18:25 -05001696 /* Compute the premaster. */
1697 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001698 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001699 &premaster_secret_len, &alert, CBS_data(&peer_key),
1700 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001701 al = alert;
1702 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001703 }
Adam Langley95c29f32014-06-20 12:00:00 -07001704
David Benjamin4298d772015-12-19 00:18:25 -05001705 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001706 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001707 } else if (alg_k & SSL_kPSK) {
1708 /* For plain PSK, other_secret is a block of 0s with the same length as the
1709 * pre-shared key. */
1710 premaster_secret_len = psk_len;
1711 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1712 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001713 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001714 goto err;
1715 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001716 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001717 } else {
1718 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001719 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001720 goto f_err;
1721 }
Adam Langley95c29f32014-06-20 12:00:00 -07001722
Adam Langleyfcf25832014-12-18 17:42:32 -08001723 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1724 * pre-shared key. */
1725 if (alg_a & SSL_aPSK) {
1726 CBB new_premaster, child;
1727 uint8_t *new_data;
1728 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001729
David Benjamina8653202015-06-28 01:26:10 -04001730 CBB_zero(&new_premaster);
1731 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1732 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1734 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1735 !CBB_add_bytes(&child, psk, psk_len) ||
1736 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001737 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001738 CBB_cleanup(&new_premaster);
1739 goto err;
1740 }
David Benjamin14c83e72014-07-13 04:54:57 -04001741
Adam Langleyfcf25832014-12-18 17:42:32 -08001742 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1743 OPENSSL_free(premaster_secret);
1744 premaster_secret = new_data;
1745 premaster_secret_len = new_len;
1746 }
David Benjamin14c83e72014-07-13 04:54:57 -04001747
Adam Langleyfcf25832014-12-18 17:42:32 -08001748 /* Compute the master secret */
Steven Valdez87eab492016-06-27 16:34:59 -04001749 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1750 ssl, ssl->s3->new_session->master_key, premaster_secret,
1751 premaster_secret_len);
1752 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001753 goto err;
1754 }
Steven Valdez87eab492016-06-27 16:34:59 -04001755 ssl->s3->new_session->extended_master_secret =
1756 ssl->s3->tmp.extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001757
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1759 OPENSSL_free(premaster_secret);
1760 return 1;
1761
Adam Langley95c29f32014-06-20 12:00:00 -07001762f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001763 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001764err:
David Benjamin4298d772015-12-19 00:18:25 -05001765 if (premaster_secret != NULL) {
1766 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001767 OPENSSL_free(premaster_secret);
1768 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001769 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001770
Adam Langleyfcf25832014-12-18 17:42:32 -08001771 return -1;
1772}
Adam Langley95c29f32014-06-20 12:00:00 -07001773
David Benjaminc3c88822016-11-14 10:32:04 +09001774static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1775 SSL *const ssl = hs->ssl;
Adam Langleyd5157222016-12-12 11:37:43 -08001776 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001777 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001778
Adam Langleyfcf25832014-12-18 17:42:32 -08001779 /* Only RSA and ECDSA client certificates are supported, so a
1780 * CertificateVerify is required if and only if there's a client certificate.
1781 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001782 if (hs->peer_pubkey == NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -05001783 ssl3_free_handshake_buffer(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001784 return 1;
1785 }
Adam Langley95c29f32014-06-20 12:00:00 -07001786
David Benjaminf71036e2017-01-21 14:49:39 -05001787 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001788 if (msg_ret <= 0) {
1789 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001790 }
David Benjaminef865502014-08-24 02:48:34 -04001791
David Benjamin276b7e82017-01-21 14:13:39 -05001792 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1793 return -1;
1794 }
1795
David Benjamin09eb6552016-07-08 14:32:11 -07001796 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001797
Adam Langleyfcf25832014-12-18 17:42:32 -08001798 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001799 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001800 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001801 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001802 al = SSL_AD_DECODE_ERROR;
1803 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1804 goto f_err;
1805 }
David Benjamin887c3002016-07-08 16:15:32 -07001806 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001807 goto f_err;
1808 }
David Benjaminf1050fd2016-12-13 20:05:36 -05001809 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001810 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001811 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001812 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001813 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001814 } else {
1815 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1816 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1817 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 }
David Benjamin854dd652014-08-26 00:32:30 -04001819
Adam Langleyfcf25832014-12-18 17:42:32 -08001820 /* Parse and verify the signature. */
1821 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1822 CBS_len(&certificate_verify) != 0) {
1823 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001824 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001825 goto f_err;
1826 }
Adam Langley95c29f32014-06-20 12:00:00 -07001827
Steven Valdez2b8415e2016-06-30 13:27:23 -04001828 int sig_ok;
1829 /* The SSL3 construction for CertificateVerify does not decompose into a
1830 * single final digest and signature, and must be special-cased. */
1831 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
David Benjamin0aa25bd2016-07-08 14:44:56 -07001832 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001833 uint8_t digest[EVP_MAX_MD_SIZE];
1834 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001835 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001836 signature_algorithm)) {
1837 goto err;
1838 }
1839
Adam Langleyd5157222016-12-12 11:37:43 -08001840 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001841 sig_ok = pctx != NULL &&
1842 EVP_PKEY_verify_init(pctx) &&
1843 EVP_PKEY_CTX_set_signature_md(pctx, md) &&
1844 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1845 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001846 EVP_PKEY_CTX_free(pctx);
1847 } else {
1848 sig_ok = ssl_public_key_verify(
1849 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001850 hs->peer_pubkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001851 ssl->s3->handshake_buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001852 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001853
David Benjaminbf82aed2016-03-01 22:57:40 -05001854#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1855 sig_ok = 1;
1856 ERR_clear_error();
1857#endif
1858 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001859 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001860 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001861 goto f_err;
1862 }
1863
Steven Valdez2b8415e2016-06-30 13:27:23 -04001864 /* The handshake buffer is no longer necessary, and we may hash the current
1865 * message.*/
1866 ssl3_free_handshake_buffer(ssl);
David Benjaminced94792016-11-14 17:12:11 +09001867 if (!ssl_hash_current_message(ssl)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001868 goto err;
1869 }
1870
Adam Langleyd5157222016-12-12 11:37:43 -08001871 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001872
Adam Langleyd5157222016-12-12 11:37:43 -08001873f_err:
1874 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin854dd652014-08-26 00:32:30 -04001875err:
Adam Langleyd5157222016-12-12 11:37:43 -08001876 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001877}
Adam Langley95c29f32014-06-20 12:00:00 -07001878
Adam Langley95c29f32014-06-20 12:00:00 -07001879/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1880 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001881static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1882 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001883 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001884 if (ret <= 0) {
1885 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001886 }
Adam Langley95c29f32014-06-20 12:00:00 -07001887
David Benjaminf71036e2017-01-21 14:49:39 -05001888 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
1889 !ssl_hash_current_message(ssl)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001890 return -1;
1891 }
1892
David Benjamin09eb6552016-07-08 14:32:11 -07001893 CBS next_protocol, selected_protocol, padding;
1894 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001895 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1896 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001897 CBS_len(&next_protocol) != 0) {
1898 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1899 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1900 return 0;
1901 }
1902
1903 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001904 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001905 return 0;
1906 }
Adam Langley95c29f32014-06-20 12:00:00 -07001907
Adam Langleyfcf25832014-12-18 17:42:32 -08001908 return 1;
1909}
Adam Langley95c29f32014-06-20 12:00:00 -07001910
Adam Langley1258b6a2014-06-20 12:00:00 -07001911/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001912static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1913 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001914 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001915 if (msg_ret <= 0) {
1916 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001917 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001918
David Benjamin276b7e82017-01-21 14:13:39 -05001919 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
1920 !tls1_verify_channel_id(ssl) ||
David Benjaminced94792016-11-14 17:12:11 +09001921 !ssl_hash_current_message(ssl)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001922 return -1;
1923 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001924 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001925}
David Benjamin9f1dc822016-06-07 17:03:46 -04001926
David Benjaminc3c88822016-11-14 10:32:04 +09001927static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1928 SSL *const ssl = hs->ssl;
David Benjamin123db572016-11-03 16:59:25 -04001929 const SSL_SESSION *session;
1930 SSL_SESSION *session_copy = NULL;
1931 if (ssl->session == NULL) {
1932 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin17b30832017-01-28 14:00:32 -05001933 ssl_session_rebase_time(ssl, ssl->s3->new_session);
David Benjamin123db572016-11-03 16:59:25 -04001934 session = ssl->s3->new_session;
1935 } else {
1936 /* We are renewing an existing session. Duplicate the session to adjust the
1937 * timeout. */
1938 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1939 if (session_copy == NULL) {
1940 return -1;
1941 }
1942
David Benjamin17b30832017-01-28 14:00:32 -05001943 ssl_session_rebase_time(ssl, session_copy);
David Benjamin123db572016-11-03 16:59:25 -04001944 session = session_copy;
1945 }
1946
David Benjamin75836432016-06-17 18:48:29 -04001947 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001948 int ok =
1949 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1950 CBB_add_u32(&body, session->timeout) &&
1951 CBB_add_u16_length_prefixed(&body, &ticket) &&
1952 ssl_encrypt_ticket(ssl, &ticket, session) &&
David Benjamindaf207a2017-01-03 18:37:41 -05001953 ssl_add_message_cbb(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001954
1955 SSL_SESSION_free(session_copy);
1956 CBB_cleanup(&cbb);
1957
1958 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001959 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001960 }
1961
David Benjamin16315f72017-01-12 20:02:05 -05001962 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001963}