blob: 9e15e7e4016d4820d57a7f18e02305a6c42608f1 [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 Benjamin59bae5a2017-02-02 23:51:22 -0500175static int ssl3_process_client_hello(SSL_HANDSHAKE *hs);
176static 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 Benjamin59bae5a2017-02-02 23:51:22 -0500206 ret = ssl->method->ssl_get_message(ssl);
207 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:
214 ret = ssl3_process_client_hello(hs);
215 if (ret <= 0) {
216 goto end;
217 }
218 hs->state = SSL3_ST_SR_CLNT_HELLO_C;
219 break;
220
221 case SSL3_ST_SR_CLNT_HELLO_C:
222 ret = ssl3_select_certificate(hs);
223 if (ret <= 0) {
224 goto end;
225 }
226 if (hs->state != SSL_ST_TLS13) {
227 hs->state = SSL3_ST_SR_CLNT_HELLO_D;
228 }
229 break;
230
231 case SSL3_ST_SR_CLNT_HELLO_D:
232 ret = ssl3_select_parameters(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 if (ret <= 0) {
234 goto end;
235 }
David Benjamindf50eec2016-06-07 16:49:42 -0400236 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500237 hs->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 break;
David Benjamin4b755cb2014-12-12 03:58:07 -0500239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 case SSL3_ST_SW_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900241 ret = ssl3_send_server_hello(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (ret <= 0) {
243 goto end;
244 }
Steven Valdez87eab492016-06-27 16:34:59 -0400245 if (ssl->session != NULL) {
David Benjaminb5f55c32017-06-15 23:15:15 -0400246 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500248 hs->state = SSL3_ST_SW_CERT_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 case SSL3_ST_SW_CERT_A:
David Benjamin0d1730d2017-06-15 23:24:25 -0400253 ret = ssl3_send_server_certificate(hs);
254 if (ret <= 0) {
255 goto end;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100256 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500257 hs->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100258 break;
259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 case SSL3_ST_SW_KEY_EXCH_A:
David Benjamin44148742017-06-17 13:20:59 -0400261 if (hs->server_params_len > 0) {
David Benjaminc3c88822016-11-14 10:32:04 +0900262 ret = ssl3_send_server_key_exchange(hs);
David Benjamin6eb000d2015-02-11 01:17:41 -0500263 if (ret <= 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800264 goto end;
David Benjamin6eb000d2015-02-11 01:17:41 -0500265 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 }
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjamincb0c29f2016-12-12 17:00:50 -0500268 hs->state = SSL3_ST_SW_SRVR_DONE_A;
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_SW_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900272 ret = ssl3_send_server_hello_done(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 if (ret <= 0) {
274 goto end;
275 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500276 hs->next_state = SSL3_ST_SR_CERT_A;
277 hs->state = SSL3_ST_SW_FLUSH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 case SSL3_ST_SR_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900281 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900282 ret = ssl3_get_client_certificate(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (ret <= 0) {
284 goto end;
285 }
286 }
David Benjamin3a1dd462017-07-11 16:13:10 -0400287 hs->state = SSL3_ST_VERIFY_CLIENT_CERT;
288 break;
289
290 case SSL3_ST_VERIFY_CLIENT_CERT:
291 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
292 switch (ssl_verify_peer_cert(hs)) {
293 case ssl_verify_ok:
294 break;
295 case ssl_verify_invalid:
296 ret = -1;
297 goto end;
298 case ssl_verify_retry:
299 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
300 ret = -1;
301 goto end;
302 }
303 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500304 hs->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 case SSL3_ST_SR_KEY_EXCH_A:
308 case SSL3_ST_SR_KEY_EXCH_B:
David Benjaminc3c88822016-11-14 10:32:04 +0900309 ret = ssl3_get_client_key_exchange(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 if (ret <= 0) {
311 goto end;
312 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500313 hs->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 case SSL3_ST_SR_CERT_VRFY_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900317 ret = ssl3_get_cert_verify(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (ret <= 0) {
319 goto end;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
David Benjamincb0c29f2016-12-12 17:00:50 -0500322 hs->state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
David Benjamina41280d2015-11-26 02:16:49 -0500325 case SSL3_ST_SR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400326 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500327 if (ret <= 0) {
328 goto end;
329 }
Adam Langley95c29f32014-06-20 12:00:00 -0700330
David Benjamin67739722016-11-17 17:03:59 +0900331 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 ret = -1;
333 goto end;
334 }
David Benjamina41280d2015-11-26 02:16:49 -0500335
David Benjamincb0c29f2016-12-12 17:00:50 -0500336 hs->state = SSL3_ST_SR_NEXT_PROTO_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_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900340 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900341 ret = ssl3_get_next_proto(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_CHANNEL_ID_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_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400350 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900351 ret = ssl3_get_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400352 if (ret <= 0) {
353 goto end;
354 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500356 hs->state = SSL3_ST_SR_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 break;
Adam Langley1258b6a2014-06-20 12:00:00 -0700358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 case SSL3_ST_SR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900360 ret = ssl3_get_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 if (ret <= 0) {
362 goto end;
363 }
Adam Langley95c29f32014-06-20 12:00:00 -0700364
David Benjamindf50eec2016-06-07 16:49:42 -0400365 ssl->method->received_flight(ssl);
Steven Valdez87eab492016-06-27 16:34:59 -0400366 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500367 hs->state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 } else {
David Benjaminb5f55c32017-06-15 23:15:15 -0400369 hs->state = SSL3_ST_SW_FINISHED_A;
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 }
David Benjamindf50eec2016-06-07 16:49:42 -0400371
Steven Valdez87eab492016-06-27 16:34:59 -0400372 /* If this is a full handshake with ChannelID then record the handshake
David Benjamin45738dd2017-02-09 20:01:26 -0500373 * hashes in |hs->new_session| in case we need them to verify a
Steven Valdez87eab492016-06-27 16:34:59 -0400374 * ChannelID signature on a resumption of this session in the future. */
375 if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) {
Steven Valdez908ac192017-01-12 13:17:07 -0500376 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 if (ret <= 0) {
378 goto end;
379 }
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 case SSL3_ST_SW_FINISHED_A:
David Benjaminb5f55c32017-06-15 23:15:15 -0400384 ret = ssl3_send_server_finished(hs);
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 if (ret <= 0) {
386 goto end;
387 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500388 hs->state = SSL3_ST_SW_FLUSH;
Steven Valdez87eab492016-06-27 16:34:59 -0400389 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500390 hs->next_state = SSL3_ST_SR_CHANGE;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500392 hs->next_state = SSL_ST_OK;
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
David Benjamin9f1dc822016-06-07 17:03:46 -0400396 case SSL3_ST_SW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500397 ret = ssl->method->flush_flight(ssl);
398 if (ret <= 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400399 goto end;
400 }
401
David Benjamincb0c29f2016-12-12 17:00:50 -0500402 hs->state = hs->next_state;
403 if (hs->state != SSL_ST_OK) {
David Benjamin9f1dc822016-06-07 17:03:46 -0400404 ssl->method->expect_flight(ssl);
405 }
406 break;
407
Steven Valdez681eb6a2016-12-19 13:19:29 -0500408 case SSL_ST_TLS13: {
409 int early_return = 0;
410 ret = tls13_handshake(hs, &early_return);
Steven Valdez143e8b32016-07-11 13:19:03 -0400411 if (ret <= 0) {
412 goto end;
413 }
Steven Valdez681eb6a2016-12-19 13:19:29 -0500414
415 if (early_return) {
416 ret = 1;
417 goto end;
418 }
419
David Benjamincb0c29f2016-12-12 17:00:50 -0500420 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400421 break;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500422 }
Steven Valdez143e8b32016-07-11 13:19:03 -0400423
Adam Langleyfcf25832014-12-18 17:42:32 -0800424 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400425 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Steven Valdez87eab492016-06-27 16:34:59 -0400427 /* If we aren't retaining peer certificates then we can discard it
428 * now. */
David Benjamin45738dd2017-02-09 20:01:26 -0500429 if (hs->new_session != NULL &&
David Benjaminbbaf3672016-11-17 10:53:09 +0900430 ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -0500431 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
432 hs->new_session->certs = NULL;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400433 ssl->ctx->x509_method->session_clear(hs->new_session.get());
Steven Valdez87eab492016-06-27 16:34:59 -0400434 }
435
436 SSL_SESSION_free(ssl->s3->established_session);
437 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400438 SSL_SESSION_up_ref(ssl->session);
439 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400440 } else {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400441 ssl->s3->established_session = hs->new_session.release();
Steven Valdez87eab492016-06-27 16:34:59 -0400442 ssl->s3->established_session->not_resumable = 0;
Steven Valdez87eab492016-06-27 16:34:59 -0400443 }
444
David Benjamin78476f62016-11-12 11:20:55 +0900445 ssl->s3->initial_handshake_complete = 1;
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900446 ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
David Benjamin78476f62016-11-12 11:20:55 +0900447
David Benjamin4e9cc712016-06-01 20:16:03 -0400448 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 ret = 1;
450 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700451
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 default:
David Benjamin3570d732015-06-29 00:28:17 -0400453 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 ret = -1;
455 goto end;
456 }
Adam Langley95c29f32014-06-20 12:00:00 -0700457
David Benjamin16b1b1d2017-02-10 23:41:14 -0500458 if (hs->state != state) {
David Benjamin4e9cc712016-06-01 20:16:03 -0400459 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 }
462
Adam Langley95c29f32014-06-20 12:00:00 -0700463end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400464 ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 return ret;
466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
David Benjamin731058e2016-12-03 23:15:13 -0500468int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
469 uint16_t id) {
David Benjamin1deb41b2016-08-09 19:36:38 -0400470 CBS cipher_suites;
471 CBS_init(&cipher_suites, client_hello->cipher_suites,
472 client_hello->cipher_suites_len);
473
474 while (CBS_len(&cipher_suites) > 0) {
475 uint16_t got_id;
476 if (!CBS_get_u16(&cipher_suites, &got_id)) {
477 return 0;
478 }
479
480 if (got_id == id) {
481 return 1;
482 }
483 }
484
485 return 0;
486}
487
David Benjaminf04c2e92016-12-06 13:35:25 -0500488static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
David Benjamin731058e2016-12-03 23:15:13 -0500489 const SSL_CLIENT_HELLO *client_hello) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500490 SSL *const ssl = hs->ssl;
David Benjamin42bfeb32017-02-02 23:28:14 -0500491 assert(!ssl->s3->have_version);
Steven Valdez8f36c512017-06-20 10:55:02 -0400492 CBS supported_versions, versions;
David Benjamin731058e2016-12-03 23:15:13 -0500493 if (ssl_client_hello_get_extension(client_hello, &supported_versions,
494 TLSEXT_TYPE_supported_versions)) {
Steven Valdezfdd10992016-09-15 16:27:05 -0400495 if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
496 CBS_len(&supported_versions) != 0 ||
497 CBS_len(&versions) == 0) {
498 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
499 *out_alert = SSL_AD_DECODE_ERROR;
500 return 0;
501 }
David Benjamin2dc02042016-09-19 19:57:37 -0400502 } else {
Steven Valdez8f36c512017-06-20 10:55:02 -0400503 /* Convert the ClientHello version to an equivalent supported_versions
504 * extension. */
505 static const uint8_t kTLSVersions[] = {
506 0x03, 0x03, /* TLS 1.2 */
507 0x03, 0x02, /* TLS 1.1 */
508 0x03, 0x01, /* TLS 1 */
509 0x03, 0x00, /* SSL 3 */
510 };
511
512 static const uint8_t kDTLSVersions[] = {
513 0xfe, 0xfd, /* DTLS 1.2 */
514 0xfe, 0xff, /* DTLS 1.0 */
515 };
516
517 size_t versions_len = 0;
Steven Valdezfdd10992016-09-15 16:27:05 -0400518 if (SSL_is_dtls(ssl)) {
519 if (client_hello->version <= DTLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400520 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400521 } else if (client_hello->version <= DTLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400522 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400523 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400524 CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
525 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400526 } else {
527 if (client_hello->version >= TLS1_2_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400528 versions_len = 8;
Steven Valdezfdd10992016-09-15 16:27:05 -0400529 } else if (client_hello->version >= TLS1_1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400530 versions_len = 6;
Steven Valdezfdd10992016-09-15 16:27:05 -0400531 } else if (client_hello->version >= TLS1_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400532 versions_len = 4;
Steven Valdezfdd10992016-09-15 16:27:05 -0400533 } else if (client_hello->version >= SSL3_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400534 versions_len = 2;
Steven Valdezfdd10992016-09-15 16:27:05 -0400535 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400536 CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
537 versions_len);
Steven Valdezfdd10992016-09-15 16:27:05 -0400538 }
David Benjamin25fe85b2016-08-09 20:00:32 -0400539 }
540
Steven Valdez8f36c512017-06-20 10:55:02 -0400541 if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
David Benjamin25fe85b2016-08-09 20:00:32 -0400542 return 0;
543 }
544
David Benjamin25fe85b2016-08-09 20:00:32 -0400545 /* At this point, the connection's version is known and |ssl->version| is
546 * fixed. Begin enforcing the record-layer version. */
547 ssl->s3->have_version = 1;
548
Steven Valdez8f36c512017-06-20 10:55:02 -0400549 /* Handle FALLBACK_SCSV. */
550 if (ssl_client_cipher_list_contains_cipher(client_hello,
551 SSL3_CK_FALLBACK_SCSV & 0xffff) &&
552 ssl3_protocol_version(ssl) < hs->max_version) {
553 OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
554 *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
555 return 0;
556 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400557
Steven Valdez8f36c512017-06-20 10:55:02 -0400558 return 1;
David Benjamin25fe85b2016-08-09 20:00:32 -0400559}
560
David Benjaminee910bf2017-07-25 22:36:00 -0400561static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
562 const SSL_CLIENT_HELLO *client_hello) {
David Benjamin2578b292016-12-03 23:19:55 -0500563 CBS cipher_suites;
564 CBS_init(&cipher_suites, client_hello->cipher_suites,
565 client_hello->cipher_suites_len);
566
David Benjaminee910bf2017-07-25 22:36:00 -0400567 UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
568 if (!sk) {
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 while (CBS_len(&cipher_suites) > 0) {
574 uint16_t cipher_suite;
575
576 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
577 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjaminee910bf2017-07-25 22:36:00 -0400578 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500579 }
580
581 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
David Benjaminee910bf2017-07-25 22:36:00 -0400582 if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500583 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjaminee910bf2017-07-25 22:36:00 -0400584 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500585 }
586 }
587
588 return sk;
David Benjamin2578b292016-12-03 23:19:55 -0500589}
590
591/* ssl_get_compatible_server_ciphers determines the key exchange and
592 * authentication cipher suite masks compatible with the server configuration
593 * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
594 * exchange mask and |*out_mask_a| to the authentication mask. */
595static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
596 uint32_t *out_mask_k,
597 uint32_t *out_mask_a) {
598 SSL *const ssl = hs->ssl;
David Benjamin2578b292016-12-03 23:19:55 -0500599 uint32_t mask_k = 0;
600 uint32_t mask_a = 0;
601
Adam Langley3a2b47a2017-01-24 13:59:42 -0800602 if (ssl_has_certificate(ssl)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400603 mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
604 if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
David Benjamin2578b292016-12-03 23:19:55 -0500605 mask_k |= SSL_kRSA;
David Benjamin2578b292016-12-03 23:19:55 -0500606 }
607 }
608
David Benjamin2578b292016-12-03 23:19:55 -0500609 /* Check for a shared group to consider ECDHE ciphers. */
610 uint16_t unused;
611 if (tls1_get_shared_group(hs, &unused)) {
612 mask_k |= SSL_kECDHE;
613 }
614
David Benjamin2578b292016-12-03 23:19:55 -0500615 /* PSK requires a server callback. */
616 if (ssl->psk_server_callback != NULL) {
617 mask_k |= SSL_kPSK;
618 mask_a |= SSL_aPSK;
619 }
620
621 *out_mask_k = mask_k;
622 *out_mask_a = mask_a;
623}
624
625static const SSL_CIPHER *ssl3_choose_cipher(
626 SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
627 const struct ssl_cipher_preference_list_st *server_pref) {
628 SSL *const ssl = hs->ssl;
David Benjaminccbb1652017-03-22 18:24:32 -0400629 STACK_OF(SSL_CIPHER) *prio, *allow;
David Benjamin2578b292016-12-03 23:19:55 -0500630 /* in_group_flags will either be NULL, or will point to an array of bytes
631 * which indicate equal-preference groups in the |prio| stack. See the
632 * comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
633 * struct. */
634 const uint8_t *in_group_flags;
635 /* group_min contains the minimal index so far found in a group, or -1 if no
636 * such value exists yet. */
637 int group_min = -1;
638
David Benjaminee910bf2017-07-25 22:36:00 -0400639 UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
David Benjaminccbb1652017-03-22 18:24:32 -0400640 ssl_parse_client_cipher_list(client_hello);
David Benjaminee910bf2017-07-25 22:36:00 -0400641 if (!client_pref) {
642 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500643 }
644
645 if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
David Benjaminccbb1652017-03-22 18:24:32 -0400646 prio = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500647 in_group_flags = server_pref->in_group_flags;
David Benjaminee910bf2017-07-25 22:36:00 -0400648 allow = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500649 } else {
David Benjaminee910bf2017-07-25 22:36:00 -0400650 prio = client_pref.get();
David Benjamin2578b292016-12-03 23:19:55 -0500651 in_group_flags = NULL;
David Benjaminccbb1652017-03-22 18:24:32 -0400652 allow = server_pref->ciphers;
David Benjamin2578b292016-12-03 23:19:55 -0500653 }
654
David Benjaminccbb1652017-03-22 18:24:32 -0400655 uint32_t mask_k, mask_a;
David Benjamin2578b292016-12-03 23:19:55 -0500656 ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
657
658 for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
David Benjaminccbb1652017-03-22 18:24:32 -0400659 const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
David Benjamin2578b292016-12-03 23:19:55 -0500660
David Benjaminccbb1652017-03-22 18:24:32 -0400661 size_t cipher_index;
662 if (/* Check if the cipher is supported for the current version. */
663 SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
664 ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
665 /* Check the cipher is supported for the server configuration. */
666 (c->algorithm_mkey & mask_k) &&
667 (c->algorithm_auth & mask_a) &&
668 /* Check the cipher is in the |allow| list. */
669 sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
David Benjamin2578b292016-12-03 23:19:55 -0500670 if (in_group_flags != NULL && in_group_flags[i] == 1) {
671 /* This element of |prio| is in a group. Update the minimum index found
672 * so far and continue looking. */
673 if (group_min == -1 || (size_t)group_min > cipher_index) {
674 group_min = cipher_index;
675 }
676 } else {
677 if (group_min != -1 && (size_t)group_min < cipher_index) {
678 cipher_index = group_min;
679 }
David Benjaminee910bf2017-07-25 22:36:00 -0400680 return sk_SSL_CIPHER_value(allow, cipher_index);
David Benjamin2578b292016-12-03 23:19:55 -0500681 }
682 }
683
684 if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
685 /* We are about to leave a group, but we found a match in it, so that's
686 * our answer. */
David Benjaminee910bf2017-07-25 22:36:00 -0400687 return sk_SSL_CIPHER_value(allow, group_min);
David Benjamin2578b292016-12-03 23:19:55 -0500688 }
689 }
690
David Benjaminee910bf2017-07-25 22:36:00 -0400691 return nullptr;
David Benjamin2578b292016-12-03 23:19:55 -0500692}
693
David Benjamin59bae5a2017-02-02 23:51:22 -0500694static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +0900695 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500696 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) {
697 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 }
Adam Langley95c29f32014-06-20 12:00:00 -0700699
David Benjamin731058e2016-12-03 23:15:13 -0500700 SSL_CLIENT_HELLO client_hello;
701 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
702 ssl->init_num)) {
David Benjamin3570d732015-06-29 00:28:17 -0400703 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin59bae5a2017-02-02 23:51:22 -0500704 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
705 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 }
Adam Langley95c29f32014-06-20 12:00:00 -0700707
David Benjamin59bae5a2017-02-02 23:51:22 -0500708 /* Run the early callback. */
709 if (ssl->ctx->select_certificate_cb != NULL) {
710 switch (ssl->ctx->select_certificate_cb(&client_hello)) {
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000711 case ssl_select_cert_retry:
David Benjamin59bae5a2017-02-02 23:51:22 -0500712 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
713 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400714
Alessandro Ghedini57e81e62017-03-14 23:36:00 +0000715 case ssl_select_cert_error:
David Benjamin59bae5a2017-02-02 23:51:22 -0500716 /* Connection rejected. */
717 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
718 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
719 return -1;
David Benjamine14ff062016-08-09 16:21:24 -0400720
David Benjamin59bae5a2017-02-02 23:51:22 -0500721 default:
722 /* fallthrough */;
David Benjamine14ff062016-08-09 16:21:24 -0400723 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500724 }
David Benjamine14ff062016-08-09 16:21:24 -0400725
David Benjamin68161cb2017-06-20 14:49:43 -0400726 /* Freeze the version range after the early callback. */
727 if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
728 return -1;
729 }
730
Adam Langley04540a72017-02-09 10:11:50 -0800731 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin59bae5a2017-02-02 23:51:22 -0500732 if (!negotiate_version(hs, &alert, &client_hello)) {
733 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
734 return -1;
735 }
David Benjamin1deb41b2016-08-09 19:36:38 -0400736
Steven Valdez8f36c512017-06-20 10:55:02 -0400737 hs->client_version = client_hello.version;
David Benjamin59bae5a2017-02-02 23:51:22 -0500738 if (client_hello.random_len != SSL3_RANDOM_SIZE) {
739 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
740 return -1;
741 }
742 OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
743 client_hello.random_len);
744
745 /* Only null compression is supported. TLS 1.3 further requires the peer
746 * advertise no other compression. */
747 if (OPENSSL_memchr(client_hello.compression_methods, 0,
748 client_hello.compression_methods_len) == NULL ||
749 (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
750 client_hello.compression_methods_len != 1)) {
751 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
752 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
753 return -1;
754 }
755
756 /* TLS extensions. */
757 if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
758 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
759 return -1;
760 }
761
762 return 1;
763}
764
765static int ssl3_select_certificate(SSL_HANDSHAKE *hs) {
766 SSL *const ssl = hs->ssl;
767 /* Call |cert_cb| to update server certificates if required. */
768 if (ssl->cert->cert_cb != NULL) {
769 int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
770 if (rv == 0) {
771 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
772 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin25fe85b2016-08-09 20:00:32 -0400773 return -1;
774 }
David Benjamin59bae5a2017-02-02 23:51:22 -0500775 if (rv < 0) {
776 ssl->rwstate = SSL_X509_LOOKUP;
777 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900778 }
David Benjamin4eb95cc2016-11-16 17:08:23 +0900779 }
780
David Benjamina232a712017-03-30 15:51:53 -0500781 if (!ssl_on_certificate_selected(hs)) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500782 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900783 }
784
David Benjamin59bae5a2017-02-02 23:51:22 -0500785 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
786 /* Jump to the TLS 1.3 state machine. */
787 hs->state = SSL_ST_TLS13;
788 hs->do_tls13_handshake = tls13_server_handshake;
789 return 1;
790 }
791
792 SSL_CLIENT_HELLO client_hello;
793 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
794 ssl->init_num)) {
795 return -1;
796 }
797
798 /* Negotiate the cipher suite. This must be done after |cert_cb| so the
799 * certificate is finalized. */
David Benjamin45738dd2017-02-09 20:01:26 -0500800 hs->new_cipher =
David Benjamin59bae5a2017-02-02 23:51:22 -0500801 ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
David Benjamin45738dd2017-02-09 20:01:26 -0500802 if (hs->new_cipher == NULL) {
David Benjamin59bae5a2017-02-02 23:51:22 -0500803 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
804 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
805 return -1;
806 }
807
808 return 1;
809}
810
811static int ssl3_select_parameters(SSL_HANDSHAKE *hs) {
812 SSL *const ssl = hs->ssl;
David Benjamin59bae5a2017-02-02 23:51:22 -0500813 SSL_CLIENT_HELLO client_hello;
814 if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg,
815 ssl->init_num)) {
816 return -1;
817 }
David Benjamin34202b92016-11-16 19:07:53 +0900818
819 /* Determine whether we are doing session resumption. */
820 int tickets_supported = 0, renew_ticket = 0;
David Benjamin86e95b82017-07-18 16:34:25 -0400821 /* TODO(davidben): Switch |ssl_get_prev_session| to take a |UniquePtr|
David Benjamind781fc42017-07-12 16:25:57 -0400822 * output and simplify this. */
823 SSL_SESSION *session_raw = nullptr;
824 auto session_ret = ssl_get_prev_session(ssl, &session_raw, &tickets_supported,
825 &renew_ticket, &client_hello);
David Benjamin86e95b82017-07-18 16:34:25 -0400826 UniquePtr<SSL_SESSION> session(session_raw);
David Benjamind781fc42017-07-12 16:25:57 -0400827 switch (session_ret) {
David Benjamin34202b92016-11-16 19:07:53 +0900828 case ssl_session_success:
829 break;
830 case ssl_session_error:
David Benjamind781fc42017-07-12 16:25:57 -0400831 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900832 case ssl_session_retry:
833 ssl->rwstate = SSL_PENDING_SESSION;
David Benjamind781fc42017-07-12 16:25:57 -0400834 return -1;
Adam Langley4c341d02017-03-08 19:33:21 -0800835 case ssl_session_ticket_retry:
836 ssl->rwstate = SSL_PENDING_TICKET;
David Benjamind781fc42017-07-12 16:25:57 -0400837 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900838 }
839
David Benjamind781fc42017-07-12 16:25:57 -0400840 if (session) {
David Benjaminfc02b592017-02-17 16:26:01 -0500841 if (session->extended_master_secret && !hs->extended_master_secret) {
David Benjamin34202b92016-11-16 19:07:53 +0900842 /* A ClientHello without EMS that attempts to resume a session with EMS
843 * is fatal to the connection. */
David Benjamin34202b92016-11-16 19:07:53 +0900844 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
David Benjamind98107b2017-06-15 22:56:37 -0400845 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamind781fc42017-07-12 16:25:57 -0400846 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900847 }
848
David Benjamind781fc42017-07-12 16:25:57 -0400849 if (!ssl_session_is_resumable(hs, session.get()) ||
David Benjamin34202b92016-11-16 19:07:53 +0900850 /* If the client offers the EMS extension, but the previous session
851 * didn't use it, then negotiate a new session. */
David Benjaminfc02b592017-02-17 16:26:01 -0500852 hs->extended_master_secret != session->extended_master_secret) {
David Benjamind781fc42017-07-12 16:25:57 -0400853 session.reset();
David Benjamin34202b92016-11-16 19:07:53 +0900854 }
855 }
856
David Benjamind781fc42017-07-12 16:25:57 -0400857 if (session) {
David Benjamin34202b92016-11-16 19:07:53 +0900858 /* Use the old session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900859 hs->ticket_expected = renew_ticket;
David Benjamind781fc42017-07-12 16:25:57 -0400860 ssl->session = session.release();
David Benjamin34202b92016-11-16 19:07:53 +0900861 ssl->s3->session_reused = 1;
862 } else {
David Benjaminc3c88822016-11-14 10:32:04 +0900863 hs->ticket_expected = tickets_supported;
David Benjamin34202b92016-11-16 19:07:53 +0900864 ssl_set_session(ssl, NULL);
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900865 if (!ssl_get_new_session(hs, 1 /* server */)) {
David Benjamind781fc42017-07-12 16:25:57 -0400866 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900867 }
868
869 /* Clear the session ID if we want the session to be single-use. */
870 if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
David Benjamin45738dd2017-02-09 20:01:26 -0500871 hs->new_session->session_id_length = 0;
David Benjamin34202b92016-11-16 19:07:53 +0900872 }
873 }
874
875 if (ssl->ctx->dos_protection_cb != NULL &&
876 ssl->ctx->dos_protection_cb(&client_hello) == 0) {
877 /* Connection rejected for DOS reasons. */
David Benjamin34202b92016-11-16 19:07:53 +0900878 OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
David Benjamind98107b2017-06-15 22:56:37 -0400879 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400880 return -1;
David Benjamin34202b92016-11-16 19:07:53 +0900881 }
882
David Benjaminf01f42a2016-11-16 19:05:33 +0900883 if (ssl->session == NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500884 hs->new_session->cipher = hs->new_cipher;
David Benjamin5c1ce292015-05-26 16:59:43 -0400885
David Benjamin4eb95cc2016-11-16 17:08:23 +0900886 /* On new sessions, stash the SNI value in the session. */
David Benjaminc3c88822016-11-14 10:32:04 +0900887 if (hs->hostname != NULL) {
David Benjamin45738dd2017-02-09 20:01:26 -0500888 OPENSSL_free(hs->new_session->tlsext_hostname);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400889 hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
David Benjamin45738dd2017-02-09 20:01:26 -0500890 if (hs->new_session->tlsext_hostname == NULL) {
David Benjamind98107b2017-06-15 22:56:37 -0400891 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400892 return -1;
David Benjamin4eb95cc2016-11-16 17:08:23 +0900893 }
894 }
895
David Benjamin5c1ce292015-05-26 16:59:43 -0400896 /* Determine whether to request a client certificate. */
David Benjaminc3c88822016-11-14 10:32:04 +0900897 hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
David Benjamin5c1ce292015-05-26 16:59:43 -0400898 /* Only request a certificate if Channel ID isn't negotiated. */
David Benjamin0d56f882015-12-19 17:05:56 -0500899 if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
900 ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900901 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400902 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400903 /* CertificateRequest may only be sent in certificate-based ciphers. */
David Benjamin45738dd2017-02-09 20:01:26 -0500904 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900905 hs->cert_request = 0;
David Benjamin5c1ce292015-05-26 16:59:43 -0400906 }
Adam Langley37646832016-08-01 16:16:46 -0700907
David Benjaminc3c88822016-11-14 10:32:04 +0900908 if (!hs->cert_request) {
Adam Langley37646832016-08-01 16:16:46 -0700909 /* OpenSSL returns X509_V_OK when no certificates are requested. This is
David Benjamindd634eb2016-08-18 16:40:28 -0400910 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -0500911 hs->new_session->verify_result = X509_V_OK;
Adam Langley37646832016-08-01 16:16:46 -0700912 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 }
914
David Benjaminf01f42a2016-11-16 19:05:33 +0900915 /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
916 * deferred. Complete it now. */
David Benjamind98107b2017-06-15 22:56:37 -0400917 uint8_t alert = SSL_AD_DECODE_ERROR;
918 if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
919 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
David Benjamind781fc42017-07-12 16:25:57 -0400920 return -1;
David Benjamin9ef31f02016-10-31 18:01:13 -0400921 }
922
David Benjaminf71036e2017-01-21 14:49:39 -0500923 /* Now that all parameters are known, initialize the handshake hash and hash
924 * the ClientHello. */
David Benjamin6dc8bf62017-07-19 16:38:21 -0400925 if (!hs->transcript.InitHash(ssl3_protocol_version(ssl),
926 hs->new_cipher->algorithm_prf) ||
Steven Valdez908ac192017-01-12 13:17:07 -0500927 !ssl_hash_current_message(hs)) {
David Benjamind98107b2017-06-15 22:56:37 -0400928 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -0400929 return -1;
David Benjamin9550c3a2015-08-05 08:50:34 -0400930 }
931
Steven Valdez2b8415e2016-06-30 13:27:23 -0400932 /* Release the handshake buffer if client authentication isn't required. */
David Benjaminc3c88822016-11-14 10:32:04 +0900933 if (!hs->cert_request) {
David Benjamin6dc8bf62017-07-19 16:38:21 -0400934 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -0800935 }
936
David Benjamind781fc42017-07-12 16:25:57 -0400937 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800938}
Adam Langley95c29f32014-06-20 12:00:00 -0700939
David Benjaminc3c88822016-11-14 10:32:04 +0900940static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) {
941 SSL *const ssl = hs->ssl;
David Benjamin56380462015-10-10 14:59:09 -0400942
943 /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
944 * known attack while we fix ChannelID itself. */
945 if (ssl->s3->tlsext_channel_id_valid &&
David Benjamin45738dd2017-02-09 20:01:26 -0500946 (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400947 ssl->s3->tlsext_channel_id_valid = 0;
948 }
949
950 /* If this is a resumption and the original handshake didn't support
951 * ChannelID then we didn't record the original handshake hashes in the
952 * session and so cannot resume with ChannelIDs. */
Steven Valdez87eab492016-06-27 16:34:59 -0400953 if (ssl->session != NULL &&
954 ssl->session->original_handshake_hash_len == 0) {
David Benjamin56380462015-10-10 14:59:09 -0400955 ssl->s3->tlsext_channel_id_valid = 0;
956 }
957
David Benjaminad8f5e12017-02-20 17:00:20 -0500958 struct OPENSSL_timeval now;
David Benjamin721e8b72016-08-03 13:13:17 -0400959 ssl_get_current_time(ssl, &now);
960 ssl->s3->server_random[0] = now.tv_sec >> 24;
961 ssl->s3->server_random[1] = now.tv_sec >> 16;
962 ssl->s3->server_random[2] = now.tv_sec >> 8;
963 ssl->s3->server_random[3] = now.tv_sec;
David Benjamin1f61f0d2016-07-10 12:20:35 -0400964 if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
David Benjamin56380462015-10-10 14:59:09 -0400965 return -1;
966 }
967
David Benjamin55108632016-08-11 22:01:18 -0400968 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
969 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400970
David Benjamin31b0c9b2017-07-20 14:49:15 -0400971 const SSL_SESSION *session = hs->new_session.get();
Steven Valdez87eab492016-06-27 16:34:59 -0400972 if (ssl->session != NULL) {
973 session = ssl->session;
974 }
975
David Benjamin1386aad2017-07-19 23:57:40 -0400976 ScopedCBB cbb;
977 CBB body, session_id;
978 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
David Benjamin75836432016-06-17 18:48:29 -0400979 !CBB_add_u16(&body, ssl->version) ||
980 !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
981 !CBB_add_u8_length_prefixed(&body, &session_id) ||
Steven Valdez87eab492016-06-27 16:34:59 -0400982 !CBB_add_bytes(&session_id, session->session_id,
983 session->session_id_length) ||
David Benjamin45738dd2017-02-09 20:01:26 -0500984 !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
David Benjamin75836432016-06-17 18:48:29 -0400985 !CBB_add_u8(&body, 0 /* no compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500986 !ssl_add_serverhello_tlsext(hs, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -0400987 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin56380462015-10-10 14:59:09 -0400988 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin56380462015-10-10 14:59:09 -0400989 return -1;
990 }
991
David Benjamin16315f72017-01-12 20:02:05 -0500992 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800993}
Adam Langley95c29f32014-06-20 12:00:00 -0700994
David Benjaminc3c88822016-11-14 10:32:04 +0900995static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) {
996 SSL *const ssl = hs->ssl;
David Benjamin86e95b82017-07-18 16:34:25 -0400997 ScopedCBB cbb;
David Benjamin0d1730d2017-06-15 23:24:25 -0400998
David Benjamin44148742017-06-17 13:20:59 -0400999 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1000 if (!ssl_has_certificate(ssl)) {
1001 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
David Benjamind781fc42017-07-12 16:25:57 -04001002 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001003 }
David Benjamin32a66d52016-07-13 22:03:11 -04001004
David Benjamin44148742017-06-17 13:20:59 -04001005 if (!ssl3_output_cert_chain(ssl)) {
David Benjamind781fc42017-07-12 16:25:57 -04001006 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001007 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001008
David Benjamin44148742017-06-17 13:20:59 -04001009 if (hs->certificate_status_expected) {
1010 CBB body, ocsp_response;
David Benjamind781fc42017-07-12 16:25:57 -04001011 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin44148742017-06-17 13:20:59 -04001012 SSL3_MT_CERTIFICATE_STATUS) ||
1013 !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
1014 !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
1015 !CBB_add_bytes(&ocsp_response,
1016 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
1017 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
David Benjamind781fc42017-07-12 16:25:57 -04001018 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjamin44148742017-06-17 13:20:59 -04001019 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind781fc42017-07-12 16:25:57 -04001020 return -1;
David Benjamin44148742017-06-17 13:20:59 -04001021 }
David Benjamin0d1730d2017-06-15 23:24:25 -04001022 }
David Benjamin75836432016-06-17 18:48:29 -04001023 }
1024
David Benjamin44148742017-06-17 13:20:59 -04001025 /* Assemble ServerKeyExchange parameters if needed. */
1026 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1027 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1028 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
1029 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +01001030
David Benjamin44148742017-06-17 13:20:59 -04001031 /* Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
1032 * the client and server randoms for the signing transcript. */
1033 CBB child;
David Benjamind781fc42017-07-12 16:25:57 -04001034 if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
1035 !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1036 !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
1037 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001038 }
1039
David Benjamin2a0b3912015-12-18 01:01:21 -05001040 /* PSK ciphers begin with an identity hint. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 if (alg_a & SSL_aPSK) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001042 size_t len =
1043 (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
David Benjamind781fc42017-07-12 16:25:57 -04001044 if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
David Benjamin2a0b3912015-12-18 01:01:21 -05001045 !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
1046 len)) {
David Benjamind781fc42017-07-12 16:25:57 -04001047 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001049 }
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001051 if (alg_k & SSL_kECDHE) {
Steven Valdezce902a92016-05-17 11:47:53 -04001052 /* Determine the group to use. */
1053 uint16_t group_id;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001054 if (!tls1_get_shared_group(hs, &group_id)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001055 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin2a0b3912015-12-18 01:01:21 -05001056 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
David Benjamind781fc42017-07-12 16:25:57 -04001057 return -1;
1058 }
David Benjamin45738dd2017-02-09 20:01:26 -05001059 hs->new_session->group_id = group_id;
Adam Langley95c29f32014-06-20 12:00:00 -07001060
David Benjamin4298d772015-12-19 00:18:25 -05001061 /* Set up ECDH, generate a key, and emit the public half. */
David Benjaminc642aca2017-07-19 23:28:43 -04001062 hs->key_share = SSLKeyShare::Create(group_id);
1063 if (!hs->key_share ||
David Benjamind781fc42017-07-12 16:25:57 -04001064 !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
1065 !CBB_add_u16(cbb.get(), group_id) ||
1066 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc642aca2017-07-19 23:28:43 -04001067 !hs->key_share->Offer(&child)) {
David Benjamind781fc42017-07-12 16:25:57 -04001068 return -1;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001069 }
David Benjamin2a0b3912015-12-18 01:01:21 -05001070 } else {
1071 assert(alg_k & SSL_kPSK);
Adam Langleyfcf25832014-12-18 17:42:32 -08001072 }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
David Benjamind781fc42017-07-12 16:25:57 -04001074 if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
1075 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001076 }
David Benjaminc42acee2016-06-17 17:47:58 -04001077 }
1078
David Benjamind781fc42017-07-12 16:25:57 -04001079 return 1;
David Benjamin44148742017-06-17 13:20:59 -04001080}
1081
1082static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) {
1083 SSL *const ssl = hs->ssl;
David Benjamin1386aad2017-07-19 23:57:40 -04001084 ScopedCBB cbb;
1085 CBB body, child;
1086 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin75836432016-06-17 18:48:29 -04001087 SSL3_MT_SERVER_KEY_EXCHANGE) ||
David Benjamin44148742017-06-17 13:20:59 -04001088 /* |hs->server_params| contains a prefix for signing. */
1089 hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
1090 !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
1091 hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001092 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
David Benjamin2a0b3912015-12-18 01:01:21 -05001095 /* Add a signature. */
David Benjamin45738dd2017-02-09 20:01:26 -05001096 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001097 if (!ssl_has_private_key(ssl)) {
1098 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001099 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 }
Adam Langley95c29f32014-06-20 12:00:00 -07001101
David Benjaminc42acee2016-06-17 17:47:58 -04001102 /* Determine the signature algorithm. */
David Benjaminea9a0d52016-07-08 15:52:59 -07001103 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001104 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001105 return -1;
David Benjaminea9a0d52016-07-08 15:52:59 -07001106 }
David Benjaminc42acee2016-06-17 17:47:58 -04001107 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001108 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjaminc42acee2016-06-17 17:47:58 -04001109 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1110 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin1386aad2017-07-19 23:57:40 -04001111 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001112 }
David Benjaminc42acee2016-06-17 17:47:58 -04001113 }
1114
1115 /* Add space for the signature. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001116 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc42acee2016-06-17 17:47:58 -04001117 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001118 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminc42acee2016-06-17 17:47:58 -04001119 !CBB_reserve(&child, &ptr, max_sig_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001120 return -1;
David Benjaminc42acee2016-06-17 17:47:58 -04001121 }
1122
David Benjamin2a0b3912015-12-18 01:01:21 -05001123 size_t sig_len;
David Benjamin44148742017-06-17 13:20:59 -04001124 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1125 signature_algorithm, hs->server_params,
1126 hs->server_params_len)) {
David Benjamin2a0b3912015-12-18 01:01:21 -05001127 case ssl_private_key_success:
David Benjamin2a0b3912015-12-18 01:01:21 -05001128 if (!CBB_did_write(&child, sig_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -04001129 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001130 }
1131 break;
1132 case ssl_private_key_failure:
David Benjamin1386aad2017-07-19 23:57:40 -04001133 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001134 case ssl_private_key_retry:
David Benjamin2a0b3912015-12-18 01:01:21 -05001135 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamin1386aad2017-07-19 23:57:40 -04001136 return -1;
David Benjamin2a0b3912015-12-18 01:01:21 -05001137 }
David Benjamin1f9f9c42015-08-28 16:17:59 -04001138 }
1139
David Benjamin1386aad2017-07-19 23:57:40 -04001140 if (!ssl_add_message_cbb(ssl, cbb.get())) {
1141 return -1;
David Benjamin1f9f9c42015-08-28 16:17:59 -04001142 }
David Benjaminc42acee2016-06-17 17:47:58 -04001143
David Benjaminc3c88822016-11-14 10:32:04 +09001144 OPENSSL_free(hs->server_params);
1145 hs->server_params = NULL;
1146 hs->server_params_len = 0;
David Benjaminc42acee2016-06-17 17:47:58 -04001147
David Benjamin16315f72017-01-12 20:02:05 -05001148 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001149}
Adam Langley95c29f32014-06-20 12:00:00 -07001150
David Benjamin0d1730d2017-06-15 23:24:25 -04001151static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001152 SSL *const ssl = hs->ssl;
David Benjamin1386aad2017-07-19 23:57:40 -04001153 ScopedCBB cbb;
1154 CBB body;
David Benjamin75836432016-06-17 18:48:29 -04001155
David Benjamin0d1730d2017-06-15 23:24:25 -04001156 if (hs->cert_request) {
1157 CBB cert_types, sigalgs_cbb;
David Benjamin1386aad2017-07-19 23:57:40 -04001158 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjamin0d1730d2017-06-15 23:24:25 -04001159 SSL3_MT_CERTIFICATE_REQUEST) ||
1160 !CBB_add_u8_length_prefixed(&body, &cert_types) ||
1161 !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
1162 (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
1163 !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
1164 (ssl3_protocol_version(ssl) >= TLS1_2_VERSION &&
1165 (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
1166 !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
1167 !ssl_add_client_CA_list(ssl, &body) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001168 !ssl_add_message_cbb(ssl, cbb.get())) {
1169 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1170 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001171 }
1172 }
1173
David Benjamin1386aad2017-07-19 23:57:40 -04001174 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1175 SSL3_MT_SERVER_HELLO_DONE) ||
1176 !ssl_add_message_cbb(ssl, cbb.get())) {
1177 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1178 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001179 }
1180
David Benjamin16315f72017-01-12 20:02:05 -05001181 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001182}
Adam Langley95c29f32014-06-20 12:00:00 -07001183
David Benjaminc3c88822016-11-14 10:32:04 +09001184static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) {
1185 SSL *const ssl = hs->ssl;
1186 assert(hs->cert_request);
David Benjamin9f1dc822016-06-07 17:03:46 -04001187
David Benjaminf71036e2017-01-21 14:49:39 -05001188 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001189 if (msg_ret <= 0) {
1190 return msg_ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001191 }
1192
1193 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1194 if (ssl->version == SSL3_VERSION &&
1195 ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
Adam Langley37646832016-08-01 16:16:46 -07001196 /* In SSL 3.0, the Certificate message is omitted to signal no
1197 * certificate. */
David Benjaminda2630c2016-08-01 19:57:43 -04001198 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001199 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001200 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1201 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001202 }
1203
Adam Langley37646832016-08-01 16:16:46 -07001204 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001205 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001206 hs->new_session->verify_result = X509_V_OK;
David Benjamin9f1dc822016-06-07 17:03:46 -04001207 ssl->s3->tmp.reuse_message = 1;
1208 return 1;
1209 }
1210
David Benjamin9f1dc822016-06-07 17:03:46 -04001211 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin5c900c82016-07-13 23:03:26 -04001212 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1213 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001214 }
1215
Steven Valdez908ac192017-01-12 13:17:07 -05001216 if (!ssl_hash_current_message(hs)) {
David Benjaminf71036e2017-01-21 14:49:39 -05001217 return -1;
1218 }
1219
David Benjamin5c900c82016-07-13 23:03:26 -04001220 CBS certificate_msg;
David Benjamin09eb6552016-07-08 14:32:11 -07001221 CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001222
Adam Langleyc68e5b92017-02-08 13:33:15 -08001223 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -04001224 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
1225 if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
1226 ssl->retain_only_sha256_of_client_certs
1227 ? hs->new_session->peer_sha256
1228 : NULL,
1229 &certificate_msg, ssl->ctx->pool)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001230 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001231 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001232 }
David Benjamine664a532017-07-20 20:19:36 -04001233 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
1234 hs->new_session->certs = chain.release();
David Benjamin9f1dc822016-06-07 17:03:46 -04001235
Adam Langley68e71242016-12-12 11:06:16 -08001236 if (CBS_len(&certificate_msg) != 0 ||
David Benjamin31b0c9b2017-07-20 14:49:15 -04001237 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001238 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001239 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001240 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001241 }
1242
David Benjamin45738dd2017-02-09 20:01:26 -05001243 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001244 /* No client certificate so the handshake buffer may be discarded. */
David Benjamin6dc8bf62017-07-19 16:38:21 -04001245 hs->transcript.FreeBuffer();
David Benjamin9f1dc822016-06-07 17:03:46 -04001246
David Benjamine455e512016-08-01 20:11:13 -04001247 /* In SSL 3.0, sending no certificate is signaled by omitting the
1248 * Certificate message. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001249 if (ssl->version == SSL3_VERSION) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001250 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
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 Benjamine455e512016-08-01 20:11:13 -04001253 }
1254
1255 if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001256 /* Fail for TLS only if we required a certificate */
1257 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
David Benjamin5c900c82016-07-13 23:03:26 -04001258 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
Adam Langley68e71242016-12-12 11:06:16 -08001259 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001260 }
Adam Langley37646832016-08-01 16:16:46 -07001261
1262 /* OpenSSL returns X509_V_OK when no certificates are received. This is
David Benjamindd634eb2016-08-18 16:40:28 -04001263 * classed by them as a bug, but it's assumed by at least NGINX. */
David Benjamin45738dd2017-02-09 20:01:26 -05001264 hs->new_session->verify_result = X509_V_OK;
Adam Langley68e71242016-12-12 11:06:16 -08001265 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001266 }
1267
Adam Langley68e71242016-12-12 11:06:16 -08001268 /* The hash will have been filled in. */
1269 if (ssl->retain_only_sha256_of_client_certs) {
David Benjamin45738dd2017-02-09 20:01:26 -05001270 hs->new_session->peer_sha256_valid = 1;
Adam Langley364f7a62016-12-12 10:51:00 -08001271 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001272
David Benjamin5c900c82016-07-13 23:03:26 -04001273 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001274}
1275
David Benjaminc3c88822016-11-14 10:32:04 +09001276static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) {
1277 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001278 CBS client_key_exchange;
Adam Langleyfcf25832014-12-18 17:42:32 -08001279 uint8_t *premaster_secret = NULL;
1280 size_t premaster_secret_len = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -08001281 uint8_t *decrypt_buf = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001282
David Benjamincb0c29f2016-12-12 17:00:50 -05001283 if (hs->state == SSL3_ST_SR_KEY_EXCH_A) {
David Benjaminf71036e2017-01-21 14:49:39 -05001284 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001285 if (ret <= 0) {
1286 return ret;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001287 }
David Benjamin44148742017-06-17 13:20:59 -04001288 }
David Benjamin276b7e82017-01-21 14:13:39 -05001289
David Benjamin44148742017-06-17 13:20:59 -04001290 if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
1291 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001292 }
Adam Langley95c29f32014-06-20 12:00:00 -07001293
David Benjamin0d56f882015-12-19 17:05:56 -05001294 CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin44148742017-06-17 13:20:59 -04001295 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1296 uint32_t alg_a = hs->new_cipher->algorithm_auth;
Adam Langleyc26c8022014-06-20 12:00:00 -07001297
David Benjamin44148742017-06-17 13:20:59 -04001298 /* If using a PSK key exchange, parse the PSK identity. */
Adam Langleyfcf25832014-12-18 17:42:32 -08001299 if (alg_a & SSL_aPSK) {
1300 CBS psk_identity;
David Benjamin35c02302014-07-13 04:14:59 -04001301
Adam Langleyfcf25832014-12-18 17:42:32 -08001302 /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
1303 * then this is the only field in the message. */
1304 if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
1305 ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
David Benjamin3570d732015-06-29 00:28:17 -04001306 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001307 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1308 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001309 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001310
Adam Langleyfcf25832014-12-18 17:42:32 -08001311 if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
1312 CBS_contains_zero_byte(&psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001313 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
David Benjamind98107b2017-06-15 22:56:37 -04001314 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1315 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001316 }
David Benjamin35c02302014-07-13 04:14:59 -04001317
David Benjamin45738dd2017-02-09 20:01:26 -05001318 if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
David Benjamin3570d732015-06-29 00:28:17 -04001319 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind98107b2017-06-15 22:56:37 -04001320 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1321 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001322 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001323 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001324
Adam Langleyfcf25832014-12-18 17:42:32 -08001325 /* Depending on the key exchange method, compute |premaster_secret| and
1326 * |premaster_secret_len|. */
1327 if (alg_k & SSL_kRSA) {
David Benjamin44148742017-06-17 13:20:59 -04001328 CBS encrypted_premaster_secret;
1329 if (ssl->version > SSL3_VERSION) {
1330 if (!CBS_get_u16_length_prefixed(&client_key_exchange,
1331 &encrypted_premaster_secret) ||
1332 CBS_len(&client_key_exchange) != 0) {
1333 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1334 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1335 goto err;
1336 }
1337 } else {
1338 encrypted_premaster_secret = client_key_exchange;
1339 }
1340
Adam Langleyfcf25832014-12-18 17:42:32 -08001341 /* Allocate a buffer large enough for an RSA decryption. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001342 const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
David Benjamind781fc42017-07-12 16:25:57 -04001343 decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
Adam Langleyfcf25832014-12-18 17:42:32 -08001344 if (decrypt_buf == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001345 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001346 goto err;
1347 }
David Benjamin35c02302014-07-13 04:14:59 -04001348
David Benjamin44148742017-06-17 13:20:59 -04001349 /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
1350 * timing-sensitive code below. */
David Benjamin3f5b43d2015-12-01 19:31:24 -05001351 size_t decrypt_len;
David Benjamin44148742017-06-17 13:20:59 -04001352 switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
1353 CBS_data(&encrypted_premaster_secret),
1354 CBS_len(&encrypted_premaster_secret))) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001355 case ssl_private_key_success:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001356 break;
1357 case ssl_private_key_failure:
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001358 goto err;
1359 case ssl_private_key_retry:
David Benjamin0d56f882015-12-19 17:05:56 -05001360 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001361 hs->state = SSL3_ST_SR_KEY_EXCH_B;
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001362 goto err;
1363 }
1364
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001365 if (decrypt_len != rsa_size) {
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001366 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001367 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1368 goto err;
Daniel Bathgate4365c3f2016-04-14 17:18:02 -04001369 }
Adam Langleyacff73f2014-06-20 12:00:00 -07001370
David Benjamin3f5b43d2015-12-01 19:31:24 -05001371 /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
1372 * section 7.4.7.1. */
1373 premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
David Benjamind781fc42017-07-12 16:25:57 -04001374 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001375 if (premaster_secret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001376 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001377 goto err;
1378 }
David Benjamin3f5b43d2015-12-01 19:31:24 -05001379 if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
nagendra modadugu3398dbf2015-08-07 14:07:52 -07001380 goto err;
1381 }
1382
David Benjamin3f5b43d2015-12-01 19:31:24 -05001383 /* The smallest padded premaster is 11 bytes of overhead. Small keys are
1384 * publicly invalid. */
1385 if (decrypt_len < 11 + premaster_secret_len) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001386 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
David Benjamind98107b2017-06-15 22:56:37 -04001387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1388 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001389 }
David Benjamin35c02302014-07-13 04:14:59 -04001390
David Benjamin3f5b43d2015-12-01 19:31:24 -05001391 /* Check the padding. See RFC 3447, section 7.2.2. */
1392 size_t padding_len = decrypt_len - premaster_secret_len;
1393 uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
1394 constant_time_eq_int_8(decrypt_buf[1], 2);
David Benjamin54091232016-09-05 12:47:25 -04001395 for (size_t i = 2; i < padding_len - 1; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001396 good &= ~constant_time_is_zero_8(decrypt_buf[i]);
1397 }
1398 good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
1399
1400 /* The premaster secret must begin with |client_version|. This too must be
1401 * checked in constant time (http://eprint.iacr.org/2003/052/). */
1402 good &= constant_time_eq_8(decrypt_buf[padding_len],
David Benjaminf04c2e92016-12-06 13:35:25 -05001403 (unsigned)(hs->client_version >> 8));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001404 good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
David Benjaminf04c2e92016-12-06 13:35:25 -05001405 (unsigned)(hs->client_version & 0xff));
David Benjamin3f5b43d2015-12-01 19:31:24 -05001406
1407 /* Select, in constant time, either the decrypted premaster or the random
1408 * premaster based on |good|. */
David Benjamin54091232016-09-05 12:47:25 -04001409 for (size_t i = 0; i < premaster_secret_len; i++) {
David Benjamin3f5b43d2015-12-01 19:31:24 -05001410 premaster_secret[i] = constant_time_select_8(
1411 good, decrypt_buf[padding_len + i], premaster_secret[i]);
1412 }
1413
1414 OPENSSL_free(decrypt_buf);
1415 decrypt_buf = NULL;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001416 } else if (alg_k & SSL_kECDHE) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001417 /* Parse the ClientKeyExchange. */
David Benjamin974c7ba2015-12-19 16:58:04 -05001418 CBS peer_key;
Matthew Braithwaite7e06de52017-04-10 15:52:14 -07001419 if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001420 CBS_len(&client_key_exchange) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001421 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001422 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1423 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001424 }
David Benjaminbd30f8e2014-08-19 16:02:38 -04001425
David Benjamin4298d772015-12-19 00:18:25 -05001426 /* Compute the premaster. */
Adam Langleyc68e5b92017-02-08 13:33:15 -08001427 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc642aca2017-07-19 23:28:43 -04001428 if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
1429 CBS_data(&peer_key), CBS_len(&peer_key))) {
David Benjamind98107b2017-06-15 22:56:37 -04001430 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1431 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001432 }
Adam Langley95c29f32014-06-20 12:00:00 -07001433
David Benjamin4298d772015-12-19 00:18:25 -05001434 /* The key exchange state may now be discarded. */
David Benjaminc642aca2017-07-19 23:28:43 -04001435 hs->key_share.reset();
David Benjamin44148742017-06-17 13:20:59 -04001436 } else if (!(alg_k & SSL_kPSK)) {
David Benjamin99a93d42017-07-01 11:02:20 -04001437 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001438 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1439 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -08001440 }
Adam Langley95c29f32014-06-20 12:00:00 -07001441
Adam Langleyfcf25832014-12-18 17:42:32 -08001442 /* For a PSK cipher suite, the actual pre-master secret is combined with the
1443 * pre-shared key. */
1444 if (alg_a & SSL_aPSK) {
David Benjamin44148742017-06-17 13:20:59 -04001445 if (ssl->psk_server_callback == NULL) {
David Benjamin99a93d42017-07-01 11:02:20 -04001446 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin44148742017-06-17 13:20:59 -04001447 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1448 goto err;
1449 }
1450
1451 /* Look up the key for the identity. */
1452 uint8_t psk[PSK_MAX_PSK_LEN];
1453 unsigned psk_len = ssl->psk_server_callback(
1454 ssl, hs->new_session->psk_identity, psk, sizeof(psk));
1455 if (psk_len > PSK_MAX_PSK_LEN) {
1456 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1457 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1458 goto err;
1459 } else if (psk_len == 0) {
1460 /* PSK related to the given identity not found */
1461 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1462 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
1463 goto err;
1464 }
1465
1466 if (alg_k & SSL_kPSK) {
1467 /* In plain PSK, other_secret is a block of 0s with the same length as the
1468 * pre-shared key. */
1469 premaster_secret_len = psk_len;
David Benjamind781fc42017-07-12 16:25:57 -04001470 premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
David Benjamin44148742017-06-17 13:20:59 -04001471 if (premaster_secret == NULL) {
1472 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1473 goto err;
1474 }
1475 OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
1476 }
1477
David Benjamin1386aad2017-07-19 23:57:40 -04001478 ScopedCBB new_premaster;
1479 CBB child;
Adam Langleyfcf25832014-12-18 17:42:32 -08001480 uint8_t *new_data;
1481 size_t new_len;
David Benjamin1386aad2017-07-19 23:57:40 -04001482 if (!CBB_init(new_premaster.get(),
1483 2 + psk_len + 2 + premaster_secret_len) ||
1484 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001485 !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001486 !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
Adam Langleyfcf25832014-12-18 17:42:32 -08001487 !CBB_add_bytes(&child, psk, psk_len) ||
David Benjamin1386aad2017-07-19 23:57:40 -04001488 !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
David Benjamin3570d732015-06-29 00:28:17 -04001489 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -08001490 goto err;
1491 }
David Benjamin14c83e72014-07-13 04:54:57 -04001492
Adam Langleyfcf25832014-12-18 17:42:32 -08001493 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1494 OPENSSL_free(premaster_secret);
1495 premaster_secret = new_data;
1496 premaster_secret_len = new_len;
1497 }
David Benjamin14c83e72014-07-13 04:54:57 -04001498
David Benjamin44148742017-06-17 13:20:59 -04001499 if (!ssl_hash_current_message(hs)) {
1500 goto err;
1501 }
1502
Adam Langleyfcf25832014-12-18 17:42:32 -08001503 /* Compute the master secret */
David Benjamin45738dd2017-02-09 20:01:26 -05001504 hs->new_session->master_key_length = tls1_generate_master_secret(
1505 hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
1506 if (hs->new_session->master_key_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001507 goto err;
1508 }
David Benjaminfc02b592017-02-17 16:26:01 -05001509 hs->new_session->extended_master_secret = hs->extended_master_secret;
David Benjamin14c83e72014-07-13 04:54:57 -04001510
Adam Langleyfcf25832014-12-18 17:42:32 -08001511 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
1512 OPENSSL_free(premaster_secret);
1513 return 1;
1514
Adam Langley95c29f32014-06-20 12:00:00 -07001515err:
David Benjamin4298d772015-12-19 00:18:25 -05001516 if (premaster_secret != NULL) {
1517 OPENSSL_cleanse(premaster_secret, premaster_secret_len);
Adam Langleyfcf25832014-12-18 17:42:32 -08001518 OPENSSL_free(premaster_secret);
1519 }
David Benjamin2755a3e2015-04-22 16:17:58 -04001520 OPENSSL_free(decrypt_buf);
Adam Langley95c29f32014-06-20 12:00:00 -07001521
Adam Langleyfcf25832014-12-18 17:42:32 -08001522 return -1;
1523}
Adam Langley95c29f32014-06-20 12:00:00 -07001524
David Benjaminc3c88822016-11-14 10:32:04 +09001525static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) {
1526 SSL *const ssl = hs->ssl;
Adam Langleyfcf25832014-12-18 17:42:32 -08001527 CBS certificate_verify, signature;
David Benjamin6553b372014-07-22 14:11:30 -04001528
Adam Langleyfcf25832014-12-18 17:42:32 -08001529 /* Only RSA and ECDSA client certificates are supported, so a
1530 * CertificateVerify is required if and only if there's a client certificate.
1531 * */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001532 if (!hs->peer_pubkey) {
David Benjamin6dc8bf62017-07-19 16:38:21 -04001533 hs->transcript.FreeBuffer();
Adam Langleyfcf25832014-12-18 17:42:32 -08001534 return 1;
1535 }
Adam Langley95c29f32014-06-20 12:00:00 -07001536
David Benjaminf71036e2017-01-21 14:49:39 -05001537 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001538 if (msg_ret <= 0) {
1539 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001540 }
David Benjaminef865502014-08-24 02:48:34 -04001541
David Benjamin276b7e82017-01-21 14:13:39 -05001542 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) {
1543 return -1;
1544 }
1545
David Benjamin09eb6552016-07-08 14:32:11 -07001546 CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num);
David Benjamin6897dbe2014-07-12 20:18:28 -04001547
Adam Langleyfcf25832014-12-18 17:42:32 -08001548 /* Determine the digest type if needbe. */
Steven Valdezf0451ca2016-06-29 13:16:27 -04001549 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001550 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001551 if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001552 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001553 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1554 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001555 }
David Benjamin8d606e32017-06-15 22:43:04 -04001556 uint8_t alert = SSL_AD_DECODE_ERROR;
1557 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001558 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1559 return -1;
David Benjamin6e807652015-11-02 12:02:20 -05001560 }
David Benjamin45738dd2017-02-09 20:01:26 -05001561 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjamina3651382017-04-20 17:49:36 -04001562 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
David Benjamin31b0c9b2017-07-20 14:49:15 -04001563 hs->peer_pubkey.get())) {
David Benjamin49ec9bb2016-07-14 00:11:26 -04001564 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
David Benjamind98107b2017-06-15 22:56:37 -04001565 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1566 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001567 }
David Benjamin854dd652014-08-26 00:32:30 -04001568
Adam Langleyfcf25832014-12-18 17:42:32 -08001569 /* Parse and verify the signature. */
1570 if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
1571 CBS_len(&certificate_verify) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001572 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamind98107b2017-06-15 22:56:37 -04001573 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1574 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001575 }
Adam Langley95c29f32014-06-20 12:00:00 -07001576
Steven Valdez2b8415e2016-06-30 13:27:23 -04001577 int sig_ok;
1578 /* The SSL3 construction for CertificateVerify does not decompose into a
1579 * single final digest and signature, and must be special-cased. */
1580 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001581 uint8_t digest[EVP_MAX_MD_SIZE];
1582 size_t digest_len;
David Benjamin6dc8bf62017-07-19 16:38:21 -04001583 if (!hs->transcript.GetSSL3CertVerifyHash(
1584 digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
David Benjamind98107b2017-06-15 22:56:37 -04001585 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001586 }
1587
David Benjamin31b0c9b2017-07-20 14:49:15 -04001588 UniquePtr<EVP_PKEY_CTX> pctx(
1589 EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
David Benjamin1386aad2017-07-19 23:57:40 -04001590 sig_ok = pctx &&
1591 EVP_PKEY_verify_init(pctx.get()) &&
1592 EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
1593 CBS_len(&signature), digest, digest_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001594 } else {
1595 sig_ok = ssl_public_key_verify(
1596 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
David Benjamin6dc8bf62017-07-19 16:38:21 -04001597 hs->peer_pubkey.get(), hs->transcript.buffer_data(),
1598 hs->transcript.buffer_len());
Adam Langleyfcf25832014-12-18 17:42:32 -08001599 }
Steven Valdezf0451ca2016-06-29 13:16:27 -04001600
David Benjaminbf82aed2016-03-01 22:57:40 -05001601#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1602 sig_ok = 1;
1603 ERR_clear_error();
1604#endif
1605 if (!sig_ok) {
David Benjamin3570d732015-06-29 00:28:17 -04001606 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
David Benjamind98107b2017-06-15 22:56:37 -04001607 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1608 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001609 }
1610
Steven Valdez2b8415e2016-06-30 13:27:23 -04001611 /* The handshake buffer is no longer necessary, and we may hash the current
1612 * message.*/
David Benjamin6dc8bf62017-07-19 16:38:21 -04001613 hs->transcript.FreeBuffer();
Steven Valdez908ac192017-01-12 13:17:07 -05001614 if (!ssl_hash_current_message(hs)) {
David Benjamind98107b2017-06-15 22:56:37 -04001615 return -1;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001616 }
1617
Adam Langleyd5157222016-12-12 11:37:43 -08001618 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001619}
Adam Langley95c29f32014-06-20 12:00:00 -07001620
Adam Langley95c29f32014-06-20 12:00:00 -07001621/* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
1622 * sets the next_proto member in s if found */
David Benjaminc3c88822016-11-14 10:32:04 +09001623static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) {
1624 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001625 int ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001626 if (ret <= 0) {
1627 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001628 }
Adam Langley95c29f32014-06-20 12:00:00 -07001629
David Benjaminf71036e2017-01-21 14:49:39 -05001630 if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001631 !ssl_hash_current_message(hs)) {
David Benjamin276b7e82017-01-21 14:13:39 -05001632 return -1;
1633 }
1634
David Benjamin09eb6552016-07-08 14:32:11 -07001635 CBS next_protocol, selected_protocol, padding;
1636 CBS_init(&next_protocol, ssl->init_msg, ssl->init_num);
Adam Langleyfcf25832014-12-18 17:42:32 -08001637 if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
1638 !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
David Benjamin639846e2016-09-09 11:41:18 -04001639 CBS_len(&next_protocol) != 0) {
1640 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1641 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1642 return 0;
1643 }
1644
1645 if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
David Benjamin79978df2015-12-25 15:56:49 -05001646 &ssl->s3->next_proto_negotiated_len)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001647 return 0;
1648 }
Adam Langley95c29f32014-06-20 12:00:00 -07001649
Adam Langleyfcf25832014-12-18 17:42:32 -08001650 return 1;
1651}
Adam Langley95c29f32014-06-20 12:00:00 -07001652
Adam Langley1258b6a2014-06-20 12:00:00 -07001653/* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
David Benjaminc3c88822016-11-14 10:32:04 +09001654static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) {
1655 SSL *const ssl = hs->ssl;
David Benjaminf71036e2017-01-21 14:49:39 -05001656 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjamin09eb6552016-07-08 14:32:11 -07001657 if (msg_ret <= 0) {
1658 return msg_ret;
Adam Langleyfcf25832014-12-18 17:42:32 -08001659 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001660
David Benjamin276b7e82017-01-21 14:13:39 -05001661 if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) ||
Steven Valdez908ac192017-01-12 13:17:07 -05001662 !tls1_verify_channel_id(hs) ||
1663 !ssl_hash_current_message(hs)) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001664 return -1;
1665 }
Nick Harper60a85cb2016-09-23 16:25:11 -07001666 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -08001667}
David Benjamin9f1dc822016-06-07 17:03:46 -04001668
David Benjaminb5f55c32017-06-15 23:15:15 -04001669static int ssl3_send_server_finished(SSL_HANDSHAKE *hs) {
David Benjaminc3c88822016-11-14 10:32:04 +09001670 SSL *const ssl = hs->ssl;
David Benjaminb5f55c32017-06-15 23:15:15 -04001671
1672 if (hs->ticket_expected) {
1673 const SSL_SESSION *session;
David Benjamin1386aad2017-07-19 23:57:40 -04001674 UniquePtr<SSL_SESSION> session_copy;
David Benjaminb5f55c32017-06-15 23:15:15 -04001675 if (ssl->session == NULL) {
1676 /* Fix the timeout to measure from the ticket issuance time. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001677 ssl_session_rebase_time(ssl, hs->new_session.get());
1678 session = hs->new_session.get();
David Benjaminb5f55c32017-06-15 23:15:15 -04001679 } else {
1680 /* We are renewing an existing session. Duplicate the session to adjust
1681 * the timeout. */
David Benjamin31b0c9b2017-07-20 14:49:15 -04001682 session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjamin1386aad2017-07-19 23:57:40 -04001683 if (!session_copy) {
David Benjaminb5f55c32017-06-15 23:15:15 -04001684 return -1;
1685 }
1686
David Benjamin1386aad2017-07-19 23:57:40 -04001687 ssl_session_rebase_time(ssl, session_copy.get());
1688 session = session_copy.get();
David Benjamin123db572016-11-03 16:59:25 -04001689 }
1690
David Benjamin1386aad2017-07-19 23:57:40 -04001691 ScopedCBB cbb;
1692 CBB body, ticket;
1693 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1694 SSL3_MT_NEW_SESSION_TICKET) ||
1695 !CBB_add_u32(&body, session->timeout) ||
1696 !CBB_add_u16_length_prefixed(&body, &ticket) ||
1697 !ssl_encrypt_ticket(ssl, &ticket, session) ||
1698 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminb5f55c32017-06-15 23:15:15 -04001699 return -1;
1700 }
David Benjamin123db572016-11-03 16:59:25 -04001701 }
1702
David Benjaminb5f55c32017-06-15 23:15:15 -04001703 if (!ssl->method->add_change_cipher_spec(ssl) ||
1704 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
David Benjamine75cc272016-11-03 18:06:27 -04001705 return -1;
David Benjamin75836432016-06-17 18:48:29 -04001706 }
1707
David Benjaminb5f55c32017-06-15 23:15:15 -04001708 return ssl3_send_finished(hs);
David Benjamin9f1dc822016-06-07 17:03:46 -04001709}
David Benjamin86e95b82017-07-18 16:34:25 -04001710
1711} // namespace bssl