blob: c3d35729accea4bc971aa47d9213ba7fe6a2b3a0 [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 Benjaminabbbee12016-10-31 19:20:42 -0400155#include <openssl/aead.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400156#include <openssl/bn.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700157#include <openssl/buf.h>
David Benjamin03973092014-06-24 23:27:17 -0400158#include <openssl/bytestring.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400159#include <openssl/dh.h>
160#include <openssl/ec_key.h>
161#include <openssl/ecdsa.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400162#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700163#include <openssl/evp.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700164#include <openssl/md5.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400165#include <openssl/mem.h>
David Benjamin961ad6a2015-06-12 01:40:23 -0400166#include <openssl/rand.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700167#include <openssl/x509.h>
David Benjamin436bf822015-06-07 19:50:37 -0400168#include <openssl/x509v3.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin17cf2cb2016-12-13 01:07:13 -0500170#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400171#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjaminc3c88822016-11-14 10:32:04 +0900174static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
175static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
176static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
177static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
178static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
179static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
180static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
181static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
182static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
183static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
184static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
185static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
186static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
187static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
188static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400189
David Benjamince8c9d22016-11-14 10:45:16 +0900190int ssl3_connect(SSL_HANDSHAKE *hs) {
191 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -0800192 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400193 int state, skip = 0;
David Benjaminbeb47022014-11-30 02:58:52 -0500194
David Benjamin0d56f882015-12-19 17:05:56 -0500195 assert(ssl->handshake_func == ssl3_connect);
196 assert(!ssl->server);
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley1bea1732014-12-17 19:06:57 -0800198 for (;;) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500199 state = hs->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjamincb0c29f2016-12-12 17:00:50 -0500201 switch (hs->state) {
David Benjaminfc0c9d92016-09-01 01:33:33 -0400202 case SSL_ST_INIT:
David Benjamincb0c29f2016-12-12 17:00:50 -0500203 hs->state = SSL_ST_CONNECT;
David Benjaminfc0c9d92016-09-01 01:33:33 -0400204 skip = 1;
205 break;
206
Adam Langley1bea1732014-12-17 19:06:57 -0800207 case SSL_ST_CONNECT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400208 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500209 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800210 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700211
Adam Langley1bea1732014-12-17 19:06:57 -0800212 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900213 ret = ssl3_send_client_hello(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800214 if (ret <= 0) {
215 goto end;
216 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400217
David Benjamince079fd2016-08-02 16:22:34 -0400218 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500219 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400220 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500221 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400222 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500223 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley1bea1732014-12-17 19:06:57 -0800224 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700225
David Benjaminaa7734b2016-06-07 16:40:46 -0400226 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamince079fd2016-08-02 16:22:34 -0400227 assert(SSL_is_dtls(ssl));
David Benjaminc3c88822016-11-14 10:32:04 +0900228 ret = dtls1_get_hello_verify(hs);
David Benjaminaa7734b2016-06-07 16:40:46 -0400229 if (ret <= 0) {
230 goto end;
231 }
232 if (ssl->d1->send_cookie) {
233 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500234 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400235 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500236 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjaminaa7734b2016-06-07 16:40:46 -0400237 }
238 break;
239
Adam Langley1bea1732014-12-17 19:06:57 -0800240 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900241 ret = ssl3_get_server_hello(hs);
David Benjamincb0c29f2016-12-12 17:00:50 -0500242 if (hs->state == SSL_ST_TLS13) {
Steven Valdez143e8b32016-07-11 13:19:03 -0400243 break;
244 }
Adam Langley1bea1732014-12-17 19:06:57 -0800245 if (ret <= 0) {
246 goto end;
247 }
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Steven Valdez87eab492016-06-27 16:34:59 -0400249 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500250 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800251 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500252 hs->state = SSL3_ST_CR_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800253 }
Adam Langley1bea1732014-12-17 19:06:57 -0800254 break;
David Benjamin2b0aeec2014-07-01 00:39:02 -0400255
Adam Langley1bea1732014-12-17 19:06:57 -0800256 case SSL3_ST_CR_CERT_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000257 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900258 ret = ssl3_get_server_certificate(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800259 if (ret <= 0) {
260 goto end;
261 }
Adam Langley1bea1732014-12-17 19:06:57 -0800262 } else {
263 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800264 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500265 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800266 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjamin9f1dc822016-06-07 17:03:46 -0400268 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900269 if (hs->certificate_status_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900270 ret = ssl3_get_cert_status(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400271 if (ret <= 0) {
272 goto end;
273 }
274 } else {
275 skip = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400276 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500277 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin9f1dc822016-06-07 17:03:46 -0400278 break;
279
Paul Lietar8f1c2682015-08-18 12:21:54 +0100280 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin3d458dc2016-09-12 22:40:27 +0000281 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900282 ret = ssl3_verify_server_cert(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400283 if (ret <= 0) {
284 goto end;
285 }
286 } else {
287 skip = 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100288 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500289 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100290 break;
291
Adam Langley1bea1732014-12-17 19:06:57 -0800292 case SSL3_ST_CR_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900293 ret = ssl3_get_server_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800294 if (ret <= 0) {
295 goto end;
296 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500297 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800298 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley1bea1732014-12-17 19:06:57 -0800300 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin3d458dc2016-09-12 22:40:27 +0000301 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900302 ret = ssl3_get_certificate_request(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400303 if (ret <= 0) {
304 goto end;
305 }
306 } else {
307 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800308 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500309 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800310 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700311
Adam Langley1bea1732014-12-17 19:06:57 -0800312 case SSL3_ST_CR_SRVR_DONE_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900313 ret = ssl3_get_server_hello_done(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800314 if (ret <= 0) {
315 goto end;
316 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400317 ssl->method->received_flight(ssl);
David Benjamincb0c29f2016-12-12 17:00:50 -0500318 hs->state = SSL3_ST_CW_CERT_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley1bea1732014-12-17 19:06:57 -0800321 case SSL3_ST_CW_CERT_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900322 if (hs->cert_request) {
David Benjaminc3c88822016-11-14 10:32:04 +0900323 ret = ssl3_send_client_certificate(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400324 if (ret <= 0) {
325 goto end;
326 }
327 } else {
328 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800329 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500330 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800331 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley1bea1732014-12-17 19:06:57 -0800333 case SSL3_ST_CW_KEY_EXCH_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900334 ret = ssl3_send_client_key_exchange(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800335 if (ret <= 0) {
336 goto end;
337 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500338 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800339 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langley1bea1732014-12-17 19:06:57 -0800341 case SSL3_ST_CW_CERT_VRFY_A:
342 case SSL3_ST_CW_CERT_VRFY_B:
David Benjamina660e7a2017-01-02 08:15:35 -0500343 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjaminc3c88822016-11-14 10:32:04 +0900344 ret = ssl3_send_cert_verify(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400345 if (ret <= 0) {
346 goto end;
347 }
348 } else {
349 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800350 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500351 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800352 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
David Benjamin352d0a92016-06-28 11:22:02 -0400354 case SSL3_ST_CW_CHANGE:
David Benjamindaf207a2017-01-03 18:37:41 -0500355 if (!ssl->method->add_change_cipher_spec(ssl) ||
356 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800357 ret = -1;
358 goto end;
359 }
Adam Langley95c29f32014-06-20 12:00:00 -0700360
David Benjamindaf207a2017-01-03 18:37:41 -0500361 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800362 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley1bea1732014-12-17 19:06:57 -0800364 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900365 if (hs->next_proto_neg_seen) {
David Benjaminc3c88822016-11-14 10:32:04 +0900366 ret = ssl3_send_next_proto(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400367 if (ret <= 0) {
368 goto end;
369 }
Adam Langley1bea1732014-12-17 19:06:57 -0800370 } else {
David Benjamin47749a62016-06-13 13:56:33 -0400371 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800372 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500373 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800374 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley1bea1732014-12-17 19:06:57 -0800376 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjamin47749a62016-06-13 13:56:33 -0400377 if (ssl->s3->tlsext_channel_id_valid) {
David Benjaminc3c88822016-11-14 10:32:04 +0900378 ret = ssl3_send_channel_id(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400379 if (ret <= 0) {
380 goto end;
381 }
382 } else {
383 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800384 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500385 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800386 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley1bea1732014-12-17 19:06:57 -0800388 case SSL3_ST_CW_FINISHED_A:
David Benjamin16315f72017-01-12 20:02:05 -0500389 ret = ssl3_send_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800390 if (ret <= 0) {
391 goto end;
392 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500393 hs->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Steven Valdez87eab492016-06-27 16:34:59 -0400395 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500396 hs->next_state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800397 } else {
398 /* This is a non-resumption handshake. If it involves ChannelID, then
399 * record the handshake hashes at this point in the session so that
400 * any resumption of this session with ChannelID can sign those
401 * hashes. */
David Benjamin0d56f882015-12-19 17:05:56 -0500402 ret = tls1_record_handshake_hashes_for_channel_id(ssl);
Adam Langley49c7af12015-07-10 14:33:46 -0700403 if (ret <= 0) {
404 goto end;
Adam Langley1bea1732014-12-17 19:06:57 -0800405 }
David Benjamin0d56f882015-12-19 17:05:56 -0500406 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
407 ssl3_can_false_start(ssl) &&
David Benjamined7c4752015-02-16 19:16:46 -0500408 /* No False Start on renegotiation (would complicate the state
409 * machine). */
David Benjamin0d56f882015-12-19 17:05:56 -0500410 !ssl->s3->initial_handshake_complete) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500411 hs->next_state = SSL3_ST_FALSE_START;
Adam Langley1bea1732014-12-17 19:06:57 -0800412 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500413 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800414 }
415 }
Adam Langley1bea1732014-12-17 19:06:57 -0800416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
David Benjamin9f1dc822016-06-07 17:03:46 -0400418 case SSL3_ST_FALSE_START:
David Benjamincb0c29f2016-12-12 17:00:50 -0500419 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamince8c9d22016-11-14 10:45:16 +0900420 hs->in_false_start = 1;
David Benjamin9f1dc822016-06-07 17:03:46 -0400421 ret = 1;
422 goto end;
423
Adam Langley1bea1732014-12-17 19:06:57 -0800424 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamince8c9d22016-11-14 10:45:16 +0900425 if (hs->ticket_expected) {
David Benjaminc3c88822016-11-14 10:32:04 +0900426 ret = ssl3_get_new_session_ticket(hs);
David Benjamin47749a62016-06-13 13:56:33 -0400427 if (ret <= 0) {
428 goto end;
429 }
430 } else {
431 skip = 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800432 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500433 hs->state = SSL3_ST_CR_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800434 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langley1bea1732014-12-17 19:06:57 -0800436 case SSL3_ST_CR_CHANGE:
David Benjaminf0ee9072016-06-15 17:44:37 -0400437 ret = ssl->method->read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500438 if (ret <= 0) {
439 goto end;
440 }
441
David Benjamin67739722016-11-17 17:03:59 +0900442 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800443 ret = -1;
444 goto end;
445 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500446 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langley1bea1732014-12-17 19:06:57 -0800447 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langley1bea1732014-12-17 19:06:57 -0800449 case SSL3_ST_CR_FINISHED_A:
David Benjaminc3c88822016-11-14 10:32:04 +0900450 ret = ssl3_get_finished(hs);
Adam Langley1bea1732014-12-17 19:06:57 -0800451 if (ret <= 0) {
452 goto end;
453 }
David Benjaminaa7734b2016-06-07 16:40:46 -0400454 ssl->method->received_flight(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Steven Valdez87eab492016-06-27 16:34:59 -0400456 if (ssl->session != NULL) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500457 hs->state = SSL3_ST_CW_CHANGE;
Adam Langley1bea1732014-12-17 19:06:57 -0800458 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -0500459 hs->state = SSL_ST_OK;
Adam Langley1bea1732014-12-17 19:06:57 -0800460 }
Adam Langley1bea1732014-12-17 19:06:57 -0800461 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langley1bea1732014-12-17 19:06:57 -0800463 case SSL3_ST_CW_FLUSH:
David Benjamin8d5f9da2017-01-01 17:41:30 -0500464 ret = ssl->method->flush_flight(ssl);
465 if (ret <= 0) {
Adam Langley1bea1732014-12-17 19:06:57 -0800466 goto end;
467 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500468 hs->state = hs->next_state;
469 if (hs->state != SSL_ST_OK) {
David Benjaminaa7734b2016-06-07 16:40:46 -0400470 ssl->method->expect_flight(ssl);
471 }
Adam Langley1bea1732014-12-17 19:06:57 -0800472 break;
Adam Langleyadb739e2014-06-20 12:00:00 -0700473
Steven Valdez143e8b32016-07-11 13:19:03 -0400474 case SSL_ST_TLS13:
David Benjaminc3c88822016-11-14 10:32:04 +0900475 ret = tls13_handshake(hs);
Steven Valdez143e8b32016-07-11 13:19:03 -0400476 if (ret <= 0) {
477 goto end;
478 }
David Benjamincb0c29f2016-12-12 17:00:50 -0500479 hs->state = SSL_ST_OK;
Steven Valdez143e8b32016-07-11 13:19:03 -0400480 break;
481
Adam Langley1bea1732014-12-17 19:06:57 -0800482 case SSL_ST_OK:
David Benjamin4497e582016-07-27 17:51:49 -0400483 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Steven Valdez87eab492016-06-27 16:34:59 -0400485 SSL_SESSION_free(ssl->s3->established_session);
486 if (ssl->session != NULL) {
David Benjaminb9195402016-08-05 10:51:43 -0400487 SSL_SESSION_up_ref(ssl->session);
488 ssl->s3->established_session = ssl->session;
Steven Valdez87eab492016-06-27 16:34:59 -0400489 } else {
490 /* We make a copy of the session in order to maintain the immutability
491 * of the new established_session due to False Start. The caller may
492 * have taken a reference to the temporary session. */
493 ssl->s3->established_session =
Steven Valdez4aa154e2016-07-29 14:32:55 -0400494 SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
Steven Valdez87eab492016-06-27 16:34:59 -0400495 if (ssl->s3->established_session == NULL) {
496 /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
497 * callers. */
David Benjamincb0c29f2016-12-12 17:00:50 -0500498 hs->state = SSL_ST_ERROR;
Steven Valdez87eab492016-06-27 16:34:59 -0400499 skip = 1;
500 ret = -1;
501 goto end;
502 }
503 ssl->s3->established_session->not_resumable = 0;
504
505 SSL_SESSION_free(ssl->s3->new_session);
506 ssl->s3->new_session = NULL;
507 }
508
David Benjamin0d56f882015-12-19 17:05:56 -0500509 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
David Benjamin0d56f882015-12-19 17:05:56 -0500510 ssl->s3->initial_handshake_complete = 1;
David Benjaminba4594a2015-06-18 18:36:15 -0400511 if (is_initial_handshake) {
512 /* Renegotiations do not participate in session resumption. */
David Benjaminf3c8f8d2016-11-17 17:20:47 +0900513 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
David Benjaminba4594a2015-06-18 18:36:15 -0400514 }
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langley1bea1732014-12-17 19:06:57 -0800516 ret = 1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400517 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley1bea1732014-12-17 19:06:57 -0800518 goto end;
519
Steven Valdez87eab492016-06-27 16:34:59 -0400520 case SSL_ST_ERROR:
521 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
522 ret = -1;
523 goto end;
524
Adam Langley1bea1732014-12-17 19:06:57 -0800525 default:
David Benjamin3570d732015-06-29 00:28:17 -0400526 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley1bea1732014-12-17 19:06:57 -0800527 ret = -1;
528 goto end;
529 }
530
David Benjamincb0c29f2016-12-12 17:00:50 -0500531 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
532 int new_state = hs->state;
533 hs->state = state;
David Benjamin4e9cc712016-06-01 20:16:03 -0400534 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
David Benjamincb0c29f2016-12-12 17:00:50 -0500535 hs->state = new_state;
Adam Langley1bea1732014-12-17 19:06:57 -0800536 }
537 skip = 0;
538 }
539
Adam Langley95c29f32014-06-20 12:00:00 -0700540end:
David Benjamin4e9cc712016-06-01 20:16:03 -0400541 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley1bea1732014-12-17 19:06:57 -0800542 return ret;
543}
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin65ac9972016-09-02 21:35:25 -0400545uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
546 /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
547 * a single byte repeatedly and ensures the values are deterministic. This
548 * allows the same ClientHello be sent twice for a HelloRetryRequest or the
549 * same group be advertised in both supported_groups and key_shares. */
550 uint16_t ret = ssl->s3->client_random[index];
551 /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
552 ret = (ret & 0xf0) | 0x0a;
553 ret |= ret << 8;
554 return ret;
555}
556
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200557static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
558 uint16_t min_version,
David Benjamine0ff7672016-09-19 18:40:03 -0400559 uint16_t max_version) {
David Benjamine8d53502015-10-10 14:13:23 -0400560 /* Prepare disabled cipher masks. */
561 ssl_set_client_disabled(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700562
David Benjamine8d53502015-10-10 14:13:23 -0400563 CBB child;
564 if (!CBB_add_u16_length_prefixed(out, &child)) {
565 return 0;
Adam Langley1bea1732014-12-17 19:06:57 -0800566 }
567
David Benjamin65ac9972016-09-02 21:35:25 -0400568 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
569 if (ssl->ctx->grease_enabled &&
570 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
571 return 0;
572 }
573
David Benjaminabbbee12016-10-31 19:20:42 -0400574 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
575 * hardware support. */
576 if (max_version >= TLS1_3_VERSION) {
577 if (!EVP_has_aes_hardware() &&
578 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
579 return 0;
David Benjaminc7817d82015-11-05 18:28:33 -0500580 }
David Benjaminabbbee12016-10-31 19:20:42 -0400581 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
582 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
583 return 0;
David Benjamine8d53502015-10-10 14:13:23 -0400584 }
David Benjaminabbbee12016-10-31 19:20:42 -0400585 if (EVP_has_aes_hardware() &&
586 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
David Benjamine8d53502015-10-10 14:13:23 -0400587 return 0;
588 }
589 }
590
David Benjaminabbbee12016-10-31 19:20:42 -0400591 if (min_version < TLS1_3_VERSION) {
592 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
593 int any_enabled = 0;
594 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
595 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
596 /* Skip disabled ciphers */
597 if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
598 (cipher->algorithm_auth & ssl->cert->mask_a)) {
599 continue;
600 }
601 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
602 SSL_CIPHER_get_max_version(cipher) < min_version) {
603 continue;
604 }
605 any_enabled = 1;
606 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
607 return 0;
608 }
609 }
610
611 /* If all ciphers were disabled, return the error to the caller. */
612 if (!any_enabled && max_version < TLS1_3_VERSION) {
613 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
614 return 0;
615 }
David Benjamine8d53502015-10-10 14:13:23 -0400616 }
617
618 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
619 * added. */
David Benjaminf04c2e92016-12-06 13:35:25 -0500620 if (max_version == SSL3_VERSION &&
David Benjamine8d53502015-10-10 14:13:23 -0400621 !ssl->s3->initial_handshake_complete) {
622 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
623 return 0;
624 }
David Benjamine8d53502015-10-10 14:13:23 -0400625 }
626
David Benjamine0ff7672016-09-19 18:40:03 -0400627 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjamin5e7e7cc2016-07-21 12:55:28 +0200628 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
629 return 0;
630 }
David Benjamine8d53502015-10-10 14:13:23 -0400631 }
632
633 return CBB_flush(out);
634}
635
David Benjamin8c880a22016-12-03 02:20:34 -0500636int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
637 SSL *const ssl = hs->ssl;
David Benjamine0ff7672016-09-19 18:40:03 -0400638 uint16_t min_version, max_version;
639 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez5440fe02016-07-18 12:40:30 -0400640 return 0;
641 }
642
Steven Valdeza833c352016-11-01 13:39:36 -0400643 CBB cbb, body;
644 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
645 goto err;
646 }
647
Steven Valdez5440fe02016-07-18 12:40:30 -0400648 /* Renegotiations do not participate in session resumption. */
649 int has_session = ssl->session != NULL &&
650 !ssl->s3->initial_handshake_complete;
651
652 CBB child;
David Benjaminf04c2e92016-12-06 13:35:25 -0500653 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdeza833c352016-11-01 13:39:36 -0400654 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
655 !CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400656 (has_session &&
657 !CBB_add_bytes(&child, ssl->session->session_id,
658 ssl->session->session_id_length))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400659 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400660 }
661
David Benjamince079fd2016-08-02 16:22:34 -0400662 if (SSL_is_dtls(ssl)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400663 if (!CBB_add_u8_length_prefixed(&body, &child) ||
Steven Valdez5440fe02016-07-18 12:40:30 -0400664 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400665 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400666 }
667 }
668
669 size_t header_len =
David Benjamince079fd2016-08-02 16:22:34 -0400670 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdeza833c352016-11-01 13:39:36 -0400671 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
672 !CBB_add_u8(&body, 1 /* one compression method */) ||
673 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin8c880a22016-12-03 02:20:34 -0500674 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400675 goto err;
Steven Valdez5440fe02016-07-18 12:40:30 -0400676 }
677
Steven Valdeza833c352016-11-01 13:39:36 -0400678 uint8_t *msg = NULL;
679 size_t len;
680 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
681 goto err;
682 }
683
684 /* Now that the length prefixes have been computed, fill in the placeholder
685 * PSK binder. */
David Benjamin8c880a22016-12-03 02:20:34 -0500686 if (hs->needs_psk_binder &&
Steven Valdeza833c352016-11-01 13:39:36 -0400687 !tls13_write_psk_binder(ssl, msg, len)) {
688 OPENSSL_free(msg);
689 goto err;
690 }
691
David Benjamindaf207a2017-01-03 18:37:41 -0500692 return ssl->method->add_message(ssl, msg, len);
Steven Valdeza833c352016-11-01 13:39:36 -0400693
694 err:
695 CBB_cleanup(&cbb);
696 return 0;
Steven Valdez5440fe02016-07-18 12:40:30 -0400697}
698
David Benjaminc3c88822016-11-14 10:32:04 +0900699static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
700 SSL *const ssl = hs->ssl;
David Benjaminaa7734b2016-06-07 16:40:46 -0400701 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
702 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
703 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin13e81fc2015-11-02 17:16:13 -0500704 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
705 return -1;
706 }
707
David Benjaminb6a0a512016-06-21 10:33:21 -0400708 uint16_t min_version, max_version;
709 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400710 return -1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400711 }
712
David Benjaminf04c2e92016-12-06 13:35:25 -0500713 uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
David Benjamincb0c29f2016-12-12 17:00:50 -0500714 assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
David Benjamine8d53502015-10-10 14:13:23 -0400715 if (!ssl->s3->have_version) {
David Benjaminf04c2e92016-12-06 13:35:25 -0500716 ssl->version = max_wire_version;
717 }
Steven Valdezfdd10992016-09-15 16:27:05 -0400718
David Benjaminf04c2e92016-12-06 13:35:25 -0500719 /* Always advertise the ClientHello version from the original maximum version,
720 * even on renegotiation. The static RSA key exchange uses this field, and
721 * some servers fail when it changes across handshakes. */
722 hs->client_version = max_wire_version;
723 if (max_version >= TLS1_3_VERSION) {
724 hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
David Benjamine8d53502015-10-10 14:13:23 -0400725 }
726
David Benjamine29ea162016-03-23 16:10:44 -0400727 /* If the configured session has expired or was created at a disabled
728 * version, drop it. */
David Benjaminb6a0a512016-06-21 10:33:21 -0400729 if (ssl->session != NULL) {
David Benjamin2dc02042016-09-19 19:57:37 -0400730 uint16_t session_version;
Adam Langley364f7a62016-12-12 10:51:00 -0800731 if (ssl->session->is_server ||
732 !ssl->method->version_from_wire(&session_version,
David Benjamin2dc02042016-09-19 19:57:37 -0400733 ssl->session->ssl_version) ||
734 (session_version < TLS1_3_VERSION &&
Steven Valdez4aa154e2016-07-29 14:32:55 -0400735 ssl->session->session_id_length == 0) ||
736 ssl->session->not_resumable ||
737 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminb6a0a512016-06-21 10:33:21 -0400738 session_version < min_version || session_version > max_version) {
David Benjamin4d0be242016-09-01 01:10:07 -0400739 ssl_set_session(ssl, NULL);
David Benjaminb6a0a512016-06-21 10:33:21 -0400740 }
David Benjamine8d53502015-10-10 14:13:23 -0400741 }
742
743 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
744 * renegerate the client_random. The random must be reused. */
David Benjamince079fd2016-08-02 16:22:34 -0400745 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
David Benjamin1f61f0d2016-07-10 12:20:35 -0400746 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdeza833c352016-11-01 13:39:36 -0400747 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400748 }
749
David Benjamin8c880a22016-12-03 02:20:34 -0500750 if (!ssl_write_client_hello(hs)) {
Steven Valdeza833c352016-11-01 13:39:36 -0400751 return -1;
David Benjamine8d53502015-10-10 14:13:23 -0400752 }
753
David Benjamin16315f72017-01-12 20:02:05 -0500754 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -0800755}
Adam Langley95c29f32014-06-20 12:00:00 -0700756
David Benjaminc3c88822016-11-14 10:32:04 +0900757static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
758 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -0700759 int al;
David Benjaminaa7734b2016-06-07 16:40:46 -0400760 CBS hello_verify_request, cookie;
761 uint16_t server_version;
762
David Benjamin09eb6552016-07-08 14:32:11 -0700763 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
764 if (ret <= 0) {
765 return ret;
David Benjaminaa7734b2016-06-07 16:40:46 -0400766 }
767
768 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
769 ssl->d1->send_cookie = 0;
770 ssl->s3->tmp.reuse_message = 1;
771 return 1;
772 }
773
David Benjamin09eb6552016-07-08 14:32:11 -0700774 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjaminaa7734b2016-06-07 16:40:46 -0400775
776 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
777 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
778 CBS_len(&hello_verify_request) != 0) {
779 al = SSL_AD_DECODE_ERROR;
780 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
781 goto f_err;
782 }
783
784 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
785 al = SSL_AD_ILLEGAL_PARAMETER;
786 goto f_err;
787 }
788
David Benjamin17cf2cb2016-12-13 01:07:13 -0500789 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjaminaa7734b2016-06-07 16:40:46 -0400790 ssl->d1->cookie_len = CBS_len(&cookie);
791
792 ssl->d1->send_cookie = 1;
793 return 1;
794
795f_err:
796 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
797 return -1;
798}
799
David Benjaminc3c88822016-11-14 10:32:04 +0900800static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
801 SSL *const ssl = hs->ssl;
David Benjamin0d56f882015-12-19 17:05:56 -0500802 CERT *ct = ssl->cert;
David Benjamin09eb6552016-07-08 14:32:11 -0700803 int al = SSL_AD_INTERNAL_ERROR;
Adam Langley1bea1732014-12-17 19:06:57 -0800804 CBS server_hello, server_random, session_id;
David Benjamin2dc02042016-09-19 19:57:37 -0400805 uint16_t server_wire_version, cipher_suite;
Adam Langley1bea1732014-12-17 19:06:57 -0800806 uint8_t compression_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Steven Valdez143e8b32016-07-11 13:19:03 -0400808 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
David Benjamin09eb6552016-07-08 14:32:11 -0700809 if (ret <= 0) {
David Benjamin780d6dd2015-01-06 12:03:19 -0500810 uint32_t err = ERR_peek_error();
811 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
812 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
813 /* Add a dedicated error code to the queue for a handshake_failure alert
814 * in response to ClientHello. This matches NSS's client behavior and
815 * gives a better error on a (probable) failure to negotiate initial
816 * parameters. Note: this error code comes after the original one.
817 *
818 * See https://crbug.com/446505. */
David Benjamin3570d732015-06-29 00:28:17 -0400819 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
David Benjamin780d6dd2015-01-06 12:03:19 -0500820 }
David Benjamin09eb6552016-07-08 14:32:11 -0700821 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -0800822 }
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Steven Valdez143e8b32016-07-11 13:19:03 -0400824 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
825 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
826 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
827 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
828 return -1;
829 }
830
David Benjamin09eb6552016-07-08 14:32:11 -0700831 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
David Benjamina03d95d2014-07-12 19:49:07 -0400832
Steven Valdez143e8b32016-07-11 13:19:03 -0400833 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800834 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400835 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800836 goto f_err;
837 }
David Benjamina03d95d2014-07-12 19:49:07 -0400838
David Benjamin2dc02042016-09-19 19:57:37 -0400839 uint16_t min_version, max_version, server_version;
David Benjamine0ff7672016-09-19 18:40:03 -0400840 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
David Benjamin2dc02042016-09-19 19:57:37 -0400841 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjamin1f61f0d2016-07-10 12:20:35 -0400842 server_version < min_version || server_version > max_version) {
843 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
844 al = SSL_AD_PROTOCOL_VERSION;
845 goto f_err;
846 }
847
David Benjamin0d56f882015-12-19 17:05:56 -0500848 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
849 if (!ssl->s3->have_version) {
David Benjaminb6a0a512016-06-21 10:33:21 -0400850 ssl->version = server_wire_version;
David Benjaminb83003e2015-12-30 17:39:26 -0500851 ssl->s3->enc_method = ssl3_get_enc_method(server_version);
852 assert(ssl->s3->enc_method != NULL);
David Benjamin0d56f882015-12-19 17:05:56 -0500853 /* At this point, the connection's version is known and ssl->version is
Adam Langley1bea1732014-12-17 19:06:57 -0800854 * fixed. Begin enforcing the record-layer version. */
David Benjamin0d56f882015-12-19 17:05:56 -0500855 ssl->s3->have_version = 1;
David Benjaminb6a0a512016-06-21 10:33:21 -0400856 } else if (server_wire_version != ssl->version) {
David Benjamin3570d732015-06-29 00:28:17 -0400857 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
Adam Langley1bea1732014-12-17 19:06:57 -0800858 al = SSL_AD_PROTOCOL_VERSION;
859 goto f_err;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Steven Valdez143e8b32016-07-11 13:19:03 -0400862 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
David Benjamincb0c29f2016-12-12 17:00:50 -0500863 hs->state = SSL_ST_TLS13;
David Benjaminc3c88822016-11-14 10:32:04 +0900864 hs->do_tls13_handshake = tls13_client_handshake;
Steven Valdez143e8b32016-07-11 13:19:03 -0400865 return 1;
866 }
867
David Benjamin6e4fc332016-11-17 16:43:08 +0900868 ssl_clear_tls13_state(hs);
David Benjamin4fe3c902016-08-16 02:17:03 -0400869
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 Benjamin17cf2cb2016-12-13 01:07:13 -0500887 OPENSSL_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 Benjaminf3c8f8d2016-11-17 17:20:47 +0900901 if (!ssl_get_new_session(hs, 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);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500906 OPENSSL_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 Benjamind2cb1c12016-11-02 17:49:09 -0400910 const SSL_CIPHER *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 }
David Benjamind2cb1c12016-11-02 17:49:09 -0400917
918 /* The cipher must be allowed in the selected version and enabled. */
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) ||
David Benjamind2cb1c12016-11-02 17:49:09 -0400921 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
922 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
Adam Langley1bea1732014-12-17 19:06:57 -0800923 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400924 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langley1bea1732014-12-17 19:06:57 -0800925 goto f_err;
926 }
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Steven Valdez87eab492016-06-27 16:34:59 -0400928 if (ssl->session != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500929 if (ssl->session->ssl_version != ssl->version) {
David Benjaminece3de92015-03-16 18:02:20 -0400930 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400931 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
David Benjaminece3de92015-03-16 18:02:20 -0400932 goto f_err;
933 }
Steven Valdez803c77a2016-09-06 14:13:43 -0400934 if (ssl->session->cipher != c) {
935 al = SSL_AD_ILLEGAL_PARAMETER;
936 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
937 goto f_err;
938 }
Steven Valdez4aa154e2016-07-29 14:32:55 -0400939 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
940 /* This is actually a client application bug. */
941 al = SSL_AD_ILLEGAL_PARAMETER;
942 OPENSSL_PUT_ERROR(SSL,
943 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
944 goto f_err;
945 }
David Benjamin0623bce2015-12-25 15:40:14 -0500946 } else {
Steven Valdez87eab492016-06-27 16:34:59 -0400947 ssl->s3->new_session->cipher = c;
Adam Langley1bea1732014-12-17 19:06:57 -0800948 }
David Benjamin0d56f882015-12-19 17:05:56 -0500949 ssl->s3->tmp.new_cipher = c;
David Benjaminbdf5e722014-11-11 00:52:15 -0500950
David Benjamin9550c3a2015-08-05 08:50:34 -0400951 /* Now that the cipher is known, initialize the handshake hash. */
David Benjamin0d56f882015-12-19 17:05:56 -0500952 if (!ssl3_init_handshake_hash(ssl)) {
David Benjamin9550c3a2015-08-05 08:50:34 -0400953 goto f_err;
954 }
955
Steven Valdez2b8415e2016-06-30 13:27:23 -0400956 /* If doing a full handshake, the server may request a client certificate
957 * which requires hashing the handshake transcript. Otherwise, the handshake
958 * buffer may be released. */
David Benjamin3d458dc2016-09-12 22:40:27 +0000959 if (ssl->session != NULL ||
960 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500961 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -0800962 }
David Benjamina03d95d2014-07-12 19:49:07 -0400963
Adam Langley1bea1732014-12-17 19:06:57 -0800964 /* Only the NULL compression algorithm is supported. */
965 if (compression_method != 0) {
966 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400967 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langley1bea1732014-12-17 19:06:57 -0800968 goto f_err;
969 }
Adam Langley95c29f32014-06-20 12:00:00 -0700970
Adam Langley1bea1732014-12-17 19:06:57 -0800971 /* TLS extensions */
David Benjamin8c880a22016-12-03 02:20:34 -0500972 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
David Benjamin3570d732015-06-29 00:28:17 -0400973 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langley1bea1732014-12-17 19:06:57 -0800974 goto err;
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langley1bea1732014-12-17 19:06:57 -0800977 /* There should be nothing left over in the record. */
978 if (CBS_len(&server_hello) != 0) {
979 /* wrong packet length */
980 al = SSL_AD_DECODE_ERROR;
David Benjamin639846e2016-09-09 11:41:18 -0400981 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -0800982 goto f_err;
983 }
Adam Langley95c29f32014-06-20 12:00:00 -0700984
Steven Valdez87eab492016-06-27 16:34:59 -0400985 if (ssl->session != NULL &&
David Benjamin0d56f882015-12-19 17:05:56 -0500986 ssl->s3->tmp.extended_master_secret !=
987 ssl->session->extended_master_secret) {
Adam Langleyba5934b2015-06-02 10:50:35 -0700988 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin0d56f882015-12-19 17:05:56 -0500989 if (ssl->session->extended_master_secret) {
David Benjamin3570d732015-06-29 00:28:17 -0400990 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700991 } else {
David Benjamin3570d732015-06-29 00:28:17 -0400992 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyba5934b2015-06-02 10:50:35 -0700993 }
994 goto f_err;
995 }
996
Adam Langley1bea1732014-12-17 19:06:57 -0800997 return 1;
998
Adam Langley95c29f32014-06-20 12:00:00 -0700999f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001000 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001001err:
Adam Langley1bea1732014-12-17 19:06:57 -08001002 return -1;
1003}
Adam Langley95c29f32014-06-20 12:00:00 -07001004
David Benjaminc3c88822016-11-14 10:32:04 +09001005static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1006 SSL *const ssl = hs->ssl;
David Benjamin5c900c82016-07-13 23:03:26 -04001007 int ret =
David Benjamin09eb6552016-07-08 14:32:11 -07001008 ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
David Benjamin5c900c82016-07-13 23:03:26 -04001009 if (ret <= 0) {
1010 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001011 }
Adam Langley95c29f32014-06-20 12:00:00 -07001012
David Benjamin5c900c82016-07-13 23:03:26 -04001013 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001014 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley68e71242016-12-12 11:06:16 -08001015
David Benjamin5c900c82016-07-13 23:03:26 -04001016 uint8_t alert;
Adam Langley68e71242016-12-12 11:06:16 -08001017 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
Adam Langleyd5157222016-12-12 11:37:43 -08001018 EVP_PKEY_free(hs->peer_pubkey);
1019 hs->peer_pubkey = NULL;
1020 ssl->s3->new_session->certs = ssl_parse_cert_chain(
1021 &alert, &hs->peer_pubkey, NULL, &cbs, ssl->ctx->pool);
Adam Langley68e71242016-12-12 11:06:16 -08001022 if (ssl->s3->new_session->certs == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -04001023 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Adam Langley68e71242016-12-12 11:06:16 -08001024 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001025 }
Adam Langley95c29f32014-06-20 12:00:00 -07001026
Adam Langley68e71242016-12-12 11:06:16 -08001027 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0 ||
1028 CBS_len(&cbs) != 0 ||
1029 !ssl_session_x509_cache_objects(ssl->s3->new_session)) {
David Benjamin3570d732015-06-29 00:28:17 -04001030 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin5c900c82016-07-13 23:03:26 -04001031 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Adam Langley68e71242016-12-12 11:06:16 -08001032 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001033 }
Alex Chernyakhovskyc6318e32014-07-04 22:52:07 -04001034
Adam Langley05672202016-12-13 12:05:49 -08001035 if (!ssl_check_leaf_certificate(
1036 ssl, hs->peer_pubkey,
1037 sk_CRYPTO_BUFFER_value(ssl->s3->new_session->certs, 0))) {
David Benjamin5c900c82016-07-13 23:03:26 -04001038 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Adam Langley68e71242016-12-12 11:06:16 -08001039 return -1;
David Benjamin436bf822015-06-07 19:50:37 -04001040 }
1041
David Benjamin5c900c82016-07-13 23:03:26 -04001042 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001043}
1044
David Benjaminc3c88822016-11-14 10:32:04 +09001045static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1046 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001047 int al;
David Benjamin9f1dc822016-06-07 17:03:46 -04001048 CBS certificate_status, ocsp_response;
1049 uint8_t status_type;
1050
David Benjamin09eb6552016-07-08 14:32:11 -07001051 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1052 if (ret <= 0) {
1053 return ret;
David Benjamin9f1dc822016-06-07 17:03:46 -04001054 }
1055
1056 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1057 /* A server may send status_request in ServerHello and then change
1058 * its mind about sending CertificateStatus. */
1059 ssl->s3->tmp.reuse_message = 1;
1060 return 1;
1061 }
1062
David Benjamin09eb6552016-07-08 14:32:11 -07001063 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin9f1dc822016-06-07 17:03:46 -04001064 if (!CBS_get_u8(&certificate_status, &status_type) ||
1065 status_type != TLSEXT_STATUSTYPE_ocsp ||
1066 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1067 CBS_len(&ocsp_response) == 0 ||
1068 CBS_len(&certificate_status) != 0) {
1069 al = SSL_AD_DECODE_ERROR;
1070 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1071 goto f_err;
1072 }
1073
Steven Valdez87eab492016-06-27 16:34:59 -04001074 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1075 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001076 al = SSL_AD_INTERNAL_ERROR;
1077 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1078 goto f_err;
1079 }
1080 return 1;
1081
1082f_err:
1083 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1084 return -1;
1085}
1086
David Benjaminc3c88822016-11-14 10:32:04 +09001087static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1088 SSL *const ssl = hs->ssl;
David Benjamin7aa31d62016-08-08 21:38:32 -04001089 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001090 ssl->s3->new_session->x509_chain)) {
David Benjamine455e512016-08-01 20:11:13 -04001091 return -1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001092 }
1093
Adam Langley9498e742016-07-18 10:17:16 -07001094 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001095}
1096
David Benjaminc3c88822016-11-14 10:32:04 +09001097static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1098 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001099 int al;
Adam Langley1bea1732014-12-17 19:06:57 -08001100 DH *dh = NULL;
1101 EC_KEY *ecdh = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001102 EC_POINT *srvr_ecpoint = NULL;
Adam Langley1bea1732014-12-17 19:06:57 -08001103
David Benjamin09eb6552016-07-08 14:32:11 -07001104 int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1105 if (ret <= 0) {
1106 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001107 }
1108
David Benjamin0d56f882015-12-19 17:05:56 -05001109 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjaminbac75b82016-09-16 19:34:02 -04001110 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
David Benjamin0d56f882015-12-19 17:05:56 -05001111 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
David Benjamin3570d732015-06-29 00:28:17 -04001112 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamin0d56f882015-12-19 17:05:56 -05001113 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001114 return -1;
1115 }
1116
David Benjamin0d56f882015-12-19 17:05:56 -05001117 ssl->s3->tmp.reuse_message = 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001118 return 1;
1119 }
1120
1121 /* Retain a copy of the original CBS to compute the signature over. */
David Benjaminb35d6842015-12-29 17:08:08 -05001122 CBS server_key_exchange;
David Benjamin09eb6552016-07-08 14:32:11 -07001123 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjaminb35d6842015-12-29 17:08:08 -05001124 CBS server_key_exchange_orig = server_key_exchange;
Adam Langley1bea1732014-12-17 19:06:57 -08001125
David Benjaminb35d6842015-12-29 17:08:08 -05001126 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1127 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1bea1732014-12-17 19:06:57 -08001128
1129 if (alg_a & SSL_aPSK) {
1130 CBS psk_identity_hint;
1131
1132 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1133 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1134 &psk_identity_hint)) {
1135 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001136 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001137 goto f_err;
1138 }
1139
1140 /* Store PSK identity hint for later use, hint is used in
1141 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1142 * identity hint can be as long as the maximum length of a PSK identity.
1143 * Also do not allow NULL characters; identities are saved as C strings.
1144 *
1145 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1146 * a specific identity. */
1147 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1148 CBS_contains_zero_byte(&psk_identity_hint)) {
1149 al = SSL_AD_HANDSHAKE_FAILURE;
David Benjamin3570d732015-06-29 00:28:17 -04001150 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langley1bea1732014-12-17 19:06:57 -08001151 goto f_err;
1152 }
1153
David Benjamin78679342016-09-16 19:42:05 -04001154 /* Save non-empty identity hints as a C string. Empty identity hints we
1155 * treat as missing. Plain PSK makes it possible to send either no hint
1156 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1157 * empty hint. Having different capabilities is odd, so we interpret empty
1158 * and missing as identical. */
1159 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjaminc3c88822016-11-14 10:32:04 +09001160 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001161 al = SSL_AD_INTERNAL_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001162 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001163 goto f_err;
1164 }
1165 }
1166
David Benjamin7061e282015-03-19 11:10:48 -04001167 if (alg_k & SSL_kDHE) {
Adam Langley1bea1732014-12-17 19:06:57 -08001168 CBS dh_p, dh_g, dh_Ys;
Adam Langley1bea1732014-12-17 19:06:57 -08001169 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1170 CBS_len(&dh_p) == 0 ||
1171 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1172 CBS_len(&dh_g) == 0 ||
1173 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1174 CBS_len(&dh_Ys) == 0) {
1175 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001176 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001177 goto f_err;
1178 }
1179
1180 dh = DH_new();
1181 if (dh == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001182 goto err;
1183 }
1184
David Benjamin48cce662015-12-17 01:33:08 -05001185 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1186 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
David Benjamin974c7ba2015-12-19 16:58:04 -05001187 if (dh->p == NULL || dh->g == NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001188 goto err;
1189 }
1190
David Benjamin4882a6c2016-12-11 02:48:12 -05001191 unsigned bits = DH_num_bits(dh);
1192 if (bits < 1024) {
David Benjamin3570d732015-06-29 00:28:17 -04001193 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langley1bea1732014-12-17 19:06:57 -08001194 goto err;
David Benjamin4882a6c2016-12-11 02:48:12 -05001195 } else if (bits > 4096) {
David Benjamincd24a392015-11-11 13:23:05 -08001196 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1197 * to prevent a server from causing us to perform too expensive of a
1198 * computation. */
1199 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
1200 goto err;
1201 }
David Benjamin974c7ba2015-12-19 16:58:04 -05001202
David Benjaminc3c88822016-11-14 10:32:04 +09001203 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001204 dh = NULL;
David Benjamin974c7ba2015-12-19 16:58:04 -05001205
1206 /* Save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001207 if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
David Benjamin974c7ba2015-12-19 16:58:04 -05001208 goto err;
1209 }
David Benjamin7061e282015-03-19 11:10:48 -04001210 } else if (alg_k & SSL_kECDHE) {
David Benjamin4298d772015-12-19 00:18:25 -05001211 /* Parse the server parameters. */
Steven Valdezce902a92016-05-17 11:47:53 -04001212 uint8_t group_type;
1213 uint16_t group_id;
David Benjamin48cce662015-12-17 01:33:08 -05001214 CBS point;
Steven Valdezce902a92016-05-17 11:47:53 -04001215 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1216 group_type != NAMED_CURVE_TYPE ||
Matt Braithwaite053931e2016-05-25 12:06:05 -07001217 !CBS_get_u16(&server_key_exchange, &group_id) ||
1218 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001219 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001220 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001221 goto f_err;
1222 }
David Benjamin4882a6c2016-12-11 02:48:12 -05001223 ssl->s3->new_session->group_id = group_id;
Adam Langley1bea1732014-12-17 19:06:57 -08001224
Steven Valdezce902a92016-05-17 11:47:53 -04001225 /* Ensure the group is consistent with preferences. */
1226 if (!tls1_check_group_id(ssl, group_id)) {
David Benjamin4298d772015-12-19 00:18:25 -05001227 al = SSL_AD_ILLEGAL_PARAMETER;
1228 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langley1bea1732014-12-17 19:06:57 -08001229 goto f_err;
1230 }
David Benjamin4298d772015-12-19 00:18:25 -05001231
Matt Braithwaite053931e2016-05-25 12:06:05 -07001232 /* Initialize ECDH and save the peer public key for later. */
David Benjaminc3c88822016-11-14 10:32:04 +09001233 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1234 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
David Benjaminc1cc8582015-12-17 01:22:15 -05001235 goto err;
1236 }
Adam Langley1bea1732014-12-17 19:06:57 -08001237 } else if (!(alg_k & SSL_kPSK)) {
1238 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -04001239 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001240 goto f_err;
1241 }
1242
1243 /* At this point, |server_key_exchange| contains the signature, if any, while
1244 * |server_key_exchange_orig| contains the entire message. From that, derive
1245 * a CBS containing just the parameter. */
David Benjaminb35d6842015-12-29 17:08:08 -05001246 CBS parameter;
Adam Langley1bea1732014-12-17 19:06:57 -08001247 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1248 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1249
David Benjaminf4958e72015-06-07 11:00:53 -04001250 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamin3d458dc2016-09-12 22:40:27 +00001251 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001252 uint16_t signature_algorithm = 0;
David Benjamina1e9cab2015-12-30 00:08:49 -05001253 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdez02563852016-06-23 13:33:05 -04001254 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001255 al = SSL_AD_DECODE_ERROR;
1256 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001257 goto f_err;
1258 }
David Benjamin887c3002016-07-08 16:15:32 -07001259 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
David Benjamin6e807652015-11-02 12:02:20 -05001260 goto f_err;
1261 }
David Benjaminf1050fd2016-12-13 20:05:36 -05001262 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
Adam Langleyd5157222016-12-12 11:37:43 -08001263 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001264 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Adam Langleyd5157222016-12-12 11:37:43 -08001265 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001266 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
David Benjamin49ec9bb2016-07-14 00:11:26 -04001267 } else {
1268 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1269 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1270 goto f_err;
Adam Langley1bea1732014-12-17 19:06:57 -08001271 }
1272
1273 /* The last field in |server_key_exchange| is the signature. */
David Benjaminf4958e72015-06-07 11:00:53 -04001274 CBS signature;
Adam Langley1bea1732014-12-17 19:06:57 -08001275 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1276 CBS_len(&server_key_exchange) != 0) {
1277 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001278 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001279 goto f_err;
1280 }
1281
Steven Valdez2b8415e2016-06-30 13:27:23 -04001282 CBB transcript;
1283 uint8_t *transcript_data;
1284 size_t transcript_len;
1285 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1286 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1287 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1288 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1289 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1290 CBB_cleanup(&transcript);
1291 al = SSL_AD_INTERNAL_ERROR;
1292 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdezf0451ca2016-06-29 13:16:27 -04001293 goto f_err;
1294 }
Steven Valdez2b8415e2016-06-30 13:27:23 -04001295
1296 int sig_ok = ssl_public_key_verify(
1297 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Adam Langleyd5157222016-12-12 11:37:43 -08001298 hs->peer_pubkey, transcript_data, transcript_len);
Steven Valdez2b8415e2016-06-30 13:27:23 -04001299 OPENSSL_free(transcript_data);
1300
David Benjaminbf82aed2016-03-01 22:57:40 -05001301#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1302 sig_ok = 1;
1303 ERR_clear_error();
1304#endif
1305 if (!sig_ok) {
Adam Langley1bea1732014-12-17 19:06:57 -08001306 /* bad signature */
1307 al = SSL_AD_DECRYPT_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001308 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langley1bea1732014-12-17 19:06:57 -08001309 goto f_err;
1310 }
1311 } else {
David Benjaminf4958e72015-06-07 11:00:53 -04001312 /* PSK ciphers are the only supported certificate-less ciphers. */
1313 assert(alg_a == SSL_aPSK);
1314
Adam Langley1bea1732014-12-17 19:06:57 -08001315 if (CBS_len(&server_key_exchange) > 0) {
1316 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -04001317 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langley1bea1732014-12-17 19:06:57 -08001318 goto f_err;
1319 }
1320 }
Adam Langley1bea1732014-12-17 19:06:57 -08001321 return 1;
1322
Adam Langley95c29f32014-06-20 12:00:00 -07001323f_err:
David Benjamin0d56f882015-12-19 17:05:56 -05001324 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001325err:
David Benjamin2755a3e2015-04-22 16:17:58 -04001326 DH_free(dh);
Adam Langley1bea1732014-12-17 19:06:57 -08001327 EC_POINT_free(srvr_ecpoint);
David Benjamin2755a3e2015-04-22 16:17:58 -04001328 EC_KEY_free(ecdh);
Adam Langley1bea1732014-12-17 19:06:57 -08001329 return -1;
1330}
Adam Langley95c29f32014-06-20 12:00:00 -07001331
David Benjaminc3c88822016-11-14 10:32:04 +09001332static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1333 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001334 int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
1335 if (msg_ret <= 0) {
1336 return msg_ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001337 }
David Benjamin688d8df2014-11-02 23:06:42 -05001338
David Benjamin3dcec452016-06-07 17:13:28 -04001339 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
David Benjamin0d56f882015-12-19 17:05:56 -05001340 ssl->s3->tmp.reuse_message = 1;
David Benjamin9550c3a2015-08-05 08:50:34 -04001341 /* If we get here we don't need the handshake buffer as we won't be doing
1342 * client auth. */
David Benjamin0d56f882015-12-19 17:05:56 -05001343 ssl3_free_handshake_buffer(ssl);
Adam Langley1bea1732014-12-17 19:06:57 -08001344 return 1;
1345 }
David Benjamined439582014-07-14 19:13:02 -04001346
David Benjamin0d56f882015-12-19 17:05:56 -05001347 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1348 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamindf28c3a2016-03-10 16:11:51 -05001349 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
David Benjamine0332e82016-07-13 22:40:36 -04001350 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001351 }
Adam Langley95c29f32014-06-20 12:00:00 -07001352
David Benjamina5177cb2016-03-11 19:38:17 -05001353 CBS cbs;
David Benjamin09eb6552016-07-08 14:32:11 -07001354 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langley95c29f32014-06-20 12:00:00 -07001355
David Benjamine0332e82016-07-13 22:40:36 -04001356 /* Get the certificate types. */
David Benjamina5177cb2016-03-11 19:38:17 -05001357 CBS certificate_types;
Adam Langley1bea1732014-12-17 19:06:57 -08001358 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001359 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001360 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001361 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001362 }
David Benjamined439582014-07-14 19:13:02 -04001363
David Benjaminc3c88822016-11-14 10:32:04 +09001364 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1365 &hs->num_certificate_types)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001366 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001367 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001368 }
Adam Langleyc26c8022014-06-20 12:00:00 -07001369
David Benjamina1e9cab2015-12-30 00:08:49 -05001370 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langley1bea1732014-12-17 19:06:57 -08001371 CBS supported_signature_algorithms;
Steven Valdez0d62f262015-09-04 12:41:04 -04001372 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001373 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
David Benjamin0d56f882015-12-19 17:05:56 -05001374 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin3570d732015-06-29 00:28:17 -04001375 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine0332e82016-07-13 22:40:36 -04001376 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001377 }
Adam Langley1bea1732014-12-17 19:06:57 -08001378 }
David Benjamined439582014-07-14 19:13:02 -04001379
David Benjamine0332e82016-07-13 22:40:36 -04001380 uint8_t alert;
1381 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
1382 if (ca_sk == NULL) {
1383 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1384 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001385 }
Adam Langley95c29f32014-06-20 12:00:00 -07001386
David Benjamin639846e2016-09-09 11:41:18 -04001387 if (CBS_len(&cbs) != 0) {
1388 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1389 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1390 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1391 return -1;
1392 }
1393
David Benjaminc3c88822016-11-14 10:32:04 +09001394 hs->cert_request = 1;
1395 sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
1396 hs->ca_names = ca_sk;
David Benjamine0332e82016-07-13 22:40:36 -04001397 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001398}
Adam Langley95c29f32014-06-20 12:00:00 -07001399
David Benjaminc3c88822016-11-14 10:32:04 +09001400static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1401 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001402 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
1403 ssl_hash_message);
1404 if (ret <= 0) {
1405 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001406 }
David Benjamin46062682014-07-14 19:14:32 -04001407
David Benjamin09eb6552016-07-08 14:32:11 -07001408 /* ServerHelloDone is empty. */
1409 if (ssl->init_num > 0) {
David Benjamin0d56f882015-12-19 17:05:56 -05001410 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin639846e2016-09-09 11:41:18 -04001411 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley1bea1732014-12-17 19:06:57 -08001412 return -1;
1413 }
David Benjamin46062682014-07-14 19:14:32 -04001414
Adam Langley1bea1732014-12-17 19:06:57 -08001415 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001416}
Adam Langley1258b6a2014-06-20 12:00:00 -07001417
David Benjaminc3c88822016-11-14 10:32:04 +09001418static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1419 SSL *const ssl = hs->ssl;
David Benjamin5edfc8c2016-12-10 15:46:58 -05001420 /* Call cert_cb to update the certificate. */
1421 if (ssl->cert->cert_cb) {
1422 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1423 if (ret < 0) {
1424 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin9f1dc822016-06-07 17:03:46 -04001425 return -1;
1426 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001427 if (ret == 0) {
1428 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1429 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001430 return -1;
1431 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001432 }
1433
David Benjamin5edfc8c2016-12-10 15:46:58 -05001434 if (!ssl_has_certificate(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001435 /* Without a client certificate, the handshake buffer may be released. */
1436 ssl3_free_handshake_buffer(ssl);
1437
David Benjamindaf207a2017-01-03 18:37:41 -05001438 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
David Benjamin5edfc8c2016-12-10 15:46:58 -05001439 if (ssl->version == SSL3_VERSION) {
David Benjamindaf207a2017-01-03 18:37:41 -05001440 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1441 SSL_AD_NO_CERTIFICATE)) {
1442 return -1;
1443 }
David Benjamin5edfc8c2016-12-10 15:46:58 -05001444 return 1;
1445 }
1446 }
1447
David Benjamin650aa1c2016-12-20 18:55:16 -05001448 if (!ssl_auto_chain_if_needed(ssl) ||
1449 !ssl3_output_cert_chain(ssl)) {
David Benjamin5edfc8c2016-12-10 15:46:58 -05001450 return -1;
1451 }
David Benjamin16315f72017-01-12 20:02:05 -05001452 return 1;
David Benjamin9f1dc822016-06-07 17:03:46 -04001453}
1454
Brian Smith274341d2015-10-08 17:10:15 -10001455OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1456 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langley1258b6a2014-06-20 12:00:00 -07001457
David Benjaminc3c88822016-11-14 10:32:04 +09001458static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1459 SSL *const ssl = hs->ssl;
Adam Langley1bea1732014-12-17 19:06:57 -08001460 uint8_t *pms = NULL;
1461 size_t pms_len = 0;
David Benjamin75836432016-06-17 18:48:29 -04001462 CBB cbb, body;
1463 if (!ssl->method->init_message(ssl, &cbb, &body,
1464 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001465 goto err;
1466 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001467
David Benjaminae0eaaa2015-12-17 02:01:25 -05001468 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1469 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley1258b6a2014-06-20 12:00:00 -07001470
David Benjaminae0eaaa2015-12-17 02:01:25 -05001471 /* If using a PSK key exchange, prepare the pre-shared key. */
1472 unsigned psk_len = 0;
1473 uint8_t psk[PSK_MAX_PSK_LEN];
1474 if (alg_a & SSL_aPSK) {
1475 if (ssl->psk_client_callback == NULL) {
1476 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1477 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001478 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001479
David Benjaminae0eaaa2015-12-17 02:01:25 -05001480 char identity[PSK_MAX_IDENTITY_LEN + 1];
David Benjamin17cf2cb2016-12-13 01:07:13 -05001481 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjaminc3c88822016-11-14 10:32:04 +09001482 psk_len =
1483 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1484 sizeof(identity), psk, sizeof(psk));
David Benjaminae0eaaa2015-12-17 02:01:25 -05001485 if (psk_len == 0) {
1486 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1487 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1488 goto err;
1489 }
1490 assert(psk_len <= PSK_MAX_PSK_LEN);
Adam Langley1258b6a2014-06-20 12:00:00 -07001491
Steven Valdez87eab492016-06-27 16:34:59 -04001492 OPENSSL_free(ssl->s3->new_session->psk_identity);
1493 ssl->s3->new_session->psk_identity = BUF_strdup(identity);
1494 if (ssl->s3->new_session->psk_identity == NULL) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001495 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1496 goto err;
1497 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001498
David Benjaminae0eaaa2015-12-17 02:01:25 -05001499 /* Write out psk_identity. */
1500 CBB child;
David Benjamin75836432016-06-17 18:48:29 -04001501 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001502 !CBB_add_bytes(&child, (const uint8_t *)identity,
1503 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjamin75836432016-06-17 18:48:29 -04001504 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001505 goto err;
1506 }
1507 }
David Benjamin758d1272015-11-20 17:47:25 -05001508
David Benjaminae0eaaa2015-12-17 02:01:25 -05001509 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1510 if (alg_k & SSL_kRSA) {
1511 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1512 pms = OPENSSL_malloc(pms_len);
1513 if (pms == NULL) {
1514 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1515 goto err;
1516 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001517
Adam Langleyd5157222016-12-12 11:37:43 -08001518 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001519 if (rsa == NULL) {
1520 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001521 goto err;
1522 }
Adam Langley1258b6a2014-06-20 12:00:00 -07001523
David Benjaminf04c2e92016-12-06 13:35:25 -05001524 pms[0] = hs->client_version >> 8;
1525 pms[1] = hs->client_version & 0xff;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001526 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1527 goto err;
1528 }
1529
David Benjamin75836432016-06-17 18:48:29 -04001530 CBB child, *enc_pms = &body;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001531 size_t enc_pms_len;
1532 /* In TLS, there is a length prefix. */
1533 if (ssl->version > SSL3_VERSION) {
David Benjamin75836432016-06-17 18:48:29 -04001534 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley1bea1732014-12-17 19:06:57 -08001535 goto err;
1536 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001537 enc_pms = &child;
1538 }
Adam Langley1bea1732014-12-17 19:06:57 -08001539
David Benjaminae0eaaa2015-12-17 02:01:25 -05001540 uint8_t *ptr;
1541 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1542 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1543 RSA_PKCS1_PADDING) ||
David Benjaminae0eaaa2015-12-17 02:01:25 -05001544 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001545 !CBB_flush(&body)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001546 goto err;
1547 }
Matthew Braithwaite651aaef2016-12-08 16:14:36 -08001548 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
Matt Braithwaitee25775b2016-05-16 16:31:05 -07001549 /* Generate a keypair and serialize the public half. */
David Benjaminae0eaaa2015-12-17 02:01:25 -05001550 CBB child;
David Benjaminc3c88822016-11-14 10:32:04 +09001551 if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001552 goto err;
1553 }
1554
David Benjamin4298d772015-12-19 00:18:25 -05001555 /* Compute the premaster. */
1556 uint8_t alert;
David Benjaminc3c88822016-11-14 10:32:04 +09001557 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1558 hs->peer_key, hs->peer_key_len)) {
David Benjamin4298d772015-12-19 00:18:25 -05001559 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1560 goto err;
1561 }
David Benjamin75836432016-06-17 18:48:29 -04001562 if (!CBB_flush(&body)) {
Matt Braithwaitef4ce8e52016-05-16 14:27:14 -07001563 goto err;
1564 }
David Benjamin4298d772015-12-19 00:18:25 -05001565
1566 /* The key exchange state may now be discarded. */
David Benjaminc3c88822016-11-14 10:32:04 +09001567 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1568 OPENSSL_free(hs->peer_key);
1569 hs->peer_key = NULL;
1570 hs->peer_key_len = 0;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001571 } else if (alg_k & SSL_kPSK) {
1572 /* For plain PSK, other_secret is a block of 0s with the same length as
1573 * the pre-shared key. */
1574 pms_len = psk_len;
1575 pms = OPENSSL_malloc(pms_len);
1576 if (pms == NULL) {
1577 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1578 goto err;
1579 }
David Benjamin17cf2cb2016-12-13 01:07:13 -05001580 OPENSSL_memset(pms, 0, pms_len);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001581 } else {
1582 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1583 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1584 goto err;
Adam Langley1bea1732014-12-17 19:06:57 -08001585 }
1586
David Benjaminae0eaaa2015-12-17 02:01:25 -05001587 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1588 * key. */
1589 if (alg_a & SSL_aPSK) {
1590 CBB pms_cbb, child;
1591 uint8_t *new_pms;
1592 size_t new_pms_len;
1593
1594 CBB_zero(&pms_cbb);
1595 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1596 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1597 !CBB_add_bytes(&child, pms, pms_len) ||
1598 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1599 !CBB_add_bytes(&child, psk, psk_len) ||
1600 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1601 CBB_cleanup(&pms_cbb);
1602 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1603 goto err;
1604 }
1605 OPENSSL_cleanse(pms, pms_len);
1606 OPENSSL_free(pms);
1607 pms = new_pms;
1608 pms_len = new_pms_len;
1609 }
1610
1611 /* The message must be added to the finished hash before calculating the
1612 * master secret. */
David Benjamindaf207a2017-01-03 18:37:41 -05001613 if (!ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001614 goto err;
1615 }
David Benjaminae0eaaa2015-12-17 02:01:25 -05001616
Steven Valdez87eab492016-06-27 16:34:59 -04001617 ssl->s3->new_session->master_key_length =
1618 tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
1619 pms_len);
1620 if (ssl->s3->new_session->master_key_length == 0) {
David Benjaminae0eaaa2015-12-17 02:01:25 -05001621 goto err;
1622 }
Steven Valdez87eab492016-06-27 16:34:59 -04001623 ssl->s3->new_session->extended_master_secret =
1624 ssl->s3->tmp.extended_master_secret;
David Benjaminae0eaaa2015-12-17 02:01:25 -05001625 OPENSSL_cleanse(pms, pms_len);
1626 OPENSSL_free(pms);
1627
David Benjamin16315f72017-01-12 20:02:05 -05001628 return 1;
Adam Langley1258b6a2014-06-20 12:00:00 -07001629
1630err:
David Benjamin2b07fa42016-03-02 00:23:57 -05001631 CBB_cleanup(&cbb);
David Benjaminae0eaaa2015-12-17 02:01:25 -05001632 if (pms != NULL) {
Adam Langley1bea1732014-12-17 19:06:57 -08001633 OPENSSL_cleanse(pms, pms_len);
1634 OPENSSL_free(pms);
1635 }
1636 return -1;
1637}
Adam Langley1258b6a2014-06-20 12:00:00 -07001638
David Benjaminc3c88822016-11-14 10:32:04 +09001639static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1640 SSL *const ssl = hs->ssl;
David Benjamin429fdc02016-06-17 17:33:30 -04001641 assert(ssl_has_private_key(ssl));
1642
David Benjamin75836432016-06-17 18:48:29 -04001643 CBB cbb, body, child;
1644 if (!ssl->method->init_message(ssl, &cbb, &body,
1645 SSL3_MT_CERTIFICATE_VERIFY)) {
David Benjaminfc825122015-12-18 01:57:43 -05001646 goto err;
1647 }
1648
David Benjaminea9a0d52016-07-08 15:52:59 -07001649 uint16_t signature_algorithm;
David Benjaminf3c8f8d2016-11-17 17:20:47 +09001650 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminea9a0d52016-07-08 15:52:59 -07001651 goto err;
1652 }
David Benjamin429fdc02016-06-17 17:33:30 -04001653 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Steven Valdezf0451ca2016-06-29 13:16:27 -04001654 /* Write out the digest type in TLS 1.2. */
1655 if (!CBB_add_u16(&body, signature_algorithm)) {
David Benjamin429fdc02016-06-17 17:33:30 -04001656 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1657 goto err;
1658 }
1659 }
David Benjaminfc825122015-12-18 01:57:43 -05001660
David Benjamin429fdc02016-06-17 17:33:30 -04001661 /* Set aside space for the signature. */
David Benjaminfc825122015-12-18 01:57:43 -05001662 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjamin429fdc02016-06-17 17:33:30 -04001663 uint8_t *ptr;
David Benjamin75836432016-06-17 18:48:29 -04001664 if (!CBB_add_u16_length_prefixed(&body, &child) ||
David Benjamin429fdc02016-06-17 17:33:30 -04001665 !CBB_reserve(&child, &ptr, max_sig_len)) {
1666 goto err;
1667 }
1668
Steven Valdez2b8415e2016-06-30 13:27:23 -04001669 size_t sig_len = max_sig_len;
David Benjaminfc825122015-12-18 01:57:43 -05001670 enum ssl_private_key_result_t sign_result;
David Benjamincb0c29f2016-12-12 17:00:50 -05001671 if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
Steven Valdez2b8415e2016-06-30 13:27:23 -04001672 /* The SSL3 construction for CertificateVerify does not decompose into a
1673 * single final digest and signature, and must be special-cased. */
1674 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1675 if (ssl->cert->key_method != NULL) {
1676 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
1677 goto err;
1678 }
1679
David Benjamin0aa25bd2016-07-08 14:44:56 -07001680 const EVP_MD *md;
Steven Valdez2b8415e2016-06-30 13:27:23 -04001681 uint8_t digest[EVP_MAX_MD_SIZE];
1682 size_t digest_len;
David Benjamin0aa25bd2016-07-08 14:44:56 -07001683 if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
Steven Valdez2b8415e2016-06-30 13:27:23 -04001684 signature_algorithm)) {
1685 goto err;
1686 }
1687
1688 sign_result = ssl_private_key_success;
1689
1690 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1691 if (pctx == NULL ||
1692 !EVP_PKEY_sign_init(pctx) ||
David Benjamin0aa25bd2016-07-08 14:44:56 -07001693 !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
Steven Valdez2b8415e2016-06-30 13:27:23 -04001694 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1695 EVP_PKEY_CTX_free(pctx);
1696 sign_result = ssl_private_key_failure;
1697 goto err;
1698 }
1699 EVP_PKEY_CTX_free(pctx);
1700 } else {
1701 sign_result = ssl_private_key_sign(
1702 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
1703 (const uint8_t *)ssl->s3->handshake_buffer->data,
1704 ssl->s3->handshake_buffer->length);
David Benjaminfc825122015-12-18 01:57:43 -05001705 }
1706
1707 /* The handshake buffer is no longer necessary. */
1708 ssl3_free_handshake_buffer(ssl);
David Benjaminfc825122015-12-18 01:57:43 -05001709 } else {
David Benjamincb0c29f2016-12-12 17:00:50 -05001710 assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjamind3440b42016-07-14 14:52:41 -04001711 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
David Benjaminfc825122015-12-18 01:57:43 -05001712 }
1713
1714 switch (sign_result) {
1715 case ssl_private_key_success:
David Benjaminfc825122015-12-18 01:57:43 -05001716 break;
1717 case ssl_private_key_failure:
David Benjaminfc825122015-12-18 01:57:43 -05001718 goto err;
1719 case ssl_private_key_retry:
1720 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
David Benjamincb0c29f2016-12-12 17:00:50 -05001721 hs->state = SSL3_ST_CW_CERT_VRFY_B;
David Benjaminfc825122015-12-18 01:57:43 -05001722 goto err;
1723 }
1724
David Benjaminfc825122015-12-18 01:57:43 -05001725 if (!CBB_did_write(&child, sig_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001726 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjaminfc825122015-12-18 01:57:43 -05001727 goto err;
1728 }
1729
David Benjamin16315f72017-01-12 20:02:05 -05001730 return 1;
David Benjaminfc825122015-12-18 01:57:43 -05001731
1732err:
1733 CBB_cleanup(&cbb);
1734 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001735}
1736
David Benjaminc3c88822016-11-14 10:32:04 +09001737static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1738 SSL *const ssl = hs->ssl;
David Benjamine97b9622015-10-10 14:59:09 -04001739 static const uint8_t kZero[32] = {0};
David Benjamin79978df2015-12-25 15:56:49 -05001740 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
David Benjamine97b9622015-10-10 14:59:09 -04001741
David Benjamin75836432016-06-17 18:48:29 -04001742 CBB cbb, body, child;
1743 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1744 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin79978df2015-12-25 15:56:49 -05001745 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1746 ssl->s3->next_proto_negotiated_len) ||
David Benjamin75836432016-06-17 18:48:29 -04001747 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamine97b9622015-10-10 14:59:09 -04001748 !CBB_add_bytes(&child, kZero, padding_len) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001749 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamine97b9622015-10-10 14:59:09 -04001750 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1751 CBB_cleanup(&cbb);
1752 return -1;
1753 }
1754
David Benjamin16315f72017-01-12 20:02:05 -05001755 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001756}
1757
David Benjaminc3c88822016-11-14 10:32:04 +09001758static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1759 SSL *const ssl = hs->ssl;
Nick Harper60a85cb2016-09-23 16:25:11 -07001760 if (!ssl_do_channel_id_callback(ssl)) {
1761 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001762 }
1763
David Benjamin96e97b12015-10-10 16:30:22 -04001764 if (ssl->tlsext_channel_id_private == NULL) {
1765 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
Adam Langley1bea1732014-12-17 19:06:57 -08001766 return -1;
1767 }
Adam Langley1bea1732014-12-17 19:06:57 -08001768
Nick Harper60a85cb2016-09-23 16:25:11 -07001769 CBB cbb, body;
David Benjamind94b83b2016-07-06 13:50:14 -07001770 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Nick Harper60a85cb2016-09-23 16:25:11 -07001771 !tls1_write_channel_id(ssl, &body) ||
David Benjamindaf207a2017-01-03 18:37:41 -05001772 !ssl_add_message_cbb(ssl, &cbb)) {
David Benjamin3570d732015-06-29 00:28:17 -04001773 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin96e97b12015-10-10 16:30:22 -04001774 CBB_cleanup(&cbb);
Nick Harper60a85cb2016-09-23 16:25:11 -07001775 return -1;
Adam Langley1bea1732014-12-17 19:06:57 -08001776 }
1777
David Benjamin16315f72017-01-12 20:02:05 -05001778 return 1;
Adam Langley1bea1732014-12-17 19:06:57 -08001779}
1780
David Benjaminc3c88822016-11-14 10:32:04 +09001781static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1782 SSL *const ssl = hs->ssl;
David Benjamin09eb6552016-07-08 14:32:11 -07001783 int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
1784 ssl_hash_message);
1785 if (ret <= 0) {
1786 return ret;
Adam Langley1bea1732014-12-17 19:06:57 -08001787 }
David Benjamin454aa4c2016-03-10 08:43:27 -05001788
David Benjamin9f1dc822016-06-07 17:03:46 -04001789 CBS new_session_ticket, ticket;
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001790 uint32_t tlsext_tick_lifetime_hint;
David Benjamin09eb6552016-07-08 14:32:11 -07001791 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001792 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin9f1dc822016-06-07 17:03:46 -04001793 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1794 CBS_len(&new_session_ticket) != 0) {
Steven Valdez87eab492016-06-27 16:34:59 -04001795 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin9f1dc822016-06-07 17:03:46 -04001796 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Steven Valdez87eab492016-06-27 16:34:59 -04001797 return -1;
David Benjamin454aa4c2016-03-10 08:43:27 -05001798 }
1799
David Benjamin9f1dc822016-06-07 17:03:46 -04001800 if (CBS_len(&ticket) == 0) {
1801 /* RFC 5077 allows a server to change its mind and send no ticket after
David Benjamin78476f62016-11-12 11:20:55 +09001802 * negotiating the extension. The value of |ticket_expected| is checked in
1803 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjaminc3c88822016-11-14 10:32:04 +09001804 hs->ticket_expected = 0;
David Benjamin9f1dc822016-06-07 17:03:46 -04001805 return 1;
1806 }
1807
Steven Valdez87eab492016-06-27 16:34:59 -04001808 int session_renewed = ssl->session != NULL;
1809 SSL_SESSION *session = ssl->s3->new_session;
1810 if (session_renewed) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001811 /* The server is sending a new ticket for an existing session. Sessions are
1812 * immutable once established, so duplicate all but the ticket of the
1813 * existing session. */
Steven Valdez4aa154e2016-07-29 14:32:55 -04001814 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
Steven Valdez87eab492016-06-27 16:34:59 -04001815 if (session == NULL) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001816 /* This should never happen. */
1817 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1818 goto err;
1819 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001820 }
1821
David Benjamin123db572016-11-03 16:59:25 -04001822 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
1823 ssl_session_refresh_time(ssl, session);
1824
Steven Valdez87eab492016-06-27 16:34:59 -04001825 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001826 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1827 goto err;
1828 }
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -07001829 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin9f1dc822016-06-07 17:03:46 -04001830
1831 /* Generate a session ID for this session based on the session ticket. We use
1832 * the session ID mechanism for detecting ticket resumption. This also fits in
1833 * with assumptions elsewhere in OpenSSL.*/
Steven Valdez87eab492016-06-27 16:34:59 -04001834 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1835 session->session_id, &session->session_id_length,
1836 EVP_sha256(), NULL)) {
David Benjamin9f1dc822016-06-07 17:03:46 -04001837 goto err;
1838 }
1839
Steven Valdez87eab492016-06-27 16:34:59 -04001840 if (session_renewed) {
1841 session->not_resumable = 0;
1842 SSL_SESSION_free(ssl->session);
1843 ssl->session = session;
1844 }
1845
David Benjamin454aa4c2016-03-10 08:43:27 -05001846 return 1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001847
David Benjamin9f1dc822016-06-07 17:03:46 -04001848err:
Steven Valdez87eab492016-06-27 16:34:59 -04001849 if (session_renewed) {
1850 SSL_SESSION_free(session);
1851 }
David Benjamin9f1dc822016-06-07 17:03:46 -04001852 return -1;
Paul Lietar8f1c2682015-08-18 12:21:54 +01001853}