blob: b8153f5aa4b417041a223d9ef588053909d42383 [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 */
149
David Benjamin9e4e01e2015-09-15 01:48:04 -0400150#include <openssl/ssl.h>
151
David Benjamin81ea0bf2014-11-23 04:20:17 -0500152#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400153#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin961ad6a2015-06-12 01:40:23 -0400155#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700156#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400157#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400158#include <openssl/dh.h>
159#include <openssl/ec_key.h>
160#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400161#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700162#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400164#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400165#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700166#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400167#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin2ee94aa2015-04-07 22:38:30 -0400169#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjaminaa7734b2016-06-07 16:40:46 -0400172static int ssl3_send_client_hello(SSL *ssl);
173static int dtls1_get_hello_verify(SSL *ssl);
174static int ssl3_get_server_hello(SSL *ssl);
David Benjaminaa7734b2016-06-07 16:40:46 -0400175static int ssl3_get_server_certificate(SSL *ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400176static int ssl3_get_cert_status(SSL *ssl);
177static int ssl3_verify_server_cert(SSL *ssl);
178static int ssl3_get_server_key_exchange(SSL *ssl);
179static int ssl3_get_certificate_request(SSL *ssl);
David Benjamin3dcec452016-06-07 17:13:28 -0400180static int ssl3_get_server_hello_done(SSL *ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400181static int ssl3_send_client_certificate(SSL *ssl);
182static int ssl3_send_client_key_exchange(SSL *ssl);
183static int ssl3_send_cert_verify(SSL *ssl);
David Benjaminaa7734b2016-06-07 16:40:46 -0400184static int ssl3_send_next_proto(SSL *ssl);
185static int ssl3_send_channel_id(SSL *ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -0400186static int ssl3_get_new_session_ticket(SSL *ssl);
David Benjaminaa7734b2016-06-07 16:40:46 -0400187
David Benjamin0d56f882015-12-19 17:05:56 -0500188int ssl3_connect(SSL *ssl) {
Adam Langley1bea1732014-12-17 19:06:57 -0800189 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400190 int state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500191
David Benjamin0d56f882015-12-19 17:05:56 -0500192 assert(ssl->handshake_func == ssl3_connect);
193 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley1bea1732014-12-17 19:06:57 -0800195 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500196 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjamin0d56f882015-12-19 17:05:56 -0500198 switch (ssl->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400199 case SSL_ST_INIT:
200 ssl->state = SSL_ST_CONNECT;
201 skip = 1;
202 break;
203
Adam Langley1bea1732014-12-17 19:06:57 -0800204 case SSL_ST_CONNECT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400205 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700206
Steven Valdez143e8b32016-07-11 13:19:03 -0400207 ssl->s3->hs = ssl_handshake_new(tls13_client_handshake);
David Benjamin481b9d22016-07-27 14:01:59 -0400208 if (ssl->s3->hs == NULL) {
David Benjamin97718f12016-07-08 09:16:50 -0700209 ret = -1;
210 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjaminb095f0f2016-05-05 21:50:24 -0400213 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800214 ret = -1;
215 goto end;
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
David Benjamin0d56f882015-12-19 17:05:56 -0500218 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800219 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley1bea1732014-12-17 19:06:57 -0800221 case SSL3_ST_CW_CLNT_HELLO_A:
222 case SSL3_ST_CW_CLNT_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500223 ret = ssl3_send_client_hello(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800224 if (ret <= 0) {
225 goto end;
226 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400227
David Benjamince079fd2016-08-02 16:22:34 -0400228 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400229 ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
230 } else {
231 ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
232 }
David Benjaminb095f0f2016-05-05 21:50:24 -0400233 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800234 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjaminaa7734b2016-06-07 16:40:46 -0400236 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400237 assert(SSL_is_dtls(ssl));
David Benjaminaa7734b2016-06-07 16:40:46 -0400238 ret = dtls1_get_hello_verify(ssl);
239 if (ret <= 0) {
240 goto end;
241 }
242 if (ssl->d1->send_cookie) {
243 ssl->method->received_flight(ssl);
244 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
245 } else {
246 ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
247 }
248 break;
249
Adam Langley1bea1732014-12-17 19:06:57 -0800250 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500251 ret = ssl3_get_server_hello(ssl);
Steven Valdez143e8b32016-07-11 13:19:03 -0400252 if (ssl->state == SSL_ST_TLS13) {
253 break;
254 }
Adam Langley1bea1732014-12-17 19:06:57 -0800255 if (ret <= 0) {
256 goto end;
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Steven Valdez87eab492016-06-27 16:34:59 -0400259 if (ssl->session != NULL) {
David Benjamin47749a62016-06-13 13:56:33 -0400260 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800261 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500262 ssl->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800263 }
Adam Langley1bea1732014-12-17 19:06:57 -0800264 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400265
Adam Langley1bea1732014-12-17 19:06:57 -0800266 case SSL3_ST_CR_CERT_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000267 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500268 ret = ssl3_get_server_certificate(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800269 if (ret <= 0) {
270 goto end;
271 }
Adam Langley1bea1732014-12-17 19:06:57 -0800272 } else {
273 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800274 }
David Benjamin47749a62016-06-13 13:56:33 -0400275 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800276 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamin9f1dc822016-06-07 17:03:46 -0400278 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamin47749a62016-06-13 13:56:33 -0400279 if (ssl->s3->tmp.certificate_status_expected) {
280 ret = ssl3_get_cert_status(ssl);
281 if (ret <= 0) {
282 goto end;
283 }
284 } else {
285 skip = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400286 }
287 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
288 break;
289
Paul Lietar8f1c2682015-08-18 12:21:54 +0100290 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin3d458dc2016-09-12 22:40:27 +0000291 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin47749a62016-06-13 13:56:33 -0400292 ret = ssl3_verify_server_cert(ssl);
293 if (ret <= 0) {
294 goto end;
295 }
296 } else {
297 skip = 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100298 }
David Benjamin0d56f882015-12-19 17:05:56 -0500299 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100300 break;
301
Adam Langley1bea1732014-12-17 19:06:57 -0800302 case SSL3_ST_CR_KEY_EXCH_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500303 ret = ssl3_get_server_key_exchange(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800304 if (ret <= 0) {
305 goto end;
306 }
David Benjamin47749a62016-06-13 13:56:33 -0400307 ssl->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800308 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langley1bea1732014-12-17 19:06:57 -0800310 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000311 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin47749a62016-06-13 13:56:33 -0400312 ret = ssl3_get_certificate_request(ssl);
313 if (ret <= 0) {
314 goto end;
315 }
316 } else {
317 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800318 }
David Benjamin0d56f882015-12-19 17:05:56 -0500319 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800320 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langley1bea1732014-12-17 19:06:57 -0800322 case SSL3_ST_CR_SRVR_DONE_A:
David Benjamin3dcec452016-06-07 17:13:28 -0400323 ret = ssl3_get_server_hello_done(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800324 if (ret <= 0) {
325 goto end;
326 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400327 ssl->method->received_flight(ssl);
David Benjamin47749a62016-06-13 13:56:33 -0400328 ssl->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800329 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley1bea1732014-12-17 19:06:57 -0800331 case SSL3_ST_CW_CERT_A:
332 case SSL3_ST_CW_CERT_B:
333 case SSL3_ST_CW_CERT_C:
David Benjamin5744ca62016-06-13 14:05:19 -0400334 if (ssl->s3->tmp.cert_request) {
David Benjamin47749a62016-06-13 13:56:33 -0400335 ret = ssl3_send_client_certificate(ssl);
336 if (ret <= 0) {
337 goto end;
338 }
339 } else {
340 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800341 }
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800343 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langley1bea1732014-12-17 19:06:57 -0800345 case SSL3_ST_CW_KEY_EXCH_A:
346 case SSL3_ST_CW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500347 ret = ssl3_send_client_key_exchange(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800348 if (ret <= 0) {
349 goto end;
350 }
David Benjamin47749a62016-06-13 13:56:33 -0400351 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800352 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langley1bea1732014-12-17 19:06:57 -0800354 case SSL3_ST_CW_CERT_VRFY_A:
355 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminb4d65fd2015-05-29 17:11:21 -0400356 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamin5744ca62016-06-13 14:05:19 -0400357 if (ssl->s3->tmp.cert_request) {
David Benjamin47749a62016-06-13 13:56:33 -0400358 ret = ssl3_send_cert_verify(ssl);
359 if (ret <= 0) {
360 goto end;
361 }
362 } else {
363 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800364 }
David Benjamin352d0a92016-06-28 11:22:02 -0400365 ssl->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800366 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
David Benjamin352d0a92016-06-28 11:22:02 -0400368 case SSL3_ST_CW_CHANGE:
369 ret = ssl->method->send_change_cipher_spec(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800370 if (ret <= 0) {
371 goto end;
372 }
Adam Langley95c29f32014-06-20 12:00:00 -0700373
David Benjamin47749a62016-06-13 13:56:33 -0400374 ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
David Benjamin57997da2015-12-25 15:48:39 -0500376 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800377 ret = -1;
378 goto end;
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langley1bea1732014-12-17 19:06:57 -0800381 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langley1bea1732014-12-17 19:06:57 -0800383 case SSL3_ST_CW_NEXT_PROTO_A:
384 case SSL3_ST_CW_NEXT_PROTO_B:
David Benjamin47749a62016-06-13 13:56:33 -0400385 if (ssl->s3->next_proto_neg_seen) {
386 ret = ssl3_send_next_proto(ssl);
387 if (ret <= 0) {
388 goto end;
389 }
Adam Langley1bea1732014-12-17 19:06:57 -0800390 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400391 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800392 }
David Benjamin47749a62016-06-13 13:56:33 -0400393 ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800394 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langley1bea1732014-12-17 19:06:57 -0800396 case SSL3_ST_CW_CHANNEL_ID_A:
397 case SSL3_ST_CW_CHANNEL_ID_B:
David Benjamin47749a62016-06-13 13:56:33 -0400398 if (ssl->s3->tlsext_channel_id_valid) {
399 ret = ssl3_send_channel_id(ssl);
400 if (ret <= 0) {
401 goto end;
402 }
403 } else {
404 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800405 }
David Benjamin0d56f882015-12-19 17:05:56 -0500406 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800407 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langley1bea1732014-12-17 19:06:57 -0800409 case SSL3_ST_CW_FINISHED_A:
410 case SSL3_ST_CW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500411 ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500412 SSL3_ST_CW_FINISHED_B);
Adam Langley1bea1732014-12-17 19:06:57 -0800413 if (ret <= 0) {
414 goto end;
415 }
David Benjamin0d56f882015-12-19 17:05:56 -0500416 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Steven Valdez87eab492016-06-27 16:34:59 -0400418 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500419 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800420 } else {
421 /* This is a non-resumption handshake. If it involves ChannelID, then
422 * record the handshake hashes at this point in the session so that
423 * any resumption of this session with ChannelID can sign those
424 * hashes. */
David Benjamin0d56f882015-12-19 17:05:56 -0500425 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langley49c7af12015-07-10 14:33:46 -0700426 if (ret <= 0) {
427 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800428 }
David Benjamin0d56f882015-12-19 17:05:56 -0500429 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
430 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500431 /* No False Start on renegotiation (would complicate the state
432 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500433 !ssl->s3->initial_handshake_complete) {
434 ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800435 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400436 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800437 }
438 }
Adam Langley1bea1732014-12-17 19:06:57 -0800439 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700440
David Benjamin9f1dc822016-06-07 17:03:46 -0400441 case SSL3_ST_FALSE_START:
David Benjamin47749a62016-06-13 13:56:33 -0400442 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamin9f1dc822016-06-07 17:03:46 -0400443 ssl->s3->tmp.in_false_start = 1;
444
445 ssl_free_wbio_buffer(ssl);
446 ret = 1;
447 goto end;
448
Adam Langley1bea1732014-12-17 19:06:57 -0800449 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamin47749a62016-06-13 13:56:33 -0400450 if (ssl->tlsext_ticket_expected) {
451 ret = ssl3_get_new_session_ticket(ssl);
452 if (ret <= 0) {
453 goto end;
454 }
455 } else {
456 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800457 }
David Benjamin0d56f882015-12-19 17:05:56 -0500458 ssl->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800459 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Adam Langley1bea1732014-12-17 19:06:57 -0800461 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400462 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500463 if (ret <= 0) {
464 goto end;
465 }
466
David Benjamin57997da2015-12-25 15:48:39 -0500467 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800468 ret = -1;
469 goto end;
470 }
David Benjamin0d56f882015-12-19 17:05:56 -0500471 ssl->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800472 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Adam Langley1bea1732014-12-17 19:06:57 -0800474 case SSL3_ST_CR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400475 ret = ssl3_get_finished(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800476 if (ret <= 0) {
477 goto end;
478 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400479 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700480
Steven Valdez87eab492016-06-27 16:34:59 -0400481 if (ssl->session != NULL) {
David Benjamin352d0a92016-06-28 11:22:02 -0400482 ssl->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800483 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500484 ssl->state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800485 }
Adam Langley1bea1732014-12-17 19:06:57 -0800486 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langley1bea1732014-12-17 19:06:57 -0800488 case SSL3_ST_CW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500489 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500490 ssl->rwstate = SSL_WRITING;
Adam Langley1bea1732014-12-17 19:06:57 -0800491 ret = -1;
492 goto end;
493 }
David Benjamin0d56f882015-12-19 17:05:56 -0500494 ssl->state = ssl->s3->tmp.next_state;
David Benjaminaa7734b2016-06-07 16:40:46 -0400495 if (ssl->state != SSL_ST_OK) {
496 ssl->method->expect_flight(ssl);
497 }
Adam Langley1bea1732014-12-17 19:06:57 -0800498 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700499
Steven Valdez143e8b32016-07-11 13:19:03 -0400500 case SSL_ST_TLS13:
501 ret = tls13_handshake(ssl);
502 if (ret <= 0) {
503 goto end;
504 }
505 ssl->state = SSL_ST_OK;
506 break;
507
Adam Langley1bea1732014-12-17 19:06:57 -0800508 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400509 /* Clean a few things up. */
David Benjamin0d56f882015-12-19 17:05:56 -0500510 ssl3_cleanup_key_block(ssl);
David Benjamin4497e582016-07-27 17:51:49 -0400511 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Steven Valdez87eab492016-06-27 16:34:59 -0400513 SSL_SESSION_free(ssl->s3->established_session);
514 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400515 SSL_SESSION_up_ref(ssl->session);
516 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400517 } else {
518 /* We make a copy of the session in order to maintain the immutability
519 * of the new established_session due to False Start. The caller may
520 * have taken a reference to the temporary session. */
521 ssl->s3->established_session =
Steven Valdez4aa154e2016-07-29 14:32:55 -0400522 SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400523 if (ssl->s3->established_session == NULL) {
524 /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
525 * callers. */
526 ssl->state = SSL_ST_ERROR;
527 skip = 1;
528 ret = -1;
529 goto end;
530 }
531 ssl->s3->established_session->not_resumable = 0;
532
533 SSL_SESSION_free(ssl->s3->new_session);
534 ssl->s3->new_session = NULL;
535 }
536
Adam Langley1bea1732014-12-17 19:06:57 -0800537 /* Remove write buffering now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500538 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Steven Valdez143e8b32016-07-11 13:19:03 -0400540 ssl_handshake_free(ssl->s3->hs);
541 ssl->s3->hs = NULL;
542
David Benjamin0d56f882015-12-19 17:05:56 -0500543 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjaminba4594a2015-06-18 18:36:15 -0400544
David Benjamin0d56f882015-12-19 17:05:56 -0500545 ssl->s3->tmp.in_false_start = 0;
546 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjaminba4594a2015-06-18 18:36:15 -0400548 if (is_initial_handshake) {
549 /* Renegotiations do not participate in session resumption. */
David Benjamin0d56f882015-12-19 17:05:56 -0500550 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley1bea1732014-12-17 19:06:57 -0800553 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400554 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800555 goto end;
556
Steven Valdez87eab492016-06-27 16:34:59 -0400557 case SSL_ST_ERROR:
558 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
559 ret = -1;
560 goto end;
561
Adam Langley1bea1732014-12-17 19:06:57 -0800562 default:
David Benjamin3570d732015-06-29 00:28:17 -0400563 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800564 ret = -1;
565 goto end;
566 }
567
David Benjamin4e9cc712016-06-01 20:16:03 -0400568 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
569 int new_state = ssl->state;
570 ssl->state = state;
571 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
572 ssl->state = new_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800573 }
574 skip = 0;
575 }
576
Adam Langley95c29f32014-06-20 12:00:00 -0700577end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400578 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800579 return ret;
580}
Adam Langley95c29f32014-06-20 12:00:00 -0700581
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200582static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
583 uint16_t min_version,
584 uint16_t max_version,
585 uint16_t real_max_version) {
David Benjamine8d53502015-10-10 14:13:23 -0400586 /* Prepare disabled cipher masks. */
587 ssl_set_client_disabled(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamine8d53502015-10-10 14:13:23 -0400589 CBB child;
590 if (!CBB_add_u16_length_prefixed(out, &child)) {
591 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800592 }
593
David Benjamine8d53502015-10-10 14:13:23 -0400594 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
595
596 int any_enabled = 0;
David Benjamin54091232016-09-05 12:47:25 -0400597 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
David Benjamine8d53502015-10-10 14:13:23 -0400598 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
599 /* Skip disabled ciphers */
David Benjaminc7817d82015-11-05 18:28:33 -0500600 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
601 (cipher->algorithm_auth & ssl->cert->mask_a)) {
602 continue;
603 }
David Benjamin929d4ee2016-06-24 23:55:58 -0400604 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
605 SSL_CIPHER_get_max_version(cipher) < min_version) {
David Benjamine8d53502015-10-10 14:13:23 -0400606 continue;
607 }
608 any_enabled = 1;
609 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
610 return 0;
611 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400612 /* Add PSK ciphers for TLS 1.3 resumption. */
613 if (ssl->session != NULL &&
614 ssl->method->version_from_wire(ssl->session->ssl_version) >=
615 TLS1_3_VERSION) {
616 uint16_t resumption_cipher;
617 if (ssl_cipher_get_ecdhe_psk_cipher(cipher, &resumption_cipher) &&
618 !CBB_add_u16(&child, resumption_cipher)) {
619 return 0;
620 }
621 }
David Benjamine8d53502015-10-10 14:13:23 -0400622 }
623
624 /* If all ciphers were disabled, return the error to the caller. */
625 if (!any_enabled) {
626 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
627 return 0;
628 }
629
630 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
631 * added. */
632 if (ssl->client_version == SSL3_VERSION &&
633 !ssl->s3->initial_handshake_complete) {
634 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
635 return 0;
636 }
David Benjamine8d53502015-10-10 14:13:23 -0400637 }
638
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200639 if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) ||
640 real_max_version > max_version) {
641 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
642 return 0;
643 }
David Benjamine8d53502015-10-10 14:13:23 -0400644 }
645
646 return CBB_flush(out);
647}
648
Steven Valdez5440fe02016-07-18 12:40:30 -0400649int ssl_add_client_hello_body(SSL *ssl, CBB *body) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200650 uint16_t min_version, max_version, real_max_version;
651 if (!ssl_get_full_version_range(ssl, &min_version, &max_version,
652 &real_max_version)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400653 return 0;
654 }
655
656 /* Renegotiations do not participate in session resumption. */
657 int has_session = ssl->session != NULL &&
658 !ssl->s3->initial_handshake_complete;
659
660 CBB child;
661 if (!CBB_add_u16(body, ssl->client_version) ||
662 !CBB_add_bytes(body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
663 !CBB_add_u8_length_prefixed(body, &child) ||
664 (has_session &&
665 !CBB_add_bytes(&child, ssl->session->session_id,
666 ssl->session->session_id_length))) {
667 return 0;
668 }
669
David Benjamince079fd2016-08-02 16:22:34 -0400670 if (SSL_is_dtls(ssl)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400671 if (!CBB_add_u8_length_prefixed(body, &child) ||
672 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
673 return 0;
674 }
675 }
676
677 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400678 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200679 if (!ssl_write_client_cipher_list(ssl, body, min_version, max_version,
680 real_max_version) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400681 !CBB_add_u8(body, 1 /* one compression method */) ||
682 !CBB_add_u8(body, 0 /* null compression */) ||
683 !ssl_add_clienthello_tlsext(ssl, body, header_len + CBB_len(body))) {
684 return 0;
685 }
686
687 return 1;
688}
689
David Benjaminaa7734b2016-06-07 16:40:46 -0400690static int ssl3_send_client_hello(SSL *ssl) {
David Benjamine8d53502015-10-10 14:13:23 -0400691 if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
David Benjamin75836432016-06-17 18:48:29 -0400692 return ssl->method->write_message(ssl);
David Benjamine8d53502015-10-10 14:13:23 -0400693 }
694
David Benjaminaa7734b2016-06-07 16:40:46 -0400695 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
696 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
697 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500698 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
699 return -1;
700 }
701
David Benjamine8d53502015-10-10 14:13:23 -0400702 CBB cbb;
703 CBB_zero(&cbb);
704
David Benjaminb6a0a512016-06-21 10:33:21 -0400705 uint16_t min_version, max_version;
706 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
707 goto err;
708 }
709
David Benjamine8d53502015-10-10 14:13:23 -0400710 assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
711 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400712 ssl->version = ssl->method->version_to_wire(max_version);
David Benjamine8d53502015-10-10 14:13:23 -0400713 /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
714 * although locked to a version, reuse the value. When using the plain RSA
715 * key exchange, the ClientHello version is checked in the premaster secret.
716 * Some servers fail when this value changes. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400717 ssl->client_version = ssl->version;
David Benjamine8d53502015-10-10 14:13:23 -0400718 }
719
David Benjamine29ea162016-03-23 16:10:44 -0400720 /* If the configured session has expired or was created at a disabled
721 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400722 if (ssl->session != NULL) {
723 uint16_t session_version =
724 ssl->method->version_from_wire(ssl->session->ssl_version);
Steven Valdez4aa154e2016-07-29 14:32:55 -0400725 if ((session_version < TLS1_3_VERSION &&
726 ssl->session->session_id_length == 0) ||
727 ssl->session->not_resumable ||
728 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminb6a0a512016-06-21 10:33:21 -0400729 session_version < min_version || session_version > max_version) {
David Benjamin4d0be242016-09-01 01:10:07 -0400730 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400731 }
David Benjamine8d53502015-10-10 14:13:23 -0400732 }
733
734 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
735 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400736 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400737 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
David Benjamine8d53502015-10-10 14:13:23 -0400738 goto err;
739 }
740
Steven Valdez5440fe02016-07-18 12:40:30 -0400741 CBB body;
David Benjamin75836432016-06-17 18:48:29 -0400742 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400743 !ssl_add_client_hello_body(ssl, &body) ||
David Benjamin75836432016-06-17 18:48:29 -0400744 !ssl->method->finish_message(ssl, &cbb)) {
David Benjamine8d53502015-10-10 14:13:23 -0400745 goto err;
746 }
747
748 ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
David Benjamin75836432016-06-17 18:48:29 -0400749 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800750
Adam Langley95c29f32014-06-20 12:00:00 -0700751err:
David Benjamine8d53502015-10-10 14:13:23 -0400752 CBB_cleanup(&cbb);
Adam Langley1bea1732014-12-17 19:06:57 -0800753 return -1;
754}
Adam Langley95c29f32014-06-20 12:00:00 -0700755
David Benjaminaa7734b2016-06-07 16:40:46 -0400756static int dtls1_get_hello_verify(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -0700757 int al;
David Benjaminaa7734b2016-06-07 16:40:46 -0400758 CBS hello_verify_request, cookie;
759 uint16_t server_version;
760
David Benjamin09eb6552016-07-08 14:32:11 -0700761 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
762 if (ret <= 0) {
763 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400764 }
765
766 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
767 ssl->d1->send_cookie = 0;
768 ssl->s3->tmp.reuse_message = 1;
769 return 1;
770 }
771
David Benjamin09eb6552016-07-08 14:32:11 -0700772 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400773
774 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
775 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
776 CBS_len(&hello_verify_request) != 0) {
777 al = SSL_AD_DECODE_ERROR;
778 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
779 goto f_err;
780 }
781
782 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
783 al = SSL_AD_ILLEGAL_PARAMETER;
784 goto f_err;
785 }
786
787 memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
788 ssl->d1->cookie_len = CBS_len(&cookie);
789
790 ssl->d1->send_cookie = 1;
791 return 1;
792
793f_err:
794 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
795 return -1;
796}
797
798static int ssl3_get_server_hello(SSL *ssl) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400799 STACK_OF(SSL_CIPHER) *sk;
Adam Langley1bea1732014-12-17 19:06:57 -0800800 const SSL_CIPHER *c;
David Benjamin0d56f882015-12-19 17:05:56 -0500801 CERT *ct = ssl->cert;
David Benjamin09eb6552016-07-08 14:32:11 -0700802 int al = SSL_AD_INTERNAL_ERROR;
Adam Langley1bea1732014-12-17 19:06:57 -0800803 CBS server_hello, server_random, session_id;
David Benjaminb6a0a512016-06-21 10:33:21 -0400804 uint16_t server_wire_version, server_version, cipher_suite;
Adam Langley1bea1732014-12-17 19:06:57 -0800805 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Steven Valdez143e8b32016-07-11 13:19:03 -0400807 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
David Benjamin09eb6552016-07-08 14:32:11 -0700808 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500809 uint32_t err = ERR_peek_error();
810 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
811 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
812 /* Add a dedicated error code to the queue for a handshake_failure alert
813 * in response to ClientHello. This matches NSS's client behavior and
814 * gives a better error on a (probable) failure to negotiate initial
815 * parameters. Note: this error code comes after the original one.
816 *
817 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400818 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500819 }
David Benjamin09eb6552016-07-08 14:32:11 -0700820 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800821 }
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Steven Valdez143e8b32016-07-11 13:19:03 -0400823 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
824 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
825 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
826 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
827 return -1;
828 }
829
David Benjamin09eb6552016-07-08 14:32:11 -0700830 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
David Benjamina03d95d2014-07-12 19:49:07 -0400831
Steven Valdez143e8b32016-07-11 13:19:03 -0400832 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800833 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800835 goto f_err;
836 }
David Benjamina03d95d2014-07-12 19:49:07 -0400837
David Benjaminb6a0a512016-06-21 10:33:21 -0400838 server_version = ssl->method->version_from_wire(server_wire_version);
839
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200840 uint16_t min_version, max_version, real_max_version;
841 if (!ssl_get_full_version_range(ssl, &min_version, &max_version,
842 &real_max_version) ||
David Benjamin1f61f0d2016-07-10 12:20:35 -0400843 server_version < min_version || server_version > max_version) {
844 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
845 al = SSL_AD_PROTOCOL_VERSION;
846 goto f_err;
847 }
848
David Benjamin0d56f882015-12-19 17:05:56 -0500849 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
850 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400851 ssl->version = server_wire_version;
David Benjaminb83003e2015-12-30 17:39:26 -0500852 ssl->s3->enc_method = ssl3_get_enc_method(server_version);
853 assert(ssl->s3->enc_method != NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500854 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800855 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500856 ssl->s3->have_version = 1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400857 } else if (server_wire_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400858 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800859 al = SSL_AD_PROTOCOL_VERSION;
860 goto f_err;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Steven Valdez143e8b32016-07-11 13:19:03 -0400863 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
864 ssl->state = SSL_ST_TLS13;
865 return 1;
866 }
867
David Benjamin4fe3c902016-08-16 02:17:03 -0400868 ssl_clear_tls13_state(ssl);
869
Steven Valdez143e8b32016-07-11 13:19:03 -0400870 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
871 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
872 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
873 return -1;
874 }
875
876 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
877 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
878 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
879 !CBS_get_u16(&server_hello, &cipher_suite) ||
880 !CBS_get_u8(&server_hello, &compression_method)) {
881 al = SSL_AD_DECODE_ERROR;
882 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
883 goto f_err;
884 }
885
Adam Langley1bea1732014-12-17 19:06:57 -0800886 /* Copy over the server random. */
David Benjamin0d56f882015-12-19 17:05:56 -0500887 memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langley95c29f32014-06-20 12:00:00 -0700888
David Benjamin55108632016-08-11 22:01:18 -0400889 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
890 * 1.3 is finalized and we are not implementing a draft version. */
David Benjamin1f61f0d2016-07-10 12:20:35 -0400891
David Benjamin0d56f882015-12-19 17:05:56 -0500892 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400893 ssl->session->session_id_length != 0 &&
David Benjamin0d56f882015-12-19 17:05:56 -0500894 CBS_mem_equal(&session_id, ssl->session->session_id,
895 ssl->session->session_id_length)) {
Steven Valdez4aa154e2016-07-29 14:32:55 -0400896 ssl->s3->session_reused = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800897 } else {
898 /* The session wasn't resumed. Create a fresh SSL_SESSION to
899 * fill out. */
David Benjamin4d0be242016-09-01 01:10:07 -0400900 ssl_set_session(ssl, NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500901 if (!ssl_get_new_session(ssl, 0 /* client */)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800902 goto f_err;
903 }
904 /* Note: session_id could be empty. */
Steven Valdez87eab492016-06-27 16:34:59 -0400905 ssl->s3->new_session->session_id_length = CBS_len(&session_id);
906 memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
David Benjamin0d56f882015-12-19 17:05:56 -0500907 CBS_len(&session_id));
Adam Langley1bea1732014-12-17 19:06:57 -0800908 }
David Benjamina03d95d2014-07-12 19:49:07 -0400909
David Benjamina1c90a52015-05-30 17:03:14 -0400910 c = SSL_get_cipher_by_value(cipher_suite);
Adam Langley1bea1732014-12-17 19:06:57 -0800911 if (c == NULL) {
912 /* unknown cipher */
913 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400914 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800915 goto f_err;
916 }
Adam Langley1bea1732014-12-17 19:06:57 -0800917 /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
918 * the server selected it, it's an error. */
David Benjamin0d56f882015-12-19 17:05:56 -0500919 if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
Nick Harper1fd39d82016-06-14 18:14:35 -0700920 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
921 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800922 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400923 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800924 goto f_err;
925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
David Benjamin0d56f882015-12-19 17:05:56 -0500927 sk = ssl_get_ciphers_by_id(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800928 if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
929 /* we did not say we would use this cipher */
930 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400931 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800932 goto f_err;
933 }
Adam Langley95c29f32014-06-20 12:00:00 -0700934
Steven Valdez87eab492016-06-27 16:34:59 -0400935 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500936 if (ssl->session->cipher != c) {
David Benjaminece3de92015-03-16 18:02:20 -0400937 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400938 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400939 goto f_err;
940 }
David Benjamin0d56f882015-12-19 17:05:56 -0500941 if (ssl->session->ssl_version != ssl->version) {
David Benjaminece3de92015-03-16 18:02:20 -0400942 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400943 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400944 goto f_err;
945 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400946 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
947 /* This is actually a client application bug. */
948 al = SSL_AD_ILLEGAL_PARAMETER;
949 OPENSSL_PUT_ERROR(SSL,
950 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
951 goto f_err;
952 }
David Benjamin0623bce2015-12-25 15:40:14 -0500953 } else {
Steven Valdez87eab492016-06-27 16:34:59 -0400954 ssl->s3->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800955 }
David Benjamin0d56f882015-12-19 17:05:56 -0500956 ssl->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500957
David Benjamin9550c3a2015-08-05 08:50:34 -0400958 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500959 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400960 goto f_err;
961 }
962
Steven Valdez2b8415e2016-06-30 13:27:23 -0400963 /* If doing a full handshake, the server may request a client certificate
964 * which requires hashing the handshake transcript. Otherwise, the handshake
965 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000966 if (ssl->session != NULL ||
967 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500968 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800969 }
David Benjamina03d95d2014-07-12 19:49:07 -0400970
Adam Langley1bea1732014-12-17 19:06:57 -0800971 /* Only the NULL compression algorithm is supported. */
972 if (compression_method != 0) {
973 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400974 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800975 goto f_err;
976 }
Adam Langley95c29f32014-06-20 12:00:00 -0700977
Adam Langley1bea1732014-12-17 19:06:57 -0800978 /* TLS extensions */
David Benjamin0d56f882015-12-19 17:05:56 -0500979 if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400980 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800981 goto err;
982 }
Adam Langley95c29f32014-06-20 12:00:00 -0700983
Adam Langley1bea1732014-12-17 19:06:57 -0800984 /* There should be nothing left over in the record. */
985 if (CBS_len(&server_hello) != 0) {
986 /* wrong packet length */
987 al = SSL_AD_DECODE_ERROR;
David Benjamin639846e2016-09-09 11:41:18 -0400988 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800989 goto f_err;
990 }
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Steven Valdez87eab492016-06-27 16:34:59 -0400992 if (ssl->session != NULL &&
David Benjamin0d56f882015-12-19 17:05:56 -0500993 ssl->s3->tmp.extended_master_secret !=
994 ssl->session->extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -0700995 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin0d56f882015-12-19 17:05:56 -0500996 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400997 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700998 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400999 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -07001000 }
1001 goto f_err;
1002 }
1003
Adam Langley1bea1732014-12-17 19:06:57 -08001004 return 1;
1005
Adam Langley95c29f32014-06-20 12:00:00 -07001006f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001007 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001008err:
Adam Langley1bea1732014-12-17 19:06:57 -08001009 return -1;
1010}
Adam Langley95c29f32014-06-20 12:00:00 -07001011
David Benjaminaa7734b2016-06-07 16:40:46 -04001012static int ssl3_get_server_certificate(SSL *ssl) {
David Benjamin5c900c82016-07-13 23:03:26 -04001013 int ret =
David Benjamin09eb6552016-07-08 14:32:11 -07001014 ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
David Benjamin5c900c82016-07-13 23:03:26 -04001015 if (ret <= 0) {
1016 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001017 }
Adam Langley95c29f32014-06-20 12:00:00 -07001018
David Benjamin5c900c82016-07-13 23:03:26 -04001019 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001020 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
David Benjamin5c900c82016-07-13 23:03:26 -04001021 uint8_t alert;
1022 STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs);
1023 if (chain == NULL) {
1024 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley1bea1732014-12-17 19:06:57 -08001025 goto err;
1026 }
Adam Langley95c29f32014-06-20 12:00:00 -07001027
David Benjamin5c900c82016-07-13 23:03:26 -04001028 if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -04001029 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001030 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1031 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001032 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001033
David Benjamin5c900c82016-07-13 23:03:26 -04001034 X509 *leaf = sk_X509_value(chain, 0);
Steven Valdezbf5aa842016-07-15 07:07:40 -04001035 if (!ssl_check_leaf_certificate(ssl, leaf)) {
David Benjamin5c900c82016-07-13 23:03:26 -04001036 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1037 goto err;
David Benjamin436bf822015-06-07 19:50:37 -04001038 }
1039
David Benjamin436bf822015-06-07 19:50:37 -04001040 /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
1041 * the leaf. */
Steven Valdez87eab492016-06-27 16:34:59 -04001042 sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
1043 ssl->s3->new_session->cert_chain = chain;
Adam Langley95c29f32014-06-20 12:00:00 -07001044
Steven Valdez87eab492016-06-27 16:34:59 -04001045 X509_free(ssl->s3->new_session->peer);
David Benjamin96a16cd2016-08-11 12:14:47 -04001046 X509_up_ref(leaf);
1047 ssl->s3->new_session->peer = leaf;
David Benjamind26aea62014-07-12 00:13:56 -04001048
David Benjamin5c900c82016-07-13 23:03:26 -04001049 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001050
1051err:
David Benjamin5c900c82016-07-13 23:03:26 -04001052 sk_X509_pop_free(chain, X509_free);
1053 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001054}
1055
David Benjamin9f1dc822016-06-07 17:03:46 -04001056static int ssl3_get_cert_status(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001057 int al;
David Benjamin9f1dc822016-06-07 17:03:46 -04001058 CBS certificate_status, ocsp_response;
1059 uint8_t status_type;
1060
David Benjamin09eb6552016-07-08 14:32:11 -07001061 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1062 if (ret <= 0) {
1063 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001064 }
1065
1066 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1067 /* A server may send status_request in ServerHello and then change
1068 * its mind about sending CertificateStatus. */
1069 ssl->s3->tmp.reuse_message = 1;
1070 return 1;
1071 }
1072
David Benjamin09eb6552016-07-08 14:32:11 -07001073 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001074 if (!CBS_get_u8(&certificate_status, &status_type) ||
1075 status_type != TLSEXT_STATUSTYPE_ocsp ||
1076 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1077 CBS_len(&ocsp_response) == 0 ||
1078 CBS_len(&certificate_status) != 0) {
1079 al = SSL_AD_DECODE_ERROR;
1080 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1081 goto f_err;
1082 }
1083
Steven Valdez87eab492016-06-27 16:34:59 -04001084 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1085 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001086 al = SSL_AD_INTERNAL_ERROR;
1087 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1088 goto f_err;
1089 }
1090 return 1;
1091
1092f_err:
1093 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1094 return -1;
1095}
1096
1097static int ssl3_verify_server_cert(SSL *ssl) {
David Benjamin7aa31d62016-08-08 21:38:32 -04001098 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
1099 ssl->s3->new_session->cert_chain)) {
David Benjamine455e512016-08-01 20:11:13 -04001100 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001101 }
1102
Adam Langley9498e742016-07-18 10:17:16 -07001103 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001104}
1105
David Benjaminaa7734b2016-06-07 16:40:46 -04001106static int ssl3_get_server_key_exchange(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001107 int al;
Adam Langley1bea1732014-12-17 19:06:57 -08001108 EVP_PKEY *pkey = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001109 DH *dh = NULL;
1110 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001111 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001112
David Benjamin09eb6552016-07-08 14:32:11 -07001113 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1114 if (ret <= 0) {
1115 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001116 }
1117
David Benjamin0d56f882015-12-19 17:05:56 -05001118 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001119 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin0d56f882015-12-19 17:05:56 -05001120 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001121 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001122 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001123 return -1;
1124 }
1125
David Benjamin0d56f882015-12-19 17:05:56 -05001126 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001127 return 1;
1128 }
1129
1130 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001131 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001132 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001133 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001134
David Benjaminb35d6842015-12-29 17:08:08 -05001135 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1136 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001137
1138 if (alg_a & SSL_aPSK) {
1139 CBS psk_identity_hint;
1140
1141 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1142 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1143 &psk_identity_hint)) {
1144 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001145 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001146 goto f_err;
1147 }
1148
1149 /* Store PSK identity hint for later use, hint is used in
1150 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1151 * identity hint can be as long as the maximum length of a PSK identity.
1152 * Also do not allow NULL characters; identities are saved as C strings.
1153 *
1154 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1155 * a specific identity. */
1156 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1157 CBS_contains_zero_byte(&psk_identity_hint)) {
1158 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001159 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001160 goto f_err;
1161 }
1162
1163 /* Save the identity hint as a C string. */
David Benjaminbac75b82016-09-16 19:34:02 -04001164 if (!CBS_strdup(&psk_identity_hint, &ssl->s3->hs->peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001165 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001166 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001167 goto f_err;
1168 }
1169 }
1170
David Benjamin7061e282015-03-19 11:10:48 -04001171 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001172 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001173 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1174 CBS_len(&dh_p) == 0 ||
1175 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1176 CBS_len(&dh_g) == 0 ||
1177 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1178 CBS_len(&dh_Ys) == 0) {
1179 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001180 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001181 goto f_err;
1182 }
1183
1184 dh = DH_new();
1185 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001186 goto err;
1187 }
1188
David Benjamin48cce662015-12-17 01:33:08 -05001189 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1190 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001191 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001192 goto err;
1193 }
1194
Steven Valdez87eab492016-06-27 16:34:59 -04001195 ssl->s3->new_session->key_exchange_info = DH_num_bits(dh);
1196 if (ssl->s3->new_session->key_exchange_info < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001197 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001198 goto err;
Steven Valdez87eab492016-06-27 16:34:59 -04001199 } else if (ssl->s3->new_session->key_exchange_info > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001200 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1201 * to prevent a server from causing us to perform too expensive of a
1202 * computation. */
1203 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1204 goto err;
1205 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001206
David Benjamin0d56f882015-12-19 17:05:56 -05001207 SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001208 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001209
1210 /* Save the peer public key for later. */
1211 size_t peer_key_len;
David Benjamin0d56f882015-12-19 17:05:56 -05001212 if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001213 goto err;
1214 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001215 /* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
1216 * fits in a uint16_t. */
David Benjamin0d56f882015-12-19 17:05:56 -05001217 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1218 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
David Benjamin7061e282015-03-19 11:10:48 -04001219 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001220 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001221 uint8_t group_type;
1222 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001223 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001224 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1225 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001226 !CBS_get_u16(&server_key_exchange, &group_id) ||
1227 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001228 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001229 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001230 goto f_err;
1231 }
Steven Valdez87eab492016-06-27 16:34:59 -04001232 ssl->s3->new_session->key_exchange_info = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001233
Steven Valdezce902a92016-05-17 11:47:53 -04001234 /* Ensure the group is consistent with preferences. */
1235 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001236 al = SSL_AD_ILLEGAL_PARAMETER;
1237 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001238 goto f_err;
1239 }
David Benjamin4298d772015-12-19 00:18:25 -05001240
Matt Braithwaite053931e2016-05-25 12:06:05 -07001241 /* Initialize ECDH and save the peer public key for later. */
1242 size_t peer_key_len;
1243 if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
1244 !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001245 goto err;
1246 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001247 /* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len
1248 * fits in a uint16_t. */
1249 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1250 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
1251 } else if (alg_k & SSL_kCECPQ1) {
David Benjamin18a35182016-06-30 13:46:24 -04001252 SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
Matt Braithwaite053931e2016-05-25 12:06:05 -07001253 CBS key;
1254 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001255 al = SSL_AD_DECODE_ERROR;
1256 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1257 goto f_err;
1258 }
1259
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001260 size_t peer_key_len;
Matt Braithwaite053931e2016-05-25 12:06:05 -07001261 if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001262 goto err;
1263 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001264 /* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
1265 * fits in a uint16_t. */
David Benjamin0d56f882015-12-19 17:05:56 -05001266 assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
1267 ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
Adam Langley1bea1732014-12-17 19:06:57 -08001268 } else if (!(alg_k & SSL_kPSK)) {
1269 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001270 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001271 goto f_err;
1272 }
1273
1274 /* At this point, |server_key_exchange| contains the signature, if any, while
1275 * |server_key_exchange_orig| contains the entire message. From that, derive
1276 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001277 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001278 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1279 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1280
David Benjaminf4958e72015-06-07 11:00:53 -04001281 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001282 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdez87eab492016-06-27 16:34:59 -04001283 pkey = X509_get_pubkey(ssl->s3->new_session->peer);
David Benjaminf4958e72015-06-07 11:00:53 -04001284 if (pkey == NULL) {
1285 goto err;
1286 }
Adam Langley1bea1732014-12-17 19:06:57 -08001287
Steven Valdezf0451ca2016-06-29 13:16:27 -04001288 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001289 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001290 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001291 al = SSL_AD_DECODE_ERROR;
1292 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001293 goto f_err;
1294 }
David Benjamin887c3002016-07-08 16:15:32 -07001295 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001296 goto f_err;
1297 }
Steven Valdez02563852016-06-23 13:33:05 -04001298 ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
Adam Langley1bea1732014-12-17 19:06:57 -08001299 } else if (pkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001300 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
1301 } else if (pkey->type == EVP_PKEY_EC) {
1302 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001303 } else {
1304 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1305 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1306 goto f_err;
Adam Langley1bea1732014-12-17 19:06:57 -08001307 }
1308
1309 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001310 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001311 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1312 CBS_len(&server_key_exchange) != 0) {
1313 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001314 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001315 goto f_err;
1316 }
1317
Steven Valdez2b8415e2016-06-30 13:27:23 -04001318 CBB transcript;
1319 uint8_t *transcript_data;
1320 size_t transcript_len;
1321 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1322 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1323 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1324 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1325 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1326 CBB_cleanup(&transcript);
1327 al = SSL_AD_INTERNAL_ERROR;
1328 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001329 goto f_err;
1330 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001331
1332 int sig_ok = ssl_public_key_verify(
1333 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
1334 pkey, transcript_data, transcript_len);
1335 OPENSSL_free(transcript_data);
1336
David Benjaminbf82aed2016-03-01 22:57:40 -05001337#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1338 sig_ok = 1;
1339 ERR_clear_error();
1340#endif
1341 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001342 /* bad signature */
1343 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001344 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001345 goto f_err;
1346 }
1347 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001348 /* PSK ciphers are the only supported certificate-less ciphers. */
1349 assert(alg_a == SSL_aPSK);
1350
Adam Langley1bea1732014-12-17 19:06:57 -08001351 if (CBS_len(&server_key_exchange) > 0) {
1352 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001353 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001354 goto f_err;
1355 }
1356 }
1357 EVP_PKEY_free(pkey);
Adam Langley1bea1732014-12-17 19:06:57 -08001358 return 1;
1359
Adam Langley95c29f32014-06-20 12:00:00 -07001360f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001361 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001362err:
Adam Langley1bea1732014-12-17 19:06:57 -08001363 EVP_PKEY_free(pkey);
David Benjamin2755a3e2015-04-22 16:17:58 -04001364 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001365 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001366 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001367 return -1;
1368}
Adam Langley95c29f32014-06-20 12:00:00 -07001369
David Benjaminaa7734b2016-06-07 16:40:46 -04001370static int ssl3_get_certificate_request(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001371 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1372 if (msg_ret <= 0) {
1373 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001374 }
David Benjamin688d8df2014-11-02 23:06:42 -05001375
David Benjamin5744ca62016-06-13 14:05:19 -04001376 ssl->s3->tmp.cert_request = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001377
David Benjamin3dcec452016-06-07 17:13:28 -04001378 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001379 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001380 /* If we get here we don't need the handshake buffer as we won't be doing
1381 * client auth. */
David Benjamin0d56f882015-12-19 17:05:56 -05001382 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001383 return 1;
1384 }
David Benjamined439582014-07-14 19:13:02 -04001385
David Benjamin0d56f882015-12-19 17:05:56 -05001386 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1387 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamindf28c3a2016-03-10 16:11:51 -05001388 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamine0332e82016-07-13 22:40:36 -04001389 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001390 }
Adam Langley95c29f32014-06-20 12:00:00 -07001391
David Benjamina5177cb2016-03-11 19:38:17 -05001392 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001393 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001394
David Benjamine0332e82016-07-13 22:40:36 -04001395 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001396 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001397 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001398 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001399 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001400 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001401 }
David Benjamined439582014-07-14 19:13:02 -04001402
David Benjamin0d56f882015-12-19 17:05:56 -05001403 if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
1404 &ssl->s3->tmp.num_certificate_types)) {
1405 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001406 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001407 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001408
David Benjamina1e9cab2015-12-30 00:08:49 -05001409 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001410 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001411 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjamin0d56f882015-12-19 17:05:56 -05001412 !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
1413 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001414 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001415 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001416 }
Adam Langley1bea1732014-12-17 19:06:57 -08001417 }
David Benjamined439582014-07-14 19:13:02 -04001418
David Benjamine0332e82016-07-13 22:40:36 -04001419 uint8_t alert;
1420 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
1421 if (ca_sk == NULL) {
1422 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1423 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001424 }
Adam Langley95c29f32014-06-20 12:00:00 -07001425
David Benjamin639846e2016-09-09 11:41:18 -04001426 if (CBS_len(&cbs) != 0) {
1427 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1428 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1429 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1430 return -1;
1431 }
1432
David Benjamin5744ca62016-06-13 14:05:19 -04001433 ssl->s3->tmp.cert_request = 1;
David Benjamin0d56f882015-12-19 17:05:56 -05001434 sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
1435 ssl->s3->tmp.ca_names = ca_sk;
David Benjamine0332e82016-07-13 22:40:36 -04001436 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001437}
Adam Langley95c29f32014-06-20 12:00:00 -07001438
David Benjamin3dcec452016-06-07 17:13:28 -04001439static int ssl3_get_server_hello_done(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001440 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
1441 ssl_hash_message);
1442 if (ret <= 0) {
1443 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001444 }
David Benjamin46062682014-07-14 19:14:32 -04001445
David Benjamin09eb6552016-07-08 14:32:11 -07001446 /* ServerHelloDone is empty. */
1447 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001448 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001449 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001450 return -1;
1451 }
David Benjamin46062682014-07-14 19:14:32 -04001452
Adam Langley1bea1732014-12-17 19:06:57 -08001453 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001454}
Adam Langley1258b6a2014-06-20 12:00:00 -07001455
David Benjamin9f1dc822016-06-07 17:03:46 -04001456static int ssl3_send_client_certificate(SSL *ssl) {
1457 if (ssl->state == SSL3_ST_CW_CERT_A) {
1458 /* Call cert_cb to update the certificate. */
1459 if (ssl->cert->cert_cb) {
1460 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1461 if (ret < 0) {
1462 ssl->rwstate = SSL_X509_LOOKUP;
1463 return -1;
1464 }
1465 if (ret == 0) {
1466 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1467 return -1;
1468 }
1469 }
1470
David Benjamin13f1ebe2016-07-20 10:11:04 +02001471 ssl->state = SSL3_ST_CW_CERT_B;
David Benjamin9f1dc822016-06-07 17:03:46 -04001472 }
1473
1474 if (ssl->state == SSL3_ST_CW_CERT_B) {
1475 /* Call client_cert_cb to update the certificate. */
David Benjamin13f1ebe2016-07-20 10:11:04 +02001476 int should_retry;
1477 if (!ssl_do_client_cert_cb(ssl, &should_retry)) {
1478 if (should_retry) {
1479 ssl->rwstate = SSL_X509_LOOKUP;
1480 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001481 return -1;
1482 }
1483
David Benjamin32a66d52016-07-13 22:03:11 -04001484 if (!ssl_has_certificate(ssl)) {
David Benjamin5744ca62016-06-13 14:05:19 -04001485 ssl->s3->tmp.cert_request = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001486 /* Without a client certificate, the handshake buffer may be released. */
1487 ssl3_free_handshake_buffer(ssl);
1488
1489 if (ssl->version == SSL3_VERSION) {
1490 /* In SSL 3.0, send no certificate by skipping both messages. */
David Benjamin9f1dc822016-06-07 17:03:46 -04001491 ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1492 return 1;
1493 }
David Benjamin32a66d52016-07-13 22:03:11 -04001494 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001495
David Benjamin32a66d52016-07-13 22:03:11 -04001496 if (!ssl3_output_cert_chain(ssl)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001497 return -1;
1498 }
David Benjamin13f1ebe2016-07-20 10:11:04 +02001499 ssl->state = SSL3_ST_CW_CERT_C;
David Benjamin9f1dc822016-06-07 17:03:46 -04001500 }
1501
David Benjamin13f1ebe2016-07-20 10:11:04 +02001502 assert(ssl->state == SSL3_ST_CW_CERT_C);
David Benjamin75836432016-06-17 18:48:29 -04001503 return ssl->method->write_message(ssl);
David Benjamin9f1dc822016-06-07 17:03:46 -04001504}
1505
Brian Smith274341d2015-10-08 17:10:15 -10001506OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1507 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001508
David Benjaminaa7734b2016-06-07 16:40:46 -04001509static int ssl3_send_client_key_exchange(SSL *ssl) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001510 if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
David Benjamin75836432016-06-17 18:48:29 -04001511 return ssl->method->write_message(ssl);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001512 }
1513 assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
1514
Adam Langley1bea1732014-12-17 19:06:57 -08001515 uint8_t *pms = NULL;
1516 size_t pms_len = 0;
David Benjamin75836432016-06-17 18:48:29 -04001517 CBB cbb, body;
1518 if (!ssl->method->init_message(ssl, &cbb, &body,
1519 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001520 goto err;
1521 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001522
David Benjaminae0eaaa2015-12-17 02:01:25 -05001523 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1524 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001525
David Benjaminae0eaaa2015-12-17 02:01:25 -05001526 /* If using a PSK key exchange, prepare the pre-shared key. */
1527 unsigned psk_len = 0;
1528 uint8_t psk[PSK_MAX_PSK_LEN];
1529 if (alg_a & SSL_aPSK) {
1530 if (ssl->psk_client_callback == NULL) {
1531 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1532 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001533 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001534
David Benjaminae0eaaa2015-12-17 02:01:25 -05001535 char identity[PSK_MAX_IDENTITY_LEN + 1];
1536 memset(identity, 0, sizeof(identity));
1537 psk_len = ssl->psk_client_callback(
David Benjaminbac75b82016-09-16 19:34:02 -04001538 ssl, ssl->s3->hs->peer_psk_identity_hint, identity, sizeof(identity),
David Benjaminae0eaaa2015-12-17 02:01:25 -05001539 psk, sizeof(psk));
1540 if (psk_len == 0) {
1541 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1542 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1543 goto err;
1544 }
1545 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001546
Steven Valdez87eab492016-06-27 16:34:59 -04001547 OPENSSL_free(ssl->s3->new_session->psk_identity);
1548 ssl->s3->new_session->psk_identity = BUF_strdup(identity);
1549 if (ssl->s3->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001550 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1551 goto err;
1552 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001553
David Benjaminae0eaaa2015-12-17 02:01:25 -05001554 /* Write out psk_identity. */
1555 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001556 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001557 !CBB_add_bytes(&child, (const uint8_t *)identity,
1558 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001559 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001560 goto err;
1561 }
1562 }
David Benjamin758d1272015-11-20 17:47:25 -05001563
David Benjaminae0eaaa2015-12-17 02:01:25 -05001564 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1565 if (alg_k & SSL_kRSA) {
1566 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1567 pms = OPENSSL_malloc(pms_len);
1568 if (pms == NULL) {
1569 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1570 goto err;
1571 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001572
Steven Valdez87eab492016-06-27 16:34:59 -04001573 EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->peer);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001574 if (pkey == NULL) {
1575 goto err;
1576 }
1577
1578 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
1579 if (rsa == NULL) {
1580 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001581 EVP_PKEY_free(pkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001582 goto err;
1583 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001584
David Benjaminae0eaaa2015-12-17 02:01:25 -05001585 EVP_PKEY_free(pkey);
1586
1587 pms[0] = ssl->client_version >> 8;
1588 pms[1] = ssl->client_version & 0xff;
1589 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1590 goto err;
1591 }
1592
David Benjamin75836432016-06-17 18:48:29 -04001593 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001594 size_t enc_pms_len;
1595 /* In TLS, there is a length prefix. */
1596 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001597 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001598 goto err;
1599 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001600 enc_pms = &child;
1601 }
Adam Langley1bea1732014-12-17 19:06:57 -08001602
David Benjaminae0eaaa2015-12-17 02:01:25 -05001603 uint8_t *ptr;
1604 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1605 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1606 RSA_PKCS1_PADDING) ||
1607 /* Log the premaster secret, if logging is enabled. */
1608 !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
1609 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001610 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001611 goto err;
1612 }
Matt Braithwaite053931e2016-05-25 12:06:05 -07001613 } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001614 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001615 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001616 if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001617 goto err;
1618 }
1619
David Benjamin4298d772015-12-19 00:18:25 -05001620 /* Compute the premaster. */
1621 uint8_t alert;
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001622 if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len,
1623 &alert, ssl->s3->tmp.peer_key,
1624 ssl->s3->tmp.peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001625 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1626 goto err;
1627 }
David Benjamin75836432016-06-17 18:48:29 -04001628 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001629 goto err;
1630 }
David Benjamin4298d772015-12-19 00:18:25 -05001631
1632 /* The key exchange state may now be discarded. */
1633 SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
1634 OPENSSL_free(ssl->s3->tmp.peer_key);
1635 ssl->s3->tmp.peer_key = NULL;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001636 } else if (alg_k & SSL_kPSK) {
1637 /* For plain PSK, other_secret is a block of 0s with the same length as
1638 * the pre-shared key. */
1639 pms_len = psk_len;
1640 pms = OPENSSL_malloc(pms_len);
1641 if (pms == NULL) {
1642 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1643 goto err;
1644 }
1645 memset(pms, 0, pms_len);
1646 } else {
1647 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1648 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1649 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001650 }
1651
David Benjaminae0eaaa2015-12-17 02:01:25 -05001652 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1653 * key. */
1654 if (alg_a & SSL_aPSK) {
1655 CBB pms_cbb, child;
1656 uint8_t *new_pms;
1657 size_t new_pms_len;
1658
1659 CBB_zero(&pms_cbb);
1660 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1661 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1662 !CBB_add_bytes(&child, pms, pms_len) ||
1663 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1664 !CBB_add_bytes(&child, psk, psk_len) ||
1665 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1666 CBB_cleanup(&pms_cbb);
1667 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1668 goto err;
1669 }
1670 OPENSSL_cleanse(pms, pms_len);
1671 OPENSSL_free(pms);
1672 pms = new_pms;
1673 pms_len = new_pms_len;
1674 }
1675
1676 /* The message must be added to the finished hash before calculating the
1677 * master secret. */
David Benjamin75836432016-06-17 18:48:29 -04001678 if (!ssl->method->finish_message(ssl, &cbb)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001679 goto err;
1680 }
1681 ssl->state = SSL3_ST_CW_KEY_EXCH_B;
1682
Steven Valdez87eab492016-06-27 16:34:59 -04001683 ssl->s3->new_session->master_key_length =
1684 tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
1685 pms_len);
1686 if (ssl->s3->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001687 goto err;
1688 }
Steven Valdez87eab492016-06-27 16:34:59 -04001689 ssl->s3->new_session->extended_master_secret =
1690 ssl->s3->tmp.extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001691 OPENSSL_cleanse(pms, pms_len);
1692 OPENSSL_free(pms);
1693
David Benjamin75836432016-06-17 18:48:29 -04001694 return ssl->method->write_message(ssl);
Adam Langley1258b6a2014-06-20 12:00:00 -07001695
1696err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001697 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001698 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001699 OPENSSL_cleanse(pms, pms_len);
1700 OPENSSL_free(pms);
1701 }
1702 return -1;
1703}
Adam Langley1258b6a2014-06-20 12:00:00 -07001704
David Benjaminaa7734b2016-06-07 16:40:46 -04001705static int ssl3_send_cert_verify(SSL *ssl) {
David Benjaminfc825122015-12-18 01:57:43 -05001706 if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
David Benjamin75836432016-06-17 18:48:29 -04001707 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001708 }
1709
David Benjamin429fdc02016-06-17 17:33:30 -04001710 assert(ssl_has_private_key(ssl));
1711
David Benjamin75836432016-06-17 18:48:29 -04001712 CBB cbb, body, child;
1713 if (!ssl->method->init_message(ssl, &cbb, &body,
1714 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjaminfc825122015-12-18 01:57:43 -05001715 goto err;
1716 }
1717
David Benjaminea9a0d52016-07-08 15:52:59 -07001718 uint16_t signature_algorithm;
1719 if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
1720 goto err;
1721 }
David Benjamin429fdc02016-06-17 17:33:30 -04001722 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001723 /* Write out the digest type in TLS 1.2. */
1724 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001725 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1726 goto err;
1727 }
1728 }
David Benjaminfc825122015-12-18 01:57:43 -05001729
David Benjamin429fdc02016-06-17 17:33:30 -04001730 /* Set aside space for the signature. */
David Benjaminfc825122015-12-18 01:57:43 -05001731 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjamin429fdc02016-06-17 17:33:30 -04001732 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001733 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001734 !CBB_reserve(&child, &ptr, max_sig_len)) {
1735 goto err;
1736 }
1737
Steven Valdez2b8415e2016-06-30 13:27:23 -04001738 size_t sig_len = max_sig_len;
David Benjaminfc825122015-12-18 01:57:43 -05001739 enum ssl_private_key_result_t sign_result;
1740 if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001741 /* The SSL3 construction for CertificateVerify does not decompose into a
1742 * single final digest and signature, and must be special-cased. */
1743 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1744 if (ssl->cert->key_method != NULL) {
1745 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1746 goto err;
1747 }
1748
David Benjamin0aa25bd2016-07-08 14:44:56 -07001749 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001750 uint8_t digest[EVP_MAX_MD_SIZE];
1751 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001752 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001753 signature_algorithm)) {
1754 goto err;
1755 }
1756
1757 sign_result = ssl_private_key_success;
1758
1759 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1760 if (pctx == NULL ||
1761 !EVP_PKEY_sign_init(pctx) ||
David Benjamin0aa25bd2016-07-08 14:44:56 -07001762 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001763 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1764 EVP_PKEY_CTX_free(pctx);
1765 sign_result = ssl_private_key_failure;
1766 goto err;
1767 }
1768 EVP_PKEY_CTX_free(pctx);
1769 } else {
1770 sign_result = ssl_private_key_sign(
1771 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
1772 (const uint8_t *)ssl->s3->handshake_buffer->data,
1773 ssl->s3->handshake_buffer->length);
David Benjaminfc825122015-12-18 01:57:43 -05001774 }
1775
1776 /* The handshake buffer is no longer necessary. */
1777 ssl3_free_handshake_buffer(ssl);
David Benjaminfc825122015-12-18 01:57:43 -05001778 } else {
1779 assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjamind3440b42016-07-14 14:52:41 -04001780 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
David Benjaminfc825122015-12-18 01:57:43 -05001781 }
1782
1783 switch (sign_result) {
1784 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001785 break;
1786 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001787 goto err;
1788 case ssl_private_key_retry:
1789 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
1790 ssl->state = SSL3_ST_CW_CERT_VRFY_B;
1791 goto err;
1792 }
1793
David Benjaminfc825122015-12-18 01:57:43 -05001794 if (!CBB_did_write(&child, sig_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001795 !ssl->method->finish_message(ssl, &cbb)) {
David Benjaminfc825122015-12-18 01:57:43 -05001796 goto err;
1797 }
1798
1799 ssl->state = SSL3_ST_CW_CERT_VRFY_C;
David Benjamin75836432016-06-17 18:48:29 -04001800 return ssl->method->write_message(ssl);
David Benjaminfc825122015-12-18 01:57:43 -05001801
1802err:
1803 CBB_cleanup(&cbb);
1804 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001805}
1806
David Benjaminaa7734b2016-06-07 16:40:46 -04001807static int ssl3_send_next_proto(SSL *ssl) {
David Benjamine97b9622015-10-10 14:59:09 -04001808 if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
David Benjamin75836432016-06-17 18:48:29 -04001809 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001810 }
1811
David Benjamine97b9622015-10-10 14:59:09 -04001812 assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
1813
1814 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001815 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001816
David Benjamin75836432016-06-17 18:48:29 -04001817 CBB cbb, body, child;
1818 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1819 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001820 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1821 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001822 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001823 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001824 !ssl->method->finish_message(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001825 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1826 CBB_cleanup(&cbb);
1827 return -1;
1828 }
1829
1830 ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
David Benjamin75836432016-06-17 18:48:29 -04001831 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001832}
1833
David Benjaminaa7734b2016-06-07 16:40:46 -04001834static int ssl3_send_channel_id(SSL *ssl) {
David Benjamin96e97b12015-10-10 16:30:22 -04001835 if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
David Benjamin75836432016-06-17 18:48:29 -04001836 return ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001837 }
1838
David Benjamin96e97b12015-10-10 16:30:22 -04001839 assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
1840
1841 if (ssl->tlsext_channel_id_private == NULL &&
1842 ssl->ctx->channel_id_cb != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001843 EVP_PKEY *key = NULL;
David Benjamin96e97b12015-10-10 16:30:22 -04001844 ssl->ctx->channel_id_cb(ssl, &key);
1845 if (key != NULL &&
1846 !SSL_set1_tls_channel_id(ssl, key)) {
1847 EVP_PKEY_free(key);
1848 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001849 }
David Benjamin96e97b12015-10-10 16:30:22 -04001850 EVP_PKEY_free(key);
Adam Langley1bea1732014-12-17 19:06:57 -08001851 }
1852
David Benjamin96e97b12015-10-10 16:30:22 -04001853 if (ssl->tlsext_channel_id_private == NULL) {
1854 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001855 return -1;
1856 }
Adam Langley1bea1732014-12-17 19:06:57 -08001857
David Benjamin758d1272015-11-20 17:47:25 -05001858 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
1859 if (ec_key == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -04001860 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin961ad6a2015-06-12 01:40:23 -04001861 return -1;
1862 }
David Benjamin961ad6a2015-06-12 01:40:23 -04001863
David Benjamin96e97b12015-10-10 16:30:22 -04001864 int ret = -1;
David Benjamin96e97b12015-10-10 16:30:22 -04001865 BIGNUM *x = BN_new();
1866 BIGNUM *y = BN_new();
1867 ECDSA_SIG *sig = NULL;
1868 if (x == NULL || y == NULL ||
1869 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
1870 EC_KEY_get0_public_key(ec_key),
1871 x, y, NULL)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001872 goto err;
1873 }
1874
David Benjamin961ad6a2015-06-12 01:40:23 -04001875 uint8_t digest[EVP_MAX_MD_SIZE];
David Benjamind6a4ae92015-08-06 11:10:51 -04001876 size_t digest_len;
David Benjamin96e97b12015-10-10 16:30:22 -04001877 if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001878 goto err;
1879 }
1880
David Benjamin961ad6a2015-06-12 01:40:23 -04001881 sig = ECDSA_do_sign(digest, digest_len, ec_key);
Adam Langley1bea1732014-12-17 19:06:57 -08001882 if (sig == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001883 goto err;
1884 }
1885
David Benjamin75836432016-06-17 18:48:29 -04001886 CBB cbb, body, child;
David Benjamind94b83b2016-07-06 13:50:14 -07001887 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
David Benjamin75836432016-06-17 18:48:29 -04001888 !CBB_add_u16(&body, TLSEXT_TYPE_channel_id) ||
1889 !CBB_add_u16_length_prefixed(&body, &child) ||
1890 !BN_bn2cbb_padded(&child, 32, x) || !BN_bn2cbb_padded(&child, 32, y) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001891 !BN_bn2cbb_padded(&child, 32, sig->r) ||
1892 !BN_bn2cbb_padded(&child, 32, sig->s) ||
David Benjamin75836432016-06-17 18:48:29 -04001893 !ssl->method->finish_message(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001894 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001895 CBB_cleanup(&cbb);
Adam Langley1bea1732014-12-17 19:06:57 -08001896 goto err;
1897 }
1898
David Benjamin96e97b12015-10-10 16:30:22 -04001899 ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
David Benjamin75836432016-06-17 18:48:29 -04001900 ret = ssl->method->write_message(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001901
1902err:
David Benjamin96e97b12015-10-10 16:30:22 -04001903 BN_free(x);
1904 BN_free(y);
David Benjamin2755a3e2015-04-22 16:17:58 -04001905 ECDSA_SIG_free(sig);
Adam Langley1bea1732014-12-17 19:06:57 -08001906 return ret;
1907}
1908
David Benjamin9f1dc822016-06-07 17:03:46 -04001909static int ssl3_get_new_session_ticket(SSL *ssl) {
David Benjamin09eb6552016-07-08 14:32:11 -07001910 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
1911 ssl_hash_message);
1912 if (ret <= 0) {
1913 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001914 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001915
David Benjamin9f1dc822016-06-07 17:03:46 -04001916 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001917 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001918 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001919 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001920 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1921 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001922 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001923 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001924 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001925 }
1926
David Benjamin9f1dc822016-06-07 17:03:46 -04001927 if (CBS_len(&ticket) == 0) {
1928 /* RFC 5077 allows a server to change its mind and send no ticket after
1929 * negotiating the extension. The value of |tlsext_ticket_expected| is
1930 * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
1931 * update. */
1932 ssl->tlsext_ticket_expected = 0;
1933 return 1;
1934 }
1935
Steven Valdez87eab492016-06-27 16:34:59 -04001936 int session_renewed = ssl->session != NULL;
1937 SSL_SESSION *session = ssl->s3->new_session;
1938 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001939 /* The server is sending a new ticket for an existing session. Sessions are
1940 * immutable once established, so duplicate all but the ticket of the
1941 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001942 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001943 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001944 /* This should never happen. */
1945 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1946 goto err;
1947 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001948 }
1949
Steven Valdez87eab492016-06-27 16:34:59 -04001950 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001951 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1952 goto err;
1953 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001954 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001955
1956 /* Generate a session ID for this session based on the session ticket. We use
1957 * the session ID mechanism for detecting ticket resumption. This also fits in
1958 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001959 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1960 session->session_id, &session->session_id_length,
1961 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001962 goto err;
1963 }
1964
Steven Valdez87eab492016-06-27 16:34:59 -04001965 if (session_renewed) {
1966 session->not_resumable = 0;
1967 SSL_SESSION_free(ssl->session);
1968 ssl->session = session;
1969 }
1970
David Benjamin454aa4c2016-03-10 08:43:27 -05001971 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001972
David Benjamin9f1dc822016-06-07 17:03:46 -04001973err:
Steven Valdez87eab492016-06-27 16:34:59 -04001974 if (session_renewed) {
1975 SSL_SESSION_free(session);
1976 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001977 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001978}