blob: 00ac54917af2ead8e39575b1ab53d40391bddad7 [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);
David Benjaminc3c88822016-11-14 10:32:04 +0900178static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900179static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
180static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
181static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
182static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
183static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
184static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
David Benjaminb5f55c32017-06-15 23:15:15 -0400185static int ssl3_send_server_finished(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400186
David Benjamince8c9d22016-11-14 10:45:16 +0900187int ssl3_accept(SSL_HANDSHAKE *hs) {
188 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 int ret = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700190
David Benjamin0d56f882015-12-19 17:05:56 -0500191 assert(ssl->handshake_func == ssl3_accept);
192 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500193
Adam Langleyfcf25832014-12-18 17:42:32 -0800194 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500195 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjamincb0c29f2016-12-12 17:00:50 -0500197 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400198 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400199 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500200 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500202
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamin59bae5a2017-02-02 23:51:22 -0500204 ret = ssl->method->ssl_get_message(ssl);
205 if (ret <= 0) {
206 goto end;
Steven Valdez143e8b32016-07-11 13:19:03 -0400207 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500208 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
209 break;
210
211 case SSL3_ST_SR_CLNT_HELLO_B:
212 ret = ssl3_process_client_hello(hs);
213 if (ret <= 0) {
214 goto end;
215 }
216 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
217 break;
218
219 case SSL3_ST_SR_CLNT_HELLO_C:
220 ret = ssl3_select_certificate(hs);
221 if (ret <= 0) {
222 goto end;
223 }
224 if (hs->state != SSL_ST_TLS13) {
225 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
226 }
227 break;
228
229 case SSL3_ST_SR_CLNT_HELLO_D:
230 ret = ssl3_select_parameters(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 if (ret <= 0) {
232 goto end;
233 }
David Benjamindf50eec2016-06-07 16:49:42 -0400234 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500235 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900239 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 if (ret <= 0) {
241 goto end;
242 }
Steven Valdez87eab492016-06-27 16:34:59 -0400243 if (ssl->session != NULL) {
David Benjaminb5f55c32017-06-15 23:15:15 -0400244 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500246 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 case SSL3_ST_SW_CERT_A:
David Benjamin0d1730d2017-06-15 23:24:25 -0400251 ret = ssl3_send_server_certificate(hs);
252 if (ret <= 0) {
253 goto end;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100254 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500255 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100256 break;
257
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 case SSL3_ST_SW_KEY_EXCH_A:
David Benjamin44148742017-06-17 13:20:59 -0400259 if (hs->server_params_len > 0) {
David Benjaminc3c88822016-11-14 10:32:04 +0900260 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500261 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500263 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 }
Adam Langley95c29f32014-06-20 12:00:00 -0700265
David Benjamincb0c29f2016-12-12 17:00:50 -0500266 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 case SSL3_ST_SW_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900270 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 if (ret <= 0) {
272 goto end;
273 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500274 hs->next_state = SSL3_ST_SR_CERT_A;
275 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900279 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900280 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 if (ret <= 0) {
282 goto end;
283 }
284 }
David Benjamin3a1dd462017-07-11 16:13:10 -0400285 hs->state = SSL3_ST_VERIFY_CLIENT_CERT;
286 break;
287
288 case SSL3_ST_VERIFY_CLIENT_CERT:
289 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
290 switch (ssl_verify_peer_cert(hs)) {
291 case ssl_verify_ok:
292 break;
293 case ssl_verify_invalid:
294 ret = -1;
295 goto end;
296 case ssl_verify_retry:
297 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
298 ret = -1;
299 goto end;
300 }
301 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500302 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 case SSL3_ST_SR_KEY_EXCH_A:
306 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900307 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 if (ret <= 0) {
309 goto end;
310 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500311 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900315 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 if (ret <= 0) {
317 goto end;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
David Benjamincb0c29f2016-12-12 17:00:50 -0500320 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700322
David Benjamina41280d2015-11-26 02:16:49 -0500323 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400324 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500325 if (ret <= 0) {
326 goto end;
327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
David Benjamin67739722016-11-17 17:03:59 +0900329 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 ret = -1;
331 goto end;
332 }
David Benjamina41280d2015-11-26 02:16:49 -0500333
David Benjamincb0c29f2016-12-12 17:00:50 -0500334 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700336
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900338 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900339 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400340 if (ret <= 0) {
341 goto end;
342 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500344 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700346
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400348 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900349 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400350 if (ret <= 0) {
351 goto end;
352 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500354 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900358 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 if (ret <= 0) {
360 goto end;
361 }
Adam Langley95c29f32014-06-20 12:00:00 -0700362
David Benjamindf50eec2016-06-07 16:49:42 -0400363 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400364 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500365 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 } else {
David Benjaminb5f55c32017-06-15 23:15:15 -0400367 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 }
David Benjamindf50eec2016-06-07 16:49:42 -0400369
Steven Valdez87eab492016-06-27 16:34:59 -0400370 /* If this is a full handshake with ChannelID then record the handshake
David Benjamin45738dd2017-02-09 20:01:26 -0500371 * hashes in |hs->new_session| in case we need them to verify a
Steven Valdez87eab492016-06-27 16:34:59 -0400372 * ChannelID signature on a resumption of this session in the future. */
373 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Steven Valdez908ac192017-01-12 13:17:07 -0500374 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 if (ret <= 0) {
376 goto end;
377 }
378 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 case SSL3_ST_SW_FINISHED_A:
David Benjaminb5f55c32017-06-15 23:15:15 -0400382 ret = ssl3_send_server_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 if (ret <= 0) {
384 goto end;
385 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500386 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400387 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500388 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500390 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700393
David Benjamin9f1dc822016-06-07 17:03:46 -0400394 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500395 ret = ssl->method->flush_flight(ssl);
396 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400397 goto end;
398 }
399
David Benjamincb0c29f2016-12-12 17:00:50 -0500400 hs->state = hs->next_state;
401 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400402 ssl->method->expect_flight(ssl);
403 }
404 break;
405
Steven Valdez681eb6a2016-12-19 13:19:29 -0500406 case SSL_ST_TLS13: {
407 int early_return = 0;
408 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400409 if (ret <= 0) {
410 goto end;
411 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500412
413 if (early_return) {
414 ret = 1;
415 goto end;
416 }
417
David Benjamincb0c29f2016-12-12 17:00:50 -0500418 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400419 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500420 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400423 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Steven Valdez87eab492016-06-27 16:34:59 -0400425 /* If we aren't retaining peer certificates then we can discard it
426 * now. */
David Benjamin45738dd2017-02-09 20:01:26 -0500427 if (hs->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900428 ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -0500429 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
430 hs->new_session->certs = NULL;
431 ssl->ctx->x509_method->session_clear(hs->new_session);
Steven Valdez87eab492016-06-27 16:34:59 -0400432 }
433
434 SSL_SESSION_free(ssl->s3->established_session);
435 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400436 SSL_SESSION_up_ref(ssl->session);
437 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400438 } else {
David Benjamin45738dd2017-02-09 20:01:26 -0500439 ssl->s3->established_session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -0400440 ssl->s3->established_session->not_resumable = 0;
David Benjamin45738dd2017-02-09 20:01:26 -0500441 hs->new_session = NULL;
Steven Valdez87eab492016-06-27 16:34:59 -0400442 }
443
David Benjamin78476f62016-11-12 11:20:55 +0900444 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900445 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900446
David Benjamin4e9cc712016-06-01 20:16:03 -0400447 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 ret = 1;
449 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700450
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 default:
David Benjamin3570d732015-06-29 00:28:17 -0400452 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 ret = -1;
454 goto end;
455 }
Adam Langley95c29f32014-06-20 12:00:00 -0700456
David Benjamin16b1b1d2017-02-10 23:41:14 -0500457 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400458 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 }
461
Adam Langley95c29f32014-06-20 12:00:00 -0700462end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400463 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 return ret;
465}
Adam Langley95c29f32014-06-20 12:00:00 -0700466
David Benjamin731058e2016-12-03 23:15:13 -0500467int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
468 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400469 CBS cipher_suites;
470 CBS_init(&cipher_suites, client_hello->cipher_suites,
471 client_hello->cipher_suites_len);
472
473 while (CBS_len(&cipher_suites) > 0) {
474 uint16_t got_id;
475 if (!CBS_get_u16(&cipher_suites, &got_id)) {
476 return 0;
477 }
478
479 if (got_id == id) {
480 return 1;
481 }
482 }
483
484 return 0;
485}
486
David Benjaminf04c2e92016-12-06 13:35:25 -0500487static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500488 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500489 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500490 assert(!ssl->s3->have_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400491 CBS supported_versions, versions;
David Benjamin731058e2016-12-03 23:15:13 -0500492 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
493 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400494 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
495 CBS_len(&supported_versions) != 0 ||
496 CBS_len(&versions) == 0) {
497 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
498 *out_alert = SSL_AD_DECODE_ERROR;
499 return 0;
500 }
David Benjamin2dc02042016-09-19 19:57:37 -0400501 } else {
Steven Valdez8f36c512017-06-20 10:55:02 -0400502 /* Convert the ClientHello version to an equivalent supported_versions
503 * extension. */
504 static const uint8_t kTLSVersions[] = {
505 0x03, 0x03, /* TLS 1.2 */
506 0x03, 0x02, /* TLS 1.1 */
507 0x03, 0x01, /* TLS 1 */
508 0x03, 0x00, /* SSL 3 */
509 };
510
511 static const uint8_t kDTLSVersions[] = {
512 0xfe, 0xfd, /* DTLS 1.2 */
513 0xfe, 0xff, /* DTLS 1.0 */
514 };
515
516 size_t versions_len = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400517 if (SSL_is_dtls(ssl)) {
518 if (client_hello->version <= DTLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400519 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400520 } else if (client_hello->version <= DTLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400521 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400522 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400523 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
524 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400525 } else {
526 if (client_hello->version >= TLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400527 versions_len = 8;
Steven Valdezfdd10992016-09-15 16:27:05 -0400528 } else if (client_hello->version >= TLS1_1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400529 versions_len = 6;
Steven Valdezfdd10992016-09-15 16:27:05 -0400530 } else if (client_hello->version >= TLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400531 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400532 } else if (client_hello->version >= SSL3_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400533 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400534 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400535 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
536 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400537 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400538 }
539
Steven Valdez8f36c512017-06-20 10:55:02 -0400540 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400541 return 0;
542 }
543
David Benjamin25fe85b2016-08-09 20:00:32 -0400544 /* At this point, the connection's version is known and |ssl->version| is
545 * fixed. Begin enforcing the record-layer version. */
546 ssl->s3->have_version = 1;
547
Steven Valdez8f36c512017-06-20 10:55:02 -0400548 /* Handle FALLBACK_SCSV. */
549 if (ssl_client_cipher_list_contains_cipher(client_hello,
550 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
551 ssl3_protocol_version(ssl) < hs->max_version) {
552 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
553 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
554 return 0;
555 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400556
Steven Valdez8f36c512017-06-20 10:55:02 -0400557 return 1;
David Benjamin25fe85b2016-08-09 20:00:32 -0400558}
559
David Benjamin2578b292016-12-03 23:19:55 -0500560static STACK_OF(SSL_CIPHER) *
561 ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) {
562 CBS cipher_suites;
563 CBS_init(&cipher_suites, client_hello->cipher_suites,
564 client_hello->cipher_suites_len);
565
566 STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null();
567 if (sk == NULL) {
568 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
569 goto err;
570 }
571
572 while (CBS_len(&cipher_suites) > 0) {
573 uint16_t cipher_suite;
574
575 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
576 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
577 goto err;
578 }
579
580 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
581 if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
582 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
583 goto err;
584 }
585 }
586
587 return sk;
588
589err:
590 sk_SSL_CIPHER_free(sk);
591 return NULL;
592}
593
594/* ssl_get_compatible_server_ciphers determines the key exchange and
595 * authentication cipher suite masks compatible with the server configuration
596 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
597 * exchange mask and |*out_mask_a| to the authentication mask. */
598static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
599 uint32_t *out_mask_k,
600 uint32_t *out_mask_a) {
601 SSL *const ssl = hs->ssl;
David Benjamin2578b292016-12-03 23:19:55 -0500602 uint32_t mask_k = 0;
603 uint32_t mask_a = 0;
604
Adam Langley3a2b47a2017-01-24 13:59:42 -0800605 if (ssl_has_certificate(ssl)) {
David Benjamin69522112017-03-28 15:38:29 -0500606 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey);
607 if (EVP_PKEY_id(hs->local_pubkey) == EVP_PKEY_RSA) {
David Benjamin2578b292016-12-03 23:19:55 -0500608 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500609 }
610 }
611
David Benjamin2578b292016-12-03 23:19:55 -0500612 /* Check for a shared group to consider ECDHE ciphers. */
613 uint16_t unused;
614 if (tls1_get_shared_group(hs, &unused)) {
615 mask_k |= SSL_kECDHE;
616 }
617
David Benjamin2578b292016-12-03 23:19:55 -0500618 /* PSK requires a server callback. */
619 if (ssl->psk_server_callback != NULL) {
620 mask_k |= SSL_kPSK;
621 mask_a |= SSL_aPSK;
622 }
623
624 *out_mask_k = mask_k;
625 *out_mask_a = mask_a;
626}
627
628static const SSL_CIPHER *ssl3_choose_cipher(
629 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
630 const struct ssl_cipher_preference_list_st *server_pref) {
631 SSL *const ssl = hs->ssl;
David Benjaminccbb1652017-03-22 18:24:32 -0400632 STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjamin2578b292016-12-03 23:19:55 -0500633 /* in_group_flags will either be NULL, or will point to an array of bytes
634 * which indicate equal-preference groups in the |prio| stack. See the
635 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
636 * struct. */
637 const uint8_t *in_group_flags;
638 /* group_min contains the minimal index so far found in a group, or -1 if no
639 * such value exists yet. */
640 int group_min = -1;
641
David Benjaminccbb1652017-03-22 18:24:32 -0400642 STACK_OF(SSL_CIPHER) *client_pref =
643 ssl_parse_client_cipher_list(client_hello);
644 if (client_pref == NULL) {
David Benjamin2578b292016-12-03 23:19:55 -0500645 return NULL;
646 }
647
648 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjaminccbb1652017-03-22 18:24:32 -0400649 prio = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500650 in_group_flags = server_pref->in_group_flags;
David Benjaminccbb1652017-03-22 18:24:32 -0400651 allow = client_pref;
David Benjamin2578b292016-12-03 23:19:55 -0500652 } else {
David Benjaminccbb1652017-03-22 18:24:32 -0400653 prio = client_pref;
David Benjamin2578b292016-12-03 23:19:55 -0500654 in_group_flags = NULL;
David Benjaminccbb1652017-03-22 18:24:32 -0400655 allow = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500656 }
657
David Benjaminccbb1652017-03-22 18:24:32 -0400658 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -0500659 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
660
David Benjaminccbb1652017-03-22 18:24:32 -0400661 const SSL_CIPHER *ret = NULL;
David Benjamin2578b292016-12-03 23:19:55 -0500662 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400663 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500664
David Benjaminccbb1652017-03-22 18:24:32 -0400665 size_t cipher_index;
666 if (/* Check if the cipher is supported for the current version. */
667 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
668 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
669 /* Check the cipher is supported for the server configuration. */
670 (c->algorithm_mkey & mask_k) &&
671 (c->algorithm_auth & mask_a) &&
672 /* Check the cipher is in the |allow| list. */
673 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500674 if (in_group_flags != NULL && in_group_flags[i] == 1) {
675 /* This element of |prio| is in a group. Update the minimum index found
676 * so far and continue looking. */
677 if (group_min == -1 || (size_t)group_min > cipher_index) {
678 group_min = cipher_index;
679 }
680 } else {
681 if (group_min != -1 && (size_t)group_min < cipher_index) {
682 cipher_index = group_min;
683 }
684 ret = sk_SSL_CIPHER_value(allow, cipher_index);
685 break;
686 }
687 }
688
689 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
690 /* We are about to leave a group, but we found a match in it, so that's
691 * our answer. */
692 ret = sk_SSL_CIPHER_value(allow, group_min);
693 break;
694 }
695 }
696
David Benjaminccbb1652017-03-22 18:24:32 -0400697 sk_SSL_CIPHER_free(client_pref);
David Benjamin2578b292016-12-03 23:19:55 -0500698 return ret;
699}
700
David Benjamin59bae5a2017-02-02 23:51:22 -0500701static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900702 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500703 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
704 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800705 }
Adam Langley95c29f32014-06-20 12:00:00 -0700706
David Benjamin731058e2016-12-03 23:15:13 -0500707 SSL_CLIENT_HELLO client_hello;
708 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
709 ssl->init_num)) {
David Benjamin3570d732015-06-29 00:28:17 -0400710 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin59bae5a2017-02-02 23:51:22 -0500711 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
712 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 }
Adam Langley95c29f32014-06-20 12:00:00 -0700714
David Benjamin59bae5a2017-02-02 23:51:22 -0500715 /* Run the early callback. */
716 if (ssl->ctx->select_certificate_cb != NULL) {
717 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000718 case ssl_select_cert_retry:
David Benjamin59bae5a2017-02-02 23:51:22 -0500719 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
720 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400721
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000722 case ssl_select_cert_error:
David Benjamin59bae5a2017-02-02 23:51:22 -0500723 /* Connection rejected. */
724 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
725 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
726 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400727
David Benjamin59bae5a2017-02-02 23:51:22 -0500728 default:
729 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400730 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500731 }
David Benjamine14ff062016-08-09 16:21:24 -0400732
David Benjamin68161cb2017-06-20 14:49:43 -0400733 /* Freeze the version range after the early callback. */
734 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
735 return -1;
736 }
737
Adam Langley04540a72017-02-09 10:11:50 -0800738 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500739 if (!negotiate_version(hs, &alert, &client_hello)) {
740 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
741 return -1;
742 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400743
Steven Valdez8f36c512017-06-20 10:55:02 -0400744 hs->client_version = client_hello.version;
David Benjamin59bae5a2017-02-02 23:51:22 -0500745 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
746 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
747 return -1;
748 }
749 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
750 client_hello.random_len);
751
752 /* Only null compression is supported. TLS 1.3 further requires the peer
753 * advertise no other compression. */
754 if (OPENSSL_memchr(client_hello.compression_methods, 0,
755 client_hello.compression_methods_len) == NULL ||
756 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
757 client_hello.compression_methods_len != 1)) {
758 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
759 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
760 return -1;
761 }
762
763 /* TLS extensions. */
764 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
765 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
766 return -1;
767 }
768
769 return 1;
770}
771
772static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
773 SSL *const ssl = hs->ssl;
774 /* Call |cert_cb| to update server certificates if required. */
775 if (ssl->cert->cert_cb != NULL) {
776 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
777 if (rv == 0) {
778 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
779 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin25fe85b2016-08-09 20:00:32 -0400780 return -1;
781 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500782 if (rv < 0) {
783 ssl->rwstate = SSL_X509_LOOKUP;
784 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900785 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900786 }
787
David Benjamina232a712017-03-30 15:51:53 -0500788 if (!ssl_on_certificate_selected(hs)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500789 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900790 }
791
David Benjamin59bae5a2017-02-02 23:51:22 -0500792 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
793 /* Jump to the TLS 1.3 state machine. */
794 hs->state = SSL_ST_TLS13;
795 hs->do_tls13_handshake = tls13_server_handshake;
796 return 1;
797 }
798
799 SSL_CLIENT_HELLO client_hello;
800 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
801 ssl->init_num)) {
802 return -1;
803 }
804
805 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
806 * certificate is finalized. */
David Benjamin45738dd2017-02-09 20:01:26 -0500807 hs->new_cipher =
David Benjamin59bae5a2017-02-02 23:51:22 -0500808 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjamin45738dd2017-02-09 20:01:26 -0500809 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500810 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
811 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
812 return -1;
813 }
814
815 return 1;
816}
817
818static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
819 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500820 SSL_CLIENT_HELLO client_hello;
821 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
822 ssl->init_num)) {
823 return -1;
824 }
David Benjamin34202b92016-11-16 19:07:53 +0900825
826 /* Determine whether we are doing session resumption. */
827 int tickets_supported = 0, renew_ticket = 0;
David Benjamind781fc42017-07-12 16:25:57 -0400828 /* TODO(davidben): Switch |ssl_get_prev_session| to take a |bssl::UniquePtr|
829 * output and simplify this. */
830 SSL_SESSION *session_raw = nullptr;
831 auto session_ret = ssl_get_prev_session(ssl, &session_raw, &tickets_supported,
832 &renew_ticket, &client_hello);
833 bssl::UniquePtr<SSL_SESSION> session(session_raw);
834 switch (session_ret) {
David Benjamin34202b92016-11-16 19:07:53 +0900835 case ssl_session_success:
836 break;
837 case ssl_session_error:
David Benjamind781fc42017-07-12 16:25:57 -0400838 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900839 case ssl_session_retry:
840 ssl->rwstate = SSL_PENDING_SESSION;
David Benjamind781fc42017-07-12 16:25:57 -0400841 return -1;
Adam Langley4c341d02017-03-08 19:33:21 -0800842 case ssl_session_ticket_retry:
843 ssl->rwstate = SSL_PENDING_TICKET;
David Benjamind781fc42017-07-12 16:25:57 -0400844 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900845 }
846
David Benjamind781fc42017-07-12 16:25:57 -0400847 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500848 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900849 /* A ClientHello without EMS that attempts to resume a session with EMS
850 * is fatal to the connection. */
David Benjamin34202b92016-11-16 19:07:53 +0900851 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400852 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamind781fc42017-07-12 16:25:57 -0400853 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900854 }
855
David Benjamind781fc42017-07-12 16:25:57 -0400856 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjamin34202b92016-11-16 19:07:53 +0900857 /* If the client offers the EMS extension, but the previous session
858 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500859 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400860 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900861 }
862 }
863
David Benjamind781fc42017-07-12 16:25:57 -0400864 if (session) {
David Benjamin34202b92016-11-16 19:07:53 +0900865 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900866 hs->ticket_expected = renew_ticket;
David Benjamind781fc42017-07-12 16:25:57 -0400867 ssl->session = session.release();
David Benjamin34202b92016-11-16 19:07:53 +0900868 ssl->s3->session_reused = 1;
869 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900870 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900871 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900872 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamind781fc42017-07-12 16:25:57 -0400873 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900874 }
875
876 /* Clear the session ID if we want the session to be single-use. */
877 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500878 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900879 }
880 }
881
882 if (ssl->ctx->dos_protection_cb != NULL &&
883 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
884 /* Connection rejected for DOS reasons. */
David Benjamin34202b92016-11-16 19:07:53 +0900885 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400886 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400887 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900888 }
889
David Benjaminf01f42a2016-11-16 19:05:33 +0900890 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500891 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400892
David Benjamin4eb95cc2016-11-16 17:08:23 +0900893 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900894 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500895 OPENSSL_free(hs->new_session->tlsext_hostname);
896 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname);
897 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamind98107b2017-06-15 22:56:37 -0400898 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400899 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900900 }
901 }
902
David Benjamin5c1ce292015-05-26 16:59:43 -0400903 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900904 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400905 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500906 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
907 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900908 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400909 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400910 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500911 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900912 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400913 }
Adam Langley37646832016-08-01 16:16:46 -0700914
David Benjaminc3c88822016-11-14 10:32:04 +0900915 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700916 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400917 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500918 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700919 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 }
921
David Benjaminf01f42a2016-11-16 19:05:33 +0900922 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
923 * deferred. Complete it now. */
David Benjamind98107b2017-06-15 22:56:37 -0400924 uint8_t alert = SSL_AD_DECODE_ERROR;
925 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
926 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamind781fc42017-07-12 16:25:57 -0400927 return -1;
David Benjamin9ef31f02016-10-31 18:01:13 -0400928 }
929
David Benjaminf71036e2017-01-21 14:49:39 -0500930 /* Now that all parameters are known, initialize the handshake hash and hash
931 * the ClientHello. */
Steven Valdez908ac192017-01-12 13:17:07 -0500932 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
David Benjamin45738dd2017-02-09 20:01:26 -0500933 hs->new_cipher->algorithm_prf) ||
Steven Valdez908ac192017-01-12 13:17:07 -0500934 !ssl_hash_current_message(hs)) {
David Benjamind98107b2017-06-15 22:56:37 -0400935 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400936 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -0400937 }
938
Steven Valdez2b8415e2016-06-30 13:27:23 -0400939 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +0900940 if (!hs->cert_request) {
Steven Valdez908ac192017-01-12 13:17:07 -0500941 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -0800942 }
943
David Benjamind781fc42017-07-12 16:25:57 -0400944 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800945}
Adam Langley95c29f32014-06-20 12:00:00 -0700946
David Benjaminc3c88822016-11-14 10:32:04 +0900947static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
948 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400949
950 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
951 * known attack while we fix ChannelID itself. */
952 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -0500953 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400954 ssl->s3->tlsext_channel_id_valid = 0;
955 }
956
957 /* If this is a resumption and the original handshake didn't support
958 * ChannelID then we didn't record the original handshake hashes in the
959 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -0400960 if (ssl->session != NULL &&
961 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400962 ssl->s3->tlsext_channel_id_valid = 0;
963 }
964
David Benjaminad8f5e12017-02-20 17:00:20 -0500965 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -0400966 ssl_get_current_time(ssl, &now);
967 ssl->s3->server_random[0] = now.tv_sec >> 24;
968 ssl->s3->server_random[1] = now.tv_sec >> 16;
969 ssl->s3->server_random[2] = now.tv_sec >> 8;
970 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400971 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -0400972 return -1;
973 }
974
David Benjamin55108632016-08-11 22:01:18 -0400975 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
976 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400977
David Benjamin45738dd2017-02-09 20:01:26 -0500978 const SSL_SESSION *session = hs->new_session;
Steven Valdez87eab492016-06-27 16:34:59 -0400979 if (ssl->session != NULL) {
980 session = ssl->session;
981 }
982
David Benjamin75836432016-06-17 18:48:29 -0400983 CBB cbb, body, session_id;
984 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
985 !CBB_add_u16(&body, ssl->version) ||
986 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
987 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400988 !CBB_add_bytes(&session_id, session->session_id,
989 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500990 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -0400991 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500992 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -0500993 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin56380462015-10-10 14:59:09 -0400994 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
995 CBB_cleanup(&cbb);
996 return -1;
997 }
998
David Benjamin16315f72017-01-12 20:02:05 -0500999 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001000}
Adam Langley95c29f32014-06-20 12:00:00 -07001001
David Benjaminc3c88822016-11-14 10:32:04 +09001002static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
1003 SSL *const ssl = hs->ssl;
David Benjamind781fc42017-07-12 16:25:57 -04001004 bssl::ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -04001005
David Benjamin44148742017-06-17 13:20:59 -04001006 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1007 if (!ssl_has_certificate(ssl)) {
1008 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamind781fc42017-07-12 16:25:57 -04001009 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001010 }
David Benjamin32a66d52016-07-13 22:03:11 -04001011
David Benjamin44148742017-06-17 13:20:59 -04001012 if (!ssl3_output_cert_chain(ssl)) {
David Benjamind781fc42017-07-12 16:25:57 -04001013 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001014 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001015
David Benjamin44148742017-06-17 13:20:59 -04001016 if (hs->certificate_status_expected) {
1017 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -04001018 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -04001019 SSL3_MT_CERTIFICATE_STATUS) ||
1020 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1021 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
1022 !CBB_add_bytes(&ocsp_response,
1023 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
1024 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamind781fc42017-07-12 16:25:57 -04001025 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -04001026 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001027 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001028 }
David Benjamin0d1730d2017-06-15 23:24:25 -04001029 }
David Benjamin75836432016-06-17 18:48:29 -04001030 }
1031
David Benjamin44148742017-06-17 13:20:59 -04001032 /* Assemble ServerKeyExchange parameters if needed. */
1033 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1034 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1035 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
1036 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001037
David Benjamin44148742017-06-17 13:20:59 -04001038 /* Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
1039 * the client and server randoms for the signing transcript. */
1040 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -04001041 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
1042 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1043 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
1044 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001045 }
1046
David Benjamin2a0b3912015-12-18 01:01:21 -05001047 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001049 size_t len =
1050 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
David Benjamind781fc42017-07-12 16:25:57 -04001051 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001052 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1053 len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001054 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001056 }
Adam Langley95c29f32014-06-20 12:00:00 -07001057
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001058 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001059 /* Determine the group to use. */
1060 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001061 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001062 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -05001063 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamind781fc42017-07-12 16:25:57 -04001064 return -1;
1065 }
David Benjamin45738dd2017-02-09 20:01:26 -05001066 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001067
David Benjamin4298d772015-12-19 00:18:25 -05001068 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc3c88822016-11-14 10:32:04 +09001069 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
David Benjamind781fc42017-07-12 16:25:57 -04001070 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
1071 !CBB_add_u16(cbb.get(), group_id) ||
1072 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc3c88822016-11-14 10:32:04 +09001073 !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) {
David Benjamind781fc42017-07-12 16:25:57 -04001074 return -1;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001075 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001076 } else {
1077 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001078 }
Adam Langley95c29f32014-06-20 12:00:00 -07001079
David Benjamind781fc42017-07-12 16:25:57 -04001080 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
1081 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001082 }
David Benjaminc42acee2016-06-17 17:47:58 -04001083 }
1084
David Benjamind781fc42017-07-12 16:25:57 -04001085 return 1;
David Benjamin44148742017-06-17 13:20:59 -04001086}
1087
1088static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1089 SSL *const ssl = hs->ssl;
1090 CBB cbb, body, child;
David Benjamin75836432016-06-17 18:48:29 -04001091 if (!ssl->method->init_message(ssl, &cbb, &body,
1092 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin44148742017-06-17 13:20:59 -04001093 /* |hs->server_params| contains a prefix for signing. */
1094 hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
1095 !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
1096 hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001097 goto err;
1098 }
Adam Langley95c29f32014-06-20 12:00:00 -07001099
David Benjamin2a0b3912015-12-18 01:01:21 -05001100 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -05001101 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001102 if (!ssl_has_private_key(ssl)) {
1103 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 goto err;
1105 }
Adam Langley95c29f32014-06-20 12:00:00 -07001106
David Benjaminc42acee2016-06-17 17:47:58 -04001107 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001108 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001109 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001110 goto err;
1111 }
David Benjaminc42acee2016-06-17 17:47:58 -04001112 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001113 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001114 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1115 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1116 goto err;
1117 }
David Benjaminc42acee2016-06-17 17:47:58 -04001118 }
1119
1120 /* Add space for the signature. */
David Benjamina232a712017-03-30 15:51:53 -05001121 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey);
David Benjaminc42acee2016-06-17 17:47:58 -04001122 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001123 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001124 !CBB_reserve(&child, &ptr, max_sig_len)) {
1125 goto err;
1126 }
1127
David Benjamin2a0b3912015-12-18 01:01:21 -05001128 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001129 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1130 signature_algorithm, hs->server_params,
1131 hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001132 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001133 if (!CBB_did_write(&child, sig_len)) {
1134 goto err;
1135 }
1136 break;
1137 case ssl_private_key_failure:
David Benjamin2a0b3912015-12-18 01:01:21 -05001138 goto err;
1139 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001140 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin2a0b3912015-12-18 01:01:21 -05001141 goto err;
1142 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001143 }
1144
David Benjamindaf207a2017-01-03 18:37:41 -05001145 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin1f9f9c42015-08-28 16:17:59 -04001146 goto err;
1147 }
David Benjaminc42acee2016-06-17 17:47:58 -04001148
David Benjaminc3c88822016-11-14 10:32:04 +09001149 OPENSSL_free(hs->server_params);
1150 hs->server_params = NULL;
1151 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001152
David Benjamin16315f72017-01-12 20:02:05 -05001153 return 1;
nagendra modadugu601448a2015-07-24 09:31:31 -07001154
Adam Langley95c29f32014-06-20 12:00:00 -07001155err:
David Benjamin2a0b3912015-12-18 01:01:21 -05001156 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001157 return -1;
1158}
Adam Langley95c29f32014-06-20 12:00:00 -07001159
David Benjamin0d1730d2017-06-15 23:24:25 -04001160static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001161 SSL *const ssl = hs->ssl;
David Benjamin0d1730d2017-06-15 23:24:25 -04001162 CBB cbb, body;
David Benjamin75836432016-06-17 18:48:29 -04001163
David Benjamin0d1730d2017-06-15 23:24:25 -04001164 if (hs->cert_request) {
1165 CBB cert_types, sigalgs_cbb;
1166 if (!ssl->method->init_message(ssl, &cbb, &body,
1167 SSL3_MT_CERTIFICATE_REQUEST) ||
1168 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1169 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
1170 (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
1171 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
1172 (ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
1173 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
1174 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
1175 !ssl_add_client_CA_list(ssl, &body) ||
1176 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001177 goto err;
1178 }
1179 }
1180
David Benjamin0d1730d2017-06-15 23:24:25 -04001181 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001182 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin75836432016-06-17 18:48:29 -04001183 goto err;
1184 }
1185
David Benjamin16315f72017-01-12 20:02:05 -05001186 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001187
Adam Langley95c29f32014-06-20 12:00:00 -07001188err:
David Benjamin75836432016-06-17 18:48:29 -04001189 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1190 CBB_cleanup(&cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -08001191 return -1;
1192}
Adam Langley95c29f32014-06-20 12:00:00 -07001193
David Benjaminc3c88822016-11-14 10:32:04 +09001194static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1195 SSL *const ssl = hs->ssl;
1196 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001197
David Benjaminf71036e2017-01-21 14:49:39 -05001198 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001199 if (msg_ret <= 0) {
1200 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001201 }
1202
1203 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1204 if (ssl->version == SSL3_VERSION &&
1205 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001206 /* In SSL 3.0, the Certificate message is omitted to signal no
1207 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001208 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001209 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001210 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1211 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001212 }
1213
Adam Langley37646832016-08-01 16:16:46 -07001214 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001215 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001216 hs->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001217 ssl->s3->tmp.reuse_message = 1;
1218 return 1;
1219 }
1220
David Benjamin9f1dc822016-06-07 17:03:46 -04001221 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001222 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1223 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001224 }
1225
Steven Valdez908ac192017-01-12 13:17:07 -05001226 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001227 return -1;
1228 }
1229
David Benjamin5c900c82016-07-13 23:03:26 -04001230 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001231 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001232
David Benjamin45738dd2017-02-09 20:01:26 -05001233 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001234 EVP_PKEY_free(hs->peer_pubkey);
1235 hs->peer_pubkey = NULL;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001236 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin45738dd2017-02-09 20:01:26 -05001237 hs->new_session->certs = ssl_parse_cert_chain(
1238 &alert, &hs->peer_pubkey,
1239 ssl->retain_only_sha256_of_client_certs ? hs->new_session->peer_sha256
1240 : NULL,
1241 &certificate_msg, ssl->ctx->pool);
1242 if (hs->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001243 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001244 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001245 }
1246
Adam Langley68e71242016-12-12 11:06:16 -08001247 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin45738dd2017-02-09 20:01:26 -05001248 !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001249 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001250 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001251 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001252 }
1253
David Benjamin45738dd2017-02-09 20:01:26 -05001254 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001255 /* No client certificate so the handshake buffer may be discarded. */
Steven Valdez908ac192017-01-12 13:17:07 -05001256 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
David Benjamin9f1dc822016-06-07 17:03:46 -04001257
David Benjamine455e512016-08-01 20:11:13 -04001258 /* In SSL 3.0, sending no certificate is signaled by omitting the
1259 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001260 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001261 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001262 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001263 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001264 }
1265
1266 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001267 /* Fail for TLS only if we required a certificate */
1268 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001269 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001270 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001271 }
Adam Langley37646832016-08-01 16:16:46 -07001272
1273 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001274 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001275 hs->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001276 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001277 }
1278
Adam Langley68e71242016-12-12 11:06:16 -08001279 /* The hash will have been filled in. */
1280 if (ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -05001281 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001282 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001283
David Benjamin5c900c82016-07-13 23:03:26 -04001284 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001285}
1286
David Benjaminc3c88822016-11-14 10:32:04 +09001287static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1288 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001289 CBS client_key_exchange;
Adam Langleyfcf25832014-12-18 17:42:32 -08001290 uint8_t *premaster_secret = NULL;
1291 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001293
David Benjamincb0c29f2016-12-12 17:00:50 -05001294 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminf71036e2017-01-21 14:49:39 -05001295 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001296 if (ret <= 0) {
1297 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001298 }
David Benjamin44148742017-06-17 13:20:59 -04001299 }
David Benjamin276b7e82017-01-21 14:13:39 -05001300
David Benjamin44148742017-06-17 13:20:59 -04001301 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
1302 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001303 }
Adam Langley95c29f32014-06-20 12:00:00 -07001304
David Benjamin0d56f882015-12-19 17:05:56 -05001305 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin44148742017-06-17 13:20:59 -04001306 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1307 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001308
David Benjamin44148742017-06-17 13:20:59 -04001309 /* If using a PSK key exchange, parse the PSK identity. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001310 if (alg_a & SSL_aPSK) {
1311 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001312
Adam Langleyfcf25832014-12-18 17:42:32 -08001313 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1314 * then this is the only field in the message. */
1315 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1316 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001317 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001318 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1319 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001320 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001321
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1323 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001324 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001325 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1326 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001327 }
David Benjamin35c02302014-07-13 04:14:59 -04001328
David Benjamin45738dd2017-02-09 20:01:26 -05001329 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001330 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001331 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1332 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001334 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001335
Adam Langleyfcf25832014-12-18 17:42:32 -08001336 /* Depending on the key exchange method, compute |premaster_secret| and
1337 * |premaster_secret_len|. */
1338 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001339 CBS encrypted_premaster_secret;
1340 if (ssl->version > SSL3_VERSION) {
1341 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1342 &encrypted_premaster_secret) ||
1343 CBS_len(&client_key_exchange) != 0) {
1344 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1345 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1346 goto err;
1347 }
1348 } else {
1349 encrypted_premaster_secret = client_key_exchange;
1350 }
1351
Adam Langleyfcf25832014-12-18 17:42:32 -08001352 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamina232a712017-03-30 15:51:53 -05001353 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey);
David Benjamind781fc42017-07-12 16:25:57 -04001354 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001355 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001356 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001357 goto err;
1358 }
David Benjamin35c02302014-07-13 04:14:59 -04001359
David Benjamin44148742017-06-17 13:20:59 -04001360 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1361 * timing-sensitive code below. */
David Benjamin3f5b43d2015-12-01 19:31:24 -05001362 size_t decrypt_len;
David Benjamin44148742017-06-17 13:20:59 -04001363 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1364 CBS_data(&encrypted_premaster_secret),
1365 CBS_len(&encrypted_premaster_secret))) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001366 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001367 break;
1368 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001369 goto err;
1370 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001371 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001372 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001373 goto err;
1374 }
1375
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001376 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001377 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001378 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1379 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001380 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001381
David Benjamin3f5b43d2015-12-01 19:31:24 -05001382 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1383 * section 7.4.7.1. */
1384 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001385 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001386 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001387 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001388 goto err;
1389 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001390 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001391 goto err;
1392 }
1393
David Benjamin3f5b43d2015-12-01 19:31:24 -05001394 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1395 * publicly invalid. */
1396 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001397 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001398 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1399 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001400 }
David Benjamin35c02302014-07-13 04:14:59 -04001401
David Benjamin3f5b43d2015-12-01 19:31:24 -05001402 /* Check the padding. See RFC 3447, section 7.2.2. */
1403 size_t padding_len = decrypt_len - premaster_secret_len;
1404 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1405 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001406 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001407 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1408 }
1409 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1410
1411 /* The premaster secret must begin with |client_version|. This too must be
1412 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1413 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001414 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001415 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001416 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001417
1418 /* Select, in constant time, either the decrypted premaster or the random
1419 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001420 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001421 premaster_secret[i] = constant_time_select_8(
1422 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1423 }
1424
1425 OPENSSL_free(decrypt_buf);
1426 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001427 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001428 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001429 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001430 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001431 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001432 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001433 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1434 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001435 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001436
David Benjamin4298d772015-12-19 00:18:25 -05001437 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001438 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc3c88822016-11-14 10:32:04 +09001439 if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret,
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001440 &premaster_secret_len, &alert, CBS_data(&peer_key),
1441 CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001442 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1443 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001444 }
Adam Langley95c29f32014-06-20 12:00:00 -07001445
David Benjamin4298d772015-12-19 00:18:25 -05001446 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001447 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
David Benjamin44148742017-06-17 13:20:59 -04001448 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001449 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001450 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1451 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001452 }
Adam Langley95c29f32014-06-20 12:00:00 -07001453
Adam Langleyfcf25832014-12-18 17:42:32 -08001454 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1455 * pre-shared key. */
1456 if (alg_a & SSL_aPSK) {
David Benjamin44148742017-06-17 13:20:59 -04001457 if (ssl->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001458 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin44148742017-06-17 13:20:59 -04001459 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1460 goto err;
1461 }
1462
1463 /* Look up the key for the identity. */
1464 uint8_t psk[PSK_MAX_PSK_LEN];
1465 unsigned psk_len = ssl->psk_server_callback(
1466 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1467 if (psk_len > PSK_MAX_PSK_LEN) {
1468 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1469 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1470 goto err;
1471 } else if (psk_len == 0) {
1472 /* PSK related to the given identity not found */
1473 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1474 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1475 goto err;
1476 }
1477
1478 if (alg_k & SSL_kPSK) {
1479 /* In plain PSK, other_secret is a block of 0s with the same length as the
1480 * pre-shared key. */
1481 premaster_secret_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001482 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
David Benjamin44148742017-06-17 13:20:59 -04001483 if (premaster_secret == NULL) {
1484 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1485 goto err;
1486 }
1487 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
1488 }
1489
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 CBB new_premaster, child;
1491 uint8_t *new_data;
1492 size_t new_len;
David Benjamina8653202015-06-28 01:26:10 -04001493 CBB_zero(&new_premaster);
1494 if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
1495 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001496 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
1497 !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
1498 !CBB_add_bytes(&child, psk, psk_len) ||
1499 !CBB_finish(&new_premaster, &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001500 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001501 CBB_cleanup(&new_premaster);
1502 goto err;
1503 }
David Benjamin14c83e72014-07-13 04:54:57 -04001504
Adam Langleyfcf25832014-12-18 17:42:32 -08001505 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1506 OPENSSL_free(premaster_secret);
1507 premaster_secret = new_data;
1508 premaster_secret_len = new_len;
1509 }
David Benjamin14c83e72014-07-13 04:54:57 -04001510
David Benjamin44148742017-06-17 13:20:59 -04001511 if (!ssl_hash_current_message(hs)) {
1512 goto err;
1513 }
1514
Adam Langleyfcf25832014-12-18 17:42:32 -08001515 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001516 hs->new_session->master_key_length = tls1_generate_master_secret(
1517 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1518 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001519 goto err;
1520 }
David Benjaminfc02b592017-02-17 16:26:01 -05001521 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001522
Adam Langleyfcf25832014-12-18 17:42:32 -08001523 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1524 OPENSSL_free(premaster_secret);
1525 return 1;
1526
Adam Langley95c29f32014-06-20 12:00:00 -07001527err:
David Benjamin4298d772015-12-19 00:18:25 -05001528 if (premaster_secret != NULL) {
1529 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001530 OPENSSL_free(premaster_secret);
1531 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001532 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001533
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 return -1;
1535}
Adam Langley95c29f32014-06-20 12:00:00 -07001536
David Benjaminc3c88822016-11-14 10:32:04 +09001537static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1538 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001539 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001540
Adam Langleyfcf25832014-12-18 17:42:32 -08001541 /* Only RSA and ECDSA client certificates are supported, so a
1542 * CertificateVerify is required if and only if there's a client certificate.
1543 * */
Adam Langleyd5157222016-12-12 11:37:43 -08001544 if (hs->peer_pubkey == NULL) {
Steven Valdez908ac192017-01-12 13:17:07 -05001545 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyfcf25832014-12-18 17:42:32 -08001546 return 1;
1547 }
Adam Langley95c29f32014-06-20 12:00:00 -07001548
David Benjaminf71036e2017-01-21 14:49:39 -05001549 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001550 if (msg_ret <= 0) {
1551 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001552 }
David Benjaminef865502014-08-24 02:48:34 -04001553
David Benjamin276b7e82017-01-21 14:13:39 -05001554 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1555 return -1;
1556 }
1557
David Benjamin09eb6552016-07-08 14:32:11 -07001558 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001559
Adam Langleyfcf25832014-12-18 17:42:32 -08001560 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001561 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001562 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001563 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001564 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001565 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1566 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001567 }
David Benjamin8d606e32017-06-15 22:43:04 -04001568 uint8_t alert = SSL_AD_DECODE_ERROR;
1569 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001570 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1571 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001572 }
David Benjamin45738dd2017-02-09 20:01:26 -05001573 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001574 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1575 hs->peer_pubkey)) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001576 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001577 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1578 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001579 }
David Benjamin854dd652014-08-26 00:32:30 -04001580
Adam Langleyfcf25832014-12-18 17:42:32 -08001581 /* Parse and verify the signature. */
1582 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1583 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001584 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001585 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1586 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001587 }
Adam Langley95c29f32014-06-20 12:00:00 -07001588
Steven Valdez2b8415e2016-06-30 13:27:23 -04001589 int sig_ok;
1590 /* The SSL3 construction for CertificateVerify does not decompose into a
1591 * single final digest and signature, and must be special-cased. */
1592 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001593 uint8_t digest[EVP_MAX_MD_SIZE];
1594 size_t digest_len;
Steven Valdez908ac192017-01-12 13:17:07 -05001595 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
David Benjamin45738dd2017-02-09 20:01:26 -05001596 &digest_len, hs->new_session,
Steven Valdez908ac192017-01-12 13:17:07 -05001597 signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001598 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001599 }
1600
Adam Langleyd5157222016-12-12 11:37:43 -08001601 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001602 sig_ok = pctx != NULL &&
1603 EVP_PKEY_verify_init(pctx) &&
Steven Valdez2b8415e2016-06-30 13:27:23 -04001604 EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
1605 digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001606 EVP_PKEY_CTX_free(pctx);
1607 } else {
1608 sig_ok = ssl_public_key_verify(
1609 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Steven Valdez908ac192017-01-12 13:17:07 -05001610 hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data,
1611 hs->transcript.buffer->length);
Adam Langleyfcf25832014-12-18 17:42:32 -08001612 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001613
David Benjaminbf82aed2016-03-01 22:57:40 -05001614#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1615 sig_ok = 1;
1616 ERR_clear_error();
1617#endif
1618 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001619 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001620 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1621 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001622 }
1623
Steven Valdez2b8415e2016-06-30 13:27:23 -04001624 /* The handshake buffer is no longer necessary, and we may hash the current
1625 * message.*/
Steven Valdez908ac192017-01-12 13:17:07 -05001626 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
1627 if (!ssl_hash_current_message(hs)) {
David Benjamind98107b2017-06-15 22:56:37 -04001628 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001629 }
1630
Adam Langleyd5157222016-12-12 11:37:43 -08001631 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001632}
Adam Langley95c29f32014-06-20 12:00:00 -07001633
Adam Langley95c29f32014-06-20 12:00:00 -07001634/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1635 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001636static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1637 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001638 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001639 if (ret <= 0) {
1640 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 }
Adam Langley95c29f32014-06-20 12:00:00 -07001642
David Benjaminf71036e2017-01-21 14:49:39 -05001643 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001644 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001645 return -1;
1646 }
1647
David Benjamin09eb6552016-07-08 14:32:11 -07001648 CBS next_protocol, selected_protocol, padding;
1649 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1651 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001652 CBS_len(&next_protocol) != 0) {
1653 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1654 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1655 return 0;
1656 }
1657
1658 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001659 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001660 return 0;
1661 }
Adam Langley95c29f32014-06-20 12:00:00 -07001662
Adam Langleyfcf25832014-12-18 17:42:32 -08001663 return 1;
1664}
Adam Langley95c29f32014-06-20 12:00:00 -07001665
Adam Langley1258b6a2014-06-20 12:00:00 -07001666/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001667static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1668 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001669 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001670 if (msg_ret <= 0) {
1671 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001672 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001673
David Benjamin276b7e82017-01-21 14:13:39 -05001674 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001675 !tls1_verify_channel_id(hs) ||
1676 !ssl_hash_current_message(hs)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001677 return -1;
1678 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001679 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001680}
David Benjamin9f1dc822016-06-07 17:03:46 -04001681
David Benjaminb5f55c32017-06-15 23:15:15 -04001682static int ssl3_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001683 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001684
1685 if (hs->ticket_expected) {
1686 const SSL_SESSION *session;
1687 SSL_SESSION *session_copy = NULL;
1688 if (ssl->session == NULL) {
1689 /* Fix the timeout to measure from the ticket issuance time. */
1690 ssl_session_rebase_time(ssl, hs->new_session);
1691 session = hs->new_session;
1692 } else {
1693 /* We are renewing an existing session. Duplicate the session to adjust
1694 * the timeout. */
1695 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
1696 if (session_copy == NULL) {
1697 return -1;
1698 }
1699
1700 ssl_session_rebase_time(ssl, session_copy);
1701 session = session_copy;
David Benjamin123db572016-11-03 16:59:25 -04001702 }
1703
David Benjaminb5f55c32017-06-15 23:15:15 -04001704 CBB cbb, body, ticket;
1705 int ok = ssl->method->init_message(ssl, &cbb, &body,
1706 SSL3_MT_NEW_SESSION_TICKET) &&
1707 CBB_add_u32(&body, session->timeout) &&
1708 CBB_add_u16_length_prefixed(&body, &ticket) &&
1709 ssl_encrypt_ticket(ssl, &ticket, session) &&
1710 ssl_add_message_cbb(ssl, &cbb);
1711 SSL_SESSION_free(session_copy);
1712 CBB_cleanup(&cbb);
1713 if (!ok) {
1714 return -1;
1715 }
David Benjamin123db572016-11-03 16:59:25 -04001716 }
1717
David Benjaminb5f55c32017-06-15 23:15:15 -04001718 if (!ssl->method->add_change_cipher_spec(ssl) ||
1719 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
David Benjamine75cc272016-11-03 18:06:27 -04001720 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001721 }
1722
David Benjaminb5f55c32017-06-15 23:15:15 -04001723 return ssl3_send_finished(hs);
David Benjamin9f1dc822016-06-07 17:03:46 -04001724}