blob: 4eaf3cb0603c93ce30eb6c871b96c0e2bb8f7679 [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;
906 uint8_t al = SSL_AD_INTERNAL_ERROR;
907 int ret = -1;
908 SSL_SESSION *session = NULL;
909
910 SSL_CLIENT_HELLO client_hello;
911 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
912 ssl->init_num)) {
913 return -1;
914 }
David Benjamin34202b92016-11-16 19:07:53 +0900915
916 /* Determine whether we are doing session resumption. */
917 int tickets_supported = 0, renew_ticket = 0;
918 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
919 &client_hello)) {
920 case ssl_session_success:
921 break;
922 case ssl_session_error:
923 goto err;
924 case ssl_session_retry:
925 ssl->rwstate = SSL_PENDING_SESSION;
926 goto err;
Adam Langley4c341d02017-03-08 19:33:21 -0800927 case ssl_session_ticket_retry:
928 ssl->rwstate = SSL_PENDING_TICKET;
929 goto err;
David Benjamin34202b92016-11-16 19:07:53 +0900930 }
931
932 if (session != NULL) {
David Benjaminfc02b592017-02-17 16:26:01 -0500933 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900934 /* A ClientHello without EMS that attempts to resume a session with EMS
935 * is fatal to the connection. */
936 al = SSL_AD_HANDSHAKE_FAILURE;
937 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
938 goto f_err;
939 }
940
David Benjamin45738dd2017-02-09 20:01:26 -0500941 if (!ssl_session_is_resumable(hs, session) ||
David Benjamin34202b92016-11-16 19:07:53 +0900942 /* If the client offers the EMS extension, but the previous session
943 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500944 hs->extended_master_secret != session->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900945 SSL_SESSION_free(session);
946 session = NULL;
947 }
948 }
949
950 if (session != NULL) {
951 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900952 hs->ticket_expected = renew_ticket;
David Benjamin34202b92016-11-16 19:07:53 +0900953 ssl->session = session;
954 session = NULL;
955 ssl->s3->session_reused = 1;
956 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900957 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900958 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900959 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamin34202b92016-11-16 19:07:53 +0900960 goto err;
961 }
962
963 /* Clear the session ID if we want the session to be single-use. */
964 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500965 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900966 }
967 }
968
969 if (ssl->ctx->dos_protection_cb != NULL &&
970 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
971 /* Connection rejected for DOS reasons. */
972 al = SSL_AD_INTERNAL_ERROR;
973 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
974 goto f_err;
975 }
976
David Benjaminf01f42a2016-11-16 19:05:33 +0900977 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500978 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400979
David Benjamin4eb95cc2016-11-16 17:08:23 +0900980 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900981 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500982 OPENSSL_free(hs->new_session->tlsext_hostname);
983 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
984 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamin4eb95cc2016-11-16 17:08:23 +0900985 al = SSL_AD_INTERNAL_ERROR;
986 goto f_err;
987 }
988 }
989
David Benjamin5c1ce292015-05-26 16:59:43 -0400990 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900991 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400992 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500993 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
994 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900995 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400996 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400997 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500998 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900999 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -04001000 }
Adam Langley37646832016-08-01 16:16:46 -07001001
David Benjaminc3c88822016-11-14 10:32:04 +09001002 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -07001003 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001004 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001005 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -07001006 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 }
1008
David Benjaminf01f42a2016-11-16 19:05:33 +09001009 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
1010 * deferred. Complete it now. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001011 if (!ssl_negotiate_alpn(hs, &al, &client_hello)) {
David Benjamin9ef31f02016-10-31 18:01:13 -04001012 goto f_err;
1013 }
1014
David Benjaminf71036e2017-01-21 14:49:39 -05001015 /* Now that all parameters are known, initialize the handshake hash and hash
1016 * the ClientHello. */
Steven Valdez908ac192017-01-12 13:17:07 -05001017 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
David Benjamin45738dd2017-02-09 20:01:26 -05001018 hs->new_cipher->algorithm_prf) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001019 !ssl_hash_current_message(hs)) {
David Benjamin9550c3a2015-08-05 08:50:34 -04001020 goto f_err;
1021 }
1022
Steven Valdez2b8415e2016-06-30 13:27:23 -04001023 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +09001024 if (!hs->cert_request) {
Steven Valdez908ac192017-01-12 13:17:07 -05001025 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 }
1027
David Benjamin0bd71eb2015-12-01 14:14:40 -05001028 ret = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001029
1030 if (0) {
1031 f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001032 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 }
1034
Adam Langley95c29f32014-06-20 12:00:00 -07001035err:
David Benjamine3aa1d92015-06-16 15:34:50 -04001036 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 return ret;
1038}
Adam Langley95c29f32014-06-20 12:00:00 -07001039
David Benjaminc3c88822016-11-14 10:32:04 +09001040static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
1041 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -04001042
1043 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
1044 * known attack while we fix ChannelID itself. */
1045 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -05001046 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001047 ssl->s3->tlsext_channel_id_valid = 0;
1048 }
1049
1050 /* If this is a resumption and the original handshake didn't support
1051 * ChannelID then we didn't record the original handshake hashes in the
1052 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -04001053 if (ssl->session != NULL &&
1054 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -04001055 ssl->s3->tlsext_channel_id_valid = 0;
1056 }
1057
David Benjaminad8f5e12017-02-20 17:00:20 -05001058 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -04001059 ssl_get_current_time(ssl, &now);
1060 ssl->s3->server_random[0] = now.tv_sec >> 24;
1061 ssl->s3->server_random[1] = now.tv_sec >> 16;
1062 ssl->s3->server_random[2] = now.tv_sec >> 8;
1063 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -04001064 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -04001065 return -1;
1066 }
1067
David Benjamin55108632016-08-11 22:01:18 -04001068 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
1069 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -04001070
David Benjamin45738dd2017-02-09 20:01:26 -05001071 const SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -04001072 if (ssl->session != NULL) {
1073 session = ssl->session;
1074 }
1075
David Benjamin75836432016-06-17 18:48:29 -04001076 CBB cbb, body, session_id;
1077 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
1078 !CBB_add_u16(&body, ssl->version) ||
1079 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1080 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -04001081 !CBB_add_bytes(&session_id, session->session_id,
1082 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -05001083 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -04001084 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -05001085 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001086 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -04001087 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1088 CBB_cleanup(&cbb);
1089 return -1;
1090 }
1091
David Benjamin16315f72017-01-12 20:02:05 -05001092 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001093}
Adam Langley95c29f32014-06-20 12:00:00 -07001094
David Benjaminc3c88822016-11-14 10:32:04 +09001095static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1096 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001097 if (!ssl_has_certificate(ssl)) {
1098 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamin16315f72017-01-12 20:02:05 -05001099 return -1;
David Benjamin32a66d52016-07-13 22:03:11 -04001100 }
1101
David Benjamin75836432016-06-17 18:48:29 -04001102 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin16315f72017-01-12 20:02:05 -05001103 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001104 }
David Benjamin16315f72017-01-12 20:02:05 -05001105 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001106}
1107
David Benjaminc3c88822016-11-14 10:32:04 +09001108static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) {
1109 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001110 CBB cbb, body, ocsp_response;
1111 if (!ssl->method->init_message(ssl, &cbb, &body,
1112 SSL3_MT_CERTIFICATE_STATUS) ||
1113 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1114 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
David Benjamin83a32122017-02-14 18:34:54 -05001115 !CBB_add_bytes(&ocsp_response,
1116 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
1117 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001118 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001119 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1120 CBB_cleanup(&cbb);
1121 return -1;
1122 }
1123
David Benjamin16315f72017-01-12 20:02:05 -05001124 return 1;
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001125}
1126
David Benjaminc3c88822016-11-14 10:32:04 +09001127static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1128 SSL *const ssl = hs->ssl;
David Benjamin2a0b3912015-12-18 01:01:21 -05001129 CBB cbb, child;
David Benjaminc42acee2016-06-17 17:47:58 -04001130 CBB_zero(&cbb);
nagendra modadugu601448a2015-07-24 09:31:31 -07001131
David Benjaminc42acee2016-06-17 17:47:58 -04001132 /* Put together the parameters. */
David Benjamincb0c29f2016-12-12 17:00:50 -05001133 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
David Benjamin45738dd2017-02-09 20:01:26 -05001134 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1135 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -07001136
David Benjaminc42acee2016-06-17 17:47:58 -04001137 /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
1138 if (!CBB_init(&cbb, 128)) {
1139 goto err;
1140 }
1141
David Benjamin2a0b3912015-12-18 01:01:21 -05001142 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001144 size_t len =
1145 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
1146 if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
1147 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1148 len)) {
1149 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 }
Adam Langley95c29f32014-06-20 12:00:00 -07001152
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001153 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001154 /* Determine the group to use. */
1155 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001156 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin3570d732015-06-29 00:28:17 -04001157 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
David Benjamin2a0b3912015-12-18 01:01:21 -05001158 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001159 goto err;
1160 }
David Benjamin45738dd2017-02-09 20:01:26 -05001161 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001162
David Benjamin4298d772015-12-19 00:18:25 -05001163 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001164 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001165 !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
Steven Valdezce902a92016-05-17 11:47:53 -04001166 !CBB_add_u16(&cbb, group_id) ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001167 !CBB_add_u8_length_prefixed(&cbb, &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001168 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
Matt Braithwaite053931e2016-05-25 12:06:05 -07001169 goto err;
1170 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001171 } else {
1172 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001173 }
Adam Langley95c29f32014-06-20 12:00:00 -07001174
David Benjaminc3c88822016-11-14 10:32:04 +09001175 if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001176 goto err;
1177 }
David Benjaminc42acee2016-06-17 17:47:58 -04001178 }
1179
1180 /* Assemble the message. */
David Benjamin75836432016-06-17 18:48:29 -04001181 CBB body;
1182 if (!ssl->method->init_message(ssl, &cbb, &body,
1183 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001184 !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001185 goto err;
1186 }
Adam Langley95c29f32014-06-20 12:00:00 -07001187
David Benjamin2a0b3912015-12-18 01:01:21 -05001188 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -05001189 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001190 if (!ssl_has_private_key(ssl)) {
1191 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001192 goto err;
1193 }
Adam Langley95c29f32014-06-20 12:00:00 -07001194
David Benjaminc42acee2016-06-17 17:47:58 -04001195 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001196 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001197 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001198 goto err;
1199 }
David Benjaminc42acee2016-06-17 17:47:58 -04001200 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001201 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001202 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1203 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1204 goto err;
1205 }
David Benjaminc42acee2016-06-17 17:47:58 -04001206 }
1207
1208 /* Add space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001209 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjaminc42acee2016-06-17 17:47:58 -04001210 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001211 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001212 !CBB_reserve(&child, &ptr, max_sig_len)) {
1213 goto err;
1214 }
1215
David Benjamin2a0b3912015-12-18 01:01:21 -05001216 size_t sig_len;
1217 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001218 if (hs->state == SSL3_ST_SW_KEY_EXCH_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001219 CBB transcript;
1220 uint8_t *transcript_data;
1221 size_t transcript_len;
1222 if (!CBB_init(&transcript,
David Benjaminc3c88822016-11-14 10:32:04 +09001223 2 * SSL3_RANDOM_SIZE + hs->server_params_len) ||
1224 !CBB_add_bytes(&transcript, ssl->s3->client_random,
1225 SSL3_RANDOM_SIZE) ||
1226 !CBB_add_bytes(&transcript, ssl->s3->server_random,
1227 SSL3_RANDOM_SIZE) ||
1228 !CBB_add_bytes(&transcript, hs->server_params,
1229 hs->server_params_len) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001230 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1231 CBB_cleanup(&transcript);
1232 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1233 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001234 goto err;
1235 }
1236
Steven Valdezf0451ca2016-06-29 13:16:27 -04001237 sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001238 signature_algorithm, transcript_data,
1239 transcript_len);
1240 OPENSSL_free(transcript_data);
David Benjamin1f9f9c42015-08-28 16:17:59 -04001241 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001242 assert(hs->state == SSL3_ST_SW_KEY_EXCH_B);
David Benjamind3440b42016-07-14 14:52:41 -04001243 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001244 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001245
1246 switch (sign_result) {
1247 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001248 if (!CBB_did_write(&child, sig_len)) {
1249 goto err;
1250 }
1251 break;
1252 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001253 goto err;
1254 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001255 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001256 hs->state = SSL3_ST_SW_KEY_EXCH_B;
David Benjamin2a0b3912015-12-18 01:01:21 -05001257 goto err;
1258 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001259 }
1260
David Benjamindaf207a2017-01-03 18:37:41 -05001261 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001262 goto err;
1263 }
David Benjaminc42acee2016-06-17 17:47:58 -04001264
David Benjaminc3c88822016-11-14 10:32:04 +09001265 OPENSSL_free(hs->server_params);
1266 hs->server_params = NULL;
1267 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001268
David Benjamin16315f72017-01-12 20:02:05 -05001269 return 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001270
Adam Langley95c29f32014-06-20 12:00:00 -07001271err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001272 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001273 return -1;
1274}
Adam Langley95c29f32014-06-20 12:00:00 -07001275
David Benjaminc3c88822016-11-14 10:32:04 +09001276static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) {
1277 SSL *const ssl = hs->ssl;
David Benjamin32a66d52016-07-13 22:03:11 -04001278 CBB cbb, body, cert_types, sigalgs_cbb;
David Benjamin75836432016-06-17 18:48:29 -04001279 if (!ssl->method->init_message(ssl, &cbb, &body,
1280 SSL3_MT_CERTIFICATE_REQUEST) ||
1281 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
David Benjamincc17c242017-03-30 22:53:03 -05001282 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
1283 (ssl->version >= TLS1_VERSION &&
1284 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN))) {
David Benjamin75836432016-06-17 18:48:29 -04001285 goto err;
1286 }
1287
1288 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjamin69522112017-03-28 15:38:29 -05001289 if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
1290 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001291 goto err;
1292 }
1293 }
1294
David Benjamin32a66d52016-07-13 22:03:11 -04001295 if (!ssl_add_client_CA_list(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001296 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001297 goto err;
1298 }
1299
David Benjamin16315f72017-01-12 20:02:05 -05001300 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001301
Adam Langley95c29f32014-06-20 12:00:00 -07001302err:
David Benjamin75836432016-06-17 18:48:29 -04001303 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1304 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 return -1;
1306}
Adam Langley95c29f32014-06-20 12:00:00 -07001307
David Benjaminc3c88822016-11-14 10:32:04 +09001308static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
1309 SSL *const ssl = hs->ssl;
David Benjamin75836432016-06-17 18:48:29 -04001310 CBB cbb, body;
1311 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001312 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001313 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1314 CBB_cleanup(&cbb);
1315 return -1;
1316 }
1317
David Benjamin16315f72017-01-12 20:02:05 -05001318 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001319}
1320
David Benjaminc3c88822016-11-14 10:32:04 +09001321static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1322 SSL *const ssl = hs->ssl;
1323 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001324
David Benjaminf71036e2017-01-21 14:49:39 -05001325 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001326 if (msg_ret <= 0) {
1327 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001328 }
1329
1330 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1331 if (ssl->version == SSL3_VERSION &&
1332 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001333 /* In SSL 3.0, the Certificate message is omitted to signal no
1334 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001335 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001336 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001337 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1338 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001339 }
1340
Adam Langley37646832016-08-01 16:16:46 -07001341 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001342 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001343 hs->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001344 ssl->s3->tmp.reuse_message = 1;
1345 return 1;
1346 }
1347
David Benjamin9f1dc822016-06-07 17:03:46 -04001348 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001349 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1350 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001351 }
1352
Steven Valdez908ac192017-01-12 13:17:07 -05001353 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001354 return -1;
1355 }
1356
David Benjamin5c900c82016-07-13 23:03:26 -04001357 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001358 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001359
David Benjamin45738dd2017-02-09 20:01:26 -05001360 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001361 EVP_PKEY_free(hs->peer_pubkey);
1362 hs->peer_pubkey = NULL;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001363 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001364 hs->new_session->certs = ssl_parse_cert_chain(
1365 &alert, &hs->peer_pubkey,
1366 ssl->retain_only_sha256_of_client_certs ? hs->new_session->peer_sha256
1367 : NULL,
1368 &certificate_msg, ssl->ctx->pool);
1369 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001370 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001371 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001372 }
1373
Adam Langley68e71242016-12-12 11:06:16 -08001374 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001375 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001376 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001377 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001378 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001379 }
1380
David Benjamin45738dd2017-02-09 20:01:26 -05001381 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001382 /* No client certificate so the handshake buffer may be discarded. */
Steven Valdez908ac192017-01-12 13:17:07 -05001383 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin9f1dc822016-06-07 17:03:46 -04001384
David Benjamine455e512016-08-01 20:11:13 -04001385 /* In SSL 3.0, sending no certificate is signaled by omitting the
1386 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001387 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001388 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001389 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001390 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001391 }
1392
1393 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001394 /* Fail for TLS only if we required a certificate */
1395 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001396 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001397 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001398 }
Adam Langley37646832016-08-01 16:16:46 -07001399
1400 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001401 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001402 hs->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001403 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001404 }
1405
Adam Langley68e71242016-12-12 11:06:16 -08001406 /* The hash will have been filled in. */
1407 if (ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -05001408 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001409 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001410
Adam Langley2a3b3432017-02-10 13:56:48 -08001411 if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) {
Adam Langley68e71242016-12-12 11:06:16 -08001412 return -1;
1413 }
Adam Langley2a3b3432017-02-10 13:56:48 -08001414
David Benjamin5c900c82016-07-13 23:03:26 -04001415 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001416}
1417
David Benjaminc3c88822016-11-14 10:32:04 +09001418static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1419 SSL *const ssl = hs->ssl;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001420 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001421 CBS client_key_exchange;
David Benjamin3fa27772015-04-17 22:32:19 -04001422 uint32_t alg_k;
1423 uint32_t alg_a;
Adam Langleyfcf25832014-12-18 17:42:32 -08001424 uint8_t *premaster_secret = NULL;
1425 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001426 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001427
David Benjamin4298d772015-12-19 00:18:25 -05001428 unsigned psk_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001429 uint8_t psk[PSK_MAX_PSK_LEN];
Adam Langley95c29f32014-06-20 12:00:00 -07001430
David Benjamincb0c29f2016-12-12 17:00:50 -05001431 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminf71036e2017-01-21 14:49:39 -05001432 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001433 if (ret <= 0) {
1434 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001435 }
David Benjamin276b7e82017-01-21 14:13:39 -05001436
David Benjaminf71036e2017-01-21 14:49:39 -05001437 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001438 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001439 return -1;
1440 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001441 }
Adam Langley95c29f32014-06-20 12:00:00 -07001442
David Benjamin0d56f882015-12-19 17:05:56 -05001443 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin45738dd2017-02-09 20:01:26 -05001444 alg_k = hs->new_cipher->algorithm_mkey;
1445 alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001446
Adam Langleyfcf25832014-12-18 17:42:32 -08001447 /* If using a PSK key exchange, prepare the pre-shared key. */
1448 if (alg_a & SSL_aPSK) {
1449 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001450
Adam Langleyfcf25832014-12-18 17:42:32 -08001451 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1452 * then this is the only field in the message. */
1453 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1454 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001455 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001456 al = SSL_AD_DECODE_ERROR;
1457 goto f_err;
1458 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001459
David Benjamin0d56f882015-12-19 17:05:56 -05001460 if (ssl->psk_server_callback == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001461 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
Adam Langleyfcf25832014-12-18 17:42:32 -08001462 al = SSL_AD_INTERNAL_ERROR;
1463 goto f_err;
1464 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001465
Adam Langleyfcf25832014-12-18 17:42:32 -08001466 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1467 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001468 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -08001469 al = SSL_AD_ILLEGAL_PARAMETER;
1470 goto f_err;
1471 }
David Benjamin35c02302014-07-13 04:14:59 -04001472
David Benjamin45738dd2017-02-09 20:01:26 -05001473 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001474 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001475 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001476 goto f_err;
1477 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001478
Adam Langleyfcf25832014-12-18 17:42:32 -08001479 /* Look up the key for the identity. */
David Benjamin45738dd2017-02-09 20:01:26 -05001480 psk_len = ssl->psk_server_callback(ssl, hs->new_session->psk_identity, psk,
1481 sizeof(psk));
Adam Langleyfcf25832014-12-18 17:42:32 -08001482 if (psk_len > PSK_MAX_PSK_LEN) {
David Benjamin3570d732015-06-29 00:28:17 -04001483 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001484 al = SSL_AD_INTERNAL_ERROR;
1485 goto f_err;
1486 } else if (psk_len == 0) {
1487 /* PSK related to the given identity not found */
David Benjamin3570d732015-06-29 00:28:17 -04001488 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
Adam Langleyfcf25832014-12-18 17:42:32 -08001489 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
1490 goto f_err;
1491 }
1492 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001493
Adam Langleyfcf25832014-12-18 17:42:32 -08001494 /* Depending on the key exchange method, compute |premaster_secret| and
1495 * |premaster_secret_len|. */
1496 if (alg_k & SSL_kRSA) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001497 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamina232a712017-03-30 15:51:53 -05001498 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey);
Adam Langleyfcf25832014-12-18 17:42:32 -08001499 decrypt_buf = OPENSSL_malloc(rsa_size);
1500 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001501 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001502 goto err;
1503 }
David Benjamin35c02302014-07-13 04:14:59 -04001504
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001505 enum ssl_private_key_result_t decrypt_result;
David Benjamin3f5b43d2015-12-01 19:31:24 -05001506 size_t decrypt_len;
David Benjamincb0c29f2016-12-12 17:00:50 -05001507 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjamin0d56f882015-12-19 17:05:56 -05001508 if (!ssl_has_private_key(ssl) ||
David Benjamina232a712017-03-30 15:51:53 -05001509 EVP_PKEY_id(hs->local_pubkey) != EVP_PKEY_RSA) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001510 al = SSL_AD_HANDSHAKE_FAILURE;
1511 OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
1512 goto f_err;
1513 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001514 CBS encrypted_premaster_secret;
David Benjamin0d56f882015-12-19 17:05:56 -05001515 if (ssl->version > SSL3_VERSION) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001516 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1517 &encrypted_premaster_secret) ||
1518 CBS_len(&client_key_exchange) != 0) {
David Benjaminef5e5152015-11-18 20:35:31 -05001519 al = SSL_AD_DECODE_ERROR;
1520 OPENSSL_PUT_ERROR(SSL,
1521 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1522 goto f_err;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001523 }
1524 } else {
1525 encrypted_premaster_secret = client_key_exchange;
1526 }
1527
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001528 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1529 * timing-sensitive code below. */
1530 decrypt_result = ssl_private_key_decrypt(
David Benjamin0d56f882015-12-19 17:05:56 -05001531 ssl, decrypt_buf, &decrypt_len, rsa_size,
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001532 CBS_data(&encrypted_premaster_secret),
1533 CBS_len(&encrypted_premaster_secret));
1534 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001535 assert(hs->state == SSL3_ST_SR_KEY_EXCH_B);
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001536 /* Complete async decrypt. */
David Benjamind3440b42016-07-14 14:52:41 -04001537 decrypt_result =
1538 ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 }
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001540
1541 switch (decrypt_result) {
1542 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001543 break;
1544 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001545 goto err;
1546 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001547 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001548 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001549 goto err;
1550 }
1551
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001552 if (decrypt_len != rsa_size) {
1553 al = SSL_AD_DECRYPT_ERROR;
1554 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1555 goto f_err;
1556 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001557
David Benjamin3f5b43d2015-12-01 19:31:24 -05001558 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1559 * section 7.4.7.1. */
1560 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
1561 premaster_secret = OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001562 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001563 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 goto err;
1565 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001566 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001567 goto err;
1568 }
1569
David Benjamin3f5b43d2015-12-01 19:31:24 -05001570 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1571 * publicly invalid. */
1572 if (decrypt_len < 11 + premaster_secret_len) {
1573 al = SSL_AD_DECRYPT_ERROR;
1574 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
1575 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001576 }
David Benjamin35c02302014-07-13 04:14:59 -04001577
David Benjamin3f5b43d2015-12-01 19:31:24 -05001578 /* Check the padding. See RFC 3447, section 7.2.2. */
1579 size_t padding_len = decrypt_len - premaster_secret_len;
1580 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1581 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001582 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001583 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1584 }
1585 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1586
1587 /* The premaster secret must begin with |client_version|. This too must be
1588 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1589 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001590 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001591 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001592 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001593
1594 /* Select, in constant time, either the decrypted premaster or the random
1595 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001596 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001597 premaster_secret[i] = constant_time_select_8(
1598 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1599 }
1600
1601 OPENSSL_free(decrypt_buf);
1602 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001603 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001604 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001605 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001606 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001607 CBS_len(&client_key_exchange) != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001608 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001609 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001610 goto f_err;
1611 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001612
David Benjamin4298d772015-12-19 00:18:25 -05001613 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001614 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001615 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001616 &premaster_secret_len, &alert, CBS_data(&peer_key),
1617 CBS_len(&peer_key))) {
David Benjamin4298d772015-12-19 00:18:25 -05001618 al = alert;
1619 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001620 }
Adam Langley95c29f32014-06-20 12:00:00 -07001621
David Benjamin4298d772015-12-19 00:18:25 -05001622 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001623 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -08001624 } else if (alg_k & SSL_kPSK) {
1625 /* For plain PSK, other_secret is a block of 0s with the same length as the
1626 * pre-shared key. */
1627 premaster_secret_len = psk_len;
1628 premaster_secret = OPENSSL_malloc(premaster_secret_len);
1629 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001630 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001631 goto err;
1632 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001633 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001634 } else {
1635 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001636 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 goto f_err;
1638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639
Adam Langleyfcf25832014-12-18 17:42:32 -08001640 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1641 * pre-shared key. */
1642 if (alg_a & SSL_aPSK) {
1643 CBB new_premaster, child;
1644 uint8_t *new_data;
1645 size_t new_len;
David Benjamin14c83e72014-07-13 04:54:57 -04001646
David Benjamina8653202015-06-28 01:26:10 -04001647 CBB_zero(&new_premaster);
1648 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1649 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1651 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1652 !CBB_add_bytes(&child, psk, psk_len) ||
1653 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001654 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001655 CBB_cleanup(&new_premaster);
1656 goto err;
1657 }
David Benjamin14c83e72014-07-13 04:54:57 -04001658
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1660 OPENSSL_free(premaster_secret);
1661 premaster_secret = new_data;
1662 premaster_secret_len = new_len;
1663 }
David Benjamin14c83e72014-07-13 04:54:57 -04001664
Adam Langleyfcf25832014-12-18 17:42:32 -08001665 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001666 hs->new_session->master_key_length = tls1_generate_master_secret(
1667 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1668 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001669 goto err;
1670 }
David Benjaminfc02b592017-02-17 16:26:01 -05001671 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001672
Adam Langleyfcf25832014-12-18 17:42:32 -08001673 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1674 OPENSSL_free(premaster_secret);
1675 return 1;
1676
Adam Langley95c29f32014-06-20 12:00:00 -07001677f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001678 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001679err:
David Benjamin4298d772015-12-19 00:18:25 -05001680 if (premaster_secret != NULL) {
1681 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001682 OPENSSL_free(premaster_secret);
1683 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001684 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001685
Adam Langleyfcf25832014-12-18 17:42:32 -08001686 return -1;
1687}
Adam Langley95c29f32014-06-20 12:00:00 -07001688
David Benjaminc3c88822016-11-14 10:32:04 +09001689static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1690 SSL *const ssl = hs->ssl;
Adam Langleyd5157222016-12-12 11:37:43 -08001691 int al;
Adam Langleyfcf25832014-12-18 17:42:32 -08001692 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001693
Adam Langleyfcf25832014-12-18 17:42:32 -08001694 /* Only RSA and ECDSA client certificates are supported, so a
1695 * CertificateVerify is required if and only if there's a client certificate.
1696 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001697 if (hs->peer_pubkey == NULL) {
Steven Valdez908ac192017-01-12 13:17:07 -05001698 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001699 return 1;
1700 }
Adam Langley95c29f32014-06-20 12:00:00 -07001701
David Benjaminf71036e2017-01-21 14:49:39 -05001702 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001703 if (msg_ret <= 0) {
1704 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001705 }
David Benjaminef865502014-08-24 02:48:34 -04001706
David Benjamin276b7e82017-01-21 14:13:39 -05001707 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1708 return -1;
1709 }
1710
David Benjamin09eb6552016-07-08 14:32:11 -07001711 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001712
Adam Langleyfcf25832014-12-18 17:42:32 -08001713 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001714 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001715 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001716 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001717 al = SSL_AD_DECODE_ERROR;
1718 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1719 goto f_err;
1720 }
David Benjamin887c3002016-07-08 16:15:32 -07001721 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001722 goto f_err;
1723 }
David Benjamin45738dd2017-02-09 20:01:26 -05001724 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001725 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1726 hs->peer_pubkey)) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001727 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1728 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1729 goto f_err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001730 }
David Benjamin854dd652014-08-26 00:32:30 -04001731
Adam Langleyfcf25832014-12-18 17:42:32 -08001732 /* Parse and verify the signature. */
1733 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1734 CBS_len(&certificate_verify) != 0) {
1735 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001736 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001737 goto f_err;
1738 }
Adam Langley95c29f32014-06-20 12:00:00 -07001739
Steven Valdez2b8415e2016-06-30 13:27:23 -04001740 int sig_ok;
1741 /* The SSL3 construction for CertificateVerify does not decompose into a
1742 * single final digest and signature, and must be special-cased. */
1743 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001744 uint8_t digest[EVP_MAX_MD_SIZE];
1745 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05001746 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
David Benjamin45738dd2017-02-09 20:01:26 -05001747 &digest_len, hs->new_session,
Steven Valdez908ac192017-01-12 13:17:07 -05001748 signature_algorithm)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001749 goto err;
1750 }
1751
Adam Langleyd5157222016-12-12 11:37:43 -08001752 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001753 sig_ok = pctx != NULL &&
1754 EVP_PKEY_verify_init(pctx) &&
Steven Valdez2b8415e2016-06-30 13:27:23 -04001755 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1756 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001757 EVP_PKEY_CTX_free(pctx);
1758 } else {
1759 sig_ok = ssl_public_key_verify(
1760 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001761 hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data,
1762 hs->transcript.buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001763 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001764
David Benjaminbf82aed2016-03-01 22:57:40 -05001765#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1766 sig_ok = 1;
1767 ERR_clear_error();
1768#endif
1769 if (!sig_ok) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001770 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001771 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001772 goto f_err;
1773 }
1774
Steven Valdez2b8415e2016-06-30 13:27:23 -04001775 /* The handshake buffer is no longer necessary, and we may hash the current
1776 * message.*/
Steven Valdez908ac192017-01-12 13:17:07 -05001777 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
1778 if (!ssl_hash_current_message(hs)) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001779 goto err;
1780 }
1781
Adam Langleyd5157222016-12-12 11:37:43 -08001782 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001783
Adam Langleyd5157222016-12-12 11:37:43 -08001784f_err:
1785 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
David Benjamin854dd652014-08-26 00:32:30 -04001786err:
Adam Langleyd5157222016-12-12 11:37:43 -08001787 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001788}
Adam Langley95c29f32014-06-20 12:00:00 -07001789
Adam Langley95c29f32014-06-20 12:00:00 -07001790/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1791 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001792static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1793 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001794 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001795 if (ret <= 0) {
1796 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001797 }
Adam Langley95c29f32014-06-20 12:00:00 -07001798
David Benjaminf71036e2017-01-21 14:49:39 -05001799 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001800 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001801 return -1;
1802 }
1803
David Benjamin09eb6552016-07-08 14:32:11 -07001804 CBS next_protocol, selected_protocol, padding;
1805 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001806 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1807 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001808 CBS_len(&next_protocol) != 0) {
1809 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1810 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1811 return 0;
1812 }
1813
1814 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001815 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001816 return 0;
1817 }
Adam Langley95c29f32014-06-20 12:00:00 -07001818
Adam Langleyfcf25832014-12-18 17:42:32 -08001819 return 1;
1820}
Adam Langley95c29f32014-06-20 12:00:00 -07001821
Adam Langley1258b6a2014-06-20 12:00:00 -07001822/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001823static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1824 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001825 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001826 if (msg_ret <= 0) {
1827 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001828 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001829
David Benjamin276b7e82017-01-21 14:13:39 -05001830 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001831 !tls1_verify_channel_id(hs) ||
1832 !ssl_hash_current_message(hs)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001833 return -1;
1834 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001835 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001836}
David Benjamin9f1dc822016-06-07 17:03:46 -04001837
David Benjaminc3c88822016-11-14 10:32:04 +09001838static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) {
1839 SSL *const ssl = hs->ssl;
David Benjamin123db572016-11-03 16:59:25 -04001840 const SSL_SESSION *session;
1841 SSL_SESSION *session_copy = NULL;
1842 if (ssl->session == NULL) {
1843 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin45738dd2017-02-09 20:01:26 -05001844 ssl_session_rebase_time(ssl, hs->new_session);
1845 session = hs->new_session;
David Benjamin123db572016-11-03 16:59:25 -04001846 } else {
1847 /* We are renewing an existing session. Duplicate the session to adjust the
1848 * timeout. */
1849 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1850 if (session_copy == NULL) {
1851 return -1;
1852 }
1853
David Benjamin17b30832017-01-28 14:00:32 -05001854 ssl_session_rebase_time(ssl, session_copy);
David Benjamin123db572016-11-03 16:59:25 -04001855 session = session_copy;
1856 }
1857
David Benjamin75836432016-06-17 18:48:29 -04001858 CBB cbb, body, ticket;
David Benjamin123db572016-11-03 16:59:25 -04001859 int ok =
1860 ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) &&
1861 CBB_add_u32(&body, session->timeout) &&
1862 CBB_add_u16_length_prefixed(&body, &ticket) &&
1863 ssl_encrypt_ticket(ssl, &ticket, session) &&
David Benjamindaf207a2017-01-03 18:37:41 -05001864 ssl_add_message_cbb(ssl, &cbb);
David Benjamin123db572016-11-03 16:59:25 -04001865
1866 SSL_SESSION_free(session_copy);
1867 CBB_cleanup(&cbb);
1868
1869 if (!ok) {
David Benjamine75cc272016-11-03 18:06:27 -04001870 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001871 }
1872
David Benjamin16315f72017-01-12 20:02:05 -05001873 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001874}