blob: 2d5b85e55237372bc193c62e5fdc83317b718812 [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 Benjamin86e95b82017-07-18 16:34:25 -0400173namespace bssl {
174
David Benjamin7934f082017-08-01 16:32:25 -0400175static int ssl3_read_client_hello(SSL_HANDSHAKE *hs);
David Benjamin59bae5a2017-02-02 23:51:22 -0500176static int ssl3_select_certificate(SSL_HANDSHAKE *hs);
177static int ssl3_select_parameters(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900178static int ssl3_send_server_hello(SSL_HANDSHAKE *hs);
179static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900180static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs);
David Benjaminc3c88822016-11-14 10:32:04 +0900181static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs);
182static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs);
183static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs);
184static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs);
185static int ssl3_get_next_proto(SSL_HANDSHAKE *hs);
186static int ssl3_get_channel_id(SSL_HANDSHAKE *hs);
David Benjaminb5f55c32017-06-15 23:15:15 -0400187static int ssl3_send_server_finished(SSL_HANDSHAKE *hs);
David Benjamindf50eec2016-06-07 16:49:42 -0400188
David Benjamince8c9d22016-11-14 10:45:16 +0900189int ssl3_accept(SSL_HANDSHAKE *hs) {
190 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 int ret = -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin0d56f882015-12-19 17:05:56 -0500193 assert(ssl->handshake_func == ssl3_accept);
194 assert(ssl->server);
David Benjaminbeb47022014-11-30 02:58:52 -0500195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 for (;;) {
David Benjamin16b1b1d2017-02-10 23:41:14 -0500197 int state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamincb0c29f2016-12-12 17:00:50 -0500199 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400200 case SSL_ST_INIT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400201 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500202 hs->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500204
Adam Langleyfcf25832014-12-18 17:42:32 -0800205 case SSL3_ST_SR_CLNT_HELLO_A:
David Benjamin7934f082017-08-01 16:32:25 -0400206 ret = ssl3_read_client_hello(hs);
David Benjamin59bae5a2017-02-02 23:51:22 -0500207 if (ret <= 0) {
208 goto end;
Steven Valdez143e8b32016-07-11 13:19:03 -0400209 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500210 hs->state = SSL3_ST_SR_CLNT_HELLO_B;
211 break;
212
213 case SSL3_ST_SR_CLNT_HELLO_B:
David Benjamin59bae5a2017-02-02 23:51:22 -0500214 ret = ssl3_select_certificate(hs);
215 if (ret <= 0) {
216 goto end;
217 }
218 if (hs->state != SSL_ST_TLS13) {
David Benjamin7934f082017-08-01 16:32:25 -0400219 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
David Benjamin59bae5a2017-02-02 23:51:22 -0500220 }
221 break;
222
David Benjamin7934f082017-08-01 16:32:25 -0400223 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin59bae5a2017-02-02 23:51:22 -0500224 ret = ssl3_select_parameters(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 if (ret <= 0) {
226 goto end;
227 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500228 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900232 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 if (ret <= 0) {
234 goto end;
235 }
Steven Valdez87eab492016-06-27 16:34:59 -0400236 if (ssl->session != NULL) {
David Benjaminb5f55c32017-06-15 23:15:15 -0400237 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500239 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langleyfcf25832014-12-18 17:42:32 -0800243 case SSL3_ST_SW_CERT_A:
David Benjamin0d1730d2017-06-15 23:24:25 -0400244 ret = ssl3_send_server_certificate(hs);
245 if (ret <= 0) {
246 goto end;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100247 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500248 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100249 break;
250
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 case SSL3_ST_SW_KEY_EXCH_A:
David Benjamin44148742017-06-17 13:20:59 -0400252 if (hs->server_params_len > 0) {
David Benjaminc3c88822016-11-14 10:32:04 +0900253 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500254 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500256 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
David Benjamincb0c29f2016-12-12 17:00:50 -0500259 hs->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700261
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 case SSL3_ST_SW_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900263 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 if (ret <= 0) {
265 goto end;
266 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500267 hs->next_state = SSL3_ST_SR_CERT_A;
268 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900272 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900273 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 if (ret <= 0) {
275 goto end;
276 }
277 }
David Benjamin3a1dd462017-07-11 16:13:10 -0400278 hs->state = SSL3_ST_VERIFY_CLIENT_CERT;
279 break;
280
281 case SSL3_ST_VERIFY_CLIENT_CERT:
282 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
283 switch (ssl_verify_peer_cert(hs)) {
284 case ssl_verify_ok:
285 break;
286 case ssl_verify_invalid:
287 ret = -1;
288 goto end;
289 case ssl_verify_retry:
290 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
291 ret = -1;
292 goto end;
293 }
294 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500295 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 case SSL3_ST_SR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900299 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 if (ret <= 0) {
301 goto end;
302 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500303 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900307 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 if (ret <= 0) {
309 goto end;
310 }
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjamincb0c29f2016-12-12 17:00:50 -0500312 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjamina41280d2015-11-26 02:16:49 -0500315 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400316 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500317 if (ret <= 0) {
318 goto end;
319 }
Adam Langley95c29f32014-06-20 12:00:00 -0700320
David Benjamin67739722016-11-17 17:03:59 +0900321 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 ret = -1;
323 goto end;
324 }
David Benjamina41280d2015-11-26 02:16:49 -0500325
David Benjamincb0c29f2016-12-12 17:00:50 -0500326 hs->state = SSL3_ST_SR_NEXT_PROTO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 case SSL3_ST_SR_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900330 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900331 ret = ssl3_get_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400332 if (ret <= 0) {
333 goto end;
334 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500336 hs->state = SSL3_ST_SR_CHANNEL_ID_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 case SSL3_ST_SR_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400340 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900341 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400342 if (ret <= 0) {
343 goto end;
344 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500346 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800347 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700348
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900350 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 if (ret <= 0) {
352 goto end;
353 }
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Steven Valdez87eab492016-06-27 16:34:59 -0400355 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500356 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 } else {
David Benjaminb5f55c32017-06-15 23:15:15 -0400358 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 }
David Benjamindf50eec2016-06-07 16:49:42 -0400360
Steven Valdez87eab492016-06-27 16:34:59 -0400361 /* If this is a full handshake with ChannelID then record the handshake
David Benjamin45738dd2017-02-09 20:01:26 -0500362 * hashes in |hs->new_session| in case we need them to verify a
Steven Valdez87eab492016-06-27 16:34:59 -0400363 * ChannelID signature on a resumption of this session in the future. */
364 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Steven Valdez908ac192017-01-12 13:17:07 -0500365 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 if (ret <= 0) {
367 goto end;
368 }
369 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 case SSL3_ST_SW_FINISHED_A:
David Benjaminb5f55c32017-06-15 23:15:15 -0400373 ret = ssl3_send_server_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 if (ret <= 0) {
375 goto end;
376 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500377 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400378 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500379 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500381 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700384
David Benjamin9f1dc822016-06-07 17:03:46 -0400385 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500386 ret = ssl->method->flush_flight(ssl);
387 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400388 goto end;
389 }
390
David Benjamincb0c29f2016-12-12 17:00:50 -0500391 hs->state = hs->next_state;
David Benjamin9f1dc822016-06-07 17:03:46 -0400392 break;
393
Steven Valdez681eb6a2016-12-19 13:19:29 -0500394 case SSL_ST_TLS13: {
395 int early_return = 0;
396 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400397 if (ret <= 0) {
398 goto end;
399 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500400
401 if (early_return) {
402 ret = 1;
403 goto end;
404 }
405
David Benjamincb0c29f2016-12-12 17:00:50 -0500406 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400407 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500408 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 case SSL_ST_OK:
David Benjamin9bbdf582017-08-02 19:46:29 -0400411 ssl->method->on_handshake_complete(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Steven Valdez87eab492016-06-27 16:34:59 -0400413 /* If we aren't retaining peer certificates then we can discard it
414 * now. */
David Benjamin45738dd2017-02-09 20:01:26 -0500415 if (hs->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900416 ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -0500417 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
418 hs->new_session->certs = NULL;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400419 ssl->ctx->x509_method->session_clear(hs->new_session.get());
Steven Valdez87eab492016-06-27 16:34:59 -0400420 }
421
422 SSL_SESSION_free(ssl->s3->established_session);
423 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400424 SSL_SESSION_up_ref(ssl->session);
425 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400426 } else {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400427 ssl->s3->established_session = hs->new_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -0400428 ssl->s3->established_session->not_resumable = 0;
Steven Valdez87eab492016-06-27 16:34:59 -0400429 }
430
David Benjamin78476f62016-11-12 11:20:55 +0900431 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900432 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900433
David Benjamin4e9cc712016-06-01 20:16:03 -0400434 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 ret = 1;
436 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 default:
David Benjamin3570d732015-06-29 00:28:17 -0400439 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 ret = -1;
441 goto end;
442 }
Adam Langley95c29f32014-06-20 12:00:00 -0700443
David Benjamin16b1b1d2017-02-10 23:41:14 -0500444 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400445 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 }
448
Adam Langley95c29f32014-06-20 12:00:00 -0700449end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400450 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800451 return ret;
452}
Adam Langley95c29f32014-06-20 12:00:00 -0700453
David Benjamin731058e2016-12-03 23:15:13 -0500454int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
455 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400456 CBS cipher_suites;
457 CBS_init(&cipher_suites, client_hello->cipher_suites,
458 client_hello->cipher_suites_len);
459
460 while (CBS_len(&cipher_suites) > 0) {
461 uint16_t got_id;
462 if (!CBS_get_u16(&cipher_suites, &got_id)) {
463 return 0;
464 }
465
466 if (got_id == id) {
467 return 1;
468 }
469 }
470
471 return 0;
472}
473
David Benjaminf04c2e92016-12-06 13:35:25 -0500474static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500475 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500476 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500477 assert(!ssl->s3->have_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400478 CBS supported_versions, versions;
David Benjamin731058e2016-12-03 23:15:13 -0500479 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
480 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400481 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
482 CBS_len(&supported_versions) != 0 ||
483 CBS_len(&versions) == 0) {
484 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
485 *out_alert = SSL_AD_DECODE_ERROR;
486 return 0;
487 }
David Benjamin2dc02042016-09-19 19:57:37 -0400488 } else {
Steven Valdez8f36c512017-06-20 10:55:02 -0400489 /* Convert the ClientHello version to an equivalent supported_versions
490 * extension. */
491 static const uint8_t kTLSVersions[] = {
492 0x03, 0x03, /* TLS 1.2 */
493 0x03, 0x02, /* TLS 1.1 */
494 0x03, 0x01, /* TLS 1 */
495 0x03, 0x00, /* SSL 3 */
496 };
497
498 static const uint8_t kDTLSVersions[] = {
499 0xfe, 0xfd, /* DTLS 1.2 */
500 0xfe, 0xff, /* DTLS 1.0 */
501 };
502
503 size_t versions_len = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400504 if (SSL_is_dtls(ssl)) {
505 if (client_hello->version <= DTLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400506 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400507 } else if (client_hello->version <= DTLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400508 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400509 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400510 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
511 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400512 } else {
513 if (client_hello->version >= TLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400514 versions_len = 8;
Steven Valdezfdd10992016-09-15 16:27:05 -0400515 } else if (client_hello->version >= TLS1_1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400516 versions_len = 6;
Steven Valdezfdd10992016-09-15 16:27:05 -0400517 } else if (client_hello->version >= TLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400518 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400519 } else if (client_hello->version >= SSL3_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400520 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400521 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400522 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
523 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400524 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400525 }
526
Steven Valdez8f36c512017-06-20 10:55:02 -0400527 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400528 return 0;
529 }
530
David Benjamin25fe85b2016-08-09 20:00:32 -0400531 /* At this point, the connection's version is known and |ssl->version| is
532 * fixed. Begin enforcing the record-layer version. */
533 ssl->s3->have_version = 1;
534
Steven Valdez8f36c512017-06-20 10:55:02 -0400535 /* Handle FALLBACK_SCSV. */
536 if (ssl_client_cipher_list_contains_cipher(client_hello,
537 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
538 ssl3_protocol_version(ssl) < hs->max_version) {
539 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
540 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
541 return 0;
542 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400543
Steven Valdez8f36c512017-06-20 10:55:02 -0400544 return 1;
David Benjamin25fe85b2016-08-09 20:00:32 -0400545}
546
David Benjaminee910bf2017-07-25 22:36:00 -0400547static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
548 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin2578b292016-12-03 23:19:55 -0500549 CBS cipher_suites;
550 CBS_init(&cipher_suites, client_hello->cipher_suites,
551 client_hello->cipher_suites_len);
552
David Benjaminee910bf2017-07-25 22:36:00 -0400553 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
554 if (!sk) {
David Benjamin2578b292016-12-03 23:19:55 -0500555 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminee910bf2017-07-25 22:36:00 -0400556 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500557 }
558
559 while (CBS_len(&cipher_suites) > 0) {
560 uint16_t cipher_suite;
561
562 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
563 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjaminee910bf2017-07-25 22:36:00 -0400564 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500565 }
566
567 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
David Benjaminee910bf2017-07-25 22:36:00 -0400568 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500569 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminee910bf2017-07-25 22:36:00 -0400570 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500571 }
572 }
573
574 return sk;
David Benjamin2578b292016-12-03 23:19:55 -0500575}
576
577/* ssl_get_compatible_server_ciphers determines the key exchange and
578 * authentication cipher suite masks compatible with the server configuration
579 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
580 * exchange mask and |*out_mask_a| to the authentication mask. */
581static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
582 uint32_t *out_mask_k,
583 uint32_t *out_mask_a) {
584 SSL *const ssl = hs->ssl;
David Benjamin2578b292016-12-03 23:19:55 -0500585 uint32_t mask_k = 0;
586 uint32_t mask_a = 0;
587
Adam Langley3a2b47a2017-01-24 13:59:42 -0800588 if (ssl_has_certificate(ssl)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400589 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
590 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin2578b292016-12-03 23:19:55 -0500591 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500592 }
593 }
594
David Benjamin2578b292016-12-03 23:19:55 -0500595 /* Check for a shared group to consider ECDHE ciphers. */
596 uint16_t unused;
597 if (tls1_get_shared_group(hs, &unused)) {
598 mask_k |= SSL_kECDHE;
599 }
600
David Benjamin2578b292016-12-03 23:19:55 -0500601 /* PSK requires a server callback. */
602 if (ssl->psk_server_callback != NULL) {
603 mask_k |= SSL_kPSK;
604 mask_a |= SSL_aPSK;
605 }
606
607 *out_mask_k = mask_k;
608 *out_mask_a = mask_a;
609}
610
611static const SSL_CIPHER *ssl3_choose_cipher(
612 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
613 const struct ssl_cipher_preference_list_st *server_pref) {
614 SSL *const ssl = hs->ssl;
David Benjaminccbb1652017-03-22 18:24:32 -0400615 STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjamin2578b292016-12-03 23:19:55 -0500616 /* in_group_flags will either be NULL, or will point to an array of bytes
617 * which indicate equal-preference groups in the |prio| stack. See the
618 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
619 * struct. */
620 const uint8_t *in_group_flags;
621 /* group_min contains the minimal index so far found in a group, or -1 if no
622 * such value exists yet. */
623 int group_min = -1;
624
David Benjaminee910bf2017-07-25 22:36:00 -0400625 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
David Benjaminccbb1652017-03-22 18:24:32 -0400626 ssl_parse_client_cipher_list(client_hello);
David Benjaminee910bf2017-07-25 22:36:00 -0400627 if (!client_pref) {
628 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500629 }
630
631 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjaminccbb1652017-03-22 18:24:32 -0400632 prio = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500633 in_group_flags = server_pref->in_group_flags;
David Benjaminee910bf2017-07-25 22:36:00 -0400634 allow = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500635 } else {
David Benjaminee910bf2017-07-25 22:36:00 -0400636 prio = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500637 in_group_flags = NULL;
David Benjaminccbb1652017-03-22 18:24:32 -0400638 allow = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500639 }
640
David Benjaminccbb1652017-03-22 18:24:32 -0400641 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -0500642 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
643
644 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400645 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500646
David Benjaminccbb1652017-03-22 18:24:32 -0400647 size_t cipher_index;
648 if (/* Check if the cipher is supported for the current version. */
649 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
650 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
651 /* Check the cipher is supported for the server configuration. */
652 (c->algorithm_mkey & mask_k) &&
653 (c->algorithm_auth & mask_a) &&
654 /* Check the cipher is in the |allow| list. */
655 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500656 if (in_group_flags != NULL && in_group_flags[i] == 1) {
657 /* This element of |prio| is in a group. Update the minimum index found
658 * so far and continue looking. */
659 if (group_min == -1 || (size_t)group_min > cipher_index) {
660 group_min = cipher_index;
661 }
662 } else {
663 if (group_min != -1 && (size_t)group_min < cipher_index) {
664 cipher_index = group_min;
665 }
David Benjaminee910bf2017-07-25 22:36:00 -0400666 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin2578b292016-12-03 23:19:55 -0500667 }
668 }
669
670 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
671 /* We are about to leave a group, but we found a match in it, so that's
672 * our answer. */
David Benjaminee910bf2017-07-25 22:36:00 -0400673 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin2578b292016-12-03 23:19:55 -0500674 }
675 }
676
David Benjaminee910bf2017-07-25 22:36:00 -0400677 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500678}
679
David Benjamin7934f082017-08-01 16:32:25 -0400680static int ssl3_read_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900681 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400682 SSLMessage msg;
683 int ret = ssl_read_message(ssl, &msg);
684 if (ret <= 0) {
685 return ret;
686 }
687 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500688 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 }
Adam Langley95c29f32014-06-20 12:00:00 -0700690
David Benjamin731058e2016-12-03 23:15:13 -0500691 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400692 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin3570d732015-06-29 00:28:17 -0400693 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin59bae5a2017-02-02 23:51:22 -0500694 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
695 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800696 }
Adam Langley95c29f32014-06-20 12:00:00 -0700697
David Benjamin59bae5a2017-02-02 23:51:22 -0500698 /* Run the early callback. */
699 if (ssl->ctx->select_certificate_cb != NULL) {
700 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000701 case ssl_select_cert_retry:
David Benjamin59bae5a2017-02-02 23:51:22 -0500702 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
703 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400704
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000705 case ssl_select_cert_error:
David Benjamin59bae5a2017-02-02 23:51:22 -0500706 /* Connection rejected. */
707 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
708 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
709 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400710
David Benjamin59bae5a2017-02-02 23:51:22 -0500711 default:
712 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400713 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500714 }
David Benjamine14ff062016-08-09 16:21:24 -0400715
David Benjamin68161cb2017-06-20 14:49:43 -0400716 /* Freeze the version range after the early callback. */
717 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
718 return -1;
719 }
720
Adam Langley04540a72017-02-09 10:11:50 -0800721 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500722 if (!negotiate_version(hs, &alert, &client_hello)) {
723 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
724 return -1;
725 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400726
Steven Valdez8f36c512017-06-20 10:55:02 -0400727 hs->client_version = client_hello.version;
David Benjamin59bae5a2017-02-02 23:51:22 -0500728 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
729 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
730 return -1;
731 }
732 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
733 client_hello.random_len);
734
735 /* Only null compression is supported. TLS 1.3 further requires the peer
736 * advertise no other compression. */
737 if (OPENSSL_memchr(client_hello.compression_methods, 0,
738 client_hello.compression_methods_len) == NULL ||
739 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
740 client_hello.compression_methods_len != 1)) {
741 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
742 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
743 return -1;
744 }
745
746 /* TLS extensions. */
747 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
748 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
749 return -1;
750 }
751
752 return 1;
753}
754
755static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
756 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400757 SSLMessage msg;
758 int ret = ssl_read_message(ssl, &msg);
759 if (ret <= 0) {
760 return ret;
761 }
762
David Benjamin59bae5a2017-02-02 23:51:22 -0500763 /* Call |cert_cb| to update server certificates if required. */
764 if (ssl->cert->cert_cb != NULL) {
765 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
766 if (rv == 0) {
767 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
768 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin25fe85b2016-08-09 20:00:32 -0400769 return -1;
770 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500771 if (rv < 0) {
772 ssl->rwstate = SSL_X509_LOOKUP;
773 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900774 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900775 }
776
David Benjamina232a712017-03-30 15:51:53 -0500777 if (!ssl_on_certificate_selected(hs)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500778 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900779 }
780
David Benjamin59bae5a2017-02-02 23:51:22 -0500781 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
782 /* Jump to the TLS 1.3 state machine. */
783 hs->state = SSL_ST_TLS13;
784 hs->do_tls13_handshake = tls13_server_handshake;
785 return 1;
786 }
787
788 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400789 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500790 return -1;
791 }
792
793 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
794 * certificate is finalized. */
David Benjamin45738dd2017-02-09 20:01:26 -0500795 hs->new_cipher =
David Benjamin59bae5a2017-02-02 23:51:22 -0500796 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjamin45738dd2017-02-09 20:01:26 -0500797 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500798 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
799 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
800 return -1;
801 }
802
803 return 1;
804}
805
806static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
807 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -0400808 SSLMessage msg;
809 int ret = ssl_read_message(ssl, &msg);
810 if (ret <= 0) {
811 return ret;
812 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500813 SSL_CLIENT_HELLO client_hello;
David Benjamin7934f082017-08-01 16:32:25 -0400814 if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500815 return -1;
816 }
David Benjamin34202b92016-11-16 19:07:53 +0900817
818 /* Determine whether we are doing session resumption. */
David Benjamin37af90f2017-07-29 01:42:16 -0400819 UniquePtr<SSL_SESSION> session;
David Benjamin34202b92016-11-16 19:07:53 +0900820 int tickets_supported = 0, renew_ticket = 0;
David Benjamin37af90f2017-07-29 01:42:16 -0400821 switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket,
822 &client_hello)) {
David Benjamin34202b92016-11-16 19:07:53 +0900823 case ssl_session_success:
824 break;
825 case ssl_session_error:
David Benjamind781fc42017-07-12 16:25:57 -0400826 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900827 case ssl_session_retry:
828 ssl->rwstate = SSL_PENDING_SESSION;
David Benjamind781fc42017-07-12 16:25:57 -0400829 return -1;
Adam Langley4c341d02017-03-08 19:33:21 -0800830 case ssl_session_ticket_retry:
831 ssl->rwstate = SSL_PENDING_TICKET;
David Benjamind781fc42017-07-12 16:25:57 -0400832 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900833 }
834
David Benjamind781fc42017-07-12 16:25:57 -0400835 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500836 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900837 /* A ClientHello without EMS that attempts to resume a session with EMS
838 * is fatal to the connection. */
David Benjamin34202b92016-11-16 19:07:53 +0900839 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400840 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamind781fc42017-07-12 16:25:57 -0400841 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900842 }
843
David Benjamind781fc42017-07-12 16:25:57 -0400844 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjamin34202b92016-11-16 19:07:53 +0900845 /* If the client offers the EMS extension, but the previous session
846 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500847 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400848 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900849 }
850 }
851
David Benjamind781fc42017-07-12 16:25:57 -0400852 if (session) {
David Benjamin34202b92016-11-16 19:07:53 +0900853 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900854 hs->ticket_expected = renew_ticket;
David Benjamind781fc42017-07-12 16:25:57 -0400855 ssl->session = session.release();
David Benjamin34202b92016-11-16 19:07:53 +0900856 ssl->s3->session_reused = 1;
857 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900858 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900859 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900860 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamind781fc42017-07-12 16:25:57 -0400861 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900862 }
863
864 /* Clear the session ID if we want the session to be single-use. */
865 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500866 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900867 }
868 }
869
870 if (ssl->ctx->dos_protection_cb != NULL &&
871 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
872 /* Connection rejected for DOS reasons. */
David Benjamin34202b92016-11-16 19:07:53 +0900873 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400874 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400875 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900876 }
877
David Benjaminf01f42a2016-11-16 19:05:33 +0900878 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500879 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400880
David Benjamin4eb95cc2016-11-16 17:08:23 +0900881 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900882 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500883 OPENSSL_free(hs->new_session->tlsext_hostname);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400884 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
David Benjamin45738dd2017-02-09 20:01:26 -0500885 if (hs->new_session->tlsext_hostname == NULL) {
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 Benjamin4eb95cc2016-11-16 17:08:23 +0900888 }
889 }
890
David Benjamin5c1ce292015-05-26 16:59:43 -0400891 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900892 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400893 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500894 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
895 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900896 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400897 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400898 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500899 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900900 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400901 }
Adam Langley37646832016-08-01 16:16:46 -0700902
David Benjaminc3c88822016-11-14 10:32:04 +0900903 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700904 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400905 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500906 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700907 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 }
909
David Benjaminf01f42a2016-11-16 19:05:33 +0900910 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
911 * deferred. Complete it now. */
David Benjamind98107b2017-06-15 22:56:37 -0400912 uint8_t alert = SSL_AD_DECODE_ERROR;
913 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
914 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamind781fc42017-07-12 16:25:57 -0400915 return -1;
David Benjamin9ef31f02016-10-31 18:01:13 -0400916 }
917
David Benjaminf71036e2017-01-21 14:49:39 -0500918 /* Now that all parameters are known, initialize the handshake hash and hash
919 * the ClientHello. */
David Benjaminca9e8f52017-08-09 15:02:34 -0400920 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
David Benjamin7934f082017-08-01 16:32:25 -0400921 !ssl_hash_message(hs, msg)) {
David Benjamind98107b2017-06-15 22:56:37 -0400922 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400923 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -0400924 }
925
Steven Valdez2b8415e2016-06-30 13:27:23 -0400926 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +0900927 if (!hs->cert_request) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400928 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 }
930
David Benjamin8f94c312017-08-01 17:35:55 -0400931 ssl->method->next_message(ssl);
David Benjamind781fc42017-07-12 16:25:57 -0400932 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800933}
Adam Langley95c29f32014-06-20 12:00:00 -0700934
David Benjaminc3c88822016-11-14 10:32:04 +0900935static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
936 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400937
938 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
939 * known attack while we fix ChannelID itself. */
940 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -0500941 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400942 ssl->s3->tlsext_channel_id_valid = 0;
943 }
944
945 /* If this is a resumption and the original handshake didn't support
946 * ChannelID then we didn't record the original handshake hashes in the
947 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -0400948 if (ssl->session != NULL &&
949 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400950 ssl->s3->tlsext_channel_id_valid = 0;
951 }
952
David Benjaminad8f5e12017-02-20 17:00:20 -0500953 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -0400954 ssl_get_current_time(ssl, &now);
955 ssl->s3->server_random[0] = now.tv_sec >> 24;
956 ssl->s3->server_random[1] = now.tv_sec >> 16;
957 ssl->s3->server_random[2] = now.tv_sec >> 8;
958 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400959 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -0400960 return -1;
961 }
962
David Benjamin55108632016-08-11 22:01:18 -0400963 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
964 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400965
David Benjamin31b0c9b2017-07-20 14:49:15 -0400966 const SSL_SESSION *session = hs->new_session.get();
Steven Valdez87eab492016-06-27 16:34:59 -0400967 if (ssl->session != NULL) {
968 session = ssl->session;
969 }
970
David Benjamin1386aad2017-07-19 23:57:40 -0400971 ScopedCBB cbb;
972 CBB body, session_id;
973 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -0400974 !CBB_add_u16(&body, ssl->version) ||
975 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
976 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400977 !CBB_add_bytes(&session_id, session->session_id,
978 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500979 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -0400980 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500981 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400982 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -0400983 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -0400984 return -1;
985 }
986
David Benjamin16315f72017-01-12 20:02:05 -0500987 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800988}
Adam Langley95c29f32014-06-20 12:00:00 -0700989
David Benjaminc3c88822016-11-14 10:32:04 +0900990static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
991 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400992 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -0400993
David Benjamin44148742017-06-17 13:20:59 -0400994 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
995 if (!ssl_has_certificate(ssl)) {
996 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamind781fc42017-07-12 16:25:57 -0400997 return -1;
David Benjamin44148742017-06-17 13:20:59 -0400998 }
David Benjamin32a66d52016-07-13 22:03:11 -0400999
David Benjamin44148742017-06-17 13:20:59 -04001000 if (!ssl3_output_cert_chain(ssl)) {
David Benjamind781fc42017-07-12 16:25:57 -04001001 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001002 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001003
David Benjamin44148742017-06-17 13:20:59 -04001004 if (hs->certificate_status_expected) {
1005 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -04001006 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -04001007 SSL3_MT_CERTIFICATE_STATUS) ||
1008 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1009 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
1010 !CBB_add_bytes(&ocsp_response,
1011 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
1012 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamind781fc42017-07-12 16:25:57 -04001013 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -04001014 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001015 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001016 }
David Benjamin0d1730d2017-06-15 23:24:25 -04001017 }
David Benjamin75836432016-06-17 18:48:29 -04001018 }
1019
David Benjamin44148742017-06-17 13:20:59 -04001020 /* Assemble ServerKeyExchange parameters if needed. */
1021 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1022 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1023 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
1024 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001025
David Benjamin44148742017-06-17 13:20:59 -04001026 /* Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
1027 * the client and server randoms for the signing transcript. */
1028 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -04001029 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
1030 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1031 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
1032 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001033 }
1034
David Benjamin2a0b3912015-12-18 01:01:21 -05001035 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001037 size_t len =
1038 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
David Benjamind781fc42017-07-12 16:25:57 -04001039 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001040 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1041 len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001042 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001046 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001047 /* Determine the group to use. */
1048 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001049 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001050 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -05001051 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamind781fc42017-07-12 16:25:57 -04001052 return -1;
1053 }
David Benjamin45738dd2017-02-09 20:01:26 -05001054 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001055
David Benjamin4298d772015-12-19 00:18:25 -05001056 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc642aca2017-07-19 23:28:43 -04001057 hs->key_share = SSLKeyShare::Create(group_id);
1058 if (!hs->key_share ||
David Benjamind781fc42017-07-12 16:25:57 -04001059 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
1060 !CBB_add_u16(cbb.get(), group_id) ||
1061 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc642aca2017-07-19 23:28:43 -04001062 !hs->key_share->Offer(&child)) {
David Benjamind781fc42017-07-12 16:25:57 -04001063 return -1;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001064 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001065 } else {
1066 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001067 }
Adam Langley95c29f32014-06-20 12:00:00 -07001068
David Benjamind781fc42017-07-12 16:25:57 -04001069 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
1070 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001071 }
David Benjaminc42acee2016-06-17 17:47:58 -04001072 }
1073
David Benjamind781fc42017-07-12 16:25:57 -04001074 return 1;
David Benjamin44148742017-06-17 13:20:59 -04001075}
1076
1077static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1078 SSL *const ssl = hs->ssl;
David Benjamin1386aad2017-07-19 23:57:40 -04001079 ScopedCBB cbb;
1080 CBB body, child;
1081 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001082 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin44148742017-06-17 13:20:59 -04001083 /* |hs->server_params| contains a prefix for signing. */
1084 hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
1085 !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
1086 hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001087 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001088 }
Adam Langley95c29f32014-06-20 12:00:00 -07001089
David Benjamin2a0b3912015-12-18 01:01:21 -05001090 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -05001091 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001092 if (!ssl_has_private_key(ssl)) {
1093 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001094 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 }
Adam Langley95c29f32014-06-20 12:00:00 -07001096
David Benjaminc42acee2016-06-17 17:47:58 -04001097 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001098 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001099 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001100 return -1;
David Benjaminea9a0d52016-07-08 15:52:59 -07001101 }
David Benjaminc42acee2016-06-17 17:47:58 -04001102 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001103 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001104 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1105 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001106 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001107 }
David Benjaminc42acee2016-06-17 17:47:58 -04001108 }
1109
1110 /* Add space for the signature. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001111 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -04001112 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001113 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001114 !CBB_reserve(&child, &ptr, max_sig_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001115 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001116 }
1117
David Benjamin2a0b3912015-12-18 01:01:21 -05001118 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001119 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1120 signature_algorithm, hs->server_params,
1121 hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001122 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001123 if (!CBB_did_write(&child, sig_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001124 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001125 }
1126 break;
1127 case ssl_private_key_failure:
David Benjamin1386aad2017-07-19 23:57:40 -04001128 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001129 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001130 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin1386aad2017-07-19 23:57:40 -04001131 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001132 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001133 }
1134
David Benjamin1386aad2017-07-19 23:57:40 -04001135 if (!ssl_add_message_cbb(ssl, cbb.get())) {
1136 return -1;
David Benjamin1f9f9c42015-08-28 16:17:59 -04001137 }
David Benjaminc42acee2016-06-17 17:47:58 -04001138
David Benjaminc3c88822016-11-14 10:32:04 +09001139 OPENSSL_free(hs->server_params);
1140 hs->server_params = NULL;
1141 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001142
David Benjamin16315f72017-01-12 20:02:05 -05001143 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001144}
Adam Langley95c29f32014-06-20 12:00:00 -07001145
David Benjamin0d1730d2017-06-15 23:24:25 -04001146static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001147 SSL *const ssl = hs->ssl;
David Benjamin1386aad2017-07-19 23:57:40 -04001148 ScopedCBB cbb;
1149 CBB body;
David Benjamin75836432016-06-17 18:48:29 -04001150
David Benjamin0d1730d2017-06-15 23:24:25 -04001151 if (hs->cert_request) {
1152 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -04001153 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -04001154 SSL3_MT_CERTIFICATE_REQUEST) ||
1155 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1156 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
1157 (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
1158 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
1159 (ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
1160 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
1161 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
1162 !ssl_add_client_CA_list(ssl, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001163 !ssl_add_message_cbb(ssl, cbb.get())) {
1164 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1165 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001166 }
1167 }
1168
David Benjamin1386aad2017-07-19 23:57:40 -04001169 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1170 SSL3_MT_SERVER_HELLO_DONE) ||
1171 !ssl_add_message_cbb(ssl, cbb.get())) {
1172 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1173 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001174 }
1175
David Benjamin16315f72017-01-12 20:02:05 -05001176 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001177}
Adam Langley95c29f32014-06-20 12:00:00 -07001178
David Benjaminc3c88822016-11-14 10:32:04 +09001179static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1180 SSL *const ssl = hs->ssl;
1181 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001182
David Benjamin7934f082017-08-01 16:32:25 -04001183 SSLMessage msg;
1184 int ret = ssl_read_message(ssl, &msg);
1185 if (ret <= 0) {
1186 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001187 }
1188
David Benjamin7934f082017-08-01 16:32:25 -04001189 if (msg.type != SSL3_MT_CERTIFICATE) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001190 if (ssl->version == SSL3_VERSION &&
David Benjamin7934f082017-08-01 16:32:25 -04001191 msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001192 /* In SSL 3.0, the Certificate message is omitted to signal no
1193 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001194 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001195 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001196 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1197 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001198 }
1199
Adam Langley37646832016-08-01 16:16:46 -07001200 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001201 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001202 hs->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001203 return 1;
1204 }
1205
David Benjamin9f1dc822016-06-07 17:03:46 -04001206 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001207 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1208 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001209 }
1210
David Benjamin7934f082017-08-01 16:32:25 -04001211 if (!ssl_hash_message(hs, msg)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001212 return -1;
1213 }
1214
David Benjamin7934f082017-08-01 16:32:25 -04001215 CBS certificate_msg = msg.body;
Adam Langleyc68e5b92017-02-08 13:33:15 -08001216 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -04001217 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
1218 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
1219 ssl->retain_only_sha256_of_client_certs
1220 ? hs->new_session->peer_sha256
1221 : NULL,
1222 &certificate_msg, ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001223 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001224 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001225 }
David Benjamine664a532017-07-20 20:19:36 -04001226 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1227 hs->new_session->certs = chain.release();
David Benjamin9f1dc822016-06-07 17:03:46 -04001228
Adam Langley68e71242016-12-12 11:06:16 -08001229 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001230 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001231 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001232 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001233 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001234 }
1235
David Benjamin45738dd2017-02-09 20:01:26 -05001236 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001237 /* No client certificate so the handshake buffer may be discarded. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001238 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001239
David Benjamine455e512016-08-01 20:11:13 -04001240 /* In SSL 3.0, sending no certificate is signaled by omitting the
1241 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001242 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001243 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
David Benjamin5c900c82016-07-13 23:03:26 -04001244 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001245 return -1;
David Benjamine455e512016-08-01 20:11:13 -04001246 }
1247
1248 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001249 /* Fail for TLS only if we required a certificate */
1250 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001251 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001252 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001253 }
Adam Langley37646832016-08-01 16:16:46 -07001254
1255 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001256 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001257 hs->new_session->verify_result = X509_V_OK;
David Benjamin8f94c312017-08-01 17:35:55 -04001258 } else if (ssl->retain_only_sha256_of_client_certs) {
1259 /* The hash will have been filled in. */
David Benjamin45738dd2017-02-09 20:01:26 -05001260 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001261 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001262
David Benjamin8f94c312017-08-01 17:35:55 -04001263 ssl->method->next_message(ssl);
David Benjamin5c900c82016-07-13 23:03:26 -04001264 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001265}
1266
David Benjaminc3c88822016-11-14 10:32:04 +09001267static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1268 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001269 uint8_t *premaster_secret = NULL;
1270 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001271 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001272
David Benjamin7934f082017-08-01 16:32:25 -04001273 SSLMessage msg;
1274 int ret = ssl_read_message(ssl, &msg);
David Benjamin8f94c312017-08-01 17:35:55 -04001275 if (ret <= 0) {
1276 return ret;
David Benjamin44148742017-06-17 13:20:59 -04001277 }
David Benjamin276b7e82017-01-21 14:13:39 -05001278
David Benjamin7934f082017-08-01 16:32:25 -04001279 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjamin44148742017-06-17 13:20:59 -04001280 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 }
Adam Langley95c29f32014-06-20 12:00:00 -07001282
David Benjamin7934f082017-08-01 16:32:25 -04001283 CBS client_key_exchange = msg.body;
David Benjamin44148742017-06-17 13:20:59 -04001284 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1285 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001286
David Benjamin44148742017-06-17 13:20:59 -04001287 /* If using a PSK key exchange, parse the PSK identity. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001288 if (alg_a & SSL_aPSK) {
1289 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001290
Adam Langleyfcf25832014-12-18 17:42:32 -08001291 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1292 * then this is the only field in the message. */
1293 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1294 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001295 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001296 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1297 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001298 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001299
Adam Langleyfcf25832014-12-18 17:42:32 -08001300 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1301 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001302 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001303 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1304 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001305 }
David Benjamin35c02302014-07-13 04:14:59 -04001306
David Benjamin45738dd2017-02-09 20:01:26 -05001307 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001308 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001309 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1310 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001312 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001313
Adam Langleyfcf25832014-12-18 17:42:32 -08001314 /* Depending on the key exchange method, compute |premaster_secret| and
1315 * |premaster_secret_len|. */
1316 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001317 CBS encrypted_premaster_secret;
1318 if (ssl->version > SSL3_VERSION) {
1319 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1320 &encrypted_premaster_secret) ||
1321 CBS_len(&client_key_exchange) != 0) {
1322 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1323 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1324 goto err;
1325 }
1326 } else {
1327 encrypted_premaster_secret = client_key_exchange;
1328 }
1329
Adam Langleyfcf25832014-12-18 17:42:32 -08001330 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001331 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamind781fc42017-07-12 16:25:57 -04001332 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001333 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001334 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001335 goto err;
1336 }
David Benjamin35c02302014-07-13 04:14:59 -04001337
David Benjamin44148742017-06-17 13:20:59 -04001338 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1339 * timing-sensitive code below. */
David Benjamin3f5b43d2015-12-01 19:31:24 -05001340 size_t decrypt_len;
David Benjamin44148742017-06-17 13:20:59 -04001341 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1342 CBS_data(&encrypted_premaster_secret),
1343 CBS_len(&encrypted_premaster_secret))) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001344 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001345 break;
1346 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001347 goto err;
1348 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001349 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001350 goto err;
1351 }
1352
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001353 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001354 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001355 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1356 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001357 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001358
David Benjamin3f5b43d2015-12-01 19:31:24 -05001359 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1360 * section 7.4.7.1. */
1361 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001362 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001363 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001364 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001365 goto err;
1366 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001367 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001368 goto err;
1369 }
1370
David Benjamin3f5b43d2015-12-01 19:31:24 -05001371 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1372 * publicly invalid. */
1373 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001374 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001375 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1376 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 }
David Benjamin35c02302014-07-13 04:14:59 -04001378
David Benjamin3f5b43d2015-12-01 19:31:24 -05001379 /* Check the padding. See RFC 3447, section 7.2.2. */
1380 size_t padding_len = decrypt_len - premaster_secret_len;
1381 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1382 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001383 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001384 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1385 }
1386 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1387
1388 /* The premaster secret must begin with |client_version|. This too must be
1389 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1390 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001391 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001392 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001393 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001394
1395 /* Select, in constant time, either the decrypted premaster or the random
1396 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001397 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001398 premaster_secret[i] = constant_time_select_8(
1399 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1400 }
1401
1402 OPENSSL_free(decrypt_buf);
1403 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001404 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001405 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001406 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001407 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001408 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001409 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001410 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1411 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001412 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001413
David Benjamin4298d772015-12-19 00:18:25 -05001414 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001415 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001416 if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
1417 CBS_data(&peer_key), CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001418 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1419 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001420 }
Adam Langley95c29f32014-06-20 12:00:00 -07001421
David Benjamin4298d772015-12-19 00:18:25 -05001422 /* The key exchange state may now be discarded. */
David Benjaminc642aca2017-07-19 23:28:43 -04001423 hs->key_share.reset();
David Benjamin44148742017-06-17 13:20:59 -04001424 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001425 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001426 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1427 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001428 }
Adam Langley95c29f32014-06-20 12:00:00 -07001429
Adam Langleyfcf25832014-12-18 17:42:32 -08001430 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1431 * pre-shared key. */
1432 if (alg_a & SSL_aPSK) {
David Benjamin44148742017-06-17 13:20:59 -04001433 if (ssl->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001434 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin44148742017-06-17 13:20:59 -04001435 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1436 goto err;
1437 }
1438
1439 /* Look up the key for the identity. */
1440 uint8_t psk[PSK_MAX_PSK_LEN];
1441 unsigned psk_len = ssl->psk_server_callback(
1442 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1443 if (psk_len > PSK_MAX_PSK_LEN) {
1444 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1445 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1446 goto err;
1447 } else if (psk_len == 0) {
1448 /* PSK related to the given identity not found */
1449 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1450 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1451 goto err;
1452 }
1453
1454 if (alg_k & SSL_kPSK) {
1455 /* In plain PSK, other_secret is a block of 0s with the same length as the
1456 * pre-shared key. */
1457 premaster_secret_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001458 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
David Benjamin44148742017-06-17 13:20:59 -04001459 if (premaster_secret == NULL) {
1460 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1461 goto err;
1462 }
1463 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
1464 }
1465
David Benjamin1386aad2017-07-19 23:57:40 -04001466 ScopedCBB new_premaster;
1467 CBB child;
Adam Langleyfcf25832014-12-18 17:42:32 -08001468 uint8_t *new_data;
1469 size_t new_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001470 if (!CBB_init(new_premaster.get(),
1471 2 + psk_len + 2 + premaster_secret_len) ||
1472 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001473 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001474 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001475 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001476 !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001477 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001478 goto err;
1479 }
David Benjamin14c83e72014-07-13 04:54:57 -04001480
Adam Langleyfcf25832014-12-18 17:42:32 -08001481 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1482 OPENSSL_free(premaster_secret);
1483 premaster_secret = new_data;
1484 premaster_secret_len = new_len;
1485 }
David Benjamin14c83e72014-07-13 04:54:57 -04001486
David Benjamin7934f082017-08-01 16:32:25 -04001487 if (!ssl_hash_message(hs, msg)) {
David Benjamin44148742017-06-17 13:20:59 -04001488 goto err;
1489 }
1490
Adam Langleyfcf25832014-12-18 17:42:32 -08001491 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001492 hs->new_session->master_key_length = tls1_generate_master_secret(
1493 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1494 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001495 goto err;
1496 }
David Benjaminfc02b592017-02-17 16:26:01 -05001497 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001498
Adam Langleyfcf25832014-12-18 17:42:32 -08001499 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1500 OPENSSL_free(premaster_secret);
David Benjamin8f94c312017-08-01 17:35:55 -04001501 ssl->method->next_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001502 return 1;
1503
Adam Langley95c29f32014-06-20 12:00:00 -07001504err:
David Benjamin4298d772015-12-19 00:18:25 -05001505 if (premaster_secret != NULL) {
1506 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 OPENSSL_free(premaster_secret);
1508 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001509 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 return -1;
1512}
Adam Langley95c29f32014-06-20 12:00:00 -07001513
David Benjaminc3c88822016-11-14 10:32:04 +09001514static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1515 SSL *const ssl = hs->ssl;
David Benjamin6553b372014-07-22 14:11:30 -04001516
Adam Langleyfcf25832014-12-18 17:42:32 -08001517 /* Only RSA and ECDSA client certificates are supported, so a
1518 * CertificateVerify is required if and only if there's a client certificate.
1519 * */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001520 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001521 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 return 1;
1523 }
Adam Langley95c29f32014-06-20 12:00:00 -07001524
David Benjamin7934f082017-08-01 16:32:25 -04001525 SSLMessage msg;
1526 int ret = ssl_read_message(ssl, &msg);
1527 if (ret <= 0) {
1528 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 }
David Benjaminef865502014-08-24 02:48:34 -04001530
David Benjamin7934f082017-08-01 16:32:25 -04001531 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001532 return -1;
1533 }
1534
David Benjamin7934f082017-08-01 16:32:25 -04001535 CBS certificate_verify = msg.body, signature;
David Benjamin6897dbe2014-07-12 20:18:28 -04001536
David Benjamin7934f082017-08-01 16:32:25 -04001537 /* Determine the signature algorithm. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001538 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001539 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001540 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001541 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001542 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1543 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001544 }
David Benjamin8d606e32017-06-15 22:43:04 -04001545 uint8_t alert = SSL_AD_DECODE_ERROR;
1546 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001547 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1548 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001549 }
David Benjamin45738dd2017-02-09 20:01:26 -05001550 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001551 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001552 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001553 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001554 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1555 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001556 }
David Benjamin854dd652014-08-26 00:32:30 -04001557
Adam Langleyfcf25832014-12-18 17:42:32 -08001558 /* Parse and verify the signature. */
1559 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1560 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001561 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001562 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1563 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001564 }
Adam Langley95c29f32014-06-20 12:00:00 -07001565
Steven Valdez2b8415e2016-06-30 13:27:23 -04001566 int sig_ok;
1567 /* The SSL3 construction for CertificateVerify does not decompose into a
1568 * single final digest and signature, and must be special-cased. */
1569 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001570 uint8_t digest[EVP_MAX_MD_SIZE];
1571 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001572 if (!hs->transcript.GetSSL3CertVerifyHash(
1573 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001574 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001575 }
1576
David Benjamin31b0c9b2017-07-20 14:49:15 -04001577 UniquePtr<EVP_PKEY_CTX> pctx(
1578 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
David Benjamin1386aad2017-07-19 23:57:40 -04001579 sig_ok = pctx &&
1580 EVP_PKEY_verify_init(pctx.get()) &&
1581 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1582 CBS_len(&signature), digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001583 } else {
1584 sig_ok = ssl_public_key_verify(
1585 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin6dc8bf62017-07-19 16:38:21 -04001586 hs->peer_pubkey.get(), hs->transcript.buffer_data(),
1587 hs->transcript.buffer_len());
Adam Langleyfcf25832014-12-18 17:42:32 -08001588 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001589
David Benjaminbf82aed2016-03-01 22:57:40 -05001590#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1591 sig_ok = 1;
1592 ERR_clear_error();
1593#endif
1594 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001595 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001596 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1597 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001598 }
1599
Steven Valdez2b8415e2016-06-30 13:27:23 -04001600 /* The handshake buffer is no longer necessary, and we may hash the current
1601 * message.*/
David Benjamin6dc8bf62017-07-19 16:38:21 -04001602 hs->transcript.FreeBuffer();
David Benjamin7934f082017-08-01 16:32:25 -04001603 if (!ssl_hash_message(hs, msg)) {
David Benjamind98107b2017-06-15 22:56:37 -04001604 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001605 }
1606
David Benjamin8f94c312017-08-01 17:35:55 -04001607 ssl->method->next_message(ssl);
Adam Langleyd5157222016-12-12 11:37:43 -08001608 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001609}
Adam Langley95c29f32014-06-20 12:00:00 -07001610
Adam Langley95c29f32014-06-20 12:00:00 -07001611/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1612 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001613static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1614 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001615 SSLMessage msg;
1616 int ret = ssl_read_message(ssl, &msg);
David Benjamin09eb6552016-07-08 14:32:11 -07001617 if (ret <= 0) {
1618 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001619 }
Adam Langley95c29f32014-06-20 12:00:00 -07001620
David Benjamin7934f082017-08-01 16:32:25 -04001621 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
1622 !ssl_hash_message(hs, msg)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001623 return -1;
1624 }
1625
David Benjamin7934f082017-08-01 16:32:25 -04001626 CBS next_protocol = msg.body, selected_protocol, padding;
Adam Langleyfcf25832014-12-18 17:42:32 -08001627 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1628 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001629 CBS_len(&next_protocol) != 0) {
1630 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1631 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin8f94c312017-08-01 17:35:55 -04001632 return -1;
David Benjamin639846e2016-09-09 11:41:18 -04001633 }
1634
1635 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001636 &ssl->s3->next_proto_negotiated_len)) {
David Benjamin8f94c312017-08-01 17:35:55 -04001637 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001638 }
Adam Langley95c29f32014-06-20 12:00:00 -07001639
David Benjamin8f94c312017-08-01 17:35:55 -04001640 ssl->method->next_message(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -08001641 return 1;
1642}
Adam Langley95c29f32014-06-20 12:00:00 -07001643
Adam Langley1258b6a2014-06-20 12:00:00 -07001644/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001645static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1646 SSL *const ssl = hs->ssl;
David Benjamin7934f082017-08-01 16:32:25 -04001647 SSLMessage msg;
1648 int ret = ssl_read_message(ssl, &msg);
1649 if (ret <= 0) {
1650 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001651 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001652
David Benjamin7934f082017-08-01 16:32:25 -04001653 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
1654 !tls1_verify_channel_id(hs, msg) ||
1655 !ssl_hash_message(hs, msg)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001656 return -1;
1657 }
David Benjamin8f94c312017-08-01 17:35:55 -04001658 ssl->method->next_message(ssl);
Nick Harper60a85cb2016-09-23 16:25:11 -07001659 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001660}
David Benjamin9f1dc822016-06-07 17:03:46 -04001661
David Benjaminb5f55c32017-06-15 23:15:15 -04001662static int ssl3_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001663 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001664
1665 if (hs->ticket_expected) {
1666 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001667 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001668 if (ssl->session == NULL) {
1669 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001670 ssl_session_rebase_time(ssl, hs->new_session.get());
1671 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001672 } else {
1673 /* We are renewing an existing session. Duplicate the session to adjust
1674 * the timeout. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001675 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001676 if (!session_copy) {
David Benjaminb5f55c32017-06-15 23:15:15 -04001677 return -1;
1678 }
1679
David Benjamin1386aad2017-07-19 23:57:40 -04001680 ssl_session_rebase_time(ssl, session_copy.get());
1681 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001682 }
1683
David Benjamin1386aad2017-07-19 23:57:40 -04001684 ScopedCBB cbb;
1685 CBB body, ticket;
1686 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1687 SSL3_MT_NEW_SESSION_TICKET) ||
1688 !CBB_add_u32(&body, session->timeout) ||
1689 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1690 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1691 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminb5f55c32017-06-15 23:15:15 -04001692 return -1;
1693 }
David Benjamin123db572016-11-03 16:59:25 -04001694 }
1695
David Benjaminb5f55c32017-06-15 23:15:15 -04001696 if (!ssl->method->add_change_cipher_spec(ssl) ||
1697 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
David Benjamine75cc272016-11-03 18:06:27 -04001698 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001699 }
1700
David Benjaminb5f55c32017-06-15 23:15:15 -04001701 return ssl3_send_finished(hs);
David Benjamin9f1dc822016-06-07 17:03:46 -04001702}
David Benjamin86e95b82017-07-18 16:34:25 -04001703
1704} // namespace bssl