blob: 427213c24f816662d08e2b0d4c7718716aa22ef7 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +0000150#include <openssl/ssl.h>
151
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152#include <assert.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700153#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800154
Steven Valdez909b19f2016-11-21 15:35:44 -0500155#include <openssl/aead.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000156#include <openssl/bn.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157#include <openssl/buf.h>
158#include <openssl/bytestring.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000159#include <openssl/dh.h>
160#include <openssl/ec_key.h>
161#include <openssl/ecdsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700162#include <openssl/err.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163#include <openssl/evp.h>
Kenny Roota04d78d2015-09-25 00:26:37 +0000164#include <openssl/md5.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000165#include <openssl/mem.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000166#include <openssl/rand.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167#include <openssl/x509.h>
Kenny Rootb8494592015-09-25 02:29:14 +0000168#include <openssl/x509v3.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800169
Robert Sloan69939df2017-01-09 10:53:07 -0800170#include "../crypto/internal.h"
Adam Langleye9ada862015-05-11 17:20:37 -0700171#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800172
173
David Benjamin1b249672016-12-06 18:25:50 -0500174static 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 Benjamin6e899c72016-06-09 18:02:18 -0400189
David Benjamin1b249672016-12-06 18:25:50 -0500190int ssl3_connect(SSL_HANDSHAKE *hs) {
191 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192 int ret = -1;
David Benjamin6e899c72016-06-09 18:02:18 -0400193 int state, skip = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194
Adam Langley4139edb2016-01-13 15:00:54 -0800195 assert(ssl->handshake_func == ssl3_connect);
196 assert(!ssl->server);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800197
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198 for (;;) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500199 state = hs->state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200
Steven Valdeze7531f02016-12-14 13:29:57 -0500201 switch (hs->state) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400202 case SSL_ST_INIT:
Steven Valdeze7531f02016-12-14 13:29:57 -0500203 hs->state = SSL_ST_CONNECT;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400204 skip = 1;
205 break;
206
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207 case SSL_ST_CONNECT:
David Benjamin6e899c72016-06-09 18:02:18 -0400208 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Steven Valdeze7531f02016-12-14 13:29:57 -0500209 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 break;
211
212 case SSL3_ST_CW_CLNT_HELLO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500213 ret = ssl3_send_client_hello(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800214 if (ret <= 0) {
215 goto end;
216 }
David Benjamin6e899c72016-06-09 18:02:18 -0400217
David Benjaminc895d6b2016-08-11 13:26:41 -0400218 if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500219 hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjamin6e899c72016-06-09 18:02:18 -0400220 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500221 hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
David Benjamin6e899c72016-06-09 18:02:18 -0400222 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500223 hs->state = SSL3_ST_CW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800224 break;
225
David Benjamin6e899c72016-06-09 18:02:18 -0400226 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400227 assert(SSL_is_dtls(ssl));
David Benjamin1b249672016-12-06 18:25:50 -0500228 ret = dtls1_get_hello_verify(hs);
David Benjamin6e899c72016-06-09 18:02:18 -0400229 if (ret <= 0) {
230 goto end;
231 }
232 if (ssl->d1->send_cookie) {
233 ssl->method->received_flight(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -0500234 hs->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjamin6e899c72016-06-09 18:02:18 -0400235 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500236 hs->state = SSL3_ST_CR_SRVR_HELLO_A;
David Benjamin6e899c72016-06-09 18:02:18 -0400237 }
238 break;
239
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500241 ret = ssl3_get_server_hello(hs);
Steven Valdeze7531f02016-12-14 13:29:57 -0500242 if (hs->state == SSL_ST_TLS13) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400243 break;
244 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800245 if (ret <= 0) {
246 goto end;
247 }
248
David Benjaminc895d6b2016-08-11 13:26:41 -0400249 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500250 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500252 hs->state = SSL3_ST_CR_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254 break;
255
256 case SSL3_ST_CR_CERT_A:
David Benjamind316cba2016-06-02 16:17:39 -0400257 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -0500258 ret = ssl3_get_server_certificate(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800259 if (ret <= 0) {
260 goto end;
261 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 } else {
263 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500265 hs->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 break;
267
David Benjamin6e899c72016-06-09 18:02:18 -0400268 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamin1b249672016-12-06 18:25:50 -0500269 if (hs->certificate_status_expected) {
270 ret = ssl3_get_cert_status(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400271 if (ret <= 0) {
272 goto end;
273 }
274 } else {
275 skip = 1;
David Benjamin6e899c72016-06-09 18:02:18 -0400276 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500277 hs->state = SSL3_ST_VERIFY_SERVER_CERT;
David Benjamin6e899c72016-06-09 18:02:18 -0400278 break;
279
Kenny Rootb8494592015-09-25 02:29:14 +0000280 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjaminc895d6b2016-08-11 13:26:41 -0400281 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -0500282 ret = ssl3_verify_server_cert(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400283 if (ret <= 0) {
284 goto end;
285 }
286 } else {
287 skip = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000288 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500289 hs->state = SSL3_ST_CR_KEY_EXCH_A;
Kenny Rootb8494592015-09-25 02:29:14 +0000290 break;
291
Adam Langleyd9e397b2015-01-22 14:27:53 -0800292 case SSL3_ST_CR_KEY_EXCH_A:
David Benjamin1b249672016-12-06 18:25:50 -0500293 ret = ssl3_get_server_key_exchange(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800294 if (ret <= 0) {
295 goto end;
296 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500297 hs->state = SSL3_ST_CR_CERT_REQ_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800298 break;
299
300 case SSL3_ST_CR_CERT_REQ_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400301 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin1b249672016-12-06 18:25:50 -0500302 ret = ssl3_get_certificate_request(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400303 if (ret <= 0) {
304 goto end;
305 }
306 } else {
307 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800308 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500309 hs->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800310 break;
311
312 case SSL3_ST_CR_SRVR_DONE_A:
David Benjamin1b249672016-12-06 18:25:50 -0500313 ret = ssl3_get_server_hello_done(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800314 if (ret <= 0) {
315 goto end;
316 }
David Benjamin6e899c72016-06-09 18:02:18 -0400317 ssl->method->received_flight(ssl);
Steven Valdeze7531f02016-12-14 13:29:57 -0500318 hs->state = SSL3_ST_CW_CERT_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 break;
320
321 case SSL3_ST_CW_CERT_A:
David Benjamin1b249672016-12-06 18:25:50 -0500322 if (hs->cert_request) {
323 ret = ssl3_send_client_certificate(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400324 if (ret <= 0) {
325 goto end;
326 }
327 } else {
328 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500330 hs->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800331 break;
332
333 case SSL3_ST_CW_KEY_EXCH_A:
David Benjamin1b249672016-12-06 18:25:50 -0500334 ret = ssl3_send_client_key_exchange(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800335 if (ret <= 0) {
336 goto end;
337 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500338 hs->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800339 break;
340
341 case SSL3_ST_CW_CERT_VRFY_A:
342 case SSL3_ST_CW_CERT_VRFY_B:
Robert Sloan69939df2017-01-09 10:53:07 -0800343 if (hs->cert_request && ssl_has_certificate(ssl)) {
David Benjamin1b249672016-12-06 18:25:50 -0500344 ret = ssl3_send_cert_verify(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400345 if (ret <= 0) {
346 goto end;
347 }
348 } else {
349 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800350 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500351 hs->state = SSL3_ST_CW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 break;
353
David Benjaminc895d6b2016-08-11 13:26:41 -0400354 case SSL3_ST_CW_CHANGE:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800355 if (!ssl->method->add_change_cipher_spec(ssl) ||
356 !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800357 ret = -1;
358 goto end;
359 }
360
Robert Sloan4d1ac502017-02-06 08:36:14 -0800361 hs->state = SSL3_ST_CW_NEXT_PROTO_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800362 break;
363
364 case SSL3_ST_CW_NEXT_PROTO_A:
David Benjamin1b249672016-12-06 18:25:50 -0500365 if (hs->next_proto_neg_seen) {
366 ret = ssl3_send_next_proto(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400367 if (ret <= 0) {
368 goto end;
369 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400371 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500373 hs->state = SSL3_ST_CW_CHANNEL_ID_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800374 break;
375
376 case SSL3_ST_CW_CHANNEL_ID_A:
David Benjaminc895d6b2016-08-11 13:26:41 -0400377 if (ssl->s3->tlsext_channel_id_valid) {
David Benjamin1b249672016-12-06 18:25:50 -0500378 ret = ssl3_send_channel_id(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400379 if (ret <= 0) {
380 goto end;
381 }
382 } else {
383 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800384 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500385 hs->state = SSL3_ST_CW_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800386 break;
387
388 case SSL3_ST_CW_FINISHED_A:
Robert Sloan4d1ac502017-02-06 08:36:14 -0800389 ret = ssl3_send_finished(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800390 if (ret <= 0) {
391 goto end;
392 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500393 hs->state = SSL3_ST_CW_FLUSH;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800394
David Benjaminc895d6b2016-08-11 13:26:41 -0400395 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500396 hs->next_state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -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. */
Robert Sloan5d625782017-02-13 09:55:39 -0800402 ret = tls1_record_handshake_hashes_for_channel_id(hs);
Kenny Rootb8494592015-09-25 02:29:14 +0000403 if (ret <= 0) {
404 goto end;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800405 }
Adam Langley4139edb2016-01-13 15:00:54 -0800406 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
407 ssl3_can_false_start(ssl) &&
Adam Langleye9ada862015-05-11 17:20:37 -0700408 /* No False Start on renegotiation (would complicate the state
409 * machine). */
Adam Langley4139edb2016-01-13 15:00:54 -0800410 !ssl->s3->initial_handshake_complete) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500411 hs->next_state = SSL3_ST_FALSE_START;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800412 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500413 hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800414 }
415 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800416 break;
417
David Benjamin6e899c72016-06-09 18:02:18 -0400418 case SSL3_ST_FALSE_START:
Steven Valdeze7531f02016-12-14 13:29:57 -0500419 hs->state = SSL3_ST_CR_SESSION_TICKET_A;
David Benjamin1b249672016-12-06 18:25:50 -0500420 hs->in_false_start = 1;
David Benjamin6e899c72016-06-09 18:02:18 -0400421 ret = 1;
422 goto end;
423
Adam Langleyd9e397b2015-01-22 14:27:53 -0800424 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamin1b249672016-12-06 18:25:50 -0500425 if (hs->ticket_expected) {
426 ret = ssl3_get_new_session_ticket(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400427 if (ret <= 0) {
428 goto end;
429 }
430 } else {
431 skip = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800432 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500433 hs->state = SSL3_ST_CR_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800434 break;
435
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436 case SSL3_ST_CR_CHANGE:
David Benjaminc895d6b2016-08-11 13:26:41 -0400437 ret = ssl->method->read_change_cipher_spec(ssl);
Adam Langley4139edb2016-01-13 15:00:54 -0800438 if (ret <= 0) {
439 goto end;
440 }
441
David Benjamin1b249672016-12-06 18:25:50 -0500442 if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443 ret = -1;
444 goto end;
445 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500446 hs->state = SSL3_ST_CR_FINISHED_A;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800447 break;
448
449 case SSL3_ST_CR_FINISHED_A:
David Benjamin1b249672016-12-06 18:25:50 -0500450 ret = ssl3_get_finished(hs);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800451 if (ret <= 0) {
452 goto end;
453 }
David Benjamin6e899c72016-06-09 18:02:18 -0400454 ssl->method->received_flight(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800455
David Benjaminc895d6b2016-08-11 13:26:41 -0400456 if (ssl->session != NULL) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500457 hs->state = SSL3_ST_CW_CHANGE;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800458 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -0500459 hs->state = SSL_ST_OK;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800460 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800461 break;
462
463 case SSL3_ST_CW_FLUSH:
David Benjaminf31229b2017-01-25 14:08:15 -0500464 ret = ssl->method->flush_flight(ssl);
465 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800466 goto end;
467 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500468 hs->state = hs->next_state;
469 if (hs->state != SSL_ST_OK) {
David Benjamin6e899c72016-06-09 18:02:18 -0400470 ssl->method->expect_flight(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800471 }
David Benjamin6e899c72016-06-09 18:02:18 -0400472 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800473
David Benjaminc895d6b2016-08-11 13:26:41 -0400474 case SSL_ST_TLS13:
David Benjamin1b249672016-12-06 18:25:50 -0500475 ret = tls13_handshake(hs);
David Benjaminc895d6b2016-08-11 13:26:41 -0400476 if (ret <= 0) {
477 goto end;
478 }
Steven Valdeze7531f02016-12-14 13:29:57 -0500479 hs->state = SSL_ST_OK;
David Benjaminc895d6b2016-08-11 13:26:41 -0400480 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800481
David Benjaminc895d6b2016-08-11 13:26:41 -0400482 case SSL_ST_OK:
David Benjaminc895d6b2016-08-11 13:26:41 -0400483 ssl->method->release_current_message(ssl, 1 /* free_buffer */);
484
485 SSL_SESSION_free(ssl->s3->established_session);
486 if (ssl->session != NULL) {
487 SSL_SESSION_up_ref(ssl->session);
488 ssl->s3->established_session = ssl->session;
489 } 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 =
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400494 SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
David Benjaminc895d6b2016-08-11 13:26:41 -0400495 if (ssl->s3->established_session == NULL) {
496 /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
497 * callers. */
Steven Valdeze7531f02016-12-14 13:29:57 -0500498 hs->state = SSL_ST_ERROR;
David Benjaminc895d6b2016-08-11 13:26:41 -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;
David Benjamin6e899c72016-06-09 18:02:18 -0400507 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800508
Adam Langley4139edb2016-01-13 15:00:54 -0800509 const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
Adam Langley4139edb2016-01-13 15:00:54 -0800510 ssl->s3->initial_handshake_complete = 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000511 if (is_initial_handshake) {
512 /* Renegotiations do not participate in session resumption. */
David Benjamin1b249672016-12-06 18:25:50 -0500513 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
Kenny Rootb8494592015-09-25 02:29:14 +0000514 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800515
David Benjamin6e899c72016-06-09 18:02:18 -0400516 ret = 1;
517 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800518 goto end;
519
David Benjaminc895d6b2016-08-11 13:26:41 -0400520 case SSL_ST_ERROR:
521 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
522 ret = -1;
523 goto end;
524
Adam Langleyd9e397b2015-01-22 14:27:53 -0800525 default:
Kenny Rootb8494592015-09-25 02:29:14 +0000526 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800527 ret = -1;
528 goto end;
529 }
530
Steven Valdeze7531f02016-12-14 13:29:57 -0500531 if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) {
532 int new_state = hs->state;
533 hs->state = state;
David Benjamin6e899c72016-06-09 18:02:18 -0400534 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
Steven Valdeze7531f02016-12-14 13:29:57 -0500535 hs->state = new_state;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800536 }
537 skip = 0;
538 }
539
540end:
David Benjamin6e899c72016-06-09 18:02:18 -0400541 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800542 return ret;
543}
544
Steven Valdezbb1ceac2016-10-07 10:34:51 -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
Robert Sloan4d1ac502017-02-06 08:36:14 -0800557/* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
558 * disabled algorithms. */
559static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
560 uint32_t *out_mask_k) {
561 int have_rsa = 0, have_ecdsa = 0;
562 *out_mask_a = 0;
563 *out_mask_k = 0;
564
565 /* Now go through all signature algorithms seeing if we support any for RSA or
566 * ECDSA. Do this for all versions not just TLS 1.2. */
567 const uint16_t *sigalgs;
568 size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
569 for (size_t i = 0; i < num_sigalgs; i++) {
570 switch (sigalgs[i]) {
571 case SSL_SIGN_RSA_PSS_SHA512:
572 case SSL_SIGN_RSA_PSS_SHA384:
573 case SSL_SIGN_RSA_PSS_SHA256:
574 case SSL_SIGN_RSA_PKCS1_SHA512:
575 case SSL_SIGN_RSA_PKCS1_SHA384:
576 case SSL_SIGN_RSA_PKCS1_SHA256:
577 case SSL_SIGN_RSA_PKCS1_SHA1:
578 have_rsa = 1;
579 break;
580
581 case SSL_SIGN_ECDSA_SECP521R1_SHA512:
582 case SSL_SIGN_ECDSA_SECP384R1_SHA384:
583 case SSL_SIGN_ECDSA_SECP256R1_SHA256:
584 case SSL_SIGN_ECDSA_SHA1:
585 have_ecdsa = 1;
586 break;
587 }
588 }
589
590 /* Disable auth if we don't include any appropriate signature algorithms. */
591 if (!have_rsa) {
592 *out_mask_a |= SSL_aRSA;
593 }
594 if (!have_ecdsa) {
595 *out_mask_a |= SSL_aECDSA;
596 }
597
598 /* PSK requires a client callback. */
599 if (ssl->psk_client_callback == NULL) {
600 *out_mask_a |= SSL_aPSK;
601 *out_mask_k |= SSL_kPSK;
602 }
603}
604
David Benjaminc895d6b2016-08-11 13:26:41 -0400605static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
606 uint16_t min_version,
David Benjamin7c0d06c2016-08-11 13:26:41 -0400607 uint16_t max_version) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800608 uint32_t mask_a, mask_k;
609 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800610
Kenny Roote99801b2015-11-06 15:31:15 -0800611 CBB child;
612 if (!CBB_add_u16_length_prefixed(out, &child)) {
613 return 0;
Adam Langleyfdeb4882015-10-30 13:15:30 -0700614 }
615
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400616 /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
617 if (ssl->ctx->grease_enabled &&
618 !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
619 return 0;
620 }
621
Steven Valdez909b19f2016-11-21 15:35:44 -0500622 /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
623 * hardware support. */
624 if (max_version >= TLS1_3_VERSION) {
625 if (!EVP_has_aes_hardware() &&
626 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
627 return 0;
Adam Langleyfad63272015-11-12 12:15:39 -0800628 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500629 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
630 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
631 return 0;
Kenny Roote99801b2015-11-06 15:31:15 -0800632 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500633 if (EVP_has_aes_hardware() &&
634 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
Kenny Roote99801b2015-11-06 15:31:15 -0800635 return 0;
636 }
637 }
638
Steven Valdez909b19f2016-11-21 15:35:44 -0500639 if (min_version < TLS1_3_VERSION) {
640 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
641 int any_enabled = 0;
642 for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
643 const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
644 /* Skip disabled ciphers */
Robert Sloan4d1ac502017-02-06 08:36:14 -0800645 if ((cipher->algorithm_mkey & mask_k) ||
646 (cipher->algorithm_auth & mask_a)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500647 continue;
648 }
649 if (SSL_CIPHER_get_min_version(cipher) > max_version ||
650 SSL_CIPHER_get_max_version(cipher) < min_version) {
651 continue;
652 }
653 any_enabled = 1;
654 if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
655 return 0;
656 }
657 }
658
659 /* If all ciphers were disabled, return the error to the caller. */
660 if (!any_enabled && max_version < TLS1_3_VERSION) {
661 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
662 return 0;
663 }
Kenny Roote99801b2015-11-06 15:31:15 -0800664 }
665
666 /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
667 * added. */
David Benjamin1b249672016-12-06 18:25:50 -0500668 if (max_version == SSL3_VERSION &&
Kenny Roote99801b2015-11-06 15:31:15 -0800669 !ssl->s3->initial_handshake_complete) {
670 if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
671 return 0;
672 }
Kenny Roote99801b2015-11-06 15:31:15 -0800673 }
674
David Benjamin7c0d06c2016-08-11 13:26:41 -0400675 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400676 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
677 return 0;
678 }
Kenny Roote99801b2015-11-06 15:31:15 -0800679 }
680
681 return CBB_flush(out);
682}
683
David Benjamin1b249672016-12-06 18:25:50 -0500684int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
685 SSL *const ssl = hs->ssl;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400686 uint16_t min_version, max_version;
687 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400688 return 0;
689 }
690
Steven Valdez909b19f2016-11-21 15:35:44 -0500691 CBB cbb, body;
692 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
693 goto err;
694 }
695
David Benjaminc895d6b2016-08-11 13:26:41 -0400696 /* Renegotiations do not participate in session resumption. */
697 int has_session = ssl->session != NULL &&
698 !ssl->s3->initial_handshake_complete;
699
700 CBB child;
David Benjamin1b249672016-12-06 18:25:50 -0500701 if (!CBB_add_u16(&body, hs->client_version) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500702 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
703 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400704 (has_session &&
705 !CBB_add_bytes(&child, ssl->session->session_id,
706 ssl->session->session_id_length))) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500707 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -0400708 }
709
710 if (SSL_is_dtls(ssl)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500711 if (!CBB_add_u8_length_prefixed(&body, &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400712 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500713 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -0400714 }
715 }
716
717 size_t header_len =
718 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
Steven Valdez909b19f2016-11-21 15:35:44 -0500719 if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
720 !CBB_add_u8(&body, 1 /* one compression method */) ||
721 !CBB_add_u8(&body, 0 /* null compression */) ||
David Benjamin1b249672016-12-06 18:25:50 -0500722 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500723 goto err;
David Benjaminc895d6b2016-08-11 13:26:41 -0400724 }
725
Steven Valdez909b19f2016-11-21 15:35:44 -0500726 uint8_t *msg = NULL;
727 size_t len;
728 if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
729 goto err;
730 }
731
732 /* Now that the length prefixes have been computed, fill in the placeholder
733 * PSK binder. */
David Benjamin1b249672016-12-06 18:25:50 -0500734 if (hs->needs_psk_binder &&
Robert Sloan5d625782017-02-13 09:55:39 -0800735 !tls13_write_psk_binder(hs, msg, len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500736 OPENSSL_free(msg);
737 goto err;
738 }
739
Robert Sloan4d1ac502017-02-06 08:36:14 -0800740 return ssl->method->add_message(ssl, msg, len);
Steven Valdez909b19f2016-11-21 15:35:44 -0500741
742 err:
743 CBB_cleanup(&cbb);
744 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400745}
746
David Benjamin1b249672016-12-06 18:25:50 -0500747static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
748 SSL *const ssl = hs->ssl;
David Benjamin6e899c72016-06-09 18:02:18 -0400749 /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
750 * may send multiple ClientHellos if we receive HelloVerifyRequest. */
Robert Sloan5d625782017-02-13 09:55:39 -0800751 if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800752 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
753 return -1;
754 }
755
David Benjaminc895d6b2016-08-11 13:26:41 -0400756 uint16_t min_version, max_version;
757 if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500758 return -1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400759 }
760
David Benjamin1b249672016-12-06 18:25:50 -0500761 uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
Steven Valdeze7531f02016-12-14 13:29:57 -0500762 assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
Kenny Roote99801b2015-11-06 15:31:15 -0800763 if (!ssl->s3->have_version) {
David Benjamin1b249672016-12-06 18:25:50 -0500764 ssl->version = max_wire_version;
765 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400766
David Benjamin1b249672016-12-06 18:25:50 -0500767 /* Always advertise the ClientHello version from the original maximum version,
768 * even on renegotiation. The static RSA key exchange uses this field, and
769 * some servers fail when it changes across handshakes. */
770 hs->client_version = max_wire_version;
771 if (max_version >= TLS1_3_VERSION) {
772 hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
Kenny Roote99801b2015-11-06 15:31:15 -0800773 }
774
David Benjamin4969cc92016-04-22 15:02:23 -0400775 /* If the configured session has expired or was created at a disabled
776 * version, drop it. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400777 if (ssl->session != NULL) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400778 uint16_t session_version;
Steven Valdeze7531f02016-12-14 13:29:57 -0500779 if (ssl->session->is_server ||
780 !ssl->method->version_from_wire(&session_version,
David Benjamin7c0d06c2016-08-11 13:26:41 -0400781 ssl->session->ssl_version) ||
782 (session_version < TLS1_3_VERSION &&
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400783 ssl->session->session_id_length == 0) ||
784 ssl->session->not_resumable ||
785 !ssl_session_is_time_valid(ssl, ssl->session) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400786 session_version < min_version || session_version > max_version) {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400787 ssl_set_session(ssl, NULL);
David Benjaminc895d6b2016-08-11 13:26:41 -0400788 }
Kenny Roote99801b2015-11-06 15:31:15 -0800789 }
790
791 /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
792 * renegerate the client_random. The random must be reused. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400793 if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
794 !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500795 return -1;
Kenny Roote99801b2015-11-06 15:31:15 -0800796 }
797
David Benjamin1b249672016-12-06 18:25:50 -0500798 if (!ssl_write_client_hello(hs)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500799 return -1;
Kenny Roote99801b2015-11-06 15:31:15 -0800800 }
801
Robert Sloan4d1ac502017-02-06 08:36:14 -0800802 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800803}
804
David Benjamin1b249672016-12-06 18:25:50 -0500805static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
806 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400807 int al;
David Benjamin6e899c72016-06-09 18:02:18 -0400808 CBS hello_verify_request, cookie;
809 uint16_t server_version;
810
Robert Sloan4d1ac502017-02-06 08:36:14 -0800811 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400812 if (ret <= 0) {
813 return ret;
David Benjamin6e899c72016-06-09 18:02:18 -0400814 }
815
816 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
817 ssl->d1->send_cookie = 0;
818 ssl->s3->tmp.reuse_message = 1;
819 return 1;
820 }
821
Robert Sloan4d1ac502017-02-06 08:36:14 -0800822 CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -0400823 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
824 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
825 CBS_len(&hello_verify_request) != 0) {
826 al = SSL_AD_DECODE_ERROR;
827 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
828 goto f_err;
829 }
830
831 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
832 al = SSL_AD_ILLEGAL_PARAMETER;
833 goto f_err;
834 }
835
Robert Sloan69939df2017-01-09 10:53:07 -0800836 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
David Benjamin6e899c72016-06-09 18:02:18 -0400837 ssl->d1->cookie_len = CBS_len(&cookie);
838
839 ssl->d1->send_cookie = 1;
840 return 1;
841
842f_err:
843 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
844 return -1;
845}
846
David Benjamin1b249672016-12-06 18:25:50 -0500847static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
848 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400849 int al = SSL_AD_INTERNAL_ERROR;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800850 CBS server_hello, server_random, session_id;
David Benjamin7c0d06c2016-08-11 13:26:41 -0400851 uint16_t server_wire_version, cipher_suite;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800852 uint8_t compression_method;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800853
Robert Sloan4d1ac502017-02-06 08:36:14 -0800854 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -0400855 if (ret <= 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800856 uint32_t err = ERR_peek_error();
857 if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
858 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
859 /* Add a dedicated error code to the queue for a handshake_failure alert
860 * in response to ClientHello. This matches NSS's client behavior and
861 * gives a better error on a (probable) failure to negotiate initial
862 * parameters. Note: this error code comes after the original one.
863 *
864 * See https://crbug.com/446505. */
Kenny Rootb8494592015-09-25 02:29:14 +0000865 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800866 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400867 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800868 }
869
David Benjaminc895d6b2016-08-11 13:26:41 -0400870 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
871 ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
872 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
873 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
874 return -1;
875 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800876
David Benjaminc895d6b2016-08-11 13:26:41 -0400877 CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
878
879 if (!CBS_get_u16(&server_hello, &server_wire_version)) {
880 al = SSL_AD_DECODE_ERROR;
881 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
882 goto f_err;
883 }
884
David Benjamin7c0d06c2016-08-11 13:26:41 -0400885 uint16_t min_version, max_version, server_version;
886 if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
887 !ssl->method->version_from_wire(&server_version, server_wire_version) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400888 server_version < min_version || server_version > max_version) {
889 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
890 al = SSL_AD_PROTOCOL_VERSION;
891 goto f_err;
892 }
893
894 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
895 if (!ssl->s3->have_version) {
896 ssl->version = server_wire_version;
David Benjaminc895d6b2016-08-11 13:26:41 -0400897 /* At this point, the connection's version is known and ssl->version is
898 * fixed. Begin enforcing the record-layer version. */
899 ssl->s3->have_version = 1;
900 } else if (server_wire_version != ssl->version) {
901 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
902 al = SSL_AD_PROTOCOL_VERSION;
903 goto f_err;
904 }
905
906 if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500907 hs->state = SSL_ST_TLS13;
David Benjamin1b249672016-12-06 18:25:50 -0500908 hs->do_tls13_handshake = tls13_client_handshake;
David Benjaminc895d6b2016-08-11 13:26:41 -0400909 return 1;
910 }
911
David Benjamin1b249672016-12-06 18:25:50 -0500912 ssl_clear_tls13_state(hs);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400913
Robert Sloan4d1ac502017-02-06 08:36:14 -0800914 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400915 return -1;
916 }
917
918 if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800919 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
920 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
921 !CBS_get_u16(&server_hello, &cipher_suite) ||
922 !CBS_get_u8(&server_hello, &compression_method)) {
923 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000924 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800925 goto f_err;
926 }
927
Adam Langleyd9e397b2015-01-22 14:27:53 -0800928 /* Copy over the server random. */
Robert Sloan69939df2017-01-09 10:53:07 -0800929 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800930
David Benjamin7c0d06c2016-08-11 13:26:41 -0400931 /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
932 * 1.3 is finalized and we are not implementing a draft version. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400933
Adam Langley4139edb2016-01-13 15:00:54 -0800934 if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400935 ssl->session->session_id_length != 0 &&
Adam Langley4139edb2016-01-13 15:00:54 -0800936 CBS_mem_equal(&session_id, ssl->session->session_id,
937 ssl->session->session_id_length)) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400938 ssl->s3->session_reused = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800939 } else {
940 /* The session wasn't resumed. Create a fresh SSL_SESSION to
941 * fill out. */
David Benjamin7c0d06c2016-08-11 13:26:41 -0400942 ssl_set_session(ssl, NULL);
David Benjamin1b249672016-12-06 18:25:50 -0500943 if (!ssl_get_new_session(hs, 0 /* client */)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800944 goto f_err;
945 }
946 /* Note: session_id could be empty. */
David Benjaminc895d6b2016-08-11 13:26:41 -0400947 ssl->s3->new_session->session_id_length = CBS_len(&session_id);
Robert Sloan69939df2017-01-09 10:53:07 -0800948 OPENSSL_memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
Adam Langley4139edb2016-01-13 15:00:54 -0800949 CBS_len(&session_id));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800950 }
951
Steven Valdez909b19f2016-11-21 15:35:44 -0500952 const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800953 if (c == NULL) {
954 /* unknown cipher */
955 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000956 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800957 goto f_err;
958 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500959
960 /* The cipher must be allowed in the selected version and enabled. */
Robert Sloan4d1ac502017-02-06 08:36:14 -0800961 uint32_t mask_a, mask_k;
962 ssl_get_client_disabled(ssl, &mask_a, &mask_k);
963 if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400964 SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500965 SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
966 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800967 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000968 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800969 goto f_err;
970 }
971
David Benjaminc895d6b2016-08-11 13:26:41 -0400972 if (ssl->session != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800973 if (ssl->session->ssl_version != ssl->version) {
Adam Langleye9ada862015-05-11 17:20:37 -0700974 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000975 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
Adam Langleye9ada862015-05-11 17:20:37 -0700976 goto f_err;
977 }
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400978 if (ssl->session->cipher != c) {
979 al = SSL_AD_ILLEGAL_PARAMETER;
980 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
981 goto f_err;
982 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400983 if (!ssl_session_is_context_valid(ssl, ssl->session)) {
984 /* This is actually a client application bug. */
985 al = SSL_AD_ILLEGAL_PARAMETER;
986 OPENSSL_PUT_ERROR(SSL,
987 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
988 goto f_err;
989 }
David Benjamin4969cc92016-04-22 15:02:23 -0400990 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400991 ssl->s3->new_session->cipher = c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800992 }
Adam Langley4139edb2016-01-13 15:00:54 -0800993 ssl->s3->tmp.new_cipher = c;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800994
Robert Sloan4d1ac502017-02-06 08:36:14 -0800995 /* Now that the cipher is known, initialize the handshake hash and hash the
996 * ServerHello. */
Robert Sloan5d625782017-02-13 09:55:39 -0800997 if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
998 c->algorithm_prf) ||
999 !ssl_hash_current_message(hs)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001000 goto f_err;
1001 }
1002
David Benjaminc895d6b2016-08-11 13:26:41 -04001003 /* If doing a full handshake, the server may request a client certificate
1004 * which requires hashing the handshake transcript. Otherwise, the handshake
1005 * buffer may be released. */
1006 if (ssl->session != NULL ||
David Benjamind316cba2016-06-02 16:17:39 -04001007 !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
Robert Sloan5d625782017-02-13 09:55:39 -08001008 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001009 }
1010
1011 /* Only the NULL compression algorithm is supported. */
1012 if (compression_method != 0) {
1013 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +00001014 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001015 goto f_err;
1016 }
1017
1018 /* TLS extensions */
David Benjamin1b249672016-12-06 18:25:50 -05001019 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001020 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001021 goto err;
1022 }
1023
1024 /* There should be nothing left over in the record. */
1025 if (CBS_len(&server_hello) != 0) {
1026 /* wrong packet length */
1027 al = SSL_AD_DECODE_ERROR;
David Benjamin7c0d06c2016-08-11 13:26:41 -04001028 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001029 goto f_err;
1030 }
1031
David Benjaminc895d6b2016-08-11 13:26:41 -04001032 if (ssl->session != NULL &&
Adam Langley4139edb2016-01-13 15:00:54 -08001033 ssl->s3->tmp.extended_master_secret !=
1034 ssl->session->extended_master_secret) {
Adam Langleyf4e42722015-06-04 17:45:09 -07001035 al = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley4139edb2016-01-13 15:00:54 -08001036 if (ssl->session->extended_master_secret) {
Kenny Rootb8494592015-09-25 02:29:14 +00001037 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -07001038 } else {
Kenny Rootb8494592015-09-25 02:29:14 +00001039 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
Adam Langleyf4e42722015-06-04 17:45:09 -07001040 }
1041 goto f_err;
1042 }
1043
Adam Langleyd9e397b2015-01-22 14:27:53 -08001044 return 1;
1045
1046f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001047 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001048err:
1049 return -1;
1050}
1051
David Benjamin1b249672016-12-06 18:25:50 -05001052static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
1053 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001054 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001055 if (ret <= 0) {
1056 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001057 }
1058
Robert Sloan4d1ac502017-02-06 08:36:14 -08001059 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001060 !ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001061 return -1;
1062 }
1063
David Benjaminc895d6b2016-08-11 13:26:41 -04001064 CBS cbs;
1065 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Steven Valdeze7531f02016-12-14 13:29:57 -05001066
Robert Sloan5d625782017-02-13 09:55:39 -08001067 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdeze7531f02016-12-14 13:29:57 -05001068 sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free);
1069 EVP_PKEY_free(hs->peer_pubkey);
1070 hs->peer_pubkey = NULL;
1071 ssl->s3->new_session->certs = ssl_parse_cert_chain(
1072 &alert, &hs->peer_pubkey, NULL, &cbs, ssl->ctx->pool);
1073 if (ssl->s3->new_session->certs == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001074 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
Steven Valdeze7531f02016-12-14 13:29:57 -05001075 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001076 }
1077
Steven Valdeze7531f02016-12-14 13:29:57 -05001078 if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0 ||
1079 CBS_len(&cbs) != 0 ||
Robert Sloan5d625782017-02-13 09:55:39 -08001080 !ssl->ctx->x509_method->session_cache_objects(ssl->s3->new_session)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001081 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001082 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdeze7531f02016-12-14 13:29:57 -05001083 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001084 }
1085
Steven Valdeze7531f02016-12-14 13:29:57 -05001086 if (!ssl_check_leaf_certificate(
1087 ssl, hs->peer_pubkey,
1088 sk_CRYPTO_BUFFER_value(ssl->s3->new_session->certs, 0))) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001089 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Steven Valdeze7531f02016-12-14 13:29:57 -05001090 return -1;
Kenny Roota04d78d2015-09-25 00:26:37 +00001091 }
Kenny Rootb8494592015-09-25 02:29:14 +00001092
David Benjaminc895d6b2016-08-11 13:26:41 -04001093 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001094}
1095
David Benjamin1b249672016-12-06 18:25:50 -05001096static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
1097 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001098 int al;
David Benjamin6e899c72016-06-09 18:02:18 -04001099 CBS certificate_status, ocsp_response;
1100 uint8_t status_type;
1101
Robert Sloan4d1ac502017-02-06 08:36:14 -08001102 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001103 if (ret <= 0) {
1104 return ret;
David Benjamin6e899c72016-06-09 18:02:18 -04001105 }
1106
1107 if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
1108 /* A server may send status_request in ServerHello and then change
1109 * its mind about sending CertificateStatus. */
1110 ssl->s3->tmp.reuse_message = 1;
1111 return 1;
1112 }
1113
Robert Sloan5d625782017-02-13 09:55:39 -08001114 if (!ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001115 return -1;
1116 }
1117
David Benjaminc895d6b2016-08-11 13:26:41 -04001118 CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
David Benjamin6e899c72016-06-09 18:02:18 -04001119 if (!CBS_get_u8(&certificate_status, &status_type) ||
1120 status_type != TLSEXT_STATUSTYPE_ocsp ||
1121 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1122 CBS_len(&ocsp_response) == 0 ||
1123 CBS_len(&certificate_status) != 0) {
1124 al = SSL_AD_DECODE_ERROR;
1125 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1126 goto f_err;
1127 }
1128
David Benjaminc895d6b2016-08-11 13:26:41 -04001129 if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
1130 &ssl->s3->new_session->ocsp_response_length)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001131 al = SSL_AD_INTERNAL_ERROR;
1132 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1133 goto f_err;
1134 }
1135 return 1;
1136
1137f_err:
1138 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
1139 return -1;
1140}
1141
David Benjamin1b249672016-12-06 18:25:50 -05001142static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
1143 SSL *const ssl = hs->ssl;
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001144 if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
Steven Valdez909b19f2016-11-21 15:35:44 -05001145 ssl->s3->new_session->x509_chain)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001146 return -1;
David Benjamin6e899c72016-06-09 18:02:18 -04001147 }
1148
David Benjaminc895d6b2016-08-11 13:26:41 -04001149 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001150}
1151
David Benjamin1b249672016-12-06 18:25:50 -05001152static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
1153 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -04001154 int al;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001155 DH *dh = NULL;
1156 EC_KEY *ecdh = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001157 EC_POINT *srvr_ecpoint = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001158
Robert Sloan4d1ac502017-02-06 08:36:14 -08001159 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001160 if (ret <= 0) {
1161 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001162 }
1163
Adam Langley4139edb2016-01-13 15:00:54 -08001164 if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
David Benjamin7c0d06c2016-08-11 13:26:41 -04001165 /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
Adam Langley4139edb2016-01-13 15:00:54 -08001166 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001167 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langley4139edb2016-01-13 15:00:54 -08001168 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001169 return -1;
1170 }
1171
Adam Langley4139edb2016-01-13 15:00:54 -08001172 ssl->s3->tmp.reuse_message = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001173 return 1;
1174 }
1175
Robert Sloan5d625782017-02-13 09:55:39 -08001176 if (!ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001177 return -1;
1178 }
1179
Adam Langleyd9e397b2015-01-22 14:27:53 -08001180 /* Retain a copy of the original CBS to compute the signature over. */
David Benjamin4969cc92016-04-22 15:02:23 -04001181 CBS server_key_exchange;
David Benjaminc895d6b2016-08-11 13:26:41 -04001182 CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
David Benjamin4969cc92016-04-22 15:02:23 -04001183 CBS server_key_exchange_orig = server_key_exchange;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001184
David Benjamin4969cc92016-04-22 15:02:23 -04001185 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1186 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001187
1188 if (alg_a & SSL_aPSK) {
1189 CBS psk_identity_hint;
1190
1191 /* Each of the PSK key exchanges begins with a psk_identity_hint. */
1192 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1193 &psk_identity_hint)) {
1194 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001195 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001196 goto f_err;
1197 }
1198
1199 /* Store PSK identity hint for later use, hint is used in
1200 * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
1201 * identity hint can be as long as the maximum length of a PSK identity.
1202 * Also do not allow NULL characters; identities are saved as C strings.
1203 *
1204 * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1205 * a specific identity. */
1206 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1207 CBS_contains_zero_byte(&psk_identity_hint)) {
1208 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +00001209 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001210 goto f_err;
1211 }
1212
David Benjamin7c0d06c2016-08-11 13:26:41 -04001213 /* Save non-empty identity hints as a C string. Empty identity hints we
1214 * treat as missing. Plain PSK makes it possible to send either no hint
1215 * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1216 * empty hint. Having different capabilities is odd, so we interpret empty
1217 * and missing as identical. */
1218 if (CBS_len(&psk_identity_hint) != 0 &&
David Benjamin1b249672016-12-06 18:25:50 -05001219 !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001220 al = SSL_AD_INTERNAL_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001221 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001222 goto f_err;
1223 }
1224 }
1225
Adam Langleye9ada862015-05-11 17:20:37 -07001226 if (alg_k & SSL_kDHE) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001227 CBS dh_p, dh_g, dh_Ys;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001228 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
1229 CBS_len(&dh_p) == 0 ||
1230 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
1231 CBS_len(&dh_g) == 0 ||
1232 !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
1233 CBS_len(&dh_Ys) == 0) {
1234 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001235 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001236 goto f_err;
1237 }
1238
1239 dh = DH_new();
1240 if (dh == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001241 goto err;
1242 }
1243
Adam Langley4139edb2016-01-13 15:00:54 -08001244 dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
1245 dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
1246 if (dh->p == NULL || dh->g == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001247 goto err;
1248 }
1249
Steven Valdeze7531f02016-12-14 13:29:57 -05001250 unsigned bits = DH_num_bits(dh);
1251 if (bits < 1024) {
Kenny Rootb8494592015-09-25 02:29:14 +00001252 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001253 goto err;
Steven Valdeze7531f02016-12-14 13:29:57 -05001254 } else if (bits > 4096) {
Adam Langley4139edb2016-01-13 15:00:54 -08001255 /* Overly large DHE groups are prohibitively expensive, so enforce a limit
1256 * to prevent a server from causing us to perform too expensive of a
1257 * computation. */
1258 OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001259 goto err;
1260 }
1261
David Benjamin1b249672016-12-06 18:25:50 -05001262 SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
Adam Langley4139edb2016-01-13 15:00:54 -08001263 dh = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001264
Adam Langley4139edb2016-01-13 15:00:54 -08001265 /* Save the peer public key for later. */
David Benjamin1b249672016-12-06 18:25:50 -05001266 if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001267 goto err;
1268 }
Adam Langley4139edb2016-01-13 15:00:54 -08001269 } else if (alg_k & SSL_kECDHE) {
1270 /* Parse the server parameters. */
David Benjamind316cba2016-06-02 16:17:39 -04001271 uint8_t group_type;
1272 uint16_t group_id;
Adam Langley4139edb2016-01-13 15:00:54 -08001273 CBS point;
David Benjamind316cba2016-06-02 16:17:39 -04001274 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1275 group_type != NAMED_CURVE_TYPE ||
1276 !CBS_get_u16(&server_key_exchange, &group_id) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001277 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001278 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001279 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001280 goto f_err;
1281 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001282 ssl->s3->new_session->group_id = group_id;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001283
David Benjamind316cba2016-06-02 16:17:39 -04001284 /* Ensure the group is consistent with preferences. */
1285 if (!tls1_check_group_id(ssl, group_id)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001286 al = SSL_AD_ILLEGAL_PARAMETER;
1287 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001288 goto f_err;
1289 }
Adam Langley4139edb2016-01-13 15:00:54 -08001290
1291 /* Initialize ECDH and save the peer public key for later. */
David Benjamin1b249672016-12-06 18:25:50 -05001292 if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
1293 !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001294 goto err;
1295 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001296 } else if (!(alg_k & SSL_kPSK)) {
1297 al = SSL_AD_UNEXPECTED_MESSAGE;
Kenny Rootb8494592015-09-25 02:29:14 +00001298 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001299 goto f_err;
1300 }
1301
1302 /* At this point, |server_key_exchange| contains the signature, if any, while
1303 * |server_key_exchange_orig| contains the entire message. From that, derive
1304 * a CBS containing just the parameter. */
David Benjamin4969cc92016-04-22 15:02:23 -04001305 CBS parameter;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001306 CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
1307 CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
1308
Kenny Rootb8494592015-09-25 02:29:14 +00001309 /* ServerKeyExchange should be signed by the server's public key. */
David Benjamind316cba2016-06-02 16:17:39 -04001310 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001311 uint16_t signature_algorithm = 0;
David Benjamin4969cc92016-04-22 15:02:23 -04001312 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001313 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001314 al = SSL_AD_DECODE_ERROR;
1315 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001316 goto f_err;
1317 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001318 if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
Adam Langleyfad63272015-11-12 12:15:39 -08001319 goto f_err;
1320 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001321 ssl->s3->new_session->peer_signature_algorithm = signature_algorithm;
1322 } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001323 signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
Steven Valdeze7531f02016-12-14 13:29:57 -05001324 } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001325 signature_algorithm = SSL_SIGN_ECDSA_SHA1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001326 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -04001327 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1328 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1329 goto f_err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001330 }
1331
1332 /* The last field in |server_key_exchange| is the signature. */
Kenny Rootb8494592015-09-25 02:29:14 +00001333 CBS signature;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001334 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1335 CBS_len(&server_key_exchange) != 0) {
1336 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001337 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001338 goto f_err;
1339 }
1340
David Benjaminc895d6b2016-08-11 13:26:41 -04001341 CBB transcript;
1342 uint8_t *transcript_data;
1343 size_t transcript_len;
1344 if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1345 !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
1346 !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
1347 !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
1348 !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
1349 CBB_cleanup(&transcript);
1350 al = SSL_AD_INTERNAL_ERROR;
1351 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1352 goto f_err;
1353 }
1354
1355 int sig_ok = ssl_public_key_verify(
1356 ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
Steven Valdeze7531f02016-12-14 13:29:57 -05001357 hs->peer_pubkey, transcript_data, transcript_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001358 OPENSSL_free(transcript_data);
1359
David Benjamin4969cc92016-04-22 15:02:23 -04001360#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
1361 sig_ok = 1;
1362 ERR_clear_error();
1363#endif
1364 if (!sig_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001365 /* bad signature */
1366 al = SSL_AD_DECRYPT_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001367 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001368 goto f_err;
1369 }
1370 } else {
Kenny Rootb8494592015-09-25 02:29:14 +00001371 /* PSK ciphers are the only supported certificate-less ciphers. */
1372 assert(alg_a == SSL_aPSK);
1373
Adam Langleyd9e397b2015-01-22 14:27:53 -08001374 if (CBS_len(&server_key_exchange) > 0) {
1375 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +00001376 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001377 goto f_err;
1378 }
1379 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001380 return 1;
1381
1382f_err:
Adam Langley4139edb2016-01-13 15:00:54 -08001383 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001384err:
Adam Langleye9ada862015-05-11 17:20:37 -07001385 DH_free(dh);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001386 EC_POINT_free(srvr_ecpoint);
Adam Langleye9ada862015-05-11 17:20:37 -07001387 EC_KEY_free(ecdh);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001388 return -1;
1389}
1390
David Benjamin1b249672016-12-06 18:25:50 -05001391static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
1392 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001393 int msg_ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001394 if (msg_ret <= 0) {
1395 return msg_ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001396 }
1397
David Benjamin6e899c72016-06-09 18:02:18 -04001398 if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
Adam Langley4139edb2016-01-13 15:00:54 -08001399 ssl->s3->tmp.reuse_message = 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001400 /* If we get here we don't need the handshake buffer as we won't be doing
1401 * client auth. */
Robert Sloan5d625782017-02-13 09:55:39 -08001402 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001403 return 1;
1404 }
1405
Robert Sloan4d1ac502017-02-06 08:36:14 -08001406 if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001407 !ssl_hash_current_message(hs)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001408 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001409 }
1410
David Benjamin4969cc92016-04-22 15:02:23 -04001411 CBS cbs;
David Benjaminc895d6b2016-08-11 13:26:41 -04001412 CBS_init(&cbs, ssl->init_msg, ssl->init_num);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001413
David Benjaminc895d6b2016-08-11 13:26:41 -04001414 /* Get the certificate types. */
David Benjamin4969cc92016-04-22 15:02:23 -04001415 CBS certificate_types;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001416 if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001417 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Kenny Rootb8494592015-09-25 02:29:14 +00001418 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001419 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001420 }
1421
David Benjamin1b249672016-12-06 18:25:50 -05001422 if (!CBS_stow(&certificate_types, &hs->certificate_types,
1423 &hs->num_certificate_types)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001424 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001425 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001426 }
1427
David Benjamin4969cc92016-04-22 15:02:23 -04001428 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001429 CBS supported_signature_algorithms;
Kenny Rootb8494592015-09-25 02:29:14 +00001430 if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
David Benjamin1b249672016-12-06 18:25:50 -05001431 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001432 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Kenny Rootb8494592015-09-25 02:29:14 +00001433 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001434 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001435 }
1436 }
1437
Robert Sloan5d625782017-02-13 09:55:39 -08001438 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjaminc895d6b2016-08-11 13:26:41 -04001439 STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
1440 if (ca_sk == NULL) {
1441 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1442 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001443 }
1444
David Benjamin7c0d06c2016-08-11 13:26:41 -04001445 if (CBS_len(&cbs) != 0) {
1446 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1447 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
1448 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1449 return -1;
1450 }
1451
David Benjamin1b249672016-12-06 18:25:50 -05001452 hs->cert_request = 1;
1453 sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
1454 hs->ca_names = ca_sk;
David Benjaminc895d6b2016-08-11 13:26:41 -04001455 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001456}
1457
David Benjamin1b249672016-12-06 18:25:50 -05001458static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
1459 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001460 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001461 if (ret <= 0) {
1462 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001463 }
1464
Robert Sloan4d1ac502017-02-06 08:36:14 -08001465 if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001466 !ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001467 return -1;
1468 }
1469
David Benjaminc895d6b2016-08-11 13:26:41 -04001470 /* ServerHelloDone is empty. */
1471 if (ssl->init_num > 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001472 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin7c0d06c2016-08-11 13:26:41 -04001473 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001474 return -1;
1475 }
1476
1477 return 1;
1478}
1479
David Benjamin1b249672016-12-06 18:25:50 -05001480static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
1481 SSL *const ssl = hs->ssl;
Steven Valdeze7531f02016-12-14 13:29:57 -05001482 /* Call cert_cb to update the certificate. */
1483 if (ssl->cert->cert_cb) {
1484 int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
1485 if (ret < 0) {
1486 ssl->rwstate = SSL_X509_LOOKUP;
David Benjamin6e899c72016-06-09 18:02:18 -04001487 return -1;
1488 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001489 if (ret == 0) {
1490 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1491 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjamin6e899c72016-06-09 18:02:18 -04001492 return -1;
1493 }
David Benjamin6e899c72016-06-09 18:02:18 -04001494 }
1495
Steven Valdeze7531f02016-12-14 13:29:57 -05001496 if (!ssl_has_certificate(ssl)) {
Steven Valdeze7531f02016-12-14 13:29:57 -05001497 /* Without a client certificate, the handshake buffer may be released. */
Robert Sloan5d625782017-02-13 09:55:39 -08001498 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Steven Valdeze7531f02016-12-14 13:29:57 -05001499
Robert Sloan4d1ac502017-02-06 08:36:14 -08001500 /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
Steven Valdeze7531f02016-12-14 13:29:57 -05001501 if (ssl->version == SSL3_VERSION) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001502 if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
1503 SSL_AD_NO_CERTIFICATE)) {
1504 return -1;
1505 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001506 return 1;
1507 }
1508 }
1509
Robert Sloan69939df2017-01-09 10:53:07 -08001510 if (!ssl_auto_chain_if_needed(ssl) ||
1511 !ssl3_output_cert_chain(ssl)) {
Steven Valdeze7531f02016-12-14 13:29:57 -05001512 return -1;
1513 }
Robert Sloan4d1ac502017-02-06 08:36:14 -08001514 return 1;
David Benjamin6e899c72016-06-09 18:02:18 -04001515}
1516
Kenny Roote99801b2015-11-06 15:31:15 -08001517OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
1518 SIZE_T_IS_SMALLER_THAN_UNSIGNED);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001519
David Benjamin1b249672016-12-06 18:25:50 -05001520static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1521 SSL *const ssl = hs->ssl;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001522 uint8_t *pms = NULL;
1523 size_t pms_len = 0;
David Benjaminc895d6b2016-08-11 13:26:41 -04001524 CBB cbb, body;
1525 if (!ssl->method->init_message(ssl, &cbb, &body,
1526 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001527 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001528 }
1529
Adam Langley4139edb2016-01-13 15:00:54 -08001530 uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
1531 uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
1532
1533 /* If using a PSK key exchange, prepare the pre-shared key. */
1534 unsigned psk_len = 0;
1535 uint8_t psk[PSK_MAX_PSK_LEN];
1536 if (alg_a & SSL_aPSK) {
1537 if (ssl->psk_client_callback == NULL) {
1538 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1539 goto err;
1540 }
1541
1542 char identity[PSK_MAX_IDENTITY_LEN + 1];
Robert Sloan69939df2017-01-09 10:53:07 -08001543 OPENSSL_memset(identity, 0, sizeof(identity));
David Benjamin1b249672016-12-06 18:25:50 -05001544 psk_len =
1545 ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
1546 sizeof(identity), psk, sizeof(psk));
Adam Langley4139edb2016-01-13 15:00:54 -08001547 if (psk_len == 0) {
1548 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1549 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1550 goto err;
1551 }
1552 assert(psk_len <= PSK_MAX_PSK_LEN);
1553
David Benjaminc895d6b2016-08-11 13:26:41 -04001554 OPENSSL_free(ssl->s3->new_session->psk_identity);
1555 ssl->s3->new_session->psk_identity = BUF_strdup(identity);
1556 if (ssl->s3->new_session->psk_identity == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -08001557 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1558 goto err;
1559 }
1560
1561 /* Write out psk_identity. */
1562 CBB child;
David Benjaminc895d6b2016-08-11 13:26:41 -04001563 if (!CBB_add_u16_length_prefixed(&body, &child) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001564 !CBB_add_bytes(&child, (const uint8_t *)identity,
1565 OPENSSL_strnlen(identity, sizeof(identity))) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001566 !CBB_flush(&body)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001567 goto err;
1568 }
1569 }
1570
1571 /* Depending on the key exchange method, compute |pms| and |pms_len|. */
1572 if (alg_k & SSL_kRSA) {
1573 pms_len = SSL_MAX_MASTER_KEY_LENGTH;
1574 pms = OPENSSL_malloc(pms_len);
1575 if (pms == NULL) {
1576 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1577 goto err;
1578 }
1579
Steven Valdeze7531f02016-12-14 13:29:57 -05001580 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
Adam Langley4139edb2016-01-13 15:00:54 -08001581 if (rsa == NULL) {
1582 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley4139edb2016-01-13 15:00:54 -08001583 goto err;
1584 }
1585
David Benjamin1b249672016-12-06 18:25:50 -05001586 pms[0] = hs->client_version >> 8;
1587 pms[1] = hs->client_version & 0xff;
Adam Langley4139edb2016-01-13 15:00:54 -08001588 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1589 goto err;
1590 }
1591
David Benjaminc895d6b2016-08-11 13:26:41 -04001592 CBB child, *enc_pms = &body;
Adam Langley4139edb2016-01-13 15:00:54 -08001593 size_t enc_pms_len;
1594 /* In TLS, there is a length prefix. */
1595 if (ssl->version > SSL3_VERSION) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001596 if (!CBB_add_u16_length_prefixed(&body, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001597 goto err;
1598 }
1599 enc_pms = &child;
1600 }
1601
1602 uint8_t *ptr;
1603 if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
1604 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
1605 RSA_PKCS1_PADDING) ||
Adam Langley4139edb2016-01-13 15:00:54 -08001606 !CBB_did_write(enc_pms, enc_pms_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001607 !CBB_flush(&body)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001608 goto err;
1609 }
Steven Valdeze7531f02016-12-14 13:29:57 -05001610 } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
David Benjamind316cba2016-06-02 16:17:39 -04001611 /* Generate a keypair and serialize the public half. */
Adam Langley4139edb2016-01-13 15:00:54 -08001612 CBB child;
David Benjamin1b249672016-12-06 18:25:50 -05001613 if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001614 goto err;
1615 }
1616
1617 /* Compute the premaster. */
Robert Sloan5d625782017-02-13 09:55:39 -08001618 uint8_t alert = SSL_AD_DECODE_ERROR;
David Benjamin1b249672016-12-06 18:25:50 -05001619 if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
1620 hs->peer_key, hs->peer_key_len)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001621 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
1622 goto err;
1623 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001624 if (!CBB_flush(&body)) {
David Benjamind316cba2016-06-02 16:17:39 -04001625 goto err;
1626 }
Adam Langley4139edb2016-01-13 15:00:54 -08001627
1628 /* The key exchange state may now be discarded. */
David Benjamin1b249672016-12-06 18:25:50 -05001629 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
1630 OPENSSL_free(hs->peer_key);
1631 hs->peer_key = NULL;
1632 hs->peer_key_len = 0;
Adam Langley4139edb2016-01-13 15:00:54 -08001633 } else if (alg_k & SSL_kPSK) {
1634 /* For plain PSK, other_secret is a block of 0s with the same length as
1635 * the pre-shared key. */
1636 pms_len = psk_len;
1637 pms = OPENSSL_malloc(pms_len);
1638 if (pms == NULL) {
1639 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1640 goto err;
1641 }
Robert Sloan69939df2017-01-09 10:53:07 -08001642 OPENSSL_memset(pms, 0, pms_len);
Adam Langley4139edb2016-01-13 15:00:54 -08001643 } else {
1644 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1645 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1646 goto err;
1647 }
1648
1649 /* For a PSK cipher suite, other_secret is combined with the pre-shared
1650 * key. */
1651 if (alg_a & SSL_aPSK) {
1652 CBB pms_cbb, child;
1653 uint8_t *new_pms;
1654 size_t new_pms_len;
1655
1656 CBB_zero(&pms_cbb);
1657 if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
1658 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1659 !CBB_add_bytes(&child, pms, pms_len) ||
1660 !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
1661 !CBB_add_bytes(&child, psk, psk_len) ||
1662 !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
1663 CBB_cleanup(&pms_cbb);
1664 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1665 goto err;
1666 }
1667 OPENSSL_cleanse(pms, pms_len);
1668 OPENSSL_free(pms);
1669 pms = new_pms;
1670 pms_len = new_pms_len;
1671 }
1672
1673 /* The message must be added to the finished hash before calculating the
1674 * master secret. */
Robert Sloan4d1ac502017-02-06 08:36:14 -08001675 if (!ssl_add_message_cbb(ssl, &cbb)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001676 goto err;
1677 }
Adam Langley4139edb2016-01-13 15:00:54 -08001678
Robert Sloan5d625782017-02-13 09:55:39 -08001679 ssl->s3->new_session->master_key_length = tls1_generate_master_secret(
1680 hs, ssl->s3->new_session->master_key, pms, pms_len);
David Benjaminc895d6b2016-08-11 13:26:41 -04001681 if (ssl->s3->new_session->master_key_length == 0) {
Adam Langley4139edb2016-01-13 15:00:54 -08001682 goto err;
1683 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001684 ssl->s3->new_session->extended_master_secret =
1685 ssl->s3->tmp.extended_master_secret;
Adam Langley4139edb2016-01-13 15:00:54 -08001686 OPENSSL_cleanse(pms, pms_len);
1687 OPENSSL_free(pms);
1688
Robert Sloan4d1ac502017-02-06 08:36:14 -08001689 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001690
1691err:
David Benjamin4969cc92016-04-22 15:02:23 -04001692 CBB_cleanup(&cbb);
Adam Langley4139edb2016-01-13 15:00:54 -08001693 if (pms != NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -08001694 OPENSSL_cleanse(pms, pms_len);
1695 OPENSSL_free(pms);
1696 }
1697 return -1;
1698}
1699
David Benjamin1b249672016-12-06 18:25:50 -05001700static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
1701 SSL *const ssl = hs->ssl;
Adam Langley4139edb2016-01-13 15:00:54 -08001702 assert(ssl_has_private_key(ssl));
1703
David Benjaminc895d6b2016-08-11 13:26:41 -04001704 CBB cbb, body, child;
1705 if (!ssl->method->init_message(ssl, &cbb, &body,
1706 SSL3_MT_CERTIFICATE_VERIFY)) {
1707 goto err;
1708 }
1709
1710 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -05001711 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001712 goto err;
1713 }
1714 if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
1715 /* Write out the digest type in TLS 1.2. */
1716 if (!CBB_add_u16(&body, signature_algorithm)) {
1717 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1718 goto err;
1719 }
1720 }
1721
1722 /* Set aside space for the signature. */
Adam Langley4139edb2016-01-13 15:00:54 -08001723 const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001724 uint8_t *ptr;
1725 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1726 !CBB_reserve(&child, &ptr, max_sig_len)) {
1727 goto err;
1728 }
1729
1730 size_t sig_len = max_sig_len;
Adam Langley4139edb2016-01-13 15:00:54 -08001731 enum ssl_private_key_result_t sign_result;
Steven Valdeze7531f02016-12-14 13:29:57 -05001732 if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001733 /* The SSL3 construction for CertificateVerify does not decompose into a
1734 * single final digest and signature, and must be special-cased. */
1735 if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
1736 if (ssl->cert->key_method != NULL) {
1737 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
Adam Langley4139edb2016-01-13 15:00:54 -08001738 goto err;
1739 }
Adam Langley4139edb2016-01-13 15:00:54 -08001740
David Benjaminc895d6b2016-08-11 13:26:41 -04001741 uint8_t digest[EVP_MAX_MD_SIZE];
1742 size_t digest_len;
Robert Sloan5d625782017-02-13 09:55:39 -08001743 if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(
1744 &hs->transcript, digest, &digest_len, ssl->s3->new_session,
1745 signature_algorithm)) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001746 goto err;
1747 }
1748
1749 sign_result = ssl_private_key_success;
1750
1751 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
1752 if (pctx == NULL ||
1753 !EVP_PKEY_sign_init(pctx) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001754 !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
1755 EVP_PKEY_CTX_free(pctx);
1756 sign_result = ssl_private_key_failure;
1757 goto err;
1758 }
1759 EVP_PKEY_CTX_free(pctx);
1760 } else {
1761 sign_result = ssl_private_key_sign(
1762 ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
Robert Sloan5d625782017-02-13 09:55:39 -08001763 (const uint8_t *)hs->transcript.buffer->data,
1764 hs->transcript.buffer->length);
Adam Langley4139edb2016-01-13 15:00:54 -08001765 }
1766
1767 /* The handshake buffer is no longer necessary. */
Robert Sloan5d625782017-02-13 09:55:39 -08001768 SSL_TRANSCRIPT_free_buffer(&hs->transcript);
Adam Langley4139edb2016-01-13 15:00:54 -08001769 } else {
Steven Valdeze7531f02016-12-14 13:29:57 -05001770 assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
David Benjaminc895d6b2016-08-11 13:26:41 -04001771 sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
Adam Langley4139edb2016-01-13 15:00:54 -08001772 }
1773
1774 switch (sign_result) {
1775 case ssl_private_key_success:
Adam Langley4139edb2016-01-13 15:00:54 -08001776 break;
1777 case ssl_private_key_failure:
Adam Langley4139edb2016-01-13 15:00:54 -08001778 goto err;
1779 case ssl_private_key_retry:
1780 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
Steven Valdeze7531f02016-12-14 13:29:57 -05001781 hs->state = SSL3_ST_CW_CERT_VRFY_B;
Adam Langley4139edb2016-01-13 15:00:54 -08001782 goto err;
1783 }
1784
Adam Langley4139edb2016-01-13 15:00:54 -08001785 if (!CBB_did_write(&child, sig_len) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001786 !ssl_add_message_cbb(ssl, &cbb)) {
Adam Langley4139edb2016-01-13 15:00:54 -08001787 goto err;
1788 }
1789
Robert Sloan4d1ac502017-02-06 08:36:14 -08001790 return 1;
Adam Langley4139edb2016-01-13 15:00:54 -08001791
1792err:
1793 CBB_cleanup(&cbb);
1794 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001795}
1796
David Benjamin1b249672016-12-06 18:25:50 -05001797static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
1798 SSL *const ssl = hs->ssl;
Kenny Roote99801b2015-11-06 15:31:15 -08001799 static const uint8_t kZero[32] = {0};
David Benjamin4969cc92016-04-22 15:02:23 -04001800 size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
Kenny Root03bcf612015-11-05 20:20:27 +00001801
David Benjaminc895d6b2016-08-11 13:26:41 -04001802 CBB cbb, body, child;
1803 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
1804 !CBB_add_u8_length_prefixed(&body, &child) ||
David Benjamin4969cc92016-04-22 15:02:23 -04001805 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
1806 ssl->s3->next_proto_negotiated_len) ||
David Benjaminc895d6b2016-08-11 13:26:41 -04001807 !CBB_add_u8_length_prefixed(&body, &child) ||
Kenny Roote99801b2015-11-06 15:31:15 -08001808 !CBB_add_bytes(&child, kZero, padding_len) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001809 !ssl_add_message_cbb(ssl, &cbb)) {
Kenny Roote99801b2015-11-06 15:31:15 -08001810 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1811 CBB_cleanup(&cbb);
Adam Langleyfdeb4882015-10-30 13:15:30 -07001812 return -1;
1813 }
Adam Langleyfdeb4882015-10-30 13:15:30 -07001814
Robert Sloan4d1ac502017-02-06 08:36:14 -08001815 return 1;
Kenny Roote99801b2015-11-06 15:31:15 -08001816}
1817
David Benjamin1b249672016-12-06 18:25:50 -05001818static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
1819 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -05001820 if (!ssl_do_channel_id_callback(ssl)) {
1821 return -1;
Kenny Roote99801b2015-11-06 15:31:15 -08001822 }
1823
1824 if (ssl->tlsext_channel_id_private == NULL) {
1825 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
1826 return -1;
1827 }
Kenny Roote99801b2015-11-06 15:31:15 -08001828
Steven Valdez909b19f2016-11-21 15:35:44 -05001829 CBB cbb, body;
1830 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001831 !tls1_write_channel_id(hs, &body) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -08001832 !ssl_add_message_cbb(ssl, &cbb)) {
Kenny Rootb8494592015-09-25 02:29:14 +00001833 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -05001834 CBB_cleanup(&cbb);
Kenny Rootb8494592015-09-25 02:29:14 +00001835 return -1;
Kenny Roota04d78d2015-09-25 00:26:37 +00001836 }
Kenny Rootb8494592015-09-25 02:29:14 +00001837
Robert Sloan4d1ac502017-02-06 08:36:14 -08001838 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001839}
1840
David Benjamin1b249672016-12-06 18:25:50 -05001841static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
1842 SSL *const ssl = hs->ssl;
Robert Sloan4d1ac502017-02-06 08:36:14 -08001843 int ret = ssl->method->ssl_get_message(ssl);
David Benjaminc895d6b2016-08-11 13:26:41 -04001844 if (ret <= 0) {
1845 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001846 }
David Benjamin4969cc92016-04-22 15:02:23 -04001847
Robert Sloan4d1ac502017-02-06 08:36:14 -08001848 if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
Robert Sloan5d625782017-02-13 09:55:39 -08001849 !ssl_hash_current_message(hs)) {
Robert Sloan4d1ac502017-02-06 08:36:14 -08001850 return -1;
1851 }
1852
David Benjamin6e899c72016-06-09 18:02:18 -04001853 CBS new_session_ticket, ticket;
David Benjaminc895d6b2016-08-11 13:26:41 -04001854 uint32_t tlsext_tick_lifetime_hint;
1855 CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
1856 if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
David Benjamin6e899c72016-06-09 18:02:18 -04001857 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1858 CBS_len(&new_session_ticket) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -04001859 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjamin6e899c72016-06-09 18:02:18 -04001860 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -04001861 return -1;
David Benjamin4969cc92016-04-22 15:02:23 -04001862 }
1863
David Benjamin6e899c72016-06-09 18:02:18 -04001864 if (CBS_len(&ticket) == 0) {
1865 /* RFC 5077 allows a server to change its mind and send no ticket after
Steven Valdez909b19f2016-11-21 15:35:44 -05001866 * negotiating the extension. The value of |ticket_expected| is checked in
1867 * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
David Benjamin1b249672016-12-06 18:25:50 -05001868 hs->ticket_expected = 0;
David Benjamin6e899c72016-06-09 18:02:18 -04001869 return 1;
1870 }
1871
David Benjaminc895d6b2016-08-11 13:26:41 -04001872 int session_renewed = ssl->session != NULL;
1873 SSL_SESSION *session = ssl->s3->new_session;
1874 if (session_renewed) {
David Benjamin6e899c72016-06-09 18:02:18 -04001875 /* The server is sending a new ticket for an existing session. Sessions are
1876 * immutable once established, so duplicate all but the ticket of the
1877 * existing session. */
David Benjaminf0c4a6c2016-08-11 13:26:41 -04001878 session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
David Benjaminc895d6b2016-08-11 13:26:41 -04001879 if (session == NULL) {
David Benjamin6e899c72016-06-09 18:02:18 -04001880 /* This should never happen. */
1881 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1882 goto err;
1883 }
David Benjamin6e899c72016-06-09 18:02:18 -04001884 }
1885
Steven Valdez909b19f2016-11-21 15:35:44 -05001886 /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
Robert Sloan4d1ac502017-02-06 08:36:14 -08001887 ssl_session_rebase_time(ssl, session);
Steven Valdez909b19f2016-11-21 15:35:44 -05001888
David Benjaminc895d6b2016-08-11 13:26:41 -04001889 if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001890 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1891 goto err;
1892 }
David Benjaminc895d6b2016-08-11 13:26:41 -04001893 session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
David Benjamin6e899c72016-06-09 18:02:18 -04001894
1895 /* Generate a session ID for this session based on the session ticket. We use
1896 * the session ID mechanism for detecting ticket resumption. This also fits in
1897 * with assumptions elsewhere in OpenSSL.*/
David Benjaminc895d6b2016-08-11 13:26:41 -04001898 if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
1899 session->session_id, &session->session_id_length,
1900 EVP_sha256(), NULL)) {
David Benjamin6e899c72016-06-09 18:02:18 -04001901 goto err;
1902 }
1903
David Benjaminc895d6b2016-08-11 13:26:41 -04001904 if (session_renewed) {
1905 session->not_resumable = 0;
1906 SSL_SESSION_free(ssl->session);
1907 ssl->session = session;
1908 }
1909
David Benjamin4969cc92016-04-22 15:02:23 -04001910 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +00001911
David Benjamin6e899c72016-06-09 18:02:18 -04001912err:
David Benjaminc895d6b2016-08-11 13:26:41 -04001913 if (session_renewed) {
1914 SSL_SESSION_free(session);
1915 }
David Benjamin6e899c72016-06-09 18:02:18 -04001916 return -1;
Kenny Rootb8494592015-09-25 02:29:14 +00001917}