blob: 3979801da5c1e7e3316c1048a0ccdff759ad823d [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>
Adam Langley1258b6a2014-06-20 12:00:00 -0700158#include <openssl/ec.h>
159#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400160#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700161#include <openssl/evp.h>
162#include <openssl/hmac.h>
163#include <openssl/md5.h>
164#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -0400165#include <openssl/nid.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/rand.h>
167#include <openssl/x509.h>
168
David Benjamin2ee94aa2015-04-07 22:38:30 -0400169#include "internal.h"
Adam Langleyaf6e45b2014-11-03 19:34:49 -0800170#include "../crypto/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172
David Benjamin59bae5a2017-02-02 23:51:22 -0500173static int ssl3_process_client_hello(SSL_HANDSHAKE *hs);
174static int ssl3_select_certificate(SSL_HANDSHAKE *hs);
175static int ssl3_select_parameters(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900176static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
177static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
178static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs);
179static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
180static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs);
181static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
183static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
184static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
185static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
186static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
187static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400188
Steven Valdez258508f2017-01-25 15:47:49 -0500189static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock =
190 CRYPTO_STATIC_MUTEX_INIT;
191static uint64_t g_v2clienthello_count = 0;
192
193uint64_t SSL_get_v2clienthello_count(void) {
194 CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock);
195 uint64_t ret = g_v2clienthello_count;
196 CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock);
197 return ret;
198}
199
David Benjamince8c9d22016-11-14 10:45:16 +0900200int ssl3_accept(SSL_HANDSHAKE *hs) {
201 SSL *const ssl = hs->ssl;
David Benjamin3fa27772015-04-17 22:32:19 -0400202 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 int ret = -1;
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 Benjamin16b1b1d2017-02-10 23:41:14 -0500209 int 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 Benjamin4e9cc712016-06-01 20:16:03 -0400213 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500214 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500216
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamin59bae5a2017-02-02 23:51:22 -0500218 ret = ssl->method->ssl_get_message(ssl);
219 if (ret <= 0) {
220 goto end;
Steven Valdez143e8b32016-07-11 13:19:03 -0400221 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500222 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
223 break;
224
225 case SSL3_ST_SR_CLNT_HELLO_B:
226 ret = ssl3_process_client_hello(hs);
227 if (ret <= 0) {
228 goto end;
229 }
230 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
231 break;
232
233 case SSL3_ST_SR_CLNT_HELLO_C:
234 ret = ssl3_select_certificate(hs);
235 if (ret <= 0) {
236 goto end;
237 }
238 if (hs->state != SSL_ST_TLS13) {
239 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
240 }
241 break;
242
243 case SSL3_ST_SR_CLNT_HELLO_D:
244 ret = ssl3_select_parameters(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 if (ret <= 0) {
246 goto end;
247 }
David Benjamindf50eec2016-06-07 16:49:42 -0400248 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500249 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900253 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (ret <= 0) {
255 goto end;
256 }
Steven Valdez87eab492016-06-27 16:34:59 -0400257 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500258 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500260 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700263
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 case SSL3_ST_SW_CERT_A:
David Benjamin45738dd2017-02-09 20:01:26 -0500265 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900266 ret = ssl3_send_server_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 if (ret <= 0) {
268 goto end;
269 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500271 hs->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700273
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100274 case SSL3_ST_SW_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900275 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900276 ret = ssl3_send_certificate_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400277 if (ret <= 0) {
278 goto end;
279 }
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100280 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500281 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100282 break;
283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 case SSL3_ST_SW_KEY_EXCH_A:
285 case SSL3_ST_SW_KEY_EXCH_B:
David Benjamin45738dd2017-02-09 20:01:26 -0500286 alg_a = hs->new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
David Benjamindf50eec2016-06-07 16:49:42 -0400288 /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
David Benjamin45738dd2017-02-09 20:01:26 -0500289 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500290 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900291 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500292 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500294 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
David Benjamincb0c29f2016-12-12 17:00:50 -0500297 hs->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 case SSL3_ST_SW_CERT_REQ_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900301 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900302 ret = ssl3_send_certificate_request(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 if (ret <= 0) {
304 goto end;
305 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500307 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 case SSL3_ST_SW_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900311 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 if (ret <= 0) {
313 goto end;
314 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500315 hs->next_state = SSL3_ST_SR_CERT_A;
316 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800317 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900320 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900321 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 if (ret <= 0) {
323 goto end;
324 }
325 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500326 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 case SSL3_ST_SR_KEY_EXCH_A:
330 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900331 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 if (ret <= 0) {
333 goto end;
334 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500335 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900339 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 if (ret <= 0) {
341 goto end;
342 }
Adam Langley95c29f32014-06-20 12:00:00 -0700343
David Benjamincb0c29f2016-12-12 17:00:50 -0500344 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700346
David Benjamina41280d2015-11-26 02:16:49 -0500347 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400348 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500349 if (ret <= 0) {
350 goto end;
351 }
Adam Langley95c29f32014-06-20 12:00:00 -0700352
David Benjamin67739722016-11-17 17:03:59 +0900353 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 ret = -1;
355 goto end;
356 }
David Benjamina41280d2015-11-26 02:16:49 -0500357
David Benjamincb0c29f2016-12-12 17:00:50 -0500358 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900362 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900363 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400364 if (ret <= 0) {
365 goto end;
366 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500368 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400372 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900373 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400374 if (ret <= 0) {
375 goto end;
376 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500378 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900382 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 if (ret <= 0) {
384 goto end;
385 }
Adam Langley95c29f32014-06-20 12:00:00 -0700386
David Benjamindf50eec2016-06-07 16:49:42 -0400387 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400388 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500389 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500391 hs->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 }
David Benjamindf50eec2016-06-07 16:49:42 -0400393
Steven Valdez87eab492016-06-27 16:34:59 -0400394 /* If this is a full handshake with ChannelID then record the handshake
David Benjamin45738dd2017-02-09 20:01:26 -0500395 * hashes in |hs->new_session| in case we need them to verify a
Steven Valdez87eab492016-06-27 16:34:59 -0400396 * ChannelID signature on a resumption of this session in the future. */
397 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Steven Valdez908ac192017-01-12 13:17:07 -0500398 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 if (ret <= 0) {
400 goto end;
401 }
402 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 case SSL3_ST_SW_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900406 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900407 ret = ssl3_send_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400408 if (ret <= 0) {
409 goto end;
410 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800411 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500412 hs->state = SSL3_ST_SW_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
David Benjamin352d0a92016-06-28 11:22:02 -0400415 case SSL3_ST_SW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500416 if (!ssl->method->add_change_cipher_spec(ssl) ||
417 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 ret = -1;
419 goto end;
420 }
David Benjamindaf207a2017-01-03 18:37:41 -0500421
422 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 case SSL3_ST_SW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500426 ret = ssl3_send_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 if (ret <= 0) {
428 goto end;
429 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500430 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400431 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500432 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500434 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700437
David Benjamin9f1dc822016-06-07 17:03:46 -0400438 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500439 ret = ssl->method->flush_flight(ssl);
440 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400441 goto end;
442 }
443
David Benjamincb0c29f2016-12-12 17:00:50 -0500444 hs->state = hs->next_state;
445 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400446 ssl->method->expect_flight(ssl);
447 }
448 break;
449
Steven Valdez681eb6a2016-12-19 13:19:29 -0500450 case SSL_ST_TLS13: {
451 int early_return = 0;
452 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400453 if (ret <= 0) {
454 goto end;
455 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500456
457 if (early_return) {
458 ret = 1;
459 goto end;
460 }
461
David Benjamincb0c29f2016-12-12 17:00:50 -0500462 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400463 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500464 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400467 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Steven Valdez87eab492016-06-27 16:34:59 -0400469 /* If we aren't retaining peer certificates then we can discard it
470 * now. */
David Benjamin45738dd2017-02-09 20:01:26 -0500471 if (hs->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900472 ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -0500473 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
474 hs->new_session->certs = NULL;
475 ssl->ctx->x509_method->session_clear(hs->new_session);
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 {
David Benjamin45738dd2017-02-09 20:01:26 -0500483 ssl->s3->established_session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -0400484 ssl->s3->established_session->not_resumable = 0;
David Benjamin45738dd2017-02-09 20:01:26 -0500485 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400486 }
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 Benjamin16b1b1d2017-02-10 23:41:14 -0500507 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400508 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 }
511
Adam Langley95c29f32014-06-20 12:00:00 -0700512end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400513 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 return ret;
515}
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamin731058e2016-12-03 23:15:13 -0500517int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
518 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400519 CBS cipher_suites;
520 CBS_init(&cipher_suites, client_hello->cipher_suites,
521 client_hello->cipher_suites_len);
522
523 while (CBS_len(&cipher_suites) > 0) {
524 uint16_t got_id;
525 if (!CBS_get_u16(&cipher_suites, &got_id)) {
526 return 0;
527 }
528
529 if (got_id == id) {
530 return 1;
531 }
532 }
533
534 return 0;
535}
536
David Benjaminf04c2e92016-12-06 13:35:25 -0500537static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500538 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500539 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500540 assert(!ssl->s3->have_version);
David Benjamin25fe85b2016-08-09 20:00:32 -0400541 uint16_t min_version, max_version;
542 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
543 *out_alert = SSL_AD_PROTOCOL_VERSION;
544 return 0;
545 }
546
David Benjamin2dc02042016-09-19 19:57:37 -0400547 uint16_t version = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400548 /* Check supported_versions extension if it is present. */
549 CBS supported_versions;
David Benjamin731058e2016-12-03 23:15:13 -0500550 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
551 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400552 CBS versions;
553 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
554 CBS_len(&supported_versions) != 0 ||
555 CBS_len(&versions) == 0) {
556 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
557 *out_alert = SSL_AD_DECODE_ERROR;
558 return 0;
559 }
560
Brian Smithf85d3232016-10-28 10:34:06 -1000561 /* Choose the newest commonly-supported version advertised by the client.
562 * The client orders the versions according to its preferences, but we're
563 * not required to honor the client's preferences. */
Steven Valdezfdd10992016-09-15 16:27:05 -0400564 int found_version = 0;
565 while (CBS_len(&versions) != 0) {
566 uint16_t ext_version;
567 if (!CBS_get_u16(&versions, &ext_version)) {
568 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
569 *out_alert = SSL_AD_DECODE_ERROR;
570 return 0;
571 }
572 if (!ssl->method->version_from_wire(&ext_version, ext_version)) {
573 continue;
574 }
575 if (min_version <= ext_version &&
Brian Smithf85d3232016-10-28 10:34:06 -1000576 ext_version <= max_version &&
577 (!found_version || version < ext_version)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400578 version = ext_version;
579 found_version = 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400580 }
581 }
582
583 if (!found_version) {
584 goto unsupported_protocol;
David Benjamin2dc02042016-09-19 19:57:37 -0400585 }
586 } else {
Steven Valdezfdd10992016-09-15 16:27:05 -0400587 /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2
588 * do not use this mechanism. */
589 if (SSL_is_dtls(ssl)) {
590 if (client_hello->version <= DTLS1_2_VERSION) {
591 version = TLS1_2_VERSION;
592 } else if (client_hello->version <= DTLS1_VERSION) {
593 version = TLS1_1_VERSION;
594 } else {
595 goto unsupported_protocol;
596 }
597 } else {
598 if (client_hello->version >= TLS1_2_VERSION) {
599 version = TLS1_2_VERSION;
600 } else if (client_hello->version >= TLS1_1_VERSION) {
601 version = TLS1_1_VERSION;
602 } else if (client_hello->version >= TLS1_VERSION) {
603 version = TLS1_VERSION;
604 } else if (client_hello->version >= SSL3_VERSION) {
605 version = SSL3_VERSION;
606 } else {
607 goto unsupported_protocol;
608 }
David Benjamin2dc02042016-09-19 19:57:37 -0400609 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400610
Steven Valdezfdd10992016-09-15 16:27:05 -0400611 /* Apply our minimum and maximum version. */
612 if (version > max_version) {
613 version = max_version;
614 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400615
Steven Valdezfdd10992016-09-15 16:27:05 -0400616 if (version < min_version) {
617 goto unsupported_protocol;
618 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400619 }
620
621 /* Handle FALLBACK_SCSV. */
622 if (ssl_client_cipher_list_contains_cipher(client_hello,
623 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
624 version < max_version) {
625 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
626 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
627 return 0;
628 }
629
David Benjaminf04c2e92016-12-06 13:35:25 -0500630 hs->client_version = client_hello->version;
David Benjamin25fe85b2016-08-09 20:00:32 -0400631 ssl->version = ssl->method->version_to_wire(version);
David Benjamin25fe85b2016-08-09 20:00:32 -0400632
633 /* At this point, the connection's version is known and |ssl->version| is
634 * fixed. Begin enforcing the record-layer version. */
635 ssl->s3->have_version = 1;
636
637 return 1;
Steven Valdezfdd10992016-09-15 16:27:05 -0400638
639unsupported_protocol:
640 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
641 *out_alert = SSL_AD_PROTOCOL_VERSION;
642 return 0;
David Benjamin25fe85b2016-08-09 20:00:32 -0400643}
644
David Benjamin2578b292016-12-03 23:19:55 -0500645static STACK_OF(SSL_CIPHER) *
646 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
647 CBS cipher_suites;
648 CBS_init(&cipher_suites, client_hello->cipher_suites,
649 client_hello->cipher_suites_len);
650
651 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
652 if (sk == NULL) {
653 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
654 goto err;
655 }
656
657 while (CBS_len(&cipher_suites) > 0) {
658 uint16_t cipher_suite;
659
660 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
661 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
662 goto err;
663 }
664
665 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
666 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
667 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
668 goto err;
669 }
670 }
671
672 return sk;
673
674err:
675 sk_SSL_CIPHER_free(sk);
676 return NULL;
677}
678
679/* ssl_get_compatible_server_ciphers determines the key exchange and
680 * authentication cipher suite masks compatible with the server configuration
681 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
682 * exchange mask and |*out_mask_a| to the authentication mask. */
683static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
684 uint32_t *out_mask_k,
685 uint32_t *out_mask_a) {
686 SSL *const ssl = hs->ssl;
687 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
688 *out_mask_k = SSL_kGENERIC;
689 *out_mask_a = SSL_aGENERIC;
690 return;
691 }
692
693 uint32_t mask_k = 0;
694 uint32_t mask_a = 0;
695
Adam Langley3a2b47a2017-01-24 13:59:42 -0800696 if (ssl_has_certificate(ssl)) {
David Benjamin69522112017-03-28 15:38:29 -0500697 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey);
698 if (EVP_PKEY_id(hs->local_pubkey) == EVP_PKEY_RSA) {
David Benjamin2578b292016-12-03 23:19:55 -0500699 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500700 }
701 }
702
David Benjamin2578b292016-12-03 23:19:55 -0500703 /* Check for a shared group to consider ECDHE ciphers. */
704 uint16_t unused;
705 if (tls1_get_shared_group(hs, &unused)) {
706 mask_k |= SSL_kECDHE;
707 }
708
David Benjamin2578b292016-12-03 23:19:55 -0500709 /* PSK requires a server callback. */
710 if (ssl->psk_server_callback != NULL) {
711 mask_k |= SSL_kPSK;
712 mask_a |= SSL_aPSK;
713 }
714
715 *out_mask_k = mask_k;
716 *out_mask_a = mask_a;
717}
718
719static const SSL_CIPHER *ssl3_choose_cipher(
720 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
721 const struct ssl_cipher_preference_list_st *server_pref) {
722 SSL *const ssl = hs->ssl;
David Benjaminccbb1652017-03-22 18:24:32 -0400723 STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjamin2578b292016-12-03 23:19:55 -0500724 /* in_group_flags will either be NULL, or will point to an array of bytes
725 * which indicate equal-preference groups in the |prio| stack. See the
726 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
727 * struct. */
728 const uint8_t *in_group_flags;
729 /* group_min contains the minimal index so far found in a group, or -1 if no
730 * such value exists yet. */
731 int group_min = -1;
732
David Benjaminccbb1652017-03-22 18:24:32 -0400733 STACK_OF(SSL_CIPHER) *client_pref =
734 ssl_parse_client_cipher_list(client_hello);
735 if (client_pref == NULL) {
David Benjamin2578b292016-12-03 23:19:55 -0500736 return NULL;
737 }
738
739 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjaminccbb1652017-03-22 18:24:32 -0400740 prio = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500741 in_group_flags = server_pref->in_group_flags;
David Benjaminccbb1652017-03-22 18:24:32 -0400742 allow = client_pref;
David Benjamin2578b292016-12-03 23:19:55 -0500743 } else {
David Benjaminccbb1652017-03-22 18:24:32 -0400744 prio = client_pref;
David Benjamin2578b292016-12-03 23:19:55 -0500745 in_group_flags = NULL;
David Benjaminccbb1652017-03-22 18:24:32 -0400746 allow = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500747 }
748
David Benjaminccbb1652017-03-22 18:24:32 -0400749 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -0500750 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
751
David Benjaminccbb1652017-03-22 18:24:32 -0400752 const SSL_CIPHER *ret = NULL;
David Benjamin2578b292016-12-03 23:19:55 -0500753 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400754 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500755
David Benjaminccbb1652017-03-22 18:24:32 -0400756 size_t cipher_index;
757 if (/* Check if the cipher is supported for the current version. */
758 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
759 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
760 /* Check the cipher is supported for the server configuration. */
761 (c->algorithm_mkey & mask_k) &&
762 (c->algorithm_auth & mask_a) &&
763 /* Check the cipher is in the |allow| list. */
764 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500765 if (in_group_flags != NULL && in_group_flags[i] == 1) {
766 /* This element of |prio| is in a group. Update the minimum index found
767 * so far and continue looking. */
768 if (group_min == -1 || (size_t)group_min > cipher_index) {
769 group_min = cipher_index;
770 }
771 } else {
772 if (group_min != -1 && (size_t)group_min < cipher_index) {
773 cipher_index = group_min;
774 }
775 ret = sk_SSL_CIPHER_value(allow, cipher_index);
776 break;
777 }
778 }
779
780 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
781 /* We are about to leave a group, but we found a match in it, so that's
782 * our answer. */
783 ret = sk_SSL_CIPHER_value(allow, group_min);
784 break;
785 }
786 }
787
David Benjaminccbb1652017-03-22 18:24:32 -0400788 sk_SSL_CIPHER_free(client_pref);
David Benjamin2578b292016-12-03 23:19:55 -0500789 return ret;
790}
791
David Benjamin59bae5a2017-02-02 23:51:22 -0500792static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900793 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500794 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
795 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
David Benjamin731058e2016-12-03 23:15:13 -0500798 SSL_CLIENT_HELLO client_hello;
799 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
800 ssl->init_num)) {
David Benjamin3570d732015-06-29 00:28:17 -0400801 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin59bae5a2017-02-02 23:51:22 -0500802 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
803 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 }
Adam Langley95c29f32014-06-20 12:00:00 -0700805
David Benjamin59bae5a2017-02-02 23:51:22 -0500806 /* Run the early callback. */
807 if (ssl->ctx->select_certificate_cb != NULL) {
808 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000809 case ssl_select_cert_retry:
David Benjamin59bae5a2017-02-02 23:51:22 -0500810 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
811 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400812
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000813 case ssl_select_cert_error:
David Benjamin59bae5a2017-02-02 23:51:22 -0500814 /* Connection rejected. */
815 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
816 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
817 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400818
David Benjamin59bae5a2017-02-02 23:51:22 -0500819 default:
820 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400821 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500822 }
David Benjamine14ff062016-08-09 16:21:24 -0400823
Adam Langley04540a72017-02-09 10:11:50 -0800824 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500825 if (!negotiate_version(hs, &alert, &client_hello)) {
826 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
827 return -1;
828 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400829
David Benjamin59bae5a2017-02-02 23:51:22 -0500830 /* Load the client random. */
831 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
832 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
833 return -1;
834 }
835 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
836 client_hello.random_len);
837
838 /* Only null compression is supported. TLS 1.3 further requires the peer
839 * advertise no other compression. */
840 if (OPENSSL_memchr(client_hello.compression_methods, 0,
841 client_hello.compression_methods_len) == NULL ||
842 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
843 client_hello.compression_methods_len != 1)) {
844 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
845 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
846 return -1;
847 }
848
849 /* TLS extensions. */
850 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
851 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
852 return -1;
853 }
854
855 return 1;
856}
857
858static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
859 SSL *const ssl = hs->ssl;
860 /* Call |cert_cb| to update server certificates if required. */
861 if (ssl->cert->cert_cb != NULL) {
862 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
863 if (rv == 0) {
864 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
865 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin25fe85b2016-08-09 20:00:32 -0400866 return -1;
867 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500868 if (rv < 0) {
869 ssl->rwstate = SSL_X509_LOOKUP;
870 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900871 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900872 }
873
David Benjamina232a712017-03-30 15:51:53 -0500874 if (!ssl_on_certificate_selected(hs)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500875 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900876 }
877
David Benjamin59bae5a2017-02-02 23:51:22 -0500878 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
879 /* Jump to the TLS 1.3 state machine. */
880 hs->state = SSL_ST_TLS13;
881 hs->do_tls13_handshake = tls13_server_handshake;
882 return 1;
883 }
884
885 SSL_CLIENT_HELLO client_hello;
886 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
887 ssl->init_num)) {
888 return -1;
889 }
890
891 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
892 * certificate is finalized. */
David Benjamin45738dd2017-02-09 20:01:26 -0500893 hs->new_cipher =
David Benjamin59bae5a2017-02-02 23:51:22 -0500894 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjamin45738dd2017-02-09 20:01:26 -0500895 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500896 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
897 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
898 return -1;
899 }
900
901 return 1;
902}
903
904static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
905 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500906 int ret = -1;
907 SSL_SESSION *session = NULL;
908
909 SSL_CLIENT_HELLO client_hello;
910 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
911 ssl->init_num)) {
912 return -1;
913 }
David Benjamin34202b92016-11-16 19:07:53 +0900914
915 /* Determine whether we are doing session resumption. */
916 int tickets_supported = 0, renew_ticket = 0;
917 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
918 &client_hello)) {
919 case ssl_session_success:
920 break;
921 case ssl_session_error:
922 goto err;
923 case ssl_session_retry:
924 ssl->rwstate = SSL_PENDING_SESSION;
925 goto err;
Adam Langley4c341d02017-03-08 19:33:21 -0800926 case ssl_session_ticket_retry:
927 ssl->rwstate = SSL_PENDING_TICKET;
928 goto err;
David Benjamin34202b92016-11-16 19:07:53 +0900929 }
930
931 if (session != NULL) {
David Benjaminfc02b592017-02-17 16:26:01 -0500932 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900933 /* A ClientHello without EMS that attempts to resume a session with EMS
934 * is fatal to the connection. */
David Benjamin34202b92016-11-16 19:07:53 +0900935 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400936 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
937 goto err;
David Benjamin34202b92016-11-16 19:07:53 +0900938 }
939
David Benjamin45738dd2017-02-09 20:01:26 -0500940 if (!ssl_session_is_resumable(hs, session) ||
David Benjamin34202b92016-11-16 19:07:53 +0900941 /* If the client offers the EMS extension, but the previous session
942 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500943 hs->extended_master_secret != session->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900944 SSL_SESSION_free(session);
945 session = NULL;
946 }
947 }
948
949 if (session != NULL) {
950 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900951 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900952 ssl->session = session;
953 session = NULL;
954 ssl->s3->session_reused = 1;
955 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900956 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900957 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900958 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900959 goto err;
960 }
961
962 /* Clear the session ID if we want the session to be single-use. */
963 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500964 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900965 }
966 }
967
968 if (ssl->ctx->dos_protection_cb != NULL &&
969 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
970 /* Connection rejected for DOS reasons. */
David Benjamin34202b92016-11-16 19:07:53 +0900971 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400972 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
973 goto err;
David Benjamin34202b92016-11-16 19:07:53 +0900974 }
975
David Benjaminf01f42a2016-11-16 19:05:33 +0900976 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500977 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400978
David Benjamin4eb95cc2016-11-16 17:08:23 +0900979 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900980 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500981 OPENSSL_free(hs->new_session->tlsext_hostname);
982 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
983 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamind98107b2017-06-15 22:56:37 -0400984 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
985 goto err;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900986 }
987 }
988
David Benjamin5c1ce292015-05-26 16:59:43 -0400989 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900990 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400991 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500992 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
993 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900994 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400995 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400996 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500997 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900998 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400999 }
Adam Langley37646832016-08-01 16:16:46 -07001000
David Benjaminc3c88822016-11-14 10:32:04 +09001001 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001002 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001003 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001004 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -07001005 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 }
1007
David Benjaminf01f42a2016-11-16 19:05:33 +09001008 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1009 * deferred. Complete it now. */
David Benjamind98107b2017-06-15 22:56:37 -04001010 uint8_t alert = SSL_AD_DECODE_ERROR;
1011 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
1012 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1013 goto err;
David Benjamin9ef31f02016-10-31 18:01:13 -04001014 }
1015
David Benjaminf71036e2017-01-21 14:49:39 -05001016 /* Now that all parameters are known, initialize the handshake hash and hash
1017 * the ClientHello. */
Steven Valdez908ac192017-01-12 13:17:07 -05001018 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
David Benjamin45738dd2017-02-09 20:01:26 -05001019 hs->new_cipher->algorithm_prf) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001020 !ssl_hash_current_message(hs)) {
David Benjamind98107b2017-06-15 22:56:37 -04001021 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1022 goto err;
David Benjamin9550c3a2015-08-05 08:50:34 -04001023 }
1024
Steven Valdez2b8415e2016-06-30 13:27:23 -04001025 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001026 if (!hs->cert_request) {
Steven Valdez908ac192017-01-12 13:17:07 -05001027 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001028 }
1029
David Benjamin0bd71eb2015-12-01 14:14:40 -05001030 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001031
Adam Langley95c29f32014-06-20 12:00:00 -07001032err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001033 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001034 return ret;
1035}
Adam Langley95c29f32014-06-20 12:00:00 -07001036
David Benjaminc3c88822016-11-14 10:32:04 +09001037static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1038 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04001039
1040 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1041 * known attack while we fix ChannelID itself. */
1042 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -05001043 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001044 ssl->s3->tlsext_channel_id_valid = 0;
1045 }
1046
1047 /* If this is a resumption and the original handshake didn't support
1048 * ChannelID then we didn't record the original handshake hashes in the
1049 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001050 if (ssl->session != NULL &&
1051 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001052 ssl->s3->tlsext_channel_id_valid = 0;
1053 }
1054
David Benjaminad8f5e12017-02-20 17:00:20 -05001055 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -04001056 ssl_get_current_time(ssl, &now);
1057 ssl->s3->server_random[0] = now.tv_sec >> 24;
1058 ssl->s3->server_random[1] = now.tv_sec >> 16;
1059 ssl->s3->server_random[2] = now.tv_sec >> 8;
1060 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001061 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001062 return -1;
1063 }
1064
David Benjamin55108632016-08-11 22:01:18 -04001065 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1066 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001067
David Benjamin45738dd2017-02-09 20:01:26 -05001068 const SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001069 if (ssl->session != NULL) {
1070 session = ssl->session;
1071 }
1072
David Benjamin75836432016-06-17 18:48:29 -04001073 CBB cbb, body, session_id;
1074 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1075 !CBB_add_u16(&body, ssl->version) ||
1076 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1077 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001078 !CBB_add_bytes(&session_id, session->session_id,
1079 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -05001080 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -04001081 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001082 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001083 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001084 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1085 CBB_cleanup(&cbb);
1086 return -1;
1087 }
1088
David Benjamin16315f72017-01-12 20:02:05 -05001089 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001090}
Adam Langley95c29f32014-06-20 12:00:00 -07001091
David Benjaminc3c88822016-11-14 10:32:04 +09001092static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1093 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001094 if (!ssl_has_certificate(ssl)) {
1095 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin16315f72017-01-12 20:02:05 -05001096 return -1;
David Benjamin32a66d52016-07-13 22:03:11 -04001097 }
1098
David Benjamin75836432016-06-17 18:48:29 -04001099 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin16315f72017-01-12 20:02:05 -05001100 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001101 }
David Benjamin16315f72017-01-12 20:02:05 -05001102 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001103}
1104
David Benjaminc3c88822016-11-14 10:32:04 +09001105static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1106 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001107 CBB cbb, body, ocsp_response;
1108 if (!ssl->method->init_message(ssl, &cbb, &body,
1109 SSL3_MT_CERTIFICATE_STATUS) ||
1110 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1111 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
David Benjamin83a32122017-02-14 18:34:54 -05001112 !CBB_add_bytes(&ocsp_response,
1113 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
1114 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001115 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001116 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1117 CBB_cleanup(&cbb);
1118 return -1;
1119 }
1120
David Benjamin16315f72017-01-12 20:02:05 -05001121 return 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001122}
1123
David Benjaminc3c88822016-11-14 10:32:04 +09001124static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1125 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -05001126 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001127 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001128
David Benjaminc42acee2016-06-17 17:47:58 -04001129 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001130 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin45738dd2017-02-09 20:01:26 -05001131 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1132 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001133
David Benjaminc42acee2016-06-17 17:47:58 -04001134 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1135 if (!CBB_init(&cbb, 128)) {
1136 goto err;
1137 }
1138
David Benjamin2a0b3912015-12-18 01:01:21 -05001139 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001141 size_t len =
1142 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1143 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1144 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1145 len)) {
1146 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001147 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001148 }
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001150 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001151 /* Determine the group to use. */
1152 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001153 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001154 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001155 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001156 goto err;
1157 }
David Benjamin45738dd2017-02-09 20:01:26 -05001158 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001159
David Benjamin4298d772015-12-19 00:18:25 -05001160 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001161 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001162 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001163 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001164 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001165 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001166 goto err;
1167 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001168 } else {
1169 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001170 }
Adam Langley95c29f32014-06-20 12:00:00 -07001171
David Benjaminc3c88822016-11-14 10:32:04 +09001172 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001173 goto err;
1174 }
David Benjaminc42acee2016-06-17 17:47:58 -04001175 }
1176
1177 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001178 CBB body;
1179 if (!ssl->method->init_message(ssl, &cbb, &body,
1180 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001181 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001182 goto err;
1183 }
Adam Langley95c29f32014-06-20 12:00:00 -07001184
David Benjamin2a0b3912015-12-18 01:01:21 -05001185 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -05001186 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001187 if (!ssl_has_private_key(ssl)) {
1188 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001189 goto err;
1190 }
Adam Langley95c29f32014-06-20 12:00:00 -07001191
David Benjaminc42acee2016-06-17 17:47:58 -04001192 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001193 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001194 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001195 goto err;
1196 }
David Benjaminc42acee2016-06-17 17:47:58 -04001197 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001198 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001199 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1200 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1201 goto err;
1202 }
David Benjaminc42acee2016-06-17 17:47:58 -04001203 }
1204
1205 /* Add space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001206 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjaminc42acee2016-06-17 17:47:58 -04001207 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001208 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001209 !CBB_reserve(&child, &ptr, max_sig_len)) {
1210 goto err;
1211 }
1212
David Benjamin2a0b3912015-12-18 01:01:21 -05001213 size_t sig_len;
1214 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001215 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001216 CBB transcript;
1217 uint8_t *transcript_data;
1218 size_t transcript_len;
1219 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001220 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1221 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1222 SSL3_RANDOM_SIZE) ||
1223 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1224 SSL3_RANDOM_SIZE) ||
1225 !CBB_add_bytes(&transcript, hs->server_params,
1226 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001227 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1228 CBB_cleanup(&transcript);
1229 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1230 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001231 goto err;
1232 }
1233
Steven Valdezf0451ca2016-06-29 13:16:27 -04001234 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001235 signature_algorithm, transcript_data,
1236 transcript_len);
1237 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001238 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001239 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001240 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001241 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001242
1243 switch (sign_result) {
1244 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001245 if (!CBB_did_write(&child, sig_len)) {
1246 goto err;
1247 }
1248 break;
1249 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001250 goto err;
1251 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001252 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001253 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001254 goto err;
1255 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001256 }
1257
David Benjamindaf207a2017-01-03 18:37:41 -05001258 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001259 goto err;
1260 }
David Benjaminc42acee2016-06-17 17:47:58 -04001261
David Benjaminc3c88822016-11-14 10:32:04 +09001262 OPENSSL_free(hs->server_params);
1263 hs->server_params = NULL;
1264 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001265
David Benjamin16315f72017-01-12 20:02:05 -05001266 return 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001267
Adam Langley95c29f32014-06-20 12:00:00 -07001268err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001269 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001270 return -1;
1271}
Adam Langley95c29f32014-06-20 12:00:00 -07001272
David Benjaminc3c88822016-11-14 10:32:04 +09001273static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1274 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001275 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001276 if (!ssl->method->init_message(ssl, &cbb, &body,
1277 SSL3_MT_CERTIFICATE_REQUEST) ||
1278 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
David Benjamincc17c242017-03-30 22:53:03 -05001279 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
1280 (ssl->version >= TLS1_VERSION &&
1281 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN))) {
David Benjamin75836432016-06-17 18:48:29 -04001282 goto err;
1283 }
1284
1285 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjamin69522112017-03-28 15:38:29 -05001286 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
1287 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001288 goto err;
1289 }
1290 }
1291
David Benjamin32a66d52016-07-13 22:03:11 -04001292 if (!ssl_add_client_CA_list(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001293 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001294 goto err;
1295 }
1296
David Benjamin16315f72017-01-12 20:02:05 -05001297 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001298
Adam Langley95c29f32014-06-20 12:00:00 -07001299err:
David Benjamin75836432016-06-17 18:48:29 -04001300 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1301 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 return -1;
1303}
Adam Langley95c29f32014-06-20 12:00:00 -07001304
David Benjaminc3c88822016-11-14 10:32:04 +09001305static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1306 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001307 CBB cbb, body;
1308 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001309 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001310 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1311 CBB_cleanup(&cbb);
1312 return -1;
1313 }
1314
David Benjamin16315f72017-01-12 20:02:05 -05001315 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001316}
1317
David Benjaminc3c88822016-11-14 10:32:04 +09001318static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1319 SSL *const ssl = hs->ssl;
1320 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001321
David Benjaminf71036e2017-01-21 14:49:39 -05001322 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001323 if (msg_ret <= 0) {
1324 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001325 }
1326
1327 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1328 if (ssl->version == SSL3_VERSION &&
1329 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001330 /* In SSL 3.0, the Certificate message is omitted to signal no
1331 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001332 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001333 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001334 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1335 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001336 }
1337
Adam Langley37646832016-08-01 16:16:46 -07001338 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001339 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001340 hs->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001341 ssl->s3->tmp.reuse_message = 1;
1342 return 1;
1343 }
1344
David Benjamin9f1dc822016-06-07 17:03:46 -04001345 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001346 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1347 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001348 }
1349
Steven Valdez908ac192017-01-12 13:17:07 -05001350 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001351 return -1;
1352 }
1353
David Benjamin5c900c82016-07-13 23:03:26 -04001354 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001355 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001356
David Benjamin45738dd2017-02-09 20:01:26 -05001357 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001358 EVP_PKEY_free(hs->peer_pubkey);
1359 hs->peer_pubkey = NULL;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001360 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001361 hs->new_session->certs = ssl_parse_cert_chain(
1362 &alert, &hs->peer_pubkey,
1363 ssl->retain_only_sha256_of_client_certs ? hs->new_session->peer_sha256
1364 : NULL,
1365 &certificate_msg, ssl->ctx->pool);
1366 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001367 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001368 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001369 }
1370
Adam Langley68e71242016-12-12 11:06:16 -08001371 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001372 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001373 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001374 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001375 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001376 }
1377
David Benjamin45738dd2017-02-09 20:01:26 -05001378 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001379 /* No client certificate so the handshake buffer may be discarded. */
Steven Valdez908ac192017-01-12 13:17:07 -05001380 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin9f1dc822016-06-07 17:03:46 -04001381
David Benjamine455e512016-08-01 20:11:13 -04001382 /* In SSL 3.0, sending no certificate is signaled by omitting the
1383 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001384 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001385 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001386 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001387 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001388 }
1389
1390 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001391 /* Fail for TLS only if we required a certificate */
1392 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001393 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001394 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001395 }
Adam Langley37646832016-08-01 16:16:46 -07001396
1397 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001398 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001399 hs->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001400 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001401 }
1402
Adam Langley68e71242016-12-12 11:06:16 -08001403 /* The hash will have been filled in. */
1404 if (ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -05001405 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001406 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001407
Adam Langley2a3b3432017-02-10 13:56:48 -08001408 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
Adam Langley68e71242016-12-12 11:06:16 -08001409 return -1;
1410 }
Adam Langley2a3b3432017-02-10 13:56:48 -08001411
David Benjamin5c900c82016-07-13 23:03:26 -04001412 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001413}
1414
David Benjaminc3c88822016-11-14 10:32:04 +09001415static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1416 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001417 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001418 uint32_t alg_k;
1419 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 uint8_t *premaster_secret = NULL;
1421 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001422 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001423
David Benjamin4298d772015-12-19 00:18:25 -05001424 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001425 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001426
David Benjamincb0c29f2016-12-12 17:00:50 -05001427 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminf71036e2017-01-21 14:49:39 -05001428 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001429 if (ret <= 0) {
1430 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001431 }
David Benjamin276b7e82017-01-21 14:13:39 -05001432
David Benjaminf71036e2017-01-21 14:49:39 -05001433 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001434 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001435 return -1;
1436 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001437 }
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin0d56f882015-12-19 17:05:56 -05001439 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin45738dd2017-02-09 20:01:26 -05001440 alg_k = hs->new_cipher->algorithm_mkey;
1441 alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001442
Adam Langleyfcf25832014-12-18 17:42:32 -08001443 /* If using a PSK key exchange, prepare the pre-shared key. */
1444 if (alg_a & SSL_aPSK) {
1445 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1448 * then this is the only field in the message. */
1449 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1450 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001451 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001452 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1453 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001454 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001455
David Benjamin0d56f882015-12-19 17:05:56 -05001456 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001457 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
David Benjamind98107b2017-06-15 22:56:37 -04001458 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1459 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001460 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001461
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1463 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001464 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001465 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1466 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001467 }
David Benjamin35c02302014-07-13 04:14:59 -04001468
David Benjamin45738dd2017-02-09 20:01:26 -05001469 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001470 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001471 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1472 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001473 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001474
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 /* Look up the key for the identity. */
David Benjamin45738dd2017-02-09 20:01:26 -05001476 psk_len = ssl->psk_server_callback(ssl, hs->new_session->psk_identity, psk,
1477 sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001479 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001480 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1481 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 } else if (psk_len == 0) {
1483 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001484 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
David Benjamind98107b2017-06-15 22:56:37 -04001485 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1486 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 }
1488 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 /* Depending on the key exchange method, compute |premaster_secret| and
1491 * |premaster_secret_len|. */
1492 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001493 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamina232a712017-03-30 15:51:53 -05001494 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 decrypt_buf = OPENSSL_malloc(rsa_size);
1496 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001497 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001498 goto err;
1499 }
David Benjamin35c02302014-07-13 04:14:59 -04001500
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001501 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001502 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001503 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001504 if (!ssl_has_private_key(ssl) ||
David Benjamina232a712017-03-30 15:51:53 -05001505 EVP_PKEY_id(hs->local_pubkey) != EVP_PKEY_RSA) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001506 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
David Benjamind98107b2017-06-15 22:56:37 -04001507 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1508 goto err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001509 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001510 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001511 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001512 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1513 &encrypted_premaster_secret) ||
1514 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001515 OPENSSL_PUT_ERROR(SSL,
1516 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
David Benjamind98107b2017-06-15 22:56:37 -04001517 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1518 goto err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001519 }
1520 } else {
1521 encrypted_premaster_secret = client_key_exchange;
1522 }
1523
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001524 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1525 * timing-sensitive code below. */
1526 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001527 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001528 CBS_data(&encrypted_premaster_secret),
1529 CBS_len(&encrypted_premaster_secret));
1530 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001531 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001532 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001533 decrypt_result =
1534 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001535 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001536
1537 switch (decrypt_result) {
1538 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001539 break;
1540 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001541 goto err;
1542 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001543 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001544 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001545 goto err;
1546 }
1547
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001548 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001549 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001550 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1551 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001552 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001553
David Benjamin3f5b43d2015-12-01 19:31:24 -05001554 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1555 * section 7.4.7.1. */
1556 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1557 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001559 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 goto err;
1561 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001562 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001563 goto err;
1564 }
1565
David Benjamin3f5b43d2015-12-01 19:31:24 -05001566 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1567 * publicly invalid. */
1568 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001569 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001570 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1571 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001572 }
David Benjamin35c02302014-07-13 04:14:59 -04001573
David Benjamin3f5b43d2015-12-01 19:31:24 -05001574 /* Check the padding. See RFC 3447, section 7.2.2. */
1575 size_t padding_len = decrypt_len - premaster_secret_len;
1576 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1577 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001578 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001579 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1580 }
1581 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1582
1583 /* The premaster secret must begin with |client_version|. This too must be
1584 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1585 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001586 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001587 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001588 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001589
1590 /* Select, in constant time, either the decrypted premaster or the random
1591 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001592 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001593 premaster_secret[i] = constant_time_select_8(
1594 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1595 }
1596
1597 OPENSSL_free(decrypt_buf);
1598 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001599 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001600 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001601 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001602 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001603 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001604 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001605 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1606 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001607 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001608
David Benjamin4298d772015-12-19 00:18:25 -05001609 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001610 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001611 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001612 &premaster_secret_len, &alert, CBS_data(&peer_key),
1613 CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001614 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1615 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001616 }
Adam Langley95c29f32014-06-20 12:00:00 -07001617
David Benjamin4298d772015-12-19 00:18:25 -05001618 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001619 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 } else if (alg_k & SSL_kPSK) {
1621 /* For plain PSK, other_secret is a block of 0s with the same length as the
1622 * pre-shared key. */
1623 premaster_secret_len = psk_len;
1624 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1625 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001626 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 goto err;
1628 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001629 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001630 } else {
David Benjamin3570d732015-06-29 00:28:17 -04001631 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001632 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1633 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 }
Adam Langley95c29f32014-06-20 12:00:00 -07001635
Adam Langleyfcf25832014-12-18 17:42:32 -08001636 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1637 * pre-shared key. */
1638 if (alg_a & SSL_aPSK) {
1639 CBB new_premaster, child;
1640 uint8_t *new_data;
1641 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001642
David Benjamina8653202015-06-28 01:26:10 -04001643 CBB_zero(&new_premaster);
1644 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1645 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001646 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1647 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1648 !CBB_add_bytes(&child, psk, psk_len) ||
1649 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001650 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 CBB_cleanup(&new_premaster);
1652 goto err;
1653 }
David Benjamin14c83e72014-07-13 04:54:57 -04001654
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1656 OPENSSL_free(premaster_secret);
1657 premaster_secret = new_data;
1658 premaster_secret_len = new_len;
1659 }
David Benjamin14c83e72014-07-13 04:54:57 -04001660
Adam Langleyfcf25832014-12-18 17:42:32 -08001661 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001662 hs->new_session->master_key_length = tls1_generate_master_secret(
1663 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1664 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 goto err;
1666 }
David Benjaminfc02b592017-02-17 16:26:01 -05001667 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001668
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1670 OPENSSL_free(premaster_secret);
1671 return 1;
1672
Adam Langley95c29f32014-06-20 12:00:00 -07001673err:
David Benjamin4298d772015-12-19 00:18:25 -05001674 if (premaster_secret != NULL) {
1675 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001676 OPENSSL_free(premaster_secret);
1677 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001678 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001679
Adam Langleyfcf25832014-12-18 17:42:32 -08001680 return -1;
1681}
Adam Langley95c29f32014-06-20 12:00:00 -07001682
David Benjaminc3c88822016-11-14 10:32:04 +09001683static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1684 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001685 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001686
Adam Langleyfcf25832014-12-18 17:42:32 -08001687 /* Only RSA and ECDSA client certificates are supported, so a
1688 * CertificateVerify is required if and only if there's a client certificate.
1689 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001690 if (hs->peer_pubkey == NULL) {
Steven Valdez908ac192017-01-12 13:17:07 -05001691 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 return 1;
1693 }
Adam Langley95c29f32014-06-20 12:00:00 -07001694
David Benjaminf71036e2017-01-21 14:49:39 -05001695 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001696 if (msg_ret <= 0) {
1697 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001698 }
David Benjaminef865502014-08-24 02:48:34 -04001699
David Benjamin276b7e82017-01-21 14:13:39 -05001700 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1701 return -1;
1702 }
1703
David Benjamin09eb6552016-07-08 14:32:11 -07001704 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001705
Adam Langleyfcf25832014-12-18 17:42:32 -08001706 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001707 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001708 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001709 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001710 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001711 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1712 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001713 }
David Benjamin8d606e32017-06-15 22:43:04 -04001714 uint8_t alert = SSL_AD_DECODE_ERROR;
1715 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001716 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1717 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001718 }
David Benjamin45738dd2017-02-09 20:01:26 -05001719 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001720 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1721 hs->peer_pubkey)) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001722 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001723 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1724 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001725 }
David Benjamin854dd652014-08-26 00:32:30 -04001726
Adam Langleyfcf25832014-12-18 17:42:32 -08001727 /* Parse and verify the signature. */
1728 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1729 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001730 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001731 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1732 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001733 }
Adam Langley95c29f32014-06-20 12:00:00 -07001734
Steven Valdez2b8415e2016-06-30 13:27:23 -04001735 int sig_ok;
1736 /* The SSL3 construction for CertificateVerify does not decompose into a
1737 * single final digest and signature, and must be special-cased. */
1738 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001739 uint8_t digest[EVP_MAX_MD_SIZE];
1740 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05001741 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
David Benjamin45738dd2017-02-09 20:01:26 -05001742 &digest_len, hs->new_session,
Steven Valdez908ac192017-01-12 13:17:07 -05001743 signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001744 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001745 }
1746
Adam Langleyd5157222016-12-12 11:37:43 -08001747 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001748 sig_ok = pctx != NULL &&
1749 EVP_PKEY_verify_init(pctx) &&
Steven Valdez2b8415e2016-06-30 13:27:23 -04001750 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1751 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001752 EVP_PKEY_CTX_free(pctx);
1753 } else {
1754 sig_ok = ssl_public_key_verify(
1755 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001756 hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data,
1757 hs->transcript.buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001758 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001759
David Benjaminbf82aed2016-03-01 22:57:40 -05001760#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1761 sig_ok = 1;
1762 ERR_clear_error();
1763#endif
1764 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001765 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001766 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1767 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001768 }
1769
Steven Valdez2b8415e2016-06-30 13:27:23 -04001770 /* The handshake buffer is no longer necessary, and we may hash the current
1771 * message.*/
Steven Valdez908ac192017-01-12 13:17:07 -05001772 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
1773 if (!ssl_hash_current_message(hs)) {
David Benjamind98107b2017-06-15 22:56:37 -04001774 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001775 }
1776
Adam Langleyd5157222016-12-12 11:37:43 -08001777 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001778}
Adam Langley95c29f32014-06-20 12:00:00 -07001779
Adam Langley95c29f32014-06-20 12:00:00 -07001780/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1781 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001782static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1783 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001784 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001785 if (ret <= 0) {
1786 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001787 }
Adam Langley95c29f32014-06-20 12:00:00 -07001788
David Benjaminf71036e2017-01-21 14:49:39 -05001789 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001790 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001791 return -1;
1792 }
1793
David Benjamin09eb6552016-07-08 14:32:11 -07001794 CBS next_protocol, selected_protocol, padding;
1795 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001796 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1797 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001798 CBS_len(&next_protocol) != 0) {
1799 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1800 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1801 return 0;
1802 }
1803
1804 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001805 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 return 0;
1807 }
Adam Langley95c29f32014-06-20 12:00:00 -07001808
Adam Langleyfcf25832014-12-18 17:42:32 -08001809 return 1;
1810}
Adam Langley95c29f32014-06-20 12:00:00 -07001811
Adam Langley1258b6a2014-06-20 12:00:00 -07001812/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001813static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1814 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001815 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001816 if (msg_ret <= 0) {
1817 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001818 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001819
David Benjamin276b7e82017-01-21 14:13:39 -05001820 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001821 !tls1_verify_channel_id(hs) ||
1822 !ssl_hash_current_message(hs)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001823 return -1;
1824 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001825 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001826}
David Benjamin9f1dc822016-06-07 17:03:46 -04001827
David Benjaminc3c88822016-11-14 10:32:04 +09001828static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1829 SSL *const ssl = hs->ssl;
David Benjamin123db572016-11-03 16:59:25 -04001830 const SSL_SESSION *session;
1831 SSL_SESSION *session_copy = NULL;
1832 if (ssl->session == NULL) {
1833 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin45738dd2017-02-09 20:01:26 -05001834 ssl_session_rebase_time(ssl, hs->new_session);
1835 session = hs->new_session;
David Benjamin123db572016-11-03 16:59:25 -04001836 } else {
1837 /* We are renewing an existing session. Duplicate the session to adjust the
1838 * timeout. */
1839 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1840 if (session_copy == NULL) {
1841 return -1;
1842 }
1843
David Benjamin17b30832017-01-28 14:00:32 -05001844 ssl_session_rebase_time(ssl, session_copy);
David Benjamin123db572016-11-03 16:59:25 -04001845 session = session_copy;
1846 }
1847
David Benjamin75836432016-06-17 18:48:29 -04001848 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001849 int ok =
1850 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1851 CBB_add_u32(&body, session->timeout) &&
1852 CBB_add_u16_length_prefixed(&body, &ticket) &&
1853 ssl_encrypt_ticket(ssl, &ticket, session) &&
David Benjamindaf207a2017-01-03 18:37:41 -05001854 ssl_add_message_cbb(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001855
1856 SSL_SESSION_free(session_copy);
1857 CBB_cleanup(&cbb);
1858
1859 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001860 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001861 }
1862
David Benjamin16315f72017-01-12 20:02:05 -05001863 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001864}